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