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" |
544
|
35 #include "help.h" |
519
|
36 #include "defun-dld.h" |
1
|
37 |
519
|
38 DEFUN_DLD ("svd", Fsvd, Ssvd, 2, 3, |
539
|
39 "S = svd (X) or [U, S, V] = svd (X [, 0])\n\ |
519
|
40 \n\ |
539
|
41 Compute the singular value decomposition of X. Given a second input\n\ |
|
42 argument, an `economy' sized factorization is computed that omits\n\ |
|
43 unnecessary rows and columns of U and V") |
1
|
44 { |
497
|
45 Octave_object retval; |
1
|
46 |
519
|
47 int nargin = args.length (); |
|
48 |
539
|
49 if (nargin < 2 || nargin > 3 || nargout == 2 || nargout > 3) |
519
|
50 { |
|
51 print_usage ("svd"); |
|
52 return retval; |
|
53 } |
|
54 |
497
|
55 tree_constant arg = args(1).make_numeric (); |
1
|
56 |
620
|
57 if (error_state) |
|
58 return retval; |
|
59 |
1
|
60 if (arg.rows () == 0 || arg.columns () == 0) |
|
61 { |
|
62 int flag = user_pref.propagate_empty_matrices; |
|
63 if (flag != 0) |
|
64 { |
|
65 if (flag < 0) |
|
66 gripe_empty_arg ("svd", 0); |
516
|
67 |
1
|
68 Matrix m; |
620
|
69 retval.resize (3, m); |
1
|
70 } |
|
71 else |
|
72 gripe_empty_arg ("svd", 1); |
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
539
|
77 SVD::type type = (nargin == 3) ? SVD::economy : SVD::std; |
|
78 |
620
|
79 if (arg.is_real_type ()) |
1
|
80 { |
620
|
81 Matrix tmp = arg.matrix_value (); |
1
|
82 |
620
|
83 SVD result (tmp, type); |
|
84 |
|
85 DiagMatrix sigma = result.singular_values (); |
1
|
86 |
620
|
87 if (nargout == 0 || nargout == 1) |
|
88 { |
|
89 retval(0) = tree_constant (sigma.diag (), 1); |
|
90 } |
|
91 else |
|
92 { |
|
93 retval(2) = result.right_singular_matrix (); |
|
94 retval(1) = sigma; |
|
95 retval(0) = result.left_singular_matrix (); |
|
96 } |
|
97 } |
|
98 else if (arg.is_complex_type ()) |
|
99 { |
|
100 ComplexMatrix ctmp = arg.complex_matrix_value (); |
|
101 |
|
102 ComplexSVD result (ctmp, type); |
1
|
103 |
620
|
104 DiagMatrix sigma = result.singular_values (); |
1
|
105 |
620
|
106 if (nargout == 0 || nargout == 1) |
|
107 { |
|
108 retval(0) = tree_constant (sigma.diag (), 1); |
|
109 } |
|
110 else |
|
111 { |
|
112 retval(2) = result.right_singular_matrix (); |
|
113 retval(1) = sigma; |
|
114 retval(0) = result.left_singular_matrix (); |
|
115 } |
|
116 } |
|
117 else |
|
118 { |
|
119 gripe_wrong_type_arg ("svd", arg); |
|
120 return retval; |
1
|
121 } |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
|
126 /* |
|
127 ;;; Local Variables: *** |
|
128 ;;; mode: C++ *** |
|
129 ;;; page-delimiter: "^/\\*" *** |
|
130 ;;; End: *** |
|
131 */ |