Mercurial > octave
annotate liboctave/array/CDiagMatrix.cc @ 21202:f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
* Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc, Array-f.cc, Array-fC.cc,
Array-i.cc, Array-idx-vec.cc, Array-s.cc, Array-str.cc, Array-util.cc,
Array-voidp.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc,
CNDArray.cc, CRowVector.cc, CSparse.cc, CSparse.h, DiagArray2.cc, MArray-C.cc,
MArray-d.cc, MArray-f.cc, MArray-fC.cc, MArray-i.cc, MArray-s.cc, MArray.cc,
MDiagArray2.cc, MSparse-C.cc, MSparse-d.cc, MSparse.h, MatrixType.cc,
PermMatrix.cc, Range.cc, Sparse-C.cc, Sparse-b.cc, Sparse-d.cc, Sparse.cc,
boolMatrix.cc, boolNDArray.cc, boolSparse.cc, chMatrix.cc, chNDArray.cc,
dColVector.cc, dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc,
dSparse.cc, dSparse.h, dim-vector.cc, fCColVector.cc, fCDiagMatrix.cc,
fCMatrix.cc, fCNDArray.cc, fCRowVector.cc, fColVector.cc, fDiagMatrix.cc,
fMatrix.cc, fNDArray.cc, fRowVector.cc, idx-vector.cc, int16NDArray.cc,
int32NDArray.cc, int64NDArray.cc, int8NDArray.cc, intNDArray.cc,
uint16NDArray.cc, uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc,
blaswrap.c, cquit.c, f77-extern.cc, f77-fcn.c, f77-fcn.h, lo-error.c, quit.cc,
quit.h, CmplxAEPBAL.cc, CmplxCHOL.cc, CmplxGEPBAL.cc, CmplxHESS.cc, CmplxLU.cc,
CmplxQR.cc, CmplxQRP.cc, CmplxSCHUR.cc, CmplxSVD.cc, CollocWt.cc, DASPK.cc,
DASRT.cc, DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, base-lu.cc, base-qr.cc,
dbleAEPBAL.cc, dbleCHOL.cc, dbleGEPBAL.cc, dbleHESS.cc, dbleLU.cc, dbleQR.cc,
dbleQRP.cc, dbleSCHUR.cc, dbleSVD.cc, eigs-base.cc, fCmplxAEPBAL.cc,
fCmplxCHOL.cc, fCmplxGEPBAL.cc, fCmplxHESS.cc, fCmplxLU.cc, fCmplxQR.cc,
fCmplxQRP.cc, fCmplxSCHUR.cc, fCmplxSVD.cc, fEIG.cc, floatAEPBAL.cc,
floatCHOL.cc, floatGEPBAL.cc, floatHESS.cc, floatLU.cc, floatQR.cc,
floatQRP.cc, floatSCHUR.cc, floatSVD.cc, lo-mappers.cc, lo-specfun.cc,
oct-convn.cc, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-rand.cc,
oct-spparms.cc, randgamma.c, randmtzig.c, randpoisson.c, sparse-chol.cc,
sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, mx-defs.h, dir-ops.cc,
file-ops.cc, file-stat.cc, lo-sysdep.cc, mach-info.cc, oct-env.cc,
oct-group.cc, oct-openmp.h, oct-passwd.cc, oct-syscalls.cc, oct-time.cc,
oct-uname.cc, pathlen.h, sysdir.h, syswait.h, cmd-edit.cc, cmd-hist.cc,
data-conv.cc, f2c-main.c, glob-match.cc, lo-array-errwarn.cc,
lo-array-gripes.cc, lo-cutils.c, lo-cutils.h, lo-ieee.cc, lo-math.h,
lo-regexp.cc, lo-utils.cc, oct-base64.cc, oct-glob.cc, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.cc, oct-mutex.cc, oct-refcount.h, oct-rl-edit.c,
oct-rl-hist.c, oct-shlib.cc, oct-sort.cc, pathsearch.cc, singleton-cleanup.cc,
sparse-sort.cc, sparse-util.cc, statdefs.h, str-vec.cc, unwind-prot.cc,
url-transfer.cc, display-available.h, main-cli.cc, main-gui.cc, main.in.cc,
mkoctfile.in.cc, octave-config.in.cc, shared-fcns.h:
indent #ifdef blocks in liboctave and src directories.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 06 Feb 2016 06:40:13 -0800 |
parents | 7cac4e7458f2 |
children | 40de9f8f23a6 |
rev | line source |
---|---|
1993 | 1 // DiagMatrix manipulations. |
458 | 2 /* |
3 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18496
diff
changeset
|
4 Copyright (C) 1994-2015 John W. Eaton |
9601
a9b37bae1802
add a couple of missing copyright statements
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
458 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
458 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
458 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21136
diff
changeset
|
26 # include <config.h> |
458 | 27 #endif |
28 | |
3503 | 29 #include <iostream> |
458 | 30 |
4669 | 31 #include "Array-util.h" |
1368 | 32 #include "lo-error.h" |
9003
0631d397fbe0
replace lo_ieee_isnan by xisnan, add missing includes
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
33 #include "lo-ieee.h" |
458 | 34 #include "mx-base.h" |
35 #include "mx-inlines.cc" | |
1650 | 36 #include "oct-cmplx.h" |
458 | 37 |
1360 | 38 // Complex Diagonal Matrix class |
458 | 39 |
40 ComplexDiagMatrix::ComplexDiagMatrix (const DiagMatrix& a) | |
1989 | 41 : MDiagArray2<Complex> (a.rows (), a.cols ()) |
458 | 42 { |
5275 | 43 for (octave_idx_type i = 0; i < length (); i++) |
458 | 44 elem (i, i) = a.elem (i, i); |
45 } | |
46 | |
2386 | 47 bool |
458 | 48 ComplexDiagMatrix::operator == (const ComplexDiagMatrix& a) const |
49 { | |
50 if (rows () != a.rows () || cols () != a.cols ()) | |
51 return 0; | |
52 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9003
diff
changeset
|
53 return mx_inline_equal (length (), data (), a.data ()); |
458 | 54 } |
55 | |
2386 | 56 bool |
458 | 57 ComplexDiagMatrix::operator != (const ComplexDiagMatrix& a) const |
58 { | |
59 return !(*this == a); | |
60 } | |
61 | |
62 ComplexDiagMatrix& | |
63 ComplexDiagMatrix::fill (double val) | |
64 { | |
5275 | 65 for (octave_idx_type i = 0; i < length (); i++) |
458 | 66 elem (i, i) = val; |
67 return *this; | |
68 } | |
69 | |
70 ComplexDiagMatrix& | |
71 ComplexDiagMatrix::fill (const Complex& val) | |
72 { | |
5275 | 73 for (octave_idx_type i = 0; i < length (); i++) |
458 | 74 elem (i, i) = val; |
75 return *this; | |
76 } | |
77 | |
78 ComplexDiagMatrix& | |
5275 | 79 ComplexDiagMatrix::fill (double val, octave_idx_type beg, octave_idx_type end) |
458 | 80 { |
81 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:
21129
diff
changeset
|
82 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 83 |
5275 | 84 for (octave_idx_type i = beg; i <= end; i++) |
458 | 85 elem (i, i) = val; |
86 | |
87 return *this; | |
88 } | |
89 | |
90 ComplexDiagMatrix& | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 ComplexDiagMatrix::fill (const Complex& val, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
92 octave_idx_type beg, octave_idx_type end) |
458 | 93 { |
94 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:
21129
diff
changeset
|
95 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 96 |
5275 | 97 for (octave_idx_type i = beg; i <= end; i++) |
458 | 98 elem (i, i) = val; |
99 | |
100 return *this; | |
101 } | |
102 | |
103 ComplexDiagMatrix& | |
104 ComplexDiagMatrix::fill (const ColumnVector& a) | |
105 { | |
5275 | 106 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
|
107 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
108 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 109 |
5275 | 110 for (octave_idx_type i = 0; i < len; i++) |
458 | 111 elem (i, i) = a.elem (i); |
112 | |
113 return *this; | |
114 } | |
115 | |
116 ComplexDiagMatrix& | |
117 ComplexDiagMatrix::fill (const ComplexColumnVector& a) | |
118 { | |
5275 | 119 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
|
120 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
121 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 122 |
5275 | 123 for (octave_idx_type i = 0; i < len; i++) |
458 | 124 elem (i, i) = a.elem (i); |
125 | |
126 return *this; | |
127 } | |
128 | |
129 ComplexDiagMatrix& | |
130 ComplexDiagMatrix::fill (const RowVector& a) | |
131 { | |
5275 | 132 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
|
133 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
134 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 135 |
5275 | 136 for (octave_idx_type i = 0; i < len; i++) |
458 | 137 elem (i, i) = a.elem (i); |
138 | |
139 return *this; | |
140 } | |
141 | |
142 ComplexDiagMatrix& | |
143 ComplexDiagMatrix::fill (const ComplexRowVector& a) | |
144 { | |
5275 | 145 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
|
146 if (a.numel () != len) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
147 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 148 |
5275 | 149 for (octave_idx_type i = 0; i < len; i++) |
458 | 150 elem (i, i) = a.elem (i); |
151 | |
152 return *this; | |
153 } | |
154 | |
155 ComplexDiagMatrix& | |
5275 | 156 ComplexDiagMatrix::fill (const ColumnVector& a, octave_idx_type beg) |
458 | 157 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
158 octave_idx_type a_len = a.numel (); |
458 | 159 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:
21129
diff
changeset
|
160 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 161 |
5275 | 162 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 163 elem (i+beg, i+beg) = a.elem (i); |
164 | |
165 return *this; | |
166 } | |
167 | |
168 ComplexDiagMatrix& | |
5275 | 169 ComplexDiagMatrix::fill (const ComplexColumnVector& a, octave_idx_type beg) |
458 | 170 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
171 octave_idx_type a_len = a.numel (); |
458 | 172 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:
21129
diff
changeset
|
173 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 174 |
5275 | 175 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 176 elem (i+beg, i+beg) = a.elem (i); |
177 | |
178 return *this; | |
179 } | |
180 | |
181 ComplexDiagMatrix& | |
5275 | 182 ComplexDiagMatrix::fill (const RowVector& a, octave_idx_type beg) |
458 | 183 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
184 octave_idx_type a_len = a.numel (); |
458 | 185 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:
21129
diff
changeset
|
186 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 187 |
5275 | 188 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 189 elem (i+beg, i+beg) = a.elem (i); |
190 | |
191 return *this; | |
192 } | |
193 | |
194 ComplexDiagMatrix& | |
5275 | 195 ComplexDiagMatrix::fill (const ComplexRowVector& a, octave_idx_type beg) |
458 | 196 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
197 octave_idx_type a_len = a.numel (); |
458 | 198 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:
21129
diff
changeset
|
199 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 200 |
5275 | 201 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 202 elem (i+beg, i+beg) = a.elem (i); |
203 | |
204 return *this; | |
205 } | |
206 | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
207 DiagMatrix |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
208 ComplexDiagMatrix::abs (void) const |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
209 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
210 return DiagMatrix (extract_diag ().abs (), rows (), columns ()); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
211 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
212 |
458 | 213 ComplexDiagMatrix |
214 conj (const ComplexDiagMatrix& a) | |
215 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
216 return ComplexDiagMatrix (conj (a.extract_diag ()), a.rows (), a.columns ()); |
458 | 217 } |
218 | |
219 // resize is the destructive analog for this one | |
220 | |
221 ComplexMatrix | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
222 ComplexDiagMatrix::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
|
223 octave_idx_type r2, octave_idx_type c2) const |
458 | 224 { |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15448
diff
changeset
|
225 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
|
226 if (c1 > c2) { std::swap (c1, c2); } |
458 | 227 |
5275 | 228 octave_idx_type new_r = r2 - r1 + 1; |
229 octave_idx_type new_c = c2 - c1 + 1; | |
458 | 230 |
231 ComplexMatrix result (new_r, new_c); | |
232 | |
5275 | 233 for (octave_idx_type j = 0; j < new_c; j++) |
234 for (octave_idx_type i = 0; i < new_r; i++) | |
458 | 235 result.elem (i, j) = elem (r1+i, c1+j); |
236 | |
237 return result; | |
238 } | |
239 | |
240 // extract row or column i. | |
241 | |
242 ComplexRowVector | |
5275 | 243 ComplexDiagMatrix::row (octave_idx_type i) const |
458 | 244 { |
5275 | 245 octave_idx_type r = rows (); |
246 octave_idx_type c = cols (); | |
3504 | 247 if (i < 0 || i >= r) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
248 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 249 |
3504 | 250 ComplexRowVector retval (c, 0.0); |
251 if (r <= c || (r > c && i < c)) | |
458 | 252 retval.elem (i) = elem (i, i); |
253 | |
254 return retval; | |
255 } | |
256 | |
257 ComplexRowVector | |
258 ComplexDiagMatrix::row (char *s) const | |
259 { | |
533 | 260 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
261 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 262 |
263 char c = *s; | |
264 if (c == 'f' || c == 'F') | |
5275 | 265 return row (static_cast<octave_idx_type>(0)); |
458 | 266 else if (c == 'l' || c == 'L') |
267 return row (rows () - 1); | |
268 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
269 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 270 } |
271 | |
272 ComplexColumnVector | |
5275 | 273 ComplexDiagMatrix::column (octave_idx_type i) const |
458 | 274 { |
5275 | 275 octave_idx_type r = rows (); |
276 octave_idx_type c = cols (); | |
3504 | 277 if (i < 0 || i >= c) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
278 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 279 |
3504 | 280 ComplexColumnVector retval (r, 0.0); |
281 if (r >= c || (r < c && i < r)) | |
458 | 282 retval.elem (i) = elem (i, i); |
283 | |
284 return retval; | |
285 } | |
286 | |
287 ComplexColumnVector | |
288 ComplexDiagMatrix::column (char *s) const | |
289 { | |
533 | 290 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
291 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 292 |
293 char c = *s; | |
294 if (c == 'f' || c == 'F') | |
5275 | 295 return column (static_cast<octave_idx_type>(0)); |
458 | 296 else if (c == 'l' || c == 'L') |
297 return column (cols () - 1); | |
298 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
299 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 300 } |
301 | |
302 ComplexDiagMatrix | |
303 ComplexDiagMatrix::inverse (void) const | |
304 { | |
8811 | 305 octave_idx_type info; |
458 | 306 return inverse (info); |
307 } | |
308 | |
309 ComplexDiagMatrix | |
8811 | 310 ComplexDiagMatrix::inverse (octave_idx_type& info) const |
458 | 311 { |
5275 | 312 octave_idx_type r = rows (); |
313 octave_idx_type c = cols (); | |
3504 | 314 if (r != c) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
315 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458 | 316 |
3504 | 317 ComplexDiagMatrix retval (r, c); |
458 | 318 |
319 info = 0; | |
5275 | 320 for (octave_idx_type i = 0; i < length (); i++) |
458 | 321 { |
322 if (elem (i, i) == 0.0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
323 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
324 info = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
325 return *this; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
326 } |
458 | 327 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
328 retval.elem (i, i) = 1.0 / elem (i, i); |
458 | 329 } |
330 | |
1627 | 331 return retval; |
458 | 332 } |
333 | |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
334 ComplexDiagMatrix |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
335 ComplexDiagMatrix::pseudo_inverse (double tol) const |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
336 { |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
337 octave_idx_type r = rows (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
338 octave_idx_type c = cols (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
339 octave_idx_type len = length (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
340 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
341 ComplexDiagMatrix retval (c, r); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
342 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
343 for (octave_idx_type i = 0; i < len; i++) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
344 { |
18481
16b0cd465ecd
Handle special case of 0 for pinv with Diagonal matrices.
Rik <rik@octave.org>
parents:
18467
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 else |
18494 | 349 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
|
350 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
351 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
352 return retval; |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
353 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
354 |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
355 bool |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
356 ComplexDiagMatrix::all_elements_are_real (void) const |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
357 { |
9825
7483fe200fab
narrow complex values with negative zero imaginary parts
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
358 return mx_inline_all_real (length (), data ()); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
359 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
360 |
458 | 361 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
362 | |
363 ComplexDiagMatrix& | |
364 ComplexDiagMatrix::operator += (const DiagMatrix& a) | |
365 { | |
5275 | 366 octave_idx_type r = rows (); |
367 octave_idx_type c = cols (); | |
2386 | 368 |
5275 | 369 octave_idx_type a_nr = a.rows (); |
370 octave_idx_type a_nc = a.cols (); | |
2386 | 371 |
3504 | 372 if (r != a_nr || c != a_nc) |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
373 err_nonconformant ("operator +=", r, c, a_nr, a_nc); |
458 | 374 |
3504 | 375 if (r == 0 || c == 0) |
458 | 376 return *this; |
377 | |
378 Complex *d = fortran_vec (); // Ensures only one reference to my privates! | |
379 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9003
diff
changeset
|
380 mx_inline_add2 (length (), d, a.data ()); |
458 | 381 return *this; |
382 } | |
383 | |
384 ComplexDiagMatrix | |
385 operator * (const ComplexDiagMatrix& a, const DiagMatrix& b) | |
386 { | |
5275 | 387 octave_idx_type a_nr = a.rows (); |
388 octave_idx_type a_nc = a.cols (); | |
2386 | 389 |
5275 | 390 octave_idx_type b_nr = b.rows (); |
391 octave_idx_type b_nc = b.cols (); | |
2386 | 392 |
3504 | 393 if (a_nc != b_nr) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
394 err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458 | 395 |
3504 | 396 ComplexDiagMatrix c (a_nr, b_nc); |
458 | 397 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
398 octave_idx_type len = c.length (); |
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
399 octave_idx_type lenm = len < a_nc ? len : a_nc; |
458 | 400 |
9698
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
401 for (octave_idx_type i = 0; i < lenm; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
402 c.dgxelem (i) = a.dgelem (i) * b.dgelem (i); |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
403 for (octave_idx_type i = lenm; i < len; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
404 c.dgxelem (i) = 0.0; |
458 | 405 |
406 return c; | |
407 } | |
408 | |
409 ComplexDiagMatrix | |
1205 | 410 operator * (const DiagMatrix& a, const ComplexDiagMatrix& b) |
411 { | |
5275 | 412 octave_idx_type a_nr = a.rows (); |
413 octave_idx_type a_nc = a.cols (); | |
2386 | 414 |
5275 | 415 octave_idx_type b_nr = b.rows (); |
416 octave_idx_type b_nc = b.cols (); | |
2386 | 417 |
3504 | 418 if (a_nc != b_nr) |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
419 err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
1205 | 420 |
3504 | 421 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
422 return ComplexDiagMatrix (a_nr, a_nc, 0.0); | |
1205 | 423 |
3504 | 424 ComplexDiagMatrix c (a_nr, b_nc); |
1205 | 425 |
5275 | 426 octave_idx_type len = a_nr < b_nc ? a_nr : b_nc; |
1205 | 427 |
5275 | 428 for (octave_idx_type i = 0; i < len; i++) |
1205 | 429 { |
430 double a_element = a.elem (i, i); | |
431 Complex b_element = b.elem (i, i); | |
432 | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
433 c.elem (i, i) = a_element * b_element; |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
434 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
435 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
436 return c; |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
437 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
438 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
439 ComplexDiagMatrix |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
440 operator * (const ComplexDiagMatrix& a, const ComplexDiagMatrix& b) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
441 { |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
442 octave_idx_type a_nr = a.rows (); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
443 octave_idx_type a_nc = a.cols (); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
444 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
445 octave_idx_type b_nr = b.rows (); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
446 octave_idx_type b_nc = b.cols (); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
447 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
448 if (a_nc != b_nr) |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
449 err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
450 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
451 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
452 return ComplexDiagMatrix (a_nr, a_nc, 0.0); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
453 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
454 ComplexDiagMatrix c (a_nr, b_nc); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
455 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
456 octave_idx_type len = a_nr < b_nc ? a_nr : b_nc; |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
457 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
458 for (octave_idx_type i = 0; i < len; i++) |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
459 { |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
460 Complex a_element = a.elem (i, i); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
461 Complex b_element = b.elem (i, i); |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
462 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7789
diff
changeset
|
463 c.elem (i, i) = a_element * b_element; |
1205 | 464 } |
465 | |
466 return c; | |
467 } | |
468 | |
458 | 469 // other operations |
470 | |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
471 ComplexDET |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
472 ComplexDiagMatrix::determinant (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
473 { |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
474 ComplexDET det (1.0); |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
475 if (rows () != cols ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
476 (*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:
21129
diff
changeset
|
477 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
478 octave_idx_type len = length (); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
479 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:
21129
diff
changeset
|
480 det *= elem (i, i); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
481 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
482 return det; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
483 } |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
484 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
485 double |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
486 ComplexDiagMatrix::rcond (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
487 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
488 ColumnVector av = extract_diag (0).map<double> (std::abs); |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
489 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
|
490 double amn = av.min (); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
491 return amx == 0 ? 0.0 : amn / amx; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
492 } |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
493 |
458 | 494 // i/o |
495 | |
3504 | 496 std::ostream& |
497 operator << (std::ostream& os, const ComplexDiagMatrix& a) | |
458 | 498 { |
499 Complex ZERO (0.0); | |
500 // int field_width = os.precision () + 7; | |
5275 | 501 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 502 { |
5275 | 503 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
|
504 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
505 if (i == j) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
506 os << " " /* setw (field_width) */ << a.elem (i, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
507 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
508 os << " " /* setw (field_width) */ << ZERO; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
509 } |
458 | 510 os << "\n"; |
511 } | |
512 return os; | |
513 } |