How can one make empty numpy arrays of type int
in Cython? The following works for me for double or float arrays:
# make array of size N of type float
cdef np.ndarray[float, ndim=1] myarr = np.empty(N)
# make array of size N of type int
cdef np.ndarray[int, ndim=1] myarr = np.empty(N)
However, if I try to do the same with int, it fails:
# this fails
cdef np.ndarray[np.int, ndim=1] myarr = np.empty(N)
# wanted to set first element to be an int
myarr[0] = 5
it gives the error:
ValueError: Buffer dtype mismatch, expected 'int' but got 'double'
Since apparently np.empty()
returns a double. I tried:
cdef np.ndarray[np.int, ndim=1] myarr = np.empty(N, dtype=int)
but it gives the same error. How can this be done?