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.
William
Did you read through the article Alfredo Portes posted, which
also explains some of the gotchas and subtleties of disallowing
comparisons? I'm curious what you thought of it.
> See also:
>
> http://trac.sagemath.org/sage_trac/ticket/3936
>
> >
>
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org
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
[...]
Just as another data point, since I happened to see it when reading
source code, in the Ginac library (http://www.ginac.de) we find this comment:
/** This method establishes a canonical order on all numbers. For complex
* numbers this is not possible in a mathematically consistent way
but we need
* to establish some order and it ought to be fast. So we simply define it
* to be compatible with our method csgn.
*
* @return csgn(*this-other)
* @see numeric::csgn() */
I'm not making or suggesting any conclusions -- just adding another data point.
William