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 { |
|
55 int a_nr = a.rows (); |
|
56 if (a_nr != a.cols ()) |
|
57 { |
|
58 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
59 return -1; |
|
60 } |
|
61 |
|
62 int n = a_nr; |
|
63 |
|
64 int info; |
|
65 |
|
66 double *tmp_data = dup (a.data (), a.length ()); |
|
67 double *wr = new double[n]; |
|
68 double *wi = new double[n]; |
|
69 Matrix vr (n, n); |
|
70 double *pvr = vr.fortran_vec (); |
|
71 int lwork = 8*n; |
|
72 double *work = new double[lwork]; |
|
73 |
1365
|
74 double *dummy = 0; |
462
|
75 int idummy = 1; |
|
76 |
1253
|
77 F77_FCN (dgeev, DGEEV) ("N", "V", n, tmp_data, n, wr, wi, dummy, |
|
78 idummy, pvr, n, work, lwork, info, 1L, 1L); |
462
|
79 |
|
80 lambda.resize (n); |
|
81 v.resize (n, n); |
|
82 |
|
83 for (int j = 0; j < n; j++) |
|
84 { |
|
85 if (wi[j] == 0.0) |
|
86 { |
|
87 lambda.elem (j) = Complex (wr[j]); |
|
88 for (int i = 0; i < n; i++) |
|
89 v.elem (i, j) = vr.elem (i, j); |
|
90 } |
|
91 else |
|
92 { |
|
93 if (j+1 >= n) |
|
94 { |
|
95 (*current_liboctave_error_handler) ("EIG: internal error"); |
|
96 return -1; |
|
97 } |
|
98 |
|
99 for (int i = 0; i < n; i++) |
|
100 { |
|
101 lambda.elem (j) = Complex (wr[j], wi[j]); |
|
102 lambda.elem (j+1) = Complex (wr[j+1], wi[j+1]); |
|
103 double real_part = vr.elem (i, j); |
|
104 double imag_part = vr.elem (i, j+1); |
|
105 v.elem (i, j) = Complex (real_part, imag_part); |
|
106 v.elem (i, j+1) = Complex (real_part, -imag_part); |
|
107 } |
|
108 j++; |
|
109 } |
|
110 } |
|
111 |
|
112 delete [] tmp_data; |
|
113 delete [] wr; |
|
114 delete [] wi; |
|
115 delete [] work; |
|
116 |
|
117 return info; |
|
118 } |
|
119 |
|
120 int |
|
121 EIG::init (const ComplexMatrix& a) |
|
122 { |
|
123 int a_nr = a.rows (); |
|
124 if (a_nr != a.cols ()) |
|
125 { |
|
126 (*current_liboctave_error_handler) ("EIG requires square matrix"); |
|
127 return -1; |
|
128 } |
|
129 |
|
130 int n = a_nr; |
|
131 |
|
132 int info; |
|
133 |
|
134 lambda.resize (n); |
|
135 v.resize (n, n); |
|
136 |
|
137 Complex *pw = lambda.fortran_vec (); |
|
138 Complex *pvr = v.fortran_vec (); |
|
139 |
|
140 Complex *tmp_data = dup (a.data (), a.length ()); |
|
141 |
|
142 int lwork = 8*n; |
|
143 Complex *work = new Complex[lwork]; |
|
144 double *rwork = new double[4*n]; |
|
145 |
1365
|
146 Complex *dummy = 0; |
462
|
147 int idummy = 1; |
|
148 |
1253
|
149 F77_FCN (zgeev, ZGEEV) ("N", "V", n, tmp_data, n, pw, dummy, idummy, |
|
150 pvr, n, work, lwork, rwork, info, 1L, 1L); |
462
|
151 |
|
152 delete [] tmp_data; |
|
153 delete [] work; |
|
154 delete [] rwork; |
|
155 |
|
156 return info; |
|
157 } |
|
158 |
|
159 /* |
|
160 ;;; Local Variables: *** |
|
161 ;;; mode: C++ *** |
|
162 ;;; page-delimiter: "^/\\*" *** |
|
163 ;;; End: *** |
|
164 */ |