462
|
1 // -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
462
|
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. |
462
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
462
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
462
|
30 #endif |
|
31 |
|
32 #include "EIG.h" |
1847
|
33 #include "f77-fcn.h" |
462
|
34 #include "lo-error.h" |
1368
|
35 #include "mx-inlines.cc" |
462
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (dgeev, DGEEV) (const char*, const char*, const int&, |
|
40 double*, const int&, double*, double*, |
|
41 double*, const int&, double*, |
|
42 const int&, double*, const int&, int&, |
|
43 long, long); |
462
|
44 |
1253
|
45 int F77_FCN (zgeev, ZGEEV) (const char*, const char*, const int&, |
|
46 Complex*, const int&, Complex*, |
|
47 Complex*, const int&, Complex*, |
|
48 const int&, Complex*, const int&, |
|
49 double*, int&, long, long); |
462
|
50 } |
|
51 |
|
52 int |
|
53 EIG::init (const Matrix& a) |
|
54 { |
1934
|
55 int n = a.rows (); |
|
56 |
|
57 if (n != a.cols ()) |
462
|
58 { |
|
59 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
60 return -1; |
|
61 } |
|
62 |
|
63 int info; |
|
64 |
1934
|
65 Matrix atmp = a; |
|
66 double *tmp_data = atmp.fortran_vec (); |
|
67 |
|
68 Array<double> wr (n); |
|
69 double *pwr = wr.fortran_vec (); |
|
70 |
|
71 Array<double> wi (n); |
|
72 double *pwi = wi.fortran_vec (); |
|
73 |
462
|
74 Matrix vr (n, n); |
|
75 double *pvr = vr.fortran_vec (); |
1934
|
76 |
462
|
77 int lwork = 8*n; |
1934
|
78 |
|
79 Array<double> work (lwork); |
|
80 double *pwork = work.fortran_vec (); |
462
|
81 |
1365
|
82 double *dummy = 0; |
462
|
83 int idummy = 1; |
|
84 |
1934
|
85 F77_XFCN (dgeev, DGEEV, ("N", "V", n, tmp_data, n, pwr, pwi, dummy, |
|
86 idummy, pvr, n, pwork, lwork, info, 1L, 1L)); |
462
|
87 |
1934
|
88 if (f77_exception_encountered) |
|
89 (*current_liboctave_error_handler) ("unrecoverable error in dgeev"); |
|
90 else |
462
|
91 { |
1934
|
92 lambda.resize (n); |
|
93 v.resize (n, n); |
|
94 |
|
95 for (int j = 0; j < n; j++) |
462
|
96 { |
1934
|
97 if (wi.elem (j) == 0.0) |
462
|
98 { |
1934
|
99 lambda.elem (j) = Complex (wr.elem (j)); |
|
100 for (int i = 0; i < n; i++) |
|
101 v.elem (i, j) = vr.elem (i, j); |
462
|
102 } |
1934
|
103 else |
462
|
104 { |
1934
|
105 if (j+1 >= n) |
|
106 { |
|
107 (*current_liboctave_error_handler) ("EIG: internal error"); |
|
108 return -1; |
|
109 } |
|
110 |
|
111 for (int i = 0; i < n; i++) |
|
112 { |
|
113 lambda.elem (j) = Complex (wr.elem (j), wi.elem (j)); |
|
114 lambda.elem (j+1) = Complex (wr.elem (j+1), wi.elem (j+1)); |
|
115 double real_part = vr.elem (i, j); |
|
116 double imag_part = vr.elem (i, j+1); |
|
117 v.elem (i, j) = Complex (real_part, imag_part); |
|
118 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
119 } |
|
120 j++; |
462
|
121 } |
|
122 } |
|
123 } |
|
124 |
|
125 return info; |
|
126 } |
|
127 |
|
128 int |
|
129 EIG::init (const ComplexMatrix& a) |
|
130 { |
1934
|
131 int n = a.rows (); |
|
132 |
|
133 if (n != a.cols ()) |
462
|
134 { |
|
135 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
136 return -1; |
|
137 } |
|
138 |
|
139 int info; |
|
140 |
|
141 lambda.resize (n); |
|
142 v.resize (n, n); |
1934
|
143 |
462
|
144 Complex *pw = lambda.fortran_vec (); |
|
145 Complex *pvr = v.fortran_vec (); |
|
146 |
1934
|
147 ComplexMatrix atmp = a; |
|
148 Complex *tmp_data = atmp.fortran_vec (); |
462
|
149 |
|
150 int lwork = 8*n; |
1934
|
151 |
|
152 Array<Complex> work (lwork); |
|
153 Complex *pwork = work.fortran_vec (); |
|
154 |
|
155 Array<double> rwork (4*n); |
|
156 double *prwork = rwork.fortran_vec (); |
462
|
157 |
1365
|
158 Complex *dummy = 0; |
462
|
159 int idummy = 1; |
|
160 |
1934
|
161 F77_XFCN (zgeev, ZGEEV, ("N", "V", n, tmp_data, n, pw, dummy, idummy, |
|
162 pvr, n, pwork, lwork, prwork, info, 1L, 1L)); |
462
|
163 |
1934
|
164 if (f77_exception_encountered) |
|
165 (*current_liboctave_error_handler) ("unrecoverable error in zgeev"); |
462
|
166 |
|
167 return info; |
|
168 } |
|
169 |
|
170 /* |
|
171 ;;; Local Variables: *** |
|
172 ;;; mode: C++ *** |
|
173 ;;; page-delimiter: "^/\\*" *** |
|
174 ;;; End: *** |
|
175 */ |