comparison src/pinv.cc @ 700:5d852fb75139

[project @ 1994-09-15 02:23:24 by jwe] Initial revision
author jwe
date Thu, 15 Sep 1994 02:23:24 +0000
parents
children 36ba0576bd1b
comparison
equal deleted inserted replaced
699:e380975350e5 700:5d852fb75139
1 // f-pinv.cc -*- C++ -*-
2 /*
3
4 Copyright (C) 1994 John W. Eaton
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
25 #include "config.h"
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
47 if (nargin < 2 || nargin > 3)
48 {
49 print_usage ("pinv");
50 return retval;
51 }
52
53 tree_constant arg = args(1);
54
55 double tol = 0.0;
56 if (nargin == 3)
57 tol = args(2).double_value ();
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;
72 else if (arg_is_empty)
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 */