Mode of #cas

det

The determinant of a square matrix with rational entries, with a theorem about Mathlib’s Matrix.det.

#cas [det] A
gives the determinant of the square matrix A with rational entries, and proves A.det = d for Mathlib’s Matrix.det.

det evaluates the determinant of a matrix over ℚ. You write the matrix as a Mathlib literal, the report prints the number, and the theorem under it is A.det = d for Mathlib’s Matrix.det over ℚ. Because it is a statement about the same matrix you would write in a proof, it can go straight into one.

Details and Options

  • A is a square matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]]. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • The theorem is A.det = d for A as a matrix over ℚ. Mathlib’s simplification procedure norm_det computes the determinant of the literal together with a proof, and Lean’s kernel checks that proof.
  • The tactic cas proves goals Matrix.det A = d for matrix literals, also over ℤ and ℝ, and proves a determinant with variables as a polynomial identity. A wrong value is refused, and the message shows the value it found.
  • There are no options.

Examples (14)#

Basic Examples (2)

The determinant of a 3×3 matrix:

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

determinant

-3
Domain
ℚ^(3×3)
Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 10]

Checked by Lean’s kernel: Determinant

!![1, 2, 3; 4, 5, 6; 7, 8, 10].det = -3
The certificate: 1 checked step
  1. Determinant

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

    Checked by Lean’s kernel


The entries can be fractions:

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

determinant

-1
Domain
ℚ^(2×2)
Input (matrix)
!![1/2, 1; 3, 4]

Checked by Lean’s kernel: Determinant

!![1 / 2, 1; 3, 4].det = -1
The certificate: 1 checked step
  1. Determinant

    !![1 / 2, 1; 3, 4].det = -1

    Checked by Lean’s kernel

Scope (3)

A 5×5 matrix, tridiagonal except for its two corners:

In[3]:=
#cas [det] !![2, -1, 0, 0, 1; -1, 2, -1, 0, 0; 0, -1, 2, -1, 0; 0, 0, -1, 2, -1; 1, 0, 0, -1, 2]
Out[3]=

determinant

4
Domain
ℚ^(5×5)
Input (matrix)
!![2, -1, 0, 0, 1; -1, 2, -1, 0, 0; 0, -1, 2, -1, 0; 0, 0, -1, 2, -1; 1, 0, 0, -1, 2]

Checked by Lean’s kernel: Determinant

!![2, -1, 0, 0, 1; -1, 2, -1, 0, 0; 0, -1, 2, -1, 0; 0, 0, -1, 2, -1; 1, 0, 0, -1, 2].det = 4
The certificate: 1 checked step
  1. Determinant

    !![2, -1, 0, 0, 1; -1, 2, -1, 0, 0; 0, -1, 2, -1, 0; 0, 0, -1, 2, -1; 1, 0, 0, -1, 2].det = 4

    Checked by Lean’s kernel


A list of rows works as well as a matrix literal:

In[4]:=
#cas [det] [[1, 2], [3, 4]]
Out[4]=

determinant

-2
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 3, 4]

Checked by Lean’s kernel: Determinant

!![1, 2; 3, 4].det = -2
The certificate: 1 checked step
  1. Determinant

    !![1, 2; 3, 4].det = -2

    Checked by Lean’s kernel


The smallest case, a 1×1 matrix:

In[5]:=
#cas [det] !![0]
Out[5]=

determinant

0
Domain
ℚ^(1×1)
Input (matrix)
!![0]

Checked by Lean’s kernel: Determinant

!![0].det = 0
The certificate: 1 checked step
  1. Determinant

    !![0].det = 0

    Checked by Lean’s kernel

Applications (3)

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

In[6]:=
theorem det_three : Matrix.det !![(1 : ℚ), 2, 3; 4, 5, 6; 7, 8, 10] = -3 := by
  cas

#print axioms det_three
Out[6]=

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


In a proof, cas also evaluates a determinant with variables. The Vandermonde determinant becomes a polynomial identity:

In[7]:=
example (a b c : ℝ) :
    Matrix.det !![1, a, a ^ 2; 1, b, b ^ 2; 1, c, c ^ 2] = (b - a) * (c - a) * (c - b) := by
  cas
