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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2928
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2928
|
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\ |
6620
|
46 A = U S V^H\n\ |
3372
|
47 $$\n\ |
|
48 @end tex\n\ |
|
49 @end iftex\n\ |
|
50 @ifinfo\n\ |
|
51 \n\ |
|
52 @example\n\ |
6620
|
53 A = U*S*V'\n\ |
3372
|
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 { |
5823
|
125 print_usage (); |
2928
|
126 return retval; |
|
127 } |
|
128 |
|
129 octave_value arg = args(0); |
|
130 |
5275
|
131 octave_idx_type nr = arg.rows (); |
|
132 octave_idx_type nc = arg.columns (); |
2928
|
133 |
4478
|
134 if (nr == 0 || nc == 0) |
2928
|
135 { |
4478
|
136 if (nargout == 3) |
2928
|
137 { |
4478
|
138 retval(3) = identity_matrix (nr, nr); |
|
139 retval(2) = Matrix (nr, nc); |
|
140 retval(1) = identity_matrix (nc, nc); |
2928
|
141 } |
4478
|
142 else |
|
143 retval(0) = Matrix (0, 1); |
2928
|
144 } |
|
145 else |
|
146 { |
4478
|
147 SVD::type type = ((nargout == 0 || nargout == 1) |
|
148 ? SVD::sigma_only |
|
149 : (nargin == 2) ? SVD::economy : SVD::std); |
|
150 |
|
151 if (arg.is_real_type ()) |
|
152 { |
|
153 Matrix tmp = arg.matrix_value (); |
|
154 |
|
155 if (! error_state) |
|
156 { |
|
157 if (tmp.any_element_is_inf_or_nan ()) |
|
158 { |
|
159 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
|
160 return retval; |
|
161 } |
|
162 |
|
163 SVD result (tmp, type); |
|
164 |
|
165 DiagMatrix sigma = result.singular_values (); |
|
166 |
|
167 if (nargout == 0 || nargout == 1) |
|
168 { |
|
169 retval(0) = sigma.diag (); |
|
170 } |
|
171 else |
|
172 { |
|
173 retval(2) = result.right_singular_matrix (); |
|
174 retval(1) = sigma; |
|
175 retval(0) = result.left_singular_matrix (); |
|
176 } |
|
177 } |
|
178 } |
|
179 else if (arg.is_complex_type ()) |
|
180 { |
|
181 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
182 |
|
183 if (! error_state) |
|
184 { |
|
185 if (ctmp.any_element_is_inf_or_nan ()) |
|
186 { |
|
187 error ("svd: cannot take SVD of matrix containing Inf or NaN values"); |
|
188 return retval; |
|
189 } |
|
190 |
|
191 ComplexSVD result (ctmp, type); |
|
192 |
|
193 DiagMatrix sigma = result.singular_values (); |
|
194 |
|
195 if (nargout == 0 || nargout == 1) |
|
196 { |
|
197 retval(0) = sigma.diag (); |
|
198 } |
|
199 else |
|
200 { |
|
201 retval(2) = result.right_singular_matrix (); |
|
202 retval(1) = sigma; |
|
203 retval(0) = result.left_singular_matrix (); |
|
204 } |
|
205 } |
|
206 } |
|
207 else |
|
208 { |
|
209 gripe_wrong_type_arg ("svd", arg); |
|
210 return retval; |
|
211 } |
2928
|
212 } |
|
213 |
|
214 return retval; |
|
215 } |
|
216 |
|
217 /* |
|
218 ;;; Local Variables: *** |
|
219 ;;; mode: C++ *** |
|
220 ;;; End: *** |
|
221 */ |