comparison src/DLD-FUNCTIONS/svd.cc @ 4478:7afd4bf05aa8

[project @ 2003-07-30 19:15:31 by jwe]
author jwe
date Wed, 30 Jul 2003 19:15:31 +0000
parents ab7fa5a8f23f
children 23b37da9fd5b
comparison
equal deleted inserted replaced
4477:87c2e107f811 4478:7afd4bf05aa8
126 return retval; 126 return retval;
127 } 127 }
128 128
129 octave_value arg = args(0); 129 octave_value arg = args(0);
130 130
131 int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ()); 131 int nr = arg.rows ();
132 132 int nc = arg.columns ();
133 if (arg_is_empty < 0) 133
134 return retval; 134 if (nr == 0 || nc == 0)
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 { 135 {
144 Matrix tmp = arg.matrix_value (); 136 if (nargout == 3)
145 137 {
146 if (! error_state) 138 retval(3) = identity_matrix (nr, nr);
147 { 139 retval(2) = Matrix (nr, nc);
148 if (tmp.any_element_is_inf_or_nan ()) 140 retval(1) = identity_matrix (nc, nc);
149 { 141 }
150 error ("svd: cannot take SVD of matrix containing Inf or\ 142 else
151 NaN values"); 143 retval(0) = Matrix (0, 1);
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 {
161 retval(0) = sigma.diag ();
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 {
190 retval(0) = sigma.diag ();
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 } 144 }
200 else 145 else
201 { 146 {
202 gripe_wrong_type_arg ("svd", arg); 147 SVD::type type = ((nargout == 0 || nargout == 1)
203 return retval; 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 }
204 } 212 }
205 213
206 return retval; 214 return retval;
207 } 215 }
208 216