annotate test/diag-perm.tst @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2009-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27093
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27093
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27093
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27093
diff changeset
5 ##
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
6 ##
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
7 ## This file is part of Octave.
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## (at your option) any later version.
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
13 ##
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
14 ## 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
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
8936
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 ## 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
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
22
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
23 ########################################
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
24 ## Permutation matrices
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
25
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
26 ## row permutation
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
27 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
28 %! n = 5;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
29 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
30 %! perm = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
31 %! Prow = eye (n) (perm, :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
32 %! assert (A(perm, :), Prow * A);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
33 %! invperm(perm) = 1:n;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
34 %! assert (Prow \ A, A(invperm, :));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
35 %! assert (Prow' * A, A(invperm, :));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
36
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
37 ## column permutation
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
38 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
39 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
40 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
41 %! perm = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
42 %! Pcol = eye (n) (:, perm);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
43 %! assert (A(:, perm), A * Pcol);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
44 %! invperm(perm) = 1:n;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
45 %! assert (A / Pcol, A(:, invperm));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
46 %! assert (A * Pcol.', A(:, invperm));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
47
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
48 ## fall back to a matrix in addition
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
49 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
50 %! n = 4;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
51 %! P1 = eye (n) (:, randperm (n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
52 %! A = zeros (n) + P1;
8975
2e9af3363669 fix tests with ones
Jaroslav Hajek <highegg@gmail.com>
parents: 8968
diff changeset
53 %! assert (sum (A), full (ones (1, n)));
2e9af3363669 fix tests with ones
Jaroslav Hajek <highegg@gmail.com>
parents: 8968
diff changeset
54 %! assert (sum (A, 2), full (ones (n, 1)));
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
55
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
56 ## preserve dense matrix structure
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
57 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
58 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
59 %! Pc = eye (n) (:, randperm (n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
60 %! Pr = eye (n) (randperm (n), :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
61 %! assert (typeinfo (rand (n) * Pc), "matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
62 %! assert (typeinfo (Pr * rand (n)), "matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
63
8968
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
64 ## preserve sparse matrix structure
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
65 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
66 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
67 %! Pc = eye (n) (:, randperm (n));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
68 %! Ac = sprand (n-3, n, .5) + I () * sprand (n-3, n, .5);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
69 %! Pr = eye (n) (randperm (n), :);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
70 %! Ar = sprand (n, n+2, .5);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
71 %! assert (typeinfo (Ac * Pc), "sparse complex matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
72 %! assert (full (Ac * Pc), full (Ac) * Pc);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
73 %! assert (full (Ac / Pc), full (Ac) / Pc);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
74 %! assert (typeinfo (Pr * Ar), "sparse matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
75 %! assert (full (Pr * Ar), Pr * full (Ar));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
76 %! assert (full (Pr \ Ar), Pr \ full (Ar));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
77
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
78 ## structure rules for 1x1 dense / scalar and 1x1 perm
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
79 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
80 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
81 %! P1 = eye (1) (:, [1]);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
82 %! A1 = 1;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
83 %! P = eye (n) (:, randperm (n));
25926
373fe1608f7c Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents: 25054
diff changeset
84 %! A = rand (n-3, n);
8968
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
85 %! assert (typeinfo (A * P1), "matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
86 %! assert (full (A * P1), full (A) * P1);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
87 %! assert (typeinfo (P1 * A), "matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
88 %! assert (full (P1 * A), P1 * full (A));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
89 %! assert (typeinfo (A1 * P), "matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
90 %! assert (full (A1 * P), full (A1) * P);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
91 %! assert (typeinfo (P * A1), "matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
92 %! assert (full (P * A1), P * full (A1));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
93
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
94 ## structure rules for 1x1 sparse and 1x1 perm
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
95 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
96 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
97 %! P1 = eye (1) (:, [1]);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
98 %! A1 = sparse (1, 1, 2);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
99 %! P = eye (n) (:, randperm (n));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
100 %! A = sprand (n-3, n, .5);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
101 %! assert (typeinfo (A * P1), "sparse matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
102 %! assert (full (A * P1), full (A) * P1);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
103 %! assert (typeinfo (P1 * A), "sparse matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
104 %! assert (full (P1 * A), P1 * full (A));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
105 %! assert (typeinfo (A1 * P), "sparse matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
106 %! assert (full (A1 * P), full (A1) * P);
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
107 %! assert (typeinfo (P * A1), "sparse matrix");
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
108 %! assert (full (P * A1), P * full (A1));
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
109
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
110 ## 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
111 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
112 %! n = 5;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
113 %! pc = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
114 %! Pc = eye (n) (:, pc);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
115 %! pr = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
116 %! Pr = eye (n) (pr, :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
117 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
118 %! A(n, n-2) = NaN;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
119 %! A(3, 1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
120 %! assert (Pr * A * Pc, A(pr, pc));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
121
8951
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
122 ## conversion to sparse form
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
123 %!test
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
124 %! n = 7;
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
125 %! P = eye (n) (:, randperm (n));
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
126 %! sP = sparse (P);
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
127 %! assert (full (sP), full (P));
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
128 %! 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
129 %! [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
130 %! assert (all (V == 1));
8951
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
131
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
132 ########################################
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
133 ## Diagonal matrices
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
134
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
135 ## square row scaling
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
136 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
137 %! m = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
138 %! n = 11;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
139 %! A = rand (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
140 %! scalefact = rand (m, 1);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
141 %! Dr = diag (scalefact);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
142 %! assert (Dr * A, repmat (scalefact, 1, n) .* A);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
143 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
144 %! scalefact(m-1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
145 %! Dr(m-1, m-1) = 0;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
146 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
147
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
148 ## square column scaling
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 %! m = 13;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
151 %! n = 11;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
152 %! A = rand (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
153 %! scalefact = rand (1, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
154 %! Dc = diag (scalefact);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
155 %! assert (A * Dc, repmat (scalefact, m, 1) .* A);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
156 %! assert (A / Dc, A ./ repmat (scalefact, m, 1));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
157 %! scalefact(n-1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
158 %! Dc(n-1, n-1) = 0;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
159 %! assert (A / Dc, A ./ repmat (scalefact, m, 1));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
160
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
161 ## arithmetic
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
162 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
163 %! m = 9;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
164 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
165 %! mn = min (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
166 %! d1 = rand (mn, 1) + I () * rand (mn, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
167 %! D1 = diag (d1, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
168 %! d2 = rand (mn, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
169 %! D2 = diag (d2, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
170 %! D1D2 = D1 + D2;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
171 %! assert (typeinfo (D1D2), "complex diagonal matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
172 %! assert (diag (D1D2), d1 + d2);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
173 %! D1D2 = D2.' * D1;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
174 %! assert (typeinfo (D1D2), "complex diagonal matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
175 %! assert (diag (D1D2), d1 .* d2);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
176
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
177 ## slicing
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
178 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
179 %! m = 13;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
180 %! n = 6;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
181 %! mn = min (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
182 %! d = rand (mn, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
183 %! D = diag (d, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
184 %! Dslice = D (1:(m-3), 1:(n-2));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
185 %! assert (typeinfo (Dslice), "diagonal matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
186
8964
f4f4d65faaa0 Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents: 8952
diff changeset
187 ## preserve dense matrix structure when scaling
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
188 %!assert (typeinfo (rand (8) * (3 * eye (8))), "matrix")
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
189 %!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix")
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
190
8964
f4f4d65faaa0 Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents: 8952
diff changeset
191 ## preserve sparse matrix structure when scaling
21317
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
192 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))), "sparse matrix")
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
193 %!assert (typeinfo (sprand (8, 8, .5) * (3 * eye (8))'), "sparse matrix")
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
194 %!assert (typeinfo (((3 + 2 * I ()) * eye (8)) * sprand (8, 8, .5)), "sparse complex matrix")
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
195 %!assert (typeinfo (((3 + 2 * I ()) * eye (8))' * sprand (8, 8, .5)), "sparse complex matrix")
a4faec57f4c8 maint: remove semicolon after %!assert tests to follow Octave conventions.
Rik <rik@octave.org>
parents: 20707
diff changeset
196 %!assert (typeinfo (sprand (8, 8, .5) * ((3 + 2 * I ()) * eye (8)).'), "sparse complex matrix")
8964
f4f4d65faaa0 Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents: 8952
diff changeset
197
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
198 ## 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
199 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
200 %! n = 6;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
201 %! dr = rand (n, 1);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
202 %! Dr = diag (dr);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
203 %! dc = rand (1, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
204 %! Dc = diag (dc);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
205 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
206 %! A(n, n-2) = NaN;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
207 %! A(4, 1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
208 %! assert (Dr * A * Dc, A .* kron (dr, dc), eps);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
209
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
210 ## sparse inverse row scaling with a zero factor
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
211 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
212 %! n = 8;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
213 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
214 %! scalefact = rand (n, 1);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
215 %! Dr = diag (scalefact);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
216 %! scalefact(n-1) = Inf;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
217 %! Dr(n-1, n-1) = 0;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
218 %! 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
219
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
220 ## narrow sparse inverse row scaling
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
221 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
222 %! n = 8;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
223 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
224 %! scalefact = rand (n-2, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
225 %! Dr = diag (scalefact, n, n-2);
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
226 %! assert (full (Dr \ A), Dr \ full(A));
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
227
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
228 ## sparse inverse column scaling with a zero factor
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
229 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
230 %! n = 11;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
231 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
232 %! scalefact = rand (1, n);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
233 %! Dc = diag (scalefact);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
234 %! scalefact(n-1) = Inf;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
235 %! Dc(n-1, n-1) = 0;
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
236 %! assert (full (A / Dc), full(A) / Dc);
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
237
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
238 ## short sparse inverse column scaling
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
239 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
240 %! n = 7;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
241 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
242 %! scalefact = rand (1, n-2) + I () * rand(1, n-2);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
243 %! Dc = diag (scalefact, n-2, n);
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
244 %! 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
245
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
246 ## 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
247 %!test
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
248 %! n = 9;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
249 %! A = sprand (n, n, .5);
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
250 %! D = 2 * eye (n);
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
251 %! assert (typeinfo (A + D), "sparse matrix");
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
252 %! assert (typeinfo (A - D), "sparse matrix");
8966
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
253 %! D = D * I () + D;
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
254 %! assert (typeinfo (A - D), "sparse complex matrix");
8966
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
255 %! A = A * I () + A;
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
256 %! assert (typeinfo (D - A), "sparse complex matrix");
8966
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
257
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
258 ## 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
259 %!test
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
260 %! n = 9;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
261 %! A = sprand (n, n, .5);
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
262 %! D = 2 * eye (n);
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
263 %! assert (full (A + D), full (A) + D);
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
264 %! assert (full (A - D), full (A) - D);
8966
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
265 %! D = D * I () + D;
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
266 %! assert (full (D + A), D + full (A));
8966
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
267 %! A = A * I () + A;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
268 %! A(6, 4) = nan ();
14131
c3309e1ec50d test: Use Octave coding and spacing conventions for fixed test scripts
Rik <octave@nomad.inbox5.com>
parents: 12570
diff changeset
269 %! assert (full (D - A), D - full (A));
20568
f61c67865d9f Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents: 19697
diff changeset
270
f61c67865d9f Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents: 19697
diff changeset
271 ## inverse preserves diagonal structure even for singular matrices (bug #46103)
27093
6e18f0ce268c Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents: 26376
diff changeset
272 ## but set all the diagonal elements to Inf (bug #56232)
20568
f61c67865d9f Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents: 19697
diff changeset
273 %!test
f61c67865d9f Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents: 19697
diff changeset
274 %! x = diag (1:3);
f61c67865d9f Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents: 19697
diff changeset
275 %! assert (inv (x), diag ([1 1/2 1/3]));
27093
6e18f0ce268c Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents: 26376
diff changeset
276 %!warning <matrix singular> A = inv (diag (0:2));
6e18f0ce268c Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents: 26376
diff changeset
277 %! assert (A, diag ([Inf Inf Inf]));
20707
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
278
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
279 ## assignment to diagonal elements preserves diagonal structure (bug #36932)
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
280 %!test
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
281 %! x = diag (1:3);
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
282 %! x(1,1) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
283 %! assert (typeinfo (x), "diagonal matrix");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
284 %! x(3,3) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
285 %! assert (typeinfo (x), "diagonal matrix");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
286
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
287 %!test
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
288 %! x = diag (1:3);
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
289 %! x(1) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
290 %! assert (typeinfo (x), "diagonal matrix");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
291 %! x(9) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
292 %! assert (typeinfo (x), "diagonal matrix");