Row Pivoting
Gaussian elimination without row exchanges fails on a zero pivot and, worse, silently loses accuracy on a small one. Partial pivoting swaps rows so that the pivot is the largest entry (in absolute value) of its column. It makes LU reliable in practice and gives the factorization used by every library.
A small pivot ruins the answer
Take with . The multiplier is , and rounds to : the information in is lost.
import numpy as np
eps = 1e-20
A = np.array([[eps, 1.0], [1.0, 1.0]])
b = np.array([1.0, 2.0]) # exact solution is close to (1, 1)
# elimination without pivoting
m = A[1, 0] / A[0, 0]
u22 = A[1, 1] - m * A[0, 1]
z2 = b[1] - m * b[0]
x2 = z2 / u22
x1 = (b[0] - A[0, 1] * x2) / A[0, 0]
print("no pivoting :", np.array([x1, x2]))
print("with pivoting:", np.linalg.solve(A, b))no pivoting : [0. 1.]
with pivoting: [1. 1.]
The matrix is perfectly well conditioned (), so this is an unstable algorithm, not a hard problem.
Partial pivoting
At step , find the row with the largest and swap rows and before eliminating. All multipliers then satisfy . Recording the swaps in a permutation matrix gives
To solve : permute , then do the two triangular solves.
import numpy as np
import scipy.linalg as sla
def plu(A):
n = A.shape[0]
U = A.astype(float).copy()
L = np.eye(n)
perm = np.arange(n)
for k in range(n - 1):
p = k + np.argmax(np.abs(U[k:, k]))
if p != k: # swap rows k and p
U[[k, p], :] = U[[p, k], :]
L[[k, p], :k] = L[[p, k], :k]
perm[[k, p]] = perm[[p, k]]
L[k + 1:, k] = U[k + 1:, k] / U[k, k]
U[k + 1:, k:] -= np.outer(L[k + 1:, k], U[k, k:])
return perm, L, np.triu(U)
A = np.array([[1.0, 2, 3], [4, 5, 6], [7, 8, 10]])
perm, L, U = plu(A)
print("row order:", perm)
print("max |multiplier|:", np.max(np.abs(L)))
print("PA == LU:", np.allclose(A[perm], L @ U))
P, L2, U2 = sla.lu(A) # SciPy returns A = P L U
print("same U as SciPy:", np.allclose(U, U2))row order: [2 0 1]
max |multiplier|: 1.0
PA == LU: True
same U as SciPy: True
Stability and the growth factor
With partial pivoting, LU is backward stable if the entries of do not grow much compared with those of . The growth factor can be as large as for contrived matrices, but in practice it is almost always small, so np.linalg.solve is trusted everywhere.
import numpy as np
import scipy.linalg as sla
n = 30
W = np.eye(n) - np.tril(np.ones((n, n)), -1) # Wilkinson's worst case
W[:, -1] = 1
P, L, U = sla.lu(W)
print(f"growth factor worst case: {np.max(np.abs(U)):.3e} = 2^{n - 1}")
rng = np.random.default_rng(4)
R = rng.normal(size=(n, n))
P, L, U = sla.lu(R)
print(f"growth factor random matrix: {np.max(np.abs(U)) / np.max(np.abs(R)):.2f}")growth factor worst case: 5.369e+08 = 2^29
growth factor random matrix: 3.16
Complete pivoting (searching the whole remaining submatrix) bounds growth much better but costs comparisons and is rarely used. Rook pivoting is a compromise.
In software
- LAPACK
getrf(behindnp.linalg.solve,scipy.linalg.lu_factor) uses partial pivoting.scipy.linalg.lureturns , so its is the transpose of the one in . - Symmetric positive definite matrices need no pivoting at all; use Cholesky (Banded and Positive Definite Matrices).
Exercises
Exercise 1: Which row?
At the first step of partial pivoting for 's first column, which row becomes the pivot row?
Answer
Row 2 (entry ).
Solution
The largest absolute value in column 1 is .
Exercise 2: Multipliers
Why does partial pivoting guarantee ?
Answer
Each multiplier is an entry divided by the largest entry in its column.
Solution
and after the swap.
Exercise 3: Pivoting on the small-pivot example (code)
Apply plu from the lesson to and solve with . Print the solution.
Answer
to machine precision.
Solution
import numpy as np
from scipy.linalg import solve_triangular
A = np.array([[1e-20, 1.0], [1.0, 1.0]]); b = np.array([1.0, 2.0])
perm = np.array([1, 0]) # pivoting swaps the rows
L = np.array([[1.0, 0], [1e-20, 1]]); U = np.array([[1.0, 1], [0, 1 - 1e-20]])
print(np.allclose(A[perm], L @ U))
z = solve_triangular(L, b[perm], lower=True)
print(solve_triangular(U, z))True
[1. 1.]
Test yourself
Partial pivoting chooses as pivot…
- the first nonzero entry
- the entry of largest absolute value in the current column
- the diagonal entry always
- the smallest entry
With partial pivoting, the factorization computed is…
Elimination without pivoting fails on because…
- the matrix is ill-conditioned
- the tiny pivot creates a huge multiplier that swamps other entries
- the matrix is singular
- of overflow