[Numpy-discussion] How to call methods from a class with custom matrices parameters with numpy arrays ?

David Cournapeau david@ar.media.kyoto-u.ac...
Thu Apr 19 02:57:02 CDT 2007


Matthieu Brucher wrote:
> Hi,
>
> I want to wrap some code I've done in the past with a custom array and 
> pass numpy arrays to it.
> So I need to transform numpy arrays to my arrays at the construction 
> of an instance of my class, as well as each call to a method (pass by 
> value). Then, some method return by value an array I have to transform 
> back in numpy arrays. I imagine that such wrappers must be done in the 
> .i file...
> I'm still new to SWIG, even though I understand the easy wrapping, 
> such wrapping is a bit difficult for me at the moment. I do not 
> understand for instance the numpy.i wrappers, what they do, ... Is 
> there a simpler doc somewhere to explain this ? Or a simple SWIG 
> tutorial and a Numpy interface tutorial - Travis' book ? - ?
>
> Matthieu
>   
Hello Matthieu :)

    You should give ctypes a try, I find it much better than swig most 
of the time for wrapping. You can find some doc here:

    http://www.scipy.org/Cookbook/Ctypes2

    Basically, once you get your dll/so with a function foo(double *a, 
int n), you can call it directly in numpy by passing directly a numpy 
array. The catch is that the layout of the array should be exactly the 
same than your function expects (most of the time a contiguous array), 
but there are numpy functions which enforce that. As long as you are not 
calling thousand of function with small arrays, the wrapping is pretty 
efficient in my experience.

    cheers,

    David


More information about the Numpy-discussion mailing list