Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIG: No typemaps are defined

Tags:

python

numpy

swig

I have a c++ class that I am trying to wrap for Python using SWIG. I am having trouble trying to wrap one of the functions which takes an array as input.

Here's the header file,

class dyndiff_data_t
    {
    private:    

        double H[3];
        double GAMMA;
        double k;
        double P;
    public:
        dyndiff_data_t(double H_[3],
                        const double GAMMA_,
                        const double k_,
                        const double P_);

        void test();
    };

and here's the swig interface file,

%module twowave
%{
  #define SWIG_FILE_WITH_INIT
  #include <twowave.h>
%}

%include "numpy.i"
%init %{
import_array();
%}

%apply (double IN_ARRAY1[3]) {(double H_[3])};

%include <twowave.h>

The problem is that for the array input, SWIG complains that there is no typemap. I don't understand why. The numpy.i file was taken from here and the typemap I am using is described here

Any help would be appreciated.

like image 929
rpg Avatar asked Oct 21 '22 17:10

rpg


1 Answers

The problem is that the typemap in numpy.i defines a two argument typemap, and you're trying to apply it to a single argument. This would work if you had parameters int len1, and double* vec1 in your function:

%apply (int DIM1, double* IN_ARRAY1) {(int len, double* H_)}

Rather than writing your own typemap, just use carrays.i.

If you WERE to write a typemap, e.g. to take a tuple of doubles as input, it would look something like:

%typemap(in) double TUPLE[ANY]
{
   ...
}

in which case you would apply it to your function the way you expect.

%apply double TUPLE[3] {double H_[3]}

A good place to start when trying to figure out why you can't use a typemap is to run SWIG with the -tmsearch option. It will tell you what it's looking for when trying to match your function parameters.

like image 74
Codie CodeMonkey Avatar answered Nov 04 '22 01:11

Codie CodeMonkey