Mercurial > hg > octave-nkf
annotate liboctave/numeric/CmplxSVD.cc @ 16660:cbb1bb7a5c3d
Added tag ss-3-7-5 for changeset 608e307b4914
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 14 May 2013 05:23:53 -0400 |
parents | fde9297ae074 |
children | d63878346099 |
rev | line source |
---|---|
457 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11574
diff
changeset
|
3 Copyright (C) 1994-2012 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 | |
27 #include "CmplxSVD.h" | |
1847 | 28 #include "f77-fcn.h" |
1543 | 29 #include "lo-error.h" |
10601 | 30 #include "oct-locbuf.h" |
457 | 31 |
32 extern "C" | |
33 { | |
4552 | 34 F77_RET_T |
35 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
|
36 F77_CONST_CHAR_ARG_DECL, |
11495 | 37 const octave_idx_type&, const octave_idx_type&, |
38 Complex*, const octave_idx_type&, | |
39 double*, Complex*, const octave_idx_type&, | |
40 Complex*, const octave_idx_type&, Complex*, | |
41 const octave_idx_type&, double*, octave_idx_type& | |
10314
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, | |
11495 | 47 const octave_idx_type&, const octave_idx_type&, |
48 Complex*, const octave_idx_type&, | |
49 double*, Complex*, const octave_idx_type&, | |
50 Complex*, const octave_idx_type&, Complex*, | |
51 const octave_idx_type&, double*, | |
52 octave_idx_type *, octave_idx_type& | |
10601 | 53 F77_CHAR_ARG_LEN_DECL); |
457 | 54 } |
55 | |
1543 | 56 ComplexMatrix |
57 ComplexSVD::left_singular_matrix (void) const | |
58 { | |
1544 | 59 if (type_computed == SVD::sigma_only) |
1543 | 60 { |
61 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
62 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
1543 | 63 return ComplexMatrix (); |
64 } | |
65 else | |
66 return left_sm; | |
67 } | |
68 | |
69 ComplexMatrix | |
70 ComplexSVD::right_singular_matrix (void) const | |
71 { | |
1544 | 72 if (type_computed == SVD::sigma_only) |
1543 | 73 { |
74 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
75 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
1543 | 76 return ComplexMatrix (); |
77 } | |
78 else | |
79 return right_sm; | |
80 } | |
81 | |
5275 | 82 octave_idx_type |
10601 | 83 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type, SVD::driver svd_driver) |
457 | 84 { |
5275 | 85 octave_idx_type info; |
457 | 86 |
5275 | 87 octave_idx_type m = a.rows (); |
88 octave_idx_type n = a.cols (); | |
457 | 89 |
1946 | 90 ComplexMatrix atmp = a; |
91 Complex *tmp_data = atmp.fortran_vec (); | |
457 | 92 |
5275 | 93 octave_idx_type min_mn = m < n ? m : n; |
94 octave_idx_type max_mn = m > n ? m : n; | |
457 | 95 |
1930 | 96 char jobu = 'A'; |
97 char jobv = 'A'; | |
537 | 98 |
5275 | 99 octave_idx_type ncol_u = m; |
100 octave_idx_type nrow_vt = n; | |
101 octave_idx_type nrow_s = m; | |
102 octave_idx_type ncol_s = n; | |
537 | 103 |
1543 | 104 switch (svd_type) |
537 | 105 { |
1543 | 106 case SVD::economy: |
1930 | 107 jobu = jobv = 'S'; |
537 | 108 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543 | 109 break; |
110 | |
111 case SVD::sigma_only: | |
2621 | 112 |
113 // Note: for this case, both jobu and jobv should be 'N', but | |
114 // there seems to be a bug in dgesvd from Lapack V2.0. To | |
115 // demonstrate the bug, set both jobu and jobv to 'N' and find | |
116 // the singular values of [eye(3), eye(3)]. The result is | |
117 // [-sqrt(2), -sqrt(2), -sqrt(2)]. | |
3335 | 118 // |
119 // For Lapack 3.0, this problem seems to be fixed. | |
2621 | 120 |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
121 jobu = jobv = 'N'; |
1545 | 122 ncol_u = nrow_vt = 1; |
1543 | 123 break; |
124 | |
125 default: | |
126 break; | |
537 | 127 } |
128 | |
1544 | 129 type_computed = svd_type; |
130 | |
2621 | 131 if (! (jobu == 'N' || jobu == 'O')) |
1930 | 132 left_sm.resize (m, ncol_u); |
133 | |
134 Complex *u = left_sm.fortran_vec (); | |
135 | |
136 sigma.resize (nrow_s, ncol_s); | |
137 double *s_vec = sigma.fortran_vec (); | |
138 | |
2621 | 139 if (! (jobv == 'N' || jobv == 'O')) |
1930 | 140 right_sm.resize (nrow_vt, n); |
141 | |
142 Complex *vt = right_sm.fortran_vec (); | |
457 | 143 |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
144 // Query ZGESVD for the correct dimension of WORK. |
3336 | 145 |
5275 | 146 octave_idx_type lwork = -1; |
457 | 147 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
148 Array<Complex> work (dim_vector (1, 1)); |
3336 | 149 |
10258 | 150 octave_idx_type one = 1; |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
151 octave_idx_type m1 = std::max (m, one); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
152 octave_idx_type 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
|
153 |
10601 | 154 if (svd_driver == SVD::GESVD) |
155 { | |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
156 octave_idx_type lrwork = 5*max_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
157 Array<double> rwork (dim_vector (lrwork, 1)); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
158 |
10601 | 159 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
160 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
161 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
162 nrow_vt1, work.fortran_vec (), lwork, | |
163 rwork.fortran_vec (), info | |
164 F77_CHAR_ARG_LEN (1) | |
165 F77_CHAR_ARG_LEN (1))); | |
166 | |
167 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
168 work.resize (dim_vector (lwork, 1)); |
1543 | 169 |
10601 | 170 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
171 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
172 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
173 nrow_vt1, work.fortran_vec (), lwork, | |
174 rwork.fortran_vec (), info | |
175 F77_CHAR_ARG_LEN (1) | |
176 F77_CHAR_ARG_LEN (1))); | |
177 } | |
178 else if (svd_driver == SVD::GESDD) | |
179 { | |
180 assert (jobu == jobv); | |
181 char jobz = jobu; | |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
182 |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
183 octave_idx_type lrwork; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
184 if (jobz == 'N') |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
185 lrwork = 7*min_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
186 else |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
187 lrwork = 5*min_mn*min_mn + 5*min_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
188 Array<double> rwork (dim_vector (lrwork, 1)); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
189 |
10601 | 190 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, 8*min_mn); |
3336 | 191 |
10601 | 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 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
199 work.resize (dim_vector (lwork, 1)); |
10601 | 200 |
201 F77_XFCN (zgesdd, ZGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
202 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
203 nrow_vt1, work.fortran_vec (), lwork, | |
204 rwork.fortran_vec (), iwork, info | |
205 F77_CHAR_ARG_LEN (1))); | |
206 } | |
207 else | |
208 assert (0); // impossible | |
3336 | 209 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
210 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
211 right_sm = right_sm.hermitian (); |
457 | 212 |
213 return info; | |
214 } |