我有以下功能,
%%cython
cdef cytest(double[:,:] arr):
return print(arr.shape)
def pytest(arr):
return cytest(arr)
我用下面的numpy数组运行pytest
dummy = np.ones((2,2))
pytest(dummy)
我得到以下结果,
[2, 2, 0, 0, 0, 0, 0, 0]
这是因为在C
中,数组的形状是固定的。cython
数组可以具有的最大维度数为8。Cython将数组的所有维度存储在这个固定长度的数组中。
这可以通过以下操作进行验证:
%%cython
cdef cytest(double[:,:,:,:,:,:,:,:,:] arr): # works up to 8 ':'
return arr.shape
def pytest(arr):
return cytest(arr)
当编译它时,它抛出以下错误:
Error compiling Cython file:
------------------------------------------------------------
...
cdef cytest(double[:,:,:,:,:,:,:,:,:] arr):
^
------------------------------------------------------------
/path/to/_cython_magic_9a9aea2a10d5eb901ad6987411e371dd.pyx:1:19: More dimensions than the maximum number of buffer dimensions were used.
这本质上意味着预设的最大尺寸为8,我认为您可以通过更改cython_magic源文件来更改这一点。