Mercurial > hg > octave-lyh
annotate liboctave/CmplxSVD.cc @ 8523:ad3afaaa19c1
implement non-copying contiguous range indexing
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 15 Jan 2009 07:22:24 +0100 |
parents | 29980c6b8604 |
children | eb63fbe60fab |
rev | line source |
---|---|
457 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2003, 2004, 2005, |
4 2007 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, | |
36 F77_CONST_CHAR_ARG_DECL, | |
5275 | 37 const octave_idx_type&, const octave_idx_type&, Complex*, |
38 const octave_idx_type&, double*, Complex*, const octave_idx_type&, | |
39 Complex*, const octave_idx_type&, Complex*, const octave_idx_type&, | |
40 double*, octave_idx_type& | |
4552 | 41 F77_CHAR_ARG_LEN_DECL |
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) | |
51 ("ComplexSVD: U not computed because type == SVD::sigma_only"); | |
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) | |
64 ("ComplexSVD: V not computed because type == SVD::sigma_only"); | |
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 |
136 Array<double> rwork (lrwork); | |
3336 | 137 |
138 // Ask ZGESVD what the dimension of WORK should be. | |
139 | |
5275 | 140 octave_idx_type lwork = -1; |
457 | 141 |
3336 | 142 Array<Complex> work (1); |
143 | |
4552 | 144 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
145 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
146 m, n, tmp_data, m, s_vec, u, m, vt, | |
147 nrow_vt, work.fortran_vec (), lwork, | |
148 rwork.fortran_vec (), info | |
149 F77_CHAR_ARG_LEN (1) | |
150 F77_CHAR_ARG_LEN (1))); | |
1543 | 151 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
152 lwork = static_cast<octave_idx_type> (work(0).real ()); |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
153 work.resize (lwork); |
3336 | 154 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
155 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
156 F77_CONST_CHAR_ARG2 (&jobv, 1), |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
157 m, n, tmp_data, m, s_vec, u, m, vt, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
158 nrow_vt, work.fortran_vec (), lwork, |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
159 rwork.fortran_vec (), info |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
160 F77_CHAR_ARG_LEN (1) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
161 F77_CHAR_ARG_LEN (1))); |
3336 | 162 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
163 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
164 right_sm = right_sm.hermitian (); |
457 | 165 |
166 return info; | |
167 } | |
168 | |
169 /* | |
170 ;;; Local Variables: *** | |
171 ;;; mode: C++ *** | |
172 ;;; End: *** | |
173 */ |