Part V · Linear algebra Chapter 36 of 60
Gaussian elimination
How to find a lost planet from a few observations: turn a system of many equations into a staircase and read the answer from the bottom up. The steps show at once whether the system has one solution, infinitely many or none.
Builds on: 35 · Matrices as transformations
You will learn
- reduce a system to row echelon form and write down all its solutions from it
- find the rank and use the Rouché–Capelli theorem to tell whether a system has one solution, infinitely many or none
- estimate how many operations Gaussian elimination costs and understand why a computer chooses a pivot
In the last chapter a matrix became a transformation: $A$ takes a vector $\mathbf x$ and carries it to $A\mathbf x$. Solving the system $A\mathbf x = \mathbf b$ means travelling that road backwards and finding out which vector the transformation sent to $\mathbf b$. For two unknowns the formula for the inverse matrix is enough. But an astronomer looking for a planet’s orbit has six unknowns, and an engineer designing the frame of a bridge has hundreds of thousands. We need a method that doesn’t care how many equations there are and never fails.
Such a method was known long before matrices, and what made it famous was a disappearance. On the first night of the nineteenth century, 1 January 1801, Giuseppe Piazzi at the Palermo observatory noticed a faint star that wasn’t in the catalogue. The next night it had moved. Piazzi took it for a comet, although for a comet it crept too slowly and too evenly. He observed it 24 times until he fell ill on 11 February, and then the new body, later named Ceres, vanished into the glare of the Sun. To find it again in the autumn an orbit was needed, and six weeks of observations gave only a tiny piece of its path.
An orbit is given by six numbers, the orbital elements: the size and elongation of the ellipse, the tilt of its plane and the line along which that plane cuts the plane of the Earth’s orbit, the turn of the ellipse within its plane, and the planet’s position at a chosen moment. The job of recovering them from Piazzi’s observations was taken on by the twenty-four-year-old Carl Friedrich Gauss. How the hunt ended we’ll tell at the end of the chapter. First, the tool Gauss couldn’t have managed without.
When refining an orbit from many observations, a computer (in those days a person who did calculations) takes approximate elements and looks for small corrections to them. For small corrections the equations become linear, just as the tangent from the chapter on the derivative replaces a curve near a point. The result is a system of linear equations in six unknowns, which has to be solved by hand, again and again. Gauss solved such systems by eliminating the unknowns one after another. Let’s see how this method works, why it always works, and what it costs.
Three permitted moves
Let’s start with a toy version of the astronomer’s problem. A point was observed three times: at the moments $t = 1$, $2$ and $3$ it was at heights $6$, $11$ and $18$. Suppose the height changes by the law $y = a + bt + ct^2$, like a thrown stone. Each observation gives an equation in the unknown coefficients:
$$\left\{\begin{aligned} a + b + c &= 6, \\ a + 2b + 4c &= 11, \\ a + 3b + 9c &= 18. \end{aligned}\right.$$
Subtract the first equation from the second and from the third. The unknown $a$ disappears from both: we’re left with $b + 3c = 5$ and $2b + 8c = 12$. From the last, subtract twice the one before and we get $2c = 2$. Now we climb back up: $c = 1$, then $b = 5 - 3c = 2$, then $a = 6 - b - c = 3$. The law of motion is $y = 3 + 2t + t^2$, and at the moment $t = 4$ the point will be at height $3 + 8 + 16 = 27$. We’ve predicted the next observation, a miniature of what Gauss had to do.
The letters in these calculations only take up space: all the work is done by the coefficients. Let’s write them out as a table, with a bar separating the right-hand sides. Each operation on the equations becomes an operation on the rows of the table:
$$\left(\begin{array}{ccc|c} 1 & 1 & 1 & 6 \\ 1 & 2 & 4 & 11 \\ 1 & 3 & 9 & 18 \end{array}\right) \xrightarrow{\substack{R_2 - R_1 \\ R_3 - R_1}} \left(\begin{array}{ccc|c} 1 & 1 & 1 & 6 \\ 0 & 1 & 3 & 5 \\ 0 & 2 & 8 & 12 \end{array}\right) \xrightarrow{R_3 - 2R_2} \left(\begin{array}{ccc|c} 1 & 1 & 1 & 6 \\ 0 & 1 & 3 & 5 \\ 0 & 0 & 2 & 2 \end{array}\right).$$
The matrix of coefficients of a system with the column of right-hand sides appended on the right is called the augmented matrix and written $(A \mid \mathbf b)$. We’ll write row number $i$ as $R_i$. The elementary row operations are three moves: swap two rows ($R_i \leftrightarrow R_j$), multiply a row by a non-zero number ($R_i \to kR_i$, $k \ne 0$), and add to a row another row multiplied by any number ($R_i \to R_i + kR_j$, $j \ne i$).
On equations the moves do what we did by hand: swap equations, multiply an equation by a number, add one equation to another. But why, after such moves, are no solutions lost and no extra ones gained? This needs proving, and the proof is the most visual part of the whole method.
If the augmented matrix $(A' \mid \mathbf b')$ is obtained from $(A \mid \mathbf b)$ by elementary row operations, then the systems $A\mathbf x = \mathbf b$ and $A'\mathbf x = \mathbf b'$ have the same solutions.
The trick is that each move can be undone by another move. So it’s enough to check one thing: after a move, the old solutions remain solutions.
A system has two equations: $x + y = 3$ and $x - y = 1$. The second equation was multiplied by $0$. What happened to the set of solutions?
The equation $0x + 0y = 0$ holds for all $x$ and $y$, so what’s left of the system is the single equation $x + y = 3$, a whole line of solutions. You may multiply only by a non-zero number: such a move is reversible, and the solutions don’t change.
The staircase
We have the moves; we need a strategy. The goal is a matrix with nothing but zeros below a “staircase”, where each next step begins further right than the one before:
$$\left(\begin{array}{cccc|c} \boxed{2} & 1 & 0 & 3 & 5 \\ 0 & 0 & \boxed{-1} & 4 & 1 \\ 0 & 0 & 0 & \boxed{7} & 14 \\ 0 & 0 & 0 & 0 & 0 \end{array}\right)$$
A matrix is in row echelon form if the zero rows are at the bottom and the first non-zero entry of each non-zero row stands strictly further right than that of the row above it. These first non-zero entries, the steps of the staircase (boxed above), are called pivots.
A system in echelon form is solved from the bottom up, as we’ve already done: the last non-zero equation contains the fewest unknowns, so we solve it first and substitute what we find into the ones above. This is back substitution. The staircase itself is obtained by forward elimination. We go through the columns from left to right. In the current column we look for a non-zero entry among the rows that haven’t yet become steps; if there’s none, we skip the column. We bring the entry found to the top of the remaining part by a swap: it’s the new pivot. From each row below we subtract the pivot row with a factor chosen to make a zero under the pivot: if the pivot is $p$ and the entry under it is $q$, the factor is $\frac qp$. Then we go down a row and move on to the next column.
Any matrix can be brought to row echelon form by elementary row operations. The forward elimination described above always ends with a matrix in echelon form.
We argue by induction on the number of columns: forward elimination deals with the first column and then works with a smaller matrix.
The staircase can be polished. Divide each pivot row by its pivot so that the steps become ones, and zero everything above them by working back through the columns. For our parabola system we get
$$\left(\begin{array}{ccc|c} 1 & 1 & 1 & 6 \\ 0 & 1 & 3 & 5 \\ 0 & 0 & 2 & 2 \end{array}\right) \longrightarrow \left(\begin{array}{ccc|c} 1 & 0 & 0 & 3 \\ 0 & 1 & 0 & 2 \\ 0 & 0 & 1 & 1 \end{array}\right),$$
and the answer $a = 3$, $b = 2$, $c = 1$ stands right there in the last column.
An echelon matrix whose pivots are all $1$, with zeros above them, is said to be in reduced row echelon form. Forward elimination together with this backward pass is called Gauss–Jordan elimination.
It’s named after Wilhelm Jordan, a German geodesist who described this variant in a surveying textbook at the end of the nineteenth century. The method is often mistakenly linked with Camille Jordan, whose normal form of matrices we’ll meet in chapter 38.
Sometimes a zero turns up in the place of the next pivot. Take the system $x + y + 2z = 5$, $2x + 2y + 5z = 11$, $3x + 4y + 7z = 17$. After the subtractions $R_2 - 2R_1$ and $R_3 - 3R_1$ both $x$ and $y$ have disappeared from the second equation: it’s become $z = 1$. The pivot for the second column has to be looked for lower down: it’s in the third row, $y + z = 2$. Swap the rows, and the staircase is ready. Try doing it yourself.
Solve the system $x + y + z = 4$, $2x + 3y + z = 4$, $x - y + 2z = 9$. Write the answer as a triple $(x, y, z)$.
$R_2 - 2R_1$ gives $y - z = -4$, and $R_3 - R_1$ gives $-2y + z = 5$. Add twice the second to the third: $-z = -3$, that is, $z = 3$. Then $y = -4 + z = -1$ and $x = 4 - y - z = 2$. A check with the third equation: $2 + 1 + 6 = 9$.
One, infinitely many or none
So far the staircase has come out complete: as many steps as unknowns. It isn’t always so. Take the system
$$\left\{\begin{aligned} x + 2y + z &= 4, \\ 2x + 4y + 3z &= 9, \\ 3x + 6y + 4z &= 13. \end{aligned}\right.$$
The subtractions $R_2 - 2R_1$ and $R_3 - 3R_1$ give identical rows $z = 1$, and after $R_3 - R_2$ the third equation turns into $0 = 0$. The backward pass leads to reduced row echelon form:
$$\left(\begin{array}{ccc|c} 1 & 2 & 0 & 3 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 \end{array}\right), \qquad\text{that is,}\qquad x + 2y = 3, \quad z = 1.$$
A step in the second column never appeared, and the unknown $y$ isn’t constrained by anything. Set it however you like, and the others adjust: $x = 3 - 2y$, $z = 1$. There are infinitely many solutions, and they all lie on one line in space.
The unknowns whose columns contain pivots are called leading (or basic), and the rest are called free. A system with at least one solution is called consistent, and a system with no solutions inconsistent.
If there are two free unknowns, the general solution has two parameters, and the solutions fill a plane. And if the right side of the third equation is $14$ instead of $13$, the same chain of moves ends with the row $0 = 1$. This equation is false for every $x$, $y$, $z$, and the system is inconsistent.
Everything is decided by the number of steps. It can only be counted once the staircase has been built, and a fair question arises: doesn’t it depend on which moves we made? Different people choose pivots differently and get different echelon matrices. It turns out that after polishing to the reduced form, all roads lead to the same place.
However a matrix $A$ is brought to reduced row echelon form, the result is the same. In particular, the number of pivots is determined by the matrix itself.
The idea: the reduced form can be read from the relations between the columns of the matrix, and the moves don’t change these relations.
The number of pivots in the echelon form of a matrix is called its rank and written $\operatorname{rank} A$.
Any echelon form can be polished into the reduced one without changing the number of steps, so the rank can be counted from any staircase. The parabola system has $\operatorname{rank} A = 3$; the system with the free $y$ has rank $2$. Now we can say when a system has solutions by comparing two numbers.
The system $A\mathbf x = \mathbf b$ is consistent if and only if $\operatorname{rank} A = \operatorname{rank}\,(A \mid \mathbf b)$. If both ranks equal $r$ and there are $n$ unknowns, then for $r = n$ the solution is unique, and for $r < n$ there are infinitely many solutions, with $n - r$ parameters in the general solution.
The trick is that forward elimination doesn’t distinguish the last column from the others: everything depends on whether a step lands there.
This theorem goes by different names in different books. In English-language books it’s the Rouché–Capelli theorem; others name it after Leopold Kronecker and Alfredo Capelli, or call it the Rouché–Fontené or the Rouché–Frobenius theorem.
In space each equation in three unknowns gives a plane, and the theorem turns into a catalogue of the ways three planes can be arranged:
| $\operatorname{rank} A$ | $\operatorname{rank}\,(A \mid \mathbf b)$ | Solutions | What you see |
|---|---|---|---|
| $3$ | $3$ | one | the planes meet at a point |
| $2$ | $2$ | a line | a “book”: a common line |
| $2$ | $3$ | none | a “prism”, or two are parallel |
| $1$ | $1$ | a plane | all three coincide |
| $1$ | $2$ | none | the planes are parallel |
The “book” row also includes the case where two planes coincide and the third cuts them. A “prism” is three planes that meet in pairs along three parallel lines; the other inconsistent case of the same rank is two parallel planes and a third that cuts both. In the last row all three are parallel, and at least two of them are distinct.
An especially simple case is a homogeneous system, where all the right-hand sides are zero. It’s always consistent: the zero column solves it. The interesting question is whether it has other solutions.
A homogeneous system with more unknowns than equations has a non-zero solution.
Let there be $m$ equations and $n > m$ unknowns. The echelon form has no more steps than rows, so $\operatorname{rank} A \le m < n$, and at least one unknown is free. The system is consistent (it has the zero solution), so by the Rouché–Capelli theorem the leading unknowns are found by back substitution for any values of the free ones. Give one free unknown the value $1$ and the others $0$. We get a solution with one coordinate equal to $1$, that is, a non-zero one.
This modest corollary will do a job in the next chapter: the proof that a space has a dimension rests on it.
A system has three equations and four unknowns. Can it have exactly one solution?
The rank is at most the number of rows, $3 < 4$, so there’s always a free unknown. If the system is consistent, it takes any value. But it may not be consistent: for example, if two equations differ only in their right-hand sides.
Find the rank of the matrix $\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}$.
$R_2 - 4R_1$ and $R_3 - 7R_1$ give the rows $(0, -3, -6)$ and $(0, -6, -12)$. The second of these is twice the first, and after $R_3 - 2R_2$ a zero row is left. There are two steps: the rank is $2$.
For which $a$ does the system $x + y + z = 1$, $x + 2y + 3z = 2$, $2x + 3y + (a + 6)z = 4$ have no solutions?
$R_2 - R_1$ gives $y + 2z = 1$, and $R_3 - 2R_1$ gives $y + (a + 4)z = 2$. Subtract one from the other: $(a + 2)z = 1$. For $a \ne -2$ this gives $z$, and the solution is unique. For $a = -2$ we get $0 = 1$: the rank of the matrix is $2$, the rank of the augmented matrix $3$, and there are no solutions.
What a solution costs
Gauss computed by hand, and every multiplication took time. How many operations does Gaussian elimination need to solve a system of $n$ equations in $n$ unknowns?
If an $n \times n$ system has a unique solution and no row swaps are needed, forward elimination and back substitution together require $\frac{n^3}{3} + n^2 - \frac n3$ multiplications and divisions and $\frac{n^3}{3} + \frac{n^2}{2} - \frac{5n}{6}$ additions and subtractions.
The trick is to see where the work is concentrated: in squares that shrink with each step.
Is that a lot or a little? Compare it with Cramer’s rule from the chapter on systems: each unknown is a ratio of two determinants. If a determinant is computed by expanding along a row, it has $n!$ terms. For $n = 20$ that’s about $10^{21}$ multiplications: at a billion operations a second, thirty thousand years. Gaussian elimination solves the same system in $5910$ operations, that is, in a few microseconds.
Cubic growth is the main law of this section. Double the size of the mesh in an engineering model, and the work grows eightfold. That’s why, for huge systems where almost all the coefficients are zero, many workarounds have been devised that exploit those zeros. But a dense system with no special structure is still solved by Gaussian elimination. It’s also how the power of supercomputers is measured: in the TOP500 ranking, kept since 1993, the machines compete in a single exercise, solving a huge dense system of linear equations.
How many multiplications and divisions will Gaussian elimination need for a system of $10$ equations in $10$ unknowns (with a unique solution and no swaps)?
By the formula $\frac{n^3}{3} + n^2 - \frac n3$ at $n = 10$: $\frac{1000}{3} + 100 - \frac{10}{3} = \frac{990}{3} + 100 = 330 + 100 = 430$.
Thin ice
A computer stores numbers with a finite number of significant digits, usually about sixteen. After each operation the result is rounded, and the errors accumulate. For Gaussian elimination this is no trifle: an unlucky pivot can ruin the whole answer. Let’s show this on a calculator that remembers only three significant digits. Solve the system
$$\left\{\begin{aligned} 0.0001x + y &= 1, \\ x + y &= 2. \end{aligned}\right.$$
Its exact solution is $x = \frac{10{,}000}{9999} \approx 1.0001$, $y = \frac{9998}{9999} \approx 0.9999$. The pivot is $0.0001$, the factor $10{,}000$. Subtract: $(1 - 10{,}000)y = 2 - 10{,}000$, that is, $-9999y = -9998$. The calculator’s three digits turn both sides into $-10{,}000$, and we get $y = 1$. Substitute into the first equation: $0.0001x = 1 - 1 = 0$, that is, $x = 0$. The answer is completely wrong. The two from the second equation drowned in the huge $10{,}000$, and then the tiny pivot blew up the remaining error.
Swap the equations. The pivot is now $1$, the factor $0.0001$: $(1 - 0.0001)y = 1 - 0.0002$, after rounding $y = 1$, and $x = 2 - y = 1$. The answer is right to three digits.
Partial pivoting is the rule by which, at each step, the pivot is the entry of the column with the largest absolute value among the remaining rows. Then all the factors are at most one in absolute value, and the numbers don’t blow up along the way.
Pivoting fixes the errors of the algorithm, but it can’t fix the problem itself. Recall the system from the chapter on systems: $x + y = 2$, $x + 1.01y = 2.01$, with solution $(1, 1)$. Shift the right side by a hundredth, to $2.02$, and the solution moves to $(0, 2)$. The right side changed by half a per cent, and the solution by a hundred per cent. The lines are almost parallel, and no order of operations can fix that: that’s how the data themselves are.
The condition number of an invertible matrix is $\kappa(A) = \|A\| \cdot \|A^{-1}\|$, where $\|A\|$ is the largest factor by which $A$ can lengthen a vector. The relative error of the solution of the system $A\mathbf x = \mathbf b$ can exceed the relative error of the right-hand side by at most a factor of $\kappa(A)$.
For the matrix in our example $\kappa \approx 402$, and half a per cent turned into a hundred per cent, two hundred times more, within the promised bound. A rough rule of numerical analysts: when $\kappa \approx 10^k$, about $k$ correct digits are lost in the answer. For the $10 \times 10$ Hilbert matrix, with entries $\frac{1}{i + j - 1}$, the condition number is about $1.6 \cdot 10^{13}$, and of the computer’s sixteen digits only two or three will be correct.
Recording the moves: LU decomposition
An astronomer gets new observations, but the moments at which they’re made are the same. In our toy this means the matrix $A$ is the same, and only the right-hand sides change. The moves of forward elimination are chosen by the matrix and don’t depend on the right-hand sides. They’re worth recording once.
Suppose forward elimination for a square matrix $A$ went through without row swaps and produced the echelon matrix $U$. Form the lower triangular matrix $L$: ones on the diagonal, and in place $(i, j)$ below the diagonal the factor $\ell_{ij}$ with which row $j$ was subtracted from row $i$. Then $A = LU$.
Follow row $i$. Forward elimination changed it only by subtractions: at step $j < i$ row $j$ was subtracted from it with factor $\ell_{ij}$. What was row $j$ at that moment? At step $j$ it’s the pivot row, and after that forward elimination changes only the rows below the pivot. So what was subtracted was already the final row $j$ of $U$. Writing the rows as $\mathbf a_i$ and $\mathbf u_j$, we get $\mathbf u_i = \mathbf a_i - \ell_{i1}\mathbf u_1 - \dots - \ell_{i,i-1}\mathbf u_{i-1}$, that is, $\mathbf a_i = \ell_{i1}\mathbf u_1 + \dots + \ell_{i,i-1}\mathbf u_{i-1} + 1 \cdot \mathbf u_i$. On the right is the $i$th row of the product $LU$: by the rule for multiplying matrices it’s made up of the rows of $U$ with coefficients from the $i$th row of $L$. The equation holds for each row, and $A = LU$.
Two triangular systems are solved in about $2n^2$ operations, against $\frac23 n^3$ for the full method. For a thousand unknowns that’s two million operations instead of almost seven hundred million. If swaps are needed, the decomposition is written as $PA = LU$: the matrix $P$ rearranges the rows of $A$ as pivoting would, after which forward elimination goes through without swaps. That’s the form in which Gaussian elimination lives in numerical libraries. LU decomposition was described in 1938 by the Polish astronomer Tadeusz Banachiewicz. In 1948 Alan Turing analysed how rounding errors accumulate in it and introduced the term “condition number”.
Ceres found
Back to 1801. Gauss worked out how to recover an orbit from three observations, computed where to look for Ceres, and sent the result to the astronomer Franz Xaver von Zach. On 31 December 1801 von Zach, and the next night Heinrich Olbers too, found it less than half a degree from the place Gauss had indicated. The twenty-four-year-old mathematician became a famous astronomer overnight. A few years later he took charge of the observatory in Göttingen.
He set out his method in 1809 in the book Theory of the Motion of the Heavenly Bodies Moving about the Sun in Conic Sections. The same book has the method of least squares: how to reconcile dozens of observations, each with its own error. It, and Gauss’s priority dispute with Legendre, will be the subject of chapter 39. The method of least squares leads to systems of linear equations, and Gauss explained how to solve them by successive elimination. In 1810, calculating the orbit of Pallas, the second asteroid, discovered by Olbers in the spring of 1802, he devised a compact notation for elimination. Professional computers used it throughout the nineteenth century.
The method itself is almost two thousand years older than Gauss: elimination of unknowns is described in the eighth chapter of the Chinese Nine Chapters on the Mathematical Art, and we saw the counting board with its columns of rods in the chapter on systems. In Europe it was taught from Isaac Newton’s Universal Arithmetick, published in 1707 from his lectures. By the end of the eighteenth century elimination of unknowns had become a routine algebra lesson. Gauss’s name became attached to the school algorithm only in the 1950s, in the era of the first computers, largely through confusion about its history.
Today Ceres is a dwarf planet, the largest body in the asteroid belt between Mars and Jupiter. From 2015 to 2018 the Dawn probe orbited it and photographed its bright spots, deposits of salts. The probe’s trajectory was refined from measurements by the method of least squares, a direct descendant of Gauss’s calculations.
Where next
Very different things were being added up in this chapter. We added the rows of a matrix to one another like vectors. Solutions of a homogeneous system can be added or multiplied by a number, and the result is again a solution. The parabolas $y = a + bt + ct^2$ add coefficient by coefficient: the sum of the parabolas from our two examples, $3 + 2t + t^2$ and $2 - t + t^2$, is again a polynomial of the same form, and it passes through points whose heights are the sums of the heights. Sound signals add when two instruments play, and are multiplied by a number when the volume knob is turned. Arrows, rows, polynomials, signals: why do they all behave the same way, and what can be understood about all such worlds at once? The answer is a single definition eight lines long. It’s where chapter 37 begins.