Gauss-Jordan Elimination
Gauss-Jordan elimination continues where Gaussian elimination stops: it also clears the entries above each pivot and scales every pivot to 1. The result, the reduced row echelon form (RREF), lets you read the general solution of a system directly, without back substitution.
Reduced row echelon form
A matrix is in reduced row echelon form if it is in row echelon form and in addition:
- every pivot equals 1, and
- every pivot is the only non-zero entry in its column.
The algorithm
- Forward phase: Gaussian elimination to row echelon form.
- Backward phase: starting from the last pivot and moving up, scale the pivot row so the pivot is 1, then subtract multiples of it from every row above to make zeros above the pivot.
(Doing the scaling and clearing both above and below in a single pass also works; it costs a bit more arithmetic.)
Reading off the general solution
Each non-zero row of the RREF expresses one pivot variable in terms of the free variables. Example:
As a vector: . A particular solution plus combinations of two direction vectors: a plane in . This shape (particular solution + homogeneous solutions) is explained in Homogeneous Systems.
Implementing RREF
from fractions import Fraction as F
def rref(M):
M = [[F(a) for a in row] for row in M]
rows, cols = len(M), len(M[0])
pivots, r = [], 0
for c in range(cols):
p = next((i for i in range(r, rows) if M[i][c] != 0), None)
if p is None:
continue
M[r], M[p] = M[p], M[r]
pv = M[r][c]
M[r] = [a / pv for a in M[r]] # pivot becomes 1
for i in range(rows): # clear above AND below
if i != r and M[i][c] != 0:
f = M[i][c]
M[i] = [a - f * b for a, b in zip(M[i], M[r])]
pivots.append(c)
r += 1
if r == rows:
break
return M, pivots
aug = [[2, 6, 1, 0, 9],
[1, 3, 0, -2, 5],
[3, 9, 2, 2, 13]]
R, piv = rref(aug)
for row in R:
print([str(a) for a in row])
print("pivot columns:", piv)['1', '3', '0', '-2', '5']
['0', '0', '1', '4', '-1']
['0', '0', '0', '0', '0']
pivot columns: [0, 2]
This is the RREF shown above. Note that rref treats the right-hand side column like any other column. If a pivot ever lands in that last column, the system is inconsistent.
Comparing with SymPy
from sympy import Matrix, symbols, linsolve
aug = Matrix([[2, 6, 1, 0, 9], [1, 3, 0, -2, 5], [3, 9, 2, 2, 13]])
R, piv = aug.rref()
print(R.tolist(), piv)
x1, x2, x3, x4 = symbols("x1:5")
print(linsolve(aug, [x1, x2, x3, x4]))[[1, 3, 0, -2, 5], [0, 0, 1, 4, -1], [0, 0, 0, 0, 0]] (0, 2)
{(-3*x2 + 2*x4 + 5, x2, -4*x4 - 1, x4)}
linsolve accepts the augmented matrix directly and returns the general solution in terms of the free variables, exactly the formula we read off by hand.
Gaussian or Gauss-Jordan?
| Gaussian + back substitution | Gauss-Jordan | |
|---|---|---|
| Result | row echelon form (not unique) | reduced row echelon form (unique) |
| Work for | operations | operations |
| Best for | solving numerically | reading off solution sets, rank, null space, inverses by hand |
Numerical software prefers plain elimination because it is cheaper. For theory and hand calculations, RREF is the clearest picture of a system. It is also how you compute an inverse by hand: row-reduce to (see Matrix Inverses).
Exercises
Exercise 1: Is it reduced?
Which are in RREF? (a) (b) (c) (d) .
Answer
(a) and (c).
Solution
(b) has a non-zero entry (2) above the second pivot. (d) has a pivot equal to 2.
Exercise 2: Read the solution
An augmented matrix reduces to . Write the general solution in vector form.
Answer
.
Solution
Exercise 3: Full reduction by hand
Reduce to RREF.
Answer
, so .
Solution
from sympy import Matrix
print(Matrix([[1, 2, 3, 9], [2, -1, 1, 8], [3, 0, -1, 3]]).rref())(Matrix([
[1, 0, 0, 2],
[0, 1, 0, -1],
[0, 0, 1, 3]]), (0, 1, 2))
Exercise 4: Detect inconsistency (code)
Extend the idea of the lesson: write classify(aug) that uses SymPy's rref() and returns "inconsistent", "unique" or "infinite". Test it on and .
Answer
"inconsistent" and "infinite".
Solution
The system is inconsistent exactly when the last column is a pivot column. Otherwise it is unique when every variable column is a pivot column.
from sympy import Matrix
def classify(aug):
M = Matrix(aug)
n = M.cols - 1
_, piv = M.rref()
if n in piv:
return "inconsistent"
return "unique" if len(piv) == n else "infinite"
print(classify([[1, 1, 2], [2, 2, 5]]))
print(classify([[1, 2, 1, 1], [0, 1, 1, 2]]))inconsistent
infinite
Test yourself
What extra conditions does RREF add to row echelon form?
- All entries are integers
- Pivots are 1 and are the only non-zero entries in their columns
- The matrix must be square
- No zero rows allowed
In the RREF of an augmented matrix, the last column is a pivot column. What does that tell you?
- The solution is unique
- There is a free variable
- The system is inconsistent
- The matrix is invertible
An RREF has pivots in columns 1 and 3 of a 4-variable system (consistent). How many free parameters does the general solution have?
- 0
- 1
- 2
- 4