Guide

Linear algebra

Determinants, inverses, ranks, linear systems and eigenvalues of matrices with rational entries, each with a theorem about Mathlib’s Matrix.

You write matrices with rational entries in Mathlib’s own notation, such as !![1, 2; 3, 4] and ![5, 6]. The arithmetic is exact, and every answer is a theorem about Mathlib’s definitions: Matrix.det, the inverse A⁻¹, Matrix.rank and Matrix.charpoly. For me that’s the point of this part, because the statement is about the same matrix I would write in a proof. A linear system, a null space or an eigenspace comes with the theorem that there are no other solutions, and an irrational eigenvalue is an algebraic number, in closed form or named by an interval.

Products and powers

  • dotThe product of two matrices, or of a matrix and a vector, with rational entries and a theorem that it is right.
  • matrixPowerThe power Ak of a square matrix with rational entries, for k up to 1000, with a theorem that it is right.
  • transposeThe transpose of a matrix of any shape with rational entries, with a theorem about Mathlib’s Matrix.transpose.
  • traceThe trace of a square matrix with rational entries, with a theorem about Mathlib’s Matrix.trace.

Determinants and inverses

  • detThe determinant of a square matrix with rational entries, with a theorem about Mathlib’s Matrix.det.
  • inverseThe inverse of a square matrix with rational entries, or a proof that its determinant is 0.

Rank, null spaces and linear systems

  • matrixRankThe rank of a matrix of any shape with rational entries, with a theorem about Mathlib’s Matrix.rank.
  • rowReduceThe reduced row echelon form of a matrix with rational entries, with the row operations that prove it.
  • nullSpaceA basis of the null space of a matrix with rational entries, with a theorem that it spans every solution.
  • linearSolveAll solutions of a linear system Ax=b with rational entries, with a theorem that there are no others.

Eigenvalues

  • characteristicPolynomialThe characteristic polynomial det(xI−A) of a square matrix with rational entries, with a theorem about Mathlib’s Matrix.charpoly.
  • eigenvaluesThe exact eigenvalues of a square matrix with rational entries, with their multiplicities and a theorem that there are no others.
  • eigenvectorsA basis of each eigenspace of a square matrix with rational entries, with a theorem for every eigenvalue, irrational ones included.

Symmetric matrices

  • positiveDefiniteWhether a square matrix with rational entries is positive definite, with a theorem about Mathlib’s Matrix.PosDef either way.
  • eigenvalueBoundsDecimal bounds on the smallest and the largest eigenvalue of a symmetric matrix, with a theorem that they hold and are sharp.

The matrix of the Fibonacci recurrence has the golden ratio and its conjugate as eigenvalues. The theorem is about Mathlib’s spectrum ℝ, and it also gives the roots of the characteristic polynomial with their multiplicities, so the two numbers are the whole spectrum:

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

eigenvalues with multiplicity

[12−52, 12+52]
[1/2 - √5/2, 1/2 + √5/2]
Domain
ℚ^(2×2)
Input (matrix)
!![0, 1; 1, 1]

Checked by Lean’s kernel: The spectrum with algebraic multiplicities

spectrum ℝ !![0, 1; 1, 1] = {1 / 2 - 1 / 2 * √5, 1 / 2 + 1 / 2 * √5} ∧
  !![0, 1; 1, 1].charpoly.roots = {1 / 2 - 1 / 2 * √5, 1 / 2 + 1 / 2 * √5}
The certificate: 1 checked step
  1. The spectrum with algebraic multiplicities

    spectrum ℝ !![0, 1; 1, 1] = {1 / 2 - 1 / 2 * √5, 1 / 2 + 1 / 2 * √5} ∧
      !![0, 1; 1, 1].charpoly.roots = {1 / 2 - 1 / 2 * √5, 1 / 2 + 1 / 2 * √5}

    Checked by Lean’s kernel

In a proof, the tactic cas proves statements about matrices written in Mathlib’s notation, also with variables among the entries. Here it is the Vandermonde determinant:

In[2]:=
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[2]=

Lean accepts the proof.

One thing to watch is that Mathlib’s inverse is total. For a singular matrix A⁻¹ is the zero matrix, so a theorem A⁻¹ = 0 would be true and tell you nothing about an inverse. That’s why inverse proves A.det = 0 for a singular matrix instead.

The #cas modes take rational entries, so a determinant with variables, like the one above, goes in as a goal for cas. eigenvalueBounds takes symmetric matrices only, and matrixPower takes exponents up to 1000. eigenvalues and eigenvectors put no limit on the size of the matrix, but their proofs run with maxHeartbeats, Lean’s budget for one command, raised to at least one million, so a large matrix can take a long time or exhaust that budget. A factor of the characteristic polynomial of degree three or more comes from a squarefree decomposition and need not be irreducible.