Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if numpy array is contiguous?

How can I find out if a n-dimensional numpy array Arr is contiguous in C-style or Fortran-style?

like image 926
Ethunxxx Avatar asked Jul 12 '18 11:07

Ethunxxx


People also ask

Is NumPy array contiguous?

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.

Is array contiguous or not?

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.

Are Python arrays contiguous?

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.

Does NumPy use non contiguous 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).


1 Answers

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

like image 141
CrepeGoat Avatar answered Sep 24 '22 03:09

CrepeGoat