Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a vector to index a multidimensional Fortran array

Tags:

arrays

fortran

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.

like image 982
Aant Avatar asked Aug 17 '12 11:08

Aant


People also ask

How do I index an array in Fortran?

Array indexing in Fortran is 1-based by default: integer,parameter :: N=8 real(4),dimension(N) :: x do i=1,N ... x(i) ... (Different from most other languages.)

How do I create a 2d array in Fortran?

Fortran stores higher dimensional arrays as a contiguous sequence of elements. It is important to know that 2-dimensional arrays are stored by column. So in the above example, array element (1,2) will follow element (3,1). Then follows the rest of the second column, thereafter the third column, and so on.

Is Fortran 0 indexed?

By default, Fortran indexes the first element of an array as 1. C and C++ index it as 0.

What is reshape Fortran?

reshape(source, shape, pad, order) It constructs an array with a specified shape shape starting from the elements in a given array source. If pad is not included then the size of source has to be at least product (shape). If pad is included, it has to have the same type as source.


1 Answers

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.

like image 123
High Performance Mark Avatar answered Nov 02 '22 14:11

High Performance Mark