[Numpy-discussion] RE: default axis for numarray

Pearu Peterson pearu at cens.ioc.ee
Wed Jun 12 08:55:03 EDT 2002

On Wed, 12 Jun 2002, Konrad Hinsen wrote:

> > How do you sort an array of complex numbers if you can't compare them?
> You could for example sort by real part first and by imaginary part
> second. That would be a well-defined sort order, but not a useful
> definition of comparison in the mathematical sense.

Releated discussion has been also in the scipy list. See the thread
starting in

But here I would like to draw your attention to the suggestion that
sort() function could take an optional argument that specifies the
comparison method for complex numbers (for real numbers they are all
equivalent). Here follows the releavant fragment of the message:

However, in different applications different conventions may be useful or
reasonable for ordering complex numbers. Whatever is the convention, their
mathematical correctness is irrelevant and this cannot be used as an
argument for prefering one convention to another.

I would propose providing number of efficient comparison methods for
complex (or any) numbers that users may use in sort functions as an
optional argument. For example,

scipy.sort([2,1+2j],cmpmth='abs') -> [1+2j,2]  # sorts by abs value
scipy.sort([2,1+2j],cmpmth='real') -> [2,1+2j] # sorts by real part
scipy.sort([2,1+2j],cmpmth='realimag') # sorts by real then by imag
scipy.sort([2,1+2j],cmpmth='imagreal') # sorts by imag then by real
scipy.sort([2,1+2j],cmpmth='absangle') # sorts by abs then by angle
scipy.sort([2,1+2j],cmpfunc=<user defined comparison function>)

Note that

scipy.sort([-1,1],cmpmth='absangle') -> [1,-1]

which also demonstrates the arbitrariness of sorting complex numbers.



More information about the NumPy-Discussion mailing list