7
|
1 // f-svd.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
28 #include "Matrix.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "user-prefs.h" |
|
32 #include "gripes.h" |
|
33 #include "error.h" |
7
|
34 #include "f-svd.h" |
1
|
35 |
|
36 #ifdef WITH_DLD |
|
37 tree_constant * |
162
|
38 builtin_svd_2 (const tree_constant *args, int nargin, int nargout) |
1
|
39 { |
|
40 return svd (args, nargin, nargout); |
|
41 } |
|
42 #endif |
|
43 |
|
44 tree_constant * |
162
|
45 svd (const tree_constant *args, int nargin, int nargout) |
1
|
46 { |
|
47 tree_constant *retval = NULL_TREE_CONST; |
|
48 |
|
49 tree_constant arg = args[1].make_numeric (); |
|
50 |
|
51 if (arg.rows () == 0 || arg.columns () == 0) |
|
52 { |
|
53 int flag = user_pref.propagate_empty_matrices; |
|
54 if (flag != 0) |
|
55 { |
|
56 if (flag < 0) |
|
57 gripe_empty_arg ("svd", 0); |
|
58 Matrix m; |
|
59 retval = new tree_constant [4]; |
|
60 retval[0] = tree_constant (m); |
|
61 retval[1] = tree_constant (m); |
|
62 retval[2] = tree_constant (m); |
|
63 } |
|
64 else |
|
65 gripe_empty_arg ("svd", 1); |
|
66 |
|
67 return retval; |
|
68 } |
|
69 |
|
70 Matrix tmp; |
|
71 ComplexMatrix ctmp; |
|
72 switch (arg.const_type ()) |
|
73 { |
|
74 case tree_constant_rep::scalar_constant: |
|
75 tmp.resize (1, 1); |
|
76 tmp.elem (0, 0) = arg.double_value (); |
|
77 break; |
|
78 case tree_constant_rep::matrix_constant: |
|
79 tmp = arg.matrix_value (); |
|
80 break; |
|
81 case tree_constant_rep::complex_scalar_constant: |
|
82 ctmp.resize (1, 1); |
|
83 ctmp.elem (0, 0) = arg.complex_value (); |
|
84 break; |
|
85 case tree_constant_rep::complex_matrix_constant: |
|
86 ctmp = arg.complex_matrix_value (); |
|
87 break; |
|
88 default: |
|
89 panic_impossible (); |
|
90 break; |
|
91 } |
|
92 |
|
93 switch (arg.const_type ()) |
|
94 { |
|
95 case tree_constant_rep::scalar_constant: |
|
96 case tree_constant_rep::matrix_constant: |
|
97 { |
|
98 SVD result (tmp); |
|
99 |
|
100 DiagMatrix sigma = result.singular_values (); |
|
101 |
|
102 if (nargout == 1) |
|
103 { |
|
104 retval = new tree_constant [2]; |
|
105 retval[0] = tree_constant (sigma.diag (), 1); |
|
106 } |
|
107 else |
|
108 { |
|
109 retval = new tree_constant [4]; |
|
110 retval[0] = tree_constant (result.left_singular_matrix ()); |
|
111 retval[1] = tree_constant (sigma); |
|
112 retval[2] = tree_constant (result.right_singular_matrix ()); |
|
113 } |
|
114 } |
|
115 break; |
|
116 case tree_constant_rep::complex_scalar_constant: |
|
117 case tree_constant_rep::complex_matrix_constant: |
|
118 { |
|
119 ComplexSVD result (ctmp); |
|
120 |
|
121 DiagMatrix sigma = result.singular_values (); |
|
122 |
|
123 if (nargout == 1) |
|
124 { |
|
125 retval = new tree_constant [2]; |
|
126 retval[0] = tree_constant (sigma.diag (), 1); |
|
127 } |
|
128 else |
|
129 { |
|
130 retval = new tree_constant [4]; |
|
131 retval[0] = tree_constant (result.left_singular_matrix ()); |
|
132 retval[1] = tree_constant (sigma); |
|
133 retval[2] = tree_constant (result.right_singular_matrix ()); |
|
134 } |
|
135 } |
|
136 break; |
|
137 default: |
|
138 panic_impossible (); |
|
139 break; |
|
140 } |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 /* |
|
146 ;;; Local Variables: *** |
|
147 ;;; mode: C++ *** |
|
148 ;;; page-delimiter: "^/\\*" *** |
|
149 ;;; End: *** |
|
150 */ |