Mode of #cas

characteristicPolynomial

The characteristic polynomial det(xI−A) of a square matrix with rational entries, with a theorem about Mathlib’s Matrix.charpoly.

#cas [characteristicPolynomial] A
gives the characteristic polynomial det(xI−A) of the square matrix A with rational entries, and proves A.charpoly = p in ℚ[X].

The characteristic polynomial holds a lot of a matrix at once: its roots are the eigenvalues, and the trace and the determinant can be read off its coefficients. It also appears in the Cayley-Hamilton theorem and in formulas for linear recurrences. characteristicPolynomial computes it, with a theorem about Mathlib’s Matrix.charpoly, the monic polynomial det(XI−A). If you are coming from Mathematica, watch the sign. Mathematica’s CharacteristicPolynomial is det(A−xI), which is (−1)n times this 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.charpoly = p in ℚ[X], with p written in Polynomial.X. A coefficient that is not an integer is written as a constant polynomial, such as Polynomial.C (7 / 10). The proof compares both sides at every point t (Polynomial.funext): Matrix.eval_charpoly turns the value of A.charpoly at t into the determinant of tI−A, which Mathlib’s norm_det evaluates.
  • For an n×n matrix the polynomial is monic of degree n. The coefficient of xn−1 is −trA and the constant term is (−1)ndetA.
  • The tactic cas proves goals Matrix.charpoly A = p for matrix literals over ℚ.
  • There are no options.

Examples (13)#

Basic Examples (2)

For a 2×2 matrix the polynomial is x2−(trA)x+detA:

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

det (x I - A)

x2−5x−2
x^2 - 5*x - 2
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 3, 4]

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

!![1, 2; 3, 4].charpoly = Polynomial.X ^ 2 - 5 * Polynomial.X - 2
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![1, 2; 3, 4].charpoly = Polynomial.X ^ 2 - 5 * Polynomial.X - 2

    Checked by Lean’s kernel


A 3×3 matrix:

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

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

Scope (3)

The matrix of second differences of size 4:

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

det (x I - A)

x4−8x3+21x2−20x+5
x^4 - 8*x^3 + 21*x^2 - 20*x + 5
Domain
ℚ^(4×4)
Input (matrix)
!![2, -1, 0, 0; -1, 2, -1, 0; 0, -1, 2, -1; 0, 0, -1, 2]

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

!![2, -1, 0, 0; -1, 2, -1, 0; 0, -1, 2, -1; 0, 0, -1, 2].charpoly =
  Polynomial.X ^ 4 - 8 * Polynomial.X ^ 3 + 21 * Polynomial.X ^ 2 - 20 * Polynomial.X + 5
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![2, -1, 0, 0; -1, 2, -1, 0; 0, -1, 2, -1; 0, 0, -1, 2].charpoly =
      Polynomial.X ^ 4 - 8 * Polynomial.X ^ 3 + 21 * Polynomial.X ^ 2 - 20 * Polynomial.X + 5

    Checked by Lean’s kernel


Fractions give rational coefficients:

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

det (x I - A)

x2−710x+160
x^2 - 7/10*x + 1/60
Domain
ℚ^(2×2)
Input (matrix)
!![1/2, 1/3; 1/4, 1/5]

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

!![1 / 2, 1 / 3; 1 / 4, 1 / 5].charpoly =
  Polynomial.X ^ 2 - Polynomial.C (7 / 10) * Polynomial.X + Polynomial.C (1 / 60)
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![1 / 2, 1 / 3; 1 / 4, 1 / 5].charpoly =
      Polynomial.X ^ 2 - Polynomial.C (7 / 10) * Polynomial.X + Polynomial.C (1 / 60)

    Checked by Lean’s kernel


A rotation by a right angle, written as a list of rows. Its polynomial x2+1 has no real roots:

In[5]:=
#cas [characteristicPolynomial] [[0, -1], [1, 0]]
Out[5]=

det (x I - A)

x2+1
x^2 + 1
Domain
ℚ^(2×2)
Input (matrix)
!![0, -1; 1, 0]

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

!![0, -1; 1, 0].charpoly = Polynomial.X ^ 2 + 1
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![0, -1; 1, 0].charpoly = Polynomial.X ^ 2 + 1

    Checked by Lean’s kernel

