[PYTHON MATRIX-SIG] Pseudo Indices

James Hugunin jjh@Goldilocks.LCS.MIT.EDU
Tue, 30 Jan 96 13:34:54 EST


   From: Guido van Rossum <guido@CNRI.Reston.VA.US>

   > ie. b[2, PseudoIndex] <--> b.reshape(2, 1)

   I'm not sure I understand reshape completely, but isn't this a rather
   drastic reinterpretation of the index arguments when a pseudo index is
   present?  In general, if no pseudo index is present, single indices
   take away that dimension, don't they?  (While slice indices can reduce
   the length in that dimension but otherwise leave it intact.)  But in
   your example, the '2' becomes a shorthand for a slice.  I don't like
   this very much...

I'm sorry, that was a type-o on my part.  What I should have said was
b[All, PseudoIndex] <--> b.reshape(2,1).

   > I'd really like to be able to say c[:, 3] -> array([3,13]).  Python
   > syntax doesn't currently allow this, so until somebody convinces Guido
   > to make the change, the following objects are used instead.

   Same comment -- I don't like the '3' index to turn into a slice by
   context, if I understand this right.  (Bear with me, I'm not even sure
   that array([3, 13]) really is a 3x13 matrix.)

Here you are simply not understanding my (admittedly poor)
description.  array([3,13]) is a one dimensional array with a shape of
[2] which contains the numeric elements 3 and 13.  So the 3 index is
not returning a slice by context.  The returned array has a dimension
one less that the initial array because it has the index 3 instead of
a slice.

   > c[:, 1:2] --> c[Slice(), Slice(1,2)]

   This is fine (and what I thought you were proposing all the time).  I
   would consider a patch if the existing 1-dim sequence and mapping
   types are not affected.

Great!  I'll get on with the process of coordinating this patch.

Currently the best way we've come up to implement this patch is to add
a new object type to python called a sliceobject.  c[:, 1:2] then gets
converted to c[ (slice(), slice(1,2)) ] in the interpreter and then
passed to the standard getitem mapping method.  Special consideration
is payed to c[:] so as not to break existing code.  

I'm a little bit troubled by the need to add a new object (admittedly
extremely small) just to get this behavior.  Does this solution
seem reasonable to you?

The only other approach I can think of is to add a new protocol for
multidimensional arrays, on equal footing with number, mapping, and
sequence.  This seems like it would be overkill.

   > a[RubberIndex, 3].

   You could sneak this into the abovementioned patch as "a[::, 3]".

Slices have an additional feature which is a third index specifying
a stride.  ie. Slice(None,None,2) will take all the even elements
along a dimension.  Slice(None,None,-1) will reverse a dimension.
This arrangement corresponds very nicely with the internals of array
objects.

Unfortunately, this means that things like ::-1 will be reasonably
common, and so using :: as a RubberIndex might be confusing.

However, I do like this syntax for the RubberIndex as it really
captures what it does which is to apply : to every unused dimension.
Maybe there's a similar proposal that will work.

   > (until somebody lobbys Guido for a better system).

   Ain't I nice today? ;-)

Gee, now that we've got Guido in such a friendly mood, what else
should we ask for?  A new operator just for matrix multiplication? ;)

-Jim

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

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