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

Holger Herrlich holgerherrlich05 at arcor.de
Thu Apr 12 07:02:31 EDT 2012


On 04/09/2012 09:19 PM, Dag Sverre Seljebotn wrote:
> 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?)

Yes and further, design patterns known to me are very java (static)
specific. In Python you can often work around the "rules" (or intension).

> 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.

On the scipy-pages I miss C/C++ samples. I got some idea of what to
include and what to link to by running distutils, but wonder why here is
no single one gcc command line. (Anyway, read further.)

> 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.

I see a bit clearer now that the task can be splited up. For first a
shared library will be used by Python (embedding C in Python). Ctypes
might do that. SWIG also. Second, to benefit from: slicing, in sito
calculation (ufunc?) and using the histogram[2D]() functions as I do in
Python. It's not necessary, for the second approach, to use Python at
all. It should be possible to compile a shared library.

Both will become mixed up if the ndarray crosses the library boundary,
maybe towards Python. More complicated SWIG. Taking care of deallocation.

So I will have a look on Blitz++, Boost, or native C++ arrays (here
performance will come to play), but I also consider the concept of using
NumPy-arrays. Or the concept of converting the array at an output layer.

I'm not that familiar yet with the C-API of NumPy, but do I see it right
that in using NumPy a whole Python is embedded to the Program?
Inclusively the garbage collector. Is this, why distutils allways link
with pthread (linux).

still reading, Holger
(And hopefully this will be better to read.)

> Dag _______________________________________________ NumPy-Discussion
> mailing list NumPy-Discussion at scipy.org 
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 



More information about the NumPy-Discussion mailing list