In NumPy, one can have arrays which are just pointing towards data that was allocated in a different array in order to avoid deep copies. For example:
import numpy as np
a = np.ones((3,4))
b = a[:2]
Now b
just contains a pointer to the array in a
, as evidenced by the flags:
b.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
This is all fine as long as one is sticking to NumPy's operations, but now suppose that I have a function that accepts a 2-d array and passes it to some C function, which will index it like this: (i,j) = j + i*ncol
(for row-major) or (i,j) = i + j*nrow
(for column-major).
If I now try this:
c = a[:2,:2]
Now the leading dimension of the array will not match with the number of columns, so it cannot be indexed like that anymore.
How can I get the leading dimension of an array?
Examples:
import numpy as np
a = np.ascontiguousarray(np.ones((3,4)))
b = np.asfortranarray(a)
c = a[:2,:2] ### LDA is 4
d = b[:2,:2] ### LDA is 3