Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swig numpy multiple matrix and array inputs

Tags:

python

c

numpy

swig

I'm trying interface a small C function I made into python using SWIG and the 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 test it, 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 am really confused about how these arguments are defined. Does anyone here have an idea why there are 6 arguments and what these arguments are? Thanks!

like image 418
mortonjt Avatar asked Aug 03 '13 00:08

mortonjt


1 Answers

Alright, I think I have figured out the problem.

As it turns out, SWIG really didn't like the apply directives I made for the cstrings.

I should have be should the following directive instead.

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

Should have been following the cookbook more carefully haha

like image 115
mortonjt Avatar answered Oct 22 '22 21:10

mortonjt