annotate test/diag-perm.tst @ 31191:bb9d776eafac stable

Fix wrong color in PDF printout of some latex strings (bug #62884) * octave-svgconvert (draw): For "rect" elements only set brush color if necessary and eventually restore to previous color.
author Pantxo Diribarne <pantxo.diribarne@gmail.com>
date Sun, 14 Aug 2022 18:24:07 +0200
parents 796f54d4ddbf
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29982
diff changeset
3 ## Copyright (C) 2009-2022 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
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
7 ##
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
8 ## This file is part of Octave.
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## 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
11 ## 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
12 ## 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
13 ## (at your option) any later version.
8936
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 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
8936
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 ## 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
21 ## 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
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
8936
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 ########################################
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
27 ## Permutation matrices
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
28
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
29 ## row permutation
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
30 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
31 %! n = 5;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
32 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
33 %! perm = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
34 %! Prow = eye (n) (perm, :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
35 %! assert (A(perm, :), Prow * A);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
36 %! invperm(perm) = 1:n;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
37 %! assert (Prow \ A, A(invperm, :));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
38 %! assert (Prow' * A, A(invperm, :));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
39
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
40 ## column permutation
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
41 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
42 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
43 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
44 %! perm = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
45 %! Pcol = eye (n) (:, perm);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
46 %! assert (A(:, perm), A * Pcol);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
47 %! invperm(perm) = 1:n;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
48 %! assert (A / Pcol, A(:, invperm));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
49 %! assert (A * Pcol.', A(:, invperm));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
50
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
51 ## fall back to a matrix in addition
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
52 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
53 %! n = 4;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
54 %! P1 = eye (n) (:, randperm (n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
55 %! A = zeros (n) + P1;
8975
2e9af3363669 fix tests with ones
Jaroslav Hajek <highegg@gmail.com>
parents: 8968
diff changeset
56 %! assert (sum (A), full (ones (1, n)));
2e9af3363669 fix tests with ones
Jaroslav Hajek <highegg@gmail.com>
parents: 8968
diff changeset
57 %! assert (sum (A, 2), full (ones (n, 1)));
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
58
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
59 ## preserve dense matrix structure
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
60 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
61 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
62 %! Pc = eye (n) (:, randperm (n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
63 %! Pr = eye (n) (randperm (n), :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
64 %! assert (typeinfo (rand (n) * Pc), "matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
65 %! assert (typeinfo (Pr * rand (n)), "matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
66
8968
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
67 ## 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
68 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
69 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
70 %! 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
71 %! 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
72 %! 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
73 %! 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
74 %! 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
75 %! 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
76 %! 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
77 %! 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
78 %! 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
79 %! 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
80
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
81 ## 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
82 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
83 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
84 %! 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
85 %! A1 = 1;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
86 %! 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
87 %! 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
88 %! 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
89 %! 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
90 %! 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
91 %! 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
92 %! 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
93 %! 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
94 %! 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
95 %! 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
96
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
97 ## 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
98 %!test
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
99 %! n = 7;
91d53dc37f79 Add perm * sparse, perm \ sparse, sparse * perm, and sparse / perm operations.
Jason Riedy <jason@acm.org>
parents: 8966
diff changeset
100 %! 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
101 %! 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
102 %! 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
103 %! 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
104 %! 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
105 %! 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
106 %! 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
107 %! 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
108 %! 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
109 %! 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
110 %! 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
111 %! 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
112
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
113 ## 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
114 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
115 %! n = 5;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
116 %! pc = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
117 %! Pc = eye (n) (:, pc);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
118 %! pr = randperm (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
119 %! Pr = eye (n) (pr, :);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
120 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
121 %! A(n, n-2) = NaN;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
122 %! A(3, 1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
123 %! assert (Pr * A * Pc, A(pr, pc));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
124
8951
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
125 ## conversion to sparse form
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
126 %!test
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
127 %! n = 7;
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
128 %! P = eye (n) (:, randperm (n));
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
129 %! sP = sparse (P);
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
130 %! assert (full (sP), full (P));
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
131 %! 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
132 %! [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
133 %! assert (all (V == 1));
8951
5bce1357edd6 Fix conversion from PermMatrix to SparseMatrix.
Jason Riedy <jason@acm.org>
parents: 8936
diff changeset
134
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
135 ########################################
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
136 ## Diagonal matrices
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
137
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
138 ## square row scaling
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
139 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
140 %! m = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
141 %! n = 11;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
142 %! A = rand (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
143 %! scalefact = rand (m, 1);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
144 %! Dr = diag (scalefact);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
145 %! assert (Dr * A, repmat (scalefact, 1, n) .* A);
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 %! scalefact(m-1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
148 %! Dr(m-1, m-1) = 0;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
149 %! assert (Dr \ A, A ./ repmat (scalefact, 1, n));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
150
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
151 ## square column scaling
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
152 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
153 %! m = 13;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
154 %! n = 11;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
155 %! A = rand (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
156 %! scalefact = rand (1, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
157 %! Dc = diag (scalefact);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
158 %! assert (A * Dc, repmat (scalefact, m, 1) .* A);
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 %! scalefact(n-1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
161 %! Dc(n-1, n-1) = 0;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
162 %! assert (A / Dc, A ./ repmat (scalefact, m, 1));
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
163
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
164 ## arithmetic
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
165 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
166 %! m = 9;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
167 %! n = 7;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
168 %! mn = min (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
169 %! 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
170 %! D1 = diag (d1, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
171 %! d2 = rand (mn, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
172 %! D2 = diag (d2, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
173 %! D1D2 = D1 + D2;
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 %! D1D2 = D2.' * D1;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
177 %! assert (typeinfo (D1D2), "complex diagonal matrix");
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
178 %! assert (diag (D1D2), d1 .* d2);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
179
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
180 ## slicing
28629
af361aea02e0 allow tests to work when disable_range is true
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
181 ## preserving diagonal matrix type is not possible if indices are
af361aea02e0 allow tests to work when disable_range is true
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
182 ## general matrix objects.
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
183 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
184 %! m = 13;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
185 %! n = 6;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
186 %! mn = min (m, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
187 %! d = rand (mn, 1);
12570
1a93988610a3 Reverse changset cc8ccdfec424. Restore 3-input form of diag().
Rik <octave@nomad.inbox5.com>
parents: 12567
diff changeset
188 %! D = diag (d, m, n);
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
189 %! Dslice = D (1:(m-3), 1:(n-2));
29982
605275522c37 Deprecate disable_range, disable_diagonal_matrix, disable_permutation_matrix.
Rik <rik@octave.org>
parents: 29359
diff changeset
190 %! if (optimize_range ())
605275522c37 Deprecate disable_range, disable_diagonal_matrix, disable_permutation_matrix.
Rik <rik@octave.org>
parents: 29359
diff changeset
191 %! assert (typeinfo (Dslice), "diagonal matrix");
605275522c37 Deprecate disable_range, disable_diagonal_matrix, disable_permutation_matrix.
Rik <rik@octave.org>
parents: 29359
diff changeset
192 %! else
28629
af361aea02e0 allow tests to work when disable_range is true
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
193 %! assert (typeinfo (Dslice), "matrix");
af361aea02e0 allow tests to work when disable_range is true
John W. Eaton <jwe@octave.org>
parents: 27923
diff changeset
194 %! endif
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
195
8964
f4f4d65faaa0 Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents: 8952
diff changeset
196 ## 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
197 %!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
198 %!assert (typeinfo ((3 * eye (8)) * rand (8)), "matrix")
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
199
8964
f4f4d65faaa0 Implement sparse * diagonal and diagonal * sparse operations, double-prec only.
Jason Riedy <jason@acm.org>
parents: 8952
diff changeset
200 ## 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
201 %!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
202 %!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
203 %!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
204 %!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
205 %!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
206
8936
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
207 ## 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
208 %!test
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
209 %! n = 6;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
210 %! dr = rand (n, 1);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
211 %! Dr = diag (dr);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
212 %! dc = rand (1, n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
213 %! Dc = diag (dc);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
214 %! A = rand (n);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
215 %! A(n, n-2) = NaN;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
216 %! A(4, 1) = Inf;
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
217 %! assert (Dr * A * Dc, A .* kron (dr, dc), eps);
42e24f4ebc8c add tests for diag & perm matrices.
Jason Riedy <jason@acm.org>
parents:
diff changeset
218
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
219 ## sparse inverse row scaling with a zero factor
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
220 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
221 %! n = 8;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
222 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
223 %! scalefact = rand (n, 1);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
224 %! Dr = diag (scalefact);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
225 %! scalefact(n-1) = Inf;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
226 %! Dr(n-1, n-1) = 0;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
227 %! 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
228
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
229 ## narrow sparse inverse row scaling
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
230 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
231 %! n = 8;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
232 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
233 %! 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
234 %! Dr = diag (scalefact, n, n-2);
28916
ec591c500fa4 maint: Use Octave convention of space after function name in test/.
Rik <rik@octave.org>
parents: 28629
diff changeset
235 %! assert (full (Dr \ A), Dr \ full (A));
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
236
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
237 ## sparse inverse column scaling with a zero factor
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
238 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
239 %! n = 11;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
240 %! A = sprand (n, n, .5);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
241 %! scalefact = rand (1, n);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
242 %! Dc = diag (scalefact);
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
243 %! scalefact(n-1) = Inf;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
244 %! Dc(n-1, n-1) = 0;
28916
ec591c500fa4 maint: Use Octave convention of space after function name in test/.
Rik <rik@octave.org>
parents: 28629
diff changeset
245 %! assert (full (A / Dc), full (A) / Dc);
8965
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
246
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
247 ## short sparse inverse column scaling
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
248 %!test
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
249 %! n = 7;
42aff15e059b Implement diag \ sparse and sparse / diag.
Jason Riedy <jason@acm.org>
parents: 8964
diff changeset
250 %! A = sprand (n, n, .5);
28916
ec591c500fa4 maint: Use Octave convention of space after function name in test/.
Rik <rik@octave.org>
parents: 28629
diff changeset
251 %! 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
252 %! Dc = diag (scalefact, n-2, n);
28916
ec591c500fa4 maint: Use Octave convention of space after function name in test/.
Rik <rik@octave.org>
parents: 28629
diff changeset
253 %! 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
254
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
255 ## 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
256 %!test
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
257 %! n = 9;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
258 %! A = sprand (n, n, .5);
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
259 %! 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
260 %! 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
261 %! 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
262 %! 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
263 %! 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
264 %! 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
265 %! 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
266
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
267 ## 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
268 %!test
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
269 %! n = 9;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
270 %! A = sprand (n, n, .5);
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
271 %! 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
272 %! 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
273 %! 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
274 %! 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
275 %! 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
276 %! A = A * I () + A;
1bba53c0a38d Implement diag + sparse, diag - sparse, sparse + diag, sparse - diag.
Jason Riedy <jason@acm.org>
parents: 8965
diff changeset
277 %! 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
278 %! 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
279
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
280 ## 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
281 ## 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
282 %!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
283 %! 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
284 %! 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
285 %!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
286 %! 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
287
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
288 ## 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
289 %!test
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
290 %! x = diag (1:3);
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
291 %! x(1,1) = -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");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
293 %! x(3,3) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
294 %! assert (typeinfo (x), "diagonal matrix");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
295
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
296 %!test
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
297 %! x = diag (1:3);
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
298 %! x(1) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
299 %! assert (typeinfo (x), "diagonal matrix");
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
300 %! x(9) = -1;
cd1bd06974d8 Preserve diagonal matrix property with linear index assignment (bug #36932).
Rik <rik@octave.org>
parents: 20568
diff changeset
301 %! assert (typeinfo (x), "diagonal matrix");