Using a vector to index a multidimensional Fortran array

前端 未结 1 604
再見小時候
再見小時候 2020-12-17 18:23

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 prec         


        
相关标签:
1条回答
  • 2020-12-17 18:26

    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.

    0 讨论(0)
提交回复
热议问题