Mercurial > octave
annotate liboctave/array/dDiagMatrix.cc @ 32660:f53ac65ffba6
maint: New method rwdata() as clearer alternative to fortran_vec().
* etc/NEWS.10.md: Announce new method and recommend its use in future code.
* doc/interpreter/external.txi: Update Octave manual to use rwdata.
* liboctave/array/Array.h (rwdata): New function prototype.
* liboctave/array/Array.h (fortran_vec): Make inline function that calls rwdata().
* liboctave/array/DiagArray2.h (rwdata): New function prototype.
* liboctave/array/DiagArray2.h (fortran_vec): Make inline function that calls rwdata().
* examples/code/fortrandemo.cc, libgui/graphics/QtHandlesUtils.cc,
libinterp/corefcn/Cell.cc, libinterp/corefcn/__dsearchn__.cc,
libinterp/corefcn/__ichol__.cc, libinterp/corefcn/__ilu__.cc,
libinterp/corefcn/__lin_interpn__.cc, libinterp/corefcn/__magick_read__.cc,
libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/amd.cc,
libinterp/corefcn/data.cc, libinterp/corefcn/dot.cc,
libinterp/corefcn/ellipj.cc, libinterp/corefcn/filter.cc,
libinterp/corefcn/gcd.cc, libinterp/corefcn/gl-render.cc,
libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h,
libinterp/corefcn/kron.cc, libinterp/corefcn/ls-mat4.cc,
libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/mex.cc,
libinterp/corefcn/oct-map.cc, libinterp/corefcn/oct-stream.cc,
libinterp/corefcn/ordqz.cc, libinterp/corefcn/ordschur.cc,
libinterp/corefcn/perms.cc, libinterp/corefcn/psi.cc,
libinterp/corefcn/quadcc.cc, libinterp/corefcn/qz.cc,
libinterp/corefcn/rand.cc, libinterp/corefcn/sqrtm.cc,
libinterp/corefcn/strfind.cc, libinterp/corefcn/symrcm.cc,
libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc,
libinterp/corefcn/xdiv.cc, libinterp/dldfcn/__delaunayn__.cc,
libinterp/dldfcn/__glpk__.cc, libinterp/dldfcn/__ode15__.cc,
libinterp/dldfcn/__voronoi__.cc, libinterp/dldfcn/audioread.cc,
libinterp/dldfcn/convhulln.cc, libinterp/octave-value/ov-base-int.cc,
libinterp/octave-value/ov-bool-mat.cc, libinterp/octave-value/ov-ch-mat.cc,
libinterp/octave-value/ov-cx-diag.cc, libinterp/octave-value/ov-cx-mat.cc,
libinterp/octave-value/ov-flt-cx-diag.cc,
libinterp/octave-value/ov-flt-cx-mat.cc,
libinterp/octave-value/ov-flt-re-diag.cc,
libinterp/octave-value/ov-flt-re-mat.cc, libinterp/octave-value/ov-intx.h,
libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-perm.cc,
libinterp/octave-value/ov-re-diag.cc, libinterp/octave-value/ov-re-mat.cc,
libinterp/octave-value/ov-str-mat.cc, liboctave/array/Array-base.cc,
liboctave/array/Array-util.cc, liboctave/array/CColVector.cc,
liboctave/array/CDiagMatrix.cc, liboctave/array/CMatrix.cc,
liboctave/array/CNDArray.cc, liboctave/array/CRowVector.cc,
liboctave/array/CSparse.cc, liboctave/array/DiagArray2.h,
liboctave/array/MArray.cc, liboctave/array/PermMatrix.cc,
liboctave/array/Range.h, liboctave/array/Sparse.cc,
liboctave/array/boolSparse.cc, liboctave/array/dColVector.cc,
liboctave/array/dDiagMatrix.cc, liboctave/array/dMatrix.cc,
liboctave/array/dNDArray.cc, liboctave/array/dRowVector.cc,
liboctave/array/dSparse.cc, liboctave/array/fCColVector.cc,
liboctave/array/fCDiagMatrix.cc, liboctave/array/fCMatrix.cc,
liboctave/array/fCNDArray.cc, liboctave/array/fCRowVector.cc,
liboctave/array/fColVector.cc, liboctave/array/fMatrix.cc,
liboctave/array/fNDArray.cc, liboctave/array/fRowVector.cc,
liboctave/array/idx-vector.cc, liboctave/numeric/CollocWt.cc,
liboctave/numeric/DASPK.cc, liboctave/numeric/DASRT.cc,
liboctave/numeric/DASSL.cc, liboctave/numeric/EIG.cc,
liboctave/numeric/LSODE.cc, liboctave/numeric/Quad.cc,
liboctave/numeric/aepbalance.cc, liboctave/numeric/bsxfun-defs.cc,
liboctave/numeric/chol.cc, liboctave/numeric/eigs-base.cc,
liboctave/numeric/fEIG.cc, liboctave/numeric/gepbalance.cc,
liboctave/numeric/gsvd.cc, liboctave/numeric/hess.cc, liboctave/numeric/lu.cc,
liboctave/numeric/oct-convn.cc, liboctave/numeric/oct-rand.cc,
liboctave/numeric/qr.cc, liboctave/numeric/qrp.cc, liboctave/numeric/schur.cc,
liboctave/numeric/sparse-dmsolve.cc, liboctave/numeric/sparse-lu.cc,
liboctave/numeric/sparse-qr.cc, liboctave/numeric/svd.cc,
liboctave/operators/mx-inlines.cc, liboctave/operators/mx-op-defs.h,
liboctave/util/oct-base64.cc, liboctave/util/oct-binmap.h,
liboctave/util/str-vec.cc:
Replace calls to fortran_vec() with calls to rwdata().
author | Rik <rik@octave.org> |
---|---|
date | Wed, 27 Dec 2023 16:55:14 -0800 |
parents | 2e484f9f1f18 |
children |
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 // |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31898
diff
changeset
|
3 // Copyright (C) 1994-2024 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
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/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
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 //////////////////////////////////////////////////////////////////////// |
458 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21723
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21202
diff
changeset
|
27 # include "config.h" |
458 | 28 #endif |
29 | |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
30 #include <ostream> |
458 | 31 |
4669 | 32 #include "Array-util.h" |
1368 | 33 #include "lo-error.h" |
458 | 34 #include "mx-base.h" |
35 #include "mx-inlines.cc" | |
1650 | 36 #include "oct-cmplx.h" |
458 | 37 |
1360 | 38 // Diagonal Matrix class. |
458 | 39 |
2385 | 40 bool |
458 | 41 DiagMatrix::operator == (const DiagMatrix& a) const |
42 { | |
43 if (rows () != a.rows () || cols () != a.cols ()) | |
44 return 0; | |
45 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
46 return mx_inline_equal (length (), data (), a.data ()); |
458 | 47 } |
48 | |
2385 | 49 bool |
458 | 50 DiagMatrix::operator != (const DiagMatrix& a) const |
51 { | |
52 return !(*this == a); | |
53 } | |
54 | |
55 DiagMatrix& | |
56 DiagMatrix::fill (double val) | |
57 { | |
5275 | 58 for (octave_idx_type i = 0; i < length (); i++) |
458 | 59 elem (i, i) = val; |
60 return *this; | |
61 } | |
62 | |
63 DiagMatrix& | |
5275 | 64 DiagMatrix::fill (double val, octave_idx_type beg, octave_idx_type end) |
458 | 65 { |
66 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
|
67 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 68 |
5275 | 69 for (octave_idx_type i = beg; i <= end; i++) |
458 | 70 elem (i, i) = val; |
71 | |
72 return *this; | |
73 } | |
74 | |
75 DiagMatrix& | |
76 DiagMatrix::fill (const ColumnVector& a) | |
77 { | |
5275 | 78 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
|
79 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
|
80 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 81 |
5275 | 82 for (octave_idx_type i = 0; i < len; i++) |
458 | 83 elem (i, i) = a.elem (i); |
84 | |
85 return *this; | |
86 } | |
87 | |
88 DiagMatrix& | |
89 DiagMatrix::fill (const RowVector& a) | |
90 { | |
5275 | 91 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
|
92 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
|
93 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 94 |
5275 | 95 for (octave_idx_type i = 0; i < len; i++) |
458 | 96 elem (i, i) = a.elem (i); |
97 | |
98 return *this; | |
99 } | |
100 | |
101 DiagMatrix& | |
5275 | 102 DiagMatrix::fill (const ColumnVector& a, octave_idx_type beg) |
458 | 103 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
104 octave_idx_type a_len = a.numel (); |
458 | 105 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
|
106 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 107 |
5275 | 108 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 109 elem (i+beg, i+beg) = a.elem (i); |
110 | |
111 return *this; | |
112 } | |
113 | |
114 DiagMatrix& | |
5275 | 115 DiagMatrix::fill (const RowVector& a, octave_idx_type beg) |
458 | 116 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
117 octave_idx_type a_len = a.numel (); |
458 | 118 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
|
119 (*current_liboctave_error_handler) ("range error for fill"); |
458 | 120 |
5275 | 121 for (octave_idx_type i = 0; i < a_len; i++) |
458 | 122 elem (i+beg, i+beg) = a.elem (i); |
123 | |
124 return *this; | |
125 } | |
126 | |
127 DiagMatrix | |
31771
21f9b34eb893
maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents:
31706
diff
changeset
|
128 DiagMatrix::abs () const |
8366
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
129 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
130 return DiagMatrix (extract_diag ().abs (), rows (), columns ()); |
8366
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 |
8b1a2555c4e2
implement diagonal matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
7924
diff
changeset
|
133 DiagMatrix |
1205 | 134 real (const ComplexDiagMatrix& a) |
135 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
136 return DiagMatrix (real (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 137 } |
138 | |
139 DiagMatrix | |
140 imag (const ComplexDiagMatrix& a) | |
141 { | |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
142 return DiagMatrix (imag (a.extract_diag ()), a.rows (), a.cols ()); |
1205 | 143 } |
144 | |
458 | 145 Matrix |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
146 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
|
147 octave_idx_type r2, octave_idx_type c2) const |
458 | 148 { |
17663
7975d75f933c
Use std::swap in liboctave instead of temporary variable.
Rik <rik@octave.org>
parents:
15448
diff
changeset
|
149 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
|
150 if (c1 > c2) { std::swap (c1, c2); } |
458 | 151 |
5275 | 152 octave_idx_type new_r = r2 - r1 + 1; |
153 octave_idx_type new_c = c2 - c1 + 1; | |
458 | 154 |
155 Matrix result (new_r, new_c); | |
156 | |
5275 | 157 for (octave_idx_type j = 0; j < new_c; j++) |
158 for (octave_idx_type i = 0; i < new_r; i++) | |
458 | 159 result.elem (i, j) = elem (r1+i, c1+j); |
160 | |
161 return result; | |
162 } | |
163 | |
164 // extract row or column i. | |
165 | |
166 RowVector | |
5275 | 167 DiagMatrix::row (octave_idx_type i) const |
458 | 168 { |
5275 | 169 octave_idx_type r = rows (); |
170 octave_idx_type c = cols (); | |
3504 | 171 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
|
172 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 173 |
3504 | 174 RowVector retval (c, 0.0); |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
175 if (r <= c || i < c) |
458 | 176 retval.elem (i) = elem (i, i); |
177 | |
178 return retval; | |
179 } | |
180 | |
181 RowVector | |
182 DiagMatrix::row (char *s) const | |
183 { | |
533 | 184 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
185 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 186 |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
187 char c = s[0]; |
458 | 188 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
|
189 return row (static_cast<octave_idx_type> (0)); |
458 | 190 else if (c == 'l' || c == 'L') |
191 return row (rows () - 1); | |
192 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
193 (*current_liboctave_error_handler) ("invalid row selection"); |
458 | 194 } |
195 | |
196 ColumnVector | |
5275 | 197 DiagMatrix::column (octave_idx_type i) const |
458 | 198 { |
5275 | 199 octave_idx_type r = rows (); |
200 octave_idx_type c = cols (); | |
3504 | 201 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
|
202 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 203 |
3504 | 204 ColumnVector retval (r, 0.0); |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
205 if (r >= c || i < r) |
458 | 206 retval.elem (i) = elem (i, i); |
207 | |
208 return retval; | |
209 } | |
210 | |
211 ColumnVector | |
212 DiagMatrix::column (char *s) const | |
213 { | |
533 | 214 if (! s) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
215 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 216 |
26503
a534d1be2aa0
dDiagMatrix.cc: Fix static analyzer detected issues (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
217 char c = s[0]; |
458 | 218 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
|
219 return column (static_cast<octave_idx_type> (0)); |
458 | 220 else if (c == 'l' || c == 'L') |
221 return column (cols () - 1); | |
222 else | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
223 (*current_liboctave_error_handler) ("invalid column selection"); |
458 | 224 } |
225 | |
226 DiagMatrix | |
31771
21f9b34eb893
maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents:
31706
diff
changeset
|
227 DiagMatrix::inverse () const |
458 | 228 { |
8811 | 229 octave_idx_type info; |
458 | 230 return inverse (info); |
231 } | |
232 | |
233 DiagMatrix | |
23433
c9fab0bc983e
maint: Use convention 'int& x' for naming references.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
234 DiagMatrix::inverse (octave_idx_type& info) const |
458 | 235 { |
5275 | 236 octave_idx_type r = rows (); |
237 octave_idx_type c = cols (); | |
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; |
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
|
244 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
|
245 octave_idx_type z_count = 0; // zeros |
31898
0db880f38b1f
maint: Use "nonzero" rather than "non-zero" in code base.
Rik <rik@octave.org>
parents:
31771
diff
changeset
|
246 octave_idx_type nz_count = 0; // nonzeros |
5275 | 247 for (octave_idx_type i = 0; i < len; i++) |
458 | 248 { |
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
|
249 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
|
250 { |
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 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
|
252 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
|
253 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
|
254 } |
458 | 255 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
|
256 { |
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 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
|
258 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
|
259 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
|
260 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
|
261 } |
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 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
|
264 { |
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 (*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
|
266 ("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
|
267 } |
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 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
|
269 { |
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 info = -1; |
32660
f53ac65ffba6
maint: New method rwdata() as clearer alternative to fortran_vec().
Rik <rik@octave.org>
parents:
32632
diff
changeset
|
271 element_type *data = retval.rwdata (); |
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
|
272 std::fill (data, data + len, octave::numeric_limits<double>::Inf ()); |
458 | 273 } |
274 | |
1627 | 275 return retval; |
458 | 276 } |
277 | |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
278 DiagMatrix |
18467
c5a101de2d88
Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents:
18084
diff
changeset
|
279 DiagMatrix::pseudo_inverse (double tol) const |
8840
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
280 { |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
281 octave_idx_type r = rows (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
282 octave_idx_type c = cols (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
283 octave_idx_type len = length (); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
284 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
285 DiagMatrix retval (c, r); |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
286 |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
287 for (octave_idx_type i = 0; i < len; i++) |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
288 { |
18481
16b0cd465ecd
Handle special case of 0 for pinv with Diagonal matrices.
Rik <rik@octave.org>
parents:
18467
diff
changeset
|
289 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
|
290 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
|
291 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
|
292 else |
18494 | 293 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
|
294 } |
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 return retval; |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
297 } |
c690e3772583
support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents:
8811
diff
changeset
|
298 |
458 | 299 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
300 | |
301 // diagonal matrix by diagonal matrix -> diagonal matrix operations | |
302 | |
303 DiagMatrix | |
304 operator * (const DiagMatrix& a, const DiagMatrix& b) | |
305 { | |
5275 | 306 octave_idx_type a_nr = a.rows (); |
307 octave_idx_type a_nc = a.cols (); | |
2385 | 308 |
5275 | 309 octave_idx_type b_nr = b.rows (); |
310 octave_idx_type b_nc = b.cols (); | |
2385 | 311 |
3504 | 312 if (a_nc != b_nr) |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
313 octave::err_nonconformant ("operator *", a_nr, a_nc, b_nr, b_nc); |
458 | 314 |
3504 | 315 DiagMatrix c (a_nr, b_nc); |
458 | 316 |
18084
8e056300994b
Follow coding convention of defining and initializing only 1 variable per line in liboctave.
Rik <rik@octave.org>
parents:
17769
diff
changeset
|
317 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
|
318 octave_idx_type lenm = (len < a_nc ? len : a_nc); |
458 | 319 |
9698
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
320 for (octave_idx_type i = 0; i < lenm; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
321 c.dgxelem (i) = a.dgelem (i) * b.dgelem (i); |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
322 for (octave_idx_type i = lenm; i < len; i++) |
7c6d5d8c8d37
fix diag*diag multiplication
Jaroslav Hajek <highegg@gmail.com>
parents:
9601
diff
changeset
|
323 c.dgxelem (i) = 0.0; |
458 | 324 |
325 return c; | |
326 } | |
327 | |
328 // other operations | |
329 | |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
330 DET |
31771
21f9b34eb893
maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents:
31706
diff
changeset
|
331 DiagMatrix::determinant () const |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
332 { |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
333 DET det (1.0); |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
334 if (rows () != cols ()) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
335 (*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
|
336 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
337 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
|
338 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
|
339 det *= elem (i, i); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
340 |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
341 return det; |
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
342 } |
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 double |
31771
21f9b34eb893
maint: Eliminate "(void)" in C++ function prototypes/declarations.
Rik <rik@octave.org>
parents:
31706
diff
changeset
|
345 DiagMatrix::rcond () const |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
346 { |
15448
0a0912a9ab6e
Replace deprecated DiagArray2<T>::diag calls with DiagArray2<T>::extract_diag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15271
diff
changeset
|
347 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
|
348 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
|
349 double amn = av.min (); |
8371
c3f7e2549abb
make det & inv aware of diagonal & permutation matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8367
diff
changeset
|
350 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
|
351 } |
8367
445d27d79f4e
support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
8366
diff
changeset
|
352 |
3504 | 353 std::ostream& |
354 operator << (std::ostream& os, const DiagMatrix& a) | |
458 | 355 { |
356 // int field_width = os.precision () + 7; | |
1360 | 357 |
5275 | 358 for (octave_idx_type i = 0; i < a.rows (); i++) |
458 | 359 { |
5275 | 360 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
|
361 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
362 if (i == j) |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23461
diff
changeset
|
363 os << ' ' /* setw (field_width) */ << a.elem (i, i); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
364 else |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23461
diff
changeset
|
365 os << ' ' /* setw (field_width) */ << 0.0; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
366 } |
458 | 367 os << "\n"; |
368 } | |
369 return os; | |
370 } |