Pivoting Methods
Source inspiration: (Mathew 2000-2019).
Gaussian Elimination With Pivoting
For a linear system
\[ A\mathbf{x}=\mathbf{b}, \tag{1} \]
Gaussian elimination transforms the augmented matrix \([A\mid \mathbf{b}]\) into an upper-triangular system that is solved by back substitution.
At elimination step \(p\), the entry used to eliminate all entries below it in column \(p\) is the pivot element. The row containing it is the pivot row.
For a square matrix \(A\), the following are equivalent:
- \(A\mathbf{x}=\mathbf{b}\) has a unique solution for every right-hand side \(\mathbf{b}\).
- \(A\) is nonsingular.
- \(\det(A)\neq 0\).
Pivoting Strategies
At step \(p\), choose a pivot from the unprocessed submatrix.
No pivoting: use \(a_{pp}\) directly.
Trivial pivoting: if \(a_{pp}=0\), swap with the first row below having nonzero entry in column \(p\).
Partial pivoting: \[ u=\arg\max_{i\ge p}|a_{ip}|, \tag{2} \] then swap rows \(p\) and \(u\).
Scaled partial pivoting: compute \[ s_i=\max_{1\le j\le n}|a_{ij}|, \qquad u=\arg\max_{i\ge p}\frac{|a_{ip}|}{s_i}, \tag{3} \] then swap rows \(p\) and \(u\). The scale factors \(s_i\) are computed once, from the original matrix; when rows are swapped, their scale factors are swapped with them.
Total pivoting: \[ (u,v)=\arg\max_{i,j\ge p}|a_{ij}|, \tag{4} \] then swap rows \(p\leftrightarrow u\) and columns \(p\leftrightarrow v\).
Algorithm
Step 1: Form \([A\mid\mathbf{b}]\) and choose a pivoting strategy.
Step 2: For \(p=1,\dots,n-1\), select the pivot with the chosen rule and perform elimination below the pivot.
Step 3: Solve the resulting upper-triangular system by back substitution.
Step 4: If total pivoting was used, undo column permutations so components map back to \((x_1,\dots,x_n)\).
Example 1 - Comparing Pivot Choices on a 4x4 System
Solve
\[ \begin{pmatrix} 1&-4&4&7\\ 0&2&-1&0\\ 2&1&1&4\\ 2&-3&2&-5 \end{pmatrix} \begin{pmatrix}x_1\\x_2\\x_3\\x_4\end{pmatrix} = \begin{pmatrix}4\\5\\2\\9\end{pmatrix}. \]
The first pivot selected by each strategy is:
| Strategy | First pivot choice | First action |
|---|---|---|
| Trivial | \(a_{11}=1\) | No swap |
| Partial | largest \(|a_{i1}|=2\) | swap row 1 with row 3 (or row 4) |
| Scaled partial | largest \(|a_{i1}|/s_i\) with \(s=(7,2,4,5)\) (ratios \(\tfrac17,0,\tfrac12,\tfrac25\)) | swap row 1 with row 3 |
| Total | largest entry in full matrix is \(7\) at \((1,4)\) | swap column 1 with column 4 |
Using partial pivoting, elimination gives the equivalent triangular system
\[ \begin{aligned} 2x_1+x_2+x_3+4x_4&=2,\\ -\tfrac92x_2+\tfrac72x_3+5x_4&=3,\\ -\tfrac{19}{9}x_3-\tfrac{121}{9}x_4&=\tfrac{13}{3},\\ -\tfrac{25}{19}x_4&=\tfrac{142}{19}. \end{aligned} \]
Step 1: Back substitute from the last row.
\[ x_4=-\frac{142}{25}. \]
Step 2: Solve for \(x_3\).
\[ x_3=\frac{853}{25}. \]
Step 3: Solve for \(x_2\).
\[ x_2=\frac{489}{25}. \]
Step 4: Solve for \(x_1\).
\[ x_1=-\frac{362}{25}. \]
So the solution is
\[ \mathbf{x}=\left(-\frac{362}{25},\frac{489}{25},\frac{853}{25},-\frac{142}{25}\right)^\top. \]
In exact arithmetic, all four pivoting strategies produce this same solution; in floating-point arithmetic they can differ due to roundoff.
Verification:
| Row | Left-hand side | Right-hand side |
|---|---|---|
| 1 | \(x_1-4x_2+4x_3+7x_4=4\) | \(4\;\checkmark\) |
| 2 | \(2x_2-x_3=5\) | \(5\;\checkmark\) |
| 3 | \(2x_1+x_2+x_3+4x_4=2\) | \(2\;\checkmark\) |
| 4 | \(2x_1-3x_2+2x_3-5x_4=9\) | \(9\;\checkmark\) |
Example 2 - Hilbert Matrix Sensitivity
The module also works a second \(5\times5\) integer system the same way as Example 1; it is left out here. Its first Hilbert test (of six, \(n=5\) to \(10\), each with \(\mathbf{b}=(n,n-1,\dots,1)^\top\)) uses \(n=5\) with
\[ H_5= \begin{pmatrix} 1&\tfrac12&\tfrac13&\tfrac14&\tfrac15\\ \tfrac12&\tfrac13&\tfrac14&\tfrac15&\tfrac16\\ \tfrac13&\tfrac14&\tfrac15&\tfrac16&\tfrac17\\ \tfrac14&\tfrac15&\tfrac16&\tfrac17&\tfrac18\\ \tfrac15&\tfrac16&\tfrac17&\tfrac18&\tfrac19 \end{pmatrix}, \qquad \mathbf{b}=\begin{pmatrix}5\\4\\3\\2\\1\end{pmatrix}. \]
The exact solution reported in the source is
\[ \mathbf{x}=\begin{pmatrix}-95\\2160\\-10710\\17920\\-9450\end{pmatrix}. \]
The same source run reports these absolute 2-norm errors for the four pivoting choices:
| Strategy | Reported error norm |
|---|---|
| Trivial pivoting | \(\lVert X-\hat X_1\rVert=1.56343\times 10^{-8}\) |
| Partial pivoting | \(\lVert X-\hat X_2\rVert=7.84695\times 10^{-9}\) |
| Scaled partial pivoting | \(\lVert X-\hat X_3\rVert=1.379\times 10^{-8}\) |
| Total pivoting | \(\lVert X-\hat X_4\rVert=1.66215\times 10^{-8}\) |
For this example, partial pivoting gave the smallest error among the four reported runs.
That ranking does not carry over to other cases. For \(n=6,7,8,9,10\) the module names trivial, partial, total, scaled partial and trivial pivoting, in turn, as slightly better than the others. A double-precision rerun of the same four eliminations shows that even the order of the floating-point operations (forming the multiplier first or not, normalizing the pivot row or not) changes which strategy wins, at every \(n\) from 5 to 10 except 9. Which strategy wins is a roundoff accident, while the size of the error follows the conditioning of \(H_n\), which worsens rapidly with \(n\): about \(10^{-9}\) to \(8\times10^{-8}\) at \(n=5\) and \(7.6\times10^{4}\) to \(1.4\times10^{5}\) at \(n=10\), for every strategy and every ordering tried.
Verification:
| Row | Left-hand side | Right-hand side |
|---|---|---|
| 1 | \(-95+1080-3570+4480-1890=5\) | \(5\;\checkmark\) |
| 2 | \(-\tfrac{95}{2}+720-\tfrac{5355}{2}+3584-1575=4\) | \(4\;\checkmark\) |
| 3 | \(-\tfrac{95}{3}+540-2142+\tfrac{8960}{3}-1350=3\) | \(3\;\checkmark\) |
| 4 | \(-\tfrac{95}{4}+432-1785+2560-\tfrac{4725}{4}=2\) | \(2\;\checkmark\) |
| 5 | \(-19+360-1530+2240-1050=1\) | \(1\;\checkmark\) |
Conditioning Warning for Hilbert Systems
For \(A=H_5\) the module reports the condition number
\[ \kappa_\infty(A)=\lVert A\rVert_\infty\,\lVert A^{-1}\rVert_\infty=943656. \]
The 2-norm condition number is smaller, \(\kappa_2(A)\approx 4.77\times10^{5}\).
The module also gives a rule of thumb: if \(A\) and \(\mathbf{b}\) are stored to machine precision, with machine epsilon \(\varepsilon=2.22045\times10^{-16}\), the error this causes in the solution is roughly at most
\[ \lVert X-\hat X\rVert\lesssim \lVert X\rVert\,\kappa(A)\,\varepsilon. \tag{5} \]
This is an estimate of the effect of rounding the data, not a bound on the error of elimination itself, whose roundoff brings further factors that depend on \(n\) and on how much the entries grow during elimination. For this example, with \(\lVert X\rVert_2=23017.6\) and the module’s \(\kappa_\infty(A)\),
\[ \lVert X\rVert\,\kappa(A)\,\varepsilon = (23017.6)(943656)(2.22045\times10^{-16}) \approx 4.823\times10^{-6}. \tag{6} \]
Using 2-norms throughout (\(\kappa_2\)) gives \(2.44\times10^{-6}\), and using \(\infty\)-norms throughout (\(\lVert X\rVert_\infty=17920\)) gives \(3.75\times10^{-6}\). The reported errors, about \(10^{-8}\), are well inside all three.
Hilbert matrices are highly ill-conditioned: here, \(\log_{10}\kappa_\infty(A)\approx 5.975\), so about 6 decimal digits can be lost due to conditioning and roundoff. No pivoting strategy can remove this sensitivity: for \(n=5\) the module’s four errors lie within a factor of about 2 of one another, and in the rerun above the errors of every strategy grow together with \(n\). Higher working precision (or a better-conditioned formulation) is what helps.