开发者

Assignment of rank1 array in fortran

开发者 https://www.devze.com 2023-02-18 02:11 出处:网络
Whenever I compile the following fortran code: program test implicit none integer:: temp(1),i integer:: z(1:10) = [(i,i=1,10)]

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 intrins开发者_如何学编程ic functions like minloc and pack return rank 1 values. So for example: z(i) = minloc(z) produces the same error.


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].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号