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

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

问题描述

我目前正在翻译一些遗留的 Fortran 代码,但我很难理解代码中的特定行.编译器似乎也发现这一行很奇怪并抛出一个错误.据我所知,它试图通过以 1 为增量对 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

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

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 编译器无法编译此类代码,除非它实现了一些非标准扩展.

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:

 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 循环,例如

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天全站免登陆