【发布时间】:2015-10-24 08:30:40
【问题描述】:
我正在编写一些需要能够处理具有任意维数的 NumPy ndarray 的 Cython 代码。目前,我只有不同的函数可以接受不同大小的 ndarray,有点像:
def func1(np.ndarray[DTYPE_float64_t, ndim=1] arr):
# Do something with the 1-D ndarray.
def func2(np.ndarray[DTYPE_float64_t, ndim=2] arr):
# Do something with the 2-D ndarray.
def func3(np.ndarray[DTYPE_float64_t, ndim=3] arr):
# Do something with the 3-D ndarray.
但我想编写一个通用函数,它将任意维度的 ndarray 作为参数。我尝试简单地关闭“ndim”参数,但随后 Cython 假定 ndim=1,这不好。
有没有办法做到这一点,或者我只需要为每个维度编写一个函数?
【问题讨论】: