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