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