Fortran中rank1数组的赋值
每当我编译以下 fortran 代码时:
program test
implicit none
integer :: temp(1),i
integer :: z(1:10) = [(i,i=1,10)]
temp(1) = 10
z(2) = temp
end program test
我收到错误:错误#6366:数组表达式的形状不符合 如果我更改行:
z(2) = temp
到
z(2) = temp(1)
它编译并运行良好。为什么不能将单个元素数组分配给另一个数组的元素而不必显式列出该元素。我问这个问题是因为一些内部函数(如 minloc 和 pack)返回排名 1 的值。例如: z(i) = minloc(z) 会产生相同的错误。
Whenever I compile the following fortran code:
program test
implicit none
integer :: temp(1),i
integer :: z(1:10) = [(i,i=1,10)]
temp(1) = 10
z(2) = temp
end program test
I get the error:error #6366: The shapes of the array expressions do not conform
If I change the line:
z(2) = temp
to
z(2) = temp(1)
It compiles and runs fine. Why can't you assign a single element array to an element of another array without having to explicitly list the element. I ask this because some intrinsic functions like minloc and pack return rank 1 values. So for example:
z(i) = minloc(z) produces the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Fortran 标准,数组的等级在分配时应该是兼容的。您可以在该文档中找到兼容性的定义。例如,在 Fortran 2003 标准中,7.4.1.2 内在赋值语句部分
,2.4.5 Array 节给了我们定义:
现在看一下您的代码。 z(2) 是标量。它的等级是 0。它的形状是零大小的数组。 temp 是秩为 1、形状为 [1] 的数组。形状不同。这两个实体并不一致。
但如果你愿意的话你可以制作它们。您可以使用数组部分:
现在两个实体的形状都是 [1]。
According to Fortran standard the arrays ranks should be compatible upon the assignment. You can find the definition of compatibility in that document. For example, in Fortran 2003 Standard, section 7.4.1.2 Intrinsic assignment statement
and section 2.4.5 Array gives us the definitions:
Now take a look at your code. z(2) is scalar. It's rank is 0. It's shape is zero-sized array. temp is array with rank 1 and shape [1]. Shapes are different. This two entities are not conformable.
But you can make them if you want. You can use array sections:
Now shape of both entities is [1].