[SciPy-User] SWIG + numpy and data types

Rohit Garg rpg.314@gmail....
Wed Feb 10 22:20:24 CST 2010

AFAIK, this will not be possible without some serious gymnastics. The
typemaps available in SWIG's numpy.i allow for both input and output
arrays to be the same size only.

One easy way (a hack, TBH)  to achieve this to make a struct with int
N, int* interactions, float* contributions, float* landscape as
members and define functions on that struct which pass in numpy arrays
corresponding to  interactions and contributions arrays and which just
copy this data into the struct.

Then you can define another function over this struct in C which is a
proxy to call your highly optimized function.

One last function could be one which just passes in the float
*landscape and copies the data back into numpy array.

I admit I have not figured out how to use the SWIG's support for
buffer protocol yet. It is supposed to be very clean and meant for
tasks like this only. If someone could point out a simple example, I'd
be very interested in having a look.

On Thu, Feb 11, 2010 at 5:14 AM, Ignacio Vergara <nolambar@gmail.com> wrote:
> Hi
> I hope this is the correct mailing list for this.
> I'm using SWIG to create a module for Python+numpy for a legacy and highly optimized C code.
> The function takes 2 array arguments (different sizes) and returns a third array. But I've been unable to define the interface file.
> The function definition is
> void cLandscape(int N, int* interactions, float* contributions, float* landscape);
> where interactions is a N*(N+1) array, contributions is another array of length 2^(K+1) [0 <= K <= N-1] and landscape is the returned array of length 2^N. So N is the only relevant length.
> The function loops over 2^N to fill the landscape array.
> The interface typemap line looks like this
> %apply (int DIM1, int* IN_ARRAY1, float* IN_ARRAY1, float* ARGOUT_ARRAY1) {(int N, int* interactions, float* contributions, float* landscape)}
> Thanks in advance for any tip and help ;)
> Ignacio Vergara Kausel
> Bachelor in Science, Physics
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

Rohit Garg


Senior Undergraduate
Department of Physics
Indian Institute of Technology

More information about the SciPy-User mailing list