简体   繁体   中英

how to initialize fixed-size integer numpy arrays in Cython?

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?

Include the statement

cimport numpy as np

and declare the array as, say, np.int32_t :

cdef np.ndarray[np.int32_t, ndim=1] myarr = np.empty(N, dtype=np.int32)

You can drop the 32 from the type declarations, and use

cdef np.ndarray[np.int_t, ndim=1] myarr = np.empty(N, dtype=np.int)

but I prefer to be explicit about the size of the elements in a numpy array.

Note that I also added the dtype to empty ; the default dtype of empty is np.float64 .

Wierd!I got the same error when I tried. However, looking at the error message, I just changed the scope of the array creation to a function, and it compiles! I don't know the reason why this is happening, but .

import numpy as np
cimport numpy as np

ctypedef np.int_t DTYPE_t
DTYPE=np.int

def new_array():
    cdef int length = 10
    cdef np.ndarray[DTYPE_t, ndim=1] x = np.zeros([length], dtype=np.int)
    return x

x = new_array()

I think http://docs.cython.org/src/userguide/language_basics.html#python-functions-vs-c-functions has some information related to scoping of python/c/mixed variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM