import sympy as sp
M = sp.Matrix([
[1, -3, -5, 1],
[1, -1, -2, 0],
[3, -1, 1, 3]
])
M_rref, pivot_columns = M.rref()
M_rref, pivot_columns(Matrix([
[1, 0, 0, 0],
[0, 1, 0, -2],
[0, 0, 1, 1]]),
(0, 1, 2))
In Chapter 1, we solved systems by eliminating variables. In Chapter 2, we learned to store a system as an augmented matrix.
Now we combine these ideas.
Instead of rewriting equations again and again, we perform operations directly on the augmented matrix. This gives one of the most important algorithms in linear algebra:
row reduction.
Row reduction is the computational engine behind:
The main algorithm of this chapter is Gauss–Jordan elimination.
After studying this chapter, you should be able to:
Consider the system
\[ \begin{cases} x_1-3x_2-5x_3=1,\\ x_1-x_2-2x_3=0,\\ 3x_1-x_2+x_3=3. \end{cases} \]
The augmented matrix is
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 1&-1&-2&0\\ 3&-1&1&3 \end{array} \right]. \]
The equations contain variables, but elimination only changes the coefficients and constants. Therefore, we can work directly with the rows of this matrix.
The goal is to transform the system into a simpler but equivalent system.
Equivalent means:
the new system has exactly the same solution set as the original system.
There are three types of elementary row operations:
These operations do not change the solution set of the corresponding linear system.
We use notation such as:
\[ R_2 \leftarrow R_2-R_1 \]
to mean:
replace row 2 by row 2 minus row 1.
Similarly,
\[ R_3 \leftarrow R_3-3R_1 \]
means:
replace row 3 by row 3 minus three times row 1.
And
\[ R_2 \leftarrow \frac{1}{2}R_2 \]
means:
replace row 2 by one half of row 2.
Each row of an augmented matrix represents an equation.
Swapping rows only changes the order of equations. The solution set does not change.
Multiplying a row by a nonzero scalar changes an equation like
\[ 2x+4y=6 \]
into
\[ x+2y=3. \]
These equations have the same solutions.
Adding a multiple of one row to another corresponds to replacing one equation by a combination of equations. If a vector satisfies the original equations, it also satisfies the new equation. Conversely, the operation can be reversed, so no solutions are lost.
Elementary row operations preserve the solution set of a linear system.
Row reduction tries to create a staircase pattern.
A matrix is in row-echelon form if:
Example:
\[ \left[ \begin{array}{ccc|c} 1&2&-1&3\\ 0&1&4&5\\ 0&0&2&6 \end{array} \right] \]
is in row-echelon form.
The pivots form a staircase:
\[ \left[ \begin{array}{ccc|c} \boxed{1}&2&-1&3\\ 0&\boxed{1}&4&5\\ 0&0&\boxed{2}&6 \end{array} \right]. \]
Row-echelon form is useful for back substitution.
Gauss–Jordan elimination goes further.
A matrix is in reduced row-echelon form, abbreviated rref, if:
Example:
\[ \left[ \begin{array}{ccc|c} 1&0&0&2\\ 0&1&0&-1\\ 0&0&1&3 \end{array} \right] \]
is in reduced row-echelon form.
Another example is
\[ \left[ \begin{array}{ccccc|c} 1&2&0&0&3&2\\ 0&0&1&0&-1&4\\ 0&0&0&1&-2&3\\ 0&0&0&0&0&0 \end{array} \right]. \]
The leading \(1\)’s are pivots. The variables corresponding to pivot columns are pivot variables. The other variables are free variables.
Solve
\[ \begin{cases} x_1-3x_2-5x_3=1,\\ x_1-x_2-2x_3=0,\\ 3x_1-x_2+x_3=3. \end{cases} \]
Start with the augmented matrix:
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 1&-1&-2&0\\ 3&-1&1&3 \end{array} \right]. \]
Eliminate below the first pivot:
\[ R_2\leftarrow R_2-R_1,\qquad R_3\leftarrow R_3-3R_1. \]
This gives
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 0&2&3&-1\\ 0&8&16&0 \end{array} \right]. \]
Make the second pivot equal to \(1\):
\[ R_2\leftarrow \frac{1}{2}R_2. \]
Then
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 0&1&\frac32&-\frac12\\ 0&8&16&0 \end{array} \right]. \]
Eliminate below the second pivot:
\[ R_3\leftarrow R_3-8R_2. \]
So
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 0&1&\frac32&-\frac12\\ 0&0&4&4 \end{array} \right]. \]
Make the third pivot equal to \(1\):
\[ R_3\leftarrow \frac14R_3. \]
Then
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 0&1&\frac32&-\frac12\\ 0&0&1&1 \end{array} \right]. \]
Now eliminate above the third pivot:
\[ R_2\leftarrow R_2-\frac32R_3,\qquad R_1\leftarrow R_1+5R_3. \]
This gives
\[ \left[ \begin{array}{ccc|c} 1&-3&0&6\\ 0&1&0&-2\\ 0&0&1&1 \end{array} \right]. \]
Finally eliminate above the second pivot:
\[ R_1\leftarrow R_1+3R_2. \]
So
\[ \left[ \begin{array}{ccc|c} 1&0&0&0\\ 0&1&0&-2\\ 0&0&1&1 \end{array} \right]. \]
Therefore
\[ x_1=0,\qquad x_2=-2,\qquad x_3=1. \]
The solution is
\[ \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = \begin{bmatrix} 0\\ -2\\ 1 \end{bmatrix}. \]
Consider the reduced matrix
\[ \left[ \begin{array}{ccccc|c} 1&2&0&0&3&2\\ 0&0&1&0&-1&4\\ 0&0&0&1&-2&3\\ 0&0&0&0&0&0 \end{array} \right]. \]
This corresponds to the system
\[ x_1+2x_2+3x_5=2, \]
\[ x_3-x_5=4, \]
\[ x_4-2x_5=3. \]
The pivot variables are
\[ x_1,\quad x_3,\quad x_4. \]
The free variables are
\[ x_2,\quad x_5. \]
Let
\[ x_2=s,\qquad x_5=t. \]
Then
\[ x_1=2-2s-3t, \]
\[ x_3=4+t, \]
\[ x_4=3+2t. \]
Thus
\[ \begin{bmatrix} x_1\\ x_2\\ x_3\\ x_4\\ x_5 \end{bmatrix} = \begin{bmatrix} 2-2s-3t\\ s\\ 4+t\\ 3+2t\\ t \end{bmatrix}. \]
In parametric vector form,
\[ \begin{bmatrix} x_1\\ x_2\\ x_3\\ x_4\\ x_5 \end{bmatrix} = \begin{bmatrix} 2\\ 0\\ 4\\ 3\\ 0 \end{bmatrix} + s \begin{bmatrix} -2\\ 1\\ 0\\ 0\\ 0 \end{bmatrix} + t \begin{bmatrix} -3\\ 0\\ 1\\ 2\\ 1 \end{bmatrix}. \]
Each non-pivot column among the variable columns gives one free variable. Each free variable creates one direction vector in the solution set.
Row reduction reveals which type of solution set a system has.
If every variable is a pivot variable and there is no contradiction, the system has a unique solution.
Example:
\[ \left[ \begin{array}{ccc|c} 1&0&0&2\\ 0&1&0&-1\\ 0&0&1&3 \end{array} \right]. \]
The solution is
\[ (x_1,x_2,x_3)=(2,-1,3). \]
If at least one variable is free and there is no contradiction, the system has infinitely many solutions.
Example:
\[ \left[ \begin{array}{ccc|c} 1&0&2&3\\ 0&1&-1&4\\ 0&0&0&0 \end{array} \right]. \]
Here \(x_3\) is free.
If row reduction produces a row of the form
\[ \left[ \begin{array}{cccc|c} 0&0&0&\cdots&1 \end{array} \right], \]
then the system is inconsistent.
This row means
\[ 0=1, \]
which is impossible.
Example:
\[ \left[ \begin{array}{ccc|c} 1&0&2&3\\ 0&1&-1&4\\ 0&0&0&1 \end{array} \right] \]
has no solution.
A homogeneous system has the form
\[ Ax=0. \]
The augmented matrix is
\[ [A\mid 0]. \]
Since the zero vector always satisfies \(Ax=0\), a homogeneous system is always consistent.
There are two possibilities:
There is no third possibility.
If there is a free variable, then there are infinitely many solutions. If every variable is a pivot variable, then the only solution is the zero solution.
Solve
\[ \begin{cases} x_1+2x_2+3x_3=0,\\ 2x_1+4x_2+6x_3=0. \end{cases} \]
The augmented matrix is
\[ \left[ \begin{array}{ccc|c} 1&2&3&0\\ 2&4&6&0 \end{array} \right]. \]
Use
\[ R_2\leftarrow R_2-2R_1. \]
Then
\[ \left[ \begin{array}{ccc|c} 1&2&3&0\\ 0&0&0&0 \end{array} \right]. \]
The equation is
\[ x_1+2x_2+3x_3=0. \]
Let
\[ x_2=s,\qquad x_3=t. \]
Then
\[ x_1=-2s-3t. \]
So
\[ \begin{bmatrix} x_1\\ x_2\\ x_3 \end{bmatrix} = s \begin{bmatrix} -2\\ 1\\ 0 \end{bmatrix} + t \begin{bmatrix} -3\\ 0\\ 1 \end{bmatrix}. \]
Notice that the solution set goes through the origin because the system is homogeneous.
The rank of a matrix is the number of pivot positions in its row-echelon form.
For example,
\[ \left[ \begin{array}{ccccc} 1&2&0&0&3\\ 0&0&1&0&-1\\ 0&0&0&1&-2\\ 0&0&0&0&0 \end{array} \right] \]
has three pivots, so its rank is \(3\).
Rank measures how much independent information is contained in the rows or columns of a matrix. Later, rank will become one of the central concepts of the course.
There are two closely related algorithms.
Gaussian elimination reduces a matrix to row-echelon form. Then we solve using back substitution.
Gauss–Jordan elimination reduces a matrix all the way to reduced row-echelon form. Then the solution can be read off directly.
For hand computation, Gaussian elimination is often faster.
For conceptual understanding, reduced row-echelon form is very useful because it displays pivot variables and free variables clearly.
Given an augmented matrix:
NumPy is excellent for numerical computation, but it does not compute exact reduced row-echelon forms. For exact symbolic row reduction, use SymPy.
import sympy as sp
M = sp.Matrix([
[1, -3, -5, 1],
[1, -1, -2, 0],
[3, -1, 1, 3]
])
M_rref, pivot_columns = M.rref()
M_rref, pivot_columns(Matrix([
[1, 0, 0, 0],
[0, 1, 0, -2],
[0, 0, 1, 1]]),
(0, 1, 2))
The first output is the reduced row-echelon form. The second output gives the pivot columns.
For this example, the rref is
\[ \left[ \begin{array}{ccc|c} 1&0&0&0\\ 0&1&0&-2\\ 0&0&1&1 \end{array} \right]. \]
So the solution is
\[ (x_1,x_2,x_3)=(0,-2,1). \]
For a square system with a unique solution, we can use np.linalg.solve.
import numpy as np
A = np.array([
[1, -3, -5],
[1, -1, -2],
[3, -1, 1]
], dtype=float)
b = np.array([1, 0, 3], dtype=float)
x = np.linalg.solve(A, b)
xarray([ 1.48029737e-16, -2.00000000e+00, 1.00000000e+00])
Check the solution:
A @ xarray([1.00000000e+00, 2.22044605e-16, 3.00000000e+00])
This should reproduce \(b\).
For theoretical row reduction, use exact arithmetic when possible.
Floating-point arithmetic can introduce small roundoff errors. In numerical linear algebra, we often use more stable algorithms than direct row reduction, especially for large systems.
Consider
\[ \begin{cases} x+y=1,\\ x+y=2. \end{cases} \]
The augmented matrix is
\[ \left[ \begin{array}{cc|c} 1&1&1\\ 1&1&2 \end{array} \right]. \]
M = sp.Matrix([
[1, 1, 1],
[1, 1, 2]
])
M.rref()(Matrix([
[1, 1, 0],
[0, 0, 1]]),
(0, 2))
The rref is
\[ \left[ \begin{array}{cc|c} 1&1&0\\ 0&0&1 \end{array} \right]. \]
The second row means
\[ 0=1. \]
Therefore the system is inconsistent.
SymPy can also solve systems symbolically.
x1, x2, x3 = sp.symbols("x1 x2 x3")
solution = sp.linsolve([
x1 + 2*x2 + 3*x3,
2*x1 + 4*x2 + 6*x3
], (x1, x2, x3))
solution\(\displaystyle \left\{\left( - 2 x_{2} - 3 x_{3}, \ x_{2}, \ x_{3}\right)\right\}\)
This returns a parametric description of the solution set.
Row reduction simplifies a system without changing its solution set.
In this sense, rref is a kind of information compression. It removes redundant equations and reveals:
In data and AI, many matrices contain redundant or nearly redundant information. Row reduction is the exact algebraic version of revealing independent structure.
Later, rank, QR factorization, and SVD will give more numerically stable and geometrically meaningful versions of this idea.
A pivot must be nonzero. If the entry is zero, swap with a lower row if possible.
You may multiply a row by a nonzero scalar only.
In an augmented matrix, row operations must be applied to both the coefficient side and the right-hand side.
A variable without a pivot is free. Do not set it to zero unless the problem asks for one particular solution.
A row of all zeros means no new information.
A row with zeros on the left and a nonzero number on the right means contradiction.
Row operations change the appearance of the system, not its solution set.
Elementary row operations are:
These operations preserve the solution set of a linear system.
Row-echelon form creates a staircase pattern of pivots.
Reduced row-echelon form makes each pivot equal to \(1\) and clears all other entries in pivot columns.
Pivots identify pivot variables.
Non-pivot variable columns identify free variables.
A system has:
For homogeneous systems \(Ax=0\), there is always at least the zero solution.
For each operation, state what it does.
Which of these operations are allowed elementary row operations?
Determine whether each matrix is in row-echelon form, reduced row-echelon form, both, or neither.
\[ \begin{bmatrix} 1&2&3\\ 0&1&4\\ 0&0&5 \end{bmatrix} \]
\[ \begin{bmatrix} 1&0&2\\ 0&1&-1\\ 0&0&0 \end{bmatrix} \]
\[ \begin{bmatrix} 0&1&2\\ 1&0&3\\ 0&0&0 \end{bmatrix} \]
\[ \begin{bmatrix} 1&0&0&2\\ 0&1&0&-1\\ 0&0&1&3 \end{bmatrix} \]
Solve each system by row reduction. Write your answer clearly.
\[ \begin{cases} x+y=5,\\ 2x-y=1. \end{cases} \]
\[ \begin{cases} x_1-3x_2-5x_3=1,\\ x_1-x_2-2x_3=0,\\ 3x_1-x_2+x_3=3. \end{cases} \]
\[ \begin{cases} x_1+x_2+x_3=2,\\ x_1-x_2+2x_3=1,\\ 2x_1+x_2+3x_3=4. \end{cases} \]
\[ \begin{cases} x_1+2x_2+3x_3=1,\\ 2x_1+4x_2+6x_3=2,\\ 3x_1+6x_2+9x_3=4. \end{cases} \]
For each reduced augmented matrix, write the solution set in parametric vector form.
\[ \left[ \begin{array}{ccc|c} 1&0&2&3\\ 0&1&-1&4\\ 0&0&0&0 \end{array} \right] \]
\[ \left[ \begin{array}{cccc|c} 1&2&0&-1&5\\ 0&0&1&3&2\\ 0&0&0&0&0 \end{array} \right] \]
\[ \left[ \begin{array}{ccccc|c} 1&2&0&0&3&2\\ 0&0&1&0&-1&4\\ 0&0&0&1&-2&3 \end{array} \right] \]
\[ \left[ \begin{array}{ccc|c} 1&0&0&2\\ 0&1&0&-1\\ 0&0&1&3 \end{array} \right] \]
For each rref matrix, determine whether the corresponding system is consistent. If consistent, say whether it has one solution or infinitely many.
\[ \left[ \begin{array}{ccc|c} 1&0&2&3\\ 0&1&-1&4\\ 0&0&0&0 \end{array} \right] \]
\[ \left[ \begin{array}{ccc|c} 1&0&2&3\\ 0&1&-1&4\\ 0&0&0&1 \end{array} \right] \]
\[ \left[ \begin{array}{ccc|c} 1&0&0&3\\ 0&1&0&4\\ 0&0&1&5 \end{array} \right] \]
\[ \left[ \begin{array}{cccc|c} 1&2&0&3&1\\ 0&0&1&-1&2\\ 0&0&0&0&0 \end{array} \right] \]
Explain why a homogeneous system can never be inconsistent.
Solve
\[ \begin{cases} x_1+2x_2+3x_3=0,\\ 2x_1+4x_2+6x_3=0. \end{cases} \]
\[ \begin{cases} x_1+x_2+x_3=0,\\ x_1-x_2+x_3=0,\\ 2x_1+x_3=0. \end{cases} \]
\[ \left[ \begin{array}{ccc|c} 1&-3&-5&1\\ 1&-1&-2&0\\ 3&-1&1&3 \end{array} \right]. \]
\[ \left[ \begin{array}{ccc|c} 1&1&1&2\\ 1&-1&2&1\\ 2&1&3&4 \end{array} \right]. \]
\[ \begin{cases} x+y+z=1,\\ 2x+2y+2z=3 \end{cases} \]
is consistent.
Use np.linalg.solve to solve a square system with a unique solution. Then check your answer by computing \(Ax-b\).
Create a matrix with one row that is a multiple of another row. Compute its rref and explain the zero row.
Row reduction is one of the first examples of an algorithm that reveals hidden structure in data. It identifies redundancy, detects inconsistency, and separates determined variables from free variables. In modern computation, large data matrices often contain redundant information. Later algorithms such as QR factorization, least squares, and SVD extend this same idea in more stable and scalable ways.