I want to understand why the following Cython code fails to compile. This function: dimsum simply computes sum of all dimensions of a numpy array:
from numpy cimport ndarray
import numpy as np
cpdef int dimsum(ndarray x):
cdef:
int N
tuple shape
shape = x.shape
N = np.sum(shape)
return N
The dimension of input array: x is unknown before run-time. As such, the shape of input array can not be unpacked and assigned to a few new variables.
I got the following error in the compilation:
Error compiling Cython file:
------------------------------------------------------------
...
cpdef int get_size(ndarray x):
cdef:
int N
tuple shape
shape = x.shape
^
------------------------------------------------------------
test_shape_tuple.pyx:9:13: Cannot convert 'npy_intp *' to Python object
Any suggestions on why this happens and how to resolve this?