Mode of #cas
matrixRank
The rank of a matrix of any shape with rational entries, with a theorem about Mathlib’s Matrix.rank.
#cas [matrixRank] AA.rank = r.The rank of a matrix is the number of its independent rows, which is also the number of its independent columns. You can use matrixRank to check that some vectors are independent, or to count how many conditions in a linear system are really independent. The theorem is about Mathlib’s Matrix.rank over , and the interesting part is how it is proved. A rank is an equality, so it needs a bound from each side, and the kernel checks one certificate for each. A factorization through columns bounds the rank from above, and an submatrix with nonzero determinant bounds it from below.
Examples (13)#
Basic Examples (2)
The third row of this matrix is twice the second minus the first, so the rank is 2:
#cas [matrixRank] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
rank
2- Domain
ℚ^(3×3)- Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1, 2, 3; 4, 5, 6; 7, 8, 9].rank = 2
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1, 2, 3; 4, 5, 6; 7, 8, 9].rank = 2
The second row is twice the first:
#cas [matrixRank] !![1, 2, 3, 4; 2, 4, 6, 8]
rank
1- Domain
ℚ^(2×4)- Input (matrix)
!![1, 2, 3, 4; 2, 4, 6, 8]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1, 2, 3, 4; 2, 4, 6, 8].rank = 1
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1, 2, 3, 4; 2, 4, 6, 8].rank = 1
Scope (3)
The zero matrix has rank 0:
#cas [matrixRank] !![0, 0; 0, 0]
rank
0- Domain
ℚ^(2×2)- Input (matrix)
!![0, 0; 0, 0]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![0, 0; 0, 0].rank = 0
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![0, 0; 0, 0].rank = 0
Entries can be fractions. Here the second row is 6 times the first:
#cas [matrixRank] !![1/2, 1/3; 3, 2]
rank
1- Domain
ℚ^(2×2)- Input (matrix)
!![1/2, 1/3; 3, 2]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1 / 2, 1 / 3; 3, 2].rank = 1
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1 / 2, 1 / 3; 3, 2].rank = 1
A column, written as a list of rows:
#cas [matrixRank] [[1], [2], [3]]
rank
1- Domain
ℚ^(3×1)- Input (matrix)
!![1; 2; 3]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1; 2; 3].rank = 1
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1; 2; 3].rank = 1
Applications (2)
The rank as a theorem. Only the three standard axioms are used:
theorem rank_three : Matrix.rank !![(1 : ℚ), 2, 3; 4, 5, 6; 7, 8, 9] = 2 := by cas #print axioms rank_three
rank_three depends on the axioms propext, Classical.choice, Quot.sound.
Three vectors in are independent when the matrix with these rows has rank 3:
#cas [matrixRank] !![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0]
rank
3- Domain
ℚ^(3×4)- Input (matrix)
!![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0].rank = 3
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0].rank = 3
Properties and Relations (3)
The rank and the dimension of the null space add up to the number of columns, here :
#cas [matrixRank] !![1, 2, 3; 2, 4, 6]
rank
1- Domain
ℚ^(2×3)- Input (matrix)
!![1, 2, 3; 2, 4, 6]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1, 2, 3; 2, 4, 6].rank = 1
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1, 2, 3; 2, 4, 6].rank = 1
#cas [nullSpace] !![1, 2, 3; 2, 4, 6]
basis of the null space, dimension 2
[![-2, 1, 0], ![-3, 0, 1]]- Domain
ℚ^(2×3)- Input (matrix)
!![1, 2, 3; 2, 4, 6]
Checked by Lean’s kernel: Basis of the null space
(∀ (i : Fin (Nat.succ 1)), !![1, 2, 3; 2, 4, 6].mulVec (![![-2, 1, 0], ![-3, 0, 1]] i) = 0) ∧ LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧ ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 2, 4, 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i
The certificate: 1 checked step
Basis of the null space
(∀ (i : Fin (Nat.succ 1)), !![1, 2, 3; 2, 4, 6].mulVec (![![-2, 1, 0], ![-3, 0, 1]] i) = 0) ∧ LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧ ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 2, 4, 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i
A system has a solution exactly when adding as a column keeps the rank. Here the rank goes up from 1 to 2, and linearSolve proves that there is no solution:
#cas [matrixRank] !![1, 2, 3, 1; 2, 4, 6, 3]
rank
2- Domain
ℚ^(2×4)- Input (matrix)
!![1, 2, 3, 1; 2, 4, 6, 3]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![1, 2, 3, 1; 2, 4, 6, 3].rank = 2
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![1, 2, 3, 1; 2, 4, 6, 3].rank = 2
#cas [linearSolve] (!![1, 2, 3; 2, 4, 6], ![1, 3])
all solutions
no solution- Domain
ℚ^(2×3)- Input (linear system)
!![1, 2, 3; 2, 4, 6] . x = ![1, 3]
Checked by Lean’s kernel: Solution set
¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]
The certificate: 1 checked step
Solution set
¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]
A square matrix below full rank has determinant 0:
#cas [det] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
determinant
0- Domain
ℚ^(3×3)- Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]
Checked by Lean’s kernel: Determinant
!![1, 2, 3; 4, 5, 6; 7, 8, 9].det = 0
The certificate: 1 checked step
Determinant
!![1, 2, 3; 4, 5, 6; 7, 8, 9].det = 0
Possible Issues (2)
A vector is not a matrix. Write it as a row !![1, 2, 3] or a column !![1; 2; 3]:
#cas [matrixRank] ![1, 2, 3]
Error
expected a matrix `!![a, b; c, d]` or `[[a, b], [c, d]]`
cas refuses a wrong rank:
example : Matrix.rank !![(1 : ℚ), 2; 2, 4] = 2 := by cas
Error
cas: the rank is 1, not 2
Neat Examples (1)
The matrix with entries has rank at most 3, because is a sum of three matrices of rank 1. The report proves that the rank is 3:
#cas [matrixRank] !![4, 9, 16, 25, 36; 9, 16, 25, 36, 49; 16, 25, 36, 49, 64; 25, 36, 49, 64, 81; 36, 49, 64, 81, 100]
rank
3- Domain
ℚ^(5×5)- Input (matrix)
!![4, 9, 16, 25, 36; 9, 16, 25, 36, 49; 16, 25, 36, 49, 64; 25, 36, 49, 64, 81; 36, 49, 64, 81, 100]
Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor
!![4, 9, 16, 25, 36; 9, 16, 25, 36, 49; 16, 25, 36, 49, 64; 25, 36, 49, 64, 81; 36, 49, 64, 81, 100].rank = 3
The certificate: 1 checked step
Rank from a factorization and a nonsingular minor
!![4, 9, 16, 25, 36; 9, 16, 25, 36, 49; 16, 25, 36, 49, 64; 25, 36, 49, 64, 81; 36, 49, 64, 81, 100].rank = 3
See Also
rowReduce · nullSpace · det · linearSolve
