Consider the following to create a linear array of size 4:
import numpy as np
cimport numpy as np
cdef np.float64_t [:] a = np.zeros(shape=(4),dtype=np.float64)
a.shape
should be (4,). However:
print(a.shape)
>>> [4, 0, 0, 0, 0, 0, 0, 0]
What is going on? The original Python code gives the correct answer:
a = np.zeros(shape=(4),dtype=np.float64)
print(a.shape)
>>> (4,)
They're functionally the same. The memoryview stores the shape in a statically allocated array of length 8 (usually - the maximum number of dimensions is configurable if you need).
The advantage of doing this is that the array can be accessed directly from Cython with no indirection and no allocation because it's just a C array. The disadvantage is that it gives a slightly surprising output.
All the dimensions above ndim
are irrelevant to how it behaves.