GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pinv.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include "defun.h"
31#include "error.h"
32#include "errwarn.h"
33#include "ovl.h"
34#include "ops.h"
35#include "ov-re-diag.h"
36#include "ov-cx-diag.h"
37#include "ov-flt-re-diag.h"
38#include "ov-flt-cx-diag.h"
39#include "ov-perm.h"
40
41OCTAVE_NAMESPACE_BEGIN
42
43DEFUN (pinv, args, ,
44 doc: /* -*- texinfo -*-
45@deftypefn {} {} pinv (@var{x})
46@deftypefnx {} {} pinv (@var{x}, @var{tol})
47Return the @nospell{Moore-Penrose} pseudoinverse of @var{x}.
48
49Singular values less than @var{tol} are ignored.
50
51If the second argument is omitted, it is taken to be
52
53@example
54tol = max ([rows(@var{x}), columns(@var{x})]) * norm (@var{x}) * eps
55@end example
56
57@seealso{inv, ldivide}
58@end deftypefn */)
59{
60 int nargin = args.length ();
61
62 if (nargin < 1 || nargin > 2)
63 print_usage ();
64
65 octave_value arg = args(0);
66
67 if (arg.isempty ())
68 return ovl (Matrix ());
69
70 octave_value retval;
71
72 bool isfloat = arg.is_single_type ();
73
74 if (arg.is_diag_matrix ())
75 {
76 if (isfloat)
77 {
78 float tol = 0.0;
79 if (nargin == 2)
80 tol = args(1).float_value ();
81
82 if (tol < 0.0)
83 error ("pinv: TOL must be greater than zero");
84
85 if (arg.isreal ())
86 retval = arg.float_diag_matrix_value ().pseudo_inverse (tol);
87 else
89 }
90 else
91 {
92 double tol = 0.0;
93 if (nargin == 2)
94 tol = args(1).double_value ();
95
96 if (tol < 0.0)
97 error ("pinv: TOL must be greater than zero");
98
99 if (arg.isreal ())
100 retval = arg.diag_matrix_value ().pseudo_inverse (tol);
101 else
102 retval = arg.complex_diag_matrix_value ().pseudo_inverse (tol);
103 }
104 }
105 else if (arg.is_perm_matrix ())
106 {
107 retval = arg.perm_matrix_value ().inverse ();
108 }
109 else if (isfloat)
110 {
111 float tol = 0.0;
112 if (nargin == 2)
113 tol = args(1).float_value ();
114
115 if (tol < 0.0)
116 error ("pinv: TOL must be greater than zero");
117
118 if (arg.isreal ())
119 {
121
122 retval = m.pseudo_inverse (tol);
123 }
124 else if (arg.iscomplex ())
125 {
127
128 retval = m.pseudo_inverse (tol);
129 }
130 else
131 err_wrong_type_arg ("pinv", arg);
132 }
133 else
134 {
135 double tol = 0.0;
136 if (nargin == 2)
137 tol = args(1).double_value ();
138
139 if (tol < 0.0)
140 error ("pinv: TOL must be greater than zero");
141
142 if (arg.isreal ())
143 {
144 Matrix m = arg.matrix_value ();
145
146 retval = m.pseudo_inverse (tol);
147 }
148 else if (arg.iscomplex ())
149 {
151
152 retval = m.pseudo_inverse (tol);
153 }
154 else
155 err_wrong_type_arg ("pinv", arg);
156 }
157
158 return retval;
159}
160
161/*
162%!shared a, b, tol, hitol, d, u, x, y
163%! old_state = rand ("state");
164%! restore_state = onCleanup (@() rand ("state", old_state));
165%! rand ("state", 42); # initialize generator to make behavior reproducible
166%! a = reshape (rand*[1:16], 4, 4); # Rank 2 matrix
167%! b = pinv (a);
168%! tol = 4e-14;
169%! hitol = 40*sqrt (eps);
170%! d = diag ([rand, rand, hitol, hitol]);
171%! u = rand (4); # Could be singular by freak accident
172%! x = inv (u)*d*u;
173%! y = pinv (x, sqrt (eps));
174
175## Verify Penrose conditions for pseudoinverse
176%!assert (a*b*a, a, tol)
177%!assert (b*a*b, b, tol)
178%!assert ((b*a)', b*a, tol)
179%!assert ((a*b)', a*b, tol)
180%!assert (x*y*x, x, -hitol)
181%!assert (y*x*y, y, -hitol)
182%!assert ((x*y)', x*y, hitol)
183%!assert ((y*x)', y*x, hitol)
184
185## Clear shared variables
186%!shared
187
188## Test pinv for Diagonal matrices
189%!test
190%! x = diag ([3 2 1 0 -0.5]);
191%! y = pinv (x);
192%! assert (typeinfo (y)(1:8), "diagonal");
193%! assert (isa (y, "double"));
194%! assert (diag (y), [1/3, 1/2, 1, 0 1/-0.5]');
195%! y = pinv (x, 1);
196%! assert (diag (y), [1/3 1/2 1 0 0]');
197%! y = pinv (x, 2);
198%! assert (diag (y), [1/3 1/2 0 0 0]');
199
200## Test special case of 0 scalars and vectors
201%!assert (pinv (0), 0)
202%!assert (pinv ([0, 0, 0]), [0; 0; 0])
203%!assert (pinv (single (0)), single (0))
204%!assert (pinv (single ([0, 0, 0])), single ([0; 0; 0]))
205%!assert (pinv (complex (0,0)), 0)
206%!assert (pinv (complex ([0,0,0], [0,0,0])), [0; 0; 0])
207%!assert (pinv (complex (single (0),0)), single (0))
208%!assert (pinv (complex (single ([0,0,0]), [0,0,0])), single ([0; 0; 0]))
209*/
210
211OCTAVE_NAMESPACE_END
OCTAVE_API ComplexDiagMatrix pseudo_inverse(double tol=0.0) const
Definition: CDiagMatrix.cc:356
OCTAVE_API ComplexMatrix pseudo_inverse(double tol=0.0) const
Definition: CMatrix.cc:995
OCTAVE_API DiagMatrix pseudo_inverse(double tol=0.0) const
Definition: dDiagMatrix.cc:279
OCTAVE_API FloatComplexDiagMatrix pseudo_inverse(float tol=0.0f) const
OCTAVE_API FloatComplexMatrix pseudo_inverse(float tol=0.0) const
Definition: fCMatrix.cc:998
OCTAVE_API FloatDiagMatrix pseudo_inverse(float tol=0.0f) const
Definition: fDiagMatrix.cc:257
OCTAVE_API FloatMatrix pseudo_inverse(float tol=0.0) const
Definition: fMatrix.cc:694
Definition: dMatrix.h:42
OCTAVE_API Matrix pseudo_inverse(double tol=0.0) const
Definition: dMatrix.cc:688
OCTAVE_API PermMatrix inverse(void) const
Definition: PermMatrix.cc:114
bool isreal(void) const
Definition: ov.h:783
DiagMatrix diag_matrix_value(bool force=false) const
Definition: ov.h:955
FloatDiagMatrix float_diag_matrix_value(bool force=false) const
Definition: ov.h:958
ComplexMatrix complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:916
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool force=false) const
Definition: ov.h:965
bool is_diag_matrix(void) const
Definition: ov.h:676
ComplexDiagMatrix complex_diag_matrix_value(bool force=false) const
Definition: ov.h:961
FloatMatrix float_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:901
PermMatrix perm_matrix_value(void) const
Definition: ov.h:968
bool isempty(void) const
Definition: ov.h:646
bool is_single_type(void) const
Definition: ov.h:743
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:898
bool is_perm_matrix(void) const
Definition: ov.h:679
FloatComplexMatrix float_complex_matrix_value(bool frc_str_conv=false) const
Definition: ov.h:920
bool iscomplex(void) const
Definition: ov.h:786
OCTINTERP_API void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:980
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:166
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211