隐式 do 循环数组初始化

发布于 2024-09-29 18:28:07 字数 166 浏览 3 评论 0原文

我想使用隐式 do 循环在一行上初始化一个数组。但是,我总是遇到语法或形状错误。谁能帮我纠正以下结构?

integer myarray :: (maxdim, nr)

myarray(1:maxdim,nr) = (/ (/i,i=1,maxdim/),nr /)

I want to initialize an array on one line with an implicit do loop. However, I always get a syntax or shape error. Can anyone help me correct the following construct?

integer myarray :: (maxdim, nr)

myarray(1:maxdim,nr) = (/ (/i,i=1,maxdim/),nr /)

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

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

发布评论

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

评论(2

挽心 2024-10-06 18:28:07

您正在初始化一个包含 MAXDIM 行和 NR 列的数组,并且每列看起来都包含整数 1 到 MAXDIM

第一步,继续写出实际的 DO 循环:

do j=1,NR
    do i=1,MAXDIM
        myarray(i,j) = i
    end do
end do

将内部循环折叠为隐式循环结构:

do j = 1,NR
    myarray(1:MAXDIM,j) = (/ (i, i=1,MAXDIM) /)
end do

但是,当我们尝试折叠外部循环时,会发生一些奇怪的事情:

myarray = (/ ((/ (i, i=1,MAXDIM) /), j=1,NR) /)

现在,我像你一样遇到了不兼容的排名错误。由于我也不太擅长隐式 do 循环,因此我查看了数组构造函数的 shape 内在

print *, shape(myarray)
print *, shape((/ ((/ (i, i=1,MAXDIM) /), j=1,NR) /))

结果

   5      10
  50

:任何嵌套数组结构。我们实际上可以删除第二组(/ /)来简化。由于一切都已经处于正确的顺序,我们可以使用reshape内在函数来确保正确的排名。我的完整测试程序是:

program sotest
    implicit none

    integer, parameter :: MAXDIM = 5
    integer, parameter :: NR     = 10

    integer :: i
    integer :: j
    integer :: myarray(MAXDIM, NR)
    integer :: myarray_implicit(MAXDIM, NR)

    do j = 1,NR
        do i = 1,MAXDIM
            myarray(i,j) = i
        end do
    end do 

    myarray_implicit = reshape((/ ((i,i=1,MAXDIM), j=1,NR) /), (/ MAXDIM, NR /))

    print *, all(myarray == myarray_implicit)
 end program sotest

You are initializing an array with MAXDIM rows and NR columns, and it looks like each column contains the integers 1 to MAXDIM.

As a first step, go ahead and write out the actual DO-loop:

do j=1,NR
    do i=1,MAXDIM
        myarray(i,j) = i
    end do
end do

Collapse the inner loop to an implicit loop structure:

do j = 1,NR
    myarray(1:MAXDIM,j) = (/ (i, i=1,MAXDIM) /)
end do

When we try to collapse the outer loop, though, something strange happens:

myarray = (/ ((/ (i, i=1,MAXDIM) /), j=1,NR) /)

Now, I get an incompatible ranks error as you did. Since I'm not very good at the implicit do-loops either, I looked at the shape intrinsic results for the array constructor:

print *, shape(myarray)
print *, shape((/ ((/ (i, i=1,MAXDIM) /), j=1,NR) /))

This prints out

   5      10
  50

The array constructor is simply expanding a 1-D array , flattening any nested array constructions. We can actually drop the second set of (/ /) to simplify. Since everything is already in the proper order, we can use the reshape intrinsic to ensure proper rank. My full test program is then:

program sotest
    implicit none

    integer, parameter :: MAXDIM = 5
    integer, parameter :: NR     = 10

    integer :: i
    integer :: j
    integer :: myarray(MAXDIM, NR)
    integer :: myarray_implicit(MAXDIM, NR)

    do j = 1,NR
        do i = 1,MAXDIM
            myarray(i,j) = i
        end do
    end do 

    myarray_implicit = reshape((/ ((i,i=1,MAXDIM), j=1,NR) /), (/ MAXDIM, NR /))

    print *, all(myarray == myarray_implicit)
 end program sotest
半世晨晓 2024-10-06 18:28:07

隐式 do 循环只会创建一个向量,因此您必须对其进行整形。像这样的东西:

integer, dimension(m,n) :: myarray
integer :: ix, jx
...
myarray = reshape( [ (ix, ix = 1, m*n) ], [ m, n ] )

或者也许您想要一个更复杂的、嵌套的、隐含的 do 循环:

myarray = reshape( [ ((ix+jx, ix = 1, m), jx = 1, n) ], [ m, n ] )

请注意,我使用 [ ] 的 Fortran2003 约定来分隔数组结构,而不是 (/ /)。另请注意,您必须声明隐含的 do 循环索引变量。

The implicit do loop will only create a vector so you'll have to reshape that. Something like this:

integer, dimension(m,n) :: myarray
integer :: ix, jx
...
myarray = reshape( [ (ix, ix = 1, m*n) ], [ m, n ] )

or perhaps you want a more complicated, nested, implied-do loop:

myarray = reshape( [ ((ix+jx, ix = 1, m), jx = 1, n) ], [ m, n ] )

Note that I'm using the Fortran2003 convention of [ ] to delimit array constructions, rather than (/ /). Note also that you have to declare the implied do loop index variables.

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