numpy.ascontiguousarray#
- numpy.ascontiguousarray(a,dtype=None,*,like=None)#
Return a contiguous array (ndim >= 1) in memory (C order).
- Parameters:
- aarray_like
Input array.
- dtypestr or dtype object, optional
Data-type of returned array.
- likearray_like, optional
Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in as
likesupportsthe__array_function__protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.New in version 1.20.0.
- Returns:
- outndarray
Contiguous array of same shape and content asa, with type
dtypeif specified.
See also
asfortranarrayConvert input to an ndarray with column-major memory order.
requireReturn an ndarray that satisfies requirements.
ndarray.flagsInformation 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']True
Calling
ascontiguousarraymakes a C-contiguous copy:>>>y=np.ascontiguousarray(x)>>>y.flags['C_CONTIGUOUS']True>>>np.may_share_memory(x,y)False
Now, starting with a C-contiguous array:
>>>x=np.ones((2,3),order='C')>>>x.flags['C_CONTIGUOUS']True
Then, calling
ascontiguousarrayreturns the same object:>>>y=np.ascontiguousarray(x)>>>xisyTrue
Note: This function returns an array with at least one-dimension (1-d)so it will not preserve 0-d arrays.