annotate libinterp/corefcn/pinv.cc @ 31316:24e45a79bf3c

Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc Remove function chains as identified by grep. Use const auto& tmp as intermediate variables. See https://octave.discourse.group/t/3433 for details.
author Arun Giridhar <arungiridhar@gmail.com>
date Mon, 17 Oct 2022 16:11:02 -0400
parents 32d2b6604a9f
children 6cf02f842e74
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 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29958
diff changeset
3 // Copyright (C) 1996-2022 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 ////////////////////////////////////////////////////////////////////////
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
26 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
27 # include "config.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
29
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14179
diff changeset
30 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
31 #include "error.h"
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20941
diff changeset
32 #include "errwarn.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20939
diff changeset
33 #include "ovl.h"
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
34 #include "ops.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
35 #include "ov-re-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
36 #include "ov-cx-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
37 #include "ov-flt-re-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
38 #include "ov-flt-cx-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
39 #include "ov-perm.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
40
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
41 OCTAVE_NAMESPACE_BEGIN
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
42
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14179
diff changeset
43 DEFUN (pinv, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
44 doc: /* -*- texinfo -*-
30888
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30609
diff changeset
45 @deftypefn {} {@var{B} =} pinv (@var{A})
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30609
diff changeset
46 @deftypefnx {} {@var{B} =} pinv (@var{A}, @var{tol})
32d2b6604a9f doc: Ensure documentation lists output argument when it exists for functions in libinterp/
Rik <rik@octave.org>
parents: 30609
diff changeset
47 Return the @nospell{Moore-Penrose} pseudoinverse of @var{A}.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
48
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
49 Singular values less than @var{tol} are ignored.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
50
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
51 If the second argument is omitted, it is taken to be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
52
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
53 @example
24197
63fc7a75a3d7 doc: Update docstring for pinv and inv.
Rik <rik@octave.org>
parents: 23219
diff changeset
54 tol = max ([rows(@var{x}), columns(@var{x})]) * norm (@var{x}) * eps
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
55 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
56
24200
140f1403d603 doc: Update docstring for pinv.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 24197
diff changeset
57 @seealso{inv, ldivide}
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
58 @end deftypefn */)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
59 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
60 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
61
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
62 if (nargin < 1 || nargin > 2)
20802
8bb38ba1bad6 eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20555
diff changeset
63 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
64
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
65 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
66
30609
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
67 if (! arg.isnumeric ())
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
68 err_wrong_type_arg ("pinv", arg);
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
69
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23220
diff changeset
70 if (arg.isempty ())
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21100
diff changeset
71 return ovl (Matrix ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
72
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
73 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
74
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
75 bool isfloat = arg.is_single_type ();
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
76
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
77 if (arg.is_diag_matrix ())
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
78 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
79 if (isfloat)
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
80 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
81 float tol = 0.0;
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
82 if (nargin == 2)
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
83 tol = args(1).float_value ();
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
84
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
85 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
86 error ("pinv: TOL must be greater than zero");
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
87
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
88 if (arg.isreal ())
31316
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
89 {
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
90 const auto& tmp = arg.float_diag_matrix_value ();
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
91 retval = tmp.pseudo_inverse (tol);
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
92 }
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
93 else
31316
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
94 {
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
95 const auto& tmp = arg.float_complex_diag_matrix_value ();
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
96 retval = tmp.pseudo_inverse (tol);
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
97 }
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
98 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
99 else
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
100 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
101 double tol = 0.0;
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
102 if (nargin == 2)
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
103 tol = args(1).double_value ();
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
104
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
105 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
106 error ("pinv: TOL must be greater than zero");
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
107
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
108 if (arg.isreal ())
31316
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
109 {
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
110 const auto& tmp = arg.diag_matrix_value ();
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
111 retval = tmp.pseudo_inverse (tol);
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
112 }
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
113 else
31316
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
114 {
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
115 const auto& tmp = arg.complex_diag_matrix_value ();
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
116 retval = tmp.pseudo_inverse (tol);
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
117 }
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
118 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
119 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
120 else if (arg.is_perm_matrix ())
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
121 {
31316
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
122 const auto& tmp = arg.perm_matrix_value ();
24e45a79bf3c Avoid function chains in det.cc, inv.cc, pinv.cc, __dsearchn__.cc
Arun Giridhar <arungiridhar@gmail.com>
parents: 30888
diff changeset
123 retval = tmp.inverse ();
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
124 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
125 else if (isfloat)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
126 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
127 float tol = 0.0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
128 if (nargin == 2)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
129 tol = args(1).float_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
130
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
131 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
132 error ("pinv: TOL must be greater than zero");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
133
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
134 if (arg.isreal ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
135 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
136 FloatMatrix m = arg.float_matrix_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
137
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
138 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
139 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23577
diff changeset
140 else if (arg.iscomplex ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
141 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
142 FloatComplexMatrix m = arg.float_complex_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
143
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
144 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
145 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
146 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20941
diff changeset
147 err_wrong_type_arg ("pinv", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
148 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
149 else
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
150 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
151 double tol = 0.0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
152 if (nargin == 2)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
153 tol = args(1).double_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
154
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
155 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
156 error ("pinv: TOL must be greater than zero");
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
157
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
158 if (arg.isreal ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
159 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
160 Matrix m = arg.matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
161
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
162 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
163 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23577
diff changeset
164 else if (arg.iscomplex ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
165 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
166 ComplexMatrix m = arg.complex_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
167
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
168 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
169 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
170 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20941
diff changeset
171 err_wrong_type_arg ("pinv", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
172 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
173
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
174 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
175 }
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
176
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
177 /*
13079
9512d7272d5b codesprint: Fix tolerance for pinv test
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 13042
diff changeset
178 %!shared a, b, tol, hitol, d, u, x, y
25124
435645a627dd Fix random seed for pinv BIST tests (bug #53560).
Rik <rik@octave.org>
parents: 25054
diff changeset
179 %! old_state = rand ("state");
435645a627dd Fix random seed for pinv BIST tests (bug #53560).
Rik <rik@octave.org>
parents: 25054
diff changeset
180 %! restore_state = onCleanup (@() rand ("state", old_state));
435645a627dd Fix random seed for pinv BIST tests (bug #53560).
Rik <rik@octave.org>
parents: 25054
diff changeset
181 %! rand ("state", 42); # initialize generator to make behavior reproducible
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 15195
diff changeset
182 %! a = reshape (rand*[1:16], 4, 4); # Rank 2 matrix
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
183 %! b = pinv (a);
14179
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
184 %! tol = 4e-14;
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
185 %! hitol = 40*sqrt (eps);
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
186 %! d = diag ([rand, rand, hitol, hitol]);
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 15195
diff changeset
187 %! u = rand (4); # Could be singular by freak accident
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
188 %! x = inv (u)*d*u;
14179
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
189 %! y = pinv (x, sqrt (eps));
25124
435645a627dd Fix random seed for pinv BIST tests (bug #53560).
Rik <rik@octave.org>
parents: 25054
diff changeset
190
435645a627dd Fix random seed for pinv BIST tests (bug #53560).
Rik <rik@octave.org>
parents: 25054
diff changeset
191 ## Verify Penrose conditions for pseudoinverse
14179
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
192 %!assert (a*b*a, a, tol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
193 %!assert (b*a*b, b, tol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
194 %!assert ((b*a)', b*a, tol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
195 %!assert ((a*b)', a*b, tol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
196 %!assert (x*y*x, x, -hitol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
197 %!assert (y*x*y, y, -hitol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
198 %!assert ((x*y)', x*y, hitol)
a7a020cd6106 Relax tolerance for pinv tests so that it fails less than 1% of the time.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
199 %!assert ((y*x)', y*x, hitol)
18494
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
200
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
201 ## Clear shared variables
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
202 %!shared
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
203
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
204 ## Test pinv for Diagonal matrices
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
205 %!test
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
206 %! x = diag ([3 2 1 0 -0.5]);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
207 %! y = pinv (x);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
208 %! assert (typeinfo (y)(1:8), "diagonal");
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
209 %! assert (isa (y, "double"));
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
210 %! assert (diag (y), [1/3, 1/2, 1, 0 1/-0.5]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
211 %! y = pinv (x, 1);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
212 %! assert (diag (y), [1/3 1/2 1 0 0]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
213 %! y = pinv (x, 2);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
214 %! assert (diag (y), [1/3 1/2 0 0 0]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
215
30609
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
216 ## Basic test for integer inputs
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
217 %!assert (pinv (int32 (2)), 0.5)
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
218 %!assert (pinv (uint32 (2)), 0.5)
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
219 %!assert (pinv (int64 (2)), 0.5)
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
220 %!assert (pinv (uint64 (2)), 0.5)
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
221
24195
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
222 ## Test special case of 0 scalars and vectors
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
223 %!assert (pinv (0), 0)
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
224 %!assert (pinv ([0, 0, 0]), [0; 0; 0])
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
225 %!assert (pinv (single (0)), single (0))
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
226 %!assert (pinv (single ([0, 0, 0])), single ([0; 0; 0]))
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
227 %!assert (pinv (complex (0,0)), 0)
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
228 %!assert (pinv (complex ([0,0,0], [0,0,0])), [0; 0; 0])
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
229 %!assert (pinv (complex (single (0),0)), single (0))
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
230 %!assert (pinv (complex (single ([0,0,0]), [0,0,0])), single ([0; 0; 0]))
30609
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
231
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
232 ## Test input validation
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
233 %!error <wrong type argument> pinv ("Hello World")
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
234 %!error <wrong type argument> pinv ({1})
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
235 %!error <wrong type argument> pinv (true)
09012191bb0c pinv: Improve input validation (Bug #61767).
Antonius Burgers <arburgers@gmail.com>
parents: 30564
diff changeset
236
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
237 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
238
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
239 OCTAVE_NAMESPACE_END