python - 如何将 3d 和 4d numpy.array 传递给 swig

标签 python numpy swig

我正在使用 python 来计算一些东西,但我想让它更快。所以我用了大口喝。

我想在同一个函数中使用 3d 数组和 4d 数组。

痛饮

%apply (double *INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3)\
{(double *trans, int trans_dim1, int trans_dim2, int trans_dim3)};

%apply (double *INPLACE_ARRAY4, int DIM1, int DIM2, int DIM3, int DIM4)\
{(double *sample,int sam_dim1, int sam_dim2, int sam_dim3, int sam_dim4)};

样本.h

void update_transition(double *trans,int trans_dim1,int trans_dim2, int trans_dim3,
 double *sample,int sam_dim1, int sam_dim2, int sam_dim3, int sam_dim4, double DENO);

但是当我在 python 中使用它时,出现错误:

**TypeError: update_transition() takes exactly 7 arguments (3 given)**

这是否意味着我的 3d 数组可以被 swig 识别,但 4d 数组不能?如何解决这个问题呢?我两个都想要。

最佳答案

Python 表示函数调用需要 7 个参数。 C++ 函数期望 10。如果 SWIG 能够应用这两个类型映射,则 Python 函数期望 3,而不是 7。从 10 到 7 的唯一方法是 SWIG 错过第二个类型映射;因此它能够匹配函数的第一个数组,但不能匹配第二个数组。要确认这一点,请使用 10.3.6 Debugging typemap pattern matching 中提到的一些技术。 。另请查看生成的代码。您可能想要尝试的一件事是 5 参数 C++ 测试函数,它接受一个 1D 数组和一个 2D 数组,使用 INPLACE_ARRAY1 和 2 导出为 2 参数 Python 函数。这是一个更容易解决的案例。

如果这不能解决问题但为您提供了有用的信息,请将其添加到您的问题中,也许我或其他人可以再看一下。

关于python - 如何将 3d 和 4d numpy.array 传递给 swig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22192448/

相关文章:

python - 来自 pandas 数据框列的字典

python - numpy vs Matlab 速度 - arctan 和 power

python - NumPy 中的加权计数

python - MinMaxScaler 如何工作 - 按行缩放还是针对整个数据集缩放?

python - 如何在 SWIG 界面中使用 C++ 宏

python - 获取在 ubuntu 上运行 python 的无效语法

python - 使用更新的索引将 python pandas 数据框的最后一行复制并添加到自身

python - 如何将 SwigPyObject 转换为 ctypes void*

python - 如何加载测试 Django API?

python - Swig 致命 Python 错误 : PyThreadState_Get: no current thread Abort trap: 6 with Python 2. 7.5