TExcelTAD
Home › Linear Algebra & Matrix Theory › Gauss-Jordan Elimination

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:

  1. every pivot equals 1, and
  2. every pivot is the only non-zero entry in its column.
[24−2600330000]⏟row echelon form[120400110000]⏟reduced row echelon form\underbrace{\left[\begin{array}{rrr|r} 2 & 4 & -2 & 6 \\ 0 & 0 & 3 & 3 \\ 0 & 0 & 0 & 0 \end{array}\right]}_{\text{row echelon form}} \qquad \underbrace{\left[\begin{array}{rrr|r} 1 & 2 & 0 & 4 \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 \end{array}\right]}_{\text{reduced row echelon form}}

The algorithm

  1. Forward phase: Gaussian elimination to row echelon form.
  2. 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:

[130−250014−100000]x1+3x2−2x4=5x3+4x4=−1\left[\begin{array}{rrrr|r} 1 & 3 & 0 & -2 & 5 \\ 0 & 0 & 1 & 4 & -1 \\ 0 & 0 & 0 & 0 & 0 \end{array}\right] \qquad \begin{aligned} x_1 + 3x_2 - 2x_4 &= 5 \\ x_3 + 4x_4 &= -1 \end{aligned}
pivot variables: x1,x3free: x2=s, x4=t\text{pivot variables: } x_1, x_3 \qquad \text{free: } x_2 = s,\ x_4 = t
x1=5−3s+2tx2=sx3=−1−4tx4=t\begin{aligned} x_1 &= 5 - 3s + 2t \\ x_2 &= s \\ x_3 &= -1 - 4t \\ x_4 &= t \end{aligned}

As a vector: (x1,x2,x3,x4)=(5,0,−1,0)+s⋅(−3,1,0,0)+t⋅(2,0,−4,1)(x_1, x_2, x_3, x_4) = (5, 0, -1, 0) + s \cdot (-3, 1, 0, 0) + t \cdot (2, 0, -4, 1). A particular solution plus combinations of two direction vectors: a plane in R4\mathbb{R}^4. This shape (particular solution + homogeneous solutions) is explained in Homogeneous Systems.

Implementing RREF

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

python · try it yourself
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 n×nn \times n ≈23n3\approx \frac{2}{3}n^3 operations ≈n3\approx n^3 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 [A∣I][A \mid I] to [I∣A−1][I \mid A^{-1}] (see Matrix Inverses).

Exercises

Exercise 1: Is it reduced?

Which are in RREF? (a) [102013]\begin{bmatrix} 1 & 0 & 2 \\ 0 & 1 & 3 \end{bmatrix} (b) [120010]\begin{bmatrix} 1 & 2 & 0 \\ 0 & 1 & 0 \end{bmatrix} (c) [100001000]\begin{bmatrix} 1 & 0 & 0 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \end{bmatrix} (d) [101020]\begin{bmatrix} 1 & 0 & 1 \\ 0 & 2 & 0 \end{bmatrix}.

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 [10−12012−3]\left[\begin{array}{rrr|r} 1 & 0 & -1 & 2 \\ 0 & 1 & 2 & -3 \end{array}\right]. Write the general solution in vector form.

Answer

(x1,x2,x3)=(2,−3,0)+t⋅(1,−2,1)(x_1, x_2, x_3) = (2, -3, 0) + t \cdot (1, -2, 1).

Solution
x3=t freex1−x3=2→x1=2+tx2+2x3=−3→x2=−3−2t\begin{aligned} &x_3 = t \text{ free} \\ &x_1 - x_3 = 2 &&\to x_1 = 2 + t \\ &x_2 + 2x_3 = -3 &&\to x_2 = -3 - 2t \end{aligned}

Exercise 3: Full reduction by hand

Reduce [12392−11830−13]\left[\begin{array}{rrr|r} 1 & 2 & 3 & 9 \\ 2 & -1 & 1 & 8 \\ 3 & 0 & -1 & 3 \end{array}\right] to RREF.

Answer

[1002010−10013]\left[\begin{array}{rrr|r} 1 & 0 & 0 & 2 \\ 0 & 1 & 0 & -1 \\ 0 & 0 & 1 & 3 \end{array}\right], so x=(2,−1,3)\mathbf{x} = (2, -1, 3).

Solution
R2−2R1, R3−3R1:[12390−5−5−100−6−10−24]R2←−15R2:[0112]R1−2R2, R3+6R2:[1015011200−4−12]R3←−14R3:[0013]R1−R3, R2−R3:[1002010−10013]\begin{aligned} &R_2 - 2R_1,\ R_3 - 3R_1\text{:} && \left[\begin{array}{rrr|r} 1 & 2 & 3 & 9 \\ 0 & -5 & -5 & -10 \\ 0 & -6 & -10 & -24 \end{array}\right] \\ &R_2 \leftarrow -\tfrac{1}{5}R_2\text{:} && \left[\begin{array}{rrr|r} 0 & 1 & 1 & 2 \end{array}\right] \\ &R_1 - 2R_2,\ R_3 + 6R_2\text{:} && \left[\begin{array}{rrr|r} 1 & 0 & 1 & 5 \\ 0 & 1 & 1 & 2 \\ 0 & 0 & -4 & -12 \end{array}\right] \\ &R_3 \leftarrow -\tfrac{1}{4}R_3\text{:} && \left[\begin{array}{rrr|r} 0 & 0 & 1 & 3 \end{array}\right] \\ &R_1 - R_3,\ R_2 - R_3\text{:} && \left[\begin{array}{rrr|r} 1 & 0 & 0 & 2 \\ 0 & 1 & 0 & -1 \\ 0 & 0 & 1 & 3 \end{array}\right] \end{aligned}
python · try it yourself
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 [112225]\left[\begin{array}{rr|r} 1 & 1 & 2 \\ 2 & 2 & 5 \end{array}\right] and [12110112]\left[\begin{array}{rrr|r} 1 & 2 & 1 & 1 \\ 0 & 1 & 1 & 2 \end{array}\right].

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.

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

  1. All entries are integers
  2. Pivots are 1 and are the only non-zero entries in their columns
  3. The matrix must be square
  4. No zero rows allowed

In the RREF of an augmented matrix, the last column is a pivot column. What does that tell you?

  1. The solution is unique
  2. There is a free variable
  3. The system is inconsistent
  4. 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?

  1. 0
  2. 1
  3. 2
  4. 4