annotate scripts/linear-algebra/expm.m @ 9819:84398271118c

fix typo in expm
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 14 Nov 2009 07:02:30 +0100
parents f0c3d3fc4903
children d1978e7364ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8757
diff changeset
1 ## Copyright (C) 2008, 2009 Jaroslav Hajek, Marco Caliari
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
3 ## This file is part of Octave.
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 ## your option) any later version.
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ## General Public License for more details.
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18
9065
8207b833557f Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents: 9048
diff changeset
19 ## -*- texinfo -*-
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {} expm (@var{a})
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 ## Return the exponential of a matrix, defined as the infinite Taylor
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22 ## series
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 ## $$
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 ## \exp (A) = I + A + {A^2 \over 2!} + {A^3 \over 3!} + \cdots
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26 ## $$
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
28 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 ## @example
9065
8207b833557f Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents: 9048
diff changeset
31 ## expm(a) = I + a + a^2/2! + a^3/3! + @dots{}
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32 ## @end example
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
34 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 ## The Taylor series is @emph{not} the way to compute the matrix
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 ## exponential; see Moler and Van Loan, @cite{Nineteen Dubious Ways to
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 ## Compute the Exponential of a Matrix}, SIAM Review, 1978. This routine
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 ## uses Ward's diagonal
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40 ## Pad\'e
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
42 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
43 ## Pade'
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
44 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 ## approximation method with three step preconditioning (SIAM Journal on
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 ## Numerical Analysis, 1977). Diagonal
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
47 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
48 ## Pad\'e
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
49 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
50 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 ## Pade'
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
52 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 ## approximations are rational polynomials of matrices
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 ## $D_q(a)^{-1}N_q(a)$
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
57 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58 ##
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 ## @example
9065
8207b833557f Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents: 9048
diff changeset
60 ## @group
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61 ## -1
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 ## D (a) N (a)
9065
8207b833557f Cleanup documentation for arith.texi, linalg.texi, nonlin.texi
Rik <rdrider0-list@yahoo.com>
parents: 9048
diff changeset
63 ## @end group
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 ## @end example
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 ##
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
66 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 ## whose Taylor series matches the first
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 ## $2 q + 1 $
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
71 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72 ## @code{2q+1}
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
73 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 ## terms of the Taylor series above; direct evaluation of the Taylor series
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 ## (with the same preconditioning steps) may be desirable in lieu of the
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77 ## Pad\'e
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
79 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80 ## Pade'
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
81 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 ## approximation when
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 ## @tex
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84 ## $D_q(a)$
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85 ## @end tex
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
86 ## @ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
87 ## @code{Dq(a)}
8517
81d6ab3ac93c Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents: 8506
diff changeset
88 ## @end ifnottex
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89 ## is ill-conditioned.
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
90 ## @end deftypefn
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
91
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 function r = expm (a)
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94 if (! ismatrix (a) || ! issquare (a))
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 8517
diff changeset
95 error ("expm requires a square matrix");
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
96 endif
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
97
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
98 n = rows (a);
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
99 ## Trace reduction.
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100 a(a == -Inf) = -realmax;
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 trshift = trace (a) / length (a);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 if (trshift > 0)
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 a -= trshift*eye (n);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
104 endif
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
105 ## Balancing.
9048
867d5d1aed06 swap out args in balance for M*b compat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
106 [d, p, aa] = balance (a);
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
107 ## FIXME: can we both permute and scale at once? Or should we rather do
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
108 ## this:
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
109 ##
9048
867d5d1aed06 swap out args in balance for M*b compat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
110 ## [d, xx, aa] = balance (a, "noperm");
867d5d1aed06 swap out args in balance for M*b compat
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
111 ## [xx, p, aa] = balance (aa, "noscal");
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
112 [f, e] = log2 (norm (aa, "inf"));
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
113 s = max (0, e);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 s = min (s, 1023);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
115 aa *= 2^(-s);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
116
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
117 ## Pade approximation for exp(A).
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
118 c = [5.0000000000000000e-1,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
119 1.1666666666666667e-1,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
120 1.6666666666666667e-2,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
121 1.6025641025641026e-3,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
122 1.0683760683760684e-4,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
123 4.8562548562548563e-6,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 1.3875013875013875e-7,...
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 1.9270852604185938e-9];
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
126
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
127 a2 = aa^2;
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 id = eye (n);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 x = (((c(8) * a2 + c(6) * id) * a2 + c(4) * id) * a2 + c(2) * id) * a2 + id;
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130 y = (((c(7) * a2 + c(5) * id) * a2 + c(3) * id) * a2 + c(1) * id) * aa;
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 r = (x - y) \ (x + y);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
134 ## Undo scaling by repeated squaring.
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
135 for k = 1:s
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
136 r ^= 2;
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
137 endfor
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
138
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
139 ## inverse balancing.
8757
79576d40acb6 fix unscaling in expm
Jaroslav Hajek <highegg@gmail.com>
parents: 8664
diff changeset
140 d = diag (d);
79576d40acb6 fix unscaling in expm
Jaroslav Hajek <highegg@gmail.com>
parents: 8664
diff changeset
141 r = d * r / d;
9819
84398271118c fix typo in expm
Jaroslav Hajek <highegg@gmail.com>
parents: 9211
diff changeset
142 r(p, p) = r;
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 8393
diff changeset
143 ## Inverse trace reduction.
8393
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
144 if (trshift >0)
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
145 r *= exp (trshift);
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
146 endif
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
147
c8a785d0e867 add omitted m-file
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
148 endfunction