[MATRIX-SIG] cmp?

Timothy A. Hochberg hochberg@wwa.com
Tue, 17 Jun 1997 10:13:51 -0500 (CDT)




On Tue, 17 Jun 1997, Aaron Watters wrote:

> Thanks for the response
> 
[I give unsatisfactory solution]
[AW says noone understands him]
> I suppose for my immediate purpose I'd like to compare arrays
> any way so that I could use the list.sort builtin on a list of arrays
> and get all equal arrays grouped together. [...]

How about this (barely tested) code:

from Numeric import *

xx = array([[1, 1, 1, 0, 0, 0, 2, 2, 2],
	    [1, 2, 3, 1, 2, 3, 1, 2, 3],
	    [1, 6, 3, 1, 2, 3, 1, 2, 3],
	    [1, 2, 3, 1, 3, 3, 1, 2, 3]])


def aw_sort(A, axis=0):
    """Sort a 2d array along axis."""
    A = asarray(A)
    if axis: A = transpose(A)
    S = shape(A)
    if len(S) != 2: 
	raise ValueError, "array must be 2D."
    for i in range(S[1]-1, -1, -1):
	A = take(A, argsort(A[:,i]), 0)
    if axis: A = transpose(A)
    return A


if __name__ == '__main__':
    print aw_sort(xx)
    print 
    print aw_sort(xx, 1)

It should give you lexicographic sorting on 2d arrays. Extending it to
arrays of arbitrary dimension is left as an exercise to the reader. How
efficient it is, I don't know.

> Is there any problem with lexicographic ordering?  I understand
> that cmp() may have no reasonable interpretation from a
> numeric perspective per se, but you'd still like to be able to do
> "row sorts".

If I recall correctly, there was some concern with doing something that
would be regretted later if functionality  for __less__, etc. ever gets
added to Python.

> >From a database perspective I'd like to implement the
> analogue of SQL "group by", ie, as in
> 
> select a,b, avg(d+e)
> from array1
> group by a,b

I think I understand this, but I'm a database uninitiate...

> The "natural" way to do this would be to
> align the rows of array1 in a list of [a,b,d,e]
> and then sort the list lexicographically to
> group equal a,b pairs together.  At least *I*
> think it would be natural.  -- Aaron Watters

Sounds good to me.

> ps: alternatively if there were some way
>   to get partitions of indices where a and b
>   were equal this might do just as well in
>   this instance.  I can't figure out how to do
>   this efficiently for all pairings of a,b at once
>   (maybe my ignorance).

I can't think of one either. Try aw_sort - let me know if it works for
you.

 ____   
  /im  

+------------------------------------------------+
|Tim Hochberg            Research Assistant      |
|hochberg <at> wwa.com   University of Illinois  |
|                        Electrical Engineering  |
+------------------------------------------------+



_______________
MATRIX-SIG  - SIG on Matrix Math for Python

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