使用向量来索引多维Fortran数组 [英] Using a vector to index a multidimensional Fortran array

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

问题描述

现代Fortran中可能使用向量来索引多维数组吗?也就是说,例如,

  integer,dimension(3):: index = [4,6,9] 
double precision,dimension(10,10,10):: data

有没有更好的(1),索引(2),索引(3))来访问数据(4,6,9) ))?不需要硬编码数据数组的排名。



(天真地我会喜欢写 data(index),但当然这实际上意味着不同的东西 - 子集收集 - 需要 data 来本身就是一个排名第一的数组。)



为什么它值得这个问题与多维索引按JavaScript中的索引数组,而是在Fortran中。不幸的是,这些聪明的答案将不会与预定义的数组排名。解决方案

没有。所有我能想到的解决方法都是可怕的黑客攻击,你最好写一个函数来获取 data index 作为参数,并吐出所需的元素。



但是,您可以使用现代Fortran的阵列等级重新映射功能来完成相反的操作,这可能会满足你希望玩数组排名的快速和松散。



鉴于声明

  double precision,dimension(1000),target :: data 

你可以定义一个rank-3指针

pre $ code> double precision,pointer :: index_3d(:,:,:)

然后像这样设置:

  index_3d(1:10,1:10,1:10)=>数据



&hey presto,现在可以使用rank-3和rank-1指数将 data ,它接近你想要做的事情。我还没有在愤怒中使用这个,但是一些简单的测试没有发现任何严重的问题。


Is it possible in modern Fortran to use a vector to index a multidimensional array? That is, given, say,

integer, dimension(3) :: index = [4,6,9]
double precision, dimension(10,10,10) :: data

is there a better (more general) way to access data(4,6,9) than writing data(index(1), index(2), index(3))? It would be good not to have to hard-code the rank of the data array.

(Naively I would like to write data(index) but of course this actually means something different - subset "gathering" - requiring data to be a rank-one array itself.)

For what it's worth this is essentially the same question as multidimensional index by array of indices in JavaScript, but in Fortran instead. Unfortunately the clever answers there won't work with predefined array ranks.

解决方案

No. And all the workarounds I can think of are ghastly hacks, you're better off writing a function to take data and index as arguments and spit out the element(s) you want.

You might, however, be able to use modern Fortran's capabilities for array rank remapping to do exactly the opposite, which might satisfy your wish to play fast-and-loose with array ranks.

Given the declaration

double precision, dimension(1000), target :: data

you can define a rank-3 pointer

double precision, pointer :: index_3d(:,:,:)

and then set it like this:

index_3d(1:10,1:10,1:10) => data

and hey presto, you can now use both rank-3 and rank-1 indices into data, which is close to what you want to do. I've not used this in anger yet, but a couple of simple tests haven't revealed any serious problems.

这篇关于使用向量来索引多维Fortran数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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