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

Chris Barker chris.barker@noaa....
Tue Apr 3 11:48:06 CDT 2012

On Tue, Apr 3, 2012 at 6:06 AM, Holger Herrlich

> Hi, I plan to migrate core classes of an application from Python to C++
> using SWIG,

if you're using SWIG, you may want the numpy.i SWIG interface files,
they can be handy.

but I probably wouldn't use SWIG, unless:

  - you are already a SWIG master
  - you are wrapping a substantial library that will use a lot of the
same constructs (i.e can re-use the same *.i files a lot)
  - you want to use SWIG to wrap the same library for multipel languages.

> The application's core classes will create the ndarrays and make
> calculations. The user interface (Python) finally receives it. C++ OOP
> features will be deployed.
> What general ways to work with NumPy ndarrays in C++ are here?

I'd take a good look at Cython -- while not all that mature for C++,
it does support the basics, and makes the transition between C/C++ and
python very smooth -- and handles ndarrays out of the box.

If your code ony  needs to be driven by Python (and not used as a C++
lib on its own), I'd tend to:

 - create your ndarrays in Python or Cython.
 - write your C++ to work with "bare" pointers -- i.e C arrays.

(also take a look at the new Cython memory views" -- they me your best bet.)

It would be nice to have a clean C++ wrapper around ndarrays, but that
doesn't exist yet (is there a good reason for that?)

you could also probably get one of the C++ array libs to work well, if
it shares a memory model with ndarray (which is likely, at least in
special cases:

  - Blitz++
  - ublas
  - others???

> I know of
> boost.python so far.

I've never used boost.python, but it's always seemed to me to be kind
of heavy weight and not all that well maintained [1]

 -- but don't take my word for it!

(there are boost arrays that may be useful)


[1] from what seems to be the most recent docs:


Provides access to the array types of Numerical Python's Numeric and
NumArray modules.

The days of Numeric an Numarray are long gone! It may only be the docs
that are that our of date, but....


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the NumPy-Discussion mailing list