I am also missing a good cookbook for using numpy.i
. As far as I understand, you can either:
- pass arrays of dynamic size, where you pass the dimensions as function parameters as well. If your function behaves differently, write a wrapper (e.g.,
IN_ARRAY2
orINPLACE_ARRAY2
). - pass arrays of fixed size (e.g.,
IN_ARRAY2
orINPLACE_ARRAY2
). - When returning arrays (e.g.,
ARGOUT_ARRAY1
), you have to pass the size when calling it from python. In the example below, you would writeoo = func3(20)
. The reason seems to be since python needs to allocate the memory, it needs to know about the size,
For example, your .i
-file could look like his:
...
%include "numpy.i"
%init %{
import_array();
%}
// Pass array of dynamic size:
%apply (double* INPLACE_ARRAY2, int DIM1, int DIM2) {(double *xx, int xx_n, int xx_m)};
void func1(double *xx,int xx_n, int xx_m);
// Pass array of fixed size:
%apply (int *INPLACE_ARRAY2[ANY][ANY]) { (double yy[4][4]) };
void func2(double yy[4][4]);
// Return a dynamic 1D array:
%apply (double* ARGOUT_ARRAY1, int DIM1) {(double* out, int out_n)}
void func3(double* out, int out_n);
Of course you can combine these - check the Docs for more information