Fortran中的阵列数组 [英] array of arrays in fortran

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

问题描述

我试图定义一个数组数组。
我已经定义:

  integer,dimension(2,2)::& 
x = reshape(source =(/ 0,1,1,0 /),shape =(/ 2,2 /)),&
y = reshape(source =(/ 1,0,0,1 /),shape =(/ 2,2 /)),&
z = reshape(source =(/ 1,1,1,1 /),shape =(/ 2,2 /))

我想定义一个数组,例如s(3),其中(x / y / z)是分量,即

  s(1)= x 
s(2)= y
和s(3)= z

我该如何做到这一点?

解决方案

最简单的方法可能是将 s 3数组,也许

 整型,维(3,2,2):: s 

,然后您可以编写语句,如

  s(1,:,:) = x 
s(2,:,:) = y
...

这是在Fortran中实现一组数组的自然方法。另一种可能更吸引你的选择是:

  type :: twodarray 
integer,dimension( 2,2):: elements
end type twodarray

type(twodarray),dimension(3):: s

s(1)%elements = x

如果您不喜欢 s(1)%元素= x 你可以为你的类型 twodarray 重新定义操作 = ,我没有现在可以为您编写代码。


I am trying to define a array of arrays. I have defined:

  integer,dimension(2,2):: & 
    x=reshape(source= (/0,1,1,0/),  shape=(/2,2/)), & 
    y=reshape(source= (/1,0,0,1/),  shape=(/2,2/)), & 
    z=reshape(source= (/1,1,1,1/),  shape=(/2,2/)) 

I want to define an array, say, s(3), of which, (x/y/z) are components, i.e.

s(1)=x 
s(2)=y 
and s(3)=z

how can I achieve that?

解决方案

The simplest approach might be to define s as a rank-3 array, perhaps

integer, dimension(3,2,2) :: s

and then you can write statements such as

s(1,:,:) = x
s(2,:,:) = y
...

This is the 'natural' way to implement an array of arrays in Fortran. An alternative, which might appeal to you more would be something like:

type :: twodarray
   integer, dimension(2,2) :: elements
end type twodarray

type(twodarray), dimension(3) :: s

s(1)%elements = x

If you don't like the wordiness of s(1)%elements = x you could redefine the operation = for your type twodarray, I don't have time right now to write that code for you.

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

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