Mode of #cas
rowReduce
The reduced row echelon form of a matrix with rational entries, with the row operations that prove it.
#cas [rowReduce] AR of the matrix A with rational entries and its pivot columns, and proves E * A = R ∧ E.det ≠ 0 ∧ R.IsReducedRowEchelon.I like row reduction because the reduced form shows so much at once: the pivots, the free columns and the relations between the columns. rowReduce gives you that form, with a theorem about Mathlib’s Matrix.IsReducedRowEchelon. The theorem gives an invertible matrix of row operations with , and since the reduced form of a matrix is unique, that pins down.
Examples (11)#
Basic Examples (2)
The reduced form of a singular matrix. The pivots are in the first two columns, and the last row is zero:
#cas [rowReduce] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
reduced row echelon form, pivot columns [0, 1]
!![1, 0, -1; 0, 1, 2; 0, 0, 0]- Domain
ℚ^(3×3)- Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]
Checked by Lean’s kernel: Row operations E with E * A = R
!![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] = !![1, 0, -1; 0, 1, 2; 0, 0, 0] ∧ !![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1].det ≠ 0 ∧ !![1, 0, -1; 0, 1, 2; 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] = !![1, 0, -1; 0, 1, 2; 0, 0, 0] ∧ !![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1].det ≠ 0 ∧ !![1, 0, -1; 0, 1, 2; 0, 0, 0].IsReducedRowEchelon
The first entry is 0, so the elimination swaps two rows, and records the swap:
#cas [rowReduce] !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3]
reduced row echelon form, pivot columns [0, 1]
!![1, 0, -1, 1/2; 0, 1, 2, 1/2; 0, 0, 0, 0]- Domain
ℚ^(3×4)- Input (matrix)
!![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3]
Checked by Lean’s kernel: Row operations E with E * A = R
!![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1] * !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3] = !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0] ∧ !![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1].det ≠ 0 ∧ !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1] * !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3] = !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0] ∧ !![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1].det ≠ 0 ∧ !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0].IsReducedRowEchelon
Scope (2)
A row whose first entry is 1 is already reduced:
#cas [rowReduce] !![1, 2, 3]
reduced row echelon form, pivot columns [0]
!![1, 2, 3]- Domain
ℚ^(1×3)- Input (matrix)
!![1, 2, 3]
Checked by Lean’s kernel: Row operations E with E * A = R
!![1] * !![1, 2, 3] = !![1, 2, 3] ∧ !![1].det ≠ 0 ∧ !![1, 2, 3].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![1] * !![1, 2, 3] = !![1, 2, 3] ∧ !![1].det ≠ 0 ∧ !![1, 2, 3].IsReducedRowEchelon
An invertible matrix reduces to the identity. A list of rows works as well as a matrix literal:
#cas [rowReduce] [[2, 4], [1, 3]]
reduced row echelon form, pivot columns [0, 1]
!![1, 0; 0, 1]- Domain
ℚ^(2×2)- Input (matrix)
!![2, 4; 1, 3]
Checked by Lean’s kernel: Row operations E with E * A = R
!![3 / 2, -2; -(1 / 2), 1] * !![2, 4; 1, 3] = !![1, 0; 0, 1] ∧ !![3 / 2, -2; -(1 / 2), 1].det ≠ 0 ∧ !![1, 0; 0, 1].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![3 / 2, -2; -(1 / 2), 1] * !![2, 4; 1, 3] = !![1, 0; 0, 1] ∧ !![3 / 2, -2; -(1 / 2), 1].det ≠ 0 ∧ !![1, 0; 0, 1].IsReducedRowEchelon
Applications (3)
The theorem of the first report contains its matrix . Kernel evaluation, the proof behind the report, checks the product again, and only the three standard axioms are used:
theorem rowReduce_certificate : !![(-(5 / 3) : ℚ), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] = !![1, 0, -1; 0, 1, 2; 0, 0, 0] := by decide +kernel #print axioms rowReduce_certificate
rowReduce_certificate depends on the axioms propext, Classical.choice, Quot.sound.
The system , as an augmented matrix. The last column of the reduced form is the solution , , which linearSolve finds too:
#cas [rowReduce] !![1, 2, 5; 3, 4, 6]
reduced row echelon form, pivot columns [0, 1]
!![1, 0, -4; 0, 1, 9/2]- Domain
ℚ^(2×3)- Input (matrix)
!![1, 2, 5; 3, 4, 6]
Checked by Lean’s kernel: Row operations E with E * A = R
!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 5; 3, 4, 6] = !![1, 0, -4; 0, 1, 9 / 2] ∧ !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -4; 0, 1, 9 / 2].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 5; 3, 4, 6] = !![1, 0, -4; 0, 1, 9 / 2] ∧ !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -4; 0, 1, 9 / 2].IsReducedRowEchelon
#cas [linearSolve] (!![1, 2; 3, 4], ![5, 6])
all solutions
![-4, 9/2]- Domain
ℚ^(2×2)- Input (linear system)
!![1, 2; 3, 4] . x = ![5, 6]
Checked by Lean’s kernel: Solution set
∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]
The certificate: 1 checked step
Solution set
∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]
For an invertible , reducing gives . The right half is the inverse that inverse prints:
#cas [rowReduce] !![1, 2, 1, 0; 3, 4, 0, 1]
reduced row echelon form, pivot columns [0, 1]
!![1, 0, -2, 1; 0, 1, 3/2, -1/2]- Domain
ℚ^(2×4)- Input (matrix)
!![1, 2, 1, 0; 3, 4, 0, 1]
Checked by Lean’s kernel: Row operations E with E * A = R
!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 1, 0; 3, 4, 0, 1] = !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)] ∧ !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 1, 0; 3, 4, 0, 1] = !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)] ∧ !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)].IsReducedRowEchelon
#cas [inverse] !![1, 2; 3, 4]
inverse
!![-2, 1; 3/2, -1/2]- Domain
ℚ^(2×2)- Input (matrix)
!![1, 2; 3, 4]
Checked by Lean’s kernel: Inverse from the product B * A = 1
!![1, 2; 3, 4]⁻¹ = !![-2, 1; 3 / 2, -(1 / 2)]
The certificate: 1 checked step
Inverse from the product B * A = 1
!![1, 2; 3, 4]⁻¹ = !![-2, 1; 3 / 2, -(1 / 2)]
Properties and Relations (2)
The number of pivot columns is the rank. The second example has two:
#cas [matrixRank] !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3]
rank
2- Domain
ℚ^(3×4)- Input (matrix)
!![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3].rank = 2
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3].rank = 2
Each column without a pivot gives a vector of the null space. For the first example the reduced form says and :
#cas [nullSpace] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
basis of the null space, dimension 1
[![1, -2, 1]]- Domain
ℚ^(3×3)- Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]
Checked by Lean’s kernel: Basis of the null space
(∀ (i : Fin (Nat.succ 0)), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec (![![1, -2, 1]] i) = 0) ∧ LinearIndependent ℚ ![![1, -2, 1]] ∧ ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, -2, 1]] i
The certificate: 1 checked step
Basis of the null space
(∀ (i : Fin (Nat.succ 0)), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec (![![1, -2, 1]] i) = 0) ∧ LinearIndependent ℚ ![![1, -2, 1]] ∧ ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, -2, 1]] i
Possible Issues (1)
The mode takes one matrix, not a system. Reduce the augmented matrix as above, or use linearSolve:
#cas [rowReduce] (!![1, 2; 3, 4], ![5, 6])
Error
expected a matrix `!![a, b; c, d]` or `[[a, b], [c, d]]`
Neat Examples (1)
Dürer’s magic square from Melencolia I is singular. The reduced form says that its last column is :
#cas [rowReduce] !![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1]
reduced row echelon form, pivot columns [0, 1, 2]
!![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0]- Domain
ℚ^(4×4)- Input (matrix)
!![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1]
Checked by Lean’s kernel: Row operations E with E * A = R
!![1 / 34, -(9 / 136), 13 / 136, 0; 8 / 17, 47 / 68, -(83 / 68), 0; -(15 / 34), -(69 / 136), 145 / 136, 0; -1, -3, 3, 1] * !![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1] = !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0] ∧ !![1 / 34, -(9 / 136), 13 / 136, 0; 8 / 17, 47 / 68, -(83 / 68), 0; -(15 / 34), -(69 / 136), 145 / 136, 0; -1, -3, 3, 1].det ≠ 0 ∧ !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
Row operations E with E * A = R
!![1 / 34, -(9 / 136), 13 / 136, 0; 8 / 17, 47 / 68, -(83 / 68), 0; -(15 / 34), -(69 / 136), 145 / 136, 0; -1, -3, 3, 1] * !![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1] = !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0] ∧ !![1 / 34, -(9 / 136), 13 / 136, 0; 8 / 17, 47 / 68, -(83 / 68), 0; -(15 / 34), -(69 / 136), 145 / 136, 0; -1, -3, 3, 1].det ≠ 0 ∧ !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0].IsReducedRowEchelon
See Also
matrixRank · nullSpace · linearSolve · inverse · det
