1function test19 2%TEST19 test cs_dmperm, cs_maxtransr, cs_dmspy, cs_scc, cspy 3% 4% Example: 5% test19 6% See also: testall 7 8% Copyright 2006-2012, Timothy A. Davis, http://www.suitesparse.com 9 10clear functions 11 12randn ('state', 0) ; 13rand ('state', 0) ; 14 15clf 16 17for trials = 1:100 18 19 m = fix (100 * rand (1)) ; 20 n = fix (100 * rand (1)) ; 21 % d = 0.1 * rand (1) ; 22 d = rand (1) * 4 * max (m,n) / max (m*n,1) ; 23 A = sprandn (m,n,d) ; 24 S = sprandn (m,m,d) + speye (m) ; 25 26 if (~ispc) 27 if (rand ( ) > .5) 28 A = A + 1i * sprand (A) ; 29 end 30 if (rand ( ) > .5) 31 S = S + 1i * sprand (S) ; 32 end 33 end 34 35 subplot (2,3,1) ; 36 cspy (A) ; 37 38 pp = dmperm (A) ; 39 40 sprnk = sum (pp > 0) ; 41 42 pp2 = cs_dmperm (A) ; 43 spr2 = sum (pp2 > 0) ; 44 if (spr2 ~= sprnk) 45 error ('!') 46 end 47 48 pp2 = cs_maxtransr (A) ; 49 spr2 = sum (pp2 > 0) ; 50 if (spr2 ~= sprnk) 51 error ('!') 52 end 53 54 [p,q,r,s] = dmperm (A) ; 55 C = A (p,q) ; 56 % r 57 % s 58 59 nk = length (r) - 1 ; 60 61 fprintf ('sprnk: %d m %d n %d nb: %d\n', sprnk, m, n, nk) ; 62 63 subplot (2,3,2) ; 64 hold off 65 spy (C) 66 hold on 67 68 for k = 1:nk 69 r1 = r(k) ; 70 r2 = r(k+1) ; 71 c1 = s(k) ; 72 c2 = s(k+1) ; 73 plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 74 end 75 76 [p2,q2,rr2,ss2,cp,rp] = cs_dmperm (A) ; 77 78 if (min (m,n) > 0) 79 if (length (rr2) ~= length (r)) 80 error ('# fine blocks!') ; 81 end 82 end 83 84 if (rp (4) - 1 ~= sprnk) 85 rp %#ok 86 sprnk %#ok 87 error ('!') ; 88 end 89 90 if (any (sort (p2) ~= 1:m)) 91 error ('p2!') ; 92 end 93 94 if (any (sort (q2) ~= 1:n)) 95 error ('q2!') ; 96 end 97 98 if (cp (5) ~= n+1) 99 error ('cp!') ; 100 end 101 102 if (rp (5) ~= m+1) 103 error ('rp!') ; 104 end 105 106 C = A (p2,q2) ; 107 108 subplot (2,3,3) ; cs_dmspy (A,0) ; 109 110 % hold off 111 % spy (C) ; 112 % hold on 113 114 % r1 = rp(1) ; 115 % r2 = rp(2) ; 116 % c1 = cp(1) ; 117 % c2 = cp(2) ; 118 % plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 119 120 r1 = rp(1) ; 121 r2 = rp(2) ; 122 c1 = cp(2) ; 123 c2 = cp(3) ; 124 % plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 125 126 B = C (r1:r2-1, c1:c2-1) ; 127 if (nnz (diag (B)) ~= size (B,1)) 128 error ('C1 diag!') ; 129 end 130 131 r1 = rp(2) ; 132 r2 = rp(3) ; 133 c1 = cp(3) ; 134 c2 = cp(4) ; 135 % plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'r') ; 136 137 B = C (r1:r2-1, c1:c2-1) ; 138 if (nnz (diag (B)) ~= size (B,1)) 139 error ('C2 diag!') ; 140 end 141 142 r1 = rp(3) ; 143 r2 = rp(4) ; 144 c1 = cp(4) ; 145 c2 = cp(5) ; 146 % plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 147 148 B = C (r1:r2-1, c1:c2-1) ; 149 if (nnz (diag (B)) ~= size (B,1)) 150 error ('C3 diag!') ; 151 end 152 153 r1 = rp(4) ; %#ok 154 r2 = rp(5) ; %#ok 155 c1 = cp(4) ; %#ok 156 c2 = cp(5) ; %#ok 157 % plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 158 159 if (~isempty (S)) 160 161 [p1,q1,r0,s0] = dmperm (S) ; 162 [p3,r3] = cs_scc (S) ; 163 if (length (r3) ~= length (r0)) 164 error ('scc size!') ; 165 end 166 167 if (any (sort (p3) ~= 1:m)) 168 error ('scc perm!') ; 169 end 170 171 nk = length (r0)-1 ; 172 173 subplot (2,3,4) ; 174 hold off 175 spy (S (p1,q1)) ; 176 hold on 177 for k = 1:nk 178 r1 = r0(k) ; 179 r2 = r0(k+1) ; 180 c1 = s0(k) ; 181 c2 = s0(k+1) ; 182 plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 183 end 184 185 subplot (2,3,5) ; 186 hold off 187 spy (S (p3,p3)) ; 188 hold on 189 for k = 1:nk 190 r1 = r3(k) ; 191 r2 = r3(k+1) ; 192 c1 = r3(k) ; 193 c2 = r3(k+1) ; 194 plot ([c1 c2 c2 c1 c1]-.5, [r1 r1 r2 r2 r1]-.5, 'g') ; 195 end 196 197 end 198 199 subplot (2,3,6) ; 200 cs_dmspy (A) ; 201 drawnow 202 % pause 203 204 205 206 207end 208