Mercurial > octave
annotate liboctave/numeric/CmplxSVD.cc @ 21202:f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
* Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc, Array-f.cc, Array-fC.cc,
Array-i.cc, Array-idx-vec.cc, Array-s.cc, Array-str.cc, Array-util.cc,
Array-voidp.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc,
CNDArray.cc, CRowVector.cc, CSparse.cc, CSparse.h, DiagArray2.cc, MArray-C.cc,
MArray-d.cc, MArray-f.cc, MArray-fC.cc, MArray-i.cc, MArray-s.cc, MArray.cc,
MDiagArray2.cc, MSparse-C.cc, MSparse-d.cc, MSparse.h, MatrixType.cc,
PermMatrix.cc, Range.cc, Sparse-C.cc, Sparse-b.cc, Sparse-d.cc, Sparse.cc,
boolMatrix.cc, boolNDArray.cc, boolSparse.cc, chMatrix.cc, chNDArray.cc,
dColVector.cc, dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc,
dSparse.cc, dSparse.h, dim-vector.cc, fCColVector.cc, fCDiagMatrix.cc,
fCMatrix.cc, fCNDArray.cc, fCRowVector.cc, fColVector.cc, fDiagMatrix.cc,
fMatrix.cc, fNDArray.cc, fRowVector.cc, idx-vector.cc, int16NDArray.cc,
int32NDArray.cc, int64NDArray.cc, int8NDArray.cc, intNDArray.cc,
uint16NDArray.cc, uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc,
blaswrap.c, cquit.c, f77-extern.cc, f77-fcn.c, f77-fcn.h, lo-error.c, quit.cc,
quit.h, CmplxAEPBAL.cc, CmplxCHOL.cc, CmplxGEPBAL.cc, CmplxHESS.cc, CmplxLU.cc,
CmplxQR.cc, CmplxQRP.cc, CmplxSCHUR.cc, CmplxSVD.cc, CollocWt.cc, DASPK.cc,
DASRT.cc, DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, base-lu.cc, base-qr.cc,
dbleAEPBAL.cc, dbleCHOL.cc, dbleGEPBAL.cc, dbleHESS.cc, dbleLU.cc, dbleQR.cc,
dbleQRP.cc, dbleSCHUR.cc, dbleSVD.cc, eigs-base.cc, fCmplxAEPBAL.cc,
fCmplxCHOL.cc, fCmplxGEPBAL.cc, fCmplxHESS.cc, fCmplxLU.cc, fCmplxQR.cc,
fCmplxQRP.cc, fCmplxSCHUR.cc, fCmplxSVD.cc, fEIG.cc, floatAEPBAL.cc,
floatCHOL.cc, floatGEPBAL.cc, floatHESS.cc, floatLU.cc, floatQR.cc,
floatQRP.cc, floatSCHUR.cc, floatSVD.cc, lo-mappers.cc, lo-specfun.cc,
oct-convn.cc, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-rand.cc,
oct-spparms.cc, randgamma.c, randmtzig.c, randpoisson.c, sparse-chol.cc,
sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, mx-defs.h, dir-ops.cc,
file-ops.cc, file-stat.cc, lo-sysdep.cc, mach-info.cc, oct-env.cc,
oct-group.cc, oct-openmp.h, oct-passwd.cc, oct-syscalls.cc, oct-time.cc,
oct-uname.cc, pathlen.h, sysdir.h, syswait.h, cmd-edit.cc, cmd-hist.cc,
data-conv.cc, f2c-main.c, glob-match.cc, lo-array-errwarn.cc,
lo-array-gripes.cc, lo-cutils.c, lo-cutils.h, lo-ieee.cc, lo-math.h,
lo-regexp.cc, lo-utils.cc, oct-base64.cc, oct-glob.cc, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.cc, oct-mutex.cc, oct-refcount.h, oct-rl-edit.c,
oct-rl-hist.c, oct-shlib.cc, oct-sort.cc, pathsearch.cc, singleton-cleanup.cc,
sparse-sort.cc, sparse-util.cc, statdefs.h, str-vec.cc, unwind-prot.cc,
url-transfer.cc, display-available.h, main-cli.cc, main-gui.cc, main.in.cc,
mkoctfile.in.cc, octave-config.in.cc, shared-fcns.h:
indent #ifdef blocks in liboctave and src directories.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 06 Feb 2016 06:40:13 -0800 |
parents | 7cac4e7458f2 |
children |
rev | line source |
---|---|
457 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
3 Copyright (C) 1994-2015 John W. Eaton |
457 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
457 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
457 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21136
diff
changeset
|
24 # include <config.h> |
457 | 25 #endif |
26 | |
27 #include "CmplxSVD.h" | |
1847 | 28 #include "f77-fcn.h" |
1543 | 29 #include "lo-error.h" |
10601 | 30 #include "oct-locbuf.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, |
11495 | 37 const octave_idx_type&, const octave_idx_type&, |
38 Complex*, const octave_idx_type&, | |
39 double*, Complex*, const octave_idx_type&, | |
40 Complex*, const octave_idx_type&, Complex*, | |
41 const octave_idx_type&, double*, octave_idx_type& | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
42 F77_CHAR_ARG_LEN_DECL |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10258
diff
changeset
|
43 F77_CHAR_ARG_LEN_DECL); |
10601 | 44 |
45 F77_RET_T | |
46 F77_FUNC (zgesdd, ZGESDD) (F77_CONST_CHAR_ARG_DECL, | |
11495 | 47 const octave_idx_type&, const octave_idx_type&, |
48 Complex*, const octave_idx_type&, | |
49 double*, Complex*, const octave_idx_type&, | |
50 Complex*, const octave_idx_type&, Complex*, | |
51 const octave_idx_type&, double*, | |
52 octave_idx_type *, octave_idx_type& | |
10601 | 53 F77_CHAR_ARG_LEN_DECL); |
457 | 54 } |
55 | |
1543 | 56 ComplexMatrix |
57 ComplexSVD::left_singular_matrix (void) const | |
58 { | |
1544 | 59 if (type_computed == SVD::sigma_only) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
60 (*current_liboctave_error_handler) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
61 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
62 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
63 return left_sm; |
1543 | 64 } |
65 | |
66 ComplexMatrix | |
67 ComplexSVD::right_singular_matrix (void) const | |
68 { | |
1544 | 69 if (type_computed == SVD::sigma_only) |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
70 (*current_liboctave_error_handler) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
71 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
72 |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
73 return right_sm; |
1543 | 74 } |
75 | |
5275 | 76 octave_idx_type |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
77 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 SVD::driver svd_driver) |
457 | 79 { |
5275 | 80 octave_idx_type info; |
457 | 81 |
5275 | 82 octave_idx_type m = a.rows (); |
83 octave_idx_type n = a.cols (); | |
457 | 84 |
1946 | 85 ComplexMatrix atmp = a; |
86 Complex *tmp_data = atmp.fortran_vec (); | |
457 | 87 |
5275 | 88 octave_idx_type min_mn = m < n ? m : n; |
89 octave_idx_type max_mn = m > n ? m : n; | |
457 | 90 |
1930 | 91 char jobu = 'A'; |
92 char jobv = 'A'; | |
537 | 93 |
5275 | 94 octave_idx_type ncol_u = m; |
95 octave_idx_type nrow_vt = n; | |
96 octave_idx_type nrow_s = m; | |
97 octave_idx_type ncol_s = n; | |
537 | 98 |
1543 | 99 switch (svd_type) |
537 | 100 { |
1543 | 101 case SVD::economy: |
1930 | 102 jobu = jobv = 'S'; |
537 | 103 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543 | 104 break; |
105 | |
106 case SVD::sigma_only: | |
2621 | 107 |
108 // Note: for this case, both jobu and jobv should be 'N', but | |
109 // there seems to be a bug in dgesvd from Lapack V2.0. To | |
110 // demonstrate the bug, set both jobu and jobv to 'N' and find | |
111 // the singular values of [eye(3), eye(3)]. The result is | |
112 // [-sqrt(2), -sqrt(2), -sqrt(2)]. | |
3335 | 113 // |
114 // For Lapack 3.0, this problem seems to be fixed. | |
2621 | 115 |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
116 jobu = jobv = 'N'; |
1545 | 117 ncol_u = nrow_vt = 1; |
1543 | 118 break; |
119 | |
120 default: | |
121 break; | |
537 | 122 } |
123 | |
1544 | 124 type_computed = svd_type; |
125 | |
2621 | 126 if (! (jobu == 'N' || jobu == 'O')) |
1930 | 127 left_sm.resize (m, ncol_u); |
128 | |
129 Complex *u = left_sm.fortran_vec (); | |
130 | |
131 sigma.resize (nrow_s, ncol_s); | |
132 double *s_vec = sigma.fortran_vec (); | |
133 | |
2621 | 134 if (! (jobv == 'N' || jobv == 'O')) |
1930 | 135 right_sm.resize (nrow_vt, n); |
136 | |
137 Complex *vt = right_sm.fortran_vec (); | |
457 | 138 |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
139 // Query ZGESVD for the correct dimension of WORK. |
3336 | 140 |
5275 | 141 octave_idx_type lwork = -1; |
457 | 142 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
143 Array<Complex> work (dim_vector (1, 1)); |
3336 | 144 |
10258 | 145 octave_idx_type one = 1; |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
146 octave_idx_type m1 = std::max (m, one); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
147 octave_idx_type 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
|
148 |
10601 | 149 if (svd_driver == SVD::GESVD) |
150 { | |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
151 octave_idx_type lrwork = 5*max_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
152 Array<double> rwork (dim_vector (lrwork, 1)); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
153 |
10601 | 154 F77_XFCN (zgesvd, ZGESVD, (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, | |
158 rwork.fortran_vec (), info | |
159 F77_CHAR_ARG_LEN (1) | |
160 F77_CHAR_ARG_LEN (1))); | |
161 | |
162 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
163 work.resize (dim_vector (lwork, 1)); |
1543 | 164 |
10601 | 165 F77_XFCN (zgesvd, ZGESVD, (F77_CONST_CHAR_ARG2 (&jobu, 1), |
166 F77_CONST_CHAR_ARG2 (&jobv, 1), | |
167 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
168 nrow_vt1, work.fortran_vec (), lwork, | |
169 rwork.fortran_vec (), info | |
170 F77_CHAR_ARG_LEN (1) | |
171 F77_CHAR_ARG_LEN (1))); | |
172 } | |
173 else if (svd_driver == SVD::GESDD) | |
174 { | |
175 assert (jobu == jobv); | |
176 char jobz = jobu; | |
15887
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
177 |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
178 octave_idx_type lrwork; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
179 if (jobz == 'N') |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
180 lrwork = 7*min_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
181 else |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
182 lrwork = 5*min_mn*min_mn + 5*min_mn; |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
183 Array<double> rwork (dim_vector (lrwork, 1)); |
8ced82e96b48
Fix segfaults with gesdd driver for svd (bug #37998).
Rik <rik@octave.org>
parents:
14138
diff
changeset
|
184 |
10601 | 185 OCTAVE_LOCAL_BUFFER (octave_idx_type, iwork, 8*min_mn); |
3336 | 186 |
10601 | 187 F77_XFCN (zgesdd, ZGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), |
188 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
189 nrow_vt1, work.fortran_vec (), lwork, | |
190 rwork.fortran_vec (), iwork, info | |
191 F77_CHAR_ARG_LEN (1))); | |
192 | |
193 lwork = static_cast<octave_idx_type> (work(0).real ()); | |
11574
a83bad07f7e3
attempt better backward compatibility for Array resize functions
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
194 work.resize (dim_vector (lwork, 1)); |
10601 | 195 |
196 F77_XFCN (zgesdd, ZGESDD, (F77_CONST_CHAR_ARG2 (&jobz, 1), | |
197 m, n, tmp_data, m1, s_vec, u, m1, vt, | |
198 nrow_vt1, work.fortran_vec (), lwork, | |
199 rwork.fortran_vec (), iwork, info | |
200 F77_CHAR_ARG_LEN (1))); | |
201 } | |
202 else | |
203 assert (0); // impossible | |
3336 | 204 |
7482
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
205 if (! (jobv == 'N' || jobv == 'O')) |
29980c6b8604
don't check f77_exception_encountered
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
206 right_sm = right_sm.hermitian (); |
457 | 207 |
208 return info; | |
209 } |