annotate libinterp/corefcn/pinv.cc @ 24195:d3dc76efb38b

Return correct value for pinv of zero vectors and scalars (bug #51246). CMatrix.cc, dMatrix.cc (pseudo_inverse): Change any computed tolerance of 0 to std::numeric_limits<double>::min() so that zero values are later dropped from calculation. fCMatrix.cc, fMatrix.cc (pseudo_inverse): Change calculation of default tolerance to us the <float> version of std::numeric_limits::epsilon () rather than the double version. Change any computed tolerance of 0 to std::numeric_limits<float>::min() so that zero values are later dropped from calculation. * pinv.cc (Fpinv): Add BIST tests.
author Rik <rik@octave.org>
date Fri, 03 Nov 2017 10:03:36 -0700
parents 0cc2011d800e
children 11cdca79d316
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
1 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2
23219
3ac9f9ecfae5 maint: Update copyright dates.
John W. Eaton <jwe@octave.org>
parents: 23083
diff changeset
3 Copyright (C) 1996-2017 John W. Eaton
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
4
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
8 under the terms of the GNU General Public License as published by
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
9 the Free Software Foundation; either version 3 of the License, or
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
10 (at your option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
11
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
12 Octave is distributed in the hope that it will be useful, but
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
13 WITHOUT ANY WARRANTY; without even the implied warranty of
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22407
diff changeset
15 GNU General Public License for more details.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5823
diff changeset
18 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5823
diff changeset
19 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
20
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
21 */
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
22
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
23 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
24 # include "config.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14179
diff changeset
27 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #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
29 #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
30 #include "ovl.h"
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
31 #include "ops.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
32 #include "ov-re-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
33 #include "ov-cx-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
34 #include "ov-flt-re-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
35 #include "ov-flt-cx-diag.h"
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
36 #include "ov-perm.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
37
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14179
diff changeset
38 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
39 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
40 @deftypefn {} {} pinv (@var{x})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
41 @deftypefnx {} {} pinv (@var{x}, @var{tol})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
42 Return the pseudoinverse of @var{x}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
43
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
44 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
45
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
46 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
47
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
48 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
49 tol = max (size (@var{x})) * sigma_max (@var{x}) * eps,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
50 @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
51
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
52 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
53 where @code{sigma_max (@var{x})} is the maximal singular value of @var{x}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
54 @end deftypefn */)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
55 {
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
56 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
57
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
58 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
59 print_usage ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
60
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
61 octave_value arg = args(0);
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
62
23577
80c42f4cca13 maint: Deprecate is_empty and replace with isempty.
Rik <rik@octave.org>
parents: 23220
diff changeset
63 if (arg.isempty ())
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21100
diff changeset
64 return ovl (Matrix ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
65
20939
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
66 octave_value retval;
b17fda023ca6 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20853
diff changeset
67
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
68 bool isfloat = arg.is_single_type ();
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
69
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
70 if (arg.is_diag_matrix ())
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
71 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
72 if (isfloat)
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
73 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
74 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
75 if (nargin == 2)
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
76 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
77
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
78 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
79 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
80
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
81 if (arg.isreal ())
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
82 retval = arg.float_diag_matrix_value ().pseudo_inverse (tol);
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
83 else
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
84 retval = arg.float_complex_diag_matrix_value ().pseudo_inverse (tol);
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
85 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
86 else
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
87 {
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
88 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
89 if (nargin == 2)
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
90 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
91
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
92 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
93 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
94
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
95 if (arg.isreal ())
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
96 retval = arg.diag_matrix_value ().pseudo_inverse (tol);
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
97 else
18467
c5a101de2d88 Allow pinv to work on Diagonal Matrices with a tolerance (bug #41546).
Rik <rik@octave.org>
parents: 17787
diff changeset
98 retval = arg.complex_diag_matrix_value ().pseudo_inverse (tol);
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
99 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
100 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
101 else if (arg.is_perm_matrix ())
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
102 {
8960
93f18f166aba remove float perm matrices
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
103 retval = arg.perm_matrix_value ().inverse ();
8840
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
104 }
c690e3772583 support diagonal matrices in pinv
Jaroslav Hajek <highegg@gmail.com>
parents: 7789
diff changeset
105 else if (isfloat)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
106 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
107 float tol = 0.0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
108 if (nargin == 2)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
109 tol = args(1).float_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
110
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
111 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
112 error ("pinv: TOL must be greater than zero");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
113
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
114 if (arg.isreal ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
115 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
116 FloatMatrix m = arg.float_matrix_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
117
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
118 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
119 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23577
diff changeset
120 else if (arg.iscomplex ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
121 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
122 FloatComplexMatrix m = arg.float_complex_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
123
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
124 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
125 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
126 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20941
diff changeset
127 err_wrong_type_arg ("pinv", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
128 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
129 else
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
130 {
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
131 double tol = 0.0;
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
132 if (nargin == 2)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
133 tol = args(1).double_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
134
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
135 if (tol < 0.0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20802
diff changeset
136 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
137
23582
0cc2011d800e maint: Deprecate is_real_type and replace with isreal.
Rik <rik@octave.org>
parents: 23581
diff changeset
138 if (arg.isreal ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
139 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
140 Matrix m = arg.matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
141
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
142 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
143 }
23581
c3075ae020e1 maint: Deprecate is_complex_type and replace with iscomplex.
Rik <rik@octave.org>
parents: 23577
diff changeset
144 else if (arg.iscomplex ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
145 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
146 ComplexMatrix m = arg.complex_matrix_value ();
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
147
20555
f90c8372b7ba eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20172
diff changeset
148 retval = m.pseudo_inverse (tol);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
149 }
7789
82be108cc558 First attempt at single precision tyeps
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
150 else
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 20941
diff changeset
151 err_wrong_type_arg ("pinv", arg);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
152 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
153
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
154 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
155 }
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
156
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
157 /*
13079
9512d7272d5b codesprint: Fix tolerance for pinv test
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 13042
diff changeset
158 %!shared a, b, tol, hitol, d, u, x, y
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 15195
diff changeset
159 %! 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
160 %! 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
161 %! 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
162 %! hitol = 40*sqrt (eps);
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
163 %! 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
164 %! 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
165 %! 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
166 %! y = pinv (x, sqrt (eps));
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
167 %!
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
168 %!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
169 %!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
170 %!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
171 %!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
172 %!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
173 %!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
174 %!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
175 %!assert ((y*x)', y*x, hitol)
18494
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
176
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
177 ## Clear shared variables
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
178 %!shared
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
179
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
180 ## Test pinv for Diagonal matrices
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
181 %!test
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
182 %! x = diag ([3 2 1 0 -0.5]);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
183 %! y = pinv (x);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
184 %! assert (typeinfo (y)(1:8), "diagonal");
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
185 %! assert (isa (y, "double"));
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
186 %! assert (diag (y), [1/3, 1/2, 1, 0 1/-0.5]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
187 %! y = pinv (x, 1);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
188 %! assert (diag (y), [1/3 1/2 1 0 0]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
189 %! y = pinv (x, 2);
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
190 %! assert (diag (y), [1/3 1/2 0 0 0]');
1fa5bba16218 [mq]: pinv
Rik <rik@octave.org>
parents: 18467
diff changeset
191
24195
d3dc76efb38b Return correct value for pinv of zero vectors and scalars (bug #51246).
Rik <rik@octave.org>
parents: 23582
diff changeset
192 ## 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
193 %!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
194 %!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
195 %!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
196 %!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
197 %!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
198 %!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
199 %!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
200 %!assert (pinv (complex (single ([0,0,0]), [0,0,0])), single ([0; 0; 0]))
13042
ca7aaf2689c3 codesprint: 8 tests for pinv.cc
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12642
diff changeset
201 */