Mercurial > hg > octave-nkf
annotate liboctave/dbleSVD.cc @ 11513:a2289858dcb2
more warning flags for GCC if configured with --enable-extra-warning-flags (on by default)
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 13 Jan 2011 07:49:46 -0500 |
parents | 8a5e980da6aa |
children | fd0a3ac60b0e |
rev | line source |
---|---|
457 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, 2004, |
8920 | 4 2005, 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 | |
3503 | 28 #include <iostream> |
1631 | 29 |
457 | 30 #include "dbleSVD.h" |
1847 | 31 #include "f77-fcn.h" |
10601 | 32 #include "oct-locbuf.h" |
457 | 33 |
34 extern "C" | |
35 { | |
4552 | 36 F77_RET_T |
37 F77_FUNC (dgesvd, DGESVD) (F77_CONST_CHAR_ARG_DECL, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
38 F77_CONST_CHAR_ARG_DECL, |
11495 | 39 const octave_idx_type&, const octave_idx_type&, |
40 double*, const octave_idx_type&, double*, | |
41 double*, const octave_idx_type&, double*, | |
42 const octave_idx_type&, double*, | |
43 const octave_idx_type&, octave_idx_type& | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
44 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
45 F77_CHAR_ARG_LEN_DECL); |
10601 | 46 |
47 F77_RET_T | |
48 F77_FUNC (dgesdd, DGESDD) (F77_CONST_CHAR_ARG_DECL, | |
11495 | 49 const octave_idx_type&, const octave_idx_type&, |
50 double*, const octave_idx_type&, double*, | |
51 double*, const octave_idx_type&, double*, | |
52 const octave_idx_type&, double*, | |
53 const octave_idx_type&, octave_idx_type *, | |
54 octave_idx_type& | |
10601 | 55 F77_CHAR_ARG_LEN_DECL); |
457 | 56 } |
57 | |
1543 | 58 Matrix |
1545 | 59 SVD::left_singular_matrix (void) const |
1543 | 60 { |
1544 | 61 if (type_computed == SVD::sigma_only) |
1543 | 62 { |
63 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
64 ("SVD: U not computed because type == SVD::sigma_only"); |
1543 | 65 return Matrix (); |
66 } | |
67 else | |
68 return left_sm; | |
69 } | |
70 | |
71 Matrix | |
1545 | 72 SVD::right_singular_matrix (void) const |
1543 | 73 { |
1544 | 74 if (type_computed == SVD::sigma_only) |
1543 | 75 { |
76 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
77 ("SVD: V not computed because type == SVD::sigma_only"); |
1543 | 78 return Matrix (); |
79 } | |
80 else | |
81 return right_sm; | |
82 } | |
83 | |
5275 | 84 octave_idx_type |
10601 | 85 SVD::init (const Matrix& a, SVD::type svd_type, SVD::driver svd_driver) |
457 | 86 { |
5275 | 87 octave_idx_type info; |
457 | 88 |
5275 | 89 octave_idx_type m = a.rows (); |
90 octave_idx_type n = a.cols (); | |
457 | 91 |
1930 | 92 Matrix atmp = a; |
93 double *tmp_data = atmp.fortran_vec (); | |
457 | 94 |
5275 | 95 octave_idx_type min_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')) |
1931 | 134 left_sm.resize (m, ncol_u); |
1930 | 135 |
136 double *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 double *vt = right_sm.fortran_vec (); | |
457 | 145 |
3336 | 146 // Ask DGESVD what the dimension of WORK should be. |
1930 | 147 |
5275 | 148 octave_idx_type lwork = -1; |
3336 | 149 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
150 Array<double> work (1, 1); |
457 | 151 |
10258 | 152 octave_idx_type one = 1; |
153 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
|
154 |
10601 | 155 if (svd_driver == SVD::GESVD) |
156 { | |
157 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), | |
158 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
159 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
160 nrow_vt1, work.fortran_vec (), lwork, info | |
161 F77_CHAR_ARG_LEN (1) | |
162 F77_CHAR_ARG_LEN (1))); | |
163 | |
164 lwork = static_cast<octave_idx_type> (work(0)); | |
165 work.resize (lwork, 1); | |
166 | |
167 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), | |
168 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
169 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
170 nrow_vt1, work.fortran_vec (), lwork, info | |
171 F77_CHAR_ARG_LEN (1) | |
172 F77_CHAR_ARG_LEN (1))); | |
1543 | 173 |
10601 | 174 } |
175 else if (svd_driver == SVD::GESDD) | |
176 { | |
177 assert (jobu == jobv); | |
178 char jobz = jobu; | |
179 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, 8*min_mn); | |
180 | |
181 F77_XFCN (dgesdd, DGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
182 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
183 nrow_vt1, work.fortran_vec (), lwork, iwork, info | |
184 F77_CHAR_ARG_LEN (1))); | |
3336 | 185 |
10601 | 186 lwork = static_cast<octave_idx_type> (work(0)); |
187 work.resize (lwork, 1); | |
188 | |
189 F77_XFCN (dgesdd, DGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
190 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
191 nrow_vt1, work.fortran_vec (), lwork, iwork, info | |
192 F77_CHAR_ARG_LEN (1))); | |
193 | |
194 } | |
195 else | |
196 assert (0); // impossible | |
3336 | 197 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
198 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
199 right_sm = right_sm.transpose (); |
457 | 200 |
201 return info; | |
202 } | |
203 | |
3504 | 204 std::ostream& |
205 operator << (std::ostream& os, const SVD& a) | |
457 | 206 { |
207 os << a.left_singular_matrix () << "\n"; | |
208 os << a.singular_values () << "\n"; | |
209 os << a.right_singular_matrix () << "\n"; | |
210 | |
211 return os; | |
212 } |