[Numpy-discussion] numpy c api general array handling

Yoshi Rokuko yoshi at rokuko.net
Wed Jun 8 03:58:10 EDT 2011


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



More information about the NumPy-Discussion mailing list