Fortran:任意维度的数组? [英] Fortran: Array of arbitrary dimension?

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

问题描述

如果我想创建可分配的多维数组,我可以说:

If I want to create an allocatable multidimensional array, I can say:

program test
    real, dimension(:,:), allocatable :: x
    integer :: i,j

    allocate(x(5, 5))

    do i = 1,size(x,1)
        do j = 1,size(x,2)
            x(i,j) = i*j
        end do
    end do

    write(*,*) x

end program test

但是,如果我不知道尺寸x将是多少,该怎么办.有办法解决这个问题吗?

However, what if I don't know how many dimension x will be. Is there a way to accommodate that?

推荐答案

较新的编译器允许使用假定等级的对象来实现互操作性. 我认为这就是您要寻找的.但这是对函数或子例程的调用.函数或子例程将伪参数声明为假定等级,并且在运行时将实际等级与实际参数一起传递.

Newer compilers allow the use of assumed-rank objects for interoperability. I think that is what you are looking for. But this is for call to functions or subroutines. The function or subroutine declares the dummy argument as assumed-rank and the actual rank is passed with the actual argument at runtime.

来自IBM网站的示例:

Example from IBM website:

REAL :: a0
REAL :: a1(10)
REAL :: a2(10, 20)
REAL, POINTER :: a3(:,:,:)

CALL sub1(a0)
CALL sub1(a1)
CALL sub1(a2)
CALL sub1(a3)

CONTAINS
    SUBROUTINE sub1(a)
        REAL :: a(..)
        PRINT *, RANK(a)
    END

END

关注那个以获取更多详细信息

follow this or that for more details

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

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