[Tutor] Complex numbers can't be ordered algebraically
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Apr 20 19:17:29 EDT 2004
On 19 Apr 2004, Lloyd Kvam wrote:
> The ordering of keys and values retrieved from a dictionary is
> undefined. There is no requirement that keys or values support an
> ordering. For instance, they could be complex numbers.
Hi Lloyd,
I just wanted to raise awareness of the fact that the complex numbers are
not "ordered" from a mathematical perspective. That is, they can't be
compared by using the less-than or greater-than operators that follow the
math definition of "order":
http://en.wikipedia.org/wiki/Ordered_field
###
>>> x = 3+4j
>>> y = 4+5j
>>> x < y
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot compare complex numbers using <, <=, >, >=
###
In this, Python follows what mathematicians have defined. But why? This
might seem weird --- why shouldn't y be 'greater' than x? All the numbers
are bigger!
But there's a strong math basis for the restriction. For an "ordered
field", the square of any number 'x' is nonnegative:
x**2 >= 0
This is a combination of "Positive times positive is positive" / "negative
times negative is positive" rules from elementary school. *grin*
Unfortunately, complex numbers violate this law:
###
>>> (-1j) * (-1j)
(-1-0j)
###
The conclusion we can make from this is that complex numbers aren't
compatible with the concept of algebraic order. So that's why the complex
numbers are excluded from being compared by '<', '<=', '>' or '>='.
Hope this helps!
More information about the Tutor
mailing list