[PYTHON MATRIX-SIG] ARRAY COMPARISONS

Chris Chase SRM chris.chase@jhuapl.edu
Fri, 25 Oct 1996 11:07:06 -0400


>>>>> "tim" == tim  <tim@lassi.ece.uiuc.edu> writes:

>> = Alistair Mees
>> > = Tim Hochberg

tim> <...>

>> > 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.

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

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

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

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

I agree with Tim's approach.  Raise an exception when two objects are
not comparable.

The implicit assumption in Python seems to be that cmp() implements a
_complete_ partial order relation [a relation that is reflexive,
transitive, and anti-symmetric, and comparable: a <= b or b <= a].

Supporting a partial order relation [dropping comparable property]
requires an exception be produced by cmp().

For arrays (finite dimensional vector spaces) the natural partial
order was mentioned by Alstair Mess ( a >= b iff a-b > 0
componentwise).  This partial order is an often used requirement in
many constrained optimization of convex functions.  Other partial
orders might be defined (e.g. Alstair mentioned positive semidefinite
for matrices).

Certain operations like sorting assume a complete order relation (or a
"chain" which is a completely ordered subset of a partial ordered
set).  Would it be a problem if an exception was generated inside
these kind of operations?

Chris

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

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