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] A
gives the rank of the matrix A with rational entries, and proves A.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 A=CF through r columns bounds the rank from above, and an r×r submatrix with nonzero determinant bounds it from below.

Details and Options

  • A is a matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]] of any shape. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • The theorem is A.rank = r for Mathlib’s Matrix.rank over ℚ. The columns of C are the pivot columns of A, and the kernel checks A=CF by evaluation, which gives the upper bound (Matrix.rank_mul_le_right). The rows of the minor come from a second elimination, and its nonzero determinant gives the lower bound (Matrix.rank_of_det_ne_zero).
  • The tactic cas proves goals Matrix.rank A = r for matrix literals over ℚ, and refuses a wrong r.
  • There are no options.

Examples (13)#

Basic Examples (2)

The third row of this matrix is twice the second minus the first, so the rank is 2:

In[1]:=
#cas [matrixRank] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
Out[1]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1, 2, 3; 4, 5, 6; 7, 8, 9].rank = 2

    Checked by Lean’s kernel


The second row is twice the first:

In[2]:=
#cas [matrixRank] !![1, 2, 3, 4; 2, 4, 6, 8]
Out[2]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1, 2, 3, 4; 2, 4, 6, 8].rank = 1

    Checked by Lean’s kernel

Scope (3)

The zero matrix has rank 0:

In[3]:=
#cas [matrixRank] !![0, 0; 0, 0]
Out[3]=

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
  1. Rank from a factorization and a nonsingular minor

    !![0, 0; 0, 0].rank = 0

    Checked by Lean’s kernel


Entries can be fractions. Here the second row is 6 times the first:

In[4]:=
#cas [matrixRank] !![1/2, 1/3; 3, 2]
Out[4]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1 / 2, 1 / 3; 3, 2].rank = 1

    Checked by Lean’s kernel


A column, written as a list of rows:

In[5]:=
#cas [matrixRank] [[1], [2], [3]]
Out[5]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1; 2; 3].rank = 1

    Checked by Lean’s kernel

Applications (2)

The rank as a theorem. Only the three standard axioms are used:

In[6]:=
theorem rank_three : Matrix.rank !![(1 : ℚ), 2, 3; 4, 5, 6; 7, 8, 9] = 2 := by
  cas

#print axioms rank_three
Out[6]=

rank_three depends on the axioms propext, Classical.choice, Quot.sound.


Three vectors in ℚ4 are independent when the matrix with these rows has rank 3:

In[7]:=
#cas [matrixRank] !![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0]
Out[7]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1, 0, 1, 0; 0, 1, 0, 1; 1, 1, 0, 0].rank = 3

    Checked by Lean’s kernel

Properties and Relations (3)

The rank and the dimension of the null space add up to the number of columns, here 1+2=3:

In[8]:=
#cas [matrixRank] !![1, 2, 3; 2, 4, 6]
Out[8]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1, 2, 3; 2, 4, 6].rank = 1

    Checked by Lean’s kernel

In[9]:=
#cas [nullSpace] !![1, 2, 3; 2, 4, 6]
Out[9]=

basis of the null space, dimension 2

[(−210), (−301)]
[![-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
  1. 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

    Checked by Lean’s kernel


A system Ax=b has a solution exactly when adding b as a column keeps the rank. Here the rank goes up from 1 to 2, and linearSolve proves that there is no solution:

In[10]:=
#cas [matrixRank] !![1, 2, 3, 1; 2, 4, 6, 3]
Out[10]=

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
  1. Rank from a factorization and a nonsingular minor

    !![1, 2, 3, 1; 2, 4, 6, 3].rank = 2

    Checked by Lean’s kernel

In[11]:=
#cas [linearSolve] (!![1, 2, 3; 2, 4, 6], ![1, 3])
Out[11]=

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
  1. Solution set

    ¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]

    Checked by Lean’s kernel


A square matrix below full rank has determinant 0:

In[12]:=
#cas [det] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
Out[12]=

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
  1. Determinant

    !![1, 2, 3; 4, 5, 6; 7, 8, 9].det = 0

    Checked by Lean’s kernel

Possible Issues (2)

A vector is not a matrix. Write it as a row !![1, 2, 3] or a column !![1; 2; 3]:

In[13]:=
#cas [matrixRank] ![1, 2, 3]
Out[13]=

Error

expected a matrix `!![a, b; c, d]` or `[[a, b], [c, d]]`

cas refuses a wrong rank:

In[14]:=
example : Matrix.rank !![(1 : ℚ), 2; 2, 4] = 2 := by
  cas
Out[14]=

Error

cas: the rank is 1, not 2

Neat Examples (1)

The 5×5 matrix with entries (i+j)2 has rank at most 3, because (i+j)2=i2·1+2i·j+1·j2 is a sum of three matrices of rank 1. The report proves that the rank is 3:

In[15]:=
#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]
Out[15]=

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
  1. 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

    Checked by Lean’s kernel

See Also

rowReduce · nullSpace · det · linearSolve