I would like the create an fortran-contiguous view of a 3-D C-contiguous array named a
. For a 2-D matrix, this is as simple as a.T
, but transpose is not defined for 3-D arrays. Happily the reshape
function takes an order
argument, which allows the output array to be given F-contiguous flag.
Unfortunately, naively reshaping the array seem to be very slow unless I ravel()
it first.
What is going on with the speed difference? Is something else happening?
Is there a better way to view the array in fortran order without making a copy (asfortranarray
makes a copy in order to preserve the shape).
Ultimately I'm trying to provide this array to a Fortran routine using f2py. Is there an easy way to suggest f2py should auto-transpose the array?
In [1]: a = np.empty([1000,2000,3000],dtype=np.float32)
In [2]: %timeit b = np.reshape(a.ravel(),[3000,2000,1000],order='F')
1.43 µs ± 5.42 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [3]: %timeit c = np.reshape(a,[3000,2000,1000],order='F')
24 s ± 46.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Both the b
and c
matrix seem to be equivalent views of a
Update: As @hpaulj points out, the c
version is slower because it makes a copy. Relevant documentation:
The order keyword gives the index ordering both for fetching the values from a, and then placing the values into the output array
Which is why the c
array is not the same ordering, and is a copy.
Additionally transpose (a.T
) is defined on n-D arrays, so that's the simplest solution.