[Numpy-discussion] Numpy and C++ integration...

Sebastian Haase haase@msg.ucsf....
Tue Feb 5 02:15:29 CST 2008

On Feb 4, 2008 9:05 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:
> Lou Pecora wrote:
> > I
> > would recommend using the C API
> I would recommend against this -- there is a lot of code to write in
> extensions to make sure you do reference counting, etc, and it is hard
> to get right.
> Much of it is also boiler-plate code, so it makes more sense to have
> that code auto-generated.
> There are just too many good tools to do this for you to do it by hand.
> The problem is that there is an embarrassment of riches -- if only one
> or tow of the C/C++ interface tools were out there, it would be a whole
> lot easier to choose! My take:
> ctypes -- best if you have dll-type interface already defined, and
> particularly if there are a smallish number of functions you want to
> call. Can it call C++ directly at all?
> pyrex -- best if you want to implement some custom functions in C from
> scratch. Also pretty good for calling external C code. Only supports
> calling C++ code that's not too fancy -- i.e. stuff that can be called
> from C -- pyrex has no explicit support for C++
> SWIG -- best if you have a lot of code to wrap that shares similar
> interfaces - once you write the typemaps, the rest is automatic. Also
> best choice if you want to support more than one scripting language, or
> you want to integrate with other packages built with SWIG (wxPython,
> GDAL, VTK, ...). Bill's numpy-swig typemaps make it easy to deal with
> classic C-style pointers to data blocks. It also comes with built-in
> wrappers for std:vector, though not numpy integration for those.
> SIP -- built for pyQT -- folks seem to like it. I don't know if anyone
> has done anything for numpy with it.
> Boost::python -- best for writing custom extensions in C++ -- also can
> be used for interfacing with legacy C++. There were boost array classes
> for numpy -- are these being maintained?
> Any of these can do that job -- so it's hard to choose, but maybe the
> above helps focus your search.
> -Chris
Which of these can automatically wrap (and "overload" !!) function templates ?
I have many "helper" functions  which can operate equally on many --
if not all -- number types.
As examples I would like to mention my favorite function that I call
"mmms" which calculates min,max,mean,std.dev. in one go.
I use SWIG with  my own typemaps which (by now) might be similar to
what is in numpy (written by Bill). They can handle uint8, int16,
uint16, uint32, int32, float32,float64.  A C-preprocessor function
instantiates the function for each type and another (SWIG command)
overloads it for python.
They work without any memory-copy (!!)  *if* the data is C-contiguous.

Can ctypes do this ?

More information about the Numpy-discussion mailing list