TExcelTAD
Home › Numerical Methods › Triangular Systems

Triangular Systems

Solving Ax=bA\mathbf{x} = \mathbf{b} 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 LL is lower triangular (Lij=0L_{ij} = 0 for j>ij > i), the first equation contains only x1x_1, the second only x1,x2x_1, x_2, and so on:

xi=1Lii(bi−∑j=1i−1Lijxj),i=1,2,…,n.x_i = \frac{1}{L_{ii}}\left(b_i - \sum_{j=1}^{i-1}L_{ij}x_j\right), \qquad i = 1, 2, \ldots, n.

Upper triangular: backward substitution

For upper triangular UU start from the last row:

xi=1Uii(bi−∑j=i+1nUijxj),i=n,n−1,…,1.x_i = \frac{1}{U_{ii}}\left(b_i - \sum_{j=i+1}^{n}U_{ij}x_j\right), \qquad i = n, n-1, \ldots, 1.

Both need nonzero diagonal entries; then the triangular matrix is nonsingular (its determinant is the product of the diagonal).

python · try it yourself
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 ii needs about ii multiplications and ii additions, so the total is

∑i=1n2i≈n2 flops.\sum_{i=1}^{n} 2i \approx n^2 \text{ flops}.

A flop is one floating-point add, subtract, multiply or divide. Doubling nn multiplies the work by 4. By comparison, factorizing a general matrix costs about 23n3\frac{2}{3}n^3 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 x~\tilde{\mathbf{x}} solves (L+δL)x~=b(L + \delta L)\tilde{\mathbf{x}} = \mathbf{b} with ∣δLij∣≤nu∣Lij∣\lvert\delta L_{ij}\rvert \le nu\lvert L_{ij}\rvert. But triangular matrices can be very ill-conditioned even with harmless-looking entries:

python · try it yourself
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 2j−i−12^{j-i-1}, which grow exponentially.

In software

  • scipy.linalg.solve_triangular(A, b, lower=True) calls LAPACK's trtrs; np.linalg.solve does 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 n2n^2 flop count.

Exercises

Exercise 1: By hand

Solve (2104)x=(58)\begin{pmatrix} 2 & 1 \\ 0 & 4\end{pmatrix}\mathbf{x} = \begin{pmatrix} 5 \\ 8\end{pmatrix}.

Answer

x=(1.5,2)\mathbf{x} = (1.5, 2).

Solution

x2=8/4=2x_2 = 8/4 = 2; then 2x1+2=52x_1 + 2 = 5, x1=1.5x_1 = 1.5.

Exercise 2: Flop count

How many flops does backward substitution take for n=1000n = 1000?

Answer

About 10610^6.

Solution

∑i=1n(2(n−i)+1)=n2\sum_{i=1}^{n}(2(n - i) + 1) = n^2 exactly.

Exercise 3: Column-oriented substitution (code)

Forward substitution can also run by columns: once xjx_j is known, subtract xjL:,jx_j L_{:,j} from the remaining right-hand side. Implement it and check it against solve_triangular on a random 50×5050 \times 50 lower triangular matrix with diagonal 5050.

Answer

Both agree to rounding error.

Solution
python · try it yourself
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?

  1. x1x_1
  2. xnx_n
  3. the largest
  4. all at once

The cost of a triangular solve is about…

  1. nn flops
  2. n2n^2 flops
  3. 23n3\frac{2}{3}n^3 flops
  4. nlog⁡nn\log n flops

A triangular matrix is singular exactly when…

  1. it has a zero diagonal entry
  2. it is upper triangular
  3. its first row is zero
  4. never