TExcelTAD
Home › Numerical Methods › Row Pivoting

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 PA=LUPA = LU used by every library.

A small pivot ruins the answer

Take A=(ϵ111)A = \begin{pmatrix} \epsilon & 1 \\ 1 & 1\end{pmatrix} with ϵ=10−20\epsilon = 10^{-20}. The multiplier is 1ϵ=1020\frac{1}{\epsilon} = 10^{20}, and U22=1−1020U_{22} = 1 - 10^{20} rounds to −1020-10^{20}: the information in A22A_{22} is lost.

python · try it yourself
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 (κ≈2.6\kappa \approx 2.6), so this is an unstable algorithm, not a hard problem.

Partial pivoting

At step kk, find the row p≥kp \ge k with the largest ∣apk∣\lvert a_{pk}\rvert and swap rows kk and pp before eliminating. All multipliers then satisfy ∣ℓik∣≤1\lvert\ell_{ik}\rvert \le 1. Recording the swaps in a permutation matrix PP gives

PA=LU.PA = LU.

To solve Ax=bA\mathbf{x} = \mathbf{b}: permute b\mathbf{b}, then do the two triangular solves.

python · try it yourself
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 UU do not grow much compared with those of AA. The growth factor ρ=max⁡∣uij∣max⁡∣aij∣\rho = \frac{\max\lvert u_{ij}\rvert}{\max\lvert a_{ij}\rvert} can be as large as 2n−12^{n-1} for contrived matrices, but in practice it is almost always small, so np.linalg.solve is trusted everywhere.

python · try it yourself
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 O(n3)O(n^3) comparisons and is rarely used. Rook pivoting is a compromise.

In software

  • LAPACK getrf (behind np.linalg.solve, scipy.linalg.lu_factor) uses partial pivoting. scipy.linalg.lu returns A=PLUA = PLU, so its PP is the transpose of the one in PA=LUPA = LU.
  • 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 (21−6340)\begin{pmatrix} 2 & 1 \\ -6 & 3 \\ 4 & 0\end{pmatrix}'s first column, which row becomes the pivot row?

Answer

Row 2 (entry −6-6).

Solution

The largest absolute value in column 1 is ∣−6∣=6\lvert -6\rvert = 6.

Exercise 2: Multipliers

Why does partial pivoting guarantee ∣ℓik∣≤1\lvert\ell_{ik}\rvert \le 1?

Answer

Each multiplier is an entry divided by the largest entry in its column.

Solution

ℓik=uikukk\ell_{ik} = \frac{u_{ik}}{u_{kk}} and ∣ukk∣≥∣uik∣\lvert u_{kk}\rvert \ge \lvert u_{ik}\rvert after the swap.

Exercise 3: Pivoting on the small-pivot example (code)

Apply plu from the lesson to (10−20111)\begin{pmatrix} 10^{-20} & 1 \\ 1 & 1\end{pmatrix} and solve with b=(1,2)\mathbf{b} = (1, 2). Print the solution.

Answer

(1,1)(1, 1) to machine precision.

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

  1. the first nonzero entry
  2. the entry of largest absolute value in the current column
  3. the diagonal entry always
  4. the smallest entry

With partial pivoting, the factorization computed is…

  1. A=LUA = LU
  2. PA=LUPA = LU
  3. A=QRA = QR
  4. A=LLTA = LL^T

Elimination without pivoting fails on (10−20111)\begin{pmatrix}10^{-20} & 1 \\ 1 & 1\end{pmatrix} because…

  1. the matrix is ill-conditioned
  2. the tiny pivot creates a huge multiplier that swamps other entries
  3. the matrix is singular
  4. of overflow