具有任意尺寸的Cython ndarray

时间:2015-10-24 08:30:41

标签: python numpy cython

我正在编写一些需要能够处理具有任意数量维度的NumPy ndarray的Cython代码。目前,我只有不同的函数接受不同大小的ndarray,类似于:

jobs may be run locally

但我想编写一个通用函数,它将任意维度的ndarray作为参数。我试着离开" ndim"参数off,但Cython假定ndim = 1,那并不好。

有没有办法做到这一点,或者我只需为每个维度编写一个函数?

1 个答案:

答案 0 :(得分:3)

如果您只是想要按元素做某事,那么诀窍就是获得数组的一维视图并对其进行操作

def func(arr):
   shape = arr.shape
   output = _func_impl(arr.ravel())
   return output.reshape(shape) # ensure that the output is the same shape
       # as the input. Skip this if it doesn't make sense!

def _func_impl(np.ndarray[DTYPE_float64_t, ndim=1] arr):
   # do something useful
相关问题