7
|
1 // f-svd.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
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 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
453
|
28 #include "dbleSVD.h" |
|
29 #include "CmplxSVD.h" |
1
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
628
|
35 #include "utils.h" |
544
|
36 #include "help.h" |
519
|
37 #include "defun-dld.h" |
1
|
38 |
701
|
39 DEFUN_DLD_BUILTIN ("svd", Fsvd, Ssvd, 2, 3, |
539
|
40 "S = svd (X) or [U, S, V] = svd (X [, 0])\n\ |
519
|
41 \n\ |
539
|
42 Compute the singular value decomposition of X. Given a second input\n\ |
|
43 argument, an `economy' sized factorization is computed that omits\n\ |
|
44 unnecessary rows and columns of U and V") |
1
|
45 { |
636
|
46 Octave_object retval; |
1
|
47 |
519
|
48 int nargin = args.length (); |
|
49 |
712
|
50 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3) |
519
|
51 { |
|
52 print_usage ("svd"); |
|
53 return retval; |
|
54 } |
|
55 |
712
|
56 tree_constant arg = args(0); |
620
|
57 |
718
|
58 int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ()); |
|
59 |
|
60 if (arg_is_empty < 0) |
636
|
61 return retval; |
718
|
62 else if (arg_is_empty > 0) |
|
63 return Octave_object (3, Matrix ()); |
636
|
64 |
712
|
65 SVD::type type = (nargin == 2) ? SVD::economy : SVD::std; |
539
|
66 |
620
|
67 if (arg.is_real_type ()) |
1
|
68 { |
620
|
69 Matrix tmp = arg.matrix_value (); |
1
|
70 |
636
|
71 if (! error_state) |
|
72 { |
|
73 SVD result (tmp, type); |
622
|
74 |
636
|
75 DiagMatrix sigma = result.singular_values (); |
1
|
76 |
636
|
77 if (nargout == 0 || nargout == 1) |
|
78 { |
|
79 retval(0) = tree_constant (sigma.diag (), 1); |
|
80 } |
|
81 else |
|
82 { |
|
83 retval(2) = result.right_singular_matrix (); |
|
84 retval(1) = sigma; |
|
85 retval(0) = result.left_singular_matrix (); |
|
86 } |
620
|
87 } |
|
88 } |
|
89 else if (arg.is_complex_type ()) |
|
90 { |
|
91 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
92 |
636
|
93 if (! error_state) |
|
94 { |
|
95 ComplexSVD result (ctmp, type); |
622
|
96 |
636
|
97 DiagMatrix sigma = result.singular_values (); |
1
|
98 |
636
|
99 if (nargout == 0 || nargout == 1) |
|
100 { |
|
101 retval(0) = tree_constant (sigma.diag (), 1); |
|
102 } |
|
103 else |
|
104 { |
|
105 retval(2) = result.right_singular_matrix (); |
|
106 retval(1) = sigma; |
|
107 retval(0) = result.left_singular_matrix (); |
|
108 } |
620
|
109 } |
|
110 } |
|
111 else |
|
112 { |
|
113 gripe_wrong_type_arg ("svd", arg); |
|
114 return retval; |
1
|
115 } |
|
116 |
|
117 return retval; |
|
118 } |
|
119 |
|
120 /* |
|
121 ;;; Local Variables: *** |
|
122 ;;; mode: C++ *** |
|
123 ;;; page-delimiter: "^/\\*" *** |
|
124 ;;; End: *** |
|
125 */ |