700
|
1 // f-pinv.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1994, 1995 John W. Eaton |
700
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
700
|
26 #endif |
|
27 |
|
28 #include "dMatrix.h" |
|
29 #include "CMatrix.h" |
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
|
35 #include "utils.h" |
|
36 #include "help.h" |
|
37 #include "defun-dld.h" |
|
38 |
|
39 DEFUN_DLD_BUILTIN ("pinv", Fpinv, Spinv, 3, 1, |
|
40 "pinv ( [, tol])\n\ |
|
41 Returns the pseudoinverse of X; singular values less than tol are ignored.") |
|
42 { |
|
43 Octave_object retval; |
|
44 |
|
45 int nargin = args.length (); |
|
46 |
712
|
47 if (nargin < 1 || nargin > 2) |
700
|
48 { |
|
49 print_usage ("pinv"); |
|
50 return retval; |
|
51 } |
|
52 |
712
|
53 tree_constant arg = args(0); |
700
|
54 |
|
55 double tol = 0.0; |
712
|
56 if (nargin == 2) |
|
57 tol = args(1).double_value (); |
700
|
58 |
|
59 if (error_state) |
|
60 return retval; |
|
61 |
|
62 if (tol < 0.0) |
|
63 { |
|
64 error ("pinv: tol must be greater than zero"); |
|
65 return retval; |
|
66 } |
|
67 |
|
68 int arg_is_empty = empty_arg ("pinv", arg.rows (), arg.columns ()); |
|
69 |
|
70 if (arg_is_empty < 0) |
|
71 return retval; |
718
|
72 else if (arg_is_empty > 0) |
700
|
73 return Matrix (); |
|
74 |
|
75 if (arg.is_real_type ()) |
|
76 { |
|
77 Matrix m = arg.matrix_value (); |
|
78 |
|
79 if (! error_state) |
|
80 retval = m.pseudo_inverse (tol); |
|
81 } |
|
82 else if (arg.is_complex_type ()) |
|
83 { |
|
84 ComplexMatrix m = arg.complex_matrix_value (); |
|
85 |
|
86 if (! error_state) |
|
87 retval = m.pseudo_inverse (tol); |
|
88 } |
|
89 else |
|
90 { |
|
91 gripe_wrong_type_arg ("pinv", arg); |
|
92 } |
|
93 |
|
94 return retval; |
|
95 } |
|
96 |
|
97 /* |
|
98 ;;; Local Variables: *** |
|
99 ;;; mode: C++ *** |
|
100 ;;; page-delimiter: "^/\\*" *** |
|
101 ;;; End: *** |
|
102 */ |