The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V07. Linear algebra

Mathlib.LinearAlgebra.Matrix.Trace

50 declarations. Mathlib docs · Source

Trace of a matrix

This file defines the trace of a matrix, the map sending a matrix to the sum of its diagonal entries.

See also LinearAlgebra.Trace for the trace of an endomorphism.

Tags

matrix, trace, diagonal

Special cases for Fin n for low values of n

theorem Matrix.blockDiagonal'.match_1.congr_eq_1 E1 #

∀ {o : Type u_1} {n' : o → Type u_2} (motive : (i : o) × n' i → Sort u_3) (x : (i : o) × n' i)
  (h_1 : (k' : o) → (j : n' k') → motive ⟨k', j⟩) (k' : o) (j : n' k'),
  x = ⟨k', j⟩ →
    (match x with
      | ⟨k', j⟩ => h_1 k' j) ≍
      h_1 k' j

def Matrix.trace E1 #

{n : Type u_3} → {R : Type u_6} → [Fintype n] → [AddCommMonoid R] → Matrix n n R → R

The trace of a square matrix. For more bundled versions, see:

  • Matrix.traceAddMonoidHom
  • Matrix.traceLinearMap

theorem Matrix.trace_diagonal E0 #

∀ {R : Type u_6} [inst : AddCommMonoid R] {o : Type u_8} [inst_1 : Fintype o] [inst_2 : DecidableEq o] (d : o → R),
  (Matrix.diagonal d).trace = ∑ i, d i

theorem Matrix.trace_zero E0 #

∀ (n : Type u_3) (R : Type u_6) [inst : Fintype n] [inst_1 : AddCommMonoid R], Matrix.trace 0 = 0

theorem Matrix.trace_eq_zero_of_isEmpty E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] [IsEmpty n] (A : Matrix n n R),
  A.trace = 0

theorem Matrix.trace_add E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] (A B : Matrix n n R),
  (A + B).trace = A.trace + B.trace

theorem Matrix.trace_smul E0 #

∀ {n : Type u_3} {α : Type u_5} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] [inst_2 : DistribSMul α R]
  (r : α) (A : Matrix n n R), (r • A).trace = r • A.trace

theorem Matrix.trace_transpose E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] (A : Matrix n n R),
  A.transpose.trace = A.trace

theorem Matrix.trace_conjTranspose E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] [inst_2 : StarAddMonoid R]
  (A : Matrix n n R), A.conjTranspose.trace = star A.trace

def Matrix.traceAddMonoidHom E1 #

(n : Type u_3) → (R : Type u_6) → [Fintype n] → [inst : AddCommMonoid R] → Matrix n n R →+ R

Matrix.trace as an AddMonoidHom

theorem Matrix.traceAddMonoidHom_apply E0 #

∀ (n : Type u_3) (R : Type u_6) [inst : Fintype n] [inst_1 : AddCommMonoid R] (A : Matrix n n R),
  (Matrix.traceAddMonoidHom n R) A = A.trace

def Matrix.traceLinearMap E1 #

(n : Type u_3) →
  (α : Type u_5) →
    (R : Type u_6) →
      [Fintype n] → [inst : AddCommMonoid R] → [inst_1 : Semiring α] → [inst_2 : Module α R] → Matrix n n R →ₗ[α] R

Matrix.trace as a LinearMap

theorem Matrix.traceLinearMap_apply E0 #

∀ (n : Type u_3) (α : Type u_5) (R : Type u_6) [inst : Fintype n] [inst_1 : AddCommMonoid R] [inst_2 : Semiring α]
  [inst_3 : Module α R] (A : Matrix n n R), (Matrix.traceLinearMap n α R) A = A.trace

theorem Matrix.trace_list_sum E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] (l : List (Matrix n n R)),
  l.sum.trace = (List.map Matrix.trace l).sum

theorem Matrix.trace_multiset_sum E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] (s : Multiset (Matrix n n R)),
  s.sum.trace = (Multiset.map Matrix.trace s).sum

theorem Matrix.trace_sum E0 #

∀ {ι : Type u_1} {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] (s : Finset ι)
  (f : ι → Matrix n n R), (∑ i ∈ s, f i).trace = ∑ i ∈ s, (f i).trace

theorem AddMonoidHom.map_trace E0 #

∀ {n : Type u_3} {R : Type u_6} {S : Type u_7} [inst : Fintype n] [inst_1 : AddCommMonoid R] [inst_2 : AddCommMonoid S]
  {F : Type u_8} [inst_3 : FunLike F R S] [AddMonoidHomClass F R S] (f : F) (A : Matrix n n R),
  f A.trace = (A.map ⇑f).trace

