Elementary Matrices
Every elementary row operation can be carried out by multiplying on the left by a special matrix, an elementary matrix. This turns row reduction, an algorithm, into algebra: products of matrices. It explains why the method finds inverses, and it is the seed of the LU factorisation used by every numerical library.
Three kinds
An elementary matrix is what you get by applying one elementary row operation to the identity matrix . There is one kind per row operation (Elementary Operations):
| Row operation | Elementary matrix ( example) | Inverse |
|---|---|---|
| Swap rows and | with rows 1 and 3 swapped: | itself |
| Multiply row by | with in position : | same with |
| Add to row | with in position : | same with |
Key fact: if is obtained from by a row operation, then for any matrix with the right number of rows, is the result of applying that same row operation to .
The reason is the row view of multiplication: row of is , a combination of the rows of with the weights in row of . For the "add to row 3" matrix, row 3 of is , so row 3 of is .
from sympy import Matrix, eye
A = Matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 10]])
E_swap = eye(3); E_swap.row_swap(0, 2)
E_scale = eye(3); E_scale[1, 1] = 5
E_add = eye(3); E_add[2, 0] = -7 # R3 <- R3 - 7 R1
print("swap R1,R3 :", (E_swap * A).tolist())
print("5 * R2 :", (E_scale * A).tolist())
print("R3 - 7 R1 :", (E_add * A).tolist())
print("E_add inverse:", E_add.inv().tolist())swap R1,R3 : [[7, 8, 10], [4, 5, 6], [1, 2, 3]]
5 * R2 : [[1, 2, 3], [20, 25, 30], [7, 8, 10]]
R3 - 7 R1 : [[1, 2, 3], [4, 5, 6], [0, -6, -11]]
E_add inverse: [[1, 0, 0], [0, 1, 0], [7, 0, 1]]
Multiplying on the right () does the corresponding operation on the columns of instead.
Elementary matrices are invertible
Every row operation can be undone by another row operation of the same type (swap back, divide by , subtract again). Hence every elementary matrix is invertible and its inverse is elementary, as the table shows.
Row reduction as a product
Row-reducing to its reduced echelon form with operations (in that order) means
If is invertible, , so . Applying the same operations to produces exactly that product, which is why works (Matrix Inverses). Taking inverses gives the
Theorem. A square matrix is invertible if and only if it is a product of elementary matrices:
from sympy import Matrix, Rational
A = Matrix([[2, 4], [3, 7]])
E1 = Matrix([[Rational(1, 2), 0], [0, 1]]) # R1 <- R1 / 2
E2 = Matrix([[1, 0], [-3, 1]]) # R2 <- R2 - 3 R1
E3 = Matrix([[1, -2], [0, 1]]) # R1 <- R1 - 2 R2
print("E3 E2 E1 A =", (E3 * E2 * E1 * A).tolist())
print("E3 E2 E1 == A^-1:", E3 * E2 * E1 == A.inv())
print("A == E1^-1 E2^-1 E3^-1:", A == E1.inv() * E2.inv() * E3.inv())E3 E2 E1 A = [[1, 0], [0, 1]]
E3 E2 E1 == A^-1: True
A == E1^-1 E2^-1 E3^-1: True
LU factorisation
If you reduce to an upper triangular using only "add a multiple of a higher row to a lower row" operations, every is lower triangular with 1s on the diagonal, and so is the product of their inverses, . The result is
With row swaps for numerical safety it becomes , where is a permutation matrix (a product of swap matrices). SciPy returns all three:
import numpy as np
from scipy.linalg import lu
A = np.array([[2.0, 1.0, 1.0],
[4.0, 3.0, 3.0],
[8.0, 7.0, 9.0]])
P, L, U = lu(A) # SciPy convention: A = P @ L @ U
print("L =", np.round(L, 4).tolist())
print("U =", np.round(U, 4).tolist())
print("A == P L U:", np.allclose(A, P @ L @ U))L = [[1.0, 0.0, 0.0], [0.25, 1.0, 0.0], [0.5, 0.6667, 1.0]]
U = [[8.0, 7.0, 9.0], [0.0, -0.75, -1.25], [0.0, 0.0, -0.6667]]
A == P L U: True
Solving then takes two cheap triangular solves (, then ), each instead of .
In software
- LU is how
solveworks.np.linalg.solve, MATLAB'sA\band LAPACK'sgesvcompute with partial pivoting (the chooses the largest available pivot for stability), then do two triangular solves. Factor once, solve many right-hand sides cheaply. - Nobody builds explicitly in code. Multiplying an matrix by a dense elementary matrix costs about operations, while the row operation itself touches one row: about operations. The matrices are a proof tool; the loops are the implementation.
- Permutation matrices reorder rows. In NumPy this is fancy indexing,
A[[2, 0, 1]], which is the same as multiplying by the matching permutation matrix but far cheaper.
Exercises
Exercise 1: Name the operation
Which row operation does perform, and what is ?
Answer
; .
Solution
differs from only in entry , so has row 3 replaced by . To undo it, add back, which is the same matrix with in position .
Exercise 2: Write the matrix
Write the elementary matrix that swaps rows 2 and 4, and check that it is its own inverse.
Answer
; .
Solution
Swap rows 2 and 4 of . Swapping twice restores the original order, so and .
from sympy import eye
E = eye(4); E.row_swap(1, 3)
print(E.tolist(), E * E == eye(4))[[1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0]] True
Exercise 3: Factor into elementary matrices
Write as a product of elementary matrices.
Answer
One answer: .
Solution
from sympy import Matrix
print(Matrix([[1, 0], [2, 1]]) * Matrix([[1, 0], [0, 2]]) * Matrix([[1, 3], [0, 1]]))Matrix([[1, 3], [2, 8]])
Exercise 4: Columns from the right
Compute where and . Which column operation happened?
Answer
: .
Solution
Test yourself
is with its rows 1 and 2 swapped. What is for a matrix ?
- with columns 1 and 2 swapped
- The transpose of
- unchanged
- with rows 1 and 2 swapped
What is the inverse of the elementary matrix that multiplies row 2 by 3?
- The matrix that multiplies row 2 by
- Itself
- The matrix that multiplies row 2 by
- It has no inverse
Which statement is true for an matrix ?
- Every product of elementary matrices is singular
- is invertible iff it is a product of elementary matrices
- Only diagonal matrices are products of elementary matrices
- Elementary matrices can be singular