The Matrix Method
The matrix method writes a system of equations as a grid of numbers and solves it by operating on the rows of that grid. Nothing new happens mathematically: it is elimination with the letters and plus signs left out. Dropping them makes the work shorter, easier to check and easy to hand to a computer.
From equations to a matrix
A matrix is a rectangular table of numbers. For a system, write each equation as one row: the coefficients of , and , then a bar, then the constant. The result is the augmented matrix:
A missing variable gets a 0 in its column, and every equation must have its variables in the same order.
The three row operations
These are the moves elimination already uses, written for rows. None of them changes the solutions:
| Operation | Notation | Equation meaning |
|---|---|---|
| swap two rows | write the equations in a different order | |
| multiply a row by a non-zero number | multiply both sides of an equation | |
| add a multiple of one row to another | add a multiple of one equation to another |
The goal is row echelon form: zeros below the diagonal, a staircase shape. Then back substitution finishes the job (see Three Unknowns by Substitution).
A worked example
Read the rows back as equations, bottom first:
Check in the original system: ✓, ✓, ✓.
If you keep going until every pivot is 1 and every other entry in the pivot columns is 0, you reach reduced row echelon form (RREF), and the last column is the answer. SymPy computes it exactly:
from sympy import Matrix
M = Matrix([[1, 2, 1, 0],
[2, 1, -1, 3],
[1, -1, 2, 7]])
R, pivots = M.rref()
for row in R.tolist():
print(row)
print("pivot columns:", pivots)[1, 0, 0, 3]
[0, 1, 0, -2]
[0, 0, 1, 1]
pivot columns: (0, 1, 2)
The rows now read , , . The pivots tuple lists the columns that hold a leading 1.
The matrix equation
You can also split the augmented matrix into a coefficient matrix , a column of unknowns and a column of constants :
If has an inverse (a matrix with , the identity), multiply both sides by it: . This is the matrix version of solving by multiplying by . For a matrix there is a short formula:
The number is the determinant from Solving with 2×2 Determinants. If it is 0, has no inverse and the system has no unique solution.
import numpy as np
A = np.array([[1, 2, 1], [2, 1, -1], [1, -1, 2]], dtype=float)
B = np.array([0, 3, 7], dtype=float)
print("solve: ", np.linalg.solve(A, B))
print("inverse · B:", np.round(np.linalg.inv(A) @ B, 10))
A2 = np.array([[4, 7], [2, 6]], dtype=float)
print("2x2 inverse:")
print(np.linalg.inv(A2) * 10) # ×10 to show the exact entriessolve: [ 3. -2. 1.]
inverse · B: [ 3. -2. 1.]
2x2 inverse:
[[ 6. -7.]
[-2. 4.]]
For , , so , which matches the printed values times 10.
Tip: In code, prefer
np.linalg.solve(A, B)tonp.linalg.inv(A) @ B. It does elimination directly, is faster, and loses less precision. Compute an inverse only when you need the matrix itself.
In software
- Every numeric library solves systems this way. NumPy, SciPy, MATLAB and Excel's
MINVERSE/MMULTall build on row operations (in the form of an LU decomposition). - Graphics. A 2D or 3D transform is a matrix; undoing it (mapping a mouse click on screen back to world coordinates) uses its inverse. See Matrix Inverses and the game-math lesson on inverse transforms.
- Machine learning. Fitting a linear regression solves a matrix equation for the weights.
The linear algebra course treats all of this in depth: Gaussian Elimination, Gauss–Jordan elimination and Matrix Inverses.
Exercises
Exercise 1: Write the augmented matrix
Write the augmented matrix of , , .
Answer
Solution
Put the variables in the order , , and write 0 for each missing one:
Exercise 2: Row reduce a 2×2
Solve , by row operations on the augmented matrix.
Answer
, .
Solution
Exercise 3: Row reduce a 3×3
Solve , , with the matrix method. Check with rref().
Answer
, , .
Solution
from sympy import Matrix
R, _ = Matrix([[1, 1, 1, 2], [1, 2, 3, 5], [2, 3, 5, 9]]).rref()
print(R.col(-1).T)Matrix([[1, -1, 2]])
Exercise 4: The 2×2 inverse
Find the inverse of with the formula and use it to solve , .
Answer
; , .
Solution
import numpy as np
A = np.array([[3, 1], [5, 2]])
print(np.round(np.linalg.inv(A)).astype(int).tolist(), np.round(np.linalg.solve(A, [4, 7]), 10).tolist())[[2, -1], [-5, 3]] [1.0, 1.0]
Test yourself
Which of these is NOT an allowed row operation?
- Swap two rows
- Multiply a row by 5
- Add 2 times row 1 to row 3
- Add 1 to every entry of a row
A matrix has determinant . What follows?
- Its inverse is the identity
- It has no inverse, and the system has no unique solution
- The solution is
- Its inverse has all entries 0
Why is np.linalg.solve(A, B) preferred over np.linalg.inv(A) @ B?
- inv does not exist in NumPy
- solve is faster and more accurate because it eliminates directly
- solve gives exact fractions
- They give different answers