roseking's picture
Sync from openai/math@faee2cf485a8 (part 23)
607ee45 verified
Raw History Blame Contribute Delete
213 kB
16
def cyclic (ρ : Representation ℂ G E) (e : E) : Subrepresentation ρ where
body · 12 lines
  toSubmodule := Submodule.span ℂ (Set.range fun g => ρ g e)
  apply_mem_toSubmodule g := by
    intro v hv
    induction hv using Submodule.span_induction with
    | mem v hv =>
      obtain ⟨h, rfl⟩ := hv
      exact Submodule.subset_span ⟨g * h, by simp⟩
    | zero => simp
    | add x y hx hy ihx ihy =>
      simpa using (Submodule.span ℂ (Set.range fun h => ρ h e)).add_mem ihx ihy
    | smul a x hx ih =>
      simpa using (Submodule.span ℂ (Set.range fun h => ρ h e)).smul_mem a ih
30
lemma self_mem_cyclic (ρ : Representation ℂ G E) (e : E) : e ∈ cyclic ρ e := by
proof · 1 line
  exact Submodule.subset_span ⟨1, by simp⟩
36
lemma starProjection_rep (ρ : Representation ℂ G E)
    (hunit : ∀ g x y, ⟪ρ g x, ρ g y⟫_ℂ = ⟪x, y⟫_ℂ)
    (U : Subrepresentation ρ) (g : G) (v : E) :
    U.toSubmodule.starProjection (ρ g v) = ρ g (U.toSubmodule.starProjection v) := by
proof · 10 lines
  apply U.toSubmodule.eq_starProjection_of_mem_of_inner_eq_zero
  · exact U.apply_mem_toSubmodule g (U.toSubmodule.starProjection_apply_mem v)
  · intro w hw
    have hw' := U.apply_mem_toSubmodule g⁻¹ hw
    have he : ρ g (ρ g⁻¹ w) = w := by
      rw [← Module.End.mul_apply, ← map_mul, mul_inv_cancel, map_one, Module.End.one_apply]
    rw [← map_sub]
    conv_lhs => rhs; rw [← he]
    rw [hunit]
    exact U.toSubmodule.starProjection_inner_eq_zero v _ hw'
54
theorem simple_eigen_submodule_alternative (ρ : Representation ℂ G E) (e : E)
    (hunit : ∀ g x y, ⟪ρ g x, ρ g y⟫_ℂ = ⟪x, y⟫_ℂ)
    (g₀ : G) (c : ℂ) (heig : ρ g₀ e = c • e)
    (hsimple : ∀ v : E, ρ g₀ v = c • v → v ∈ Submodule.span ℂ {e})
    (U : Subrepresentation ρ) :
    cyclic ρ e ≤ U ∨ ∀ u ∈ U, ∀ v ∈ cyclic ρ e, ⟪v, u⟫_ℂ = 0 := by
proof · 33 lines
  classical
  by_cases he : e ∈ U
  · left
    exact Submodule.span_le.mpr (by rintro _ ⟨g, rfl⟩; exact U.apply_mem_toSubmodule g he)
  · right
    have hp : U.toSubmodule.starProjection e = 0 := by
      have hpeig : ρ g₀ (U.toSubmodule.starProjection e) =
          c • U.toSubmodule.starProjection e := by
        rw [← starProjection_rep ρ hunit U g₀ e, heig, map_smul]
      obtain ⟨a, ha⟩ := Submodule.mem_span_singleton.mp (hsimple _ hpeig)
      by_cases ha0 : a = 0
      · simpa [ha0] using ha.symm
      · apply False.elim
        apply he
        have hm := U.toSubmodule.starProjection_apply_mem e
        rw [← ha] at hm
        have hh := U.toSubmodule.smul_mem a⁻¹ hm
        change e ∈ U.toSubmodule
        simpa [smul_smul, ha0] using hh
    have heorth (u : E) (hu : u ∈ U) : ⟪e, u⟫_ℂ = 0 := by
      have hh := U.toSubmodule.starProjection_inner_eq_zero e u hu
      simpa [hp] using hh
    intro u hu v hv
    have horth (g : G) : ⟪ρ g e, u⟫_ℂ = 0 := by
      have hgu : ρ g⁻¹ u ∈ U := U.apply_mem_toSubmodule g⁻¹ hu
      have hi := hunit g e (ρ g⁻¹ u)
      simpa only [← Module.End.mul_apply, ← map_mul, mul_inv_cancel, map_one,
        Module.End.one_apply] using hi.trans (heorth _ hgu)
    induction hv using Submodule.span_induction with
    | mem v hv => obtain ⟨g, rfl⟩ := hv; exact horth g
    | zero => simp
    | add x y hx hy ihx ihy => simp [ihx, ihy]
    | smul a x hx ih => simp [ih]
94
theorem cyclic_irreducible_of_simple_eigen (ρ : Representation ℂ G E) (e : E) (he : e ≠ 0)
    (hunit : ∀ g x y, ⟪ρ g x, ρ g y⟫_ℂ = ⟪x, y⟫_ℂ)
    (g₀ : G) (c : ℂ) (heig : ρ g₀ e = c • e)
    (hsimple : ∀ v : E, ρ g₀ v = c • v → v ∈ Submodule.span ℂ {e}) :
    Representation.IsIrreducible (cyclic ρ e).toRepresentation := by
proof · 30 lines
  let S := cyclic ρ e
  let eS : S.toSubmodule := ⟨e, self_mem_cyclic ρ e⟩
  have heS : eS ≠ 0 := by intro hh; apply he; exact congrArg Subtype.val hh
  have htb : (⊤ : Subrepresentation S.toRepresentation) ≠ ⊥ := by
    intro hh
    have hel : eS ∈ (⊤ : Subrepresentation S.toRepresentation) := by trivial
    rw [hh] at hel
    exact heS hel
  let : Nontrivial (Subrepresentation S.toRepresentation) := ⟨⟨⊤, ⊥, htb⟩⟩
  refine ⟨fun U => ?_⟩
  let U' : Subrepresentation ρ := {
    toSubmodule := U.toSubmodule.map S.toSubmodule.subtype
    apply_mem_toSubmodule := by
      intro g v hv
      obtain ⟨u, hu, rfl⟩ := hv
      exact ⟨S.toRepresentation g u, U.apply_mem_toSubmodule g hu, rfl⟩ }
  rcases simple_eigen_submodule_alternative ρ e hunit g₀ c heig hsimple U' with h | h
  · right
    apply top_unique
    intro u _
    obtain ⟨v, hv, heq⟩ := h u.property
    have hvu : v = u := Subtype.ext heq
    exact hvu ▸ hv
  · left
    apply bot_unique
    intro u hu
    have hum : (u : E) ∈ U' := ⟨u, hu, rfl⟩
    have hh := h _ hum _ u.property
    have hz : (u : E) = 0 := inner_self_eq_zero.mp hh
    exact Subtype.ext hz
140
def tensor (a : ι → Matrix n n ℂ) : Matrix (ι → n) (ι → n) ℂ :=
body · 1 line
  fun x y => ∏ i, a i (x i) (y i)
