Consider this simple program

program main
implicit none

integer :: array(2,3) = transpose(reshape((/1,2,3,4,5,6/),(/ size(array, 2), size(array, 1) /)))
integer i,j
do i=1,2
   write(*,*) array(i,:)
end do
end program main

when I compiled with intel fortran compiler (version 13.0.0) I get error:

main.f90(4): error #6263: This intrinsic function is invalid in constant expressions. [TRANSPOSE]

integer :: array(2,3) = transpose(reshape((/1,2,3,4,5,6/),(/ size(array, 2), size(array, 1) /)))

It seems that transpose cannot be used on constant expressions(?). So is there a way to initialize the array upon definition? In my problem the array is quite large, so transpose by hand would not be an option.

有帮助吗?

解决方案

Use of TRANSPOSE in an initialization expression (or constant expression in F2008 terminology) is a Fortran 2003 feature not yet supported by that compiler.

Use of RESHAPE, with the appropriate ORDER argument, is part of Fortran 95, is supported by that compiler and can give the equivalent of what you want:

integer :: array(2,3) = reshape([1,2,3,4,5,6], shape(array), ORDER=[2,1])

其他提示

All it means is that you cannot define array there. If you do

integer :: array(2,3)
integer :: i,j
array = transpose....

It will compile.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top