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