import numpy as np
Numpy presents an n-dimensional abstraction that has to be fit into 1-dimensional computer memory.
Even for 2 dimensions (matrices), this leads to confusion: row-major, column-major.
A = np.arange(9).reshape(3, 3)
print(A)
How is this represented in memory?
A.strides
strides stores for each axis by how many bytes one needs to jump to get from one entry to the next (in that axis)We can also ask for Fortran order:
A2 = np.arange(9).reshape(3, 3, order="F")
A2
numpy defaults to row-major order.
A2.strides
How is the stride model more general than just saying "row major" or "column major"?
A = np.arange(16).reshape(4, 4)
A
A.strides
Asub = A[:3, :3]
Asub
Recall that Asub constitutes a view of the original data in A.
Asub.strides
Now Asub is no longer a contiguous array!
From the linear-memory representation (as show by the increasing numbers in A) 3, 7, 11 are missing.
This is easy to check by a flag:
Asub.flags