462
|
1 /* |
|
2 |
1882
|
3 Copyright (C) 1996 John W. Eaton |
462
|
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. |
462
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
462
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
462
|
29 #endif |
|
30 |
|
31 #include "EIG.h" |
2815
|
32 #include "dColVector.h" |
1847
|
33 #include "f77-fcn.h" |
462
|
34 #include "lo-error.h" |
|
35 |
|
36 extern "C" |
|
37 { |
1253
|
38 int F77_FCN (dgeev, DGEEV) (const char*, const char*, const int&, |
|
39 double*, const int&, double*, double*, |
|
40 double*, const int&, double*, |
|
41 const int&, double*, const int&, int&, |
|
42 long, long); |
462
|
43 |
1253
|
44 int F77_FCN (zgeev, ZGEEV) (const char*, const char*, const int&, |
|
45 Complex*, const int&, Complex*, |
|
46 Complex*, const int&, Complex*, |
|
47 const int&, Complex*, const int&, |
|
48 double*, int&, long, long); |
2815
|
49 |
|
50 int F77_FCN (dsyev, DSYEV) (const char*, const char*, const int&, |
|
51 double*, const int&, double*, double*, |
|
52 const int&, int&, long, long); |
|
53 |
|
54 int F77_FCN (zheev, ZHEEV) (const char*, const char*, const int&, |
|
55 Complex*, const int&, double*, Complex*, |
|
56 const int&, double*, int&, long, long); |
462
|
57 } |
|
58 |
|
59 int |
|
60 EIG::init (const Matrix& a) |
|
61 { |
2815
|
62 if (a.is_symmetric ()) |
|
63 return symmetric_init (a); |
|
64 |
1934
|
65 int n = a.rows (); |
|
66 |
|
67 if (n != a.cols ()) |
462
|
68 { |
|
69 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
70 return -1; |
|
71 } |
|
72 |
2815
|
73 int info = 0; |
462
|
74 |
1934
|
75 Matrix atmp = a; |
|
76 double *tmp_data = atmp.fortran_vec (); |
|
77 |
|
78 Array<double> wr (n); |
|
79 double *pwr = wr.fortran_vec (); |
|
80 |
|
81 Array<double> wi (n); |
|
82 double *pwi = wi.fortran_vec (); |
|
83 |
462
|
84 Matrix vr (n, n); |
|
85 double *pvr = vr.fortran_vec (); |
1934
|
86 |
2815
|
87 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
88 // lwork that would result in more efficient computations. |
|
89 |
462
|
90 int lwork = 8*n; |
1934
|
91 Array<double> work (lwork); |
|
92 double *pwork = work.fortran_vec (); |
462
|
93 |
1365
|
94 double *dummy = 0; |
462
|
95 int idummy = 1; |
|
96 |
1934
|
97 F77_XFCN (dgeev, DGEEV, ("N", "V", n, tmp_data, n, pwr, pwi, dummy, |
|
98 idummy, pvr, n, pwork, lwork, info, 1L, 1L)); |
462
|
99 |
2815
|
100 if (f77_exception_encountered || info < 0) |
1934
|
101 (*current_liboctave_error_handler) ("unrecoverable error in dgeev"); |
|
102 else |
462
|
103 { |
2815
|
104 if (info > 0) |
|
105 (*current_liboctave_error_handler) ("dgeev failed to converge"); |
|
106 else |
|
107 { |
|
108 lambda.resize (n); |
|
109 v.resize (n, n); |
1934
|
110 |
2815
|
111 for (int j = 0; j < n; j++) |
462
|
112 { |
2815
|
113 if (wi.elem (j) == 0.0) |
|
114 { |
|
115 lambda.elem (j) = Complex (wr.elem (j)); |
|
116 for (int i = 0; i < n; i++) |
|
117 v.elem (i, j) = vr.elem (i, j); |
|
118 } |
|
119 else |
1934
|
120 { |
2815
|
121 if (j+1 >= n) |
|
122 { |
|
123 (*current_liboctave_error_handler) |
|
124 ("EIG: internal error"); |
|
125 return -1; |
|
126 } |
|
127 |
|
128 for (int i = 0; i < n; i++) |
|
129 { |
|
130 lambda.elem(j) = Complex (wr.elem(j), wi.elem(j)); |
|
131 lambda.elem(j+1) = Complex (wr.elem(j+1), wi.elem(j+1)); |
|
132 double real_part = vr.elem (i, j); |
|
133 double imag_part = vr.elem (i, j+1); |
|
134 v.elem (i, j) = Complex (real_part, imag_part); |
|
135 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
136 } |
|
137 j++; |
1934
|
138 } |
2815
|
139 } |
|
140 } |
|
141 } |
|
142 |
|
143 return info; |
|
144 } |
|
145 |
|
146 int |
|
147 EIG::symmetric_init (const Matrix& a) |
|
148 { |
|
149 int n = a.rows (); |
1934
|
150 |
2815
|
151 if (n != a.cols ()) |
|
152 { |
|
153 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
154 return -1; |
|
155 } |
|
156 |
|
157 int info = 0; |
|
158 |
|
159 Matrix atmp = a; |
|
160 double *tmp_data = atmp.fortran_vec (); |
|
161 |
|
162 Array<double> wr (n); |
|
163 double *pwr = wr.fortran_vec (); |
|
164 |
|
165 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
166 // lwork that would result in more efficient computations. |
|
167 |
|
168 int lwork = 8*n; |
|
169 Array<double> work (lwork); |
|
170 double *pwork = work.fortran_vec (); |
|
171 |
|
172 F77_XFCN (dsyev, DSYEV, ("V", "U", n, tmp_data, n, pwr, pwork, |
|
173 lwork, info, 1L, 1L)); |
|
174 |
|
175 if (f77_exception_encountered || info < 0) |
|
176 (*current_liboctave_error_handler) ("unrecoverable error in dsyev"); |
|
177 else |
|
178 { |
|
179 if (info > 0) |
|
180 (*current_liboctave_error_handler) ("dsyev failed to converge"); |
|
181 else |
|
182 { |
|
183 lambda.resize (n); |
|
184 |
|
185 for (int j = 0; j < n; j++) |
|
186 lambda.elem (j) = Complex (wr.elem (j)); |
|
187 |
|
188 v = atmp; |
462
|
189 } |
|
190 } |
|
191 |
|
192 return info; |
|
193 } |
|
194 |
|
195 int |
|
196 EIG::init (const ComplexMatrix& a) |
|
197 { |
2815
|
198 if (a.is_hermitian ()) |
|
199 return hermitian_init (a); |
|
200 |
1934
|
201 int n = a.rows (); |
|
202 |
|
203 if (n != a.cols ()) |
462
|
204 { |
|
205 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
206 return -1; |
|
207 } |
|
208 |
2815
|
209 int info = 0; |
462
|
210 |
1934
|
211 ComplexMatrix atmp = a; |
|
212 Complex *tmp_data = atmp.fortran_vec (); |
462
|
213 |
2815
|
214 ComplexColumnVector w (n); |
|
215 Complex *pw = w.fortran_vec (); |
|
216 |
|
217 ComplexMatrix vtmp (n, n); |
|
218 Complex *pv = vtmp.fortran_vec (); |
|
219 |
|
220 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
221 // lwork that would result in more efficient computations. |
|
222 |
462
|
223 int lwork = 8*n; |
1934
|
224 Array<Complex> work (lwork); |
|
225 Complex *pwork = work.fortran_vec (); |
|
226 |
2815
|
227 int lrwork = 2*n; |
|
228 Array<double> rwork (lrwork); |
1934
|
229 double *prwork = rwork.fortran_vec (); |
462
|
230 |
1365
|
231 Complex *dummy = 0; |
462
|
232 int idummy = 1; |
|
233 |
1934
|
234 F77_XFCN (zgeev, ZGEEV, ("N", "V", n, tmp_data, n, pw, dummy, idummy, |
2815
|
235 pv, n, pwork, lwork, prwork, info, 1L, 1L)); |
|
236 |
|
237 if (f77_exception_encountered || info < 0) |
|
238 (*current_liboctave_error_handler) ("unrecoverable error in zgeev"); |
|
239 else if (info > 0) |
|
240 (*current_liboctave_error_handler) ("zgeev failed to converge"); |
|
241 else |
|
242 { |
|
243 lambda = w; |
|
244 v = vtmp; |
|
245 } |
|
246 |
|
247 return info; |
|
248 } |
|
249 |
|
250 int |
|
251 EIG::hermitian_init (const ComplexMatrix& a) |
|
252 { |
|
253 int n = a.rows (); |
|
254 |
|
255 if (n != a.cols ()) |
|
256 { |
|
257 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
258 return -1; |
|
259 } |
|
260 |
|
261 int info = 0; |
462
|
262 |
2815
|
263 ComplexMatrix atmp = a; |
|
264 Complex *tmp_data = atmp.fortran_vec (); |
|
265 |
|
266 ColumnVector w (n); |
|
267 double *pw = w.fortran_vec (); |
|
268 |
|
269 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
270 // lwork that would result in more efficient computations. |
|
271 |
|
272 int lwork = 8*n; |
|
273 Array<Complex> work (lwork); |
|
274 Complex *pwork = work.fortran_vec (); |
|
275 |
|
276 int lrwork = 3*n; |
|
277 Array<double> rwork (lrwork); |
|
278 double *prwork = rwork.fortran_vec (); |
|
279 |
|
280 F77_XFCN (zheev, ZHEEV, ("V", "U", n, tmp_data, n, pw, pwork, |
|
281 lwork, prwork, info, 1L, 1L)); |
|
282 |
|
283 if (f77_exception_encountered || info < 0) |
|
284 (*current_liboctave_error_handler) ("unrecoverable error in zheev"); |
|
285 else if (info > 0) |
|
286 (*current_liboctave_error_handler) ("zheev failed to converge"); |
|
287 else |
|
288 { |
|
289 lambda = w; |
|
290 v = atmp; |
|
291 } |
462
|
292 |
|
293 return info; |
|
294 } |
|
295 |
|
296 /* |
|
297 ;;; Local Variables: *** |
|
298 ;;; mode: C++ *** |
|
299 ;;; End: *** |
|
300 */ |