Ethunxxx
Ethunxxx

Reputation: 1337

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?

Upvotes: 16

Views: 16142

Answers (2)

CrepeGoat
CrepeGoat

Reputation: 2515

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

Upvotes: 13

Ethunxxx
Ethunxxx

Reputation: 1337

The numpy documentation states that it is possible to check whether an array is C-contiguous or Fortran-contiguous via the attribute flags:

Arr.flags['C_CONTIGUOUS']
Arr.flags['F_CONTIGUOUS']

These attributes return a boolean indicating which of the two cases is true.

Upvotes: 25

Related Questions