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 "CmplxSVD.h" |
|
28 #include "dbleSVD.h" |
|
29 |
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "pr-output.h" |
|
35 #include "utils.h" |
|
36 |
|
37 DEFUN_DLD (svd, args, nargout, |
3548
|
38 "-*- texinfo -*-\n\ |
3372
|
39 @deftypefn {Loadable Function} {@var{s} =} svd (@var{a})\n\ |
|
40 @deftypefnx {Loadable Function} {[@var{u}, @var{s}, @var{v}] =} svd (@var{a})\n\ |
|
41 @cindex singular value decomposition\n\ |
|
42 Compute the singular value decomposition of @var{a}\n\ |
|
43 @iftex\n\ |
|
44 @tex\n\ |
|
45 $$\n\ |
|
46 A = U\\Sigma V^H\n\ |
|
47 $$\n\ |
|
48 @end tex\n\ |
|
49 @end iftex\n\ |
|
50 @ifinfo\n\ |
|
51 \n\ |
|
52 @example\n\ |
|
53 a = u * sigma * v'\n\ |
|
54 @end example\n\ |
|
55 @end ifinfo\n\ |
|
56 \n\ |
|
57 The function @code{svd} normally returns the vector of singular values.\n\ |
|
58 If asked for three return values, it computes\n\ |
|
59 @iftex\n\ |
|
60 @tex\n\ |
|
61 $U$, $S$, and $V$.\n\ |
|
62 @end tex\n\ |
|
63 @end iftex\n\ |
|
64 @ifinfo\n\ |
|
65 U, S, and V.\n\ |
|
66 @end ifinfo\n\ |
|
67 For example,\n\ |
|
68 \n\ |
|
69 @example\n\ |
|
70 svd (hilb (3))\n\ |
|
71 @end example\n\ |
|
72 \n\ |
|
73 @noindent\n\ |
|
74 returns\n\ |
2928
|
75 \n\ |
3372
|
76 @example\n\ |
|
77 ans =\n\ |
|
78 \n\ |
|
79 1.4083189\n\ |
|
80 0.1223271\n\ |
|
81 0.0026873\n\ |
|
82 @end example\n\ |
|
83 \n\ |
|
84 @noindent\n\ |
|
85 and\n\ |
|
86 \n\ |
|
87 @example\n\ |
|
88 [u, s, v] = svd (hilb (3))\n\ |
|
89 @end example\n\ |
|
90 \n\ |
|
91 @noindent\n\ |
|
92 returns\n\ |
|
93 \n\ |
|
94 @example\n\ |
|
95 u =\n\ |
2928
|
96 \n\ |
3372
|
97 -0.82704 0.54745 0.12766\n\ |
|
98 -0.45986 -0.52829 -0.71375\n\ |
|
99 -0.32330 -0.64901 0.68867\n\ |
|
100 \n\ |
|
101 s =\n\ |
|
102 \n\ |
|
103 1.40832 0.00000 0.00000\n\ |
|
104 0.00000 0.12233 0.00000\n\ |
|
105 0.00000 0.00000 0.00269\n\ |
|
106 \n\ |
|
107 v =\n\ |
|
108 \n\ |
|
109 -0.82704 0.54745 0.12766\n\ |
|
110 -0.45986 -0.52829 -0.71375\n\ |
|
111 -0.32330 -0.64901 0.68867\n\ |
|
112 @end example\n\ |
|
113 \n\ |
|
114 If given a second argument, @code{svd} returns an economy-sized\n\ |
|
115 decomposition, eliminating the unnecessary rows or columns of @var{u} or\n\ |
|
116 @var{v}.\n\ |
|
117 @end deftypefn") |
2928
|
118 { |
|
119 octave_value_list retval; |
|
120 |
|
121 int nargin = args.length (); |
|
122 |
|
123 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3) |
|
124 { |
|
125 print_usage ("svd"); |
|
126 return retval; |
|
127 } |
|
128 |
|
129 octave_value arg = args(0); |
|
130 |
|
131 int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ()); |
|
132 |
|
133 if (arg_is_empty < 0) |
|
134 return retval; |
|
135 else if (arg_is_empty > 0) |
|
136 return octave_value_list (3, Matrix ()); |
|
137 |
|
138 SVD::type type = ((nargout == 0 || nargout == 1) |
|
139 ? SVD::sigma_only |
|
140 : (nargin == 2) ? SVD::economy : SVD::std); |
|
141 |
|
142 if (arg.is_real_type ()) |
|
143 { |
|
144 Matrix tmp = arg.matrix_value (); |
|
145 |
|
146 if (! error_state) |
|
147 { |
|
148 if (tmp.any_element_is_inf_or_nan ()) |
|
149 { |
|
150 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
151 NaN values"); |
|
152 return retval; |
|
153 } |
|
154 |
|
155 SVD result (tmp, type); |
|
156 |
|
157 DiagMatrix sigma = result.singular_values (); |
|
158 |
|
159 if (nargout == 0 || nargout == 1) |
|
160 { |
3418
|
161 retval(0) = sigma.diag (); |
2928
|
162 } |
|
163 else |
|
164 { |
|
165 retval(2) = result.right_singular_matrix (); |
|
166 retval(1) = sigma; |
|
167 retval(0) = result.left_singular_matrix (); |
|
168 } |
|
169 } |
|
170 } |
|
171 else if (arg.is_complex_type ()) |
|
172 { |
|
173 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
174 |
|
175 if (! error_state) |
|
176 { |
|
177 if (ctmp.any_element_is_inf_or_nan ()) |
|
178 { |
|
179 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
180 NaN values"); |
|
181 return retval; |
|
182 } |
|
183 |
|
184 ComplexSVD result (ctmp, type); |
|
185 |
|
186 DiagMatrix sigma = result.singular_values (); |
|
187 |
|
188 if (nargout == 0 || nargout == 1) |
|
189 { |
3418
|
190 retval(0) = sigma.diag (); |
2928
|
191 } |
|
192 else |
|
193 { |
|
194 retval(2) = result.right_singular_matrix (); |
|
195 retval(1) = sigma; |
|
196 retval(0) = result.left_singular_matrix (); |
|
197 } |
|
198 } |
|
199 } |
|
200 else |
|
201 { |
|
202 gripe_wrong_type_arg ("svd", arg); |
|
203 return retval; |
|
204 } |
|
205 |
|
206 return retval; |
|
207 } |
|
208 |
|
209 /* |
|
210 ;;; Local Variables: *** |
|
211 ;;; mode: C++ *** |
|
212 ;;; End: *** |
|
213 */ |