[Numpy-discussion] using numpy array data in C++ matrix template library (MTL) matrices

David Cournapeau david@ar.media.kyoto-u.ac...
Wed Apr 8 01:35:41 CDT 2009

JJ wrote:
> Hello:
> I would like to make a numpy array accessible in C++ as a matrix template library (MTL) matrix object.  Has anyone done this before?  I see that there is a project numpy_boost that will make numpy arrays accessible as boost::multi_array objects.  Any ideas on how I might do this, or even better, a simple example of passing a numpy array to C++, accessing it in a MatrixRow container, changing one element, and then passing it back to python/numpy?  I'm a novice C++ programmer, so bear with me.

The details will depend on your matrix library, but the underlying numpy
array object has a full C api, so you can do whatever you want with it
in your C++ code. But it can get quite messy :)

I don't know for MTL, and for C++, boost can be useful, like Neal
suggested. One thing to keep in mind is that C arrays are much more
general that most low level C++ array containers (numpy arrays may not
be contiguous in memory, for example). The easy solution is to use the
numpy C api to get C contiguous arrays, so that the underlying data
pointer can be accessed as expected by a plain C array. At this point,
it is no different than using an C pointer pointing to an array, which
virtually every C++ array library can deal with.



More information about the Numpy-discussion mailing list