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 "help.h" |
|
31 #include "oct-obj.h" |
|
32 #include "utils.h" |
|
33 |
|
34 DEFUN_DLD (pinv, args, , |
|
35 "pinv ( [, tol])\n\ |
|
36 Returns the pseudoinverse of X; singular values less than tol are ignored.") |
|
37 { |
|
38 octave_value_list retval; |
|
39 |
|
40 int nargin = args.length (); |
|
41 |
|
42 if (nargin < 1 || nargin > 2) |
|
43 { |
|
44 print_usage ("pinv"); |
|
45 return retval; |
|
46 } |
|
47 |
|
48 octave_value arg = args(0); |
|
49 |
|
50 double tol = 0.0; |
|
51 if (nargin == 2) |
|
52 tol = args(1).double_value (); |
|
53 |
|
54 if (error_state) |
|
55 return retval; |
|
56 |
|
57 if (tol < 0.0) |
|
58 { |
|
59 error ("pinv: tol must be greater than zero"); |
|
60 return retval; |
|
61 } |
|
62 |
|
63 int arg_is_empty = empty_arg ("pinv", arg.rows (), arg.columns ()); |
|
64 |
|
65 if (arg_is_empty < 0) |
|
66 return retval; |
|
67 else if (arg_is_empty > 0) |
|
68 return Matrix (); |
|
69 |
|
70 if (arg.is_real_type ()) |
|
71 { |
|
72 Matrix m = arg.matrix_value (); |
|
73 |
|
74 if (! error_state) |
|
75 retval = m.pseudo_inverse (tol); |
|
76 } |
|
77 else if (arg.is_complex_type ()) |
|
78 { |
|
79 ComplexMatrix m = arg.complex_matrix_value (); |
|
80 |
|
81 if (! error_state) |
|
82 retval = m.pseudo_inverse (tol); |
|
83 } |
|
84 else |
|
85 { |
|
86 gripe_wrong_type_arg ("pinv", arg); |
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
|
92 /* |
|
93 ;;; Local Variables: *** |
|
94 ;;; mode: C++ *** |
|
95 ;;; End: *** |
|
96 */ |