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