Mercurial > octave-nkf
annotate test/test_diag_perm.m @ 8966:1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Date: Mon, 9 Mar 2009 17:45:22 -0400
This does not use the typical sparse-mx-ops generator. I suspect the
sematics of elementwise multiplication and division to be rather
controversial, so they are not included. If comparison operations are
added, the implementation should be shifted over to use the typical
generator.
The template in Sparse-diag-op-defs.h likely could use const bools
rather than functional argument operations. I haven't measured which
is optimized more effectively.
Also, the Octave binding layer in op-dm-scm.cc likely could use all
sorts of macro or template trickery, but it's far easier to let Emacs
handle it for now. That would be worth revisiting if further
elementwise sparse and diagonal operations are added.
author | Jason Riedy <jason@acm.org> |
---|---|
date | Mon, 09 Mar 2009 17:49:14 -0400 |
parents | 42aff15e059b |
children | 91d53dc37f79 |
rev | line source |
---|---|
8936
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2009 E. Jason Riedy |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
2 ## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
4 ## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
8 ## your option) any later version. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
9 ## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
13 ## General Public License for more details. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
14 ## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
18 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
19 ######################################## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
20 ## Permutation matrices |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
21 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
22 ## row permutation |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
23 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
24 %! n = 5; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
25 %! A = rand (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
26 %! perm = randperm (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
27 %! Prow = eye (n) (perm, :); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
28 %! assert (A(perm, :), Prow * A); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
29 %! invperm(perm) = 1:n; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
30 %! assert (Prow \ A, A(invperm, :)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
31 %! assert (Prow' * A, A(invperm, :)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
32 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
33 ## column permutation |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
34 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
35 %! n = 7; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
36 %! A = rand (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
37 %! perm = randperm (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
38 %! Pcol = eye (n) (:, perm); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
39 %! assert (A(:, perm), A * Pcol); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
40 %! invperm(perm) = 1:n; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
41 %! assert (A / Pcol, A(:, invperm)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
42 %! assert (A * Pcol.', A(:, invperm)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
43 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
44 ## fall back to a matrix in addition |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
45 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
46 %! n = 4; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
47 %! P1 = eye (n) (:, randperm (n)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
48 %! A = zeros (n) + P1; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
49 %! assert (sum (A), ones (1, n)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
50 %! assert (sum (A, 2), ones (n, 1)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
51 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
52 ## preserve dense matrix structure |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
53 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
54 %! n = 7; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
55 %! Pc = eye (n) (:, randperm (n)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
56 %! Pr = eye (n) (randperm (n), :); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
57 %! assert (typeinfo (rand (n) * Pc), "matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
58 %! assert (typeinfo (Pr * rand (n)), "matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
59 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
60 ## permuting a matrix with exceptional values does not introduce new ones. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
61 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
62 %! n = 5; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
63 %! pc = randperm (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
64 %! Pc = eye (n) (:, pc); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
65 %! pr = randperm (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
66 %! Pr = eye (n) (pr, :); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
67 %! A = rand (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
68 %! A(n, n-2) = NaN; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
69 %! A(3, 1) = Inf; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
70 %! assert (Pr * A * Pc, A(pr, pc)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
71 |
8951
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
72 ## conversion to sparse form |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
73 %!test |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
74 %! n = 7; |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
75 %! P = eye (n) (:, randperm (n)); |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
76 %! sP = sparse (P); |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
77 %! assert (full (sP), full (P)); |
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
78 %! assert (size (find (sP), 1), n); |
8952
43aec7c168eb
test_diag_per.m: additional assert for sparse conversion test
Jason Riedy <jason@acm.org>
parents:
8951
diff
changeset
|
79 %! [I, J, V] = find (sP); |
43aec7c168eb
test_diag_per.m: additional assert for sparse conversion test
Jason Riedy <jason@acm.org>
parents:
8951
diff
changeset
|
80 %! assert (all (V == 1)); |
8951
5bce1357edd6
Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents:
8936
diff
changeset
|
81 |
8936
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
82 ######################################## |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
83 ## Diagonal matrices |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
84 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
85 ## square row scaling |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
86 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
87 %! m = 7; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
88 %! n = 11; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
89 %! A = rand (m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
90 %! scalefact = rand (m, 1); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
91 %! Dr = diag (scalefact); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
92 %! assert (Dr * A, repmat (scalefact, 1, n) .* A); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
93 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
94 %! scalefact(m-1) = Inf; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
95 %! Dr(m-1, m-1) = 0; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
96 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
97 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
98 ## square column scaling |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
99 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
100 %! m = 13; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
101 %! n = 11; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
102 %! A = rand (m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
103 %! scalefact = rand (1, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
104 %! Dc = diag (scalefact); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
105 %! assert (A * Dc, repmat (scalefact, m, 1) .* A); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
106 %! assert (A / Dc, A ./ repmat (scalefact, m, 1)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
107 %! scalefact(n-1) = Inf; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
108 %! Dc(n-1, n-1) = 0; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
109 %! assert (A / Dc, A ./ repmat (scalefact, m, 1)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
110 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
111 ## arithmetic |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
112 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
113 %! m = 9; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
114 %! n = 7; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
115 %! mn = min (m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
116 %! d1 = rand (mn, 1) + I () * rand (mn, 1); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
117 %! D1 = diag (d1, m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
118 %! d2 = rand (mn, 1); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
119 %! D2 = diag (d2, m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
120 %! D1D2 = D1 + D2; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
121 %! assert (typeinfo (D1D2), "complex diagonal matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
122 %! assert (diag (D1D2), d1 + d2); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
123 %! D1D2 = D2.' * D1; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
124 %! assert (typeinfo (D1D2), "complex diagonal matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
125 %! assert (diag (D1D2), d1 .* d2); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
126 |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
127 ## slicing |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
128 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
129 %! m = 13; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
130 %! n = 6; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
131 %! mn = min (m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
132 %! d = rand (mn, 1); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
133 %! D = diag (d, m, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
134 %! Dslice = D (1:(m-3), 1:(n-2)); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
135 %! assert (typeinfo (Dslice), "diagonal matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
136 |
8964
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
137 ## preserve dense matrix structure when scaling |
8936
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
138 %!assert (typeinfo (rand (8) * (3 * eye (8))), "matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
139 %!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix"); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
140 |
8964
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
141 ## preserve sparse matrix structure when scaling |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
142 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))), "sparse matrix"); |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
143 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))'), "sparse matrix"); |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
144 %!assert (typeinfo (((3 + 2 * I ()) * eye (8)) * sprand (8, 8, .5)), "sparse complex matrix"); |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
145 %!assert (typeinfo (((3 + 2 * I ()) * eye (8))' * sprand (8, 8, .5)), "sparse complex matrix"); |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
146 %!assert (typeinfo (sprand (8, 8, .5) * ((3 + 2 * I ()) * eye (8)).'), "sparse complex matrix"); |
f4f4d65faaa0
Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents:
8952
diff
changeset
|
147 |
8936
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
148 ## scaling a matrix with exceptional values does not introduce new ones. |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
149 %!test |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
150 %! n = 6; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
151 %! dr = rand (n, 1); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
152 %! Dr = diag (dr); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
153 %! dc = rand (1, n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
154 %! Dc = diag (dc); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
155 %! A = rand (n); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
156 %! A(n, n-2) = NaN; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
157 %! A(4, 1) = Inf; |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
158 %! assert (Dr * A * Dc, A .* kron (dr, dc), eps); |
42e24f4ebc8c
add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff
changeset
|
159 |
8965
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
160 ## sparse inverse row scaling with a zero factor |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
161 %!test |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
162 %! n = 8; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
163 %! A = sprand (n, n, .5); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
164 %! scalefact = rand (n, 1); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
165 %! Dr = diag (scalefact); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
166 %! scalefact(n-1) = Inf; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
167 %! Dr(n-1, n-1) = 0; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
168 %! assert (full (Dr \ A), full (A) ./ repmat (scalefact, 1, n)); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
169 |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
170 ## narrow sparse inverse row scaling |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
171 %!test |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
172 %! n = 8; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
173 %! A = sprand (n, n, .5); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
174 %! scalefact = rand (n-2, 1); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
175 %! Dr = diag (scalefact, n, n-2); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
176 %! assert (full (Dr \ A), Dr \ full(A)) |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
177 |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
178 ## sparse inverse column scaling with a zero factor |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
179 %!test |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
180 %! n = 11; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
181 %! A = sprand (n, n, .5); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
182 %! scalefact = rand (1, n); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
183 %! Dc = diag (scalefact); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
184 %! scalefact(n-1) = Inf; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
185 %! Dc(n-1, n-1) = 0; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
186 %! assert (full (A / Dc), full(A) / Dc) |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
187 |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
188 ## short sparse inverse column scaling |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
189 %!test |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
190 %! n = 7; |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
191 %! A = sprand (n, n, .5); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
192 %! scalefact = rand (1, n-2) + I () * rand(1, n-2); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
193 %! Dc = diag (scalefact, n-2, n); |
42aff15e059b
Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents:
8964
diff
changeset
|
194 %! assert (full (A / Dc), full(A) / Dc) |
8966
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
195 |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
196 ## adding sparse and diagonal stays sparse |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
197 %!test |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
198 %! n = 9; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
199 %! A = sprand (n, n, .5); |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
200 %! D = 2 * eye (n); |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
201 %! assert (typeinfo (A + D), "sparse matrix") |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
202 %! assert (typeinfo (A - D), "sparse matrix") |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
203 %! D = D * I () + D; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
204 %! assert (typeinfo (A - D), "sparse complex matrix") |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
205 %! A = A * I () + A; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
206 %! assert (typeinfo (D - A), "sparse complex matrix") |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
207 |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
208 ## adding sparse and diagonal stays sparse |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
209 %!test |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
210 %! n = 9; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
211 %! A = sprand (n, n, .5); |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
212 %! D = 2 * eye (n); |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
213 %! assert (full (A + D), full (A) + D) |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
214 %! assert (full (A - D), full (A) - D) |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
215 %! D = D * I () + D; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
216 %! assert (full (D + A), D + full (A)) |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
217 %! A = A * I () + A; |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
218 %! A(6, 4) = nan (); |
1bba53c0a38d
Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents:
8965
diff
changeset
|
219 %! assert (full (D - A), D - full (A)) |