Out[7]=

Lean accepts the proof.


A 5×5 determinant over the integers:

In[8]:=
example : Matrix.det !![(-1 : ℤ), -1, -2, 3, 0; -2, 4, 0, -5, 1; -4, 1, -2, -1, 0;
    -1, 4, -1, 4, 0; -5, 4, 1, -5, -3] = 228 := by
  cas
Out[8]=

Lean accepts the proof.

Properties and Relations (2)

The constant term of the characteristic polynomial det(xI−A) is (−1)ndetA. For the first matrix on this page that is (−1)3·(−3)=3:

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

det (x I - A)

x3−16x2−12x+3
x^3 - 16*x^2 - 12*x + 3
Domain
ℚ^(3×3)
Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 10]

Checked by Lean’s kernel: Characteristic polynomial in ℚ[X]

!![1, 2, 3; 4, 5, 6; 7, 8, 10].charpoly =
  Polynomial.X ^ 3 - 16 * Polynomial.X ^ 2 - 12 * Polynomial.X + 3
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![1, 2, 3; 4, 5, 6; 7, 8, 10].charpoly =
      Polynomial.X ^ 3 - 16 * Polynomial.X ^ 2 - 12 * Polynomial.X + 3

    Checked by Lean’s kernel


A matrix with determinant 0 has no inverse, and inverse proves that its determinant is 0:

In[10]:=
#cas [inverse] !![1, 2; 2, 4]
Out[10]=

no inverse

the matrix is singular (determinant 0); Mathlib's A⁻¹ is 0
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 2, 4]

Checked by Lean’s kernel: The determinant vanishes

!![1, 2; 2, 4].det = 0
The certificate: 1 checked step
  1. The determinant vanishes

    !![1, 2; 2, 4].det = 0

    Checked by Lean’s kernel

Possible Issues (3)

The matrix has to be square:

In[11]:=
#cas [det] !![1, 2, 3; 4, 5, 6]
Out[11]=

Error

det: the matrix must be square

The entries have to be rational numbers. For a determinant with variables, state it as a goal for cas, as in the Vandermonde example:

In[12]:=
#cas [det] !![x, 1; 1, 1]
Out[12]=

Error

expected a rational number, such as -3/2, 0.25 or 1/10^15

cas refuses a wrong value. The message shows the goal after the determinant was evaluated:

In[13]:=
example : Matrix.det !![(2 : ℤ), 1, 1; 1, 2, 1; 1, 1, 2] = 5 := by
  cas
Out[13]=

Error

cas evaluated the determinant with `norm_det`; the goal became
  4 = 5
which is false, so the stated value is not the determinant.
cas could not prove this arithmetic relation. The statement may be false, or it may lie outside the supported normalization and arithmetic fragment
The rest of the message
Polynomial equation route:
  `grind` failed
  case grind
  ⊢ False
  [grind] Goal diagnostics
    [facts] Asserted facts
    [limits] Thresholds reached
      [limit] maximum number of E-matching rounds has been reached, threshold: `(ematch := 0)`
      [limit] maximum number of case-splits has been reached, threshold: `(splits := 0)`
Checked identity route:
  cas proves checked identities between real numbers; this equality is in
    ℤ
Grind fallback:
  `grind` failed (`set_option trace.cas.fallback true` shows its diagnostics)

Neat Examples (1)

The 3×3 Hilbert matrix, with entries 1/(i+j−1), has determinant 1/2160:

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

determinant

12160
1/2160
Domain
ℚ^(3×3)
Input (matrix)
!![1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5]

Checked by Lean’s kernel: Determinant

!![1, 1 / 2, 1 / 3; 1 / 2, 1 / 3, 1 / 4; 1 / 3, 1 / 4, 1 / 5].det = 1 / 2160
The certificate: 1 checked step
  1. Determinant

    !![1, 1 / 2, 1 / 3; 1 / 2, 1 / 3, 1 / 4; 1 / 3, 1 / 4, 1 / 5].det = 1 / 2160

    Checked by Lean’s kernel

See Also

inverse · characteristicPolynomial · matrixRank · cas