143
lemma tensor_star
    {ι : Type*} {n : Type*}
    [contextInstance135_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [DecidableEq n] (a : ι → Matrix n n ℂ) :
    tensor (fun i => star (a i)) = star (tensor a) := by
proof · 2 lines
  ext x y
  simp [tensor, Matrix.star_apply]
153
lemma tensor_mul
    {ι : Type*} {n : Type*}
    [contextInstance135_23 : Fintype ι]
    [contextInstance135_35 : DecidableEq ι]
    [contextInstance135_51 : Fintype n]
    [DecidableEq n] (a b : ι → Matrix n n ℂ) :
    tensor (fun i => a i * b i) = tensor a * tensor b := by
proof · 5 lines
  ext x y
  simp only [tensor, Matrix.mul_apply, ← Finset.prod_mul_distrib]
  symm
  simpa using (Finset.sum_prod_piFinset (ι := ι) Finset.univ
    (fun i j => a i (x i) j * b i j (y i)))
166
lemma tensor_one
    {ι : Type*} {n : Type*}
    [contextInstance135_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [contextInstance135_63 : DecidableEq n] : tensor (fun _ : ι => (1 : Matrix n n ℂ)) = 1 := by
proof · 9 lines
  ext x y
  classical
  by_cases h : x = y
  · subst y
    simp [tensor]
  · obtain ⟨i, hi⟩ := Function.ne_iff.mp h
    simp only [tensor, Matrix.one_apply, ite_eq_right h]
    apply Finset.prod_eq_zero (Finset.mem_univ i)
    simp [hi]
182
lemma tensor_diagonal
    {ι : Type*} {n : Type*}
    [contextInstance135_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [contextInstance135_63 : DecidableEq n] (a : ι → n → ℂ) :
    tensor (fun i => Matrix.diagonal (a i)) = Matrix.diagonal (fun x => ∏ i, a i (x i)) := by
proof · 9 lines
  ext x y
  classical
  by_cases h : x = y
  · subst y
    simp [tensor]
  · obtain ⟨i, hi⟩ := Function.ne_iff.mp h
    simp only [tensor, Matrix.diagonal_apply, ite_eq_right h]
    apply Finset.prod_eq_zero (Finset.mem_univ i)
    simp [hi]
199
lemma toEuclideanLin_mul
    {ι : Type*} {n : Type*}
    [Fintype ι]
    [DecidableEq ι]
    [contextInstance135_51 : Fintype n]
    [contextInstance135_63 : DecidableEq n] (a b : Matrix n n ℂ) :
    Matrix.toEuclideanLin (a * b) =
      Matrix.toEuclideanLin a * Matrix.toEuclideanLin b := by
proof · 3 lines
  ext v x
  change ((a * b) *ᵥ (WithLp.ofLp v)) x = (a *ᵥ (b *ᵥ (WithLp.ofLp v))) x
  rw [Matrix.mulVec_mulVec]
211
lemma toEuclideanLin_one
    {ι : Type*} {n : Type*}
    [Fintype ι]
    [DecidableEq ι]
    [contextInstance135_51 : Fintype n]
    [contextInstance135_63 : DecidableEq n] : Matrix.toEuclideanLin (1 : Matrix n n ℂ) = 1 := by
proof · 3 lines
  ext v x
  change ((1 : Matrix n n ℂ) *ᵥ (WithLp.ofLp v)) x = v x
  simp
222
def action : Matrix n n ℂ →* Module.End ℂ (EuclideanSpace ℂ (ι → n)) where
body · 3 lines
  toFun a := Matrix.toEuclideanLin (tensor (fun _ : ι => a))
  map_one' := by rw [tensor_one, toEuclideanLin_one (ι := ι)]
  map_mul' a b := by rw [tensor_mul, toEuclideanLin_mul (ι := ι)]
229
def rep : Representation ℂ (Matrix.unitaryGroup n ℂ) (EuclideanSpace ℂ (ι → n)) :=
body · 1 line
  action.comp (Matrix.unitaryGroup n ℂ).subtype
232
lemma tensor_mem_unitary (a : ι → Matrix.unitaryGroup n ℂ) :
    tensor (fun i => (a i : Matrix n n ℂ)) ∈ Matrix.unitaryGroup (ι → n) ℂ := by
proof · 5 lines
  rw [Matrix.mem_unitaryGroup_iff', ← tensor_star, ← tensor_mul]
  convert tensor_one (ι := ι) (n := n) using 1
  congr 1
  funext i
  exact (a i).property.1
240
lemma toEuclideanLin_unitary (a : Matrix n n ℂ) (ha : a ∈ Matrix.unitaryGroup n ℂ)
    (v w : EuclideanSpace ℂ n) :
    ⟪Matrix.toEuclideanLin a v, Matrix.toEuclideanLin a w⟫_ℂ = ⟪v, w⟫_ℂ := by
proof · 5 lines
  rw [← LinearMap.adjoint_inner_right]
  rw [← Matrix.toEuclideanLin_conjTranspose_eq_adjoint]
  change ⟪v, (Matrix.toEuclideanLin (star a) * Matrix.toEuclideanLin a) w⟫_ℂ = _
  rw [← toEuclideanLin_mul (ι := n), ha.1, toEuclideanLin_one (ι := n)]
  rfl
249
lemma rep_unitary (g : Matrix.unitaryGroup n ℂ)
    (v w : EuclideanSpace ℂ (ι → n)) : ⟪rep g v, rep g w⟫_ℂ = ⟪v, w⟫_ℂ :=
proof · 1 line
  toEuclideanLin_unitary _ (tensor_mem_unitary (fun _ : ι => g)) v w
254
lemma tensor_reindex
    {ι : Type*} {n : Type*}
    [contextInstance135_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [DecidableEq n] (a : Matrix n n ℂ) (σ : Equiv.Perm ι) (x y : ι → n) :
    tensor (fun _ : ι => a) (x ∘ σ) (y ∘ σ) = tensor (fun _ : ι => a) x y := by
proof · 1 line
  exact Equiv.prod_comp σ (fun i => a (x i) (y i))
264
lemma action_diagonal (a : n → ℂ) (v : EuclideanSpace ℂ (ι → n)) (x : ι → n) :
    action (Matrix.diagonal a) v x = (∏ i, a (x i)) * v x := by
proof · 4 lines
  change (Matrix.toEuclideanLin (tensor (fun _ : ι => Matrix.diagonal a)) v) x = _
  rw [tensor_diagonal]
  change (Matrix.diagonal (fun x : ι → n => ∏ i, a (x i)) *ᵥ WithLp.ofLp v) x = _
  exact Matrix.mulVec_diagonal _ _ _
282
def rep : Representation ℂ G (EuclideanSpace ℂ X) where
body · 10 lines
  toFun g := {
    toFun v := WithLp.toLp 2 (fun x => v (g⁻¹ • x))
    map_add' := by intro v w; rfl
    map_smul' := by intro a v; rfl }
  map_one' := by ext v x; simp
  map_mul' := by
    intro g h
    ext v x
    change v ((g * h)⁻¹ • x) = v (h⁻¹ • g⁻¹ • x)
    simp [mul_smul]
295
@[simp] lemma rep_apply (g : G) (v : EuclideanSpace ℂ X) (x : X) :
    rep g v x = v (g⁻¹ • x) := rfl
298
lemma rep_unitary (g : G) (v w : EuclideanSpace ℂ X) :
    ⟪rep g v, rep g w⟫_ℂ = ⟪v, w⟫_ℂ := by
proof · 2 lines
  simp only [PiLp.inner_apply, rep_apply]
  exact Equiv.sum_comp (MulAction.toPerm g⁻¹) (fun x => inner ℂ (v x) (w x))
304
lemma rep_single (g : G) (x : X) :
    rep g (EuclideanSpace.single x (1 : ℂ)) = EuclideanSpace.single (g • x) (1 : ℂ) := by
proof · 3 lines
  classical
  ext y
  simp only [rep_apply, PiLp.single_apply, inv_smul_eq_iff]
314
def alternator (ι : H →* G) (χ : H →* ℂ) : Module.End ℂ (EuclideanSpace ℂ X) :=
body · 1 line
  ∑ h, χ h • rep (ι h)
318
lemma alternator_apply (ι : H →* G) (χ : H →* ℂ) (v : EuclideanSpace ℂ X) :
    alternator ι χ v = ∑ h, χ h • rep (ι h) v := by
proof · 1 line
  simp [alternator, LinearMap.sum_apply]
323
lemma alternator_stable (ι : H →* G) (χ : H →* ℂ)
    (U : Subrepresentation (rep : Representation ℂ G (EuclideanSpace ℂ X)))
    (v : EuclideanSpace ℂ X) (hv : v ∈ U) : alternator ι χ v ∈ U := by
proof · 2 lines
  rw [alternator_apply]
  exact U.toSubmodule.sum_mem fun h _ => U.toSubmodule.smul_mem _ (U.apply_mem_toSubmodule _ hv)
330
lemma alternator_translate (ι : H →* G) (χ : H →* ℂ) (a : H)
    (v : EuclideanSpace ℂ X) :
    alternator ι χ (rep (ι a) v) = χ a⁻¹ • alternator ι χ v := by
proof · 12 lines
  rw [alternator_apply, alternator_apply, Finset.smul_sum]
  have he := Equiv.sum_comp (Equiv.mulRight a)
    (fun h => χ a⁻¹ • (χ h • (rep (ι h) v : EuclideanSpace ℂ X)))
  rw [← he]
  apply Finset.sum_congr rfl
  intro h _
  simp only [Equiv.coe_mulRight, smul_smul, map_mul]
  rw [show χ a⁻¹ * (χ h * χ a) = χ h by
    calc
      _ = χ h * (χ a⁻¹ * χ a) := by ring
      _ = χ h := by rw [← map_mul, inv_mul_cancel, map_one, mul_one]]
  simp only [Module.End.mul_apply]
347
lemma alternator_single_zero (ι : H →* G) (χ : H →* ℂ)
    (x : X) (a : H) (ha : ι a • x = x) (hs : χ a = -1) :
    alternator ι χ (EuclideanSpace.single x (1 : ℂ)) = 0 := by
proof · 6 lines
  have hh := alternator_translate ι χ a (EuclideanSpace.single x (1 : ℂ))
  rw [rep_single, ha, map_inv, hs, inv_neg, inv_one, neg_smul, one_smul] at hh
  have hz := congrArg (fun z => z + alternator ι χ (EuclideanSpace.single x (1 : ℂ))) hh
  have ht : (2 : ℂ) • alternator ι χ (EuclideanSpace.single x (1 : ℂ)) = 0 := by
    simpa [two_smul] using hz
  exact (smul_eq_zero.mp ht).resolve_left (by norm_num)
358
lemma alternator_coeff (ι : H →* G) (χ : H →* ℂ) (v : EuclideanSpace ℂ X) (x : X) :
    alternator ι χ v x = ∑ h, χ h * v ((ι h)⁻¹ • x) := by
proof · 1 line
  simp [alternator_apply, WithLp.ofLp_sum, Finset.sum_apply]
363
lemma polytabloid_base [DecidableEq X] (ι : H →* G) (χ : H →* ℂ) (b : X)
    (hfree : ∀ h : H, ι h • b = b → h = 1) :
    alternator ι χ (EuclideanSpace.single b (1 : ℂ)) b = 1 := by
proof · 10 lines
  classical
  rw [alternator_coeff, Finset.sum_eq_single 1]
  · simp
  · intro h _ hh
    have hn : (ι h)⁻¹ • b ≠ b := by
      intro he
      have hi : h⁻¹ = 1 := hfree h⁻¹ (by simpa using he)
      exact hh (inv_eq_one.mp hi)
    simp [hn]
  · simp
377
lemma polytabloid_inner (ι : H →* G) (χ : H →* ℂ) (b : X)
    (hreal : ∀ h, conj (χ h) = χ h) (hinv : ∀ h, χ h⁻¹ = χ h)
    (v : EuclideanSpace ℂ X) :
    ⟪alternator ι χ (EuclideanSpace.single b (1 : ℂ)), v⟫_ℂ = alternator ι χ v b := by
proof · 6 lines
  classical
  rw [alternator_apply, sum_inner, alternator_coeff]
  simp only [inner_smul_left, rep_single, EuclideanSpace.inner_single_left, map_one,
    one_mul, hreal]
  have hh := Equiv.sum_comp (Equiv.inv H) (fun h => χ h * v ((ι h)⁻¹ • b))
  simpa only [Equiv.inv_apply, map_inv, inv_inv, hinv] using hh
390
theorem alternator_rank_one (ι : H →* G) (χ : H →* ℂ) (b : X)
    (hreal : ∀ h, conj (χ h) = χ h) (hinv : ∀ h, χ h⁻¹ = χ h)
    (hfree : ∀ h : H, ι h • b = b → h = 1)
    (halt : ∀ x : X, (∃ a : H, ι a • x = x ∧ χ a = -1) ∨
      ∃ a : H, x = ι a • b) (v : EuclideanSpace ℂ X) :
    alternator ι χ v =
      ⟪alternator ι χ (EuclideanSpace.single b (1 : ℂ)), v⟫_ℂ •
        alternator ι χ (EuclideanSpace.single b (1 : ℂ)) := by
proof · 21 lines
  classical
  let e := alternator ι χ (EuclideanSpace.single b (1 : ℂ))
  have hs (x : X) : alternator ι χ (EuclideanSpace.single x (1 : ℂ)) ∈ Submodule.span ℂ {e} := by
    rcases halt x with ⟨a, ha, hχ⟩ | ⟨a, rfl⟩
    · rw [alternator_single_zero ι χ x a ha hχ]
      exact Submodule.zero_mem _
    · rw [← rep_single, alternator_translate]
      exact Submodule.smul_mem _ _ (Submodule.subset_span (Set.mem_singleton e))
  have hv : alternator ι χ v ∈ Submodule.span ℂ {e} := by
    have hexp := (EuclideanSpace.basisFun X ℂ).sum_repr v
    simp only [EuclideanSpace.basisFun_repr, EuclideanSpace.basisFun_apply] at hexp
    rw [← hexp, map_sum]
    exact Submodule.sum_mem _ fun x _ => by
      rw [map_smul]
      exact Submodule.smul_mem _ _ (hs x)
  obtain ⟨a, ha⟩ := Submodule.mem_span_singleton.mp hv
  have heb : e b = 1 := polytabloid_base ι χ b hfree
  have hab : a = alternator ι χ v b := by
    have hh := congrArg (fun z : EuclideanSpace ℂ X => z b) ha
    simpa only [PiLp.smul_apply, smul_eq_mul, heb, mul_one] using hh
  rw [← ha, hab, polytabloid_inner ι χ b hreal hinv]
423
def complexSign : Equiv.Perm α →* ℂ where
body · 3 lines
  toFun g := ((Equiv.Perm.sign g : ℤˣ) : ℤ)
  map_one' := by simp
  map_mul' := by intro g h; simp
428
@[simp] lemma complexSign_conj (g : Equiv.Perm α) : conj (complexSign g) = complexSign g := by
proof · 1 line
  simp [complexSign]
431
@[simp] lemma complexSign_inv (g : Equiv.Perm α) : complexSign g⁻¹ = complexSign g := by
proof · 1 line
  simp [complexSign]
434
@[simp] lemma complexSign_swap (x y : α) (h : x ≠ y) : complexSign (Equiv.swap x y) = -1 := by
proof · 1 line
  simp [complexSign, Equiv.Perm.sign_swap h]
442
abbrev Cells (D : YoungDiagram) := {p : ℕ × ℕ // p ∈ D.cells}
444
lemma fin_le_strictMono {n : ℕ} (f : Fin n → ℕ) (hf : StrictMono f) (i : Fin n) :
    i.val ≤ f i := by
proof · 11 lines
  have aux : ∀ k (hk : k < n), k ≤ f ⟨k, hk⟩ := by
    intro k
    induction k with
    | zero => intro hk; exact Nat.zero_le _
    | succ k ih =>
      intro hk
      have hk' : k < n := by omega
      have hl := hf (show (⟨k, hk'⟩ : Fin n) < ⟨k + 1, hk⟩ from by simp)
      have hi := ih hk'
      omega
  exact aux i.val i.isLt
458
lemma card_column (D : YoungDiagram) (j : ℕ) :
    (Finset.univ.filter fun x : Cells D => x.val.2 = j).card = D.colLen j := by
proof · 10 lines
  classical
  let e : {x : Cells D // x.val.2 = j} ≃ Fin (D.colLen j) := {
    toFun := fun x => ⟨x.val.val.1, YoungDiagram.mem_iff_lt_colLen.mp (by
      have hx : (x.val.val.1, x.val.val.2) ∈ D := x.val.property
      simpa only [x.property] using hx)⟩
    invFun := fun i => ⟨⟨(i, j), YoungDiagram.mem_iff_lt_colLen.mpr i.isLt⟩, rfl⟩
    left_inv := by intro x; apply Subtype.ext; apply Subtype.ext; exact Prod.ext rfl x.property.symm
    right_inv := by intro i; rfl }
  rw [← Fintype.card_subtype]
  exact (Fintype.card_congr e).trans (Fintype.card_fin _)
471
lemma card_row_subtype (D : YoungDiagram) (i : ℕ) :
    Fintype.card {x : Cells D // x.val.1 = i} = D.rowLen i := by
proof · 8 lines
  let e : {x : Cells D // x.val.1 = i} ≃ Fin (D.rowLen i) := {
    toFun := fun x => ⟨x.val.val.2, YoungDiagram.mem_iff_lt_rowLen.mp (by
      have hx : (x.val.val.1, x.val.val.2) ∈ D := x.val.property
      simpa only [x.property] using hx)⟩
    invFun := fun j => ⟨⟨(i, j), YoungDiagram.mem_iff_lt_rowLen.mpr j.isLt⟩, rfl⟩
    left_inv := by intro x; apply Subtype.ext; apply Subtype.ext; exact Prod.ext x.property.symm rfl
    right_inv := by intro j; rfl }
  exact (Fintype.card_congr e).trans (Fintype.card_fin _)
482
lemma eq_of_rowPreserving (D E : YoungDiagram) (e : Cells D ≃ Cells E)
    (he : ∀ x, (e x).val.1 = x.val.1) : D = E := by
proof · 8 lines
  have hr (i : ℕ) : D.rowLen i = E.rowLen i := by
    rw [← card_row_subtype D i, ← card_row_subtype E i]
    exact Fintype.card_congr (e.subtypeEquiv (fun x => by rw [he]))
  apply YoungDiagram.ext
  ext p
  obtain ⟨i, j⟩ := p
  change (i, j) ∈ D ↔ (i, j) ∈ E
  simp only [YoungDiagram.mem_iff_lt_rowLen, hr]
496
theorem column_sort_le (D : YoungDiagram) (r : Cells D → ℕ)
    (hinj : ∀ x y : Cells D, x.val.2 = y.val.2 → r x = r y → x = y)
    :
    ∃ σ : Equiv.Perm (Cells D), (∀ x, (σ x).val.2 = x.val.2) ∧
      ∀ x, x.val.1 ≤ r (σ x) := by
proof · 37 lines
  classical
  let S (j : ℕ) : Finset ℕ := (Finset.univ.filter fun x : Cells D => x.val.2 = j).image r
  have hc (j : ℕ) : (S j).card = D.colLen j := by
    change ((Finset.univ.filter fun x : Cells D => x.val.2 = j).image r).card = _
    rw [Finset.card_image_iff.mpr (show Set.InjOn r ↑(Finset.univ.filter fun x : Cells D => x.val.2 = j) from by
      intro x hx y hy hxy
      exact hinj x y ((Finset.mem_filter.mp hx).2.trans (Finset.mem_filter.mp hy).2.symm) hxy)]
    exact card_column D j
  let F (j : ℕ) := (S j).orderEmbOfFin (hc j)
  have hpre (x : Cells D) : ∃ y : Cells D, y.val.2 = x.val.2 ∧
      r y = F x.val.2 ⟨x.val.1, YoungDiagram.mem_iff_lt_colLen.mp x.property⟩ := by
    have hm := (S x.val.2).orderEmbOfFin_mem (hc _) ⟨x.val.1, YoungDiagram.mem_iff_lt_colLen.mp x.property⟩
    obtain ⟨y, hy, he⟩ := Finset.mem_image.mp hm
    exact ⟨y, (Finset.mem_filter.mp hy).2, he⟩
  choose f hcol hr using hpre
  have hf : Function.Injective f := by
    intro x y he
    have hj : x.val.2 = y.val.2 := (hcol x).symm.trans ((congrArg (fun z : Cells D => z.val.2) he).trans (hcol y))
    have hi : x.val.1 = y.val.1 := by
      have hval : F x.val.2 ⟨x.val.1, YoungDiagram.mem_iff_lt_colLen.mp x.property⟩ =
          F y.val.2 ⟨y.val.1, YoungDiagram.mem_iff_lt_colLen.mp y.property⟩ := by
        rw [← hr x, ← hr y, he]
      have hy : y.val.1 < D.colLen x.val.2 := by rw [hj]; exact YoungDiagram.mem_iff_lt_colLen.mp y.property
      have hid : F y.val.2 ⟨y.val.1, YoungDiagram.mem_iff_lt_colLen.mp y.property⟩ = F x.val.2 ⟨y.val.1, hy⟩ := by
        have aux (j k i : ℕ) (hk : j = k) (hi : i < D.colLen j) (hi' : i < D.colLen k) :
            F j ⟨i, hi⟩ = F k ⟨i, hi'⟩ := by subst k; rfl
        exact aux _ _ _ hj.symm _ _
      rw [hid] at hval
      exact congrArg Fin.val ((F x.val.2).injective hval)
    exact Subtype.ext (Prod.ext hi hj)
  let σ : Equiv.Perm (Cells D) := Equiv.ofBijective f ⟨hf, Finite.surjective_of_injective hf⟩
  refine ⟨σ, hcol, ?_⟩
  have hle (x : Cells D) : x.val.1 ≤ r (σ x) := by
    change x.val.1 ≤ r (f x)
    rw [hr]
    exact fin_le_strictMono _ (F x.val.2).strictMono ⟨x.val.1, YoungDiagram.mem_iff_lt_colLen.mp x.property⟩
  exact hle
539
lemma rowRankSum_le (D : YoungDiagram) (r : Cells D → ℕ)
    (hinj : ∀ x y : Cells D, x.val.2 = y.val.2 → r x = r y → x = y) :
    ∑ x : Cells D, x.val.1 ≤ ∑ x : Cells D, r x := by
proof · 3 lines
  obtain ⟨σ, -, hσ⟩ := column_sort_le D r hinj
  rw [← Equiv.sum_comp σ r]
  exact Finset.sum_le_sum fun x _ => hσ x
546
theorem column_sort (D : YoungDiagram) (r : Cells D → ℕ)
    (hinj : ∀ x y : Cells D, x.val.2 = y.val.2 → r x = r y → x = y)
    (hsum : ∑ x : Cells D, r x = ∑ x : Cells D, x.val.1) :
    ∃ σ : Equiv.Perm (Cells D), (∀ x, (σ x).val.2 = x.val.2) ∧
      ∀ x, r (σ x) = x.val.1 := by
proof · 7 lines
  obtain ⟨σ, hc, hle⟩ := column_sort_le D r hinj
  refine ⟨σ, hc, ?_⟩
  have heq : ∑ x : Cells D, x.val.1 = ∑ x : Cells D, r (σ x) := by
    rw [Equiv.sum_comp σ r, hsum]
  have hh := (Finset.sum_eq_sum_iff_of_le (s := Finset.univ) (fun x _ => hle x)).mp heq
  intro x
  exact (hh x (Finset.mem_univ x)).symm
567
instance coloringAction : MulAction (Equiv.Perm ι) (ι → n) where
body · 3 lines
  smul g x := x ∘ g.symm
  one_smul _ := rfl
  mul_smul _ _ _ := rfl
572
lemma site_apply
    {ι : Type*} {n : Type*}
    [Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [DecidableEq n] (g : Equiv.Perm ι) (v : EuclideanSpace ℂ (ι → n)) (x : ι → n) :
    PermutationSpace.rep g v x = v (x ∘ g) := rfl
580
def reindex (g : Equiv.Perm ι) : (ι → n) ≃ (ι → n) where
body · 4 lines
  toFun x := x ∘ g
  invFun x := x ∘ g.symm
  left_inv x := by funext i; simp
  right_inv x := by funext i; simp
586
lemma action_site_commute (a : Matrix n n ℂ) (g : Equiv.Perm ι)
    (v : EuclideanSpace ℂ (ι → n)) :
    TensorRep.action a (PermutationSpace.rep g v) =
      PermutationSpace.rep g (TensorRep.action a v) := by
proof · 9 lines
  ext x
  change (∑ y, TensorRep.tensor (fun _ : ι => a) x y * v (y ∘ g)) =
    ∑ y, TensorRep.tensor (fun _ : ι => a) (x ∘ g) y * v y
  rw [← Equiv.sum_comp (reindex (n := n) g)
    (fun y => TensorRep.tensor (fun _ : ι => a) (x ∘ g) y * v y)]
  apply Finset.sum_congr rfl
  intro y _
  change _ = TensorRep.tensor (fun _ : ι => a) (x ∘ g) (y ∘ g) * v (y ∘ g)
  rw [TensorRep.tensor_reindex]
601
def columnGroup (D : YoungDiagram) : Subgroup (Equiv.Perm (Cells D)) where
body · 6 lines
  carrier := {g | ∀ x, (g x).val.2 = x.val.2}
  one_mem' := fun _ => rfl
  mul_mem' := by intro g h hg hh x; exact (hg (h x)).trans (hh x)
  inv_mem' := by
    intro g hg x
    simpa using (hg (g⁻¹ x)).symm
610
abbrev TensorSpace := EuclideanSpace ℂ (Cells D → Fin q)
614
def alternating : Subrepresentation (TensorRep.rep (ι := Cells D) (n := Fin q)) where
body · 11 lines
  toSubmodule := {
    carrier := {v | ∀ g : columnGroup D,
      PermutationSpace.rep (g : Equiv.Perm (Cells D)) v = complexSign g.val • v}
    zero_mem' := by intro g; simp
    add_mem' := by intro x y hx hy g; simp [map_add, hx g, hy g, smul_add]
    smul_mem' := by intro c x hx g; simp [map_smul, hx g, smul_comm c] }
  apply_mem_toSubmodule a := by
    intro v hv g
    change PermutationSpace.rep g.val (TensorRep.action (a : Matrix (Fin q) (Fin q) ℂ) v) = _
    rw [← action_site_commute, hv g, map_smul]
    rfl
629
def rowColor : Cells D → Fin q := fun x => ⟨x.val.1, by
body · 3 lines
  have hm := D.up_left_mem le_rfl (Nat.zero_le x.val.2) x.property
  have hl := YoungDiagram.mem_iff_lt_colLen.mp hm
  exact lt_of_lt_of_le hl hq⟩
634
def highest : TensorSpace D q := by
body · 4 lines
  classical
  exact PermutationSpace.alternator (columnGroup D).subtype
    (complexSign.comp (columnGroup D).subtype)
    (EuclideanSpace.single (rowColor D q hq) (1 : ℂ))
640
lemma rowColor_column_free
    {ι : Type*}
    [Fintype ι]
    [DecidableEq ι]
    (D : YoungDiagram) (q : ℕ) (hq : D.colLen 0 ≤ q) (g : columnGroup D)
    (hg : g.val • rowColor D q hq = rowColor D q hq) : g = 1 := by
proof · 12 lines
  apply Subtype.ext
  apply Equiv.Perm.ext
  intro x
  apply Subtype.ext
  apply Prod.ext
  · have hh := congrFun hg (g.val x)
    have hh' := congrArg Fin.val hh
    change (g.val x).val.1 = x.val.1
    change ((g.val x |> g.val.symm)).val.1 = (g.val x).val.1 at hh'
    simp only [Equiv.symm_apply_apply] at hh'
    exact hh'.symm
  · exact g.property x
659
lemma highest_base : highest D q hq (rowColor D q hq) = 1 := by
proof · 5 lines
  classical
  unfold highest
  exact PermutationSpace.polytabloid_base (columnGroup D).subtype
    (complexSign.comp (columnGroup D).subtype) (rowColor D q hq)
    (rowColor_column_free (ι := Cells D) D q hq)
666
lemma highest_ne_zero : highest D q hq ≠ 0 := by
proof · 3 lines
  intro he
  have hh := congrArg (fun v : TensorSpace D q => v (rowColor D q hq)) he
  simp [highest_base] at hh
676
lemma rep_alternator
    {G : Type*} {H : Type*} {X : Type*}
    [contextInstance635_25 : Group G]
    [contextInstance635_35 : Group H]
    [contextInstance635_45 : Fintype H]
    [Fintype X]
    [contextInstance635_69 : MulAction G X] (ι : H →* G) (χ : H →* ℂ) (a : H)
    (v : EuclideanSpace ℂ X) :
    rep (ι a) (alternator ι χ v) = χ a⁻¹ • alternator ι χ v := by
proof · 10 lines
  rw [alternator_apply, map_sum, Finset.smul_sum]
  have he := Equiv.sum_comp (Equiv.mulLeft a)
    (fun h => χ a⁻¹ • (χ h • (rep (ι h) v : EuclideanSpace ℂ X)))
  rw [← he]
  apply Finset.sum_congr rfl
  intro h _
  simp only [Equiv.coe_mulLeft, smul_smul, map_mul, map_smul]
  rw [show χ a⁻¹ * (χ a * χ h) = χ h by
    rw [← mul_assoc, ← map_mul, inv_mul_cancel, map_one, one_mul]]
  rfl
702
lemma highest_mem : highest D q hq ∈ alternating D q := by
proof · 8 lines
  classical
  intro g
  change PermutationSpace.rep g.val (PermutationSpace.alternator _ _ _) = _
  have hh := PermutationSpace.rep_alternator (columnGroup D).subtype
    (complexSign.comp (columnGroup D).subtype) g (EuclideanSpace.single (rowColor D q hq) (1 : ℂ))
  change PermutationSpace.rep g.val (highest D q hq) = complexSign (g.val⁻¹) • highest D q hq at hh
  rw [complexSign_inv] at hh
  exact hh
713
lemma alternating_collision (v : TensorSpace D q) (hv : v ∈ alternating D q)
    (x : Cells D → Fin q) (i j : Cells D) (hij : i ≠ j)
    (hc : i.val.2 = j.val.2) (hx : x i = x j) : v x = 0 := by
proof · 23 lines
  classical
  let : DecidableEq (Cells D) := Classical.decEq _
  let g : columnGroup D := ⟨Equiv.swap i j, by
    intro k
    by_cases hki : k = i
    · subst k; simp [hc]
    by_cases hkj : k = j
    · subst k; simp [hc]
    rw [Equiv.swap_apply_of_ne_of_ne hki hkj]⟩
  have hperm : x ∘ (g : Equiv.Perm (Cells D)) = x := by
    funext k
    change x (Equiv.swap i j k) = x k
    by_cases hki : k = i
    · subst k; simp [hx]
    by_cases hkj : k = j
    · subst k; simp [hx]
    rw [Equiv.swap_apply_of_ne_of_ne hki hkj]
  have hh := congrArg (fun w : TensorSpace D q => w x) (hv g)
  simp only [site_apply, hperm, PiLp.smul_apply, smul_eq_mul] at hh
  have hg : complexSign g.val = -1 := by
    simpa only [g] using complexSign_swap i j hij
  rw [hg, neg_one_mul] at hh
  linear_combination (1/2 : ℂ) * hh
740
lemma alternating_injective (v : TensorSpace D q) (hv : v ∈ alternating D q)
    (x : Cells D → Fin q) (hx : v x ≠ 0) :
    ∀ i j : Cells D, i.val.2 = j.val.2 → (x i).val = (x j).val → i = j := by
proof · 3 lines
  intro i j hc he
  by_contra hij
  exact hx (alternating_collision D q v hv x i j hij hc (Fin.ext he))
749
lemma alternating_minimal_weight (v : TensorSpace D q) (hv : v ∈ alternating D q)
    (x : Cells D → Fin q) (hw : ∑ i, (x i).val = ∑ i : Cells D, i.val.1) :
    v x = v (rowColor D q hq) * highest D q hq x := by
proof · 28 lines
  by_cases hx : v x = 0
  · by_cases he : highest D q hq x = 0
    · rw [hx, he, mul_zero]
    · obtain ⟨σ, hc, hr⟩ := column_sort D (fun i => (x i).val)
        (alternating_injective D q (highest D q hq) (highest_mem D q hq) x he) hw
      let g : columnGroup D := ⟨σ, hc⟩
      have hcolor : x ∘ σ = rowColor D q hq := by funext i; exact Fin.ext (hr i)
      have hh := congrArg (fun w : TensorSpace D q => w x) (hv g)
      simp only [site_apply, PiLp.smul_apply, smul_eq_mul] at hh
      change v (x ∘ σ) = complexSign σ * v x at hh
      rw [hcolor, hx, mul_zero] at hh
      rw [hx, hh, zero_mul]
  · obtain ⟨σ, hc, hr⟩ := column_sort D (fun i => (x i).val)
      (alternating_injective D q v hv x hx) hw
    let g : columnGroup D := ⟨σ, hc⟩
    have hcolor : x ∘ σ = rowColor D q hq := by funext i; exact Fin.ext (hr i)
    have hh := congrArg (fun w : TensorSpace D q => w x) (hv g)
    have he := congrArg (fun w : TensorSpace D q => w x) (highest_mem D q hq g)
    simp only [site_apply, PiLp.smul_apply, smul_eq_mul] at hh he
    change v (x ∘ σ) = complexSign σ * v x at hh
    change highest D q hq (x ∘ σ) = complexSign σ * highest D q hq x at he
    rw [hcolor] at hh he
    rw [highest_base] at he
    rw [hh]
    calc
      v x = v x * 1 := (mul_one _).symm
      _ = v x * (complexSign σ * highest D q hq x) := by rw [← he]
      _ = complexSign σ * v x * highest D q hq x := by ring
788
lemma diagonal_single (a : Fin q → ℂ) (b : ι → Fin q) :
    TensorRep.action (Matrix.diagonal a) (EuclideanSpace.single b (1 : ℂ)) =
      (∏ i, a (b i)) • EuclideanSpace.single b (1 : ℂ) := by
proof · 6 lines
  ext x
  rw [TensorRep.action_diagonal]
  simp only [PiLp.smul_apply, smul_eq_mul, PiLp.single_apply]
  by_cases h : x = b
  · subst x; rfl
  · simp [h]
798
lemma diagonal_unitary {z : ℂ} (hz : star z * z = 1) :
    Matrix.diagonal (fun i : Fin q => z ^ i.val) ∈ Matrix.unitaryGroup (Fin q) ℂ := by
proof · 9 lines
  have hd : star (Matrix.diagonal (fun i : Fin q => z ^ i.val)) =
      Matrix.diagonal (fun i : Fin q => star (z ^ i.val)) := by
    ext i j
    by_cases h : i = j
    · subst j; simp [Matrix.star_apply]
    · simp [Matrix.star_apply, h, Ne.symm h]
  rw [Matrix.mem_unitaryGroup_iff', hd, Matrix.diagonal_mul_diagonal]
  simp only [star_pow, ← mul_pow, hz, one_pow]
  exact Matrix.diagonal_one
812
def weightOrder : ℕ := Fintype.card (Cells D) * q + 1
814
def weightRoot : ℂ := Complex.exp (2 * Real.pi * Complex.I / weightOrder D q)
816
lemma weightRoot_primitive : IsPrimitiveRoot (weightRoot D q) (weightOrder D q) :=
proof · 1 line
  Complex.isPrimitiveRoot_exp _ (by unfold weightOrder; omega)
819
lemma weightRoot_unitary : star (weightRoot D q) * weightRoot D q = 1 := by
proof · 4 lines
  have hn := Complex.norm_eq_one_of_pow_eq_one
    (weightRoot_primitive D q).pow_eq_one (show weightOrder D q ≠ 0 by unfold weightOrder; omega)
  rw [mul_comm, Complex.star_def, Complex.mul_conj, Complex.normSq_eq_norm_sq, hn]
  norm_num
825
def weightElement : Matrix.unitaryGroup (Fin q) ℂ :=
body · 1 line
  ⟨Matrix.diagonal (fun i => weightRoot D q ^ i.val), diagonal_unitary (weightRoot_unitary D q)⟩
828
lemma weight_sum_lt (x : Cells D → Fin q) : ∑ i, (x i).val < weightOrder D q := by
proof · 5 lines
  have hh : (∑ i, (x i).val) ≤ ∑ _i : Cells D, q :=
    Finset.sum_le_sum fun i _ => (x i).isLt.le
  simp only [Finset.sum_const, Finset.card_univ, smul_eq_mul] at hh
  unfold weightOrder
  omega
835
lemma weight_action (v : TensorSpace D q) (x : Cells D → Fin q) :
    TensorRep.rep (weightElement D q) v x = weightRoot D q ^ (∑ i, (x i).val) * v x := by
proof · 2 lines
  change TensorRep.action (Matrix.diagonal (fun i : Fin q => weightRoot D q ^ i.val)) v x = _
  rw [TensorRep.action_diagonal, Finset.prod_pow_eq_pow_sum]
840
lemma highest_diagonal (a : Fin q → ℂ) :
    TensorRep.action (Matrix.diagonal a) (highest D q hq) =
      (∏ i, a (rowColor D q hq i)) • highest D q hq := by
proof · 6 lines
  unfold highest
  rw [PermutationSpace.alternator_apply, map_sum, Finset.smul_sum]
  apply Finset.sum_congr rfl
  intro g _
  rw [map_smul, action_site_commute, diagonal_single, map_smul]
  exact smul_comm _ _ _
850
lemma highest_weight_eigen :
    TensorRep.rep (weightElement D q) (highest D q hq) =
      weightRoot D q ^ (∑ i : Cells D, i.val.1) • highest D q hq := by
proof · 4 lines
  change TensorRep.action (Matrix.diagonal _) _ = _
  rw [highest_diagonal]
  congr 1
  exact Finset.prod_pow_eq_pow_sum _ _ _
859
lemma weight_eigen_support (v : TensorSpace D q)
    (hv : TensorRep.rep (weightElement D q) v =
      weightRoot D q ^ (∑ i : Cells D, i.val.1) • v)
    (x : Cells D → Fin q) (hx : v x ≠ 0) :
    ∑ i, (x i).val = ∑ i : Cells D, i.val.1 := by
proof · 6 lines
  have hh := congrArg (fun w : TensorSpace D q => w x) hv
  rw [weight_action] at hh
  simp only [PiLp.smul_apply, smul_eq_mul] at hh
  have hp := mul_right_cancel₀ hx hh
  apply (weightRoot_primitive D q).pow_inj (weight_sum_lt D q x) _ hp
  exact weight_sum_lt D q (rowColor D q hq)
872
theorem weight_eigenspace_one_dimensional (v : TensorSpace D q)
    (hv : v ∈ alternating D q)
    (heig : TensorRep.rep (weightElement D q) v =
      weightRoot D q ^ (∑ i : Cells D, i.val.1) • v) :
    v = v (rowColor D q hq) • highest D q hq := by
proof · 11 lines
  ext x
  change v x = v (rowColor D q hq) * highest D q hq x
  by_cases hw : ∑ i, (x i).val = ∑ i : Cells D, i.val.1
  · exact alternating_minimal_weight D q hq v hv x hw
  · have hv0 : v x = 0 := by
      by_contra hn
      exact hw (weight_eigen_support D q hq v heig x hn)
    have he0 : highest D q hq x = 0 := by
      by_contra hn
      exact hw (weight_eigen_support D q hq _ (highest_weight_eigen D q hq) x hn)
    rw [hv0, he0, mul_zero]
891
def ordinary : Subrepresentation (alternating D q).toRepresentation :=
body · 2 lines
  cyclic (alternating D q).toRepresentation
    (⟨highest D q hq, highest_mem D q hq⟩ : (alternating D q).toSubmodule)
895
theorem ordinary_irreducible : Representation.IsIrreducible (ordinary D q hq).toRepresentation := by
proof · 12 lines
  refine cyclic_irreducible_of_simple_eigen _ _ ?_ ?_ (weightElement D q)
    (weightRoot D q ^ (∑ i : Cells D, i.val.1)) ?_ ?_
  · intro he
    exact highest_ne_zero D q hq (congrArg Subtype.val he)
  · intro g x y
    exact TensorRep.rep_unitary g (x : TensorSpace D q) (y : TensorSpace D q)
  · exact Subtype.ext (highest_weight_eigen D q hq)
  · intro v hv
    apply Submodule.mem_span_singleton.mpr
    refine ⟨v.val (rowColor D q hq), ?_⟩
    apply Subtype.ext
    exact (weight_eigenspace_one_dimensional D q hq v.val v.property (congrArg Subtype.val hv)).symm
916
lemma diagonal_unitary_of_star_mul (a : n → ℂ) (ha : ∀ i, star (a i) * a i = 1) :
    Matrix.diagonal a ∈ Matrix.unitaryGroup n ℂ := by
proof · 8 lines
  have hd : star (Matrix.diagonal a) = Matrix.diagonal (fun i => star (a i)) := by
    ext i j
    by_cases h : i = j
    · subst j; simp [Matrix.star_apply]
    · simp [Matrix.star_apply, h, Ne.symm h]
  rw [Matrix.mem_unitaryGroup_iff', hd, Matrix.diagonal_mul_diagonal]
  simp only [ha]
  exact Matrix.diagonal_one
927
lemma diagonal_single [DecidableEq (ι → n)] (a : n → ℂ) (b : ι → n) :
    action (Matrix.diagonal a) (EuclideanSpace.single b (1 : ℂ)) =
      (∏ i, a (b i)) • EuclideanSpace.single b (1 : ℂ) := by
proof · 6 lines
  ext x
  rw [action_diagonal]
  simp only [PiLp.smul_apply, smul_eq_mul, PiLp.single_apply]
  by_cases h : x = b
  · subst x; rfl
  · simp [h]
937
def content (x : ι → n) (j : n) : ℕ := (Finset.univ.filter fun i => x i = j).card
939
lemma product_content
    {ι : Type*} {n : Type*}
    [contextInstance868_25 : Fintype ι]
    [DecidableEq ι]
    [contextInstance869_2 : Fintype n]
    [contextInstance869_14 : DecidableEq n] (a : n → ℂ) (x : ι → n) :
    ∏ i, a (x i) = ∏ j, a j ^ content x j := by
proof · 2 lines
  rw [← Finset.prod_fiberwise' Finset.univ x a]
  simp only [Finset.prod_const, content]
949
lemma phase_product
    {ι : Type*} {n : Type*}
    [contextInstance868_25 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [contextInstance869_14 : DecidableEq n] (z : ℂ) (j : n) (x : ι → n) :
    ∏ i, (if x i = j then z else 1) = z ^ content x j := by
proof · 2 lines
  rw [← Finset.prod_filter]
  simp [content]
959
lemma content_le_card
    {ι : Type*} {n : Type*}
    [contextInstance868_25 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [contextInstance869_14 : DecidableEq n] (x : ι → n) (j : n) : content x j ≤ Fintype.card ι :=
proof · 1 line
  (Finset.card_filter_le _ _).trans_eq (Finset.card_univ)
969
lemma intertwiner_content_zero
    (L : EuclideanSpace ℂ (ι → n) →ₗ[ℂ] EuclideanSpace ℂ (κ → n))
    (hL : ∀ (g : Matrix.unitaryGroup n ℂ) v, L (rep g v) = rep g (L v))
    (x : ι → n) (y : κ → n) (j : n) (hxy : content x j ≠ content y j) :
    L (EuclideanSpace.single x (1 : ℂ)) y = 0 := by
proof · 18 lines
  let N := Fintype.card ι + Fintype.card κ + 1
  let z : ℂ := Complex.exp (2 * Real.pi * Complex.I / N)
  have hz : IsPrimitiveRoot z N := Complex.isPrimitiveRoot_exp _ (by dsimp [N]; omega)
  have hzn : ‖z‖ = 1 := Complex.norm_eq_one_of_pow_eq_one hz.pow_eq_one (by dsimp [N]; omega)
  have hzu : star z * z = 1 := by
    rw [mul_comm, Complex.star_def, Complex.mul_conj, Complex.normSq_eq_norm_sq, hzn]
    norm_num
  let g : Matrix.unitaryGroup n ℂ := ⟨Matrix.diagonal (fun k => if k = j then z else 1),
    diagonal_unitary_of_star_mul _ (fun k => by split_ifs; exact hzu; simp)⟩
  have hh := congrArg (fun v : EuclideanSpace ℂ (κ → n) => v y)
    (hL g (EuclideanSpace.single x (1 : ℂ)))
  change L (action (Matrix.diagonal _) _) y = action (Matrix.diagonal _) _ y at hh
  rw [diagonal_single, map_smul, action_diagonal] at hh
  simp only [PiLp.smul_apply, smul_eq_mul, phase_product] at hh
  by_contra hn
  apply hxy
  exact hz.pow_inj (by have := content_le_card x j; dsimp [N]; omega)
    (by have := content_le_card y j; dsimp [N]; omega) (mul_right_cancel₀ hn hh)
995
lemma intertwiner_diagonal
    (L : EuclideanSpace ℂ (ι → n) →ₗ[ℂ] EuclideanSpace ℂ (κ → n))
    (hL : ∀ (g : Matrix.unitaryGroup n ℂ) v, L (rep g v) = rep g (L v))
    (a : n → ℂ) (v : EuclideanSpace ℂ (ι → n)) :
    L (action (Matrix.diagonal a) v) = action (Matrix.diagonal a) (L v) := by
proof · 19 lines
  have hsingle (x : ι → n) :
      L (action (Matrix.diagonal a) (EuclideanSpace.single x (1 : ℂ))) =
        action (Matrix.diagonal a) (L (EuclideanSpace.single x (1 : ℂ))) := by
    ext y
    rw [diagonal_single, map_smul, action_diagonal]
    simp only [PiLp.smul_apply, smul_eq_mul]
    by_cases he : ∀ j, content x j = content y j
    · rw [product_content a x, product_content a y]
      congr 1
      exact Finset.prod_congr rfl fun j _ => congrArg (fun k => a j ^ k) (he j)
    · push Not at he
      obtain ⟨j, hj⟩ := he
      rw [intertwiner_content_zero L hL x y j hj, mul_zero, mul_zero]
  have hexp := (EuclideanSpace.basisFun (ι → n) ℂ).sum_repr v
  simp only [EuclideanSpace.basisFun_repr, EuclideanSpace.basisFun_apply] at hexp
  rw [← hexp, map_sum, map_sum, map_sum, map_sum]
  apply Finset.sum_congr rfl
  intro x _
  rw [map_smul, map_smul, map_smul, map_smul, hsingle]
1022
lemma intertwiner_hermitian
    (L : EuclideanSpace ℂ (ι → n) →ₗ[ℂ] EuclideanSpace ℂ (κ → n))
    (hL : ∀ (g : Matrix.unitaryGroup n ℂ) v, L (rep g v) = rep g (L v))
    (a : Matrix n n ℂ) (ha : a.IsHermitian) (v : EuclideanSpace ℂ (ι → n)) :
    L (action a v) = action a (L v) := by
proof · 5 lines
  rw [ha.spectral_theorem]
  simp only [Unitary.conjStarAlgAut_apply, map_mul, Module.End.mul_apply]
  change L (rep ha.eigenvectorUnitary (action (Matrix.diagonal _) (rep (star ha.eigenvectorUnitary) v))) = _
  rw [hL, intertwiner_diagonal L hL, hL]
  rfl
1041
lemma apply_mem_hermitian (U : Subrepresentation (rep (ι := ι) (n := n)))
    (a : Matrix n n ℂ) (ha : a.IsHermitian)
    (v : EuclideanSpace ℂ (ι → n)) (hv : v ∈ U) : action a v ∈ U := by
proof · 13 lines
  let L : EuclideanSpace ℂ (ι → n) →ₗ[ℂ] EuclideanSpace ℂ (ι → n) :=
    U.toSubmodule.starProjection.toLinearMap
  have hu : ∀ (g : Matrix.unitaryGroup n ℂ) (x y : EuclideanSpace ℂ (ι → n)),
      ⟪rep g x, rep g y⟫_ℂ = ⟪x, y⟫_ℂ := rep_unitary
  have hL : ∀ (g : Matrix.unitaryGroup n ℂ) (w : EuclideanSpace ℂ (ι → n)),
      L (rep g w) = rep g (L w) := by
    intro g w
    exact starProjection_rep (rep (ι := ι) (n := n)) hu U g w
  have hlv : L v = v := U.toSubmodule.starProjection_eq_self_iff.mpr hv
  have hh := intertwiner_hermitian L hL a ha v
  rw [hlv] at hh
  rw [← hh]
  exact U.toSubmodule.starProjection_apply_mem _
1066
def highestMonomial (t : Fin q → ℝ) : ℝ := ∏ i : Cells D, t (rowColor D q hq i)
1068
lemma highestMonomial_nonneg (t : Fin q → ℝ) (ht : ∀ j, 0 ≤ t j) :
    0 ≤ highestMonomial D q hq t := Finset.prod_nonneg fun _ _ => ht _
1073
lemma monomial_le_highest (t : Fin q → ℝ) (ht : ∀ j, 0 ≤ t j) (hanti : Antitone t)
    (x : Cells D → Fin q)
    (hinj : ∀ i j : Cells D, i.val.2 = j.val.2 → (x i).val = (x j).val → i = j) :
    (∏ i, t (x i)) ≤ highestMonomial D q hq t := by
proof · 5 lines
  obtain ⟨σ, -, hσ⟩ := column_sort_le D (fun i => (x i).val) hinj
  rw [← Equiv.prod_comp σ (fun i => t (x i))]
  apply Finset.prod_le_prod₀ (fun _ _ => ht _)
  intro i _
  exact hanti (show rowColor D q hq i ≤ x (σ i) from hσ i)
1085
theorem diagonal_norm_bound (t : Fin q → ℝ) (ht : ∀ j, 0 ≤ t j) (hanti : Antitone t)
    (v : TensorSpace D q) (hv : v ∈ alternating D q) :
    ‖TensorRep.action (Matrix.diagonal (fun j => (t j : ℂ))) v‖ ≤
      highestMonomial D q hq t * ‖v‖ := by
proof · 12 lines
  have hb := highestMonomial_nonneg D q hq t ht
  apply (sq_le_sq₀ (norm_nonneg _) (mul_nonneg hb (norm_nonneg _))).mp
  rw [mul_pow, EuclideanSpace.norm_sq_eq, EuclideanSpace.norm_sq_eq, Finset.mul_sum]
  apply Finset.sum_le_sum
  intro x _
  rw [TensorRep.action_diagonal, norm_mul, norm_prod, mul_pow]
  simp only [Complex.norm_real, Real.norm_eq_abs, abs_of_nonneg (ht _)]
  by_cases hz : v x = 0
  · simp [hz]
  · apply mul_le_mul_of_nonneg_right _ (sq_nonneg _)
    exact pow_le_pow_left₀ (Finset.prod_nonneg fun _ _ => ht _)
      (monomial_le_highest D q hq t ht hanti x (alternating_injective D q v hv x hz)) 2
1113
def values : Fin q → ℝ :=
body · 1 line
  (Matrix.isSymmetric_toEuclideanLin_iff.mpr hA).eigenvalues (by simp)
1116
def basis : OrthonormalBasis (Fin q) ℂ (EuclideanSpace ℂ (Fin q)) :=
body · 1 line
  (Matrix.isSymmetric_toEuclideanLin_iff.mpr hA).eigenvectorBasis (by simp)
1119
def changeBasis : Matrix.unitaryGroup (Fin q) ℂ :=
body · 2 lines
  ⟨(EuclideanSpace.basisFun (Fin q) ℂ).toBasis.toMatrix (basis A hA).toBasis,
    (EuclideanSpace.basisFun (Fin q) ℂ).toMatrix_orthonormalBasis_mem_unitary (basis A hA)⟩
1123
@[simp] lemma changeBasis_apply (i j : Fin q) : changeBasis A hA i j = basis A hA j i := rfl
1125
lemma values_antitone : Antitone (values A hA) :=
proof · 1 line
  LinearMap.IsSymmetric.eigenvalues_antitone _ _
1128
lemma eigenvector (i : Fin q) :
    Matrix.toEuclideanLin A (basis A hA i) = (values A hA i : ℂ) • basis A hA i :=
proof · 1 line
  LinearMap.IsSymmetric.apply_eigenvectorBasis _ _ _
1132
lemma spectral_theorem : A = (changeBasis A hA : Matrix _ _ ℂ) *
    Matrix.diagonal (fun i => (values A hA i : ℂ)) * star (changeBasis A hA : Matrix _ _ ℂ) := by
proof · 9 lines
  have he : A * (changeBasis A hA : Matrix _ _ ℂ) =
      (changeBasis A hA : Matrix _ _ ℂ) * Matrix.diagonal (fun i => (values A hA i : ℂ)) := by
    ext i j
    rw [Matrix.mul_diagonal]
    simpa only [Matrix.mul_apply, changeBasis_apply,
      Matrix.toLpLin_apply, Matrix.mulVec, dotProduct, PiLp.smul_apply, smul_eq_mul,
      mul_comm] using congrArg (fun v : EuclideanSpace ℂ (Fin q) => v i) (eigenvector A hA j)
  have hh := congrArg (fun B : Matrix (Fin q) (Fin q) ℂ => B * star (changeBasis A hA : Matrix _ _ ℂ)) he
  simpa only [Matrix.mul_assoc, (changeBasis A hA).property.2, Matrix.mul_one] using hh
1144
lemma values_nonneg (hpos : A.PosSemidef) (i : Fin q) : 0 ≤ values A hA i := by
proof · 4 lines
  have hp := (Matrix.isPositive_toEuclideanLin_iff.mpr hpos).inner_nonneg_left (basis A hA i)
  rw [eigenvector, inner_smul_left, (basis A hA).inner_eq_one i] at hp
  have hp' : (0 : ℂ) ≤ (values A hA i : ℂ) := by simpa only [Complex.conj_ofReal, mul_one] using hp
  exact RCLike.ofReal_nonneg.mp hp'
1150
lemma trace_eq_sum : Matrix.trace A = ∑ i, (values A hA i : ℂ) := by
proof · 3 lines
  conv_lhs => rw [spectral_theorem A hA]
  rw [Matrix.trace_mul_cycle, (changeBasis A hA).property.1,
    Matrix.one_mul, Matrix.trace_diagonal]
1162
abbrev Carrier : Type := (ordinary D q hq).toSubmodule
1164
instance carrierInner : InnerProductSpace ℂ (Carrier D q hq) :=
body · 2 lines
  @Submodule.innerProductSpace ℂ (alternating D q).toSubmodule _ _ inferInstance
    (ordinary D q hq).toSubmodule
1169
def inclusion : Carrier D q hq →ₗᵢ[ℂ] TensorSpace D q :=
body · 1 line
  (alternating D q).toSubmodule.subtypeₗᵢ.comp (ordinary D q hq).toSubmodule.subtypeₗᵢ
1172
@[simp] lemma inclusion_apply (v : Carrier D q hq) : inclusion D q hq v = v.val.val := rfl
1174
lemma inclusion_equivariant (g : Matrix.unitaryGroup (Fin q) ℂ) (v : Carrier D q hq) :
    inclusion D q hq ((ordinary D q hq).toRepresentation g v) =
      TensorRep.rep g (inclusion D q hq v) := rfl
1178
def image : Subrepresentation (TensorRep.rep (ι := Cells D) (n := Fin q)) where
body · 4 lines
  toSubmodule := LinearMap.range (inclusion D q hq).toLinearMap
  apply_mem_toSubmodule := by
    rintro g v ⟨x,rfl⟩
    exact ⟨(ordinary D q hq).toRepresentation g x,rfl⟩
1184
lemma inclusion_image (v : Carrier D q hq) : inclusion D q hq v ∈ image D q hq := ⟨v,rfl⟩
1187
def hermitianAction (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.IsHermitian) :
    Carrier D q hq →ₗ[ℂ] Carrier D q hq :=
body · 3 lines
  (LinearEquiv.ofInjective (inclusion D q hq).toLinearMap (inclusion D q hq).injective).symm.toLinearMap.comp
    ((TensorRep.action a).comp (inclusion D q hq).toLinearMap |>.codRestrict
      (image D q hq).toSubmodule (fun v => TensorRep.apply_mem_hermitian _ a ha _ (inclusion_image D q hq v)))
1193
@[simp] lemma inclusion_hermitianAction (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.IsHermitian)
    (v : Carrier D q hq) :
    inclusion D q hq (hermitianAction D q hq a ha v) = TensorRep.action a (inclusion D q hq v) := by
proof · 2 lines
  exact congrArg Subtype.val ((LinearEquiv.ofInjective (inclusion D q hq).toLinearMap
    (inclusion D q hq).injective).apply_symm_apply _)
1199
lemma tensor_hermitian (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.IsHermitian) :
    (TensorRep.tensor (fun _ : Cells D => a)).IsHermitian := by
proof · 5 lines
  change star (TensorRep.tensor (fun _ : Cells D => a)) = _
  rw [← TensorRep.tensor_star]
  congr 1
  funext i
  exact ha
1207
lemma hermitianAction_symmetric (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.IsHermitian) :
    LinearMap.IsSymmetric (𝕜 := ℂ) (E := Carrier D q hq) (hermitianAction D q hq a ha) := by
proof · 4 lines
  intro x y
  rw [← (inclusion D q hq).inner_map_map, ← (inclusion D q hq).inner_map_map x,
    inclusion_hermitianAction, inclusion_hermitianAction]
  exact (Matrix.isSymmetric_toEuclideanLin_iff.mpr (tensor_hermitian D q a ha)) x.val.val y.val.val
1214
lemma ordinary_rep_unitary (g : Matrix.unitaryGroup (Fin q) ℂ) (v w : Carrier D q hq) :
    ⟪(ordinary D q hq).toRepresentation g v, (ordinary D q hq).toRepresentation g w⟫_ℂ = ⟪v,w⟫_ℂ :=
proof · 1 line
  TensorRep.rep_unitary g v.val.val w.val.val
1218
lemma raw_norm_unitary (g : Matrix.unitaryGroup (Fin q) ℂ) (v : TensorSpace D q) :
    ‖TensorRep.rep g v‖ = ‖v‖ :=
proof · 1 line
  (TensorRep.rep g).isometryOfInner (TensorRep.rep_unitary g) |>.norm_map v
1223
theorem hermitianAction_norm_bound (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.PosSemidef)
    (v : Carrier D q hq) :
    ‖hermitianAction D q hq a ha.isHermitian v‖ ≤
      highestMonomial D q hq (SortedSpectrum.values a ha.isHermitian) * ‖v‖ := by
proof · 15 lines
  rw [← (inclusion D q hq).norm_map (hermitianAction D q hq a ha.isHermitian v),
    inclusion_hermitianAction, ← (inclusion D q hq).norm_map v]
  let u := SortedSpectrum.changeBasis a ha.isHermitian
  let t := SortedSpectrum.values a ha.isHermitian
  have he := SortedSpectrum.spectral_theorem a ha.isHermitian
  have hin : TensorRep.rep u⁻¹ (inclusion D q hq v) ∈ alternating D q :=
    (alternating D q).apply_mem_toSubmodule u⁻¹ v.val.property
  have hb := diagonal_norm_bound D q hq t (SortedSpectrum.values_nonneg a ha.isHermitian ha)
    (SortedSpectrum.values_antitone a ha.isHermitian) _ hin
  change a = (u : Matrix _ _ ℂ) * Matrix.diagonal (fun i => (t i : ℂ)) * (↑(u⁻¹) : Matrix _ _ ℂ) at he
  conv_lhs => rw [he]
  simp only [map_mul, Module.End.mul_apply]
  change ‖TensorRep.rep u (TensorRep.action (Matrix.diagonal _) (TensorRep.rep u⁻¹ _))‖ ≤ _
  rw [raw_norm_unitary]
  simpa only [raw_norm_unitary] using hb
1257
def rep (ρ : Representation ℂ G E) (σ : Representation ℂ H F) :
    Representation ℂ (G × H) (E ⊗[ℂ] F) :=
body · 1 line
  Representation.tprod (ρ.comp (MonoidHom.fst G H)) (σ.comp (MonoidHom.snd G H))
1261
@[simp] lemma rep_tmul
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F]
    {G : Type*} {H : Type*}
    [contextInstance1198_23 : Group G]
    [contextInstance1198_33 : Group H] (ρ : Representation ℂ G E) (σ : Representation ℂ H F)
    (g : G × H) (x : E) (y : F) :
    rep ρ σ g (x ⊗ₜ[ℂ] y) = ρ g.1 x ⊗ₜ σ g.2 y := rfl
1275
def rightSlice (β : Module.Dual ℂ F) : E ⊗[ℂ] F →ₗ[ℂ] E :=
body · 1 line
  (TensorProduct.rid ℂ E).toLinearMap.comp (TensorProduct.map LinearMap.id β)
1278
def leftSlice (α : Module.Dual ℂ E) : E ⊗[ℂ] F →ₗ[ℂ] F :=
body · 1 line
  (TensorProduct.lid ℂ F).toLinearMap.comp (TensorProduct.map α LinearMap.id)
1281
@[simp] lemma rightSlice_tmul
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F] (β : Module.Dual ℂ F) (x : E) (y : F) :
    rightSlice β (x ⊗ₜ[ℂ] y) = β y • x := rfl
1290
@[simp] lemma leftSlice_tmul
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F] (α : Module.Dual ℂ E) (x : E) (y : F) :
    leftSlice α (x ⊗ₜ[ℂ] y) = α x • y := rfl
1300
lemma slice_commute
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F] (α : Module.Dual ℂ E) (β : Module.Dual ℂ F) (v : E ⊗[ℂ] F) :
    α (rightSlice β v) = β (leftSlice α v) := by
proof · 3 lines
  induction v using TensorProduct.inductionOn with
  | tmul x y => simp [mul_comm]
  | add x y hx hy => simp [hx, hy]
1313
lemma slice_map_left
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F] (β : Module.Dual ℂ F) (A : E →ₗ[ℂ] E) (v : E ⊗[ℂ] F) :
    rightSlice β (TensorProduct.map A LinearMap.id v) = A (rightSlice β v) := by
proof · 3 lines
  induction v using TensorProduct.inductionOn with
  | tmul x y => simp
  | add x y hx hy => simp [hx, hy]
1326
lemma eq_sum_slices
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F] {ι : Type*} [Fintype ι] (b : Basis ι ℂ F) (v : E ⊗[ℂ] F) :
    v = ∑ i, rightSlice (b.coord i) v ⊗ₜ[ℂ] b i := by
proof · 5 lines
  induction v using TensorProduct.inductionOn with
  | tmul x y =>
    simp only [rightSlice_tmul, TensorProduct.smul_tmul,
      ← TensorProduct.tmul_sum, Basis.coord_apply, Basis.sum_repr]
  | add x y hx hy => simp only [map_add, TensorProduct.add_tmul, Finset.sum_add_distrib, ← hx, ← hy]
1341
lemma rightSlice_ext {v w : E ⊗[ℂ] F}
    (h : ∀ β : Module.Dual ℂ F, rightSlice β v = rightSlice β w) : v = w := by
proof · 4 lines
  classical
  let b := Module.finBasis ℂ F
  rw [eq_sum_slices b v, eq_sum_slices b w]
  exact Finset.sum_congr rfl fun i _ => congrArg (fun x : E => x ⊗ₜ[ℂ] b i) (h _)
1348
lemma scalar_of_commute (ρ : Representation ℂ G E) [ρ.IsIrreducible]
    (A : E →ₗ[ℂ] E) (hA : ∀ g x, A (ρ g x) = ρ g (A x)) :
    ∃ z : ℂ, A = z • LinearMap.id := by
proof · 6 lines
  let f := A.intertwiningMap_of_isIntertwiningMap ρ ρ hA
  obtain ⟨z, hz⟩ :=
    (Representation.IsIrreducible.algebraMap_intertwiningMap_bijective_of_isAlgClosed (ρ := ρ)).surjective f
  refine ⟨z, ?_⟩
  ext x
  exact (congrArg (fun L : ρ.IntertwiningMap ρ => L x) hz).symm
1359
lemma left_commutant (ρ : Representation ℂ G E) [ρ.IsIrreducible]
    (d : E ⊗[ℂ] F →ₗ[ℂ] E ⊗[ℂ] F)
    (hd : ∀ g v, d (TensorProduct.map (ρ g) LinearMap.id v) =
      TensorProduct.map (ρ g) LinearMap.id (d v)) :
    ∃ B : F →ₗ[ℂ] F, d = TensorProduct.map LinearMap.id B := by
proof · 23 lines
  classical
  let : Nontrivial E := IsSimpleModule.nontrivial (MonoidAlgebra ℂ G) ρ.asModule
  obtain ⟨x₀, hx₀⟩ := exists_ne (0 : E)
  obtain ⟨α, hα⟩ := Module.Projective.exists_dual_eq_one ℂ hx₀
  let B : F →ₗ[ℂ] F := (leftSlice α).comp (d.comp (TensorProduct.mk ℂ E F x₀))
  refine ⟨B, ?_⟩
  ext x y
  change d (x ⊗ₜ[ℂ] y) = TensorProduct.map LinearMap.id B (x ⊗ₜ[ℂ] y)
  apply rightSlice_ext
  intro β
  let A : E →ₗ[ℂ] E := (rightSlice β).comp (d.comp ((TensorProduct.mk ℂ E F).flip y))
  have hA (g : G) (v : E) : A (ρ g v) = ρ g (A v) := by
    change rightSlice β (d (ρ g v ⊗ₜ[ℂ] y)) = ρ g (rightSlice β (d (v ⊗ₜ[ℂ] y)))
    rw [← slice_map_left, ← hd]
    rfl
  obtain ⟨z,hz⟩ := scalar_of_commute ρ A hA
  have hzv (v : E) : rightSlice β (d (v ⊗ₜ[ℂ] y)) = z • v :=
    congrArg (fun f : E →ₗ[ℂ] E => f v) hz
  have hzB : β (B y) = z := by
    change β (leftSlice α (d (x₀ ⊗ₜ[ℂ] y))) = z
    rw [← slice_commute, hzv, map_smul, hα, smul_eq_mul, mul_one]
  change rightSlice β (d (x ⊗ₜ[ℂ] y)) = β (B y) • x
  rw [hzv, hzB]
1388
lemma commutant_scalar (ρ : Representation ℂ G E) (σ : Representation ℂ H F)
    [ρ.IsIrreducible] [σ.IsIrreducible]
    (d : (rep ρ σ).IntertwiningMap (rep ρ σ)) :
    ∃ z : ℂ, d.toLinearMap = z • LinearMap.id := by
proof · 20 lines
  obtain ⟨B,hB⟩ := left_commutant ρ d.toLinearMap (by
    intro g v
    simpa [rep, Representation.tprod_apply, Module.End.one_eq_id] using Representation.IntertwiningMap.isIntertwining _ _ d (g,1) v)
  have hσ (h : H) (y : F) : B (σ h y) = σ h (B y) := by
    let : Nontrivial E := IsSimpleModule.nontrivial (MonoidAlgebra ℂ G) ρ.asModule
    obtain ⟨x,hx⟩ := exists_ne (0 : E)
    obtain ⟨α,hα⟩ := Module.Projective.exists_dual_eq_one ℂ hx
    have hh := Representation.IntertwiningMap.isIntertwining _ _ d (1,h) (x ⊗ₜ[ℂ] y)
    change d.toLinearMap (rep ρ σ (1,h) (x ⊗ₜ y)) =
      rep ρ σ (1,h) (d.toLinearMap (x ⊗ₜ y)) at hh
    rw [hB] at hh
    simp only [rep_tmul, map_one, Module.End.one_apply, TensorProduct.map_tmul,
      LinearMap.id_apply] at hh
    have := congrArg (leftSlice α) hh
    simpa [hα] using this
  obtain ⟨z,hz⟩ := scalar_of_commute σ B hσ
  refine ⟨z, ?_⟩
  rw [hB, hz]
  ext x y
  simp
1413
lemma unitary
    {E : Type*} {F : Type*}
    [contextInstance1195_23 : NormedAddCommGroup E]
    [contextInstance1195_46 : InnerProductSpace ℂ E]
    [contextInstance1196_2 : NormedAddCommGroup F]
    [contextInstance1196_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F]
    {G : Type*} {H : Type*}
    [contextInstance1198_23 : Group G]
    [contextInstance1198_33 : Group H] (ρ : Representation ℂ G E) (σ : Representation ℂ H F)
    (hρ : ∀ g x y, ⟪ρ g x, ρ g y⟫_ℂ = ⟪x,y⟫_ℂ)
    (hσ : ∀ h x y, ⟪σ h x, σ h y⟫_ℂ = ⟪x,y⟫_ℂ)
    (g : G × H) (v w : E ⊗[ℂ] F) :
    ⟪rep ρ σ g v, rep ρ σ g w⟫_ℂ = ⟪v,w⟫_ℂ := by
proof · 3 lines
  let a : E →ₗᵢ[ℂ] E := (ρ g.1).isometryOfInner (hρ g.1)
  let b : F →ₗᵢ[ℂ] F := (σ g.2).isometryOfInner (hσ g.2)
  exact TensorProduct.inner_map_map a b v w
1432
lemma projection_commute (τ : Representation ℂ G E)
    (hτ : ∀ g x y, ⟪τ g x, τ g y⟫_ℂ = ⟪x,y⟫_ℂ)
    (U : Subrepresentation τ) (g : G) (v : E) :
    U.toSubmodule.starProjection (τ g v) = τ g (U.toSubmodule.starProjection v) := by
proof · 10 lines
  apply U.toSubmodule.eq_starProjection_of_mem_of_inner_eq_zero
  · exact U.apply_mem_toSubmodule g (U.toSubmodule.starProjection_apply_mem v)
  · intro w hw
    have hw' := U.apply_mem_toSubmodule g⁻¹ hw
    have he : τ g (τ g⁻¹ w) = w := by
      rw [← Module.End.mul_apply, ← map_mul, mul_inv_cancel, map_one, Module.End.one_apply]
    rw [← map_sub]
    conv_lhs => rhs; rw [← he]
    rw [hτ]
    exact U.toSubmodule.starProjection_inner_eq_zero v _ hw'
1449
theorem irreducible (ρ : Representation ℂ G E) (σ : Representation ℂ H F)
    [ρ.IsIrreducible] [σ.IsIrreducible]
    (hρ : ∀ g x y, ⟪ρ g x, ρ g y⟫_ℂ = ⟪x,y⟫_ℂ)
    (hσ : ∀ h x y, ⟪σ h x, σ h y⟫_ℂ = ⟪x,y⟫_ℂ) :
    (rep ρ σ).IsIrreducible := by
proof · 36 lines
  let : Nontrivial E := IsSimpleModule.nontrivial (MonoidAlgebra ℂ G) ρ.asModule
  let : Nontrivial F := IsSimpleModule.nontrivial (MonoidAlgebra ℂ H) σ.asModule
  obtain ⟨x,hx⟩ := exists_ne (0 : E)
  obtain ⟨y,hy⟩ := exists_ne (0 : F)
  have hxy : x ⊗ₜ[ℂ] y ≠ 0 := by
    intro h
    have := congrArg norm h
    simp only [TensorProduct.norm_tmul, norm_zero] at this
    exact (mul_ne_zero (norm_ne_zero_iff.mpr hx) (norm_ne_zero_iff.mpr hy)) this
  have htb : (⊤ : Subrepresentation (rep ρ σ)) ≠ ⊥ := by
    intro h
    have hh : x ⊗ₜ[ℂ] y ∈ (⊤ : Subrepresentation (rep ρ σ)) := by trivial
    rw [h] at hh
    exact hxy hh
  let : Nontrivial (Subrepresentation (rep ρ σ)) := ⟨⟨⊤,⊥,htb⟩⟩
  refine ⟨fun U => ?_⟩
  let d : (rep ρ σ).IntertwiningMap (rep ρ σ) :=
    U.toSubmodule.starProjection.toLinearMap.intertwiningMap_of_isIntertwiningMap _ _
      (projection_commute _ (unitary ρ σ hρ hσ) U)
  obtain ⟨z,hz⟩ := commutant_scalar ρ σ d
  have hproj (v : E ⊗[ℂ] F) : U.toSubmodule.starProjection v = z • v :=
    congrArg (fun f : E ⊗[ℂ] F →ₗ[ℂ] E ⊗[ℂ] F => f v) hz
  by_cases hz0 : z = 0
  · left
    apply bot_unique
    intro v hv
    change v = 0
    have hh := hproj v
    rw [U.toSubmodule.starProjection_eq_self_iff.mpr hv, hz0, zero_smul] at hh
    exact hh
  · right
    apply top_unique
    intro v _
    have hv := U.toSubmodule.starProjection_apply_mem (z⁻¹ • v)
    rw [hproj, smul_smul, mul_inv_cancel₀ hz0, one_smul] at hv
    exact hv
1511
lemma repr_diagonal (a : n → ℂ) (f : F) (y : Y) :
    c.repr (D' a f) y = (∏ j, a j ^ v y j) * c.repr f y := by
proof · 7 lines
  classical
  calc
    c.repr (D' a f) y = c.repr (D' a (∑ k, c.repr f k • c k)) y := by rw [c.sum_repr]
    _ = _ := by
      simp only [map_sum, map_smul, hc,
        Basis.repr_self]
      simp [Finset.sum_apply, Finsupp.single_apply, mul_comm]
1521
lemma coefficient_zero
    {n : Type*} {X : Type*} {Y : Type*} {E : Type*} {F : Type*}
    [contextInstance1400_29 : Fintype n]
    [Fintype X]
    [contextInstance1400_53 : Fintype Y]
    [contextInstance1401_2 : AddCommGroup E]
    [contextInstance1401_19 : Module ℂ E]
    [contextInstance1401_32 : AddCommGroup F]
    [contextInstance1401_49 : Module ℂ F]
    (b : Basis X ℂ E) (c : Basis Y ℂ F)
    (w : X → n → ℕ) (v : Y → n → ℕ)
    (D : (n → ℂ) → E →ₗ[ℂ] E) (D' : (n → ℂ) → F →ₗ[ℂ] F)
    (hb : ∀ a x, D a (b x) = (∏ j, a j ^ w x j) • b x)
    (hc : ∀ a y, D' a (c y) = (∏ j, a j ^ v y j) • c y) (L : E →ₗ[ℂ] F)
    (hL : ∀ a : n → ℂ, (∀ j, star (a j) * a j = 1) → ∀ x, L (D a x) = D' a (L x))
    (x : X) (y : Y) (j : n) (hxy : w x j ≠ v y j) :
    c.repr (L (b x)) y = 0 := by
proof · 25 lines
  classical
  let N := w x j + v y j + 1
  let z : ℂ := Complex.exp (2 * Real.pi * Complex.I / N)
  have hN : N ≠ 0 := by dsimp [N]; omega
  have hz : IsPrimitiveRoot z N := Complex.isPrimitiveRoot_exp _ hN
  have hzn : ‖z‖ = 1 := Complex.norm_eq_one_of_pow_eq_one hz.pow_eq_one hN
  have hzu : star z * z = 1 := by
    rw [mul_comm, Complex.star_def, Complex.mul_conj, Complex.normSq_eq_norm_sq, hzn]
    norm_num
  let a : n → ℂ := fun k => if k = j then z else 1
  have ha : ∀ k, star (a k) * a k = 1 := by
    intro k
    dsimp [a]
    split_ifs
    · exact hzu
    · simp
  have hh := congrArg (fun f => c.repr f y) (hL a ha (b x))
  rw [hb, map_smul, map_smul, Finsupp.smul_apply, smul_eq_mul,
    repr_diagonal c v D' hc] at hh
  have hprod (u : n → ℕ) : (∏ k, a k ^ u k) = z ^ u j := by
    simp [a]
  rw [hprod, hprod] at hh
  by_contra hn
  exact hxy (hz.pow_inj (by dsimp [N]; omega) (by dsimp [N]; omega)
    (mul_right_cancel₀ hn hh))
1567
theorem diagonal (L : E →ₗ[ℂ] F)
    (hL : ∀ a : n → ℂ, (∀ j, star (a j) * a j = 1) → ∀ x, L (D a x) = D' a (L x))
    (a : n → ℂ) : L.comp (D a) = (D' a).comp L := by
proof · 12 lines
  classical
  apply b.ext
  intro x
  apply c.repr.injective
  ext y
  simp only [LinearMap.comp_apply, hb, map_smul, Finsupp.smul_apply, smul_eq_mul,
    repr_diagonal c v D' hc]
  by_cases hw : w x = v y
  · rw [hw]
  · obtain ⟨j,hj⟩ := Function.ne_iff.mp hw
    rw [coefficient_zero b c w v D D' hb hc L hL x y j hj]
    simp
1590
abbrev Spin (q : ℕ) := Fin q × Bool
1591
abbrev Mat (q : ℕ) := Matrix (Fin q) (Fin q) ℂ
1592
abbrev Group (q : ℕ) := Matrix.unitaryGroup (Fin q) ℂ × Matrix.unitaryGroup (Fin q) ℂ
1594
def block : (Mat q × Mat q) →* Matrix (Spin q) (Spin q) ℂ where
body · 11 lines
  toFun a := Matrix.blockDiagonal (fun b : Bool => cond b a.2 a.1)
  map_one' := by
    convert (Matrix.blockDiagonal_one (m := Fin q) (o := Bool) (α := ℂ)) using 1
    congr 1
    funext b
    cases b <;> rfl
  map_mul' a b := by
    rw [← Matrix.blockDiagonal_mul]
    congr 1
    funext c
    cases c <;> rfl
1607
lemma block_star (a : Mat q × Mat q) : block (star a) = star (block a) := by
proof · 5 lines
  change Matrix.blockDiagonal (fun b : Bool => cond b (star a.2) (star a.1)) = (Matrix.blockDiagonal (fun b : Bool => cond b a.2 a.1))ᴴ
  rw [Matrix.blockDiagonal_conjTranspose]
  congr 1
  funext b
  cases b <;> rfl
1614
def blockGroup : Group q →* Matrix.unitaryGroup (Spin q) ℂ where
body · 14 lines
  toFun g := ⟨block (g.1.val,g.2.val), by
    rw [Matrix.mem_unitaryGroup_iff', ← block_star, ← map_mul]
    have he : star (g.1.val,g.2.val) * (g.1.val,g.2.val) = 1 :=
      Prod.ext g.1.property.1 g.2.property.1
    rw [he, map_one]⟩
  map_one' := by
    apply Subtype.ext
    change block (1 : Mat q × Mat q) = 1
    exact block.map_one
  map_mul' g h := by
    apply Subtype.ext
    change block ((g.1.val,g.2.val) * (h.1.val,h.2.val)) =
      block (g.1.val,g.2.val) * block (h.1.val,h.2.val)
    exact block.map_mul _ _
1633
abbrev Raw := EuclideanSpace ℂ (τ → Spin q)
1634
abbrev PairRaw := EuclideanSpace ℂ (ι → Fin q) ⊗[ℂ] EuclideanSpace ℂ (κ → Fin q)
1636
def rep : Representation ℂ (Group q) (Raw (q := q) (τ := τ)) :=
body · 1 line
  (TensorRep.rep (ι := τ)).comp blockGroup
1639
def pairRep : Representation ℂ (Group q) (PairRaw (q := q) (ι := ι) (κ := κ)) :=
body · 1 line
  ExternalTensor.rep TensorRep.rep TensorRep.rep
1642
def pairAction : (Mat q × Mat q) →* Module.End ℂ (PairRaw (q := q) (ι := ι) (κ := κ)) where
body · 12 lines
  toFun a := TensorProduct.map (TensorRep.action a.1) (TensorRep.action a.2)
  map_one' := by
    ext x y
    change TensorProduct.map _ _ (x ⊗ₜ[ℂ] y) = x ⊗ₜ[ℂ] y
    simp only [Prod.fst_one, Prod.snd_one, map_one, TensorProduct.map_tmul,
      Module.End.one_apply]
  map_mul' a b := by
    ext x y
    change TensorProduct.map _ _ (x ⊗ₜ[ℂ] y) =
      TensorProduct.map _ _ (TensorProduct.map _ _ (x ⊗ₜ[ℂ] y))
    simp only [Prod.fst_mul, Prod.snd_mul, map_mul, TensorProduct.map_tmul,
      Module.End.mul_apply]
1656
@[simp] lemma pairAction_tmul (a : Mat q × Mat q)
    (x : EuclideanSpace ℂ (ι → Fin q)) (y : EuclideanSpace ℂ (κ → Fin q)) :
    pairAction a (x ⊗ₜ[ℂ] y) = TensorRep.action a.1 x ⊗ₜ TensorRep.action a.2 y := rfl
1660
lemma pairRep_eq (g : Group q) : pairRep (ι := ι) (κ := κ) g = pairAction (g.1.val,g.2.val) := rfl
1662
lemma rep_unitary (g : Group q) (v w : Raw (q := q) (τ := τ)) :
    ⟪rep g v,rep g w⟫_ℂ = ⟪v,w⟫_ℂ := TensorRep.rep_unitary (blockGroup g) v w
1665
lemma pairRep_unitary (g : Group q) (v w : PairRaw (q := q) (ι := ι) (κ := κ)) :
    ⟪pairRep g v,pairRep g w⟫_ℂ = ⟪v,w⟫_ℂ :=
proof · 2 lines
  ExternalTensor.unitary (TensorRep.rep (ι := ι) (n := Fin q)) (TensorRep.rep (ι := κ) (n := Fin q))
    TensorRep.rep_unitary TensorRep.rep_unitary g v w
1670
def pairBasis : OrthonormalBasis ((ι → Fin q) × (κ → Fin q)) ℂ (PairRaw (q := q) (ι := ι) (κ := κ)) :=
body · 1 line
  (EuclideanSpace.basisFun _ ℂ).tensorProduct (EuclideanSpace.basisFun _ ℂ)
1673
def pairContent (x : (ι → Fin q) × (κ → Fin q)) (j : Spin q) : ℕ :=
body · 1 line
  cond j.2 (TensorRep.content x.2 j.1) (TensorRep.content x.1 j.1)
1676
def diagPair (a : Spin q → ℂ) : Mat q × Mat q :=
body · 1 line
  (Matrix.diagonal (fun j => a (j,false)), Matrix.diagonal (fun j => a (j,true)))
1679
lemma block_diagPair (a : Spin q → ℂ) : block (diagPair a) = Matrix.diagonal a := by
proof · 5 lines
  change Matrix.blockDiagonal (fun b : Bool => cond b (Matrix.diagonal (fun j => a (j,true))) (Matrix.diagonal (fun j => a (j,false)))) = Matrix.diagonal a
  convert Matrix.blockDiagonal_diagonal (fun (b : Bool) (j : Fin q) => a (j,b)) using 1
  congr 1
  funext b
  cases b <;> rfl
1686
lemma pairBasis_diagonal (a : Spin q → ℂ) (x : (ι → Fin q) × (κ → Fin q)) :
    pairAction (diagPair a) (pairBasis x) =
      (∏ j, a j ^ pairContent x j) • pairBasis x := by
proof · 8 lines
  simp only [pairBasis, OrthonormalBasis.tensorProduct_apply', EuclideanSpace.basisFun_apply,
    pairAction_tmul, diagPair, TensorRep.diagonal_single, TensorProduct.smul_tmul,
    TensorProduct.tmul_smul, smul_smul]
  congr 1
  rw [Fintype.prod_prod_type]
  simp only [pairContent, Fintype.prod_bool, Bool.cond_false, Bool.cond_true]
  rw [Finset.prod_mul_distrib, TensorRep.product_content (fun j : Fin q => a (j,true)) x.2,
    TensorRep.product_content (fun j : Fin q => a (j,false)) x.1]
1700
theorem intertwiner_diagonal
    (L : PairRaw (q := q) (ι := ι) (κ := κ) →ₗ[ℂ] Raw (q := q) (τ := τ))
    (hL : ∀ g : Group q, ∀ v, L (pairRep g v) = rep g (L v))
    (a : Spin q → ℂ) (v : PairRaw (q := q) (ι := ι) (κ := κ)) :
    L (pairAction (diagPair a) v) = TensorRep.action (Matrix.diagonal a) (L v) := by
proof · 13 lines
  have hh := TorusNaturality.diagonal (pairBasis (q := q) (ι := ι) (κ := κ)).toBasis
    (EuclideanSpace.basisFun (τ → Spin q) ℂ).toBasis pairContent TensorRep.content
    (fun a => pairAction (diagPair a)) (fun a => TensorRep.action (Matrix.diagonal a))
    pairBasis_diagonal (fun a x => by
      simpa only [OrthonormalBasis.coe_toBasis, EuclideanSpace.basisFun_apply, TensorRep.product_content] using
        (TensorRep.diagonal_single a x)) L (fun a ha v => by
      let g : Group q :=
        (⟨(diagPair a).1, TensorRep.diagonal_unitary_of_star_mul _ (fun j => ha (j,false))⟩,
         ⟨(diagPair a).2, TensorRep.diagonal_unitary_of_star_mul _ (fun j => ha (j,true))⟩)
      have hh := hL g v
      change L (pairAction (diagPair a) v) = TensorRep.action (block (diagPair a)) (L v) at hh
      rwa [block_diagPair] at hh) a
  exact congrArg (fun f : _ →ₗ[ℂ] _ => f v) hh
1719
lemma pair_spectral (a : Mat q × Mat q) (ha : a.1.IsHermitian) (hb : a.2.IsHermitian) :
    ∃ (g : Group q) (d : Spin q → ℂ),
      a = (g.1.val,g.2.val) * diagPair d * (g.1⁻¹.val,g.2⁻¹.val) := by
proof · 5 lines
  refine ⟨(ha.eigenvectorUnitary,hb.eigenvectorUnitary),
    (fun j => cond j.2 (hb.eigenvalues j.1 : ℂ) (ha.eigenvalues j.1 : ℂ)), ?_⟩
  apply Prod.ext
  · exact ha.spectral_theorem
  · exact hb.spectral_theorem
1730
theorem intertwiner_hermitian
    (L : PairRaw (q := q) (ι := ι) (κ := κ) →ₗ[ℂ] Raw (q := q) (τ := τ))
    (hL : ∀ g : Group q, ∀ v, L (pairRep g v) = rep g (L v))
    (a : Mat q × Mat q) (ha : a.1.IsHermitian) (hb : a.2.IsHermitian)
    (v : PairRaw (q := q) (ι := ι) (κ := κ)) :
    L (pairAction a v) = TensorRep.action (block a) (L v) := by
proof · 6 lines
  obtain ⟨g,d,he⟩ := pair_spectral a ha hb
  rw [he]
  simp only [map_mul, Module.End.mul_apply, block_diagPair]
  change L (pairRep g (pairAction (diagPair d) (pairRep g⁻¹ v))) =
    rep g (TensorRep.action (Matrix.diagonal d) (rep g⁻¹ (L v)))
  rw [hL, intertwiner_diagonal L hL, hL]
1761
lemma inner_move
    {G : Type*}
    [contextInstance1640_21 : Group G]
    {E : Type*}
    [contextInstance1641_23 : NormedAddCommGroup E]
    [contextInstance1641_46 : InnerProductSpace ℂ E]
    [FiniteDimensional ℂ E]
    (ρ : Representation ℂ G E)
    (hρ : ∀ g x y, ⟪ρ g x,ρ g y⟫_ℂ = ⟪x,y⟫_ℂ) (g : G) (x y : E) : ⟪x,ρ g y⟫_ℂ = ⟪ρ g⁻¹ x,y⟫_ℂ := by
proof · 3 lines
  have h := hρ g (ρ g⁻¹ x) y
  simpa only [← Module.End.mul_apply, ← map_mul, mul_inv_cancel, map_one,
    Module.End.one_apply] using h
1775
lemma adjoint_intertwines (L : E →ₗ[ℂ] F)
    (hL : ∀ g x, L (ρ g x) = σ g (L x)) (g : G) (y : F) :
    LinearMap.adjoint L (σ g y) = ρ g (LinearMap.adjoint L y) := by
proof · 4 lines
  apply ext_inner_left ℂ
  intro x
  rw [LinearMap.adjoint_inner_right, inner_move σ hσ, ← hL,
    ← LinearMap.adjoint_inner_right, ← inner_move ρ hρ]
1783
lemma adjoint_isometry (L : E →ₗᵢ[ℂ] F) (x : E) :
    LinearMap.adjoint L.toLinearMap (L x) = x := by
proof · 4 lines
  apply ext_inner_left ℂ
  intro y
  rw [LinearMap.adjoint_inner_right]
  exact L.inner_map_map y x
1797
abbrev Carrier := PolynomialCarrier.Carrier a q ha ⊗[ℂ] PolynomialCarrier.Carrier b q hb
1799
instance carrierNormed : NormedAddCommGroup (Carrier a b q ha hb) :=
body · 2 lines
  TensorProduct.instNormedAddCommGroup (𝕜 := ℂ)
    (E := PolynomialCarrier.Carrier a q ha) (F := PolynomialCarrier.Carrier b q hb)
1803
instance carrierInner : InnerProductSpace ℂ (Carrier a b q ha hb) :=
body · 2 lines
  TensorProduct.instInnerProductSpace (𝕜 := ℂ)
    (E := PolynomialCarrier.Carrier a q ha) (F := PolynomialCarrier.Carrier b q hb)
1807
instance carrierFinite : FiniteDimensional ℂ (Carrier a b q ha hb) :=
body · 1 line
  inferInstanceAs (FiniteDimensional ℂ (PolynomialCarrier.Carrier a q ha ⊗[ℂ] PolynomialCarrier.Carrier b q hb))
1810
abbrev rep : Representation ℂ (Group q) (Carrier a b q ha hb) :=
body · 3 lines
  ExternalTensor.rep (E := PolynomialCarrier.Carrier a q ha)
    (F := PolynomialCarrier.Carrier b q hb)
    (ordinary a q ha).toRepresentation (ordinary b q hb).toRepresentation
1815
instance irreducible : (rep a b q ha hb).IsIrreducible := by
body · 3 lines
  let := ordinary_irreducible a q ha
  let := ordinary_irreducible b q hb
  exact ExternalTensor.irreducible _ _ (ordinary_rep_unitary a q ha) (ordinary_rep_unitary b q hb)
1820
def inclusion : Carrier a b q ha hb →ₗᵢ[ℂ]
    PairRaw (q := q) (ι := Cells a) (κ := Cells b) :=
body · 2 lines
  TensorProduct.mapIsometry (E := PolynomialCarrier.Carrier a q ha) (F := PolynomialCarrier.Carrier b q hb)
    (PolynomialCarrier.inclusion a q ha) (PolynomialCarrier.inclusion b q hb)
1825
lemma inclusion_equivariant (g : Group q) (v : Carrier a b q ha hb) :
    inclusion a b q ha hb (rep a b q ha hb g v) =
      pairRep g (inclusion a b q ha hb v) := by
proof · 3 lines
  induction v using TensorProduct.inductionOn with
  | tmul x y => rfl
  | add x y hx hy => simp [hx,hy]
1832
lemma rep_unitary (g : Group q) (v w : Carrier a b q ha hb) :
    ⟪rep a b q ha hb g v,rep a b q ha hb g w⟫_ℂ = ⟪v,w⟫_ℂ := by
proof · 4 lines
  rw [← (inclusion a b q ha hb).inner_map_map,
    ← (inclusion a b q ha hb).inner_map_map v w,
    inclusion_equivariant, inclusion_equivariant]
  exact SignedTensor.pairRep_unitary g _ _
1839
def hermitianAction (A : Mat q × Mat q) (hA : A.1.IsHermitian) (hB : A.2.IsHermitian) :
    Carrier a b q ha hb →ₗ[ℂ] Carrier a b q ha hb :=
body · 2 lines
  TensorProduct.map (PolynomialCarrier.hermitianAction a q ha A.1 hA)
    (PolynomialCarrier.hermitianAction b q hb A.2 hB)
1844
lemma inclusion_hermitianAction (A : Mat q × Mat q) (hA : A.1.IsHermitian) (hB : A.2.IsHermitian)
    (v : Carrier a b q ha hb) :
    inclusion a b q ha hb (hermitianAction a b q ha hb A hA hB v) =
      pairAction A (inclusion a b q ha hb v) := by
proof · 9 lines
  induction v using TensorProduct.inductionOn with
  | tmul x y =>
    simp only [inclusion, hermitianAction, TensorProduct.mapIsometry_apply,
      TensorProduct.map_tmul, pairAction_tmul]
    change PolynomialCarrier.inclusion a q ha (PolynomialCarrier.hermitianAction a q ha A.1 hA x) ⊗ₜ[ℂ]
      PolynomialCarrier.inclusion b q hb (PolynomialCarrier.hermitianAction b q hb A.2 hB y) = _
    rw [PolynomialCarrier.inclusion_hermitianAction, PolynomialCarrier.inclusion_hermitianAction]
    rfl
  | add x y hx hy => simp [hx,hy]
1862
theorem intertwiner_hermitian
    (L : (rep a b q ha hb).IntertwiningMap (SignedTensor.rep (q := q) (τ := τ)))
    (A : Mat q × Mat q) (hA : A.1.IsHermitian) (hB : A.2.IsHermitian)
    (v : Carrier a b q ha hb) :
    L (hermitianAction a b q ha hb A hA hB v) = TensorRep.action (block A) (L v) := by
proof · 14 lines
  let J := inclusion a b q ha hb
  let L' := L.toLinearMap.comp (LinearMap.adjoint J.toLinearMap)
  have hequiv (g : Group q) (x : PairRaw (q := q) (ι := Cells a) (κ := Cells b)) :
      L' (pairRep g x) = SignedTensor.rep g (L' x) := by
    dsimp [L']
    rw [UnitaryNaturality.adjoint_intertwines (rep a b q ha hb) pairRep
      (rep_unitary a b q ha hb) SignedTensor.pairRep_unitary J.toLinearMap
      (inclusion_equivariant a b q ha hb), L.isIntertwining]
  have h := SignedTensor.intertwiner_hermitian L' hequiv A hA hB (J v)
  have hj (x : Carrier a b q ha hb) : L' (J x) = L x := by
    dsimp [L']
    rw [UnitaryNaturality.adjoint_isometry]
  rw [← inclusion_hermitianAction a b q ha hb A hA hB v, hj, hj] at h
  exact h
1889
lemma continuous_action (v : EuclideanSpace ℂ (ι → n)) :
    Continuous (fun A : Matrix n n ℂ => action A v) := by
proof · 4 lines
  change Continuous (fun A : Matrix n n ℂ =>
    WithLp.toLp 2 (fun x : ι → n => ∑ y, (∏ i, A (x i) (y i)) * v y))
  apply (PiLp.continuous_toLp 2 (fun _ : ι → n => ℂ)).comp
  fun_prop
1896
lemma continuous_rep : Continuous (fun g : Matrix.unitaryGroup n ℂ =>
    (rep (ι := ι) g).toContinuousLinearMap) := by
proof · 3 lines
  apply continuous_clm_apply.mpr
  intro v
  exact (continuous_action v).comp continuous_subtype_val
1906
lemma continuous_rep : Continuous (fun g : Matrix.unitaryGroup (Fin q) ℂ =>
    ((ordinary D q hq).toRepresentation g).toContinuousLinearMap) := by
proof · 7 lines
  let : InnerProductSpace ℂ (ordinary D q hq).toSubmodule := carrierInner D q hq
  apply (continuous_clm_apply (𝕜 := ℂ) (E := Carrier D q hq) (F := Carrier D q hq)).mpr
  intro v
  apply (inclusion D q hq).comp_continuous_iff.mp
  change Continuous (fun g : Matrix.unitaryGroup (Fin q) ℂ =>
    TensorRep.rep g (inclusion D q hq v))
  exact continuous_clm_apply.mp (TensorRep.continuous_rep (ι := Cells D) (n := Fin q)) _
1923
lemma continuous_rep (ρ : Representation ℂ G E) (σ : Representation ℂ H F)
    (hρ : Continuous (fun g => (ρ g).toContinuousLinearMap))
    (hσ : Continuous (fun h => (σ h).toContinuousLinearMap)) :
    Continuous (fun g : G × H => (rep ρ σ g).toContinuousLinearMap) := by
proof · 14 lines
  apply continuous_clm_apply.mpr
  intro v
  induction v using TensorProduct.inductionOn with
  | tmul x y =>
    change Continuous (fun g : G × H => ρ g.1 x ⊗ₜ[ℂ] σ g.2 y)
    exact TensorProduct.continuous_tmul.comp
      (((continuous_clm_apply.mp hρ x).comp continuous_fst).prodMk
        ((continuous_clm_apply.mp hσ y).comp continuous_snd))
  | add x y hx hy =>
    have hh : Continuous (fun g : G × H => (rep ρ σ g).toContinuousLinearMap x +
        (rep ρ σ g).toContinuousLinearMap y) := hx.add hy
    convert hh using 1
    funext g
    exact map_add _ x y
1945
lemma continuous_block : Continuous (block (q := q)) := by
proof · 8 lines
  apply continuous_pi
  intro i
  apply continuous_pi
  intro j
  change Continuous (fun A : Mat q × Mat q => if i.2 = j.2 then (cond i.2 A.2 A.1) i.1 j.1 else 0)
  split_ifs
  · cases i.2 <;> simp only [Bool.cond_false, Bool.cond_true] <;> fun_prop
  · fun_prop
1955
lemma continuous_blockGroup : Continuous (blockGroup (q := q)) := by
proof · 3 lines
  apply Continuous.subtype_mk
  exact continuous_block.comp ((continuous_subtype_val.comp continuous_fst).prodMk
    (continuous_subtype_val.comp continuous_snd))
1960
lemma continuous_rep : Continuous (fun g : Group q => (rep (τ := τ) g).toContinuousLinearMap) :=
proof · 1 line
  TensorRep.continuous_rep.comp continuous_blockGroup
1967
lemma continuous_rep : Continuous (fun g : Group q => (rep a b q ha hb g).toContinuousLinearMap) :=
proof · 2 lines
  ExternalTensor.continuous_rep _ _ (PolynomialCarrier.continuous_rep a q ha)
    (PolynomialCarrier.continuous_rep b q hb)
1976
lemma tensor_posSemidef (a : ι → Matrix n n ℂ) (ha : ∀ i, (a i).PosSemidef) :
    (tensor a).PosSemidef := by
proof · 10 lines
  let r : ι → Matrix n n ℂ := fun i => CFC.sqrt (a i)
  have he : tensor a = star (tensor r) * tensor r := by
    rw [← tensor_star, ← tensor_mul]
    congr 1
    funext i
    dsimp [r]
    rw [(IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg (a i))).star_eq,
      CFC.sqrt_mul_sqrt_self (a i) (ha i).nonneg]
  rw [he]
  exact Matrix.posSemidef_conjTranspose_mul_self _
1989
lemma action_positive (a : Matrix n n ℂ) (ha : a.PosSemidef) :
    LinearMap.IsPositive (𝕜 := ℂ) (E := EuclideanSpace ℂ (ι → n)) (action a) :=
proof · 1 line
  Matrix.isPositive_toEuclideanLin_iff.mpr (tensor_posSemidef (fun _ : ι => a) (fun _ => ha))
1999
lemma hermitianAction_positive (a : Matrix (Fin q) (Fin q) ℂ) (ha : a.PosSemidef) :
    LinearMap.IsPositive (𝕜 := ℂ) (E := Carrier D q hq) (hermitianAction D q hq a ha.isHermitian) := by
proof · 4 lines
  refine ⟨hermitianAction_symmetric D q hq a ha.isHermitian, ?_⟩
  intro x
  rw [← (inclusion D q hq).inner_map_map, inclusion_hermitianAction]
  exact (TensorRep.action_positive a ha).2 (inclusion D q hq x)
2006
def highestVector : Carrier D q hq :=
body · 1 line
  ⟨⟨highest D q hq,highest_mem D q hq⟩, self_mem_cyclic _ _⟩
2009
lemma highestVector_ne_zero : highestVector D q hq ≠ 0 := by
proof · 3 lines
  intro h
  have he := congrArg (inclusion D q hq) h
  exact highest_ne_zero D q hq (by simpa [highestVector] using he)
2014
lemma inclusion_linear_hermitianAction (A : Matrix (Fin q) (Fin q) ℂ) (hA : A.IsHermitian)
    (v : Carrier D q hq) :
    (inclusion D q hq).toLinearMap (hermitianAction D q hq A hA v) =
      TensorRep.action A ((inclusion D q hq).toLinearMap v) :=
proof · 1 line
  inclusion_hermitianAction D q hq A hA v
2020
lemma highestVector_diagonal (t : Fin q → ℝ) :
    hermitianAction D q hq (Matrix.diagonal (fun i => (t i : ℂ)))
      (Matrix.isHermitian_diagonal_iff.mpr (fun i => by simp [IsSelfAdjoint])) (highestVector D q hq) =
        (highestMonomial D q hq t : ℂ) • highestVector D q hq := by
proof · 10 lines
  have hinj : Function.Injective (inclusion D q hq).toLinearMap := by
    intro x y h
    exact Subtype.ext (Subtype.ext h)
  apply hinj
  rw [inclusion_linear_hermitianAction, map_smul]
  change TensorRep.action (Matrix.diagonal (fun i => (t i : ℂ))) (highest D q hq) =
    (highestMonomial D q hq t : ℂ) • highest D q hq
  rw [highest_diagonal]
  congr 1
  exact (map_prod Complex.ofRealHom _ _).symm
2035
lemma card_rowColor (j : Fin q) :
    (Finset.univ.filter fun i : Cells D => rowColor D q hq i = j).card = D.rowLen j := by
proof · 5 lines
  rw [← Fintype.card_subtype]
  calc
    Fintype.card {i : Cells D // rowColor D q hq i = j} = Fintype.card {i : Cells D // i.val.1 = j.val} :=
      Fintype.card_congr (Equiv.subtypeEquivRight (fun i => Fin.ext_iff))
    _ = D.rowLen j := card_row_subtype D j
2043
lemma highestMonomial_eq_rowLen (t : Fin q → ℝ) :
    highestMonomial D q hq t = ∏ j : Fin q, t j ^ D.rowLen j := by
proof · 3 lines
  unfold highestMonomial
  rw [← Finset.prod_fiberwise' Finset.univ (rowColor D q hq) t]
  simp only [Finset.prod_const, card_rowColor]
2050
lemma sum_rowLen_eq_card : ∑ j : Fin q, D.rowLen j = Fintype.card (Cells D) := by
proof · 3 lines
  have h := Finset.sum_fiberwise Finset.univ (rowColor D q hq) (fun _ : Cells D => (1 : ℕ))
  simp only [Finset.sum_const, nsmul_eq_mul, mul_one, card_rowColor, Finset.card_univ] at h
  exact_mod_cast h
2067
lemma mapL_nonneg (A : E →L[ℂ] E) (B : F →L[ℂ] F) (hA : 0 ≤ A) (hB : 0 ≤ B) :
    0 ≤ TensorProduct.mapL A B := by
proof · 15 lines
  let a := CFC.sqrt A
  let b := CFC.sqrt B
  have ha : star a = a := (IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg A)).star_eq
  have hb : star b = b := (IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg B)).star_eq
  have hstar : star (TensorProduct.mapL a b) = TensorProduct.mapL a b := by
    change ContinuousLinearMap.adjoint (TensorProduct.mapL a b) = _
    rw [TensorProduct.adjoint_mapL]
    change TensorProduct.mapL (star a) (star b) = _
    rw [ha,hb]
  have he : star (TensorProduct.mapL a b) * TensorProduct.mapL a b = TensorProduct.mapL A B := by
    rw [hstar, ← TensorProduct.mapL_mul]
    dsimp [a,b]
    rw [CFC.sqrt_mul_sqrt_self A hA, CFC.sqrt_mul_sqrt_self B hB]
  rw [← he]
  exact star_mul_self_nonneg _
2085
lemma map_positive (A : E →ₗ[ℂ] E) (B : F →ₗ[ℂ] F) (hA : A.IsPositive) (hB : B.IsPositive) :
    (TensorProduct.map A B).IsPositive := by
proof · 7 lines
  have ha : 0 ≤ A.toContinuousLinearMap :=
    (ContinuousLinearMap.nonneg_iff_isPositive).mpr hA
  have hb : 0 ≤ B.toContinuousLinearMap :=
    (ContinuousLinearMap.nonneg_iff_isPositive).mpr hB
  have h := (ContinuousLinearMap.nonneg_iff_isPositive).mp (mapL_nonneg _ _ ha hb)
  change ((TensorProduct.mapL A.toContinuousLinearMap B.toContinuousLinearMap).toLinearMap).IsPositive at h
  simpa only [TensorProduct.toLinearMap_mapL, LinearMap.coe_toContinuousLinearMap] using h
2095
lemma map_norm_bound (A : E →ₗ[ℂ] E) (B : F →ₗ[ℂ] F)
    (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b)
    (hA : ∀ x, ‖A x‖ ≤ a * ‖x‖) (hB : ∀ y, ‖B y‖ ≤ b * ‖y‖)
    (v : E ⊗[ℂ] F) : ‖TensorProduct.map A B v‖ ≤ (a*b)*‖v‖ := by
proof · 10 lines
  have ha' : ‖A.toContinuousLinearMap‖ ≤ a :=
    ContinuousLinearMap.opNorm_le_bound _ ha hA
  have hb' : ‖B.toContinuousLinearMap‖ ≤ b :=
    ContinuousLinearMap.opNorm_le_bound _ hb hB
  calc
    ‖TensorProduct.map A B v‖ ≤ ‖TensorProduct.mapL A.toContinuousLinearMap B.toContinuousLinearMap‖ * ‖v‖ :=
      by simpa only [TensorProduct.mapL_apply, LinearMap.coe_toContinuousLinearMap] using
        (ContinuousLinearMap.le_opNorm (TensorProduct.mapL A.toContinuousLinearMap B.toContinuousLinearMap) v)
    _ ≤ (a*b)*‖v‖ := mul_le_mul_of_nonneg_right
      ((TensorProduct.norm_mapL_le _ _).trans (mul_le_mul ha' hb' (norm_nonneg _) ha)) (norm_nonneg _)
2119
def entropy (n : ι → ℕ) : ℝ :=
body · 1 line
  (∑ i, n i : ℕ) * Real.log (∑ i, n i : ℕ) - ∑ i, (n i : ℝ) * Real.log (n i)
2122
lemma sum_log_ratio (n : ι → ℕ) (p : ℕ) (hp : 0 < p) (hn : ∑ i, n i = p) :
    ∑ i, (n i : ℝ) * Real.log ((n i : ℝ) / p) = -entropy n := by
proof · 11 lines
  have hp0 : (p : ℝ) ≠ 0 := by exact_mod_cast hp.ne'
  have he (i : ι) : (n i : ℝ) * Real.log ((n i : ℝ) / p) =
      (n i : ℝ) * Real.log (n i) - (n i : ℝ) * Real.log p := by
    by_cases hi : n i = 0
    · simp [hi]
    · rw [Real.log_div (by exact_mod_cast hi) hp0, mul_sub]
  simp only [he, Finset.sum_sub_distrib, ← Finset.sum_mul]
  have hn' : ∑ i, (n i : ℝ) = p := by exact_mod_cast hn
  rw [hn']
  simp only [entropy, hn]
  ring
2136
lemma power_le_exponential (k p : ℕ) (hp : 0 < p) (t : ℝ) (ht : 0 ≤ t) :
    t ^ k ≤ Real.exp ((k : ℝ) * Real.log ((k : ℝ) / p) + p * t - k) := by
proof · 21 lines
  by_cases hk : k = 0
  · subst k
    simpa using (Real.one_le_exp_iff.mpr (mul_nonneg (Nat.cast_nonneg p) ht))
  by_cases ht0 : t = 0
  · simp only [ht0, zero_pow hk]
    exact (Real.exp_pos _).le
  have hk' : 0 < (k : ℝ) := by exact_mod_cast Nat.pos_of_ne_zero hk
  have hp' : 0 < (p : ℝ) := by exact_mod_cast hp
  have ht' : 0 < t := lt_of_le_of_ne ht (Ne.symm ht0)
  have hr : 0 < (k : ℝ) / p := div_pos hk' hp'
  have hlog := Real.log_le_sub_one_of_pos (div_pos ht' hr)
  rw [Real.log_div ht0 hr.ne'] at hlog
  have hmul := mul_le_mul_of_nonneg_left hlog hk'.le
  have hc : (k : ℝ) * (t / ((k : ℝ) / p) - 1) = p * t - k := by
    field_simp
    
  rw [hc] at hmul
  calc
    t ^ k = Real.exp ((k : ℝ) * Real.log t) := by
      rw [Real.exp_nat_mul, Real.exp_log ht']
    _ ≤ _ := Real.exp_le_exp.mpr (by nlinarith)
2162
theorem prod_pow_le (n : ι → ℕ) (t : ι → ℝ) (ht : ∀ i, 0 ≤ t i)
    (hsum : ∑ i, t i ≤ 1) :
    ∏ i, t i ^ n i ≤ Real.exp (-entropy n) := by
proof · 23 lines
  classical
  let p : ℕ := ∑ i, n i
  by_cases hp : p = 0
  · have hn : ∀ i, n i = 0 := by
      intro i
      have hle := Finset.single_le_sum (fun j (_ : j ∈ Finset.univ) => Nat.zero_le (n j)) (Finset.mem_univ i)
      change n i ≤ p at hle
      omega
    simp [entropy, hn]
  have hp' : 0 < p := Nat.pos_of_ne_zero hp
  calc
    ∏ i, t i ^ n i ≤ ∏ i, Real.exp ((n i : ℝ) * Real.log ((n i : ℝ) / p) + p * t i - n i) :=
      Finset.prod_le_prod₀ (fun i _ => pow_nonneg (ht i) _) (fun i _ => power_le_exponential _ p hp' _ (ht i))
    _ = Real.exp (∑ i, ((n i : ℝ) * Real.log ((n i : ℝ) / p) + p * t i - n i)) :=
      (Real.exp_sum _ _).symm
    _ ≤ Real.exp (-entropy n) := by
      apply Real.exp_le_exp.mpr
      simp only [Finset.sum_sub_distrib, Finset.sum_add_distrib, ← Finset.mul_sum]
      rw [sum_log_ratio n p hp' rfl]
      have hn : ∑ i, (n i : ℝ) = p := by simp [p]
      rw [hn]
      have h := mul_le_mul_of_nonneg_left hsum (Nat.cast_nonneg p : (0 : ℝ) ≤ p)
      nlinarith
2189
lemma optimal_prod (n : ι → ℕ) (p : ℕ) (hp : 0 < p) (hn : ∑ i, n i = p) :
    ∏ i, ((n i : ℝ) / p) ^ n i = Real.exp (-entropy n) := by
proof · 7 lines
  rw [← sum_log_ratio n p hp hn, Real.exp_sum]
  apply Finset.prod_congr rfl
  intro i _
  by_cases hi : n i = 0
  · simp [hi]
  have hpos : 0 < (n i : ℝ) / p := div_pos (by exact_mod_cast Nat.pos_of_ne_zero hi) (by exact_mod_cast hp)
  rw [Real.exp_nat_mul, Real.exp_log hpos]
2209
lemma eigenvalues_nonneg (A : E →ₗ[ℂ] E) (hA : A.IsPositive)
    (i : Fin (Module.finrank ℂ E)) : 0 ≤ hA.isSymmetric.eigenvalues rfl i := by
proof · 4 lines
  have h := hA.inner_nonneg_right (hA.isSymmetric.eigenvectorBasis rfl i)
  rw [hA.isSymmetric.apply_eigenvectorBasis rfl i, inner_smul_right,
    (hA.isSymmetric.eigenvectorBasis rfl).inner_eq_one] at h
  simpa only [mul_one, RCLike.ofReal_nonneg] using h
2217
theorem eigenvalue_le_trace (A : E →ₗ[ℂ] E) (hA : A.IsPositive)
    (v : E) (hv : v ≠ 0) (t : ℝ) (he : A v = (t : ℂ) • v) :
    t ≤ (LinearMap.trace ℂ E A).re := by
proof · 17 lines
  classical
  let b := hA.isSymmetric.eigenvectorBasis rfl
  have hcoord : ∃ i, b.repr v i ≠ 0 := by
    by_contra! hh
    apply hv
    apply b.repr.injective
    ext i
    simpa using hh i
  obtain ⟨i,hi⟩ := hcoord
  have h := congrArg (fun x : E => b.repr x i) he
  rw [hA.isSymmetric.eigenvectorBasis_apply_self_apply, map_smul, PiLp.smul_apply,
    smul_eq_mul] at h
  have ht : hA.isSymmetric.eigenvalues rfl i = t := by
    exact Complex.ofReal_injective (mul_right_cancel₀ hi h)
  change t ≤ RCLike.re (LinearMap.trace ℂ E A)
  rw [hA.isSymmetric.re_trace_eq_sum_eigenvalues rfl, ← ht]
  exact Finset.single_le_sum (fun j _ => eigenvalues_nonneg A hA j) (Finset.mem_univ i)
2253
def space : Subrepresentation σ where
body · 12 lines
  toSubmodule := Submodule.span ℂ (Set.range fun z : ρ.IntertwiningMap σ × E => z.1 z.2)
  apply_mem_toSubmodule := by
    intro g v hv
    induction hv using Submodule.span_induction with
    | mem v hv =>
      obtain ⟨⟨f,x⟩,rfl⟩ := hv
      apply Submodule.subset_span
      refine ⟨⟨f,ρ g x⟩, ?_⟩
      exact Representation.IntertwiningMap.isIntertwining ρ σ f g x
    | zero => simp
    | add x y hx hy ihx ihy => simpa only [map_add] using Submodule.add_mem _ ihx ihy
    | smul a x hx ih => simpa only [map_smul] using Submodule.smul_mem _ a ih
2267
def projection : F →L[ℂ] F := (space ρ σ).toSubmodule.starProjection
2269
lemma projection_mem
    {G : Type*}
    [contextInstance2131_21 : Group G]
    {E : Type*} {F : Type*}
    [contextInstance2132_23 : NormedAddCommGroup E]
    [contextInstance2132_46 : InnerProductSpace ℂ E]
    [contextInstance2133_2 : NormedAddCommGroup F]
    [contextInstance2133_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [contextInstance2134_26 : FiniteDimensional ℂ F]
    (ρ : Representation ℂ G E) (σ : Representation ℂ G F) (v : F) : projection ρ σ v ∈ space ρ σ :=
proof · 1 line
  (space ρ σ).toSubmodule.starProjection_apply_mem v
2282
lemma projection_norm_apply_le
    {G : Type*}
    [contextInstance2131_21 : Group G]
    {E : Type*} {F : Type*}
    [contextInstance2132_23 : NormedAddCommGroup E]
    [contextInstance2132_46 : InnerProductSpace ℂ E]
    [contextInstance2133_2 : NormedAddCommGroup F]
    [contextInstance2133_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [contextInstance2134_26 : FiniteDimensional ℂ F]
    (ρ : Representation ℂ G E) (σ : Representation ℂ G F) (v : F) : ‖projection ρ σ v‖ ≤ ‖v‖ :=
proof · 1 line
  (space ρ σ).toSubmodule.norm_starProjection_apply_le v
2295
lemma map_mem
    {G : Type*}
    [contextInstance2131_21 : Group G]
    {E : Type*} {F : Type*}
    [contextInstance2132_23 : NormedAddCommGroup E]
    [contextInstance2132_46 : InnerProductSpace ℂ E]
    [contextInstance2133_2 : NormedAddCommGroup F]
    [contextInstance2133_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F]
    (ρ : Representation ℂ G E) (σ : Representation ℂ G F) (f : ρ.IntertwiningMap σ) (x : E) : f x ∈ space ρ σ :=
proof · 1 line
  Submodule.subset_span ⟨⟨f,x⟩,rfl⟩
2308
lemma endomorphism_mem (d : σ.IntertwiningMap σ) (v : F) (hv : v ∈ space ρ σ) :
    d v ∈ space ρ σ := by
proof · 7 lines
  induction hv using Submodule.span_induction with
  | mem v hv =>
    obtain ⟨⟨f,x⟩,rfl⟩ := hv
    exact map_mem ρ σ (d.comp f) x
  | zero => simp only [map_zero]; exact (space ρ σ).toSubmodule.zero_mem
  | add x y hx hy ihx ihy => rw [map_add]; exact (space ρ σ).toSubmodule.add_mem ihx ihy
  | smul a x hx ih => rw [map_smul]; exact (space ρ σ).toSubmodule.smul_mem a ih
2318
lemma natural_mem (a : E →ₗ[ℂ] E) (A : F →ₗ[ℂ] F)
    (h : ∀ f : ρ.IntertwiningMap σ, ∀ x, A (f x) = f (a x))
    (v : F) (hv : v ∈ space ρ σ) : A v ∈ space ρ σ := by
proof · 8 lines
  induction hv using Submodule.span_induction with
  | mem v hv =>
    obtain ⟨⟨f,x⟩,rfl⟩ := hv
    rw [h]
    exact map_mem ρ σ f _
  | zero => simp only [map_zero]; exact (space ρ σ).toSubmodule.zero_mem
  | add x y hx hy ihx ihy => rw [map_add]; exact (space ρ σ).toSubmodule.add_mem ihx ihy
  | smul z x hx ih => rw [map_smul]; exact (space ρ σ).toSubmodule.smul_mem z ih
2330
lemma scalar_on_space
    {G : Type*}
    [contextInstance2131_21 : Group G]
    {E : Type*} {F : Type*}
    [contextInstance2132_23 : NormedAddCommGroup E]
    [contextInstance2132_46 : InnerProductSpace ℂ E]
    [contextInstance2133_2 : NormedAddCommGroup F]
    [contextInstance2133_25 : InnerProductSpace ℂ F]
    [FiniteDimensional ℂ E]
    [FiniteDimensional ℂ F]
    (ρ : Representation ℂ G E) (σ : Representation ℂ G F) (A : F →ₗ[ℂ] F) (c : ℂ)
    (h : ∀ f : ρ.IntertwiningMap σ, ∀ x, A (f x) = c • f x)
    (v : F) (hv : v ∈ space ρ σ) : A v = c • v := by
proof · 5 lines
  induction hv using Submodule.span_induction with
  | mem v hv => obtain ⟨⟨f,x⟩,rfl⟩ := hv; exact h f x
  | zero => simp
  | add x y hx hy ihx ihy => simp [ihx, ihy, smul_add]
  | smul z x hx ih => simp [ih, smul_comm c z]
2349
lemma exists_nonzero_adjoint (v : F) (hv : v ∈ space ρ σ) (hv0 : v ≠ 0) :
    ∃ f : ρ.IntertwiningMap σ, LinearMap.adjoint f.toLinearMap v ≠ 0 := by
proof · 12 lines
  by_contra! hn
  have horth (w : F) (hw : w ∈ space ρ σ) : ⟪w,v⟫_ℂ = 0 := by
    induction hw using Submodule.span_induction with
    | mem w hw =>
      obtain ⟨⟨f,x⟩,rfl⟩ := hw
      change ⟪f.toLinearMap x,v⟫_ℂ = 0
      rw [← LinearMap.adjoint_inner_right, hn]
      simp
    | zero => simp
    | add x y hx hy ihx ihy => simp [ihx, ihy]
    | smul z x hx ih => simp [ih]
  exact hv0 (inner_self_eq_zero.mp (horth v hv))
2364
lemma transfer_eigenvalue_bound (a : E →ₗ[ℂ] E) (A : F →ₗ[ℂ] F)
    (ha : a.IsSymmetric) (hA : A.IsSymmetric)
    (hnat : ∀ f : ρ.IntertwiningMap σ, ∀ x, A (f x) = f (a x))
    (c : ℝ) (hc : ∀ x, ‖a x‖ ≤ c * ‖x‖)
    (v : F) (hv : v ∈ space ρ σ) (hv0 : v ≠ 0)
    (t : ℂ) (ht : A v = t • v) : ‖t‖ ≤ c := by
proof · 10 lines
  obtain ⟨f,hf⟩ := exists_nonzero_adjoint ρ σ v hv hv0
  have he : A.comp f.toLinearMap = f.toLinearMap.comp a := by ext x; exact hnat f x
  have hadj := congrArg LinearMap.adjoint he
  rw [LinearMap.adjoint_comp, LinearMap.adjoint_comp, ha.adjoint_eq, hA.adjoint_eq] at hadj
  have hev : a (LinearMap.adjoint f.toLinearMap v) = t • LinearMap.adjoint f.toLinearMap v := by
    have hh := congrArg (fun L : F →ₗ[ℂ] E => L v) hadj
    simpa only [LinearMap.comp_apply, ht, map_smul] using hh.symm
  have hh := hc (LinearMap.adjoint f.toLinearMap v)
  rw [hev, norm_smul] at hh
  exact (mul_le_mul_iff_left₀ (norm_pos_iff.mpr hf)).mp hh
2383
theorem norm_bound (a : E →ₗ[ℂ] E) (A : F →ₗ[ℂ] F)
    (ha : a.IsSymmetric) (hA : A.IsSymmetric)
    (hnat : ∀ f : ρ.IntertwiningMap σ, ∀ x, A (f x) = f (a x))
    (c : ℝ) (hc0 : 0 ≤ c) (hc : ∀ x, ‖a x‖ ≤ c * ‖x‖)
    (v : F) (hv : v ∈ space ρ σ) : ‖A v‖ ≤ c * ‖v‖ := by
proof · 21 lines
  classical
  let S := (space ρ σ).toSubmodule
  have hinv : ∀ w ∈ S, A w ∈ S := natural_mem ρ σ a A hnat
  let B : S →ₗ[ℂ] S := A.restrict hinv
  have hB : B.IsSymmetric := hA.restrict_invariant hinv
  let b := hB.eigenvectorBasis rfl
  have hb (i : Fin (Module.finrank ℂ S)) : ‖(hB.eigenvalues rfl i : ℂ)‖ ≤ c := by
    apply transfer_eigenvalue_bound ρ σ a A ha hA hnat c hc (b i).val (b i).property
    · intro hh
      have hn := b.orthonormal.ne_zero i
      exact hn (Subtype.ext hh)
    · exact congrArg Subtype.val (hB.apply_eigenvectorBasis rfl i)
  let w : S := ⟨v,hv⟩
  change ‖B w‖ ≤ c * ‖w‖
  apply (sq_le_sq₀ (norm_nonneg _) (mul_nonneg hc0 (norm_nonneg _))).mp
  rw [← b.repr.norm_map (B w), ← b.repr.norm_map w, mul_pow,
    EuclideanSpace.norm_sq_eq, EuclideanSpace.norm_sq_eq, Finset.mul_sum]
  apply Finset.sum_le_sum
  intro i _
  rw [hB.eigenvectorBasis_apply_self_apply, norm_mul, mul_pow]
  exact mul_le_mul_of_nonneg_right (pow_le_pow_left₀ (norm_nonneg _) (hb i) 2) (sq_nonneg _)
2410
lemma projection_commute_of_invariant
    {G : Type*}
    [Group G]
    {F : Type*}
    [contextInstance2133_2 : NormedAddCommGroup F]
    [contextInstance2133_25 : InnerProductSpace ℂ F]
    [contextInstance2134_26 : FiniteDimensional ℂ F] (S : Submodule ℂ F) (A : F →ₗ[ℂ] F)
    (hA : A.IsSymmetric) (hinv : ∀ v ∈ S, A v ∈ S) (v : F) :
    S.starProjection (A v) = A (S.starProjection v) := by
proof · 5 lines
  apply S.eq_starProjection_of_mem_of_inner_eq_zero
  · exact hinv _ (S.starProjection_apply_mem v)
  · intro w hw
    rw [← map_sub, hA]
    exact S.starProjection_inner_eq_zero v _ (hinv _ hw)
2425
lemma projection_commute_endomorphism (d : σ.IntertwiningMap σ)
    (hd : d.toLinearMap.IsSymmetric) (v : F) :
    projection ρ σ (d v) = d (projection ρ σ v) :=
proof · 1 line
  projection_commute_of_invariant (G := G) _ d.toLinearMap hd (endomorphism_mem ρ σ d) v
2430
lemma projection_commute_natural (a : E →ₗ[ℂ] E) (A : F →ₗ[ℂ] F)
    (hA : A.IsSymmetric)
    (h : ∀ f : ρ.IntertwiningMap σ, ∀ x, A (f x) = f (a x)) (v : F) :
    projection ρ σ (A v) = A (projection ρ σ v) :=
proof · 1 line
  projection_commute_of_invariant (G := G) _ A hA (natural_mem ρ σ a A h) v
2442
def IsEvenDensity (A : Mat q × Mat q) : Prop :=
body · 1 line
  A.1.PosSemidef ∧ A.2.PosSemidef ∧ Matrix.trace A.1 + Matrix.trace A.2 = 1
2445
lemma block_posSemidef (A : Mat q × Mat q) (hA : A.1.PosSemidef) (hB : A.2.PosSemidef) :
    (block A).PosSemidef := by
proof · 13 lines
  let R : Mat q × Mat q := (CFC.sqrt A.1, CFC.sqrt A.2)
  have he : block A = star (block R) * block R := by
    rw [← block_star, ← map_mul]
    congr 1
    apply Prod.ext
    · change A.1 = star (CFC.sqrt A.1) * CFC.sqrt A.1
      rw [(IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg A.1)).star_eq,
        CFC.sqrt_mul_sqrt_self A.1 hA.nonneg]
    · change A.2 = star (CFC.sqrt A.2) * CFC.sqrt A.2
      rw [(IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg A.2)).star_eq,
        CFC.sqrt_mul_sqrt_self A.2 hB.nonneg]
  rw [he]
  exact Matrix.posSemidef_conjTranspose_mul_self _
2467
lemma hermitianAction_positive (A : Mat q × Mat q) (hA : A.1.PosSemidef) (hB : A.2.PosSemidef) :
    LinearMap.IsPositive (𝕜 := ℂ) (E := Carrier a b q ha hb)
      (hermitianAction a b q ha hb A hA.isHermitian hB.isHermitian) :=
proof · 4 lines
  TensorPositive.map_positive (E := PolynomialCarrier.Carrier a q ha)
    (F := PolynomialCarrier.Carrier b q hb) _ _
    (PolynomialCarrier.hermitianAction_positive a q ha A.1 hA)
    (PolynomialCarrier.hermitianAction_positive b q hb A.2 hB)
2475
lemma hermitianAction_norm_bound (A : Mat q × Mat q) (hA : A.1.PosSemidef) (hB : A.2.PosSemidef)
    (v : Carrier a b q ha hb) :
    ‖hermitianAction a b q ha hb A hA.isHermitian hB.isHermitian v‖ ≤
      (PolynomialCarrier.highestMonomial a q ha (SortedSpectrum.values A.1 hA.isHermitian) *
       PolynomialCarrier.highestMonomial b q hb (SortedSpectrum.values A.2 hB.isHermitian)) * ‖v‖ := by
proof · 6 lines
  apply TensorPositive.map_norm_bound (E := PolynomialCarrier.Carrier a q ha)
    (F := PolynomialCarrier.Carrier b q hb)
  · exact Finset.prod_nonneg (fun i _ => SortedSpectrum.values_nonneg A.1 hA.isHermitian hA _)
  · exact Finset.prod_nonneg (fun i _ => SortedSpectrum.values_nonneg A.2 hB.isHermitian hB _)
  · exact PolynomialCarrier.hermitianAction_norm_bound a q ha A.1 hA
  · exact PolynomialCarrier.hermitianAction_norm_bound b q hb A.2 hB
2487
def parts : Fin q ⊕ Fin q → ℕ := Sum.elim (fun i => a.rowLen i) (fun i => b.rowLen i)
2488
def entropy : ℝ := EntropyMonomial.entropy (parts a b q)
2489
def dimension : ℕ := Module.finrank ℂ (Carrier a b q ha hb)
2492
lemma sum_parts : ∑ i, parts a b q i = Fintype.card (Cells a) + Fintype.card (Cells b) := by
proof · 3 lines
  rw [Fintype.sum_sum_type]
  change (∑ i : Fin q, a.rowLen i) + (∑ i : Fin q, b.rowLen i) = _
  rw [PolynomialCarrier.sum_rowLen_eq_card a q ha, PolynomialCarrier.sum_rowLen_eq_card b q hb]
2498
lemma entropy_eq_source : entropy a b q =
    (Fintype.card (Cells a) + Fintype.card (Cells b) : ℕ) *
      Real.log (Fintype.card (Cells a) + Fintype.card (Cells b) : ℕ) -
      (∑ i : Fin q, (a.rowLen i : ℝ) * Real.log (a.rowLen i)) -
      (∑ i : Fin q, (b.rowLen i : ℝ) * Real.log (b.rowLen i)) := by
proof · 4 lines
  unfold entropy EntropyMonomial.entropy
  rw [sum_parts a b q ha hb, Fintype.sum_sum_type]
  simp only [parts, Sum.elim_inl, Sum.elim_inr]
  ring
2508
lemma hermitianAction_density_bound (A : Mat q × Mat q) (hA : IsEvenDensity A)
    (v : Carrier a b q ha hb) :
    ‖hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian v‖ ≤
      Real.exp (-entropy a b q) * ‖v‖ := by
proof · 20 lines
  apply (hermitianAction_norm_bound a b q ha hb A hA.1 hA.2.1 v).trans
  apply mul_le_mul_of_nonneg_right _ (norm_nonneg _)
  rw [PolynomialCarrier.highestMonomial_eq_rowLen, PolynomialCarrier.highestMonomial_eq_rowLen]
  let t : Fin q ⊕ Fin q → ℝ := Sum.elim
    (SortedSpectrum.values A.1 hA.1.isHermitian) (SortedSpectrum.values A.2 hA.2.1.isHermitian)
  have ht (i) : 0 ≤ t i := by
    cases i with
    | inl i => exact SortedSpectrum.values_nonneg A.1 hA.1.isHermitian hA.1 i
    | inr i => exact SortedSpectrum.values_nonneg A.2 hA.2.1.isHermitian hA.2.1 i
  have hs : ∑ i, t i = 1 := by
    rw [Fintype.sum_sum_type]
    have he1 := SortedSpectrum.trace_eq_sum A.1 hA.1.isHermitian
    have he2 := SortedSpectrum.trace_eq_sum A.2 hA.2.1.isHermitian
    have h : (∑ i, (SortedSpectrum.values A.1 hA.1.isHermitian i : ℂ)) +
        (∑ i, (SortedSpectrum.values A.2 hA.2.1.isHermitian i : ℂ)) = 1 :=
      (congrArg₂ (· + ·) he1.symm he2.symm).trans hA.2.2
    have hh := congrArg Complex.re h
    simpa only [t, Sum.elim_inl, Sum.elim_inr, Complex.add_re, Complex.re_sum, Complex.ofReal_re, Complex.one_re] using hh
  have h := EntropyMonomial.prod_pow_le (parts a b q) t ht hs.le
  simpa only [Fintype.prod_sum_type, parts, t, entropy, Sum.elim_inl, Sum.elim_inr] using h
2539
lemma exists_optimal (n : ι → ℕ) : ∃ t : ι → ℝ,
    (∀ i, 0 ≤ t i) ∧ (∑ i, t i = 1) ∧ (∏ i, t i ^ n i = Real.exp (-entropy n)) := by
proof · 18 lines
  classical
  let p := ∑ i, n i
  by_cases hp : p = 0
  · have hn : ∀ i, n i = 0 := by
      intro i
      have h := Finset.single_le_sum (fun j (_ : j ∈ Finset.univ) => Nat.zero_le (n j)) (Finset.mem_univ i)
      change n i ≤ p at h
      omega
    let i₀ := Classical.choice (inferInstance : Nonempty ι)
    refine ⟨fun i => if i = i₀ then 1 else 0, ?_, ?_, ?_⟩
    · intro i; dsimp only; split_ifs <;> norm_num
    · simp
    · simp [hn, entropy]
  · refine ⟨fun i => (n i : ℝ) / p, fun i => div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _), ?_, ?_⟩
    · rw [← Finset.sum_div]
      have he : ∑ i, (n i : ℝ) = p := by simp [p]
      rw [he, div_self (by exact_mod_cast hp)]
    · exact optimal_prod n p (Nat.pos_of_ne_zero hp) rfl
2567
abbrev typeProjection : Raw (q := q) (τ := τ) →L[ℂ] Raw (q := q) (τ := τ) :=
body · 1 line
  Isotypic.projection (rep a b q ha hb) SignedTensor.rep
2570
lemma density_on_space (A : Mat q × Mat q) (hA : IsEvenDensity A)
    (v : Raw (q := q) (τ := τ))
    (hv : v ∈ Isotypic.space (rep a b q ha hb) (SignedTensor.rep (q := q) (τ := τ))) :
    ‖TensorRep.action (block A) v‖ ≤ Real.exp (-entropy a b q) * ‖v‖ := by
proof · 9 lines
  exact Isotypic.norm_bound (E := Carrier a b q ha hb) (F := Raw (q := q) (τ := τ))
    (rep a b q ha hb) (SignedTensor.rep (q := q) (τ := τ))
    (hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian)
    (TensorRep.action (block A))
    (hermitianAction_positive a b q ha hb A hA.1 hA.2.1).isSymmetric
    (TensorRep.action_positive (block A) (block_posSemidef A hA.1 hA.2.1)).isSymmetric
    (fun L x => (intertwiner_hermitian a b q ha hb L A hA.1.isHermitian hA.2.1.isHermitian x).symm)
    (Real.exp (-entropy a b q)) (Real.exp_pos _).le
    (hermitianAction_density_bound a b q ha hb A hA) v hv
2584
lemma projected_density_bound (A : Mat q × Mat q) (hA : IsEvenDensity A)
    (v : Raw (q := q) (τ := τ)) :
    ‖TensorRep.action (block A) (typeProjection a b q ha hb v)‖ ≤
      Real.exp (-entropy a b q) * ‖v‖ := by
proof · 5 lines
  exact (density_on_space a b q ha hb A hA _
    (Isotypic.projection_mem (rep a b q ha hb) (SignedTensor.rep (q := q) (τ := τ)) v)).trans
    (mul_le_mul_of_nonneg_left
      (Isotypic.projection_norm_apply_le (rep a b q ha hb) (SignedTensor.rep (q := q) (τ := τ)) v)
      (Real.exp_pos _).le)
2595
theorem global_density_upper (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    ‖LinearMap.toContinuousLinearMap (TensorRep.action (ι := τ) (block A)) *
      typeProjection (τ := τ) a b q ha hb‖ ≤ Real.exp (-entropy a b q) := by
proof · 3 lines
  apply ContinuousLinearMap.opNorm_le_bound _ (Real.exp_pos _).le
  intro v
  exact projected_density_bound a b q ha hb A hA v
2602
def highestVector : Carrier a b q ha hb :=
body · 1 line
  PolynomialCarrier.highestVector a q ha ⊗ₜ[ℂ] PolynomialCarrier.highestVector b q hb
2605
lemma highestVector_ne_zero : highestVector a b q ha hb ≠ 0 := by
proof · 6 lines
  intro h
  have hn := congrArg norm h
  rw [highestVector, TensorProduct.norm_tmul (E := PolynomialCarrier.Carrier a q ha)
    (F := PolynomialCarrier.Carrier b q hb), norm_zero] at hn
  exact (mul_ne_zero (norm_ne_zero_iff.mpr (PolynomialCarrier.highestVector_ne_zero a q ha))
    (norm_ne_zero_iff.mpr (PolynomialCarrier.highestVector_ne_zero b q hb))) hn
2613
lemma highestVector_diagonal (t : Fin q ⊕ Fin q → ℝ) :
    hermitianAction a b q ha hb
      (Matrix.diagonal (fun i => (t (.inl i) : ℂ)), Matrix.diagonal (fun i => (t (.inr i) : ℂ)))
      (Matrix.isHermitian_diagonal_iff.mpr (fun i => by simp [IsSelfAdjoint]))
      (Matrix.isHermitian_diagonal_iff.mpr (fun i => by simp [IsSelfAdjoint]))
      (highestVector a b q ha hb) =
        ((∏ i, t i ^ parts a b q i : ℝ) : ℂ) • highestVector a b q ha hb := by
proof · 7 lines
  rw [hermitianAction, highestVector, TensorProduct.map_tmul,
    PolynomialCarrier.highestVector_diagonal, PolynomialCarrier.highestVector_diagonal,
    TensorProduct.smul_tmul_smul, PolynomialCarrier.highestMonomial_eq_rowLen,
    PolynomialCarrier.highestMonomial_eq_rowLen]
  congr 1
  rw [Fintype.prod_sum_type, Complex.ofReal_mul]
  rfl
2629
lemma exists_density_trace_lower [Nonempty (Fin q)] : ∃ (A : Mat q × Mat q) (hA : IsEvenDensity A),
    Real.exp (-entropy a b q) ≤
      (LinearMap.trace ℂ (Carrier a b q ha hb)
        (hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian)).re := by
proof · 15 lines
  obtain ⟨t,ht,hs,he⟩ := EntropyMonomial.exists_optimal (parts a b q)
  let A : Mat q × Mat q :=
    (Matrix.diagonal (fun i => (t (.inl i) : ℂ)), Matrix.diagonal (fun i => (t (.inr i) : ℂ)))
  have hA : IsEvenDensity A := by
    refine ⟨Matrix.PosSemidef.diagonal (fun i => by simpa using ht (.inl i)),
      Matrix.PosSemidef.diagonal (fun i => by simpa using ht (.inr i)), ?_⟩
    simp only [A, Matrix.trace_diagonal]
    have hh := congrArg Complex.ofReal hs
    simpa only [Fintype.sum_sum_type, Complex.ofReal_add, Complex.ofReal_sum, Complex.ofReal_one] using hh
  refine ⟨A,hA,?_⟩
  apply PositiveTrace.eigenvalue_le_trace _ (hermitianAction_positive a b q ha hb A hA.1 hA.2.1)
    (highestVector a b q ha hb) (highestVector_ne_zero a b q ha hb)
  have h := highestVector_diagonal a b q ha hb t
  rw [he] at h
  exact h
2655
abbrev Mat (n : ℕ) := Matrix (Fin n) (Fin n) ℂ
2657
def border {n : ℕ} (a : ℂ) (b c : Fin n → ℂ) (D : Mat n) : Mat (n+1) :=
body · 1 line
  fun i => Fin.cases (Fin.cases a b) (fun i => Fin.cases (c i) (D i)) i
2660
@[simp] lemma border_zero_zero {n : ℕ} (a : ℂ) (b c : Fin n → ℂ) (D : Mat n) :
    border a b c D 0 0 = a := rfl
2662
@[simp] lemma border_zero_succ {n : ℕ} (a : ℂ) (b c : Fin n → ℂ) (D : Mat n) (j : Fin n) :
    border a b c D 0 j.succ = b j := rfl
2664
@[simp] lemma border_succ_zero {n : ℕ} (a : ℂ) (b c : Fin n → ℂ) (D : Mat n) (i : Fin n) :
    border a b c D i.succ 0 = c i := rfl
2666
@[simp] lemma border_succ_succ {n : ℕ} (a : ℂ) (b c : Fin n → ℂ) (D : Mat n) (i j : Fin n) :
    border a b c D i.succ j.succ = D i j := rfl
2669
lemma border_eq {n : ℕ} (A : Mat (n+1)) :
    border (A 0 0) (fun j => A 0 j.succ) (fun i => A i.succ 0)
      (fun i j => A i.succ j.succ) = A := by
proof · 2 lines
  ext i j
  refine Fin.cases ?_ (fun i => ?_) i <;> refine Fin.cases ?_ (fun j => ?_) j <;> rfl
2675
lemma continuous_border {X : Type*} [TopologicalSpace X] {n : ℕ}
    {a : X → ℂ} {b c : X → Fin n → ℂ} {D : X → Mat n}
    (ha : Continuous a) (hb : Continuous b) (hc : Continuous c) (hD : Continuous D) :
    Continuous (fun x => border (a x) (b x) (c x) (D x)) := by
proof · 9 lines
  apply continuous_pi
  intro i
  apply continuous_pi
  intro j
  refine Fin.cases ?_ (fun i => ?_) i <;> refine Fin.cases ?_ (fun j => ?_) j
  · exact ha
  · exact (continuous_apply j).comp hb
  · exact (continuous_apply i).comp hc
  · exact (continuous_apply j).comp ((continuous_apply i).comp hD)
2690
lemma border_factor {n : ℕ} (a : ℂ) (ha : a ≠ 0) (b c : Fin n → ℂ) (L U : Mat n) :
    border 1 0 (fun i => c i / a) L * border a b 0 U =
      border a b c (L * U + (Matrix.of fun i j => c i / a * b j)) := by
proof · 6 lines
  ext i j
  refine Fin.cases ?_ (fun i => ?_) i <;> refine Fin.cases ?_ (fun j => ?_) j
  · simp [Matrix.mul_apply, Fin.sum_univ_succ]
  · simp [Matrix.mul_apply, Fin.sum_univ_succ]
  · simp [Matrix.mul_apply, Fin.sum_univ_succ, ha]
  · simp [Matrix.mul_apply, Fin.sum_univ_succ, add_comm]
2700
def LowerUnipotent {n : ℕ} (L : Mat n) : Prop :=
body · 1 line
  L.IsLowerTriangular ∧ ∀ i, L i i = 1
2703
lemma lower_border {n : ℕ} (c : Fin n → ℂ) (L : Mat n) (hL : LowerUnipotent L) :
    LowerUnipotent (border 1 0 c L) := by
proof · 12 lines
  constructor
  · intro i j hij
    change i < j at hij
    induction i using Fin.cases <;> induction j using Fin.cases
    · simp at hij
    · rfl
    · simp at hij
    · exact hL.1 (by simpa using hij)
  · intro i
    refine Fin.cases ?_ (fun i => ?_) i
    · rfl
    · exact hL.2 i
2718
lemma upper_border {n : ℕ} (a : ℂ) (b : Fin n → ℂ) (U : Mat n) (hU : U.IsUpperTriangular) :
    (border a b 0 U).IsUpperTriangular := by
proof · 6 lines
  intro i j hij
  induction i using Fin.cases <;> induction j using Fin.cases
  · simp at hij
  · simp at hij
  · rfl
  · exact hU (by simpa using hij)
2727
def bigCell (n : ℕ) : Set (Mat n) :=
body · 1 line
  {A | ∃ L U, LowerUnipotent L ∧ U.IsUpperTriangular ∧ A = L * U}
2730
lemma border_mem_bigCell {n : ℕ} (a : ℂ) (ha : a ≠ 0) (b c : Fin n → ℂ) (S : Mat n)
    (hS : S ∈ bigCell n) :
    border a b c (S + (Matrix.of fun i j => c i / a * b j)) ∈ bigCell (n+1) := by
proof · 3 lines
  obtain ⟨L,U,hL,hU,rfl⟩ := hS
  exact ⟨border 1 0 (fun i => c i / a) L, border a b 0 U,
    lower_border _ _ hL, upper_border _ _ _ hU, (border_factor a ha b c L U).symm⟩
2739
theorem dense_bigCell (n : ℕ) : Dense (bigCell n) := by
proof · 28 lines
  induction n with
  | zero =>
    intro A
    apply subset_closure
    refine ⟨1,1,?_,?_,?_⟩
    · refine ⟨?_,?_⟩ <;> intro i <;> exact Fin.elim0 i
    · intro i; exact Fin.elim0 i
    · exact Subsingleton.elim _ _
  | succ n ih =>
    have hpivot (a : ℂ) (ha : a ≠ 0) (b c : Fin n → ℂ) (D : Mat n) :
        border a b c D ∈ closure (bigCell (n+1)) := by
      let X : Mat n := fun i j => c i / a * b j
      have hf : Continuous (fun S : Mat n => border a b c (S + X)) :=
        continuous_border continuous_const continuous_const continuous_const
          (continuous_id.add continuous_const)
      have hh := map_mem_closure hf (ih (D-X))
        (fun S hS => border_mem_bigCell a ha b c S hS)
      change border a b c (D - X + X) ∈ closure (bigCell (n+1)) at hh
      simpa only [sub_add_cancel] using hh
    intro A
    let b : Fin n → ℂ := fun j => A 0 j.succ
    let c : Fin n → ℂ := fun i => A i.succ 0
    let D : Mat n := fun i j => A i.succ j.succ
    have hf : Continuous (fun a : ℂ => border a b c D) :=
      continuous_border continuous_id continuous_const continuous_const continuous_const
    have hh := map_mem_closure hf ((dense_compl_singleton (0 : ℂ)) (A 0 0))
      (fun a ha => hpivot a (by simpa using ha) b c D)
    simpa only [closure_closure, b, c, D, border_eq] using hh
2776
lemma action_mem_alternating (A : Matrix (Fin q) (Fin q) ℂ)
    (v : TensorSpace D q) (hv : v ∈ alternating D q) :
    TensorRep.action A v ∈ alternating D q := by
proof · 2 lines
  intro g
  rw [← action_site_commute, hv g, map_smul]
2782
lemma upper_weight_support (U : Matrix (Fin q) (Fin q) ℂ) (hU : U.IsUpperTriangular)
    (x : Cells D → Fin q) (hx : TensorRep.action U (highest D q hq) x ≠ 0) :
    ∑ i, (x i).val = ∑ i : Cells D, i.val.1 := by
proof · 13 lines
  have hl := rowRankSum_le D (fun i => (x i).val)
    (alternating_injective D q _ (action_mem_alternating D q U _ (highest_mem D q hq)) x hx)
  apply le_antisymm _ hl
  change (∑ y, (∏ i, U (x i) (y i)) * highest D q hq y) ≠ 0 at hx
  obtain ⟨y, -, hy⟩ := Finset.exists_ne_zero_of_sum_ne_zero hx
  have hyv : highest D q hq y ≠ 0 := (mul_ne_zero_iff.mp hy).2
  have hp : ∀ i, x i ≤ y i := by
    intro i
    by_contra hi
    have hz : U (x i) (y i) = 0 := hU (lt_of_not_ge hi)
    exact (mul_ne_zero_iff.mp hy).1 (Finset.prod_eq_zero (Finset.mem_univ i) hz)
  rw [← weight_eigen_support D q hq _ (highest_weight_eigen D q hq) y hyv]
  exact Finset.sum_le_sum fun i _ => hp i
2801
lemma upper_highest (U : Matrix (Fin q) (Fin q) ℂ) (hU : U.IsUpperTriangular) :
    TensorRep.action U (highest D q hq) =
      (TensorRep.action U (highest D q hq) (rowColor D q hq)) • highest D q hq := by
proof · 12 lines
  ext x
  change _ = _ * highest D q hq x
  by_cases hw : ∑ i, (x i).val = ∑ i : Cells D, i.val.1
  · exact alternating_minimal_weight D q hq _
      (action_mem_alternating D q U _ (highest_mem D q hq)) x hw
  · have hv0 : TensorRep.action U (highest D q hq) x = 0 := by
      by_contra hn
      exact hw (upper_weight_support D q hq U hU x hn)
    have he0 : highest D q hq x = 0 := by
      by_contra hn
      exact hw (weight_eigen_support D q hq _ (highest_weight_eigen D q hq) x hn)
    rw [hv0, he0, mul_zero]
2817
lemma continuous_action_highest :
    Continuous (fun A : Matrix (Fin q) (Fin q) ℂ => TensorRep.action A (highest D q hq)) := by
proof · 4 lines
  change Continuous (fun A : Matrix (Fin q) (Fin q) ℂ =>
    WithLp.toLp 2 (fun x : Cells D → Fin q => ∑ y, (∏ i, A (x i) (y i)) * highest D q hq y))
  apply (PiLp.continuous_toLp 2 (fun _ : Cells D → Fin q => ℂ)).comp
  fun_prop
2826
lemma action_highest_mem_of_lower (S : Submodule ℂ (TensorSpace D q))
    (hS : ∀ L : Matrix (Fin q) (Fin q) ℂ, DenseLU.LowerUnipotent L →
      TensorRep.action L (highest D q hq) ∈ S)
    (A : Matrix (Fin q) (Fin q) ℂ) : TensorRep.action A (highest D q hq) ∈ S := by
proof · 11 lines
  have hsub : DenseLU.bigCell q ⊆
      (fun B : Matrix (Fin q) (Fin q) ℂ => TensorRep.action B (highest D q hq)) ⁻¹'
        (S : Set (TensorSpace D q)) := by
    intro B hB
    obtain ⟨L,U,hL,hU,rfl⟩ := hB
    change TensorRep.action (L * U) (highest D q hq) ∈ S
    rw [map_mul, Module.End.mul_apply, upper_highest D q hq U hU, map_smul]
    exact S.smul_mem _ (hS L hL)
  exact closure_minimal hsub
    (S.closed_of_finiteDimensional.preimage (continuous_action_highest D q hq))
    (DenseLU.dense_bigCell q A)
2850
def boundedExponentEquiv : {f : σ →₀ ℕ // ∀ i, f i ≤ p} ≃ (σ → Fin (p+1)) where
body · 5 lines
  toFun f i := ⟨f.val i, Nat.lt_succ_of_le (f.property i)⟩
  invFun f := ⟨Finsupp.equivFunOnFinite.symm (fun i => (f i).val), by
    intro i; exact Nat.le_of_lt_succ (f i).isLt⟩
  left_inv f := by apply Subtype.ext; ext i; rfl
  right_inv f := by funext i; apply Fin.ext; rfl
2858
theorem finrank_restrictDegree :
    Module.finrank ℂ (restrictDegree σ ℂ p) = (p+1) ^ Fintype.card σ := by
proof · 7 lines
  let s : Set (σ →₀ ℕ) := {f | ∀ i, f i ≤ p}
  let : Fintype s := Fintype.ofEquiv (σ → Fin (p+1)) (boundedExponentEquiv σ p).symm
  change Module.finrank ℂ (restrictSupport ℂ s) = _
  rw [Module.finrank_eq_card_basis (basisRestrictSupport ℂ s)]
  calc
    Fintype.card s = Fintype.card (σ → Fin (p+1)) := Fintype.card_congr (boundedExponentEquiv σ p)
    _ = _ := by simp
2868
abbrev LowerIndex (q : ℕ) := (i : Fin q) × Fin i.val
2870
lemma card_lowerIndex (q : ℕ) : Fintype.card (LowerIndex q) = q * (q-1) / 2 := by
proof · 2 lines
  simp only [LowerIndex, Fintype.card_sigma, Fintype.card_fin]
  rw [Fin.sum_univ_eq_sum_range (fun i : ℕ => i) q, Finset.sum_range_id]
2874
lemma twice_card_lowerIndex (q : ℕ) : 2 * Fintype.card (LowerIndex q) = q * (q-1) := by
proof · 2 lines
  simp only [LowerIndex, Fintype.card_sigma, Fintype.card_fin]
  rw [Fin.sum_univ_eq_sum_range (fun i : ℕ => i) q, mul_comm, Finset.sum_range_id_mul_two]
2879
def lowerEntry (i j : Fin q) : MvPolynomial (LowerIndex q) ℂ :=
body · 1 line
  if h : j < i then X ⟨i, ⟨j.val, h⟩⟩ else if i = j then 1 else 0
2882
lemma lowerEntry_degree (i j : Fin q) : (lowerEntry i j).totalDegree ≤ 1 := by
proof · 2 lines
  unfold lowerEntry
  split_ifs <;> simp
2886
lemma totalDegree_sum_le
    (σ : Type*)
    [Fintype σ]
    [DecidableEq σ]
    (p : ℕ) {ι : Type*} (s : Finset ι) (f : ι → MvPolynomial σ ℂ)
    (hf : ∀ i ∈ s, (f i).totalDegree ≤ p) : (∑ i ∈ s, f i).totalDegree ≤ p :=
proof · 1 line
  (MvPolynomial.totalDegree_finsetSum s f).trans (Finset.sup_le hf)
2903
def valueTranspose (P : X → restrictDegree σ ℂ p) :
    Module.Dual ℂ (restrictDegree σ ℂ p) →ₗ[ℂ] EuclideanSpace ℂ X where
body · 3 lines
  toFun φ := WithLp.toLp 2 (fun x => φ (P x))
  map_add' φ ψ := by ext x; rfl
  map_smul' c φ := by ext x; rfl
2909
lemma value_mem_range
    {σ : Type*} {X : Type*}
    [Fintype σ]
    [DecidableEq σ]
    [Fintype X]
    (p : ℕ) (P : X → restrictDegree σ ℂ p) (a : σ → ℂ) :
    WithLp.toLp 2 (fun x => MvPolynomial.aeval a (P x).val) ∈
      LinearMap.range (valueTranspose p P) := by
proof · 2 lines
  refine ⟨(MvPolynomial.aeval a).toLinearMap.comp (restrictDegree σ ℂ p).subtype, ?_⟩
  rfl
2920
lemma finrank_value_range
    {σ : Type*} {X : Type*}
    [contextInstance2737_23 : Fintype σ]
    [contextInstance2737_35 : DecidableEq σ]
    [Fintype X]
    (p : ℕ) (P : X → restrictDegree σ ℂ p) :
    Module.finrank ℂ (LinearMap.range (valueTranspose p P)) ≤ (p+1)^Fintype.card σ := by
proof · 3 lines
  have hh := (valueTranspose p P).finrank_range_le
  rw [Subspace.dual_finrank_eq, finrank_restrictDegree] at hh
  exact hh
2939
def lowerCoordinate (x : Cells D → Fin q) : MvPolynomial (LowerIndex q) ℂ :=
body · 1 line
  ∑ y : Cells D → Fin q, (∏ i : Cells D, lowerEntry (x i) (y i)) * C (highest D q hq y)
2942
lemma lowerCoordinate_degree (x : Cells D → Fin q) :
    (lowerCoordinate D q hq x).totalDegree ≤ Fintype.card (Cells D) := by
proof · 6 lines
  apply PolyDimension.totalDegree_sum_le
  intro y _
  apply (MvPolynomial.totalDegree_mul _ _).trans
  rw [MvPolynomial.totalDegree_C, add_zero]
  apply (MvPolynomial.totalDegree_finsetProd _ _).trans
  exact (Finset.sum_le_sum fun i _ => lowerEntry_degree (x i) (y i)).trans_eq (by simp)
2951
def lowerCoordinateBounded (x : Cells D → Fin q) :
    restrictDegree (LowerIndex q) ℂ (Fintype.card (Cells D)) :=
body · 3 lines
  ⟨lowerCoordinate D q hq x,
    restrictTotalDegree_le_restrictDegree _ _ _
      ((mem_restrictTotalDegree _ _ _).mpr (lowerCoordinate_degree D q hq x))⟩
2957
def lowerParameters (L : Matrix (Fin q) (Fin q) ℂ) (z : LowerIndex q) : ℂ :=
body · 1 line
  L z.1 ⟨z.2.val, z.2.isLt.trans z.1.isLt⟩
2960
lemma eval_lowerEntry (L : Matrix (Fin q) (Fin q) ℂ) (hL : DenseLU.LowerUnipotent L)
    (i j : Fin q) : aeval (lowerParameters q L) (lowerEntry i j) = L i j := by
proof · 6 lines
  unfold lowerEntry
  split_ifs with hlt heq
  · simp [aeval_X, lowerParameters]
  · subst j; simp [hL.2 i]
  · have hij : i < j := lt_of_le_of_ne (le_of_not_gt hlt) heq
    rw [map_zero, hL.1 hij]
2969
lemma eval_lowerCoordinate (L : Matrix (Fin q) (Fin q) ℂ) (hL : DenseLU.LowerUnipotent L)
    (x : Cells D → Fin q) :
    aeval (lowerParameters q L) (lowerCoordinate D q hq x) =
      TensorRep.action L (highest D q hq) x := by
proof · 7 lines
  simp only [lowerCoordinate, map_sum, map_mul, map_prod, eval_lowerEntry q L hL]
  change (∑ y : Cells D → Fin q, (∏ i, L (x i) (y i)) *
    aeval (lowerParameters q L) (C (highest D q hq y))) =
      ∑ y : Cells D → Fin q, (∏ i, L (x i) (y i)) * highest D q hq y
  apply Finset.sum_congr rfl
  intro y _
  simp
2983
theorem ordinary_finrank_le :
    Module.finrank ℂ (ordinary D q hq).toSubmodule ≤
      (Fintype.card (Cells D)+1) ^ (q*(q-1)/2) := by
proof · 35 lines
  let P := lowerCoordinateBounded D q hq
  let S := LinearMap.range (PolyDimension.valueTranspose (Fintype.card (Cells D)) P)
  have hL (L : Matrix (Fin q) (Fin q) ℂ) (hL : DenseLU.LowerUnipotent L) :
      TensorRep.action L (highest D q hq) ∈ S := by
    have hh := PolyDimension.value_mem_range (Fintype.card (Cells D)) P (lowerParameters q L)
    have he : WithLp.toLp 2 (fun x => aeval (lowerParameters q L) (P x).val) =
        TensorRep.action L (highest D q hq) := by
      ext x; exact eval_lowerCoordinate D q hq L hL x
    rw [he] at hh
    exact hh
  let inc : (ordinary D q hq).toSubmodule →ₗ[ℂ] TensorSpace D q :=
    (alternating D q).toSubmodule.subtype.comp (ordinary D q hq).toSubmodule.subtype
  have hcarrier (v : (alternating D q).toSubmodule)
      (hv : v ∈ (ordinary D q hq).toSubmodule) : (v : TensorSpace D q) ∈ S := by
    change v ∈ Submodule.span ℂ (Set.range fun g =>
      (alternating D q).toRepresentation g
        (⟨highest D q hq, highest_mem D q hq⟩ : (alternating D q).toSubmodule)) at hv
    induction hv using Submodule.span_induction with
    | mem v hv =>
      obtain ⟨g,rfl⟩ := hv
      exact action_highest_mem_of_lower D q hq S hL g.val
    | zero => exact S.zero_mem
    | add x y hx hy ihx ihy => exact S.add_mem ihx ihy
    | smul a x hx ih => exact S.smul_mem a ih
  have hinc (v : (ordinary D q hq).toSubmodule) : inc v ∈ S := hcarrier v.val v.property
  let f : (ordinary D q hq).toSubmodule →ₗ[ℂ] S := LinearMap.codRestrict S inc hinc
  have hf : Function.Injective f := by
    intro x y he
    apply Subtype.ext
    apply Subtype.ext
    exact congrArg (fun v : S => v.val) he
  have hh := LinearMap.finrank_le_finrank_of_injective hf
  exact hh.trans (by
    simpa only [PolyDimension.card_lowerIndex] using
      PolyDimension.finrank_value_range (Fintype.card (Cells D)) P)
3033
def compactHaar : Measure G :=
body · 1 line
  Measure.haarMeasure (⟨⟨Set.univ, isCompact_univ⟩, by simp⟩ : PositiveCompacts G)
3036
instance : IsProbabilityMeasure (compactHaar (G := G)) where
body · 1 line
  measure_univ := Measure.haarMeasure_self
3039
instance : (compactHaar (G := G)).IsMulLeftInvariant :=
body · 1 line
  inferInstanceAs (Measure.IsMulLeftInvariant (Measure.haarMeasure _))
3042
lemma continuous_integrable {A : Type*} [NormedAddCommGroup A] (f : G → A)
    (hf : Continuous f) : Integrable f (compactHaar (G := G)) :=
proof · 1 line
  hf.integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace f)
3049
def conjugate (ρ : G →* A) (a : A) (g : G) : A := ρ g * a * ρ g⁻¹
3051
def twirl (ρ : G →* A) (a : A) : A := ∫ g, conjugate ρ a g ∂compactHaar
3053
lemma continuous_conjugate
    {G : Type*}
    [contextInstance2861_21 : Group G]
    [contextInstance2861_31 : TopologicalSpace G]
    [contextInstance2861_52 : IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {A : Type*}
    [contextInstance2877_21 : NormedRing A]
    [NormedAlgebra ℝ A]
    [CompleteSpace A] (ρ : G →* A) (hρ : Continuous ρ) (a : A) :
    Continuous (conjugate ρ a) :=
proof · 1 line
  (hρ.mul continuous_const).mul (hρ.comp continuous_inv)
3068
lemma integrable_conjugate (ρ : G →* A) (hρ : Continuous ρ) (a : A) :
    Integrable (conjugate ρ a) compactHaar :=
proof · 1 line
  continuous_integrable _ (continuous_conjugate ρ hρ a)
3072
lemma conjugate_mul
    {G : Type*}
    [contextInstance2861_21 : Group G]
    [TopologicalSpace G]
    [IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {A : Type*}
    [contextInstance2877_21 : NormedRing A]
    [NormedAlgebra ℝ A]
    [CompleteSpace A] (ρ : G →* A) (a : A) (g h : G) :
    conjugate ρ a (g * h) = ρ g * conjugate ρ a h * ρ g⁻¹ := by
proof · 1 line
  simp only [conjugate, mul_inv_rev, map_mul, mul_assoc]
3088
lemma twirl_commute (ρ : G →* A) (hρ : Continuous ρ) (a : A) (g : G) :
    Commute (ρ g) (twirl ρ a) := by
proof · 9 lines
  have hi := integrable_conjugate ρ hρ a
  have hinv : (ρ g⁻¹) * ρ g = 1 := by rw [← map_mul, inv_mul_cancel, map_one]
  have he : ρ g * twirl ρ a * ρ g⁻¹ = twirl ρ a := by
    rw [twirl, ← integral_const_mul_of_integrable hi]
    rw [← integral_mul_const_of_integrable (hi.const_mul _)]
    simp_rw [← conjugate_mul]
    exact integral_mul_left_eq_self _ g
  have hc := congrArg (fun x : A => x * ρ g) he
  simpa only [Commute, SemiconjBy, mul_assoc, hinv, mul_one] using hc
3100
lemma twirl_trace (τ : A →L[ℝ] ℂ) (hτ : ∀ x y : A, τ (x * y) = τ (y * x))
    (ρ : G →* A) (hρ : Continuous ρ) (a : A) : τ (twirl ρ a) = τ a := by
proof · 4 lines
  rw [twirl, ← τ.integral_comp_comm (integrable_conjugate ρ hρ a)]
  have he (g : G) : τ (conjugate ρ a g) = τ a := by
    rw [conjugate, hτ, ← mul_assoc, ← map_mul, inv_mul_cancel, map_one, one_mul]
  simp [he, integral_const, measureReal_def]
3111
lemma twirl_nonneg
    {G : Type*}
    [contextInstance2861_21 : Group G]
    [contextInstance2861_31 : TopologicalSpace G]
    [contextInstance2861_52 : IsTopologicalGroup G]
    [contextInstance2862_2 : CompactSpace G]
    [contextInstance2862_19 : MeasurableSpace G]
    [contextInstance2862_39 : BorelSpace G]
    {A : Type*}
    [contextInstance2877_21 : NormedRing A]
    [contextInstance2877_36 : NormedAlgebra ℝ A]
    [CompleteSpace A]
    [contextInstance2919_9 : PartialOrder A]
    [contextInstance2919_26 : IsOrderedAddMonoid A]
    [contextInstance2919_49 : StarRing A]
    [contextInstance2919_62 : StarOrderedRing A]
    [contextInstance2920_2 : ClosedIciTopology A]
    [contextInstance2920_24 : IsOrderedModule ℝ A] (ρ : G →* A) (a : A)
    (hρ : ∀ g, ρ g⁻¹ = star (ρ g)) (ha : 0 ≤ a) : 0 ≤ twirl ρ a := by
proof · 5 lines
  apply integral_nonneg
  intro g
  dsimp [conjugate]
  rw [hρ]
  exact star_right_conjugate_nonneg ha _
3143
lemma unitaryGroup_compactSpace (n : Type*) [Fintype n] [DecidableEq n] [Nonempty n] :
    CompactSpace (Matrix.unitaryGroup n ℂ) := by
proof · 5 lines
  apply isCompact_iff_compactSpace.mp
  apply (isCompact_closedBall (0 : Matrix n n ℂ) 1).of_isClosed_subset isClosed_unitary
  intro a ha
  rw [Metric.mem_closedBall, dist_zero_right]
  exact (CStarRing.norm_coe_unitary (⟨a, ha⟩ : unitary (Matrix n n ℂ))).le
3160
def continuousAction (ρ : Representation ℂ G E) : G →* (E →L[ℂ] E) :=
body · 1 line
  (Module.End.toContinuousLinearMap (𝕜 := ℂ) E).toMonoidHom.comp ρ
3163
@[simp] lemma continuousAction_apply
    {G : Type*}
    [contextInstance2949_21 : Group G]
    [TopologicalSpace G]
    [IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {E : Type*}
    [contextInstance2951_21 : NormedAddCommGroup E]
    [contextInstance2951_44 : InnerProductSpace ℂ E]
    [contextInstance2952_2 : FiniteDimensional ℂ E] (ρ : Representation ℂ G E) (g : G) (v : E) :
    continuousAction ρ g v = ρ g v := rfl
3177
def operatorTrace : (E →L[ℂ] E) →L[ℂ] ℂ :=
body · 2 lines
  ((LinearMap.trace ℂ E).comp
    (Module.End.toContinuousLinearMap (𝕜 := ℂ) E).symm.toLinearMap).toContinuousLinearMap
3181
@[simp] lemma operatorTrace_apply
    {G : Type*}
    [Group G]
    [TopologicalSpace G]
    [IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {E : Type*}
    [contextInstance2951_21 : NormedAddCommGroup E]
    [contextInstance2951_44 : InnerProductSpace ℂ E]
    [contextInstance2952_2 : FiniteDimensional ℂ E] (a : E →L[ℂ] E) :
    operatorTrace a = LinearMap.trace ℂ E a.toLinearMap := rfl
3195
lemma operatorTrace_mul_comm
    {G : Type*}
    [Group G]
    [TopologicalSpace G]
    [IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {E : Type*}
    [contextInstance2951_21 : NormedAddCommGroup E]
    [contextInstance2951_44 : InnerProductSpace ℂ E]
    [FiniteDimensional ℂ E] (a b : E →L[ℂ] E) :
    operatorTrace (a * b) = operatorTrace (b * a) :=
proof · 1 line
  LinearMap.trace_mul_comm ℂ a.toLinearMap b.toLinearMap
3212
theorem twirl_irreducible (ρ : Representation ℂ G E) [ρ.IsIrreducible]
    (hρ : Continuous (continuousAction ρ)) (a : E →L[ℂ] E) :
    twirl (continuousAction ρ) a =
      (operatorTrace a / (Module.finrank ℂ E : ℂ)) • (1 : E →L[ℂ] E) := by
proof · 22 lines
  let t := twirl (continuousAction ρ) a
  let f : ρ.IntertwiningMap ρ :=
    t.toLinearMap.intertwiningMap_of_isIntertwiningMap ρ ρ (by
      intro g v
      have h := twirl_commute (continuousAction ρ) hρ a g
      exact (congrArg (fun L : E →L[ℂ] E => L v) h.eq).symm)
  obtain ⟨z, hz⟩ :=
    (Representation.IsIrreducible.algebraMap_intertwiningMap_bijective_of_isAlgClosed (ρ := ρ)).surjective f
  have ht : t = z • (1 : E →L[ℂ] E) := by
    ext v
    exact (congrArg (fun L : ρ.IntertwiningMap ρ => L v) hz).symm
  have htr : operatorTrace t = operatorTrace a :=
    twirl_trace (operatorTrace.restrictScalars ℝ) (operatorTrace_mul_comm (G := G))
      (continuousAction ρ) hρ a
  let : Nontrivial E := IsSimpleModule.nontrivial (MonoidAlgebra ℂ G) ρ.asModule
  have hn : (Module.finrank ℂ E : ℂ) ≠ 0 := by
    exact_mod_cast Module.finrank_pos.ne'
  have hz' : z = operatorTrace a / (Module.finrank ℂ E : ℂ) := by
    apply (eq_div_iff hn).mpr
    rw [ht, map_smul] at htr
    simpa [operatorTrace_apply (G := G), LinearMap.trace_one] using htr
  simpa only [hz'] using ht
3251
lemma twirl_natural (ρ : Representation ℂ G E) (σ : Representation ℂ G F)
    (hρ : Continuous (continuousAction ρ)) (hσ : Continuous (continuousAction σ))
    (a : E →L[ℂ] E) (A : F →L[ℂ] F) (L : ρ.IntertwiningMap σ)
    (hnat : ∀ x, A (L x) = L (a x)) (x : E) :
    twirl (continuousAction σ) A (L x) = L (twirl (continuousAction ρ) a x) := by
proof · 9 lines
  have hi := integrable_conjugate (continuousAction ρ) hρ a
  have hI := integrable_conjugate (continuousAction σ) hσ A
  rw [twirl, ContinuousLinearMap.integral_apply hI, twirl, ContinuousLinearMap.integral_apply hi]
  change _ = L.toLinearMap.toContinuousLinearMap (∫ g, conjugate (continuousAction ρ) a g x ∂compactHaar)
  rw [← ContinuousLinearMap.integral_comp_comm _ (hi.apply_continuousLinearMap x)]
  apply integral_congr_ae
  filter_upwards [] with g
  change σ g (A (σ g⁻¹ (L x))) = L (ρ g (a (ρ g⁻¹ x)))
  rw [← L.isIntertwining, hnat, L.isIntertwining]
3266
lemma continuousAction_inverse_star
    {G : Type*}
    [contextInstance3008_21 : Group G]
    [TopologicalSpace G]
    [IsTopologicalGroup G]
    [CompactSpace G]
    [MeasurableSpace G]
    [BorelSpace G]
    {E : Type*}
    [contextInstance3010_23 : NormedAddCommGroup E]
    [contextInstance3010_46 : InnerProductSpace ℂ E]
    [contextInstance3012_2 : FiniteDimensional ℂ E] (ρ : Representation ℂ G E)
    (hρ : ∀ g x y, ⟪ρ g x,ρ g y⟫_ℂ = ⟪x,y⟫_ℂ) (g : G) :
    continuousAction ρ g⁻¹ = star (continuousAction ρ g) := by
proof · 6 lines
  ext x
  apply ext_inner_left ℂ
  intro y
  change ⟪y,ρ g⁻¹ x⟫_ℂ = ⟪y,(continuousAction ρ g).adjoint x⟫_ℂ
  rw [ContinuousLinearMap.adjoint_inner_right]
  simpa only [inv_inv, continuousAction_apply] using (UnitaryNaturality.inner_move ρ hρ g⁻¹ y x)
3293
lemma scalar_projection_le (T P : A) (hT : 0 ≤ T) (hP : IsStarProjection P)
    (c : ℝ) (he : T * P = c • P) : c • P ≤ T := by
proof · 10 lines
  have hstar := congrArg star he
  have hpT : P * T = c • P := by
    simpa only [star_mul, hP.isSelfAdjoint.star_eq, (IsSelfAdjoint.of_nonneg hT).star_eq,
      star_smul, star_trivial] using hstar
  have hc : Commute (1-P) T := by
    dsimp [Commute,SemiconjBy]
    rw [sub_mul,mul_sub,one_mul,mul_one,hpT,he]
  have hn := hc.mul_nonneg (sub_nonneg.mpr hP.le_one) hT
  rw [sub_mul,one_mul,hpT] at hn
  exact sub_nonneg.mp hn
3315
lemma isotypic_twirl_scalar (ρ : Representation ℂ G E) [ρ.IsIrreducible]
    (σ : Representation ℂ G F)
    (hρ : Continuous (continuousAction ρ)) (hσ : Continuous (continuousAction σ))
    (a : E →L[ℂ] E) (A : F →L[ℂ] F)
    (hnat : ∀ L : ρ.IntertwiningMap σ, ∀ x, A (L x) = L (a x)) :
    twirl (continuousAction σ) A * Isotypic.projection ρ σ =
      (operatorTrace a / (Module.finrank ℂ E : ℂ)) • Isotypic.projection ρ σ := by
proof · 9 lines
  ext v
  apply Isotypic.scalar_on_space ρ σ
    (twirl (continuousAction σ) A).toLinearMap
    (operatorTrace a / (Module.finrank ℂ E : ℂ))
  · intro L x
    change twirl (continuousAction σ) A (L x) = _
    rw [twirl_natural ρ σ hρ hσ a A L (hnat L), twirl_irreducible ρ hρ a]
    simp only [smul_apply, one_apply_eq_self, map_smul]
  · exact (Isotypic.space ρ σ).toSubmodule.starProjection_apply_mem v
3333
lemma isotypic_twirl_lower (ρ : Representation ℂ G E) [ρ.IsIrreducible]
    (σ : Representation ℂ G F)
    (hρ : Continuous (continuousAction ρ)) (hσ : Continuous (continuousAction σ))
    (hσunit : ∀ g x y, ⟪σ g x,σ g y⟫_ℂ = ⟪x,y⟫_ℂ)
    (a : E →L[ℂ] E) (A : F →L[ℂ] F) (ha : 0 ≤ a) (hA : 0 ≤ A)
    (hnat : ∀ L : ρ.IntertwiningMap σ, ∀ x, A (L x) = L (a x)) :
    ((operatorTrace a).re / (Module.finrank ℂ E : ℝ)) • Isotypic.projection ρ σ ≤
      twirl (continuousAction σ) A := by
proof · 13 lines
  apply ProjectionOrder.scalar_projection_le _ _
    (twirl_nonneg _ A (continuousAction_inverse_star σ hσunit) hA)
    (isStarProjection_starProjection (U := (Isotypic.space ρ σ).toSubmodule))
  have ht : operatorTrace a = ((operatorTrace a).re : ℂ) :=
    Complex.eq_re_of_ofReal_le (LinearMap.IsPositive.trace_nonneg
      ((ContinuousLinearMap.nonneg_iff_isPositive).mp ha))
  have h := isotypic_twirl_scalar ρ σ hρ hσ a A hnat
  rw [ht] at h
  convert h using 1 <;> simp only [Isotypic.projection,
    RCLike.real_smul_eq_coe_smul (K := ℂ)]
  congr 1
  change (((operatorTrace a).re / (Module.finrank ℂ E : ℝ) : ℝ) : ℂ) = _
  simp only [Complex.ofReal_div, Complex.ofReal_natCast]
3368
lemma convexHull_compact {s : Set E} (hs : IsCompact s) : IsCompact (convexHull ℝ s) := by
proof · 45 lines
  classical
  let : CompactSpace s := isCompact_iff_compactSpace.mp hs
  let C (k : ℕ) := (Convexity.StdSimplex ℝ (Fin k)) × (Fin k → s)
  let f (k : ℕ) (v : C k) : E := ∑ i, v.1.weights i • (v.2 i).val
  have hf (k : ℕ) : Continuous (f k) := by
    change Continuous (fun v : (Convexity.StdSimplex ℝ (Fin k)) × (Fin k → s) =>
      ∑ i, v.1.weights i • (v.2 i).val)
    apply continuous_finsetSum
    intro i hi
    exact ((Convexity.StdSimplex.continuous_weights_apply ℝ i).comp continuous_fst).smul
      (continuous_subtype_val.comp ((continuous_apply i).comp continuous_snd))
  have hc : IsCompact (⋃ k : Fin (Module.finrank ℝ E + 2), Set.range (f k.val)) :=
    isCompact_iUnion (fun k => isCompact_range (hf k.val))
  convert hc using 1
  ext x
  constructor
  · intro hx
    let t := Caratheodory.minCardFinsetOfMemConvexHull hx
    have ht : (t : Set E) ⊆ s := Caratheodory.minCardFinsetOfMemConvexHull_subseteq hx
    have hm : x ∈ convexHull ℝ (t : Set E) := Caratheodory.mem_minCardFinsetOfMemConvexHull hx
    have hi : AffineIndependent ℝ ((↑) : t → E) :=
      Caratheodory.affineIndependent_minCardFinsetOfMemConvexHull hx
    have hn : Fintype.card t ≤ Module.finrank ℝ E + 1 :=
      hi.card_le_finrank_succ.trans (Nat.add_le_add_right (Submodule.finrank_le _) 1)
    let k : Fin (Module.finrank ℝ E + 2) := ⟨Fintype.card t, by omega⟩
    let e := (Fintype.equivFin t).symm
    obtain ⟨w,hw,hws,hxw⟩ := Finset.mem_convexHull'.mp hm
    let z : Fin k.val → s := fun i => ⟨(e i).val, ht (e i).property⟩
    have hsum : ∑ i : Fin k.val, w (e i).val = 1 := by
      rw [show (∑ i : Fin k.val, w (e i).val) = ∑ j : t, w j.val from
        e.sum_comp (fun j : t => w j.val)]
      simpa only [Finset.sum_coe_sort] using hws
    let v : C k.val := (⟨Finsupp.equivFunOnFinite.symm (fun i => w (e i).val),
      fun i => by simpa using hw _ (e i).property,
      by simpa [Finsupp.sum_fintype] using hsum⟩, z)
    apply Set.mem_iUnion.mpr
    refine ⟨k,v,?_⟩
    simp only [f, v, z, Finsupp.coe_equivFunOnFinite_symm]
    rw [show (∑ i : Fin k.val, w (e i).val • (e i).val) = ∑ j : t, w j.val • j.val from
      e.sum_comp (fun j : t => w j.val • j.val)]
    exact (Finset.sum_coe_sort t (fun j : E => w j • j)).trans hxw
  · intro hx
    obtain ⟨k,v,rfl⟩ := Set.mem_iUnion.mp hx
    exact mem_convexHull_of_exists_fintype (fun i => v.1.weights i) (fun i => (v.2 i).val)
      v.1.weights_nonneg v.1.total_of_fintype (fun i => (v.2 i).property) rfl
3424
lemma twirl_finite_mixture (ρ : Representation ℂ G E)
    (hρ : Continuous (continuousAction ρ)) (a : E →L[ℂ] E) :
    ∃ (ι : Type) (_ : Fintype ι) (w : ι → ℝ) (g : ι → G),
      (∀ i, 0 ≤ w i) ∧ (∑ i, w i = 1) ∧
      twirl (continuousAction ρ) a = ∑ i, w i • conjugate (continuousAction ρ) a (g i) := by
proof · 15 lines
  let f := conjugate (continuousAction ρ) a
  have hf : Continuous f := continuous_conjugate _ hρ a
  have hc : IsCompact (Set.range f) := isCompact_range hf
  have hm : twirl (continuousAction ρ) a ∈ convexHull ℝ (Set.range f) :=
    (convex_convexHull ℝ _).integral_mem (CompactMixture.convexHull_compact hc).isClosed
      (Filter.Eventually.of_forall fun g => subset_convexHull ℝ _ (Set.mem_range_self g))
      (integrable_conjugate _ hρ a)
  obtain ⟨ι,hι,w,z,hw,hs,hz,he⟩ := mem_convexHull_iff_exists_fintype.mp hm
  let := hι
  choose g hg using hz
  refine ⟨ι,hι,w,g,hw,hs,?_⟩
  rw [← he]
  apply Finset.sum_congr rfl
  intro i _
  rw [← hg i]
3451
lemma dimension_eq : dimension a b q ha hb =
    Module.finrank ℂ (PolynomialCarrier.Carrier a q ha) *
      Module.finrank ℂ (PolynomialCarrier.Carrier b q hb) :=
proof · 1 line
  Module.finrank_tensorProduct
3456
lemma dimension_pos : 0 < dimension a b q ha hb := by
proof · 3 lines
  have : Nontrivial (Carrier a b q ha hb) := IsSimpleModule.nontrivial (MonoidAlgebra ℂ (Group q))
    (rep a b q ha hb).asModule
  exact Module.finrank_pos
3462
theorem dimension_le : dimension a b q ha hb ≤
    (Fintype.card (Cells a) + Fintype.card (Cells b) + 1) ^ (q * (q-1)) := by
proof · 13 lines
  rw [dimension_eq]
  let p := Fintype.card (Cells a) + Fintype.card (Cells b) + 1
  have hA : Module.finrank ℂ (PolynomialCarrier.Carrier a q ha) ≤ p ^ (q*(q-1)/2) :=
    (PolynomialCarrier.ordinary_finrank_le a q ha).trans (Nat.pow_le_pow_left (by dsimp [p]; omega) _)
  have hB : Module.finrank ℂ (PolynomialCarrier.Carrier b q hb) ≤ p ^ (q*(q-1)/2) :=
    (PolynomialCarrier.ordinary_finrank_le b q hb).trans (Nat.pow_le_pow_left (by dsimp [p]; omega) _)
  apply (Nat.mul_le_mul hA hB).trans
  rw [← pow_add]
  have he : q*(q-1)/2 + q*(q-1)/2 = q*(q-1) := by
    have hh := PolyDimension.twice_card_lowerIndex q
    rw [PolyDimension.card_lowerIndex] at hh
    omega
  rw [he]
3484
def conjugateDensity (g : Group q) (A : Mat q × Mat q) : Mat q × Mat q :=
body · 1 line
  (g.1.val * A.1 * star g.1.val, g.2.val * A.2 * star g.2.val)
3487
lemma conjugateDensity_isEvenDensity (g : Group q) (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    IsEvenDensity (conjugateDensity g A) := by
proof · 7 lines
  refine ⟨?_, ?_, ?_⟩
  · exact Matrix.nonneg_iff_posSemidef.mp (star_right_conjugate_nonneg hA.1.nonneg g.1.val)
  · exact Matrix.nonneg_iff_posSemidef.mp (star_right_conjugate_nonneg hA.2.1.nonneg g.2.val)
  · change (g.1.val * A.1 * star g.1.val).trace + (g.2.val * A.2 * star g.2.val).trace = 1
    rw [Matrix.trace_mul_cycle, g.1.property.1, one_mul,
      Matrix.trace_mul_cycle, g.2.property.1, one_mul]
    exact hA.2.2
3497
lemma block_conjugateDensity (g : Group q) (A : Mat q × Mat q) :
    block (conjugateDensity g A) = (blockGroup g).val * block A * (blockGroup g⁻¹).val := by
proof · 3 lines
  change block (((g.1.val,g.2.val) * A) * star (g.1.val,g.2.val)) = _
  rw [map_mul, map_mul, block_star, map_inv]
  rfl
3504
lemma tensor_conjugateDensity (g : Group q) (A : Mat q × Mat q) :
    LinearMap.toContinuousLinearMap (TensorRep.action (ι := τ) (block (conjugateDensity g A))) =
      CompactTwirl.conjugate (CompactTwirl.continuousAction (rep (τ := τ)))
        (LinearMap.toContinuousLinearMap (TensorRep.action (block A))) g := by
proof · 7 lines
  apply ContinuousLinearMap.ext
  intro v
  change TensorRep.action (block (conjugateDensity g A)) v =
    TensorRep.action (blockGroup g).val (TensorRep.action (block A)
      (TensorRep.action (blockGroup g⁻¹).val v))
  rw [block_conjugateDensity, map_mul, map_mul]
  rfl
3520
instance groupMeasurableSpace : MeasurableSpace (Group q) := borel _
3521
instance groupBorelSpace : BorelSpace (Group q) := ⟨rfl⟩
3523
instance groupCompactSpace : CompactSpace (Group q) := by
body · 2 lines
  let := unitaryGroup_compactSpace (Fin q)
  infer_instance
3534
lemma projection_isStarProjection : IsStarProjection (typeProjection (τ := τ) a b q ha hb) :=
proof · 1 line
  isStarProjection_starProjection
3538
lemma twirl_lower (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    ((LinearMap.trace ℂ (Carrier a b q ha hb)
      (hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian)).re /
        (dimension a b q ha hb : ℝ)) • typeProjection (τ := τ) a b q ha hb ≤
    twirl (continuousAction (SignedTensor.rep (q := q) (τ := τ)))
      (LinearMap.toContinuousLinearMap (TensorRep.action (block A))) := by
proof · 10 lines
  exact isotypic_twirl_lower (E := Carrier a b q ha hb) (F := Raw (q := q) (τ := τ))
    (rep a b q ha hb) SignedTensor.rep
    (continuous_rep a b q ha hb) SignedTensor.continuous_rep SignedTensor.rep_unitary
    (LinearMap.toContinuousLinearMap (hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian))
    (LinearMap.toContinuousLinearMap (TensorRep.action (block A)))
    ((ContinuousLinearMap.nonneg_iff_isPositive).mpr
      (hermitianAction_positive a b q ha hb A hA.1 hA.2.1))
    ((ContinuousLinearMap.nonneg_iff_isPositive).mpr
      (TensorRep.action_positive (block A) (block_posSemidef A hA.1 hA.2.1)))
    (fun L x => (intertwiner_hermitian a b q ha hb L A hA.1.isHermitian hA.2.1.isHermitian x).symm)
3560
lemma rescale_trace_lower (P B : A) (hP : 0 ≤ P) (F t d : ℝ) (hd : 0 < d)
    (ht : Real.exp (-F) ≤ t) (h : (t / d) • P ≤ B) :
    P ≤ (Real.exp F * d) • B := by
proof · 7 lines
  have hbase : (Real.exp (-F) / d) • P ≤ B :=
    (smul_le_smul_of_nonneg_right (div_le_div_of_nonneg_right ht hd.le) hP).trans h
  have hs := smul_le_smul_of_nonneg_left hbase (mul_pos (Real.exp_pos F) hd).le
  have he : (Real.exp F * d) * (Real.exp (-F) / d) = 1 := by
    rw [← mul_div_assoc, mul_right_comm, mul_div_cancel_right₀ _ hd.ne', ← Real.exp_add]
    simp
  simpa only [smul_smul, he, one_smul] using hs
3575
lemma inverse_mono (a b : EuclideanSpace ℂ n →L[ℂ] EuclideanSpace ℂ n) (h : a ≤ b) :
    (Matrix.toEuclideanCLM (n := n) (𝕜 := ℂ)).symm a ≤ (Matrix.toEuclideanCLM (n := n) (𝕜 := ℂ)).symm b := by
proof · 3 lines
  apply sub_nonneg.mp
  rw [← map_sub]
  exact map_nonneg (Matrix.toEuclideanCLM (n := n) (𝕜 := ℂ)).symm (sub_nonneg.mpr h)
3590
theorem exists_density_mixture :
    ∃ (ι : Type) (_ : Fintype ι) (w : ι → ℝ) (R : ι → Mat q × Mat q),
      (∀ i, 0 ≤ w i) ∧ (∑ i, w i = 1) ∧ (∀ i, IsEvenDensity (R i)) ∧
      typeProjection (τ := τ) a b q ha hb ≤
        (Real.exp (entropy a b q) * (dimension a b q ha hb : ℝ)) •
          ∑ i, w i • LinearMap.toContinuousLinearMap (TensorRep.action (block (R i))) := by
proof · 13 lines
  obtain ⟨A,hA,htr⟩ := exists_density_trace_lower a b q ha hb
  obtain ⟨ι,hι,w,g,hw,hs,he⟩ := twirl_finite_mixture
    (SignedTensor.rep (q := q) (τ := τ)) SignedTensor.continuous_rep
    (LinearMap.toContinuousLinearMap (TensorRep.action (block A)))
  let := hι
  refine ⟨ι,hι,w,fun i => conjugateDensity (g i) A,hw,hs,
    fun i => conjugateDensity_isEvenDensity (g i) A hA,?_⟩
  have hh := twirl_lower (τ := τ) a b q ha hb A hA
  have hbound := ProjectionOrder.rescale_trace_lower _ _
    (projection_isStarProjection (τ := τ) a b q ha hb).nonneg (entropy a b q)
    _ (dimension a b q ha hb : ℝ) (by exact_mod_cast dimension_pos a b q ha hb) htr hh
  rw [he] at hbound
  simpa only [tensor_conjugateDensity] using hbound
3615
lemma inverse_real_smul (r : ℝ) (a : EuclideanSpace ℂ n →L[ℂ] EuclideanSpace ℂ n) :
    (Φ).symm (r • a) = r • (Φ).symm a := by
proof · 2 lines
  change (Φ).symm ((r : ℂ) • a) = (r : ℂ) • (Φ).symm a
  exact map_smul (Φ).symm _ _
3620
lemma forward_real_smul (r : ℝ) (a : Matrix n n ℂ) :
    Φ (r • a) = r • Φ a := by
proof · 2 lines
  change Φ ((r : ℂ) • a) = (r : ℂ) • Φ a
  exact map_smul Φ _ _
3625
lemma forward_norm (a : Matrix n n ℂ) : ‖Φ a‖ = ‖a‖ := rfl
3627
lemma inverse_norm (a : EuclideanSpace ℂ n →L[ℂ] EuclideanSpace ℂ n) :
    ‖(Φ).symm a‖ = ‖a‖ := by
proof · 1 line
  rw [← forward_norm, (Φ).apply_symm_apply]
3631
lemma inverse_toContinuous (a : Matrix n n ℂ) :
    (Φ).symm (LinearMap.toContinuousLinearMap (Matrix.toEuclideanLin a)) = a := by
proof · 1 line
  exact (Φ).symm_apply_apply a
3643
def matrixProjection : Matrix (τ → Spin q) (τ → Spin q) ℂ :=
body · 1 line
  (Matrix.toEuclideanCLM (n := τ → Spin q) (𝕜 := ℂ)).symm (typeProjection a b q ha hb)
3646
lemma matrixProjection_isStarProjection : IsStarProjection (matrixProjection (τ := τ) a b q ha hb) := by
proof · 10 lines
  have hp := projection_isStarProjection (τ := τ) a b q ha hb
  constructor
  · change matrixProjection a b q ha hb * matrixProjection a b q ha hb = _
    unfold matrixProjection
    rw [← map_mul, hp.isIdempotentElem.eq]
  · change star (matrixProjection a b q ha hb) = _
    unfold matrixProjection
    have hs := (Matrix.toEuclideanCLM (n := τ → Spin q) (𝕜 := ℂ)).symm.map_star'
      (typeProjection a b q ha hb)
    exact hs.symm.trans (congrArg (Matrix.toEuclideanCLM (n := τ → Spin q) (𝕜 := ℂ)).symm hp.isSelfAdjoint.star_eq)
3658
lemma inverse_action (A : Matrix (Spin q) (Spin q) ℂ) :
    (Matrix.toEuclideanCLM (n := τ → Spin q) (𝕜 := ℂ)).symm
      (LinearMap.toContinuousLinearMap (TensorRep.action A)) = TensorRep.tensor (fun _ : τ => A) :=
proof · 1 line
  MatrixBridge.inverse_toContinuous _
3663
lemma matrix_density_upper (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    ‖TensorRep.tensor (fun _ : τ => block A) * matrixProjection a b q ha hb‖ ≤
      Real.exp (-entropy a b q) := by
proof · 3 lines
  have h := global_density_upper (τ := τ) a b q ha hb A hA
  rw [← MatrixBridge.inverse_norm] at h
  simpa only [map_mul, inverse_action, matrixProjection] using h
3670
lemma density_projection_commute (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    Commute (LinearMap.toContinuousLinearMap (TensorRep.action (ι := τ) (block A)))
      (typeProjection a b q ha hb) := by
proof · 10 lines
  change _ * _ = _ * _
  apply ContinuousLinearMap.ext
  intro v
  exact (Isotypic.projection_commute_natural (E := Carrier a b q ha hb) (F := Raw (q := q) (τ := τ))
    (rep a b q ha hb) SignedTensor.rep
    (hermitianAction a b q ha hb A hA.1.isHermitian hA.2.1.isHermitian)
    (TensorRep.action (block A))
    (TensorRep.action_positive (block A) (block_posSemidef A hA.1 hA.2.1)).isSymmetric
    (fun L x => (intertwiner_hermitian a b q ha hb L A hA.1.isHermitian hA.2.1.isHermitian x).symm)
    v).symm
3684
lemma matrix_density_commute (A : Mat q × Mat q) (hA : IsEvenDensity A) :
    Commute (TensorRep.tensor (fun _ : τ => block A)) (matrixProjection a b q ha hb) := by
proof · 3 lines
  have h := (density_projection_commute (τ := τ) a b q ha hb A hA).map
    (Matrix.toEuclideanCLM (n := τ → Spin q) (𝕜 := ℂ)).symm
  simpa only [inverse_action, matrixProjection] using h
3693
theorem exists_matrix_density_mixture :
    ∃ (ι : Type) (_ : Fintype ι) (w : ι → ℝ) (R : ι → Mat q × Mat q),
      (∀ i, 0 ≤ w i) ∧ (∑ i, w i = 1) ∧ (∀ i, IsEvenDensity (R i)) ∧
      matrixProjection (τ := τ) a b q ha hb ≤
        (Real.exp (entropy a b q) * (dimension a b q ha hb : ℝ)) •
          ∑ i, w i • TensorRep.tensor (fun _ : τ => block (R i)) := by
proof · 5 lines
  obtain ⟨ι,hι,w,R,hw,hs,hR,hdom⟩ := exists_density_mixture (τ := τ) a b q ha hb
  let := hι
  refine ⟨ι,hι,w,R,hw,hs,hR,?_⟩
  have h := MatrixBridge.inverse_mono _ _ hdom
  simpa only [MatrixBridge.inverse_real_smul, map_sum, inverse_action, matrixProjection] using h
3709
lemma block_add (A B : Mat q × Mat q) : block (A+B) = block A + block B := by
proof · 3 lines
  ext i j
  rcases i with ⟨i,bi⟩; rcases j with ⟨j,bj⟩
  cases bi <;> cases bj <;> simp [block, Matrix.blockDiagonal_apply]
3714
lemma block_real_smul (r : ℝ) (A : Mat q × Mat q) : block (r • A) = r • block A := by
proof · 3 lines
  ext i j
  rcases i with ⟨i,bi⟩; rcases j with ⟨j,bj⟩
  cases bi <;> cases bj <;> simp [block, Matrix.blockDiagonal_apply]
3719
lemma block_zero : block (0 : Mat q × Mat q) = 0 := by
proof · 3 lines
  ext i j
  rcases i with ⟨i,bi⟩; rcases j with ⟨j,bj⟩
  cases bi <;> cases bj <;> simp [block, Matrix.blockDiagonal_apply]
3724
lemma block_sum {ι : Type*} (s : Finset ι) (A : ι → Mat q × Mat q) :
    block (∑ i ∈ s, A i) = ∑ i ∈ s, block (A i) := by
proof · 4 lines
  classical
  induction s using Finset.induction_on with
  | empty => simp [block_zero]
  | insert i s hi ih => simp only [Finset.sum_insert hi, block_add, ih]
3731
lemma block_trace (A : Mat q × Mat q) : (block A).trace = A.1.trace + A.2.trace := by
proof · 3 lines
  change (Matrix.blockDiagonal (fun b : Bool => cond b A.2 A.1)).trace = _
  rw [Matrix.trace_blockDiagonal]
  simp [add_comm]
3738
def evenAverage (A : J → Mat q × Mat q) : Mat q × Mat q :=
body · 1 line
  (Fintype.card J : ℝ)⁻¹ • ∑ j, A j
3741
lemma evenAverage_isEvenDensity [Nonempty J] (A : J → Mat q × Mat q)
    (hA : ∀ j, IsEvenDensity (A j)) : IsEvenDensity (evenAverage A) := by
proof · 16 lines
  have hpos : 0 < (Fintype.card J : ℝ) := by exact_mod_cast Fintype.card_pos
  have he (A : Mat q × Mat q) : A.1.trace + A.2.trace = (block A).trace := (block_trace A).symm
  refine ⟨?_,?_,?_⟩
  · apply Matrix.nonneg_iff_posSemidef.mp
    simp only [evenAverage, Prod.smul_fst, Prod.fst_sum]
    exact smul_nonneg (inv_nonneg.mpr hpos.le) (Finset.sum_nonneg fun j _ => (hA j).1.nonneg)
  · apply Matrix.nonneg_iff_posSemidef.mp
    simp only [evenAverage, Prod.smul_snd, Prod.snd_sum]
    exact smul_nonneg (inv_nonneg.mpr hpos.le) (Finset.sum_nonneg fun j _ => (hA j).2.1.nonneg)
  · rw [he]
    unfold evenAverage
    rw [block_real_smul, block_sum, Matrix.trace_smul, Matrix.trace_sum]
    simp only [block_trace, (hA _).2.2, Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one]
    rw [RCLike.real_smul_eq_coe_smul (K := ℂ), smul_eq_mul]
    norm_cast
    exact inv_mul_cancel₀ (by exact_mod_cast hpos.ne')
3771
abbrev Mat (n : Type*) := Matrix n n ℂ
3774
def root (a : Mat n) : Mat n := cfc Real.sqrt a
3776
def invRoot (a : Mat n) : Mat n := cfc (fun x : ℝ => (Real.sqrt x)⁻¹) a
3778
def support (a : Mat n) : Mat n := cfc (fun x : ℝ => if x = 0 then 0 else 1) a
3780
lemma root_selfAdjoint (a : Mat n) : IsSelfAdjoint (root a) :=
proof · 1 line
  cfc_predicate _ _
3782
lemma root_nonneg (a : Mat n) : 0 ≤ root a :=
proof · 1 line
  cfc_nonneg fun _ _ => Real.sqrt_nonneg _
3784
lemma invRoot_selfAdjoint (a : Mat n) : IsSelfAdjoint (invRoot a) :=
proof · 1 line
  cfc_predicate _ _
3786
lemma invRoot_nonneg (a : Mat n) : 0 ≤ invRoot a :=
proof · 1 line
  cfc_nonneg fun _ _ => inv_nonneg.mpr (Real.sqrt_nonneg _)
3788
lemma support_selfAdjoint (a : Mat n) : IsSelfAdjoint (support a) :=
proof · 1 line
  cfc_predicate _ _
3791
lemma root_mul_self (a : Mat n) (ha : 0 ≤ a) : root a * root a = a := by
proof · 8 lines
  rw [root, ← cfc_mul _ _ _ (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)
    (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)]
  calc
    _ = cfc (fun x : ℝ => x) a := by
      apply cfc_congr
      intro x hx
      exact Real.mul_self_sqrt (spectrum_nonneg_of_nonneg ha hx)
    _ = a := cfc_id' ℝ (ha := .of_nonneg ha)
3801
lemma invRoot_mul_root (a : Mat n) (ha : 0 ≤ a) :
    invRoot a * root a = support a := by
proof · 9 lines
  rw [invRoot, root, ← cfc_mul _ _ _ (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)
    (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)]
  apply cfc_congr
  intro x hx
  by_cases hx0 : x = 0
  · simp [hx0]
  · have hs : Real.sqrt x ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr
      (lt_of_le_of_ne (spectrum_nonneg_of_nonneg ha hx) (Ne.symm hx0)))
    simp [hx0, hs]
3813
lemma support_isStarProjection (a : Mat n) : IsStarProjection (support a) := by
proof · 8 lines
  refine ⟨?_, support_selfAdjoint a⟩
  change support a * support a = support a
  rw [support, ← cfc_mul _ _ _ (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)
    (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)]
  apply cfc_congr
  intro x _
  dsimp only
  split_ifs <;> norm_num
3823
lemma mul_support (a : Mat n) (ha : 0 ≤ a) : a * support a = a := by
proof · 9 lines
  conv_lhs => lhs; rw [← cfc_id' ℝ (a := a)]
  rw [support, ← cfc_mul _ _ _ (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)
    (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)]
  calc
    _ = cfc (fun x : ℝ => x) a := by
      apply cfc_congr
      intro x _
      by_cases hx : x = 0 <;> simp [hx]
    _ = a := cfc_id' ℝ (ha := .of_nonneg ha)
3834
lemma root_mul_support (a : Mat n) : root a * support a = root a := by
proof · 5 lines
  rw [root, support, ← cfc_mul _ _ _ (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)
    (Matrix.finite_real_spectrum (A := a) |>.continuousOn _)]
  apply cfc_congr
  intro x _
  by_cases hx : x = 0 <;> simp [hx]
3841
lemma root_commute {a b : Mat n} (h : Commute a b) : Commute (root a) b :=
proof · 1 line
  h.cfc_real _
3843
lemma invRoot_commute {a b : Mat n} (h : Commute a b) : Commute (invRoot a) b :=
proof · 1 line
  h.cfc_real _
3845
lemma support_commute {a b : Mat n} (h : Commute a b) : Commute (support a) b :=
proof · 1 line
  h.cfc_real _
3850
lemma norm_le_re_trace (a : Mat n) (ha : 0 ≤ a) : ‖a‖ ≤ (Matrix.trace a).re := by
proof · 12 lines
  have hA := Matrix.nonneg_iff_posSemidef.mp ha
  rw [hA.isHermitian.trace_eq_sum_eigenvalues, Complex.re_sum]
  conv_lhs => rw [hA.isHermitian.spectral_theorem]
  simp only [Unitary.conjStarAlgAut_apply, ← Unitary.coe_star,
    CStarRing.norm_mul_coe_unitary, CStarRing.norm_coe_unitary_mul,
    Matrix.l2_opNorm_diagonal]
  apply (pi_norm_le_iff_of_nonneg (by
    exact Finset.sum_nonneg fun i _ => hA.eigenvalues_nonneg i)).mpr
  intro i
  simp only [Function.comp_apply, RCLike.norm_ofReal,
    abs_of_nonneg (hA.eigenvalues_nonneg i)]
  exact Finset.single_le_sum (fun j _ => hA.eigenvalues_nonneg j) (Finset.mem_univ i)
3864
lemma norm_sq_le_re_trace_star_mul_self (a : Mat n) :
    ‖a‖ ^ 2 ≤ (Matrix.trace (star a * a)).re := by
proof · 2 lines
  rw [sq, ← CStarRing.norm_star_mul_self]
  exact norm_le_re_trace _ (star_mul_self_nonneg _)
3871
lemma root_mul_support_of_le_smul (a b : Mat n) (ha : 0 ≤ a) (hb : 0 ≤ b)
    (c : ℝ) (hba : b ≤ c • a) : root b * support a = root b := by
proof · 20 lines
  let e := 1 - support a
  have he : IsStarProjection e := (support_isStarProjection a).one_sub
  have hae : a * e = 0 := by
    dsimp [e]
    rw [mul_sub, mul_one, mul_support a ha, sub_self]
  have hupper : star e * b * e ≤ 0 := by
    have hc := star_left_conjugate_le_conjugate hba e
    simpa only [mul_smul_comm, smul_mul_assoc, mul_assoc, hae, mul_zero, smul_zero] using hc
  have hzero : star e * b * e = 0 :=
    le_antisymm hupper (star_left_conjugate_nonneg hb e)
  have hrootzero : star (root b * e) * (root b * e) = 0 := by
    rw [star_mul, (root_selfAdjoint b).star_eq]
    calc
      (star e * root b) * (root b * e) = star e * (root b * root b) * e := by
        simp only [mul_assoc]
      _ = 0 := by rw [root_mul_self b hb, hzero]
  have h := (CStarRing.star_mul_self_eq_zero_iff _).mp hrootzero
  dsimp [e] at h
  rw [mul_sub, mul_one] at h
  exact (sub_eq_zero.mp h).symm
3894
lemma invRoot_mul_self_mul_invRoot (a : Mat n) (ha : 0 ≤ a) :
    invRoot a * a * invRoot a = support a := by
proof · 8 lines
  have hc : Commute (invRoot a) a := invRoot_commute (Commute.refl a)
  have hr : Commute (root a) (invRoot a) := root_commute hc.symm
  calc
    invRoot a * a * invRoot a = invRoot a * (root a * root a) * invRoot a :=
      congrArg (fun x => invRoot a * x * invRoot a) (root_mul_self a ha).symm
    _ = (invRoot a * root a) * (root a * invRoot a) := by simp only [mul_assoc]
    _ = support a * support a := by rw [hr.eq, invRoot_mul_root a ha]
    _ = support a := (support_isStarProjection a).isIdempotentElem.eq
3905
lemma cell_trace_identity (a b c : Mat n) (ha : 0 ≤ a) (hc : 0 ≤ c) :
    (star (root a * invRoot b * root c) * (root a * invRoot b * root c)).trace =
      (invRoot b * a * invRoot b * c).trace := by
proof · 11 lines
  have he : star (root a * invRoot b * root c) * (root a * invRoot b * root c) =
      root c * (invRoot b * a * invRoot b) * root c := by
    simp only [star_mul, (root_selfAdjoint a).star_eq,
      (root_selfAdjoint c).star_eq, (invRoot_selfAdjoint b).star_eq]
    calc
      root c * (invRoot b * root a) * (root a * invRoot b * root c) =
          root c * (invRoot b * (root a * root a) * invRoot b) * root c := by
            simp only [mul_assoc]
      _ = _ := by rw [root_mul_self a ha]
  rw [he, Matrix.trace_mul_comm (root c * (invRoot b * a * invRoot b)) (root c)]
  rw [← mul_assoc, root_mul_self c hc, Matrix.trace_mul_comm c]
3920
lemma cell_trace_nonneg (a b c : Mat n) (ha : 0 ≤ a) (hc : 0 ≤ c) :
    0 ≤ (invRoot b * a * invRoot b * c).trace := by
proof · 2 lines
  rw [← cell_trace_identity a b c ha hc]
  exact (Matrix.nonneg_iff_posSemidef.mp (star_mul_self_nonneg _)).trace_nonneg
3926
lemma re_trace_nonneg
    {n : Type*}
    [contextInstance3521_21 : Fintype n]
    [DecidableEq n] (a : Mat n) (ha : 0 ≤ a) : 0 ≤ a.trace.re :=
proof · 1 line
  (Complex.nonneg_iff.mp ((Matrix.nonneg_iff_posSemidef.mp ha).trace_nonneg)).1
3932
lemma re_trace_mono {a b : Mat n} (hab : a ≤ b) : a.trace.re ≤ b.trace.re := by
proof · 2 lines
  have h := re_trace_nonneg (b - a) (sub_nonneg.mpr hab)
  simpa only [Matrix.trace_sub, Complex.sub_re, sub_nonneg] using h
3938
lemma re_trace_projection_mul_le (q b : Mat n) (hq : IsStarProjection q) (hb : 0 ≤ b) :
    (q * b).trace.re ≤ b.trace.re := by
proof · 5 lines
  have h := re_trace_mono (star_left_conjugate_le_conjugate hq.le_one (root b))
  rw [mul_one, (root_selfAdjoint b).star_eq, root_mul_self b hb] at h
  rw [Matrix.trace_mul_comm (root b * q) (root b), ← mul_assoc,
    root_mul_self b hb, Matrix.trace_mul_comm b q] at h
  exact h
3947
lemma cell_norm_sq_le_trace (a b c : Mat n) (ha : 0 ≤ a) (hc : 0 ≤ c) :
    ‖root a * invRoot b * root c‖ ^ 2 ≤
      (invRoot b * a * invRoot b * c).trace.re := by
proof · 3 lines
  have h := norm_sq_le_re_trace_star_mul_self (root a * invRoot b * root c)
  rw [cell_trace_identity a b c ha hc] at h
  exact h
3961
def tensor (a : ι → Mat n) : Mat (ι → n) := fun x y => ∏ i, a i (x i) (y i)
3963
lemma tensor_star
    {ι : Type*} {n : Type*}
    [contextInstance3707_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [DecidableEq n] (a : ι → Mat n) :
    tensor (fun i => star (a i)) = star (tensor a) := by
proof · 2 lines
  ext x y
  simp [tensor, Matrix.star_apply]
3973
lemma tensor_mul
    {ι : Type*} {n : Type*}
    [contextInstance3707_23 : Fintype ι]
    [contextInstance3707_35 : DecidableEq ι]
    [contextInstance3707_51 : Fintype n]
    [DecidableEq n] (a b : ι → Mat n) :
    tensor (fun i => a i * b i) = tensor a * tensor b := by
proof · 5 lines
  ext x y
  simp only [tensor, Matrix.mul_apply, ← Finset.prod_mul_distrib]
  symm
  simpa using (Finset.sum_prod_piFinset (ι := ι) Finset.univ
    (fun i j => a i (x i) j * b i j (y i)))
3986
lemma tensor_one
    {ι : Type*} {n : Type*}
    [contextInstance3707_23 : Fintype ι]
    [DecidableEq ι]
    [Fintype n]
    [contextInstance3707_63 : DecidableEq n] : tensor (fun _ : ι => (1 : Mat n)) = 1 := by
proof · 10 lines
  ext x y
  classical
  by_cases h : x = y
  · subst y
    simp [tensor]
  · have hxy : ∃ i, x i ≠ y i := Function.ne_iff.mp h
    obtain ⟨i, hi⟩ := hxy
    simp only [tensor, Matrix.one_apply, ite_eq_right h]
    apply Finset.prod_eq_zero (Finset.mem_univ i)
    simp [hi]
4003
lemma tensor_trace
    {ι : Type*} {n : Type*}
    [contextInstance3707_23 : Fintype ι]
    [contextInstance3707_35 : DecidableEq ι]
    [contextInstance3707_51 : Fintype n]
    [DecidableEq n] (a : ι → Mat n) :
    (tensor a).trace = ∏ i, (a i).trace := by
proof · 3 lines
  simp only [Matrix.trace, Matrix.diag, tensor]
  simpa using (Finset.sum_prod_piFinset (ι := ι) Finset.univ
    (fun i j => a i j j))
4014
lemma tensor_nonneg (a : ι → Mat n) (ha : ∀ i, 0 ≤ a i) :
    0 ≤ tensor a := by
proof · 7 lines
  have hr : tensor a = star (tensor (fun i => root (a i))) * tensor (fun i => root (a i)) := by
    rw [← tensor_star, ← tensor_mul]
    congr 1
    funext i
    rw [(root_selfAdjoint (a i)).star_eq, root_mul_self _ (ha i)]
  rw [hr]
  exact star_mul_self_nonneg _
4024
lemma tensor_selfAdjoint (a : ι → Mat n) (ha : ∀ i, IsSelfAdjoint (a i)) :
    IsSelfAdjoint (tensor a) := by
proof · 5 lines
  change star (tensor a) = tensor a
  rw [← tensor_star]
  congr 1
  funext i
  exact (ha i).star_eq
4032
lemma tensor_norm_sq_le_trace_product (a : ι → Mat n) :
    ‖tensor a‖ ^ 2 ≤ (∏ i, (star (a i) * a i).trace).re := by
proof · 3 lines
  have h := norm_sq_le_re_trace_star_mul_self (tensor a)
  rw [← tensor_star, ← tensor_mul, tensor_trace] at h
  exact h
4038
lemma tensor_norm_sq_le_re_trace_product (a : ι → Mat n) :
    ‖tensor a‖ ^ 2 ≤ ∏ i, (star (a i) * a i).trace.re := by
proof · 8 lines
  have hreal (i : ι) : (star (a i) * a i).trace = ((star (a i) * a i).trace.re : ℂ) :=
    Complex.eq_re_of_ofReal_le (r := 0)
      (by exact (Matrix.nonneg_iff_posSemidef.mp (star_mul_self_nonneg _)).trace_nonneg)
  calc
    _ ≤ (∏ i, (star (a i) * a i).trace).re := tensor_norm_sq_le_trace_product a
    _ = (∏ i, ((star (a i) * a i).trace.re : ℂ)).re :=
      congrArg Complex.re (Finset.prod_congr rfl fun i _ => hreal i)
    _ = _ := by rw [← Complex.ofReal_prod, Complex.ofReal_re]
4049
lemma tensor_cell_norm_sq_le_product (a b c : ι → Mat n)
    (ha : ∀ i, 0 ≤ a i) (hc : ∀ i, 0 ≤ c i) :
    ‖tensor (fun i => root (a i) * invRoot (b i) * root (c i))‖ ^ 2 ≤
      ∏ i, (invRoot (b i) * a i * invRoot (b i) * c i).trace.re := by
proof · 2 lines
  have h := tensor_norm_sq_le_re_trace_product (fun i => root (a i) * invRoot (b i) * root (c i))
  simpa only [cell_trace_identity _ _ _ (ha _) (hc _)] using h
4065
def missingCellFactor (m b : ℕ) : ℝ :=
body · 1 line
  if b < m then ((m : ℝ) / (m - b : ℕ)) ^ (m - b) else 1
4068
def missingCellProductFactor {ι : Type*} [Fintype ι] (m : ℕ) (b : ι → ℕ) : ℝ :=
body · 1 line
  ∏ i, missingCellFactor m (b i)
4071
lemma missingCellFactor_nonneg (m b : ℕ) : 0 ≤ missingCellFactor m b := by
proof · 2 lines
  unfold missingCellFactor
  split_ifs <;> positivity
4075
lemma missingCellFactor_le_exp (m b : ℕ) (hb : b ≤ m) :
    missingCellFactor m b ≤ Real.exp b := by
proof · 16 lines
  unfold missingCellFactor
  split_ifs with h
  · have hk : 0 < m - b := Nat.sub_pos_of_lt h
    have hkR : (0 : ℝ) < (m - b : ℕ) := by exact_mod_cast hk
    have hmR : 0 < (m : ℝ) := by exact_mod_cast (lt_of_le_of_lt (Nat.zero_le b) h)
    have hratio : 0 < (m : ℝ) / (m - b : ℕ) := div_pos hmR hkR
    apply (Real.log_le_iff_le_exp (pow_pos hratio _)).mp
    rw [Real.log_pow]
    calc
      (m - b : ℕ) * Real.log ((m : ℝ) / (m - b : ℕ))
        ≤ (m - b : ℕ) * ((m : ℝ) / (m - b : ℕ) - 1) :=
          mul_le_mul_of_nonneg_left (Real.log_le_sub_one_of_pos hratio) hkR.le
      _ = (b : ℝ) := by
        rw [mul_sub, mul_div_cancel₀ _ hkR.ne', mul_one, Nat.cast_sub hb]
        ring
  · exact Real.one_le_exp (Nat.cast_nonneg b)
4094
theorem missingCellProduct {ι : Type*} [Fintype ι]
    (m : ℕ) (b : ι → ℕ) (hb : ∀ i, b i ≤ m) :
    missingCellProductFactor m b ≤ Real.exp (∑ i, (b i : ℝ)) := by
proof · 6 lines
  unfold missingCellProductFactor
  calc
    ∏ i, missingCellFactor m (b i) ≤ ∏ i, Real.exp (b i : ℝ) :=
      Finset.prod_le_prod₀ (fun i _ => missingCellFactor_nonneg m (b i))
        (fun i _ => missingCellFactor_le_exp m (b i) (hb i))
    _ = Real.exp (∑ i, (b i : ℝ)) := (Real.exp_sum _ _).symm
4111
lemma retained_product_le
    {ι : Type*}
    [contextInstance3841_21 : Fintype ι]
    [DecidableEq ι] (t : Finset ι) (z : ι → ℝ)
    (hz : ∀ i, 0 ≤ z i) (hs : ∑ i, z i ≤ Fintype.card ι) :
    ∏ i ∈ t, z i ≤ ((Fintype.card ι : ℝ) / t.card) ^ t.card := by
proof · 29 lines
  by_cases ht : t.Nonempty
  · have hkR : 0 < (t.card : ℝ) := by exact_mod_cast ht.card_pos
    have hmR : 0 < (Fintype.card ι : ℝ) :=
      hkR.trans_le (by exact_mod_cast Finset.card_le_univ t)
    let c : ℝ := Fintype.card ι / t.card
    have hc : 0 < c := div_pos hmR hkR
    have hsum : ∑ i ∈ t, z i ≤ Fintype.card ι :=
      (Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _) (by
        intro i _ _; exact hz i)).trans hs
    have hnorm : ∑ i ∈ t, z i / c ≤ t.card := by
      rw [← Finset.sum_div]
      apply (div_le_iff₀ hc).mpr
      dsimp [c]
      rwa [mul_div_cancel₀ _ hkR.ne']
    have hn : ∏ i ∈ t, z i / c ≤ 1 := calc
      _ ≤ ∏ i ∈ t, Real.exp (z i / c - 1) :=
        Finset.prod_le_prod₀ (fun i _ => div_nonneg (hz i) hc.le) fun i _ => by
          have h := Real.add_one_le_exp (z i / c - 1)
          linarith
      _ = Real.exp (∑ i ∈ t, (z i / c - 1)) := (Real.exp_sum _ _).symm
      _ ≤ Real.exp 0 := by
        apply Real.exp_le_exp.mpr
        rw [Finset.sum_sub_distrib]
        simpa using sub_nonpos.mpr hnorm
      _ = 1 := Real.exp_zero
    rw [Finset.prod_div_distrib, Finset.prod_const] at hn
    exact (div_le_one (pow_pos hc _)).mp hn
  · have he : t = ∅ := Finset.not_nonempty_iff_eq_empty.mp ht
    simp [he]
4150
def occupiedRow (W : Finset (R × C)) (i : R) : Finset C :=
body · 1 line
  Finset.univ.filter (fun j => (i, j) ∈ W)
4153
def missingInRow (W : Finset (R × C)) (i : R) : ℕ :=
body · 1 line
  Fintype.card C - (occupiedRow W i).card
4156
lemma occupiedRow_card_le
    {R : Type*} {C : Type*}
    [Fintype R]
    [contextInstance3877_35 : DecidableEq R]
    [contextInstance3877_51 : Fintype C]
    [contextInstance3877_63 : DecidableEq C] (W : Finset (R × C)) (i : R) :
    (occupiedRow W i).card ≤ Fintype.card C := Finset.card_le_univ _
4164
lemma missingInRow_le
    {R : Type*} {C : Type*}
    [Fintype R]
    [contextInstance3877_35 : DecidableEq R]
    [contextInstance3877_51 : Fintype C]
    [contextInstance3877_63 : DecidableEq C] (W : Finset (R × C)) (i : R) :
    missingInRow W i ≤ Fintype.card C := Nat.sub_le _ _
4172
lemma retained_factor_eq_missingCellFactor (W : Finset (R × C)) (i : R) :
    ((Fintype.card C : ℝ) / (occupiedRow W i).card) ^ (occupiedRow W i).card =
      missingCellFactor (Fintype.card C) (missingInRow W i) := by
proof · 7 lines
  have hc := occupiedRow_card_le W i
  unfold missingInRow missingCellFactor
  rw [Nat.sub_sub_self hc]
  split_ifs with h
  · rfl
  · have hk : (occupiedRow W i).card = 0 := by omega
    simp [hk]
4183
lemma prod_occupiedRow
    {ι : Type*}
    [Fintype ι]
    [DecidableEq ι]
    {R : Type*} {C : Type*}
    [contextInstance3877_23 : Fintype R]
    [contextInstance3877_35 : DecidableEq R]
    [contextInstance3877_51 : Fintype C]
    [contextInstance3877_63 : DecidableEq C] (W : Finset (R × C)) (z : R → C → ℝ) :
    (∏ i, ∏ j ∈ occupiedRow W i, z i j) = ∏ x ∈ W, z x.1 x.2 := by
proof · 6 lines
  simp only [occupiedRow, Finset.prod_filter]
  rw [← Finset.prod_product (f := fun x : R × C => if x ∈ W then z x.1 x.2 else 1)]
  simp only [Finset.univ_product_univ, ← Finset.prod_filter]
  congr 1
  ext x
  simp
4201
lemma board_product_le (W : Finset (R × C)) (z : R → C → ℝ)
    (hz : ∀ i j, 0 ≤ z i j) (hs : ∀ i, ∑ j, z i j ≤ Fintype.card C) :
    (∏ x ∈ W, z x.1 x.2) ≤
      missingCellProductFactor (Fintype.card C) (missingInRow W) := by
proof · 8 lines
  rw [← prod_occupiedRow (ι := R) W z]
  unfold missingCellProductFactor
  apply Finset.prod_le_prod₀
  · intro i _
    exact Finset.prod_nonneg fun j _ => hz i j
  · intro i _
    rw [← retained_factor_eq_missingCellFactor]
    exact retained_product_le (occupiedRow W i) (z i) (hz i) (hs i)
4220
structure Density (n : Type*) [Fintype n] [DecidableEq n] where
body · 3 lines
  matrix : Mat n
  nonneg : 0 ≤ matrix
  trace_eq_one : matrix.trace = 1
4225
instance : Coe (Density n) (Mat n) := ⟨Density.matrix⟩
4227
def average (r : C → Density n) : Mat n :=
body · 1 line
  (Fintype.card C : ℝ)⁻¹ • ∑ j, (r j).matrix
4230
lemma average_nonneg
    {n : Type*} {C : Type*}
    [contextInstance3931_23 : Fintype n]
    [contextInstance3931_35 : DecidableEq n]
    [contextInstance3931_51 : Fintype C]
    [DecidableEq C] (r : C → Density n) : 0 ≤ average r :=
proof · 1 line
  smul_nonneg (inv_nonneg.mpr (Nat.cast_nonneg _)) (Finset.sum_nonneg fun j _ => (r j).nonneg)
4238
lemma sum_eq_smul_average
    {n : Type*} {C : Type*}
    [contextInstance3931_23 : Fintype n]
    [contextInstance3931_35 : DecidableEq n]
    [contextInstance3931_51 : Fintype C]
    [DecidableEq C] [Nonempty C] (r : C → Density n) :
    (∑ j, (r j).matrix) = (Fintype.card C : ℝ) • average r := by
proof · 2 lines
  unfold average
  rw [smul_smul, mul_inv_cancel₀ (by exact_mod_cast Fintype.card_ne_zero (α := C)), one_smul]
4248
lemma le_smul_average [Nonempty C] (r : C → Density n) (j : C) :
    (r j).matrix ≤ (Fintype.card C : ℝ) • average r := by
proof · 2 lines
  rw [← sum_eq_smul_average]
  exact Finset.single_le_sum (fun k _ => (r k).nonneg) (Finset.mem_univ j)
4253
lemma support_cell_trace_sum [Nonempty C] (r : C → Density n) (s : Density n) :
    ∑ j, (invRoot (average r) * (r j).matrix * invRoot (average r) * s.matrix).trace.re ≤
      Fintype.card C := by
proof · 12 lines
  have he : (∑ j, invRoot (average r) * (r j).matrix * invRoot (average r) * s.matrix) =
      (Fintype.card C : ℝ) • (support (average r) * s.matrix) := by
    rw [← Finset.sum_mul, ← Finset.sum_mul, ← Finset.mul_sum,
      sum_eq_smul_average r, mul_smul_comm, smul_mul_assoc, smul_mul_assoc,
      invRoot_mul_self_mul_invRoot _ (average_nonneg r)]
  have ht := congrArg (fun a : Mat n => a.trace.re) he
  simp only [Matrix.trace_sum, Complex.re_sum, Matrix.trace_smul, Complex.smul_re, smul_eq_mul] at ht
  rw [ht]
  have h := re_trace_projection_mul_le (support (average r)) s.matrix
    (support_isStarProjection _) s.nonneg
  rw [s.trace_eq_one, Complex.one_re] at h
  simpa using mul_le_mul_of_nonneg_left h (Nat.cast_nonneg (Fintype.card C))
4278
theorem normalized_board_bound (W : Finset (R × C))
    (r : C → Density n) (s : R → Density n) :
    ‖tensor (fun w : W => root (r w.val.2).matrix *
      invRoot (average r) * root (s w.val.1).matrix)‖ ^ 2 ≤
        missingCellProductFactor (Fintype.card C) (missingInRow W) := by
proof · 12 lines
  let z : R → C → ℝ := fun i j =>
    (invRoot (average r) * (r j).matrix * invRoot (average r) * (s i).matrix).trace.re
  have hz : ∀ i j, 0 ≤ z i j := fun i j =>
    (Complex.nonneg_iff.mp (cell_trace_nonneg _ _ _ (r j).nonneg (s i).nonneg)).1
  have hs : ∀ i, ∑ j, z i j ≤ Fintype.card C := fun i => support_cell_trace_sum r (s i)
  calc
    _ ≤ ∏ w : W, z w.val.1 w.val.2 :=
      tensor_cell_norm_sq_le_product _ _ _ (fun w => (r w.val.2).nonneg)
        (fun w => (s w.val.1).nonneg)
    _ = ∏ w ∈ W, z w.1 w.2 :=
      (Finset.prod_subtype W (by intro w; rfl) (fun w => z w.1 w.2)).symm
    _ ≤ _ := board_product_le W z hz hs
4309
abbrev Mat (n : Type*) := Matrix n n ℂ
4311
def tensor (A : ∀ i, Mat (ν i)) : Mat (∀ i, ν i) := fun x y => ∏ i, A i (x i) (y i)
4313
lemma tensor_star
    {ι : Type*}
    [contextInstance4012_21 : Fintype ι]
    [DecidableEq ι]
    {ν : ι → Type*}
    [∀ i, Fintype (ν i)]
    [∀ i, DecidableEq (ν i)] (A : ∀ i, Mat (ν i)) : tensor (fun i => star (A i)) = star (tensor A) := by
proof · 2 lines
  ext x y
  simp [tensor, Matrix.star_apply]
4323
lemma tensor_mul
    {ι : Type*}
    [contextInstance4012_21 : Fintype ι]
    [contextInstance4012_33 : DecidableEq ι]
    {ν : ι → Type*}
    [contextInstance4013_25 : ∀ i, Fintype (ν i)]
    [∀ i, DecidableEq (ν i)] (A B : ∀ i, Mat (ν i)) : tensor (fun i => A i * B i) = tensor A * tensor B := by
proof · 3 lines
  ext x y
  simp only [tensor, Matrix.mul_apply, ← Finset.prod_mul_distrib]
  exact (Fintype.prod_sum (fun i (j : ν i) => A i (x i) j * B i j (y i)))
4334
lemma tensor_one
    {ι : Type*}
    [contextInstance4012_21 : Fintype ι]
    [DecidableEq ι]
    {ν : ι → Type*}
    [∀ i, Fintype (ν i)]
    [contextInstance4013_46 : ∀ i, DecidableEq (ν i)] : tensor (fun i : ι => (1 : Mat (ν i))) = 1 := by
proof · 8 lines
  ext x y
  classical
  by_cases h : x = y
  · subst y; simp [tensor]
  · obtain ⟨i,hi⟩ := Function.ne_iff.mp h
    simp only [tensor, Matrix.one_apply, ite_eq_right h]
    apply Finset.prod_eq_zero (Finset.mem_univ i)
    simp [hi]
4350
lemma tensor_nonneg (A : ∀ i, Mat (ν i)) (hA : ∀ i, 0 ≤ A i) : 0 ≤ tensor A := by
proof · 8 lines
  have he : tensor A = star (tensor (fun i => CFC.sqrt (A i))) * tensor (fun i => CFC.sqrt (A i)) := by
    rw [← tensor_star, ← tensor_mul]
    congr 1
    funext i
    rw [(IsSelfAdjoint.of_nonneg (CFC.sqrt_nonneg (A i))).star_eq,
      CFC.sqrt_mul_sqrt_self (A i) (hA i)]
  rw [he]
  exact star_mul_self_nonneg _
4360
lemma tensor_projection (P : ∀ i, Mat (ν i)) (hP : ∀ i, IsStarProjection (P i)) :
    IsStarProjection (tensor P) := by
proof · 11 lines
  constructor
  · change tensor P * tensor P = tensor P
    rw [← tensor_mul]
    congr 1
    funext i
    exact (hP i).isIdempotentElem.eq
  · change star (tensor P) = tensor P
    rw [← tensor_star]
    congr 1
    funext i
    exact (hP i).isSelfAdjoint.star_eq
4374
lemma tensor_sum
    {ι : Type*}
    [contextInstance4012_21 : Fintype ι]
    [contextInstance4012_33 : DecidableEq ι]
    {ν : ι → Type*}
    [∀ i, Fintype (ν i)]
    [∀ i, DecidableEq (ν i)] {κ : ι → Type*} [∀ i, Fintype (κ i)]
    (A : ∀ i, κ i → Mat (ν i)) :
    tensor (fun i => ∑ k, A i k) = ∑ f : ∀ i, κ i, tensor (fun i => A i (f i)) := by
proof · 3 lines
  ext x y
  simp only [tensor, Matrix.sum_apply]
  exact (Fintype.prod_sum (fun i (k : κ i) => A i k (x i) (y i)))
4387
lemma tensor_smul
    {ι : Type*}
    [contextInstance4012_21 : Fintype ι]
    [DecidableEq ι]
    {ν : ι → Type*}
    [∀ i, Fintype (ν i)]
    [∀ i, DecidableEq (ν i)] (w : ι → ℝ) (A : ∀ i, Mat (ν i)) :
    tensor (fun i => w i • A i) = (∏ i, w i) • tensor A := by
proof · 5 lines
  ext x y
  simp only [tensor, Matrix.smul_apply, RCLike.real_smul_eq_coe_smul (K := ℂ), smul_eq_mul,
    Finset.prod_mul_distrib]
  change (∏ i, (w i : ℂ)) * _ = ((∏ i, w i : ℝ) : ℂ) * _
  rw [Complex.ofReal_prod]
4401
lemma tensor_sum_smul {κ : ι → Type*} [∀ i, Fintype (κ i)]
    (w : ∀ i, κ i → ℝ) (A : ∀ i, κ i → Mat (ν i)) :
    tensor (fun i => ∑ k, w i k • A i k) =
      ∑ f : ∀ i, κ i, (∏ i, w i (f i)) • tensor (fun i => A i (f i)) := by
proof · 2 lines
  rw [tensor_sum]
  simp only [tensor_smul]
4408
lemma tensor_mono (A B : ∀ i, Mat (ν i)) (hA : ∀ i, 0 ≤ A i) (hAB : ∀ i, A i ≤ B i) :
    tensor A ≤ tensor B := by
proof · 12 lines
  let M (i : ι) (b : Bool) : Mat (ν i) := if b then B i - A i else A i
  have he (i : ι) : (∑ b : Bool, M i b) = B i := by
    simp [M]
  have hm (i : ι) (b : Bool) : 0 ≤ M i b := by
    cases b
    · exact hA i
    · exact sub_nonneg.mpr (hAB i)
  rw [show B = (fun i => ∑ b : Bool, M i b) by funext i; exact (he i).symm, tensor_sum]
  have h := Finset.single_le_sum (s := (Finset.univ : Finset (ι → Bool)))
    (f := fun f => tensor (fun i => M i (f i)))
    (fun f _ => tensor_nonneg _ (fun i => hm i (f i))) (Finset.mem_univ (fun _ => false))
  exact h
4430
abbrev Fiber (j : J) := {i : τ // f i = j}
4432
def splitEquiv : (τ → n) ≃ (∀ j : J, Fiber f j → n) where
body · 8 lines
  toFun x j i := x i.val
  invFun y i := y (f i) ⟨i,rfl⟩
  left_inv x := rfl
  right_inv y := by
    funext j i
    rcases i with ⟨i,hi⟩
    subst j
    rfl
4443
def tensor (A : ∀ j, Mat (Fiber f j → n)) : Mat (τ → n) :=
body · 1 line
  fun x y => DependentTensor.tensor A (splitEquiv f x) (splitEquiv f y)
4446
lemma tensor_mul (A B : ∀ j, Mat (Fiber f j → n)) :
    tensor f (fun j => A j * B j) = tensor f A * tensor f B := by
proof · 3 lines
  change (DependentTensor.tensor (fun j => A j * B j)).submatrix (splitEquiv f) (splitEquiv f) = _
  rw [DependentTensor.tensor_mul]
  exact (Matrix.submatrix_mul_equiv _ _ (splitEquiv f) (splitEquiv f) (splitEquiv f)).symm
4452
lemma tensor_star
    {τ : Type*} {J : Type*} {n : Type*}
    [Fintype τ]
    [DecidableEq τ]
    [contextInstance4107_53 : Fintype J]
    [DecidableEq J]
    [Fintype n]
    [DecidableEq n]
    (f : τ → J) (A : ∀ j, Mat (Fiber f j → n)) :
    tensor f (fun j => star (A j)) = star (tensor f A) := by
proof · 2 lines
  ext x y
  simp [tensor, DependentTensor.tensor, Matrix.star_apply]
4465
lemma tensor_one : tensor f (fun j => (1 : Mat (Fiber f j → n))) = 1 := by
proof · 4 lines
  ext x y
  change DependentTensor.tensor (fun j => (1 : Mat (Fiber f j → n))) _ _ = _
  rw [DependentTensor.tensor_one]
  simp only [Matrix.one_apply, Equiv.apply_eq_iff_eq]
4471
lemma tensor_nonneg (A : ∀ j, Mat (Fiber f j → n)) (hA : ∀ j, 0 ≤ A j) : 0 ≤ tensor f A := by
proof · 1 line
  exact (Matrix.nonneg_iff_posSemidef.mp (DependentTensor.tensor_nonneg A hA)).submatrix (splitEquiv f) |>.nonneg
4474
lemma tensor_mono (A B : ∀ j, Mat (Fiber f j → n)) (hA : ∀ j, 0 ≤ A j) (hAB : ∀ j, A j ≤ B j) :
    tensor f A ≤ tensor f B := by
proof · 3 lines
  have he := DependentTensor.tensor_mono A B hA hAB
  have hn := (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr he)).submatrix (splitEquiv f) |>.nonneg
  exact sub_nonneg.mp hn
4480
lemma tensor_projection (P : ∀ j, Mat (Fiber f j → n)) (hP : ∀ j, IsStarProjection (P j)) :
    IsStarProjection (tensor f P) := by
proof · 11 lines
  constructor
  · change tensor f P * tensor f P = tensor f P
    rw [← tensor_mul]
    congr 1
    funext j
    exact (hP j).isIdempotentElem.eq
  · change star (tensor f P) = tensor f P
    rw [← tensor_star]
    congr 1
    funext j
    exact (hP j).isSelfAdjoint.star_eq
4494
lemma tensor_sum_smul {κ : J → Type*} [∀ j, Fintype (κ j)]
    (w : ∀ j, κ j → ℝ) (A : ∀ j, κ j → Mat (Fiber f j → n)) :
    tensor f (fun j => ∑ k, w j k • A j k) =
      ∑ k : ∀ j, κ j, (∏ j, w j (k j)) • tensor f (fun j => A j (k j)) := by
proof · 4 lines
  ext x y
  change DependentTensor.tensor (fun j => ∑ k, w j k • A j k) _ _ = _
  rw [DependentTensor.tensor_sum_smul]
  simp only [Matrix.sum_apply, Matrix.smul_apply, tensor]
4510
lemma prod_fibers
    {τ : Type*} {J : Type*}
    [contextInstance4180_25 : Fintype τ]
    [DecidableEq τ]
    [contextInstance4180_53 : Fintype J]
    [contextInstance4180_65 : DecidableEq J]
    (f : τ → J) {M : Type*} [CommMonoid M] (a : τ → M) :
    (∏ j : J, ∏ i : Fiber f j, a i.val) = ∏ i, a i := by
proof · 4 lines
  calc
    _ = ∏ z : Sigma (Fiber f), a z.2.val :=
      (Fintype.prod_sigma (fun z : Sigma (Fiber f) => a z.2.val)).symm
    _ = _ := (Equiv.sigmaFiberEquiv f).prod_comp a
4524
lemma tensor_cells
    {τ : Type*} {J : Type*} {n : Type*}
    [contextInstance4180_25 : Fintype τ]
    [contextInstance4180_37 : DecidableEq τ]
    [contextInstance4180_53 : Fintype J]
    [contextInstance4180_65 : DecidableEq J]
    [Fintype n]
    [DecidableEq n]
    (f : τ → J) (A : τ → Mat n) :
    tensor f (fun j => DependentTensor.tensor (fun i : Fiber f j => A i.val)) =
      DependentTensor.tensor A := by
proof · 3 lines
  ext x y
  change (∏ j, ∏ i : Fiber f j, A i.val (x i.val) (y i.val)) = _
  exact prod_fibers f (fun i => A i (x i) (y i))
4539
lemma tensor_commute (A B : ∀ j, Mat (Fiber f j → n))
    (h : ∀ j, Commute (A j) (B j)) : Commute (tensor f A) (tensor f B) := by
proof · 5 lines
  change tensor f A * tensor f B = tensor f B * tensor f A
  rw [← tensor_mul, ← tensor_mul]
  congr 1
  funext j
  exact (h j).eq
4554
lemma tensor_root_square (A : ι → Mat n) (hA : ∀ i, 0 ≤ A i) :
    tensor (fun i => root (A i)) * tensor (fun i => root (A i)) = tensor A := by
proof · 4 lines
  rw [← tensor_mul]
  congr 1
  funext i
  exact root_mul_self _ (hA i)
4561
lemma tensor_root (A : ι → Mat n) (hA : ∀ i, 0 ≤ A i) :
    tensor (fun i => root (A i)) = root (tensor A) := by
proof · 3 lines
  apply (CFC.mul_self_eq_mul_self_iff _ _
    (tensor_nonneg _ (fun i => root_nonneg (A i))) (root_nonneg _)).mp
  rw [tensor_root_square A hA, root_mul_self _ (tensor_nonneg A hA)]
4567
lemma tensor_invRoot_mul_root (A : ι → Mat n) (hA : ∀ i, 0 ≤ A i) :
    tensor (fun i => invRoot (A i)) * tensor (fun i => root (A i)) =
      tensor (fun i => support (A i)) := by
proof · 4 lines
  rw [← tensor_mul]
  congr 1
  funext i
  exact invRoot_mul_root _ (hA i)
4575
lemma tensor_root_mul_support_of_le_smul (A B : ι → Mat n)
    (hA : ∀ i, 0 ≤ A i) (hB : ∀ i, 0 ≤ B i)
    (c : ℝ) (hAB : ∀ i, A i ≤ c • B i) :
    tensor (fun i => root (A i)) * tensor (fun i => support (B i)) =
      tensor (fun i => root (A i)) := by
proof · 4 lines
  rw [← tensor_mul]
  congr 1
  funext i
  exact root_mul_support_of_le_smul _ _ (hB i) (hA i) c (hAB i)
4590
lemma sum_occupiedRow_card (W : Finset (R × C)) :
    ∑ i, (occupiedRow W i).card = W.card := by
proof · 8 lines
  have he (i : R) : (occupiedRow W i).card = ∑ j : C, if (i,j) ∈ W then 1 else 0 := by
    simp only [occupiedRow, Finset.card_eq_sum_ones, Finset.sum_filter]
  simp only [he]
  rw [← Finset.sum_product (f := fun x : R × C => if x ∈ W then (1 : ℕ) else 0)]
  simp only [Finset.univ_product_univ, ← Finset.sum_filter]
  have he : (Finset.univ.filter fun x : R × C => x ∈ W) = W := by ext x; simp
  rw [he]
  simp
4601
lemma sum_missingInRow (W : Finset (R × C)) :
    ∑ i, missingInRow W i = Fintype.card R * Fintype.card C - W.card := by
proof · 6 lines
  have he : (∑ i, missingInRow W i) + W.card = Fintype.card R * Fintype.card C := by
    rw [← sum_occupiedRow_card W, ← Finset.sum_add_distrib]
    have hi (i : R) : missingInRow W i + (occupiedRow W i).card = Fintype.card C :=
      Nat.sub_add_cancel (occupiedRow_card_le W i)
    simp [hi]
  omega
4610
lemma boardPhi_nonneg (W : Finset (R × C)) :
    0 ≤ missingCellProductFactor (Fintype.card C) (missingInRow W) :=
proof · 1 line
  Finset.prod_nonneg fun rowIndex _ => missingCellFactor_nonneg _ (missingInRow W rowIndex)
4614
lemma boardPhi_le_exp_holes (W : Finset (R × C)) :
    missingCellProductFactor (Fintype.card C) (missingInRow W) ≤
      Real.exp (Fintype.card R * Fintype.card C - W.card : ℕ) := by
proof · 5 lines
  have h := missingCellProduct (Fintype.card C) (missingInRow W) (missingInRow_le W)
  have hs : (∑ i, (missingInRow W i : ℝ)) =
      (Fintype.card R * Fintype.card C - W.card : ℕ) := by
    exact_mod_cast sum_missingInRow W
  rwa [hs] at h
4631
structure TypeLabel (q : ℕ) where
body · 4 lines
  even : YoungDiagram
  odd : YoungDiagram
  even_height : even.colLen 0 ≤ q
  odd_height : odd.colLen 0 ≤ q
4639
def size (D : TypeLabel q) : ℕ := Fintype.card (Cells D.even) + Fintype.card (Cells D.odd)
4640
def entropy (D : TypeLabel q) : ℝ := SignedCarrier.entropy D.even D.odd q
4641
def dimension (D : TypeLabel q) : ℕ := SignedCarrier.dimension D.even D.odd q D.even_height D.odd_height
4643
def projection (D : TypeLabel q) (τ : Type*) [Fintype τ] [DecidableEq τ] :
    Matrix (τ → SignedTensor.Spin q) (τ → SignedTensor.Spin q) ℂ :=
body · 1 line
  SignedCarrier.matrixProjection D.even D.odd q D.even_height D.odd_height
4647
lemma projection_isStarProjection (D : TypeLabel q) (τ : Type*) [Fintype τ] [DecidableEq τ] :
    IsStarProjection (D.projection τ) :=
proof · 1 line
  SignedCarrier.matrixProjection_isStarProjection D.even D.odd q D.even_height D.odd_height
4651
lemma dimension_pos (D : TypeLabel q) : 0 < D.dimension :=
proof · 1 line
  SignedCarrier.dimension_pos D.even D.odd q D.even_height D.odd_height
4654
lemma dimension_le (D : TypeLabel q) : D.dimension ≤ (D.size + 1) ^ (q*(q-1)) :=
proof · 1 line
  SignedCarrier.dimension_le D.even D.odd q D.even_height D.odd_height
4657
lemma entropy_eq_zero_of_size_zero (D : TypeLabel q) (hD : D.size = 0) : D.entropy = 0 := by
proof · 9 lines
  have hs : ∑ i, SignedCarrier.parts D.even D.odd q i = 0 :=
    (SignedCarrier.sum_parts D.even D.odd q D.even_height D.odd_height).trans hD
  have hi : ∀ i, SignedCarrier.parts D.even D.odd q i = 0 := by
    intro i
    have h := Finset.single_le_sum (s := Finset.univ)
      (fun j _ => Nat.zero_le (SignedCarrier.parts D.even D.odd q j)) (Finset.mem_univ i)
    rw [hs] at h
    exact Nat.eq_zero_of_le_zero h
  simp [entropy, SignedCarrier.entropy, EntropyMonomial.entropy, hi]
4669
abbrev EvenDensity (q : ℕ) := {A : SignedTensor.Mat q × SignedTensor.Mat q // SignedTensor.IsEvenDensity A}
4672
def matrix (A : EvenDensity q) : Matrix (SignedTensor.Spin q) (SignedTensor.Spin q) ℂ :=
body · 1 line
  SignedTensor.block A.val
4674
lemma nonneg (A : EvenDensity q) : 0 ≤ A.matrix :=
proof · 1 line
  (SignedTensor.block_posSemidef A.val A.property.1 A.property.2.1).nonneg
4676
lemma trace_eq_one (A : EvenDensity q) : A.matrix.trace = 1 := by
proof · 2 lines
  rw [matrix, SignedTensor.block_trace]
  exact A.property.2.2
4680
def toDensity (A : EvenDensity q) : MatrixAnalysis.Density (SignedTensor.Spin q) :=
body · 1 line
  ⟨A.matrix,A.nonneg,A.trace_eq_one⟩
4684
def mean (A : J → EvenDensity q) : EvenDensity q :=
body · 2 lines
  ⟨SignedTensor.evenAverage (fun j => (A j).val),
    SignedTensor.evenAverage_isEvenDensity _ (fun j => (A j).property)⟩
4687
lemma mean_matrix (A : J → EvenDensity q) [DecidableEq J] :
    (mean A).matrix = MatrixAnalysis.average (fun j => (A j).toDensity) := by
proof · 3 lines
  unfold mean matrix SignedTensor.evenAverage
  rw [SignedTensor.block_real_smul, SignedTensor.block_sum]
  rfl
4697
lemma density_upper (D : TypeLabel q) (A : EvenDensity q) :
    ‖TensorMatrix.tensor (fun _ : τ => A.matrix) * D.projection τ‖ ≤ Real.exp (-D.entropy) :=
proof · 1 line
  SignedCarrier.matrix_density_upper D.even D.odd q D.even_height D.odd_height A.val A.property
4701
lemma density_commute (D : TypeLabel q) (A : EvenDensity q) :
    Commute (TensorMatrix.tensor (fun _ : τ => A.matrix)) (D.projection τ) :=
proof · 1 line
  SignedCarrier.matrix_density_commute D.even D.odd q D.even_height D.odd_height A.val A.property
4706
lemma density_mixture (D : TypeLabel q) :
    ∃ (ι : Type) (_ : Fintype ι) (w : ι → ℝ) (R : ι → EvenDensity q),
      (∀ i, 0 ≤ w i) ∧ (∑ i, w i = 1) ∧
      D.projection τ ≤ (Real.exp D.entropy * (D.dimension : ℝ)) •
        ∑ i, w i • TensorMatrix.tensor (fun _ : τ => (R i).matrix) := by
proof · 4 lines
  obtain ⟨ι,hι,w,R,hw,hs,hR,hdom⟩ := SignedCarrier.exists_matrix_density_mixture
    (τ := τ) D.even D.odd q D.even_height D.odd_height
  let := hι
  exact ⟨ι,hι,w,fun i => ⟨R i,hR i⟩,hw,hs,hdom⟩
4723
abbrev Fiber (f : τ → J) (j : J) := BlockTensor.Fiber f j
4725
def projection (f : τ → J) (D : J → TypeLabel q) : Mat (τ → SignedTensor.Spin q) :=
body · 1 line
  BlockTensor.tensor f (fun j => (D j).projection (Fiber f j))
4728
def entropy (D : J → TypeLabel q) : ℝ := ∑ j, (D j).entropy
4729
def dimension (D : J → TypeLabel q) : ℕ := ∏ j, (D j).dimension
4731
lemma projection_isStarProjection (f : τ → J) (D : J → TypeLabel q) :
    IsStarProjection (projection f D) :=
proof · 1 line
  BlockTensor.tensor_projection f _ (fun j => (D j).projection_isStarProjection _)
4735
lemma density_commute (f : τ → J) (D : J → TypeLabel q) (A : EvenDensity q) :
    Commute (TensorMatrix.tensor (fun _ : τ => A.matrix)) (projection f D) := by
proof · 10 lines
  have h := BlockTensor.tensor_commute f
    (fun j => TensorMatrix.tensor (fun _ : Fiber f j => A.matrix))
    (fun j => (D j).projection (Fiber f j)) (fun j => (D j).density_commute A)
  have he : BlockTensor.tensor f (fun j => TensorMatrix.tensor (fun _ : Fiber f j => A.matrix)) =
      TensorMatrix.tensor (fun _ : τ => A.matrix) := by
    ext x y
    change (∏ j : J, ∏ t : Fiber f j, A.matrix (x t.val) (y t.val)) =
      ∏ t : τ, A.matrix (x t) (y t)
    exact BlockTensor.prod_fibers f (fun t => A.matrix (x t) (y t))
  rwa [he] at h
4751
theorem density_mixture (f : τ → J) (D : J → TypeLabel q) :
    ∃ (ι : Type u) (_ : Fintype ι) (w : ι → ℝ) (R : ι → J → EvenDensity q),
      (∀ i, 0 ≤ w i) ∧ (∑ i, w i = Real.exp (entropy D) * (dimension D : ℝ)) ∧
      projection f D ≤ ∑ i, w i • TensorMatrix.tensor (fun t : τ => (R i (f t)).matrix) := by
proof · 36 lines
  classical
  choose κ hκ w R hw hs hdom using (fun j : J => (D j).density_mixture (τ := Fiber f j))
  let : ∀ j, Fintype (κ j) := hκ
  let c (j : J) : ℝ := Real.exp (D j).entropy * (D j).dimension
  let z (j : J) (i : κ j) : ℝ := c j * w j i
  have hz (j : J) (i : κ j) : 0 ≤ z j i :=
    mul_nonneg (mul_nonneg (Real.exp_pos _).le (Nat.cast_nonneg _)) (hw j i)
  have hsum (j : J) : ∑ i, z j i = c j := by
    simp only [z, ← Finset.mul_sum, hs, mul_one]
  have hlocal (j : J) : (D j).projection (Fiber f j) ≤
      ∑ i, z j i • TensorMatrix.tensor (fun _ : Fiber f j => (R j i).matrix) := by
    simpa only [z, c, Finset.smul_sum, smul_smul] using hdom j
  refine ⟨(∀ j, κ j), inferInstance, (fun i => ∏ j, z j (i j)),
    (fun i j => R j (i j)), (fun i => Finset.prod_nonneg fun j _ => hz j (i j)), ?_, ?_⟩
  · rw [← Fintype.prod_sum]
    simp only [hsum, c, Finset.prod_mul_distrib]
    rw [← Real.exp_sum]
    simp only [entropy, dimension, Nat.cast_prod]
  · have h := BlockTensor.tensor_mono f _ _
      (fun j => ((D j).projection_isStarProjection (Fiber f j)).nonneg) hlocal
    rw [BlockTensor.tensor_sum_smul] at h
    have he (i : ∀ j, κ j) : BlockTensor.tensor f
        (fun j => TensorMatrix.tensor (fun _ : Fiber f j => (R j (i j)).matrix)) =
        TensorMatrix.tensor (fun t : τ => (R (f t) (i (f t))).matrix) := by
      ext x y
      change (∏ j : J, ∏ t : Fiber f j, (R j (i j)).matrix (x t.val) (y t.val)) =
        ∏ t : τ, (R (f t) (i (f t))).matrix (x t) (y t)
      calc
        _ = ∏ j : J, ∏ t : Fiber f j, (R (f t.val) (i (f t.val))).matrix (x t.val) (y t.val) := by
          apply Finset.prod_congr rfl
          intro j hj
          apply Finset.prod_congr rfl
          intro t ht
          rw [t.property]
        _ = _ := BlockTensor.prod_fibers f (fun t => (R (f t) (i (f t))).matrix (x t) (y t))
    simpa only [he, projection] using h