Passing a numpy array in Cython

I study Keaton. I have a problem with passing numpy arrays to Cython and really don't understand what is going on. could you help me?

I have two simple arrays:

a = np.array([1,2]) b = np.array([[1,4],[3,4]]) 

I want to calculate the point product of them. In python / numpy everything works fine:

 >>> np.dot(a,b) array([ 7, 12]) 

I translated the code in Cython (as here: http://docs.cython.org/src/tutorial/numpy.html ):

 import numpy as np cimport numpy as np DTYPE = np.int ctypedef np.int_t DTYPE_t def dot(np.ndarray a, np.ndarray b): cdef int d = np.dot(a, b) return d 

It compiled without problems, but returns an error:

 >>> dot(a,b) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "test.pyx", line 8, in test.dot (test.c:1262) cdef int d = np.dot(a, b) TypeError: only length-1 arrays can be converted to Python scalars 

Could you tell me why and how to do it right? Unfortunately, Google did not help ...

Thanks!

+7
python numpy cython
source share
1 answer

Your result is np.ndarray, not int. He is not trying to convert the first to the last. Instead of this

 def dot(np.ndarray a, np.ndarray b): cdef np.ndarray d = np.dot(a, b) return d 
+7
source share

All Articles