[C++-sig] [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with boost)

Philip Austin paustin at eos.ubc.ca
Tue Sep 11 19:46:31 CEST 2007


Alexander Schmolck writes:

 > So my question is: might it make sense to use (a slightly wrapped)
 > numpy.ndarray, and if so is some code already floating around for that (on
 > first glance it seems like there's a bit of support for the obsolete Numeric
 > package in boost, but none for the newer numpy that supercedes it); if not is
 > my impression correct that making the existing code numpy compatible shouldn't
 > be too much work.

Right, it should work more or less as is if you just do:

set_module_and_type("numpy", "ArrayType");

in the examples.  Some tests will fail because of numpy changes to
function signatures, etc.

The current library doesn't wrap numpy.zeros, numpy.ones or
numpy.empty constructors, so the only way to construct an empty
is to pass the constructor a tuple and then resize.  Because
it (by design) doesn't include arrayobject.h, there's also no clean
way to get at the underlying data pointer to share the memory.  You
can use helper functions like this, though:

//Create a one-dimensional numpy array of length n and type t
boost::python::numeric::array makeNum(intp n, PyArray_TYPES t=PyArray_DOUBLE){
  object obj(handle<>(PyArray_FromDims(1, &n, t)));
  return extract<boost::python::numeric::array>(obj);
}

http://www.eos.ubc.ca/research/clouds/software/pythonlibs/num_util/num_util_release2/Readme.html

has more examples -- Phil




More information about the Cplusplus-sig mailing list