457
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
457
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
457
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
457
|
30 #endif |
|
31 |
|
32 #include "CmplxSVD.h" |
1368
|
33 #include "f77-uscore.h" |
1543
|
34 #include "lo-error.h" |
457
|
35 #include "mx-inlines.cc" |
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (zgesvd, ZGESVD) (const char*, const char*, const int&, |
|
40 const int&, Complex*, const int&, |
|
41 double*, Complex*, const int&, |
|
42 Complex*, const int&, Complex*, |
|
43 const int&, double*, int&, long, |
|
44 long); |
457
|
45 } |
|
46 |
1543
|
47 ComplexMatrix |
|
48 ComplexSVD::left_singular_matrix (void) const |
|
49 { |
1544
|
50 if (type_computed == SVD::sigma_only) |
1543
|
51 { |
|
52 (*current_liboctave_error_handler) |
|
53 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
54 return ComplexMatrix (); |
|
55 } |
|
56 else |
|
57 return left_sm; |
|
58 } |
|
59 |
|
60 ComplexMatrix |
|
61 ComplexSVD::right_singular_matrix (void) const |
|
62 { |
1544
|
63 if (type_computed == SVD::sigma_only) |
1543
|
64 { |
|
65 (*current_liboctave_error_handler) |
|
66 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
67 return ComplexMatrix (); |
|
68 } |
|
69 else |
|
70 return right_sm; |
|
71 } |
|
72 |
457
|
73 int |
537
|
74 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type) |
457
|
75 { |
|
76 int info; |
|
77 |
|
78 int m = a.rows (); |
|
79 int n = a.cols (); |
|
80 |
|
81 Complex *tmp_data = dup (a.data (), a.length ()); |
|
82 |
|
83 int min_mn = m < n ? m : n; |
|
84 int max_mn = m > n ? m : n; |
|
85 |
1251
|
86 char *jobu = "A"; |
|
87 char *jobv = "A"; |
537
|
88 |
|
89 int ncol_u = m; |
|
90 int nrow_vt = n; |
|
91 int nrow_s = m; |
|
92 int ncol_s = n; |
|
93 |
1543
|
94 switch (svd_type) |
537
|
95 { |
1543
|
96 case SVD::economy: |
|
97 jobu = jobv ="S"; |
537
|
98 ncol_u = nrow_vt = nrow_s = ncol_s = min_mn; |
1543
|
99 break; |
|
100 |
|
101 case SVD::sigma_only: |
|
102 jobu = jobv ="N"; |
|
103 ncol_u = nrow_vt = 0; |
|
104 break; |
|
105 |
|
106 default: |
|
107 break; |
537
|
108 } |
|
109 |
1544
|
110 type_computed = svd_type; |
|
111 |
1543
|
112 Complex *u = (ncol_u > 0 ? new Complex[m * ncol_u] : 0); |
457
|
113 double *s_vec = new double[min_mn]; |
1543
|
114 Complex *vt = (nrow_vt > 0 ? new Complex[nrow_vt * n] : 0); |
457
|
115 |
|
116 int lwork = 2*min_mn + max_mn; |
|
117 Complex *work = new Complex[lwork]; |
|
118 |
|
119 int lrwork = 5*max_mn; |
|
120 double *rwork = new double[lrwork]; |
|
121 |
1253
|
122 F77_FCN (zgesvd, ZGESVD) (jobu, jobv, m, n, tmp_data, m, s_vec, u, |
|
123 m, vt, nrow_vt, work, lwork, rwork, info, |
|
124 1L, 1L); |
457
|
125 |
1543
|
126 if (ncol_u > 0) |
|
127 left_sm = ComplexMatrix (u, m, ncol_u); |
|
128 |
537
|
129 sigma = DiagMatrix (s_vec, nrow_s, ncol_s); |
1543
|
130 |
|
131 if (nrow_vt > 0) |
|
132 { |
|
133 ComplexMatrix vt_m (vt, nrow_vt, n); |
|
134 right_sm = vt_m.hermitian (); |
|
135 } |
457
|
136 |
|
137 delete [] tmp_data; |
|
138 delete [] work; |
|
139 |
|
140 return info; |
|
141 } |
|
142 |
|
143 /* |
|
144 ;;; Local Variables: *** |
|
145 ;;; mode: C++ *** |
|
146 ;;; page-delimiter: "^/\\*" *** |
|
147 ;;; End: *** |
|
148 */ |