Cython:声明unsigned int的正确方法

时间:2015-11-16 23:34:13

标签: python numpy cython

在scikit-learn中查看一些源代码,我在tree.pxd中注意到以下一些类型声明:

import numpy as np
cimport numpy as np

ctypedef np.npy_float32 DTYPE_t          # Type of X
ctypedef np.npy_float64 DOUBLE_t         # Type of y, sample_weight
ctypedef np.npy_intp SIZE_t              # Type for indices and counters
ctypedef np.npy_int32 INT32_t            # Signed 32 bit integer
ctypedef np.npy_uint32 UINT32_t          # Unsigned 32 bit integer

我知道Cython docs here关于C类型和cython类型之间的区别有一些讨论,但这些类型似乎是来自numpy的类型,并且在文档中没有提到它们。

我对我应该使用的类型感到困惑。对于索引,我应该使用上面定义的SIZE_t还是unsigned int?这些ctypedef确实存在吗?

1 个答案:

答案 0 :(得分:3)

根据init.pxd file对于cython的numpy,似乎unsigned intnpy_uint32完全相同。

另一方面,根据文件的this linenpy_intpPy_intptr_t相同。而且我很确定这意味着指针的大小,它对应于数组中项目之间的间距等。

我认为基于this discussion,不提及Py_intptr_t是首选,以适应架构差异。因此,要真正确定建筑住宿,应使用npy_intp

相关问题