[C++-sig] Status of Numpy support in boost python

Ger van Diepen diepen at astron.nl
Thu Apr 22 09:31:14 CEST 2010

Another library that can pass C++ arrays to/from python is pyrap
(pyrap.googlecode.com). It is the python binding to the casacore
package, so it maps (possibly strided) Python arrays to casa::Array

Unlike the other C++ array classes I know (Blitz, boost::multi_array,
vigra::MultiArray) the dimensionality of casa::Array is a runtime
parameter, not a template parameter. It makes it easier to handle arrays
(e.g. reading from a file, passing in from python) of which the
dimensionality is unknown at compile time. Iteration through a strided
casa::Array is as fast as boost::multi_array.
casa::Array axes are Fortran ordered, while numpy is basically C
ordered (numpy can handle Fortran order, but slower; furthermore it
converts to C order as soon you do something like an addition).
Therefore pyrap reverses the axes. 

Note that pyrap can also handle the conversion of a numpy array element
to a basic C++ type (like float). 

pyrap is based on an old version of Boost_Python, so it does not use
newer, handier features. 

Ger van Diepen 

>>> Hans Meine <hans_meine at gmx.net> 4/21/2010 10:36 PM >>>
Am Montag 08 März 2010 13:32:22 schrieb Pim Schellart:
> we are working on a project for which it would be extremely useful
> numpy arrays could be passed as arguments to wrapped C++ methods.
> On the website I cannot find any evidence that this is currently
> supported by Boost Python.
> Is this (or will this be) implemented?

After all the recent discussion, I want to point to yet another
interesting bit of code which I am involved with:

Our "vigra" (computer vision) library now comes with numpy- and
python bindings.

In addition to type-safe conversions between C++ and Python(*), this
offers a
vigra::MultiArray on the C++ side, which is an n-dimensional array
template, complete with n-dimensional iterators (using raw pointers for
innermost dimension to offer the best possible performance for
arrays) and a lot of algorithms, i.e. pointwise transformation /
type-coercing] copying / inspection etc.

One particular feature Ulli is proud of is the automatic conversion to
*subclasses* of ndarray, such that we can register and use e.g.
python classes for 3D arrays.

I really want to stress that VIGRA is a very versatile template library
should be able to work in conjunction with any other libraries and
structures, i.e. true generic code (usually you don't even need to
VIGRA, if you are not interested in an image import/export library). 
So don't
hesitate to give it a shot even if you are /not/ interested in any of
array / linalg algorithms.  (Actually, the PythonArray template class
be even useful without boost::python, i.e. I am using it with SIP,

I don't think there will ever be just /one/ single solution for
boost::python+numpy, since everyone seems to have different
requirements/expectations w.r.t. available algorithms, static vs.
element types etc.

However, I do think that our vigranumpy approach could be very useful
for some
of this list's readers.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100422/b754c2d5/attachment.html>

More information about the Cplusplus-sig mailing list