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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With