Mode of #cas

inverse

The inverse of a square matrix with rational entries, or a proof that its determinant is 0.

#cas [inverse] A
gives the inverse of the square matrix A with rational entries and proves A⁻¹ = B, or proves A.det = 0 when A has no inverse.

inverse computes the entries of A−1 over ℚ. The theorem is about Mathlib’s A⁻¹, and here a Mathlib convention matters. That inverse is total, and for a singular matrix it is the zero matrix. A statement A⁻¹ = 0 would then be true and still tell you nothing about an inverse, which is the kind of statement I don’t want a tool to print. So for a singular matrix the report proves A.det = 0 instead.

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.
  • For an invertible A the theorem is A⁻¹ = B for Mathlib’s inverse over ℚ. The kernel evaluates the product BA and checks that it is the identity, and Matrix.inv_eq_left_inv turns that into the inverse.
  • For a singular A the output is “no inverse” and the theorem is A.det = 0, proved with Mathlib’s norm_det.
  • The tactic cas proves goals A⁻¹ = B for matrix literals over ℚ, and refuses a wrong B.
  • There are no options.

Examples (14)#

Basic Examples (2)

The inverse of a 2×2 matrix:

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

inverse

(−2132−12)
!![-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
  1. Inverse from the product B * A = 1

    !![1, 2; 3, 4]⁻¹ = !![-2, 1; 3 / 2, -(1 / 2)]

    Checked by Lean’s kernel


This 3×3 matrix has determinant 1, so its inverse has integer entries:

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

inverse

(−2418520−15−4−541)
!![-24, 18, 5; 20, -15, -4; -5, 4, 1]
Domain
ℚ^(3×3)
Input (matrix)
!![1, 2, 3; 0, 1, 4; 5, 6, 0]

Checked by Lean’s kernel: Inverse from the product B * A = 1

!![1, 2, 3; 0, 1, 4; 5, 6, 0]⁻¹ = !![-24, 18, 5; 20, -15, -4; -5, 4, 1]
The certificate: 1 checked step
  1. Inverse from the product B * A = 1

    !![1, 2, 3; 0, 1, 4; 5, 6, 0]⁻¹ = !![-24, 18, 5; 20, -15, -4; -5, 4, 1]

    Checked by Lean’s kernel

Scope (3)

A 1×1 matrix:

In[3]:=
#cas [inverse] !![5]
Out[3]=

inverse

(15)
!![1/5]
Domain
ℚ^(1×1)
Input (matrix)
!![5]

Checked by Lean’s kernel: Inverse from the product B * A = 1

!![5]⁻¹ = !![1 / 5]
The certificate: 1 checked step
  1. Inverse from the product B * A = 1

    !![5]⁻¹ = !![1 / 5]

    Checked by Lean’s kernel


Entries can be fractions:

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

inverse

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

Checked by Lean’s kernel: Inverse from the product B * A = 1

!![1 / 2, 1; 3, 4]⁻¹ = !![-4, 1; 3, -(1 / 2)]
The certificate: 1 checked step
  1. Inverse from the product B * A = 1

    !![1 / 2, 1; 3, 4]⁻¹ = !![-4, 1; 3, -(1 / 2)]

    Checked by Lean’s kernel


A singular matrix has no inverse. The report proves that its determinant is 0:

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

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

Applications (2)

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

In[6]:=
theorem inverse_two : (!![(1 : ℚ), 2; 3, 4])⁻¹ = !![-2, 1; 3 / 2, -1 / 2] := by
  cas

#print axioms inverse_two
Out[6]=

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


The inverse solves Ax=b as x=A−1b. Multiplying by the inverse gives the same solution that linearSolve finds:

In[7]:=
#cas [dot] (!![-2, 1; 3/2, -1/2], ![5, 6])
Out[7]=

product

(−492)
![-4, 9/2]
Domain
ℚ^(2×2)
Input (product)
!![-2, 1; 3/2, -1/2] . ![5, 6]

Checked by Lean’s kernel: Decided by kernel evaluation

!![-2, 1; 3 / 2, -(1 / 2)].mulVec ![5, 6] = ![-4, 9 / 2]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![-2, 1; 3 / 2, -(1 / 2)].mulVec ![5, 6] = ![-4, 9 / 2]

    Checked by Lean’s kernel

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

all solutions

(−492)
![-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
  1. Solution set

    ∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]

    Checked by Lean’s kernel

Properties and Relations (3)

The product of the inverse and the matrix is the identity:

In[9]:=
#cas [dot] (!![-2, 1; 3/2, -1/2], !![1, 2; 3, 4])
Out[9]=

product

(1001)
!![1, 0; 0, 1]
Domain
ℚ^(2×2)
Input (product)
!![-2, 1; 3/2, -1/2] . !![1, 2; 3, 4]

Checked by Lean’s kernel: Decided by kernel evaluation

!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2; 3, 4] = !![1, 0; 0, 1]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2; 3, 4] = !![1, 0; 0, 1]

    Checked by Lean’s kernel


The determinant of A−1 is 1/detA. Here detA=−2:

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

determinant

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

Checked by Lean’s kernel: Determinant

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

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

    Checked by Lean’s kernel


Mathlib’s inverse of a singular matrix is the zero matrix, a fact any proof can use:

In[11]:=
example : (!![(1 : ℚ), 2; 2, 4])⁻¹ = 0 :=
  Matrix.nonsing_inv_apply_not_isUnit _ (by simp only [norm_det]; norm_num)
Out[11]=

Lean accepts the proof.

Possible Issues (3)

The matrix has to be square:

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

Error

inverse: the matrix must be square

Every row needs the same number of entries:

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

Error

the rows of the matrix have different lengths

cas refuses a wrong inverse. Here one sign is wrong:

In[14]:=
example : (!![(1 : ℚ), 2; 3, 4])⁻¹ = !![-2, 1; 3 / 2, 1 / 2] := by
  cas
Out[14]=

Error

cas: the inverse is !![-2, 1; 3/2, -1/2], not !![-2, 1; 3/2, 1/2]

Neat Examples (1)

The 3×3 Hilbert matrix, with entries 1/(i+j−1), has fractions as entries, and its inverse has integers:

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

inverse

(9−3630−36192−18030−180180)
!![9, -36, 30; -36, 192, -180; 30, -180, 180]
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: Inverse from the product B * A = 1

!![1, 1 / 2, 1 / 3; 1 / 2, 1 / 3, 1 / 4; 1 / 3, 1 / 4, 1 / 5]⁻¹ =
  !![9, -36, 30; -36, 192, -180; 30, -180, 180]
The certificate: 1 checked step
  1. Inverse from the product B * A = 1

    !![1, 1 / 2, 1 / 3; 1 / 2, 1 / 3, 1 / 4; 1 / 3, 1 / 4, 1 / 5]⁻¹ =
      !![9, -36, 30; -36, 192, -180; 30, -180, 180]

    Checked by Lean’s kernel

See Also

det · linearSolve · dot · matrixPower · rowReduce