[Numpy-discussion] C++ Example

Sturla Molden sturla@molden...
Tue Mar 6 14:39:46 CST 2012


On 03.03.2012 17:07, Luis Pedro Coelho wrote:

> I sort of missed the big C++ discussion, but I'd like to give some examples of
> how writing code can become much simpler if you are based on C++. This is from
> my mahotas package, which has a thin C++ wrapper around numpy's C API

Here you are using NumPy arrays, not implementing them. That makes a big 
difference. Your code would be even simpler if you had used Cython or 
Fortran (f2py) instead of C++.

The only thing you have proved is that using NumPy arrays in C can be 
messy, which I suspect everybody knows already.

C is a good systems programming language, but it sucks for any kind of 
numerical computing. C++ sucks a little bit less. Using either for 
numerical programming usually a mistake. But NumPy core is not a case of 
numerical programming, it is a case of systems programming, for which C 
is actually very nice.

And no, unmaintainable C is NOT an example of "very good C", rather the 
opposite. This is a big mistake:

    "If you compare this with the equivalent scipy.ndimage function, 
which is very good C code (but mostly write-only—in fact, ndimage has 
not been maintainable..."

Another thing to observe is that the ndimage C code you cited has a 
major DRY violation: We have the best text (pre)processor there is to 
our disposal: Python. So why are there macros and switch statements for 
so many dtypes in there? As with C++ templates, generics in C is easy if 
we just let Python generate the type-specialized code we need. (Actually 
that is what NumPy's .src files do.) Generics is therefore not an 
argument for using C++. We can augment standard C with syntax suger for 
generics, or even NumPy arrays and Python types, using a Python script 
as meta-compiler. We don't need C++ for that.


Sturla


More information about the NumPy-Discussion mailing list