Applications (3)

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

In[6]:=
theorem charpoly_two :
    Matrix.charpoly !![(1 : ℚ), 2; 3, 4] = Polynomial.X ^ 2 - 5 * Polynomial.X - 2 := by
  cas

#print axioms charpoly_two
Out[6]=

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


With the theorem above, Mathlib’s Cayley-Hamilton theorem Matrix.aeval_self_charpoly says that the matrix is a root of the printed polynomial, A2−5A−2I=0:

In[7]:=
example : Polynomial.aeval !![(1 : ℚ), 2; 3, 4]
    (Polynomial.X ^ 2 - 5 * Polynomial.X - 2 : Polynomial ℚ) = 0 := by
  rw [← charpoly_two]
  exact Matrix.aeval_self_charpoly _
Out[7]=

Lean accepts the proof.


Every monic polynomial is the characteristic polynomial of its companion matrix. This one belongs to x3−2:

In[8]:=
#cas [characteristicPolynomial] !![0, 0, 2; 1, 0, 0; 0, 1, 0]
Out[8]=

det (x I - A)

x3−2
x^3 - 2
Domain
ℚ^(3×3)
Input (matrix)
!![0, 0, 2; 1, 0, 0; 0, 1, 0]

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

!![0, 0, 2; 1, 0, 0; 0, 1, 0].charpoly = Polynomial.X ^ 3 - 2
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![0, 0, 2; 1, 0, 0; 0, 1, 0].charpoly = Polynomial.X ^ 3 - 2

    Checked by Lean’s kernel

Properties and Relations (3)

The roots of the polynomial are the eigenvalues. For the first example they are 5±332:

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

eigenvalues with multiplicity

[52−332, 52+332]
[5/2 - √33/2, 5/2 + √33/2]
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 3, 4]

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

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

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

    Checked by Lean’s kernel


The trace and the determinant are coefficients. For the second example the trace is 16 and the determinant is −3, so the polynomial is x3−16x2+…+3:

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

trace

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

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2, 3; 4, 5, 6; 7, 8, 10].trace = 16
The certificate: 1 checked step
  1. Decided by kernel evaluation

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

    Checked by Lean’s kernel

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

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


A matrix and its transpose have the same characteristic polynomial. This is the transpose of the first example:

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

det (x I - A)

x2−5x−2
x^2 - 5*x - 2
Domain
ℚ^(2×2)
Input (matrix)
!![1, 3; 2, 4]

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

!![1, 3; 2, 4].charpoly = Polynomial.X ^ 2 - 5 * Polynomial.X - 2
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![1, 3; 2, 4].charpoly = Polynomial.X ^ 2 - 5 * Polynomial.X - 2

    Checked by Lean’s kernel

Possible Issues (1)

The matrix has to be square:

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

Error

characteristicPolynomial: the matrix must be square

Neat Examples (1)

The companion matrix of x4−10x2+1, whose roots are the four numbers ±2±3. Their eigenvalue report is on the eigenvalues page:

In[14]:=
#cas [characteristicPolynomial] !![0, 0, 0, -1; 1, 0, 0, 0; 0, 1, 0, 10; 0, 0, 1, 0]
Out[14]=

det (x I - A)

x4−10x2+1
x^4 - 10*x^2 + 1
Domain
ℚ^(4×4)
Input (matrix)
!![0, 0, 0, -1; 1, 0, 0, 0; 0, 1, 0, 10; 0, 0, 1, 0]

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

!![0, 0, 0, -1; 1, 0, 0, 0; 0, 1, 0, 10; 0, 0, 1, 0].charpoly =
  Polynomial.X ^ 4 - 10 * Polynomial.X ^ 2 + 1
The certificate: 1 checked step
  1. Characteristic polynomial in ℚ[X]

    !![0, 0, 0, -1; 1, 0, 0, 0; 0, 1, 0, 10; 0, 0, 1, 0].charpoly =
      Polynomial.X ^ 4 - 10 * Polynomial.X ^ 2 + 1

    Checked by Lean’s kernel

See Also

eigenvalues · eigenvectors · det · trace · matrixPower