[Numpy-discussion] creating/working NumPy-ndarrays in C++

Dag Sverre Seljebotn d.s.seljebotn@astro.uio...
Mon Apr 9 14:19:35 CDT 2012

On 04/08/2012 08:25 PM, Holger Herrlich wrote:
> That all sounds like no option -- sad.
> Cython is no solution cause, all I want is to leave Python Syntax in
> favor for strong OOP design patterns.

I'm sorry, I'm trying and trying to make heads and tails of this 
paragraph, but I don't manage to. If you could rephrase it that would be 
very helpful. (But I'm afraid that if you believe that C++ is more 
object-oriented than Python, you'll find most people disagree. Perhaps 
you meant that you want static typing?)

Any wrapping tool (Cython, ctypes, probably SWIG but don't know) will 
allow you to pass the pointer to the data array, the npy_intp* shape 
array, and the npy_intp* strides array.

Really, that's all you need. And given those three, writing a simple C++ 
class wrapping the arrays and allowing you to conveniently index into 
the array is done in 15 minutes.

If you need more than that -- that is, what you want is essentially to 
"use NumPy from C++", which slicing and ufuncs and reductions and so on 
-- then you should probably look into a C++ array library (such as Eigen 
or Blitz++ or the array stuff in boost). Then, pass the aforementioned 
data and shape/stride arrays to the array library.


More information about the NumPy-Discussion mailing list