I am having a very weird error when I cast a numpy ndarray from one type to the other in Cython. For instance when I write the following convert_test.pyx:
cimport cython
import numpy as np
cimport numpy as np
DTYPE = np.float32
ctypedef np.float32_t DTYPE_t
def convert_to_int(np.ndarray[DTYPE_t, ndim=2] float_matrix):
cdef np.ndarray[np.int, ndim=2] int_matrix=float_matrix.astype(np.int)
return int_matrix
compile it and test:
from convert_test import convert_to_int
a_int=convert_to_int(np.zeros((2,2)).astype(np.float32))
I got the very familiar message:
ValueError: Buffer dtype mismatch, expected 'int object' but got 'long'
It is probably super obvious but I don't quite see it yet.
inttype. It has been implemented aslong. So, maybe you should convert tolonginstead ofint. Although, I have no experience with Cython, so I can't say if it would work. - xyres