File: expm.m

package info (click to toggle)
octave 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,396 kB
  • ctags: 45,547
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (156 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
## Copyright (C) 2008-2013 Jaroslav Hajek, Marco Caliari
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} expm (@var{A})
## Return the exponential of a matrix, defined as the infinite Taylor
## series
## @tex
## $$
##  \exp (A) = I + A + {A^2 \over 2!} + {A^3 \over 3!} + \cdots
## $$
## @end tex
## @ifnottex
##
## @example
## expm (A) = I + A + A^2/2! + A^3/3! + @dots{}
## @end example
##
## @end ifnottex
## The Taylor series is @emph{not} the way to compute the matrix
## exponential; see Moler and Van Loan, @cite{Nineteen Dubious Ways to
## Compute the Exponential of a Matrix}, SIAM Review, 1978.  This routine
## uses Ward's diagonal Pad@'e approximation method with three step
## preconditioning (SIAM Journal on Numerical Analysis, 1977).  Diagonal
## Pad@'e approximations are rational polynomials of matrices
## @tex
## $D_q(A)^{-1}N_q(A)$
## @end tex
## @ifnottex
##
## @example
## @group
##      -1
## D (A)   N (A)
## @end group
## @end example
##
## @end ifnottex
## whose Taylor series matches the first
## @tex
## $2 q + 1 $
## @end tex
## @ifnottex
## @code{2q+1}
## @end ifnottex
## terms of the Taylor series above; direct evaluation of the Taylor series
## (with the same preconditioning steps) may be desirable in lieu of the
## Pad@'e approximation when
## @tex
## $D_q(A)$
## @end tex
## @ifnottex
## @code{Dq(A)}
## @end ifnottex
## is ill-conditioned.
## @seealso{logm, sqrtm}
## @end deftypefn

function r = expm (A)

  if (nargin != 1)
    print_usage ();
  endif

  if (! ismatrix (A) || ! issquare (A))
    error ("expm: A must be a square matrix");
  endif

  if (isscalar (A))
    r = exp (A);
    return;
  elseif (strfind (typeinfo (A), "diagonal matrix"))
    r = diag (exp (diag (A)));
    return;
  endif

  n = rows (A);
  ## Trace reduction.
  A(A == -Inf) = -realmax;
  trshift = trace (A) / length (A);
  if (trshift > 0)
    A -= trshift*eye (n);
  endif
  ## Balancing.
  [d, p, aa] = balance (A);
  ## FIXME: can we both permute and scale at once? Or should we rather do
  ## this:
  ##
  ##   [d, xx, aa] = balance (A, "noperm");
  ##   [xx, p, aa] = balance (aa, "noscal");
  [f, e] = log2 (norm (aa, "inf"));
  s = max (0, e);
  s = min (s, 1023);
  aa *= 2^(-s);

  ## Pade approximation for exp(A).
  c = [5.0000000000000000e-1,...
       1.1666666666666667e-1,...
       1.6666666666666667e-2,...
       1.6025641025641026e-3,...
       1.0683760683760684e-4,...
       4.8562548562548563e-6,...
       1.3875013875013875e-7,...
       1.9270852604185938e-9];

  a2 = aa^2;
  id = eye (n);
  x = (((c(8) * a2 + c(6) * id) * a2 + c(4) * id) * a2 + c(2) * id) * a2 + id;
  y = (((c(7) * a2 + c(5) * id) * a2 + c(3) * id) * a2 + c(1) * id) * aa;

  r = (x - y) \ (x + y);

  ## Undo scaling by repeated squaring.
  for k = 1:s
    r ^= 2;
  endfor

  ## inverse balancing.
  d = diag (d);
  r = d * r / d;
  r(p, p) = r;
  ## Inverse trace reduction.
  if (trshift >0)
    r *= exp (trshift);
  endif

endfunction


%!assert (norm (expm ([1 -1;0 1]) - [e -e; 0 e]) < 1e-5);
%!assert (expm ([1 -1 -1;0 1 -1; 0 0 1]), [e -e -e/2; 0 e -e; 0 0 e], 1e-5);

%!assert (expm (10), expm (10))
%!assert (full (expm (eye (3))), expm (full (eye (3))))
%!assert (full (expm (10*eye (3))), expm (full (10*eye (3))), 8*eps)

%% Test input validation
%!error expm ()
%!error expm (1, 2)
%!error <expm: A must be a square matrix> expm ([1 0;0 1; 2 2])