[Numpy-discussion] Complex comparisions

Rick White rlw at stsci.edu
Wed Jun 12 13:25:02 EDT 2002

On 12 Jun 2002, Travis Oliphant wrote:

> I'd be interested to know what IDL does?  Does it compare complex
> numbers.

Well, that was an interesting question with a surprising answer (at least
to me, a long-time IDL user):

(1) IDL allows comparisons of complex number using equality and
    inequality, but attempts to compare using GT, LT, etc. cause an
    illegal exception.

(2) IDL sorts complex numbers by the amplitude.  It ignores the phase.
    Numbers with the same amplitude and different phases are randomly
    ordered depending on their positions in the original array.

> Matlab allows comparisons of complex numbers but just compares the real
> part.  I think this is reasonable.  Often during a calculation of
> limited precision one ends up with a complex number when the result is
> in a "mathematically pure sense" real.

So neither IDL nor Matlab has what I consider the desirable feature
that the sort order be unique at least to the extent that equal values
wind up next to each other in the sorted array.  (Sorting by real value
and then, for equal real values, by imaginary value would accomplish
that.)  Since complex numbers can't be fully ordered there is no single
comparison function that can be plugged into a standard sort algorithm
and give that result -- it would require a special complex sort algorithm.

I guess if neither of the major array processing systems (that I know
about) have this property in their complex sorts, it must not be *that*
important.  And since I've been using IDL for 13 years without discovering
that complex greater-than comparisons are illegal, I guess that must not
be an important property either (at least to me :-).

My conclusion now is similar to Paul Dubois's suggestion -- we should
allow equality comparisons and sorting.  Beyond that I guess whatever
other people want should carry the day, since it clearly doesn't matter
to the sorts of things that I do with Numeric!

More information about the NumPy-Discussion mailing list