theorem Matrix.trace_blockDiagonal E1 #

∀ {n : Type u_3} {p : Type u_4} {R : Type u_6} [inst : Fintype n] [inst_1 : Fintype p] [inst_2 : AddCommMonoid R]
  [inst_3 : DecidableEq p] (M : p → Matrix n n R), (Matrix.blockDiagonal M).trace = ∑ i, (M i).trace

theorem Matrix.trace_blockDiagonal' E1 #

∀ {p : Type u_4} {R : Type u_6} [inst : Fintype p] [inst_1 : AddCommMonoid R] [inst_2 : DecidableEq p]
  {m : p → Type u_8} [inst_3 : (i : p) → Fintype (m i)] (M : (i : p) → Matrix (m i) (m i) R),
  (Matrix.blockDiagonal' M).trace = ∑ i, (M i).trace

theorem Matrix.trace_fromBlocks E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : AddCommMonoid R]
  (A : Matrix n n R) (B : Matrix n m R) (C : Matrix m n R) (D : Matrix m m R),
  (Matrix.fromBlocks A B C D).trace = A.trace + D.trace

theorem Matrix.trace_comp E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : AddCommMonoid R]
  (M : Matrix n n (Matrix m m R)), ((Matrix.comp n n m m R) M).trace = M.trace.trace

theorem Matrix.trace_trace_comp_symm E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : AddCommMonoid R]
  (M : Matrix (n × m) (n × m) R), ((Matrix.comp n n m m R).symm M).trace.trace = M.trace

theorem Matrix.trace_sub E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommGroup R] (A B : Matrix n n R),
  (A - B).trace = A.trace - B.trace

theorem Matrix.trace_neg E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommGroup R] (A : Matrix n n R), (-A).trace = -A.trace

theorem Matrix.trace_one E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : DecidableEq n] [inst_2 : AddCommMonoidWithOne R],
  Matrix.trace 1 = ↑(Fintype.card n)

theorem Matrix.trace_transpose_mul E0 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : AddCommMonoid R]
  [inst_3 : Mul R] (A : Matrix m n R) (B : Matrix n m R), (A.transpose * B.transpose).trace = (A * B).trace

theorem Matrix.trace_mul_comm E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : AddCommMonoid R]
  [inst_3 : CommMagma R] (A : Matrix m n R) (B : Matrix n m R), (A * B).trace = (B * A).trace

theorem Matrix.trace_mul_cycle E1 #

∀ {m : Type u_2} {n : Type u_3} {p : Type u_4} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n]
  [inst_2 : Fintype p] [inst_3 : NonUnitalCommSemiring R] (A : Matrix m n R) (B : Matrix n p R) (C : Matrix p m R),
  (A * B * C).trace = (C * A * B).trace

theorem Matrix.trace_mul_cycle' E1 #

∀ {m : Type u_2} {n : Type u_3} {p : Type u_4} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n]
  [inst_2 : Fintype p] [inst_3 : NonUnitalCommSemiring R] (A : Matrix m n R) (B : Matrix n p R) (C : Matrix p m R),
  (A * (B * C)).trace = (C * (A * B)).trace

theorem Matrix.trace_mul_pow_comm_of_ne_zero E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : CommSemiring R]
  [inst_3 : DecidableEq m] [inst_4 : DecidableEq n] (A : Matrix m n R) (B : Matrix n m R) {k : ℕ},
  k ≠ 0 → ((A * B) ^ k).trace = ((B * A) ^ k).trace

The trace of a positive power of A * B is unchanged when the factors are reversed. The matrices A and B may be rectangular.

This requires a positive exponent because the identity does not hold at exponent zero when the index types have different cardinalities.

theorem Matrix.trace_mul_pow_comm E1 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : CommSemiring R] [inst_2 : DecidableEq n]
  (A B : Matrix n n R) (k : ℕ), ((A * B) ^ k).trace = ((B * A) ^ k).trace

For square matrices, the trace of any power of A * B is unchanged when the factors are reversed.

Unlike the rectangular version trace_mul_pow_comm_of_ne_zero, this also holds at exponent zero.

theorem Matrix.trace_replicateCol_mul_replicateRow E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] {ι : Type u_8} [inst_1 : Unique ι]
  [inst_2 : NonUnitalNonAssocSemiring R] (a b : n → R),
  (Matrix.replicateCol ι a * Matrix.replicateRow ι b).trace = a ⬝ᵥ b

theorem Matrix.trace_vecMulVec E0 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : NonUnitalNonAssocSemiring R] (a b : n → R),
  (Matrix.vecMulVec a b).trace = a ⬝ᵥ b

