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" |
1
|
37 #include "user-prefs.h" |
628
|
38 #include "utils.h" |
1
|
39 |
1957
|
40 DEFUN_DLD_BUILTIN (svd, args, nargout, |
539
|
41 "S = svd (X) or [U, S, V] = svd (X [, 0])\n\ |
519
|
42 \n\ |
539
|
43 Compute the singular value decomposition of X. Given a second input\n\ |
|
44 argument, an `economy' sized factorization is computed that omits\n\ |
1310
|
45 unnecessary rows and columns of U and V.\n\ |
|
46 \n\ |
|
47 X may not contain any Inf or NaN values.") |
1
|
48 { |
636
|
49 Octave_object retval; |
1
|
50 |
519
|
51 int nargin = args.length (); |
|
52 |
712
|
53 if (nargin < 1 || nargin > 2 || nargout == 2 || nargout > 3) |
519
|
54 { |
|
55 print_usage ("svd"); |
|
56 return retval; |
|
57 } |
|
58 |
712
|
59 tree_constant arg = args(0); |
620
|
60 |
718
|
61 int arg_is_empty = empty_arg ("svd", arg.rows (), arg.columns ()); |
|
62 |
|
63 if (arg_is_empty < 0) |
636
|
64 return retval; |
718
|
65 else if (arg_is_empty > 0) |
|
66 return Octave_object (3, Matrix ()); |
636
|
67 |
1544
|
68 SVD::type type = ((nargout == 0 || nargout == 1) |
|
69 ? SVD::sigma_only |
1543
|
70 : (nargin == 2) ? SVD::economy : SVD::std); |
539
|
71 |
620
|
72 if (arg.is_real_type ()) |
1
|
73 { |
620
|
74 Matrix tmp = arg.matrix_value (); |
1
|
75 |
636
|
76 if (! error_state) |
|
77 { |
1310
|
78 if (any_element_is_inf_or_nan (tmp)) |
|
79 { |
|
80 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
81 NaN values"); |
|
82 return retval; |
|
83 } |
|
84 |
636
|
85 SVD result (tmp, type); |
622
|
86 |
636
|
87 DiagMatrix sigma = result.singular_values (); |
1
|
88 |
636
|
89 if (nargout == 0 || nargout == 1) |
|
90 { |
|
91 retval(0) = tree_constant (sigma.diag (), 1); |
|
92 } |
|
93 else |
|
94 { |
|
95 retval(2) = result.right_singular_matrix (); |
|
96 retval(1) = sigma; |
|
97 retval(0) = result.left_singular_matrix (); |
|
98 } |
620
|
99 } |
|
100 } |
|
101 else if (arg.is_complex_type ()) |
|
102 { |
|
103 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
104 |
636
|
105 if (! error_state) |
|
106 { |
1310
|
107 if (any_element_is_inf_or_nan (ctmp)) |
|
108 { |
|
109 error ("svd: cannot take SVD of matrix containing Inf or\ |
|
110 NaN values"); |
|
111 return retval; |
|
112 } |
|
113 |
636
|
114 ComplexSVD result (ctmp, type); |
622
|
115 |
636
|
116 DiagMatrix sigma = result.singular_values (); |
1
|
117 |
636
|
118 if (nargout == 0 || nargout == 1) |
|
119 { |
|
120 retval(0) = tree_constant (sigma.diag (), 1); |
|
121 } |
|
122 else |
|
123 { |
|
124 retval(2) = result.right_singular_matrix (); |
|
125 retval(1) = sigma; |
|
126 retval(0) = result.left_singular_matrix (); |
|
127 } |
620
|
128 } |
|
129 } |
|
130 else |
|
131 { |
|
132 gripe_wrong_type_arg ("svd", arg); |
|
133 return retval; |
1
|
134 } |
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
|
139 /* |
|
140 ;;; Local Variables: *** |
|
141 ;;; mode: C++ *** |
|
142 ;;; End: *** |
|
143 */ |