Mercurial > octave
annotate liboctave/array/dDiagMatrix.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" |
458 | 33 #include "mx-base.h" |
34 #include "mx-inlines.cc" | |
1650 | 35 #include "oct-cmplx.h" |
458 | 36 |
1360 | 37 // Diagonal Matrix class. |
458 | 38 |
2385 | 39 bool |
458 | 40 DiagMatrix::operator == (const DiagMatrix& a) const |
41 { | |
42 if (rows () != a.rows () || cols () != a.cols ()) | |
43 return 0; | |
44 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
45 return mx_inline_equal (length (), data (), a.data ()); |
458 | 46 } |
47 | |
2385 | 48 bool |
458 | 49 DiagMatrix::operator != (const DiagMatrix& a) const |
50 { | |
51 return !(*this == a); | |
52 } | |
53 | |
54 DiagMatrix& | |
55 DiagMatrix::fill (double val) | |
56 { | |
5275 | 57 for (octave_idx_type i = 0; i < length (); i++) |
458 | 58 elem (i, i) = val; |
59 return *this; | |
60 } | |
61 | |
62 DiagMatrix& | |
5275 | 63 DiagMatrix::fill (double val, octave_idx_type beg, octave_idx_type end) |
458 | 64 { |
65 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
|
66 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 67 |
5275 | 68 for (octave_idx_type i = beg; i <= end; i++) |
458 | 69 elem (i, i) = val; |
70 | |
71 return *this; | |
72 } | |
73 | |
74 DiagMatrix& | |
75 DiagMatrix::fill (const ColumnVector& a) | |
76 { | |
5275 | 77 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
|
78 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
|
79 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 80 |
5275 | 81 for (octave_idx_type i = 0; i < len; i++) |
458 | 82 elem (i, i) = a.elem (i); |
83 | |
84 return *this; | |
85 } | |
86 | |
87 DiagMatrix& | |
88 DiagMatrix::fill (const RowVector& a) | |
89 { | |
5275 | 90 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
|
91 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
|
92 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 93 |
5275 | 94 for (octave_idx_type i = 0; i < len; i++) |
458 | 95 elem (i, i) = a.elem (i); |
96 | |
97 return *this; | |
98 } | |
99 | |
100 DiagMatrix& | |
5275 | 101 DiagMatrix::fill (const ColumnVector& a, octave_idx_type beg) |
458 | 102 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
103 octave_idx_type a_len = a.numel (); |
458 | 104 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
|
105 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 106 |
5275 | 107 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 108 elem (i+beg, i+beg) = a.elem (i); |
109 | |
110 return *this; | |
111 } | |
112 | |
113 DiagMatrix& | |
5275 | 114 DiagMatrix::fill (const RowVector& a, octave_idx_type beg) |
458 | 115 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
116 octave_idx_type a_len = a.numel (); |
458 | 117 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
|
118 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 119 |
5275 | 120 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 121 elem (i+beg, i+beg) = a.elem (i); |
122 | |
123 return *this; | |
124 } | |
125 | |
126 DiagMatrix | |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
127 DiagMatrix::abs (void) const |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
128 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
129 return DiagMatrix (extract_diag ().abs (), rows (), columns ()); |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
130 } |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
131 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
132 DiagMatrix |
1205 | 133 real (const ComplexDiagMatrix& a) |
134 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
135 return DiagMatrix (real (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 136 } |
137 | |
138 DiagMatrix | |
139 imag (const ComplexDiagMatrix& a) | |
140 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
141 return DiagMatrix (imag (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 142 } |
143 | |
458 | 144 Matrix |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
145 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
|
146 octave_idx_type r2, octave_idx_type c2) const |
458 | 147 { |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15448
diff
changeset
|
148 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
|
149 if (c1 > c2) { std::swap (c1, c2); } |
458 | 150 |
5275 | 151 octave_idx_type new_r = r2 - r1 + 1; |
152 octave_idx_type new_c = c2 - c1 + 1; | |
458 | 153 |
154 Matrix result (new_r, new_c); | |
155 | |
5275 | 156 for (octave_idx_type j = 0; j < new_c; j++) |
157 for (octave_idx_type i = 0; i < new_r; i++) | |
458 | 158 result.elem (i, j) = elem (r1+i, c1+j); |
159 | |
160 return result; | |
161 } | |
162 | |
163 // extract row or column i. | |
164 | |
165 RowVector | |
5275 | 166 DiagMatrix::row (octave_idx_type i) const |
458 | 167 { |
5275 | 168 octave_idx_type r = rows (); |
169 octave_idx_type c = cols (); | |
3504 | 170 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
|
171 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 172 |
3504 | 173 RowVector retval (c, 0.0); |
174 if (r <= c || (r > c && i < c)) | |
458 | 175 retval.elem (i) = elem (i, i); |
176 | |
177 return retval; | |
178 } | |
179 | |
180 RowVector | |
181 DiagMatrix::row (char *s) const | |
182 { | |
533 | 183 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
184 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 185 |
186 char c = *s; | |
187 if (c == 'f' || c == 'F') | |
5275 | 188 return row (static_cast<octave_idx_type>(0)); |
458 | 189 else if (c == 'l' || c == 'L') |
190 return row (rows () - 1); | |
191 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
192 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 193 } |
194 | |
195 ColumnVector | |
5275 | 196 DiagMatrix::column (octave_idx_type i) const |
458 | 197 { |
5275 | 198 octave_idx_type r = rows (); |
199 octave_idx_type c = cols (); | |
3504 | 200 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
|
201 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 202 |
3504 | 203 ColumnVector retval (r, 0.0); |
204 if (r >= c || (r < c && i < r)) | |
458 | 205 retval.elem (i) = elem (i, i); |
206 | |
207 return retval; | |
208 } | |
209 | |
210 ColumnVector | |
211 DiagMatrix::column (char *s) const | |
212 { | |
533 | 213 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
214 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 215 |
216 char c = *s; | |
217 if (c == 'f' || c == 'F') | |
5275 | 218 return column (static_cast<octave_idx_type>(0)); |
458 | 219 else if (c == 'l' || c == 'L') |
220 return column (cols () - 1); | |
221 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
222 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 223 } |
224 | |
225 DiagMatrix | |
226 DiagMatrix::inverse (void) const | |
227 { | |
8811 | 228 octave_idx_type info; |
458 | 229 return inverse (info); |
230 } | |
231 | |
232 DiagMatrix | |
8811 | 233 DiagMatrix::inverse (octave_idx_type &info) const |
458 | 234 { |
5275 | 235 octave_idx_type r = rows (); |
236 octave_idx_type c = cols (); | |
237 octave_idx_type len = length (); | |
3504 | 238 if (r != c) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
239 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
458 | 240 |
3504 | 241 DiagMatrix retval (r, c); |
1627 | 242 |
458 | 243 info = 0; |
5275 | 244 for (octave_idx_type i = 0; i < len; i++) |
458 | 245 { |
246 if (elem (i, i) == 0.0) | |
20568
f61c67865d9f
Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103).
Rik <rik@octave.org>
parents:
20232
diff
changeset
|
247 retval.elem (i, i) = octave_Inf; |
458 | 248 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
249 retval.elem (i, i) = 1.0 / elem (i, i); |
458 | 250 } |
251 | |
1627 | 252 return retval; |
458 | 253 } |
254 | |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
255 DiagMatrix |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
256 DiagMatrix::pseudo_inverse (double tol) const |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
257 { |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
258 octave_idx_type r = rows (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
259 octave_idx_type c = cols (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
260 octave_idx_type len = length (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
261 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
262 DiagMatrix retval (c, r); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
263 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
264 for (octave_idx_type i = 0; i < len; i++) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
265 { |
18481
16b0cd465ecd
Handle special case of 0 for pinv with Diagonal matrices.
Rik <rik@octave.org>
parents:
18467
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 else |
18494 | 270 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
|
271 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
272 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
273 return retval; |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
274 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
275 |
458 | 276 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
277 | |
278 // diagonal matrix by diagonal matrix -> diagonal matrix operations | |
279 | |
280 DiagMatrix | |
281 operator * (const DiagMatrix& a, const DiagMatrix& b) | |
282 { | |
5275 | 283 octave_idx_type a_nr = a.rows (); |
284 octave_idx_type a_nc = a.cols (); | |
2385 | 285 |
5275 | 286 octave_idx_type b_nr = b.rows (); |
287 octave_idx_type b_nc = b.cols (); | |
2385 | 288 |
3504 | 289 if (a_nc != b_nr) |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20568
diff
changeset
|
290 err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458 | 291 |
3504 | 292 DiagMatrix c (a_nr, b_nc); |
458 | 293 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
294 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
|
295 octave_idx_type lenm = len < a_nc ? len : a_nc; |
458 | 296 |
9698
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
297 for (octave_idx_type i = 0; i < lenm; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
298 c.dgxelem (i) = a.dgelem (i) * b.dgelem (i); |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
299 for (octave_idx_type i = lenm; i < len; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
300 c.dgxelem (i) = 0.0; |
458 | 301 |
302 return c; | |
303 } | |
304 | |
305 // other operations | |
306 | |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
307 DET |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
308 DiagMatrix::determinant (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
309 { |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
310 DET det (1.0); |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
311 if (rows () != cols ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
312 (*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
|
313 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
314 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
|
315 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
|
316 det *= elem (i, i); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
317 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
318 return det; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
319 } |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
320 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
321 double |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
322 DiagMatrix::rcond (void) const |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
323 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
324 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
|
325 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
|
326 double amn = av.min (); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
327 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
|
328 } |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
329 |
3504 | 330 std::ostream& |
331 operator << (std::ostream& os, const DiagMatrix& a) | |
458 | 332 { |
333 // int field_width = os.precision () + 7; | |
1360 | 334 |
5275 | 335 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 336 { |
5275 | 337 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
|
338 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
339 if (i == j) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
340 os << " " /* setw (field_width) */ << a.elem (i, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
341 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
342 os << " " /* setw (field_width) */ << 0.0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
343 } |
458 | 344 os << "\n"; |
345 } | |
346 return os; | |
347 } |