[Numpy-discussion] access ndarray in C++

Robert Kern robert.kern@gmail....
Tue Apr 22 21:28:04 CDT 2008


On Tue, Apr 22, 2008 at 4:38 PM, Thomas Hrabe <thrabe@burnham.org> wrote:
>
> 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

That page refers to Numeric, numpy's old predecessor. If you #included
"Numeric/arrayobject.h" at the top of that C file, then the C
extension is expecting a Numeric array rather than a numpy one. Here
is more up-to-date documentation:

  http://projects.scipy.org/scipy/numpy/wiki/NumPyCAPI

>  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?)

There is a bit of terminological confusion. The Python standard
library has a module named "array" which provides homogeneous, typed
1D arrays. numpy provides a much richer array object (technically,
numpy.ndarray, but you usually construct one using the numpy.array()
function). If you have numpy, the standard library's array module is
of very little use; you will almost always want to use numpy.

This has nothing to do with the error you got above.

>  3. which one of both will be used in the future?

Well, the standard library's array module will continue to exist as
will numpy. Numeric, the likely cause of your problem above, is no
longer maintained, so please use numpy.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco


More information about the Numpy-discussion mailing list