python - Swig and multidimensional arrays -


i using swig interface python c code.

i want call c function takes argument struct containing int** var:

typedef struct {     (...)     int** my2darray; } mystruct;  void mycfunction( mystruct struct ); 

i struggling multi dimensional arrays.

my code looks this:

in interface file, using carray this:

%include carrays.i %array_class( int, intarray ); %array_class( intarray, intarrayarray ); 

in python, have:

mystruct = mymodule.mystruct() var = mymodule.intarrayarray(28)  j in range(28):     var1 = mymodule.intarray(28)      in range(28):         var1[i] = (...) filling var1 (...)      var[j] = var1  mystruct.my2darray = var  mycfonction( mystruct ) 

i error on line mystruct.my2darray = var:

typeerror: in method 'mastruct_montableau2d_set', argument 2 of type 'int **' 

i did doubt linge %array_class( intarray, intarrayarray ) tried using typedef int* create array this: %array_class( mytypedef, intarrayarray ); didn't seem work.

do know how handle mutlidimensional arrays in swig ?

thanks help.

have considered using numpy this? have used numpy swig-wrapped c++ project 1d, 2d, , 3d arrays of double , std::complex elements lot of success.

you need get numpy.i , install numpy in python environment.

here example of how structure it:

.i file:

// numpy related includes: %{ #define swig_file_with_init %} // numpy arrays %include "numpy.i" %init %{ import_array(); // essential. crash in python without it. %} // these names must match function declaration. %apply (int* inplace_array2, int dim1, int dim2) \       {(int* npyarray2d, int npylength1d, int npylength2d)}  %include "yourheader.h"  %clear (int* npyarray2d, int npylength1d, int npylength2d); 

.h file:

/// data in 2d array. void arrayfunction(int* npyarray2d, int npylength1d, int npylength2d); 

.cpp file:

void arrayfunction(int* npyarray2d, int npylength1d, int npylength2d) {     for(int = 0; < npylength1d; ++i)     {         for(int j = 0; j < npylength2d; ++j)         {             int nindexj = * npylength2d + j;             // operate on array             npyarray2d[nindexj];         }     } } 

.py file:

def makearray(rows, cols):     return numpy.array(numpy.zeros(shape=(rows, cols)), dtype=numpy.int)  arr2d = makearray(28, 28) mymodule.arrayfunction(arr2d) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -