Multiple Matrix and Input Arrays

I am trying to connect a small C function that I made in python using SWIG and Numpy typemaps

This function is defined as follows

void nw(int* D, int Dx, int Dy, int* mat, int mx, int my, char *xstr, int xL,char *ystr, int yL); 

And my interface file is as follows

 %module nw %{ #define SWIG_FILE_WITH_INIT #include "nw.h" %} %include "numpy.i" %init %{ import_array(); %} /*type maps for input arrays and strings*/ %apply (int* INPLACE_ARRAY2, int DIM1, int DIM2) {(int* D, int Dx, int Dy)} %apply (int* IN_ARRAY2, int DIM1, int DIM2) {(int* mat, int mx, int my)} %apply (char* IN_ARRAY, int DIM1){(char *xstr, int xL),(char *ystr, int yL)} %include "nw.h" 

To check this, I used the following input

 D = numpy.zeros((5,5),numpy.int) mat = numpy.array([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1]],numpy.int) x = numpy.array(list("ABCD")) y = numpy.array(list("ABCD")) import nw nw.nw(D,mat,x,y) 

But when I run it, I get the following

 TypeError: nw() takes exactly 6 arguments (4 given) 

I'm really confused about how these arguments are defined. Does anyone have an idea why there are 6 arguments and what are these arguments? Thanks!

+4
source share
1 answer

Ok, I think I understood the problem.

As it turned out, SWIG really didn't like the directives I applied for cstrings.

I would have to use the following directive.

 %apply (char *STRING, int LENGTH) {(char *xstr, int xL),(char *ystr, int yL)} 

Should have followed the cookbook more carefully haha

+3
source

Source: https://habr.com/ru/post/1495049/


All Articles