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