Details about pythons set implementation

Arnaud Delobelle arnodel at googlemail.com
Fri Jan 4 18:12:47 EST 2008


On Jan 4, 10:15 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 04 Jan 2008 09:29:50 -0800, bukzor wrote:
> > Why cant you implement < for complex numbers? Maybe I'm being naive, but
> > isn't this the normal definition?
> >     a + bi < c + di iff sqrt(a**2 + b**2) < sqrt(c**2, d**2)
>
> No, it is not.

Mathematically speaking, it is an order, and a very useful one.  Only
it's a partial order, which means that if a != b then they are not
necessarily ordered (e.g. 1 and i).  This is not ideal for sorting.
What you need is a total order, where any two distinct elements are
comparable.

> Ordered comparisons are not defined for complex numbers.

You can't define a total order which is compatible with addition and
multiplication, but this restriction is not required to implement sets
of complex numbers.  Any total order will do, the easiest to pick
being lexicographical as already pointed out, i.e.

a+bi < c + di iff a < c or a == c and b < d

> Which is bigger, 4+2j or 2+4j?

4+2j, lexicographically!

--
Arnaud




More information about the Python-list mailing list