[MATRIX-SIG] reverse of take?

Michael McLay mclay@nist.gov
Wed, 2 Jul 1997 09:26:06 -0400


Andrew P. Mullhaupt writes:
 > I think you're confused about the meaning of this based on your previous
 > statements.
 > 
 > As to useful, it is perhaps the single most common case of indexing in
 > array languages, and not only that, perhaps the _most efficient_.
 > 
 > Here's an example:
 > 
 >     a = reshape(range(12), [3,4])
 > 
 > which looks like
 > 
 >      0  1  2  3
 >      4  5  6  7
 >      8  9 10 11
 > 
 > then you want
 > 
 >     a[[0,2], [2,3]]
 > 
 > to be
 > 
 >      2  3
 >     10 11
 > 
 > right? I think a lot of people have been thinking that this would be the
 > case (like APL, S, Matlab, etc.).
 > 
 > The question here is actually not _can_ you do this, (I can easily give a
 > good implementation of what I outlined so far). But we are not at that
 > point yet.

Just for fun I created a trivial implementation.  It only works for
2-d arrays and it requires making the index either a list of lists, or
a 2-d array.  A C implementation would obviously be needed and it
should be generalized to n-dimensional arrays. 

This notation does break the orthogonality in the use of sequences as
indexes, but is that really a problem?  This is already true for
dicitonary indexes.  Dictionaries can be indexed with tuples, but
dictionaries cannot be indexed with lists.  Perhaps for array type
objects it is also resaonable that indexing with different sequence
types can yield different results.

What is the arguement for making array[1,3,4] == array[[1,3,4]]


#!/usr/bin/env python 
from Numeric import *

class C:
    def __init__(self,arg):
	self.data = array(arg)
    def __getitem__(self, arg):
	if type(arg) == type([]) or type(arg) == type(array([])):
	    res = []
	    for x in arg[0]:
		res1 = []
		res.append(res1)
		for y in arg[1]:
		    res1.append( self.data[x,y])
	    return res
	    
	else:
	    return self.data[arg]

# this code just sets up the x.data inside the demonstration class.

a3x4 = reshape(arange(12),[3,4])

x = C(a3x4)

print x.data
print "\nUsing a gather syntax"
print x[[[0,2], [2,3]]]

print "\nNnow back to the existing indexing"
print x[2]

print "\nTuples still work as before"
print x[0,2]

a = array([[0,2], [2,3]])

print "\nThe gather syntax could also use arrays as an index"
print x[a]
print "\n using take"

dict = {}

dict [1,3,4 ] = "a tuple as an index"

print "dict indexing isn't orthogonal", 
print dict [1,3,4 ]

print "dict indexing with lists doesn't work"

dict [[1,3,4]] = "a list as an index"


_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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