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 |
4066
|
23 #if defined (__GNUG__) && ! defined (NO_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 { |
3887
|
37 int F77_FUNC (zgesvd, ZGESVD) (const char*, const char*, const int&, |
1253
|
38 const int&, Complex*, const int&, |
|
39 double*, Complex*, const int&, |
|
40 Complex*, const int&, Complex*, |
|
41 const int&, double*, int&, long, |
|
42 long); |
457
|
43 } |
|
44 |
1543
|
45 ComplexMatrix |
|
46 ComplexSVD::left_singular_matrix (void) const |
|
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 ComplexMatrix (); |
|
53 } |
|
54 else |
|
55 return left_sm; |
|
56 } |
|
57 |
|
58 ComplexMatrix |
|
59 ComplexSVD::right_singular_matrix (void) const |
|
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 ComplexMatrix (); |
|
66 } |
|
67 else |
|
68 return right_sm; |
|
69 } |
|
70 |
457
|
71 int |
537
|
72 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type) |
457
|
73 { |
|
74 int info; |
|
75 |
|
76 int m = a.rows (); |
|
77 int n = a.cols (); |
|
78 |
1946
|
79 ComplexMatrix atmp = a; |
|
80 Complex *tmp_data = atmp.fortran_vec (); |
457
|
81 |
|
82 int min_mn = m < n ? m : n; |
|
83 int max_mn = m > n ? m : n; |
|
84 |
1930
|
85 char jobu = 'A'; |
|
86 char jobv = 'A'; |
537
|
87 |
|
88 int ncol_u = m; |
|
89 int nrow_vt = n; |
|
90 int nrow_s = m; |
|
91 int ncol_s = n; |
|
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')) |
1930
|
122 left_sm.resize (m, ncol_u); |
|
123 |
|
124 Complex *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 Complex *vt = right_sm.fortran_vec (); |
457
|
133 |
|
134 int lrwork = 5*max_mn; |
1930
|
135 |
|
136 Array<double> rwork (lrwork); |
3336
|
137 |
|
138 // Ask ZGESVD what the dimension of WORK should be. |
|
139 |
|
140 int lwork = -1; |
457
|
141 |
3336
|
142 Array<Complex> work (1); |
|
143 |
|
144 F77_XFCN (zgesvd, ZGESVD, (&jobu, &jobv, m, n, tmp_data, m, s_vec, |
|
145 u, m, vt, nrow_vt, work.fortran_vec (), |
|
146 lwork, rwork.fortran_vec (), info, 1L, |
|
147 1L)); |
1543
|
148 |
1930
|
149 if (f77_exception_encountered) |
|
150 (*current_liboctave_error_handler) ("unrecoverable error in zgesvd"); |
|
151 else |
1543
|
152 { |
3336
|
153 lwork = static_cast<int> (work(0).real ()); |
|
154 work.resize (lwork); |
|
155 |
|
156 F77_XFCN (zgesvd, ZGESVD, (&jobu, &jobv, m, n, tmp_data, m, |
|
157 s_vec, u, m, vt, nrow_vt, |
|
158 work.fortran_vec (), lwork, |
|
159 rwork.fortran_vec (), |
|
160 info, 1L, 1L)); |
|
161 |
|
162 if (f77_exception_encountered) |
|
163 (*current_liboctave_error_handler) ("unrecoverable error in zgesvd"); |
|
164 else |
|
165 { |
|
166 if (! (jobv == 'N' || jobv == 'O')) |
|
167 right_sm = right_sm.hermitian (); |
|
168 } |
1543
|
169 } |
457
|
170 |
|
171 return info; |
|
172 } |
|
173 |
|
174 /* |
|
175 ;;; Local Variables: *** |
|
176 ;;; mode: C++ *** |
|
177 ;;; End: *** |
|
178 */ |