[C++-sig] Numpy ndarray as argument or return value using boost python

Anthony Foglia AFoglia at princeton.com
Mon Oct 12 19:54:43 CEST 2009

Pim Schellart wrote:
> I also posted this to the boost users list.

Most of the Boost Python discussion goes on here, so this is probably 
the better group.

> I want to give a numpy ndarray as an argument to a c++ method,
> calculate something and return the output as an ndarray to Python.
> I would prefer not to use another library (such as PyUblas) for this
> so I do not have to add another dependency to my program.

I did this myself last year, and here's what I can tell you.

> 1. Is boost python still maintained or should I switch to another tool.

Yes it is maintained.

> 2. Is numpy supported, and if so,

Yes and no.  Boost.Python has a header for dealing with Numeric arrays 
that works just as well with NumPy. 
  But it works by passing all methods through the Python interpreter, so 
you will see no increase in speed compared to using a more low-level 
NumPy API.

There is num_util header created by a third-party that claims to do a 
lot of the wrapping. 
  I ended up not using it, but instead going getting using NumPy's C API 
directly and keeping the pointers wrapped in boost::python::handle 
objects as much as possible.

> 3. can someone give me a basic example of how to use it.

The num_util page has an example of their code.

Anthony Foglia
Princeton Consultants
(609) 987-8787 x233

More information about the Cplusplus-sig mailing list