[Numpy-discussion] access ndarray in C++

Thomas Hrabe thrabe at burnham.org
Wed Apr 23 14:03:15 EDT 2008


Hi all,

first of all, thank you for the rich spectrum of answers, especially to Joris' sample code.

What I still do not understand is the following:

One can find multiple approaches to arrays, the old numeric array and the new ndarray.
The new stuff seems to be backward compatible. However, where is a free documentation of the ndarray?

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?
I find definitions of the old array types, but none for the ndarray.
I tend to give Joris code a try; but, however, the whole situation confueses me.
Joris code (the ndarray.h) does not include any numpy object definition for instance.

All I want to do is the following:

>a = numpy.array([1,1,1])
>a.__class__
  <type 'numpy.ndarray'>


mymod.run(a )

run in C should look like this:
PyObject run{
PyObject* ndArray;
PyArg_ParseTuple( argv, ???? ,ndArray);

for(x axis)
 for(y axis)
  do something with ndArray.data[x][y];

return something.
}

Is it possible to access the ndarray object like this? I do not want to create new array objects right now, later maybe. 
There must be a predefined way to access the data in C without using a custom hack.

Finally, my primary aim is to access 3dimensional arrays of floats in C.

Best,
Thomas


-----Original Message-----
From: numpy-discussion-bounces at scipy.org on behalf of Thomas Hrabe
Sent: Tue 4/22/2008 2:38 PM
To: numpy-discussion at scipy.org
Subject: [Numpy-discussion] access ndarray in C++
 
Hi all!

I am currently developing a python module in C/C++ which is supposed to access nd arrays as defined by the following command in python

a = numpy.array([1,1,1])

I want to access the array the following way and use the nd array data for further processing in C.

mymod.doSthg(a)

The example code on 
http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm#pgfId-36721

 (!PyArg_ParseTuple(args, "O!",&PyArray_Type, &array))

does not work for nd arrays. I always get 
TypeError: argument 1 must be array, not numpy.ndarray

I assume the error is the constant provided as the third parameter, saying that the imput is of PyArray_Type and no nd array.

So here are my questions:
1. is there any good tutorial / example code for acessing nd arrays in C?
2. what is the difference between both (arrays and nd arrays? - I am new to python and heard there were different approaches to arrays and that nd arrays work better for multi dimensional applications. Is this right?)
3. which one of both will be used in the future?

Thank you in advance for your help,
Thomas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080423/bdf9b6b3/attachment.html>


More information about the NumPy-Discussion mailing list