Triangular Systems
Solving is the most common task in scientific computing. The standard strategy reduces a general system to triangular ones, which are solved by simple substitution. This lesson writes forward and backward substitution and counts their cost.
Lower triangular: forward substitution
If is lower triangular ( for ), the first equation contains only , the second only , and so on:
Upper triangular: backward substitution
For upper triangular start from the last row:
Both need nonzero diagonal entries; then the triangular matrix is nonsingular (its determinant is the product of the diagonal).
import numpy as np
from scipy.linalg import solve_triangular
def forward_sub(L, b):
n = len(b)
x = np.zeros(n)
for i in range(n):
x[i] = (b[i] - L[i, :i] @ x[:i]) / L[i, i]
return x
def backward_sub(U, b):
n = len(b)
x = np.zeros(n)
for i in range(n - 1, -1, -1):
x[i] = (b[i] - U[i, i + 1:] @ x[i + 1:]) / U[i, i]
return x
L = np.array([[2.0, 0, 0], [1, 3, 0], [-1, 2, 4]])
b = np.array([4.0, 11, 15])
print("forward :", forward_sub(L, b))
U = L.T
print("backward:", backward_sub(U, b))
print("scipy :", solve_triangular(U, b, lower=False))forward : [2. 3. 2.75]
backward: [3.29166667 1.16666667 3.75 ]
scipy : [3.29166667 1.16666667 3.75 ]
Cost
Row needs about multiplications and additions, so the total is
A flop is one floating-point add, subtract, multiply or divide. Doubling multiplies the work by 4. By comparison, factorizing a general matrix costs about flops (Operation Counts and Efficiency), so once a matrix is factorized into triangular pieces, each additional right-hand side is cheap.
Accuracy
Triangular substitution is backward stable: the computed solves with . But triangular matrices can be very ill-conditioned even with harmless-looking entries:
import numpy as np
for n in (10, 20, 40):
U = np.eye(n) + np.triu(-np.ones((n, n)), 1) # 1 on diagonal, -1 above
print(f"n = {n}: cond(U) = {np.linalg.cond(U):.2e}")n = 10: cond(U) = 1.92e+03
n = 20: cond(U) = 4.15e+06
n = 40: cond(U) = 9.00e+12
The inverse of this matrix has entries , which grow exponentially.
In software
scipy.linalg.solve_triangular(A, b, lower=True)calls LAPACK'strtrs;np.linalg.solvedoes not detect triangular structure, so call the specialized routine when you know it.- The Python loops above are fine for teaching. Compiled code is hundreds of times faster at the same flop count.
Exercises
Exercise 1: By hand
Solve .
Answer
.
Solution
; then , .
Exercise 2: Flop count
How many flops does backward substitution take for ?
Answer
About .
Solution
exactly.
Exercise 3: Column-oriented substitution (code)
Forward substitution can also run by columns: once is known, subtract from the remaining right-hand side. Implement it and check it against solve_triangular on a random lower triangular matrix with diagonal .
Answer
Both agree to rounding error.
Solution
import numpy as np
from scipy.linalg import solve_triangular
rng = np.random.default_rng(5)
n = 50
L = np.tril(rng.normal(size=(n, n))) + 50 * np.eye(n)
b = rng.normal(size=n)
r = b.copy(); x = np.zeros(n)
for j in range(n):
x[j] = r[j] / L[j, j]
r[j + 1:] -= x[j] * L[j + 1:, j]
print(f"{np.max(np.abs(x - solve_triangular(L, b, lower=True))):.1e}")2.1e-17
The column version accesses memory column by column, which suits Fortran (column-major) storage.
Test yourself
Backward substitution solves for which unknown first?
- the largest
- all at once
The cost of a triangular solve is about…
- flops
- flops
- flops
- flops
A triangular matrix is singular exactly when…
- it has a zero diagonal entry
- it is upper triangular
- its first row is zero
- never