简体   繁体   中英

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. so I used swig.

I want to use a 3d-array and a 4d-array in the same function.

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

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:

**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? How to solve this problem? I want them both.

Python is saying that it expects 7 arguments for the function call. 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; 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 . 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. 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. It appears because it did not recognize the 4d-array, especially the numpy.i did not implement this part. So I just write a similar one as the 3d-array written in numpy.i , and add numpy.i into makefile.

It works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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