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 |
|
31 #include "CmplxSVD.h" |
1847
|
32 #include "f77-fcn.h" |
1543
|
33 #include "lo-error.h" |
457
|
34 |
|
35 extern "C" |
|
36 { |
4552
|
37 F77_RET_T |
|
38 F77_FUNC (zgesvd, ZGESVD) (F77_CONST_CHAR_ARG_DECL, |
|
39 F77_CONST_CHAR_ARG_DECL, |
|
40 const int&, const int&, Complex*, |
|
41 const int&, double*, Complex*, const int&, |
|
42 Complex*, const int&, Complex*, const int&, |
|
43 double*, int& |
|
44 F77_CHAR_ARG_LEN_DECL |
|
45 F77_CHAR_ARG_LEN_DECL); |
457
|
46 } |
|
47 |
1543
|
48 ComplexMatrix |
|
49 ComplexSVD::left_singular_matrix (void) const |
|
50 { |
1544
|
51 if (type_computed == SVD::sigma_only) |
1543
|
52 { |
|
53 (*current_liboctave_error_handler) |
|
54 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
55 return ComplexMatrix (); |
|
56 } |
|
57 else |
|
58 return left_sm; |
|
59 } |
|
60 |
|
61 ComplexMatrix |
|
62 ComplexSVD::right_singular_matrix (void) const |
|
63 { |
1544
|
64 if (type_computed == SVD::sigma_only) |
1543
|
65 { |
|
66 (*current_liboctave_error_handler) |
|
67 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
68 return ComplexMatrix (); |
|
69 } |
|
70 else |
|
71 return right_sm; |
|
72 } |
|
73 |
457
|
74 int |
537
|
75 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type) |
457
|
76 { |
|
77 int info; |
|
78 |
|
79 int m = a.rows (); |
|
80 int n = a.cols (); |
|
81 |
1946
|
82 ComplexMatrix atmp = a; |
|
83 Complex *tmp_data = atmp.fortran_vec (); |
457
|
84 |
|
85 int min_mn = m < n ? m : n; |
|
86 int max_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')) |
1930
|
125 left_sm.resize (m, ncol_u); |
|
126 |
|
127 Complex *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 Complex *vt = right_sm.fortran_vec (); |
457
|
136 |
|
137 int lrwork = 5*max_mn; |
1930
|
138 |
|
139 Array<double> rwork (lrwork); |
3336
|
140 |
|
141 // Ask ZGESVD what the dimension of WORK should be. |
|
142 |
|
143 int lwork = -1; |
457
|
144 |
3336
|
145 Array<Complex> work (1); |
|
146 |
4552
|
147 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
|
148 F77_CONST_CHAR_ARG2 (&jobv, 1), |
|
149 m, n, tmp_data, m, s_vec, u, m, vt, |
|
150 nrow_vt, work.fortran_vec (), lwork, |
|
151 rwork.fortran_vec (), info |
|
152 F77_CHAR_ARG_LEN (1) |
|
153 F77_CHAR_ARG_LEN (1))); |
1543
|
154 |
1930
|
155 if (f77_exception_encountered) |
|
156 (*current_liboctave_error_handler) ("unrecoverable error in zgesvd"); |
|
157 else |
1543
|
158 { |
3336
|
159 lwork = static_cast<int> (work(0).real ()); |
|
160 work.resize (lwork); |
|
161 |
4552
|
162 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
|
163 F77_CONST_CHAR_ARG2 (&jobv, 1), |
|
164 m, n, tmp_data, m, s_vec, u, m, vt, |
|
165 nrow_vt, work.fortran_vec (), lwork, |
|
166 rwork.fortran_vec (), info |
|
167 F77_CHAR_ARG_LEN (1) |
|
168 F77_CHAR_ARG_LEN (1))); |
3336
|
169 |
|
170 if (f77_exception_encountered) |
|
171 (*current_liboctave_error_handler) ("unrecoverable error in zgesvd"); |
|
172 else |
|
173 { |
|
174 if (! (jobv == 'N' || jobv == 'O')) |
|
175 right_sm = right_sm.hermitian (); |
|
176 } |
1543
|
177 } |
457
|
178 |
|
179 return info; |
|
180 } |
|
181 |
|
182 /* |
|
183 ;;; Local Variables: *** |
|
184 ;;; mode: C++ *** |
|
185 ;;; End: *** |
|
186 */ |