[MATRIX-SIG] A proposal (LONG) - "reverse of take" revisited

Chase, Chris chase@att.com
Fri, 11 Jul 1997 20:34:56 -0400



> ----------
> From: 	Andrew P. Mullhaupt[SMTP:amullhau@ix.netcom.com]
> Sent: 	Friday, July 11, 1997 5:54 PM
> To: 	Chase, Chris; 'matrix-sig@python.org'
> Subject: 	RE: [MATRIX-SIG] A proposal (LONG) - "reverse of take"
> revisited
> 
> At 12:08 PM 7/11/97 -0400, Chase, Chris wrote:
> >
> >Slices that go outside array bounds are silently projected onto the
> >bounds while take() generates an error when the index vector goes
> >outside the bounds.  If arrays were allowed as indexes should they
> >behave like slices or like take()?
> >
> 
> Actually, _functions_ such as take should not be tied to the behavior
> of indices. 
> 
Sorry for the confusion, but that is not the question I was trying to
ask.  I was asking what should the behavior of arrays as indexes be with
respect to array bounds and I was illustrating two possible behaviors
now seen in NumPy.  Reasonable possible behaviors for arrays used as
indexes when the indexes are outside the array bounds:

1) project the indexes onto the bounds.  This takes the intersection of
the bounds and the index dropping individual indexes outside the bounds.
This is the type of behavior currently exhibited by slices.  The problem
with clipping is that you get back fewer elements than expected.


2) project the indexes by clipping.  This clips indexes outside the
bounds to the bounds.  For example, an index of [20] on a size 3 array
uses [2] as the index.  This is the way IDL array indexes behave (which
is incongruous with the errors that out-of-bounds scalar indices
generate in IDL).  For some applications this can be convenient while in
other situations you would prefer an error.

3) generate an error.  This is the behavior of take() when the index is
out of bounds.

> You can write all sorts of functions to do anything you
> want, so the user shouldn't have an expectation of a connection
> between
> index semantics and function semantics except for the _very few_
> necessary functions which explicitly involve the shape of the array.
> 
> So for consistency they can behave like slices, but I'm a bit
> surprised
> at how you claim slice indices work.
> 
> So I tried some examples and I think slice indices are broken. For
> example:
> 
> Python 1.4 (Feb 24 1997) [C]
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> a = reshape(range(12), [3, 4])
> >>> a[0:3, 0:4]
>  0  1  2  3
>  4  5  6  7
>  8  9 10 11
> >>> a[0:4, 0:4]
>  0  1  2  3
>  4  5  6  7
>  8  9 10 11
> 
The above is the slice clipping behavior.

> Python 1.4 (Feb 24 1997) [C]
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> a = reshape(range(12), [3, 4])
> >>> a = reshape(range(12), [3, 4])
> >>> a[3:0:-1, 4:0:-1]
> 1936654336    7103790 1328443513 1768843081
>          0         11         10          9
>          8          7          6          5
> 
This is indeed a bug in the slice clipping.  The bug is in arrayobject.c
in routine slice_GetIndices():

	if (*start < 0) *start = 0;
	else if (*start > length) *start = length;

should be something like:

	if (*start > (length-1)) *start = length-1;
	if (*start < 0) *start = 0;

Chris



_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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