On 24/08/2008, at 6:15 AM, William Stein wrote:

>
> On Sat, Aug 23, 2008 at 1:00 PM, Fredrik Johansson
> <[EMAIL PROTECTED]> wrote:
>>
>> On Sat, Aug 23, 2008 at 9:57 PM, Nils Bruin <[EMAIL PROTECTED]> wrote:
>>>
>>> Would it break Python too much if comparison would simply throw an
>>> exception in these cases?
>>
>> Hardly, considering that this is what Python itself does:
>>
>>>>> 1+1j > 1-1j
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: no ordering relation is defined for complex numbers
>>
>> Fredrik
>
> Note by the way that this has consequences:
>
>>>> v = [1+1j , 1-1j]
> sage: v.sort()
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent  
> call last)
>
> /Users/was/<ipython console> in <module>()
>
> TypeError: no ordering relation is defined for complex numbers
>
> However, these aren't too hard to deal with:
>
>>>> v.sort( lambda x,y: cmp(str(x),str(y)) )
>>>> v
> [(1+1j), (1-1j)]
>
> So, for places in our code -- e.g., roots of polynomials -- where
> we want the result "sorted" for consistency of output, we could
> just have a bunch of "pseudo comparison" functions.

I think what you are suggesting is roughly how Mathematica does it.   
Details might help:

Mathematica provides two functions, "Less" and "Order".  "<" is an  
infix notation for Less.

1+i < 2+i throws an exception ("invalid comparison with 1+i  
attempted").  Order[a, b] is the same idea as python's cmp, except  
that Order doesn't use "<", but some arbitrary ("canonical")  
ordering.  (To avoid weirdness, small numbers precede bigger numbers).

Sort[ list ] uses Order to perform the sort.  This always works.  List  
can contain any thing, including numbers, plots, etc, and they will  
always come sorted in the same order.

Sort[ list, fn ] uses fn to perform the sort.  If you provide fn =  
Less, and list contains complex numbers, exceptions are thrown.

Order is generally a pretty useful basic function, it would be  
worthwhile providing something like it.

D




--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to