Fortran中rank1数组的赋值

发布于 2024-10-26 03:18:57 字数 471 浏览 3 评论 0原文

每当我编译以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-11-02 03:18:57

为什么不能分配单个元素
数组到另一个数组的元素
无需明确列出
元素。

根据 Fortran 标准,数组的等级在分配时应该是兼容的。您可以在该文档中找到兼容性的定义。例如,在 Fortran 2003 标准中,7.4.1.2 内在赋值语句部分

(2) 任一变量应是与 expr 具有相同等级的可分配数组或形状
变量和expr应一致

2.4.5 Array 节给了我们定义:

数组最多可以有七个维度,并且可以在任何维度上具有任意范围(元素数量)。
数组的秩是维数;它的大小是元素的总数,即
等于范围的乘积。数组的大小可能为零。数组的形状已确定
通过其在每个维度中的等级及其范围,并且可以表示为一个等级一数组,其元素
是范围。
[...]
如果两个数组具有相同的形状,则它们是一致的。

现在看一下您的代码。 z(2) 是标量。它的等级是 0。它的形状是零大小的数组。 temp 是秩为 1、形状为 [1] 的数组。形状不同。这两个实体并不一致。

但如果你愿意的话你可以制作它们。您可以使用数组部分:

z(2:2)    = temp

现在两个实体的形状都是 [1]。

Why can't you assign a single element
array to an element of another array
without having to explicitly list the
element.

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

(2) Either variable shall be an allocatable array of the same rank as expr or the shapes of
variable and expr shall conform

and section 2.4.5 Array gives us the definitions:

An array may have up to seven dimensions, and any extent (number of elements) in any dimension.
The rank of the array is the number of dimensions; its size is the total number of elements, which is
equal to the product of the extents. An array may have zero size. The shape of an array is determined
by its rank and its extent in each dimension, and may be represented as a rank-one array whose elements
are the extents.
[...]
Two arrays are conformable if they have the same shape.

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:

z(2:2)    = temp

Now shape of both entities is [1].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文