[Numpy-discussion] Please help - pointer to slice

Fernando Perez fperez at colorado.edu
Fri Mar 7 18:01:04 CST 2003


Sebastian Haase wrote:
> Thanks, SO MUCH ...
> I almost went crazy yesterday - and was really desperate by the time I wrote
> that email.
> Somehow I newer needed that offset field until now.  So: when I do the
> NA_InputArray call  I get a "proper" C-array
> just that it does NOT necessarily start at  NAimg->data
> but rather at
> NAimg->data + NAimg->byteoffset

Well, in case you (or others) find it useful, I'm including here a little 
library I wrote for accessing general Numeric 2-d arrays (contiguous or not) 
in an easy manner.

Here's a snippet of a simple (included) example of a function to print an 
integer array:

static PyObject *idisp(PyObject *self, PyObject *args)
{
     PyArrayObject *array;
     int **arr;  // for the data area
     int i,j,cs;

     if (!PyArg_ParseTuple(args, "O!",&PyArray_Type,&array ) )
	return NULL;

     arr = imatrix_data(array,&cs);
     for (i=0;i<array->dimensions[0];++i) {
       for (j=0;j<cs*array->dimensions[1];j+=cs)
	printf("%5d ",arr[i][j]);
       printf("\n");
     }
     free(arr);

     Py_INCREF(Py_None);
     return Py_None;
}

You get the **arr pointer and you can then manipulate it as a[i][j] 
conveniently.  The supplied example file may be enough for many to write their 
Numpy C extensions without much trouble.

> Again: thanks so much.
> BTW: Is there general interest in my SWIG typemaps. (SWIG is maybe the
> easiest way to wrap C/C++ functions (and classes)
> into Python (and/or Perl, Java, Ruby,...) ?  I think especially for
> numerical stuff, that "link" in of interest.

I'd love to see them.  So far I've either used high-level stuff like 
weave.inline() or just written the extensions by hand (as in the code I'm 
supplying here).  I'd like to see this, especially if there is an easy way to 
handle contiguity issues with it.

Best,

f.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libnumpy-access.tgz
Type: application/x-gzip
Size: 4516 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20030307/9276fe1e/attachment.tgz 


More information about the Numpy-discussion mailing list