BaR*_*Rud 3 arrays fortran fortran90
我正在尝试定义一个数组数组.我已经定义:
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/))
Run Code Online (Sandbox Code Playgroud)
我想定义一个数组,比如s(3),其中,(x/y/z)是组件,即
s(1)=x
s(2)=y
and s(3)=z
Run Code Online (Sandbox Code Playgroud)
我怎么能实现这一目标?
最简单的方法可能是定义s为rank-3数组
integer, dimension(3,2,2) :: s
Run Code Online (Sandbox Code Playgroud)
然后你可以编写如下的语句
s(1,:,:) = x
s(2,:,:) = y
...
Run Code Online (Sandbox Code Playgroud)
这是在Fortran中实现数组数组的"自然"方式.另一种可能对您有吸引力的替代方案是:
type :: twodarray
integer, dimension(2,2) :: elements
end type twodarray
type(twodarray), dimension(3) :: s
s(1)%elements = x
Run Code Online (Sandbox Code Playgroud)
如果你不喜欢你的单词,s(1)%elements = x你可以重新定义=你的类型的操作twodarray,我现在没有时间为你编写代码.
您始终可以使用指针(在 Fortran 95 中)
program main
implicit none
type :: my_type
integer, pointer :: my_size(:) ! F95
!integer, allocatable :: my_size(:) ! F95 + TR 15581 or F2003
end type my_type
type(my_type), allocatable :: x(:)
allocate(x(3))
allocate(x(1)%my_size(3))
allocate(x(2)%my_size(2))
allocate(x(3)%my_size(1))
print*, x(1)%my_size
print*, x(2)%my_size
print*, x(3)%my_size
deallocate(x(3)%my_size, x(2)%my_size, x(1)%my_size)
deallocate(x)
end program main
Run Code Online (Sandbox Code Playgroud)
它将打印
0 0 0
0 0
0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4669 次 |
| 最近记录: |