Initilalising an array with a sequence in Fortran

前提是你 提交于 2019-12-20 02:35:09

问题


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.

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?


回答1:


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

Intel Fortran accepts this:

 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

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

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

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



来源:https://stackoverflow.com/questions/46698017/initilalising-an-array-with-a-sequence-in-fortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!