Elementary Operations
To solve a system we transform it, step by step, into a simpler system with the same solutions. Only three kinds of steps are needed, the elementary row operations, and each one can be undone. That reversibility is the reason the solutions never change.
The three operations
On a system of equations (or on the rows of its augmented matrix) we may:
| Operation | Notation | Undo it with |
|---|---|---|
| 1. Swap two rows | the same swap | |
| 2. Scale a row by a non-zero number | ||
| 3. Add a multiple of one row to another () |
Example with the system , :
The last matrix says and , so . Check in the original: ✓ and ✓.
Why the solutions do not change
Suppose satisfies every equation of the old system. After a swap the equations are the same list in a new order. After scaling, still holds. After , the new equation is a sum of two true equations, so it is true. Hence every old solution is a new solution.
Because each operation has an inverse operation of the same kind, the same argument run backwards shows that every new solution is an old solution. The two systems are equivalent.
Note: Scaling by is forbidden: it turns an equation into and throws information away, and it cannot be undone. Likewise (adding a multiple of a row to itself) is just scaling by , which is 0 when .
Row operations in code
We store a matrix as a list of rows of Fractions so that every step is exact. Each operation returns a new matrix and leaves the input alone, which makes it easy to print every stage:
from fractions import Fraction as F
def swap(M, i, j):
M = [row[:] for row in M]
M[i], M[j] = M[j], M[i]
return M
def scale(M, i, c):
if c == 0:
raise ValueError("scaling by 0 is not an elementary operation")
M = [row[:] for row in M]
M[i] = [c * a for a in M[i]]
return M
def add_multiple(M, i, j, c): # R_i <- R_i + c * R_j
assert i != j
M = [row[:] for row in M]
M[i] = [a + c * b for a, b in zip(M[i], M[j])]
return M
def show(M):
for row in M:
print(" ".join(f"{str(a):>5}" for a in row))
print()
M = [[F(2), F(1), F(0)], [F(1), F(3), F(5)]]
show(M)
M = swap(M, 0, 1); show(M)
M = add_multiple(M, 1, 0, -2); show(M)
M = scale(M, 1, F(-1, 5)); show(M) 2 1 0
1 3 5
1 3 5
2 1 0
1 3 5
0 -5 -10
1 3 5
0 1 2
Rows are numbered from 0 in Python, from 1 on paper. These three helpers are all you need to write Gaussian elimination yourself.
The same in SymPy
SymPy matrices have built-in row operations: M.elementary_row_op(op, row, k, row2) with op one of "n<->m" (swap), "n->kn" (scale) and "n->n+km" (add a multiple).
from sympy import Matrix, Rational
M = Matrix([[2, 1, 0], [1, 3, 5]])
M = M.elementary_row_op("n<->m", row1=0, row2=1)
M = M.elementary_row_op("n->n+km", row=1, k=-2, row2=0)
M = M.elementary_row_op("n->kn", row=1, k=Rational(-1, 5))
print(M.tolist())[[1, 3, 5], [0, 1, 2]]
In software
Each elementary operation equals multiplication on the left by a simple elementary matrix (see Elementary Matrices). Numerical libraries record the sequence of operations instead of redoing them: an LU factorization stores the "add a multiple" steps in and the row swaps in a permutation, so that later systems with the same but new right-hand sides are solved almost for free. That is how physics engines re-solve the same contact matrix every frame.
Exercises
Exercise 1: Name the operation
Which elementary operation turns into ?
Answer
.
Solution
Exercise 2: Undo it
You applied and then . Which operations, in which order, restore the original matrix?
Answer
First , then .
Solution
Undo the last operation first (like taking off socks and shoes). The swap is its own inverse; then the added row is in position 3 again and subtracting removes what was added. Row 1 was never changed, so it is still the right row to subtract.
Exercise 3: Is it allowed?
Is "replace by and at the same time replace by " a valid step? Test it on , .
Answer
No. It produces two identical equations , losing information: the new system has infinitely many solutions, the old one only .
Solution
Elementary operations change one row at a time, using rows that are not changed in that step.
Exercise 4: Row reduce with the helpers (code)
Using swap, scale and add_multiple from the lesson, reduce to .
Answer
One possible sequence: , , , . The solution is , .
Solution
from fractions import Fraction as F
def swap(M, i, j):
M = [r[:] for r in M]; M[i], M[j] = M[j], M[i]; return M
def scale(M, i, c):
M = [r[:] for r in M]; M[i] = [c * a for a in M[i]]; return M
def add_multiple(M, i, j, c):
M = [r[:] for r in M]; M[i] = [a + c * b for a, b in zip(M[i], M[j])]; return M
M = [[F(0), F(2), F(4)], [F(3), F(6), F(3)]]
M = swap(M, 0, 1)
M = scale(M, 0, F(1, 3))
M = scale(M, 1, F(1, 2))
M = add_multiple(M, 0, 1, -2)
print([[str(a) for a in row] for row in M])[['1', '0', '-3'], ['0', '1', '2']]
Test yourself
Which of these is NOT an elementary row operation?
- Swap rows 1 and 3
- Multiply row 2 by −4
- Multiply row 2 by 0
- Add 5 times row 1 to row 3
Why do elementary row operations never change the solution set?
- Because they keep the determinant equal to 1
- Because every solution of the old system solves the new one and each operation can be reversed
- Because they only work on square matrices
- They do change it, but only slightly
What is the inverse of the operation ?