Mercurial > octave
annotate liboctave/CmplxSVD.cc @ 10601:3ce0c530a9c9
implement svd_driver
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 03 May 2010 13:21:35 +0200 |
parents | 12884915a8e4 |
children | 8a5e980da6aa |
rev | line source |
---|---|
457 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005, |
8920 | 4 2007, 2008 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 | |
28 #include "CmplxSVD.h" | |
1847 | 29 #include "f77-fcn.h" |
1543 | 30 #include "lo-error.h" |
10601 | 31 #include "oct-locbuf.h" |
457 | 32 |
33 extern "C" | |
34 { | |
4552 | 35 F77_RET_T |
36 F77_FUNC (zgesvd, ZGESVD) (F77_CONST_CHAR_ARG_DECL, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
37 F77_CONST_CHAR_ARG_DECL, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
38 const octave_idx_type&, const octave_idx_type&, Complex*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
39 const octave_idx_type&, double*, Complex*, const octave_idx_type&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
40 Complex*, const octave_idx_type&, Complex*, const octave_idx_type&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
41 double*, octave_idx_type& |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
42 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
43 F77_CHAR_ARG_LEN_DECL); |
10601 | 44 |
45 F77_RET_T | |
46 F77_FUNC (zgesdd, ZGESDD) (F77_CONST_CHAR_ARG_DECL, | |
47 const octave_idx_type&, const octave_idx_type&, Complex*, | |
48 const octave_idx_type&, double*, Complex*, const octave_idx_type&, | |
49 Complex*, const octave_idx_type&, Complex*, const octave_idx_type&, | |
50 double*, octave_idx_type *, octave_idx_type& | |
51 F77_CHAR_ARG_LEN_DECL); | |
457 | 52 } |
53 | |
1543 | 54 ComplexMatrix |
55 ComplexSVD::left_singular_matrix (void) const | |
56 { | |
1544 | 57 if (type_computed == SVD::sigma_only) |
1543 | 58 { |
59 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
60 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
1543 | 61 return ComplexMatrix (); |
62 } | |
63 else | |
64 return left_sm; | |
65 } | |
66 | |
67 ComplexMatrix | |
68 ComplexSVD::right_singular_matrix (void) const | |
69 { | |
1544 | 70 if (type_computed == SVD::sigma_only) |
1543 | 71 { |
72 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
73 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
1543 | 74 return ComplexMatrix (); |
75 } | |
76 else | |
77 return right_sm; | |
78 } | |
79 | |
5275 | 80 octave_idx_type |
10601 | 81 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type, SVD::driver svd_driver) |
457 | 82 { |
5275 | 83 octave_idx_type info; |
457 | 84 |
5275 | 85 octave_idx_type m = a.rows (); |
86 octave_idx_type n = a.cols (); | |
457 | 87 |
1946 | 88 ComplexMatrix atmp = a; |
89 Complex *tmp_data = atmp.fortran_vec (); | |
457 | 90 |
5275 | 91 octave_idx_type min_mn = m < n ? m : n; |
92 octave_idx_type max_mn = m > n ? m : n; | |
457 | 93 |
1930 | 94 char jobu = 'A'; |
95 char jobv = 'A'; | |
537 | 96 |
5275 | 97 octave_idx_type ncol_u = m; |
98 octave_idx_type nrow_vt = n; | |
99 octave_idx_type nrow_s = m; | |
100 octave_idx_type ncol_s = n; | |
537 | 101 |
1543 | 102 switch (svd_type) |
537 | 103 { |
1543 | 104 case SVD::economy: |
1930 | 105 jobu = jobv = 'S'; |
537 | 106 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543 | 107 break; |
108 | |
109 case SVD::sigma_only: | |
2621 | 110 |
111 // Note: for this case, both jobu and jobv should be 'N', but | |
112 // there seems to be a bug in dgesvd from Lapack V2.0. To | |
113 // demonstrate the bug, set both jobu and jobv to 'N' and find | |
114 // the singular values of [eye(3), eye(3)]. The result is | |
115 // [-sqrt(2), -sqrt(2), -sqrt(2)]. | |
3335 | 116 // |
117 // For Lapack 3.0, this problem seems to be fixed. | |
2621 | 118 |
3335 | 119 jobu = 'N'; |
2621 | 120 jobv = 'N'; |
1545 | 121 ncol_u = nrow_vt = 1; |
1543 | 122 break; |
123 | |
124 default: | |
125 break; | |
537 | 126 } |
127 | |
1544 | 128 type_computed = svd_type; |
129 | |
2621 | 130 if (! (jobu == 'N' || jobu == 'O')) |
1930 | 131 left_sm.resize (m, ncol_u); |
132 | |
133 Complex *u = left_sm.fortran_vec (); | |
134 | |
135 sigma.resize (nrow_s, ncol_s); | |
136 double *s_vec = sigma.fortran_vec (); | |
137 | |
2621 | 138 if (! (jobv == 'N' || jobv == 'O')) |
1930 | 139 right_sm.resize (nrow_vt, n); |
140 | |
141 Complex *vt = right_sm.fortran_vec (); | |
457 | 142 |
5275 | 143 octave_idx_type lrwork = 5*max_mn; |
1930 | 144 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
145 Array<double> rwork (lrwork, 1); |
3336 | 146 |
147 // Ask ZGESVD what the dimension of WORK should be. | |
148 | |
5275 | 149 octave_idx_type lwork = -1; |
457 | 150 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
151 Array<Complex> work (1, 1); |
3336 | 152 |
10258 | 153 octave_idx_type one = 1; |
154 octave_idx_type m1 = std::max (m, one), nrow_vt1 = std::max (nrow_vt, one); | |
10185
455759a5fcbe
fix norm and svd on empty matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
10158
diff
changeset
|
155 |
10601 | 156 if (svd_driver == SVD::GESVD) |
157 { | |
158 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), | |
159 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
160 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
161 nrow_vt1, work.fortran_vec (), lwork, | |
162 rwork.fortran_vec (), info | |
163 F77_CHAR_ARG_LEN (1) | |
164 F77_CHAR_ARG_LEN (1))); | |
165 | |
166 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
167 work.resize (lwork, 1); | |
1543 | 168 |
10601 | 169 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
170 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
171 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
172 nrow_vt1, work.fortran_vec (), lwork, | |
173 rwork.fortran_vec (), info | |
174 F77_CHAR_ARG_LEN (1) | |
175 F77_CHAR_ARG_LEN (1))); | |
176 } | |
177 else if (svd_driver == SVD::GESDD) | |
178 { | |
179 assert (jobu == jobv); | |
180 char jobz = jobu; | |
181 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, 8*min_mn); | |
3336 | 182 |
10601 | 183 F77_XFCN (zgesdd, ZGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), |
184 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
185 nrow_vt1, work.fortran_vec (), lwork, | |
186 rwork.fortran_vec (), iwork, info | |
187 F77_CHAR_ARG_LEN (1))); | |
188 | |
189 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
190 work.resize (lwork, 1); | |
191 | |
192 F77_XFCN (zgesdd, ZGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
193 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
194 nrow_vt1, work.fortran_vec (), lwork, | |
195 rwork.fortran_vec (), iwork, info | |
196 F77_CHAR_ARG_LEN (1))); | |
197 } | |
198 else | |
199 assert (0); // impossible | |
3336 | 200 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
201 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
202 right_sm = right_sm.hermitian (); |
457 | 203 |
204 return info; | |
205 } |