comparison test/test_diag_perm.m @ 8936:42e24f4ebc8c

add tests for diag & perm matrices.
author Jason Riedy <jason@acm.org>
date Sun, 08 Mar 2009 16:40:10 -0400
parents
children 5bce1357edd6
comparison
equal deleted inserted replaced
8935:cae073411b03 8936:42e24f4ebc8c
1 ## Copyright (C) 2009 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), ones (1, n));
50 %! assert (sum (A, 2), 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 ## permuting a matrix with exceptional values does not introduce new ones.
61 %!test
62 %! n = 5;
63 %! pc = randperm (n);
64 %! Pc = eye (n) (:, pc);
65 %! pr = randperm (n);
66 %! Pr = eye (n) (pr, :);
67 %! A = rand (n);
68 %! A(n, n-2) = NaN;
69 %! A(3, 1) = Inf;
70 %! assert (Pr * A * Pc, A(pr, pc));
71
72 ########################################
73 ## Diagonal matrices
74
75 ## square row scaling
76 %!test
77 %! m = 7;
78 %! n = 11;
79 %! A = rand (m, n);
80 %! scalefact = rand (m, 1);
81 %! Dr = diag (scalefact);
82 %! assert (Dr * A, repmat (scalefact, 1, n) .* A);
83 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
84 %! scalefact(m-1) = Inf;
85 %! Dr(m-1, m-1) = 0;
86 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
87
88 ## square column scaling
89 %!test
90 %! m = 13;
91 %! n = 11;
92 %! A = rand (m, n);
93 %! scalefact = rand (1, n);
94 %! Dc = diag (scalefact);
95 %! assert (A * Dc, repmat (scalefact, m, 1) .* A);
96 %! assert (A / Dc, A ./ repmat (scalefact, m, 1));
97 %! scalefact(n-1) = Inf;
98 %! Dc(n-1, n-1) = 0;
99 %! assert (A / Dc, A ./ repmat (scalefact, m, 1));
100
101 ## arithmetic
102 %!test
103 %! m = 9;
104 %! n = 7;
105 %! mn = min (m, n);
106 %! d1 = rand (mn, 1) + I () * rand (mn, 1);
107 %! D1 = diag (d1, m, n);
108 %! d2 = rand (mn, 1);
109 %! D2 = diag (d2, m, n);
110 %! D1D2 = D1 + D2;
111 %! assert (typeinfo (D1D2), "complex diagonal matrix");
112 %! assert (diag (D1D2), d1 + d2);
113 %! D1D2 = D2.' * D1;
114 %! assert (typeinfo (D1D2), "complex diagonal matrix");
115 %! assert (diag (D1D2), d1 .* d2);
116
117 ## slicing
118 %!test
119 %! m = 13;
120 %! n = 6;
121 %! mn = min (m, n);
122 %! d = rand (mn, 1);
123 %! D = diag (d, m, n);
124 %! Dslice = D (1:(m-3), 1:(n-2));
125 %! assert (typeinfo (Dslice), "diagonal matrix");
126
127 ## preserve dense matrix structure
128 %!assert (typeinfo (rand (8) * (3 * eye (8))), "matrix");
129 %!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix");
130
131 ## scaling a matrix with exceptional values does not introduce new ones.
132 %!test
133 %! n = 6;
134 %! dr = rand (n, 1);
135 %! Dr = diag (dr);
136 %! dc = rand (1, n);
137 %! Dc = diag (dc);
138 %! A = rand (n);
139 %! A(n, n-2) = NaN;
140 %! A(4, 1) = Inf;
141 %! assert (Dr * A * Dc, A .* kron (dr, dc), eps);
142