Inverse Matrix
Source inspiration: (Mathew 2000-2019).
Background
Given an \(n\times n\) nonsingular matrix \(A\), its inverse \(A^{-1}\) is the unique matrix satisfying \(A A^{-1} = A^{-1} A = I_n\). Once \(A^{-1}\) is known, each new system \(A\mathbf{x} = \mathbf{b}\) is answered by \(\mathbf{x} = A^{-1}\mathbf{b}\) at \(O(n^2)\) cost. A stored LU factorization of \(A\) gives the same \(O(n^2)\) cost per right-hand side (two triangular solves), takes about a third of the work to compute (roughly \(\tfrac{2}{3}n^3\) operations against \(2n^3\) for \(A^{-1}\)), and in floating point usually gives the more accurate solution. So even with many right-hand sides, LU is the better way to solve systems; the inverse is mainly useful in theoretical analysis, or when its entries themselves are wanted (as with the Hilbert matrix below).
Theorem — Computing the Inverse by Gauss–Jordan
Assume \(A\) is an \(n\times n\) nonsingular matrix. Form the \(n\times 2n\) augmented matrix \([A\,|\,I_n]\). Apply Gauss–Jordan elimination (reducing every column, not just forward elimination) until the identity \(I_n\) occupies the first \(n\) columns. Then \(A^{-1}\) occupies columns \(n+1\) through \(2n\):
\[ [A \mid I_n] \xrightarrow{\text{Gauss–Jordan}} [I_n \mid A^{-1}]. \]
The algorithm uses the same row operations as Gauss–Jordan elimination for solving \(A\mathbf{x} = \mathbf{b}\) but applies them simultaneously to \(n\) right-hand sides (the columns of \(I_n\)).
Algorithm — Complete Gauss–Jordan Elimination
For \(j = 1, 2, \ldots, n\) (pivot column):
- Find the entry of largest absolute value in column \(j\) at or below row \(j\); swap that row with row \(j\) (partial pivoting for numerical stability).
- Divide row \(j\) by the pivot \(a_{jj}\) so that \(a_{jj} = 1\).
- For every other row \(i \neq j\): subtract \(a_{ij}\) times row \(j\) from row \(i\), zeroing the entire column \(j\) above and below the pivot.
After all \(n\) steps the left half of the augmented matrix is \(I_n\) and the right half is \(A^{-1}\).
Example 1 — Inverse of a \(4\times 4\) Matrix
Find the inverse of
\[ A = \begin{pmatrix} 4 & 8 & 4 & 0 \\ 1 & 4 & 7 & 2 \\ 1 & 5 & 4 & -3 \\ 1 & 3 & 0 & -2 \end{pmatrix}. \]
Solution
Form the \(4\times 8\) augmented matrix \([A \mid I_4]\):
\[ \left( \begin{array}{cccc|cccc} 4 & 8 & 4 & 0 & 1 & 0 & 0 & 0 \\ 1 & 4 & 7 & 2 & 0 & 1 & 0 & 0 \\ 1 & 5 & 4 & -3 & 0 & 0 & 1 & 0 \\ 1 & 3 & 0 & -2 & 0 & 0 & 0 & 1 \end{array} \right). \]
For hand computation we take the pivots in order without row interchanges and postpone dividing by the pivots to the end. The algorithm above would swap \(R_2\) and \(R_3\) at step 2 (pivot \(3\)) and normalize each pivot row as it goes, so its intermediate matrices differ, but it reaches the same \(A^{-1}\).
Step 1 — Eliminate column 1 (pivot \(= 4\); subtract multiples of \(R_1\) from \(R_2, R_3, R_4\)):
\[ \left( \begin{array}{cccc|cccc} 4 & 8 & 4 & 0 & 1 & 0 & 0 & 0 \\ 0 & 2 & 6 & 2 & -\tfrac{1}{4} & 1 & 0 & 0 \\ 0 & 3 & 3 & -3 & -\tfrac{1}{4} & 0 & 1 & 0 \\ 0 & 1 & -1 & -2 & -\tfrac{1}{4} & 0 & 0 & 1 \end{array} \right). \]
Step 2 — Eliminate column 2 (pivot \(= 2\); also clear \(R_1\)):
\[ \left( \begin{array}{cccc|cccc} 4 & 0 & -20 & -8 & 2 & -4 & 0 & 0 \\ 0 & 2 & 6 & 2 & -\tfrac{1}{4} & 1 & 0 & 0 \\ 0 & 0 & -6 & -6 & \tfrac{1}{8} & -\tfrac{3}{2} & 1 & 0 \\ 0 & 0 & -4 & -3 & -\tfrac{1}{8} & -\tfrac{1}{2} & 0 & 1 \end{array} \right). \]
Step 3 — Eliminate column 3 (pivot \(= -6\); clear \(R_1, R_2, R_4\)):
\[ \left( \begin{array}{cccc|cccc} 4 & 0 & 0 & 12 & \tfrac{19}{12} & 1 & -\tfrac{10}{3} & 0 \\ 0 & 2 & 0 & -4 & -\tfrac{1}{8} & -\tfrac{1}{2} & 1 & 0 \\ 0 & 0 & -6 & -6 & \tfrac{1}{8} & -\tfrac{3}{2} & 1 & 0 \\ 0 & 0 & 0 & 1 & -\tfrac{5}{24} & \tfrac{1}{2} & -\tfrac{2}{3} & 1 \end{array} \right). \]
Step 4 — Eliminate column 4 (pivot \(= 1\); clear \(R_1, R_2, R_3\)), then normalize \(R_1, R_2, R_3\) by their diagonal entries:
\[ \left( \begin{array}{cccc|cccc} 1 & 0 & 0 & 0 & \tfrac{49}{48} & -\tfrac{5}{4} & \tfrac{7}{6} & -3 \\ 0 & 1 & 0 & 0 & -\tfrac{23}{48} & \tfrac{3}{4} & -\tfrac{5}{6} & 2 \\ 0 & 0 & 1 & 0 & \tfrac{3}{16} & -\tfrac{1}{4} & \tfrac{1}{2} & -1 \\ 0 & 0 & 0 & 1 & -\tfrac{5}{24} & \tfrac{1}{2} & -\tfrac{2}{3} & 1 \end{array} \right). \]
The inverse is
\[ A^{-1} = \begin{pmatrix} \tfrac{49}{48} & -\tfrac{5}{4} & \tfrac{7}{6} & -3 \\[4pt] -\tfrac{23}{48} & \tfrac{3}{4} & -\tfrac{5}{6} & 2 \\[4pt] \tfrac{3}{16} & -\tfrac{1}{4} & \tfrac{1}{2} & -1 \\[4pt] -\tfrac{5}{24} & \tfrac{1}{2} & -\tfrac{2}{3} & 1 \end{pmatrix}. \]
Note: \(\det(A) = -48\) (verified by cofactor expansion).
Verification
Compute the first column of \(A A^{-1}\) (it should equal \(e_1 = (1,0,0,0)^T\)):
| Row | \(\sum_j A_{ij}(A^{-1})_{j1}\) | Result |
|---|---|---|
| 1 | \(4\cdot\tfrac{49}{48} + 8\cdot(-\tfrac{23}{48}) + 4\cdot\tfrac{3}{16} + 0\) | \(\tfrac{196-184}{48}+\tfrac{3}{4} = \tfrac{12}{48}+\tfrac{36}{48} = 1\) \(\checkmark\) |
| 2 | \(\tfrac{49}{48} + 4\cdot(-\tfrac{23}{48}) + 7\cdot\tfrac{3}{16} + 2\cdot(-\tfrac{5}{24})\) | \(\tfrac{49-92+63-20}{48} = 0\) \(\checkmark\) |
| 3 | \(\tfrac{49}{48} + 5\cdot(-\tfrac{23}{48}) + 4\cdot\tfrac{3}{16} + (-3)\cdot(-\tfrac{5}{24})\) | \(\tfrac{49-115+36+30}{48} = 0\) \(\checkmark\) |
| 4 | \(\tfrac{49}{48} + 3\cdot(-\tfrac{23}{48}) + 0 + (-2)\cdot(-\tfrac{5}{24})\) | \(\tfrac{49-69+20}{48} = 0\) \(\checkmark\) |
The Hilbert Matrix
Hilbert matrices appear naturally in polynomial approximation problems and are classic examples of severely ill-conditioned matrices.
The Hilbert matrix \(H_n\) of order \(n\) has elements
\[ (H_n)_{ij} = \frac{1}{i+j-1}, \quad i,j = 1,\ldots,n. \]
The Hilbert matrices of orders 2, 3 and 4 are:
\[ H_2 = \begin{pmatrix}1 & \tfrac{1}{2}\\\tfrac{1}{2} & \tfrac{1}{3}\end{pmatrix}, \quad H_3 = \begin{pmatrix}1&\tfrac{1}{2}&\tfrac{1}{3}\\\tfrac{1}{2}&\tfrac{1}{3}&\tfrac{1}{4}\\\tfrac{1}{3}&\tfrac{1}{4}&\tfrac{1}{5}\end{pmatrix}, \quad H_4 = \begin{pmatrix} 1&\tfrac{1}{2}&\tfrac{1}{3}&\tfrac{1}{4}\\ \tfrac{1}{2}&\tfrac{1}{3}&\tfrac{1}{4}&\tfrac{1}{5}\\ \tfrac{1}{3}&\tfrac{1}{4}&\tfrac{1}{5}&\tfrac{1}{6}\\ \tfrac{1}{4}&\tfrac{1}{5}&\tfrac{1}{6}&\tfrac{1}{7} \end{pmatrix}. \]
Hilbert matrices become extremely ill-conditioned as \(n\) grows. For \(n=5\) the 2-norm condition number is \(\kappa(H_5) \approx 4.77 \times 10^5\): a small relative change in the data can change the inverse, or a solution, by up to about \(5\times10^5\) times as much (Example 2 below). Inverting \(H_5\) in double precision (about 16 digits) still gives every entry to about 12 digits, but by \(n=12\), \(\kappa(H_{12}) \approx 1.7\times10^{16}\) and a computed inverse typically has only one or two correct digits: its relative error (2-norm) is usually between about 1% and 20%, and it varies erratically with the method and with last-bit rounding of the entries (about 6% to 9% for the correctly rounded \(H_{12}\); occasionally much smaller or larger). The exact formulas below give \(H_n^{-1}\) with no rounding at all.
Inverse Hilbert Matrix
An exact closed-form formula for the inverse exists. It can be expressed in two equivalent ways.
Factorial form:
\[ (H_n^{-1})_{ij} = \frac{(-1)^{i+j}}{i+j-1} \cdot\frac{(n+i-1)!\,(n+j-1)!}{\bigl[(i-1)!\,(j-1)!\bigr]^2\,(n-i)!\,(n-j)!}. \tag{7} \]
Binomial-coefficient form:
\[ (H_n^{-1})_{ij} = (-1)^{i+j}(i+j-1) \binom{n+i-1}{n-j} \binom{n+j-1}{n-i} \binom{i+j-2}{i-1}^{\!2}. \tag{8} \]
Example: for \(n=5\) the formulas yield the exact integer matrix
\[ H_5^{-1} = \begin{pmatrix} 25 & -300 & 1050 & -1400 & 630 \\ -300 & 4800 & -18900 & 26880 & -12600 \\ 1050 & -18900 & 79380 & -117600 & 56700 \\ -1400 & 26880 & -117600 & 179200 & -88200 \\ 630 & -12600 & 56700 & -88200 & 44100 \end{pmatrix}. \]
Verification of entry \((1,1)\): \((H_5^{-1})_{11} = (-1)^2 \cdot 1 \cdot \binom{5}{4}\binom{5}{4}\binom{0}{0}^2 = 5 \cdot 5 \cdot 1 = 25\) \(\checkmark\)
Verification of entry \((1,2)\): \((H_5^{-1})_{12} = (-1)^3 \cdot 2 \cdot \binom{5}{3}\binom{6}{4}\binom{1}{0}^2 = -2 \cdot 10 \cdot 15 \cdot 1 = -300\) \(\checkmark\)
Example 2 — The Hilbert Matrix Rounded to Six Decimals
Round each entry of \(H_5\) to six decimal places (Mathews calls this the chopped Hilbert matrix) and find the inverse of
\[ A = \begin{pmatrix} 1 & 0.5 & 0.333333 & 0.25 & 0.2 \\ 0.5 & 0.333333 & 0.25 & 0.2 & 0.166667 \\ 0.333333 & 0.25 & 0.2 & 0.166667 & 0.142857 \\ 0.25 & 0.2 & 0.166667 & 0.142857 & 0.125 \\ 0.2 & 0.166667 & 0.142857 & 0.125 & 0.111111 \end{pmatrix}. \]
Solution
Gauss–Jordan elimination on \([A \mid I_5]\), carried out in exact rational arithmetic so that no further rounding enters, gives (to two decimals)
\[ A^{-1} \approx \begin{pmatrix} 26.93 & -336.02 & 1205.11 & -1634.03 & 744.41 \\ -336.02 & 5471.18 & -21788.96 & 31237.24 & -14729.52 \\ 1205.11 & -21788.96 & 91810.65 & -136343.92 & 65859.12 \\ -1634.03 & 31237.24 & -136343.92 & 207458.85 & -102006.85 \\ 744.41 & -14729.52 & 65859.12 & -102006.85 & 50845.21 \end{pmatrix}. \]
No entry of \(A\) differs from the corresponding entry of \(H_5\) by more than \(3.4\times10^{-7}\), yet \(A^{-1}\) is far from the exact \(H_5^{-1}\) above: entry \((4,4)\) is \(207458.85\) instead of \(179200\), an error of \(28258.85\) (16%), and entry \((1,1)\) is \(26.93\) instead of \(25\). Measured in the 2-norm, a relative change of \(3.9\times10^{-7}\) in the matrix has become a relative change of \(0.16\) in its inverse, a magnification of about \(4\times10^5\), close to \(\kappa(H_5) \approx 4.77\times10^5\).
Verification
In exact arithmetic \(A A^{-1} = I_5\) (for example, row 1 of \(A\) times the exact \(A^{-1}\) is \((1, 0, 0, 0, 0)\)), so the matrix above is the true inverse of the rounded matrix, to the two decimals shown. The error is caused by rounding the data, not by the elimination: a double-precision inversion of \(A\) agrees with its exact inverse to a relative error of about \(2\times10^{-12}\).
Application: Continuous Least-Squares Approximation
Theory
The continuous least-squares approximation to a function \(f(x)\) on \([0,1]\) using the basis \(\{\phi_0, \phi_1, \ldots, \phi_{n-1}\} = \{1, x, x^2, \ldots, x^{n-1}\}\) minimises \(\int_0^1 \bigl[f(x) - p(x)\bigr]^2\,dx\) where \(p(x) = c_1 + c_2 x + \cdots + c_n x^{n-1}\).
Taking partial derivatives leads to the normal equations
\[ G\,\mathbf{c} = \mathbf{y}, \quad G_{ij} = \langle \phi_{i-1},\phi_{j-1}\rangle = \int_0^1 x^{i+j-2}\,dx, \quad y_i = \langle f,\phi_{i-1}\rangle = \int_0^1 f(x)\,x^{i-1}\,dx. \tag{9} \]
Since \(G_{ij} = 1/(i+j-1)\), the Gram matrix \(G\) is exactly the Hilbert matrix \(H_n\).
The Gram matrix \(G\) is the matrix of inner products: \(G_{ij} = \langle \phi_{i-1}, \phi_{j-1} \rangle\).
When the basis is \(\{1, x, x^2, \ldots, x^{n-1}\}\) with inner product \(\langle f,g\rangle = \int_0^1 f(x)g(x)\,dx\), the Gram matrix equals \(H_n\).
The coefficient vector is \(\mathbf{c} = H_n^{-1}\,\mathbf{y}\). With the exact formula (7)–(8) for \(H_n^{-1}\) the inverse carries no rounding error and each \(c_i\) comes out in closed form. Because \(H_n\) is ill-conditioned, \(\mathbf{c}\) is sensitive to errors in \(\mathbf{y}\), so the \(y_i\) must be known to many digits.
Example 3 — Degree-4 Polynomial Approximation of \(\cos x\) on \([0,1]\)
Find the continuous least-squares polynomial of degree 4 that approximates \(f(x) = \cos x\) over \([0,1]\).
Setup. The basis is \(\{1, x, x^2, x^3, x^4\}\) so \(n = 5\) and \(G = H_5\). The right-hand side vector has components
\[ y_i = \int_0^1 x^{i-1}\cos x\,dx. \]
Evaluating by parts:
\[ \begin{aligned} y_1 &= \int_0^1 \cos x\,dx = \sin(1) \approx 0.84147, \\[4pt] y_2 &= \int_0^1 x\cos x\,dx = \bigl[\sin(1) + \cos(1) - 1\bigr] \approx 0.38177, \\[4pt] y_3 &= \int_0^1 x^2\cos x\,dx = 2\cos(1) - \sin(1) \approx 0.23913, \\[4pt] y_4 &= \int_0^1 x^3\cos x\,dx = 6 - 5\sin(1) - 3\cos(1) \approx 0.17174, \\[4pt] y_5 &= \int_0^1 x^4\cos x\,dx = 13\sin(1) - 20\cos(1) \approx 0.13308. \end{aligned} \]
Solution. The coefficients satisfy \(H_5\,\mathbf{c} = \mathbf{y}\), so \(\mathbf{c} = H_5^{-1}\,\mathbf{y}\). Computing the matrix–vector product with the exact integer matrix \(H_5^{-1}\) (and using high-precision values of \(y_i\)) yields
\[ c_1 = 25\,y_1 - 300\,y_2 + 1050\,y_3 - 1400\,y_4 + 630\,y_5. \]
Substituting the integral values:
\[ c_1 = 25\sin(1) - 300\bigl[\sin(1)+\cos(1)-1\bigr] + 1050\bigl[2\cos(1)-\sin(1)\bigr] - 1400\bigl[6-5\sin(1)-3\cos(1)\bigr] + 630\bigl[13\sin(1)-20\cos(1)\bigr]. \]
After collecting terms the coefficient simplifies to \(c_1 = 13865\sin(1) - 6600\cos(1) - 8100 \approx 0.999986\), close to but not equal to \(\cos 0 = 1\): a least-squares fit need not pass through \(f(0)\).
The same row products give all five coefficients:
\[ \begin{aligned} c_1 &= 13865\sin(1) - 6600\cos(1) - 8100 \approx 0.999986, \\[4pt] c_2 &= 156480 + 138360\cos(1) - 274800\sin(1) \approx 0.000415, \\[4pt] c_3 &= 1227870\sin(1) - 641340\cos(1) - 686700 \approx -0.502729, \\[4pt] c_4 &= 1048320 + 1018080\cos(1) - 1899520\sin(1) \approx 0.006496, \\[4pt] c_5 &= 945630\sin(1) - 516600\cos(1) - 516600 \approx 0.036152. \end{aligned} \]
The terms of each row product are large (up to about \(3\times10^4\) for \(c_4\)) and cancel to leave small coefficients, so the \(y_i\) must be accurate: with the five-decimal values of \(y_i\) shown above, the same exact \(H_5^{-1}\) gives \(\mathbf{c} \approx (0.99665,\ 0.0612,\ -0.7581,\ 0.3836,\ -0.1449)\), in which only \(c_1\) is right even to two decimals. With the \(y_i\) in full double precision, both the exact \(H_5^{-1}\) and a floating-point solve of \(H_5\mathbf{c} = \mathbf{y}\) give \(\mathbf{c}\) to a norm-wise relative error of a few parts in \(10^{12}\). The resulting degree-4 least-squares polynomial is
\[ p(x) \approx 0.999986 + 0.000415\,x - 0.502729\,x^2 + 0.006496\,x^3 + 0.036152\,x^4, \]
as Mathews also finds. Its coefficients are close to, but not the same as, the Taylor coefficients \(1, 0, -\tfrac{1}{2}, 0, \tfrac{1}{24}\) of \(\cos x = 1 - \tfrac{x^2}{2} + \tfrac{x^4}{24} - \cdots\), because least squares spreads the error over all of \([0,1]\) instead of making it small near \(x=0\).
Verification. Substituting the exact \(\mathbf{c}\) back gives \(H_5\mathbf{c} - \mathbf{y} = \mathbf{0}\) identically. Compare \(p\) with \(\cos x\) at \(x = 0\) and \(x = 1\):
| \(x\) | \(\cos x\) | \(p(x)\) | error |
|---|---|---|---|
| \(0\) | \(1.00000\) | \(0.99999\) | \(1.4\times10^{-5}\) |
| \(1\) | \(0.54030\) | \(0.54032\) | \(1.7\times10^{-5}\) |
The largest error on \([0,1]\) is \(1.7\times10^{-5}\) (at \(x=1\)), against \(1.4\times10^{-3}\) for the Taylor polynomial \(1 - \tfrac{x^2}{2} + \tfrac{x^4}{24}\).
The Gram matrix for the monomial basis on \([0,1]\) is the Hilbert matrix, so the normal equations inherit its ill-conditioning: with \(\kappa(H_5) \approx 4.77 \times 10^5\), errors in \(\mathbf{y}\) can be magnified by up to that factor in \(\mathbf{c}\). For \(n = 5\) double precision still has digits to spare, but the margin shrinks quickly as \(n\) grows. The exact \(H_n^{-1}\) gives the coefficients in exact closed form (integer combinations of \(1\), \(\sin 1\) and \(\cos 1\) here), so they can be evaluated to any precision. It removes the error of inverting \(H_n\), not the sensitivity of \(\mathbf{c}\) to errors in \(\mathbf{y}\).