在Fortran中使用序列初始化数组 [英] Initilalising an array with a sequence in Fortran

查看:537
本文介绍了在Fortran中使用序列初始化数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在翻译一些旧的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

这种语法在fortran中可以接受吗? (必须是因为它来自旧代码) 我误会了线路的作用吗?

解决方案

语法不正确,除非实现某些非标准扩展,否则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

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

要以标准方式生成序列,可以使用隐式的do循环,例如

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

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

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?

解决方案

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.

这篇关于在Fortran中使用序列初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