2015-11-16 24 views
2

通過一些源代碼去scikit學習,我在tree.pxd注意到以下一些類型聲明:用Cython:正確的方式來聲明一個unsigned int

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

FYI:http://stackoverflow.com/questions/20987390/cython-why-when-is-it-preferable-to-use-py-ssize-t-for-indexing –

+0

謝謝@WarrenWeckesser。你的文章讓我做這個'__init __。pxd'文件:https://github.com/cython/cython/blob/master/Cython/Includes/numpy/__init__.pxd#L325 – hlin117

回答

1

根據這個init.pxd file對於cython的numpy,看起來unsigned intnpy_uint32是一樣的東西。

另一方面,根據文件的this linenpy_intpPy_intptr_t相同。我相當確定這意味着指針的大小,這對應於陣列中的項目之間的間距等。

我認爲基於this discussion,沒有提到,Py_intptr_t是優選的,以適應不同體系結構之間的差異。所以要真正確定建築住宿,應該使用npy_intp

相關問題