set_order#

pybear.base.set_order(X, *, order='C', copy_X=True)#

Set the memory layout of X. X must be a numpy ndarray.

‘C’ is row-major order, ‘F’ is column major order.

For 1D and trivial 2D (shape=(10, 1) or (1, 10)) numpy arrays, the ‘flags’ attribute will report both ‘C_CONTIGUOUS’ and ‘F_CONTIGUOUS’ as True. This is because these arrays are a single continuous block of memory with no dimensions to reorder. Both ‘C_CONTIGUOUS’ and ‘F_CONTIGUOUS’ are True because there is no ambiguity in accessing elements — the memory layout trivially satisfies both definitions.

Parameters:
Xnumpy.ndarray

The numpy array for which to set the memory layout.

orderLiteral[‘c’, ‘C’, ‘f’, ‘F’]

The memory layout for X. ‘C’ is row-major order, ‘F’ is column-major order.

copy_Xbool

Whether to make a copy of X when setting the memory layout or operate directly on the passed X.

Returns:
Xnumpy.ndarray

X in the desired memory layout.

Examples

>>> from pybear.base import set_order
>>> import numpy as np
>>> X = np.array([[1, 2], [3, 4], [5, 6]], dtype=np.int8)
>>> print(X.flags['C_CONTIGUOUS'])
True
>>> print(X.flags['F_CONTIGUOUS'])
False
>>> out = set_order(X, order='F', copy_X=True)
>>> print(out.flags['C_CONTIGUOUS'])
False
>>> print(out.flags['F_CONTIGUOUS'])
True