简体   繁体   中英

Numpy types for Cython users

I'm not quite understand what is the difference between numpy.{typename}, numpy.npy_{typename} and numpy.{typename}_t when i use them from Cython code?

ie what is the difference in these types:

# test.pyx
cimport numpy as np
import numpy as np

np.float32
np.npy_float32
np.float32_t

As i understand it now: first type is dynamic, ie Cython will generate some code to detect size of that type at runtime. Two other types are static, ie code which uses it will be compiled with predefined sizes of each type. Please correct me.

Additional link: https://docs.scipy.org/doc/numpy/reference/c-api.dtype.html#c-type-names

np.float32 is NumPy's TypeDescriptor, which is a Python object which can be queried and passed to NumPy to construct arrays just as in Python.

np.npy_float32 is a C type, which can be used wherever a C type is needed, eg

cdef np.npy_float32 x = 1.902
cdef np.ndarray[np.npy_float32, ndim=2] A = np.zeros((3, 4), dtype=np.float32)

np.float32_t is simply a typedef of np.npy_float32 which can be used as a shorthand.

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