[SciPy-user] C/C++ extension with swig

Prabhu Ramachandran prabhu_r at users.sf.net
Sat Dec 10 11:46:49 CST 2005


>>>>> "JR" == Jaonary Rabarisoa <jaonary at free.fr> writes:

    JR> 	void (double *input, some other args, ...., double
    JR> 	*output1, double
    JR> *output2, ...)

    JR> The input and output arguments represent arrays (matirx,
    JR> vectors, .nd- arrays, ...). In python, I'd like to passe the
    JR> argument as a list (or scipy array) of the form

    JR> 	[ [row1], .........., [rowN] ]

I sent a related reply on the swig-users list a while back that
probably never made it to the list for reasons unknown to me.  Anyway,
here is part of that message and it might be of some use to you.

In short, you could use the buffer protocol of the array.  scipy
arrays (and Numeric/numarray) feature a buffer protocol (for numarray
you need to use the array's '_data' attribute to get a buffer capable
object) and internally manages its data using a single "char *data".

I've attached a simple example buffer.i file (along with a
test_buffer.py example) that should give you an idea of how this can
be done.  In the example I have some simple for loops to pump the data
into scipy (or Numeric or numarray) arrays but you could instead use
memcpy or anything else.

A faster approach would be to create the scipy array from the Python
side and then point your internal data pointers to use the the pointer
used by the scipy arrays.  This eliminates the need for a copy.  I
suggest you experiment with the buffer.i and figure out an approach
you can use.

cheers,
prabhu

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: buffer.i
Url: http://www.scipy.net/pipermail/scipy-user/attachments/20051210/8a61431c/buffer.bat
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test_buffer.py
Url: http://www.scipy.net/pipermail/scipy-user/attachments/20051210/8a61431c/test_buffer.bat


More information about the SciPy-user mailing list