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

Matthieu Brucher matthieu.brucher@gmail....
Thu Apr 19 03:09:57 CDT 2007

>     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

Thanks for the fast answer ;)
I was wondering if ctypes would fit better, but in this case, I have to make
the wrapper myself, I suppose ?
Here is the basic prototype of what I have - it's a cost function I want to
optimize with optimizers I proposed on scipy recently - :

template<class MatrixType>
struct CostFunction
  CostFunction(const MatrixType& points, ...)

  const MatrixType gradient(const Matrixtype& parameters)
    // ...
    return aNewGradient;

So I have to create a function that takes a numpy array and that returns and
instance of my cost function, this is pretty simple to do, the matrix can be
constructed from its two dimensions and a pointer, but for the gradient
method, I have trouble seeing how to do wrap it :(

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070419/99bea961/attachment.html 

More information about the Numpy-discussion mailing list