7
|
1 // f-svd.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
453
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
453
|
28 #include "dbleSVD.h" |
|
29 #include "CmplxSVD.h" |
1
|
30 |
|
31 #include "tree-const.h" |
|
32 #include "user-prefs.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
519
|
35 #include "defun-dld.h" |
1
|
36 |
519
|
37 DEFUN_DLD ("svd", Fsvd, Ssvd, 2, 3, |
539
|
38 "S = svd (X) or [U, S, V] = svd (X [, 0])\n\ |
519
|
39 \n\ |
539
|
40 Compute the singular value decomposition of X. Given a second input\n\ |
|
41 argument, an `economy' sized factorization is computed that omits\n\ |
|
42 unnecessary rows and columns of U and V") |
1
|
43 { |
497
|
44 Octave_object retval; |
1
|
45 |
519
|
46 int nargin = args.length (); |
|
47 |
539
|
48 if (nargin < 2 || nargin > 3 || nargout == 2 || nargout > 3) |
519
|
49 { |
|
50 print_usage ("svd"); |
|
51 return retval; |
|
52 } |
|
53 |
497
|
54 tree_constant arg = args(1).make_numeric (); |
1
|
55 |
|
56 if (arg.rows () == 0 || arg.columns () == 0) |
|
57 { |
|
58 int flag = user_pref.propagate_empty_matrices; |
|
59 if (flag != 0) |
|
60 { |
|
61 if (flag < 0) |
|
62 gripe_empty_arg ("svd", 0); |
516
|
63 |
1
|
64 Matrix m; |
497
|
65 retval.resize (3); |
516
|
66 retval(0) = m; |
|
67 retval(1) = m; |
|
68 retval(2) = m; |
1
|
69 } |
|
70 else |
|
71 gripe_empty_arg ("svd", 1); |
|
72 |
|
73 return retval; |
|
74 } |
|
75 |
|
76 Matrix tmp; |
|
77 ComplexMatrix ctmp; |
|
78 switch (arg.const_type ()) |
|
79 { |
|
80 case tree_constant_rep::scalar_constant: |
|
81 tmp.resize (1, 1); |
|
82 tmp.elem (0, 0) = arg.double_value (); |
|
83 break; |
|
84 case tree_constant_rep::matrix_constant: |
|
85 tmp = arg.matrix_value (); |
|
86 break; |
|
87 case tree_constant_rep::complex_scalar_constant: |
|
88 ctmp.resize (1, 1); |
|
89 ctmp.elem (0, 0) = arg.complex_value (); |
|
90 break; |
|
91 case tree_constant_rep::complex_matrix_constant: |
|
92 ctmp = arg.complex_matrix_value (); |
|
93 break; |
|
94 default: |
|
95 panic_impossible (); |
|
96 break; |
|
97 } |
|
98 |
539
|
99 SVD::type type = (nargin == 3) ? SVD::economy : SVD::std; |
|
100 |
1
|
101 switch (arg.const_type ()) |
|
102 { |
|
103 case tree_constant_rep::scalar_constant: |
|
104 case tree_constant_rep::matrix_constant: |
|
105 { |
539
|
106 SVD result (tmp, type); |
1
|
107 |
|
108 DiagMatrix sigma = result.singular_values (); |
|
109 |
497
|
110 if (nargout == 0 || nargout == 1) |
1
|
111 { |
497
|
112 retval.resize (1); |
|
113 retval(0) = tree_constant (sigma.diag (), 1); |
1
|
114 } |
|
115 else |
|
116 { |
497
|
117 retval.resize (3); |
516
|
118 retval(0) = result.left_singular_matrix (); |
|
119 retval(1) = sigma; |
|
120 retval(2) = result.right_singular_matrix (); |
1
|
121 } |
|
122 } |
|
123 break; |
|
124 case tree_constant_rep::complex_scalar_constant: |
|
125 case tree_constant_rep::complex_matrix_constant: |
|
126 { |
539
|
127 ComplexSVD result (ctmp, type); |
1
|
128 |
|
129 DiagMatrix sigma = result.singular_values (); |
|
130 |
497
|
131 if (nargout == 0 || nargout == 1) |
1
|
132 { |
497
|
133 retval.resize (1); |
|
134 retval(0) = tree_constant (sigma.diag (), 1); |
1
|
135 } |
|
136 else |
|
137 { |
497
|
138 retval.resize (3); |
516
|
139 retval(0) = result.left_singular_matrix (); |
|
140 retval(1) = sigma; |
|
141 retval(2) = result.right_singular_matrix (); |
1
|
142 } |
|
143 } |
|
144 break; |
|
145 default: |
|
146 panic_impossible (); |
|
147 break; |
|
148 } |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
|
153 /* |
|
154 ;;; Local Variables: *** |
|
155 ;;; mode: C++ *** |
|
156 ;;; page-delimiter: "^/\\*" *** |
|
157 ;;; End: *** |
|
158 */ |