[Numpy-discussion] numpy c api general array handling

Christopher Barker Chris.Barker@noaa....
Wed Jun 8 11:57:01 CDT 2011


Mark Wiebe wrote:
> I believe what you may want is PyArray_ContiguousFromAny instead of 
> PyArray_ContiguousFromObject.

I would also strongly encourage you to take a look at Cython:

http://cython.org/

It has built-in support for numpy arrays, so it can take care of a lot 
of this bookkeeping for you.

http://wiki.cython.org/tutorials/numpy

-Chris



> Cheers,
> Mark
> 
> On Wed, Jun 8, 2011 at 2:58 AM, Yoshi Rokuko <yoshi@rokuko.net 
> <mailto:yoshi@rokuko.net>> wrote:
> 
>     hey,
> 
>     i'm writing my first python module in c using numpy/arrayobject.h
>     and i have some problems with different platforms (both linux but
>     really different setup) so i suspect my array handling is not cor-
>     rect.
> 
>     i'm used to c arrays and want to access large numpy arrays from
>     within my c module with no strange array-iter-methods. So what are
>     your remarks to the following:
> 
>       PyArg_ParseTuple(args, "OO|ii", &arg1, &arg2, &start, &stop);
>       index = (PyArrayObject *) PyArray_ContiguousFromObject(arg1,
>                                 PyArray_INT, 1, 1);
>       ix = (int *)index->data;
> 
>     then using like:
> 
>       for(k = ix[i]; k < ix[i+1]; k++) l = ix[k];
> 
>     this seems to work pretty well, but real problems come with:
>       PyArg_ParseTuple(args, "O", &arg);
>       adjc = (PyArrayObject *) PyArray_ContiguousFromObject(arg,
>                                PyArray_INT, 2, 2);
>       a = (int *)adjc->data;
> 
>     and then using like:
> 
>       aik = a[i + n * k];
> 
>     it seems like on one system it does not accept 2d numpy arrays just
>     1d ones or i must hand him a list of 1d numpy arrays like that:
> 
>       >>> A = np.array([[1,0],[0,1]])
>       >>> B = my.method(list(A))
> 
>     i would prefer to avoid the list() call in python.
> 
>     what are your remarks on performance would it be faster to do:
> 
>       PyArg_ParseTuple(args, "OO|ii", &arg1, &arg2, &start, &stop);
>       index = (PyArrayObject *) PyArray_ContiguousFromObject(arg1,
>                                 PyArray_INT, 1, 1);
>       ix = malloc(n * sizeof(int));
>       for(i = 0; i < n; i++)
>           ix[i] = (int *)index->data[i];
> 
>     and then use ix[] (i call a lot on ix).
> 
>     thank you and best regards, yoshi
>     _______________________________________________
>     NumPy-Discussion mailing list
>     NumPy-Discussion@scipy.org <mailto:NumPy-Discussion@scipy.org>
>     http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


-- 
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@noaa.gov


More information about the NumPy-Discussion mailing list