BUG DESCRIPTION:
----------------
Presently, gsort() sorts complex numbers only according to their moduli.
As a consequence, the sorting of complex numbers is not complete.
For instance sorting [-%i %i] and [%i -%i] returns two distinct results:
--> gsort([%i -%i])
ans =
i -i
--> gsort([-%i %i])
ans =
-i i
This prevents using gsort() with complex numbers for many operations.
For instance, the bug 15734 and the bug 15737 are due to this limitation.
PROPOSAL
--------
* It should become possible to choose the main criterion according to which complex numbers are sorted.
Proposed criteria are the following:
- "abs" : to first sort according to moduli. For back-compatibility, this should stay the default.
- "phase": to first sort according to the phase, computed on ]-pi, pi]
- "phase+": to first sort according to the phase, computed on [0, 2.pi[
- "real" : to first sort according to real parts
- "imag" : to first sort according to imaginary parts
This choice could be done through one of the above key strings provided in a new fourth option xCriteria.
* It should become possible to choose a secondary sorting criterion, to sort subsets that have the same
value wrt the first criterion. This choice could be done by providing 2 keys in xCriteria instead
of only one. For instance, xCriteria = ["phase+" "abs"]. This will enable complete sorting.
* Then, we have to provide a way to specify the sorting direction for each of both sorting criteria.
In addition to the "i" and "d" flags, other values "ii", "id", "di" and "dd" could be implemented.
For instance, "id" means that numbers must be sorted in Increasing order wrt the first criterion,
and in Decreasing order wrt the secondary criterion.
If "i" or "d" is specified, then only the first criterion xCriteria(1) is used.
For back-compatibility, "d" is the default.
All the existing methods "g" "r" "c" "lr" "lc" must support these new sorting features of complex numbers.
Examples of syntaxes:
--------------------
gsort(C, "g", "i", "phase+")
gsort(C, "g", "ii", ["abs" "phase+"])
gsort(C, "g", "id", ["real" "imag"])
gsort(C, "r", "d", "real")
gsort(C, "r", "di", ["imag" "real"])
gsort(C, "lc", "d", "real")
gsort(C, "lc", "di", ["abs" "phase"])
Remarks
-------
* The phase of (0,0) is set to 0, as returned by atan(0,0).
* Please note that using a secondary sorting criterion may still leave some input numbers not completely sorted.
For instance, xCriteria=["imag" "phase"] will leave all positive reals in their initial order.
Indeed, whatever are their values, they all have a null imaginary part and a null phase.