[C++-sig] boost-python interface numarray to c++

Philip Austin paustin at eos.ubc.ca
Fri Jun 20 19:03:46 CEST 2003

Neal D. Becker writes:
 > On Thursday 19 June 2003 04:22 pm, Philip Austin wrote:
 > > Neal D. Becker writes:
 > >  > It would probably be useful also to create arrays in python.  Is it
 > >  > difficult to interface a python Numeric and/or numarray array to c++
 > >  > stl-style iterator interface?
 > >
 > > See http://www.boost.org/libs/python/doc/v2/numeric.html#array-spec
 > >
 > Thanks for the pointer.  Sorry for my slowness, but I'm afraid that it wasn't 
 > obvious to me what to do to get stl-style iterators.  Could you suggest 
 > something or maybe a small example?  Thanks.

Well, I'm not exactly sure what you mean by "get stl-style iterators"
but if you want to pass a  Numpy array to C++ and
construct an array that supports iterators (like boost::ublas or
mtl) using its data pointer, then our approach looks something like this:

a C++ histogram module, on the python side:

import hist

on the C++ side:

using namespace boost::python;
namespace nbpl = num_util;

  dict histo::hist(numeric::array x, int numbins, 
		       float mindata, float maxdata)
    nbpl::check_rank(x, 1);
    nbpl::check_type(x, PyArray_FLOAT);
    float* dataPtr = (float*) nbpl::data(x);

    both mtl and ublas have constructors that will make an
    array from dataPtr, and let Python manage
    the memory.

Regards, Phil    

More information about the Cplusplus-sig mailing list