Mercurial > octave-nkf
comparison test/diag-perm.tst @ 16030:1af8d21608b7
rename all test files in the test directory from test_X.m to X.tst
* Use - instead of _ for .tst file names. Fix all file lists in
module.mk and Makefile.am files.
* __run_test_suite__.m: Adapt to new naming convention.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 09 Feb 2013 21:35:55 -0500 |
parents | test/test_diag_perm.m@72c96de7a403 |
children | d63878346099 |
comparison
equal
deleted
inserted
replaced
16029:b8157404614f | 16030:1af8d21608b7 |
---|---|
1 ## Copyright (C) 2009-2012 E. Jason Riedy | |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ######################################## | |
20 ## Permutation matrices | |
21 | |
22 ## row permutation | |
23 %!test | |
24 %! n = 5; | |
25 %! A = rand (n); | |
26 %! perm = randperm (n); | |
27 %! Prow = eye (n) (perm, :); | |
28 %! assert (A(perm, :), Prow * A); | |
29 %! invperm(perm) = 1:n; | |
30 %! assert (Prow \ A, A(invperm, :)); | |
31 %! assert (Prow' * A, A(invperm, :)); | |
32 | |
33 ## column permutation | |
34 %!test | |
35 %! n = 7; | |
36 %! A = rand (n); | |
37 %! perm = randperm (n); | |
38 %! Pcol = eye (n) (:, perm); | |
39 %! assert (A(:, perm), A * Pcol); | |
40 %! invperm(perm) = 1:n; | |
41 %! assert (A / Pcol, A(:, invperm)); | |
42 %! assert (A * Pcol.', A(:, invperm)); | |
43 | |
44 ## fall back to a matrix in addition | |
45 %!test | |
46 %! n = 4; | |
47 %! P1 = eye (n) (:, randperm (n)); | |
48 %! A = zeros (n) + P1; | |
49 %! assert (sum (A), full (ones (1, n))); | |
50 %! assert (sum (A, 2), full (ones (n, 1))); | |
51 | |
52 ## preserve dense matrix structure | |
53 %!test | |
54 %! n = 7; | |
55 %! Pc = eye (n) (:, randperm (n)); | |
56 %! Pr = eye (n) (randperm (n), :); | |
57 %! assert (typeinfo (rand (n) * Pc), "matrix"); | |
58 %! assert (typeinfo (Pr * rand (n)), "matrix"); | |
59 | |
60 ## preserve sparse matrix structure | |
61 %!test | |
62 %! n = 7; | |
63 %! Pc = eye (n) (:, randperm (n)); | |
64 %! Ac = sprand (n-3, n, .5) + I () * sprand (n-3, n, .5); | |
65 %! Pr = eye (n) (randperm (n), :); | |
66 %! Ar = sprand (n, n+2, .5); | |
67 %! assert (typeinfo (Ac * Pc), "sparse complex matrix"); | |
68 %! assert (full (Ac * Pc), full (Ac) * Pc); | |
69 %! assert (full (Ac / Pc), full (Ac) / Pc); | |
70 %! assert (typeinfo (Pr * Ar), "sparse matrix"); | |
71 %! assert (full (Pr * Ar), Pr * full (Ar)); | |
72 %! assert (full (Pr \ Ar), Pr \ full (Ar)); | |
73 | |
74 ## structure rules for 1x1 dense / scalar and 1x1 perm | |
75 %!test | |
76 %! n = 7; | |
77 %! P1 = eye (1) (:, [1]); | |
78 %! A1 = 1; | |
79 %! P = eye (n) (:, randperm (n)); | |
80 %! A = rand (n-3, n, .5); | |
81 %! assert (typeinfo (A * P1), "matrix"); | |
82 %! assert (full (A * P1), full (A) * P1); | |
83 %! assert (typeinfo (P1 * A), "matrix"); | |
84 %! assert (full (P1 * A), P1 * full (A)); | |
85 %! assert (typeinfo (A1 * P), "matrix"); | |
86 %! assert (full (A1 * P), full (A1) * P); | |
87 %! assert (typeinfo (P * A1), "matrix"); | |
88 %! assert (full (P * A1), P * full (A1)); | |
89 | |
90 ## structure rules for 1x1 sparse and 1x1 perm | |
91 %!test | |
92 %! n = 7; | |
93 %! P1 = eye (1) (:, [1]); | |
94 %! A1 = sparse (1, 1, 2); | |
95 %! P = eye (n) (:, randperm (n)); | |
96 %! A = sprand (n-3, n, .5); | |
97 %! assert (typeinfo (A * P1), "sparse matrix"); | |
98 %! assert (full (A * P1), full (A) * P1); | |
99 %! assert (typeinfo (P1 * A), "sparse matrix"); | |
100 %! assert (full (P1 * A), P1 * full (A)); | |
101 %! assert (typeinfo (A1 * P), "sparse matrix"); | |
102 %! assert (full (A1 * P), full (A1) * P); | |
103 %! assert (typeinfo (P * A1), "sparse matrix"); | |
104 %! assert (full (P * A1), P * full (A1)); | |
105 | |
106 ## permuting a matrix with exceptional values does not introduce new ones. | |
107 %!test | |
108 %! n = 5; | |
109 %! pc = randperm (n); | |
110 %! Pc = eye (n) (:, pc); | |
111 %! pr = randperm (n); | |
112 %! Pr = eye (n) (pr, :); | |
113 %! A = rand (n); | |
114 %! A(n, n-2) = NaN; | |
115 %! A(3, 1) = Inf; | |
116 %! assert (Pr * A * Pc, A(pr, pc)); | |
117 | |
118 ## conversion to sparse form | |
119 %!test | |
120 %! n = 7; | |
121 %! P = eye (n) (:, randperm (n)); | |
122 %! sP = sparse (P); | |
123 %! assert (full (sP), full (P)); | |
124 %! assert (size (find (sP), 1), n); | |
125 %! [I, J, V] = find (sP); | |
126 %! assert (all (V == 1)); | |
127 | |
128 ######################################## | |
129 ## Diagonal matrices | |
130 | |
131 ## square row scaling | |
132 %!test | |
133 %! m = 7; | |
134 %! n = 11; | |
135 %! A = rand (m, n); | |
136 %! scalefact = rand (m, 1); | |
137 %! Dr = diag (scalefact); | |
138 %! assert (Dr * A, repmat (scalefact, 1, n) .* A); | |
139 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n)); | |
140 %! scalefact(m-1) = Inf; | |
141 %! Dr(m-1, m-1) = 0; | |
142 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n)); | |
143 | |
144 ## square column scaling | |
145 %!test | |
146 %! m = 13; | |
147 %! n = 11; | |
148 %! A = rand (m, n); | |
149 %! scalefact = rand (1, n); | |
150 %! Dc = diag (scalefact); | |
151 %! assert (A * Dc, repmat (scalefact, m, 1) .* A); | |
152 %! assert (A / Dc, A ./ repmat (scalefact, m, 1)); | |
153 %! scalefact(n-1) = Inf; | |
154 %! Dc(n-1, n-1) = 0; | |
155 %! assert (A / Dc, A ./ repmat (scalefact, m, 1)); | |
156 | |
157 ## arithmetic | |
158 %!test | |
159 %! m = 9; | |
160 %! n = 7; | |
161 %! mn = min (m, n); | |
162 %! d1 = rand (mn, 1) + I () * rand (mn, 1); | |
163 %! D1 = diag (d1, m, n); | |
164 %! d2 = rand (mn, 1); | |
165 %! D2 = diag (d2, m, n); | |
166 %! D1D2 = D1 + D2; | |
167 %! assert (typeinfo (D1D2), "complex diagonal matrix"); | |
168 %! assert (diag (D1D2), d1 + d2); | |
169 %! D1D2 = D2.' * D1; | |
170 %! assert (typeinfo (D1D2), "complex diagonal matrix"); | |
171 %! assert (diag (D1D2), d1 .* d2); | |
172 | |
173 ## slicing | |
174 %!test | |
175 %! m = 13; | |
176 %! n = 6; | |
177 %! mn = min (m, n); | |
178 %! d = rand (mn, 1); | |
179 %! D = diag (d, m, n); | |
180 %! Dslice = D (1:(m-3), 1:(n-2)); | |
181 %! assert (typeinfo (Dslice), "diagonal matrix"); | |
182 | |
183 ## preserve dense matrix structure when scaling | |
184 %!assert (typeinfo (rand (8) * (3 * eye (8))), "matrix"); | |
185 %!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix"); | |
186 | |
187 ## preserve sparse matrix structure when scaling | |
188 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))), "sparse matrix"); | |
189 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))'), "sparse matrix"); | |
190 %!assert (typeinfo (((3 + 2 * I ()) * eye (8)) * sprand (8, 8, .5)), "sparse complex matrix"); | |
191 %!assert (typeinfo (((3 + 2 * I ()) * eye (8))' * sprand (8, 8, .5)), "sparse complex matrix"); | |
192 %!assert (typeinfo (sprand (8, 8, .5) * ((3 + 2 * I ()) * eye (8)).'), "sparse complex matrix"); | |
193 | |
194 ## scaling a matrix with exceptional values does not introduce new ones. | |
195 %!test | |
196 %! n = 6; | |
197 %! dr = rand (n, 1); | |
198 %! Dr = diag (dr); | |
199 %! dc = rand (1, n); | |
200 %! Dc = diag (dc); | |
201 %! A = rand (n); | |
202 %! A(n, n-2) = NaN; | |
203 %! A(4, 1) = Inf; | |
204 %! assert (Dr * A * Dc, A .* kron (dr, dc), eps); | |
205 | |
206 ## sparse inverse row scaling with a zero factor | |
207 %!test | |
208 %! n = 8; | |
209 %! A = sprand (n, n, .5); | |
210 %! scalefact = rand (n, 1); | |
211 %! Dr = diag (scalefact); | |
212 %! scalefact(n-1) = Inf; | |
213 %! Dr(n-1, n-1) = 0; | |
214 %! assert (full (Dr \ A), full (A) ./ repmat (scalefact, 1, n)); | |
215 | |
216 ## narrow sparse inverse row scaling | |
217 %!test | |
218 %! n = 8; | |
219 %! A = sprand (n, n, .5); | |
220 %! scalefact = rand (n-2, 1); | |
221 %! Dr = diag (scalefact, n, n-2); | |
222 %! assert (full (Dr \ A), Dr \ full(A)); | |
223 | |
224 ## sparse inverse column scaling with a zero factor | |
225 %!test | |
226 %! n = 11; | |
227 %! A = sprand (n, n, .5); | |
228 %! scalefact = rand (1, n); | |
229 %! Dc = diag (scalefact); | |
230 %! scalefact(n-1) = Inf; | |
231 %! Dc(n-1, n-1) = 0; | |
232 %! assert (full (A / Dc), full(A) / Dc); | |
233 | |
234 ## short sparse inverse column scaling | |
235 %!test | |
236 %! n = 7; | |
237 %! A = sprand (n, n, .5); | |
238 %! scalefact = rand (1, n-2) + I () * rand(1, n-2); | |
239 %! Dc = diag (scalefact, n-2, n); | |
240 %! assert (full (A / Dc), full(A) / Dc); | |
241 | |
242 ## adding sparse and diagonal stays sparse | |
243 %!test | |
244 %! n = 9; | |
245 %! A = sprand (n, n, .5); | |
246 %! D = 2 * eye (n); | |
247 %! assert (typeinfo (A + D), "sparse matrix"); | |
248 %! assert (typeinfo (A - D), "sparse matrix"); | |
249 %! D = D * I () + D; | |
250 %! assert (typeinfo (A - D), "sparse complex matrix"); | |
251 %! A = A * I () + A; | |
252 %! assert (typeinfo (D - A), "sparse complex matrix"); | |
253 | |
254 ## adding sparse and diagonal stays sparse | |
255 %!test | |
256 %! n = 9; | |
257 %! A = sprand (n, n, .5); | |
258 %! D = 2 * eye (n); | |
259 %! assert (full (A + D), full (A) + D); | |
260 %! assert (full (A - D), full (A) - D); | |
261 %! D = D * I () + D; | |
262 %! assert (full (D + A), D + full (A)); | |
263 %! A = A * I () + A; | |
264 %! A(6, 4) = nan (); | |
265 %! assert (full (D - A), D - full (A)); |