Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any Fortran function to transpose 3D array?

I need to transpose a 3D matrix in Fortran. I have a 3d array: V(111,222,333); I need to transpose it to V(333,111,222). Is there any function to do that in Fortran?

like image 778
komolkanto Avatar asked Feb 20 '26 03:02

komolkanto


2 Answers

I hesitate to disagree with @IanBush and perhaps what follows is neither easy nor clear. The following statement will return a permutation of the array V. If I have got it right then the element at V(i,j,k) is sent to V_prime(k,i,j).

V_prime = RESHAPE(source=V, shape=[size(V,3),size(V,1),size(V,2)], order=[2,3,1])

Whether this creates the permutation OP asks for is a bit unclear, I'm not aware that there is a single definition of the transpose of an array of rank other than 2. Changing the order will produce different permutations.

This question is probably a duplicate of Fortran reshape - N-dimensional transpose. It is certainly worth reading the answers to that question which explain the use of reshape with order very well.

like image 71
High Performance Mark Avatar answered Feb 27 '26 10:02

High Performance Mark


There is no routine that will do this simply. I would write some loops, that is often the easiest and clearest way.

like image 20
Ian Bush Avatar answered Feb 27 '26 08:02

Ian Bush