在Fortran中使用序列初始化数组

Bab*_*ara 3 arrays fortran translation

我目前正在翻译一些遗留的fortran代码,并且很难理解代码中的特定行。编译器似乎也发现这行很奇怪,并抛出错误。据我了解,它试图通过以1为增量对1到9进行排序来初始化数组,并以列主形式将此序列填充到数组矩阵中。

program arrayProg

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

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

end program arrayProg
Run Code Online (Sandbox Code Playgroud)

这种语法在fortran中可以接受吗?(这一定是因为它来自旧代码)我误解了该行的含义吗?

Vla*_*r F 5

语法不正确,并且除非实现某些非标准扩展,否则Fortran编译器无法编译此类代码。

英特尔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
Run Code Online (Sandbox Code Playgroud)

来自https://software.intel.com/zh-cn/node/678554

为了以一种标准的方式生成一个序列,可以使用一个隐式的do循环,例如

 (/ (i, i=1,9) /)
Run Code Online (Sandbox Code Playgroud)

重塑不仅仅是像您猜中的那样将一维数组按列主顺序更改为二维一维数组。

  • 顺便说一句,每当有人建议在新代码中使用某些非标准扩展名,尤其是DEC扩展之类的丑陋东西时,请考虑这种情况。有人将继承该代码,将得到一些不同的编译器,并且将被搞砸或至少感到困惑,将PIA重写为标准格式将是PIA。您不能说“它是便携式的,因为我一直使用英特尔,这是胡说八道”。 (4认同)