The Matrix Exponential
The scalar equation has the solution . The system has the solution
once we define the exponential of a matrix. It handles distinct, complex and repeated eigenvalues with one formula, and it is how software actually solves constant-coefficient linear systems.
Definition
Use the power series of :
The series converges for every square matrix. Differentiating term by term gives , so is the fundamental matrix with (Linear Systems in Matrix Form).
Properties: ; ; . But only when .
Computing it
- Diagonal: .
- Diagonalizable: gives .
- Nilpotent part: if the series stops. For (with and commuting), . For example
which contains the of Repeated Eigenvalues automatically.
import sympy as sp
t = sp.symbols("t")
for M in (sp.Matrix([[2, 1], [0, 2]]), sp.Matrix([[0, 1], [-1, 0]]), sp.Matrix([[1, 1], [4, 1]])):
print(sp.simplify((t * M).exp()).tolist())[[exp(2*t), t*exp(2*t)], [0, exp(2*t)]]
[[cos(t), sin(t)], [-sin(t), cos(t)]]
[[(exp(4*t) + 1)*exp(-t)/2, (exp(4*t) - 1)*exp(-t)/4], [(exp(4*t) - 1)*exp(-t), (exp(4*t) + 1)*exp(-t)/2]]
The second matrix gives the rotation matrix : the exponential of an antisymmetric matrix is a rotation.
Numerically: scipy.linalg.expm
Summing the series naively is a bad algorithm (huge cancellations for large ). SciPy uses scaling and squaring with a Padé approximation: compute for a small matrix, then square times.
import numpy as np
from scipy.linalg import expm
from scipy.integrate import solve_ivp
A = np.array([[-1.0, 2.0, 0.0], [-2.0, -1.0, 1.0], [0.0, 0.0, -0.5]])
x0 = np.array([1.0, 0.0, 2.0])
x_expm = expm(3.0 * A) @ x0
sol = solve_ivp(lambda t, x: A @ x, (0, 3), x0, rtol=1e-12, atol=1e-14)
print("expm: ", np.round(x_expm, 8))
print("solve_ivp:", np.round(sol.y[:, -1], 8))
def expm_series(M, terms):
S, term = np.eye(len(M)), np.eye(len(M))
for k in range(1, terms):
term = term @ M / k
S = S + term
return S
B = -20 * np.eye(2)
print("naive series for e^{-20}:", expm_series(B, 100)[0, 0], " true:", np.exp(-20))expm: [0.2160901 0.04207151 0.44626032]
solve_ivp: [0.2160901 0.04207151 0.44626032]
naive series for e^{-20}: 5.621884472130418e-09 true: 2.061153622438558e-09
The naive series for adds terms as large as with alternating signs, and the rounding errors of those huge terms swamp the tiny answer: the result is wrong by a factor of almost 3.
Time stepping with a fixed step
For a constant matrix, exactly. Precompute once, and each step is a matrix-vector product. This "exponential integrator" is exact for linear systems at any step size.
In software
- Continuous-time Markov chains: transition probabilities after time are (used in phylogenetics software, reliability models and queueing).
- Robotics and graphics: rotations are exponentials of antisymmetric matrices (Lie groups);
scipy.spatial.transform.Rotation.from_rotveccomputes one. - Control systems discretize with
expm(zero-order hold), asscipy.signal.cont2discretedoes.
Exercises
Exercise 1: Diagonal
Compute for .
Answer
.
Solution
Powers of a diagonal matrix are diagonal with powered entries, so the series acts entry by entry.
Exercise 2: Nilpotent
Compute for .
Answer
.
Solution
has a single 1 in the top-right corner and , so . This system is , , : position under constant acceleration.
Exercise 3: Exponential stepping (code)
For and , take 100 steps of with . Where do you end up?
Answer
Back at (up to rounding): the period is .
Solution
import numpy as np
from scipy.linalg import expm
A = np.array([[0.0, 1.0], [-4.0, 0.0]])
E = expm(np.pi / 100 * A)
x = np.array([1.0, 0.0])
for _ in range(100):
x = E @ x
print(np.round(x, 10) + 0)[1. 0.]
Test yourself
The solution of , is…
holds…
- always
- when
- never
- only for matrices
Why does scipy.linalg.expm not just sum the power series?
- The series diverges
- Summing it can lose all precision through cancellation for large
- It is too exact
- Python cannot multiply matrices