3

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

Ales
  • 495
  • 3
  • 11
  • Related: https://stackoverflow.com/questions/38760864/how-do-you-transpose-a-dask-dataframe-convert-columns-to-rows-to-approach-tidy – Daniel F Aug 03 '17 at 10:15

1 Answers1

2

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
jeffhale
  • 3,759
  • 7
  • 40
  • 56
shoyer
  • 9,165
  • 1
  • 37
  • 55