Mercurial > hg > octave-lyh
annotate liboctave/dbleSVD.cc @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | 3ce0c530a9c9 |
children | 8a5e980da6aa |
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, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
39 const octave_idx_type&, const octave_idx_type&, double*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
40 const octave_idx_type&, double*, double*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
41 const octave_idx_type&, double*, const octave_idx_type&, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
42 double*, const octave_idx_type&, octave_idx_type& |
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 (dgesdd, DGESDD) (F77_CONST_CHAR_ARG_DECL, | |
48 const octave_idx_type&, const octave_idx_type&, double*, | |
49 const octave_idx_type&, double*, double*, | |
50 const octave_idx_type&, double*, const octave_idx_type&, | |
51 double*, const octave_idx_type&, octave_idx_type *, octave_idx_type& | |
52 F77_CHAR_ARG_LEN_DECL); | |
457 | 53 } |
54 | |
1543 | 55 Matrix |
1545 | 56 SVD::left_singular_matrix (void) const |
1543 | 57 { |
1544 | 58 if (type_computed == SVD::sigma_only) |
1543 | 59 { |
60 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
61 ("SVD: U not computed because type == SVD::sigma_only"); |
1543 | 62 return Matrix (); |
63 } | |
64 else | |
65 return left_sm; | |
66 } | |
67 | |
68 Matrix | |
1545 | 69 SVD::right_singular_matrix (void) const |
1543 | 70 { |
1544 | 71 if (type_computed == SVD::sigma_only) |
1543 | 72 { |
73 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
74 ("SVD: V not computed because type == SVD::sigma_only"); |
1543 | 75 return Matrix (); |
76 } | |
77 else | |
78 return right_sm; | |
79 } | |
80 | |
5275 | 81 octave_idx_type |
10601 | 82 SVD::init (const Matrix& a, SVD::type svd_type, SVD::driver svd_driver) |
457 | 83 { |
5275 | 84 octave_idx_type info; |
457 | 85 |
5275 | 86 octave_idx_type m = a.rows (); |
87 octave_idx_type n = a.cols (); | |
457 | 88 |
1930 | 89 Matrix atmp = a; |
90 double *tmp_data = atmp.fortran_vec (); | |
457 | 91 |
5275 | 92 octave_idx_type min_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')) |
1931 | 131 left_sm.resize (m, ncol_u); |
1930 | 132 |
133 double *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 double *vt = right_sm.fortran_vec (); | |
457 | 142 |
3336 | 143 // Ask DGESVD what the dimension of WORK should be. |
1930 | 144 |
5275 | 145 octave_idx_type lwork = -1; |
3336 | 146 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
147 Array<double> work (1, 1); |
457 | 148 |
10258 | 149 octave_idx_type one = 1; |
150 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
|
151 |
10601 | 152 if (svd_driver == SVD::GESVD) |
153 { | |
154 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), | |
155 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
156 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
157 nrow_vt1, work.fortran_vec (), lwork, info | |
158 F77_CHAR_ARG_LEN (1) | |
159 F77_CHAR_ARG_LEN (1))); | |
160 | |
161 lwork = static_cast<octave_idx_type> (work(0)); | |
162 work.resize (lwork, 1); | |
163 | |
164 F77_XFCN (dgesvd, DGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), | |
165 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
166 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
167 nrow_vt1, work.fortran_vec (), lwork, info | |
168 F77_CHAR_ARG_LEN (1) | |
169 F77_CHAR_ARG_LEN (1))); | |
1543 | 170 |
10601 | 171 } |
172 else if (svd_driver == SVD::GESDD) | |
173 { | |
174 assert (jobu == jobv); | |
175 char jobz = jobu; | |
176 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, 8*min_mn); | |
177 | |
178 F77_XFCN (dgesdd, DGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
179 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
180 nrow_vt1, work.fortran_vec (), lwork, iwork, info | |
181 F77_CHAR_ARG_LEN (1))); | |
3336 | 182 |
10601 | 183 lwork = static_cast<octave_idx_type> (work(0)); |
184 work.resize (lwork, 1); | |
185 | |
186 F77_XFCN (dgesdd, DGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
187 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
188 nrow_vt1, work.fortran_vec (), lwork, iwork, info | |
189 F77_CHAR_ARG_LEN (1))); | |
190 | |
191 } | |
192 else | |
193 assert (0); // impossible | |
3336 | 194 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
195 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
196 right_sm = right_sm.transpose (); |
457 | 197 |
198 return info; | |
199 } | |
200 | |
3504 | 201 std::ostream& |
202 operator << (std::ostream& os, const SVD& a) | |
457 | 203 { |
204 os << a.left_singular_matrix () << "\n"; | |
205 os << a.singular_values () << "\n"; | |
206 os << a.right_singular_matrix () << "\n"; | |
207 | |
208 return os; | |
209 } |