简体   繁体   中英

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 ).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM