Mercurial > hg > octave-nkf
annotate liboctave/CmplxSVD.cc @ 10431:5dd7a7bf4950
simplify sparse->full conversions in liboctave
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 22 Mar 2010 12:05:33 +0100 |
parents | 12884915a8e4 |
children | 3ce0c530a9c9 |
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" |
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, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
37 const octave_idx_type&, const octave_idx_type&, Complex*, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
38 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
|
39 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
|
40 double*, octave_idx_type& |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
41 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
42 F77_CHAR_ARG_LEN_DECL); |
457 | 43 } |
44 | |
1543 | 45 ComplexMatrix |
46 ComplexSVD::left_singular_matrix (void) const | |
47 { | |
1544 | 48 if (type_computed == SVD::sigma_only) |
1543 | 49 { |
50 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
51 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
1543 | 52 return ComplexMatrix (); |
53 } | |
54 else | |
55 return left_sm; | |
56 } | |
57 | |
58 ComplexMatrix | |
59 ComplexSVD::right_singular_matrix (void) const | |
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 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
1543 | 65 return ComplexMatrix (); |
66 } | |
67 else | |
68 return right_sm; | |
69 } | |
70 | |
5275 | 71 octave_idx_type |
537 | 72 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type) |
457 | 73 { |
5275 | 74 octave_idx_type info; |
457 | 75 |
5275 | 76 octave_idx_type m = a.rows (); |
77 octave_idx_type n = a.cols (); | |
457 | 78 |
1946 | 79 ComplexMatrix atmp = a; |
80 Complex *tmp_data = atmp.fortran_vec (); | |
457 | 81 |
5275 | 82 octave_idx_type min_mn = m < n ? m : n; |
83 octave_idx_type max_mn = m > n ? m : n; | |
457 | 84 |
1930 | 85 char jobu = 'A'; |
86 char jobv = 'A'; | |
537 | 87 |
5275 | 88 octave_idx_type ncol_u = m; |
89 octave_idx_type nrow_vt = n; | |
90 octave_idx_type nrow_s = m; | |
91 octave_idx_type ncol_s = n; | |
537 | 92 |
1543 | 93 switch (svd_type) |
537 | 94 { |
1543 | 95 case SVD::economy: |
1930 | 96 jobu = jobv = 'S'; |
537 | 97 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543 | 98 break; |
99 | |
100 case SVD::sigma_only: | |
2621 | 101 |
102 // Note: for this case, both jobu and jobv should be 'N', but | |
103 // there seems to be a bug in dgesvd from Lapack V2.0. To | |
104 // demonstrate the bug, set both jobu and jobv to 'N' and find | |
105 // the singular values of [eye(3), eye(3)]. The result is | |
106 // [-sqrt(2), -sqrt(2), -sqrt(2)]. | |
3335 | 107 // |
108 // For Lapack 3.0, this problem seems to be fixed. | |
2621 | 109 |
3335 | 110 jobu = 'N'; |
2621 | 111 jobv = 'N'; |
1545 | 112 ncol_u = nrow_vt = 1; |
1543 | 113 break; |
114 | |
115 default: | |
116 break; | |
537 | 117 } |
118 | |
1544 | 119 type_computed = svd_type; |
120 | |
2621 | 121 if (! (jobu == 'N' || jobu == 'O')) |
1930 | 122 left_sm.resize (m, ncol_u); |
123 | |
124 Complex *u = left_sm.fortran_vec (); | |
125 | |
126 sigma.resize (nrow_s, ncol_s); | |
127 double *s_vec = sigma.fortran_vec (); | |
128 | |
2621 | 129 if (! (jobv == 'N' || jobv == 'O')) |
1930 | 130 right_sm.resize (nrow_vt, n); |
131 | |
132 Complex *vt = right_sm.fortran_vec (); | |
457 | 133 |
5275 | 134 octave_idx_type lrwork = 5*max_mn; |
1930 | 135 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
136 Array<double> rwork (lrwork, 1); |
3336 | 137 |
138 // Ask ZGESVD what the dimension of WORK should be. | |
139 | |
5275 | 140 octave_idx_type lwork = -1; |
457 | 141 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
142 Array<Complex> work (1, 1); |
3336 | 143 |
10258 | 144 octave_idx_type one = 1; |
145 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
|
146 |
4552 | 147 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
148 F77_CONST_CHAR_ARG2 (&jobv, 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
149 m, n, tmp_data, m1, s_vec, u, m1, vt, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
150 nrow_vt1, work.fortran_vec (), lwork, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
151 rwork.fortran_vec (), info |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
152 F77_CHAR_ARG_LEN (1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
153 F77_CHAR_ARG_LEN (1))); |
1543 | 154 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
155 lwork = static_cast<octave_idx_type> (work(0).real ()); |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
156 work.resize (lwork, 1); |
3336 | 157 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
158 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
159 F77_CONST_CHAR_ARG2 (&jobv, 1), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
160 m, n, tmp_data, m1, s_vec, u, m1, vt, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
161 nrow_vt1, work.fortran_vec (), lwork, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
162 rwork.fortran_vec (), info |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
163 F77_CHAR_ARG_LEN (1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
164 F77_CHAR_ARG_LEN (1))); |
3336 | 165 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
166 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
167 right_sm = right_sm.hermitian (); |
457 | 168 |
169 return info; | |
170 } |