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