[PYTHON MATRIX-SIG] newbie question: accessing array elements

Jim Hugunin hugunin@mit.edu
Thu, 19 Sep 1996 09:11:12 -0400


I'd really like to know what happened to cause this message to get bounced
to the list so many times.  Kyle, if you hear anything from your sysadmins
that suggests it might have happened on your end, please let me know.

Anyway...

There are three approachs to this problem.  The first two are easiest, but
involve copying on discontiguous arrays, and can not be used to modify an
arbitrary array in-place.

1) Use PyArray_As[1,2,3]D to get the data in the array into an appropriate
C pointer.  Then you can access elements as a[i][j][k].

2) Use PyArray_ContiguousFromObject and do your own math to determine the
location of any given element.

3) Define macros similar to the following:

#define Index1f(a, i) *(float *)(a->data + a->strides[0]*i)
#define Index2f(a, i, j) *(float *)(a->data + a->strides[0]*i +
a->strides[1]*j)
...

If enough people want macros like this and can agree on the best notation,
I'll add them to arrayobject.h

-Jim

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
=================