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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
28 #endif
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
31
4669
334a27c8f453 [project @ 2003-11-26 07:02:42 by jwe]
jwe
parents: 4513
diff changeset
32 #include "Array-util.h"
1368
33bb7975f866 [project @ 1995-09-06 00:12:48 by jwe]
jwe
parents: 1360
diff changeset
33 #include "lo-error.h"
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
34 #include "mx-base.h"
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
35 #include "mx-inlines.cc"
1650
23aa282707e8 [project @ 1995-12-20 06:53:12 by jwe]
jwe
parents: 1627
diff changeset
36 #include "oct-cmplx.h"
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
37
1360
7eb93d12654c [project @ 1995-09-05 21:51:54 by jwe]
jwe
parents: 1315
diff changeset
38 // Diagonal Matrix class.
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
39
2385
170053c0f75e [project @ 1996-10-12 18:23:01 by jwe]
jwe
parents: 1993
diff changeset
40 bool
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
41 DiagMatrix::operator == (const DiagMatrix& a) const
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
42 {
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
43 if (rows () != a.rows () || cols () != a.cols ())
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
44 return 0;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
47 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
48
2385
170053c0f75e [project @ 1996-10-12 18:23:01 by jwe]
jwe
parents: 1993
diff changeset
49 bool
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
50 DiagMatrix::operator != (const DiagMatrix& a) const
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
51 {
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
52 return !(*this == a);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
53 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
54
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
55 DiagMatrix&
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
56 DiagMatrix::fill (double val)
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
57 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
58 for (octave_idx_type i = 0; i < length (); i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
59 elem (i, i) = val;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
60 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
61 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
62
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
63 DiagMatrix&
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
64 DiagMatrix::fill (double val, octave_idx_type beg, octave_idx_type end)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
65 {
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
68
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
69 for (octave_idx_type i = beg; i <= end; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
70 elem (i, i) = val;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
71
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
72 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
73 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
74
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
75 DiagMatrix&
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
76 DiagMatrix::fill (const ColumnVector& a)
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
77 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
81
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
82 for (octave_idx_type i = 0; i < len; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
83 elem (i, i) = a.elem (i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
84
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
85 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
86 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
87
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
88 DiagMatrix&
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
89 DiagMatrix::fill (const RowVector& a)
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
90 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
94
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
95 for (octave_idx_type i = 0; i < len; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
96 elem (i, i) = a.elem (i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
97
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
98 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
99 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
100
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
101 DiagMatrix&
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
102 DiagMatrix::fill (const ColumnVector& a, octave_idx_type beg)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
107
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
108 for (octave_idx_type i = 0; i < a_len; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
109 elem (i+beg, i+beg) = a.elem (i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
110
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
111 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
112 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
113
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
114 DiagMatrix&
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
115 DiagMatrix::fill (const RowVector& a, octave_idx_type beg)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
120
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
121 for (octave_idx_type i = 0; i < a_len; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
122 elem (i+beg, i+beg) = a.elem (i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
123
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
124 return *this;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
125 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
126
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
134 real (const ComplexDiagMatrix& a)
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
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
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
137 }
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
138
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
139 DiagMatrix
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
140 imag (const ComplexDiagMatrix& a)
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
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
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
143 }
8302fab9fe24 [project @ 1995-04-04 02:05:01 by jwe]
jwe
parents: 1192
diff changeset
144
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
151
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
152 octave_idx_type new_r = r2 - r1 + 1;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
153 octave_idx_type new_c = c2 - c1 + 1;
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
154
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
155 Matrix result (new_r, new_c);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
156
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
157 for (octave_idx_type j = 0; j < new_c; j++)
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
158 for (octave_idx_type i = 0; i < new_r; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
159 result.elem (i, j) = elem (r1+i, c1+j);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
160
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
161 return result;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
162 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
163
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
164 // extract row or column i.
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
165
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
166 RowVector
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
167 DiagMatrix::row (octave_idx_type i) const
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
168 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
169 octave_idx_type r = rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
170 octave_idx_type c = cols ();
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
173
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
176 retval.elem (i) = elem (i, i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
177
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
178 return retval;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
179 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
180
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
181 RowVector
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
182 DiagMatrix::row (char *s) const
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
183 {
533
883197c5ad75 [project @ 1994-07-20 20:02:51 by jwe]
jwe
parents: 458
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
190 else if (c == 'l' || c == 'L')
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
191 return row (rows () - 1);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
194 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
195
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
196 ColumnVector
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
197 DiagMatrix::column (octave_idx_type i) const
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
198 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
199 octave_idx_type r = rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
200 octave_idx_type c = cols ();
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
203
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
206 retval.elem (i) = elem (i, i);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
207
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
208 return retval;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
209 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
210
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
211 ColumnVector
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
212 DiagMatrix::column (char *s) const
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
213 {
533
883197c5ad75 [project @ 1994-07-20 20:02:51 by jwe]
jwe
parents: 458
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
220 else if (c == 'l' || c == 'L')
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
221 return column (cols () - 1);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
224 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
225
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
228 {
8811
20dfb885f877 int -> octave_idx fixes
John W. Eaton <jwe@octave.org>
parents: 8375
diff changeset
229 octave_idx_type info;
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
230 return inverse (info);
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
231 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
232
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
235 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
236 octave_idx_type r = rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
237 octave_idx_type c = cols ();
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
240
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
241 DiagMatrix retval (r, c);
1627
8f798f963805 [project @ 1995-11-13 14:37:05 by jwe]
jwe
parents: 1368
diff changeset
242
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
247 for (octave_idx_type i = 0; i < len; i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
273 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
274
1627
8f798f963805 [project @ 1995-11-13 14:37:05 by jwe]
jwe
parents: 1368
diff changeset
275 return retval;
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
276 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18481
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
299 // diagonal matrix by diagonal matrix -> diagonal matrix operations
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
300
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
301 // diagonal matrix by diagonal matrix -> diagonal matrix operations
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
302
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
303 DiagMatrix
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
304 operator * (const DiagMatrix& a, const DiagMatrix& b)
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
305 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
306 octave_idx_type a_nr = a.rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
307 octave_idx_type a_nc = a.cols ();
2385
170053c0f75e [project @ 1996-10-12 18:23:01 by jwe]
jwe
parents: 1993
diff changeset
308
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
309 octave_idx_type b_nr = b.rows ();
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
310 octave_idx_type b_nc = b.cols ();
2385
170053c0f75e [project @ 1996-10-12 18:23:01 by jwe]
jwe
parents: 1993
diff changeset
311
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
314
3504
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
315 DiagMatrix c (a_nr, b_nc);
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
324
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
325 return c;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
326 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
327
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
328 // other operations
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
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
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
353 std::ostream&
5eef8a2294bd [project @ 2000-02-01 10:06:51 by jwe]
jwe
parents: 3503
diff changeset
354 operator << (std::ostream& os, const DiagMatrix& a)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
355 {
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
356 // int field_width = os.precision () + 7;
1360
7eb93d12654c [project @ 1995-09-05 21:51:54 by jwe]
jwe
parents: 1315
diff changeset
357
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
358 for (octave_idx_type i = 0; i < a.rows (); i++)
458
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
359 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5105
diff changeset
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
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
367 os << "\n";
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
368 }
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
369 return os;
38cb88095913 [project @ 1994-06-06 00:41:10 by jwe]
jwe
parents:
diff changeset
370 }