简体   繁体   English

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

[英]how to pass a 3d and 4d numpy.array to swig

I am using python to calculate something, but i want to make it faster. 我正在使用python计算某些内容,但我想使其更快。 so I used swig. 所以我喝了

I want to use a 3d-array and a 4d-array in the same function. 我想在同一函数中使用3d数组和4d数组。

swig.i swig.i

%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)};

sample.h sample.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);

but when i use it in python, error: 但是当我在python中使用它时,错误:

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

is that means my 3d-array can be recognized by swig, but 4d-array can't? 这意味着我的3d数组可以被swig识别,但是4d数组不能? How to solve this problem? 如何解决这个问题呢? I want them both. 我要他们两个。

Python is saying that it expects 7 arguments for the function call. Python表示希望函数调用有7个参数。 The C++ function expects 10. If SWIG were able to apply both these typemaps, the Python function would expect 3, not 7. The only way to go from 10 to 7 is for SWIG to miss the second typemap; C ++函数的期望值为10。如果SWIG能够同时应用这两个类型映射,则Python函数的期望值为3,而不是7。唯一的从10变为7的方法是SWIG错过了第二个类型映射。 so it is able to match your function's first array, but not the second one. 因此它可以匹配函数的第一个数组,但不能匹配第二个数组。 To confirm this, use some techniques mentioned on 10.3.6 Debugging typemap pattern matching . 为了确认这一点,请使用10.3.6调试类型图模式匹配中提到的一些技术。 Also look at the code generated. 还要看看生成的代码。 One thing you might want to try is a 5-paramater C++ test function that takes a 1D array and a 2D array, export with INPLACE_ARRAY1 and 2 for a 2-parameter Python function. 您可能想尝试的一件事是使用5参数C ++测试函数,该函数需要一个1D数组和一个2D数组,并使用INPLACE_ARRAY1和2导出以生成2参数Python函数。 This is a simpler case to solve. 这是一个较简单的情况。

If this doesn't solve problem but gives you useful info, add it to your question and maybe I or others can take another look. 如果这不能解决问题,但可以为您提供有用的信息,请将其添加到您的问题中,也许我或其他人可以再看看。

Thanks for Schollii's help. 感谢Schollii的帮助。 It appears because it did not recognize the 4d-array, especially the numpy.i did not implement this part. 似乎是因为它无法识别4D数组,尤其是numpy.i没有实现这一部分。 So I just write a similar one as the 3d-array written in numpy.i , and add numpy.i into makefile. 因此,我只编写了一个与numpy.i中编写的3d数组类似的代码,并将numpy.i添加到makefile中。

It works! 有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM