50
|
1 // tc-expm.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Written by A. S. Hodel <scotte@eng.auburn.edu> |
|
25 |
|
26 #ifdef __GNUG__ |
|
27 #pragma implementation |
|
28 #endif |
|
29 |
|
30 #include <math.h> |
|
31 |
|
32 #include "Matrix.h" |
|
33 |
|
34 #include "tree-const.h" |
|
35 #include "user-prefs.h" |
|
36 #include "gripes.h" |
|
37 #include "error.h" |
|
38 #include "f-expm.h" |
|
39 |
|
40 #ifdef WITH_DLD |
|
41 tree_constant * |
|
42 builtin_matrix_exp_2 (tree_constant *args, int nargin, int nargout) |
|
43 { |
|
44 tree_constant *retval = new tree_constant [2]; |
|
45 retval[0] = matrix_exp (args[1]); |
|
46 return retval; |
|
47 } |
|
48 #endif |
|
49 |
|
50 extern "C" |
|
51 { |
|
52 double F77_FCN (dlange) (const char*, const int*, const int*, |
|
53 const double*, const int*, double*); |
|
54 |
|
55 double F77_FCN (zlange) (const char*, const int*, const int*, |
|
56 const Complex*, const int*, double*); |
|
57 } |
|
58 |
|
59 tree_constant |
52
|
60 matrix_exp (tree_constant& a) |
50
|
61 { |
|
62 tree_constant retval; |
|
63 tree_constant tmp = a.make_numeric (); |
|
64 |
|
65 // Constants for matrix exponential calculation. |
|
66 |
|
67 static double padec[] = |
|
68 { |
|
69 5.0000000000000000e-1, |
|
70 1.1666666666666667e-1, |
|
71 1.6666666666666667e-2, |
|
72 1.6025641025641026e-3, |
|
73 1.0683760683760684e-4, |
|
74 4.8562548562548563e-6, |
|
75 1.3875013875013875e-7, |
|
76 1.9270852604185938e-9, |
|
77 }; |
|
78 |
90
|
79 if (tmp.is_empty ()) |
50
|
80 { |
|
81 int flag = user_pref.propagate_empty_matrices; |
|
82 if (flag != 0) |
|
83 { |
|
84 if (flag < 0) |
|
85 gripe_empty_arg ("expm", 0); |
|
86 Matrix m; |
|
87 retval = tree_constant (m); |
|
88 } |
|
89 else gripe_empty_arg ("expm", 1); |
|
90 } |
|
91 else if (tmp.rows () != tmp.columns ()) |
|
92 gripe_square_matrix_required ("expm"); |
|
93 else |
|
94 { |
|
95 int i, j; |
|
96 int n_cols = tmp.columns (); |
|
97 |
|
98 char* balance_job = "B"; // variables for balancing |
|
99 |
|
100 int sqpow; // power for scaling and squaring |
|
101 double inf_norm; // norm of preconditioned matrix |
|
102 int minus_one_j; // used in computing pade approx |
|
103 |
|
104 switch (tmp.const_type ()) |
|
105 { |
|
106 case tree_constant_rep::complex_matrix_constant: |
|
107 { |
|
108 ComplexMatrix m = tmp.complex_matrix_value (); |
|
109 Complex trshift = 0.0; // trace shift value |
|
110 |
|
111 // Preconditioning step 1: trace normalization. |
|
112 |
|
113 for (i = 0; i < n_cols; i++) |
|
114 trshift += m.elem (i, i); |
|
115 trshift /= n_cols; |
|
116 for (i = 0; i < n_cols; i++) |
|
117 m.elem (i, i) -= trshift; |
|
118 |
|
119 // Preconditioning step 2: eigenvalue balancing. |
|
120 |
|
121 ComplexAEPBALANCE mbal (m, balance_job); |
|
122 m = mbal.balanced_matrix (); |
|
123 ComplexMatrix d = mbal.balancing_matrix (); |
|
124 |
|
125 // Preconditioning step 3: scaling. |
|
126 |
|
127 ColumnVector work (n_cols); |
|
128 inf_norm = F77_FCN (zlange) ("I", &n_cols, &n_cols, m. |
|
129 fortran_vec (), &n_cols, |
|
130 work.fortran_vec ()); |
|
131 |
52
|
132 sqpow = (int) (1.0 + log (inf_norm) / log (2.0)); |
50
|
133 |
|
134 // Check whether we need to square at all. |
|
135 |
52
|
136 if (sqpow < 0) |
|
137 sqpow = 0; |
50
|
138 else |
|
139 { |
|
140 for (inf_norm = 1.0, i = 0; i < sqpow; i++) |
|
141 inf_norm *= 2.0; |
|
142 |
|
143 m = m / inf_norm; |
|
144 } |
|
145 |
|
146 // npp, dpp: pade' approx polynomial matrices. |
|
147 |
|
148 ComplexMatrix npp (n_cols, n_cols, 0.0); |
|
149 ComplexMatrix dpp = npp; |
|
150 |
|
151 // Now powers a^8 ... a^1. |
|
152 |
|
153 minus_one_j = -1; |
|
154 for (j = 7; j >= 0; j--) |
|
155 { |
|
156 npp = m * npp + m * padec[j]; |
|
157 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
158 minus_one_j *= -1; |
|
159 } |
|
160 |
|
161 // Zero power. |
|
162 |
|
163 dpp = -dpp; |
|
164 for (j = 0; j < n_cols; j++) |
|
165 { |
|
166 npp.elem (j, j) += 1.0; |
|
167 dpp.elem (j, j) += 1.0; |
|
168 } |
|
169 |
|
170 // Compute pade approximation = inverse (dpp) * npp. |
|
171 |
|
172 ComplexMatrix result = dpp.solve (npp); |
|
173 |
|
174 // Reverse preconditioning step 3: repeated squaring. |
|
175 |
|
176 while (sqpow) |
|
177 { |
|
178 result = result * result; |
|
179 sqpow--; |
|
180 } |
|
181 |
|
182 // reverse preconditioning step 2: inverse balancing XXX FIXME XXX: |
|
183 // should probably do this with lapack calls instead of a complete |
|
184 // matrix inversion. |
|
185 |
|
186 result = result.transpose (); |
|
187 d = d.transpose (); |
|
188 result = result * d; |
|
189 result = d.solve (result); |
|
190 result = result.transpose (); |
|
191 |
|
192 // Reverse preconditioning step 1: fix trace normalization. |
|
193 |
|
194 result = result * exp (trshift); |
|
195 |
|
196 retval = tree_constant (result); |
|
197 } |
|
198 break; |
|
199 case tree_constant_rep::complex_scalar_constant: |
|
200 { |
|
201 Complex c = tmp.complex_value (); |
|
202 retval = tree_constant (exp (c)); |
|
203 } |
|
204 break; |
|
205 case tree_constant_rep::matrix_constant: |
|
206 { |
|
207 |
|
208 // Compute the exponential. |
|
209 |
|
210 Matrix m = tmp.matrix_value (); |
|
211 |
|
212 double trshift = 0; // trace shift value |
|
213 |
|
214 // Preconditioning step 1: trace normalization. |
|
215 |
|
216 for (i = 0; i < n_cols; i++) |
|
217 trshift += m.elem (i, i); |
|
218 trshift /= n_cols; |
|
219 for (i = 0; i < n_cols; i++) |
|
220 m.elem (i, i) -= trshift; |
|
221 |
|
222 // Preconditioning step 2: balancing. |
|
223 |
|
224 AEPBALANCE mbal (m, balance_job); |
|
225 m = mbal.balanced_matrix (); |
|
226 Matrix d = mbal.balancing_matrix (); |
|
227 |
|
228 // Preconditioning step 3: scaling. |
|
229 |
|
230 ColumnVector work(n_cols); |
|
231 inf_norm = F77_FCN (dlange) ("I", &n_cols, &n_cols, |
|
232 m.fortran_vec (), &n_cols, |
|
233 work.fortran_vec ()); |
|
234 |
52
|
235 sqpow = (int) (1.0 + log (inf_norm) / log (2.0)); |
50
|
236 |
|
237 // Check whether we need to square at all. |
|
238 |
52
|
239 if (sqpow < 0) |
|
240 sqpow = 0; |
50
|
241 else |
|
242 { |
|
243 for (inf_norm = 1.0, i = 0; i < sqpow; i++) |
|
244 inf_norm *= 2.0; |
|
245 |
|
246 m = m / inf_norm; |
|
247 } |
|
248 |
|
249 // npp, dpp: pade' approx polynomial matrices. |
|
250 |
|
251 Matrix npp (n_cols, n_cols, 0.0); |
|
252 Matrix dpp = npp; |
|
253 |
|
254 // now powers a^8 ... a^1. |
|
255 |
|
256 minus_one_j = -1; |
|
257 for (j = 7; j >= 0; j--) |
|
258 { |
|
259 npp = m * npp + m * padec[j]; |
|
260 dpp = m * dpp + m * (minus_one_j * padec[j]); |
|
261 minus_one_j *= -1; |
|
262 } |
|
263 // Zero power. |
|
264 |
|
265 dpp = -dpp; |
|
266 for(j = 0; j < n_cols; j++) |
|
267 { |
|
268 npp.elem (j, j) += 1.0; |
|
269 dpp.elem (j, j) += 1.0; |
|
270 } |
|
271 |
|
272 // Compute pade approximation = inverse (dpp) * npp. |
|
273 |
|
274 Matrix result = dpp.solve (npp); |
|
275 |
|
276 // Reverse preconditioning step 3: repeated squaring. |
|
277 |
|
278 while(sqpow) |
|
279 { |
|
280 result = result * result; |
|
281 sqpow--; |
|
282 } |
|
283 |
|
284 // Reverse preconditioning step 2: inverse balancing. |
|
285 |
|
286 result = result.transpose(); |
|
287 d = d.transpose (); |
|
288 result = result * d; |
|
289 result = d.solve (result); |
|
290 result = result.transpose (); |
|
291 |
|
292 // Reverse preconditioning step 1: fix trace normalization. |
|
293 |
|
294 result = result * exp (trshift); |
|
295 |
|
296 retval = tree_constant (result); |
|
297 } |
|
298 break; |
|
299 case tree_constant_rep::scalar_constant: |
|
300 { |
|
301 double d = tmp.double_value (); |
|
302 retval = tree_constant (exp (d)); |
|
303 } |
|
304 break; |
|
305 default: |
|
306 panic_impossible(); |
|
307 break; |
|
308 } |
|
309 } |
|
310 return retval; |
|
311 } |
|
312 |
|
313 /* |
|
314 ;;; Local Variables: *** |
|
315 ;;; mode: C++ *** |
|
316 ;;; page-delimiter: "^/\\*" *** |
|
317 ;;; End: *** |
|
318 */ |