Search code examples
pythonarraysnumpyreshapedask

Reshaping a dask.array in Fortran-contiguous order


I would like to ask if there is a way how to reshape a dask array in Fortran-contiguous (column-major) order since the parallelized version of the np.reshape function is not supported yet (see here).


Solution

  • Fortran-contiguous (column-major) order is simply C-contiguous (row-major) order in reverse. So there's a simple work around for the fact that dask array doesn't support order='F':

    • Transpose your array to reverse its dimensions.
    • Reshape it to the reverse of your desired shape.
    • Transpose it back.

    In a function:

    def reshape_fortran(x, shape):
        return x.T.reshape(shape[::-1]).T
    

    Transposing with NumPy/dask is basically free (it doesn't copy any data), so in principle this operation should also be quite efficient.

    Here's a simple test to verify it does the right thing:

    In [48]: import numpy as np
    
    In [49]: import dask.array as da
    
    In [50]: x = np.arange(100).reshape(10, 10)
    
    In [51]: y = da.from_array(x, chunks=5)
    
    In [52]: shape = (2, 5, 10)
    
    In [53]: np.array_equal(reshape_fortran(y, shape).compute(),
        ...:                x.reshape(shape, order='F'))
        ...:
    Out[53]: True