numpy.ascontiguousarray — NumPy v2.6.dev0 Manual

numpy.ascontiguousarray(a, dtype=None, *, like=None)

#

Return a contiguous array (ndim >= 1) in memory (C order).

Parameters:aarray_likeInput array.

dtypestr or dtype object, optionalData-type of returned array.

likearray_like, optionalReference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

Added in version 1.20.0.

Returns:outndarrayContiguous array of same shape and content as a, with type

dtype

if specified.

See also

asfortranarray

Convert input to an ndarray with column-major memory order.

require

Return an ndarray that satisfies requirements.

ndarray.flags

Information about the memory layout of the array.

Examples

Starting with a Fortran-contiguous array:

>>> importnumpyasnp>>> x=np.ones((2,3),order='F')>>> x.flags['F_CONTIGUOUS']TrueCalling ascontiguousarray makes a C-contiguous copy:

>>> y=np.ascontiguousarray(x)>>> y.flags['C_CONTIGUOUS']True>>> np.may_share_memory(x,y)FalseNow, starting with a C-contiguous array:

>>> x=np.ones((2,3),order='C')>>> x.flags['C_CONTIGUOUS']TrueThen, calling ascontiguousarray returns the same object:

>>> y=np.ascontiguousarray(x)>>> xisyTrueNote: This function returns an array with at least one-dimension (1-d) so it will not preserve 0-d arrays.