[PYTHON MATRIX-SIG] Slices

Konrad HINSEN hinsenk@ere.umontreal.ca
Wed, 3 Apr 1996 13:49:51 -0500


> > Another idea: introduce an object "index_expression" that can be
> > indexed to get an equivalent expression. Then
> >    i = index_expression[2:4,...]
> >    a[i]
> > would be equivalent to
> >    a[2:4,...]
> 
> Very clever.  The index_expression object would simply package all its
> arguments and return them as a Slice() object.  Way to go!

I actually realized that the implementation is completely trivial:

   class _index_expression_class:

       def __getitem__(self, item):
	   return item

   index_expression = _index_expression_class()


Now let's try it:

   from Numeric import *

   a = array([[1,2,3],[6,5,4]])

   i = index_expression[..., 1:3]

   print i
   print a[i]

Output:

   (..., Slice(1, 3, 1))
   2 3
   5 4

It works! Let's just put that into Numeric and be happy...

-------------------------------------------------------------------------------
Konrad Hinsen                     | E-Mail: hinsenk@ere.umontreal.ca
Departement de chimie             | Tel.: +1-514-343-6111 ext. 3953
Universite de Montreal            | Fax:  +1-514-343-7586
C.P. 6128, succ. Centre-Ville     | Deutsch/Esperanto/English/Nederlands/
Montreal (QC) H3C 3J7             | Francais (phase experimentale)
-------------------------------------------------------------------------------

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

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