TExcelTAD
Home › Linear Algebra & Matrix Theory › Elementary Operations

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 Ri↔RjR_i \leftrightarrow R_j the same swap
2. Scale a row by a non-zero number cc Ri←c⋅RiR_i \leftarrow c \cdot R_i Ri←1c⋅RiR_i \leftarrow \frac{1}{c} \cdot R_i
3. Add a multiple of one row to another (i≠ji \ne j) Ri←Ri+c⋅RjR_i \leftarrow R_i + c \cdot R_j Ri←Ri−c⋅RjR_i \leftarrow R_i - c \cdot R_j

Example with the system x+3y=5x + 3y = 5, 2x+y=02x + y = 0:

[135210]→R2←R2−2R1[1350−5−10]→R2←(−15)R2[135012]\left[\begin{array}{rr|r} 1 & 3 & 5 \\ 2 & 1 & 0 \end{array}\right] \xrightarrow{R_2 \leftarrow R_2 - 2R_1} \left[\begin{array}{rr|r} 1 & 3 & 5 \\ 0 & -5 & -10 \end{array}\right] \xrightarrow{R_2 \leftarrow (-\frac{1}{5})R_2} \left[\begin{array}{rr|r} 1 & 3 & 5 \\ 0 & 1 & 2 \end{array}\right]

The last matrix says x+3y=5x + 3y = 5 and y=2y = 2, so x=5−6=−1x = 5 - 6 = -1. Check in the original: −1+6=5-1 + 6 = 5 ✓ and −2+2=0-2 + 2 = 0 ✓.

Why the solutions do not change

Suppose (x,y)(x, y) satisfies every equation of the old system. After a swap the equations are the same list in a new order. After scaling, c⋅(left side)=c⋅(right side)c \cdot (\text{left side}) = c \cdot (\text{right side}) still holds. After Ri←Ri+c⋅RjR_i \leftarrow R_i + c \cdot R_j, 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 c=0c = 0 is forbidden: it turns an equation into 0=00 = 0 and throws information away, and it cannot be undone. Likewise Ri←Ri+c⋅RiR_i \leftarrow R_i + c \cdot R_i (adding a multiple of a row to itself) is just scaling by 1+c1 + c, which is 0 when c=−1c = -1.

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:

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

python · try it yourself
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 LL and the row swaps in a permutation, so that later systems with the same AA 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 [123456]\left[\begin{array}{rr|r} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}\right] into [1230−3−6]\left[\begin{array}{rr|r} 1 & 2 & 3 \\ 0 & -3 & -6 \end{array}\right]?

Answer

R2←R2−4R1R_2 \leftarrow R_2 - 4R_1.

Solution
R2−4R1=(4−4, 5−8, 6−12)=(0,−3,−6)✓R_2 - 4R_1 = (4 - 4,\ 5 - 8,\ 6 - 12) = (0, -3, -6) \quad \checkmark

Exercise 2: Undo it

You applied R3←R3+7R1R_3 \leftarrow R_3 + 7R_1 and then R2↔R3R_2 \leftrightarrow R_3. Which operations, in which order, restore the original matrix?

Answer

First R2↔R3R_2 \leftrightarrow R_3, then R3←R3−7R1R_3 \leftarrow R_3 - 7R_1.

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 7R17R_1 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 R1R_1 by R1+R2R_1 + R_2 and at the same time replace R2R_2 by R1+R2R_1 + R_2" a valid step? Test it on x+y=1x + y = 1, x−y=3x - y = 3.

Answer

No. It produces two identical equations 2x=42x = 4, losing information: the new system has infinitely many solutions, the old one only (2,−1)(2, -1).

Solution
R1+R2: 2x+0y=4R1+R2: 2x+0y=4(both rows become the same)New solution set: x=2, y anything.Old: (2,−1) only.\begin{aligned} &R_1 + R_2\text{: } 2x + 0y = 4 \\ &R_1 + R_2\text{: } 2x + 0y = 4 && \text{(both rows become the same)} \\ &\text{New solution set: } x = 2,\ y \text{ anything.} \qquad \text{Old: } (2, -1) \text{ only.} \end{aligned}

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 [024363]\left[\begin{array}{rr|r} 0 & 2 & 4 \\ 3 & 6 & 3 \end{array}\right] to [10−3012]\left[\begin{array}{rr|r} 1 & 0 & -3 \\ 0 & 1 & 2 \end{array}\right].

Answer

One possible sequence: R1↔R2R_1 \leftrightarrow R_2, R1←13R1R_1 \leftarrow \frac{1}{3}R_1, R2←12R2R_2 \leftarrow \frac{1}{2}R_2, R1←R1−2R2R_1 \leftarrow R_1 - 2R_2. The solution is x=−3x = -3, y=2y = 2.

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

  1. Swap rows 1 and 3
  2. Multiply row 2 by −4
  3. Multiply row 2 by 0
  4. Add 5 times row 1 to row 3

Why do elementary row operations never change the solution set?

  1. Because they keep the determinant equal to 1
  2. Because every solution of the old system solves the new one and each operation can be reversed
  3. Because they only work on square matrices
  4. They do change it, but only slightly

What is the inverse of the operation R2←R2+3R1R_2 \leftarrow R_2 + 3R_1?

  1. R2←R2−3R1R_2 \leftarrow R_2 - 3R_1
  2. R1←R1−3R2R_1 \leftarrow R_1 - 3R_2
  3. R2←13R2R_2 \leftarrow \frac{1}{3}R_2
  4. R1↔R2R_1 \leftrightarrow R_2