在 Fortran 90 中,是否必须事先声明数组维度?

发布于 2024-11-17 08:08:40 字数 1737 浏览 4 评论 0原文

是否有必要在任何其他代码之前声明数组维度?例如,我编写了以下简化的示例代码:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  REAL, DIMENSION(k, k**2) :: c

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

我想要说明的一点是,我想创建一个数组 c ,它是 k-by- k**2 的大小,并且 k 仅由代码中的其他计算确定; k 一开始是未知的。

但是,上面的代码在编译时给了我以下错误消息:

mytest.f90:13.31:

  REAL, DIMENSION(k, k**2) :: c
                               1
Error: Unexpected data declaration statement at (1)

代码中的第 13 行是我最终声明 c 的行:REAL, DIMENSION(k, k**2 )::c.

另一方面,如果我声明 k 并预先指定其尺寸,

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(3,9) :: c

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

我会得到正确的输出:

 k=           3
 k**2=           9

   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0

但是,由于我事先不知道 k,所以我可以'在我的实际代码中并没有这样做。有没有某种方法可以首先“声明”数组 c,而不指定其维度,然后在知道 k 的值后指定维度?

Is it necessary to declare array dimensions before any other code? For example, I have written the following simplified example code:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  REAL, DIMENSION(k, k**2) :: c

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

The point that I am trying to make is that I would like to create an array c that is k-by-k**2 in size, and k is only determined by other computations within the code; k is not known at the very beginning.

But, the above code gives me the following error message at compile time:

mytest.f90:13.31:

  REAL, DIMENSION(k, k**2) :: c
                               1
Error: Unexpected data declaration statement at (1)

where line 13 in my code is the line where I finally declare c: REAL, DIMENSION(k, k**2) :: c.

On the other hand, if I instead declare k and specify its dimensions upfront,

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(3,9) :: c

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)
  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO
END PROGRAM mytest

I get the correct output:

 k=           3
 k**2=           9

   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0

But, since I don't know k beforehand, I can't do exactly this in my actual code. Is there some way to "declare" the array c initially, without specifying its dimensions, and then later specify the dimensions once the value of k is known?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-11-24 08:08:40

您想使用可分配数组:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(:,:), allocatable :: c   !<-  c is allocatable, rank 2

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)

  allocate(c(k,k**2))                  ! <-- allocate array c with supplied shape

  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO

  deallocate(c)                        ! <-- deallocate when done
END PROGRAM mytest

You want to use allocatable arrays:

PROGRAM mytest
  IMPLICIT NONE
  INTEGER :: i, j, k, mysum
  REAL, DIMENSION(:,:), allocatable :: c   !<-  c is allocatable, rank 2

  ! Let array c be a k-by-k**2 array
  ! Determine k within the program by some means...for example,
  mysum=0
  DO i=1, 3
    mysum=mysum+1
  END DO
  k=mysum

  WRITE(*,*) "k=", k
  WRITE(*,*) "k**2=", k**2
  WRITE(*,*)

  allocate(c(k,k**2))                  ! <-- allocate array c with supplied shape

  DO i=1,size(c,1)
    WRITE(*,"(100(3X,F3.1))") (c(i,j), j=1,size(c,2))
  END DO

  deallocate(c)                        ! <-- deallocate when done
END PROGRAM mytest
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文