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