1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) DIGITEO - 2009 - Allan CORNET
3 // Copyrifht (C) 2012 - Scilab Enterprises - Adeline CARNIS
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 // Copyright (C) 2018 - 2020 - Samuel GOUGEON : complete rewritting
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
14 function [A, k] = %sp_gsort(A, sortype, sortdir, criteria)
18 // ===================
19 // CHECKING PARAMETERS
20 // ===================
21 if ~isdef("sortype", "l") then
24 sortype = convstr(sortype(1))
25 if ~or(sortype == ["g", "r", "c", "lr", "lc"]) then
26 msg = _("%s: Argument #%d: Must be in the set {%s}.\n")
27 error(msprintf(msg, "gsort", 2, """g"",""r"",""c"",""lr"",""lc"""))
30 if ~isdef("sortdir", "l")
33 sortdir = convstr(sortdir)
34 if and(sortdir <> "d" & sortdir <> "i") then
35 msg = _("%s: Argument #%d: Must be in the set {%s}.\n")
36 error(msprintf(msg, "gsort", 3, """i"",""d"""))
38 if ~isdef("criteria","l")
49 // Gets non zero values by increasing linearized indices:
50 [ij, v, mn] = spget(A.');
56 // ------------------------
57 // "g" general sorting mode
58 // ------------------------
60 v($+1) = 0 // To get the position of all sorted implicit zeros
61 ij($+1,:) = [mn(1)+1 1] // (the value does not matter)
63 // Sorting non zero values:
65 if ~isdef("criteria", "l")
66 v = gsort(v, "g", sortdir)
68 v = gsort(v, "g", sortdir, criteria)
71 if ~isdef("criteria", "l")
72 [v, ks] = gsort(v, "g", sortdir)
74 [v, ks] = gsort(v, "g", sortdir, criteria)
78 kz = find(v==0) // Here is the position of zeros
79 v(kz) = [] // Cleaning
80 K = [ 1:kz-1 s-length(v)+kz:s] // We build K
82 A = sparse(ind2sub(mn,K), v, mn) // We build the sorted sparse
83 // Building the dense matrix of initial indices of sorted elements
84 // A new "sparse_k" option could be implemented later to return a sparse k
88 k(K) = sub2ind(mn, ij(ks,:));
89 k(k==0) = find(Ain(:)==0)
90 k = matrix(k, size(A))
95 // -------------------------------------
96 // Sorting inside rows or inside columns
97 // -------------------------------------
98 if or(sortype==["r" "c"]) then // "r" sorts rows of each column
99 a = 2; // "c" sorts columns of each row
103 Kin = (1:mn(1))'*ones(1,mn(2))
107 if ~isdef("criteria", "l")
108 v = gsort(vec, "g", sortdir)
110 v = gsort(vec, "g", sortdir, criteria)
113 if ~isdef("criteria", "l")
114 [v, k] = gsort(vec, "g", sortdir)
116 [v, k] = gsort(vec, "g", sortdir, criteria)
127 A = sparse(K, V, mn);
138 // ---------------------
139 // Lexicographic sorting
140 // ---------------------
141 msg = _("%s: Argument #%d: Complex sparse not yet supported in ""%s"" mode.\n")
143 // Vector = special simple case
144 // ----------------------------
147 if (isRow & sortype=="lr") | (iscolumn(A) & sortype=="lc")
151 if ~isdef("criteria", "l")
152 A = gsort(A(:), "g", sortdir)
154 A = gsort(A(:), "g", sortdir, criteria)
157 if ~isdef("criteria", "l")
158 [A, k] = gsort(A(:), "g", sortdir)
160 [A, k] = gsort(A(:), "g", sortdir, criteria)
173 if sortype=="lc" then
176 if ~isdef("criteria", "l")
177 [A, k] = %sp_gsort_lr(A, sortdir);
179 [A, k] = %sp_gsort_lr(A, sortdir, criteria);
181 if sortype == "lc" then
187 // ===================================================================
189 function [S, K] = %sp_gsort_lr(S, order, criteria)
192 crit = isdef("criteria","l")
194 // List of column according to which sorting must be done
196 // We ignore columns that are uniform. Sorting them is useless
197 Std = sum(S.^2,"r")/nr - (sum(S,"r")/nr).^2
200 // Processing (bulky. A more clever algo required (but hard))
203 [?, k] = gsort(S(K, j), "g", order, criteria)
205 [?, k] = gsort(S(K, j), "g", order)