457
|
1 /* |
|
2 |
1882
|
3 Copyright (C) 1996 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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
457
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
457
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
457
|
29 #endif |
|
30 |
|
31 #include "CmplxSVD.h" |
1847
|
32 #include "f77-fcn.h" |
1543
|
33 #include "lo-error.h" |
457
|
34 #include "mx-inlines.cc" |
|
35 |
|
36 extern "C" |
|
37 { |
1253
|
38 int F77_FCN (zgesvd, ZGESVD) (const char*, const char*, const int&, |
|
39 const int&, Complex*, const int&, |
|
40 double*, Complex*, const int&, |
|
41 Complex*, const int&, Complex*, |
|
42 const int&, double*, int&, long, |
|
43 long); |
457
|
44 } |
|
45 |
1543
|
46 ComplexMatrix |
|
47 ComplexSVD::left_singular_matrix (void) const |
|
48 { |
1544
|
49 if (type_computed == SVD::sigma_only) |
1543
|
50 { |
|
51 (*current_liboctave_error_handler) |
|
52 ("ComplexSVD: U not computed because type == SVD::sigma_only"); |
|
53 return ComplexMatrix (); |
|
54 } |
|
55 else |
|
56 return left_sm; |
|
57 } |
|
58 |
|
59 ComplexMatrix |
|
60 ComplexSVD::right_singular_matrix (void) const |
|
61 { |
1544
|
62 if (type_computed == SVD::sigma_only) |
1543
|
63 { |
|
64 (*current_liboctave_error_handler) |
|
65 ("ComplexSVD: V not computed because type == SVD::sigma_only"); |
|
66 return ComplexMatrix (); |
|
67 } |
|
68 else |
|
69 return right_sm; |
|
70 } |
|
71 |
457
|
72 int |
537
|
73 ComplexSVD::init (const ComplexMatrix& a, SVD::type svd_type) |
457
|
74 { |
|
75 int info; |
|
76 |
|
77 int m = a.rows (); |
|
78 int n = a.cols (); |
|
79 |
1946
|
80 ComplexMatrix atmp = a; |
|
81 Complex *tmp_data = atmp.fortran_vec (); |
457
|
82 |
|
83 int min_mn = m < n ? m : n; |
|
84 int max_mn = m > n ? m : n; |
|
85 |
1930
|
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: |
1930
|
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: |
1930
|
102 jobu = jobv = 'N'; |
1545
|
103 ncol_u = nrow_vt = 1; |
1543
|
104 break; |
|
105 |
|
106 default: |
|
107 break; |
537
|
108 } |
|
109 |
1544
|
110 type_computed = svd_type; |
|
111 |
1930
|
112 if (jobu != 'N') |
|
113 left_sm.resize (m, ncol_u); |
|
114 |
|
115 Complex *u = left_sm.fortran_vec (); |
|
116 |
|
117 sigma.resize (nrow_s, ncol_s); |
|
118 double *s_vec = sigma.fortran_vec (); |
|
119 |
|
120 if (jobv != 'N') |
|
121 right_sm.resize (nrow_vt, n); |
|
122 |
|
123 Complex *vt = right_sm.fortran_vec (); |
457
|
124 |
|
125 int lwork = 2*min_mn + max_mn; |
1930
|
126 |
|
127 Array<Complex> work (lwork); |
|
128 Complex *pwork = work.fortran_vec (); |
457
|
129 |
|
130 int lrwork = 5*max_mn; |
1930
|
131 |
|
132 Array<double> rwork (lrwork); |
|
133 double *prwork = rwork.fortran_vec (); |
457
|
134 |
1930
|
135 F77_XFCN (zgesvd, ZGESVD, (&jobu, &jobv, m, n, tmp_data, m, s_vec, u, |
|
136 m, vt, nrow_vt, pwork, lwork, prwork, info, |
|
137 1L, 1L)); |
1543
|
138 |
1930
|
139 if (f77_exception_encountered) |
|
140 (*current_liboctave_error_handler) ("unrecoverable error in zgesvd"); |
|
141 else |
1543
|
142 { |
1930
|
143 if (jobv != 'N') |
|
144 right_sm = right_sm.hermitian (); |
1543
|
145 } |
457
|
146 |
|
147 return info; |
|
148 } |
|
149 |
|
150 /* |
|
151 ;;; Local Variables: *** |
|
152 ;;; mode: C++ *** |
|
153 ;;; End: *** |
|
154 */ |