Mercurial > octave
annotate liboctave/array/dDiagMatrix.cc @ 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 |
rev | line source |
---|---|
458 | 1 /* |
2 | |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 Copyright (C) 1994-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
|
4 |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27093
diff
changeset
|
5 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
|
6 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
|
7 |
458 | 8 |
9 This file is part of Octave. | |
10 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23807
diff
changeset
|
11 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
12 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:
23807
diff
changeset
|
13 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
14 (at your option) any later version. |
458 | 15 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
16 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
17 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22402
diff
changeset
|
19 GNU General Public License for more details. |
458 | 20 |
21 You should have received a copy of the GNU General Public License | |
7016 | 22 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:
23807
diff
changeset
|
23 <https://www.gnu.org/licenses/>. |
458 | 24 |
25 */ | |
26 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21723
diff
changeset
|
27 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
28 # include "config.h" |
458 | 29 #endif |
30 | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
31 #include <ostream> |
458 | 32 |
4669 | 33 #include "Array-util.h" |
1368 | 34 #include "lo-error.h" |
458 | 35 #include "mx-base.h" |
36 #include "mx-inlines.cc" | |
1650 | 37 #include "oct-cmplx.h" |
458 | 38 |
1360 | 39 // Diagonal Matrix class. |
458 | 40 |
2385 | 41 bool |
458 | 42 DiagMatrix::operator == (const DiagMatrix& a) const |
43 { | |
44 if (rows () != a.rows () || cols () != a.cols ()) | |
45 return 0; | |
46 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
47 return mx_inline_equal (length (), data (), a.data ()); |
458 | 48 } |
49 | |
2385 | 50 bool |
458 | 51 DiagMatrix::operator != (const DiagMatrix& a) const |
52 { | |
53 return !(*this == a); | |
54 } | |
55 | |
56 DiagMatrix& | |
57 DiagMatrix::fill (double val) | |
58 { | |
5275 | 59 for (octave_idx_type i = 0; i < length (); i++) |
458 | 60 elem (i, i) = val; |
61 return *this; | |
62 } | |
63 | |
64 DiagMatrix& | |
5275 | 65 DiagMatrix::fill (double val, octave_idx_type beg, octave_idx_type end) |
458 | 66 { |
67 if (beg < 0 || end >= length () || end < beg) | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
68 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 69 |
5275 | 70 for (octave_idx_type i = beg; i <= end; i++) |
458 | 71 elem (i, i) = val; |
72 | |
73 return *this; | |
74 } | |
75 | |
76 DiagMatrix& | |
77 DiagMatrix::fill (const ColumnVector& a) | |
78 { | |
5275 | 79 octave_idx_type len = length (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
80 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
81 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 82 |
5275 | 83 for (octave_idx_type i = 0; i < len; i++) |
458 | 84 elem (i, i) = a.elem (i); |
85 | |
86 return *this; | |
87 } | |
88 | |
89 DiagMatrix& | |
90 DiagMatrix::fill (const RowVector& a) | |
91 { | |
5275 | 92 octave_idx_type len = length (); |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
93 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
94 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 95 |
5275 | 96 for (octave_idx_type i = 0; i < len; i++) |
458 | 97 elem (i, i) = a.elem (i); |
98 | |
99 return *this; | |
100 } | |
101 | |
102 DiagMatrix& | |
5275 | 103 DiagMatrix::fill (const ColumnVector& a, octave_idx_type beg) |
458 | 104 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
105 octave_idx_type a_len = a.numel (); |
458 | 106 if (beg < 0 || beg + a_len >= length ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
107 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 108 |
5275 | 109 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 110 elem (i+beg, i+beg) = a.elem (i); |
111 | |
112 return *this; | |
113 } | |
114 | |
115 DiagMatrix& | |
5275 | 116 DiagMatrix::fill (const RowVector& a, octave_idx_type beg) |
458 | 117 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
118 octave_idx_type a_len = a.numel (); |
458 | 119 if (beg < 0 || beg + a_len >= length ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
120 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 121 |
5275 | 122 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 123 elem (i+beg, i+beg) = a.elem (i); |
124 | |
125 return *this; | |
126 } | |
127 | |
128 DiagMatrix | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
129 DiagMatrix::abs (void) const |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
130 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
131 return DiagMatrix (extract_diag ().abs (), rows (), columns ()); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
132 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
133 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
134 DiagMatrix |
1205 | 135 real (const ComplexDiagMatrix& a) |
136 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
137 return DiagMatrix (real (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 138 } |
139 | |
140 DiagMatrix | |
141 imag (const ComplexDiagMatrix& a) | |
142 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
143 return DiagMatrix (imag (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 144 } |
145 | |
458 | 146 Matrix |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
147 DiagMatrix::extract (octave_idx_type r1, octave_idx_type c1, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
148 octave_idx_type r2, octave_idx_type c2) const |
458 | 149 { |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15448
diff
changeset
|
150 if (r1 > r2) { std::swap (r1, r2); } |
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15448
diff
changeset
|
151 if (c1 > c2) { std::swap (c1, c2); } |
458 | 152 |
5275 | 153 octave_idx_type new_r = r2 - r1 + 1; |
154 octave_idx_type new_c = c2 - c1 + 1; | |
458 | 155 |
156 Matrix result (new_r, new_c); | |
157 | |
5275 | 158 for (octave_idx_type j = 0; j < new_c; j++) |
159 for (octave_idx_type i = 0; i < new_r; i++) | |
458 | 160 result.elem (i, j) = elem (r1+i, c1+j); |
161 | |
162 return result; | |
163 } | |
164 | |
165 // extract row or column i. | |
166 | |
167 RowVector | |
5275 | 168 DiagMatrix::row (octave_idx_type i) const |
458 | 169 { |
5275 | 170 octave_idx_type r = rows (); |
171 octave_idx_type c = cols (); | |
3504 | 172 if (i < 0 || i >= r) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
173 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 174 |
3504 | 175 RowVector retval (c, 0.0); |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
176 if (r <= c || i < c) |
458 | 177 retval.elem (i) = elem (i, i); |
178 | |
179 return retval; | |
180 } | |
181 | |
182 RowVector | |
183 DiagMatrix::row (char *s) const | |
184 { | |
533 | 185 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
186 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 187 |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
188 char c = s[0]; |
458 | 189 if (c == 'f' || c == 'F') |
25688
b2917b7858ba
maint: Use Octave convention for spacing of C++ cast statements.
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
190 return row (static_cast<octave_idx_type> (0)); |
458 | 191 else if (c == 'l' || c == 'L') |
192 return row (rows () - 1); | |
193 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
194 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 195 } |
196 | |
197 ColumnVector | |
5275 | 198 DiagMatrix::column (octave_idx_type i) const |
458 | 199 { |
5275 | 200 octave_idx_type r = rows (); |
201 octave_idx_type c = cols (); | |
3504 | 202 if (i < 0 || i >= c) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
203 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 204 |
3504 | 205 ColumnVector retval (r, 0.0); |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
206 if (r >= c || i < r) |
458 | 207 retval.elem (i) = elem (i, i); |
208 | |
209 return retval; | |
210 } | |
211 | |
212 ColumnVector | |
213 DiagMatrix::column (char *s) const | |
214 { | |
533 | 215 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
216 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 217 |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
218 char c = s[0]; |
458 | 219 if (c == 'f' || c == 'F') |
25688
b2917b7858ba
maint: Use Octave convention for spacing of C++ cast statements.
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
220 return column (static_cast<octave_idx_type> (0)); |
458 | 221 else if (c == 'l' || c == 'L') |
222 return column (cols () - 1); | |
223 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
224 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 225 } |
226 | |
227 DiagMatrix | |
228 DiagMatrix::inverse (void) const | |
229 { | |
8811 | 230 octave_idx_type info; |
458 | 231 return inverse (info); |
232 } | |
233 | |
234 DiagMatrix | |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
235 DiagMatrix::inverse (octave_idx_type& info) const |
458 | 236 { |
5275 | 237 octave_idx_type r = rows (); |
238 octave_idx_type c = cols (); | |
3504 | 239 if (r != c) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
240 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458 | 241 |
3504 | 242 DiagMatrix retval (r, c); |
1627 | 243 |
458 | 244 info = 0; |
27093
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
245 octave_idx_type len = r; // alias for readability |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
246 octave_idx_type z_count = 0; // zeros |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
247 octave_idx_type nz_count = 0; // non-zeros |
5275 | 248 for (octave_idx_type i = 0; i < len; i++) |
458 | 249 { |
27093
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
250 if (xelem (i, i) == 0.0) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
251 { |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
252 z_count++; |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
253 if (nz_count > 0) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
254 break; |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
255 } |
458 | 256 else |
27093
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
257 { |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
258 nz_count++; |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
259 if (z_count > 0) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
260 break; |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
261 retval.elem (i, i) = 1.0 / xelem (i, i); |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
262 } |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
263 } |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
264 if (nz_count == 0) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
265 { |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
266 (*current_liboctave_error_handler) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
267 ("inverse of the null matrix not defined"); |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
268 } |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
269 else if (z_count > 0) |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
270 { |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
271 info = -1; |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
272 element_type *data = retval.fortran_vec (); |
6e18f0ce268c
Inverse of a sparse/diagonal singular matrix should be a sparse/diagonal matrix of Infs.
marco.caliari@univr.it
parents:
26503
diff
changeset
|
273 std::fill (data, data + len, octave::numeric_limits<double>::Inf ()); |
458 | 274 } |
275 | |
1627 | 276 return retval; |
458 | 277 } |
278 | |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
279 DiagMatrix |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
280 DiagMatrix::pseudo_inverse (double tol) const |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
281 { |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
282 octave_idx_type r = rows (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
283 octave_idx_type c = cols (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
284 octave_idx_type len = length (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
285 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
286 DiagMatrix retval (c, r); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
287 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
288 for (octave_idx_type i = 0; i < len; i++) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
289 { |
18481
16b0cd465ecd
Handle special case of 0 for pinv with Diagonal matrices.
Rik <rik@octave.org>
parents:
18467
diff
changeset
|
290 double val = std::abs (elem (i, i)); |
18496
80b8873c90ba
Fix pinv bug with 0 values introduced in cset 16b0cd465ecd.
Rik <rik@octave.org>
parents:
18494
diff
changeset
|
291 if (val < tol || val == 0.0) |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
292 retval.elem (i, i) = 0.0; |
18496
80b8873c90ba
Fix pinv bug with 0 values introduced in cset 16b0cd465ecd.
Rik <rik@octave.org>
parents:
18494
diff
changeset
|
293 else |
18494 | 294 retval.elem (i, i) = 1.0 / elem (i, i); |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
295 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
296 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
297 return retval; |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
298 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
299 |
458 | 300 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
301 | |
302 // diagonal matrix by diagonal matrix -> diagonal matrix operations | |
303 | |
304 DiagMatrix | |
305 operator * (const DiagMatrix& a, const DiagMatrix& b) | |
306 { | |
5275 | 307 octave_idx_type a_nr = a.rows (); |
308 octave_idx_type a_nc = a.cols (); | |
2385 | 309 |
5275 | 310 octave_idx_type b_nr = b.rows (); |
311 octave_idx_type b_nc = b.cols (); | |
2385 | 312 |
3504 | 313 if (a_nc != b_nr) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
314 octave::err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458 | 315 |
3504 | 316 DiagMatrix c (a_nr, b_nc); |
458 | 317 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
318 octave_idx_type len = c.length (); |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23433
diff
changeset
|
319 octave_idx_type lenm = (len < a_nc ? len : a_nc); |
458 | 320 |
9698
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
321 for (octave_idx_type i = 0; i < lenm; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
322 c.dgxelem (i) = a.dgelem (i) * b.dgelem (i); |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
323 for (octave_idx_type i = lenm; i < len; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
324 c.dgxelem (i) = 0.0; |
458 | 325 |
326 return c; | |
327 } | |
328 | |
329 // other operations | |
330 | |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
331 DET |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
332 DiagMatrix::determinant (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
333 { |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
334 DET det (1.0); |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
335 if (rows () != cols ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
336 (*current_liboctave_error_handler) ("determinant requires square matrix"); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
337 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
338 octave_idx_type len = length (); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
339 for (octave_idx_type i = 0; i < len; i++) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
340 det *= elem (i, i); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
341 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
342 return det; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
343 } |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
344 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
345 double |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
346 DiagMatrix::rcond (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
347 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
348 ColumnVector av = extract_diag (0).map<double> (fabs); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
349 double amx = av.max (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
350 double amn = av.min (); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
351 return amx == 0 ? 0.0 : amn / amx; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
352 } |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
353 |
3504 | 354 std::ostream& |
355 operator << (std::ostream& os, const DiagMatrix& a) | |
458 | 356 { |
357 // int field_width = os.precision () + 7; | |
1360 | 358 |
5275 | 359 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 360 { |
5275 | 361 for (octave_idx_type j = 0; j < a.cols (); j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
362 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
363 if (i == j) |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23461
diff
changeset
|
364 os << ' ' /* setw (field_width) */ << a.elem (i, i); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
365 else |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23461
diff
changeset
|
366 os << ' ' /* setw (field_width) */ << 0.0; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
367 } |
458 | 368 os << "\n"; |
369 } | |
370 return os; | |
371 } |