[bug #68605] Signed 0 affects sort result in sparse complex arrays

"Dmitri A. Sergatskov" <[email protected]>
Newsgroups gmane.comp.gnu.octave.bugs
Message-ID <[email protected]>
Please use the bug tracker to post updates to a bug report.  The mailing list is intended as a read-only notification stream.  Info posted to this mailing list address won't appear in the tracker database where it is most useful.

URL:
  <https://savannah.gnu.org/bugs/?68605>

                 Summary: Signed 0 affects sort result in sparse complex
arrays
                   Group: GNU Octave
               Submitter: dasergatskov
               Submitted: Mon 10 Aug 2026 01:18:21 PM UTC
                Category: Octave Function
                Severity: 3 - Normal
                Priority: 5 - Normal
              Item Group: Incorrect Result
                  Status: None
             Assigned to: None
         Originator Name:
        Originator Email:
             Open/Closed: Open
         Discussion Lock: Unlocked
                 Release: dev
        Operating System: Any
           Fixed Release: None
         Planned Release: None


    _______________________________________________________

Follow-up Comments:


-------------------------------------------------------
Date: Mon 10 Aug 2026 01:18:21 PM UTC By: Dmitri A. Sergatskov <dasergatskov>

octave:1> z = [complex(0, 2), complex(-2, -0.0)]
z =

   0 + 2i  -2 - 0i

octave:2> [y, idx] = sort(z) 
y =

   0 + 2i  -2 - 0i

idx =

   1   2

octave:3> [y_s, idx_s] = sort(sparse(z)) 
y_s =

Compressed Column Sparse (rows = 1, cols = 2, nnz = 2 [100%])

  (1, 1) -> -2 - 0i
  (1, 2) ->  0 + 2i

idx_s =

   2   1

octave:4> z1 = [complex(0, 2), complex(-2, 0.0)]
z1 =

   0 + 2i  -2 + 0i

octave:5> [y, idx] = sort(z1) 
y =

   0 + 2i  -2 + 0i

idx =

   1   2

octave:6> [y_s, idx_s] = sort(sparse(z1)) 
y_s =

Compressed Column Sparse (rows = 1, cols = 2, nnz = 2 [100%])

  (1, 1) ->  0 + 2i
  (1, 2) -> -2 + 0i

idx_s =

   1   2


In `liboctave/util/oct-cmplx.cc`:

32 // For complex-complex and complex-real comparisons, Octave uses the
following
 33 // ordering: compare absolute values first; if they match, compare phase
 34 // angles.  This is partially inconsistent with M*b, which compares
complex
 35 // numbers only by their real parts; OTOH, it uses the same definition
for
 36 // max/min and sort.  The abs/arg comparison is definitely more useful
(the
 37 // other one is emulated rather trivially), so let's be consistent and use
that
 38 // all over.
 39 
 40 // The standard C library function arg() returns [-pi,pi], which creates
a
 41 // non-unique representation for numbers along the negative real axis
branch
 42 // cut.  Change this to principal value (-pi,pi] by mapping -pi to pi.


The same should be done for sparse. 
I will provide a patch shortly.

Dmitri.
-- 









    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?68605>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
signature.asc (application/pgp-signature, 228 B)
-----BEGIN PGP SIGNATURE-----

iHUEABYIAB0WIQQk97aszIMMAvLLwm6qLAuaBUf3TgUCannPogAKCRCqLAuaBUf3
TuVsAQCgE24SIY+vOwJR31icMK8MHs034XKcUhc/AwvKQ24QHgD+ISzl+G3kBwmc
yByoxqEbnaWAvg6LADMrICgjTmbBPwU=
=QX86
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.