I’d like to tranpose arrays to get some data from C-style to Fortran-style ordering. For a rank-2 array I can use the intrinsic
transpose. But how best to do it for higher ranks? The intrinsic
reshape offers the
order optional argument, so I tried, for example for rank-4:
x = reshape(x, shape=reverse(shape(x)), order=[4, 3, 2, 1])
reverse is a function that reverses the order of elements in a rank-1 array.
However, the value of
shape must be a constant expression, and I don’t know the shape of
x at compile time, so I can’t use this approach.
Currently, I have:
pure function reverse_dim_order(x) result(res) real, intent(in) :: x(:,:,:,:) real, allocatable :: res(:,:,:,:) integer :: dims(4) integer :: i, j, k, l dims = shape(x) allocate(res(dims(4), dims(3), dims(2), dims(1))) do concurrent(i = 1:dims(1), j = 1:dims(2), k = 1:dims(3), l = 1:dims(4)) res(l,k,j,i) = x(i,j,k,l) end do end function reverse_dim_order
which works, but I’m wondering is there a more intrinsic approach to this that I could take? Performance is not critical, so I’d like to take an approach that’s as concise, intrinsic, and Fortrannic as possible.