1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1352
|
27 #include "CmplxSVD.h" |
453
|
28 #include "dbleSVD.h" |
1
|
29 |
1352
|
30 #include "defun-dld.h" |
|
31 #include "error.h" |
|
32 #include "gripes.h" |
|
33 #include "help.h" |
|
34 #include "mappers.h" |
1740
|
35 #include "oct-obj.h" |
1352
|
36 #include "pr-output.h" |
628
|
37 #include "utils.h" |
1
|
38 |
1957
|
39 DEFUN_DLD_BUILTIN (svd, args, nargout, |
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\ |
1310
|
44 unnecessary rows and columns of U and V.\n\ |
|
45 \n\ |
|
46 X may not contain any Inf or NaN values.") |
1
|
47 { |
2086
|
48 octave_value_list retval; |
1
|
49 |
519
|
50 int nargin = args.length (); |
|
51 |
712
|
52 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3) |
519
|
53 { |
|
54 print_usage ("svd"); |
|
55 return retval; |
|
56 } |
|
57 |
2086
|
58 octave_value arg = args(0); |
620
|
59 |
718
|
60 int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ()); |
|
61 |
|
62 if (arg_is_empty < 0) |
636
|
63 return retval; |
718
|
64 else if (arg_is_empty > 0) |
2086
|
65 return octave_value_list (3, Matrix ()); |
636
|
66 |
1544
|
67 SVD::type type = ((nargout == 0 || nargout == 1) |
|
68 ? SVD::sigma_only |
1543
|
69 : (nargin == 2) ? SVD::economy : SVD::std); |
539
|
70 |
620
|
71 if (arg.is_real_type ()) |
1
|
72 { |
620
|
73 Matrix tmp = arg.matrix_value (); |
1
|
74 |
636
|
75 if (! error_state) |
|
76 { |
2367
|
77 if (tmp.any_element_is_inf_or_nan ()) |
1310
|
78 { |
|
79 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
80 NaN values"); |
|
81 return retval; |
|
82 } |
|
83 |
636
|
84 SVD result (tmp, type); |
622
|
85 |
636
|
86 DiagMatrix sigma = result.singular_values (); |
1
|
87 |
636
|
88 if (nargout == 0 || nargout == 1) |
|
89 { |
2086
|
90 retval(0) = octave_value (sigma.diag (), 1); |
636
|
91 } |
|
92 else |
|
93 { |
|
94 retval(2) = result.right_singular_matrix (); |
|
95 retval(1) = sigma; |
|
96 retval(0) = result.left_singular_matrix (); |
|
97 } |
620
|
98 } |
|
99 } |
|
100 else if (arg.is_complex_type ()) |
|
101 { |
|
102 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
103 |
636
|
104 if (! error_state) |
|
105 { |
2367
|
106 if (ctmp.any_element_is_inf_or_nan ()) |
1310
|
107 { |
|
108 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
109 NaN values"); |
|
110 return retval; |
|
111 } |
|
112 |
636
|
113 ComplexSVD result (ctmp, type); |
622
|
114 |
636
|
115 DiagMatrix sigma = result.singular_values (); |
1
|
116 |
636
|
117 if (nargout == 0 || nargout == 1) |
|
118 { |
2086
|
119 retval(0) = octave_value (sigma.diag (), 1); |
636
|
120 } |
|
121 else |
|
122 { |
|
123 retval(2) = result.right_singular_matrix (); |
|
124 retval(1) = sigma; |
|
125 retval(0) = result.left_singular_matrix (); |
|
126 } |
620
|
127 } |
|
128 } |
|
129 else |
|
130 { |
|
131 gripe_wrong_type_arg ("svd", arg); |
|
132 return retval; |
1
|
133 } |
|
134 |
|
135 return retval; |
|
136 } |
|
137 |
|
138 /* |
|
139 ;;; Local Variables: *** |
|
140 ;;; mode: C++ *** |
|
141 ;;; End: *** |
|
142 */ |