[PYTHON MATRIX-SIG] ARRAY COMPARISONS

tim@lassi.ece.uiuc.edu tim@lassi.ece.uiuc.edu
Fri, 25 Oct 1996 08:27:46 -0500


> = Alistair Mees
> > = Tim Hochberg

<...>

> > The problem is that __cmp__ can only validly return an integer. Notice
> > that for your first example that am array might be neither >=0 nor <0
> > ([1,-1] comes to mind), so there's really no valid value to return.

>That is not a problem.  The answer to "is [1,-1]>=0" is "no" so you return
>0, as you do for the comparisons [1,-1]<=0, [1,-1]>0 and [1,-1]<0.  These
>are standard elementary results!  Likewise, if we are working with
>symmetric matrices then A>=B and A<=B can both return 0 (since they are
>equivalent to A-B>=0 and A-B<=0, neither of which might be true if we
>decide to define the inequalities to be statements about positive
>semidefiniteness.

I don't entirely disagree that this is a reasonable solution in
principle, although I tend to think the correct answer to [-1,1]>=0 is
"undefined" and so you should get an exception. 

However, the comparison operators (<=, >, etc.) are just veneer on top
of __cmp__ (or tp_compare for C classes). If I do cmp(a,b) I get back
an integer: if its positive, >, >=, and != return true, if 0, ==, >=,
<= return true and if negative, <, <=, or != return true.

For cmp([1,-1],0) I want >, >=, <, and <= to raise an exception (or be
false for your semantics), I want == to be false, and != to be
true. What integer do you return for that? None ;) (at least in the
future - hopefully)

With the patch I'm working on, returning None in Python or INT_MIN
(sorry) in C will tell the veneer that the objects compared are not
equal but don't have an ordering. (This is in general, not just for
the Numerics extension.) This could be used to either raise an
exception if you try to order them or to return false for all the
ordering operators (<,<=,>,>=). However, I much prefer the former, if
you want to make questionable comparisons you should have to do it
explicitly!!


-- 
	-tim

+--------------------------------------------------------------------+
| Tim Hochberg               Ultrahigh Speed Digital Electronics Lab |
| tim@lassi.ece.uiuc.edu              University of Illinois         |
| http://dogbert.ece.uiuc.edu/~tim         (217) 333-6014            |
+--------------------------------------------------------------------+

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

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