Mode of #cas

trace

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

#cas [trace] A
gives the trace of the square matrix A with rational entries, the sum of its diagonal entries, and proves A.trace = t.

The trace is the sum of the diagonal entries. It is also the sum of the eigenvalues, and minus the second coefficient of the characteristic polynomial, so it makes a quick check on both. trace prints it with a theorem about Mathlib’s Matrix.trace.

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.trace = t for A as a matrix over ℚ. The kernel decides it by evaluation (decide +kernel).
  • The tactic cas proves the same equations in a proof.
  • There are no options.

Examples (11)#

Basic Examples (2)

The trace of a 2×2 matrix:

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

trace

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

Checked by Lean’s kernel: Decided by kernel evaluation

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

    !![1, 2; 3, 4].trace = 5

    Checked by Lean’s kernel


Fractions on the diagonal:

In[2]:=
#cas [trace] !![1/2, 7; 0, 1/3]
Out[2]=

trace

56
5/6
Domain
ℚ^(2×2)
Input (matrix)
!![1/2, 7; 0, 1/3]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1 / 2, 7; 0, 1 / 3].trace = 5 / 6
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1 / 2, 7; 0, 1 / 3].trace = 5 / 6

    Checked by Lean’s kernel

Scope (2)

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

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

trace

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

Checked by Lean’s kernel: Decided by kernel evaluation

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

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

    Checked by Lean’s kernel


The trace of the identity is its size:

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

trace

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

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1].trace = 4
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 0; 0, 0, 0, 1].trace = 4

    Checked by Lean’s kernel

Applications (1)

The trace as a theorem. cas proves it, and only the three standard axioms are used:

In[5]:=
theorem trace_two : Matrix.trace !![(1 : ℚ), 2; 3, 4] = 5 := by
  cas

#print axioms trace_two
Out[5]=

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

Properties and Relations (3)

The trace is the sum of the eigenvalues. Here they are 1, 2 and 11, and the trace is 14:

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

eigenvalues with multiplicity

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

Checked by Lean’s kernel: Characteristic polynomial factored over ℚ

!![2, 0, 0; 0, 3, 4; 0, 4, 9].charpoly =
  (Polynomial.X - 1) * (Polynomial.X - 2) * (Polynomial.X - 11)
The certificate: 1 checked step
  1. Characteristic polynomial factored over ℚ

    !![2, 0, 0; 0, 3, 4; 0, 4, 9].charpoly =
      (Polynomial.X - 1) * (Polynomial.X - 2) * (Polynomial.X - 11)

    Checked by Lean’s kernel

In[7]:=
#cas [trace] !![2, 0, 0; 0, 3, 4; 0, 4, 9]
Out[7]=

trace

14
Domain
ℚ^(3×3)
Input (matrix)
!![2, 0, 0; 0, 3, 4; 0, 4, 9]

Checked by Lean’s kernel: Decided by kernel evaluation

!![2, 0, 0; 0, 3, 4; 0, 4, 9].trace = 14
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![2, 0, 0; 0, 3, 4; 0, 4, 9].trace = 14

    Checked by Lean’s kernel


The coefficient of xn−1 in the characteristic polynomial is minus the trace:

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

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


The eigenvalues of the same matrix are irrational, and they add up to 5:

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

Possible Issues (2)

The matrix has to be square:

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

Error

trace: the matrix must be square

The entries have to be rational numbers:

In[11]:=
#cas [trace] !![a, b; c, d]
Out[11]=

Error

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

For a 2×2 literal with variables, Mathlib has the formula:

In[12]:=
example (a b c d : ℝ) : Matrix.trace !![a, b; c, d] = a + d :=
  Matrix.trace_fin_two_of a b c d
Out[12]=

Lean accepts the proof.

Neat Examples (1)

The powers of (1110) hold the Fibonacci numbers, and their traces are the Lucas numbers. The trace of the tenth power is L10=123:

In[13]:=
#cas [matrixPower 10] !![1, 1; 1, 0]
Out[13]=

power

(89555534)
!![89, 55; 55, 34]
Domain
ℚ^(2×2)
Input (matrix)
!![1, 1; 1, 0]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 1; 1, 0] ^ 10 = !![89, 55; 55, 34]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 1; 1, 0] ^ 10 = !![89, 55; 55, 34]

    Checked by Lean’s kernel

In[14]:=
#cas [trace] !![89, 55; 55, 34]
Out[14]=

trace

123
Domain
ℚ^(2×2)
Input (matrix)
!![89, 55; 55, 34]

Checked by Lean’s kernel: Decided by kernel evaluation

!![89, 55; 55, 34].trace = 123
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![89, 55; 55, 34].trace = 123

    Checked by Lean’s kernel

See Also

det · characteristicPolynomial · eigenvalues · transpose · matrixPower