隐do循环数组初始化 [英] Implicit do loop array initialization

查看:234
本文介绍了隐do循环数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化在同一行的数组和一个隐含do循环。不过,我总是得到一个语法或形状误差。谁能帮我改正以下构建?

 整数数组myarray ::(maxdim,NR)myArray的(1:maxdim,NR)=(/(/ I,I = 1,maxdim /),NR /)


解决方案

正在初始化与 MAXDIM 行的阵列和 NR 列,它看起来像每一列都包含整数1 MAXDIM

作为第一步,继续前进,写出来的实际 DO -loop:

 做的J = 1,NR
    做I = 1,MAXDIM
        myArray的(I,J)= I
    做到底
做到底

收起内环到一个隐含的循环结构:

 做的J = 1,NR
    myArray的(1:MAXDIM,J)=(/(I,I = 1,MAXDIM)/)
做到底

当我们试图瓦解外循环,但是,奇怪的事情发生了:

  myArray的=(/((/(I,I = 1,MAXDIM)/),J = 1,NR)/)

现在,我得到一个不兼容的行列错误像你一样。因为我不是在隐含do循环很好要么,我看着形状的数组构造内在的结果:

 打印*,形状(myarray中)
打印*,形状((/((/(I,I = 1,MAXDIM)/),J = 1,NR)/))

这打印出

  10 10
  50

数组构造简单地扩大一维数组,压扁任何嵌套数组构造。事实上,我们可以删除第二组的(/ /)简化。既然一切都已经在正确的顺序,我们可以使用重塑内在,以确保正确的排名。我完整的测试程序则为:

 程序sotest
    隐无    整数参数:: MAXDIM = 5
    整数参数:: NR = 10    整数::我
    整数::Ĵ
    整数:: myArray的(MAXDIM,NR)
    整数:: myarray_implicit(MAXDIM,NR)    做J = 1,NR
        做I = 1,MAXDIM
            myArray的(I,J)= I
        做到底
    做到底    myarray_implicit =重塑((/((I,I = 1,MAXDIM),J = 1,NR)/),(/ MAXDIM,NR /))    打印*,所有(myArray的== myarray_implicit)
 程序结束sotest

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 /)

解决方案

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

这篇关于隐do循环数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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