[MATRIX-SIG] cmp?

Timothy A. Hochberg hochberg@wwa.com
Mon, 16 Jun 1997 21:16:58 -0500 (CDT)



On Mon, 16 Jun 1997, Aaron Watters wrote:

> I'm certain there are good reasons
> why array cmp works the way it does,
> please educate me.
> 
> >>> cmp(xx[0],xx[1])
> -1
> >>> cmp(xx[1],xx[0])
> -1
> >>> xx
> array([[1, 1, 1, 0, 0, 0, 2, 2, 2],
>        [1, 2, 3, 1, 2, 3, 1, 2, 3]])
> 
> what's the rationale here?  I want to sort arrays.
> If I must write my own cmp function, it'll be really
> really slow.

I believe that what we would like to do here is a) return an array that
compares cmp element by element, or b) raise an exception. Since these
are(were) not possible, Jim H. did c) punt. (I believe I heard that 1.5 is
gonna let exceptions be raised in in comparisons, so maybe this will be
"fixed".)

How do you want to compare your arrays? If you just want to compare them
like tuples you could use:
> >>> cmp(tuple(xx[0]),tuple(xx[1]))
If you want to compare the sums of the elements, you could use:

cmp(sum(xx[0]), sum(xx[1]))

or the product of the absolute values of the cosines:

cmp(product(abs(cos(xx[0])), product(abs(cos(xx[1]))))

or if you want to compare element be element look at less, less_equal,
etc.

> Also
> 
> >>> sort(xx)
> array([[0, 0, 0, 1, 1, 1, 2, 2, 2],
>        [1, 1, 1, 2, 2, 2, 3, 3, 3]])
> 
> Each sub array is sorted individually.  It seems
> to me this should be written
> 
> array(map(sort, xx))
> 
> and in the former the subarrays should be sorted 
> lexicographically as Python sorts tuples and lists.
> 
> Please explain. I'm missing something here.
> Sorry if I'm being stupid again.

Hmmm...again, if you want to sort them lexigraphically like tuples and
lists, you could convert them to tuples and lists and sort them then
convert them back. The numeric functions all operate pretty much the same
way - they operate on the smallest chunk of the matrix that they can and
they do it over and over again. Since that's amazingly unclear, let me try
some examples:

cos(xx) # applies cos to every element of the matrix.
sort(xx) # sorts every sub vector of xx along axis -1 (=1)
sort(xx,0) # sorts every sub vector of xx along axis 0
sum(xx,1) # sum every sub vector along axis 1 - returns array([ 9 18])
determinant(xx) # wouldn't work with your xx, but gets the determinant of
                # every sub matrix of xx (imagine xx was 2x3x3)

Now again how do you want to sort your array. If you want to sort
according to some numeric criteria other than lexigraphic ordering, I
would a) construct a sort key using sum, abs, +, *, etc, b) use argsort to
determine the sort ordering, then use take to get the sorted matrix.  So,
if I wanted to sort the columns of xx by sum I'd use: 

take(xx, argsort(sum(xx)), 1)

Hope that's more enlightening than confusing....


 ____   
  /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
_______________