[Numpy-discussion] access ndarray in C++
Christopher Barker
Chris.Barker at noaa.gov
Wed Apr 23 15:08:40 EDT 2008
Thomas Hrabe wrote:
> One can find multiple approaches to arrays, the old numeric array and
> the new ndarray. The new stuff seems to be backward compatible.
mostly, yes. numpy (ndarray) is the only option going forward, as it's
the only one being maintained.
> However, where is a free documentation of the ndarray?
You are right, the most complete set is Travis' book. It's a pretty good
deal when you consider that he wrote much of numpy -- if it saves you an
hour, it's worth the money!
That being said, I think you were pointed to:
http://projects.scipy.org/scipy/numpy/wiki/NumPyCAPI
Which should get you going, though it does seem to be missing your key
question:
> Which headers do I have to use in order to access the ndarray object
> that, in my eyes, should be compatible wit PyObject in some way?
#include <numpy/arrayobject.h>
You should use distutils to compile your extension, and in your
setup.py, you can put:
include_dirs=[numpy.get_include()]
and the header should be found.
> run in C should look like this:
> PyObject run{
> PyObject* ndArray;
> PyArg_ParseTuple( argv, ???? ,ndArray);
I think you had that right before -- you were just using the wrong headers:
PyArg_ParseTuple(args, "O!",&PyArray_Type, &array)
> Is it possible to access the ndarray object like this?
yes, something like that certainly should work.
When you've got it worked out, please add it to the Wiki.
I'd poke around the wiki, and this mailing list archives, for more examples.
NOTE:
Most folks now think that the pain of writing extensions completely by
hand is not worth it -- it's just too easy to make reference counting
mistakes, etc. Most folks are now using one of:
Cython (or Pyrex)
SWIG
ctypes
For example, the SWIG typemaps distributed with numpy make it very easy
to pass a numpy array into a C function such as:
void MyFunc(int i, int j, int k, double *arr)
where *arr is a pointer to a block of doubles that represent an (i,j,k)
3-d array.
Equally easy methods are available with Cython and ctypes.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
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
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list