457
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
457
|
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. |
457
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
457
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
457
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
1631
|
32 |
457
|
33 #include "dbleSVD.h" |
1847
|
34 #include "f77-fcn.h" |
457
|
35 |
|
36 extern "C" |
|
37 { |
4552
|
38 F77_RET_T |
|
39 F77_FUNC (dgesvd, DGESVD) (F77_CONST_CHAR_ARG_DECL, |
|
40 F77_CONST_CHAR_ARG_DECL, |
|
41 const int&, const int&, double*, |
|
42 const int&, double*, double*, |
|
43 const int&, double*, const int&, |
|
44 double*, const int&, int& |
|
45 F77_CHAR_ARG_LEN_DECL |
|
46 F77_CHAR_ARG_LEN_DECL); |
457
|
47 } |
|
48 |
1543
|
49 Matrix |
1545
|
50 SVD::left_singular_matrix (void) const |
1543
|
51 { |
1544
|
52 if (type_computed == SVD::sigma_only) |
1543
|
53 { |
|
54 (*current_liboctave_error_handler) |
|
55 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
56 return Matrix (); |
|
57 } |
|
58 else |
|
59 return left_sm; |
|
60 } |
|
61 |
|
62 Matrix |
1545
|
63 SVD::right_singular_matrix (void) const |
1543
|
64 { |
1544
|
65 if (type_computed == SVD::sigma_only) |
1543
|
66 { |
|
67 (*current_liboctave_error_handler) |
|
68 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
69 return Matrix (); |
|
70 } |
|
71 else |
|
72 return right_sm; |
|
73 } |
|
74 |
457
|
75 int |
537
|
76 SVD::init (const Matrix& a, SVD::type svd_type) |
457
|
77 { |
|
78 int info; |
|
79 |
|
80 int m = a.rows (); |
|
81 int n = a.cols (); |
|
82 |
1930
|
83 Matrix atmp = a; |
|
84 double *tmp_data = atmp.fortran_vec (); |
457
|
85 |
|
86 int min_mn = m < n ? m : n; |
|
87 |
1930
|
88 char jobu = 'A'; |
|
89 char jobv = 'A'; |
537
|
90 |
|
91 int ncol_u = m; |
|
92 int nrow_vt = n; |
|
93 int nrow_s = m; |
|
94 int ncol_s = n; |
|
95 |
1543
|
96 switch (svd_type) |
537
|
97 { |
1543
|
98 case SVD::economy: |
1930
|
99 jobu = jobv = 'S'; |
537
|
100 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543
|
101 break; |
|
102 |
|
103 case SVD::sigma_only: |
2621
|
104 |
|
105 // Note: for this case, both jobu and jobv should be 'N', but |
|
106 // there seems to be a bug in dgesvd from Lapack V2.0. To |
|
107 // demonstrate the bug, set both jobu and jobv to 'N' and find |
|
108 // the singular values of [eye(3), eye(3)]. The result is |
|
109 // [-sqrt(2), -sqrt(2), -sqrt(2)]. |
3335
|
110 // |
|
111 // For Lapack 3.0, this problem seems to be fixed. |
2621
|
112 |
3335
|
113 jobu = 'N'; |
2621
|
114 jobv = 'N'; |
1545
|
115 ncol_u = nrow_vt = 1; |
1543
|
116 break; |
|
117 |
|
118 default: |
|
119 break; |
537
|
120 } |
|
121 |
1544
|
122 type_computed = svd_type; |
|
123 |
2621
|
124 if (! (jobu == 'N' || jobu == 'O')) |
1931
|
125 left_sm.resize (m, ncol_u); |
1930
|
126 |
|
127 double *u = left_sm.fortran_vec (); |
|
128 |
|
129 sigma.resize (nrow_s, ncol_s); |
|
130 double *s_vec = sigma.fortran_vec (); |
|
131 |
2621
|
132 if (! (jobv == 'N' || jobv == 'O')) |
1930
|
133 right_sm.resize (nrow_vt, n); |
|
134 |
|
135 double *vt = right_sm.fortran_vec (); |
457
|
136 |
3336
|
137 // Ask DGESVD what the dimension of WORK should be. |
1930
|
138 |
3336
|
139 int lwork = -1; |
|
140 |
|
141 Array<double> work (1); |
457
|
142 |
4552
|
143 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
|
144 F77_CONST_CHAR_ARG2 (&jobv, 1), |
|
145 m, n, tmp_data, m, s_vec, u, m, vt, |
|
146 nrow_vt, work.fortran_vec (), lwork, info |
|
147 F77_CHAR_ARG_LEN (1) |
|
148 F77_CHAR_ARG_LEN (1))); |
1543
|
149 |
1930
|
150 if (f77_exception_encountered) |
|
151 (*current_liboctave_error_handler) ("unrecoverable error in dgesvd"); |
|
152 else |
1543
|
153 { |
3336
|
154 lwork = static_cast<int> (work(0)); |
|
155 work.resize (lwork); |
|
156 |
4552
|
157 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
|
158 F77_CONST_CHAR_ARG2 (&jobv, 1), |
|
159 m, n, tmp_data, m, s_vec, u, m, vt, |
|
160 nrow_vt, work.fortran_vec (), lwork, info |
|
161 F77_CHAR_ARG_LEN (1) |
|
162 F77_CHAR_ARG_LEN (1))); |
3336
|
163 |
|
164 if (f77_exception_encountered) |
|
165 (*current_liboctave_error_handler) ("unrecoverable error in dgesvd"); |
|
166 else |
|
167 { |
|
168 if (! (jobv == 'N' || jobv == 'O')) |
|
169 right_sm = right_sm.transpose (); |
|
170 } |
1543
|
171 } |
457
|
172 |
|
173 return info; |
|
174 } |
|
175 |
3504
|
176 std::ostream& |
|
177 operator << (std::ostream& os, const SVD& a) |
457
|
178 { |
|
179 os << a.left_singular_matrix () << "\n"; |
|
180 os << a.singular_values () << "\n"; |
|
181 os << a.right_singular_matrix () << "\n"; |
|
182 |
|
183 return os; |
|
184 } |
|
185 |
|
186 /* |
|
187 ;;; Local Variables: *** |
|
188 ;;; mode: C++ *** |
|
189 ;;; End: *** |
|
190 */ |