How can I find out if a n-dimensional numpy array Arr
is contiguous in C-style or Fortran-style?
ascontiguousarray() function is used to return a contiguous array where the dimension of the array is greater or equal to 1 and stored in memory (C order). Note: A contiguous array is stored in an unbroken block of memory. To access the subsequent value in the array, we move to the next memory address.
A contiguous array is just an array stored in an unbroken block of memory: to access the next value in the array, we just move to the next memory address. This means arr is a C contiguous array because the rows are stored as contiguous blocks of memory. The next memory address holds the next row value on that row.
Python has a built-in module named 'array' which is similar to arrays in C or C++. In this container, the data is stored in a contiguous block of memory.
A NumPy array is basically described by metadata (notably the number of dimensions, the shape, and the data type) and the actual data. The data is stored in a homogeneous and contiguous block of memory, at a particular address in system memory (Random Access Memory, or RAM).
You can also try the ndarray.data.contiguous
member. E.g. (on my machine):
arr = np.arange(6).reshape(2, 3)
print(arr.data.contiguous) # True
print(arr.data.c_contiguous) # True
print(arr.data.f_contiguous) # False
(I can't find any information re: which numpy versions support this, even on their docs. Any leads welcome in the comments!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With