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 |
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 |
3178
|
128 lambda.elem(j) = Complex (wr.elem(j), wi.elem(j)); |
|
129 lambda.elem(j+1) = Complex (wr.elem(j+1), wi.elem(j+1)); |
|
130 |
2815
|
131 for (int i = 0; i < n; i++) |
|
132 { |
|
133 double real_part = vr.elem (i, j); |
|
134 double imag_part = vr.elem (i, j+1); |
|
135 v.elem (i, j) = Complex (real_part, imag_part); |
|
136 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
137 } |
|
138 j++; |
1934
|
139 } |
2815
|
140 } |
|
141 } |
|
142 } |
|
143 |
|
144 return info; |
|
145 } |
|
146 |
|
147 int |
|
148 EIG::symmetric_init (const Matrix& a) |
|
149 { |
|
150 int n = a.rows (); |
1934
|
151 |
2815
|
152 if (n != a.cols ()) |
|
153 { |
|
154 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
155 return -1; |
|
156 } |
|
157 |
|
158 int info = 0; |
|
159 |
|
160 Matrix atmp = a; |
|
161 double *tmp_data = atmp.fortran_vec (); |
|
162 |
3585
|
163 ColumnVector wr (n); |
2815
|
164 double *pwr = wr.fortran_vec (); |
|
165 |
|
166 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
167 // lwork that would result in more efficient computations. |
|
168 |
|
169 int lwork = 8*n; |
|
170 Array<double> work (lwork); |
|
171 double *pwork = work.fortran_vec (); |
|
172 |
|
173 F77_XFCN (dsyev, DSYEV, ("V", "U", n, tmp_data, n, pwr, pwork, |
|
174 lwork, info, 1L, 1L)); |
|
175 |
|
176 if (f77_exception_encountered || info < 0) |
|
177 (*current_liboctave_error_handler) ("unrecoverable error in dsyev"); |
3585
|
178 else if (info > 0) |
|
179 (*current_liboctave_error_handler) ("dsyev failed to converge"); |
2815
|
180 else |
|
181 { |
3585
|
182 lambda = ComplexColumnVector (wr); |
|
183 v = ComplexMatrix (atmp); |
462
|
184 } |
|
185 |
|
186 return info; |
|
187 } |
|
188 |
|
189 int |
|
190 EIG::init (const ComplexMatrix& a) |
|
191 { |
2815
|
192 if (a.is_hermitian ()) |
|
193 return hermitian_init (a); |
|
194 |
1934
|
195 int n = a.rows (); |
|
196 |
|
197 if (n != a.cols ()) |
462
|
198 { |
|
199 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
200 return -1; |
|
201 } |
|
202 |
2815
|
203 int info = 0; |
462
|
204 |
1934
|
205 ComplexMatrix atmp = a; |
|
206 Complex *tmp_data = atmp.fortran_vec (); |
462
|
207 |
2815
|
208 ComplexColumnVector w (n); |
|
209 Complex *pw = w.fortran_vec (); |
|
210 |
|
211 ComplexMatrix vtmp (n, n); |
|
212 Complex *pv = vtmp.fortran_vec (); |
|
213 |
|
214 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
215 // lwork that would result in more efficient computations. |
|
216 |
462
|
217 int lwork = 8*n; |
1934
|
218 Array<Complex> work (lwork); |
|
219 Complex *pwork = work.fortran_vec (); |
|
220 |
2815
|
221 int lrwork = 2*n; |
|
222 Array<double> rwork (lrwork); |
1934
|
223 double *prwork = rwork.fortran_vec (); |
462
|
224 |
1365
|
225 Complex *dummy = 0; |
462
|
226 int idummy = 1; |
|
227 |
1934
|
228 F77_XFCN (zgeev, ZGEEV, ("N", "V", n, tmp_data, n, pw, dummy, idummy, |
2815
|
229 pv, n, pwork, lwork, prwork, info, 1L, 1L)); |
|
230 |
|
231 if (f77_exception_encountered || info < 0) |
|
232 (*current_liboctave_error_handler) ("unrecoverable error in zgeev"); |
|
233 else if (info > 0) |
|
234 (*current_liboctave_error_handler) ("zgeev failed to converge"); |
|
235 else |
|
236 { |
|
237 lambda = w; |
|
238 v = vtmp; |
|
239 } |
|
240 |
|
241 return info; |
|
242 } |
|
243 |
|
244 int |
|
245 EIG::hermitian_init (const ComplexMatrix& a) |
|
246 { |
|
247 int n = a.rows (); |
|
248 |
|
249 if (n != a.cols ()) |
|
250 { |
|
251 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
252 return -1; |
|
253 } |
|
254 |
|
255 int info = 0; |
462
|
256 |
2815
|
257 ComplexMatrix atmp = a; |
|
258 Complex *tmp_data = atmp.fortran_vec (); |
|
259 |
3585
|
260 ColumnVector wr (n); |
|
261 double *pwr = wr.fortran_vec (); |
2815
|
262 |
|
263 // XXX FIXME XXX -- it might be possible to choose a better value of |
|
264 // lwork that would result in more efficient computations. |
|
265 |
|
266 int lwork = 8*n; |
|
267 Array<Complex> work (lwork); |
|
268 Complex *pwork = work.fortran_vec (); |
|
269 |
|
270 int lrwork = 3*n; |
|
271 Array<double> rwork (lrwork); |
|
272 double *prwork = rwork.fortran_vec (); |
|
273 |
3585
|
274 F77_XFCN (zheev, ZHEEV, ("V", "U", n, tmp_data, n, pwr, pwork, |
2815
|
275 lwork, prwork, info, 1L, 1L)); |
|
276 |
|
277 if (f77_exception_encountered || info < 0) |
|
278 (*current_liboctave_error_handler) ("unrecoverable error in zheev"); |
|
279 else if (info > 0) |
|
280 (*current_liboctave_error_handler) ("zheev failed to converge"); |
|
281 else |
|
282 { |
3585
|
283 lambda = ComplexColumnVector (wr); |
|
284 v = ComplexMatrix (atmp); |
2815
|
285 } |
462
|
286 |
|
287 return info; |
|
288 } |
|
289 |
|
290 /* |
|
291 ;;; Local Variables: *** |
|
292 ;;; mode: C++ *** |
|
293 ;;; End: *** |
|
294 */ |