[Matrix-SIG] SWIG, Python, and NumPy

Joe Van Andel vanandel@ucar.edu
Sat, 09 Jan 1999 18:22:12 -0700


I'm starting a project where I'm writing some analysis code using 
NumPy.  I wanted to manipulate PyArrayObject's, without having to write
all the low level Python interface code.  I wanted to let you know that 
your system of typemapping, in SWIG, although complex to use, was powerful 
enough to let me do what I wanted.  I simply wrote the following
type maps, and SWIG generates the interface code that I didn't want to 
think about.

Thanks again for furnishing such a powerful tool.  I'd appreciate it if 
you put these typemaps into your next distribution.

My C++ class interface definition looks like:

%module pulsepair
%{
#include "pulsepair.h"
%}

%include "numpy_typemaps.i"
class PulsePair {
    public:
    PulsePair(int numGates, int pulsesPerBeam, double powerScale);

    PyObject *compute(int pulsesPerBeam, PyArrayObject *INPUT,
                      PyArrayObject *INPUT);
}
-----------------------------------------
// NumPy typemaps

%typemap(python,in) PyArrayObject *INPUT
{
    $target = (PyArrayObject *)$source;    
}

%typemap(python,out) PyObject * {
    $target = $source;
}


Joe VanAndel  		Internet: vanandel@ucar.edu
National Center for	http://www.atd.ucar.edu/~vanandel/home.html
Atmospheric Research