theorem Matrix.trace_submatrix_succ E1 #

∀ {R : Type u_6} {n : ℕ} [inst : AddCommMonoid R] (M : Matrix (Fin n.succ) (Fin n.succ) R),
  M 0 0 + (M.submatrix Fin.succ Fin.succ).trace = M.trace

theorem Matrix.trace_units_conj E1 #

∀ {m : Type u_2} {R : Type u_6} [inst : Fintype m] [inst_1 : DecidableEq m] [inst_2 : CommSemiring R]
  (M : (Matrix m m R)ˣ) (N : Matrix m m R), (↑M * N * ↑M⁻¹).trace = N.trace

theorem Matrix.trace_units_conj' E1 #

∀ {m : Type u_2} {R : Type u_6} [inst : Fintype m] [inst_1 : DecidableEq m] [inst_2 : CommSemiring R]
  (M : (Matrix m m R)ˣ) (N : Matrix m m R), (↑M⁻¹ * N * ↑M).trace = N.trace

The trace is invariant under conjugation by a unit, variant of Matrix.trace_units_conj with the inverse on the left.

theorem Matrix.trace_fin_zero E0 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (A : Matrix (Fin 0) (Fin 0) R), A.trace = 0

theorem Matrix.trace_fin_one E1 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (A : Matrix (Fin 1) (Fin 1) R), A.trace = A 0 0

theorem Matrix.trace_fin_two E1 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (A : Matrix (Fin 2) (Fin 2) R), A.trace = A 0 0 + A 1 1

theorem Matrix.trace_fin_three E1 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (A : Matrix (Fin 3) (Fin 3) R), A.trace = A 0 0 + A 1 1 + A 2 2

theorem Matrix.trace_fin_one_of E0 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (a : R), !![a].trace = a

theorem Matrix.trace_fin_two_of E0 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (a b c d : R), !![a, b; c, d].trace = a + d

theorem Matrix.trace_fin_three_of E0 #

∀ {R : Type u_6} [inst : AddCommMonoid R] (a b c d e f g h i : R), !![a, b, c; d, e, f; g, h, i].trace = a + e + i

theorem Matrix.trace_single_eq_of_ne E0 #

∀ {n : Type u_9} {α : Type u_11} [inst : DecidableEq n] [inst_1 : Fintype n] [inst_2 : AddCommMonoid α] (i j : n)
  (c : α), i ≠ j → (Matrix.single i j c).trace = 0

theorem Matrix.trace_single_eq_same E0 #

∀ {n : Type u_9} {α : Type u_11} [inst : DecidableEq n] [inst_1 : Fintype n] [inst_2 : AddCommMonoid α] (i : n) (c : α),
  (Matrix.single i i c).trace = c

theorem Matrix.trace_single_mul E1 #

∀ {m : Type u_8} {n : Type u_9} {R : Type u_10} [inst : DecidableEq m] [inst_1 : DecidableEq n] [inst_2 : Fintype n]
  [inst_3 : NonUnitalNonAssocSemiring R] [inst_4 : Fintype m] (i : n) (j : m) (a : R) (x : Matrix m n R),
  (Matrix.single i j a * x).trace = a • x j i

theorem Matrix.trace_mul_single E1 #

∀ {m : Type u_8} {n : Type u_9} {R : Type u_10} [inst : DecidableEq m] [inst_1 : DecidableEq n] [inst_2 : Fintype n]
  [inst_3 : NonUnitalNonAssocSemiring R] [inst_4 : Fintype m] (x : Matrix m n R) (i : n) (j : m) (a : R),
  (x * Matrix.single i j a).trace = MulOpposite.op a • x j i

theorem Matrix.trace_surjective E1 #

∀ {n : Type u_3} {R : Type u_6} [inst : Fintype n] [inst_1 : AddCommMonoid R] [Nonempty n],
  Function.Surjective Matrix.trace

theorem Matrix.ext_iff_trace_mul_left E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : NonAssocSemiring R]
  {A B : Matrix m n R}, A = B ↔ ∀ (x : Matrix n m R), (x * A).trace = (x * B).trace

Matrices A and B are equal iff (x * A).trace = (x * B).trace for all x.

theorem Matrix.ext_iff_trace_mul_right E1 #

∀ {m : Type u_2} {n : Type u_3} {R : Type u_6} [inst : Fintype m] [inst_1 : Fintype n] [inst_2 : NonAssocSemiring R]
  {A B : Matrix m n R}, A = B ↔ ∀ (x : Matrix n m R), (A * x).trace = (B * x).trace

Matrices A and B are equal iff (A * x).trace = (B * x).trace for all x.