具有一维变量的Fortran 95数组中的二维数组 [英] 2D array in Fortran 95 array with one dimension variable

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

问题描述

我想在我的代码中存储2维数组中的东西,并且稍后要扫描该数组。在数组中有 N_ {1} 行(第一个索引的数目,比如 i )。然而,对于给定的 i 值, j 值的数量并不是固定的,尽管我知道可能的最大值 j (说它是 N_ {2} )。



我当然可以创建大小为(N_ {1},N_ {2})的数组来存储我的数据。但是,这看起来浪费了空间,因为我的 N_ {2} 值波动很大,我的数组中的元素总数也非常大。是否可以创建一个二维数组,它可以根据 i 值具有不同数量的 j 值?另外,即使我可以通过一个Fortran命令创建多个一维数组并正确分配它们,对我来说也是可以的。

解决方案

当您建议回答另一个问题,看看粗糙的数组可能是你想要的,我会简单地提一下这个答案的可用性扩展(在Doug Lipinski的评论中暗示)。 >

对于基础类型,表示由高性能标记给出的可变长度维度

  type :: vector 
integer,dimension(:),allocatable :: elements
结束类型向量

以及这些数组的类型

  type :: ragged_array 
type向量),维(:),allocatable :: vectors
结束类型ragged_array

分配步骤

  type(ragged_array):: ragarr 

allocate(ragarr%vectors(5) )
分配(ragarr%向量(1)%元素(3))
!等等。

[或者,您可能会想要一个类型(vector)。]



对于可用性方面,可以创建一个结构构造函数,它执行大量的分配,甚至依靠自动分配对于可变长度的组件。

在后一种情况下,如果在创建时已知值(而不仅仅是范围),这是有意义的。

  allocate(ragarr%vectors(5))
ragarr%vectors(1)%elements = [1,6,13]
!等等。

对于前一种情况,像

  module ragged 

隐式none none

type :: vector
integer,dimension(:),allocatable ::元素
结束类型向量

类型:: ragged_array
类型(向量),维度(:),allocatable :: vectors
结束类型ragged_array

interface ragged_array
模块过程ragged_constructor
结束接口ragged_array

包含

函数ragged_constructor(sizes)result(ra)
整数,intent(in):: sizes(:)
type(ragged_array)ra
整数i

分配(ra%vectors(SIZE(sizes)))
(i)%元素(大小(i)))
结束做

结束函数ragged_constructor $ b $(i = 1,SIZE(大小)
分配b
最终模块不合格

程序测试

使用不合格
隐含无

类型(衣衫褴褛_array):: ra

ra = ragged_array([3,4,6,1,12])

结束程序


I want to store something in 2 dimensional array in my code and later want to scan that array. There are N_{1} rows (number of first indices, say i) in the array. However, for a given value of i, number of j values is not fixed though I know the maximum possible value of j (say that it is N_{2}).

I can of course create array of size (N_{1},N_{2}) to store my data. This, however seems wastage of space because my N_{2} values fluctuate a lot and the total number of elements in my array is also very large. Is is possible to create a 2D array that can have different number of j values depending on i value? Alternatively, even if I could create many-many 1D arrays by a single Fortran command and allocate them properly, that is also OK with me.

解决方案

As you suggest that the answer to another question, looking at ragged arrays may be what you want, I'll briefly mention a usability extension to that answer (hinted at in comment by Doug Lipinski).

For a base type, representing the variable-length dimension, given by High Performance Mark

type :: vector
    integer, dimension(:), allocatable :: elements
end type vector

and a type for an array of those

type :: ragged_array
    type(vector), dimension(:), allocatable :: vectors
end type ragged_array

one has the allocation steps

type(ragged_array) :: ragarr

allocate(ragarr%vectors(5))
allocate(ragarr%vectors(1)%elements(3))
! etc.

[Alternatively, one may be tempted to just have an array of type(vector).]

For the usability aspect one could create a structure constructor which does the numerous allocations, or even rely on automatic allocation for the variable length components.

In the latter case, which makes sense if the values, not just the extents, are known at creation.

allocate(ragarr%vectors(5))
ragarr%vectors(1)%elements = [1, 6, 13]
! etc.

For the former case, something like

module ragged

  implicit none

  type :: vector
      integer, dimension(:), allocatable :: elements
  end type vector

  type :: ragged_array
      type(vector), dimension(:), allocatable :: vectors
  end type ragged_array

  interface ragged_array
      module procedure ragged_constructor
  end interface ragged_array

contains

  function ragged_constructor(sizes) result(ra)
      integer, intent(in) :: sizes(:)
      type(ragged_array) ra
      integer i

      allocate(ra%vectors(SIZE(sizes)))
      do i=1,SIZE(sizes)
          allocate(ra%vectors(i)%elements(sizes(i)))
      end do

  end function ragged_constructor

end module ragged

program test

    use ragged
    implicit none

    type(ragged_array) :: ra

    ra = ragged_array([3,4,6,1,12])

end program

这篇关于具有一维变量的Fortran 95数组中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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