2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "defun-dld.h" |
|
28 #include "error.h" |
|
29 #include "gripes.h" |
|
30 #include "oct-obj.h" |
|
31 #include "utils.h" |
|
32 |
|
33 DEFUN_DLD (pinv, args, , |
3365
|
34 "-*- texinfo -*-\n\ |
3372
|
35 @deftypefn {Loadable Function} {} pinv (@var{x}, @var{tol})\n\ |
|
36 Return the pseudoinverse of @var{x}. Singular values less than\n\ |
|
37 @var{tol} are ignored. \n\ |
|
38 \n\ |
|
39 If the second argument is omitted, it is assumed that\n\ |
3365
|
40 \n\ |
|
41 @example\n\ |
3372
|
42 tol = max (size (@var{x})) * sigma_max (@var{x}) * eps,\n\ |
3365
|
43 @end example\n\ |
3372
|
44 \n\ |
|
45 @noindent\n\ |
|
46 where @code{sigma_max (@var{x})} is the maximal singular value of @var{x}.\n\ |
3365
|
47 @end deftypefn") |
2928
|
48 { |
|
49 octave_value_list retval; |
|
50 |
|
51 int nargin = args.length (); |
|
52 |
|
53 if (nargin < 1 || nargin > 2) |
|
54 { |
|
55 print_usage ("pinv"); |
|
56 return retval; |
|
57 } |
|
58 |
|
59 octave_value arg = args(0); |
|
60 |
|
61 double tol = 0.0; |
|
62 if (nargin == 2) |
|
63 tol = args(1).double_value (); |
|
64 |
|
65 if (error_state) |
|
66 return retval; |
|
67 |
|
68 if (tol < 0.0) |
|
69 { |
|
70 error ("pinv: tol must be greater than zero"); |
|
71 return retval; |
|
72 } |
|
73 |
|
74 int arg_is_empty = empty_arg ("pinv", arg.rows (), arg.columns ()); |
|
75 |
|
76 if (arg_is_empty < 0) |
|
77 return retval; |
|
78 else if (arg_is_empty > 0) |
|
79 return Matrix (); |
|
80 |
|
81 if (arg.is_real_type ()) |
|
82 { |
|
83 Matrix m = arg.matrix_value (); |
|
84 |
|
85 if (! error_state) |
|
86 retval = m.pseudo_inverse (tol); |
|
87 } |
|
88 else if (arg.is_complex_type ()) |
|
89 { |
|
90 ComplexMatrix m = arg.complex_matrix_value (); |
|
91 |
|
92 if (! error_state) |
|
93 retval = m.pseudo_inverse (tol); |
|
94 } |
|
95 else |
|
96 { |
|
97 gripe_wrong_type_arg ("pinv", arg); |
|
98 } |
|
99 |
|
100 return retval; |
|
101 } |
|
102 |
|
103 /* |
|
104 ;;; Local Variables: *** |
|
105 ;;; mode: C++ *** |
|
106 ;;; End: *** |
|
107 */ |