用Fortran中的序列初始化数组

时间:2021-03-10 21:23:07

I am currently working on translating some legacy fortran code and I am having a hard time understanding a particular line in the code. The compiler also seems to find this line weird and throws out an error. From what I understand it is trying to initialize an array by sequencing 1 to 9 by increments of 1 and filling up the array matrix with this sequence in column major form.

我目前正在翻译一些遗留的fortran代码,我很难理解代码中的某一行。编译器似乎也发现这一行很奇怪,并抛出一个错误。根据我的理解,它试图初始化一个数组,按1到9的顺序排列,以1的增量填充数组矩阵,用这个序列作为列的主要形式。

program arrayProg

  integer :: matrix(3,3), i , j !two dimensional real array

  matrix = reshape((/1:9:1/), (/3,3/))

end program arrayProg

Is this syntax acceptable in fortran? (It has to be because it comes from the legacy code) Am I misunderstanding what the line does?

这种语法在fortran中可以接受吗?(一定是因为它来自遗留代码)我是否误解了这一行的功能?

1 个解决方案

#1


4  

The syntax is incorrect and such code cannot be compiled by a Fortran compiler, unless it implements some non-standard extension.

语法不正确,这样的代码不能被Fortran编译器编译,除非它实现了一些非标准的扩展。

Intel Fortran accepts this:

英特尔Fortran接受:

 A colon-separated triplet (instead of an implied-DO loop) to specify a range of values and a stride; for example, the following two array constructors are equivalent:
1       INTEGER D(3)
2       D = (/1:5:2/)              ! Triplet form - also [1:5:2]
3       D = (/(I, I=1, 5, 2)/)     ! implied-DO loop form

from https://software.intel.com/en-us/node/678554

从https://software.intel.com/en-us/node/678554

To generate a sequence in a standard way one uses an implied do loop like

要以一种标准的方式生成序列,可以使用一个隐含的do循环

 (/ (i, i=1,9) /)

the reshape than just changes the 1D array into a 2D one in column major order as you guessed.

这个重塑不仅仅是将1D数组按照列的主要顺序更改为2D数组。

#1


4  

The syntax is incorrect and such code cannot be compiled by a Fortran compiler, unless it implements some non-standard extension.

语法不正确,这样的代码不能被Fortran编译器编译,除非它实现了一些非标准的扩展。

Intel Fortran accepts this:

英特尔Fortran接受:

 A colon-separated triplet (instead of an implied-DO loop) to specify a range of values and a stride; for example, the following two array constructors are equivalent:
1       INTEGER D(3)
2       D = (/1:5:2/)              ! Triplet form - also [1:5:2]
3       D = (/(I, I=1, 5, 2)/)     ! implied-DO loop form

from https://software.intel.com/en-us/node/678554

从https://software.intel.com/en-us/node/678554

To generate a sequence in a standard way one uses an implied do loop like

要以一种标准的方式生成序列,可以使用一个隐含的do循环

 (/ (i, i=1,9) /)

the reshape than just changes the 1D array into a 2D one in column major order as you guessed.

这个重塑不仅仅是将1D数组按照列的主要顺序更改为2D数组。