rpg
rpg

Reputation: 975

SWIG: No typemaps are defined

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.

Upvotes: 5

Views: 3579

Answers (2)

Ruben de Bruin
Ruben de Bruin

Reputation: 1

This works for me:

for

void get_position(double outarray[3])

use

%apply (double ARGOUT_ARRAY1[ANY]) {(double outarray[3])};

(note [ANY] and [3])

result in

a.get_position()
Out[2]: array([0., 0., 0.])

Upvotes: 0

Codie CodeMonkey
Codie CodeMonkey

Reputation: 7946

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.

Upvotes: 3

Related Questions