From 84ffe5e0d424189c77cc77b89ff05e26887af8c8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:07 +0000 Subject: [PATCH 001/242] feat(Tensors): Variance and Color, with Variance.dual and Color.dual involutive by rfl Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Color.lean | 120 +++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Color.lean diff --git a/Physlib.lean b/Physlib.lean index 756a446e30..8c21af5afd 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -195,6 +195,7 @@ public import Physlib.Mathematics.Modules.CrossProduct public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite +public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.VariationalCalculus.Basic public import Physlib.Mathematics.VariationalCalculus.HasVarAdjDeriv public import Physlib.Mathematics.VariationalCalculus.HasVarAdjoint diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean new file mode 100644 index 0000000000..18bdb00bba --- /dev/null +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -0,0 +1,120 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.Init +/-! + +# Colors of tensor slots + +A color is a kind of index with a variance, up or down. Its dual has the same kind and the other +variance. + +## i. Overview + +The color of a tensor slot says which representation the slot carries. It is unrelated to the +color charge of QCD. A color `⟨κ, v⟩ : Color K` pairs a kind of index `κ : K` with a variance `v`, +`up` or `down`. For Lorentz tensors there is one kind, `vec`. The index of `v^μ` has color +`⟨vec, up⟩` and the index of `w_μ` has color `⟨vec, down⟩`. A color is a label, not a space. A +species (`Species.lean`) assigns a space to each color, `V ⟨vec, up⟩ = ℝ⁴` and +`V ⟨vec, down⟩ = (ℝ⁴)*`. + +The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, with `up.dual = down` and `down.dual = up`. A contraction +such as `v^μ w_μ` pairs a slot of color `c` with a slot of color `c.dual`. + +A variance stores two booleans, whether it is up and whether it is down, and `dual` swaps them. +So `v.dual.dual = v` and `c.dual.dual = c` hold by `rfl` for a variable `v` or `c`, and a +statement that takes a dual twice needs no cast. With an inductive `up | down` the same equation +needs a case split on `v`. + +## ii. Key results + +- `Variance` : the variance of an index, with `Variance.up`, `Variance.down` and `Variance.dual`. +- `Color` : a kind of index and a variance. +- `Color.dual` : the dual color, with `Color.dual_dual : c.dual.dual = c` by `rfl`. + +## iii. Table of contents + +- A. Up and down +- B. Colors + +## iv. References + +-/ + +@[expose] public section + +namespace Tensors + +/-! + +## A. Up and down + +-/ + +/-- The variance of an index, up (contravariant, `v^μ`) or down (covariant, `w_μ`). It stores +whether the index is up and whether it is down, so that `dual` swaps the two fields and +`v.dual.dual = v` holds by `rfl`. A match on both fields has two impossible cases, closed by +`ne`. -/ +structure Variance where + /-- Whether the index is up. -/ + isUp : Bool + /-- Whether the index is down. -/ + isDown : Bool + /-- An index is either up or down. -/ + ne : isUp ≠ isDown +deriving DecidableEq + +namespace Variance + +/-- An upper index, as in `v^μ`. -/ +def up : Variance := ⟨true, false, by decide⟩ + +/-- A lower index, as in `w_μ`. -/ +def down : Variance := ⟨false, true, by decide⟩ + +/-- The dual variance, which exchanges up and down. It is an `abbrev` because rewriting with the +species laws needs `c.dual.dual` and `c` to agree when only reducible definitions unfold. -/ +abbrev dual (v : Variance) : Variance := ⟨v.isDown, v.isUp, v.ne.symm⟩ + +@[simp] +lemma dual_up : up.dual = down := rfl + +@[simp] +lemma dual_down : down.dual = up := rfl + +/-- Taking the dual twice gives back the variance, by `rfl`. -/ +lemma dual_dual (v : Variance) : v.dual.dual = v := rfl + +end Variance + +/-! + +## B. Colors + +-/ + +/-- A color `⟨κ, v⟩`: a kind of index `κ : K` and its variance `v`. -/ +structure Color (K : Type) where + /-- The kind of index, such as the Lorentz vector index `vec` or a left-handed Weyl index. -/ + kind : K + /-- Whether the index is up or down. -/ + variance : Variance +deriving DecidableEq + +/-- The dual color: the same kind, the other variance. -/ +abbrev Color.dual {K : Type} (c : Color K) : Color K := ⟨c.kind, c.variance.dual⟩ + +/-- The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, so that `simp` rewrites the dual of `⟨κ, up⟩` to +`⟨κ, down⟩`. -/ +@[simp] +lemma Color.dual_mk {K : Type} (κ : K) (v : Variance) : + (⟨κ, v⟩ : Color K).dual = ⟨κ, v.dual⟩ := rfl + +/-- Taking the dual twice gives back the color, by `rfl`. -/ +lemma Color.dual_dual {K : Type} (c : Color K) : c.dual.dual = c := rfl + +end Tensors From c13cb89136418ed05c80e55a6769e1190297af51 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:09 +0000 Subject: [PATCH 002/242] feat(ForMathlib): pairingToLinearMap, a tensor read as a linear map through a pairing, with pairingToLinearMap_apply and lTensor_pairingToLinearMap_apply Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../Mathematics/ForMathlib/Contraction.lean | 108 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 Physlib/Mathematics/ForMathlib/Contraction.lean diff --git a/Physlib.lean b/Physlib.lean index 8c21af5afd..bfd11f3050 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -156,6 +156,7 @@ public import Physlib.Mathematics.Calculus.Wirtinger.Hessian public import Physlib.Mathematics.Distribution.PowMul public import Physlib.Mathematics.Distribution.TemperedDistribution public import Physlib.Mathematics.ForMathlib.ComplexLinear +public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.ForMathlib.DataStructures.Matrix.LieTrace public import Physlib.Mathematics.ForMathlib.FDerivCurry public import Physlib.Mathematics.ForMathlib.Fin diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean new file mode 100644 index 0000000000..1fa7861bea --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -0,0 +1,108 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.Contraction + +/-! + +# A tensor read as a linear map through a pairing + +A pairing `p : V ⊗ W → R` reads a tensor `z` in `W ⊗ U` as a linear map from `V` to `U`. + +## i. Overview + +Here `V`, `W` and `U` are spaces, and `x`, `w`, `y` and `z` are elements. The pairing `p` contracts +an element `x` of `V` with an element `w` of `W` to the scalar `p (x ⊗ w)`. A tensor `z` in `W ⊗ U` +reads as the linear map from `V` to `U` that contracts its input with the first factor of `z` and +keeps the second. On a pure tensor this is `x ↦ p (x ⊗ w) • y` for `z = w ⊗ y`. + +For Lorentz tensors take `V = ℝ⁴` the vectors `x^μ`, `W = (ℝ⁴)*` the covectors `w_μ`, and +`p (x ⊗ w) = x^μ w_μ`. In the standard basis of `ℝ⁴` and its dual basis, a tensor `z` in `W ⊗ U` +has components `z_μ^A`, where `A` labels a basis of `U`, and reads as the map `x^μ ↦ x^μ z_μ^A`. + +- With `U = V` and `z` the Kronecker delta `δ_μ^ν`, the map is `x^μ ↦ x^μ δ_μ^ν = x^ν`, the + identity. +- With `U = W` and `z` the Minkowski metric `η_μν`, the map is `x^μ ↦ x^μ η_μν = x_ν`, lowering + the index. + +## ii. Key results + +- `TensorProduct.pairingToLinearMap` : a tensor in `W ⊗ U` as a linear map `V → U` through `p`. +- `TensorProduct.pairingToLinearMap_tmul` : its value on a pure tensor `w ⊗ y`. +- `TensorProduct.pairingToLinearMap_lTensor` : a linear map applied to the second factor of `z` is + applied to the values. +- `TensorProduct.pairingToLinearMap_apply` : the value at `x` as `x ⊗ z` with its first two factors + contracted. +- `TensorProduct.lTensor_pairingToLinearMap_apply` : the map of `N` on the second factor of `M` as + `M ⊗ N` with its middle pair contracted. + +## iii. Table of contents + +- A. Reading a tensor as a linear map + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +namespace TensorProduct + +variable {R V W U U' : Type*} [CommSemiring R] [AddCommMonoid V] [Module R V] [AddCommMonoid W] + [Module R W] [AddCommMonoid U] [Module R U] [AddCommMonoid U'] [Module R U'] + +/-! + +## A. Reading a tensor as a linear map + +-/ + +/-- The tensor `z` in `W ⊗ U` read through the pairing `p` as a linear map from `V` to `U`. The +pairing takes the input on the left, `p (x ⊗ w)`, and contracts it with the first factor of `z`. +It is Mathlib's `dualTensorHom R V U` with each `w` read as the form +`x ↦ p (x ⊗ w)`. -/ +noncomputable def pairingToLinearMap (p : V ⊗[R] W →ₗ[R] R) : W ⊗[R] U →ₗ[R] V →ₗ[R] U := + dualTensorHom R V U ∘ₗ (TensorProduct.curry p).flip.rTensor U + +/-- On a pure tensor, `(w ⊗ y)(x) = p (x ⊗ w) • y`. -/ +@[simp] +lemma pairingToLinearMap_tmul (p : V ⊗[R] W →ₗ[R] R) (w : W) (y : U) (x : V) : + pairingToLinearMap p (w ⊗ₜ y) x = p (x ⊗ₜ w) • y := rfl + +/-- Applying a linear map `f : U → U'` to the second factor of `z` composes the map of `z` with +`f`. -/ +lemma pairingToLinearMap_lTensor (p : V ⊗[R] W →ₗ[R] R) (f : U →ₗ[R] U') (z : W ⊗[R] U) : + pairingToLinearMap p (f.lTensor W z) = f ∘ₗ pairingToLinearMap p z := by + induction z using TensorProduct.inductionOn with + | tmul w y => ext x; simp + | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] + +/-- The value at `x` is `x ⊗ z` with its first two factors contracted by `p`, +`lid (p.rTensor U (assoc.symm (x ⊗ z)))`. This is the form of a contraction built from Mathlib's +associator `TensorProduct.assoc` and unitor `TensorProduct.lid`. -/ +lemma pairingToLinearMap_apply (p : V ⊗[R] W →ₗ[R] R) (z : W ⊗[R] U) (x : V) : + pairingToLinearMap p z x = + TensorProduct.lid R U (p.rTensor U ((TensorProduct.assoc R V W U).symm (x ⊗ₜ z))) := by + induction z using TensorProduct.inductionOn with + | tmul w y => rfl + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] + +/-- Applying the map of `N` to the second factor of `M ∈ U' ⊗ V` is `M ⊗ N` with its middle pair +contracted by `p`, as in `M^{aμ} N_μ^b`. The right side is that contraction built from Mathlib's +associator `TensorProduct.assoc` and unitor `TensorProduct.lid`, as in +`pairingToLinearMap_apply`. -/ +lemma lTensor_pairingToLinearMap_apply (p : V ⊗[R] W →ₗ[R] R) (M : U' ⊗[R] V) (N : W ⊗[R] U) : + (pairingToLinearMap p N).lTensor U' M = (TensorProduct.lid R U).lTensor U' + ((p.rTensor U).lTensor U' ((TensorProduct.assoc R V W U).symm.toLinearMap.lTensor U' + (TensorProduct.assoc R U' V (W ⊗[R] U) (M ⊗ₜ N)))) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] + | tmul u v => simp [pairingToLinearMap_apply] + +end TensorProduct From 08b3917e586a67826854fddfec3059ea71cbc1ff Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:12 +0000 Subject: [PATCH 003/242] feat(Tensors): Species, with delta_eq_comm_delta_dual, delta_invariant and pairingToLinearMap_contr_injective, its tensors, group action, map_smul_of_pure, tensorBasis and contr_tmul_eq_sum_basis Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Species.lean | 329 +++++++++++++++++++++++ 2 files changed, 330 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Species.lean diff --git a/Physlib.lean b/Physlib.lean index bfd11f3050..8a10371c34 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -197,6 +197,7 @@ public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color +public import Physlib.Mathematics.Tensors.Species public import Physlib.Mathematics.VariationalCalculus.Basic public import Physlib.Mathematics.VariationalCalculus.HasVarAdjDeriv public import Physlib.Mathematics.VariationalCalculus.HasVarAdjoint diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean new file mode 100644 index 0000000000..f6d989872f --- /dev/null +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -0,0 +1,329 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.ForMathlib.Contraction +public import Physlib.Mathematics.Tensors.Color +public import Mathlib.RepresentationTheory.Intertwining +public import Mathlib.LinearAlgebra.PiTensorProduct.Basis +public import Mathlib.LinearAlgebra.Basis.Bilinear +/-! + +# Tensor species and their tensors + +A species assigns to each color a representation with a basis, the contraction with the dual +color and the Kronecker delta. Its tensors with colors `c : ι → Color K` form Mathlib's +`⨂[k] i, V (c i)`, for any slot type `ι`. + +## i. Overview + +A species `S : Species k G K basisIdx` assigns to each color `c` a `k`-module `S.V c`, a +representation `S.rep c` of the group `G` on it, and a basis `S.basis c` labelled by +`basisIdx c.kind`. It also gives the invariant contraction `S.contr c : V c ⊗ V c.dual → k`, which +pairs an index with its dual as in `u^μ α_μ`, and the Kronecker delta `S.delta c ∈ V c.dual ⊗ V c`, +written `δ_μ^ν`. Two laws relate them. The contraction is symmetric, `u^μ α_μ = α_μ u^μ` +(`contr_tmul_symm`). The delta acts as the identity, `u^μ δ_μ^ν = u^ν` (`snake`). It follows that +the delta is invariant and that the delta of `c.dual` is the delta of `c` with its factors +swapped. Since `c.dual.dual = c` holds by `rfl`, no statement needs a cast. + +Indices name slots, as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)` in any +basis. Components are taken with `tensorBasis`. The bases `S.basis c` and `S.basis c.dual` are not +related by the laws. The components of `S.delta c` form the identity matrix, and +`S.contr c (e_μ ⊗ e^ν)` is `1` for `μ = ν` and `0` otherwise, only when the two bases are dual. A +species does not require this, and a concrete species can prove it. + +The basis labels `basisIdx κ` depend only on the kind `κ` of a color, not on its variance, so an +index and its dual have the same labels by definition, as `μ` in `v^μ` and `α_μ`. They are a +parameter, not a field, so that a conjugate species can give a kind and its conjugate the same +labels by definition. A species is a value, so a new species can +be built from an old one, for example by restricting the group. + +`S.Tensor c` is a space and `S.pure f` an element of it. The space is `⨂[k] i, V (c i)`. The pure +tensor of a family `f` of vectors, one per slot, is `⨂ₜ[k] i, f i`, such as `u ⊗ α ⊗ β`. Every +tensor is a sum of pure tensors. A group element `Λ` acts on every slot by the representation of +its color. The components `T^μ_ν` of a tensor `T` are its coordinates in the basis +`e_μ ⊗ e^ν ⊗ …` of Mathlib's `Basis.piTensorProduct`. + +For example, Lorentz tensors have one kind `vec`. An upper index has color `⟨vec, up⟩` with +`V ⟨vec, up⟩ = ℝ⁴` and basis `e_μ`. A lower index has color `⟨vec, down⟩` with +`V ⟨vec, down⟩ = (ℝ⁴)*` and basis `e^μ`. The colors `![⟨vec, up⟩, ⟨vec, down⟩, ⟨vec, down⟩]` give +the space `ℝ⁴ ⊗ (ℝ⁴)* ⊗ (ℝ⁴)*` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. The pure tensor +`u ⊗ α ⊗ β` has components `u^μ α_ν β_ρ`. A Lorentz transformation `Λ` acts as +`(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ`. Lower indices transform with `Λ⁻¹`, which keeps +`u^μ α_μ` invariant. + +## ii. Key results + +- `Species` : the data and laws of a tensor species. +- `Species.contr_invariant`, `Species.delta_invariant` : `u^μ α_μ` and `δ_μ^ν` are invariant. +- `Species.delta_eq_comm_delta_dual` : the delta of `c` is the delta of `c.dual`, swapped. +- `Species.pairingToLinearMap_contr_injective` : a tensor in `V c.dual ⊗ U` is determined by the + linear map `V c → U` it defines through the contraction. +- `Species.Tensor`, `Species.pure`, `Species.inductionOn_pure` : tensors, pure tensors, and + induction through pure tensors. +- `Species.smul_pure` : the group acts on a pure tensor slot by slot. +- `Species.map_smul_of_pure` : a map of tensors that commutes with the group on pure tensors + commutes with it on all tensors. +- `Species.tensorBasis`, `Species.tensorBasis_repr_pure` : the basis of tensors, and the + components of a pure tensor. +- `Species.contr_tmul_eq_sum_basis` : the contraction of two vectors in components. + +## iii. Table of contents + +- A. Species +- B. Invariance of the contraction, invariance and symmetry of the delta +- C. Tensors +- D. The group action +- E. The basis of tensors + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +/-! + +## A. Species + +-/ + +/-- A tensor species: for each color a representation with a basis, the contraction `u^μ α_μ` +with the dual color, and the Kronecker delta `δ_μ^ν`. The basis labels `basisIdx κ` depend only on +the kind, so a color and its dual share them. They are a parameter so that a conjugate species can +give a kind and its conjugate the same labels by definition. -/ +structure Species (k : Type) [CommRing k] (G : Type) [Group G] (K : Type) + (basisIdx : K → Type) [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] where + /-- The vector space of a color. -/ + V : Color K → Type + /-- The vector spaces are additive groups. -/ + [addCommGroup : ∀ c, AddCommGroup (V c)] + /-- The vector spaces are modules over `k`. -/ + [module : ∀ c, Module k (V c)] + /-- The representation of a color. -/ + rep : (c : Color K) → Representation k G (V c) + /-- The basis of a color. -/ + basis : (c : Color K) → Module.Basis (basisIdx c.kind) k (V c) + /-- The invariant contraction `V c ⊗ V c.dual → k`, `u ⊗ α ↦ u^μ α_μ`. The vector of color `c` + is the first factor. -/ + contr : (c : Color K) → + ((rep c).tprod (rep c.dual)).IntertwiningMap (Representation.trivial k G k) + /-- The Kronecker delta `δ_μ^ν ∈ V c.dual ⊗ V c`. The factor of the dual color is first. -/ + delta : (c : Color K) → V c.dual ⊗[k] V c + /-- The contraction is symmetric, `u^μ α_μ = α_μ u^μ`. -/ + contr_tmul_symm : ∀ c (x : V c) (y : V c.dual), contr c (x ⊗ₜ[k] y) = contr c.dual (y ⊗ₜ[k] x) + /-- The snake law `u^μ δ_μ^ν = u^ν`: the delta, read as a linear map through the contraction + (`pairingToLinearMap`), is the identity. The vector `u` is contracted with the first factor of + the delta. -/ + snake : ∀ c, pairingToLinearMap (contr c).toLinearMap (delta c) = LinearMap.id + +attribute [instance] Species.addCommGroup Species.module + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + +/-! + +## B. Invariance of the contraction, invariance and symmetry of the delta + +-/ + +/-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ +lemma contr_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) (x : S.V c) + (y : S.V c.dual) : S.contr c (S.rep c g x ⊗ₜ[k] S.rep c.dual g y) = S.contr c (x ⊗ₜ[k] y) := by + change S.contr c ((S.rep c).tprod (S.rep c.dual) g (x ⊗ₜ[k] y)) = _ + rw [(S.contr c).isIntertwining] + simp + +/-- A tensor `z ∈ V c.dual ⊗ U` read through the contraction as a linear map `x ↦ z x` from +`V c` to `U` satisfies `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and linear map `f` on `U`. +For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ +lemma pairingToLinearMap_contr_map_rep (S : Species k G K basisIdx) (c : Color K) (g : G) + {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') + (z : S.V c.dual ⊗[k] U) (x : S.V c) : + pairingToLinearMap (S.contr c).toLinearMap (TensorProduct.map (S.rep c.dual g) f z) + (S.rep c g x) = f (pairingToLinearMap (S.contr c).toLinearMap z x) := by + induction z using TensorProduct.inductionOn with + | tmul w y => + simp only [TensorProduct.map_tmul, pairingToLinearMap_tmul, map_smul, + Representation.IntertwiningMap.toLinearMap_apply, S.contr_invariant] + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] + +/-- A tensor `z ∈ V c.dual ⊗ U` is recovered from its linear map `x ↦ z x` by applying the map +to the `V c` factor of the delta of `c.dual` with its factors swapped. In bases of `c` and `c.dual` +that are dual, this reads `z = e^μ ⊗ z(e_μ)`. -/ +lemma lTensor_pairingToLinearMap_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) + {U : Type} [AddCommGroup U] [Module k U] (z : S.V c.dual ⊗[k] U) : + (pairingToLinearMap (S.contr c).toLinearMap z).lTensor (S.V c.dual) + (TensorProduct.comm k _ _ (S.delta c.dual)) = z := by + induction z using TensorProduct.inductionOn with + | add z₁ z₂ h₁ h₂ => simp only [map_add, LinearMap.lTensor_add, LinearMap.add_apply, h₁, h₂] + | tmul w y => + -- On `w ⊗ y` this is the snake law at `c.dual`, applied to `w`, with `contr_tmul_symm`. + conv_rhs => rw [← LinearMap.id_apply (R := k) w, ← S.snake c.dual] + induction S.delta c.dual using TensorProduct.inductionOn with + | add D₁ D₂ h₁ h₂ => simp only [map_add, LinearMap.add_apply, add_tmul, h₁, h₂] + | tmul e₁ e₂ => + simp only [TensorProduct.comm_tmul, LinearMap.lTensor_tmul, pairingToLinearMap_tmul, + Representation.IntertwiningMap.toLinearMap_apply, tmul_smul, smul_tmul'] + rw [S.contr_tmul_symm c e₁ w] + +/-- A tensor `z ∈ V c.dual ⊗ U` is determined by the linear map `x ↦ z x` from `V c` to `U` it +defines through the contraction. In particular `δ_μ^ν` is the only tensor acting as the +identity. -/ +lemma pairingToLinearMap_contr_injective (S : Species k G K basisIdx) (c : Color K) {U : Type} + [AddCommGroup U] [Module k U] : + Function.Injective (pairingToLinearMap (S.contr c).toLinearMap : + S.V c.dual ⊗[k] U →ₗ[k] S.V c →ₗ[k] U) := fun z z' h => by + rw [← S.lTensor_pairingToLinearMap_comm_delta_dual c z, + ← S.lTensor_pairingToLinearMap_comm_delta_dual c z', h] + +/-- The delta of `c` is the delta of `c.dual ∈ V c ⊗ V c.dual` with its factors swapped. -/ +lemma delta_eq_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) : + S.delta c = TensorProduct.comm k _ _ (S.delta c.dual) := by + simpa [S.snake c] using (S.lTensor_pairingToLinearMap_comm_delta_dual c (S.delta c)).symm + +/-- The delta is invariant, `Λ δ = δ`. -/ +lemma delta_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) : + TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by + -- Both act as the identity: `(Λδ)(Λx) = Λx` by the snake law. + apply S.pairingToLinearMap_contr_injective c + ext x + conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g x] + rw [S.pairingToLinearMap_contr_map_rep, S.snake c, LinearMap.id_apply, LinearMap.id_apply, + Representation.self_inv_apply] + +/-! + +## C. Tensors + +-/ + +/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the slots `ι`. It is a `def`, not +an `abbrev`, so that the type remembers `S` and instances on `⨂` from elsewhere do not apply. -/ +def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := + ⨂[k] i, S.V (c i) + +noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : + AddCommGroup (S.Tensor c) := + inferInstanceAs (AddCommGroup (⨂[k] i, S.V (c i))) + +noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : + Module k (S.Tensor c) := + inferInstanceAs (Module k (⨂[k] i, S.V (c i))) + +variable {ι : Type} {c : ι → Color K} + +/-- The identity from `S.Tensor c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. +Mathlib's constructions on `⨂` are composed with it. -/ +noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : + S.Tensor c ≃ₗ[k] ⨂[k] i, S.V (c i) := + LinearEquiv.refl k _ + +/-- The pure tensor of a family of vectors, one per slot, such as `u ⊗ α ⊗ β` with components +`u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ +noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : S.Tensor c := + (S.toPiTensor c).symm (⨂ₜ[k] i, f i) + +variable {S : Species k G K basisIdx} + +@[simp] +lemma toPiTensor_pure (f : Π i, S.V (c i)) : S.toPiTensor c (S.pure f) = ⨂ₜ[k] i, f i := rfl + +@[simp] +lemma toPiTensor_symm_tprod (f : Π i, S.V (c i)) : + (S.toPiTensor c).symm (⨂ₜ[k] i, f i) = S.pure f := rfl + +/-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ +lemma inductionOn_pure {P : S.Tensor c → Prop} + (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) + (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : S.Tensor c) : P t := + PiTensorProduct.induction_on (motive := P) t (fun r f => hsmul r _ (h f)) hadd + +/-! + +## D. The group action + +-/ + +/-- The group acts on every slot by the representation of its color, +`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`, through Mathlib's `PiTensorProduct.mapMonoidHom`. -/ +noncomputable instance : DistribMulAction G (S.Tensor c) := + DistribMulAction.compHom (M := Module.End k (S.Tensor c)) _ + (PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi fun i => S.rep (c i))) + +@[simp] +lemma smul_pure (g : G) (f : Π i, S.V (c i)) : + g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := + PiTensorProduct.map_tprod _ _ + +noncomputable instance : SMulCommClass G k (S.Tensor c) where + smul_comm g r t := by + change PiTensorProduct.map (fun i => S.rep (c i) g) (r • t) = r • PiTensorProduct.map _ t + exact map_smul _ r t + +noncomputable instance : SMulCommClass k G (S.Tensor c) := .symm _ _ _ + +/-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes +with `Λ` on all tensors. -/ +lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} + [FunLike F (S.Tensor c) (S.Tensor c')] [SemilinearMapClass F σ (S.Tensor c) (S.Tensor c')] + (f : F) (g : G) (h : ∀ p : Π i, S.V (c i), f (g • S.pure p) = g • f (S.pure p)) + (t : S.Tensor c) : f (g • t) = g • f t := by + induction t using inductionOn_pure with + | h p => exact h p + | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] + | hadd x y hx hy => rw [smul_add, map_add, hx, hy, map_add, smul_add] + +/-- Moving a vector along an equality of colors `a = a'` commutes with the group action. -/ +lemma cast_rep {a a' : Color K} (p : a = a') (g : G) (x : S.V a) : + LinearEquiv.cast (R := k) (M := S.V) p (S.rep a g x) = + S.rep a' g (LinearEquiv.cast (R := k) (M := S.V) p x) := by + subst p + rfl + +/-! + +## E. The basis of tensors + +-/ + +/-- The basis `e_μ ⊗ e^ν ⊗ …` of tensors, Mathlib's `Basis.piTensorProduct` of the bases of the +colors. -/ +noncomputable def tensorBasis (S : Species k G K basisIdx) [Fintype ι] (c : ι → Color K) : + Module.Basis (Π i, basisIdx (c i).kind) k (S.Tensor c) := + (Basis.piTensorProduct fun i => S.basis (c i)).map (S.toPiTensor c).symm + +/-- A basis tensor is the pure tensor of basis vectors, `e_μ ⊗ e^ν ⊗ …`. -/ +lemma tensorBasis_apply [Fintype ι] (x : Π i, basisIdx (c i).kind) : + S.tensorBasis c x = S.pure fun i => S.basis (c i) (x i) := by + simp [tensorBasis, Basis.piTensorProduct_apply] + +/-- The components of a pure tensor are products, `(u ⊗ α)^μ_ν = u^μ α_ν`. -/ +lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) + (x : Π i, basisIdx (c i).kind) : + (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by + simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] + +/-- The contraction in components, `∑ μ ν, u^μ α_ν C_μ^ν`, where `C_μ^ν = S.contr c (e_μ ⊗ e^ν)`. +When the bases of `c` and `c.dual` are dual, `C_μ^ν` is `1` for `μ = ν` and `0` otherwise, and +the sum has one index. A species does not require this. -/ +lemma contr_tmul_eq_sum_basis (c : Color K) (x : S.V c) (y : S.V c.dual) : + S.contr c (x ⊗ₜ y) = + ∑ μ, ∑ ν, (S.basis c).repr x μ * (S.basis c.dual).repr y ν * + S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) := by + simpa [Finsupp.sum_fintype, mul_assoc] using (LinearMap.sum_repr_mul_repr_mul (S.basis c) + (S.basis c.dual) (B := TensorProduct.curry (S.contr c).toLinearMap) x y).symm + +end Species + +end Tensors From bdf7da0a3e204d2449584959a7fd66583dc80bc8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:14 +0000 Subject: [PATCH 004/242] feat(Tensors): ColorEquiv and reindex, with reindex_reindex, reindex_smul and tensorBasis_repr_reindex Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Reindexing.lean | 189 ++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Reindexing.lean diff --git a/Physlib.lean b/Physlib.lean index 8a10371c34..e10588a6e2 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -197,6 +197,7 @@ public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color +public import Physlib.Mathematics.Tensors.Reindexing public import Physlib.Mathematics.Tensors.Species public import Physlib.Mathematics.VariationalCalculus.Basic public import Physlib.Mathematics.VariationalCalculus.HasVarAdjDeriv diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean new file mode 100644 index 0000000000..4d84c94e7c --- /dev/null +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -0,0 +1,189 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Species +/-! + +# Relabeling the slots of a tensor + +A bijection of the slots of a tensor that keeps their colors, and its action on tensors, as in the +transpose `T^μ_ν ↦ T_ν^μ`. + +## i. Overview + +A tensor of a species `S` with colors `c₁ : ι₁ → Color K` has one slot for each label `i : ι₁`. +A relabeling `σ : ColorEquiv c₁ c₂` from colors `c₁` on `ι₁` to colors `c₂` on `ι₂` is a bijection +`σ.toEquiv : ι₁ ≃ ι₂` together with the proof `σ.color_eq j : c₁ (σ.toEquiv.symm j) = c₂ j` that +every new slot `j` has the color of the old slot it comes from. The proof is stated at the new +labels, so that `c₂ j` reduces for concrete colors. For these the default proof of the structure +finds it, by `rfl` or by `rfl` on each slot, and rejects a relabeling that changes a color. + +`S.reindex σ : S.Tensor c₁ ≃ₗ[k] S.Tensor c₂` is a linear equivalence between two spaces of +tensors. The new slot `j` of `S.reindex σ T` holds the vector of the old slot `σ.toEquiv.symm j`, +moved along `σ.color_eq j` from `S.V (c₁ (σ.toEquiv.symm j))` to `S.V (c₂ j)`. The move changes +the type and no data. The components move with the slots (`tensorBasis_repr_reindex`). + +For example, a Lorentz upper index has the color `⟨vec, up⟩` with `V ⟨vec, up⟩ = ℝ⁴` and basis +`e_μ`, and a lower index has the color `⟨vec, down⟩` with `V ⟨vec, down⟩ = (ℝ⁴)*` and basis `e^μ`. +The colors `c₁ = ![⟨vec, up⟩, ⟨vec, down⟩]` on `Fin 2` give the space `ℝ⁴ ⊗ (ℝ⁴)*`, whose elements +are `T = T^μ_ν e_μ ⊗ e^ν`. The transpose relabels the slots by `Equiv.swap 0 1`, written +`.mk (Equiv.swap 0 1) : ColorEquiv c₁ ![⟨vec, down⟩, ⟨vec, up⟩]`. It sends `T` to its transpose +`Tᵀ = T^μ_ν e^ν ⊗ e_μ` in `(ℝ⁴)* ⊗ ℝ⁴`, whose components are `(Tᵀ)_ν^μ = T^μ_ν`. + +## ii. Key results + +- `ColorEquiv` : a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. +- `Species.reindex`, `Species.reindex_pure` : a relabeling applied to tensors, and to a pure + tensor. +- `Species.reindex_reindex` : two relabelings in sequence are their composite. +- `Species.reindex_smul` : relabeling commutes with the group action. +- `Species.tensorBasis_repr_reindex` : the components of a relabeled tensor, `(Tᵀ)_ν^μ = T^μ_ν`. + +## iii. Table of contents + +- A. Relabelings that keep colors +- B. Relabeling tensors +- C. Composition and equivariance +- D. Components + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +/-! + +## A. Relabelings that keep colors + +-/ + +/-- A relabeling of slots that keeps their colors: a bijection of slots, with the proof that each +new slot has the color of the old slot it comes from. The proof is stated at the new labels `j`, +so that `c₂ j` reduces at labels such as `inl x` or `0`. For finitely many concrete slots it is +found by `rfl` or by cases. Two relabelings are equal when their bijections are +(`ColorEquiv.ext`). -/ +@[ext] +structure ColorEquiv {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) where + /-- The bijection of slots, old slot `i` to new slot `toEquiv i`. -/ + toEquiv : ι₁ ≃ ι₂ + /-- The new slot `j` has the color of the old slot `toEquiv.symm j`. -/ + color_eq : ∀ j, c₁ (toEquiv.symm j) = c₂ j := by + first | exact fun _ => rfl | (intro j; fin_cases j <;> rfl) + +namespace ColorEquiv + +variable {K : Type} {ι₁ ι₂ ι₃ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} + {c₃ : ι₃ → Color K} + +/-- The identity relabeling, where every slot keeps its label. -/ +def refl (c₁ : ι₁ → Color K) : ColorEquiv c₁ c₁ := ⟨Equiv.refl ι₁, fun _ => rfl⟩ + +/-- The inverse relabeling, where every slot gets back its old label. -/ +def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := + ⟨σ.toEquiv.symm, fun i => by simpa using (σ.color_eq (σ.toEquiv i)).symm⟩ + +/-- The relabeling `σ₁` followed by `σ₂`. -/ +def trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : ColorEquiv c₁ c₃ := + ⟨σ₁.toEquiv.trans σ₂.toEquiv, fun j => (σ₁.color_eq _).trans (σ₂.color_eq j)⟩ + +end ColorEquiv + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + +variable {ι₁ ι₂ ι₃ : Type} + +/-! + +## B. Relabeling tensors + +-/ + +variable (S) in +/-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new slot `j` +holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is Mathlib's +`PiTensorProduct.reindex` followed by `PiTensorProduct.congr` of `LinearEquiv.cast`. -/ +noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : + S.Tensor c₁ ≃ₗ[k] S.Tensor c₂ := + S.toPiTensor c₁ ≪≫ₗ PiTensorProduct.reindex k _ σ.toEquiv ≪≫ₗ + PiTensorProduct.congr (fun j => LinearEquiv.cast (M := S.V) (σ.color_eq j)) ≪≫ₗ + (S.toPiTensor c₂).symm + +/-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved +along `σ.color_eq j`. -/ +@[simp] +lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) + (f : Π i, S.V (c₁ i)) : + S.reindex σ (S.pure f) = S.pure fun j => + LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq j) (f (σ.toEquiv.symm j)) := by + simp [reindex, PiTensorProduct.reindex_tprod, congr_tprod] + +/-! + +## C. Composition and equivariance + +-/ + +/-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`. -/ +lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} + (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : + S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by + induction t using inductionOn_pure with + | h f => + simp only [reindex_pure, LinearEquiv.cast_apply, cast_cast] + rfl + | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] + | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add] + +/-- Relabeling commutes with the group action, as in `(ΛT)ᵀ = Λ Tᵀ`. -/ +lemma reindex_smul {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (g : G) + (t : S.Tensor c₁) : S.reindex σ (g • t) = g • S.reindex σ t := + map_smul_of_pure (S.reindex σ) g (fun f => by + simp only [smul_pure, reindex_pure] + congr 1 + funext j + exact cast_rep _ _ _) t + +/-! + +## D. Components + +-/ + +/-- The components of a relabeled tensor are those of the tensor, as in `(Tᵀ)_ν^μ = T^μ_ν`. The +old slot `i` takes the label `y (σ.toEquiv i)` of its new slot, cast along the equality of their +colors. For concrete colors and labels the cast is between equal types, and `rfl` removes it. -/ +lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → Color K} + {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (t : S.Tensor c₁) + (y : Π j, basisIdx (c₂ j).kind) : + (S.tensorBasis c₂).repr (S.reindex σ t) y = + (S.tensorBasis c₁).repr t fun i => + cast (congrArg (basisIdx ∘ Color.kind) (σ.symm.color_eq i)) (y (σ.toEquiv i)) := by + induction t using inductionOn_pure with + | hsmul r t ht => simp only [map_smul, Finsupp.smul_apply, ht] + | hadd t t' ht ht' => simp only [map_add, Finsupp.add_apply, ht, ht'] + | h f => + rw [reindex_pure, tensorBasis_repr_pure, tensorBasis_repr_pure] + refine (Fintype.prod_equiv σ.toEquiv _ _ fun i => ?_).symm + -- The old slot `σ.toEquiv.symm (σ.toEquiv i)` is `i` only up to a proof, so it is generalized. + have h : ∀ i' (_ : i' = i) (d : Color K) (p : c₁ i' = d) (q : d = c₁ i) (z : basisIdx d.kind), + (S.basis (c₁ i)).repr (f i) (cast (congrArg (basisIdx ∘ Color.kind) q) z) = + (S.basis d).repr (LinearEquiv.cast (R := k) (M := S.V) p (f i')) z := by + rintro _ rfl _ rfl _ _ + rfl + exact h _ (σ.toEquiv.symm_apply_apply i) _ _ (σ.symm.color_eq i) _ + +end Species + +end Tensors From 6474250ff8f6ceb32e6506f353ec25070021e0d7 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:17 +0000 Subject: [PATCH 005/242] feat(Tensors): tmulEquiv with its assoc, comm and smul laws, twoSlotEquiv with twoSlotEquiv_map_rep, deltaTensor and their components Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Product.lean | 306 +++++++++++++++++++++++ 2 files changed, 307 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Product.lean diff --git a/Physlib.lean b/Physlib.lean index e10588a6e2..a0ea9121f7 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -197,6 +197,7 @@ public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color +public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing public import Physlib.Mathematics.Tensors.Species public import Physlib.Mathematics.VariationalCalculus.Basic diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean new file mode 100644 index 0000000000..7db6e8e89b --- /dev/null +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -0,0 +1,306 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Reindexing +/-! + +# The product of two tensors + +The product `T ⊗ U` of two tensors has the indices of `T` followed by those of `U`. + +## i. Overview + +Let `T = T^μ_ν e_μ ⊗ e^ν` be a Lorentz tensor with colors `c₁ = ![⟨vec, up⟩, ⟨vec, down⟩]` on the +slots `Fin 2`, and `U = U^ρ e_ρ` a vector with colors `c₂ = ![⟨vec, up⟩]` on the slot `Fin 1`. +Here `V ⟨vec, up⟩ = ℝ⁴` and `V ⟨vec, down⟩ = (ℝ⁴)*`. Their product is the element + + `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` of the space `ℝ⁴ ⊗ (ℝ⁴)* ⊗ ℝ⁴`. + +Its slots are labeled by `Fin 2 ⊕ Fin 1`. The slots `inl 0` and `inl 1` carry the indices `μ` and +`ν` of `T`, and the slot `inr 0` carries the index `ρ` of `U`. No slot is renumbered, so no +equality of colors has to be proved. + +- `prodColors c₁ c₂ : ι₁ ⊕ ι₂ → Color K` gives the colors of `T ⊗ U`, those of `T` on the slots + `inl i` and those of `U` on the slots `inr j`. +- `S.tmulEquiv c₁ c₂` sends the element `T ⊗ₜ U` of the space `S.Tensor c₁ ⊗ S.Tensor c₂` to the + tensor `T ⊗ U` in `S.Tensor (prodColors c₁ c₂)`. It is Mathlib's + `PiTensorProduct.tmulEquivDep`. Its inverse splits a tensor on `ι₁ ⊕ ι₂` into its two groups of + indices. + +The product is associative and symmetric up to a relabeling of slots. For a third tensor `W`, +`(T ⊗ U) ⊗ W` on `(ι₁ ⊕ ι₂) ⊕ ι₃` and `T ⊗ (U ⊗ W)` on `ι₁ ⊕ (ι₂ ⊕ ι₃)` both have components +`T^μ_ν U^ρ W^σ`, and they are equal after the relabeling `ColorEquiv.sumAssoc`. A group element +`Λ`, such as a Lorentz transformation, acts on a product factor by factor, `Λ T ⊗ Λ U = Λ (T ⊗ U)`. + +An element `M` of `V a ⊗ V b` is the tensor `S.twoSlotEquiv a b M` with colors `![a, b]`. For +example, the metric `η_μν e^μ ⊗ e^ν` in `(ℝ⁴)* ⊗ (ℝ⁴)*` is the tensor `η_μν`. The delta of an +upper color `c` is the tensor `S.deltaTensor c = δ_μ^ν`, with colors `![c.dual, c]`. + +The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ` +(`tensorBasis_repr_tmulEquiv`). The components of `S.twoSlotEquiv a b M` are the coordinates of +`M` in Mathlib's basis `Basis.tensorProduct` of `V a ⊗ V b` (`tensorBasis_repr_twoSlotEquiv`). + +## ii. Key results + +- `prodColors` : the colors of a product. +- `ColorEquiv.sumCongr`, `ColorEquiv.sumAssoc`, `ColorEquiv.sumComm` : relabelings of the slots + of a product. +- `Species.tmulEquiv` : the product of two tensors, `T ⊗ₜ U ↦ T ⊗ U`. +- `Species.tmulEquiv_assoc`, `Species.tmulEquiv_comm` : `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and + `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. +- `Species.tmulEquiv_smul` : `Λ T ⊗ Λ U = Λ (T ⊗ U)`. +- `Species.twoSlotEquiv`, `Species.deltaTensor` : an element of `V a ⊗ V b` as a tensor on two + slots, and the delta `δ_μ^ν`. +- `Species.twoSlotEquiv_map_rep` : `Λ` acts on a tensor on two slots factor by factor. +- `Species.tensorBasis_repr_tmulEquiv` : `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. +- `Species.tensorBasis_repr_twoSlotEquiv` : the components of a tensor on two slots. + +## iii. Table of contents + +- A. The colors and relabelings of a product +- B. The product +- C. Associativity, symmetry and equivariance +- D. Tensors on two slots +- E. Components + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +/-! + +## A. The colors and relabelings of a product + +-/ + +/-- The colors of a product `T ⊗ U`, the colors `c₁` of `T` on the slots `inl i` and the colors +`c₂` of `U` on the slots `inr j`. It equals Mathlib's `Sum.elim c₁ c₂` by `rfl`. It is reducible, +so that `simp` and `rw` see `prodColors c₁ c₂ (.inl i)` as `c₁ i` and can apply Mathlib's lemmas +on `PiTensorProduct.tmulEquivDep`. -/ +noncomputable abbrev prodColors {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) + (c₂ : ι₂ → Color K) : ι₁ ⊕ ι₂ → Color K := + fun s => Sum.rec (motive := fun _ => Color K) c₁ c₂ s + +namespace ColorEquiv + +variable {K : Type} {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} + +/-- The relabeling `σ` of the slots of `T` and `τ` of the slots of `U`, as a relabeling of the +slots of `T ⊗ U`. It is Mathlib's `Equiv.sumCongr`. -/ +def sumCongr {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} + {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') : + ColorEquiv (prodColors c₁ c₂) (prodColors c₁' c₂') := + ⟨Equiv.sumCongr σ.toEquiv τ.toEquiv, by rintro (j | j); exacts [σ.color_eq j, τ.color_eq j]⟩ + +/-- The slots of `(T ⊗ U) ⊗ W` relabeled as those of `T ⊗ (U ⊗ W)`. It is Mathlib's +`Equiv.sumAssoc`. -/ +def sumAssoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : + ColorEquiv (prodColors (prodColors c₁ c₂) c₃) (prodColors c₁ (prodColors c₂ c₃)) := + ⟨Equiv.sumAssoc ι₁ ι₂ ι₃, by rintro (_ | _ | _) <;> rfl⟩ + +/-- The slots of `T ⊗ U` relabeled as those of `U ⊗ T`. It is Mathlib's `Equiv.sumComm`. -/ +def sumComm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : + ColorEquiv (prodColors c₁ c₂) (prodColors c₂ c₁) := + ⟨Equiv.sumComm ι₁ ι₂, by rintro (_ | _) <;> rfl⟩ + +end ColorEquiv + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + +variable {ι₁ ι₂ ι₃ : Type} + +/-! + +## B. The product + +-/ + +variable (S) in +/-- The product of tensors, `T ⊗ₜ U ↦ T ⊗ U` with `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. The indices of +`T` sit on the slots `inl i` and those of `U` on the slots `inr j`. It is Mathlib's +`PiTensorProduct.tmulEquivDep`. -/ +noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : + S.Tensor c₁ ⊗[k] S.Tensor c₂ ≃ₗ[k] S.Tensor (prodColors c₁ c₂) := + TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ + tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm + +/-- The product of the pure tensors `u ⊗ α` and `w` is the pure tensor `u ⊗ α ⊗ w`. The vectors +`f₁` of the first sit on the slots `inl i` and the vectors `f₂` of the second on the slots +`inr j`. -/ +@[simp] +lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ : Π i, S.V (c₁ i)) + (f₂ : Π j, S.V (c₂ j)) : + S.tmulEquiv c₁ c₂ (S.pure f₁ ⊗ₜ S.pure f₂) = + S.pure fun s => Sum.rec (motive := fun s => S.V (prodColors c₁ c₂ s)) f₁ f₂ s := by + simp [tmulEquiv, tmulEquivDep_apply] + +/-- The inverse of the product splits a pure tensor into the pure tensors of its two groups of +slots, `u ⊗ α ⊗ w ↦ (u ⊗ α) ⊗ₜ w`. -/ +@[simp] +lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) + (f : Π s, S.V (prodColors c₁ c₂ s)) : + (S.tmulEquiv c₁ c₂).symm (S.pure f) = + S.pure (c := c₁) (fun i => f (.inl i)) ⊗ₜ S.pure (c := c₂) fun j => f (.inr j) := by + simp [tmulEquiv, tmulEquivDep_symm_apply] + +/-! + +## C. Associativity, symmetry and equivariance + +-/ + +/-- The product is associative, `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)`, after the slots of the left side +are regrouped by `ColorEquiv.sumAssoc`. -/ +lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) + (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : + S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃) + (S.tmulEquiv _ _ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃)) = + S.tmulEquiv _ _ (t₁ ⊗ₜ S.tmulEquiv c₂ c₃ (t₂ ⊗ₜ t₃)) := by + induction t₁ using inductionOn_pure with + | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] + | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] + | h f₁ => + induction t₂ using inductionOn_pure with + | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] + | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] + | h f₂ => + induction t₃ using inductionOn_pure with + | hadd t t' ht ht' => simp only [tmul_add, map_add, ht, ht'] + | hsmul r t ht => simp only [tmul_smul, map_smul, ht] + | h f₃ => + simp only [tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl) + +/-- The product is symmetric, `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`, after the indices of `U` are moved in +front by `ColorEquiv.sumComm`. -/ +lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ : S.Tensor c₁) + (t₂ : S.Tensor c₂) : + S.reindex (ColorEquiv.sumComm c₁ c₂) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = + S.tmulEquiv c₂ c₁ (t₂ ⊗ₜ t₁) := by + induction t₁ using inductionOn_pure with + | hsmul r t ht => simp only [smul_tmul, tmul_smul, map_smul, ht] + | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] + | h f₁ => + induction t₂ using inductionOn_pure with + | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] + | hadd t t' ht ht' => simp only [tmul_add, add_tmul, map_add, ht, ht'] + | h f₂ => + rw [tmulEquiv_pure, tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + +/-- The product commutes with the group action, `Λ T ⊗ Λ U = Λ (T ⊗ U)`, as in +`(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ +lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : G) (t₁ : S.Tensor c₁) + (t₂ : S.Tensor c₂) : + S.tmulEquiv c₁ c₂ ((g • t₁) ⊗ₜ (g • t₂)) = g • S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) := by + induction t₁ using inductionOn_pure with + | hsmul r t ht => rw [smul_comm, ← smul_tmul', map_smul, ht, ← smul_tmul', map_smul, smul_comm] + | hadd t t' ht ht' => simp only [smul_add, add_tmul, map_add, ht, ht'] + | h f₁ => + induction t₂ using inductionOn_pure with + | hsmul r t ht => rw [smul_comm, tmul_smul, map_smul, ht, tmul_smul, map_smul, smul_comm] + | hadd t t' ht ht' => simp only [smul_add, tmul_add, map_add, ht, ht'] + | h f₂ => + rw [smul_pure, smul_pure, tmulEquiv_pure, tmulEquiv_pure, smul_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + +/-! + +## D. Tensors on two slots + +-/ + +variable (S) in +/-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` +is the tensor `η_μν`. Each factor is a one-slot tensor by Mathlib's +`PiTensorProduct.subsingletonEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are renamed +`Fin 2` by Mathlib's `finSumFinEquiv`. -/ +noncomputable def twoSlotEquiv (a b : Color K) : S.V a ⊗[k] S.V b ≃ₗ[k] S.Tensor ![a, b] := + TensorProduct.congr (S.toPiTensor (fun _ : Fin 1 => a) ≪≫ₗ subsingletonEquiv 0).symm + (S.toPiTensor (fun _ : Fin 1 => b) ≪≫ₗ subsingletonEquiv 0).symm ≪≫ₗ S.tmulEquiv _ _ ≪≫ₗ + S.reindex (.mk finSumFinEquiv) + +/-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ +@[simp] +lemma twoSlotEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : + S.twoSlotEquiv a b (x ⊗ₜ y) = + S.pure (c := ![a, b]) (Fin.cons x (Fin.cons y finZeroElim)) := by + simp only [twoSlotEquiv, LinearEquiv.trans_apply, TensorProduct.congr_tmul, + LinearEquiv.trans_symm, subsingletonEquiv_symm_apply', toPiTensor_symm_tprod, + tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) + +/-- `twoSlotEquiv` commutes with the group action, which acts on `M ∈ V a ⊗ V b` factor by factor, +`Λ (x ⊗ y) = Λx ⊗ Λy`. -/ +lemma twoSlotEquiv_map_rep (a b : Color K) (Λ : G) (M : S.V a ⊗[k] S.V b) : + S.twoSlotEquiv a b (TensorProduct.map (S.rep a Λ) (S.rep b Λ) M) = + Λ • S.twoSlotEquiv a b M := by + induction M using TensorProduct.inductionOn with + | add x y hx hy => simp only [map_add, smul_add, hx, hy] + | tmul x y => + simp only [TensorProduct.map_tmul, twoSlotEquiv_tmul, smul_pure] + exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) + +variable (S) in +/-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper +color `c` and `δ^μ_ν` for a lower one. -/ +noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := + S.twoSlotEquiv c.dual c (S.delta c) + +/-! + +## E. Components + +-/ + +/-- The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. +The labels `y (inl i)` are those of `T` and the labels `y (inr j)` those of `U`. -/ +lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ → Color K) + (c₂ : ι₂ → Color K) (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) + (y : Π s, basisIdx (prodColors c₁ c₂ s).kind) : + (S.tensorBasis (prodColors c₁ c₂)).repr (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) y = + (S.tensorBasis c₁).repr t₁ (fun i => y (.inl i)) * + (S.tensorBasis c₂).repr t₂ (fun j => y (.inr j)) := by + induction t₁ using inductionOn_pure with + | hsmul r t ht => + simp only [← smul_tmul', map_smul, Finsupp.smul_apply, ht, smul_eq_mul, mul_assoc] + | hadd t t' ht ht' => simp only [add_tmul, map_add, Finsupp.add_apply, ht, ht', add_mul] + | h f₁ => + induction t₂ using inductionOn_pure with + | hsmul r t ht => + simp only [tmul_smul, map_smul, Finsupp.smul_apply, ht, smul_eq_mul, mul_left_comm] + | hadd t t' ht ht' => simp only [tmul_add, map_add, Finsupp.add_apply, ht, ht', mul_add] + | h f₂ => + rw [tmulEquiv_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, + Fintype.prod_sum_type] + +/-- The components of the tensor of `M` in `V a ⊗ V b` are the coordinates of `M` in Mathlib's +product basis `Basis.tensorProduct`. -/ +lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) + (y : Π j, basisIdx (![a, b] j).kind) : + (S.tensorBasis ![a, b]).repr (S.twoSlotEquiv a b M) y = + ((S.basis a).tensorProduct (S.basis b)).repr M (y 0, y 1) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, Finsupp.add_apply, hM, hM'] + | tmul x x' => + rw [twoSlotEquiv_tmul, tensorBasis_repr_pure, Fin.prod_univ_two, mul_comm] + -- `y 0` is a label at `a` only after unfolding `![a, b] 0`, so `rw` and `simp` cannot use + -- Mathlib's lemma, and `exact` checks it up to unfolding. + exact ((Module.Basis.tensorProduct_repr_tmul_apply _ _ _ _ _ _).trans (smul_eq_mul _ _)).symm + +end Species + +end Tensors From c23873aca758b6f9fcf1730e0a5efb16286ea896 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:19 +0000 Subject: [PATCH 006/242] feat(Tensors): Tensor.contr of chosen pairs, with contr_contr, tmulEquiv_contr, contr_matMul and tensorBasis_repr_contr Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Contraction.lean | 357 +++++++++++++++++++ 2 files changed, 358 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Contraction.lean diff --git a/Physlib.lean b/Physlib.lean index a0ea9121f7..983c84b001 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -197,6 +197,7 @@ public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color +public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing public import Physlib.Mathematics.Tensors.Species diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean new file mode 100644 index 0000000000..44e57ea8d6 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -0,0 +1,357 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Product +public import Mathlib.LinearAlgebra.PiTensorProduct.Dual +/-! + +# Contraction of tensors + +The contraction of pairs of indices, an index with its dual in each pair, as in the trace +`T^ρμ_μ` or the full contraction `T^μν U_μν`, and its laws. + +## i. Overview + +Take a Lorentz tensor `T = T^ρμ_ν e_ρ ⊗ e_μ ⊗ e^ν`, an element of `ℝ⁴ ⊗ ℝ⁴ ⊗ (ℝ⁴)*`, on the +slots `Fin 3` with colors `c = ![⟨vec, up⟩, ⟨vec, up⟩, ⟨vec, down⟩]`. Its trace over the last two +indices is the vector `(tr T)^ρ = T^ρμ_μ` in `ℝ⁴`. On a pure tensor `u ⊗ v ⊗ α` it is +`(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. + +A contraction sorts the slots into the standard arrangement `κ ⊕ (J ⊕ J)`. +- The slot `inl x`, for `x : κ`, remains, with the color `c' x`. +- For each pair `j : J`, the slot `inr (inl j)` has the color `a j` and the slot `inr (inr j)` + the dual color `(a j).dual`. The two are contracted. + +These colors are `contrColors c' a`. For the trace, `κ = Fin 1`, `c' = ![⟨vec, up⟩]`, `J = Unit` +and `a = fun _ => ⟨vec, up⟩`. `Tensor.contrStd c' a` contracts a tensor in this arrangement. The +two slots of a pair have dual colors by construction, so its formula on pure tensors has no cast +(`contrStd_pure`). + +`Tensor.contr σ` contracts the pairs chosen by a relabeling `σ : ColorEquiv c (contrColors c' a)`, +which sends each slot of `T` to its place in the standard arrangement. It is `S.reindex σ` +followed by `Tensor.contrStd c' a`. For the trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and +`2 ↦ inr (inr ())`. The duality of the two colors of each pair is part of the color proof of `σ`, +not a separate hypothesis. The index notation `{T | ρ μ μ}ᵀ` builds `σ` from the names. + +Each pair is ordered: its slot `inr (inl j)` is the first argument of `S.contr (a j)`. No lemma +here exchanges the two slots of a pair. + +A contraction of two tensors is a contraction of their product. The full contraction `T^μν U_μν` +contracts every slot of `T ⊗ U`, with no remaining slots, `κ = Empty`. The laws let a network of +tensors be contracted in pieces. +- Relabeling the pairs does not change the contraction (`contr_reindex_pairs`). +- Two contractions in sequence are one, as contracting `μ` and then `ν` in `T^μ_μ^ν_ν` is + contracting both pairs at once (`contr_contr`). +- Contracting `T` and then multiplying by `U` is contracting `T ⊗ U`, + `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ` (`tmulEquiv_contr`). + +For tensors on two slots, the contraction of the inner indices is the product of matrices +`M^μ_ν N^ν_ρ`, along the relabeling `ColorEquiv.matMul` (`contr_matMul`). + +The components of a contraction are sums of components of the tensor, as in +`(tr T)^ρ = ∑ μ ν, C_μ^ν T^ρμ_ν` (`tensorBasis_repr_contr`). Here `C_μ^ν = S.contr (e_μ ⊗ e^ν)` +is the contraction of the basis vectors of the two slots of the pair. The formula assumes nothing +about the bases. When the bases of a color and of its dual are dual, `C_μ^ν` is `1` for `μ = ν` +and `0` otherwise, and the formula is the sum `(tr T)^ρ = ∑ μ, T^ρμ_μ` +(`tensorBasis_repr_contr_of_dual`). + +## ii. Key results + +- `contrColors` : the colors of the standard arrangement, the remaining slots and then the pairs. +- `Species.Tensor.contrStd` : the contraction of a tensor in the standard arrangement. +- `Species.Tensor.contr` : the contraction of the pairs of indices chosen by `σ`, as in `T^ρμ_μ`. +- `Species.Tensor.contr_pure` : the contraction of a pure tensor. +- `Species.Tensor.contr_smul` : contraction commutes with the group action. +- `Species.Tensor.contr_reindex_pairs` : relabeling the pairs does not change the contraction. +- `Species.Tensor.contr_contr` : two contractions in sequence are one, along `ColorEquiv.chain`. +- `Species.Tensor.tmulEquiv_contr` : contraction commutes with a product by a tensor that is not + contracted, along `ColorEquiv.withSpectators`. +- `Species.Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ` of two tensors on two slots. +- `Species.Tensor.tensorBasis_repr_contr` : the components of a contraction. +- `Species.Tensor.tensorBasis_repr_contr_of_dual` : the components of a contraction in dual bases. + +## iii. Table of contents + +- A. Contraction in the standard arrangement of slots +- B. Contraction of chosen pairs of indices + - B.1. Relabeling the pairs + - B.2. Two contractions are one + - B.3. Products with a tensor that is not contracted +- C. The product of two tensors on two slots +- D. Components + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + +variable {ι₁ ι₂ κ κ' κ₁ J J' J₁ J₂ : Type} + +namespace Tensor + +/-! + +## A. Contraction in the standard arrangement of slots + +-/ + +/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of slots. The remaining slot `inl x` has +the color `c x`, and the two slots `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors +`a j` and `(a j).dual`. -/ +noncomputable abbrev _root_.Tensors.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : + κ ⊕ (J ⊕ J) → Color K := + prodColors c (prodColors a fun j => (a j).dual) + +/-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the +slot `inr (inl j)` is contracted with the slot `inr (inr j)` by `S.contr (a j)`, with the slot +`inr (inl j)` as its first argument. The slots `inl x` remain, with the colors `c`. -/ +noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : + S.Tensor (contrColors c a) →ₗ[k] S.Tensor c := + -- The pairs are split into their first halves, of colors `a`, and their second halves, of the + -- dual colors. Each vector of a second half is read as a linear form on the first half through + -- `S.contr`, and the halves are paired by Mathlib's `PiTensorProduct.dualDistrib`. + (TensorProduct.rid k _).toLinearMap ∘ₗ + (lift ((PiTensorProduct.dualDistrib ∘ₗ + PiTensorProduct.map (fun j => (curry (S.contr (a j)).toLinearMap).flip) ∘ₗ + (S.toPiTensor _).toLinearMap).flip ∘ₗ (S.toPiTensor a).toLinearMap) ∘ₗ + (S.tmulEquiv a _).symm.toLinearMap).lTensor _ ∘ₗ + (S.tmulEquiv c _).symm.toLinearMap + +/-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the +two vectors of each pair, as in `u ⊗ v ⊗ α ↦ (v^μ α_μ) u`. -/ +lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) + (f : Π x, S.V (contrColors c a x)) : + contrStd c a (S.pure f) = + (∏ j, S.contr (a j) (f (.inr (.inl j)) ⊗ₜ f (.inr (.inr j)))) • + S.pure (c := c) fun x => f (.inl x) := by + simp [contrStd, PiTensorProduct.map_tprod, PiTensorProduct.dualDistrib_apply] + +/-! + +## B. Contraction of chosen pairs of indices + +-/ + +/-- The contraction of the pairs of indices chosen by `σ`, as in `T^ρμ_ν ↦ T^ρμ_μ`. It is +`S.reindex σ` followed by `Tensor.contrStd c' a`. For each pair `j` the slot +`σ.toEquiv.symm (inr (inl j))`, of color `a j`, is contracted with the slot +`σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The slots `σ.toEquiv.symm (inl x)` remain, +with the colors `c'`. -/ +noncomputable def contr [Fintype J] {c : ι₁ → Color K} {c' : κ → Color K} + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) : S.Tensor c →ₗ[k] S.Tensor c' := + contrStd c' a ∘ₗ (S.reindex σ).toLinearMap + +variable {c : ι₁ → Color K} {c' : κ → Color K} + +/-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the +two vectors of each pair. Each vector is moved along `σ.color_eq` to the color of its slot in the +standard arrangement. -/ +lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) + (f : Π x, S.V (c x)) : + contr σ (S.pure f) = + (∏ j, S.contr (a j) (LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq (.inr (.inl j))) + (f (σ.toEquiv.symm (.inr (.inl j)))) ⊗ₜ LinearEquiv.cast (R := k) (M := S.V) + (σ.color_eq (.inr (.inr j))) (f (σ.toEquiv.symm (.inr (.inr j)))))) • + S.pure (c := c') fun x => LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq (.inl x)) + (f (σ.toEquiv.symm (.inl x))) := + (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) + +/-- Contraction commutes with the group action, `(ΛT)^ρμ_μ = Λ^ρ_λ T^λμ_μ` for the action `Λ` of +`g : G`. -/ +lemma contr_smul [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (g : G) + (t : S.Tensor c) : contr σ (g • t) = g • contr σ t := + map_smul_of_pure (contr σ) g (fun f => by + rw [smul_pure, contr_pure, contr_pure, smul_comm, smul_pure] + simp only [cast_rep] + congr 1 + exact Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) t + +/-! + +### B.1. Relabeling the pairs + +-/ + +/-- Relabeling the pairs along `e : J ≃ J'` does not change the contraction. Each pair keeps its +order. -/ +lemma contr_reindex_pairs [Fintype J] [Fintype J'] {a : J → Color K} + (σ : ColorEquiv c (contrColors c' a)) (e : J ≃ J') (t : S.Tensor c) : + contr (σ.trans ((ColorEquiv.refl c').sumCongr + ((ColorEquiv.mk e).sumCongr (.mk e)))) t = contr σ t := by + simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe] + rw [← reindex_reindex] + generalize S.reindex σ t = u + induction u using inductionOn_pure with + | h p => + rw [reindex_pure, contrStd_pure, contrStd_pure] + congr 1 + exact Equiv.prod_comp e.symm fun j => S.contr (a j) (p (.inr (.inl j)) ⊗ₜ p (.inr (.inr j))) + | hsmul r u hu => rw [map_smul, map_smul, hu, map_smul] + | hadd u u' hu hu' => rw [map_add, map_add, hu, hu', map_add] + +/-! + +### B.2. Two contractions are one + +-/ + +/-- The pairs of two contractions in sequence, `σ₁` and then `σ₂`, as one choice of pairs +`J₁ ⊕ J₂`, the pairs `J₁` of `σ₁` followed by the pairs `J₂` of `σ₂`. -/ +noncomputable def _root_.Tensors.ColorEquiv.chain {c : ι₁ → Color K} {c₁ : κ₁ → Color K} + {c' : κ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} + (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) : + ColorEquiv c (contrColors c' (prodColors a₁ a₂)) := + σ₁.trans ((σ₂.sumCongr (.refl _)).trans ⟨(Equiv.sumAssoc _ _ _).trans + (Equiv.sumCongr (Equiv.refl _) ((Equiv.sumComm _ _).trans (Equiv.sumSumSumComm _ _ _ _))), + by rintro (_ | (_ | _) | (_ | _)) <;> rfl⟩) + +/-- Two contractions in sequence are one contraction, along `σ₁.chain σ₂`. Contracting `μ` and +then `ν` in `T^μ_μ^ν_ν` is contracting both pairs at once. -/ +lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} + {a₂ : J₂ → Color K} (σ₁ : ColorEquiv c (contrColors c₁ a₁)) + (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) (t : S.Tensor c) : + contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := by + induction t using inductionOn_pure with + | h f => + simp only [contr_pure, map_smul, smul_smul, Fintype.prod_sum_type, LinearEquiv.cast_apply, + cast_cast] + rfl + | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] + | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add] + +/-! + +### B.3. Products with a tensor that is not contracted + +-/ + +/-- The pairs `σ` of `T`, as pairs of `T ⊗ U`. The indices of `U`, on the slots `ι₂` with the +colors `d`, remain after those of `T` that are not contracted. -/ +noncomputable def _root_.Tensors.ColorEquiv.withSpectators {c : ι₁ → Color K} {c' : κ → Color K} + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : + ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := + (σ.sumCongr (.refl d)).trans ((ColorEquiv.sumAssoc _ _ _).trans + (((ColorEquiv.refl c').sumCongr (ColorEquiv.sumComm _ _)).trans + (ColorEquiv.sumAssoc _ _ _).symm)) + +/-- Contracting indices of `T` and then multiplying by `U` is contracting the same indices of +`T ⊗ U`, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ +lemma tmulEquiv_contr [Fintype J] {a : J → Color K} {d : ι₂ → Color K} + (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (u : S.Tensor d) : + S.tmulEquiv c' d (contr σ t ⊗ₜ u) = + contr (σ.withSpectators d) (S.tmulEquiv c d (t ⊗ₜ u)) := by + induction t using inductionOn_pure with + | hsmul r t ht => simp only [map_smul, ← smul_tmul', ht] + | hadd x y hx hy => simp only [map_add, add_tmul, hx, hy] + | h p => + induction u using inductionOn_pure with + | hsmul r u hu => simp only [map_smul, tmul_smul, hu] + | hadd x y hx hy => simp only [map_add, tmul_add, hx, hy] + | h p' => + rw [contr_pure, ← smul_tmul', map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] + congr 1 + exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) + +/-! + +## C. The product of two tensors on two slots + +-/ + +/-- The pair of the product `M^μ_ν N^ν_ρ` of two tensors on two slots, the second slot of `M`, of +color `c`, with the first slot of `N`, of color `c.dual`. The slots of `μ` and `ρ` remain, as `0` +and `1`. -/ +def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : + ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where + toEquiv.toFun + | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 + toEquiv.invFun + | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 + toEquiv.left_inv := by decide + toEquiv.right_inv := by decide + +/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots. It is `M` with its second factor +mapped by `N`, read through `S.contr c` as the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ +lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : + contr (ColorEquiv.matMul a c b) + (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.twoSlotEquiv _ b N)) = + S.twoSlotEquiv a b ((pairingToLinearMap (S.contr c).toLinearMap N).lTensor _ M) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] + | tmul m₁ m₂ => + induction N using TensorProduct.inductionOn with + | add N N' hN hN' => + simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] + | tmul n₁ n₂ => + simp only [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, + LinearMap.lTensor_tmul, pairingToLinearMap_tmul, tmul_smul, map_smul] + congr 2 + funext j + fin_cases j <;> rfl + +/-! + +## D. Components + +-/ + +/-- The components of a contraction, as `(tr T)^ρ = ∑ μ ν, C_μ^ν T^ρμ_ν` for a trace. The labels +`μ` and `ν` run over the first and the second slots of the pairs, `C_μ^ν` is the product over the +pairs of `S.contr (a j) (e_μ ⊗ e^ν)`, and the components of `T` are those of `S.reindex σ T`. -/ +lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) + (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (contr σ t) φ = + ∑ μ : Π j, basisIdx (a j).kind, ∑ ν : Π j, basisIdx (a j).dual.kind, + (∏ j, S.contr (a j) (S.basis (a j) (μ j) ⊗ₜ S.basis (a j).dual (ν j))) * + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ ν)) := by + simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe] + generalize S.reindex σ t = u + induction u using inductionOn_pure with + | hsmul r u hu => + simp only [map_smul, Finsupp.smul_apply, hu, smul_eq_mul, Finset.mul_sum] + exact Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => by ring + | hadd u u' hu hu' => + simp only [map_add, Finsupp.add_apply, hu, hu', mul_add, Finset.sum_add_distrib] + | h f => + -- Each contraction of a pair is expanded in components, and the product of sums over the + -- pairs becomes a sum over the families of labels `μ` and `ν`. + rw [contrStd_pure, map_smul, Finsupp.smul_apply, tensorBasis_repr_pure, smul_eq_mul, + Finset.prod_congr rfl fun j _ => contr_tmul_eq_sum_basis (a j) _ _, Fintype.prod_sum] + simp only [tensorBasis_repr_pure, Fintype.prod_sum_type, Fintype.prod_sum, Finset.sum_mul] + refine Finset.sum_congr rfl fun μ _ => Finset.sum_congr rfl fun ν _ => ?_ + simp only [Finset.prod_mul_distrib] + ring + +/-- The components of a contraction in dual bases, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. When +the bases of each pair are dual, `S.contr (a j) (e_μ ⊗ e^ν)` is `1` for `μ = ν` and `0` otherwise, +and the double sum of `tensorBasis_repr_contr` has one label per pair. -/ +lemma tensorBasis_repr_contr_of_dual [Fintype κ] [Fintype J] [DecidableEq J] + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) + (φ : Π x, basisIdx (c' x).kind) + (h : ∀ j μ ν, S.contr (a j) (S.basis (a j) μ ⊗ₜ S.basis (a j).dual ν) = + if μ = ν then 1 else 0) : + (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := by + simp only [tensorBasis_repr_contr, h, Finset.prod_boole, Finset.mem_univ, forall_const, + ← funext_iff, boole_mul, Fintype.sum_ite_eq] + +end Tensor + +end Species + +end Tensors From 34ae711288fec5e3ff093051c56cbe497abe0651 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:21 +0000 Subject: [PATCH 007/242] feat(Tensors): WithInvariantForm, WithMetric, toDual, toDualAt and formTensor, with toDualAt_toDualAt, formTensor_invariant, contr_formTensor_formTensor and toDualAt_eq_contr_formTensor Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Metric.lean | 286 ++++++++++++++++++++++++ 2 files changed, 287 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Metric.lean diff --git a/Physlib.lean b/Physlib.lean index 983c84b001..a7b82aaaa2 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -198,6 +198,7 @@ public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.Tensors.Contraction +public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing public import Physlib.Mathematics.Tensors.Species diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean new file mode 100644 index 0000000000..24ff3c6b61 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -0,0 +1,286 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Contraction +/-! + +# Invariant forms, metrics, and lowering and raising indices + +An invariant form takes an index to its dual color, lowering an upper index and raising a lower +one. + +## i. Overview + +For Lorentz tensors `V ⟨vec, up⟩ = ℝ⁴` and `V ⟨vec, down⟩ = (ℝ⁴)*`. The Minkowski metric is the +element `η_μν e^μ ⊗ e^ν` of the space `(ℝ⁴)* ⊗ (ℝ⁴)*`, and its inverse is the element +`η^μν e_μ ⊗ e_ν` of `ℝ⁴ ⊗ ℝ⁴`. They lower and raise indices, `v_ν = v^μ η_μν` and +`w^ν = w_μ η^μν`, and they are inverse to each other, `v^μ η_μν η^νρ = v^ρ`. + +An invariant form on a species `S`, the class `S.WithInvariantForm`, gives such a pair for every +color. The form `form c` is an element of `V c ⊗ V c`, written `g^μν` at an upper color and `g_μν` +at a lower one. It is invariant under the group, and the forms at `c` and at `c.dual` are inverse +to each other. A metric, the class `S.WithMetric`, is an invariant form that is symmetric, +`g_μν = g_νμ`, as `η`. The Weyl `ε` is an invariant form that is antisymmetric, so it is not a +metric. + +The form is a choice, since `-η` serves as well as `η`. Given `g_μν`, its inverse `g^μν` is unique +(`form_inverse_unique`). The scalars `k` are only a commutative ring, over which a non-degenerate +form need not have an inverse, so the class stores the inverse and the law `form_inverse` checks +it. + +`S.toDual c : V c ≃ V c.dual` contracts a vector with the first index of the form at `c.dual`. At +an upper color it lowers the index, `v^μ ↦ v^μ g_μν`, and at a lower color it raises it, +`w_μ ↦ w_μ g^μν`. Its inverse is `S.toDual c.dual`, by `rfl` (`toDual_symm`). + +For a symmetric metric such as `η` the first index gives the textbook `v_ν = η_νμ v^μ`, as +`TensorSpecies.Tensor.toDualMapAtIndex` does. For the antisymmetric `ε` it gives +`ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`. With `ε^12 = ε_21 = 1` this is minus the convention +`ψ_α = ε_αβ ψ^β`, so a Weyl species following that convention stores `-ε`. + +`S.toDualAt i hi h` lowers or raises the index at the slot `i` of a tensor and keeps the others, as +in `T^μν ↦ T^μ_ν` for `i` the second slot. The caller states the colors `c'` of the result, such as +`![⟨vec, up⟩, ⟨vec, down⟩]`. The hypothesis `hi` says that `c'` has the dual color at `i`, and `h` +that it agrees with the colors `c` of the tensor elsewhere. + +`S.formTensor c` is the form as a tensor on two slots, `η^μν` at `⟨vec, up⟩` and `η_μν` at +`⟨vec, down⟩`. The law `form_inverse` stated for tensors is `g^μν g_νρ = δ^μ_ρ` +(`contr_formTensor_formTensor`). Lowering an index is contracting it with the form, +`M^μν g_νρ = M^μ_ρ` (`toDualAt_eq_contr_formTensor`). + +## ii. Key results + +- `Species.WithInvariantForm` : an invariant form with its inverse, `g_μν` and `g^μν`. +- `Species.WithMetric` : a symmetric invariant form, `g_μν = g_νμ`. +- `Species.toDual` : `S.V c ≃ S.V c.dual`, lowering an upper index and raising a lower one. +- `Species.toDual_symm` : raising is the inverse of lowering. +- `Species.form_inverse_unique` : `g^μν` is determined by `g_μν`. +- `Species.toDual_rep`, `Species.toDualAt_smul` : `(Λv)_ν = Λ(v_ν)`, at one index of a tensor. +- `Species.toDualAt` : lowering or raising the index at one slot of a tensor. +- `Species.toDualAt_toDualAt` : raising at a slot undoes lowering there. +- `Species.toDualAt_twoSlotEquiv` : lowering the second index of a tensor on two slots, + `M^μν ↦ M^μ_ν`, is `toDual` on the second factor. +- `Species.toDualAt_eq_contr_formTensor` : lowering is contracting with the form, + `M^μν g_νρ = M^μ_ρ`. +- `Species.formTensor`, `Species.formTensor_invariant` : the form as an invariant tensor on two + slots. +- `Species.contr_formTensor_formTensor` : `g^μν g_νρ = δ^μ_ρ`. + +## iii. Table of contents + +- A. Invariant forms and metrics +- B. Lowering and raising vectors +- C. Lowering and raising at one index +- D. The form as a tensor + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + +/-! + +## A. Invariant forms and metrics + +-/ + +/-- An invariant form on a species: for every color `c` an invariant element `g` of `V c ⊗ V c`, +`g^μν` at an upper color and `g_μν` at a lower one, with the forms at `c` and at `c.dual` inverse +to each other. It need not be symmetric, so it covers both `η` and the Weyl `ε`. Every color must +carry one, so a species containing the SU(3) triplet has none. The invariance at `c.dual` follows +from that at `c` and `form_inverse`, but a field for every color cannot state only half. -/ +class WithInvariantForm (S : Species k G K basisIdx) where + /-- The form at the color `c`, an element of `V c ⊗ V c`. -/ + form : (c : Color K) → S.V c ⊗[k] S.V c + /-- The form is invariant, `Λ g = g` for every `Λ : G`. -/ + form_invariant : ∀ c (Λ : G), TensorProduct.map (S.rep c Λ) (S.rep c Λ) (form c) = form c + /-- The forms at `c` and at `c.dual` are inverse to each other, `v^μ g_μν g^νρ = v^ρ` at an + upper color `c`. The vector is contracted with the first index of each form. -/ + form_inverse : ∀ c, pairingToLinearMap (S.contr c.dual).toLinearMap (form c) ∘ₗ + pairingToLinearMap (S.contr c).toLinearMap (form c.dual) = LinearMap.id + +export WithInvariantForm (form form_invariant form_inverse) + +/-- A metric on a species: an invariant form that is symmetric at every color, as `η`. -/ +class WithMetric (S : Species k G K basisIdx) extends S.WithInvariantForm where + /-- The form is symmetric, `g_μν = g_νμ`. -/ + form_symm : ∀ c, TensorProduct.comm k (S.V c) (S.V c) (form c) = form c + +variable {S : Species k G K basisIdx} [S.WithInvariantForm] + +/-! + +## B. Lowering and raising vectors + +-/ + +variable (S) in +/-- The vector `v` of color `c` contracted with the first index of the form at `c.dual`. It lowers +an upper index, `v^μ ↦ v^μ g_μν`, and raises a lower one, `w_μ ↦ w_μ g^μν`. -/ +noncomputable def toDual (c : Color K) : S.V c ≃ₗ[k] S.V c.dual := + LinearEquiv.ofLinearMap (pairingToLinearMap (S.contr c).toLinearMap (form c.dual)) + (pairingToLinearMap (S.contr c.dual).toLinearMap (form c)) + (WithInvariantForm.form_inverse c.dual) (WithInvariantForm.form_inverse c) + +lemma toDual_apply (c : Color K) (v : S.V c) : + S.toDual c v = pairingToLinearMap (S.contr c).toLinearMap (form c.dual) v := rfl + +/-- Raising is the inverse of lowering: the inverse of `S.toDual c` is `S.toDual c.dual`. -/ +lemma toDual_symm (c : Color K) : (S.toDual c).symm = S.toDual c.dual := rfl + +/-- The inverse of the form is unique: an element `N` of `V c.dual ⊗ V c.dual` inverse to the form +at `c` is the form at `c.dual`. -/ +lemma form_inverse_unique (c : Color K) {N : S.V c.dual ⊗[k] S.V c.dual} + (h : pairingToLinearMap (S.contr c.dual).toLinearMap (form c) ∘ₗ + pairingToLinearMap (S.contr c).toLinearMap N = LinearMap.id) : + N = form c.dual := + S.pairingToLinearMap_contr_injective c ((S.toDual c).toLinearMap_symm_comp_eq _ _ |>.1 h) + +/-- Lowering and raising commute with the group action, `(Λv)_ν = Λ(v_ν)`. -/ +lemma toDual_rep (c : Color K) (g : G) (v : S.V c) : + S.toDual c (S.rep c g v) = S.rep c.dual g (S.toDual c v) := by + rw [toDual_apply, toDual_apply, ← S.pairingToLinearMap_contr_map_rep c g, form_invariant] + +/-! + +## C. Lowering and raising at one index + +-/ + +variable {ι : Type} [DecidableEq ι] + +variable (S) in +/-- `S.toDual` at the slot `i` of a tensor and the identity at the other slots, as in +`T^μν ↦ T^μ_ν` for `i` the second slot. The colors `c'` of the result have the dual color at `i` +by `hi` and agree with `c` elsewhere by `h`. It is Mathlib's `PiTensorProduct.congr`. -/ +noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) + (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c ≃ₗ[k] S.Tensor c' := + -- At the slot `i` the map to the dual color, moved along `hi`. At every other slot `x` the + -- identity, moved along `h x`. + S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => if hx : x = i then + hx ▸ (S.toDual (c i) ≪≫ₗ LinearEquiv.cast (M := S.V) hi.symm) + else LinearEquiv.cast (M := S.V) (h x hx)) ≪≫ₗ (S.toPiTensor c').symm + +/-- Lowering or raising at a slot commutes with the group action, `(ΛT)^μ_ν = Λ(T^μ_ν)`. -/ +lemma toDualAt_smul {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) + (h : ∀ x, x ≠ i → c x = c' x) (g : G) (t : S.Tensor c) : + S.toDualAt i hi h (g • t) = g • S.toDualAt i hi h t := + map_smul_of_pure (S.toDualAt i hi h) g (fun p => by + simp only [smul_pure, toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, + PiTensorProduct.congr_tprod, toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp only [dite_true, LinearEquiv.trans_apply, toDual_rep, cast_rep] + · simp only [hx, dite_false, cast_rep]) t + +/-- On a pure tensor, `toDualAt` applies `toDual` to the vector at `i` and keeps the others. The +result `q` is explicit, so that for concrete colors `hqi` and `hq` hold by `rfl`. -/ +lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) + (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) + (hqi : q i = LinearEquiv.cast (R := k) (M := S.V) hi.symm (S.toDual (c i) (p i))) + (hq : ∀ x (hx : x ≠ i), q x = LinearEquiv.cast (R := k) (M := S.V) (h x hx) (p x)) : + S.toDualAt i hi h (S.pure p) = S.pure q := by + simp only [toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, PiTensorProduct.congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp [hqi] + · simp [hx, hq x hx] + +/-- Raising at a slot undoes lowering at that slot, `(T^μ_ν)^ν = T^μν`. -/ +lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) + (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + S.toDualAt (c := c') (c' := c) i (by rw [hi]) (fun x hx => (h x hx).symm) + (S.toDualAt i hi h t) = t := by + induction t using inductionOn_pure with + | h p => + simp only [toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, PiTensorProduct.congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp only [dite_true, LinearEquiv.trans_apply] + generalize_proofs _ h₁ h₂ + revert h₁ h₂ hi + generalize c' x = d + rintro h₁ h₂ rfl + exact (S.toDual _).symm_apply_apply (p x) + · simp [hx] + | hsmul r t ht => rw [map_smul, map_smul, ht] + | hadd x y hx hy => rw [map_add, map_add, hx, hy] + +/-- Lowering or raising the second index of a tensor on two slots, `M^μν ↦ M^μ_ν`, is `toDual` on +the second factor of `M ∈ V a ⊗ V c`. -/ +lemma toDualAt_twoSlotEquiv (a c : Color K) (M : S.V a ⊗[k] S.V c) : + S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 rfl + (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = + S.twoSlotEquiv a c.dual ((S.toDual c).toLinearMap.lTensor _ M) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, hM, hM'] + | tmul x y => + rw [twoSlotEquiv_tmul, LinearMap.lTensor_tmul, twoSlotEquiv_tmul] + exact toDualAt_pure _ _ _ _ _ rfl + (fun x hx => by fin_cases x <;> first | rfl | exact absurd rfl hx) + +/-! + +## D. The form as a tensor + +-/ + +variable (S) in +/-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a +lower one. -/ +noncomputable def formTensor (c : Color K) : S.Tensor ![c, c] := + S.twoSlotEquiv c c (form c) + +/-- The form is an invariant tensor: every group element fixes it, as every Lorentz +transformation fixes the Minkowski metric `η`. -/ +lemma formTensor_invariant (c : Color K) (Λ : G) : Λ • S.formTensor c = S.formTensor c := by + rw [formTensor, ← twoSlotEquiv_map_rep, form_invariant] + +/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`: the second slot of the form at `c` contracted with +the first slot of the form at `c.dual` is the delta. -/ +lemma contr_formTensor_formTensor (c : Color K) : + Tensor.contr (ColorEquiv.matMul c c c.dual) + (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual)) = + S.deltaTensor c.dual := by + rw [formTensor, formTensor, deltaTensor, Tensor.contr_matMul] + congr 1 + -- Both tensors in `V c ⊗ V c.dual` act on `V c.dual` as the identity, the first by the law of + -- the form and the second by the snake law. + apply S.pairingToLinearMap_contr_injective c.dual + rw [pairingToLinearMap_lTensor, S.snake c.dual] + exact form_inverse c.dual + +/-- Lowering the second index is contracting it with the form, `M^μν g_νρ = M^μ_ρ` at an upper +color `c`, along `ColorEquiv.matMul`. -/ +lemma toDualAt_eq_contr_formTensor (a c : Color K) (M : S.V a ⊗[k] S.V c) : + S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 rfl + (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = + Tensor.contr (ColorEquiv.matMul a c c.dual) + (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.formTensor c.dual)) := by + rw [toDualAt_twoSlotEquiv, formTensor, Tensor.contr_matMul] + rfl + +end Species + +end Tensors From b6fc1823cf9b735240c9d85e32d6268cd6c8431f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:24 +0000 Subject: [PATCH 008/242] feat(Tensors): ConjKind, ConjSpecies, HasConjugation and Tensor.conj, with conj_conj, conj_smul and conj_contr Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Conjugation.lean | 257 +++++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Conjugation.lean diff --git a/Physlib.lean b/Physlib.lean index a7b82aaaa2..99f163c8da 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -197,6 +197,7 @@ public import Physlib.Mathematics.Modules.CrossProductMatrix public import Physlib.Mathematics.SpecialFunctions.EllipticIntegral public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color +public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean new file mode 100644 index 0000000000..0c11b64dc5 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -0,0 +1,257 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Contraction +/-! + +# Complex conjugation of tensors + +The conjugate of a tensor conjugates its components and moves each index to the conjugate color. + +## i. Overview + +Complex conjugation takes a left-handed Weyl spinor `ψ_α` to a right-handed one `ψ̄_α̇`, and a +Lorentz vector `v^μ` to the Lorentz vector `(v^μ)*`. It changes the kind of an index and keeps its +variance. It is not duality. For `SL(2, ℂ)` the dotted index of `ψ̄_α̇` is not the lowered +undotted index of `ψ_α`. So `bar` and `dual` are separate operations, and they commute. + +A kind `κ : ConjKind O isReal` has an orbit `o : O`, which is the kind together with its +conjugate. The orbit fixes the basis labels. An orbit is real or complex, as `isReal o` says. A +real orbit has one kind, its own conjugate. A complex orbit has two kinds, conjugate to each other. +The flags `left` and `right` of a kind say which it is, and the conjugate kind `κ.bar` swaps them. +The conjugate color `c.bar` has the kind `c.kind.bar` and the variance of `c`. Because `bar` swaps +two flags, `c.bar.bar = c` (`Color.bar_bar`) and `c.dual.bar = c.bar.dual` (`Color.bar_dual`) hold +by `rfl` for every color, so a statement that conjugates twice needs no cast. + +For example, a species of complex Lorentz tensors, over `ℂ` with `G = SL(2, ℂ)`, would have the +complex orbit `weyl`, the real orbit `vec`, and three kinds. The left-handed kind is +`⟨weyl, true, false, _⟩`, its conjugate is the right-handed kind `⟨weyl, false, true, _⟩`, and the +vector kind `⟨vec, true, true, _⟩` is its own conjugate. The last field `_` is the proof of +`valid`. The space `S.V ⟨⟨vec, true, true, _⟩, up⟩ = ℂ⁴` would hold the vectors `v^μ`, with +basis labels `I vec = Fin 4`, and both Weyl kinds would have the basis labels `I weyl = Fin 2`. +This file defines no such species. + +In a conjugate species `S : ConjSpecies k G O isReal I` the basis labels `I o` of a color depend +only on the orbit `o` of its kind, so `c` and `c.bar` have the same basis labels by `rfl`. The +scalars `k` have a conjugation `star`, as `ℂ` does. The conjugate `S.conjEquiv c v` of a vector +`v` of color `c` is the vector `v̄` of color `c.bar` with components `(v̄)^μ = (v^μ)*` in the bases +of the species. The conjugate `Tensor.conj T` of a tensor `T` is the tensor `T̄` with components +`(T̄)^μν = (T^μν)*` and every index moved to the conjugate color. Both are conjugate-linear. +`Tensor.conj` is an involution (`Tensor.conj_conj`) and conjugates each factor of a pure tensor +(`Tensor.conj_pure`). + +The class `Species.HasConjugation` states two laws. Conjugation commutes with the action `Λ` of +each `g : G`, `(Λv)‾ = Λ v̄`, so the representation at `c.bar` is the conjugate of the one at `c`. +Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. On tensors it follows that +conjugation commutes with the group action (`Tensor.conj_smul`) and with contraction +(`Tensor.conj_contr`). + +## ii. Key results + +- `ConjKind` : a kind of index, its own conjugate on a real orbit, or one of two conjugate kinds + on a complex orbit. +- `Color.bar` : the conjugate color, with `Color.bar_bar` and `Color.bar_dual` by `rfl`. +- `ConjSpecies` : a species on conjugate kinds, with basis labels fixed by the orbit. +- `Species.conjEquiv` : the conjugate of a vector, `(v̄)^μ = (v^μ)*`. +- `Species.HasConjugation` : conjugation commutes with the group action and preserves the + contraction. +- `Species.Tensor.conj` : the conjugate of a tensor, `(T̄)^μν = (T^μν)*`, with + `Species.Tensor.conj_conj`, `Species.Tensor.conj_pure`, `Species.Tensor.conj_smul` and + `Species.Tensor.conj_contr`. + +## iii. Table of contents + +- A. Conjugate kinds and colors +- B. Conjugate species and the conjugation of vectors +- C. Species with a conjugation +- D. Conjugation of tensors + - D.1. The group action and contraction + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +/-! + +## A. Conjugate kinds and colors + +-/ + +/-- A kind of index with a conjugate. Its orbit is the kind together with its conjugate and fixes +the basis labels. A real orbit (`isReal o = true`) has one kind, its own conjugate, such as the +Lorentz vector index. A complex orbit has two kinds, conjugate to each other, such as the indices +of `ψ_α` and `ψ̄_α̇`. A real kind has both flags `left` and `right`, and a kind of a complex orbit +has one of them. -/ +structure ConjKind (O : Type) (isReal : O → Bool) where + /-- The orbit, shared with the conjugate kind. It fixes the basis labels. -/ + orbit : O + /-- The kind is left-handed, as `ψ_α`, or real. -/ + left : Bool + /-- The kind is right-handed, as `ψ̄_α̇`, or real. -/ + right : Bool + /-- A real orbit has only the kind with both flags, and a complex orbit only the two kinds with + one flag each. -/ + valid : if isReal orbit then left ∧ right else left ≠ right + +variable {O : Type} {isReal : O → Bool} + +/-- The conjugate kind, with `left` and `right` swapped and the same orbit. -/ +abbrev ConjKind.bar (κ : ConjKind O isReal) : ConjKind O isReal := + ⟨κ.orbit, κ.right, κ.left, by simpa [and_comm, ne_comm] using κ.valid⟩ + +/-- The conjugate color, with the conjugate kind and the same variance, as in `ψ_α ↦ ψ̄_α̇`. -/ +abbrev Color.bar (c : Color (ConjKind O isReal)) : Color (ConjKind O isReal) := + ⟨c.kind.bar, c.variance⟩ + +/-- Conjugating twice gives back the color, by `rfl`. -/ +lemma Color.bar_bar (c : Color (ConjKind O isReal)) : c.bar.bar = c := rfl + +/-- Conjugation commutes with duality, by `rfl`. -/ +lemma Color.bar_dual (c : Color (ConjKind O isReal)) : c.dual.bar = c.bar.dual := rfl + +/-! + +## B. Conjugate species and the conjugation of vectors + +-/ + +/-- A species on conjugate kinds whose basis labels `I o` depend only on the orbit `o`, so that +`c` and `c.bar` have the same basis labels by `rfl`. A species declared with this type keeps `I` +visible, which lets the lemmas below find the labels. -/ +abbrev ConjSpecies (k : Type) [CommRing k] (G : Type) [Group G] (O : Type) (isReal : O → Bool) + (I : O → Type) [∀ o, Fintype (I o)] [∀ o, DecidableEq (I o)] : Type 1 := + Species k G (ConjKind O isReal) fun κ => I κ.orbit + +namespace Species + +variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {I : O → Type} + [∀ o, Fintype (I o)] [∀ o, DecidableEq (I o)] {S : ConjSpecies k G O isReal I} + +variable (S) in +/-- The conjugate `v̄` of a vector `v` of color `c`, the vector of color `c.bar` with components +`(v̄)^μ = (v^μ)*`. It is conjugate-linear. The components are taken in the bases of the species, +so the conjugate of the basis vector `e_μ` of `c` is the basis vector `e_μ` of `c.bar` +(`conjEquiv_basis`). The laws of `HasConjugation` therefore constrain the bases at `c` and `c.bar` +together. -/ +noncomputable def conjEquiv (c : Color (ConjKind O isReal)) : S.V c ≃ₗ⋆[k] S.V c.bar := + (S.basis c).repr.trans <| (Finsupp.mapRange.linearEquiv (starLinearEquiv k)).trans + (S.basis c.bar).repr.symm + +/-- The components of the conjugate are the conjugates of the components, `(v̄)^μ = (v^μ)*`. -/ +@[simp] +lemma basis_repr_conjEquiv (c : Color (ConjKind O isReal)) (v : S.V c) (μ : I c.kind.orbit) : + (S.basis c.bar).repr (S.conjEquiv c v) μ = star ((S.basis c).repr v μ) := by + simp [conjEquiv] + +/-- The conjugate of the basis vector `e_μ` of `c` is the basis vector `e_μ` of `c.bar`. -/ +@[simp] +lemma conjEquiv_basis (c : Color (ConjKind O isReal)) (μ : I c.kind.orbit) : + S.conjEquiv c (S.basis c μ) = S.basis c.bar μ := by + simp [conjEquiv, Finsupp.mapRange_single] + +/-! + +## C. Species with a conjugation + +-/ + +variable (S) in +/-- A conjugate species has a conjugation when conjugating components commutes with the action +`Λ` of each `g : G`, `(Λv)‾ = Λ v̄`, and preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. Then +the representation at `c.bar` is the conjugate of the one at `c`. At a real kind, its own +conjugate, the matrices `Λ^μ_ν` are therefore real. -/ +class HasConjugation : Prop where + /-- Conjugation commutes with the group action, `(Λv)‾ = Λ v̄`. -/ + conjEquiv_rep (c : Color (ConjKind O isReal)) (g : G) (v : S.V c) : + S.conjEquiv c (S.rep c g v) = S.rep c.bar g (S.conjEquiv c v) + /-- Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`, for `u` of color `c` and + `α` of color `c.dual`. -/ + star_contr (c : Color (ConjKind O isReal)) (u : S.V c) (α : S.V c.dual) : + star (S.contr c (u ⊗ₜ[k] α)) = S.contr c.bar (S.conjEquiv c u ⊗ₜ[k] S.conjEquiv c.dual α) + +/-! + +## D. Conjugation of tensors + +-/ + +namespace Tensor + +variable {ι : Type} [Fintype ι] {c : ι → Color (ConjKind O isReal)} + +/-- The conjugate `T̄` of a tensor `T`, with components `(T̄)^μν = (T^μν)*` and every index moved +to the conjugate color. It is conjugate-linear. -/ +noncomputable def conj : S.Tensor c ≃ₗ⋆[k] S.Tensor fun i => (c i).bar := + (S.tensorBasis c).repr.trans <| (Finsupp.mapRange.linearEquiv (starLinearEquiv k)).trans + (S.tensorBasis fun i => (c i).bar).repr.symm + +/-- The components of the conjugate are the conjugates of the components, `(T̄)^μν = (T^μν)*`. -/ +@[simp] +lemma tensorBasis_repr_conj (t : S.Tensor c) (μ : Π i, I (c i).kind.orbit) : + (S.tensorBasis _).repr (conj t) μ = star ((S.tensorBasis c).repr t μ) := by + simp [conj] + +/-- Conjugating twice gives back the tensor. No relabeling is needed, since `c.bar.bar = c` holds +by `rfl`. -/ +lemma conj_conj (t : S.Tensor c) : conj (conj t) = t := + (S.tensorBasis c).repr.injective <| Finsupp.ext fun μ => + (tensorBasis_repr_conj (conj t) μ).trans (by rw [tensorBasis_repr_conj, star_star]) + +/-- The conjugate of a pure tensor conjugates each factor, `(u ⊗ α)‾ = ū ⊗ ᾱ`. -/ +@[simp] +lemma conj_pure (f : Π i, S.V (c i)) : + conj (S.pure f) = S.pure fun i => S.conjEquiv (c i) (f i) := + (S.tensorBasis _).repr.injective <| Finsupp.ext fun μ => by + simp [tensorBasis_repr_pure, star_prod] + +/-! + +### D.1. The group action and contraction + +-/ + +variable [S.HasConjugation] + +/-- Conjugation commutes with the action `Λ` of `g : G`, `(ΛT)‾ = Λ T̄`. -/ +lemma conj_smul (g : G) (t : S.Tensor c) : conj (g • t) = g • conj t := + map_smul_of_pure conj g (fun f => by simp [HasConjugation.conjEquiv_rep]) t + +variable {κ J : Type} [Fintype κ] [Fintype J] {c' : κ → Color (ConjKind O isReal)} + {a : J → Color (ConjKind O isReal)} + +/-- Conjugating a contraction is contracting the conjugate along the same pairs of slots, +`(T^ρμ_μ)‾ = T̄^ρμ_μ`. The relabeling of `T̄` is `σ` with its colors conjugated. -/ +lemma conj_contr (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) : + conj (contr σ t) = contr (c' := fun x => (c' x).bar) (a := fun j => (a j).bar) + -- The colors are checked slot by slot: the conjugates of `contrColors c' a` and the + -- `contrColors` of the conjugates agree at each slot but not as functions. + ⟨σ.toEquiv, by rintro (_ | _ | _) <;> exact congrArg Color.bar (σ.color_eq _)⟩ (conj t) := by + -- Conjugating a vector commutes with moving it along an equality of colors. + have hcast : ∀ {b b' : Color (ConjKind O isReal)} (p : b = b') (x : S.V b), + S.conjEquiv b' (LinearEquiv.cast (R := k) (M := S.V) p x) = + LinearEquiv.cast (R := k) (M := S.V) (congrArg Color.bar p) (S.conjEquiv b x) := by + intro b b' p x + subst p + rfl + induction t using inductionOn_pure with + | hsmul r t ht => rw [map_smul, LinearEquiv.map_smulₛₗ, ht, LinearEquiv.map_smulₛₗ, map_smul] + | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] + | h f => + simp only [contr_pure, conj_pure, LinearEquiv.map_smulₛₗ, starRingEnd_apply, star_prod, + HasConjugation.star_contr, hcast] + +end Tensor + +end Species + +end Tensors From ba39abc9e1397ee99cd19a6514dbc0c1d8acda96 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:30 +0000 Subject: [PATCH 009/242] =?UTF-8?q?feat(Tensors):=20index=20notation=20{T?= =?UTF-8?q?=20|=20=CE=BC=20=CE=BD=20=E2=8A=97=20U=20|=20=CE=BD=20=CF=81}?= =?UTF-8?q?=E1=B5=80,=20one=20Tensor.contr=20per=20product=20chain,=20?= =?UTF-8?q?=E2=86=93=CE=BC=20and=20=E2=86=91=CE=BC=20by=20toDualAt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Elab.lean | 503 ++++++++++++++++++++++++++ 2 files changed, 504 insertions(+) create mode 100644 Physlib/Mathematics/Tensors/Elab.lean diff --git a/Physlib.lean b/Physlib.lean index 99f163c8da..5743ad2632 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -199,6 +199,7 @@ public import Physlib.Mathematics.SpecialFunctions.PhysHermite public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction +public import Physlib.Mathematics.Tensors.Elab public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean new file mode 100644 index 0000000000..0e85791ec3 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -0,0 +1,503 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Metric +/-! + +# Index notation + +Tensor expressions with named indices, such as `{T | μ ν ⊗ U | ν ρ}ᵀ` for `T^μ_ν U^ν_ρ`. + +## i. Overview + +Take the species `S` of real Lorentz tensors in `3 + 1` dimensions. An upper index has the color +`⟨vec, up⟩`, whose space `S.V ⟨vec, up⟩ = ℝ⁴` holds the vectors `v^μ`, and a lower index has the +color `⟨vec, down⟩`. +A tensor `T` with colors `![⟨vec, up⟩, ⟨vec, down⟩]` has the components `T^μ_ν`. In index notation +it is written `T | μ ν`, with one name per slot. The heights of the indices are not written, since +the colors of `T` fix them. + +An expression `{…}ᵀ` is a tensor, an element of `S.Tensor ![…]`, or a proposition if it is an +equation. For a second tensor `U` with the same colors as `T`, `{T | μ ν ⊗ U | ν ρ}ᵀ` is the tensor +`T^μ_ν U^ν_ρ`, with colors `![⟨vec, up⟩, ⟨vec, down⟩]`. For an upper color `c`, the forms +`S.formTensor c = g^μν` and `S.formTensor c.dual = g_μν` are inverse, `g^μν g_νρ = δ^μ_ρ`: + + `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`. + +This is `contr_formTensor_formTensor`, whose relabeling `ColorEquiv.matMul` agrees slot by slot with +the one the notation generates (section C). + +The names are bound only inside the braces. The notation elaborates to the operations of a species. + +- `A ⊗ B` is the product `S.tmulEquiv _ _ (A ⊗ₜ B)`. +- In a product, every name written twice is contracted, all pairs in one `Tensor.contr σ`. The + relabeling `σ` is generated from the names, and its color proof checks that the two slots of + each pair have dual colors. The first occurrence of a name is the first argument of `S.contr`. +- The remaining slots are numbered `0, 1, …` in the order written, so the result is a tensor on + `Fin m` with a literal list of colors `![…]`. +- `A + B` and `A = B` relabel `B` by `S.reindex` when its names come in another order. Each name + must have the same color on both sides. +- `T | ↓μ` lowers the upper index at that slot and `T | ↑μ` raises a lower one, by `S.toDualAt`. + For a vector `v`, `{v | ↓ν}ᵀ` is `v_ν = v^μ g_μν`. This needs an instance `S.WithInvariantForm`. + The arrow is checked against the color of the slot, so its variance must be visible, as in + `⟨κ, .up⟩` for a kind `κ`. +- A scalar `r : k`, a group element `Λ : G` and negation are ordinary terms in front of `|`, as in + `r • T | μ ν`, `Λ • T | μ ν` and `-T | μ ν`. A compound expression is scaled by nesting it, + `r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ`. + +An expression in parentheses, each side of a sum, and a nested `{…}ᵀ` are contracted first, before +they enter an enclosing product. The names they contract are free again outside: +`{(T | μ μ) ⊗ U | μ ρ}ᵀ` is `(T^λ_λ) U^μ_ρ`, and `{(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ` is +`(T^λ_λ + U^λ_λ) T^μ_ν`. Within one product chain a name may appear at most twice. + +The slots of each tensor must be labeled by a type built from `Fin n` and `⊕` with a literal `n`. +Its colors must be a literal list `![a, b, …]` or `prodColors` of such lists. A tensor with a +variable family of colors cannot be written, so general lemmas are stated without the notation. +A tensor with no slots is written `s |` with a space after the bar, since Mathlib reads `|` +followed directly by a term as an absolute value `|x|`. An equation with such a tensor can also be +written outside the braces, `{T | μ μ}ᵀ = s`. + +## ii. Key results + +- `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. + +## iii. Table of contents + +- A. Syntax +- B. Elaboration + - B.1. Slot types + - B.2. The steps of an expression + - B.3. The elaborator +- C. Examples + +## iv. References + +-/ + +public meta section + +open Lean Meta Elab Term + +namespace Tensors.Notation + +/-! + +## A. Syntax + +-/ + +/-- An index of a tensor in index notation. -/ +declare_syntax_cat tensorIndex +/-- A named index. -/ +syntax ident : tensorIndex +/-- A named upper index lowered with the invariant form, as `v_μ` from `v^μ`. -/ +syntax "↓" ident : tensorIndex +/-- A named lower index raised with the invariant form, as `α^μ` from `α_μ`. -/ +syntax "↑" ident : tensorIndex + +/-- A tensor expression in index notation. -/ +declare_syntax_cat tensorIndexExpr +/-- A tensor with a name on each slot. -/ +syntax term "|" (ppSpace tensorIndex)* : tensorIndexExpr +/-- An equation. -/ +syntax:40 tensorIndexExpr "=" tensorIndexExpr:41 : tensorIndexExpr +/-- A product. -/ +syntax:70 tensorIndexExpr "⊗" tensorIndexExpr:71 : tensorIndexExpr +/-- A sum. -/ +syntax:65 tensorIndexExpr "+" tensorIndexExpr:66 : tensorIndexExpr +/-- Parentheses. -/ +syntax "(" tensorIndexExpr ")" : tensorIndexExpr + +/-- Index notation. -/ +syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term + +/-! + +## B. Elaboration + +### B.1. Slot types + +-/ + +/-- A slot type built from `Fin n`, `Unit` and `⊕`. -/ +inductive Slots where + /-- `Fin n`. -/ + | fin (n : Nat) + /-- `Unit`. -/ + | unit + /-- `a ⊕ b`. -/ + | sum (a b : Slots) +deriving BEq + +/-- A slot of a slot type: the path of `Sum.inl` (`false`) and `Sum.inr` (`true`) to a leaf, and +the position in that leaf. -/ +abbrev Addr := List Bool × Nat + +/-- The slots of a slot type, in order. -/ +def Slots.addrs : Slots → List Addr + | .fin n => (List.range n).map (([], ·)) + | .unit => [([], 0)] + | .sum a b => a.addrs.map (fun (p, i) => (false :: p, i)) ++ + b.addrs.map (fun (p, i) => (true :: p, i)) + +/-- The slot type as a term. -/ +def Slots.type : Slots → TermElabM Term + | .fin n => `(Fin $(quote n)) + | .unit => `(Unit) + | .sum a b => do `($(← a.type) ⊕ $(← b.type)) + +/-- A slot as a term. -/ +def Slots.elem : Slots → Addr → TermElabM Term + | .fin n, (_, i) => `(($(quote i) : Fin $(quote n))) + | .unit, _ => `(()) + | .sum a _, (false :: p, i) => do `(Sum.inl $(← a.elem (p, i))) + | .sum _ b, (true :: p, i) => do `(Sum.inr $(← b.elem (p, i))) + | .sum _ _, ([], _) => throwError "index notation: bad slot" + +/-- The function on a slot type with the values `img`, as vector literals on `Fin n`, constants on +`Unit`, and `comb` on `⊕`. -/ +def Slots.fn (comb : Name) (img : Addr → TermElabM Term) : Slots → TermElabM Term + | .fin n => do `(![$((← (List.range n).mapM fun i => img ([], i)).toArray),*]) + | .unit => do `(fun _ : Unit => $(← img ([], 0))) + | .sum a b => do + `($(mkIdent comb) $(← a.fn comb fun (p, i) => img (false :: p, i)) + $(← b.fn comb fun (p, i) => img (true :: p, i))) + +/-- The slot type of a type built from `Fin n` and `⊕` with a literal `n`. -/ +partial def Slots.ofType? (ι : Expr) : MetaM (Option Slots) := do + let ι ← whnfR ι + if ι.isAppOfArity ``Fin 1 then + return (← (evalNat ι.appArg!).run).map .fin + else if ι.isAppOfArity ``Sum 2 then + let some a ← Slots.ofType? (ι.getArg! 0) | return none + let some b ← Slots.ofType? (ι.getArg! 1) | return none + return some (.sum a b) + else return none + +/-- The bijection from `dom` to `cod` sending the `i`-th slot of `dom` to `img[i]`, with its +inverse laws checked slot by slot by `fin_cases`. `decide` would need a `DecidableEq` instance on +the whole slot type, which exceeds the instance size limit from four contracted pairs on. -/ +def slotEquiv (dom cod : Slots) (img : List Addr) : TermElabM Term := do + let table := dom.addrs.zip img + let toFun ← dom.fn ``Sum.elim fun x => match table.find? (·.1 == x) with + | some (_, y) => cod.elem y + | none => throwError "index notation: a slot has no image" + let invFun ← cod.fn ``Sum.elim fun y => match table.find? (·.2 == y) with + | some (x, _) => dom.elem x + | none => throwError "index notation: a slot has no preimage" + `(({ toFun := $toFun, invFun := $invFun, + left_inv := (by intro x; fin_cases x <;> rfl), + right_inv := (by intro x; fin_cases x <;> rfl) } : + $(← dom.type) ≃ $(← cod.type))) + +/-- The entries of a literal color family `![a, b, …]`, or of `prodColors` of literal families. -/ +partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do + if c.isAppOfArity ``Tensors.prodColors 5 then + return (· ++ ·) <$> (← colorList? (c.getArg! 3)) <*> (← colorList? (c.getArg! 4)) + let (cs, _, tail) ← Matrix.matchVecConsPrefix (mkNatLit 0) c + return if tail.isAppOfArity ``Matrix.vecEmpty 1 then some cs else none + +/-- The literal color family `![a, b, …]`. -/ +def colorsTerm (cs : List Expr) : TermElabM Term := do + `(![$((← cs.toArray.mapM exprToSyntax)),*]) + +/-! + +### B.2. The steps of an expression + +-/ + +/-- A partly elaborated expression: the name and color of each slot, the slot type, the tensor +and its species. -/ +structure Node where + /-- The index name of each slot. -/ + names : List Name + /-- The color of each slot. -/ + colors : List Expr + /-- The slot type. -/ + slots : Slots + /-- The tensor. -/ + tm : Term + /-- The species. -/ + species : Expr + +/-- The expression relabeled to `Fin n`, its slots numbered in order. -/ +def Node.toFin (a : Node) : TermElabM Node := do + let n := a.names.length + if a.slots == .fin n then return a + let σ ← slotEquiv a.slots (.fin n) ((List.range n).map ([], ·)) + let tm ← `(Tensors.Species.reindex $(← exprToSyntax a.species) + (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm a.colors)) $σ) $a.tm) + return { a with slots := .fin n, tm } + +/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ` and raised at each `↑μ`. -/ +def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do + let e ← instantiateMVars (← elabTerm T none) + if e.hasSyntheticSorry then throwAbortTerm + let ty ← whnfR (← instantiateMVars (← inferType e)) + unless ty.isAppOf ``Tensors.Species.Tensor do + throwError "index notation: {T} is not a tensor" + let args := ty.getAppArgs + let some slots ← Slots.ofType? args[args.size - 2]! + | throwError "index notation: the slots of {T} must be `Fin n` and `⊕`" + let some colors ← colorList? args[args.size - 1]! + | throwError "index notation: the colors of {T} must be a literal list" + unless colors.length = idxs.size do + throwError "index notation: {T} has {colors.length} slots, {idxs.size} indices given" + -- Each index with whether its slot moves to the dual color, after checking the arrow. + let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => do + let isUp ← whnfD (← mkAppM ``Tensors.Variance.isUp #[← mkAppM ``Tensors.Color.variance #[col]]) + let check (x : Ident) (up : Bool) (arrow : String) : TermElabM (Name × Bool) := do + unless isUp.isConstOf (if up then ``Bool.true else ``Bool.false) do + throwError "index notation: {arrow}{x.getId} needs a visibly \ + {if up then "upper" else "lower"} index" + pure (x.getId, true) + match i with + | `(tensorIndex| $x:ident) => pure (x.getId, false) + | `(tensorIndex| ↓$x:ident) => check x true "↓" + | `(tensorIndex| ↑$x:ident) => check x false "↑" + | _ => throwError "index notation: unsupported index {i}" + let mut a : Node := ⟨kinds.map (·.1), colors, slots, ← exprToSyntax e, args[args.size - 3]!⟩ + if kinds.all (!·.2) then return a + a ← a.toFin + let n := colors.length + for ((_, d), i) in kinds.zipIdx do + if d then + let colors := a.colors.set i (← mkAppM ``Tensors.Color.dual #[a.colors[i]!]) + -- The proof at `i` is `id rfl`, of type `c' i = (c i).dual` as in the lemmas on `toDualAt`. + -- A bare `rfl` has the type `c' i = c' i`, which `rw` matches only by unfolding `![…] i`. + -- The new colors agree with the old ones away from `i`, by cases on the slot. + let tm ← `(Tensors.Species.toDualAt $(← exprToSyntax a.species) + (c' := $(← colorsTerm colors)) ($(quote i) : Fin $(quote n)) (id rfl) + (by intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) $a.tm) + a := { a with colors, tm } + return a + +/-- Contracts every repeated name in one `Tensor.contr`, and numbers the remaining slots in the +order written. -/ +def contract (a : Node) : TermElabM Node := do + let idx := a.names.zipIdx + let mut free : List Nat := [] + let mut pairs : List (Nat × Nat) := [] + for (nm, i) in idx do + match idx.filter (·.1 == nm) with + | [_] => free := free ++ [i] + | [(_, i₁), (_, i₂)] => + if i == i₁ then + unless ← isDefEq a.colors[i₂]! (← mkAppM ``Tensors.Color.dual #[a.colors[i₁]!]) do + throwError "index notation: {nm} joins the colors {a.colors[i₁]!} and \ + {a.colors[i₂]!}, which are not dual" + pairs := pairs ++ [(i₁, i₂)] + | _ => throwError "index notation: {nm} appears more than twice" + if pairs.isEmpty then return ← a.toFin + let m := free.length + -- The pairs are labeled by `Unit` for one pair, as in `ColorEquiv.matMul`, and by + -- `(… ⊕ Unit) ⊕ Unit` for more, as `ColorEquiv.chain` combines two contractions in sequence. + let J := (List.range (pairs.length - 1)).foldl (fun J _ => Slots.sum J .unit) .unit + -- A remaining slot goes to `inl k`, the pair `j` to `inr (inl j)` and `inr (inr j)`. + let img : List Addr := (List.range a.names.length).map fun i => + match free.idxOf? i, pairs.findIdx? (·.1 == i), pairs.findIdx? (·.2 == i) with + | some k, _, _ => ([false], k) + | _, some j, _ => (true :: false :: (J.addrs[j]!).1, 0) + | _, _, some j => (true :: true :: (J.addrs[j]!).1, 0) + | _, _, _ => ([], 0) + let σ ← slotEquiv a.slots (.sum (.fin m) (.sum J J)) img + let pairColors ← J.fn ``Tensors.prodColors fun (p, _) => + exprToSyntax a.colors[(pairs[J.addrs.idxOf (p, 0)]!).1]! + let tm ← `(Tensors.Species.Tensor.contr (S := $(← exprToSyntax a.species)) + (Tensors.ColorEquiv.mk + (c₂ := Tensors.contrColors $(← colorsTerm (free.map (a.colors[·]!))) $pairColors) $σ) + $a.tm) + return ⟨free.map (a.names[·]!), free.map (a.colors[·]!), .fin m, tm, a.species⟩ + +/-- The right side relabeled to the order of the left side's names, after checking that each name +has the same color on both sides. -/ +def reorder (l r : Node) : TermElabM Term := do + unless l.names.length == r.names.length && l.names.all (r.names.contains ·) do + throwError "index notation: the two sides have different free indices" + for (nm, c) in r.names.zip r.colors do + let c' := l.colors[l.names.idxOf nm]! + unless ← isDefEq c c' do + throwError "index notation: {nm} has the color {c'} on the left and {c} on the right" + if l.names == r.names then return r.tm + let m := l.names.length + let σ ← slotEquiv (.fin m) (.fin m) (r.names.map fun nm => ([], l.names.idxOf nm)) + `(Tensors.Species.reindex $(← exprToSyntax l.species) + (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm l.colors)) $σ) $r.tm) + +/-! + +### B.3. The elaborator + +-/ + +mutual + +/-- An expression before its outer contraction. A product chain `A ⊗ B` is +`S.tmulEquiv _ _ (A ⊗ₜ B)` with its repeated names not yet contracted. An expression in +parentheses and each side of a sum are contracted first. -/ +partial def uncontracted (stx : Syntax) : TermElabM Node := do + match stx with + | `(tensorIndexExpr| $T:term | $ids:tensorIndex*) => leaf T ids + | `(tensorIndexExpr| $a:tensorIndexExpr ⊗ $b:tensorIndexExpr) => do + let a ← uncontracted a + let b ← uncontracted b + let tm ← `(Tensors.Species.tmulEquiv $(← exprToSyntax a.species) _ _ + (TensorProduct.tmul _ $a.tm $b.tm)) + return ⟨a.names ++ b.names, a.colors ++ b.colors, .sum a.slots b.slots, tm, a.species⟩ + | `(tensorIndexExpr| ($a:tensorIndexExpr)) => build a + | `(tensorIndexExpr| $a:tensorIndexExpr + $b:tensorIndexExpr) => do + let l ← build a + return { l with tm := ← `($l.tm + $(← reorder l (← build b))) } + | `(tensorIndexExpr| $_:tensorIndexExpr = $_:tensorIndexExpr) => + throwError "index notation: an equation is allowed only at the top" + | _ => throwError "index notation: unsupported expression {stx}" + +/-- An expression, contracted. -/ +partial def build (stx : Syntax) : TermElabM Node := do contract (← uncontracted stx) + +end + +/-- Elaborates index notation. An equation is allowed only at the top, possibly in parentheses. -/ +@[term_elab tensorIndexSyntax] def elabTensorIndex : TermElab := fun stx expectedType? => do + match stx with + | `({ ($e:tensorIndexExpr) }ᵀ) => elabTerm (← `({ $e:tensorIndexExpr }ᵀ)) expectedType? + | `({ $l:tensorIndexExpr = $r:tensorIndexExpr }ᵀ) => + let l ← build l + elabTerm (← `($l.tm = $(← reorder l (← build r)))) none + | `({ $e:tensorIndexExpr }ᵀ) => elabTerm (← build e).tm expectedType? + | _ => throwUnsupportedSyntax + +end Tensors.Notation + +end + +/-! + +## C. Examples + +-/ + +namespace Tensors.Notation + +open Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) + (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) + +/-- info: T : S.Tensor ![a, a.dual] -/ +#guard_msgs in +#check {T | μ ν}ᵀ + +/-- The matrix product `T^μ_ν U^ν_ρ`. -/ +noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ + +/-- A product written as a term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the +product written in the notation. -/ +example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl + +/-- An equation with a tensor `s` with no slots, `T^μ_μ = s`. The space after the bar keeps +Mathlib's absolute value `|x|` from reading it. -/ +example (s : S.Tensor (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl + +/-- An equation in parentheses is the same equation. -/ +example : {(T | μ ν = U | μ ν)}ᵀ ↔ {T | μ ν = U | μ ν}ᵀ := Iff.rfl + +/-- An equation whose right side lists its indices in the other order, `T^μ_ν = T'_ν^μ`. -/ +example : Prop := {T | μ ν = T' | ν μ}ᵀ + +/-- The trace of a product of four matrices, `T^μ_ν U^ν_ρ T^ρ_σ U^σ_μ`. -/ +noncomputable example : S.Tensor (![] : Fin 0 → Color K) := + {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ}ᵀ + +/-- The trace of a product of five matrices, `T^μ_ν U^ν_ρ W^ρ_σ T^σ_τ U^τ_μ`. -/ +noncomputable example : S.Tensor (![] : Fin 0 → Color K) := + {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ ⊗ T | σ τ ⊗ U | τ μ}ᵀ + +/-- Sums, negatives and scalar multiples, `-T^μ_ν + r U^μ_ν`. -/ +example : {-T | μ ν + r • U | μ ν}ᵀ = -T + r • U := rfl + +/-- A scalar multiple of a contraction, `r T^μ_ν U^ν_ρ`. -/ +example : {r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ}ᵀ = r • {T | μ ν ⊗ U | ν ρ}ᵀ := rfl + +/-- Each side of a sum is contracted first, so `μ` is free again in the product, +`(T^λ_λ + U^λ_λ) T^μ_ν`. -/ +example : {(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ = {({T | μ μ}ᵀ + {U | μ μ}ᵀ) | ⊗ T | μ ν}ᵀ := rfl + +-- The two sides of a sum or an equation must agree in the colors of each name. +/-- error: index notation: ν has the color a.dual on the left and a on the right -/ +#guard_msgs in +#check {T | μ ν = T | ν μ}ᵀ + +-- A term that is not a tensor is rejected. +/-- error: index notation: r is not a tensor -/ +#guard_msgs in +#check {r | μ}ᵀ + +/-- An expression in parentheses is contracted first, so `μ` is free again in the product, +`(T^λ_λ) U^μ_ρ`. -/ +example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ := rfl + +-- A name appearing three times in one product chain is rejected. +/-- error: index notation: μ appears more than twice -/ +#guard_msgs in +#check {T | μ μ ⊗ U | μ ρ}ᵀ + +-- A tensor whose colors are not a literal list is rejected. +/-- error: index notation: the colors of t must be a literal list -/ +#guard_msgs in +example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ + +-- A pair of slots whose colors are not dual is rejected. +/-- error: index notation: μ joins the colors a and a, which are not dual -/ +#guard_msgs in +#check {T | μ ν ⊗ T | μ ρ}ᵀ + +/-- Contracting `ν` and then `ρ` is contracting both at once, `(T U)^μ_ρ W^ρ_τ = T^μ_ν U^ν_ρ W^ρ_τ`, +by `tmulEquiv_contr` and `contr_contr`. The two choices of pairs agree slot by slot. -/ +example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U | ν ρ ⊗ W | ρ τ}ᵀ := by + rw [Tensor.tmulEquiv_contr, Tensor.contr_contr] + congr 2 + ext x + fin_cases x <;> rfl + +section Metric + +variable [S.WithInvariantForm] + +/-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ + +-- An arrow that does not match the variance of the slot is rejected. +/-- error: index notation: ↑μ needs a visibly lower index -/ +#guard_msgs in +#check {v | ↑μ}ᵀ + +/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`. The lemmas on `toDualAt` apply +by `rw` and `simp`. -/ +example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [toDualAt_smul] + +/-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by + simp only [toDualAt_smul] + +/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by `rw [Tensor.contr_smul]`. -/ +example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [Tensor.contr_smul] + +/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The notation generates the pairs of +`ColorEquiv.matMul` slot by slot. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + rw [← contr_formTensor_formTensor] + congr 2 + ext x + fin_cases x <;> rfl + +end Metric + +end Tensors.Notation From bedd51b0fa96a85e1d1da9204fad0323c545e57a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 1 Nov 2026 20:07:32 +0000 Subject: [PATCH 010/242] =?UTF-8?q?feat(Relativity):=20realLorentz=20speci?= =?UTF-8?q?es=20with=20the=20Minkowski=20metric,=20contr=5Fbasis,=20tensor?= =?UTF-8?q?Basis=5Frepr=5FformTensor=20and=20the=20scoped=20notation=20?= =?UTF-8?q?=E2=84=9DT[d,=20up,=20down]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../Tensors/RealTensor/Species.lean | 241 ++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 Physlib/Relativity/Tensors/RealTensor/Species.lean diff --git a/Physlib.lean b/Physlib.lean index 5743ad2632..7ceb53a630 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -555,6 +555,7 @@ public import Physlib.Relativity.Tensors.RealTensor.Matrix.Pre public import Physlib.Relativity.Tensors.RealTensor.Metrics.Basic public import Physlib.Relativity.Tensors.RealTensor.Metrics.Pre public import Physlib.Relativity.Tensors.RealTensor.Representation.Contraction +public import Physlib.Relativity.Tensors.RealTensor.Species public import Physlib.Relativity.Tensors.RealTensor.ToComplex public import Physlib.Relativity.Tensors.RealTensor.Units.Basic public import Physlib.Relativity.Tensors.RealTensor.Units.Pre diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean new file mode 100644 index 0000000000..64d7399423 --- /dev/null +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -0,0 +1,241 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Relativity.Tensors.RealTensor.Metrics.Pre +/-! + +# The species of real Lorentz tensors + +Real Lorentz tensors in `d + 1` dimensions as a species, with the Minkowski metric `η`. + +## i. Overview + +Real Lorentz tensors have one kind of index, the vector index `vec`. An upper index has the color +`⟨vec, up⟩` and a lower index the color `⟨vec, down⟩`. A color is a label. The species assigns to +it a space: `V ⟨vec, up⟩ = ContrMod d`, the contravariant vectors `v^μ` with basis `e_μ`, and +`V ⟨vec, down⟩ = CoMod d`, the covariant vectors `w_μ` with basis `e^μ`. Both are copies of +`ℝ^(d+1)`, so `V ⟨vec, up⟩ = ℝ⁴` for `d = 3`. The labels `μ` run over `Fin 1 ⊕ Fin d`, with +`Sum.inl 0` the time direction and `Sum.inr i` the space directions. A Lorentz transformation +`Λ : LorentzGroup d` acts on an upper index by `(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by +`(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named `realLorentz.up` and `realLorentz.down`, and +`ℝT[d, up, down]` is the space of the tensors `T^μ_ν`, under `open scoped Tensors.realLorentz`. + +The contraction `u^μ w_μ` is `ContrMod.contr` and `CoMod.contr`, the delta `δ_μ^ν` is +`ContrMod.unitVal` and `CoMod.unitVal`, and the metric is `η^μν = ContrMod.metricVal` and +`η_μν = CoMod.metricVal`, both with the matrix `diag(1, -1, …, -1)`. The laws of a species and of a +metric are `ContrMod.contr_tmul_symm`, `ContrMod.contr_unit` and `ContrMod.contr_metric` with their +`CoMod` versions, read through `TensorProduct.pairingToLinearMap_apply`. + +So the general results apply. For example `η^μν η_νρ = δ^μ_ρ` is +`Species.contr_formTensor_formTensor` at `up`, and lowering an index, `v^μ ↦ v_ν = v^μ η_μν`, is +`(realLorentz d).toDual up`. + +A variance stores whether it is up and whether it is down. The fields that pair a color with its +dual match on both booleans and list only the two consistent cases. Lean's `match` closes the other +two from `Variance.ne`. + +## ii. Key results + +- `realLorentz` : the species of real Lorentz tensors, with the colors `realLorentz.up` and + `realLorentz.down` and the spaces of tensors `ℝT[d, up, down]` (scoped). +- the instance `(realLorentz d).WithMetric` : the Minkowski metric `η`. +- `realLorentz.contr_basis` : the bases of upper and lower indices are dual. +- `realLorentz.tensorBasis_repr_formTensor` : the components of `η^μν` and `η_μν` are the + Minkowski matrix. + +## iii. Table of contents + +- A. The species +- B. The Minkowski metric +- C. Dual bases and components of the metric + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct Lorentz + +namespace Tensors + +/-! + +## A. The species + +-/ + +/-- The kinds of real Lorentz index. There is one, the vector index `vec`, carried up as `v^μ` and +down as `w_μ`. -/ +inductive realLorentz.Kind + /-- The Lorentz vector index. -/ + | vec + +/-- The vector space of a real Lorentz index of color `c`: `ContrMod d` for an upper index and +`CoMod d` for a lower one. A `def` with its own instances, because instance search does not find +the instances of a field written inline once a proof splits the color into cases. -/ +def realLorentz.IndexSpace (d : ℕ) (c : Color realLorentz.Kind) : Type := + match c.variance.isUp with + | true => ContrMod d + | false => CoMod d + +noncomputable instance (d : ℕ) : ∀ c, AddCommGroup (realLorentz.IndexSpace d c) + | ⟨_, ⟨true, _, _⟩⟩ => inferInstanceAs (AddCommGroup (ContrMod d)) + | ⟨_, ⟨false, _, _⟩⟩ => inferInstanceAs (AddCommGroup (CoMod d)) + +noncomputable instance (d : ℕ) : ∀ c, Module ℝ (realLorentz.IndexSpace d c) + | ⟨_, ⟨true, _, _⟩⟩ => inferInstanceAs (Module ℝ (ContrMod d)) + | ⟨_, ⟨false, _, _⟩⟩ => inferInstanceAs (Module ℝ (CoMod d)) + +open realLorentz in +/-- Real Lorentz tensors in `d + 1` dimensions, on `ContrMod d` and `CoMod d` with their +representations, bases, contraction and delta. The labels `Fin 1 ⊕ Fin d` put time first. At a +concrete color each field is the `ContrMod` or `CoMod` one by `rfl`, as in +`(realLorentz d).rep up = ContrMod.rep`. -/ +noncomputable abbrev realLorentz (d : ℕ) : + Species ℝ (LorentzGroup d) Kind (fun _ => Fin 1 ⊕ Fin d) where + V := IndexSpace d + rep + | ⟨_, ⟨true, _, _⟩⟩ => ContrMod.rep + | ⟨_, ⟨false, _, _⟩⟩ => CoMod.rep + basis + | ⟨_, ⟨true, _, _⟩⟩ => contrBasis d + | ⟨_, ⟨false, _, _⟩⟩ => coBasis d + contr + | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.contr + | ⟨_, ⟨false, true, _⟩⟩ => CoMod.contr + delta + | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.unitVal d + | ⟨_, ⟨false, true, _⟩⟩ => CoMod.unitVal d + contr_tmul_symm + | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.contr_tmul_symm + | ⟨_, ⟨false, true, _⟩⟩ => CoMod.contr_tmul_symm + snake + | ⟨_, ⟨true, false, _⟩⟩ => LinearMap.ext fun x => (pairingToLinearMap_apply _ _ x).trans + (ContrMod.unit_apply_one (d := d) ▸ ContrMod.contr_unit x) + | ⟨_, ⟨false, true, _⟩⟩ => LinearMap.ext fun x => (pairingToLinearMap_apply _ _ x).trans + (CoMod.unit_apply_one (d := d) ▸ CoMod.contr_unit x) + +namespace realLorentz + +/-- The color of an upper Lorentz index, `v^μ`. -/ +abbrev up : Color Kind := ⟨.vec, .up⟩ + +/-- The color of a lower Lorentz index, `w_μ`. -/ +abbrev down : Color Kind := ⟨.vec, .down⟩ + +/-- The real Lorentz tensors in `d + 1` dimensions with the colors listed, as `ℝT[d, up, down]` for +`T^μ_ν`. The colors are read in the namespace `realLorentz`. The notation is scoped: it is active +under `open scoped Tensors.realLorentz`, so it does not clash with `ℝT[…]` of +`RealTensor.Basic`. -/ +scoped syntax (name := realLorentzSyntax) "ℝT[" term,+ "]" : term + +macro_rules (kind := realLorentzSyntax) + | `(ℝT[$d]) => `((Tensors.realLorentz $d).Tensor ![]) + | `(ℝT[$d, $cs,*]) => `((Tensors.realLorentz $d).Tensor (open Tensors.realLorentz in ![$cs,*])) + +end realLorentz + +/-! + +## B. The Minkowski metric + +-/ + +open realLorentz in +/-- The Minkowski metric, `η^μν` at an upper color and `η_μν` at a lower one, both with the matrix +`diag(1, -1, …, -1)`: the signature is mostly minus. -/ +noncomputable instance (d : ℕ) : (realLorentz d).WithMetric where + form + | ⟨_, ⟨true, _, _⟩⟩ => ContrMod.metricVal d + | ⟨_, ⟨false, _, _⟩⟩ => CoMod.metricVal d + form_invariant c g := by + rcases c with ⟨_, _ | _, _, _⟩ + · have h := (LinearMap.congr_fun ((CoMod.metric d).isIntertwining' g) 1).symm + simp [CoMod.metric_apply_one] at h + exact h + · have h := (LinearMap.congr_fun ((ContrMod.metric d).isIntertwining' g) 1).symm + simp [ContrMod.metric_apply_one] at h + exact h + form_inverse c := by + -- `ContrMod.contr_metric` states `η^μν η_νρ = δ^μ_ρ` with associators, `lid` and `comm`, the + -- form of `lTensor_pairingToLinearMap_apply`. + rw [← pairingToLinearMap_lTensor, ← (realLorentz d).snake c, + (realLorentz d).delta_eq_comm_delta_dual c, lTensor_pairingToLinearMap_apply] + congr 1 + match c with + | ⟨_, ⟨true, false, _⟩⟩ => + have h := CoMod.contr_metric (d := d) + rw [ContrMod.metric_apply_one, CoMod.metric_apply_one, CoMod.unit_apply_one] at h + exact (TensorProduct.comm_comm _ _ _ _).symm.trans (congrArg (TensorProduct.comm ℝ _ _) h) + | ⟨_, ⟨false, true, _⟩⟩ => + have h := ContrMod.contr_metric (d := d) + rw [ContrMod.metric_apply_one, CoMod.metric_apply_one, ContrMod.unit_apply_one] at h + exact (TensorProduct.comm_comm _ _ _ _).symm.trans (congrArg (TensorProduct.comm ℝ _ _) h) + form_symm + | ⟨_, ⟨true, _, _⟩⟩ => (by simp [ContrMod.metricVal_expand_tmul_minkowskiMatrix] : + TensorProduct.comm ℝ _ _ (ContrMod.metricVal d) = _) + | ⟨_, ⟨false, _, _⟩⟩ => (by simp [CoMod.metricVal_expand_tmul_minkowskiMatrix] : + TensorProduct.comm ℝ _ _ (CoMod.metricVal d) = _) + +/-! + +## C. Dual bases and components of the metric + +-/ + +namespace realLorentz + +/-- The bases of an upper and a lower index are dual: `e_μ(e^ν)` is `1` for `μ = ν` and `0` +otherwise. At a lower color `c` the left factor is `e^μ` and the right `e_ν`. -/ +lemma contr_basis (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : + (realLorentz d).contr c ((realLorentz d).basis c μ ⊗ₜ (realLorentz d).basis c.dual ν) = + if μ = ν then 1 else 0 := by + match c with + | ⟨_, ⟨true, false, _⟩⟩ => exact ContrMod.contr_basis μ ν + | ⟨_, ⟨false, true, _⟩⟩ => exact CoMod.contr_basis μ ν + +/-- The components of the metric are the Minkowski matrix `diag(1, -1, …, -1)`: at the labels +`![α, β]` they are `η^αβ` at an upper color and `η_αβ` at a lower one. The labels are any +`μ : Fin 2 → Fin 1 ⊕ Fin d`, as in `Species.tensorBasis_repr_twoSlotEquiv`. -/ +lemma tensorBasis_repr_formTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis ![c, c]).repr ((realLorentz d).formTensor c) μ = + minkowskiMatrix (μ 0) (μ 1) := by + rw [Species.formTensor, Species.tensorBasis_repr_twoSlotEquiv] + rcases c with ⟨_, _ | _, _, _⟩ + · exact congrFun (congrFun (coCoToMatrixRe.apply_symm_apply minkowskiMatrix) (μ 0)) (μ 1) + · exact congrFun (congrFun (contrContrToMatrixRe.apply_symm_apply minkowskiMatrix) (μ 0)) (μ 1) + +/-- `η^00 = 1` and `η^11 = -1` in `3 + 1` dimensions: the signature is mostly minus. -/ +example : ((realLorentz 3).tensorBasis ![up, up]).repr ((realLorentz 3).formTensor up) + ![.inl 0, .inl 0] = 1 ∧ + ((realLorentz 3).tensorBasis ![up, up]).repr ((realLorentz 3).formTensor up) + ![.inr 0, .inr 0] = -1 := by + simp [tensorBasis_repr_formTensor, minkowskiMatrix.inl_0_inl_0, minkowskiMatrix.inr_i_inr_i] + +/-- `η_μν η^μν = d + 1`: contracting both indices of `η_μν` with those of `η^μν` gives the number +of dimensions. -/ +example (d : ℕ) : ((realLorentz d).tensorBasis ![]).repr + {(realLorentz d).formTensor down | μ ν ⊗ (realLorentz d).formTensor up | μ ν}ᵀ + (fun i => i.elim0) = d + 1 := by + rw [Species.Tensor.tensorBasis_repr_contr_of_dual _ _ _ fun _ => contr_basis d _] + simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, + tensorBasis_repr_formTensor] + -- The labels of `η` are casts of `ν (.inl ())` and `ν (.inr ())`, equal to them by `rfl`. + change ∑ ν : Unit ⊕ Unit → Fin 1 ⊕ Fin d, minkowskiMatrix (ν (.inl ())) (ν (.inr ())) * + minkowskiMatrix (ν (.inl ())) (ν (.inr ())) = d + 1 + rw [← ((Equiv.sumArrowEquivProdArrow Unit Unit _).trans ((Equiv.funUnique Unit _).prodCongr + (Equiv.funUnique Unit _))).symm.sum_comp, Fintype.sum_prod_type] + simp [minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, Fintype.sum_sum_type, add_comm] + + +/-- `ℝT[3, up, down]` is the space of tensors `T^μ_ν` in four dimensions. -/ +example : ℝT[3, up, down] = (realLorentz 3).Tensor ![up, down] := rfl +end realLorentz + +end Tensors From da2bfbb3cc2ecc24df6c0eecad5217570a663a99 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 09:40:48 +0000 Subject: [PATCH 011/242] refactor(Tensors): the core as on the full port (Tensor, Inverse, Tensorial, the structural laws, tensor_regroup); realLorentz on it Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 5 + .../Mathematics/ForMathlib/Contraction.lean | 87 ++- .../ForMathlib/SemilinearMaps.lean | 186 ++++++ .../ForMathlib/TensorProductMatrix.lean | 123 ++++ Physlib/Mathematics/Tensors/Color.lean | 59 +- Physlib/Mathematics/Tensors/Conjugation.lean | 550 ++++++++++++------ Physlib/Mathematics/Tensors/Contraction.lean | 443 +++++++++++--- Physlib/Mathematics/Tensors/Elab.lean | 541 +++++++++++++---- Physlib/Mathematics/Tensors/Inverse.lean | 213 +++++++ Physlib/Mathematics/Tensors/Metric.lean | 402 ++++++++----- Physlib/Mathematics/Tensors/Product.lean | 218 ++++++- Physlib/Mathematics/Tensors/Reindexing.lean | 94 +-- Physlib/Mathematics/Tensors/Species.lean | 416 +++++++------ Physlib/Mathematics/Tensors/Tensor.lean | 330 +++++++++++ Physlib/Mathematics/Tensors/Tensorial.lean | 167 ++++++ .../Tensors/RealTensor/Species.lean | 464 +++++++++++---- 16 files changed, 3353 insertions(+), 945 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/SemilinearMaps.lean create mode 100644 Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean create mode 100644 Physlib/Mathematics/Tensors/Inverse.lean create mode 100644 Physlib/Mathematics/Tensors/Tensor.lean create mode 100644 Physlib/Mathematics/Tensors/Tensorial.lean diff --git a/Physlib.lean b/Physlib.lean index d4b2b67b28..2b92ab428f 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -173,7 +173,9 @@ public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Basic public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Unitary public import Physlib.Mathematics.ForMathlib.OrthogonalMatrix public import Physlib.Mathematics.ForMathlib.SchurTriangulation +public import Physlib.Mathematics.ForMathlib.SemilinearMaps public import Physlib.Mathematics.ForMathlib.SesquilinearForm +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.ForMathlib.Trigonometry.SinSq public import Physlib.Mathematics.ForMathlib.Trigonometry.Tanh public import Physlib.Mathematics.Groups.SO3.Basic @@ -201,10 +203,13 @@ public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.Inverse public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing public import Physlib.Mathematics.Tensors.Species +public import Physlib.Mathematics.Tensors.Tensor +public import Physlib.Mathematics.Tensors.Tensorial public import Physlib.Mathematics.VariationalCalculus.Basic public import Physlib.Mathematics.VariationalCalculus.HasVarAdjDeriv public import Physlib.Mathematics.VariationalCalculus.HasVarAdjoint diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 1fa7861bea..64dd8b89fa 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.Contraction +public import Mathlib.LinearAlgebra.Matrix.SesquilinearForm /-! @@ -37,12 +38,17 @@ has components `z_μ^A`, where `A` labels a basis of `U`, and reads as the map ` applied to the values. - `TensorProduct.pairingToLinearMap_apply` : the value at `x` as `x ⊗ z` with its first two factors contracted. -- `TensorProduct.lTensor_pairingToLinearMap_apply` : the map of `N` on the second factor of `M` as - `M ⊗ N` with its middle pair contracted. +- `Module.Basis.coordPairing`, `Module.Basis.coevaluation` : the coordinate pairing + `∑_I (b x)_I (b' y)_I` of two bases with the same labels, and the delta `∑_I b_I ⊗ b'_I`. +- `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and + `Module.Basis.eq_coordPairing` : it is the only such pairing. +- `Module.Basis.pairingToLinearMap_coordPairing_coevaluation` : the delta read through the + coordinate pairing is the identity. ## iii. Table of contents - A. Reading a tensor as a linear map +- B. The coordinate pairing of two bases ## iv. References @@ -93,16 +99,69 @@ lemma pairingToLinearMap_apply (p : V ⊗[R] W →ₗ[R] R) (z : W ⊗[R] U) (x | tmul w y => rfl | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] -/-- Applying the map of `N` to the second factor of `M ∈ U' ⊗ V` is `M ⊗ N` with its middle pair -contracted by `p`, as in `M^{aμ} N_μ^b`. The right side is that contraction built from Mathlib's -associator `TensorProduct.assoc` and unitor `TensorProduct.lid`, as in -`pairingToLinearMap_apply`. -/ -lemma lTensor_pairingToLinearMap_apply (p : V ⊗[R] W →ₗ[R] R) (M : U' ⊗[R] V) (N : W ⊗[R] U) : - (pairingToLinearMap p N).lTensor U' M = (TensorProduct.lid R U).lTensor U' - ((p.rTensor U).lTensor U' ((TensorProduct.assoc R V W U).symm.toLinearMap.lTensor U' - (TensorProduct.assoc R U' V (W ⊗[R] U) (M ⊗ₜ N)))) := by - induction M using TensorProduct.inductionOn with - | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] - | tmul u v => simp [pairingToLinearMap_apply] - end TensorProduct + +namespace Module.Basis + +open TensorProduct + +/-! + +## B. The coordinate pairing of two bases + +Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by +their coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`), with delta `∑_I b_I ⊗ b'_I` +(`coevaluation`). + +-/ + +variable {R ι M N : Type*} [CommSemiring R] [Fintype ι] [DecidableEq ι] [AddCommMonoid M] + [Module R M] [AddCommMonoid N] [Module R N] + +/-- The coordinate pairing `M ⊗ N → R` of two bases with the same labels, Mathlib's +`Matrix.toLinearMap₂ b b'` of the identity matrix. -/ +noncomputable def coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + M ⊗[R] N →ₗ[R] R := + TensorProduct.lift (Matrix.toLinearMap₂ b b' 1) + +/-- `b.coordPairing b' (x ⊗ₜ y) = ∑_I (b x)_I (b' y)_I`. -/ +lemma coordPairing_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : + b.coordPairing b' (x ⊗ₜ[R] y) = ∑ I, b.equivFun x I * b'.equivFun y I := by + simp [coordPairing, Matrix.toLinearMap₂_apply, Matrix.one_apply] + +/-- `b.coordPairing b' (x ⊗ₜ b' J) = x_J`: pairing with the second basis reads off a coordinate. -/ +lemma coordPairing_tmul_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (J : ι) : + b.coordPairing b' (x ⊗ₜ[R] b' J) = b.equivFun x J := by + simp [coordPairing_tmul, Module.Basis.equivFun_self] + +/-- `b.coordPairing b' (b I ⊗ₜ b' J) = δ_{IJ}`: the two bases are dual. -/ +lemma coordPairing_basis_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I J : ι) : + b.coordPairing b' (b I ⊗ₜ[R] b' J) = if I = J then 1 else 0 := by + rw [coordPairing_tmul_basis, Module.Basis.equivFun_self] + +/-- A pairing for which the two bases are dual is the coordinate pairing. -/ +lemma eq_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (p : M ⊗[R] N →ₗ[R] R) + (h : ∀ I J, p (b I ⊗ₜ[R] b' J) = if I = J then 1 else 0) : p = b.coordPairing b' := + (b.tensorProduct b').ext fun IJ => by + rw [Module.Basis.tensorProduct_apply, h, coordPairing_basis_basis] + +/-- `b.coordPairing b' (x ⊗ₜ y) = b'.coordPairing b (y ⊗ₜ x)`: swapping slots swaps the bases. -/ +lemma coordPairing_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : + b.coordPairing b' (x ⊗ₜ[R] y) = b'.coordPairing b (y ⊗ₜ[R] x) := by + rw [coordPairing_tmul, coordPairing_tmul] + exact Finset.sum_congr rfl fun I _ => mul_comm _ _ + +/-- The delta `∑_I b_I ⊗ b'_I ∈ M ⊗ N` of two bases with the same labels. -/ +noncomputable def coevaluation (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N := + ∑ I, b I ⊗ₜ[R] b' I + +/-- The snake law: contracting `x ∈ M` with the first factor of `∑_I b'_I ⊗ b_I` gives `x`. -/ +lemma pairingToLinearMap_coordPairing_coevaluation (b : Module.Basis ι R M) + (b' : Module.Basis ι R N) : + pairingToLinearMap (b.coordPairing b') (b'.coevaluation b) = LinearMap.id := by + refine LinearMap.ext fun x => ?_ + rw [coevaluation, map_sum, LinearMap.sum_apply] + simp only [pairingToLinearMap_tmul, coordPairing_tmul_basis, LinearMap.id_apply] + exact b.sum_equivFun x + +end Module.Basis diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean new file mode 100644 index 0000000000..0f9f39ff8a --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -0,0 +1,186 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.PiTensorProduct.Basic +public import Physlib.Mathematics.ForMathlib.Contraction + +/-! + +# Semilinear maps of tensor products and of bases + +A family of semilinear maps acts on a tensor product factor by factor, and two bases give the +conjugate-linear equivalence that conjugates the coordinates. + +## i. Overview + +Mathlib's `PiTensorProduct.map` applies a family of linear maps to `⨂[R] i, s i`, one per factor. +`PiTensorProduct.mapₛₗ` does the same for a family of maps that are semilinear along one ring +endomorphism `σ`, as conjugate-linear maps are along `star`. It sends `⨂ x i` to `⨂ f i (x i)` and +is semilinear along `σ`, since a scalar of any one factor passes through `σ` once. +`PiTensorProduct.congrₛₗ` is the equivalence of a family of semilinear equivalences. + +A basis `b` of `M`, a basis `b'` of `M'` and an equivalence `e` of their labels give the +conjugate-linear equivalence `Module.Basis.starEquiv b b' e : M ≃ₗ⋆[R] M'`. It conjugates the +coordinates of a vector in `b` and reads them in `b'`, the coordinate at `μ` moving to `e μ`. A +conjugate-linear map that sends each `b μ` to `b' (e μ)` is this equivalence. Conjugating both +factors along the same labels conjugates the coordinate pairing of two bases. + +## ii. Key results + +- `PiTensorProduct.mapₛₗ` applies a family of semilinear maps to a tensor product, and + `PiTensorProduct.congrₛₗ` a family of semilinear equivalences. +- `Module.Basis.starEquiv` conjugates the coordinates of `b` and reads them in `b'`, and + `Module.Basis.eq_starEquiv` proves that it is the only conjugate-linear map sending `b μ` to + `b' (e μ)`. +- `Module.Basis.star_coordPairing_starEquiv` proves that conjugating both factors conjugates the + coordinate pairing. + +## iii. Table of contents + +- A. Semilinear maps of tensor products +- B. Conjugating the coordinates of a basis + +## iv. References + +None. + +-/ + +@[expose] public section + +open TensorProduct + +/-! + +## A. Semilinear maps of tensor products + +-/ + +namespace PiTensorProduct + +variable {ι R : Type*} [CommSemiring R] {s t : ι → Type*} [∀ i, AddCommMonoid (s i)] + [∀ i, Module R (s i)] [∀ i, AddCommMonoid (t i)] [∀ i, Module R (t i)] + +/-- The semilinear map `⨂ x i ↦ ⨂ f i (x i)` of a family of maps semilinear along `σ`. -/ +noncomputable def mapₛₗ {σ : R →+* R} (f : Π i, s i →ₛₗ[σ] t i) : + (⨂[R] i, s i) →ₛₗ[σ] ⨂[R] i, t i := + let φ : (⨂[R] i, s i) →+ ⨂[R] i, t i := + liftAddHom (fun p : R × Π i, s i => σ p.1 • tprod R fun i => f i (p.2 i)) + (fun r x i hx => by + rw [(tprod R).map_coord_zero i (show f i (x i) = 0 by rw [hx, map_zero]), smul_zero]) + (fun x => by rw [map_zero, zero_smul]) + (fun r x i m₁ m₂ => by + simp only [Function.apply_update (fun i => f i), map_add, ← smul_add, + (tprod R).map_update_add]) + (fun r r' x => by rw [map_add, add_smul]) + (fun r x i r' => by + simp only [Function.apply_update (fun i => f i), map_smulₛₗ, (tprod R).map_update_smul, + Function.update_eq_self, smul_smul, map_mul, mul_comm]) + { toFun := φ + map_add' := φ.map_add + map_smul' := fun r x => by + induction x using PiTensorProduct.induction_on' with + | tprodCoeff z x => + rw [smul_tprodCoeff'] + change σ (r • z) • (tprod R fun i => f i (x i)) = + σ r • σ z • (tprod R fun i => f i (x i)) + rw [smul_eq_mul, map_mul, mul_smul] + | add x y hx hy => rw [smul_add, φ.map_add, φ.map_add, hx, hy, smul_add] } + +@[simp] +lemma mapₛₗ_tprod {σ : R →+* R} (f : Π i, s i →ₛₗ[σ] t i) (x : Π i, s i) : + mapₛₗ f (tprod R x) = tprod R fun i => f i (x i) := by + change σ 1 • (tprod R fun i => f i (x i)) = _ + rw [map_one, one_smul] + +/-- The semilinear equivalence `⨂ x i ↦ ⨂ f i (x i)` of a family of semilinear equivalences. -/ +noncomputable def congrₛₗ {σ σ' : R →+* R} [RingHomInvPair σ σ'] [RingHomInvPair σ' σ] + (f : Π i, s i ≃ₛₗ[σ] t i) : (⨂[R] i, s i) ≃ₛₗ[σ] ⨂[R] i, t i := + LinearEquiv.ofLinearMap (mapₛₗ fun i => (f i).toLinearMap) + (mapₛₗ fun i => (f i).symm.toLinearMap) + (PiTensorProduct.ext <| MultilinearMap.ext fun x => by simp) + (PiTensorProduct.ext <| MultilinearMap.ext fun x => by simp) + +@[simp] +lemma congrₛₗ_tprod {σ σ' : R →+* R} [RingHomInvPair σ σ'] [RingHomInvPair σ' σ] + (f : Π i, s i ≃ₛₗ[σ] t i) (x : Π i, s i) : + congrₛₗ f (tprod R x) = tprod R fun i => f i (x i) := + mapₛₗ_tprod _ x + +end PiTensorProduct + +/-! + +## B. Conjugating the coordinates of a basis + +-/ + +namespace Module.Basis + +variable {R : Type*} [CommSemiring R] [StarRing R] {ι ι' M M' : Type*} [AddCommMonoid M] + [Module R M] [AddCommMonoid M'] [Module R M'] + +/-- The conjugate-linear equivalence that conjugates the coordinates of a vector in `b` and reads +them in `b'`, the coordinate at `μ` moving to `e μ`. -/ +noncomputable def starEquiv (b : Basis ι R M) (b' : Basis ι' R M') (e : ι ≃ ι') : + M ≃ₗ⋆[R] M' := + b.repr.trans <| (Finsupp.mapRange.linearEquiv (starLinearEquiv R)).trans <| + (Finsupp.domLCongr e).trans b'.repr.symm + +/-- The coordinates of `starEquiv b b' e v` are the conjugates of those of `v`. -/ +@[simp] +lemma repr_starEquiv (b : Basis ι R M) (b' : Basis ι' R M') (e : ι ≃ ι') (v : M) (μ : ι') : + b'.repr (starEquiv b b' e v) μ = star (b.repr v (e.symm μ)) := by + simp only [starEquiv, LinearEquiv.trans_apply, LinearEquiv.apply_symm_apply, + Finsupp.domLCongr_apply] + rfl + +/-- `starEquiv b b' e` sends the basis vector `b μ` to `b' (e μ)`. -/ +@[simp] +lemma starEquiv_apply_self (b : Basis ι R M) (b' : Basis ι' R M') (e : ι ≃ ι') (μ : ι) : + starEquiv b b' e (b μ) = b' (e μ) := by + simp [starEquiv, Finsupp.mapRange_single] + +/-- Conjugating back along an inverse label map gives back the vector. -/ +lemma starEquiv_starEquiv (b : Basis ι R M) (b' : Basis ι' R M') (e : ι ≃ ι') (e' : ι' ≃ ι) + (he : ∀ μ, e' (e μ) = μ) (v : M) : starEquiv b' b e' (starEquiv b b' e v) = v := + b.repr.injective <| Finsupp.ext fun μ => by + simp [e'.symm_apply_eq.2 (he μ).symm] + +/-- A conjugate-linear map that sends `b μ` to `b' (e μ)` is `starEquiv b b' e`. -/ +lemma eq_starEquiv {F : Type*} [FunLike F M M'] + [SemilinearMapClass F (starRingEnd R) M M'] (b : Basis ι R M) (b' : Basis ι' R M') + (e : ι ≃ ι') (f : F) (hf : ∀ μ, f (b μ) = b' (e μ)) (v : M) : + f v = starEquiv b b' e v := + LinearMap.congr_fun (b.ext (f₁ := (f : M →ₗ⋆[R] M')) (f₂ := (starEquiv b b' e).toLinearMap) + fun μ => by simp [hf]) v + +/-- `starEquiv` intertwines `ρ` and `ρ'` when the matrix of `ρ'` is the conjugate of that of +`ρ`, `ρ'_{e ν, e μ} = (ρ_{ν μ})*`. -/ +lemma starEquiv_map_of_repr (b : Basis ι R M) (b' : Basis ι' R M') (e : ι ≃ ι') + (ρ : M →ₗ[R] M) (ρ' : M' →ₗ[R] M') + (h : ∀ μ ν, b'.repr (ρ' (b' (e μ))) (e ν) = star (b.repr (ρ (b μ)) ν)) (v : M) : + starEquiv b b' e (ρ v) = ρ' (starEquiv b b' e v) := + LinearMap.congr_fun (b.ext (f₁ := (starEquiv b b' e).toLinearMap.comp ρ) + (f₂ := ρ'.comp (starEquiv b b' e).toLinearMap) fun μ => + b'.repr.injective <| Finsupp.ext fun ν => by + obtain ⟨ν, rfl⟩ := e.surjective ν + simpa using (h μ ν).symm) v + +/-- Conjugating both factors along the same labels conjugates the coordinate pairing, +`(∑ μ, x^μ y_μ)* = ∑ μ, (x^μ)* (y_μ)*`. -/ +lemma star_coordPairing_starEquiv [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] + {N N' : Type*} [AddCommMonoid N] [Module R N] [AddCommMonoid N'] [Module R N'] + (b₁ : Basis ι R M) (b₂ : Basis ι R N) (b₁' : Basis ι' R M') (b₂' : Basis ι' R N') + (e : ι ≃ ι') (x : M) (y : N) : + star (b₁.coordPairing b₂ (x ⊗ₜ y)) = + b₁'.coordPairing b₂' (starEquiv b₁ b₁' e x ⊗ₜ starEquiv b₂ b₂' e y) := by + rw [coordPairing_tmul, coordPairing_tmul] + simp only [star_sum, star_mul'] + exact Fintype.sum_equiv e _ _ fun μ => by simp + +end Module.Basis diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean new file mode 100644 index 0000000000..b6b20e4d9f --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -0,0 +1,123 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.TensorProduct.Matrix + +/-! + +# Tensor products of linear maps on coefficient matrices + +In bases `b₁` and `b₂`, `TensorProduct.map f g` acts on the coefficient matrix of a tensor by +`X ↦ A * X * Bᵀ`, with `A` and `B` the matrices of `f` and `g`. + +## i. Overview + +A tensor `v` in `M ⊗ N` has the coefficient matrix `X i j = (b₁.tensorProduct b₂).repr v (i, j)`, +and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The map `f ⊗ g` sends `X` to +`A * X * Bᵀ`. For a Lorentz tensor `T^μν` and a Lorentz transformation `Λ` this is +`T^μν ↦ Λ^μ_ρ T^ρσ Λ^ν_σ`. + +## ii. Key results + +- `Module.Basis.tensorProductToMatrix` : the coefficient matrix of a tensor in `M ⊗ N`. +- `Module.Basis.tensorProductToMatrix_symm_apply` : the tensor with coefficient matrix `X` is + `∑ i j, X i j • b₁ i ⊗ b₂ j`. +- `Module.Basis.tensorProductToMatrix_map` : the coefficient matrix of `f ⊗ g` applied to `v`. +- `Module.Basis.tensorProductToMatrix_symm_map` : `f ⊗ g` applied to the tensor with coefficient + matrix `X`. +- `Module.Basis.tensorProduct_repr_comm` : swapping the factors of a tensor transposes its + coefficients. + +## iii. Table of contents + +- A. The action on coefficient matrices +- B. Swapping the factors + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +/-! + +## A. The action on coefficient matrices + +-/ + +namespace Module.Basis + +open Matrix LinearMap TensorProduct + +section Action + +variable {R M N ι κ : Type*} [CommRing R] [AddCommGroup M] [AddCommGroup N] [Module R M] + [Module R N] [Fintype ι] [Fintype κ] (b₁ : Basis ι R M) (b₂ : Basis κ R N) + +/-- The coefficient matrix `X i j = (b₁.tensorProduct b₂).repr v (i, j)` of a tensor `v` in +`M ⊗ N`. -/ +noncomputable def tensorProductToMatrix : M ⊗[R] N ≃ₗ[R] Matrix ι κ R := + (b₁.tensorProduct b₂).repr ≪≫ₗ Finsupp.linearEquivFunOnFinite R R (ι × κ) ≪≫ₗ + LinearEquiv.curry R R ι κ + +@[simp] +lemma tensorProductToMatrix_apply (v : M ⊗[R] N) (i : ι) (j : κ) : + b₁.tensorProductToMatrix b₂ v i j = (b₁.tensorProduct b₂).repr v (i, j) := rfl + +variable [DecidableEq ι] [DecidableEq κ] + +/-- The tensor with coefficient matrix `X` is `∑ i j, X i j • b₁ i ⊗ b₂ j`. -/ +lemma tensorProductToMatrix_symm_apply (X : Matrix ι κ R) : + (b₁.tensorProductToMatrix b₂).symm X = ∑ i, ∑ j, X i j • (b₁ i ⊗ₜ[R] b₂ j) := by + rw [LinearEquiv.symm_apply_eq] + ext i j + simp [Finsupp.single_apply, Finset.sum_apply] + +/-- The coefficient matrix of `f ⊗ g v` is `A * X * Bᵀ`, with `X` that of `v` and `A` and `B` the +matrices of `f` and `g`. -/ +lemma tensorProductToMatrix_map (f : M →ₗ[R] M) (g : N →ₗ[R] N) (v : M ⊗[R] N) : + b₁.tensorProductToMatrix b₂ (TensorProduct.map f g v) = + toMatrix b₁ b₁ f * b₁.tensorProductToMatrix b₂ v * (toMatrix b₂ b₂ g)ᵀ := by + ext i j + simp only [tensorProductToMatrix_apply, Matrix.mul_apply, Matrix.transpose_apply] + rw [← LinearMap.toMatrix_mulVec_repr (b₁.tensorProduct b₂) (b₁.tensorProduct b₂), + TensorProduct.toMatrix_map] + simp only [Matrix.mulVec, dotProduct, Fintype.sum_prod_type, kroneckerMap_apply, + Finset.sum_mul] + rw [Finset.sum_comm] + exact Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => by ring + +/-- `f ⊗ g` sends the tensor with coefficient matrix `X` to the one with coefficient matrix +`A * X * Bᵀ`. -/ +lemma tensorProductToMatrix_symm_map (f : M →ₗ[R] M) (g : N →ₗ[R] N) (X : Matrix ι κ R) : + TensorProduct.map f g ((b₁.tensorProductToMatrix b₂).symm X) = + (b₁.tensorProductToMatrix b₂).symm (toMatrix b₁ b₁ f * X * (toMatrix b₂ b₂ g)ᵀ) := by + rw [LinearEquiv.eq_symm_apply, tensorProductToMatrix_map, LinearEquiv.apply_symm_apply] + +end Action + +/-! + +## B. Swapping the factors + +-/ + +variable {R M N ι κ : Type*} [CommRing R] [AddCommGroup M] [AddCommGroup N] [Module R M] + [Module R N] + +/-- Swapping the factors of `v` transposes its coefficients, as `(Tᵀ)^νμ = T^μν`. -/ +lemma tensorProduct_repr_comm (b₁ : Basis ι R M) (b₂ : Basis κ R N) (v : M ⊗[R] N) (i : ι) + (j : κ) : + (b₂.tensorProduct b₁).repr (TensorProduct.comm R M N v) (j, i) = + (b₁.tensorProduct b₂).repr v (i, j) := by + induction v using TensorProduct.inductionOn with + | tmul x y => simp [mul_comm] + | add x y hx hy => simp [hx, hy] + +end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 18bdb00bba..98f73f1f4f 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -15,26 +15,27 @@ variance. ## i. Overview -The color of a tensor slot says which representation the slot carries. It is unrelated to the -color charge of QCD. A color `⟨κ, v⟩ : Color K` pairs a kind of index `κ : K` with a variance `v`, -`up` or `down`. For Lorentz tensors there is one kind, `vec`. The index of `v^μ` has color -`⟨vec, up⟩` and the index of `w_μ` has color `⟨vec, down⟩`. A color is a label, not a space. A -species (`Species.lean`) assigns a space to each color, `V ⟨vec, up⟩ = ℝ⁴` and -`V ⟨vec, down⟩ = (ℝ⁴)*`. +A color is the type of an index, made of a kind `κ : K` and a variance, `up` or `down`. A +species (`Species.lean`) assigns a vector space to each color. The kind fixes the basis labels, +and the variance says whether the index is that of a vector (`up`) or a covector (`down`). Lorentz +tensors have one kind, `vec`. The vector `v^μ` has color `⟨vec, up⟩` with space `ℝ⁴`, and the +covector `w_μ` has color `⟨vec, down⟩` with space `(ℝ⁴)*`. -The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, with `up.dual = down` and `down.dual = up`. A contraction -such as `v^μ w_μ` pairs a slot of color `c` with a slot of color `c.dual`. +The slots of a tensor are labelled by colors, not by spaces, so that matching a slot with its +dual is a decidable equality of labels, not an equality of types. The dual of `⟨κ, v⟩` is +`⟨κ, v.dual⟩`, and a contraction such as `v^μ w_μ` pairs a slot of color `c` with a slot of +color `c.dual`. -A variance stores two booleans, whether it is up and whether it is down, and `dual` swaps them. -So `v.dual.dual = v` and `c.dual.dual = c` hold by `rfl` for a variable `v` or `c`, and a -statement that takes a dual twice needs no cast. With an inductive `up | down` the same equation -needs a case split on `v`. +`Variance` is two booleans, and `dual` swaps them. Swapping twice gives back the same structure, +so `c.dual.dual = c` holds by `rfl` for a variable color `c`. `Color K` is a structure, rather +than the product `K × Variance`, so that `c.dual` works as dot notation. ## ii. Key results -- `Variance` : the variance of an index, with `Variance.up`, `Variance.down` and `Variance.dual`. -- `Color` : a kind of index and a variance. -- `Color.dual` : the dual color, with `Color.dual_dual : c.dual.dual = c` by `rfl`. +- `Variance` is the variance of an index, with `Variance.up`, `Variance.down` and + `Variance.dual`. +- `Color` is a kind of index and a variance. +- `Color.dual` is the dual color, and `Color.dual_dual` proves `c.dual.dual = c` by `rfl`. ## iii. Table of contents @@ -43,6 +44,8 @@ needs a case split on `v`. ## iv. References +None. + -/ @[expose] public section @@ -55,10 +58,7 @@ namespace Tensors -/ -/-- The variance of an index, up (contravariant, `v^μ`) or down (covariant, `w_μ`). It stores -whether the index is up and whether it is down, so that `dual` swaps the two fields and -`v.dual.dual = v` holds by `rfl`. A match on both fields has two impossible cases, closed by -`ne`. -/ +/-- The variance of an index, up (contravariant, `v^μ`) or down (covariant, `w_μ`). -/ structure Variance where /-- Whether the index is up. -/ isUp : Bool @@ -71,13 +71,15 @@ deriving DecidableEq namespace Variance /-- An upper index, as in `v^μ`. -/ -def up : Variance := ⟨true, false, by decide⟩ +@[match_pattern] +abbrev up : Variance := ⟨true, false, by decide⟩ /-- A lower index, as in `w_μ`. -/ -def down : Variance := ⟨false, true, by decide⟩ +@[match_pattern] +abbrev down : Variance := ⟨false, true, by decide⟩ -/-- The dual variance, which exchanges up and down. It is an `abbrev` because rewriting with the -species laws needs `c.dual.dual` and `c` to agree when only reducible definitions unfold. -/ +-- An abbrev, since the species laws rewrite with `c.dual.dual = c` at reducible transparency. +/-- The dual variance, exchanging up and down. -/ abbrev dual (v : Variance) : Variance := ⟨v.isDown, v.isUp, v.ne.symm⟩ @[simp] @@ -86,7 +88,7 @@ lemma dual_up : up.dual = down := rfl @[simp] lemma dual_down : down.dual = up := rfl -/-- Taking the dual twice gives back the variance, by `rfl`. -/ +/-- Taking the dual twice gives back the variance. -/ lemma dual_dual (v : Variance) : v.dual.dual = v := rfl end Variance @@ -97,7 +99,7 @@ end Variance -/ -/-- A color `⟨κ, v⟩`: a kind of index `κ : K` and its variance `v`. -/ +/-- A color `⟨κ, v⟩`, made of a kind of index `κ : K` and its variance `v`. -/ structure Color (K : Type) where /-- The kind of index, such as the Lorentz vector index `vec` or a left-handed Weyl index. -/ kind : K @@ -105,16 +107,15 @@ structure Color (K : Type) where variance : Variance deriving DecidableEq -/-- The dual color: the same kind, the other variance. -/ +/-- The dual color, with the same kind and the other variance. -/ abbrev Color.dual {K : Type} (c : Color K) : Color K := ⟨c.kind, c.variance.dual⟩ -/-- The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, so that `simp` rewrites the dual of `⟨κ, up⟩` to -`⟨κ, down⟩`. -/ +/-- The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`. -/ @[simp] lemma Color.dual_mk {K : Type} (κ : K) (v : Variance) : (⟨κ, v⟩ : Color K).dual = ⟨κ, v.dual⟩ := rfl -/-- Taking the dual twice gives back the color, by `rfl`. -/ +/-- Taking the dual twice gives back the color. -/ lemma Color.dual_dual {K : Type} (c : Color K) : c.dual.dual = c := rfl end Tensors diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 0c11b64dc5..7d04ec9bbb 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -5,7 +5,9 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Contraction +public import Physlib.Mathematics.ForMathlib.SemilinearMaps +public import Physlib.Mathematics.ForMathlib.SesquilinearForm +public import Physlib.Mathematics.Tensors.Inverse /-! # Complex conjugation of tensors @@ -14,65 +16,99 @@ The conjugate of a tensor conjugates its components and moves each index to the ## i. Overview -Complex conjugation takes a left-handed Weyl spinor `ψ_α` to a right-handed one `ψ̄_α̇`, and a -Lorentz vector `v^μ` to the Lorentz vector `(v^μ)*`. It changes the kind of an index and keeps its -variance. It is not duality. For `SL(2, ℂ)` the dotted index of `ψ̄_α̇` is not the lowered -undotted index of `ψ_α`. So `bar` and `dual` are separate operations, and they commute. - -A kind `κ : ConjKind O isReal` has an orbit `o : O`, which is the kind together with its -conjugate. The orbit fixes the basis labels. An orbit is real or complex, as `isReal o` says. A -real orbit has one kind, its own conjugate. A complex orbit has two kinds, conjugate to each other. -The flags `left` and `right` of a kind say which it is, and the conjugate kind `κ.bar` swaps them. -The conjugate color `c.bar` has the kind `c.kind.bar` and the variance of `c`. Because `bar` swaps -two flags, `c.bar.bar = c` (`Color.bar_bar`) and `c.dual.bar = c.bar.dual` (`Color.bar_dual`) hold -by `rfl` for every color, so a statement that conjugates twice needs no cast. - -For example, a species of complex Lorentz tensors, over `ℂ` with `G = SL(2, ℂ)`, would have the -complex orbit `weyl`, the real orbit `vec`, and three kinds. The left-handed kind is -`⟨weyl, true, false, _⟩`, its conjugate is the right-handed kind `⟨weyl, false, true, _⟩`, and the -vector kind `⟨vec, true, true, _⟩` is its own conjugate. The last field `_` is the proof of -`valid`. The space `S.V ⟨⟨vec, true, true, _⟩, up⟩ = ℂ⁴` would hold the vectors `v^μ`, with -basis labels `I vec = Fin 4`, and both Weyl kinds would have the basis labels `I weyl = Fin 2`. -This file defines no such species. - -In a conjugate species `S : ConjSpecies k G O isReal I` the basis labels `I o` of a color depend -only on the orbit `o` of its kind, so `c` and `c.bar` have the same basis labels by `rfl`. The -scalars `k` have a conjugation `star`, as `ℂ` does. The conjugate `S.conjEquiv c v` of a vector -`v` of color `c` is the vector `v̄` of color `c.bar` with components `(v̄)^μ = (v^μ)*` in the bases -of the species. The conjugate `Tensor.conj T` of a tensor `T` is the tensor `T̄` with components -`(T̄)^μν = (T^μν)*` and every index moved to the conjugate color. Both are conjugate-linear. -`Tensor.conj` is an involution (`Tensor.conj_conj`) and conjugates each factor of a pure tensor -(`Tensor.conj_pure`). - -The class `Species.HasConjugation` states two laws. Conjugation commutes with the action `Λ` of -each `g : G`, `(Λv)‾ = Λ v̄`, so the representation at `c.bar` is the conjugate of the one at `c`. -Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. On tensors it follows that -conjugation commutes with the group action (`Tensor.conj_smul`) and with contraction -(`Tensor.conj_contr`). +Conjugation conjugates the components of a tensor and moves each index to the conjugate kind, +keeping its variance. A left-handed spinor `ψ_α` becomes a right-handed spinor `ψ̄_α̇`, and a +vector index is its own conjugate, so `v̄^μ` is again a vector. + +A species declares conjugation in two parts. First, its kind type has an instance of Mathlib's +`InvolutiveStar`, whose `star` says which kind is conjugate to which, with `star (star κ) = κ`. +Complex Lorentz lists its kinds and defines `star` by cases. + +```lean +inductive Kind | left | right | vec + +instance : InvolutiveStar Kind where + star | left => right | right => left | vec => vec + star_involutive κ := by cases κ <;> rfl +``` + +The conjugate color `c.bar = ⟨star c.kind, c.variance⟩` has the conjugate kind and the same +variance. Second, the species gives the conjugation maps as data, the class +`Species.HasConjugation`, with two laws. Conjugating twice gives back the vector, and conjugation +preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. That it commutes with the group action, +`(Λv)‾ = Λ v̄`, is a separate property, `Species.IsEquivariantConjugation`, as symmetry is a +separate property of a form. Conjugation is structure that the species declares, not a consequence +of its bases, so the library assumes nothing about the basis labels of a kind and of its +conjugate. + +The conjugate `S.conjEquiv h v` of a vector `v` of color `c` is a vector of color `c'`, and +`S.conjEquiv h` is a conjugate-linear equivalence `V c ≃ V c'`. The target color is given with +the equation `h : c' = c.bar`, which holds by `rfl` at concrete colors, as in +`S.conjEquiv rfl v`. The laws are stated in this form, so that conjugating twice gives back the +vector (`conjEquiv_conjEquiv`) with no cast along `c.bar.bar = c`. + +Most species conjugate componentwise, `(v̄)^μ = (v^μ)*`. The constructor +`Species.HasConjugation.ofBasis` builds the conjugation from the bases of the species and a label +map from each kind to its conjugate kind. It commutes with the group action when the matrices of +the group at `c.bar` are the conjugates of those at `c` +(`HasConjugation.isEquivariantConjugation_ofBasis`), which is needed at the upper colors only. +The identification of the labels of a kind with those of its conjugate lives in this label map, +which the species gives, and not in the library. For complex Lorentz both kinds of spinor have the +labels `Fin 2` and the map is the identity. + +The conjugate `Tensor.conj h T` of a tensor `T` applies the conjugation to every slot, with the +target colors given by `h`, slot by slot. It is conjugate-linear, an involution +(`Tensor.conj_conj`), commutes with contraction (`Tensor.conj_contr`), and for an equivariant +conjugation with the group action (`Tensor.conj_smul`). Its components are the conjugates of those +of `T` when the species conjugates componentwise (`Tensor.tensorBasis_repr_conj_of_basis`). + +A tensor `g_{IJ̄}` in `V a ⊗ V b` with `b = a.bar` is read as a sesquilinear form on the vectors +(`sesqForm`), so that Hermiticity is Mathlib's `LinearMap.IsSymm` and positive-definiteness is +`LinearMap.IsPosDef` from `ForMathlib.SesquilinearForm`. The inverse `g^{J̄I}` in +`V b.dual ⊗ V a.dual` is again such a form, with `a.dual = b.dual.bar` +(`Color.dual_eq_bar_dual`). The inverse of an invertible Hermitian or positive-definite `g_{IJ̄}` +is again Hermitian or positive-definite, and over a field a positive-definite `g_{IJ̄}` is +invertible. ## ii. Key results -- `ConjKind` : a kind of index, its own conjugate on a real orbit, or one of two conjugate kinds - on a complex orbit. -- `Color.bar` : the conjugate color, with `Color.bar_bar` and `Color.bar_dual` by `rfl`. -- `ConjSpecies` : a species on conjugate kinds, with basis labels fixed by the orbit. -- `Species.conjEquiv` : the conjugate of a vector, `(v̄)^μ = (v^μ)*`. -- `Species.HasConjugation` : conjugation commutes with the group action and preserves the - contraction. -- `Species.Tensor.conj` : the conjugate of a tensor, `(T̄)^μν = (T^μν)*`, with - `Species.Tensor.conj_conj`, `Species.Tensor.conj_pure`, `Species.Tensor.conj_smul` and - `Species.Tensor.conj_contr`. +- `Color.bar` is the conjugate color, with `Color.bar_bar` and `Color.bar_dual`. +- `Species.HasConjugation` gives the conjugation of a species with its two laws, and + `Species.conjEquiv` is the conjugate of a vector. +- `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action, and + `Species.rep_dual_of_star_contr` that equivariance at a color gives it at the dual color. +- `Species.conjEquiv_conjEquiv` proves that conjugating twice gives back the vector, + `Species.star_contr` that conjugation preserves the contraction, and `Species.conjEquiv_rep` + that an equivariant conjugation commutes with the group action. +- `Species.HasConjugation.ofBasis` builds a componentwise conjugation from a label map, + `Species.HasConjugation.isEquivariantConjugation_ofBasis` gives its equivariance from the + matrices of the group, and `Species.basis_repr_conjEquiv_of_basis` gives the components of the + conjugate. +- `Species.Tensor.conj` is the conjugate of a tensor, with `Species.Tensor.conj_conj`, + `Species.Tensor.conj_pure`, `Species.Tensor.conj_smul` and `Species.Tensor.conj_contr`. +- `Species.Tensor.tensorBasis_repr_conj_of_basis` proves that the components of the conjugate are + the conjugates of the components. +- `Species.sesqForm` reads a tensor `g_{IJ̄}` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄`. +- `Species.isSymm_sesqForm_inverse` and `Species.isPosDef_sesqForm_inverse` prove that the inverse + of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite. +- `Species.toLin_injective_of_isPosDef` proves that the linear map of a positive-definite `g_{IJ̄}` + is injective, and `Species.toLin_bijective_of_isPosDef` that over a field it is bijective. ## iii. Table of contents -- A. Conjugate kinds and colors -- B. Conjugate species and the conjugation of vectors -- C. Species with a conjugation +- A. Conjugate colors +- B. Species with a conjugation +- C. Componentwise conjugation - D. Conjugation of tensors - D.1. The group action and contraction + - D.2. Components +- E. Hermitian forms and their inverses + - E.1. Positive-definite forms over a field ## iv. References +None. + -/ @[expose] public section @@ -83,101 +119,180 @@ namespace Tensors /-! -## A. Conjugate kinds and colors +## A. Conjugate colors -/ -/-- A kind of index with a conjugate. Its orbit is the kind together with its conjugate and fixes -the basis labels. A real orbit (`isReal o = true`) has one kind, its own conjugate, such as the -Lorentz vector index. A complex orbit has two kinds, conjugate to each other, such as the indices -of `ψ_α` and `ψ̄_α̇`. A real kind has both flags `left` and `right`, and a kind of a complex orbit -has one of them. -/ -structure ConjKind (O : Type) (isReal : O → Bool) where - /-- The orbit, shared with the conjugate kind. It fixes the basis labels. -/ - orbit : O - /-- The kind is left-handed, as `ψ_α`, or real. -/ - left : Bool - /-- The kind is right-handed, as `ψ̄_α̇`, or real. -/ - right : Bool - /-- A real orbit has only the kind with both flags, and a complex orbit only the two kinds with - one flag each. -/ - valid : if isReal orbit then left ∧ right else left ≠ right - -variable {O : Type} {isReal : O → Bool} - -/-- The conjugate kind, with `left` and `right` swapped and the same orbit. -/ -abbrev ConjKind.bar (κ : ConjKind O isReal) : ConjKind O isReal := - ⟨κ.orbit, κ.right, κ.left, by simpa [and_comm, ne_comm] using κ.valid⟩ - -/-- The conjugate color, with the conjugate kind and the same variance, as in `ψ_α ↦ ψ̄_α̇`. -/ -abbrev Color.bar (c : Color (ConjKind O isReal)) : Color (ConjKind O isReal) := - ⟨c.kind.bar, c.variance⟩ - -/-- Conjugating twice gives back the color, by `rfl`. -/ -lemma Color.bar_bar (c : Color (ConjKind O isReal)) : c.bar.bar = c := rfl +variable {K : Type} [InvolutiveStar K] + +/-- The conjugate color, with the conjugate kind `star κ` and the same variance, as in +`ψ_α ↦ ψ̄_α̇`. -/ +abbrev Color.bar (c : Color K) : Color K := ⟨star c.kind, c.variance⟩ + +/-- Conjugating twice gives back the color. -/ +lemma Color.bar_bar (c : Color K) : c.bar.bar = c := by + simp [Color.bar] /-- Conjugation commutes with duality, by `rfl`. -/ -lemma Color.bar_dual (c : Color (ConjKind O isReal)) : c.dual.bar = c.bar.dual := rfl +lemma Color.bar_dual (c : Color K) : c.dual.bar = c.bar.dual := rfl + +/-- If `b` is the conjugate of `a`, then `a.dual` is the conjugate of `b.dual`. This is the color +equation of the inverse of a form in `V a ⊗ V b`. -/ +lemma Color.dual_eq_bar_dual {a b : Color K} (hb : b = a.bar) : a.dual = b.dual.bar := by + subst hb + simp [Color.bar] /-! -## B. Conjugate species and the conjugation of vectors +## B. Species with a conjugation -/ -/-- A species on conjugate kinds whose basis labels `I o` depend only on the orbit `o`, so that -`c` and `c.bar` have the same basis labels by `rfl`. A species declared with this type keeps `I` -visible, which lets the lemmas below find the labels. -/ -abbrev ConjSpecies (k : Type) [CommRing k] (G : Type) [Group G] (O : Type) (isReal : O → Bool) - (I : O → Type) [∀ o, Fintype (I o)] [∀ o, DecidableEq (I o)] : Type 1 := - Species k G (ConjKind O isReal) fun κ => I κ.orbit - namespace Species -variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {I : O → Type} - [∀ o, Fintype (I o)] [∀ o, DecidableEq (I o)] {S : ConjSpecies k G O isReal I} +variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + {S : Species k G K basisIdx} {c c' : Color K} variable (S) in -/-- The conjugate `v̄` of a vector `v` of color `c`, the vector of color `c.bar` with components -`(v̄)^μ = (v^μ)*`. It is conjugate-linear. The components are taken in the bases of the species, -so the conjugate of the basis vector `e_μ` of `c` is the basis vector `e_μ` of `c.bar` -(`conjEquiv_basis`). The laws of `HasConjugation` therefore constrain the bases at `c` and `c.bar` -together. -/ -noncomputable def conjEquiv (c : Color (ConjKind O isReal)) : S.V c ≃ₗ⋆[k] S.V c.bar := - (S.basis c).repr.trans <| (Finsupp.mapRange.linearEquiv (starLinearEquiv k)).trans - (S.basis c.bar).repr.symm - -/-- The components of the conjugate are the conjugates of the components, `(v̄)^μ = (v^μ)*`. -/ -@[simp] -lemma basis_repr_conjEquiv (c : Color (ConjKind O isReal)) (v : S.V c) (μ : I c.kind.orbit) : - (S.basis c.bar).repr (S.conjEquiv c v) μ = star ((S.basis c).repr v μ) := by - simp [conjEquiv] +/-- A conjugation of the species, a conjugate-linear equivalence `v ↦ v̄` from each color `c` to +its conjugate `c'`. Conjugating twice gives back the vector, and conjugation preserves the +contraction. The conjugate color is given with an equation `h : c' = c.bar`, so that no law moves +a vector along `c.bar.bar = c`. Commuting with the group action is the separate property +`IsEquivariantConjugation`. -/ +class HasConjugation where + /-- The conjugate of a vector of color `c`, a vector of the conjugate color `c'`. -/ + conj {c c' : Color K} (h : c' = c.bar) : S.V c ≃ₗ⋆[k] S.V c' + /-- Conjugating twice gives back the vector, `v̄̄ = v`. -/ + conj_conj {c c' : Color K} (h : c' = c.bar) (h' : c = c'.bar) (v : S.V c) : + conj h' (conj h v) = v + /-- Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. -/ + star_contr_conj {c c' : Color K} (h : c' = c.bar) (u : S.V c) (α : S.V c.dual) : + star (S.contr c (u ⊗ₜ[k] α)) = S.contr c' (conj h u ⊗ₜ[k] conj (congrArg Color.dual h) α) + +variable (S) in +/-- The conjugation commutes with the group action, `(Λv)‾ = Λ v̄`, so the representation at +`c.bar` is the conjugate of the one at `c`. -/ +class IsEquivariantConjugation [S.HasConjugation] : Prop where + /-- Conjugation commutes with the group action, `(Λv)‾ = Λ v̄`. -/ + conj_rep {c c' : Color K} (h : c' = c.bar) (g : G) (v : S.V c) : + HasConjugation.conj h (S.rep c g v) = S.rep c' g (HasConjugation.conj h v) + +section + +variable [S.HasConjugation] -/-- The conjugate of the basis vector `e_μ` of `c` is the basis vector `e_μ` of `c.bar`. -/ +variable (S) in +/-- The conjugate `v̄` of a vector `v` of color `c`, a vector of color `c'`, the conjugate of `c` +by `h`, which holds by `rfl` at concrete colors. It is conjugate-linear. -/ +def conjEquiv (h : c' = c.bar) : S.V c ≃ₗ⋆[k] S.V c' := + HasConjugation.conj h + +/-- Conjugating twice gives back the vector, `v̄̄ = v`. -/ @[simp] -lemma conjEquiv_basis (c : Color (ConjKind O isReal)) (μ : I c.kind.orbit) : - S.conjEquiv c (S.basis c μ) = S.basis c.bar μ := by - simp [conjEquiv, Finsupp.mapRange_single] +lemma conjEquiv_conjEquiv (h : c' = c.bar) (h' : c = c'.bar) (v : S.V c) : + S.conjEquiv h' (S.conjEquiv h v) = v := + HasConjugation.conj_conj h h' v + +/-- Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`, for `u` of color `c` and `α` of +color `c.dual`. -/ +lemma star_contr (h : c' = c.bar) (u : S.V c) (α : S.V c.dual) : + star (S.contr c (u ⊗ₜ[k] α)) = + S.contr c' (S.conjEquiv h u ⊗ₜ[k] S.conjEquiv (congrArg Color.dual h) α) := + HasConjugation.star_contr_conj h u α + +/-- Conjugation commutes with the group action, `(Λv)‾ = Λ v̄`. -/ +lemma conjEquiv_rep [S.IsEquivariantConjugation] (h : c' = c.bar) (g : G) (v : S.V c) : + S.conjEquiv h (S.rep c g v) = S.rep c' g (S.conjEquiv h v) := + IsEquivariantConjugation.conj_rep h g v + +/-- Moving the conjugate along an equality of colors conjugates to the new color. -/ +lemma cast_conjEquiv {c'' : Color K} (q : c' = c'') (h : c' = c.bar) (x : S.V c) : + S.cast q (S.conjEquiv h x) = S.conjEquiv (q.symm.trans h) x := by + subst q + rfl + +/-- Conjugating a vector moved along an equality of colors conjugates the vector. -/ +lemma conjEquiv_cast {b b' : Color K} (p : b = b') (h : c' = b'.bar) (x : S.V b) : + S.conjEquiv h (S.cast p x) = S.conjEquiv (h.trans (congrArg Color.bar p.symm)) x := by + subst p + rfl + +end + +omit [InvolutiveStar K] in +/-- If conjugate-linear maps `f` at `c` and `f'` at `c.dual` preserve the contraction and `f` +commutes with the group action, then so does `f'`. Equivariance at a dual color follows from +equivariance at the color. -/ +lemma rep_dual_of_star_contr (f : S.V c ≃ₗ⋆[k] S.V c') (f' : S.V c.dual ≃ₗ⋆[k] S.V c'.dual) + (hf' : ∀ u α, star (S.contr c (u ⊗ₜ α)) = S.contr c' (f u ⊗ₜ f' α)) + (hf : ∀ g v, f (S.rep c g v) = S.rep c' g (f v)) (g : G) (α : S.V c.dual) : + f' (S.rep c.dual g α) = S.rep c'.dual g (f' α) := + ext_contr fun u' => by + obtain ⟨u, rfl⟩ := f.surjective u' + rw [← hf', contr_tmul_rep, contr_tmul_rep, ← hf, hf'] /-! -## C. Species with a conjugation +## C. Componentwise conjugation -/ variable (S) in -/-- A conjugate species has a conjugation when conjugating components commutes with the action -`Λ` of each `g : G`, `(Λv)‾ = Λ v̄`, and preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. Then -the representation at `c.bar` is the conjugate of the one at `c`. At a real kind, its own -conjugate, the matrices `Λ^μ_ν` are therefore real. -/ -class HasConjugation : Prop where - /-- Conjugation commutes with the group action, `(Λv)‾ = Λ v̄`. -/ - conjEquiv_rep (c : Color (ConjKind O isReal)) (g : G) (v : S.V c) : - S.conjEquiv c (S.rep c g v) = S.rep c.bar g (S.conjEquiv c v) - /-- Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`, for `u` of color `c` and - `α` of color `c.dual`. -/ - star_contr (c : Color (ConjKind O isReal)) (u : S.V c) (α : S.V c.dual) : - star (S.contr c (u ⊗ₜ[k] α)) = S.contr c.bar (S.conjEquiv c u ⊗ₜ[k] S.conjEquiv c.dual α) +/-- The componentwise conjugation, `(v̄)^{e μ} = (v^μ)*`, which sends the basis vector `e_μ` of +`c` to the basis vector `e_{e μ}` of the conjugate color. The label map `e h` reads a label of a +kind `κ` as a label of its conjugate `κ'`, given with an equation `h : κ' = star κ`, and reading +it back gives the label (`e_e`). -/ +@[instance_reducible] +noncomputable def HasConjugation.ofBasis + (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') + (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) : + S.HasConjugation where + conj h := (S.basis _).starEquiv (S.basis _) (e (congrArg Color.kind h)) + conj_conj _ _ v := Module.Basis.starEquiv_starEquiv _ _ _ _ (e_e _ _) v + star_contr_conj _ u α := Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α + +/-- The componentwise conjugation commutes with the group action when the matrix of each `g : G` +at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The law is needed at the +upper colors only, since it follows at the lower ones (`rep_dual_of_star_contr`). -/ +lemma HasConjugation.isEquivariantConjugation_ofBasis + (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') + (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) + (rep_basis : ∀ (κ : K) (g : G) (μ ν : basisIdx κ), + (S.basis ⟨star κ, .up⟩).repr (S.rep ⟨star κ, .up⟩ g (S.basis ⟨star κ, .up⟩ (e rfl μ))) + (e rfl ν) = star ((S.basis ⟨κ, .up⟩).repr (S.rep ⟨κ, .up⟩ g (S.basis ⟨κ, .up⟩ μ)) ν)) : + letI := ofBasis S e e_e + S.IsEquivariantConjugation := by + let := ofBasis S e e_e + refine ⟨fun {c _} h g v => ?_⟩ + subst h + obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c + · exact absurd rfl hv + · -- A lower color is the dual of `⟨κ, up⟩`, where the law holds. + exact rep_dual_of_star_contr (c := ⟨κ, .up⟩) (c' := ⟨star κ, .up⟩) _ _ + (fun u α => Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α) + (fun g => Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g)) g v + · exact Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g) v + · exact absurd rfl hv + +/-- The componentwise conjugate of the basis vector `e_μ` is the basis vector `e_{e μ}`. -/ +lemma HasConjugation.ofBasis_conjEquiv_basis + (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') + (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) + (h : c' = c.bar) (μ : basisIdx c.kind) : + letI := ofBasis S e e_e + S.conjEquiv h (S.basis c μ) = S.basis c' (e (congrArg Color.kind h) μ) := + Module.Basis.starEquiv_apply_self _ _ _ μ + +/-- The components of the conjugate are the conjugates of the components, +`(v̄)^μ = (v^{e⁻¹ μ})*`, when conjugation sends each basis vector `e_μ` of `c` to the basis vector +`e_{e μ}` of `c'`. -/ +lemma basis_repr_conjEquiv_of_basis [S.HasConjugation] (h : c' = c.bar) + (e : basisIdx c.kind ≃ basisIdx c'.kind) + (he : ∀ μ, S.conjEquiv h (S.basis c μ) = S.basis c' (e μ)) (v : S.V c) + (μ : basisIdx c'.kind) : + (S.basis c').repr (S.conjEquiv h v) μ = star ((S.basis c).repr v (e.symm μ)) := by + rw [Module.Basis.eq_starEquiv _ _ e (S.conjEquiv h) he v, Module.Basis.repr_starEquiv] /-! @@ -187,32 +302,28 @@ class HasConjugation : Prop where namespace Tensor -variable {ι : Type} [Fintype ι] {c : ι → Color (ConjKind O isReal)} - -/-- The conjugate `T̄` of a tensor `T`, with components `(T̄)^μν = (T^μν)*` and every index moved -to the conjugate color. It is conjugate-linear. -/ -noncomputable def conj : S.Tensor c ≃ₗ⋆[k] S.Tensor fun i => (c i).bar := - (S.tensorBasis c).repr.trans <| (Finsupp.mapRange.linearEquiv (starLinearEquiv k)).trans - (S.tensorBasis fun i => (c i).bar).repr.symm - -/-- The components of the conjugate are the conjugates of the components, `(T̄)^μν = (T^μν)*`. -/ -@[simp] -lemma tensorBasis_repr_conj (t : S.Tensor c) (μ : Π i, I (c i).kind.orbit) : - (S.tensorBasis _).repr (conj t) μ = star ((S.tensorBasis c).repr t μ) := by - simp [conj] +variable [S.HasConjugation] {ι : Type} {c c' : ι → Color K} -/-- Conjugating twice gives back the tensor. No relabeling is needed, since `c.bar.bar = c` holds -by `rfl`. -/ -lemma conj_conj (t : S.Tensor c) : conj (conj t) = t := - (S.tensorBasis c).repr.injective <| Finsupp.ext fun μ => - (tensorBasis_repr_conj (conj t) μ).trans (by rw [tensorBasis_repr_conj, star_star]) +/-- The conjugate `T̄` of a tensor `T`, with every slot conjugated and moved to the conjugate color. +The colors `c'` of the result are the conjugates of `c` by `h`. For concrete colors `h` holds by +`rfl` slot by slot, `fun i => by fin_cases i <;> rfl`. It is conjugate-linear. -/ +noncomputable def conj (h : ∀ i, c' i = (c i).bar) : S.Tensor c ≃ₗ⋆[k] S.Tensor c' := + (S.toPiTensor c).trans <| (PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i)).trans + (S.toPiTensor c').symm /-- The conjugate of a pure tensor conjugates each factor, `(u ⊗ α)‾ = ū ⊗ ᾱ`. -/ @[simp] -lemma conj_pure (f : Π i, S.V (c i)) : - conj (S.pure f) = S.pure fun i => S.conjEquiv (c i) (f i) := - (S.tensorBasis _).repr.injective <| Finsupp.ext fun μ => by - simp [tensorBasis_repr_pure, star_prod] +lemma conj_pure (h : ∀ i, c' i = (c i).bar) (f : Π i, S.V (c i)) : + conj h (S.pure f) = S.pure fun i => S.conjEquiv (h i) (f i) := + congrₛₗ_tprod _ f + +/-- Conjugating twice gives back the tensor. -/ +lemma conj_conj (h : ∀ i, c' i = (c i).bar) (h' : ∀ i, c i = (c' i).bar) (t : S.Tensor c) : + conj h' (conj h t) = t := by + induction t using inductionOn_pure with + | h f => simp + | hsmul r t ht => rw [LinearEquiv.map_smulₛₗ, LinearEquiv.map_smulₛₗ, ht, starRingEnd_self_apply] + | hadd x y hx hy => rw [map_add, map_add, hx, hy] /-! @@ -220,38 +331,153 @@ lemma conj_pure (f : Π i, S.V (c i)) : -/ -variable [S.HasConjugation] - /-- Conjugation commutes with the action `Λ` of `g : G`, `(ΛT)‾ = Λ T̄`. -/ -lemma conj_smul (g : G) (t : S.Tensor c) : conj (g • t) = g • conj t := - map_smul_of_pure conj g (fun f => by simp [HasConjugation.conjEquiv_rep]) t +lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : G) + (t : S.Tensor c) : + conj h (g • t) = g • conj h t := + map_smul_of_pure (conj h) g (fun f => by simp [conjEquiv_rep]) t -variable {κ J : Type} [Fintype κ] [Fintype J] {c' : κ → Color (ConjKind O isReal)} - {a : J → Color (ConjKind O isReal)} +variable {κ J : Type} [Fintype J] {b b' : κ → Color K} {a a' : J → Color K} /-- Conjugating a contraction is contracting the conjugate along the same pairs of slots, -`(T^ρμ_μ)‾ = T̄^ρμ_μ`. The relabeling of `T̄` is `σ` with its colors conjugated. -/ -lemma conj_contr (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) : - conj (contr σ t) = contr (c' := fun x => (c' x).bar) (a := fun j => (a j).bar) - -- The colors are checked slot by slot: the conjugates of `contrColors c' a` and the - -- `contrColors` of the conjugates agree at each slot but not as functions. - ⟨σ.toEquiv, by rintro (_ | _ | _) <;> exact congrArg Color.bar (σ.color_eq _)⟩ (conj t) := by - -- Conjugating a vector commutes with moving it along an equality of colors. - have hcast : ∀ {b b' : Color (ConjKind O isReal)} (p : b = b') (x : S.V b), - S.conjEquiv b' (LinearEquiv.cast (R := k) (M := S.V) p x) = - LinearEquiv.cast (R := k) (M := S.V) (congrArg Color.bar p) (S.conjEquiv b x) := by - intro b b' p x - subst p - rfl +`(T^ρμ_μ)‾ = T̄^ρμ_μ`. The relabeling `τ` of `T̄` moves the slots as `σ` does (`hτ`). -/ +lemma conj_contr (h : ∀ i, c' i = (c i).bar) (hb : ∀ x, b' x = (b x).bar) + (ha : ∀ j, a' j = (a j).bar) (σ : ColorEquiv c (contrColors b a)) + (τ : ColorEquiv c' (contrColors b' a')) (hτ : τ.toEquiv = σ.toEquiv) (t : S.Tensor c) : + conj hb (contr σ t) = contr τ (conj h t) := by + obtain rfl : c' = fun i => (c i).bar := funext h + obtain rfl : b' = fun x => (b x).bar := funext hb + obtain rfl : a' = fun j => (a j).bar := funext ha + obtain ⟨τ, hτ'⟩ := τ + subst hτ induction t using inductionOn_pure with | hsmul r t ht => rw [map_smul, LinearEquiv.map_smulₛₗ, ht, LinearEquiv.map_smulₛₗ, map_smul] | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] | h f => simp only [contr_pure, conj_pure, LinearEquiv.map_smulₛₗ, starRingEnd_apply, star_prod, - HasConjugation.star_contr, hcast] + star_contr rfl, conjEquiv_cast, cast_conjEquiv] + +/-! + +### D.2. Components + +-/ + +/-- The components of the conjugate are the conjugates of the components, +`(T̄)^μν = (T^{e⁻¹ μ, e⁻¹ ν})*`, when conjugation sends each basis vector `e_μ` at the slot `i` to +the basis vector `e_{e i μ}`. -/ +lemma tensorBasis_repr_conj_of_basis [Fintype ι] (h : ∀ i, c' i = (c i).bar) + (e : ∀ i, basisIdx (c i).kind ≃ basisIdx (c' i).kind) + (he : ∀ i μ, S.conjEquiv (h i) (S.basis (c i) μ) = S.basis (c' i) (e i μ)) (t : S.Tensor c) + (μ : Π i, basisIdx (c' i).kind) : + (S.tensorBasis c').repr (conj h t) μ = + star ((S.tensorBasis c).repr t fun i => (e i).symm (μ i)) := by + rw [Module.Basis.eq_starEquiv (S.tensorBasis c) (S.tensorBasis c') (Equiv.piCongrRight e) + (conj h) (fun ν => by simp [tensorBasis_apply, he]) t, Module.Basis.repr_starEquiv] + rfl end Tensor +/-! + +## E. Hermitian forms and their inverses + +-/ + +variable [S.HasConjugation] {a b : Color K} + +variable (S) in +/-- A tensor `g_{IJ̄}` in `V a ⊗ V b` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄` on the +vectors of color `a.dual`. The color `b` is the conjugate of `a` by `hb`, which holds by `rfl` at +concrete colors. The form is conjugate-linear in its first argument, Mathlib's convention, so it is +the physicist's `g(y, x) = g_{IJ̄} y^I x̄^J̄` with the arguments swapped. -/ +noncomputable def sesqForm (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) : + S.V a.dual →ₗ⋆[k] S.V a.dual →ₗ[k] k := + (((TensorProduct.mk k _ _).compr₂ (S.contr b.dual).toLinearMap).compl₂ (S.toLin g)).comp + (S.conjEquiv (congrArg Color.dual hb)).toLinearMap + +/-- The form on `x, y` contracts `x̄` with `g y`, where `g y` is the contraction of `g` with `y`. -/ +lemma sesqForm_apply (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (x y : S.V a.dual) : + S.sesqForm g hb x y = + S.contr b.dual (S.conjEquiv (congrArg Color.dual hb) x ⊗ₜ S.toLin g y) := + rfl + +/-- The form of the inverse on `g x` and `g y` is the conjugate of the form of `g` on `y` and `x`, +`g^{J̄I} (g y)_J̄ (g x)‾_I = (g_{IJ̄} x^I ȳ^J̄)*`. The inverse `g^{J̄I}` acts on barred +covectors, so its form is the conjugate of the usual `g^{IJ̄} w_I w̄'_J̄`. -/ +lemma sesqForm_inverse_toLin (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) + (x y : S.V a.dual) (hb : b = a.bar := by rfl) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + S.sesqForm (S.inverse g) ha (S.toLin g x) (S.toLin g y) = star (S.sesqForm g hb y x) := by + rw [sesqForm_apply, sesqForm_apply, toLin_inverse g h, LinearEquiv.coe_coe, + LinearEquiv.ofBijective_symm_apply_apply, ← S.contr_tmul_symm, star_contr ha, + conjEquiv_conjEquiv] + +/-- The inverse of a Hermitian `g_{IJ̄}` is Hermitian. -/ +lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsSymm := by + refine ⟨fun u w => ?_⟩ + obtain ⟨x, rfl⟩ := h.2 u + obtain ⟨y, rfl⟩ := h.2 w + rw [sesqForm_inverse_toLin g h _ _ hb ha, sesqForm_inverse_toLin g h _ _ hb ha, + starRingEnd_apply, star_star] + exact (hg.eq x y).symm + +/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives +`g(y, y) = 0`. -/ +lemma toLin_injective_of_isPosDef [Preorder k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by + rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] + intro y hy + by_contra hne + simpa [sesqForm_apply, hy] using hg.pos y hne + +/-- The inverse of a positive-definite `g_{IJ̄}` with bijective linear map is positive-definite. -/ +lemma isPosDef_sesqForm_inverse_of_bijective [LT k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsPosDef) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsPosDef := by + refine ⟨isSymm_sesqForm_inverse g h hg.isSymm ha, fun u hu => ?_⟩ + obtain ⟨x, rfl⟩ := h.2 u + rw [sesqForm_inverse_toLin g h _ _ hb ha, ← starRingEnd_apply, hg.isSymm.eq x x] + exact hg.pos x fun hx => hu (by rw [hx, map_zero]) + +end Species + +/-! + +### E.1. Positive-definite forms over a field + +-/ + +namespace Species + +variable {k : Type} [Field k] [StarRing k] [Preorder k] {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} + +/-- A positive-definite `g_{IJ̄}` is invertible. Its linear map `y ↦ g y` is bijective. -/ +lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by + -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. + have hi := toLin_injective_of_isPosDef g hg + refine ⟨hi, ?_⟩ + have : ∀ c, Module.Finite k (S.V c) := fun c => Module.Finite.of_basis (S.basis c) + refine (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi + let f : S.V a.dual ≃ₗ⋆[k] S.V b.dual := S.conjEquiv (congrArg Color.dual hb) + rw [Module.finrank, rank_eq_of_equiv_equiv (starRingEnd k) f.toAddEquiv + (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ f r m), + ← Module.finrank, Module.finrank_eq_card_basis (S.basis b.dual), + Module.finrank_eq_card_basis (S.basis b)] + +/-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ +lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsPosDef := + isPosDef_sesqForm_inverse_of_bijective g (toLin_bijective_of_isPosDef g hg) hg ha + end Species end Tensors diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 44e57ea8d6..c95fa3b45e 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -16,76 +16,93 @@ The contraction of pairs of indices, an index with its dual in each pair, as in ## i. Overview -Take a Lorentz tensor `T = T^ρμ_ν e_ρ ⊗ e_μ ⊗ e^ν`, an element of `ℝ⁴ ⊗ ℝ⁴ ⊗ (ℝ⁴)*`, on the -slots `Fin 3` with colors `c = ![⟨vec, up⟩, ⟨vec, up⟩, ⟨vec, down⟩]`. Its trace over the last two -indices is the vector `(tr T)^ρ = T^ρμ_μ` in `ℝ⁴`. On a pure tensor `u ⊗ v ⊗ α` it is -`(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. - -A contraction sorts the slots into the standard arrangement `κ ⊕ (J ⊕ J)`. -- The slot `inl x`, for `x : κ`, remains, with the color `c' x`. -- For each pair `j : J`, the slot `inr (inl j)` has the color `a j` and the slot `inr (inr j)` - the dual color `(a j).dual`. The two are contracted. - -These colors are `contrColors c' a`. For the trace, `κ = Fin 1`, `c' = ![⟨vec, up⟩]`, `J = Unit` -and `a = fun _ => ⟨vec, up⟩`. `Tensor.contrStd c' a` contracts a tensor in this arrangement. The -two slots of a pair have dual colors by construction, so its formula on pure tensors has no cast -(`contrStd_pure`). - -`Tensor.contr σ` contracts the pairs chosen by a relabeling `σ : ColorEquiv c (contrColors c' a)`, -which sends each slot of `T` to its place in the standard arrangement. It is `S.reindex σ` -followed by `Tensor.contrStd c' a`. For the trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and -`2 ↦ inr (inr ())`. The duality of the two colors of each pair is part of the color proof of `σ`, -not a separate hypothesis. The index notation `{T | ρ μ μ}ᵀ` builds `σ` from the names. - -Each pair is ordered: its slot `inr (inl j)` is the first argument of `S.contr (a j)`. No lemma -here exchanges the two slots of a pair. - -A contraction of two tensors is a contraction of their product. The full contraction `T^μν U_μν` -contracts every slot of `T ⊗ U`, with no remaining slots, `κ = Empty`. The laws let a network of -tensors be contracted in pieces. -- Relabeling the pairs does not change the contraction (`contr_reindex_pairs`). -- Two contractions in sequence are one, as contracting `μ` and then `ν` in `T^μ_μ^ν_ν` is - contracting both pairs at once (`contr_contr`). -- Contracting `T` and then multiplying by `U` is contracting `T ⊗ U`, - `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ` (`tmulEquiv_contr`). - -For tensors on two slots, the contraction of the inner indices is the product of matrices -`M^μ_ν N^ν_ρ`, along the relabeling `ColorEquiv.matMul` (`contr_matMul`). - -The components of a contraction are sums of components of the tensor, as in -`(tr T)^ρ = ∑ μ ν, C_μ^ν T^ρμ_ν` (`tensorBasis_repr_contr`). Here `C_μ^ν = S.contr (e_μ ⊗ e^ν)` -is the contraction of the basis vectors of the two slots of the pair. The formula assumes nothing -about the bases. When the bases of a color and of its dual are dual, `C_μ^ν` is `1` for `μ = ν` -and `0` otherwise, and the formula is the sum `(tr T)^ρ = ∑ μ, T^ρμ_μ` -(`tensorBasis_repr_contr_of_dual`). +A contraction pairs indices of dual colors and sums over their common label, as in the trace +`(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the slots `Fin 3`. On a pure tensor +`u ⊗ v ⊗ α` the trace is `(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. + +Contraction is first defined with the slots in a standard arrangement, where they come in three +groups. The remaining slots are labeled by a type `κ`, and the pairs by a type `J`. Each pair +`j : J` has a first slot, of color `a j`, and a second slot, of the dual color `(a j).dual`. The +slots are then labeled by the disjoint union `κ ⊕ (J ⊕ J)`, with `inl x` the remaining slot `x`, +`inr (inl j)` the first slot of the pair `j` and `inr (inr j)` its second slot. For the trace +`T^ρμ_μ`, `κ = Fin 1` holds `ρ` and `J = Unit` holds the single pair `μ`. These colors are +`contrColors c' a`, and `Tensor.contrStd c' a` contracts each pair by `S.contr (a j)`, with the +first slot as its first argument. The colors of a pair are dual by construction, so no cast +appears. + +Any other choice of pairs is reduced to the standard arrangement by a relabeling +`σ : ColorEquiv c (contrColors c' a)`, which sends each slot to its place there. +`Tensor.contr σ` is `S.reindex σ` followed by `Tensor.contrStd c' a`. Since `σ` keeps colors, the +two slots of each pair are dual without a separate hypothesis. For the trace, `σ` sends +`0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`. The index notation `{T | ρ μ μ}ᵀ` builds +`σ` from the repeated name. + +Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the +product `T ⊗ U` has four slots, which form two pairs and leave no slot, `κ = Empty`. The following +laws let a contraction of several tensors be computed one step at a time. +- The order in which the pairs are listed does not matter. Relabeling the pairs gives the same + contraction. +- A tensor that changes sign when its pairs are permuted contracts to zero, since its contraction + equals its own negative. For `A` antisymmetric and `B` symmetric, renaming `μ ↔ ν` turns + `A^μν B_μν` into `A^νμ B_νμ = -A^μν B_μν`, so `A^μν B_μν = 0`. +- Pairs can be contracted one at a time. Contracting `μ` and then `ν` in `T^μ_μ^ν_ν` is + contracting both pairs at once. +- A factor that takes no part in a contraction can be multiplied before or after it, + `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. + +For tensors on two slots, the contraction `M^μ_ν N^ν_ρ` of the inner indices is the product of +matrices. The components of a contraction are sums of components of the tensor, +`(tr T)^ρ = ∑ μ, T^ρμ_μ`, since the bases of a color and its dual are dual. ## ii. Key results -- `contrColors` : the colors of the standard arrangement, the remaining slots and then the pairs. -- `Species.Tensor.contrStd` : the contraction of a tensor in the standard arrangement. -- `Species.Tensor.contr` : the contraction of the pairs of indices chosen by `σ`, as in `T^ρμ_μ`. -- `Species.Tensor.contr_pure` : the contraction of a pure tensor. -- `Species.Tensor.contr_smul` : contraction commutes with the group action. -- `Species.Tensor.contr_reindex_pairs` : relabeling the pairs does not change the contraction. -- `Species.Tensor.contr_contr` : two contractions in sequence are one, along `ColorEquiv.chain`. -- `Species.Tensor.tmulEquiv_contr` : contraction commutes with a product by a tensor that is not - contracted, along `ColorEquiv.withSpectators`. -- `Species.Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ` of two tensors on two slots. -- `Species.Tensor.tensorBasis_repr_contr` : the components of a contraction. -- `Species.Tensor.tensorBasis_repr_contr_of_dual` : the components of a contraction in dual bases. +- `contrColors` gives the colors of the standard arrangement, the remaining slots and then the + pairs. +- `Species.Tensor.contrStd` contracts a tensor in the standard arrangement. +- `Species.Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`. +- `Species.Tensor.contr_pure` gives the contraction of a pure tensor. +- `Species.Tensor.contr_smul` proves that contraction commutes with the group action. +- `Species.Tensor.contr_congr` proves that a contraction depends only on the bijection of slots. +- `Species.Tensor.contr_reindex_pairs` proves that relabeling the pairs does not change the + contraction, and `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a + relabeling of the slots before or after a contraction. +- `ColorEquiv.SamePair` says that two choices of pairs contract the same two slots, and + `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of slots, + in any order, are equal. +- `Species.Tensor.contr_eq_zero_of_reindex_eq_neg` and + `Species.Tensor.contr_tmulEquiv_eq_zero_of_antisymm_symm` prove `A^μν B_μν = 0` for `A` + antisymmetric and `B` symmetric. +- `Species.Tensor.contr_contr` proves that two contractions in sequence are one, along + `ColorEquiv.chain`. +- `Species.Tensor.tmulEquiv_contr` proves that contraction commutes with a product by a tensor + that is not contracted, along `ColorEquiv.withSpectators`. +- `Species.Tensor.tmulEquiv_contr_right` is the same with the tensor that is not contracted on the + left, along `ColorEquiv.withSpectatorsLeft`. +- `Species.Tensor.contr_matMul` gives the contraction `M^μ_ν N^ν_ρ` of two tensors on two slots, + and `Species.Tensor.contr_matMul_eq_deltaTensor_iff` proves that it is the delta exactly when + their linear maps compose to the identity. +- `Species.Tensor.contr_matMulAt_pure` contracts any slot `i` with the first slot of a tensor on + two slots, along `ColorEquiv.matMulAt`, and `Species.Tensor.contr_deltaTensor` proves + `T^…μ… δ_μ^ν = T^…ν…`. +- `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction. +- `Species.Tensor.tensorBasis_repr_contr_unit` and + `Species.Tensor.tensorBasis_repr_contr_unit_sum` give the same with one and with two pairs, one + label per pair. ## iii. Table of contents - A. Contraction in the standard arrangement of slots - B. Contraction of chosen pairs of indices - - B.1. Relabeling the pairs + - B.1. Relabeling the slots and the pairs - B.2. Two contractions are one - B.3. Products with a tensor that is not contracted -- C. The product of two tensors on two slots +- C. Products with a tensor on two slots - D. Components ## iv. References +None. + -/ @[expose] public section @@ -163,11 +180,9 @@ standard arrangement. -/ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (f : Π x, S.V (c x)) : contr σ (S.pure f) = - (∏ j, S.contr (a j) (LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq (.inr (.inl j))) - (f (σ.toEquiv.symm (.inr (.inl j)))) ⊗ₜ LinearEquiv.cast (R := k) (M := S.V) - (σ.color_eq (.inr (.inr j))) (f (σ.toEquiv.symm (.inr (.inr j)))))) • - S.pure (c := c') fun x => LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq (.inl x)) - (f (σ.toEquiv.symm (.inl x))) := + (∏ j, S.contr (a j) (S.cast (σ.color_eq (.inr (.inl j))) (f (σ.toEquiv.symm (.inr (.inl j)))) + ⊗ₜ S.cast (σ.color_eq (.inr (.inr j))) (f (σ.toEquiv.symm (.inr (.inr j)))))) • + S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) /-- Contraction commutes with the group action, `(ΛT)^ρμ_μ = Λ^ρ_λ T^λμ_μ` for the action `Λ` of @@ -180,9 +195,19 @@ lemma contr_smul [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors congr 1 exact Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) t +/-- A contraction depends only on the bijection of slots, not on the proof that it keeps colors. -/ +lemma contr_congr [Fintype J] {a a' : J → Color K} (ha : a = a') + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) + (h : σ.toEquiv = τ.toEquiv) (t : S.Tensor c) : contr σ t = contr τ t := by + subst ha + obtain ⟨σ, _⟩ := σ + obtain ⟨τ, _⟩ := τ + subst h + rfl + /-! -### B.1. Relabeling the pairs +### B.1. Relabeling the slots and the pairs -/ @@ -203,6 +228,123 @@ lemma contr_reindex_pairs [Fintype J] [Fintype J'] {a : J → Color K} | hsmul r u hu => rw [map_smul, map_smul, hu, map_smul] | hadd u u' hu hu' => rw [map_add, map_add, hu, hu', map_add] +/-- Relabeling the slots and then contracting is contracting along the composite relabeling. -/ +lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} + (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : + contr σ (S.reindex τ t) = contr (τ.trans σ) t := by + simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, reindex_reindex] + +/-- Relabeling the remaining slots of a contraction is contracting along the composite +relabeling. -/ +lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : S.Tensor c) : + S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] + | hadd t t' ht ht' => rw [map_add, map_add, ht, ht', map_add] + | h p => + rw [contr_pure, map_smul, reindex_pure, contr_pure] + congr 1 + exact congrArg S.pure (funext fun x => by simp only [LinearEquiv.cast_apply, cast_cast]; rfl) + +/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two slots, in either order. -/ +def _root_.Tensors.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) + (j' : J') : Prop := + (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inl j')) ∧ + σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inr j'))) ∨ + (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inr j')) ∧ + σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inl j'))) + +instance [DecidableEq ι₁] {a : J → Color K} {a' : J' → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) + (j' : J') : Decidable (σ.SamePair τ j j') := by + unfold ColorEquiv.SamePair + infer_instance + +/-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ +lemma _root_.Tensors.ColorEquiv.SamePair.left_unique {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j₁ j₂ : J} + {j' : J'} (h₁ : σ.SamePair τ j₁ j') (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by + have hσ := σ.toEquiv.symm.injective + rcases h₁ with ⟨h₁, h₁'⟩ | ⟨h₁, h₁'⟩ <;> rcases h₂ with ⟨h₂, h₂'⟩ | ⟨h₂, h₂'⟩ + · simpa using hσ (h₁.trans h₂.symm) + · simpa using hσ (h₁'.trans h₂.symm) + · simpa using hσ (h₁.trans h₂'.symm) + · simpa using hσ (h₁.trans h₂.symm) + +/-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ +lemma _root_.Tensors.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j : J} + {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by + have hτ := τ.toEquiv.symm.injective + rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> + simpa using hτ (h₁.symm.trans h₂) + +/-- Two contractions are equal when they keep the same slots in the same order and contract the +same pairs of slots, in any order and with either slot of a pair first. On literal slots the +hypotheses are checked by `decide`. -/ +lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) + (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (h₂ : ∀ j', ∃ j, σ.SamePair τ j j') (t : S.Tensor c) : + contr σ t = contr τ t := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [map_smul, map_smul, ht] + | hadd t t' ht ht' => rw [map_add, map_add, ht, ht'] + | h p => + rw [contr_pure, contr_pure] + choose g hg using h₁ + congr 1 + · -- The pair `j` of `σ` is the pair `g j` of `τ`, in the same order or swapped. + refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), + fun j' => (h₂ j').imp fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ + rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ + · rw [cast_eq_cast_of_eq p h, cast_eq_cast_of_eq p h'] + exact contr_cast_tmul_cast _ _ _ _ _ _ + · rw [cast_eq_cast_of_eq p h, cast_eq_cast_of_eq p h'] + exact contr_cast_tmul_cast_symm _ _ _ _ _ _ + · exact congrArg S.pure (funext fun x => cast_eq_cast_of_eq p (hfree x) _) + +/-- A contraction of a tensor that changes sign under a relabeling `π` of its slots is zero, when +`π` only permutes the pairs along `e` and keeps the remaining slots. The hypothesis `ht` has the +form of the index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ +lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] [Invertible (2 : k)] {a : J → Color K} + (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) + (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) + {t : S.Tensor c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by + -- The colors of the pairs are invariant under `e`, since `π` keeps colors. + have he : ∀ j, a (e.symm j) = a j := fun j => by + set x := σ.toEquiv.symm (.inr (.inl (e.symm j))) + have hx : π.toEquiv x = σ.toEquiv.symm (.inr (.inl j)) := by + rw [Equiv.eq_symm_apply, ← Equiv.trans_apply, hπ] + simp [x] + have := π.color_eq (π.toEquiv x) + rw [Equiv.symm_apply_apply, hx] at this + exact (σ.color_eq _).symm.trans (this.trans (σ.color_eq _)) + have h : contr σ t + contr σ t = 0 := by + nth_rw 2 [← contr_reindex_pairs σ e t] + rw [contr_congr (funext he) _ (π.trans σ) hπ.symm] + nth_rw 1 [ht] + simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, + reindex_reindex, map_neg, neg_add_cancel] + rw [← one_smul k (contr σ t), ← invOf_mul_self (2 : k), mul_smul, two_smul, h, smul_zero] + +/-- `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. The contraction of `A ⊗ B` is zero +when `π₁` reverses the sign of `A`, `π₂` fixes `B`, and together they only permute the pairs. The +hypotheses have the form of the index notation, `{A | μ ν = -A | ν μ}ᵀ` and +`{B | μ ν = B | ν μ}ᵀ`. -/ +lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] [Invertible (2 : k)] + {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {a : J → Color K} + (σ : ColorEquiv (prodColors c₁ c₂) (contrColors c' a)) (π₁ : ColorEquiv c₁ c₁) + (π₂ : ColorEquiv c₂ c₂) (e : J ≃ J) + (hπ : (π₁.sumCongr π₂).toEquiv.trans σ.toEquiv = + σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) + {A : S.Tensor c₁} {B : S.Tensor c₂} (hA : A = S.reindex π₁ (-A)) (hB : B = S.reindex π₂ B) : + contr σ (S.tmulEquiv c₁ c₂ (A ⊗ₜ B)) = 0 := + contr_eq_zero_of_reindex_eq_neg σ _ e hπ (by + rw [← map_neg, ← neg_tmul, reindex_sumCongr_tmulEquiv, ← hA, ← hB]) + /-! ### B.2. Two contractions are one @@ -266,9 +408,34 @@ lemma tmulEquiv_contr [Fintype J] {a : J → Color K} {d : ι₂ → Color K} congr 1 exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) +/-- The pairs `σ` of `T`, as pairs of `U ⊗ T`. The indices of `U`, with the colors `d`, come before +those of `T` that are not contracted. -/ +noncomputable def _root_.Tensors.ColorEquiv.withSpectatorsLeft {c : ι₁ → Color K} + {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : + ColorEquiv (prodColors d c) (contrColors (prodColors d c') a) := + ((ColorEquiv.refl d).sumCongr σ).trans (ColorEquiv.sumAssoc _ _ _).symm + +/-- Contracting indices of `T` and then multiplying by `U` on the left is contracting the same +indices of `U ⊗ T`, `U^ρ (T^μ_μν) = (U ⊗ T)^ρμ_μν`. -/ +lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color K} + (σ : ColorEquiv c (contrColors c' a)) (u : S.Tensor d) (t : S.Tensor c) : + S.tmulEquiv d c' (u ⊗ₜ contr σ t) = + contr (σ.withSpectatorsLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := by + induction t using inductionOn_pure with + | hsmul r t ht => simp only [map_smul, tmul_smul, ht] + | hadd x y hx hy => simp only [map_add, tmul_add, hx, hy] + | h p => + induction u using inductionOn_pure with + | hsmul r u hu => simp only [map_smul, ← smul_tmul', hu] + | hadd x y hx hy => simp only [map_add, add_tmul, hx, hy] + | h p' => + rw [contr_pure, tmul_smul, map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] + congr 1 + exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) + /-! -## C. The product of two tensors on two slots +## C. Products with a tensor on two slots -/ @@ -303,52 +470,142 @@ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual funext j fin_cases j <;> rfl +/-- Two tensors on two slots contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear +maps compose to the identity, `Y ∘ X = id`. -/ +lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) + (Y : S.V y.dual ⊗[k] S.V x.dual) : + contr (ColorEquiv.matMul x y x.dual) + (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ + S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by + rw [deltaTensor, contr_matMul, (S.twoSlotEquiv _ _).injective.eq_iff, + ← (S.toLin (a := x)).injective.eq_iff, toLin, LinearEquiv.coe_ofLinearMap, + pairingToLinearMap_lTensor, S.snake x.dual] + exact Iff.rfl + +/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a +tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is +`ColorEquiv.matMul` at any slot of a tensor on any slots. -/ +def _root_.Tensors.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) + (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : + ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where + toEquiv := + { toFun := Sum.elim (fun x => if x = i then .inr (.inl ()) else .inl x) + (fun j => if j = 0 then .inr (.inr ()) else .inl i) + invFun := Sum.elim (fun x => if x = i then .inr 1 else .inl x) + (Sum.elim (fun _ => .inl i) fun _ => .inr 0) + left_inv := by + rintro (x | j) + · by_cases hx : x = i <;> simp [hx] + · fin_cases j <;> simp + right_inv := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i <;> simp [hx] + · simp + · simp } + color_eq := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i + · subst hx + simp [hi] + · simp [hx, h x hx] + · simp [ha] + · simp + +/-- On a pure tensor, contracting the slot `i` with the first slot of `F` applies `F`, read as a +linear map through `S.contr a`, to the vector at `i`. The vectors `q` at the other slots are +those of `p`, moved along `h`. -/ +lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) + (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) + (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) + (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : + contr (ColorEquiv.matMulAt i a b ha hi h) + (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoSlotEquiv a.dual b F)) = + S.pure (Function.update q i + (S.cast hi.symm (pairingToLinearMap (S.contr a).toLinearMap F (S.cast ha (p i))))) := by + induction F using TensorProduct.inductionOn with + | add F F' hF hF' => + simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] + rw [pure, pure, pure, MultilinearMap.map_update_add, map_add] + | tmul x y => + rw [pairingToLinearMap_tmul, map_smul] + conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] + rw [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] + congr 2 + funext z + by_cases hz : z = i + · subst hz + rw [Function.update_self, cast_eq_cast_of_eq _ (show + (ColorEquiv.matMulAt z a b ha hi h).toEquiv.symm (.inl z) = .inr 1 by + simp [ColorEquiv.matMulAt])] + rfl + · rw [Function.update_of_ne hz, cast_eq_cast_of_eq _ (show + (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by + simp [ColorEquiv.matMulAt, hz]), hq z hz] + +/-- Contracting the slot `i` with the first slot of the delta is the identity, +`T^…μ… δ_μ^ν = T^…ν…`. -/ +lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) + (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) + (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [← smul_tmul', map_smul, map_smul, ht] + | hadd t t' ht ht' => rw [add_tmul, map_add, map_add, ht, ht'] + | h p => + rw [deltaTensor, contr_matMulAt_pure i a a ha ha _ _ p p (fun _ _ => rfl), snake, + LinearMap.id_apply, LinearEquiv.cast_apply, LinearEquiv.cast_apply, cast_cast] + exact congrArg S.pure (Function.update_eq_self_iff.2 rfl) + /-! ## D. Components -/ -/-- The components of a contraction, as `(tr T)^ρ = ∑ μ ν, C_μ^ν T^ρμ_ν` for a trace. The labels -`μ` and `ν` run over the first and the second slots of the pairs, `C_μ^ν` is the product over the -pairs of `S.contr (a j) (e_μ ⊗ e^ν)`, and the components of `T` are those of `S.reindex σ T`. -/ +/-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. The labels `μ` run +over the pairs, and the components of `T` are those of `S.reindex σ T`. -/ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : - (S.tensorBasis c').repr (contr σ t) φ = - ∑ μ : Π j, basisIdx (a j).kind, ∑ ν : Π j, basisIdx (a j).dual.kind, - (∏ j, S.contr (a j) (S.basis (a j) (μ j) ⊗ₜ S.basis (a j).dual (ν j))) * - (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ ν)) := by + (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := by simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe] generalize S.reindex σ t = u induction u using inductionOn_pure with - | hsmul r u hu => - simp only [map_smul, Finsupp.smul_apply, hu, smul_eq_mul, Finset.mul_sum] - exact Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => by ring - | hadd u u' hu hu' => - simp only [map_add, Finsupp.add_apply, hu, hu', mul_add, Finset.sum_add_distrib] + | hsmul r u hu => simp only [map_smul, Finsupp.smul_apply, hu, smul_eq_mul, Finset.mul_sum] + | hadd u u' hu hu' => simp only [map_add, Finsupp.add_apply, hu, hu', Finset.sum_add_distrib] | h f => - -- Each contraction of a pair is expanded in components, and the product of sums over the - -- pairs becomes a sum over the families of labels `μ` and `ν`. + -- Each contraction of a pair is a sum over one label, and the product of sums over the + -- pairs becomes a sum over the families of labels `μ`. rw [contrStd_pure, map_smul, Finsupp.smul_apply, tensorBasis_repr_pure, smul_eq_mul, - Finset.prod_congr rfl fun j _ => contr_tmul_eq_sum_basis (a j) _ _, Fintype.prod_sum] - simp only [tensorBasis_repr_pure, Fintype.prod_sum_type, Fintype.prod_sum, Finset.sum_mul] - refine Finset.sum_congr rfl fun μ _ => Finset.sum_congr rfl fun ν _ => ?_ - simp only [Finset.prod_mul_distrib] + Finset.prod_congr rfl fun j _ => contr_tmul_eq_sum_basis (a j) _ _, Fintype.prod_sum, + Finset.sum_mul] + refine Finset.sum_congr rfl fun μ _ => ?_ + simp only [tensorBasis_repr_pure, Fintype.prod_sum_type, Finset.prod_mul_distrib] ring -/-- The components of a contraction in dual bases, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. When -the bases of each pair are dual, `S.contr (a j) (e_μ ⊗ e^ν)` is `1` for `μ = ν` and `0` otherwise, -and the double sum of `tensorBasis_repr_contr` has one label per pair. -/ -lemma tensorBasis_repr_contr_of_dual [Fintype κ] [Fintype J] [DecidableEq J] - {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) - (φ : Π x, basisIdx (c' x).kind) - (h : ∀ j μ ν, S.contr (a j) (S.basis (a j) μ ⊗ₜ S.basis (a j).dual ν) = - if μ = ν then 1 else 0) : - (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, - (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := by - simp only [tensorBasis_repr_contr, h, Finset.prod_boole, Finset.mem_univ, forall_const, - ← funext_iff, boole_mul, Fintype.sum_ite_eq] +/-- The components of the contraction of one pair in dual bases, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a +single label `μ`. -/ +lemma tensorBasis_repr_contr_unit [Fintype κ] {a : Unit → Color K} + (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a ()).kind, + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) + (Sum.rec φ (Sum.rec (fun _ => μ) (fun _ => μ))) := by + rw [tensorBasis_repr_contr] + exact Fintype.sum_equiv (Equiv.funUnique Unit (basisIdx (a ()).kind)) _ _ fun _ => rfl + +/-- The components of the contraction of two pairs in dual bases, `(tr T)^ρ = ∑ μ ν, T^ρμν_μν` +with one label per pair. -/ +lemma tensorBasis_repr_contr_unit_sum [Fintype κ] {a : Unit ⊕ Unit → Color K} + (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (contr σ t) φ = + ∑ μ : basisIdx (a (.inl ())).kind, ∑ ν : basisIdx (a (.inr ())).kind, + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ + (Sum.rec (Sum.rec (fun _ => μ) fun _ => ν) (Sum.rec (fun _ => μ) fun _ => ν))) := by + rw [tensorBasis_repr_contr, ← Fintype.sum_prod_type'] + exact Fintype.sum_equiv ((Equiv.sumPiEquivProdPi fun j => basisIdx (a j).kind).trans + ((Equiv.piUnique _).prodCongr (Equiv.piUnique _))) _ _ fun _ => congrArg _ (funext fun j => by + rcases j with _ | (⟨⟨⟩⟩ | ⟨⟨⟩⟩) | (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> rfl) end Tensor diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 0e85791ec3..497737dc99 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -5,7 +5,8 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Metric +public import Physlib.Mathematics.Tensors.Inverse +public import Physlib.Mathematics.Tensors.Tensorial /-! # Index notation @@ -14,56 +15,56 @@ Tensor expressions with named indices, such as `{T | μ ν ⊗ U | ν ρ}ᵀ` fo ## i. Overview -Take the species `S` of real Lorentz tensors in `3 + 1` dimensions. An upper index has the color -`⟨vec, up⟩`, whose space `S.V ⟨vec, up⟩ = ℝ⁴` holds the vectors `v^μ`, and a lower index has the -color `⟨vec, down⟩`. -A tensor `T` with colors `![⟨vec, up⟩, ⟨vec, down⟩]` has the components `T^μ_ν`. In index notation -it is written `T | μ ν`, with one name per slot. The heights of the indices are not written, since -the colors of `T` fix them. +A tensor `T` of a species `S` with colors `![c₀, …, cₙ₋₁]` is written `T | μ₀ … μₙ₋₁`, one name +per slot. The heights of the indices are not written, since the colors fix them. A real Lorentz +tensor `T : S.Tensor ![⟨vec, up⟩, ⟨vec, down⟩]`, with components `T^μ_ν`, is written `T | μ ν`. -An expression `{…}ᵀ` is a tensor, an element of `S.Tensor ![…]`, or a proposition if it is an -equation. For a second tensor `U` with the same colors as `T`, `{T | μ ν ⊗ U | ν ρ}ᵀ` is the tensor -`T^μ_ν U^ν_ρ`, with colors `![⟨vec, up⟩, ⟨vec, down⟩]`. For an upper color `c`, the forms -`S.formTensor c = g^μν` and `S.formTensor c.dual = g_μν` are inverse, `g^μν g_νρ = δ^μ_ρ`: +An expression `{…}ᵀ` is a tensor in `S.Tensor ![…]`, or a proposition if it is an equation. The +names are bound only inside the braces. Each construct is an operation of the species. - `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`. - -This is `contr_formTensor_formTensor`, whose relabeling `ColorEquiv.matMul` agrees slot by slot with -the one the notation generates (section C). - -The names are bound only inside the braces. The notation elaborates to the operations of a species. - -- `A ⊗ B` is the product `S.tmulEquiv _ _ (A ⊗ₜ B)`. -- In a product, every name written twice is contracted, all pairs in one `Tensor.contr σ`. The - relabeling `σ` is generated from the names, and its color proof checks that the two slots of - each pair have dual colors. The first occurrence of a name is the first argument of `S.contr`. -- The remaining slots are numbered `0, 1, …` in the order written, so the result is a tensor on - `Fin m` with a literal list of colors `![…]`. +- `A ⊗ B` is `S.tmulEquiv _ _ (A ⊗ₜ B)`. +- A name written twice in a product is contracted. All pairs of the product are contracted in one + `Tensor.contr σ`, with the relabeling `σ` generated from the names. The two slots of a pair must + have dual colors, and the first one written is the first argument of `S.contr`. +- The free indices are numbered `0, 1, …` in the order written. The result is a tensor on `Fin m` + with a literal color list `![…]`. - `A + B` and `A = B` relabel `B` by `S.reindex` when its names come in another order. Each name must have the same color on both sides. -- `T | ↓μ` lowers the upper index at that slot and `T | ↑μ` raises a lower one, by `S.toDualAt`. - For a vector `v`, `{v | ↓ν}ᵀ` is `v_ν = v^μ g_μν`. This needs an instance `S.WithInvariantForm`. - The arrow is checked against the color of the slot, so its variance must be visible, as in - `⟨κ, .up⟩` for a kind `κ`. -- A scalar `r : k`, a group element `Λ : G` and negation are ordinary terms in front of `|`, as in - `r • T | μ ν`, `Λ • T | μ ν` and `-T | μ ν`. A compound expression is scaled by nesting it, - `r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ`. - -An expression in parentheses, each side of a sum, and a nested `{…}ᵀ` are contracted first, before -they enter an enclosing product. The names they contract are free again outside: -`{(T | μ μ) ⊗ U | μ ρ}ᵀ` is `(T^λ_λ) U^μ_ρ`, and `{(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ` is -`(T^λ_λ + U^λ_λ) T^μ_ν`. Within one product chain a name may appear at most twice. - -The slots of each tensor must be labeled by a type built from `Fin n` and `⊕` with a literal `n`. -Its colors must be a literal list `![a, b, …]` or `prodColors` of such lists. A tensor with a -variable family of colors cannot be written, so general lemmas are stated without the notation. -A tensor with no slots is written `s |` with a space after the bar, since Mathlib reads `|` -followed directly by a term as an absolute value `|x|`. An equation with such a tensor can also be -written outside the braces, `{T | μ μ}ᵀ = s`. +- `↓μ` lowers an upper index and `↑μ` raises a lower one, by `S.toDualAt`, so `{v | ↓ν}ᵀ` is + `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the slot, and a visible + variance, as in `⟨κ, .up⟩`. +- A term `m : M` with an instance `Tensorial S c M`, such as a Lorentz vector, is read as + `Tensorial.toTensor m`. +- Scalars, group elements and negatives are ordinary terms in front of `|`, as in `r • T | μ ν`, + `Λ • T | μ ν` and `-T | μ ν`. A compound expression is scaled by nesting it, as in + `r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ`. The form `-(T | μ ν)` is a parse error. + +Parentheses, each side of a sum and a nested `{…}ᵀ` are contracted before they enter an enclosing +product, so the names they contract are free again outside. Thus `{(T | μ μ) ⊗ U | μ ρ}ᵀ` is +`(T^λ_λ) U^μ_ρ`. Within one product a name appears at most twice. + +When the expected type is known, the result is fitted to it. On a tensor type with other slots, +such as `Fin 1 ⊕ Fin 1` for `Fin 2`, the free indices are placed on those slots in the order +written. On a type `M` with an instance `Tensorial S c M`, the result is read back by +`Tensorial.toTensor.symm`. So `({v | ↓μ}ᵀ : CoVector d)` is the covector `v_μ`, and +`({x | μ ⊗ y | ν}ᵀ : Vector d ⊗ Vector d)` is `x ⊗ₜ y`. + +Every tensor needs literal slots and colors. Its slot type is built from `Fin n` and `⊕` with a +literal `n`, and its colors are a literal list `![a, b, …]` or `prodColors` of such lists. General +lemmas over a variable color family are therefore stated without the notation. A tensor `s` with +no slots is written `s |` with a space after the bar, since Mathlib reads `|x|` as an absolute +value. + +For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written +`{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is +`contr_formTensor_formTensor` (section C). ## ii. Key results - `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. +- `tensor_components` : reduces an equation of tensors to an equation of their components. +- `tensor_regroup` : proves an equation of two contractions of the same factors, grouped or + ordered differently. ## iii. Table of contents @@ -72,6 +73,8 @@ written outside the braces, `{T | μ μ}ᵀ = s`. - B.1. Slot types - B.2. The steps of an expression - B.3. The elaborator + - B.4. Components + - B.5. Regrouping contractions - C. Examples ## iv. References @@ -123,7 +126,7 @@ syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term -/ -/-- A slot type built from `Fin n`, `Unit` and `⊕`. -/ +/-- A slot type built from `Fin n`, `Unit` and `⊕`. Its slots are numbered `0, 1, …` in order. -/ inductive Slots where /-- `Fin n`. -/ | fin (n : Nat) @@ -133,16 +136,11 @@ inductive Slots where | sum (a b : Slots) deriving BEq -/-- A slot of a slot type: the path of `Sum.inl` (`false`) and `Sum.inr` (`true`) to a leaf, and -the position in that leaf. -/ -abbrev Addr := List Bool × Nat - -/-- The slots of a slot type, in order. -/ -def Slots.addrs : Slots → List Addr - | .fin n => (List.range n).map (([], ·)) - | .unit => [([], 0)] - | .sum a b => a.addrs.map (fun (p, i) => (false :: p, i)) ++ - b.addrs.map (fun (p, i) => (true :: p, i)) +/-- The number of slots. -/ +def Slots.size : Slots → Nat + | .fin n => n + | .unit => 1 + | .sum a b => a.size + b.size /-- The slot type as a term. -/ def Slots.type : Slots → TermElabM Term @@ -150,22 +148,20 @@ def Slots.type : Slots → TermElabM Term | .unit => `(Unit) | .sum a b => do `($(← a.type) ⊕ $(← b.type)) -/-- A slot as a term. -/ -def Slots.elem : Slots → Addr → TermElabM Term - | .fin n, (_, i) => `(($(quote i) : Fin $(quote n))) +/-- The slot number `i` as a term. -/ +def Slots.elem : Slots → Nat → TermElabM Term + | .fin n, i => `(($(quote i) : Fin $(quote n))) | .unit, _ => `(()) - | .sum a _, (false :: p, i) => do `(Sum.inl $(← a.elem (p, i))) - | .sum _ b, (true :: p, i) => do `(Sum.inr $(← b.elem (p, i))) - | .sum _ _, ([], _) => throwError "index notation: bad slot" - -/-- The function on a slot type with the values `img`, as vector literals on `Fin n`, constants on -`Unit`, and `comb` on `⊕`. -/ -def Slots.fn (comb : Name) (img : Addr → TermElabM Term) : Slots → TermElabM Term - | .fin n => do `(![$((← (List.range n).mapM fun i => img ([], i)).toArray),*]) - | .unit => do `(fun _ : Unit => $(← img ([], 0))) - | .sum a b => do - `($(mkIdent comb) $(← a.fn comb fun (p, i) => img (false :: p, i)) - $(← b.fn comb fun (p, i) => img (true :: p, i))) + | .sum a b, i => + if i < a.size then do `(Sum.inl $(← a.elem i)) else do `(Sum.inr $(← b.elem (i - a.size))) + +/-- The function on a slot type taking the values `vs` in slot order, built from vector literals on +`Fin n`, constants on `Unit` and `comb` on `⊕`. -/ +def Slots.fn (comb : Name) : Slots → List Term → TermElabM Term + | .fin _, vs => `(![$(vs.toArray),*]) + | .unit, vs => `(fun _ : Unit => $(vs[0]!)) + | .sum a b, vs => do + `($(mkIdent comb) $(← a.fn comb (vs.take a.size)) $(← b.fn comb (vs.drop a.size))) /-- The slot type of a type built from `Fin n` and `⊕` with a literal `n`. -/ partial def Slots.ofType? (ι : Expr) : MetaM (Option Slots) := do @@ -178,21 +174,16 @@ partial def Slots.ofType? (ι : Expr) : MetaM (Option Slots) := do return some (.sum a b) else return none -/-- The bijection from `dom` to `cod` sending the `i`-th slot of `dom` to `img[i]`, with its -inverse laws checked slot by slot by `fin_cases`. `decide` would need a `DecidableEq` instance on -the whole slot type, which exceeds the instance size limit from four contracted pairs on. -/ -def slotEquiv (dom cod : Slots) (img : List Addr) : TermElabM Term := do - let table := dom.addrs.zip img - let toFun ← dom.fn ``Sum.elim fun x => match table.find? (·.1 == x) with - | some (_, y) => cod.elem y - | none => throwError "index notation: a slot has no image" - let invFun ← cod.fn ``Sum.elim fun y => match table.find? (·.2 == y) with - | some (x, _) => dom.elem x - | none => throwError "index notation: a slot has no preimage" - `(({ toFun := $toFun, invFun := $invFun, - left_inv := (by intro x; fin_cases x <;> rfl), - right_inv := (by intro x; fin_cases x <;> rfl) } : - $(← dom.type) ≃ $(← cod.type))) +/-- The bijection from `dom` to `cod` sending the slot `pre[j]` of `dom` to the slot `j` of `cod`. +The list `pre` names every slot of `dom` once. The inverse laws are proved slot by slot by +`fin_cases`. -/ +def slotEquiv (dom cod : Slots) (pre : List Nat) : TermElabM Term := do + let toFun ← dom.fn ``Sum.elim (← (List.range dom.size).mapM fun i => cod.elem (pre.idxOf i)) + let invFun ← cod.fn ``Sum.elim (← pre.mapM dom.elem) + -- `decide` would need a `DecidableEq` instance on the whole slot type, which exceeds the + -- instance size limit from four contracted pairs on. + `((Equiv.mk $toFun $invFun (by intro x; fin_cases x <;> rfl) + (by intro x; fin_cases x <;> rfl) : $(← dom.type) ≃ $(← cod.type))) /-- The entries of a literal color family `![a, b, …]`, or of `prodColors` of literal families. -/ partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do @@ -211,7 +202,7 @@ def colorsTerm (cs : List Expr) : TermElabM Term := do -/ -/-- A partly elaborated expression: the name and color of each slot, the slot type, the tensor +/-- A partly elaborated expression, with the name and color of each slot, its slot type, its tensor and its species. -/ structure Node where /-- The index name of each slot. -/ @@ -225,22 +216,37 @@ structure Node where /-- The species. -/ species : Expr +/-- The tensor of `a` relabeled onto `Fin pre.length` with the colors `cs`, its slot `pre[j]` +becoming the slot `j`. -/ +def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term := do + `(Tensors.Species.reindex $(← exprToSyntax a.species) + (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) $(← slotEquiv a.slots (.fin pre.length) pre)) + $a.tm) + /-- The expression relabeled to `Fin n`, its slots numbered in order. -/ def Node.toFin (a : Node) : TermElabM Node := do let n := a.names.length if a.slots == .fin n then return a - let σ ← slotEquiv a.slots (.fin n) ((List.range n).map ([], ·)) - let tm ← `(Tensors.Species.reindex $(← exprToSyntax a.species) - (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm a.colors)) $σ) $a.tm) - return { a with slots := .fin n, tm } + return { a with slots := .fin n, tm := ← a.reindex (List.range n) a.colors } /-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ` and raised at each `↑μ`. -/ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do - let e ← instantiateMVars (← elabTerm T none) + let mut e ← instantiateMVars (← elabTerm T none) if e.hasSyntheticSorry then throwAbortTerm - let ty ← whnfR (← instantiateMVars (← inferType e)) + let mut ty ← whnfR (← instantiateMVars (← inferType e)) + if ty.isMVar then + throwError "index notation: the type of {T} is not known; give it, as `({T} : S.Tensor ![…])`" unless ty.isAppOf ``Tensors.Species.Tensor do - throwError "index notation: {T} is not a tensor" + -- Not a tensor: read it through its `Tensorial` instance, if it has one. + try + e ← withoutErrToSorry do + let e ← elabTerm (← `(Tensors.Tensorial.toTensor $(← exprToSyntax e))) none + synthesizeSyntheticMVarsNoPostponing + instantiateMVars e + ty ← whnfR (← instantiateMVars (← inferType e)) + catch ex => + throwError "index notation: {T} is not a tensor and has no `Tensorial` instance:\n\ + {ex.toMessageData}" let args := ty.getAppArgs let some slots ← Slots.ofType? args[args.size - 2]! | throwError "index notation: the slots of {T} must be `Fin n` and `⊕`" @@ -268,17 +274,20 @@ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do for ((_, d), i) in kinds.zipIdx do if d then let colors := a.colors.set i (← mkAppM ``Tensors.Color.dual #[a.colors[i]!]) - -- The proof at `i` is `id rfl`, of type `c' i = (c i).dual` as in the lemmas on `toDualAt`. - -- A bare `rfl` has the type `c' i = c' i`, which `rw` matches only by unfolding `![…] i`. - -- The new colors agree with the old ones away from `i`, by cases on the slot. - let tm ← `(Tensors.Species.toDualAt $(← exprToSyntax a.species) - (c' := $(← colorsTerm colors)) ($(quote i) : Fin $(quote n)) (id rfl) + -- The color `col` of the slot is passed explicitly, so the form is found at the kind of + -- `col` and every lowering at that kind uses the same instance. The proofs `c i = col` and + -- `c' i = col.dual` are `id rfl`, since a bare `rfl` has the type `c' i = c' i`, which + -- `rw` matches only after unfolding `![…] i`. Away from `i` the colors agree by cases. + let sp ← exprToSyntax a.species + let col ← exprToSyntax a.colors[i]! + let tm ← `(Tensors.Species.toDualAt $sp (c' := $(← colorsTerm colors)) + ($(quote i) : Fin $(quote n)) $col (id rfl) (id rfl) (by intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) $a.tm) a := { a with colors, tm } return a -/-- Contracts every repeated name in one `Tensor.contr`, and numbers the remaining slots in the -order written. -/ +/-- Contracts every repeated name in one `Tensor.contr`, and numbers the free slots in the order +written. -/ def contract (a : Node) : TermElabM Node := do let idx := a.names.zipIdx let mut free : List Nat := [] @@ -298,16 +307,11 @@ def contract (a : Node) : TermElabM Node := do -- The pairs are labeled by `Unit` for one pair, as in `ColorEquiv.matMul`, and by -- `(… ⊕ Unit) ⊕ Unit` for more, as `ColorEquiv.chain` combines two contractions in sequence. let J := (List.range (pairs.length - 1)).foldl (fun J _ => Slots.sum J .unit) .unit - -- A remaining slot goes to `inl k`, the pair `j` to `inr (inl j)` and `inr (inr j)`. - let img : List Addr := (List.range a.names.length).map fun i => - match free.idxOf? i, pairs.findIdx? (·.1 == i), pairs.findIdx? (·.2 == i) with - | some k, _, _ => ([false], k) - | _, some j, _ => (true :: false :: (J.addrs[j]!).1, 0) - | _, _, some j => (true :: true :: (J.addrs[j]!).1, 0) - | _, _, _ => ([], 0) - let σ ← slotEquiv a.slots (.sum (.fin m) (.sum J J)) img - let pairColors ← J.fn ``Tensors.prodColors fun (p, _) => - exprToSyntax a.colors[(pairs[J.addrs.idxOf (p, 0)]!).1]! + -- The free slots go to `inl k`, the first slot of the pair `j` to `inr (inl j)` and its second + -- slot to `inr (inr j)`. + let σ ← slotEquiv a.slots (.sum (.fin m) (.sum J J)) + (free ++ pairs.map (·.1) ++ pairs.map (·.2)) + let pairColors ← J.fn ``Tensors.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) let tm ← `(Tensors.Species.Tensor.contr (S := $(← exprToSyntax a.species)) (Tensors.ColorEquiv.mk (c₂ := Tensors.contrColors $(← colorsTerm (free.map (a.colors[·]!))) $pairColors) $σ) @@ -324,10 +328,7 @@ def reorder (l r : Node) : TermElabM Term := do unless ← isDefEq c c' do throwError "index notation: {nm} has the color {c'} on the left and {c} on the right" if l.names == r.names then return r.tm - let m := l.names.length - let σ ← slotEquiv (.fin m) (.fin m) (r.names.map fun nm => ([], l.names.idxOf nm)) - `(Tensors.Species.reindex $(← exprToSyntax l.species) - (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm l.colors)) $σ) $r.tm) + r.reindex (l.names.map r.names.idxOf) l.colors /-! @@ -337,9 +338,8 @@ def reorder (l r : Node) : TermElabM Term := do mutual -/-- An expression before its outer contraction. A product chain `A ⊗ B` is -`S.tmulEquiv _ _ (A ⊗ₜ B)` with its repeated names not yet contracted. An expression in -parentheses and each side of a sum are contracted first. -/ +/-- An expression before its outer contraction. A product `A ⊗ B` is `S.tmulEquiv _ _ (A ⊗ₜ B)`, +its repeated names not yet contracted. Parentheses and each side of a sum are contracted first. -/ partial def uncontracted (stx : Syntax) : TermElabM Node := do match stx with | `(tensorIndexExpr| $T:term | $ids:tensorIndex*) => leaf T ids @@ -362,6 +362,50 @@ partial def build (stx : Syntax) : TermElabM Node := do contract (← uncontract end +/-- The tensor of `e` on the slots and colors of the tensor type `target`, its free indices in the +order written. It is `none` when `target` is not a tensor type with literal slots and colors. -/ +def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do + let target ← whnfR (← instantiateMVars target) + unless target.isAppOf ``Tensors.Species.Tensor do return none + let args := target.getAppArgs + let some slots ← Slots.ofType? args[args.size - 2]! | return none + let c ← instantiateMVars args[args.size - 1]! + let some cs ← colorList? c | return none + let u ← uncontracted e + -- A product with no contraction is already on the slots of its factors. + let a ← if u.slots == slots && u.names.eraseDups.length == u.names.length then pure u + else contract u + unless cs.length == a.colors.length do + throwError "index notation: {a.colors.length} free indices, but {cs.length} slots expected" + for (col, i) in a.colors.zipIdx do + unless ← isDefEq col cs[i]! do + throwError "index notation: the free index {a.names[i]!} has the color {col}, but its slot \ + is expected to have the color {cs[i]!}" + if a.slots == slots then return a.tm + `(Tensors.Species.reindex $(← exprToSyntax a.species) + (Tensors.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) + $(← slotEquiv a.slots slots (List.range slots.size))) $a.tm) + +/-- The term of `e` for the expected type. It is relabeled onto the slots of a tensor type, and read +back by `Tensorial.toTensor.symm` for a `Tensorial` type. -/ +def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do + let some ty := expectedType? | return (← build e).tm + let ty ← instantiateMVars ty + if ty.getAppFn.isMVar || ty.isSort then return (← build e).tm + if (← whnfR ty).isAppOf ``Tensors.Species.Tensor then + return (← buildTo e ty).getD (← build e).tm + -- For a `Tensorial` type, the tensor type is the codomain of `toTensor`. + let M ← exprToSyntax ty + let s ← saveState + let some tensorTy ← try withoutErrToSorry do + let f ← elabTerm (← `(fun m : $M => Tensors.Tensorial.toTensor m)) none + synthesizeSyntheticMVarsNoPostponing + pure (some (← inferType (← instantiateMVars f)).bindingBody!) + catch _ => s.restore; pure none + | return (← build e).tm + let some tm ← buildTo e tensorTy | return (← build e).tm + `((Tensors.Tensorial.toTensor (M := $M)).symm $tm) + /-- Elaborates index notation. An equation is allowed only at the top, possibly in parentheses. -/ @[term_elab tensorIndexSyntax] def elabTensorIndex : TermElab := fun stx expectedType? => do match stx with @@ -369,9 +413,193 @@ end | `({ $l:tensorIndexExpr = $r:tensorIndexExpr }ᵀ) => let l ← build l elabTerm (← `($l.tm = $(← reorder l (← build r)))) none - | `({ $e:tensorIndexExpr }ᵀ) => elabTerm (← build e).tm expectedType? + | `({ $e:tensorIndexExpr }ᵀ) => elabTerm (← buildFor e expectedType?) expectedType? | _ => throwUnsupportedSyntax +/-! + +### B.4. Components + +-/ + +/-- Removes `cast h a` when the two types agree at reducible transparency. -/ +dsimproc_decl reduceCast (cast _ _) := fun e => do + let_expr cast α β _ a := e | return .continue + if ← withReducible (isDefEq α β) then return .done a else return .continue + +/-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, +using the component lemmas `lᵢ` as well. It unfolds products, relabelings, sums, negatives, zeros, +scalar multiples and contractions of one or two pairs. For more pairs, pass +`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal slot that +`tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a +goal that is already a component the first step is skipped. -/ +syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.simpArg,* "]" : + tactic + +macro_rules + | `(tactic| tensor_components [$ls,*]) => `(tactic| focus + try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) + -- `Function.update_of_ne` proves `j ≠ i` for literal slots by `ne_eq` and `Fin.reduceEq`. + simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, + Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_unit, + Species.Tensor.tensorBasis_repr_contr_unit_sum, Species.tensorBasis_repr_reindex, + Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, + Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] + -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are + -- evaluated. The option `+instances` reaches those inside instance arguments. + try dsimp +instances only [ColorEquiv.symm, Equiv.symm_symm, Equiv.coe_fn_mk, Sum.elim_inl, + Sum.elim_inr, Matrix.cons_val, Function.comp_apply, Tensors.Notation.reduceCast]) + +/-! + +### B.5. Regrouping contractions + +-/ + +open Tactic + +/-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ +private def productFactors? (e : Expr) : Option (Expr × Expr) := + let e := e.consumeMData + if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.tmulEquiv then + let x := e.appArg!.consumeMData + if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none + else none + +/-- The factors of a product, from left to right. -/ +private partial def factors (e : Expr) : List Expr := + match productFactors? e with + | some (a, b) => factors a ++ factors b + | none => [e] + +/-- The product of the first `n` factors of a left-nested product `e` of `m` factors. -/ +private def leadingProduct (e : Expr) (m n : Nat) : Expr := Id.run do + let mut e := e + for _ in [0:m - n] do + if let some (a, _) := productFactors? e then e := a + return e + +/-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ +private def contractedProducts : TacticM (Expr × Expr) := do + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + | throwError "tensor_regroup: the goal is not an equation" + let arg? (e : Expr) : Option Expr := + let e := e.consumeMData + if e.isAppOfArity ``DFunLike.coe 6 && + e.appFn!.appArg!.isAppOf ``Tensors.Species.Tensor.contr then some e.appArg! else none + let some p := arg? l | throwError "tensor_regroup: the left side is not a contraction" + let some q := arg? r | throwError "tensor_regroup: the right side is not a contraction" + return (p, q) + +/-- Whether every product inside the product `e` is in its left factor. -/ +private partial def isLeftNested (e : Expr) : Bool := + match productFactors? e with + | some (a, b) => (productFactors? b).isNone && isLeftNested a + | none => true + +/-- Writes both sides of the goal as one contraction of a left-nested product, repeating until the +goal no longer changes, at most ten times. The three steps run separately, since together their +lemmas loop. -/ +private def flattenContractions : TacticM Unit := do + for _ in [0:10] do + let before ← instantiateMVars (← getMainTarget) + evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr, + Tensors.Species.Tensor.tmulEquiv_contr_right, Tensors.Species.Tensor.contr_contr])) + evalTactic (← `(tactic| try simp only [← Tensors.Species.tmulEquiv_assoc])) + evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, + Tensors.Species.tmulEquiv_reindex_right, Tensors.Species.reindex_reindex, + Tensors.Species.Tensor.contr_reindex, Tensors.Species.Tensor.reindex_contr])) + if (← instantiateMVars (← getMainTarget)) == before then return + throwError "tensor_regroup: flattening the products did not stop after ten rounds" + +/-- Swaps the factors `m` and `m + 1` of the product `q`, of `n` factors, on the right side of the +goal, and absorbs the relabeling into the contraction. For two equal factors the two sides of the +swap lemma agree up to the relabeling, so it is rewritten once with `rw` rather than `simp`. -/ +private def swapFactors (q : Expr) (n m : Nat) (equal : Bool) : TacticM Unit := do + let fs := factors q + let thm ← if m = 0 then + `((Tensors.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) + $(← exprToSyntax fs[0]!)).symm) + else + `((Tensors.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) + $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) + if equal then + evalTactic (← `(tactic| conv_rhs => rw [$thm:term])) + evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, + Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) + else + evalTactic (← `(tactic| conv_rhs => simp only [$thm:term, + Tensors.Species.tmulEquiv_reindex_left, Tensors.Species.reindex_reindex, + Tensors.Species.Tensor.contr_reindex])) + +/-- The ways to match each factor on the left with a different factor on the right, given for +each factor on the left the factors on the right equal to it. The matching in order comes first. -/ +private partial def matchings : List (List Nat) → List Nat → List (List Nat) + | [], _ => [[]] + | c :: cs, used => + (c.filter (!used.contains ·)).flatMap fun j => (matchings cs (j :: used)).map (j :: ·) + +/-- Orders the factors on the right side of the goal so that the factor at each position `k` is +the one that started at position `target[k]`, by swapping neighbours. `equal i j` says whether +the factors that started at `i` and `j` are equal. -/ +private def arrange (target : List Nat) (equal : Nat → Nat → Bool) : TacticM Unit := do + let n := target.length + -- The starting positions of the factors in their current order. + let mut labels := List.range n + for pos in (List.range n).reverse do + let k := labels.idxOf target[pos]! + for m in [k:pos] do + let (_, q) ← contractedProducts + swapFactors q n m (equal labels[m]! labels[m+1]!) + labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! + +/-- `tensor_regroup` proves an equation `Tensor.contr σ P = Tensor.contr τ Q` of two contractions +of the same factors, grouped or ordered differently, such as +`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. It writes each +side as one contraction of a product, orders the factors of the right side as on the left, and +checks by evaluation that both sides keep the same slots and contract the same pairs of slots +(`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of +matching the equal factors is tried in turn, so the number of attempts is the product of `k!` +over the factors that appear `k` times. + +Both sides must be contractions once relabelings are absorbed. A scalar multiple or a side with no +contraction is rejected, and is first brought to that form by hand. -/ +elab "tensor_regroup" : tactic => do + flattenContractions + let (p, q) ← contractedProducts + unless isLeftNested p && isLeftNested q do + throwError "tensor_regroup: a side is not a left-nested product after flattening" + let ps := factors p + let qs := factors q + unless ps.length = qs.length do + throwError "tensor_regroup: the two sides have different numbers of factors" + let n := ps.length + -- Factors are compared once, without assigning metavariables of the goal. + let same (a b : Expr) : TacticM Bool := withNewMCtxDepth (isDefEq a b) + let candidates ← ps.mapM fun x => (List.range n).filterM fun j => same x qs[j]! + let table ← (List.range n).toArray.mapM fun i => + (List.range n).toArray.mapM fun j => same qs[i]! qs[j]! + let equal : Nat → Nat → Bool := fun i j => (table[i]!)[j]! + let targets := matchings candidates [] + if targets.isEmpty then + throwError "tensor_regroup: some factor on the left has no equal factor on the right" + let s ← saveState + let mut lastError : MessageData := m!"" + for target in targets do + s.restore + try + arrange target equal + -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The + -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free + -- color variables of the goal although the bijections of slots do not depend on them. + evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in + exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) + (of_decide_eq_true rfl) (of_decide_eq_true rfl) _)) + return + catch e => lastError := e.toMessageData + throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ + of slots. The last attempt failed with\n{lastError}" + end Tensors.Notation end @@ -398,8 +626,8 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K /-- The matrix product `T^μ_ν U^ν_ρ`. -/ noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ -/-- A product written as a term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the -product written in the notation. -/ +/-- A product given as one term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the +product in the notation. -/ example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl /-- An equation with a tensor `s` with no slots, `T^μ_μ = s`. The space after the bar keeps @@ -435,11 +663,18 @@ example : {(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ = {({T | μ μ}ᵀ + {U | #guard_msgs in #check {T | μ ν = T | ν μ}ᵀ --- A term that is not a tensor is rejected. -/-- error: index notation: r is not a tensor -/ -#guard_msgs in +-- A term that is neither a tensor nor tensorial is rejected, with the error of the instance search. +/-- error: index notation: r is not a tensor and has no `Tensorial` instance: +failed to synthesize instance of type class + Tensorial -/ +#guard_msgs (substring := true) in #check {r | μ}ᵀ +-- A term whose type is not known, such as a bare numeral, is rejected. +/-- error: index notation: the type of 0 is not known; give it, as `(0 : S.Tensor ![…])` -/ +#guard_msgs in +#check {0 | μ ν}ᵀ + /-- An expression in parentheses is contracted first, so `μ` is free again in the product, `(T^λ_λ) U^μ_ρ`. -/ example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ := rfl @@ -460,27 +695,43 @@ example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ #check {T | μ ν ⊗ T | μ ρ}ᵀ /-- Contracting `ν` and then `ρ` is contracting both at once, `(T U)^μ_ρ W^ρ_τ = T^μ_ν U^ν_ρ W^ρ_τ`, -by `tmulEquiv_contr` and `contr_contr`. The two choices of pairs agree slot by slot. -/ +by `tmulEquiv_contr` and `contr_contr`. -/ example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U | ν ρ ⊗ W | ρ τ}ᵀ := by rw [Tensor.tmulEquiv_contr, Tensor.contr_contr] congr 2 ext x fin_cases x <;> rfl +/-- The same by `tensor_regroup`, with the other grouping on the right, +`(T U)^μ_ρ W^ρ_τ = T^μ_ν (U W)^ν_τ`. -/ +example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ := by + tensor_regroup + +/-- `tensor_regroup` reorders the factors, `T^μ_ν U^ν_ρ = U^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ U | ν ρ = U | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- A factor that appears twice, `T^μ_ν T^ν_ρ = T^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ T | ν ρ = T | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- The trace is cyclic, `tr(T U T U) = tr(U T U T)`. -/ +example : {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ = U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ ⊗ T | μ ν}ᵀ := by + tensor_regroup + section Metric -variable [S.WithInvariantForm] +variable [S.HasForm κ] [S.HasForm c.kind] /-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ -example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ -- An arrow that does not match the variance of the slot is rejected. /-- error: index notation: ↑μ needs a visibly lower index -/ #guard_msgs in #check {v | ↑μ}ᵀ -/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`. The lemmas on `toDualAt` apply -by `rw` and `simp`. -/ +/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [toDualAt_smul]`. -/ example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [toDualAt_smul] /-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ @@ -490,8 +741,8 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ /-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by `rw [Tensor.contr_smul]`. -/ example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [Tensor.contr_smul] -/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The notation generates the pairs of -`ColorEquiv.matMul` slot by slot. -/ +/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates +is `ColorEquiv.matMul`, checked slot by slot. -/ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by rw [← contr_formTensor_formTensor] congr 2 @@ -500,4 +751,36 @@ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTenso end Metric +section Tensorial + +variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) + +/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ +#guard_msgs in +#check {m | μ ν}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ +noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ + +/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ +noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ +noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ + +open TensorProduct in +/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ +example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by + rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] + +/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ +noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ + +/-- error: index notation: the free index μ has the color a, but its slot is expected to have the +color a.dual -/ +#guard_msgs in +example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ + +end Tensorial + end Tensors.Notation diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean new file mode 100644 index 0000000000..21d6e9a2ac --- /dev/null +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -0,0 +1,213 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Metric +/-! + +# The inverse of a tensor on two slots + +The inverse `g^νμ` of a tensor `g_μν`, defined through the linear map that `g` defines. + +## i. Overview + +A tensor `g` in `V a ⊗ V b` is also a linear map `S.toLin g : V a.dual → V b`, which contracts a +vector with the first index of `g`, `v^μ ↦ v^μ g_μν` for lower colors `a` and `b`. When this map +is invertible, its inverse is again a tensor, in `V b.dual ⊗ V a.dual`. That is the inverse +`S.inverse g`, with components `g^νμ`. Its first index is the dual of the second index of `g`, so +the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, with its barred index first. + +When `S.toLin g` is not invertible, `S.inverse g` is `0`, as Mathlib's inverse of a singular matrix +is. This junk value makes `inverse` a total function and is the inverse of nothing. Every lemma +about `S.inverse g` assumes that `S.toLin g` is bijective or derives it from its hypotheses. + +The inverse satisfies the contraction laws `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`, and +it is the only tensor whose linear map inverts that of `g`, so `(g⁻¹)⁻¹ = g`. + +The inverse commutes with the group action, `(Λg)⁻¹ = Λ(g⁻¹)`, so the inverse of an invariant +tensor is invariant. The inverse of the form at `c` is the form at `c.dual`, so `g^μν` is the +inverse of `g_μν`. + +## ii. Key results + +- `Species.inverse` is the inverse `g^νμ` of `g_μν`, and `Species.toLin_inverse` gives its + linear map. +- `Species.contr_mul_inverse` and `Species.contr_inverse_mul` prove `g_μν g^νρ = δ_μ^ρ` and + `g^νμ g_μρ = δ^ν_ρ`. +- `Species.inverse_eq_left_inv`, `Species.inverse_eq_right_inv` and + `Species.inverse_eq_of_contr_eq_deltaTensor` prove that the inverse is unique, and + `Species.inverse_inverse` that `(g⁻¹)⁻¹ = g`. +- `Species.inverse_map_rep` proves `(Λg)⁻¹ = Λ(g⁻¹)`, and `Species.inverse_invariant` that the + inverse of an invariant tensor is invariant. +- `Species.inverse_form` proves that the inverse of the form at `c` is the form at `c.dual`, and + `Species.contr_formTensor_formTensor` that `g^μν g_νρ = δ^μ_ρ`. + +## iii. Table of contents + +- A. The inverse +- B. The contraction laws and uniqueness +- C. The inverse commutes with the group action +- D. The inverse of an invariant form + +## iv. References + +None. + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensors + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + {a b : Color K} + +/-! + +## A. The inverse + +-/ + +variable (S) in +open scoped Classical in +/-- The inverse `g^νμ ∈ V b.dual ⊗ V a.dual` of `g_μν ∈ V a ⊗ V b`, the tensor whose linear map +inverts that of `g`. When `S.toLin g` is not bijective it is the junk value `0`. Its first index +is the dual of the second index of `g`, so the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, +the usual `g^{IJ̄}` with its indices swapped (`TensorProduct.comm`). -/ +noncomputable def inverse (g : S.V a ⊗[k] S.V b) : S.V b.dual ⊗[k] S.V a.dual := + if h : Function.Bijective (S.toLin g) then + S.toLin.symm (LinearEquiv.ofBijective _ h).symm.toLinearMap + else 0 + +/-- The linear map of the inverse is the inverse of the linear map. -/ +lemma toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + S.toLin (S.inverse g) = (LinearEquiv.ofBijective _ h).symm.toLinearMap := by + simp [inverse, h] + +/-- The linear map of the inverse is bijective. -/ +lemma toLin_inverse_bijective (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + Function.Bijective (S.toLin (S.inverse g)) := by + rw [toLin_inverse g h] + exact (LinearEquiv.ofBijective _ h).symm.bijective + +/-! + +## B. The contraction laws and uniqueness + +-/ + +/-- Inverting after `g` is the identity, as linear maps. -/ +lemma toLin_inverse_comp (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + S.toLin (S.inverse g) ∘ₗ S.toLin g = LinearMap.id := + LinearMap.ext fun x => by + rw [toLin_inverse g h] + exact (LinearEquiv.ofBijective _ h).symm_apply_apply x + +/-- `g` after inverting is the identity, as linear maps. -/ +lemma comp_toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + S.toLin g ∘ₗ S.toLin (S.inverse g) = LinearMap.id := + LinearMap.ext fun x => by + rw [toLin_inverse g h] + exact (LinearEquiv.ofBijective _ h).apply_symm_apply x + +/-- `g_μν g^νρ = δ_μ^ρ`, the contraction of `g` with its inverse on the adjacent indices. -/ +lemma contr_mul_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + Tensor.contr (ColorEquiv.matMul a b a.dual) + (S.tmulEquiv _ _ (S.twoSlotEquiv a b g ⊗ₜ S.twoSlotEquiv _ _ (S.inverse g))) = + S.deltaTensor a.dual := + (Tensor.contr_matMul_eq_deltaTensor_iff g (S.inverse g)).2 (toLin_inverse_comp g h) + +/-- `g^νμ g_μρ = δ^ν_ρ`, the contraction of the inverse with `g` on the adjacent indices. -/ +lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + Tensor.contr (ColorEquiv.matMul b.dual a.dual b) + (S.tmulEquiv _ _ (S.twoSlotEquiv _ _ (S.inverse g) ⊗ₜ S.twoSlotEquiv a b g)) = + S.deltaTensor b := + (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 (comp_toLin_inverse g h) + +/-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `g`. -/ +lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) + {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : + S.inverse g = N := + Eq.symm <| S.toLin.injective <| by + rw [toLin_inverse g h, ← LinearMap.id_comp (LinearEquiv.ofBijective _ h).symm.toLinearMap] + exact (LinearEquiv.eq_comp_toLinearMap_symm _ _).2 hN + +/-- A tensor `N` with `g ∘ N = id`, as linear maps, is the inverse of `g`. -/ +lemma inverse_eq_right_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) + {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin g ∘ₗ S.toLin N = LinearMap.id) : + S.inverse g = N := + Eq.symm <| S.toLin.injective <| by + rw [toLin_inverse g h, ← LinearMap.comp_id (LinearEquiv.ofBijective _ h).symm.toLinearMap] + exact (LinearEquiv.eq_toLinearMap_symm_comp _ _).2 hN + +/-- A tensor `N` with `g_μν N^νρ = δ_μ^ρ` is the inverse of `g`. -/ +lemma inverse_eq_of_contr_eq_deltaTensor (g : S.V a ⊗[k] S.V b) + (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} + (hN : Tensor.contr (ColorEquiv.matMul a b a.dual) + (S.tmulEquiv _ _ (S.twoSlotEquiv a b g ⊗ₜ S.twoSlotEquiv _ _ N)) = + S.deltaTensor a.dual) : S.inverse g = N := + inverse_eq_left_inv g h ((Tensor.contr_matMul_eq_deltaTensor_iff g N).1 hN) + +/-- The inverse of the inverse is the tensor itself, `(g⁻¹)⁻¹ = g`. -/ +lemma inverse_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : + S.inverse (S.inverse g) = g := + inverse_eq_left_inv _ (toLin_inverse_bijective g h) (comp_toLin_inverse g h) + +/-! + +## C. The inverse commutes with the group action + +-/ + +/-- The inverse of `Λg` is `Λ` applied to the inverse of `g`, `(Λg)⁻¹ = Λ(g⁻¹)`. -/ +lemma inverse_map_rep (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) : + S.inverse (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) = + TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := by + refine inverse_eq_left_inv _ (S.toLin_map_rep_bijective g h Λ) (LinearMap.ext fun x => ?_) + obtain ⟨y, rfl⟩ : ∃ y, S.rep a.dual Λ y = x := ⟨S.rep a.dual Λ⁻¹ x, by simp⟩ + rw [LinearMap.comp_apply, S.toLin_map_rep, S.toLin_map_rep (a := b.dual) (S.inverse g), + ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h, LinearMap.id_apply, + LinearMap.id_apply] + +/-- The inverse of an invariant tensor is invariant, `Λg = g` implies `Λ(g⁻¹) = g⁻¹`. -/ +lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) + (hg : TensorProduct.map (S.rep a Λ) (S.rep b Λ) g = g) : + TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) = S.inverse g := by + rw [← inverse_map_rep g h Λ, hg] + +/-! + +## D. The inverse of an invariant form + +-/ + +/-- The linear map of the form at `c` is bijective. -/ +lemma toLin_form_bijective (c : Color K) [S.HasForm c.kind] : + Function.Bijective (S.toLin (form c)) := + (S.toDual c.dual).bijective + +/-- The inverse of the form at `c` is the form at `c.dual`, as `η^μν` is the inverse of +`η_μν`. -/ +lemma inverse_form (c : Color K) [S.HasForm c.kind] : S.inverse (form c) = form c.dual := + inverse_eq_left_inv _ (toLin_form_bijective c) (form_inverse c.dual) + +/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`. The second slot of the form at `c` contracted +with the first slot of the form at `c.dual` is the delta. -/ +lemma contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : + Tensor.contr (ColorEquiv.matMul c c c.dual) + (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual)) = + S.deltaTensor c.dual := by + rw [formTensor, formTensor, ← inverse_form c] + exact contr_mul_inverse _ (toLin_form_bijective c) + +end Species + +end Tensors diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 24ff3c6b61..1d4d4b0bf4 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -15,59 +15,70 @@ one. ## i. Overview -For Lorentz tensors `V ⟨vec, up⟩ = ℝ⁴` and `V ⟨vec, down⟩ = (ℝ⁴)*`. The Minkowski metric is the -element `η_μν e^μ ⊗ e^ν` of the space `(ℝ⁴)* ⊗ (ℝ⁴)*`, and its inverse is the element -`η^μν e_μ ⊗ e_ν` of `ℝ⁴ ⊗ ℝ⁴`. They lower and raise indices, `v_ν = v^μ η_μν` and -`w^ν = w_μ η^μν`, and they are inverse to each other, `v^μ η_μν η^νρ = v^ρ`. - -An invariant form on a species `S`, the class `S.WithInvariantForm`, gives such a pair for every -color. The form `form c` is an element of `V c ⊗ V c`, written `g^μν` at an upper color and `g_μν` -at a lower one. It is invariant under the group, and the forms at `c` and at `c.dual` are inverse -to each other. A metric, the class `S.WithMetric`, is an invariant form that is symmetric, -`g_μν = g_νμ`, as `η`. The Weyl `ε` is an invariant form that is antisymmetric, so it is not a -metric. - -The form is a choice, since `-η` serves as well as `η`. Given `g_μν`, its inverse `g^μν` is unique -(`form_inverse_unique`). The scalars `k` are only a commutative ring, over which a non-degenerate -form need not have an inverse, so the class stores the inverse and the law `form_inverse` checks -it. +An invariant form lowers and raises indices. For Lorentz tensors it is the Minkowski metric, +`η_μν` in `(ℝ⁴)* ⊗ (ℝ⁴)*`, with inverse `η^μν` in `ℝ⁴ ⊗ ℝ⁴`. It lowers `v_ν = v^μ η_μν`, raises +`w^ν = w_μ η^μν`, and the two are inverse, `v^μ η_μν η^νρ = v^ρ`. + +An invariant form on a kind `κ`, the class `S.HasForm κ`, is a chosen invariant invertible +bilinear form. It gives an element `form c` of `V c ⊗ V c` at both colors of the kind, written +`g^μν` at the upper color and `g_μν` at the lower one. It is invariant under the group, and the +forms at `c` and at `c.dual` are inverse to each other. Not every kind needs a form. The SU(3) +species has one on the adjoint and none on the triplet, so it has a `HasForm` instance at the +adjoint only and lowering a triplet index does not typecheck. The form is a choice, since `-η` +serves as well as `η`. The class stores the inverse, checked by the law `form_inverse`, since over +a commutative ring `k` a non-degenerate form need not have an inverse. + +Symmetry is an optional property of the chosen form. The class `S.IsSymmetricForm κ` says that +`g_μν = g_νμ`, as for `η`, and a metric is a symmetric form. The class `S.IsAntisymmetricForm κ` +says that `g_μν = -g_νμ`, as for the Weyl `ε`. A form on a kind that is a sum of two kinds may be +neither, and lowering and raising never ask for either property. `S.toDual c : V c ≃ V c.dual` contracts a vector with the first index of the form at `c.dual`. At an upper color it lowers the index, `v^μ ↦ v^μ g_μν`, and at a lower color it raises it, -`w_μ ↦ w_μ g^μν`. Its inverse is `S.toDual c.dual`, by `rfl` (`toDual_symm`). - -For a symmetric metric such as `η` the first index gives the textbook `v_ν = η_νμ v^μ`, as -`TensorSpecies.Tensor.toDualMapAtIndex` does. For the antisymmetric `ε` it gives +`w_μ ↦ w_μ g^μν`. Its inverse is `S.toDual c.dual`, by `rfl`. For a symmetric metric such as `η` +the first index gives the textbook `v_ν = η_νμ v^μ`. For the antisymmetric `ε` it gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`. With `ε^12 = ε_21 = 1` this is minus the convention `ψ_α = ε_αβ ψ^β`, so a Weyl species following that convention stores `-ε`. -`S.toDualAt i hi h` lowers or raises the index at the slot `i` of a tensor and keeps the others, as -in `T^μν ↦ T^μ_ν` for `i` the second slot. The caller states the colors `c'` of the result, such as -`![⟨vec, up⟩, ⟨vec, down⟩]`. The hypothesis `hi` says that `c'` has the dual color at `i`, and `h` -that it agrees with the colors `c` of the tensor elsewhere. +`S.toDualAt i a ha hi h` lowers or raises the index at the slot `i` of a tensor and keeps the +others, as in `T^μν ↦ T^μ_ν` for `i` the second slot. It is `congrAt` of `toDual`. The caller +names the color `a` of the slot, with `ha : c i = a`, so that the form is found at the kind of `a`. +Lowering and raising at that slot can then use the same instance without moving it, as in +`toDualAt_toDualAt`. The caller also states the colors `c'` of the result, such as +`![⟨vec, up⟩, ⟨vec, down⟩]`. The hypothesis `hi` says that `c'` has the dual color `a.dual` at `i`, +and `h` that it agrees with the colors `c` of the tensor elsewhere. `S.formTensor c` is the form as a tensor on two slots, `η^μν` at `⟨vec, up⟩` and `η_μν` at -`⟨vec, down⟩`. The law `form_inverse` stated for tensors is `g^μν g_νρ = δ^μ_ρ` -(`contr_formTensor_formTensor`). Lowering an index is contracting it with the form, -`M^μν g_νρ = M^μ_ρ` (`toDualAt_eq_contr_formTensor`). +`⟨vec, down⟩`. Lowering an index is contracting it with the form tensor, `M^μν g_νρ = M^μ_ρ`. ## ii. Key results -- `Species.WithInvariantForm` : an invariant form with its inverse, `g_μν` and `g^μν`. -- `Species.WithMetric` : a symmetric invariant form, `g_μν = g_νμ`. -- `Species.toDual` : `S.V c ≃ S.V c.dual`, lowering an upper index and raising a lower one. -- `Species.toDual_symm` : raising is the inverse of lowering. -- `Species.form_inverse_unique` : `g^μν` is determined by `g_μν`. -- `Species.toDual_rep`, `Species.toDualAt_smul` : `(Λv)_ν = Λ(v_ν)`, at one index of a tensor. -- `Species.toDualAt` : lowering or raising the index at one slot of a tensor. -- `Species.toDualAt_toDualAt` : raising at a slot undoes lowering there. -- `Species.toDualAt_twoSlotEquiv` : lowering the second index of a tensor on two slots, +- `Species.HasForm` is an invariant form on a kind with its inverse, `g_μν` and `g^μν`, and + `Species.form` is the form at a color of that kind, with `Species.form_invariant` and + `Species.form_inverse`. +- `Species.formMatrix` gives the components `g_μν` of the form, on which each species states its + component lemma. +- `Species.IsSymmetricForm` and `Species.IsAntisymmetricForm` say that the form is symmetric or + antisymmetric, with `Species.form_symm` and `Species.form_antisymm`. +- `Species.toDual` is `S.V c ≃ S.V c.dual`, lowering an upper index and raising a lower one. +- `Species.toDual_symm` proves that raising is the inverse of lowering. +- `Species.toDual_rep` and `Species.toDualAt_smul` prove `(Λv)_ν = Λ(v_ν)`, for a vector and at + one index of a tensor. +- `Species.toDualAt` lowers or raises the index at one slot of a tensor. +- `Species.toDualAt_toDualAt` proves that raising at a slot undoes lowering there, and + `Species.toDualAt_comm` that lowering at two different slots commute. +- `Species.tensorBasis_repr_toDualAt` gives lowering at a slot in components, + `T^…_ν… = T^…μ… g_μν`. +- `Species.toDualAt_twoSlotEquiv` proves that lowering the second index of a tensor on two slots, `M^μν ↦ M^μ_ν`, is `toDual` on the second factor. -- `Species.toDualAt_eq_contr_formTensor` : lowering is contracting with the form, - `M^μν g_νρ = M^μ_ρ`. -- `Species.formTensor`, `Species.formTensor_invariant` : the form as an invariant tensor on two - slots. -- `Species.contr_formTensor_formTensor` : `g^μν g_νρ = δ^μ_ρ`. +- `Species.toDualAt_eq_contr` proves that lowering at any slot is contracting with the form, + `T^…_ν… = T^…μ… g_μν`, and `Species.toDualAt_eq_contr_formTensor` gives the same at the second + slot of a tensor on two slots, `M^μν g_νρ = M^μ_ρ`. +- `Species.formTensor` is the form as a tensor on two slots, and `Species.formTensor_invariant` + proves that it is invariant. +- `Species.tensorBasis_repr_formTensor` gives the components of the form tensor. +- `Species.formTensor_symm` and `Species.formTensor_antisymm` prove `g^μν = g^νμ` and + `g^μν = -g^νμ` as tensors for a symmetric or antisymmetric form. ## iii. Table of contents @@ -78,6 +89,8 @@ that it agrees with the colors `c` of the tensor elsewhere. ## iv. References +None. + -/ @[expose] public section @@ -97,29 +110,69 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K -/ -/-- An invariant form on a species: for every color `c` an invariant element `g` of `V c ⊗ V c`, -`g^μν` at an upper color and `g_μν` at a lower one, with the forms at `c` and at `c.dual` inverse -to each other. It need not be symmetric, so it covers both `η` and the Weyl `ε`. Every color must -carry one, so a species containing the SU(3) triplet has none. The invariance at `c.dual` follows -from that at `c` and `form_inverse`, but a field for every color cannot state only half. -/ -class WithInvariantForm (S : Species k G K basisIdx) where - /-- The form at the color `c`, an element of `V c ⊗ V c`. -/ - form : (c : Color K) → S.V c ⊗[k] S.V c +variable (S : Species k G K basisIdx) in +/-- A chosen invariant invertible bilinear form on the kind `κ`. At both colors of `κ` it gives an +invariant element `g` of `V c ⊗ V c`, `g^μν` at the upper color and `g_μν` at the lower one, with +the two forms inverse to each other. It need not be symmetric, so it covers both `η` and the Weyl +`ε`. The invariance at the lower color follows from that at the upper one and `formAt_inverse`, +but a field for both variances cannot state only half. -/ +class HasForm (κ : K) where + /-- The form at the color `⟨κ, v⟩`, an element of `V ⟨κ, v⟩ ⊗ V ⟨κ, v⟩`. -/ + formAt : (v : Variance) → S.V ⟨κ, v⟩ ⊗[k] S.V ⟨κ, v⟩ /-- The form is invariant, `Λ g = g` for every `Λ : G`. -/ - form_invariant : ∀ c (Λ : G), TensorProduct.map (S.rep c Λ) (S.rep c Λ) (form c) = form c - /-- The forms at `c` and at `c.dual` are inverse to each other, `v^μ g_μν g^νρ = v^ρ` at an - upper color `c`. The vector is contracted with the first index of each form. -/ - form_inverse : ∀ c, pairingToLinearMap (S.contr c.dual).toLinearMap (form c) ∘ₗ - pairingToLinearMap (S.contr c).toLinearMap (form c.dual) = LinearMap.id + formAt_invariant : ∀ v (Λ : G), + TensorProduct.map (S.rep ⟨κ, v⟩ Λ) (S.rep ⟨κ, v⟩ Λ) (formAt v) = formAt v + /-- The forms at the two variances are inverse as linear maps, `v^μ g_μν g^νρ = v^ρ`. The + vector is contracted with the first index of each form. -/ + formAt_inverse : ∀ v, S.toLin (formAt v) ∘ₗ S.toLin (formAt v.dual) = LinearMap.id -export WithInvariantForm (form form_invariant form_inverse) +variable {S : Species k G K basisIdx} -/-- A metric on a species: an invariant form that is symmetric at every color, as `η`. -/ -class WithMetric (S : Species k G K basisIdx) extends S.WithInvariantForm where - /-- The form is symmetric, `g_μν = g_νμ`. -/ - form_symm : ∀ c, TensorProduct.comm k (S.V c) (S.V c) (form c) = form c +/-- The form at the color `c`, `g^μν` at an upper color and `g_μν` at a lower one. -/ +noncomputable def form (c : Color K) [S.HasForm c.kind] : S.V c ⊗[k] S.V c := + HasForm.formAt c.variance -variable {S : Species k G K basisIdx} [S.WithInvariantForm] +variable (S) in +/-- The components of the form at `c`, `g^μν` at an upper color and `g_μν` at a lower one, its +coordinates in Mathlib's product basis. Component lemmas of a species are stated on it, so that +`simp` finds them at a literal color. -/ +noncomputable def formMatrix (c : Color K) [S.HasForm c.kind] : + Matrix (basisIdx c.kind) (basisIdx c.kind) k := + fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr (form c) (μ, ν) + +/-- The form is invariant, `Λ g = g` for every `Λ : G`. -/ +lemma form_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : + TensorProduct.map (S.rep c Λ) (S.rep c Λ) (form c) = form c := + HasForm.formAt_invariant c.variance Λ + +/-- The forms at `c` and at `c.dual` are inverse as linear maps, `v^μ g_μν g^νρ = v^ρ` at an +upper color `c`. -/ +lemma form_inverse (c : Color K) [S.HasForm c.kind] : + S.toLin (form c) ∘ₗ S.toLin (form c.dual) = LinearMap.id := + HasForm.formAt_inverse c.variance + +variable (S) in +/-- The form on the kind `κ` is symmetric, `g_μν = g_νμ`, as `η`. A metric is a symmetric form. -/ +class IsSymmetricForm (κ : K) [S.HasForm κ] : Prop where + /-- Swapping the two indices of the form gives the form. -/ + comm_form : ∀ v, TensorProduct.comm k _ _ (form (S := S) ⟨κ, v⟩) = form ⟨κ, v⟩ + +variable (S) in +/-- The form on the kind `κ` is antisymmetric, `g_μν = -g_νμ`, as the Weyl `ε`. Antisymmetric is +not alternating: when `2 = 0` in `k` the form `[1]` on `k` is antisymmetric with `g(1, 1) = 1`. -/ +class IsAntisymmetricForm (κ : K) [S.HasForm κ] : Prop where + /-- Swapping the two indices of the form gives minus the form. -/ + comm_form : ∀ v, TensorProduct.comm k _ _ (form (S := S) ⟨κ, v⟩) = -form ⟨κ, v⟩ + +/-- A symmetric form, `g_μν = g_νμ`. -/ +lemma form_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : + TensorProduct.comm k (S.V c) (S.V c) (form c) = form c := + IsSymmetricForm.comm_form c.variance + +/-- An antisymmetric form, `g_μν = -g_νμ`. -/ +lemma form_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : + TensorProduct.comm k (S.V c) (S.V c) (form c) = -form c := + IsAntisymmetricForm.comm_form c.variance /-! @@ -130,30 +183,29 @@ variable {S : Species k G K basisIdx} [S.WithInvariantForm] variable (S) in /-- The vector `v` of color `c` contracted with the first index of the form at `c.dual`. It lowers an upper index, `v^μ ↦ v^μ g_μν`, and raises a lower one, `w_μ ↦ w_μ g^μν`. -/ -noncomputable def toDual (c : Color K) : S.V c ≃ₗ[k] S.V c.dual := - LinearEquiv.ofLinearMap (pairingToLinearMap (S.contr c).toLinearMap (form c.dual)) - (pairingToLinearMap (S.contr c.dual).toLinearMap (form c)) - (WithInvariantForm.form_inverse c.dual) (WithInvariantForm.form_inverse c) +noncomputable def toDual (c : Color K) [S.HasForm c.kind] : S.V c ≃ₗ[k] S.V c.dual := + LinearEquiv.ofLinearMap (S.toLin (form c.dual)) (S.toLin (form c)) (form_inverse c.dual) + (form_inverse c) -lemma toDual_apply (c : Color K) (v : S.V c) : +/-- `toDual` contracts `v` with the first index of the form at `c.dual`. -/ +lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : S.toDual c v = pairingToLinearMap (S.contr c).toLinearMap (form c.dual) v := rfl -/-- Raising is the inverse of lowering: the inverse of `S.toDual c` is `S.toDual c.dual`. -/ -lemma toDual_symm (c : Color K) : (S.toDual c).symm = S.toDual c.dual := rfl - -/-- The inverse of the form is unique: an element `N` of `V c.dual ⊗ V c.dual` inverse to the form -at `c` is the form at `c.dual`. -/ -lemma form_inverse_unique (c : Color K) {N : S.V c.dual ⊗[k] S.V c.dual} - (h : pairingToLinearMap (S.contr c.dual).toLinearMap (form c) ∘ₗ - pairingToLinearMap (S.contr c).toLinearMap N = LinearMap.id) : - N = form c.dual := - S.pairingToLinearMap_contr_injective c ((S.toDual c).toLinearMap_symm_comp_eq _ _ |>.1 h) +/-- Raising is the inverse of lowering. The inverse of `S.toDual c` is `S.toDual c.dual`. -/ +lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDual c.dual := rfl /-- Lowering and raising commute with the group action, `(Λv)_ν = Λ(v_ν)`. -/ -lemma toDual_rep (c : Color K) (g : G) (v : S.V c) : +lemma toDual_rep (c : Color K) [S.HasForm c.kind] (g : G) (v : S.V c) : S.toDual c (S.rep c g v) = S.rep c.dual g (S.toDual c v) := by rw [toDual_apply, toDual_apply, ← S.pairingToLinearMap_contr_map_rep c g, form_invariant] +/-- Lowering or raising a vector in components, `v_ν = v^μ g_μν`. -/ +lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : + (S.basis c.dual).repr (S.toDual c v) ν = ∑ μ, (S.basis c).repr v μ * + S.formMatrix c.dual μ ν := by + rw [toDual_apply, basis_repr_pairingToLinearMap_contr] + rfl + /-! ## C. Lowering and raising at one index @@ -164,82 +216,103 @@ variable {ι : Type} [DecidableEq ι] variable (S) in /-- `S.toDual` at the slot `i` of a tensor and the identity at the other slots, as in -`T^μν ↦ T^μ_ν` for `i` the second slot. The colors `c'` of the result have the dual color at `i` -by `hi` and agree with `c` elsewhere by `h`. It is Mathlib's `PiTensorProduct.congr`. -/ -noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) - (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c ≃ₗ[k] S.Tensor c' := - -- At the slot `i` the map to the dual color, moved along `hi`. At every other slot `x` the - -- identity, moved along `h x`. - S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => if hx : x = i then - hx ▸ (S.toDual (c i) ≪≫ₗ LinearEquiv.cast (M := S.V) hi.symm) - else LinearEquiv.cast (M := S.V) (h x hx)) ≪≫ₗ (S.toPiTensor c').symm +`T^μν ↦ T^μ_ν` for `i` the second slot. The slot has the color `a` by `ha`, and the form is the one +at the kind of `a`. The colors `c'` of the result have the dual color at `i` by `hi` and agree +with `c` elsewhere by `h`. It is `congrAt` of `toDual`. -/ +noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) : + S.Tensor c ≃ₗ[k] S.Tensor c' := + S.congrAt i (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm) h /-- Lowering or raising at a slot commutes with the group action, `(ΛT)^μ_ν = Λ(T^μ_ν)`. -/ -lemma toDualAt_smul {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) - (h : ∀ x, x ≠ i → c x = c' x) (g : G) (t : S.Tensor c) : - S.toDualAt i hi h (g • t) = g • S.toDualAt i hi h t := - map_smul_of_pure (S.toDualAt i hi h) g (fun p => by - simp only [smul_pure, toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, - PiTensorProduct.congr_tprod, toPiTensor_symm_tprod] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp only [dite_true, LinearEquiv.trans_apply, toDual_rep, cast_rep] - · simp only [hx, dite_false, cast_rep]) t +lemma toDualAt_smul {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (g : G) + (t : S.Tensor c) : S.toDualAt i a ha hi h (g • t) = g • S.toDualAt i a ha hi h t := + congrAt_smul i _ h (fun g x => by simp only [LinearEquiv.trans_apply, toDual_rep, cast_rep]) + g t /-- On a pure tensor, `toDualAt` applies `toDual` to the vector at `i` and keeps the others. The result `q` is explicit, so that for concrete colors `hqi` and `hq` hold by `rfl`. -/ -lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) - (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) - (hqi : q i = LinearEquiv.cast (R := k) (M := S.V) hi.symm (S.toDual (c i) (p i))) - (hq : ∀ x (hx : x ≠ i), q x = LinearEquiv.cast (R := k) (M := S.V) (h x hx) (p x)) : - S.toDualAt i hi h (S.pure p) = S.pure q := by - simp only [toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, PiTensorProduct.congr_tprod, - toPiTensor_symm_tprod] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp [hqi] - · simp [hx, hq x hx] +lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) + (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) + (hqi : q i = S.cast hi.symm (S.toDual a (S.cast ha (p i)))) + (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : + S.toDualAt i a ha hi h (S.pure p) = S.pure q := + congrAt_pure i _ h p q hqi hq /-- Raising at a slot undoes lowering at that slot, `(T^μ_ν)^ν = T^μν`. -/ -lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (hi : c' i = (c i).dual) - (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : - S.toDualAt (c := c') (c' := c) i (by rw [hi]) (fun x hx => (h x hx).symm) - (S.toDualAt i hi h t) = t := by - induction t using inductionOn_pure with - | h p => - simp only [toDualAt, LinearEquiv.trans_apply, toPiTensor_pure, PiTensorProduct.congr_tprod, - toPiTensor_symm_tprod] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp only [dite_true, LinearEquiv.trans_apply] - generalize_proofs _ h₁ h₂ - revert h₁ h₂ hi - generalize c' x = d - rintro h₁ h₂ rfl - exact (S.toDual _).symm_apply_apply (p x) - · simp [hx] - | hsmul r t ht => rw [map_smul, map_smul, ht] - | hadd x y hx hy => rw [map_add, map_add, hx, hy] +lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + S.toDualAt i a.dual hi ha (fun x hx => (h x hx).symm) (S.toDualAt i a ha hi h t) = t := by + rw [toDualAt, toDualAt, show S.cast hi ≪≫ₗ S.toDual a.dual ≪≫ₗ S.cast ha.symm = + (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm).symm from rfl, ← congrAt_symm] + exact (S.congrAt i _ h).symm_apply_apply t + +/-- Lowering or raising at two different slots commute, `(T_μ)_ν = (T_ν)_μ`. The colors `c₁` and +`c₂` are those after the first step in either order. On literal slots the hypotheses `h₂` and +`h₂'` are found automatically, as the index notation finds them. -/ +lemma toDualAt_comm {c c₁ c₂ c' : ι → Color K} {i j : ι} (hij : i ≠ j) (a b : Color K) + [S.HasForm a.kind] [S.HasForm b.kind] + {ha : c i = a} {hi : c₁ i = a.dual} {h : ∀ x, x ≠ i → c x = c₁ x} + {hb : c₁ j = b} {hj : c' j = b.dual} {h' : ∀ x, x ≠ j → c₁ x = c' x} + (hb₂ : c j = b) (hj₂ : c₂ j = b.dual) (ha₂ : c₂ i = a) (hi₂ : c' i = a.dual) + (h₂ : ∀ x, x ≠ j → c x = c₂ x := by + intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) + (h₂': ∀ x, x ≠ i → c₂ x = c' x := by + intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) (t : S.Tensor c) : + S.toDualAt j b hb hj h' (S.toDualAt i a ha hi h t) = + S.toDualAt i a ha₂ hi₂ h₂' (S.toDualAt j b hb₂ hj₂ h₂ t) := + congrAt_comm hij _ _ _ _ _ _ _ _ + (fun v => by simp only [LinearEquiv.trans_apply, LinearEquiv.cast_apply, cast_cast]) + (fun v => by simp only [LinearEquiv.trans_apply, LinearEquiv.cast_apply, cast_cast]) t /-- Lowering or raising the second index of a tensor on two slots, `M^μν ↦ M^μ_ν`, is `toDual` on the second factor of `M ∈ V a ⊗ V c`. -/ -lemma toDualAt_twoSlotEquiv (a c : Color K) (M : S.V a ⊗[k] S.V c) : - S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 rfl +lemma toDualAt_twoSlotEquiv (a c : Color K) [S.HasForm c.kind] (M : S.V a ⊗[k] S.V c) : + S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 c rfl rfl (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = S.twoSlotEquiv a c.dual ((S.toDual c).toLinearMap.lTensor _ M) := by induction M using TensorProduct.inductionOn with | add M M' hM hM' => simp only [map_add, hM, hM'] | tmul x y => rw [twoSlotEquiv_tmul, LinearMap.lTensor_tmul, twoSlotEquiv_tmul] - exact toDualAt_pure _ _ _ _ _ rfl + exact toDualAt_pure _ _ _ _ _ _ _ rfl (fun x hx => by fin_cases x <;> first | rfl | exact absurd rfl hx) +/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = T^…μ… g_μν`. The +labels `φ` of the result are cast to labels of `t`, and the label at `i` is replaced by the summed +`μ`. For concrete colors the casts are along `rfl`. -/ +lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a : Color K) + (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) + (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (S.toDualAt i a ha hi h t) φ = + ∑ μ, S.formMatrix a.dual μ (cast (congrArg (basisIdx ∘ Color.kind) hi) (φ i)) * + (S.tensorBasis c).repr t (Function.update (fun x => cast (by + by_cases hx : x = i + · subst hx; rw [hi, ha] + · rw [h x hx]) (φ x)) i (cast (congrArg (basisIdx ∘ Color.kind) ha.symm) μ)) := by + subst ha + induction t using inductionOn_pure with + | hsmul r t ht => + simp only [map_smul, Finsupp.smul_apply, ht, smul_eq_mul, Finset.mul_sum] + exact Finset.sum_congr rfl fun _ _ => by ring + | hadd t t' ht ht' => + simp only [map_add, Finsupp.add_apply, ht, ht', mul_add, Finset.sum_add_distrib] + | h p => + rw [toDualAt_pure i _ rfl hi h p (fun x => if hx : x = i then hx ▸ + S.cast hi.symm (S.toDual (c i) (p i)) else S.cast (h x hx) (p x)) + (dite_eq_left rfl) (fun x hx => dite_eq_right hx), tensorBasis_repr_pure, + ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), dite_eq_left rfl, basis_repr_cast, + basis_repr_toDual, Finset.sum_mul] + refine Finset.sum_congr rfl fun μ _ => ?_ + rw [tensorBasis_repr_pure, ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), + Function.update_self, mul_left_comm, mul_assoc] + congr 2 + refine Finset.prod_congr rfl fun x hx => ?_ + have hx := Finset.ne_of_mem_erase hx + rw [dite_eq_right hx, basis_repr_cast, Function.update_of_ne hx] + /-! ## D. The form as a tensor @@ -249,32 +322,55 @@ lemma toDualAt_twoSlotEquiv (a c : Color K) (M : S.V a ⊗[k] S.V c) : variable (S) in /-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a lower one. -/ -noncomputable def formTensor (c : Color K) : S.Tensor ![c, c] := +noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] := S.twoSlotEquiv c c (form c) -/-- The form is an invariant tensor: every group element fixes it, as every Lorentz +/-- The form is an invariant tensor. Every group element fixes it, as every Lorentz transformation fixes the Minkowski metric `η`. -/ -lemma formTensor_invariant (c : Color K) (Λ : G) : Λ • S.formTensor c = S.formTensor c := by +lemma formTensor_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : + Λ • S.formTensor c = S.formTensor c := by rw [formTensor, ← twoSlotEquiv_map_rep, form_invariant] -/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`: the second slot of the form at `c` contracted with -the first slot of the form at `c.dual` is the delta. -/ -lemma contr_formTensor_formTensor (c : Color K) : - Tensor.contr (ColorEquiv.matMul c c c.dual) - (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual)) = - S.deltaTensor c.dual := by - rw [formTensor, formTensor, deltaTensor, Tensor.contr_matMul] - congr 1 - -- Both tensors in `V c ⊗ V c.dual` act on `V c.dual` as the identity, the first by the law of - -- the form and the second by the snake law. - apply S.pairingToLinearMap_contr_injective c.dual - rw [pairingToLinearMap_lTensor, S.snake c.dual] - exact form_inverse c.dual +/-- The components of the form tensor are those of the form, `g_μν` at the pair of labels +`(μ 0, μ 1)`. -/ +lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] + (μ : Π j, basisIdx (![c, c] j).kind) : + (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := + tensorBasis_repr_twoSlotEquiv _ _ _ μ + +/-- A symmetric form is symmetric under the slot swap `σ` as a tensor, `g^μν = g^νμ`. -/ +lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] + {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : + S.formTensor c = S.reindex σ (S.formTensor c) := by + rw [formTensor, reindex_twoSlotEquiv hσ, form_symm] + +/-- An antisymmetric form is antisymmetric under the slot swap `σ` as a tensor, +`g^μν = -g^νμ`. -/ +lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : + S.formTensor c = S.reindex σ (-S.formTensor c) := by + rw [formTensor, ← map_neg, reindex_twoSlotEquiv hσ, map_neg, form_antisymm, neg_neg] + +/-- Lowering or raising at the slot `i` is contracting that slot with the first index of the form, +`T^…_ν… = T^…μ… g_μν`. -/ +lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) + (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.formTensor a.dual)) := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [map_smul, ht, ← smul_tmul', map_smul, map_smul] + | hadd t t' ht ht' => rw [map_add, ht, ht', add_tmul, map_add, map_add] + | h p => + let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) + rw [toDualAt_pure i a ha hi h p (Function.update q i _) (Function.update_self _ _ _) + (fun x hx => by simp [q, hx]), toDual_apply, formTensor, + Tensor.contr_matMulAt_pure i a a.dual ha hi h _ p q (fun x hx => by simp [q, hx])] /-- Lowering the second index is contracting it with the form, `M^μν g_νρ = M^μ_ρ` at an upper -color `c`, along `ColorEquiv.matMul`. -/ -lemma toDualAt_eq_contr_formTensor (a c : Color K) (M : S.V a ⊗[k] S.V c) : - S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 rfl +color `c`, along `ColorEquiv.matMul`. It is `toDualAt_eq_contr` for a tensor on two slots, in the +form of a product of matrices. -/ +lemma toDualAt_eq_contr_formTensor (a c : Color K) [S.HasForm c.kind] (M : S.V a ⊗[k] S.V c) : + S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 c rfl rfl (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = Tensor.contr (ColorEquiv.matMul a c c.dual) (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.formTensor c.dual)) := by diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 7db6e8e89b..41a14faf3d 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -16,7 +16,7 @@ The product `T ⊗ U` of two tensors has the indices of `T` followed by those of Let `T = T^μ_ν e_μ ⊗ e^ν` be a Lorentz tensor with colors `c₁ = ![⟨vec, up⟩, ⟨vec, down⟩]` on the slots `Fin 2`, and `U = U^ρ e_ρ` a vector with colors `c₂ = ![⟨vec, up⟩]` on the slot `Fin 1`. -Here `V ⟨vec, up⟩ = ℝ⁴` and `V ⟨vec, down⟩ = (ℝ⁴)*`. Their product is the element +Their product is the element `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` of the space `ℝ⁴ ⊗ (ℝ⁴)* ⊗ ℝ⁴`. @@ -37,8 +37,9 @@ The product is associative and symmetric up to a relabeling of slots. For a thir `Λ`, such as a Lorentz transformation, acts on a product factor by factor, `Λ T ⊗ Λ U = Λ (T ⊗ U)`. An element `M` of `V a ⊗ V b` is the tensor `S.twoSlotEquiv a b M` with colors `![a, b]`. For -example, the metric `η_μν e^μ ⊗ e^ν` in `(ℝ⁴)* ⊗ (ℝ⁴)*` is the tensor `η_μν`. The delta of an -upper color `c` is the tensor `S.deltaTensor c = δ_μ^ν`, with colors `![c.dual, c]`. +example, the metric `η_μν e^μ ⊗ e^ν` in `(ℝ⁴)* ⊗ (ℝ⁴)*` is the tensor `η_μν`. The delta tensor +`S.deltaTensor c` has colors `![c.dual, c]`. It is `δ_μ^ν` for an upper color `c` and `δ^μ_ν` for +a lower one. The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ` (`tensorBasis_repr_tmulEquiv`). The components of `S.twoSlotEquiv a b M` are the coordinates of @@ -46,29 +47,43 @@ The components of a product are the products of the components, `(T ⊗ U)^μ_ν ## ii. Key results -- `prodColors` : the colors of a product. -- `ColorEquiv.sumCongr`, `ColorEquiv.sumAssoc`, `ColorEquiv.sumComm` : relabelings of the slots - of a product. -- `Species.tmulEquiv` : the product of two tensors, `T ⊗ₜ U ↦ T ⊗ U`. -- `Species.tmulEquiv_assoc`, `Species.tmulEquiv_comm` : `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and +- `prodColors` gives the colors of a product. +- `ColorEquiv.sumCongr`, `ColorEquiv.sumAssoc`, `ColorEquiv.sumComm`, `ColorEquiv.swapLast` and + `ColorEquiv.ofFinTwo` relabel the slots of a product. +- `Species.tmulEquiv` is the product of two tensors, `T ⊗ₜ U ↦ T ⊗ U`. +- `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. -- `Species.tmulEquiv_smul` : `Λ T ⊗ Λ U = Λ (T ⊗ U)`. -- `Species.twoSlotEquiv`, `Species.deltaTensor` : an element of `V a ⊗ V b` as a tensor on two - slots, and the delta `δ_μ^ν`. -- `Species.twoSlotEquiv_map_rep` : `Λ` acts on a tensor on two slots factor by factor. -- `Species.tensorBasis_repr_tmulEquiv` : `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. -- `Species.tensorBasis_repr_twoSlotEquiv` : the components of a tensor on two slots. +- `Species.reindex_sumCongr_tmulEquiv` proves that relabeling each factor relabels the product, + with `Species.tmulEquiv_reindex_left` and `Species.tmulEquiv_reindex_right` for one factor. +- `Species.tmulEquiv_swapLast` proves `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` up to relabeling. +- `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. +- `Species.oneSlotEquiv` turns a vector of `V c` into a tensor on one slot, and + `Species.oneSlotEquiv_rep` proves that it commutes with the group action. +- `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots, and + `Species.deltaTensor` is the delta `δ_μ^ν`. +- `Species.twoSlotEquiv_map_rep` proves that `Λ` acts on a tensor on two slots factor by factor. +- `Species.reindex_twoSlotEquiv` proves that swapping the two slots swaps the factors, + `M^μν ↦ M^νμ`. +- `Species.deltaTensor_invariant` proves that the delta tensor is invariant. +- `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. +- `Species.tensorBasis_repr_oneSlotEquiv` and `Species.tensorBasis_repr_twoSlotEquiv` give the + components of a tensor on one and on two slots. +- `Species.tensorBasis_repr_deltaTensor` gives the components of the delta tensor. +- `Species.tensorBasis_repr_deltaTensor_eq_one` proves that in dual bases they form the identity + matrix. ## iii. Table of contents - A. The colors and relabelings of a product - B. The product - C. Associativity, symmetry and equivariance -- D. Tensors on two slots +- D. Tensors on one and two slots - E. Components ## iv. References +None. + -/ @[expose] public section @@ -113,6 +128,19 @@ def sumComm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ColorEquiv (prodColors c₁ c₂) (prodColors c₂ c₁) := ⟨Equiv.sumComm ι₁ ι₂, by rintro (_ | _) <;> rfl⟩ +/-- The slots of `(T ⊗ W) ⊗ U` relabeled as those of `(T ⊗ U) ⊗ W`, swapping the last two +factors. -/ +def swapLast (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : + ColorEquiv (prodColors (prodColors c₁ c₃) c₂) (prodColors (prodColors c₁ c₂) c₃) := + ⟨(Equiv.sumAssoc _ _ _).trans ((Equiv.sumCongr (Equiv.refl _) (Equiv.sumComm _ _)).trans + (Equiv.sumAssoc _ _ _).symm), by rintro ((_ | _) | _) <;> rfl⟩ + +/-- The slots `Fin 2` of a tensor `T^μν` relabeled as the slots `Fin 1 ⊕ Fin 1` of a product +`x^μ ⊗ y^ν`. It is Mathlib's `finSumFinEquiv`, reversed. `Species.twoSlotEquiv` relabels by its +inverse. -/ +noncomputable def ofFinTwo (a b : Color K) : ColorEquiv ![a, b] (prodColors ![a] ![b]) := + (ColorEquiv.mk (c₂ := ![a, b]) finSumFinEquiv).symm + end ColorEquiv namespace Species @@ -120,7 +148,7 @@ namespace Species variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} -variable {ι₁ ι₂ ι₃ : Type} +variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} /-! @@ -201,6 +229,61 @@ lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ rw [tmulEquiv_pure, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) +/-- Relabeling the product along `σ.sumCongr τ` is relabeling its factors along `σ` and `τ`. -/ +lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} + {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') + (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + S.reindex (σ.sumCongr τ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = + S.tmulEquiv c₁' c₂' (S.reindex σ t₁ ⊗ₜ S.reindex τ t₂) := by + induction t₁ using inductionOn_pure with + | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] + | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] + | h f₁ => + induction t₂ using inductionOn_pure with + | hsmul r t ht => simp only [tmul_smul, map_smul, ht] + | hadd t t' ht ht' => simp only [tmul_add, map_add, ht, ht'] + | h f₂ => + simp only [tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + +/-- Relabeling the first factor relabels the product. With `tmulEquiv_reindex_right` it moves +relabelings out of products, the form `tensor_regroup` works in. -/ +lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} + (σ : ColorEquiv c₁ c₁') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = + S.reindex (σ.sumCongr (.refl c₂)) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by + rw [reindex_sumCongr_tmulEquiv, reindex_refl] + +/-- Relabeling the second factor relabels the product. With `tmulEquiv_reindex_left` it moves +relabelings out of products, the form `tensor_regroup` works in. -/ +lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} + (σ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = + S.reindex ((ColorEquiv.refl c₁).sumCongr σ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by + rw [reindex_sumCongr_tmulEquiv, reindex_refl] + +/-- Swapping the last two factors of a product, `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` after the slots of +`U` are moved before those of `W` by `ColorEquiv.swapLast`. -/ +lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} + (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : + S.reindex (ColorEquiv.swapLast c₁ c₂ c₃) + (S.tmulEquiv _ c₂ (S.tmulEquiv c₁ c₃ (t₁ ⊗ₜ t₃) ⊗ₜ t₂)) = + S.tmulEquiv _ c₃ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃) := by + induction t₁ using inductionOn_pure with + | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] + | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] + | h f₁ => + induction t₂ using inductionOn_pure with + | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] + | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] + | h f₂ => + induction t₃ using inductionOn_pure with + | hadd t t' ht ht' => simp only [tmul_add, add_tmul, map_add, ht, ht'] + | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] + | h f₃ => + simp only [tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with (_ | _) | _ <;> rfl) + /-- The product commutes with the group action, `Λ T ⊗ Λ U = Λ (T ⊗ U)`, as in `(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : G) (t₁ : S.Tensor c₁) @@ -219,27 +302,58 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : /-! -## D. Tensors on two slots +## D. Tensors on one and two slots + +A species gives its objects with two indices, such as the delta `S.delta c`, in Mathlib's binary +tensor product `V a ⊗ V b`, built from pairs `x ⊗ₜ y`. A tensor with two slots lives in +`S.Tensor ![a, b]`, the tensor product of a family of spaces, built from vectors assigned to the +slots `0` and `1`. The two spaces are isomorphic but are different types in Lean, as `ℝ × ℝ` and +`Fin 2 → ℝ` are. `twoSlotEquiv` identifies them, so that such an object can be used as a tensor, +for example in index notation, and its components can be read with Mathlib's +`Basis.tensorProduct`. It is built from the product. Each factor is a tensor on one slot by +`oneSlotEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2`. The delta +tensor `S.deltaTensor c` is the delta seen this way. -/ +variable (S) in +/-- A vector `v` of `V c` as a tensor with the one slot `![c]`, by Mathlib's +`PiTensorProduct.subsingletonEquiv`. -/ +noncomputable def oneSlotEquiv (c : Color K) : S.V c ≃ₗ[k] S.Tensor ![c] := + (subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm + +/-- The tensor of `v` is the pure tensor with `v` on its one slot. -/ +@[simp] +lemma oneSlotEquiv_apply (c : Color K) (v : S.V c) : + S.oneSlotEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := + congrArg S.pure (funext fun i => by fin_cases i; rfl) + +/-- `oneSlotEquiv` commutes with the group action, `Λv ↦ Λ • v`. -/ +lemma oneSlotEquiv_rep (c : Color K) (g : G) (v : S.V c) : + S.oneSlotEquiv c (S.rep c g v) = g • S.oneSlotEquiv c v := by + rw [oneSlotEquiv_apply, oneSlotEquiv_apply, smul_pure] + exact congrArg S.pure (funext fun i => by fin_cases i; rfl) + +/-- The tensor of the basis vector `S.basis c (μ 0)` is the basis tensor with label `μ`. -/ +lemma oneSlotEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : + S.oneSlotEquiv c (S.basis c (μ 0)) = S.tensorBasis ![c] μ := by + rw [oneSlotEquiv_apply, tensorBasis_apply] + exact congrArg S.pure (funext fun j => by fin_cases j; rfl) + variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` -is the tensor `η_μν`. Each factor is a one-slot tensor by Mathlib's -`PiTensorProduct.subsingletonEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are renamed -`Fin 2` by Mathlib's `finSumFinEquiv`. -/ +is the tensor `η_μν`. Each factor is a one-slot tensor by `oneSlotEquiv`, and the slots +`Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by `ColorEquiv.ofFinTwo`. -/ noncomputable def twoSlotEquiv (a b : Color K) : S.V a ⊗[k] S.V b ≃ₗ[k] S.Tensor ![a, b] := - TensorProduct.congr (S.toPiTensor (fun _ : Fin 1 => a) ≪≫ₗ subsingletonEquiv 0).symm - (S.toPiTensor (fun _ : Fin 1 => b) ≪≫ₗ subsingletonEquiv 0).symm ≪≫ₗ S.tmulEquiv _ _ ≪≫ₗ - S.reindex (.mk finSumFinEquiv) + TensorProduct.congr (S.oneSlotEquiv a) (S.oneSlotEquiv b) ≪≫ₗ S.tmulEquiv ![a] ![b] ≪≫ₗ + S.reindex (ColorEquiv.ofFinTwo a b).symm /-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ @[simp] lemma twoSlotEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : S.twoSlotEquiv a b (x ⊗ₜ y) = S.pure (c := ![a, b]) (Fin.cons x (Fin.cons y finZeroElim)) := by - simp only [twoSlotEquiv, LinearEquiv.trans_apply, TensorProduct.congr_tmul, - LinearEquiv.trans_symm, subsingletonEquiv_symm_apply', toPiTensor_symm_tprod, + simp only [twoSlotEquiv, LinearEquiv.trans_apply, TensorProduct.congr_tmul, oneSlotEquiv_apply, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) @@ -254,16 +368,40 @@ lemma twoSlotEquiv_map_rep (a b : Color K) (Λ : G) (M : S.V a ⊗[k] S.V b) : simp only [TensorProduct.map_tmul, twoSlotEquiv_tmul, smul_pure] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) +/-- Swapping the two slots of a tensor on two slots swaps the factors, `M^μν ↦ M^νμ`. -/ +lemma reindex_twoSlotEquiv {a b : Color K} {σ : ColorEquiv ![a, b] ![b, a]} + (hσ : σ.toEquiv = Equiv.swap 0 1) (M : S.V a ⊗[k] S.V b) : + S.reindex σ (S.twoSlotEquiv a b M) = S.twoSlotEquiv b a (TensorProduct.comm k _ _ M) := by + obtain ⟨e, he⟩ := σ + subst hσ + induction M using TensorProduct.inductionOn with + | add x y hx hy => simp only [map_add, hx, hy] + | tmul x y => + simp only [twoSlotEquiv_tmul, reindex_pure, TensorProduct.comm_tmul] + exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) + variable (S) in /-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper color `c` and `δ^μ_ν` for a lower one. -/ noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := S.twoSlotEquiv c.dual c (S.delta c) +/-- The delta tensor is invariant, `Λ δ = δ`. -/ +lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := by + rw [deltaTensor, ← twoSlotEquiv_map_rep, delta_invariant] + /-! ## E. Components +A component of a tensor with colors `c` chooses one basis label `y j` for each slot `j`, and +`(S.tensorBasis c).repr t y` is the coefficient of `t` on the basis tensor with those labels, as +`T^μ_ν` for `y = (μ, ν)`. The components of a product are the products of the components of its +factors, with the labels `y (inl i)` on the slots of the first factor and `y (inr j)` on those of +the second. The components of a tensor on two slots are the coordinates of the element of +`V a ⊗ V b` in Mathlib's basis `Basis.tensorProduct`, so they can be computed with Mathlib's lemmas +on the binary tensor product. For the delta tensor they form the identity matrix. + -/ /-- The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. @@ -287,6 +425,13 @@ lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ rw [tmulEquiv_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, Fintype.prod_sum_type] +/-- The components of the tensor of a vector `v` of `V c` are its components `v^μ`. -/ +lemma tensorBasis_repr_oneSlotEquiv (c : Color K) (v : S.V c) + (μ : Π i, basisIdx (![c] i).kind) : + (S.tensorBasis ![c]).repr (S.oneSlotEquiv c v) μ = (S.basis c).repr v (μ 0) := by + rw [oneSlotEquiv_apply, tensorBasis_repr_pure, Fin.prod_univ_one] + rfl + /-- The components of the tensor of `M` in `V a ⊗ V b` are the coordinates of `M` in Mathlib's product basis `Basis.tensorProduct`. -/ lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) @@ -301,6 +446,29 @@ lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) -- Mathlib's lemma, and `exact` checks it up to unfolding. exact ((Module.Basis.tensorProduct_repr_tmul_apply _ _ _ _ _ _).trans (smul_eq_mul _ _)).symm +/-- The components of the delta tensor are the coordinates of `S.delta c` in Mathlib's product +basis, at the pair of labels `(μ 0, μ 1)`. -/ +lemma tensorBasis_repr_deltaTensor (c : Color K) (μ : Π j, basisIdx (![c.dual, c] j).kind) : + (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = + ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (μ 0, μ 1) := + tensorBasis_repr_twoSlotEquiv _ _ _ μ + +/-- The components of the delta tensor form the identity matrix, `δ_μ^ν = 1` for `μ = ν` and `0` +otherwise. -/ +lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) + (μ : Π j, basisIdx (![c.dual, c] j).kind) : + (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = + (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) (μ 0) (μ 1) := by + -- The labels `μ 0` and `μ 1` are labels at `c` only after unfolding, so they are generalized. + suffices h : ∀ a b, ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (a, b) = + (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) a b from + (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) + intro a b + have h := basis_repr_pairingToLinearMap_contr c (S.delta c) (S.basis c a) b + simp only [S.snake, LinearMap.id_apply, Module.Basis.repr_self, Finsupp.single_apply, ite_mul, + one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true] at h + rw [Matrix.one_apply, ← h] + end Species end Tensors diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 4d84c94e7c..09a2f54ff7 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Species +public import Physlib.Mathematics.Tensors.Tensor /-! # Relabeling the slots of a tensor @@ -15,33 +15,44 @@ transpose `T^μ_ν ↦ T_ν^μ`. ## i. Overview -A tensor of a species `S` with colors `c₁ : ι₁ → Color K` has one slot for each label `i : ι₁`. -A relabeling `σ : ColorEquiv c₁ c₂` from colors `c₁` on `ι₁` to colors `c₂` on `ι₂` is a bijection -`σ.toEquiv : ι₁ ≃ ι₂` together with the proof `σ.color_eq j : c₁ (σ.toEquiv.symm j) = c₂ j` that -every new slot `j` has the color of the old slot it comes from. The proof is stated at the new -labels, so that `c₂ j` reduces for concrete colors. For these the default proof of the structure -finds it, by `rfl` or by `rfl` on each slot, and rejects a relabeling that changes a color. - -`S.reindex σ : S.Tensor c₁ ≃ₗ[k] S.Tensor c₂` is a linear equivalence between two spaces of -tensors. The new slot `j` of `S.reindex σ T` holds the vector of the old slot `σ.toEquiv.symm j`, -moved along `σ.color_eq j` from `S.V (c₁ (σ.toEquiv.symm j))` to `S.V (c₂ j)`. The move changes -the type and no data. The components move with the slots (`tensorBasis_repr_reindex`). - -For example, a Lorentz upper index has the color `⟨vec, up⟩` with `V ⟨vec, up⟩ = ℝ⁴` and basis -`e_μ`, and a lower index has the color `⟨vec, down⟩` with `V ⟨vec, down⟩ = (ℝ⁴)*` and basis `e^μ`. -The colors `c₁ = ![⟨vec, up⟩, ⟨vec, down⟩]` on `Fin 2` give the space `ℝ⁴ ⊗ (ℝ⁴)*`, whose elements -are `T = T^μ_ν e_μ ⊗ e^ν`. The transpose relabels the slots by `Equiv.swap 0 1`, written -`.mk (Equiv.swap 0 1) : ColorEquiv c₁ ![⟨vec, down⟩, ⟨vec, up⟩]`. It sends `T` to its transpose -`Tᵀ = T^μ_ν e^ν ⊗ e_μ` in `(ℝ⁴)* ⊗ ℝ⁴`, whose components are `(Tᵀ)_ν^μ = T^μ_ν`. +A tensor has one slot for each index, and each slot has a color, such as up or down. `T^μ_ν` has +two slots, the first up and the second down. The transpose `T_ν^μ` has the same components with +the indices written in the other order. Its first slot is down and its second is up. + +A relabeling `σ` is a permutation of the indices. It says which position each index moves to, and +it may only move an index together with its color. For the transpose, `σ` swaps the positions `0` +and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would turn an up +index into a down one is not a relabeling. That operation is raising or lowering, and it needs a +metric. + +Formally `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the slots, where the +labels `ι₁` and `ι₂` may be different types, together with the proof +`σ.color_eq j : c₁ (σ.toEquiv.symm j) = c₂ j` that the new slot `j` has the color of the old slot +it comes from. For concrete colors the proof is found automatically. + +`S.reindex σ : S.Tensor c₁ ≃ₗ[k] S.Tensor c₂` moves each vector to its new slot, so the transpose +sends `u ⊗ α` to `α ⊗ u` and has components `(Tᵀ)_ν^μ = T^μ_ν`. Nothing is computed. Lean writes +the color of the new slot differently from the color of the old one, so each vector is retyped +along `σ.color_eq`, and for concrete colors this retyping is the identity. The name follows +Mathlib's `PiTensorProduct.reindex`. + +Relabelings appear wherever indices are lined up. Index notation relabels `B` in `A^μ_ν + B_ν^μ` +before adding, so the result is the tensor `(A + Bᵀ)^μ_ν` with the slots in the order of `A`. The +symmetry `g_μν = g_νμ` compares `g` with its relabeling. The product `u^μ v^ν` of two vectors, +whose slots are labeled "slot of `u`" and "slot of `v`" (`Fin 1 ⊕ Fin 1`), is relabeled to a +tensor with slots `0, 1` (`Fin 2`). A relabeling can change the type of the labels as well as +their order. ## ii. Key results -- `ColorEquiv` : a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. -- `Species.reindex`, `Species.reindex_pure` : a relabeling applied to tensors, and to a pure +- `ColorEquiv` is a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. +- `Species.reindex` applies a relabeling to tensors, and `Species.reindex_pure` gives it on a pure tensor. -- `Species.reindex_reindex` : two relabelings in sequence are their composite. -- `Species.reindex_smul` : relabeling commutes with the group action. -- `Species.tensorBasis_repr_reindex` : the components of a relabeled tensor, `(Tᵀ)_ν^μ = T^μ_ν`. +- `Species.reindex_reindex` proves that two relabelings in sequence are their composite, and + `Species.reindex_refl` that the identity relabeling does nothing. +- `Species.reindex_smul` proves that relabeling commutes with the group action. +- `Species.tensorBasis_repr_reindex` gives the components of a relabeled tensor, + `(Tᵀ)_ν^μ = T^μ_ν`. ## iii. Table of contents @@ -52,6 +63,8 @@ are `T = T^μ_ν e_μ ⊗ e^ν`. The transpose relabels the slots by `Equiv.swap ## iv. References +None. + -/ @[expose] public section @@ -66,10 +79,10 @@ namespace Tensors -/ -/-- A relabeling of slots that keeps their colors: a bijection of slots, with the proof that each -new slot has the color of the old slot it comes from. The proof is stated at the new labels `j`, -so that `c₂ j` reduces at labels such as `inl x` or `0`. For finitely many concrete slots it is -found by `rfl` or by cases. Two relabelings are equal when their bijections are +/-- A relabeling of slots that keeps their colors, made of a bijection of slots and the proof that +each new slot has the color of the old slot it comes from. The proof is stated at the new labels +`j`, so that `c₂ j` reduces at labels such as `inl x` or `0`. For finitely many concrete slots it +is found by `rfl` or by cases. Two relabelings are equal when their bijections are (`ColorEquiv.ext`). -/ @[ext] structure ColorEquiv {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) where @@ -117,7 +130,7 @@ holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : S.Tensor c₁ ≃ₗ[k] S.Tensor c₂ := S.toPiTensor c₁ ≪≫ₗ PiTensorProduct.reindex k _ σ.toEquiv ≪≫ₗ - PiTensorProduct.congr (fun j => LinearEquiv.cast (M := S.V) (σ.color_eq j)) ≪≫ₗ + PiTensorProduct.congr (fun j => S.cast (σ.color_eq j)) ≪≫ₗ (S.toPiTensor c₂).symm /-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved @@ -125,8 +138,7 @@ along `σ.color_eq j`. -/ @[simp] lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (f : Π i, S.V (c₁ i)) : - S.reindex σ (S.pure f) = S.pure fun j => - LinearEquiv.cast (R := k) (M := S.V) (σ.color_eq j) (f (σ.toEquiv.symm j)) := by + S.reindex σ (S.pure f) = S.pure fun j => S.cast (σ.color_eq j) (f (σ.toEquiv.symm j)) := by simp [reindex, PiTensorProduct.reindex_tprod, congr_tprod] /-! @@ -135,16 +147,20 @@ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C -/ +/-- Relabeling by the identity does nothing. -/ +lemma reindex_refl {c₁ : ι₁ → Color K} (t : S.Tensor c₁) : S.reindex (ColorEquiv.refl c₁) t = t := + LinearMap.congr_fun (f := (S.reindex (ColorEquiv.refl c₁)).toLinearMap) + (ext_pure (g := LinearMap.id) fun f => by rw [LinearEquiv.coe_coe, reindex_pure]; rfl) t + /-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`. -/ lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : - S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by - induction t using inductionOn_pure with - | h f => - simp only [reindex_pure, LinearEquiv.cast_apply, cast_cast] - rfl - | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] - | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add] + S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := + LinearMap.congr_fun (f := (S.reindex σ₁ ≪≫ₗ S.reindex σ₂).toLinearMap) + (ext_pure (g := (S.reindex (σ₁.trans σ₂)).toLinearMap) fun f => by + simp only [LinearEquiv.coe_coe, LinearEquiv.trans_apply, reindex_pure, + LinearEquiv.cast_apply, cast_cast] + rfl) t /-- Relabeling commutes with the group action, as in `(ΛT)ᵀ = Λ Tᵀ`. -/ lemma reindex_smul {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (g : G) @@ -179,7 +195,7 @@ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → -- The old slot `σ.toEquiv.symm (σ.toEquiv i)` is `i` only up to a proof, so it is generalized. have h : ∀ i' (_ : i' = i) (d : Color K) (p : c₁ i' = d) (q : d = c₁ i) (z : basisIdx d.kind), (S.basis (c₁ i)).repr (f i) (cast (congrArg (basisIdx ∘ Color.kind) q) z) = - (S.basis d).repr (LinearEquiv.cast (R := k) (M := S.V) p (f i')) z := by + (S.basis d).repr (S.cast p (f i')) z := by rintro _ rfl _ rfl _ _ rfl exact h _ (σ.toEquiv.symm_apply_apply i) _ _ (σ.symm.color_eq i) _ diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index f6d989872f..721a2ca9dd 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -8,97 +8,91 @@ module public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color public import Mathlib.RepresentationTheory.Intertwining -public import Mathlib.LinearAlgebra.PiTensorProduct.Basis public import Mathlib.LinearAlgebra.Basis.Bilinear /-! -# Tensor species and their tensors +# Tensor species -A species assigns to each color a representation with a basis, the contraction with the dual -color and the Kronecker delta. Its tensors with colors `c : ι → Color K` form Mathlib's -`⨂[k] i, V (c i)`, for any slot type `ι`. +A species assigns to each color a representation with a basis whose coordinate pairing with the +basis of the dual color is invariant. ## i. Overview A species `S : Species k G K basisIdx` assigns to each color `c` a `k`-module `S.V c`, a -representation `S.rep c` of the group `G` on it, and a basis `S.basis c` labelled by -`basisIdx c.kind`. It also gives the invariant contraction `S.contr c : V c ⊗ V c.dual → k`, which -pairs an index with its dual as in `u^μ α_μ`, and the Kronecker delta `S.delta c ∈ V c.dual ⊗ V c`, -written `δ_μ^ν`. Two laws relate them. The contraction is symmetric, `u^μ α_μ = α_μ u^μ` -(`contr_tmul_symm`). The delta acts as the identity, `u^μ δ_μ^ν = u^ν` (`snake`). It follows that -the delta is invariant and that the delta of `c.dual` is the delta of `c` with its factors -swapped. Since `c.dual.dual = c` holds by `rfl`, no statement needs a cast. - -Indices name slots, as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)` in any -basis. Components are taken with `tensorBasis`. The bases `S.basis c` and `S.basis c.dual` are not -related by the laws. The components of `S.delta c` form the identity matrix, and -`S.contr c (e_μ ⊗ e^ν)` is `1` for `μ = ν` and `0` otherwise, only when the two bases are dual. A -species does not require this, and a concrete species can prove it. - -The basis labels `basisIdx κ` depend only on the kind `κ` of a color, not on its variance, so an -index and its dual have the same labels by definition, as `μ` in `v^μ` and `α_μ`. They are a -parameter, not a field, so that a conjugate species can give a kind and its conjugate the same -labels by definition. A species is a value, so a new species can -be built from an old one, for example by restricting the group. - -`S.Tensor c` is a space and `S.pure f` an element of it. The space is `⨂[k] i, V (c i)`. The pure -tensor of a family `f` of vectors, one per slot, is `⨂ₜ[k] i, f i`, such as `u ⊗ α ⊗ β`. Every -tensor is a sum of pure tensors. A group element `Λ` acts on every slot by the representation of -its color. The components `T^μ_ν` of a tensor `T` are its coordinates in the basis -`e_μ ⊗ e^ν ⊗ …` of Mathlib's `Basis.piTensorProduct`. - -For example, Lorentz tensors have one kind `vec`. An upper index has color `⟨vec, up⟩` with -`V ⟨vec, up⟩ = ℝ⁴` and basis `e_μ`. A lower index has color `⟨vec, down⟩` with -`V ⟨vec, down⟩ = (ℝ⁴)*` and basis `e^μ`. The colors `![⟨vec, up⟩, ⟨vec, down⟩, ⟨vec, down⟩]` give -the space `ℝ⁴ ⊗ (ℝ⁴)* ⊗ (ℝ⁴)*` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. The pure tensor -`u ⊗ α ⊗ β` has components `u^μ α_ν β_ρ`. A Lorentz transformation `Λ` acts as -`(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ`. Lower indices transform with `Λ⁻¹`, which keeps -`u^μ α_μ` invariant. +representation `S.rep c` of `G` and a basis `S.basis c`. The data is indexed by colors, so that +one definition covers both variances. The contraction `S.contr c : V c ⊗ V c.dual → k` is +`u^μ α_μ` for `c = ⟨vec, up⟩` and `α_μ u^μ` for `c = ⟨vec, down⟩`. + +The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an index of that kind +runs over. For Lorentz tensors `basisIdx vec = Fin 1 ⊕ Fin d`, one time label and `d` space +labels, and the number of labels is the dimension. The labels depend on the kind alone, so an +index and its dual share them, as `μ` in `v^μ` and `α_μ`. `basisIdx` is a parameter of the +structure, not a field, so that a conjugate species gives conjugate kinds the same labels +(`Conjugation.lean`). + +The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that +existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says +that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, +which makes `V c.dual` the dual of `V c`. + +The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in +`V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`), the contraction is +symmetric (`contr_tmul_symm`), the delta acts as the identity, `u^μ δ_μ^ν = u^ν` (`snake`), and +both are invariant. Indices name slots, as in abstract index notation, so `u^μ α_μ` is +`S.contr c (u ⊗ α)`. ## ii. Key results -- `Species` : the data and laws of a tensor species. -- `Species.contr_invariant`, `Species.delta_invariant` : `u^μ α_μ` and `δ_μ^ν` are invariant. -- `Species.delta_eq_comm_delta_dual` : the delta of `c` is the delta of `c.dual`, swapped. -- `Species.pairingToLinearMap_contr_injective` : a tensor in `V c.dual ⊗ U` is determined by the - linear map `V c → U` it defines through the contraction. -- `Species.Tensor`, `Species.pure`, `Species.inductionOn_pure` : tensors, pure tensors, and - induction through pure tensors. -- `Species.smul_pure` : the group acts on a pure tensor slot by slot. -- `Species.map_smul_of_pure` : a map of tensors that commutes with the group on pure tensors - commutes with it on all tensors. -- `Species.tensorBasis`, `Species.tensorBasis_repr_pure` : the basis of tensors, and the - components of a pure tensor. -- `Species.contr_tmul_eq_sum_basis` : the contraction of two vectors in components. +- `Species` is the data and the law of a tensor species. +- `Species.contr` and `Species.delta` are the contraction `u^μ α_μ` and the delta `δ_μ^ν`, with + `Species.contr_basis`, `Species.contr_tmul_symm` and `Species.snake`. +- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, and + `Species.ext_contr` proves that the contraction is nondegenerate. +- `Species.coordPairing_rep_of_contr` gives the law `invariant` from an invariant pairing for + which the bases are dual. +- `Species.contr_invariant` and `Species.delta_invariant` prove that `u^μ α_μ` and `δ_μ^ν` are + invariant. +- `Species.delta_eq_comm_delta_dual` proves that the delta of `c` is the delta of `c.dual` with + its factors swapped. +- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, and + `Species.toLin_map_rep` proves that it commutes with the group action, `(Λg)(Λv) = Λ(g v)`. +- `Species.basis_repr_pairingToLinearMap_contr` gives the components of a tensor in + `V c.dual ⊗ V d` read as a linear map, and `Species.toLin_comp_toLin_of_mul_eq_one` proves that + two such maps are inverse when their matrices are. +- `Species.cast` moves a vector along an equality of colors, `Species.cast_rep` proves that this + commutes with the group action, and `Species.basis_repr_cast` that it keeps components. +- `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` prove that the + contraction does not depend on the colors its two vectors are moved to. ## iii. Table of contents -- A. Species -- B. Invariance of the contraction, invariance and symmetry of the delta -- C. Tensors -- D. The group action -- E. The basis of tensors +- A. Species, the contraction and the delta +- B. Invariance, symmetry of the delta, and tensors of two factors as linear maps +- C. Components of a tensor of two factors read as a linear map +- D. Moving a vector along an equality of colors ## iv. References +None. + -/ @[expose] public section -open TensorProduct PiTensorProduct +open TensorProduct namespace Tensors /-! -## A. Species +## A. Species, the contraction and the delta -/ -/-- A tensor species: for each color a representation with a basis, the contraction `u^μ α_μ` -with the dual color, and the Kronecker delta `δ_μ^ν`. The basis labels `basisIdx κ` depend only on -the kind, so a color and its dual share them. They are a parameter so that a conjugate species can -give a kind and its conjugate the same labels by definition. -/ +/-- A tensor species, which gives each color a representation with a basis such that the +coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` with the basis of the dual color is invariant. The basis +of a color of kind `κ` is labelled by `basisIdx κ`, the values that an index of kind `κ` runs +over, and a color and its dual share these labels. -/ structure Species (k : Type) [CommRing k] (G : Type) [Group G] (K : Type) (basisIdx : K → Type) [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] where /-- The vector space of a color. -/ @@ -111,18 +105,10 @@ structure Species (k : Type) [CommRing k] (G : Type) [Group G] (K : Type) rep : (c : Color K) → Representation k G (V c) /-- The basis of a color. -/ basis : (c : Color K) → Module.Basis (basisIdx c.kind) k (V c) - /-- The invariant contraction `V c ⊗ V c.dual → k`, `u ⊗ α ↦ u^μ α_μ`. The vector of color `c` - is the first factor. -/ - contr : (c : Color K) → - ((rep c).tprod (rep c.dual)).IntertwiningMap (Representation.trivial k G k) - /-- The Kronecker delta `δ_μ^ν ∈ V c.dual ⊗ V c`. The factor of the dual color is first. -/ - delta : (c : Color K) → V c.dual ⊗[k] V c - /-- The contraction is symmetric, `u^μ α_μ = α_μ u^μ`. -/ - contr_tmul_symm : ∀ c (x : V c) (y : V c.dual), contr c (x ⊗ₜ[k] y) = contr c.dual (y ⊗ₜ[k] x) - /-- The snake law `u^μ δ_μ^ν = u^ν`: the delta, read as a linear map through the contraction - (`pairingToLinearMap`), is the identity. The vector `u` is contracted with the first factor of - the delta. -/ - snake : ∀ c, pairingToLinearMap (contr c).toLinearMap (delta c) = LinearMap.id + /-- The coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is + invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ`. -/ + invariant : ∀ c g x y, (basis c).coordPairing (basis c.dual) (rep c g x ⊗ₜ rep c.dual g y) = + (basis c).coordPairing (basis c.dual) (x ⊗ₜ y) attribute [instance] Species.addCommGroup Species.module @@ -131,9 +117,67 @@ namespace Species variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] +/-- The invariant contraction `V c ⊗ V c.dual → k`, `u ⊗ α ↦ u^μ α_μ`, the coordinate pairing of +the bases. The vector of color `c` is the first factor. -/ +noncomputable def contr (S : Species k G K basisIdx) (c : Color K) : + ((S.rep c).tprod (S.rep c.dual)).IntertwiningMap (Representation.trivial k G k) := + ⟨(S.basis c).coordPairing (S.basis c.dual), fun g => + TensorProduct.ext' fun x y => S.invariant c g x y⟩ + +/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of +the dual color is first. -/ +noncomputable def delta (S : Species k G K basisIdx) (c : Color K) : S.V c.dual ⊗[k] S.V c := + (S.basis c.dual).coevaluation (S.basis c) + +/-- The bases of a color and its dual are dual, `e^μ(e_ν) = δ^μ_ν`. -/ +lemma contr_basis (S : Species k G K basisIdx) (c : Color K) (μ ν : basisIdx c.kind) : + S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) = if μ = ν then 1 else 0 := + Module.Basis.coordPairing_basis_basis _ _ _ _ + +/-- The contraction in components, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. -/ +lemma contr_tmul_eq_sum_basis {S : Species k G K basisIdx} (c : Color K) (x : S.V c) + (y : S.V c.dual) : + S.contr c (x ⊗ₜ y) = ∑ μ, (S.basis c).repr x μ * (S.basis c.dual).repr y μ := + Module.Basis.coordPairing_tmul _ _ x y + +/-- The contraction is symmetric, `u^μ α_μ = α_μ u^μ`. -/ +lemma contr_tmul_symm (S : Species k G K basisIdx) (c : Color K) (x : S.V c) (y : S.V c.dual) : + S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := + Module.Basis.coordPairing_comm _ _ _ _ + +/-- Contracting a basis vector `e_μ` with `α` reads off its component `α_μ`. -/ +lemma contr_basis_tmul (S : Species k G K basisIdx) (c : Color K) (μ : basisIdx c.kind) + (α : S.V c.dual) : S.contr c (S.basis c μ ⊗ₜ α) = (S.basis c.dual).repr α μ := by + rw [contr_tmul_symm] + exact Module.Basis.coordPairing_tmul_basis _ _ _ _ + +/-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the +same contraction against every vector of `c` are equal. -/ +lemma ext_contr {S : Species k G K basisIdx} {c : Color K} {α α' : S.V c.dual} + (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := + (S.basis c.dual).repr.injective <| Finsupp.ext fun μ => by + simpa [contr_basis_tmul] using h (S.basis c μ) + +/-- The snake law `u^μ δ_μ^ν = u^ν`: the delta, read as a linear map through the contraction +(`pairingToLinearMap`), is the identity. The vector `u` is contracted with the first factor of +the delta. -/ +lemma snake (S : Species k G K basisIdx) (c : Color K) : + pairingToLinearMap (S.contr c).toLinearMap (S.delta c) = LinearMap.id := + Module.Basis.pairingToLinearMap_coordPairing_coevaluation _ _ + +/-- The invariance `Species.invariant`, from an invariant pairing for which the bases are dual. -/ +lemma coordPairing_rep_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommGroup M] + [Module k M] [AddCommGroup N] [Module k N] {ρ : Representation k G M} + {ρ' : Representation k G N} {b : Module.Basis ι k M} {b' : Module.Basis ι k N} + (p : (ρ.tprod ρ').IntertwiningMap (Representation.trivial k G k)) + (h : ∀ I J, p (b I ⊗ₜ b' J) = if I = J then 1 else 0) (g : G) (x : M) (y : N) : + b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y) := by + rw [← b.eq_coordPairing b' p.toLinearMap h] + simpa using Representation.IntertwiningMap.isIntertwining _ _ p g (x ⊗ₜ y) + /-! -## B. Invariance of the contraction, invariance and symmetry of the delta +## B. Invariance, symmetry of the delta, and tensors of two factors as linear maps -/ @@ -144,6 +188,12 @@ lemma contr_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) (x : S. rw [(S.contr c).isIntertwining] simp +/-- Contracting `u` with `Λα` is contracting `Λ⁻¹u` with `α`. -/ +lemma contr_tmul_rep (S : Species k G K basisIdx) (c : Color K) (g : G) (u : S.V c) + (α : S.V c.dual) : S.contr c (u ⊗ₜ S.rep c.dual g α) = S.contr c (S.rep c g⁻¹ u ⊗ₜ α) := by + conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g u] + exact S.contr_invariant c g _ α + /-- A tensor `z ∈ V c.dual ⊗ U` read through the contraction as a linear map `x ↦ z x` from `V c` to `U` satisfies `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ @@ -159,8 +209,7 @@ lemma pairingToLinearMap_contr_map_rep (S : Species k G K basisIdx) (c : Color K | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] /-- A tensor `z ∈ V c.dual ⊗ U` is recovered from its linear map `x ↦ z x` by applying the map -to the `V c` factor of the delta of `c.dual` with its factors swapped. In bases of `c` and `c.dual` -that are dual, this reads `z = e^μ ⊗ z(e_μ)`. -/ +to the `V c` factor of the delta of `c.dual` with its factors swapped, `z = e^μ ⊗ z(e_μ)`. -/ lemma lTensor_pairingToLinearMap_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) {U : Type} [AddCommGroup U] [Module k U] (z : S.V c.dual ⊗[k] U) : (pairingToLinearMap (S.contr c).toLinearMap z).lTensor (S.V c.dual) @@ -177,152 +226,143 @@ lemma lTensor_pairingToLinearMap_comm_delta_dual (S : Species k G K basisIdx) (c Representation.IntertwiningMap.toLinearMap_apply, tmul_smul, smul_tmul'] rw [S.contr_tmul_symm c e₁ w] -/-- A tensor `z ∈ V c.dual ⊗ U` is determined by the linear map `x ↦ z x` from `V c` to `U` it -defines through the contraction. In particular `δ_μ^ν` is the only tensor acting as the -identity. -/ -lemma pairingToLinearMap_contr_injective (S : Species k G K basisIdx) (c : Color K) {U : Type} - [AddCommGroup U] [Module k U] : - Function.Injective (pairingToLinearMap (S.contr c).toLinearMap : - S.V c.dual ⊗[k] U →ₗ[k] S.V c →ₗ[k] U) := fun z z' h => by - rw [← S.lTensor_pairingToLinearMap_comm_delta_dual c z, - ← S.lTensor_pairingToLinearMap_comm_delta_dual c z', h] - /-- The delta of `c` is the delta of `c.dual ∈ V c ⊗ V c.dual` with its factors swapped. -/ lemma delta_eq_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) : S.delta c = TensorProduct.comm k _ _ (S.delta c.dual) := by - simpa [S.snake c] using (S.lTensor_pairingToLinearMap_comm_delta_dual c (S.delta c)).symm + simp only [delta, Module.Basis.coevaluation, map_sum, TensorProduct.comm_tmul] + +/-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the +first factor of `z`, as `v^μ ↦ v^μ g_μν` for `g` in `V a ⊗ V b`. Every linear map arises so. -/ +noncomputable def toLin (S : Species k G K basisIdx) {a : Color K} {U : Type} [AddCommGroup U] + [Module k U] : S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := + -- The inverse sends `f` to `(id ⊗ f) δ`, by the snake law. + LinearEquiv.ofLinearMap (pairingToLinearMap (S.contr a.dual).toLinearMap) + ((LinearMap.lTensorHom (S.V a.dual.dual)).flip (S.delta a.dual)) + (LinearMap.ext fun f => by simp [pairingToLinearMap_lTensor, S.snake]) + (LinearMap.ext fun z => by simpa [S.delta_eq_comm_delta_dual a.dual] using + S.lTensor_pairingToLinearMap_comm_delta_dual a.dual z) /-- The delta is invariant, `Λ δ = δ`. -/ lemma delta_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by -- Both act as the identity: `(Λδ)(Λx) = Λx` by the snake law. - apply S.pairingToLinearMap_contr_injective c + apply (S.toLin (a := c.dual)).injective + change pairingToLinearMap (S.contr c).toLinearMap _ = pairingToLinearMap (S.contr c).toLinearMap _ ext x conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g x] rw [S.pairingToLinearMap_contr_map_rep, S.snake c, LinearMap.id_apply, LinearMap.id_apply, Representation.self_inv_apply] +/-- The linear map of `Λg` sends `Λv` to `Λ(g v)`, as `(Λg)_μν (Λv)^μ = Λ(g_μν v^μ)`. -/ +lemma toLin_map_rep (S : Species k G K basisIdx) {a b : Color K} (g : S.V a ⊗[k] S.V b) (Λ : G) + (x : S.V a.dual) : + S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) (S.rep a.dual Λ x) = + S.rep b Λ (S.toLin g x) := + S.pairingToLinearMap_contr_map_rep a.dual Λ (S.rep b Λ) g x + +/-- The linear map of `Λg` is bijective when that of `g` is. -/ +lemma toLin_map_rep_bijective (S : Species k G K basisIdx) {a b : Color K} + (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) : + Function.Bijective (S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g)) := by + -- The map of `Λg` is `Λ ∘ g ∘ Λ⁻¹`, a composite of bijections. + have e : ⇑(S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g)) = + S.rep b Λ ∘ S.toLin g ∘ S.rep a.dual Λ⁻¹ := by + funext x + simp only [Function.comp_apply, ← S.toLin_map_rep, Representation.self_inv_apply] + rw [e] + refine (?_ : Function.Bijective (S.rep b Λ)).comp (h.comp ?_) + · exact ⟨fun x y hxy => by simpa using congrArg (S.rep b Λ⁻¹) hxy, + fun y => ⟨S.rep b Λ⁻¹ y, Representation.self_inv_apply _ _ _⟩⟩ + · exact ⟨fun x y hxy => by simpa using congrArg (S.rep a.dual Λ) hxy, + fun y => ⟨S.rep a.dual Λ y, Representation.inv_self_apply _ _ _⟩⟩ + /-! -## C. Tensors +## C. Components of a tensor of two factors read as a linear map -/ -/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the slots `ι`. It is a `def`, not -an `abbrev`, so that the type remembers `S` and instances on `⨂` from elsewhere do not apply. -/ -def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := - ⨂[k] i, S.V (c i) - -noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : - AddCommGroup (S.Tensor c) := - inferInstanceAs (AddCommGroup (⨂[k] i, S.V (c i))) - -noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : - Module k (S.Tensor c) := - inferInstanceAs (Module k (⨂[k] i, S.V (c i))) - -variable {ι : Type} {c : ι → Color K} - -/-- The identity from `S.Tensor c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. -Mathlib's constructions on `⨂` are composed with it. -/ -noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : - S.Tensor c ≃ₗ[k] ⨂[k] i, S.V (c i) := - LinearEquiv.refl k _ - -/-- The pure tensor of a family of vectors, one per slot, such as `u ⊗ α ⊗ β` with components -`u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ -noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : S.Tensor c := - (S.toPiTensor c).symm (⨂ₜ[k] i, f i) - variable {S : Species k G K basisIdx} -@[simp] -lemma toPiTensor_pure (f : Π i, S.V (c i)) : S.toPiTensor c (S.pure f) = ⨂ₜ[k] i, f i := rfl - -@[simp] -lemma toPiTensor_symm_tprod (f : Π i, S.V (c i)) : - (S.toPiTensor c).symm (⨂ₜ[k] i, f i) = S.pure f := rfl - -/-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ -lemma inductionOn_pure {P : S.Tensor c → Prop} - (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) - (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : S.Tensor c) : P t := - PiTensorProduct.induction_on (motive := P) t (fun r f => hsmul r _ (h f)) hadd +/-- A tensor `F = F_μ^ν e^μ ⊗ e_ν` in `V c.dual ⊗ V d`, read through the contraction as a linear +map, sends `v` to `v^μ F_μ^ν e_ν`. -/ +lemma basis_repr_pairingToLinearMap_contr (c : Color K) {d : Color K} + (F : S.V c.dual ⊗[k] S.V d) (v : S.V c) (ν : basisIdx d.kind) : + (S.basis d).repr (pairingToLinearMap (S.contr c).toLinearMap F v) ν = + ∑ μ, (S.basis c).repr v μ * ((S.basis c.dual).tensorProduct (S.basis d)).repr F (μ, ν) := by + induction F using TensorProduct.inductionOn with + | add x y hx hy => + simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, + Finset.sum_add_distrib] + | tmul x y => + rw [pairingToLinearMap_tmul, map_smul, Finsupp.smul_apply, smul_eq_mul] + change S.contr c (v ⊗ₜ x) * _ = _ + simp only [contr_tmul_eq_sum_basis, Module.Basis.tensorProduct_repr_tmul_apply, smul_eq_mul, + Finset.sum_mul] + exact Finset.sum_congr rfl fun _ _ => by ring + +/-- `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of `F'` and `F` multiply +to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ +lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) + (F' : S.V c.dual ⊗[k] S.V c.dual) + (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * + Matrix.of (fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr F (μ, ν)) = 1) : + S.toLin F ∘ₗ S.toLin F' = LinearMap.id := by + refine (S.basis c).ext fun μ => (S.basis c).repr.injective (Finsupp.ext fun ν => ?_) + have hμν := congrFun (congrFun h μ) ν + rw [Matrix.mul_apply, Matrix.one_apply] at hμν + change (S.basis c).repr (pairingToLinearMap (S.contr c.dual).toLinearMap F + (pairingToLinearMap (S.contr c).toLinearMap F' (S.basis c μ))) ν = _ + simp only [basis_repr_pairingToLinearMap_contr, Module.Basis.repr_self, Finsupp.single_apply, + ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true, LinearMap.id_apply] + exact hμν /-! -## D. The group action +## D. Moving a vector along an equality of colors -/ -/-- The group acts on every slot by the representation of its color, -`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`, through Mathlib's `PiTensorProduct.mapMonoidHom`. -/ -noncomputable instance : DistribMulAction G (S.Tensor c) := - DistribMulAction.compHom (M := Module.End k (S.Tensor c)) _ - (PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi fun i => S.rep (c i))) - -@[simp] -lemma smul_pure (g : G) (f : Π i, S.V (c i)) : - g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := - PiTensorProduct.map_tprod _ _ - -noncomputable instance : SMulCommClass G k (S.Tensor c) where - smul_comm g r t := by - change PiTensorProduct.map (fun i => S.rep (c i) g) (r • t) = r • PiTensorProduct.map _ t - exact map_smul _ r t - -noncomputable instance : SMulCommClass k G (S.Tensor c) := .symm _ _ _ - -/-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes -with `Λ` on all tensors. -/ -lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} - [FunLike F (S.Tensor c) (S.Tensor c')] [SemilinearMapClass F σ (S.Tensor c) (S.Tensor c')] - (f : F) (g : G) (h : ∀ p : Π i, S.V (c i), f (g • S.pure p) = g • f (S.pure p)) - (t : S.Tensor c) : f (g • t) = g • f t := by - induction t using inductionOn_pure with - | h p => exact h p - | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] - | hadd x y hx hy => rw [smul_add, map_add, hx, hy, map_add, smul_add] +variable (S) in +/-- Moving a vector along an equality of colors `a = a'`, Mathlib's `LinearEquiv.cast`. -/ +protected abbrev cast {a a' : Color K} (p : a = a') : S.V a ≃ₗ[k] S.V a' := + LinearEquiv.cast p /-- Moving a vector along an equality of colors `a = a'` commutes with the group action. -/ lemma cast_rep {a a' : Color K} (p : a = a') (g : G) (x : S.V a) : - LinearEquiv.cast (R := k) (M := S.V) p (S.rep a g x) = - S.rep a' g (LinearEquiv.cast (R := k) (M := S.V) p x) := by + S.cast p (S.rep a g x) = S.rep a' g (S.cast p x) := by subst p rfl -/-! +/-- Moving a vector along an equality of colors `a = b` keeps its components, with the label cast +back to a label at `a`. -/ +lemma basis_repr_cast {a b : Color K} (p : a = b) (v : S.V a) (μ : basisIdx b.kind) : + (S.basis b).repr (S.cast p v) μ = + (S.basis a).repr v (cast (congrArg (basisIdx ∘ Color.kind) p.symm) μ) := by + subst p + rfl -## E. The basis of tensors +/-- Moving the vector of a family `f` at the slot `s` depends only on the slot, so it may be read +at any slot `s'` equal to `s`. -/ +lemma cast_eq_cast_of_eq {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} + (e : s = s') {b : Color K} (p : d s = b) : S.cast p (f s) = S.cast (e ▸ p) (f s') := by + subst e + rfl --/ +/-- The contraction of two vectors does not depend on the colors they are moved to. -/ +lemma contr_cast_tmul_cast {d e a b : Color K} (x : S.V d) (y : S.V e) (p : d = a) + (q : e = a.dual) (p' : d = b) (q' : e = b.dual) : + S.contr a (S.cast p x ⊗ₜ S.cast q y) = S.contr b (S.cast p' x ⊗ₜ S.cast q' y) := by + subst p p' + rfl -/-- The basis `e_μ ⊗ e^ν ⊗ …` of tensors, Mathlib's `Basis.piTensorProduct` of the bases of the -colors. -/ -noncomputable def tensorBasis (S : Species k G K basisIdx) [Fintype ι] (c : ι → Color K) : - Module.Basis (Π i, basisIdx (c i).kind) k (S.Tensor c) := - (Basis.piTensorProduct fun i => S.basis (c i)).map (S.toPiTensor c).symm - -/-- A basis tensor is the pure tensor of basis vectors, `e_μ ⊗ e^ν ⊗ …`. -/ -lemma tensorBasis_apply [Fintype ι] (x : Π i, basisIdx (c i).kind) : - S.tensorBasis c x = S.pure fun i => S.basis (c i) (x i) := by - simp [tensorBasis, Basis.piTensorProduct_apply] - -/-- The components of a pure tensor are products, `(u ⊗ α)^μ_ν = u^μ α_ν`. -/ -lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) - (x : Π i, basisIdx (c i).kind) : - (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by - simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] - -/-- The contraction in components, `∑ μ ν, u^μ α_ν C_μ^ν`, where `C_μ^ν = S.contr c (e_μ ⊗ e^ν)`. -When the bases of `c` and `c.dual` are dual, `C_μ^ν` is `1` for `μ = ν` and `0` otherwise, and -the sum has one index. A species does not require this. -/ -lemma contr_tmul_eq_sum_basis (c : Color K) (x : S.V c) (y : S.V c.dual) : - S.contr c (x ⊗ₜ y) = - ∑ μ, ∑ ν, (S.basis c).repr x μ * (S.basis c.dual).repr y ν * - S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) := by - simpa [Finsupp.sum_fintype, mul_assoc] using (LinearMap.sum_repr_mul_repr_mul (S.basis c) - (S.basis c.dual) (B := TensorProduct.curry (S.contr c).toLinearMap) x y).symm +/-- `contr_tmul_symm` for vectors moved along equalities of colors, `u^μ α_μ = α_μ u^μ`. -/ +lemma contr_cast_tmul_cast_symm {d e a b : Color K} (x : S.V d) (y : S.V e) (p : d = a) + (q : e = a.dual) (p' : e = b) (q' : d = b.dual) : + S.contr a (S.cast p x ⊗ₜ S.cast q y) = S.contr b (S.cast p' y ⊗ₜ S.cast q' x) := by + subst p p' + subst q + exact S.contr_tmul_symm _ _ _ end Species diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean new file mode 100644 index 0000000000..2ba24e7ed9 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -0,0 +1,330 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Species +public import Mathlib.LinearAlgebra.PiTensorProduct.Basis +/-! + +# Tensors of a species + +The tensors of a species with colors `c : ι → Color K` form Mathlib's `⨂[k] i, V (c i)`, for any +slot type `ι`, and the group acts on them slot by slot. + +## i. Overview + +A tensor of a species `S` has one slot for each label `i : ι`, and slot `i` has the color `c i`. +The tensors with colors `c : ι → Color K` form the space `S.Tensor c`, which is Mathlib's +`⨂[k] i, V (c i)`. The slots `ι` can be any type, so later files label slots by `Fin n`, sums or +options without renumbering. + +A pure tensor takes one vector in each slot, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in +`V (c 1)` and `β` in `V (c 2)`. Its components are products, `(u ⊗ α ⊗ β)^μ_νρ = u^μ α_ν β_ρ`. +Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not +a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its +values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case +(`inductionOn_pure`). The group action, relabeling, products and contraction are each given by +their value on pure tensors, and their laws are proved there first. `S.pure f` is Mathlib's +`⨂ₜ[k] i, f i` as an element of `S.Tensor c`. + +A group element `Λ` acts on every slot by the representation of its color, +`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). A map that commutes with `Λ` on pure tensors +commutes with it on all tensors (`map_smul_of_pure`). + +The components `T^μ_νρ` of a tensor `T` are its coordinates in the basis `e_μ ⊗ e^ν ⊗ e^ρ` +(`tensorBasis`), which is Mathlib's `Basis.piTensorProduct`. Each basis tensor is a pure tensor of +basis vectors. + +For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, down⟩, ⟨vec, down⟩]` give the space +`ℝ⁴ ⊗ (ℝ⁴)* ⊗ (ℝ⁴)*` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. A Lorentz transformation `Λ` acts +as `(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ` (`tensorBasis_repr_smul`). Lower indices +transform with `Λ⁻¹`, which keeps `u^μ α_μ` invariant. + +A linear equivalence `f` from the space of one color to that of another acts on tensors through a +single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the other slots, +`u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`, as lowering one index of a tensor does. + +## ii. Key results + +- `Species.Tensor` and `Species.pure` are the tensors and the pure tensors, and + `Species.inductionOn_pure` is induction through pure tensors. +- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. +- `Species.smul_pure` proves that the group acts on a pure tensor slot by slot. +- `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure + tensors commutes with it on all tensors. +- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no slots is invariant. +- `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the + components of a pure tensor. +- `Species.tensorBasis_repr_smul` gives the components of `g • t`. +- `Species.congrAt` applies a linear equivalence at one slot of a tensor and the identity at the + others, with `Species.congrAt_pure`, `Species.congrAt_smul` and `Species.congrAt_symm`, and + `Species.congrAt_comm` proves that equivalences at two different slots commute. + +## iii. Table of contents + +- A. Tensors +- B. The group action +- C. The basis of tensors +- D. A linear equivalence at one slot + +## iv. References + +None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensors + +namespace Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + +/-! + +## A. Tensors + +-/ + +/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the slots `ι`. It is a `def`, not +an `abbrev`, so that the type `S.Tensor c` keeps the species and the colors, which index notation +reads off the type of a tensor. -/ +def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := + ⨂[k] i, S.V (c i) + +noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : + AddCommGroup (S.Tensor c) := + inferInstanceAs (AddCommGroup (⨂[k] i, S.V (c i))) + +noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : + Module k (S.Tensor c) := + inferInstanceAs (Module k (⨂[k] i, S.V (c i))) + +variable {ι : Type} {c : ι → Color K} + +/-- The identity from `S.Tensor c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. +Mathlib's constructions on `⨂` are composed with it. -/ +noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : + S.Tensor c ≃ₗ[k] ⨂[k] i, S.V (c i) := + LinearEquiv.refl k _ + +/-- The pure tensor of a family of vectors, one per slot, such as `u ⊗ α ⊗ β` with components +`u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ +noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : S.Tensor c := + (S.toPiTensor c).symm (⨂ₜ[k] i, f i) + +variable {S : Species k G K basisIdx} + +@[simp] +lemma toPiTensor_pure (f : Π i, S.V (c i)) : S.toPiTensor c (S.pure f) = ⨂ₜ[k] i, f i := rfl + +@[simp] +lemma toPiTensor_symm_tprod (f : Π i, S.V (c i)) : + (S.toPiTensor c).symm (⨂ₜ[k] i, f i) = S.pure f := rfl + +/-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ +lemma inductionOn_pure {P : S.Tensor c → Prop} + (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) + (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : S.Tensor c) : P t := + PiTensorProduct.induction_on (motive := P) t (fun r f => hsmul r _ (h f)) hadd + +/-- Two linear maps out of tensors are equal when they agree on pure tensors. -/ +lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : S.Tensor c →ₗ[k] E} + (h : ∀ p : Π i, S.V (c i), f (S.pure p) = g (S.pure p)) : f = g := + PiTensorProduct.ext (MultilinearMap.ext h) + +/-! + +## B. The group action + +-/ + +/-- The group acts on every slot by the representation of its color, +`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. -/ +noncomputable instance : DistribMulAction G (S.Tensor c) := + DistribMulAction.compHom (M := Module.End k (S.Tensor c)) _ + (PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi fun i => S.rep (c i))) + +@[simp] +lemma smul_pure (g : G) (f : Π i, S.V (c i)) : + g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := + PiTensorProduct.map_tprod _ _ + +noncomputable instance : SMulCommClass G k (S.Tensor c) where + smul_comm g r t := by + change PiTensorProduct.map (fun i => S.rep (c i) g) (r • t) = r • PiTensorProduct.map _ t + exact map_smul _ r t + +noncomputable instance : SMulCommClass k G (S.Tensor c) := .symm _ _ _ + +/-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes +with `Λ` on all tensors. -/ +lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} + [FunLike F (S.Tensor c) (S.Tensor c')] [SemilinearMapClass F σ (S.Tensor c) (S.Tensor c')] + (f : F) (g : G) (h : ∀ p : Π i, S.V (c i), f (g • S.pure p) = g • f (S.pure p)) + (t : S.Tensor c) : f (g • t) = g • f t := by + induction t using inductionOn_pure with + | h p => exact h p + | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] + | hadd x y hx hy => rw [smul_add, map_add, hx, hy, map_add, smul_add] + +/-- A tensor with no slots is invariant, `Λ s = s`. -/ +lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = t := by + induction t using inductionOn_pure with + | h f => rw [smul_pure]; exact congrArg _ (funext isEmptyElim) + | hsmul r t ht => rw [smul_comm, ht] + | hadd t t' ht ht' => rw [smul_add, ht, ht'] + +/-! + +## C. The basis of tensors + +-/ + +/-- The basis `e_μ ⊗ e^ν ⊗ …` of tensors, Mathlib's `Basis.piTensorProduct` of the bases of the +colors. -/ +noncomputable def tensorBasis (S : Species k G K basisIdx) [Fintype ι] (c : ι → Color K) : + Module.Basis (Π i, basisIdx (c i).kind) k (S.Tensor c) := + (Basis.piTensorProduct fun i => S.basis (c i)).map (S.toPiTensor c).symm + +/-- A basis tensor is the pure tensor of basis vectors, `e_μ ⊗ e^ν ⊗ …`. -/ +lemma tensorBasis_apply [Fintype ι] (x : Π i, basisIdx (c i).kind) : + S.tensorBasis c x = S.pure fun i => S.basis (c i) (x i) := by + simp [tensorBasis, Basis.piTensorProduct_apply] + +/-- The components of a pure tensor are products, `(u ⊗ α)^μ_ν = u^μ α_ν`. -/ +lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) + (x : Π i, basisIdx (c i).kind) : + (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by + simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] + +/-- The coefficient of `e_ψ` in `g • e_φ` is the product over the indices of the matrix entries of +`g` in the color of that index. -/ +lemma tensorBasis_repr_smul_tensorBasis [Fintype ι] (g : G) (φ ψ : Π i, basisIdx (c i).kind) : + (S.tensorBasis c).repr (g • S.tensorBasis c φ) ψ = + ∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) (S.rep (c i) g) (ψ i) (φ i) := by + simp [tensorBasis_apply, tensorBasis_repr_pure, LinearMap.toMatrix_apply] + +/-- The action of `g` on a basis tensor, `g e_φ = ∑ ψ, (∏ i, g^{ψ i}_{φ i}) e_ψ`, with one matrix +entry of `g` per index, in the color of that index. -/ +lemma smul_tensorBasis_eq_sum [Fintype ι] [DecidableEq ι] (g : G) (φ : Π i, basisIdx (c i).kind) : + g • S.tensorBasis c φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) + (S.rep (c i) g) (ψ i) (φ i)) • S.tensorBasis c ψ := by + conv_lhs => rw [← (S.tensorBasis c).sum_repr (g • S.tensorBasis c φ)] + simp_rw [tensorBasis_repr_smul_tensorBasis] + +/-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of +`g` per index, in the color of that index. -/ +lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor c) + (φ : Π i, basisIdx (c i).kind) : + (S.tensorBasis c).repr (g • t) φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) + (S.basis (c i)) (S.rep (c i) g) (φ i) (ψ i)) * (S.tensorBasis c).repr t ψ := by + conv_lhs => rw [← (S.tensorBasis c).sum_repr t, Finset.smul_sum] + simp_rw [smul_comm g, map_sum, map_smul, Finsupp.coe_finsetSum, Finset.sum_apply, + Finsupp.smul_apply, tensorBasis_repr_smul_tensorBasis, smul_eq_mul, mul_comm] + +/-! + +## D. A linear equivalence at one slot + +-/ + +section + +variable [DecidableEq ι] {c' : ι → Color K} + +variable (S) in +/-- The linear equivalence `f` at the slot `i`, and the identity, moved along `h x`, at every other +slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ +noncomputable def congrAt (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c ≃ₗ[k] S.Tensor c' := + S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => if hx : x = i then + hx ▸ f else S.cast (h x hx)) ≪≫ₗ (S.toPiTensor c').symm + +/-- On a pure tensor, `congrAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. +The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ +lemma congrAt_pure (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) + (hqi : q i = f (p i)) + (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : + S.congrAt i f h (S.pure p) = S.pure q := by + simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp [hqi] + · simp [hx, hq x hx] + +/-- `congrAt` commutes with the group action when `f` does, `(ΛT)_ν… = Λ(T_ν…)` for lowering +with an invariant metric. -/ +lemma congrAt_smul (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) + (hf : ∀ g x, f (S.rep (c i) g x) = S.rep (c' i) g (f x)) (g : G) (t : S.Tensor c) : + S.congrAt i f h (g • t) = g • S.congrAt i f h t := + map_smul_of_pure (S.congrAt i f h) g (fun p => by + simp only [smul_pure, congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp [hf] + · simp only [hx, dite_false, cast_rep]) t + +/-- The inverse of `congrAt` of `f` is `congrAt` of `f.symm`. -/ +lemma congrAt_symm (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : + (S.congrAt i f h).symm = S.congrAt i f.symm (fun x hx => (h x hx).symm) := by + refine LinearEquiv.ext fun t => (LinearEquiv.symm_apply_eq _).2 ?_ + induction t using inductionOn_pure with + | h p => + simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp + · simp [hx] + | hsmul r t ht => rw [map_smul, map_smul, ← ht] + | hadd x y hx hy => rw [map_add, map_add, ← hx, ← hy] + +/-- Linear equivalences at two different slots `i` and `j` commute, `f` and `f'` at `i`, `g` and +`g'` at `j`, when they agree after moving vectors between the intermediate colors `c₁` and `c₂`. -/ +lemma congrAt_comm {c₁ c₂ : ι → Color K} {i j : ι} (hij : i ≠ j) + (f : S.V (c i) ≃ₗ[k] S.V (c₁ i)) (h₁ : ∀ x, x ≠ i → c x = c₁ x) + (g : S.V (c₁ j) ≃ₗ[k] S.V (c' j)) (h₁' : ∀ x, x ≠ j → c₁ x = c' x) + (g' : S.V (c j) ≃ₗ[k] S.V (c₂ j)) (h₂ : ∀ x, x ≠ j → c x = c₂ x) + (f' : S.V (c₂ i) ≃ₗ[k] S.V (c' i)) (h₂' : ∀ x, x ≠ i → c₂ x = c' x) + (hf : ∀ v, f' (S.cast (h₂ i hij) v) = S.cast (h₁' i hij) (f v)) + (hg : ∀ v, g (S.cast (h₁ j hij.symm) v) = S.cast (h₂' j hij.symm) (g' v)) + (t : S.Tensor c) : + S.congrAt j g h₁' (S.congrAt i f h₁ t) = S.congrAt i f' h₂' (S.congrAt j g' h₂ t) := by + induction t using inductionOn_pure with + | h p => + simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, + toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hxi : x = i + · subst hxi + simpa [hij] using (hf (p _)).symm + · by_cases hxj : x = j + · subst hxj + simpa [hxi] using hg (p _) + · simp [hxi, hxj] + | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul, map_smul] + | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] + +end + +end Species + +end Tensors diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean new file mode 100644 index 0000000000..cde33a82e7 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -0,0 +1,167 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Product +/-! + +# Types whose elements are tensors + +A type `M` with its own action of a group is tensorial when its elements are identified with the +tensors of a species with given colors, compatibly with the action. + +## i. Overview + +A Lorentz vector `v` can be stored as its components `v^μ`, a function `Fin 1 ⊕ Fin 3 → ℝ`, on +which Mathlib's analysis acts. The same vector is also a tensor with one upper index, an element of +`S.Tensor c` for a one-slot color list `c`, which is what index notation and contraction act on. +An instance `Tensorial S c M` relates the two. + +The type `M` keeps its own transformation law, the representation `rep`. For a Lorentz vector it is +`Λ v = Λ *ᵥ v`, matrix times column. The identification `toTensor : M → S.Tensor c` respects the +action, `toTensor (Λ v) = Λ • toTensor v`, so `v` transforms as a tensor with colors `c`. This is +the content of the instance. `toTensor` is a Mathlib `Representation.Equiv`, a linear equivalence +that commutes with the action. + +An instance on the space of one index, such as a Lorentz vector or a Weyl spinor, comes from +`ofIndexSpace`. An instance on a type equivalent to a tensorial type, compatibly with the actions, +comes from `ofEquiv`. + +The tensor product `M ⊗ N` of two tensorial types is tensorial (`Tensorial.prod`). Its action is +Mathlib's product representation, `Λ (x ⊗ y) = Λ x ⊗ Λ y`, and `x ⊗ y` is read as the product of +the tensors of `x` and `y` through `Species.tmulEquiv`. A field strength `F^μν` in +`Vector ⊗ Vector` is an example. + +The action on `M` is the field `rep`, not an instance `SMul G M`. If `M` has an instance +`SMul G M`, Mathlib gives `M ⊗ N` an instance `SMul G (M ⊗ N)` that acts on the left factor only, +`Λ • (x ⊗ y) = Λ x ⊗ y`. A diagonal `SMul` instance on `M ⊗ N` would give `Λ • F` two meanings. + +The species and colors are determined by `M`, so a type has one tensor structure. An instance +belongs on a type that always means one physical object, such as `Lorentz.Vector d`, and not on a +type such as `Matrix (Fin 4) (Fin 4) ℝ` that is used for several. + +## ii. Key results + +- `Tensorial` : a type whose elements are tensors, compatibly with the action of the group. +- `Tensorial.toTensor_rep` : `toTensor (Λ m) = Λ • toTensor m`. +- `Tensorial.ofIndexSpace` : the space of one index is tensorial, with one slot. +- `Tensorial.ofEquiv` : a type equivalent to a tensorial type, compatibly with the actions, is + tensorial with the same colors. +- `Tensorial.prod` : the tensor product of two tensorial types is tensorial. +- `Tensorial.tensorBasis_repr_toTensor_prod` : the components of the tensor of `T` in `M ⊗ N` are + its coordinates in the product basis. + +## iii. Table of contents + +- A. Tensorial types +- B. Tensor products of tensorial types + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensors + +/-! + +## A. Tensorial types + +-/ + +/-- A type `M` whose elements are the tensors of `S` with colors `c`. `M` has its own action of +the group, `rep`, and `toTensor` identifies `M` with `S.Tensor c` compatibly with it. -/ +class Tensorial {k : outParam Type} [CommRing k] {G : outParam Type} [Group G] {K : outParam Type} + {basisIdx : outParam (K → Type)} [∀ κ, Fintype (basisIdx κ)] + [∀ κ, DecidableEq (basisIdx κ)] (S : outParam (Species k G K basisIdx)) {ι : outParam Type} + (c : outParam (ι → Color K)) (M : Type) [AddCommMonoid M] [Module k M] where + /-- The action of the group on `M`, such as `Λ v = Λ *ᵥ v` on Lorentz vectors. -/ + rep : Representation k G M + /-- The elements of `M` as tensors, compatibly with the action. -/ + toTensor : rep.Equiv (Representation.ofDistribMulAction k G (S.Tensor c)) + +namespace Tensorial + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + {ι₁ ι₂ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {M N : Type} [AddCommMonoid M] + [Module k M] [AddCommMonoid N] [Module k N] + +/-- Transforming `m` and then reading it as a tensor is transforming the tensor, +`toTensor (Λ m) = Λ • toTensor m`. -/ +@[simp] +lemma toTensor_rep [Tensorial S c₁ M] (g : G) (m : M) : + toTensor (rep g m) = g • toTensor m := + Representation.IntertwiningMap.isIntertwining _ _ (toTensor (M := M)).toIntertwiningMap g m + +variable (S) in +/-- The space `S.V c` of one index as the tensors with the one slot `![c]`, with the action +`S.rep c`. A type that is such a space, such as a Weyl spinor, gets its instance from this. -/ +noncomputable abbrev ofIndexSpace (c : Color K) : Tensorial S ![c] (S.V c) where + rep := S.rep c + toTensor := .mk (S.oneSlotEquiv c) fun g => LinearMap.ext (S.oneSlotEquiv_rep c g) + +/-- A type `M` with an action `r` and an equivalence `φ` to a tensorial type `N` commuting with +the actions is tensorial with the colors of `N`. -/ +noncomputable abbrev ofEquiv [Tensorial S c₁ N] {r : Representation k G M} + (φ : r.Equiv (rep (M := N))) : Tensorial S c₁ M where + rep := r + toTensor := φ.trans (toTensor (M := N)) + +/-- The tensor of `m` is the tensor of `φ m`. -/ +lemma toTensor_ofEquiv [Tensorial S c₁ N] {r : Representation k G M} + (φ : r.Equiv (rep (M := N))) (m : M) : toTensor (self := ofEquiv φ) m = toTensor (φ m) := rfl + +/-! + +## B. Tensor products of tensorial types + +-/ + +/-- The tensor product `M ⊗ N` of two tensorial types is tensorial on the slots of both, with the +product action `Λ (x ⊗ y) = Λ x ⊗ Λ y` and `x ⊗ y` read as the product of the tensors of `x` and +`y`. -/ +noncomputable instance prod [Tensorial S c₁ M] [Tensorial S c₂ N] : + Tensorial S (prodColors c₁ c₂) (M ⊗[k] N) where + rep := (rep (M := M)).tprod (rep (M := N)) + toTensor := .mk (TensorProduct.congr (toTensor (M := M)).toLinearEquiv + (toTensor (M := N)).toLinearEquiv ≪≫ₗ S.tmulEquiv c₁ c₂) fun g => by + ext x y + simp [Representation.tprod_apply, Species.tmulEquiv_smul] + +/-- The group acts on `x ⊗ y` factor by factor, `Λ (x ⊗ y) = Λ x ⊗ Λ y`. -/ +@[simp] +lemma rep_tmul [Tensorial S c₁ M] [Tensorial S c₂ N] (g : G) (x : M) (y : N) : + rep g (x ⊗ₜ[k] y) = rep g x ⊗ₜ rep g y := rfl + +/-- The tensor of `x ⊗ y` is the product of the tensors of `x` and `y`. -/ +@[simp] +lemma toTensor_tmul [Tensorial S c₁ M] [Tensorial S c₂ N] (x : M) (y : N) : + toTensor (x ⊗ₜ[k] y) = S.tmulEquiv c₁ c₂ (toTensor x ⊗ₜ toTensor y) := rfl + +/-- The components of the tensor of `T` in `M ⊗ N` are the coordinates of `T` in the product +basis `b₁ ⊗ b₂`, when the components of the tensors of `M` and `N` are the coordinates in `b₁` and +`b₂` at the labels `j₁ μ` and `j₂ ν`. -/ +lemma tensorBasis_repr_toTensor_prod [Fintype ι₁] [Fintype ι₂] [Tensorial S c₁ M] + [Tensorial S c₂ N] {κ₁ κ₂ : Type} (b₁ : Module.Basis κ₁ k M) (b₂ : Module.Basis κ₂ k N) + (j₁ : (Π i, basisIdx (c₁ i).kind) → κ₁) (j₂ : (Π i, basisIdx (c₂ i).kind) → κ₂) + (h₁ : ∀ x μ, (S.tensorBasis c₁).repr (toTensor x) μ = b₁.repr x (j₁ μ)) + (h₂ : ∀ y ν, (S.tensorBasis c₂).repr (toTensor y) ν = b₂.repr y (j₂ ν)) (T : M ⊗[k] N) + (μ : Π s, basisIdx (prodColors c₁ c₂ s).kind) : + (S.tensorBasis (prodColors c₁ c₂)).repr (toTensor T) μ = + (b₁.tensorProduct b₂).repr T (j₁ fun i => μ (.inl i), j₂ fun i => μ (.inr i)) := by + induction T using TensorProduct.inductionOn with + | add x y hx hy => simp only [map_add, Finsupp.add_apply, hx, hy] + | tmul x y => + rw [toTensor_tmul, Species.tensorBasis_repr_tmulEquiv, h₁, h₂, + Module.Basis.tensorProduct_repr_tmul_apply, smul_eq_mul, mul_comm] + +end Tensorial + +end Tensors diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 64d7399423..19a24471e9 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -5,8 +5,11 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Elab -public import Physlib.Relativity.Tensors.RealTensor.Metrics.Pre +public import Physlib.Relativity.Tensors.RealTensor.CoVector.Representation +public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct +public import Physlib.Relativity.Tensors.RealTensor.Vector.Representation /-! # The species of real Lorentz tensors @@ -17,42 +20,63 @@ Real Lorentz tensors in `d + 1` dimensions as a species, with the Minkowski metr Real Lorentz tensors have one kind of index, the vector index `vec`. An upper index has the color `⟨vec, up⟩` and a lower index the color `⟨vec, down⟩`. A color is a label. The species assigns to -it a space: `V ⟨vec, up⟩ = ContrMod d`, the contravariant vectors `v^μ` with basis `e_μ`, and -`V ⟨vec, down⟩ = CoMod d`, the covariant vectors `w_μ` with basis `e^μ`. Both are copies of -`ℝ^(d+1)`, so `V ⟨vec, up⟩ = ℝ⁴` for `d = 3`. The labels `μ` run over `Fin 1 ⊕ Fin d`, with -`Sum.inl 0` the time direction and `Sum.inr i` the space directions. A Lorentz transformation -`Λ : LorentzGroup d` acts on an upper index by `(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by -`(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named `realLorentz.up` and `realLorentz.down`, and -`ℝT[d, up, down]` is the space of the tensors `T^μ_ν`, under `open scoped Tensors.realLorentz`. - -The contraction `u^μ w_μ` is `ContrMod.contr` and `CoMod.contr`, the delta `δ_μ^ν` is -`ContrMod.unitVal` and `CoMod.unitVal`, and the metric is `η^μν = ContrMod.metricVal` and -`η_μν = CoMod.metricVal`, both with the matrix `diag(1, -1, …, -1)`. The laws of a species and of a -metric are `ContrMod.contr_tmul_symm`, `ContrMod.contr_unit` and `ContrMod.contr_metric` with their -`CoMod` versions, read through `TensorProduct.pairingToLinearMap_apply`. +it a space: `V ⟨vec, up⟩ = Lorentz.Vector d`, the contravariant vectors `v^μ` with basis `e_μ`, +and `V ⟨vec, down⟩ = Lorentz.CoVector d`, the covariant vectors `w_μ` with basis `e^μ`. Both are +functions `Fin 1 ⊕ Fin d → ℝ` with Mathlib's analysis, so `V ⟨vec, up⟩ = ℝ⁴` for `d = 3`. The +labels `μ` run over `Fin 1 ⊕ Fin d`, with `Sum.inl 0` the time direction and `Sum.inr i` the space +directions. A Lorentz transformation `Λ : LorentzGroup d` acts on an upper index by +`(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by `(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named +`realLorentz.up` and `realLorentz.down`, and `ℝT[d, up, down]` is the space of the tensors +`T^μ_ν`, under `open scoped Tensors.realLorentz`. + +The contraction `u^μ w_μ` is the coordinate pairing `∑ μ, u^μ w_μ` of the two bases and the +delta is `δ_μ^ν = ∑ μ, e^μ ⊗ e_μ`. The one physical input is that the pairing is Lorentz invariant +(`realLorentz.coordPairing_rep`). The metric is `η = ∑ μ, η_μμ e_μ ⊗ e_μ` (`realLorentz.metric`), +with the matrix `diag(1, -1, …, -1)` at both colors. So the general results apply. For example `η^μν η_νρ = δ^μ_ρ` is `Species.contr_formTensor_formTensor` at `up`, and lowering an index, `v^μ ↦ v_ν = v^μ η_μν`, is `(realLorentz d).toDual up`. -A variance stores whether it is up and whether it is down. The fields that pair a color with its -dual match on both booleans and list only the two consistent cases. Lean's `match` closes the other -two from `Variance.ne`. +The Lorentz vectors and covectors, the spaces of the two colors, are tensors with one upper index +and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ v` and a covector by +`(Λ⁻¹)ᵀ *ᵥ ω`, and both enter index notation directly, as in `{v | μ ⊗ ω | μ}ᵀ` for `v^μ ω_μ`. ## ii. Key results +- `realLorentz.coordPairing_rep` : the pairing `v^μ ω_μ` is Lorentz invariant. - `realLorentz` : the species of real Lorentz tensors, with the colors `realLorentz.up` and `realLorentz.down` and the spaces of tensors `ℝT[d, up, down]` (scoped). -- the instance `(realLorentz d).WithMetric` : the Minkowski metric `η`. -- `realLorentz.contr_basis` : the bases of upper and lower indices are dual. +- `realLorentz.metric` and the instances `(realLorentz d).HasForm κ` and + `(realLorentz d).IsSymmetricForm κ` : the Minkowski metric `η`. - `realLorentz.tensorBasis_repr_formTensor` : the components of `η^μν` and `η_μν` are the - Minkowski matrix. + Minkowski matrix, and `realLorentz.tensorBasis_repr_deltaTensor` : those of `δ` the identity. +- `realLorentz.vectorTensorial` and `realLorentz.coVectorTensorial` : Lorentz vectors and + covectors as tensors, with the actions `Lorentz.Vector.rep` and `Lorentz.CoVector.rep`. +- `realLorentz.tensorBasis_repr_toTensor_vector` and + `realLorentz.tensorBasis_repr_toTensor_coVector` : the components of the tensor of a vector or a + covector are its components, and `realLorentz.toTensor_symm_vector_apply` : the converse for a + vector. `realLorentz.toTensor_basis_vector` : a basis vector is a basis tensor. +- `realLorentz.tensorBasis_repr_toTensor_vector_tmul` and + `realLorentz.tensorBasis_repr_toTensor_coVector_tmul` : the components of the tensor of an + element of `Vector d ⊗ Vector d` or `CoVector d ⊗ Vector d` are its coordinates in the product + basis. +- `realLorentz.tensorBasis_repr_contr_vector_coVector` : the contraction `v^μ ω_μ` is + `∑ μ, v^μ ω_μ`, and `realLorentz.contr_vector_coVector_rep` : it is Lorentz invariant. +- `realLorentz.tensorBasis_repr_formTensor_contr_vector` : the Minkowski product `⟪v, w⟫ₘ` is the + contraction `η_μν v^μ w^ν`. +- `realLorentz.basis_repr_toDual` and `realLorentz.tensorBasis_repr_toDualAt` : lowering or raising + an index multiplies its component by `η_νν`. +- `Lorentz.Vector.tensorComponent` : the component `F^μν` of `F` in `Vector d ⊗ Vector d`. +- `realLorentz.tensorBasis_repr_toDualAt_contr_vector_tmul` : the contraction `F_μν G^μν` in + components. ## iii. Table of contents - A. The species - B. The Minkowski metric -- C. Dual bases and components of the metric +- C. Dual bases and components of the metric and the delta +- D. Lorentz vectors and covectors as tensors ## iv. References @@ -60,7 +84,7 @@ two from `Variance.ne`. @[expose] public section -open TensorProduct Lorentz +open TensorProduct Lorentz Matrix namespace Tensors @@ -76,50 +100,73 @@ inductive realLorentz.Kind /-- The Lorentz vector index. -/ | vec -/-- The vector space of a real Lorentz index of color `c`: `ContrMod d` for an upper index and -`CoMod d` for a lower one. A `def` with its own instances, because instance search does not find -the instances of a field written inline once a proof splits the color into cases. -/ +/-- The vector space of a real Lorentz index of color `c`: `Vector d` for an upper index and +`CoVector d` for a lower one. -/ def realLorentz.IndexSpace (d : ℕ) (c : Color realLorentz.Kind) : Type := match c.variance.isUp with - | true => ContrMod d - | false => CoMod d + | true => Vector d + | false => CoVector d noncomputable instance (d : ℕ) : ∀ c, AddCommGroup (realLorentz.IndexSpace d c) - | ⟨_, ⟨true, _, _⟩⟩ => inferInstanceAs (AddCommGroup (ContrMod d)) - | ⟨_, ⟨false, _, _⟩⟩ => inferInstanceAs (AddCommGroup (CoMod d)) + | ⟨_, .up⟩ => inferInstanceAs (AddCommGroup (Vector d)) + | ⟨_, .down⟩ => inferInstanceAs (AddCommGroup (CoVector d)) noncomputable instance (d : ℕ) : ∀ c, Module ℝ (realLorentz.IndexSpace d c) - | ⟨_, ⟨true, _, _⟩⟩ => inferInstanceAs (Module ℝ (ContrMod d)) - | ⟨_, ⟨false, _, _⟩⟩ => inferInstanceAs (Module ℝ (CoMod d)) + | ⟨_, .up⟩ => inferInstanceAs (Module ℝ (Vector d)) + | ⟨_, .down⟩ => inferInstanceAs (Module ℝ (CoVector d)) + +namespace realLorentz + +/-- The action of the Lorentz group on an index, `Vector.rep` up and `CoVector.rep` down. -/ +noncomputable abbrev IndexSpace.rep (d : ℕ) : + (c : Color Kind) → Representation ℝ (LorentzGroup d) (IndexSpace d c) + | ⟨_, .up⟩ => Vector.rep + | ⟨_, .down⟩ => CoVector.rep + +/-- The basis of an index, `Vector.basis` up and `CoVector.basis` down. -/ +noncomputable abbrev IndexSpace.basis (d : ℕ) : + (c : Color Kind) → Module.Basis (Fin 1 ⊕ Fin d) ℝ (IndexSpace d c) + | ⟨_, .up⟩ => Vector.basis + | ⟨_, .down⟩ => CoVector.basis + +/-- The coordinate pairing of `Vector.basis` and `CoVector.basis` is `v^μ ω_μ = v ⬝ᵥ ω`. -/ +lemma coordPairing_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : + Vector.basis.coordPairing CoVector.basis (v ⊗ₜ ω) = (v : Fin 1 ⊕ Fin d → ℝ) ⬝ᵥ ω := by + simp [Module.Basis.coordPairing_tmul, Vector.basis_repr_apply, CoVector.basis_repr_apply, + dotProduct] + +/-- The pairing `v^μ ω_μ` of a vector and a covector is Lorentz invariant, +`(Λ v) ⬝ᵥ ((Λ⁻¹)ᵀ ω) = v ⬝ᵥ ω`. -/ +lemma dotProduct_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) (ω : CoVector d) : + (Vector.rep Λ v : Fin 1 ⊕ Fin d → ℝ) ⬝ᵥ CoVector.rep Λ ω = v ⬝ᵥ ω := by + change (Λ.1 *ᵥ v) ⬝ᵥ ((Λ⁻¹).1ᵀ *ᵥ ω) = _ + rw [dotProduct_mulVec, vecMul_transpose, mulVec_mulVec, LorentzGroup.coe_inv, + LorentzGroup.subtype_inv_mul, one_mulVec] + +/-- The coordinate pairing of an index with its dual is Lorentz invariant. -/ +lemma coordPairing_rep (d : ℕ) : ∀ (c : Color Kind) (Λ : LorentzGroup d) (x : IndexSpace d c) + (y : IndexSpace d c.dual), + (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) + (IndexSpace.rep d c Λ x ⊗ₜ IndexSpace.rep d c.dual Λ y) = + (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) (x ⊗ₜ y) + | ⟨_, .up⟩, Λ, x, y => (coordPairing_vector_coVector d _ _).trans <| + (dotProduct_rep d Λ x y).trans (coordPairing_vector_coVector d x y).symm + | ⟨_, .down⟩, Λ, x, y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| + (coordPairing_vector_coVector d _ _).trans <| (dotProduct_rep d Λ y x).trans <| + (coordPairing_vector_coVector d y x).symm.trans (Module.Basis.coordPairing_comm _ _ _ _) + +end realLorentz open realLorentz in -/-- Real Lorentz tensors in `d + 1` dimensions, on `ContrMod d` and `CoMod d` with their -representations, bases, contraction and delta. The labels `Fin 1 ⊕ Fin d` put time first. At a -concrete color each field is the `ContrMod` or `CoMod` one by `rfl`, as in -`(realLorentz d).rep up = ContrMod.rep`. -/ +/-- Real Lorentz tensors in `d + 1` dimensions, on `Vector d` and `CoVector d` with their +representations and bases. The labels `Fin 1 ⊕ Fin d` put time first. At a concrete color each +field is the `Vector` or `CoVector` one by `rfl`, as in `(realLorentz d).rep up = Vector.rep`. -/ noncomputable abbrev realLorentz (d : ℕ) : Species ℝ (LorentzGroup d) Kind (fun _ => Fin 1 ⊕ Fin d) where V := IndexSpace d - rep - | ⟨_, ⟨true, _, _⟩⟩ => ContrMod.rep - | ⟨_, ⟨false, _, _⟩⟩ => CoMod.rep - basis - | ⟨_, ⟨true, _, _⟩⟩ => contrBasis d - | ⟨_, ⟨false, _, _⟩⟩ => coBasis d - contr - | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.contr - | ⟨_, ⟨false, true, _⟩⟩ => CoMod.contr - delta - | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.unitVal d - | ⟨_, ⟨false, true, _⟩⟩ => CoMod.unitVal d - contr_tmul_symm - | ⟨_, ⟨true, false, _⟩⟩ => ContrMod.contr_tmul_symm - | ⟨_, ⟨false, true, _⟩⟩ => CoMod.contr_tmul_symm - snake - | ⟨_, ⟨true, false, _⟩⟩ => LinearMap.ext fun x => (pairingToLinearMap_apply _ _ x).trans - (ContrMod.unit_apply_one (d := d) ▸ ContrMod.contr_unit x) - | ⟨_, ⟨false, true, _⟩⟩ => LinearMap.ext fun x => (pairingToLinearMap_apply _ _ x).trans - (CoMod.unit_apply_one (d := d) ▸ CoMod.contr_unit x) + rep := IndexSpace.rep d + basis := IndexSpace.basis d + invariant := coordPairing_rep d namespace realLorentz @@ -130,9 +177,8 @@ abbrev up : Color Kind := ⟨.vec, .up⟩ abbrev down : Color Kind := ⟨.vec, .down⟩ /-- The real Lorentz tensors in `d + 1` dimensions with the colors listed, as `ℝT[d, up, down]` for -`T^μ_ν`. The colors are read in the namespace `realLorentz`. The notation is scoped: it is active -under `open scoped Tensors.realLorentz`, so it does not clash with `ℝT[…]` of -`RealTensor.Basic`. -/ +`T^μ_ν`. The colors are read in the namespace `realLorentz`. The notation is active under +`open scoped Tensors.realLorentz`. -/ scoped syntax (name := realLorentzSyntax) "ℝT[" term,+ "]" : term macro_rules (kind := realLorentzSyntax) @@ -147,58 +193,96 @@ end realLorentz -/ -open realLorentz in -/-- The Minkowski metric, `η^μν` at an upper color and `η_μν` at a lower one, both with the matrix -`diag(1, -1, …, -1)`: the signature is mostly minus. -/ -noncomputable instance (d : ℕ) : (realLorentz d).WithMetric where - form - | ⟨_, ⟨true, _, _⟩⟩ => ContrMod.metricVal d - | ⟨_, ⟨false, _, _⟩⟩ => CoMod.metricVal d - form_invariant c g := by - rcases c with ⟨_, _ | _, _, _⟩ - · have h := (LinearMap.congr_fun ((CoMod.metric d).isIntertwining' g) 1).symm - simp [CoMod.metric_apply_one] at h - exact h - · have h := (LinearMap.congr_fun ((ContrMod.metric d).isIntertwining' g) 1).symm - simp [ContrMod.metric_apply_one] at h - exact h - form_inverse c := by - -- `ContrMod.contr_metric` states `η^μν η_νρ = δ^μ_ρ` with associators, `lid` and `comm`, the - -- form of `lTensor_pairingToLinearMap_apply`. - rw [← pairingToLinearMap_lTensor, ← (realLorentz d).snake c, - (realLorentz d).delta_eq_comm_delta_dual c, lTensor_pairingToLinearMap_apply] - congr 1 - match c with - | ⟨_, ⟨true, false, _⟩⟩ => - have h := CoMod.contr_metric (d := d) - rw [ContrMod.metric_apply_one, CoMod.metric_apply_one, CoMod.unit_apply_one] at h - exact (TensorProduct.comm_comm _ _ _ _).symm.trans (congrArg (TensorProduct.comm ℝ _ _) h) - | ⟨_, ⟨false, true, _⟩⟩ => - have h := ContrMod.contr_metric (d := d) - rw [ContrMod.metric_apply_one, CoMod.metric_apply_one, ContrMod.unit_apply_one] at h - exact (TensorProduct.comm_comm _ _ _ _).symm.trans (congrArg (TensorProduct.comm ℝ _ _) h) - form_symm - | ⟨_, ⟨true, _, _⟩⟩ => (by simp [ContrMod.metricVal_expand_tmul_minkowskiMatrix] : - TensorProduct.comm ℝ _ _ (ContrMod.metricVal d) = _) - | ⟨_, ⟨false, _, _⟩⟩ => (by simp [CoMod.metricVal_expand_tmul_minkowskiMatrix] : - TensorProduct.comm ℝ _ _ (CoMod.metricVal d) = _) +namespace realLorentz + +/-- The Minkowski metric `∑ μ, η_μμ e_μ ⊗ e_μ` at the color `c`, with `η = diag(1, -1, …, -1)`: +`η^μν` at an upper color and `η_μν` at a lower one. -/ +noncomputable def metric (d : ℕ) (c : Color Kind) : (realLorentz d).V c ⊗[ℝ] (realLorentz d).V c := + ∑ μ, minkowskiMatrix μ μ • ((realLorentz d).basis c μ ⊗ₜ (realLorentz d).basis c μ) + +/-- The coordinates of `metric d c` in Mathlib's product basis are the Minkowski matrix. -/ +lemma basis_tensorProduct_repr_metric (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : + (((realLorentz d).basis c).tensorProduct ((realLorentz d).basis c)).repr (metric d c) (μ, ν) = + minkowskiMatrix μ ν := by + rw [metric, map_sum, Finsupp.finsetSum_apply, Finset.sum_eq_single μ + (fun x _ hx => by simp [Module.Basis.tensorProduct_repr_tmul_apply, Finsupp.single_apply, hx]) + (by simp)] + by_cases h : μ = ν + · subst h + simp [Module.Basis.tensorProduct_repr_tmul_apply] + · simp [Module.Basis.tensorProduct_repr_tmul_apply, h] + +/-- The metric is Lorentz invariant, `Λ η Λᵀ = η` at an upper color and `Λ⁻¹ᵀ η Λ⁻¹ = η` at a +lower one. -/ +lemma metric_invariant (d : ℕ) (c : Color Kind) (Λ : LorentzGroup d) : + TensorProduct.map ((realLorentz d).rep c Λ) ((realLorentz d).rep c Λ) (metric d c) = + metric d c := by + let e := ((realLorentz d).basis c).tensorProductToMatrix ((realLorentz d).basis c) + have he : e (metric d c) = minkowskiMatrix := Matrix.ext (basis_tensorProduct_repr_metric d c) + refine e.injective ((Module.Basis.tensorProductToMatrix_map _ _ _ _ _).trans ?_) + rw [he] + match c with + | ⟨_, .up⟩ => + rw [show LinearMap.toMatrix ((realLorentz d).basis up) ((realLorentz d).basis up) + ((realLorentz d).rep up Λ) = Λ.1 from Vector.rep_toMatrix d Λ, + LorentzGroup.mul_minkowskiMatrix_mul_transpose] + | ⟨_, .down⟩ => + rw [show LinearMap.toMatrix ((realLorentz d).basis down) ((realLorentz d).basis down) + ((realLorentz d).rep down Λ) = Λ.1⁻¹ᵀ from CoVector.rep_toMatrix d Λ, + Matrix.transpose_transpose, ← LorentzGroup.coe_inv, + LorentzGroup.transpose_mul_minkowskiMatrix_mul_self] + +/-- Contracting the metric at `c` with a basis vector of `c.dual` gives `η_νν e_ν`. -/ +lemma toLin_metric_basis (d : ℕ) (c : Color Kind) (ν : Fin 1 ⊕ Fin d) : + (realLorentz d).toLin (metric d c) ((realLorentz d).basis c.dual ν) = + minkowskiMatrix ν ν • (realLorentz d).basis c ν := by + refine ((realLorentz d).basis c).repr.injective (Finsupp.ext fun ρ => ?_) + change ((realLorentz d).basis c).repr (pairingToLinearMap + ((realLorentz d).contr c.dual).toLinearMap (metric d c) _) ρ = _ + rw [Species.basis_repr_pairingToLinearMap_contr, map_smul] + simp only [Module.Basis.repr_self, Finsupp.single_apply, ite_mul, one_mul, zero_mul, + Finset.sum_ite_eq, Finset.mem_univ, ite_true, Finsupp.smul_apply, smul_eq_mul, mul_ite, mul_one, + mul_zero] + refine (basis_tensorProduct_repr_metric d c ν ρ).trans ?_ + by_cases h : ν = ρ <;> simp [h] + +/-- The metrics at a color and at its dual are inverse, `η^μν η_νρ = δ^μ_ρ`: `η` is diagonal with +`η_μμ² = 1`. -/ +lemma metric_inverse (d : ℕ) (c : Color Kind) : + (realLorentz d).toLin (metric d c) ∘ₗ (realLorentz d).toLin (metric d c.dual) = + LinearMap.id := + ((realLorentz d).basis c).ext fun ν => by + rw [LinearMap.comp_apply, toLin_metric_basis d c.dual ν, map_smul, toLin_metric_basis d c ν, + smul_smul, minkowskiMatrix.η_apply_mul_η_apply_diag, one_smul, LinearMap.id_apply] + +end realLorentz + +/-- The Minkowski metric `η`, `diag(1, -1, …, -1)` at both colors of every kind. The signature is +mostly minus. -/ +noncomputable instance (d : ℕ) (κ : realLorentz.Kind) : (realLorentz d).HasForm κ where + formAt v := realLorentz.metric d ⟨κ, v⟩ + formAt_invariant v := realLorentz.metric_invariant d ⟨κ, v⟩ + formAt_inverse v := realLorentz.metric_inverse d ⟨κ, v⟩ + +/-- The Minkowski metric is symmetric. -/ +instance (d : ℕ) (κ : realLorentz.Kind) : (realLorentz d).IsSymmetricForm κ where + comm_form v := by + change TensorProduct.comm ℝ _ _ (realLorentz.metric d ⟨κ, v⟩) = realLorentz.metric d ⟨κ, v⟩ + simp [realLorentz.metric] /-! -## C. Dual bases and components of the metric +## C. Dual bases and components of the metric and the delta -/ namespace realLorentz -/-- The bases of an upper and a lower index are dual: `e_μ(e^ν)` is `1` for `μ = ν` and `0` -otherwise. At a lower color `c` the left factor is `e^μ` and the right `e_ν`. -/ -lemma contr_basis (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : - (realLorentz d).contr c ((realLorentz d).basis c μ ⊗ₜ (realLorentz d).basis c.dual ν) = - if μ = ν then 1 else 0 := by - match c with - | ⟨_, ⟨true, false, _⟩⟩ => exact ContrMod.contr_basis μ ν - | ⟨_, ⟨false, true, _⟩⟩ => exact CoMod.contr_basis μ ν +/-- The components of the metric are the Minkowski matrix, `η_μν` at a lower color and `η^μν` at an +upper one. -/ +lemma formMatrix_apply (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : + (realLorentz d).formMatrix c μ ν = minkowskiMatrix μ ν := + basis_tensorProduct_repr_metric d c μ ν /-- The components of the metric are the Minkowski matrix `diag(1, -1, …, -1)`: at the labels `![α, β]` they are `η^αβ` at an upper color and `η_αβ` at a lower one. The labels are any @@ -206,10 +290,34 @@ lemma contr_basis (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : lemma tensorBasis_repr_formTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis ![c, c]).repr ((realLorentz d).formTensor c) μ = minkowskiMatrix (μ 0) (μ 1) := by - rw [Species.formTensor, Species.tensorBasis_repr_twoSlotEquiv] - rcases c with ⟨_, _ | _, _, _⟩ - · exact congrFun (congrFun (coCoToMatrixRe.apply_symm_apply minkowskiMatrix) (μ 0)) (μ 1) - · exact congrFun (congrFun (contrContrToMatrixRe.apply_symm_apply minkowskiMatrix) (μ 0)) (μ 1) + rw [Species.tensorBasis_repr_formTensor, formMatrix_apply] + +/-- The components of the delta are those of the identity matrix, `δ^μ_ν = 1` for `μ = ν` and `0` +otherwise, at both colors. -/ +lemma tensorBasis_repr_deltaTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis ![c.dual, c]).repr ((realLorentz d).deltaTensor c) μ = + (1 : Matrix (Fin 1 ⊕ Fin d) (Fin 1 ⊕ Fin d) ℝ) (μ 0) (μ 1) := by + rw [Species.tensorBasis_repr_deltaTensor_eq_one] + +/-- Lowering or raising a vector in components, `v_ν = η_νν v^ν`: the Minkowski metric is +diagonal. -/ +lemma basis_repr_toDual (d : ℕ) (c : Color Kind) (v : (realLorentz d).V c) + (ν : Fin 1 ⊕ Fin d) : + ((realLorentz d).basis c.dual).repr ((realLorentz d).toDual c v) ν = + minkowskiMatrix ν ν * ((realLorentz d).basis c).repr v ν := by + simp [Species.basis_repr_toDual c v ν, formMatrix_apply, + minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, mul_comm] + +/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = η_νν T^…ν…`: the +Minkowski metric is diagonal. -/ +lemma tensorBasis_repr_toDualAt (d : ℕ) {ι : Type} [Fintype ι] [DecidableEq ι] + {c c' : ι → Color Kind} (i : ι) (a : Color Kind) (ha : c i = a) (hi : c' i = a.dual) + (h : ∀ x, x ≠ i → c x = c' x) (t : (realLorentz d).Tensor c) (φ : ι → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis c').repr ((realLorentz d).toDualAt i a ha hi h t) φ = + minkowskiMatrix (φ i) (φ i) * ((realLorentz d).tensorBasis c).repr t φ := by + subst ha + simp [Species.tensorBasis_repr_toDualAt i _ rfl hi h, formMatrix_apply, + minkowskiMatrix.as_diagonal, Matrix.diagonal_apply] /-- `η^00 = 1` and `η^11 = -1` in `3 + 1` dimensions: the signature is mostly minus. -/ example : ((realLorentz 3).tensorBasis ![up, up]).repr ((realLorentz 3).formTensor up) @@ -223,19 +331,149 @@ of dimensions. -/ example (d : ℕ) : ((realLorentz d).tensorBasis ![]).repr {(realLorentz d).formTensor down | μ ν ⊗ (realLorentz d).formTensor up | μ ν}ᵀ (fun i => i.elim0) = d + 1 := by - rw [Species.Tensor.tensorBasis_repr_contr_of_dual _ _ _ fun _ => contr_basis d _] + rw [Species.Tensor.tensorBasis_repr_contr_unit_sum] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor] - -- The labels of `η` are casts of `ν (.inl ())` and `ν (.inr ())`, equal to them by `rfl`. - change ∑ ν : Unit ⊕ Unit → Fin 1 ⊕ Fin d, minkowskiMatrix (ν (.inl ())) (ν (.inr ())) * - minkowskiMatrix (ν (.inl ())) (ν (.inr ())) = d + 1 - rw [← ((Equiv.sumArrowEquivProdArrow Unit Unit _).trans ((Equiv.funUnique Unit _).prodCongr - (Equiv.funUnique Unit _))).symm.sum_comp, Fintype.sum_prod_type] simp [minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, Fintype.sum_sum_type, add_comm] - /-- `ℝT[3, up, down]` is the space of tensors `T^μ_ν` in four dimensions. -/ example : ℝT[3, up, down] = (realLorentz 3).Tensor ![up, down] := rfl end realLorentz +/-! + +## D. Lorentz vectors and covectors as tensors + +-/ + +namespace realLorentz + +open Matrix + +/-- A Lorentz vector `v^μ` is a tensor with one upper index, the space of the index. The Lorentz +group acts by `Vector.rep`, `Λ v = Λ *ᵥ v`. -/ +noncomputable instance vectorTensorial (d : ℕ) : Tensorial (realLorentz d) ![up] (Vector d) := + Tensorial.ofIndexSpace (realLorentz d) up + +/-- A Lorentz covector `ω_μ` is a tensor with one lower index, the space of the index. The Lorentz +group acts by `CoVector.rep`, `Λ ω = (Λ⁻¹)ᵀ *ᵥ ω`. -/ +noncomputable instance coVectorTensorial (d : ℕ) : + Tensorial (realLorentz d) ![down] (CoVector d) := + Tensorial.ofIndexSpace (realLorentz d) down + +/-- The components of the tensor of a vector are its components, `v^μ`. -/ +lemma tensorBasis_repr_toTensor_vector (d : ℕ) (v : Vector d) (μ : Fin 1 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis ![up]).repr (Tensorial.toTensor v) μ = v (μ 0) := + Species.tensorBasis_repr_oneSlotEquiv (S := realLorentz d) up v μ + +/-- The vector of a tensor `T` with one upper index has the components of `T`, `v^ν = T^ν`. -/ +lemma toTensor_symm_vector_apply (d : ℕ) (T : (realLorentz d).Tensor ![up]) + (ν : Fin 1 ⊕ Fin d) : + (Tensorial.toTensor (M := Vector d)).symm T ν = + ((realLorentz d).tensorBasis ![up]).repr T fun _ => ν := by + conv_rhs => rw [← Representation.Equiv.apply_symm_apply (Tensorial.toTensor (M := Vector d)) T] + rw [tensorBasis_repr_toTensor_vector] + +/-- The components of the tensor of a covector are its components, `ω_μ`. -/ +lemma tensorBasis_repr_toTensor_coVector (d : ℕ) (ω : CoVector d) (μ : Fin 1 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis ![down]).repr (Tensorial.toTensor ω) μ = ω (μ 0) := + Species.tensorBasis_repr_oneSlotEquiv (S := realLorentz d) down ω μ + +/-- The tensor of the basis vector `e_μ` is the basis tensor with label `μ`. -/ +lemma toTensor_basis_vector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : + Tensorial.toTensor (Vector.basis μ) = (realLorentz d).tensorBasis ![up] fun _ => μ := + Species.oneSlotEquiv_basis (S := realLorentz d) up fun _ => μ + +/-- The tensor of the basis covector `e^μ` is the basis tensor with label `μ`. -/ +lemma toTensor_basis_coVector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : + Tensorial.toTensor (CoVector.basis μ) = (realLorentz d).tensorBasis ![down] fun _ => μ := + Species.oneSlotEquiv_basis (S := realLorentz d) down fun _ => μ + +/-- The components `F^μν` of the tensor of `F` in `Vector d ⊗ Vector d` are its coordinates in +Mathlib's product basis `Basis.tensorProduct`. -/ +lemma tensorBasis_repr_toTensor_vector_tmul (d : ℕ) (F : Vector d ⊗[ℝ] Vector d) + (μ : Fin 1 ⊕ Fin 1 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis (prodColors ![up] ![up])).repr (Tensorial.toTensor F) μ = + (Vector.basis.tensorProduct Vector.basis).repr F (μ (.inl 0), μ (.inr 0)) := + Tensorial.tensorBasis_repr_toTensor_prod (S := realLorentz d) Vector.basis Vector.basis + (fun ν => ν 0) (fun ν => ν 0) (tensorBasis_repr_toTensor_vector d) + (tensorBasis_repr_toTensor_vector d) F μ + +/-- The component `F^μν` of `F` in `Vector d ⊗ Vector d`, its coordinate at `(μ, ν)` in Mathlib's +product basis `e_μ ⊗ e_ν`. -/ +noncomputable def _root_.Lorentz.Vector.tensorComponent {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) : + Vector d ⊗[ℝ] Vector d →ₗ[ℝ] ℝ := + Finsupp.lapply (μ, ν) ∘ₗ (Vector.basis.tensorProduct Vector.basis).repr.toLinearMap + +lemma _root_.Lorentz.Vector.tensorComponent_apply {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) + (F : Vector d ⊗[ℝ] Vector d) : + Vector.tensorComponent μ ν F = (Vector.basis.tensorProduct Vector.basis).repr F (μ, ν) := rfl + +@[simp] +lemma _root_.Lorentz.Vector.tensorComponent_tmul {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (v w : Vector d) : + Vector.tensorComponent μ ν (v ⊗ₜ w) = v μ * w ν := by + simp [Vector.tensorComponent_apply, Module.Basis.tensorProduct_repr_tmul_apply, + Vector.basis_repr_apply, mul_comm] + +/-- Two elements of `Vector d ⊗ Vector d` with the same components are equal. -/ +lemma _root_.Lorentz.Vector.tensorComponent_ext {d : ℕ} {F G : Vector d ⊗[ℝ] Vector d} + (h : ∀ μ ν, Vector.tensorComponent μ ν F = Vector.tensorComponent μ ν G) : F = G := + (Vector.basis.tensorProduct Vector.basis).repr.injective (Finsupp.ext fun μν => h μν.1 μν.2) + +/-- An element of `Vector d ⊗ Vector d` is the sum `F^μν e_μ ⊗ e_ν` of its components. -/ +lemma _root_.Lorentz.Vector.eq_sum_tensorComponent {d : ℕ} (F : Vector d ⊗[ℝ] Vector d) : + F = ∑ μ, ∑ ν, Vector.tensorComponent μ ν F • Vector.basis μ ⊗ₜ[ℝ] Vector.basis ν := by + simp only [Vector.tensorComponent_apply, ← Fintype.sum_prod_type'] + exact ((Vector.basis.tensorProduct Vector.basis).sum_repr _).symm.trans + (Finset.sum_congr rfl fun _ _ => by rw [Module.Basis.tensorProduct_apply]) + +/-- The components `T_μ^ν` of the tensor of `T` in `CoVector d ⊗ Vector d` are its coordinates in +Mathlib's product basis `Basis.tensorProduct`. -/ +lemma tensorBasis_repr_toTensor_coVector_tmul (d : ℕ) (T : CoVector d ⊗[ℝ] Vector d) + (μ : Fin 1 ⊕ Fin 1 → Fin 1 ⊕ Fin d) : + ((realLorentz d).tensorBasis (prodColors ![down] ![up])).repr (Tensorial.toTensor T) μ = + (CoVector.basis.tensorProduct Vector.basis).repr T (μ (.inl 0), μ (.inr 0)) := + Tensorial.tensorBasis_repr_toTensor_prod (S := realLorentz d) CoVector.basis Vector.basis + (fun ν => ν 0) (fun ν => ν 0) (tensorBasis_repr_toTensor_coVector d) + (tensorBasis_repr_toTensor_vector d) T μ + +/-- The contraction `v^μ ω_μ` of a vector with a covector, a tensor with no slots whose one +component is `∑ μ, v^μ ω_μ`. -/ +lemma tensorBasis_repr_contr_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : + ((realLorentz d).tensorBasis ![]).repr {v | μ ⊗ ω | μ}ᵀ (fun i => i.elim0) = + ∑ μ, v μ * ω μ := by + simp [Species.Tensor.tensorBasis_repr_contr_unit, Species.tensorBasis_repr_reindex, + Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_toTensor_vector, + tensorBasis_repr_toTensor_coVector] + +open scoped Lorentz.Vector in +/-- The Minkowski product is the contraction `η_μν v^μ w^ν`, a tensor with no slots whose one +component is `⟪v, w⟫ₘ`. -/ +lemma tensorBasis_repr_formTensor_contr_vector (d : ℕ) (v w : Vector d) : + ((realLorentz d).tensorBasis ![]).repr + {(realLorentz d).formTensor down | μ ν ⊗ v | μ ⊗ w | ν}ᵀ (fun i => i.elim0) = ⟪v, w⟫ₘ := by + tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_toTensor_vector] + rw [Vector.minkowskiProduct_toCoord_minkowskiMatrix] + refine Finset.sum_congr rfl fun μ _ => ?_ + rw [Finset.sum_eq_single μ (fun ν _ h => by simp [minkowskiMatrix.off_diag_zero (Ne.symm h)]) + (by simp)] + +/-- The contraction `F_μν G^μν` of `F` and `G` in `Vector d ⊗ Vector d`, with both indices of `F` +lowered, is `∑ η_μμ η_νν F^μν G^μν` in components. -/ +lemma tensorBasis_repr_toDualAt_contr_vector_tmul (d : ℕ) (F G : Vector d ⊗[ℝ] Vector d) : + ((realLorentz d).tensorBasis ![]).repr {F | ↓μ ↓ν ⊗ G | μ ν}ᵀ (fun i => i.elim0) = + ∑ μ, ∑ ν, minkowskiMatrix μ μ * minkowskiMatrix ν ν * Vector.tensorComponent μ ν F * + Vector.tensorComponent μ ν G := by + tensor_components [tensorBasis_repr_toDualAt, tensorBasis_repr_toTensor_vector_tmul, + ← Vector.tensorComponent_apply] + exact Finset.sum_congr rfl fun μ _ => Finset.sum_congr rfl fun ν _ => by ring + +/-- The pairing `v^μ ω_μ` of a vector with a covector is Lorentz invariant. -/ +lemma contr_vector_coVector_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) (ω : CoVector d) : + {Tensorial.rep Λ v | μ ⊗ Tensorial.rep Λ ω | μ}ᵀ = {v | μ ⊗ ω | μ}ᵀ := by + rw [Tensorial.toTensor_rep, Tensorial.toTensor_rep, Species.tmulEquiv_smul, + Species.Tensor.contr_smul, Species.smul_eq_self_of_isEmpty] + +end realLorentz + end Tensors From db1861d20bda673428e5f1051e9ec359b40ee5b7 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 10:18:53 +0000 Subject: [PATCH 012/242] refactor(Species): toLin is the interface for a tensor read as a linear map (snake, toLin_tmul, toLin_map, basis_repr_toLin); invariant_of_contr, invariant_of_up; Module.Basis.lTensor_pairingToLinearMap_coevaluation; cast_apply_congr Co-authored-by: Claude Opus 5.5 --- .../Mathematics/ForMathlib/Contraction.lean | 17 + Physlib/Mathematics/Tensors/Contraction.lean | 21 +- Physlib/Mathematics/Tensors/Inverse.lean | 2 +- Physlib/Mathematics/Tensors/Metric.lean | 6 +- Physlib/Mathematics/Tensors/Product.lean | 7 +- Physlib/Mathematics/Tensors/Species.lean | 303 +++++++++--------- .../Tensors/RealTensor/Species.lean | 4 +- 7 files changed, 186 insertions(+), 174 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 64dd8b89fa..00f9d60707 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -42,6 +42,8 @@ has components `z_μ^A`, where `A` labels a basis of `U`, and reads as the map ` `∑_I (b x)_I (b' y)_I` of two bases with the same labels, and the delta `∑_I b_I ⊗ b'_I`. - `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and `Module.Basis.eq_coordPairing` : it is the only such pairing. +- `Module.Basis.lTensor_pairingToLinearMap_coevaluation` : a tensor is recovered from its linear + map through the delta. - `Module.Basis.pairingToLinearMap_coordPairing_coevaluation` : the delta read through the coordinate pairing is the identity. @@ -164,4 +166,19 @@ lemma pairingToLinearMap_coordPairing_coevaluation (b : Module.Basis ι R M) simp only [pairingToLinearMap_tmul, coordPairing_tmul_basis, LinearMap.id_apply] exact b.sum_equivFun x +/-- The other half of the snake law: a tensor `z ∈ N ⊗ U` is recovered from its linear map +`x ↦ z x` by applying the map to the `M` factor of `∑_I b'_I ⊗ b_I`, `z = b'_I ⊗ z(b_I)`. -/ +lemma lTensor_pairingToLinearMap_coevaluation {U : Type*} [AddCommMonoid U] [Module R U] + (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (z : N ⊗[R] U) : + (pairingToLinearMap (b.coordPairing b') z).lTensor N (b'.coevaluation b) = z := by + induction z using TensorProduct.inductionOn with + | add z₁ z₂ h₁ h₂ => simp only [map_add, LinearMap.lTensor_add, LinearMap.add_apply, h₁, h₂] + | tmul w y => + simp only [coevaluation, map_sum, LinearMap.lTensor_tmul, pairingToLinearMap_tmul, + TensorProduct.tmul_smul] + conv_rhs => rw [← b'.sum_repr w, TensorProduct.sum_tmul] + exact Finset.sum_congr rfl fun I _ => by + rw [coordPairing_comm, coordPairing_tmul_basis, Module.Basis.equivFun_apply, + TensorProduct.smul_tmul'] + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index c95fa3b45e..07f0f53d09 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -300,11 +300,11 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), fun j' => (h₂ j').imp fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ - · rw [cast_eq_cast_of_eq p h, cast_eq_cast_of_eq p h'] + · rw [cast_apply_congr p h, cast_apply_congr p h'] exact contr_cast_tmul_cast _ _ _ _ _ _ - · rw [cast_eq_cast_of_eq p h, cast_eq_cast_of_eq p h'] + · rw [cast_apply_congr p h, cast_apply_congr p h'] exact contr_cast_tmul_cast_symm _ _ _ _ _ _ - · exact congrArg S.pure (funext fun x => cast_eq_cast_of_eq p (hfree x) _) + · exact congrArg S.pure (funext fun x => cast_apply_congr p (hfree x) _) /-- A contraction of a tensor that changes sign under a relabeling `π` of its slots is zero, when `π` only permutes the pairs along `e` and keeps the remaining slots. The hypothesis `ht` has the @@ -479,8 +479,8 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by rw [deltaTensor, contr_matMul, (S.twoSlotEquiv _ _).injective.eq_iff, ← (S.toLin (a := x)).injective.eq_iff, toLin, LinearEquiv.coe_ofLinearMap, - pairingToLinearMap_lTensor, S.snake x.dual] - exact Iff.rfl + pairingToLinearMap_lTensor] + exact Iff.of_eq (congrArg (_ = ·) (S.snake x.dual)) /-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is @@ -512,7 +512,7 @@ def _root_.Tensors.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Col · simp /-- On a pure tensor, contracting the slot `i` with the first slot of `F` applies `F`, read as a -linear map through `S.contr a`, to the vector at `i`. The vectors `q` at the other slots are +linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other slots are those of `p`, moved along `h`. -/ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) @@ -520,25 +520,24 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : contr (ColorEquiv.matMulAt i a b ha hi h) (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoSlotEquiv a.dual b F)) = - S.pure (Function.update q i - (S.cast hi.symm (pairingToLinearMap (S.contr a).toLinearMap F (S.cast ha (p i))))) := by + S.pure (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) := by induction F using TensorProduct.inductionOn with | add F F' hF hF' => simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] rw [pure, pure, pure, MultilinearMap.map_update_add, map_add] | tmul x y => - rw [pairingToLinearMap_tmul, map_smul] + rw [toLin_tmul, map_smul] conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] rw [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] congr 2 funext z by_cases hz : z = i · subst hz - rw [Function.update_self, cast_eq_cast_of_eq _ (show + rw [Function.update_self, cast_apply_congr _ (show (ColorEquiv.matMulAt z a b ha hi h).toEquiv.symm (.inl z) = .inr 1 by simp [ColorEquiv.matMulAt])] rfl - · rw [Function.update_of_ne hz, cast_eq_cast_of_eq _ (show + · rw [Function.update_of_ne hz, cast_apply_congr _ (show (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by simp [ColorEquiv.matMulAt, hz]), hq z hz] diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 21d6e9a2ac..17c06779b8 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -173,7 +173,7 @@ lemma inverse_map_rep (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := by refine inverse_eq_left_inv _ (S.toLin_map_rep_bijective g h Λ) (LinearMap.ext fun x => ?_) obtain ⟨y, rfl⟩ : ∃ y, S.rep a.dual Λ y = x := ⟨S.rep a.dual Λ⁻¹ x, by simp⟩ - rw [LinearMap.comp_apply, S.toLin_map_rep, S.toLin_map_rep (a := b.dual) (S.inverse g), + rw [LinearMap.comp_apply, S.toLin_map, S.toLin_map (a := b.dual) _ _ (S.inverse g), ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h, LinearMap.id_apply, LinearMap.id_apply] diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 1d4d4b0bf4..813bc12f8f 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -189,7 +189,7 @@ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : S.V c ≃ₗ[k] S.V /-- `toDual` contracts `v` with the first index of the form at `c.dual`. -/ lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : - S.toDual c v = pairingToLinearMap (S.contr c).toLinearMap (form c.dual) v := rfl + S.toDual c v = S.toLin (form c.dual) v := rfl /-- Raising is the inverse of lowering. The inverse of `S.toDual c` is `S.toDual c.dual`. -/ lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDual c.dual := rfl @@ -197,13 +197,13 @@ lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDua /-- Lowering and raising commute with the group action, `(Λv)_ν = Λ(v_ν)`. -/ lemma toDual_rep (c : Color K) [S.HasForm c.kind] (g : G) (v : S.V c) : S.toDual c (S.rep c g v) = S.rep c.dual g (S.toDual c v) := by - rw [toDual_apply, toDual_apply, ← S.pairingToLinearMap_contr_map_rep c g, form_invariant] + rw [toDual_apply, toDual_apply, ← S.toLin_map, form_invariant] /-- Lowering or raising a vector in components, `v_ν = v^μ g_μν`. -/ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : (S.basis c.dual).repr (S.toDual c v) ν = ∑ μ, (S.basis c).repr v μ * S.formMatrix c.dual μ ν := by - rw [toDual_apply, basis_repr_pairingToLinearMap_contr] + rw [toDual_apply, basis_repr_toLin] rfl /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 41a14faf3d..1a76b8cb5a 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -464,9 +464,10 @@ lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) a b from (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) intro a b - have h := basis_repr_pairingToLinearMap_contr c (S.delta c) (S.basis c a) b - simp only [S.snake, LinearMap.id_apply, Module.Basis.repr_self, Finsupp.single_apply, ite_mul, - one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true] at h + have h := basis_repr_toLin c.dual (S.delta c) (S.basis c a) b + rw [S.snake, LinearMap.id_apply, Module.Basis.repr_self] at h + simp only [Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, + ite_true] at h rw [Matrix.one_apply, ← h] end Species diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 721a2ca9dd..674e75a3ab 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -27,38 +27,39 @@ The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an in runs over. For Lorentz tensors `basisIdx vec = Fin 1 ⊕ Fin d`, one time label and `d` space labels, and the number of labels is the dimension. The labels depend on the kind alone, so an index and its dual share them, as `μ` in `v^μ` and `α_μ`. `basisIdx` is a parameter of the -structure, not a field, so that a conjugate species gives conjugate kinds the same labels -(`Conjugation.lean`). +structure, not a field, so that a conjugate species can give conjugate kinds the same labels. The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, -which makes `V c.dual` the dual of `V c`. +which makes `V c.dual` the dual of `V c`. The law is stated at every color for a uniform interface. +At a lower color it follows from the upper one, and `invariant_of_up` gives it so. The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in -`V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`), the contraction is -symmetric (`contr_tmul_symm`), the delta acts as the identity, `u^μ δ_μ^ν = u^ν` (`snake`), and -both are invariant. Indices name slots, as in abstract index notation, so `u^μ α_μ` is -`S.contr c (u ⊗ α)`. +`V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction +is symmetric (`contr_tmul_symm`). A tensor `z` in `V a ⊗ U` is read as the linear map +`S.toLin z : V a.dual → U` that contracts a vector with the first factor of `z`, and the delta is +then the identity, `u^μ δ_μ^ν = u^ν` (`snake`). Indices name slots, as in abstract index notation, +so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## ii. Key results -- `Species` is the data and the law of a tensor species. -- `Species.contr` and `Species.delta` are the contraction `u^μ α_μ` and the delta `δ_μ^ν`, with - `Species.contr_basis`, `Species.contr_tmul_symm` and `Species.snake`. +- `Species` is the data and the law of a tensor species, and `Species.invariant_of_up` gives the + law from its upper colors. +- `Species.contr` is the contraction `u^μ α_μ`, with `Species.contr_basis`, + `Species.contr_tmul_symm` and `Species.contr_invariant`. - `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, and `Species.ext_contr` proves that the contraction is nondegenerate. -- `Species.coordPairing_rep_of_contr` gives the law `invariant` from an invariant pairing for - which the bases are dual. -- `Species.contr_invariant` and `Species.delta_invariant` prove that `u^μ α_μ` and `δ_μ^ν` are - invariant. -- `Species.delta_eq_comm_delta_dual` proves that the delta of `c` is the delta of `c.dual` with - its factors swapped. -- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, and - `Species.toLin_map_rep` proves that it commutes with the group action, `(Λg)(Λv) = Λ(g v)`. -- `Species.basis_repr_pairingToLinearMap_contr` gives the components of a tensor in - `V c.dual ⊗ V d` read as a linear map, and `Species.toLin_comp_toLin_of_mul_eq_one` proves that - two such maps are inverse when their matrices are. +- `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the + bases are dual. +- `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and + `Species.delta_invariant`. +- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.snake` proves + that the delta is the identity, and `Species.toLin_map` that `toLin` commutes with the group + action, `(Λz)(Λv) = Λ(z v)`. +- `Species.basis_repr_toLin` gives `toLin` in components, and + `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their + matrices are. - `Species.cast` moves a vector along an equality of colors, `Species.cast_rep` proves that this commutes with the group action, and `Species.basis_repr_cast` that it keeps components. - `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` prove that the @@ -66,9 +67,9 @@ both are invariant. Indices name slots, as in abstract index notation, so `u^μ ## iii. Table of contents -- A. Species, the contraction and the delta -- B. Invariance, symmetry of the delta, and tensors of two factors as linear maps -- C. Components of a tensor of two factors read as a linear map +- A. Species +- B. The contraction +- C. The delta, and tensors of two factors as linear maps - D. Moving a vector along an equality of colors ## iv. References @@ -85,7 +86,7 @@ namespace Tensors /-! -## A. Species, the contraction and the delta +## A. Species -/ @@ -114,59 +115,81 @@ attribute [instance] Species.addCommGroup Species.module namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] +/-- The law `invariant` at every color from the law at the upper colors. At a lower color the +pairing is the pairing at the dual, upper color with its factors swapped. -/ +lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, Module k (V c)] + (rep : (c : Color K) → Representation k G (V c)) + (basis : (c : Color K) → Module.Basis (basisIdx c.kind) k (V c)) + (h : ∀ κ g x y, (basis ⟨κ, .up⟩).coordPairing (basis ⟨κ, .down⟩) + (rep ⟨κ, .up⟩ g x ⊗ₜ rep ⟨κ, .down⟩ g y) = + (basis ⟨κ, .up⟩).coordPairing (basis ⟨κ, .down⟩) (x ⊗ₜ y)) : + ∀ c g x y, (basis c).coordPairing (basis c.dual) (rep c g x ⊗ₜ rep c.dual g y) = + (basis c).coordPairing (basis c.dual) (x ⊗ₜ y) + | ⟨κ, .up⟩, g, x, y => h κ g x y + | ⟨κ, .down⟩, g, x, y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| + (h κ g y x).trans (Module.Basis.coordPairing_comm _ _ _ _) + +/-! + +## B. The contraction + +-/ + +variable (S : Species k G K basisIdx) + /-- The invariant contraction `V c ⊗ V c.dual → k`, `u ⊗ α ↦ u^μ α_μ`, the coordinate pairing of the bases. The vector of color `c` is the first factor. -/ -noncomputable def contr (S : Species k G K basisIdx) (c : Color K) : +noncomputable def contr (c : Color K) : ((S.rep c).tprod (S.rep c.dual)).IntertwiningMap (Representation.trivial k G k) := ⟨(S.basis c).coordPairing (S.basis c.dual), fun g => TensorProduct.ext' fun x y => S.invariant c g x y⟩ -/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of -the dual color is first. -/ -noncomputable def delta (S : Species k G K basisIdx) (c : Color K) : S.V c.dual ⊗[k] S.V c := - (S.basis c.dual).coevaluation (S.basis c) - /-- The bases of a color and its dual are dual, `e^μ(e_ν) = δ^μ_ν`. -/ -lemma contr_basis (S : Species k G K basisIdx) (c : Color K) (μ ν : basisIdx c.kind) : +lemma contr_basis (c : Color K) (μ ν : basisIdx c.kind) : S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) = if μ = ν then 1 else 0 := Module.Basis.coordPairing_basis_basis _ _ _ _ +variable {S} in /-- The contraction in components, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. -/ -lemma contr_tmul_eq_sum_basis {S : Species k G K basisIdx} (c : Color K) (x : S.V c) - (y : S.V c.dual) : +lemma contr_tmul_eq_sum_basis (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ y) = ∑ μ, (S.basis c).repr x μ * (S.basis c.dual).repr y μ := Module.Basis.coordPairing_tmul _ _ x y /-- The contraction is symmetric, `u^μ α_μ = α_μ u^μ`. -/ -lemma contr_tmul_symm (S : Species k G K basisIdx) (c : Color K) (x : S.V c) (y : S.V c.dual) : +lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := Module.Basis.coordPairing_comm _ _ _ _ -/-- Contracting a basis vector `e_μ` with `α` reads off its component `α_μ`. -/ -lemma contr_basis_tmul (S : Species k G K basisIdx) (c : Color K) (μ : basisIdx c.kind) - (α : S.V c.dual) : S.contr c (S.basis c μ ⊗ₜ α) = (S.basis c.dual).repr α μ := by - rw [contr_tmul_symm] - exact Module.Basis.coordPairing_tmul_basis _ _ _ _ - +variable {S} in /-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the same contraction against every vector of `c` are equal. -/ -lemma ext_contr {S : Species k G K basisIdx} {c : Color K} {α α' : S.V c.dual} +lemma ext_contr {c : Color K} {α α' : S.V c.dual} (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := (S.basis c.dual).repr.injective <| Finsupp.ext fun μ => by - simpa [contr_basis_tmul] using h (S.basis c μ) + -- Contracting `e_μ` with `α` reads off the component `α_μ`. + have hα (β : S.V c.dual) : S.contr c (S.basis c μ ⊗ₜ β) = (S.basis c.dual).repr β μ := by + rw [contr_tmul_symm] + exact Module.Basis.coordPairing_tmul_basis _ _ _ _ + simpa [hα] using h (S.basis c μ) -/-- The snake law `u^μ δ_μ^ν = u^ν`: the delta, read as a linear map through the contraction -(`pairingToLinearMap`), is the identity. The vector `u` is contracted with the first factor of -the delta. -/ -lemma snake (S : Species k G K basisIdx) (c : Color K) : - pairingToLinearMap (S.contr c).toLinearMap (S.delta c) = LinearMap.id := - Module.Basis.pairingToLinearMap_coordPairing_coevaluation _ _ +/-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ +lemma contr_invariant (c : Color K) (g : G) (x : S.V c) (y : S.V c.dual) : + S.contr c (S.rep c g x ⊗ₜ[k] S.rep c.dual g y) = S.contr c (x ⊗ₜ[k] y) := + S.invariant c g x y + +/-- Contracting `u` with `Λα` is contracting `Λ⁻¹u` with `α`. -/ +lemma contr_tmul_rep (c : Color K) (g : G) (u : S.V c) (α : S.V c.dual) : + S.contr c (u ⊗ₜ S.rep c.dual g α) = S.contr c (S.rep c g⁻¹ u ⊗ₜ α) := by + conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g u] + exact S.contr_invariant c g _ α -/-- The invariance `Species.invariant`, from an invariant pairing for which the bases are dual. -/ -lemma coordPairing_rep_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommGroup M] +omit S in +/-- The law `invariant`, from an invariant pairing `p` for which the bases are dual. It is how a +species is built from a pairing that is known to be invariant. -/ +lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommGroup M] [Module k M] [AddCommGroup N] [Module k N] {ρ : Representation k G M} {ρ' : Representation k G N} {b : Module.Basis ι k M} {b' : Module.Basis ι k N} (p : (ρ.tprod ρ').IntertwiningMap (Representation.trivial k G k)) @@ -177,126 +200,97 @@ lemma coordPairing_rep_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [A /-! -## B. Invariance, symmetry of the delta, and tensors of two factors as linear maps +## C. The delta, and tensors of two factors as linear maps -/ -/-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ -lemma contr_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) (x : S.V c) - (y : S.V c.dual) : S.contr c (S.rep c g x ⊗ₜ[k] S.rep c.dual g y) = S.contr c (x ⊗ₜ[k] y) := by - change S.contr c ((S.rep c).tprod (S.rep c.dual) g (x ⊗ₜ[k] y)) = _ - rw [(S.contr c).isIntertwining] - simp - -/-- Contracting `u` with `Λα` is contracting `Λ⁻¹u` with `α`. -/ -lemma contr_tmul_rep (S : Species k G K basisIdx) (c : Color K) (g : G) (u : S.V c) - (α : S.V c.dual) : S.contr c (u ⊗ₜ S.rep c.dual g α) = S.contr c (S.rep c g⁻¹ u ⊗ₜ α) := by - conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g u] - exact S.contr_invariant c g _ α - -/-- A tensor `z ∈ V c.dual ⊗ U` read through the contraction as a linear map `x ↦ z x` from -`V c` to `U` satisfies `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and linear map `f` on `U`. -For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ -lemma pairingToLinearMap_contr_map_rep (S : Species k G K basisIdx) (c : Color K) (g : G) - {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') - (z : S.V c.dual ⊗[k] U) (x : S.V c) : - pairingToLinearMap (S.contr c).toLinearMap (TensorProduct.map (S.rep c.dual g) f z) - (S.rep c g x) = f (pairingToLinearMap (S.contr c).toLinearMap z x) := by - induction z using TensorProduct.inductionOn with - | tmul w y => - simp only [TensorProduct.map_tmul, pairingToLinearMap_tmul, map_smul, - Representation.IntertwiningMap.toLinearMap_apply, S.contr_invariant] - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] - -/-- A tensor `z ∈ V c.dual ⊗ U` is recovered from its linear map `x ↦ z x` by applying the map -to the `V c` factor of the delta of `c.dual` with its factors swapped, `z = e^μ ⊗ z(e_μ)`. -/ -lemma lTensor_pairingToLinearMap_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) - {U : Type} [AddCommGroup U] [Module k U] (z : S.V c.dual ⊗[k] U) : - (pairingToLinearMap (S.contr c).toLinearMap z).lTensor (S.V c.dual) - (TensorProduct.comm k _ _ (S.delta c.dual)) = z := by - induction z using TensorProduct.inductionOn with - | add z₁ z₂ h₁ h₂ => simp only [map_add, LinearMap.lTensor_add, LinearMap.add_apply, h₁, h₂] - | tmul w y => - -- On `w ⊗ y` this is the snake law at `c.dual`, applied to `w`, with `contr_tmul_symm`. - conv_rhs => rw [← LinearMap.id_apply (R := k) w, ← S.snake c.dual] - induction S.delta c.dual using TensorProduct.inductionOn with - | add D₁ D₂ h₁ h₂ => simp only [map_add, LinearMap.add_apply, add_tmul, h₁, h₂] - | tmul e₁ e₂ => - simp only [TensorProduct.comm_tmul, LinearMap.lTensor_tmul, pairingToLinearMap_tmul, - Representation.IntertwiningMap.toLinearMap_apply, tmul_smul, smul_tmul'] - rw [S.contr_tmul_symm c e₁ w] +/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of +the dual color is first. -/ +noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := + (S.basis c.dual).coevaluation (S.basis c) /-- The delta of `c` is the delta of `c.dual ∈ V c ⊗ V c.dual` with its factors swapped. -/ -lemma delta_eq_comm_delta_dual (S : Species k G K basisIdx) (c : Color K) : +lemma delta_eq_comm_delta_dual (c : Color K) : S.delta c = TensorProduct.comm k _ _ (S.delta c.dual) := by simp only [delta, Module.Basis.coevaluation, map_sum, TensorProduct.comm_tmul] /-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the -first factor of `z`, as `v^μ ↦ v^μ g_μν` for `g` in `V a ⊗ V b`. Every linear map arises so. -/ -noncomputable def toLin (S : Species k G K basisIdx) {a : Color K} {U : Type} [AddCommGroup U] - [Module k U] : S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := - -- The inverse sends `f` to `(id ⊗ f) δ`, by the snake law. +first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. +Every linear map arises so. -/ +noncomputable def toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : + S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := + -- The inverse sends `f` to `(id ⊗ f) δ`. Both composites are the identity, by the two halves of + -- the snake law for dual bases. LinearEquiv.ofLinearMap (pairingToLinearMap (S.contr a.dual).toLinearMap) ((LinearMap.lTensorHom (S.V a.dual.dual)).flip (S.delta a.dual)) - (LinearMap.ext fun f => by simp [pairingToLinearMap_lTensor, S.snake]) - (LinearMap.ext fun z => by simpa [S.delta_eq_comm_delta_dual a.dual] using - S.lTensor_pairingToLinearMap_comm_delta_dual a.dual z) + (LinearMap.ext fun f => by + simp [pairingToLinearMap_lTensor, delta, contr, + Module.Basis.pairingToLinearMap_coordPairing_coevaluation]) + (LinearMap.ext fun z => Module.Basis.lTensor_pairingToLinearMap_coevaluation _ _ z) + +/-- A pure tensor `x ⊗ y` acts as `v ↦ (v^μ x_μ) y`, contracting `v` with its first factor. -/ +@[simp] +lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V a) (y : U) + (v : S.V a.dual) : S.toLin (x ⊗ₜ y) v = S.contr a.dual (v ⊗ₜ x) • y := + rfl + +/-- The snake law `u^μ δ_μ^ν = u^ν`, the delta read as a linear map is the identity. -/ +lemma snake (c : Color K) : S.toLin (S.delta c) = LinearMap.id := + Module.Basis.pairingToLinearMap_coordPairing_coevaluation _ _ + +/-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and +linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ +lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U] + [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : + S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by + induction z using TensorProduct.inductionOn with + | tmul w y => + change pairingToLinearMap (S.contr a.dual).toLinearMap (S.rep a g w ⊗ₜ f y) _ = + f (pairingToLinearMap (S.contr a.dual).toLinearMap (w ⊗ₜ y) x) + simp only [pairingToLinearMap_tmul, map_smul, Representation.IntertwiningMap.toLinearMap_apply] + rw [show S.rep a g w = S.rep a.dual.dual g w from rfl, S.contr_invariant] + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] /-- The delta is invariant, `Λ δ = δ`. -/ -lemma delta_invariant (S : Species k G K basisIdx) (c : Color K) (g : G) : +lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by -- Both act as the identity: `(Λδ)(Λx) = Λx` by the snake law. - apply (S.toLin (a := c.dual)).injective - change pairingToLinearMap (S.contr c).toLinearMap _ = pairingToLinearMap (S.contr c).toLinearMap _ + apply S.toLin.injective ext x conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g x] - rw [S.pairingToLinearMap_contr_map_rep, S.snake c, LinearMap.id_apply, LinearMap.id_apply, - Representation.self_inv_apply] - -/-- The linear map of `Λg` sends `Λv` to `Λ(g v)`, as `(Λg)_μν (Λv)^μ = Λ(g_μν v^μ)`. -/ -lemma toLin_map_rep (S : Species k G K basisIdx) {a b : Color K} (g : S.V a ⊗[k] S.V b) (Λ : G) - (x : S.V a.dual) : - S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) (S.rep a.dual Λ x) = - S.rep b Λ (S.toLin g x) := - S.pairingToLinearMap_contr_map_rep a.dual Λ (S.rep b Λ) g x - -/-- The linear map of `Λg` is bijective when that of `g` is. -/ -lemma toLin_map_rep_bijective (S : Species k G K basisIdx) {a b : Color K} - (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) : - Function.Bijective (S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g)) := by - -- The map of `Λg` is `Λ ∘ g ∘ Λ⁻¹`, a composite of bijections. - have e : ⇑(S.toLin (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g)) = - S.rep b Λ ∘ S.toLin g ∘ S.rep a.dual Λ⁻¹ := by + rw [show S.rep c g⁻¹ x = S.rep c.dual.dual g⁻¹ x from rfl, S.toLin_map, S.snake c, + LinearMap.id_apply, LinearMap.id_apply, Representation.self_inv_apply] + +/-- The linear map of `Λz` is bijective when that of `z` is. -/ +lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) + (h : Function.Bijective (S.toLin z)) (g : G) : + Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by + -- The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, a composite of bijections. + have e : ⇑(S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) = + S.rep b g ∘ S.toLin z ∘ S.rep a.dual g⁻¹ := by funext x - simp only [Function.comp_apply, ← S.toLin_map_rep, Representation.self_inv_apply] + rw [Function.comp_apply, Function.comp_apply, ← S.toLin_map, Representation.self_inv_apply] + have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := + Function.bijective_iff_has_inverse.mpr ⟨S.rep c g⁻¹, Representation.inv_self_apply _ _, + Representation.self_inv_apply _ _⟩ rw [e] - refine (?_ : Function.Bijective (S.rep b Λ)).comp (h.comp ?_) - · exact ⟨fun x y hxy => by simpa using congrArg (S.rep b Λ⁻¹) hxy, - fun y => ⟨S.rep b Λ⁻¹ y, Representation.self_inv_apply _ _ _⟩⟩ - · exact ⟨fun x y hxy => by simpa using congrArg (S.rep a.dual Λ) hxy, - fun y => ⟨S.rep a.dual Λ y, Representation.inv_self_apply _ _ _⟩⟩ + exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) -/-! +variable {S} -## C. Components of a tensor of two factors read as a linear map - --/ - -variable {S : Species k G K basisIdx} - -/-- A tensor `F = F_μ^ν e^μ ⊗ e_ν` in `V c.dual ⊗ V d`, read through the contraction as a linear -map, sends `v` to `v^μ F_μ^ν e_ν`. -/ -lemma basis_repr_pairingToLinearMap_contr (c : Color K) {d : Color K} - (F : S.V c.dual ⊗[k] S.V d) (v : S.V c) (ν : basisIdx d.kind) : - (S.basis d).repr (pairingToLinearMap (S.contr c).toLinearMap F v) ν = - ∑ μ, (S.basis c).repr v μ * ((S.basis c.dual).tensorProduct (S.basis d)).repr F (μ, ν) := by - induction F using TensorProduct.inductionOn with +/-- `toLin` in components, `(z v)^ν = v^μ z_μ^ν` for `z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ +lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : S.V c.dual) + (ν : basisIdx d.kind) : + (S.basis d).repr (S.toLin z v) ν = + ∑ μ, (S.basis c.dual).repr v μ * ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν) := by + induction z using TensorProduct.inductionOn with | add x y hx hy => simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, Finset.sum_add_distrib] | tmul x y => + change (S.basis d).repr (pairingToLinearMap (S.contr c.dual).toLinearMap (x ⊗ₜ y) v) ν = _ rw [pairingToLinearMap_tmul, map_smul, Finsupp.smul_apply, smul_eq_mul] - change S.contr c (v ⊗ₜ x) * _ = _ + change S.contr c.dual (v ⊗ₜ x) * _ = _ simp only [contr_tmul_eq_sum_basis, Module.Basis.tensorProduct_repr_tmul_apply, smul_eq_mul, Finset.sum_mul] exact Finset.sum_congr rfl fun _ _ => by ring @@ -311,11 +305,13 @@ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) refine (S.basis c).ext fun μ => (S.basis c).repr.injective (Finsupp.ext fun ν => ?_) have hμν := congrFun (congrFun h μ) ν rw [Matrix.mul_apply, Matrix.one_apply] at hμν - change (S.basis c).repr (pairingToLinearMap (S.contr c.dual).toLinearMap F - (pairingToLinearMap (S.contr c).toLinearMap F' (S.basis c μ))) ν = _ - simp only [basis_repr_pairingToLinearMap_contr, Module.Basis.repr_self, Finsupp.single_apply, - ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true, LinearMap.id_apply] - exact hμν + rw [LinearMap.comp_apply, LinearMap.id_apply, basis_repr_toLin, Module.Basis.repr_self, + Finsupp.single_apply, ← hμν] + refine Finset.sum_congr rfl fun ρ _ => congrArg (· * _) ?_ + -- The components of `F'` applied to `e_μ` are the row `μ` of its matrix. + rw [basis_repr_toLin c.dual F' (S.basis c μ)] + erw [Module.Basis.repr_self] + simp [Finsupp.single_apply] /-! @@ -344,7 +340,7 @@ lemma basis_repr_cast {a b : Color K} (p : a = b) (v : S.V a) (μ : basisIdx b.k /-- Moving the vector of a family `f` at the slot `s` depends only on the slot, so it may be read at any slot `s'` equal to `s`. -/ -lemma cast_eq_cast_of_eq {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} +lemma cast_apply_congr {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} (e : s = s') {b : Color K} (p : d s = b) : S.cast p (f s) = S.cast (e ▸ p) (f s') := by subst e rfl @@ -356,7 +352,8 @@ lemma contr_cast_tmul_cast {d e a b : Color K} (x : S.V d) (y : S.V e) (p : d = subst p p' rfl -/-- `contr_tmul_symm` for vectors moved along equalities of colors, `u^μ α_μ = α_μ u^μ`. -/ +/-- The contraction of two vectors moved along equalities of colors is symmetric, +`u^μ α_μ = α_μ u^μ`. -/ lemma contr_cast_tmul_cast_symm {d e a b : Color K} (x : S.V d) (y : S.V e) (p : d = a) (q : e = a.dual) (p' : e = b) (q' : d = b.dual) : S.contr a (S.cast p x ⊗ₜ S.cast q y) = S.contr b (S.cast p' y ⊗ₜ S.cast q' x) := by diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 19a24471e9..be6caded9c 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -237,9 +237,7 @@ lemma toLin_metric_basis (d : ℕ) (c : Color Kind) (ν : Fin 1 ⊕ Fin d) : (realLorentz d).toLin (metric d c) ((realLorentz d).basis c.dual ν) = minkowskiMatrix ν ν • (realLorentz d).basis c ν := by refine ((realLorentz d).basis c).repr.injective (Finsupp.ext fun ρ => ?_) - change ((realLorentz d).basis c).repr (pairingToLinearMap - ((realLorentz d).contr c.dual).toLinearMap (metric d c) _) ρ = _ - rw [Species.basis_repr_pairingToLinearMap_contr, map_smul] + rw [Species.basis_repr_toLin, map_smul] simp only [Module.Basis.repr_self, Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true, Finsupp.smul_apply, smul_eq_mul, mul_ite, mul_one, mul_zero] From 6c140444b5d4d96ce949baeb6612d7e68b35adb7 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 10:43:24 +0000 Subject: [PATCH 013/242] refactor(Contraction): contr_matMulAt for any tensor via mapAt; contr_eq_of_samePair without its second hypothesis (ColorEquiv.exists_samePair_left); tmulEquiv_contr_left with ColorEquiv.tmulRight and tmulLeft; tensorBasis_repr_contr_one_pair and two_pairs; contr_congr and contr_reindex_pairs removed; mapAt, mapAt_pure, congrAt_eq_mapAt, toLin_lTensor Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 267 +++++++++--------- Physlib/Mathematics/Tensors/Elab.lean | 12 +- Physlib/Mathematics/Tensors/Metric.lean | 10 +- Physlib/Mathematics/Tensors/Species.lean | 11 +- Physlib/Mathematics/Tensors/Tensor.lean | 43 ++- .../Tensors/RealTensor/Species.lean | 4 +- 6 files changed, 198 insertions(+), 149 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 07f0f53d09..509c11be28 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -20,28 +20,19 @@ A contraction pairs indices of dual colors and sums over their common label, as `(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the slots `Fin 3`. On a pure tensor `u ⊗ v ⊗ α` the trace is `(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. -Contraction is first defined with the slots in a standard arrangement, where they come in three -groups. The remaining slots are labeled by a type `κ`, and the pairs by a type `J`. Each pair -`j : J` has a first slot, of color `a j`, and a second slot, of the dual color `(a j).dual`. The -slots are then labeled by the disjoint union `κ ⊕ (J ⊕ J)`, with `inl x` the remaining slot `x`, -`inr (inl j)` the first slot of the pair `j` and `inr (inr j)` its second slot. For the trace -`T^ρμ_μ`, `κ = Fin 1` holds `ρ` and `J = Unit` holds the single pair `μ`. These colors are -`contrColors c' a`, and `Tensor.contrStd c' a` contracts each pair by `S.contr (a j)`, with the -first slot as its first argument. The colors of a pair are dual by construction, so no cast -appears. - -Any other choice of pairs is reduced to the standard arrangement by a relabeling -`σ : ColorEquiv c (contrColors c' a)`, which sends each slot to its place there. -`Tensor.contr σ` is `S.reindex σ` followed by `Tensor.contrStd c' a`. Since `σ` keeps colors, the -two slots of each pair are dual without a separate hypothesis. For the trace, `σ` sends -`0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`. The index notation `{T | ρ μ μ}ᵀ` builds -`σ` from the repeated name. +The pairs to contract are chosen by a relabeling `σ : ColorEquiv c (contrColors c' a)` of the +slots. It sends each free index to a slot `inl x`, of color `c' x`, and the two indices of a pair +`j` to the slots `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the trace, +`σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`, and the index notation +`{T | ρ μ μ}ᵀ` builds it from the repeated name. Since `σ` keeps colors, the two indices of each +pair are dual without a separate hypothesis. `Tensor.contr σ` relabels by `σ` and then contracts +each pair by `S.contr (a j)`, with the first index of the pair as its first argument. Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the product `T ⊗ U` has four slots, which form two pairs and leave no slot, `κ = Empty`. The following laws let a contraction of several tensors be computed one step at a time. -- The order in which the pairs are listed does not matter. Relabeling the pairs gives the same - contraction. +- Only the pairs of indices matter. Two contractions of the same pairs of indices are equal, in + whatever order the pairs are listed and whichever index of a pair comes first. - A tensor that changes sign when its pairs are permuted contracts to zero, since its contraction equals its own negative. For `A` antisymmetric and `B` symmetric, renaming `μ ↔ ν` turns `A^μν B_μν` into `A^νμ B_νμ = -A^μν B_μν`, so `A^μν B_μν = 0`. @@ -51,21 +42,20 @@ laws let a contraction of several tensors be computed one step at a time. `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. For tensors on two slots, the contraction `M^μ_ν N^ν_ρ` of the inner indices is the product of -matrices. The components of a contraction are sums of components of the tensor, -`(tr T)^ρ = ∑ μ, T^ρμ_μ`, since the bases of a color and its dual are dual. +matrices. Contracting an index of any tensor with the first index of a tensor `F` on two slots +applies `F`, read as a linear map, at that index, and for the delta this is the identity. + +The components of a contraction are sums of components of the tensor, `(tr T)^ρ = ∑ μ, T^ρμ_μ`, +since the bases of a color and its dual are dual. ## ii. Key results -- `contrColors` gives the colors of the standard arrangement, the remaining slots and then the - pairs. -- `Species.Tensor.contrStd` contracts a tensor in the standard arrangement. -- `Species.Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`. +- `Species.Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`, with the + colors `contrColors` of the free indices and the pairs. - `Species.Tensor.contr_pure` gives the contraction of a pure tensor. - `Species.Tensor.contr_smul` proves that contraction commutes with the group action. -- `Species.Tensor.contr_congr` proves that a contraction depends only on the bijection of slots. -- `Species.Tensor.contr_reindex_pairs` proves that relabeling the pairs does not change the - contraction, and `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a - relabeling of the slots before or after a contraction. +- `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a relabeling of the + slots before or after a contraction. - `ColorEquiv.SamePair` says that two choices of pairs contract the same two slots, and `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of slots, in any order, are equal. @@ -74,28 +64,28 @@ matrices. The components of a contraction are sums of components of the tensor, antisymmetric and `B` symmetric. - `Species.Tensor.contr_contr` proves that two contractions in sequence are one, along `ColorEquiv.chain`. -- `Species.Tensor.tmulEquiv_contr` proves that contraction commutes with a product by a tensor - that is not contracted, along `ColorEquiv.withSpectators`. -- `Species.Tensor.tmulEquiv_contr_right` is the same with the tensor that is not contracted on the - left, along `ColorEquiv.withSpectatorsLeft`. +- `Species.Tensor.tmulEquiv_contr_left` and `Species.Tensor.tmulEquiv_contr_right` prove that + contracting a factor commutes with the product, along `ColorEquiv.tmulRight` and + `ColorEquiv.tmulLeft`. - `Species.Tensor.contr_matMul` gives the contraction `M^μ_ν N^ν_ρ` of two tensors on two slots, and `Species.Tensor.contr_matMul_eq_deltaTensor_iff` proves that it is the delta exactly when their linear maps compose to the identity. -- `Species.Tensor.contr_matMulAt_pure` contracts any slot `i` with the first slot of a tensor on - two slots, along `ColorEquiv.matMulAt`, and `Species.Tensor.contr_deltaTensor` proves - `T^…μ… δ_μ^ν = T^…ν…`. +- `Species.Tensor.contr_matMulAt` proves that contracting the index `i` with the first index of a + tensor `F` on two slots, along `ColorEquiv.matMulAt`, applies `F` at `i`, and + `Species.Tensor.contr_deltaTensor` that for the delta this is `T^…μ… δ_μ^ν = T^…ν…`. - `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction. -- `Species.Tensor.tensorBasis_repr_contr_unit` and - `Species.Tensor.tensorBasis_repr_contr_unit_sum` give the same with one and with two pairs, one - label per pair. +- `Species.Tensor.tensorBasis_repr_contr_one_pair` and + `Species.Tensor.tensorBasis_repr_contr_two_pairs` give the same with one and with two pairs. ## iii. Table of contents - A. Contraction in the standard arrangement of slots - B. Contraction of chosen pairs of indices - - B.1. Relabeling the slots and the pairs - - B.2. Two contractions are one - - B.3. Products with a tensor that is not contracted + - B.1. Relabeling the slots + - B.2. Contractions of the same pairs of indices + - B.3. Symmetric against antisymmetric + - B.4. Two contractions are one + - B.5. Products with a tensor that is not contracted - C. Products with a tensor on two slots - D. Components @@ -126,7 +116,7 @@ namespace Tensor -/ -/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of slots. The remaining slot `inl x` has +/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of slots. The free index `inl x` has the color `c x`, and the two slots `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors `a j` and `(a j).dual`. -/ noncomputable abbrev _root_.Tensors.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : @@ -135,7 +125,8 @@ noncomputable abbrev _root_.Tensors.contrColors {K κ J : Type} (c : κ → Colo /-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the slot `inr (inl j)` is contracted with the slot `inr (inr j)` by `S.contr (a j)`, with the slot -`inr (inl j)` as its first argument. The slots `inl x` remain, with the colors `c`. -/ +`inr (inl j)` as its first argument. The slots `inl x` are the free indices, with the colors +`c`. It is the body of `Tensor.contr`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : S.Tensor (contrColors c a) →ₗ[k] S.Tensor c := -- The pairs are split into their first halves, of colors `a`, and their second halves, of the @@ -166,8 +157,8 @@ lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) /-- The contraction of the pairs of indices chosen by `σ`, as in `T^ρμ_ν ↦ T^ρμ_μ`. It is `S.reindex σ` followed by `Tensor.contrStd c' a`. For each pair `j` the slot `σ.toEquiv.symm (inr (inl j))`, of color `a j`, is contracted with the slot -`σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The slots `σ.toEquiv.symm (inl x)` remain, -with the colors `c'`. -/ +`σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The slots `σ.toEquiv.symm (inl x)` are the +free indices, with the colors `c'`. -/ noncomputable def contr [Fintype J] {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) : S.Tensor c →ₗ[k] S.Tensor c' := contrStd c' a ∘ₗ (S.reindex σ).toLinearMap @@ -195,47 +186,19 @@ lemma contr_smul [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors congr 1 exact Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) t -/-- A contraction depends only on the bijection of slots, not on the proof that it keeps colors. -/ -lemma contr_congr [Fintype J] {a a' : J → Color K} (ha : a = a') - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) - (h : σ.toEquiv = τ.toEquiv) (t : S.Tensor c) : contr σ t = contr τ t := by - subst ha - obtain ⟨σ, _⟩ := σ - obtain ⟨τ, _⟩ := τ - subst h - rfl - /-! -### B.1. Relabeling the slots and the pairs +### B.1. Relabeling the slots -/ -/-- Relabeling the pairs along `e : J ≃ J'` does not change the contraction. Each pair keeps its -order. -/ -lemma contr_reindex_pairs [Fintype J] [Fintype J'] {a : J → Color K} - (σ : ColorEquiv c (contrColors c' a)) (e : J ≃ J') (t : S.Tensor c) : - contr (σ.trans ((ColorEquiv.refl c').sumCongr - ((ColorEquiv.mk e).sumCongr (.mk e)))) t = contr σ t := by - simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe] - rw [← reindex_reindex] - generalize S.reindex σ t = u - induction u using inductionOn_pure with - | h p => - rw [reindex_pure, contrStd_pure, contrStd_pure] - congr 1 - exact Equiv.prod_comp e.symm fun j => S.contr (a j) (p (.inr (.inl j)) ⊗ₜ p (.inr (.inr j))) - | hsmul r u hu => rw [map_smul, map_smul, hu, map_smul] - | hadd u u' hu hu' => rw [map_add, map_add, hu, hu', map_add] - /-- Relabeling the slots and then contracting is contracting along the composite relabeling. -/ lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : contr σ (S.reindex τ t) = contr (τ.trans σ) t := by simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, reindex_reindex] -/-- Relabeling the remaining slots of a contraction is contracting along the composite -relabeling. -/ +/-- Relabeling the free indices of a contraction is contracting along the composite relabeling. -/ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : S.Tensor c) : S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := by @@ -247,6 +210,12 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → congr 1 exact congrArg S.pure (funext fun x => by simp only [LinearEquiv.cast_apply, cast_cast]; rfl) +/-! + +### B.2. Contractions of the same pairs of indices + +-/ + /-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two slots, in either order. -/ def _root_.Tensors.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) @@ -281,13 +250,36 @@ lemma _root_.Tensors.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> simpa using hτ (h₁.symm.trans h₂) -/-- Two contractions are equal when they keep the same slots in the same order and contract the -same pairs of slots, in any order and with either slot of a pair first. On literal slots the -hypotheses are checked by `decide`. -/ +/-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair +of `τ` is a pair of `σ`. The slots of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ +lemma _root_.Tensors.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} + (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by + have hτ := τ.toEquiv.symm.injective + rcases hs : σ.toEquiv (τ.toEquiv.symm (.inr (.inl j'))) with x | j | j <;> + rw [← Equiv.eq_symm_apply] at hs + · rw [hfree] at hs + have := hτ hs + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · obtain rfl : j'' = j' := by simpa using hτ (h.1.symm.trans hs.symm) + exact ⟨j, .inl h⟩ + · have := hτ (h.1.symm.trans hs.symm) + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · have := hτ (h.2.symm.trans hs.symm) + simp at this + · obtain rfl : j'' = j' := by simpa using hτ (h.2.symm.trans hs.symm) + exact ⟨j, .inr h⟩ + +/-- Two contractions are equal when they keep the same free indices in the same order and contract +the same pairs of indices, in any order and with either index of a pair first. On literal slots +the hypotheses are checked by evaluation. -/ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) - (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (h₂ : ∀ j', ∃ j, σ.SamePair τ j j') (t : S.Tensor c) : + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : S.Tensor c) : contr σ t = contr τ t := by induction t using inductionOn_pure with | hsmul r t ht => rw [map_smul, map_smul, ht] @@ -298,7 +290,8 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' congr 1 · -- The pair `j` of `σ` is the pair `g j` of `τ`, in the same order or swapped. refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), - fun j' => (h₂ j').imp fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ + fun j' => (ColorEquiv.exists_samePair_left hfree (fun j => ⟨g j, hg j⟩) j').imp + fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ · rw [cast_apply_congr p h, cast_apply_congr p h'] exact contr_cast_tmul_cast _ _ _ _ _ _ @@ -306,28 +299,29 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' exact contr_cast_tmul_cast_symm _ _ _ _ _ _ · exact congrArg S.pure (funext fun x => cast_apply_congr p (hfree x) _) +/-! + +### B.3. Symmetric against antisymmetric + +-/ + /-- A contraction of a tensor that changes sign under a relabeling `π` of its slots is zero, when -`π` only permutes the pairs along `e` and keeps the remaining slots. The hypothesis `ht` has the +`π` only permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the form of the index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] [Invertible (2 : k)] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) {t : S.Tensor c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by - -- The colors of the pairs are invariant under `e`, since `π` keeps colors. - have he : ∀ j, a (e.symm j) = a j := fun j => by - set x := σ.toEquiv.symm (.inr (.inl (e.symm j))) - have hx : π.toEquiv x = σ.toEquiv.symm (.inr (.inl j)) := by - rw [Equiv.eq_symm_apply, ← Equiv.trans_apply, hπ] - simp [x] - have := π.color_eq (π.toEquiv x) - rw [Equiv.symm_apply_apply, hx] at this - exact (σ.color_eq _).symm.trans (this.trans (σ.color_eq _)) + -- After `π`, each pair `j` of `σ` is its pair `e.symm j`, and the free indices are kept. + have hs (y) : (π.trans σ).toEquiv.symm y = + σ.toEquiv.symm (((Equiv.refl κ).sumCongr (e.sumCongr e)).symm y) := + congrArg (fun f : Equiv _ _ => f.symm y) hπ + have hσ : contr (π.trans σ) t = contr σ t := + contr_eq_of_samePair _ _ (fun x => by simp [hs]) (fun j => ⟨e.symm j, .inl ⟨by simp [hs], + by simp [hs]⟩⟩) t have h : contr σ t + contr σ t = 0 := by - nth_rw 2 [← contr_reindex_pairs σ e t] - rw [contr_congr (funext he) _ (π.trans σ) hπ.symm] - nth_rw 1 [ht] - simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, - reindex_reindex, map_neg, neg_add_cancel] + conv_lhs => enter [1]; rw [ht] + rw [contr_reindex, map_neg, hσ, neg_add_cancel] rw [← one_smul k (contr σ t), ← invOf_mul_self (2 : k), mul_smul, two_smul, h, smul_zero] /-- `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. The contraction of `A ⊗ B` is zero @@ -347,7 +341,7 @@ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] [Invertible (2 : k)] /-! -### B.2. Two contractions are one +### B.4. Two contractions are one -/ @@ -377,25 +371,25 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ /-! -### B.3. Products with a tensor that is not contracted +### B.5. Products with a tensor that is not contracted -/ -/-- The pairs `σ` of `T`, as pairs of `T ⊗ U`. The indices of `U`, on the slots `ι₂` with the -colors `d`, remain after those of `T` that are not contracted. -/ -noncomputable def _root_.Tensors.ColorEquiv.withSpectators {c : ι₁ → Color K} {c' : κ → Color K} +/-- The pairs `σ` of `T`, as pairs of `T ⊗ U` for a factor `U` on the right, with colors `d`. The +indices of `U` are free and come after the free indices of `T`. -/ +noncomputable def _root_.Tensors.ColorEquiv.tmulRight {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := (σ.sumCongr (.refl d)).trans ((ColorEquiv.sumAssoc _ _ _).trans (((ColorEquiv.refl c').sumCongr (ColorEquiv.sumComm _ _)).trans (ColorEquiv.sumAssoc _ _ _).symm)) -/-- Contracting indices of `T` and then multiplying by `U` is contracting the same indices of -`T ⊗ U`, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ -lemma tmulEquiv_contr [Fintype J] {a : J → Color K} {d : ι₂ → Color K} +/-- Contracting indices of the left factor commutes with the product, +`(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ +lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (u : S.Tensor d) : S.tmulEquiv c' d (contr σ t ⊗ₜ u) = - contr (σ.withSpectators d) (S.tmulEquiv c d (t ⊗ₜ u)) := by + contr (σ.tmulRight d) (S.tmulEquiv c d (t ⊗ₜ u)) := by induction t using inductionOn_pure with | hsmul r t ht => simp only [map_smul, ← smul_tmul', ht] | hadd x y hx hy => simp only [map_add, add_tmul, hx, hy] @@ -408,19 +402,19 @@ lemma tmulEquiv_contr [Fintype J] {a : J → Color K} {d : ι₂ → Color K} congr 1 exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) -/-- The pairs `σ` of `T`, as pairs of `U ⊗ T`. The indices of `U`, with the colors `d`, come before -those of `T` that are not contracted. -/ -noncomputable def _root_.Tensors.ColorEquiv.withSpectatorsLeft {c : ι₁ → Color K} +/-- The pairs `σ` of `T`, as pairs of `U ⊗ T` for a factor `U` on the left, with colors `d`. The +indices of `U` are free and come before the free indices of `T`. -/ +noncomputable def _root_.Tensors.ColorEquiv.tmulLeft {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : ColorEquiv (prodColors d c) (contrColors (prodColors d c') a) := ((ColorEquiv.refl d).sumCongr σ).trans (ColorEquiv.sumAssoc _ _ _).symm -/-- Contracting indices of `T` and then multiplying by `U` on the left is contracting the same -indices of `U ⊗ T`, `U^ρ (T^μ_μν) = (U ⊗ T)^ρμ_μν`. -/ +/-- Contracting indices of the right factor commutes with the product, +`U^ρ (T^μ_μν) = (U ⊗ T)^ρμ_μν`. -/ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color K} (σ : ColorEquiv c (contrColors c' a)) (u : S.Tensor d) (t : S.Tensor c) : S.tmulEquiv d c' (u ⊗ₜ contr σ t) = - contr (σ.withSpectatorsLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := by + contr (σ.tmulLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := by induction t using inductionOn_pure with | hsmul r t ht => simp only [map_smul, tmul_smul, ht] | hadd x y hx hy => simp only [map_add, tmul_add, hx, hy] @@ -439,9 +433,9 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color -/ -/-- The pair of the product `M^μ_ν N^ν_ρ` of two tensors on two slots, the second slot of `M`, of -color `c`, with the first slot of `N`, of color `c.dual`. The slots of `μ` and `ρ` remain, as `0` -and `1`. -/ +/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two slots that contracts the +second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are +`0` and `1`. -/ def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where toEquiv.toFun @@ -451,12 +445,12 @@ def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : toEquiv.left_inv := by decide toEquiv.right_inv := by decide -/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots. It is `M` with its second factor -mapped by `N`, read through `S.contr c` as the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ +/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots is `M` with its second factor mapped +by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : contr (ColorEquiv.matMul a c b) (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.twoSlotEquiv _ b N)) = - S.twoSlotEquiv a b ((pairingToLinearMap (S.contr c).toLinearMap N).lTensor _ M) := by + S.twoSlotEquiv a b ((S.toLin N).lTensor _ M) := by induction M using TensorProduct.inductionOn with | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] | tmul m₁ m₂ => @@ -465,7 +459,7 @@ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] | tmul n₁ n₂ => simp only [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, - LinearMap.lTensor_tmul, pairingToLinearMap_tmul, tmul_smul, map_smul] + LinearMap.lTensor_tmul, toLin_tmul, tmul_smul, map_smul] congr 2 funext j fin_cases j <;> rfl @@ -478,9 +472,7 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by rw [deltaTensor, contr_matMul, (S.twoSlotEquiv _ _).injective.eq_iff, - ← (S.toLin (a := x)).injective.eq_iff, toLin, LinearEquiv.coe_ofLinearMap, - pairingToLinearMap_lTensor] - exact Iff.of_eq (congrArg (_ = ·) (S.snake x.dual)) + ← (S.toLin (a := x)).injective.eq_iff, toLin_lTensor, S.snake x.dual] /-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is @@ -541,19 +533,38 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by simp [ColorEquiv.matMulAt, hz]), hq z hz] -/-- Contracting the slot `i` with the first slot of the delta is the identity, +/-- Contracting the index `i` of `t` with the first index of a tensor `F` on two slots applies `F`, +read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ +lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) + (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) + (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a b ha hi h) + (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoSlotEquiv a.dual b F)) = + S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [← smul_tmul', map_smul, map_smul, ht, map_smul] + | hadd t t' ht ht' => rw [add_tmul, map_add, map_add, ht, ht', map_add] + | h p => + let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) + rw [contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx]), + mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) + (Function.update_self _ _ _) (fun x hx => by simp [q, hx])] + +/-- Contracting the index `i` with the first index of the delta is the identity, `T^…μ… δ_μ^ν = T^…ν…`. -/ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) (t : S.Tensor c) : contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by + rw [deltaTensor, contr_matMulAt, snake] induction t using inductionOn_pure with - | hsmul r t ht => rw [← smul_tmul', map_smul, map_smul, ht] - | hadd t t' ht ht' => rw [add_tmul, map_add, map_add, ht, ht'] + | hsmul r t ht => rw [map_smul, ht] + | hadd t t' ht ht' => rw [map_add, ht, ht'] | h p => - rw [deltaTensor, contr_matMulAt_pure i a a ha ha _ _ p p (fun _ _ => rfl), snake, - LinearMap.id_apply, LinearEquiv.cast_apply, LinearEquiv.cast_apply, cast_cast] - exact congrArg S.pure (Function.update_eq_self_iff.2 rfl) + refine mapAt_pure i _ _ p p ?_ fun _ _ => rfl + simp only [LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, LinearMap.id_apply, + LinearEquiv.cast_apply, cast_cast] + rfl /-! @@ -585,7 +596,7 @@ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] /-- The components of the contraction of one pair in dual bases, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a single label `μ`. -/ -lemma tensorBasis_repr_contr_unit [Fintype κ] {a : Unit → Color K} +lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a ()).kind, (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) @@ -595,7 +606,7 @@ lemma tensorBasis_repr_contr_unit [Fintype κ] {a : Unit → Color K} /-- The components of the contraction of two pairs in dual bases, `(tr T)^ρ = ∑ μ ν, T^ρμν_μν` with one label per pair. -/ -lemma tensorBasis_repr_contr_unit_sum [Fintype κ] {a : Unit ⊕ Unit → Color K} +lemma tensorBasis_repr_contr_two_pairs [Fintype κ] {a : Unit ⊕ Unit → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a (.inl ())).kind, ∑ ν : basisIdx (a (.inr ())).kind, diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 497737dc99..a8f312e5b2 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -441,8 +441,8 @@ macro_rules try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) -- `Function.update_of_ne` proves `j ≠ i` for literal slots by `ne_eq` and `Fin.reduceEq`. simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, - Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_unit, - Species.Tensor.tensorBasis_repr_contr_unit_sum, Species.tensorBasis_repr_reindex, + Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_one_pair, + Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are @@ -503,7 +503,7 @@ lemmas loop. -/ private def flattenContractions : TacticM Unit := do for _ in [0:10] do let before ← instantiateMVars (← getMainTarget) - evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr, + evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr_left, Tensors.Species.Tensor.tmulEquiv_contr_right, Tensors.Species.Tensor.contr_contr])) evalTactic (← `(tactic| try simp only [← Tensors.Species.tmulEquiv_assoc])) evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, @@ -594,7 +594,7 @@ elab "tensor_regroup" : tactic => do -- color variables of the goal although the bijections of slots do not depend on them. evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) - (of_decide_eq_true rfl) (of_decide_eq_true rfl) _)) + (of_decide_eq_true rfl) _)) return catch e => lastError := e.toMessageData throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ @@ -695,9 +695,9 @@ example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ #check {T | μ ν ⊗ T | μ ρ}ᵀ /-- Contracting `ν` and then `ρ` is contracting both at once, `(T U)^μ_ρ W^ρ_τ = T^μ_ν U^ν_ρ W^ρ_τ`, -by `tmulEquiv_contr` and `contr_contr`. -/ +by `tmulEquiv_contr_left` and `contr_contr`. -/ example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U | ν ρ ⊗ W | ρ τ}ᵀ := by - rw [Tensor.tmulEquiv_contr, Tensor.contr_contr] + rw [Tensor.tmulEquiv_contr_left, Tensor.contr_contr] congr 2 ext x fin_cases x <;> rfl diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 813bc12f8f..9fb00a8635 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -357,14 +357,8 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.formTensor a.dual)) := by - induction t using inductionOn_pure with - | hsmul r t ht => rw [map_smul, ht, ← smul_tmul', map_smul, map_smul] - | hadd t t' ht ht' => rw [map_add, ht, ht', add_tmul, map_add, map_add] - | h p => - let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) - rw [toDualAt_pure i a ha hi h p (Function.update q i _) (Function.update_self _ _ _) - (fun x hx => by simp [q, hx]), toDual_apply, formTensor, - Tensor.contr_matMulAt_pure i a a.dual ha hi h _ p q (fun x hx => by simp [q, hx])] + rw [formTensor, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] + rfl /-- Lowering the second index is contracting it with the form, `M^μν g_νρ = M^μ_ρ` at an upper color `c`, along `ColorEquiv.matMul`. It is `toDualAt_eq_contr` for a tensor on two slots, in the diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 674e75a3ab..efa0a31394 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -55,8 +55,8 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and `Species.delta_invariant`. - `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.snake` proves - that the delta is the identity, and `Species.toLin_map` that `toLin` commutes with the group - action, `(Λz)(Λv) = Λ(z v)`. + that the delta is the identity, `Species.toLin_map` that `toLin` commutes with the group + action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that `(id ⊗ f) z = f ∘ z`. - `Species.basis_repr_toLin` gives `toLin` in components, and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their matrices are. @@ -234,6 +234,13 @@ lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V (v : S.V a.dual) : S.toLin (x ⊗ₜ y) v = S.contr a.dual (v ⊗ₜ x) • y := rfl +/-- Applying a linear map `f` to the second factor of `z` composes the linear map of `z` with `f`, +`(id ⊗ f) z = f ∘ z`. -/ +lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] + [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) : + S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := + pairingToLinearMap_lTensor _ f z + /-- The snake law `u^μ δ_μ^ν = u^ν`, the delta read as a linear map is the identity. -/ lemma snake (c : Color K) : S.toLin (S.delta c) = LinearMap.id := Module.Basis.pairingToLinearMap_coordPairing_coevaluation _ _ diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 2ba24e7ed9..9fb9248437 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -59,16 +59,19 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. +- `Species.mapAt` applies a linear map at one slot of a tensor and the identity at the others, + with `Species.mapAt_pure`. - `Species.congrAt` applies a linear equivalence at one slot of a tensor and the identity at the others, with `Species.congrAt_pure`, `Species.congrAt_smul` and `Species.congrAt_symm`, and - `Species.congrAt_comm` proves that equivalences at two different slots commute. + `Species.congrAt_comm` proves that equivalences at two different slots commute, and + `Species.congrAt_eq_mapAt` that `congrAt` is `mapAt` of the underlying map. ## iii. Table of contents - A. Tensors - B. The group action - C. The basis of tensors -- D. A linear equivalence at one slot +- D. A linear map at one slot ## iv. References @@ -232,7 +235,7 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor /-! -## D. A linear equivalence at one slot +## D. A linear map at one slot -/ @@ -296,6 +299,40 @@ lemma congrAt_symm (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x | hsmul r t ht => rw [map_smul, map_smul, ← ht] | hadd x y hx hy => rw [map_add, map_add, ← hx, ← hy] +variable (S) in +/-- The linear map `f` at the slot `i`, and the identity, moved along `h x`, at every other slot +`x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. It is Mathlib's `PiTensorProduct.map` at one slot. -/ +noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := + (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map (fun x => if hx : x = i then + hx ▸ f else (S.cast (h x hx)).toLinearMap) ∘ₗ (S.toPiTensor c).toLinearMap + +/-- On a pure tensor, `mapAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The +result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ +lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) + (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hqi : q i = f (p i)) + (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : + S.mapAt i f h (S.pure p) = S.pure q := by + simp only [mapAt, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, toPiTensor_pure, + PiTensorProduct.map_tprod, toPiTensor_symm_tprod] + congr 1 + funext x + by_cases hx : x = i + · subst hx + simp [hqi] + · simp [hx, hq x hx] + +/-- `congrAt` is `mapAt` of the underlying linear map. -/ +lemma congrAt_eq_mapAt (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) + (t : S.Tensor c) : S.congrAt i f h t = S.mapAt i f.toLinearMap h t := by + induction t using inductionOn_pure with + | h p => + let q : Π x, S.V (c' x) := fun x => if hx : x = i then hx ▸ f (p i) else S.cast (h x hx) (p x) + rw [congrAt_pure i f h p q (by simp [q]) (fun x hx => by simp [q, hx]), + mapAt_pure i _ h p q (by simp [q]) (fun x hx => by simp [q, hx])] + | hsmul r t ht => rw [map_smul, map_smul, ht] + | hadd x y hx hy => rw [map_add, map_add, hx, hy] + /-- Linear equivalences at two different slots `i` and `j` commute, `f` and `f'` at `i`, `g` and `g'` at `j`, when they agree after moving vectors between the intermediate colors `c₁` and `c₂`. -/ lemma congrAt_comm {c₁ c₂ : ι → Color K} {i j : ι} (hij : i ≠ j) diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index be6caded9c..2c62f97064 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -329,7 +329,7 @@ of dimensions. -/ example (d : ℕ) : ((realLorentz d).tensorBasis ![]).repr {(realLorentz d).formTensor down | μ ν ⊗ (realLorentz d).formTensor up | μ ν}ᵀ (fun i => i.elim0) = d + 1 := by - rw [Species.Tensor.tensorBasis_repr_contr_unit_sum] + rw [Species.Tensor.tensorBasis_repr_contr_two_pairs] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor] simp [minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, Fintype.sum_sum_type, add_comm] @@ -440,7 +440,7 @@ component is `∑ μ, v^μ ω_μ`. -/ lemma tensorBasis_repr_contr_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : ((realLorentz d).tensorBasis ![]).repr {v | μ ⊗ ω | μ}ᵀ (fun i => i.elim0) = ∑ μ, v μ * ω μ := by - simp [Species.Tensor.tensorBasis_repr_contr_unit, Species.tensorBasis_repr_reindex, + simp [Species.Tensor.tensorBasis_repr_contr_one_pair, Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_toTensor_vector, tensorBasis_repr_toTensor_coVector] From d63d0a4a65f404f4401cf38738b3d391c90c3f52 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 12:05:37 +0000 Subject: [PATCH 014/242] =?UTF-8?q?refactor(Metric):=20mapSlots=20and=20ma?= =?UTF-8?q?pAt=20on=20Function.update;=20toDual=5FtoDual,=20toDualAt=5Feq?= =?UTF-8?q?=5FmapAt,=20toDualAt=5Feq=5Fcontr=20from=20contr=5FmatMulAt;=20?= =?UTF-8?q?formBilin=20with=20isSymm=5FformBilin,=20isAlt=5FformBilin=20an?= =?UTF-8?q?d=20formMatrix=5Feq=5FtoMatrix;=20schouten;=20formTensor=5Fsymm?= =?UTF-8?q?=20and=20formTensor=5Fantisymm=20as=20reindex=20=CF=83=20g=20?= =?UTF-8?q?=3D=20=C2=B1g;=20toDualAt=5Fcomm,=20congrAt=5Fcomm,=20toDualAt?= =?UTF-8?q?=5FtwoSlotEquiv=20and=20toDualAt=5Feq=5Fcontr=5FformTensor=20re?= =?UTF-8?q?moved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 288 +++++++++++++----------- Physlib/Mathematics/Tensors/Tensor.lean | 71 +++--- 2 files changed, 196 insertions(+), 163 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 9fb00a8635..4da20e501c 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -6,6 +6,8 @@ Authors: Andrea Pari module public import Physlib.Mathematics.Tensors.Contraction +public import Mathlib.Tactic.Module +public import Mathlib.LinearAlgebra.Matrix.BilinearForm /-! # Invariant forms, metrics, and lowering and raising indices @@ -30,8 +32,10 @@ a commutative ring `k` a non-degenerate form need not have an inverse. Symmetry is an optional property of the chosen form. The class `S.IsSymmetricForm κ` says that `g_μν = g_νμ`, as for `η`, and a metric is a symmetric form. The class `S.IsAntisymmetricForm κ` -says that `g_μν = -g_νμ`, as for the Weyl `ε`. A form on a kind that is a sum of two kinds may be -neither, and lowering and raising never ask for either property. +says that `g_μν = -g_νμ`, as for the Weyl `ε`, which with two labels gives the Schouten identity. +Both are stated on the tensor `g`, and `formBilin` carries them to Mathlib's bilinear forms. A form +on a kind that is a sum of two kinds may be neither, and lowering and raising never ask for either +property. `S.toDual c : V c ≃ V c.dual` contracts a vector with the first index of the form at `c.dual`. At an upper color it lowers the index, `v^μ ↦ v^μ g_μν`, and at a lower color it raises it, @@ -40,52 +44,40 @@ the first index gives the textbook `v_ν = η_νμ v^μ`. For the antisymmetric `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`. With `ε^12 = ε_21 = 1` this is minus the convention `ψ_α = ε_αβ ψ^β`, so a Weyl species following that convention stores `-ε`. -`S.toDualAt i a ha hi h` lowers or raises the index at the slot `i` of a tensor and keeps the -others, as in `T^μν ↦ T^μ_ν` for `i` the second slot. It is `congrAt` of `toDual`. The caller -names the color `a` of the slot, with `ha : c i = a`, so that the form is found at the kind of `a`. -Lowering and raising at that slot can then use the same instance without moving it, as in -`toDualAt_toDualAt`. The caller also states the colors `c'` of the result, such as -`![⟨vec, up⟩, ⟨vec, down⟩]`. The hypothesis `hi` says that `c'` has the dual color `a.dual` at `i`, -and `h` that it agrees with the colors `c` of the tensor elsewhere. +`S.toDualAt i …` lowers or raises the index at the slot `i` of a tensor and keeps the others, as +in `T^μν ↦ T^μ_ν` for `i` the second slot. Lowering at several slots composes slot by slot +(`toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), so a lowering and a raising at the same slot +cancel in any order. `S.formTensor c` is the form as a tensor on two slots, `η^μν` at `⟨vec, up⟩` and `η_μν` at -`⟨vec, down⟩`. Lowering an index is contracting it with the form tensor, `M^μν g_νρ = M^μ_ρ`. +`⟨vec, down⟩`. Lowering an index is contracting it with the form tensor, `T^…_ν… = T^…μ… g_μν`. ## ii. Key results -- `Species.HasForm` is an invariant form on a kind with its inverse, `g_μν` and `g^μν`, and - `Species.form` is the form at a color of that kind, with `Species.form_invariant` and - `Species.form_inverse`. -- `Species.formMatrix` gives the components `g_μν` of the form, on which each species states its - component lemma. +- `Species.HasForm` is an invariant form on a kind with its inverse, `g_μν` and `g^μν`, with + `Species.form`, `Species.form_invariant` and `Species.form_inverse`. - `Species.IsSymmetricForm` and `Species.IsAntisymmetricForm` say that the form is symmetric or - antisymmetric, with `Species.form_symm` and `Species.form_antisymm`. -- `Species.toDual` is `S.V c ≃ S.V c.dual`, lowering an upper index and raising a lower one. -- `Species.toDual_symm` proves that raising is the inverse of lowering. -- `Species.toDual_rep` and `Species.toDualAt_smul` prove `(Λv)_ν = Λ(v_ν)`, for a vector and at - one index of a tensor. -- `Species.toDualAt` lowers or raises the index at one slot of a tensor. -- `Species.toDualAt_toDualAt` proves that raising at a slot undoes lowering there, and - `Species.toDualAt_comm` that lowering at two different slots commute. -- `Species.tensorBasis_repr_toDualAt` gives lowering at a slot in components, + antisymmetric, `Species.form_symm` and `Species.form_antisymm`. +- `Species.formBilin` reads the form as a Mathlib bilinear form, symmetric by + `Species.isSymm_formBilin` and alternating by `Species.isAlt_formBilin`. +- `Species.toDual` lowers an upper index and raises a lower one, and `Species.toDual_toDual` that + raising undoes lowering. +- `Species.formTensor` is the form as a tensor on two slots. +- `Species.toDualAt` lowers or raises the index at one slot of a tensor, with + `Species.toDualAt_toDualAt`, `Species.toDualAt_eq_mapAt` and, in components, + `Species.tensorBasis_repr_toDualAt`. +- `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, `T^…_ν… = T^…μ… g_μν`. -- `Species.toDualAt_twoSlotEquiv` proves that lowering the second index of a tensor on two slots, - `M^μν ↦ M^μ_ν`, is `toDual` on the second factor. -- `Species.toDualAt_eq_contr` proves that lowering at any slot is contracting with the form, - `T^…_ν… = T^…μ… g_μν`, and `Species.toDualAt_eq_contr_formTensor` gives the same at the second - slot of a tensor on two slots, `M^μν g_νρ = M^μ_ρ`. -- `Species.formTensor` is the form as a tensor on two slots, and `Species.formTensor_invariant` - proves that it is invariant. -- `Species.tensorBasis_repr_formTensor` gives the components of the form tensor. -- `Species.formTensor_symm` and `Species.formTensor_antisymm` prove `g^μν = g^νμ` and - `g^μν = -g^νμ` as tensors for a symmetric or antisymmetric form. +- `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an + antisymmetric form on a kind with two labels. ## iii. Table of contents - A. Invariant forms and metrics - B. Lowering and raising vectors -- C. Lowering and raising at one index -- D. The form as a tensor +- C. The form as a tensor +- D. Lowering and raising at one index +- E. Forms on a kind with two labels ## iv. References @@ -114,8 +106,7 @@ variable (S : Species k G K basisIdx) in /-- A chosen invariant invertible bilinear form on the kind `κ`. At both colors of `κ` it gives an invariant element `g` of `V c ⊗ V c`, `g^μν` at the upper color and `g_μν` at the lower one, with the two forms inverse to each other. It need not be symmetric, so it covers both `η` and the Weyl -`ε`. The invariance at the lower color follows from that at the upper one and `formAt_inverse`, -but a field for both variances cannot state only half. -/ +`ε`. -/ class HasForm (κ : K) where /-- The form at the color `⟨κ, v⟩`, an element of `V ⟨κ, v⟩ ⊗ V ⟨κ, v⟩`. -/ formAt : (v : Variance) → S.V ⟨κ, v⟩ ⊗[k] S.V ⟨κ, v⟩ @@ -132,14 +123,6 @@ variable {S : Species k G K basisIdx} noncomputable def form (c : Color K) [S.HasForm c.kind] : S.V c ⊗[k] S.V c := HasForm.formAt c.variance -variable (S) in -/-- The components of the form at `c`, `g^μν` at an upper color and `g_μν` at a lower one, its -coordinates in Mathlib's product basis. Component lemmas of a species are stated on it, so that -`simp` finds them at a literal color. -/ -noncomputable def formMatrix (c : Color K) [S.HasForm c.kind] : - Matrix (basisIdx c.kind) (basisIdx c.kind) k := - fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr (form c) (μ, ν) - /-- The form is invariant, `Λ g = g` for every `Λ : G`. -/ lemma form_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : TensorProduct.map (S.rep c Λ) (S.rep c Λ) (form c) = form c := @@ -151,6 +134,36 @@ lemma form_inverse (c : Color K) [S.HasForm c.kind] : S.toLin (form c) ∘ₗ S.toLin (form c.dual) = LinearMap.id := HasForm.formAt_inverse c.variance +variable (S) in +/-- The form at `c` read as a bilinear form on `V c.dual`, `(u, v) ↦ u_μ g^μν v_ν`, with the first +argument contracted with the first index of the form. -/ +noncomputable def formBilin (c : Color K) [S.HasForm c.kind] : + LinearMap.BilinForm k (S.V c.dual) := + (TensorProduct.curry (S.contr c.dual).toLinearMap).flip ∘ₗ S.toLin (form c) + +lemma formBilin_apply (c : Color K) [S.HasForm c.kind] (u v : S.V c.dual) : + S.formBilin c u v = S.contr c.dual (v ⊗ₜ S.toLin (form c) u) := + rfl + +variable (S) in +/-- The components of the form at `c`, `g^μν` at an upper color and `g_μν` at a lower one, its +coordinates in Mathlib's product basis. Component lemmas of a species are stated on it, so that +`simp` finds them at a literal color. -/ +noncomputable def formMatrix (c : Color K) [S.HasForm c.kind] : + Matrix (basisIdx c.kind) (basisIdx c.kind) k := + fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr (form c) (μ, ν) + +/-- The components of the form are Mathlib's matrix of `formBilin c` in the basis of `c.dual`, +`g^μν = g(e^μ, e^ν)`. -/ +lemma formMatrix_eq_toMatrix (c : Color K) [S.HasForm c.kind] : + S.formMatrix c = LinearMap.BilinForm.toMatrix (S.basis c.dual) (S.formBilin c) := by + ext μ ν + rw [LinearMap.BilinForm.toMatrix_apply, formBilin_apply, contr_tmul_eq_sum_basis] + simp only [Module.Basis.repr_self, Finsupp.single_apply, ite_mul, one_mul, zero_mul, + Finset.sum_ite_eq, Finset.mem_univ, ite_true] + refine ((S.basis_repr_toLin c (d := c) (form c) _ ν).trans ?_).symm + simp [Finsupp.single_apply, formMatrix] + variable (S) in /-- The form on the kind `κ` is symmetric, `g_μν = g_νμ`, as `η`. A metric is a symmetric form. -/ class IsSymmetricForm (κ : K) [S.HasForm κ] : Prop where @@ -158,22 +171,46 @@ class IsSymmetricForm (κ : K) [S.HasForm κ] : Prop where comm_form : ∀ v, TensorProduct.comm k _ _ (form (S := S) ⟨κ, v⟩) = form ⟨κ, v⟩ variable (S) in -/-- The form on the kind `κ` is antisymmetric, `g_μν = -g_νμ`, as the Weyl `ε`. Antisymmetric is -not alternating: when `2 = 0` in `k` the form `[1]` on `k` is antisymmetric with `g(1, 1) = 1`. -/ +/-- The form on the kind `κ` is antisymmetric, `g_μν = -g_νμ`, as the Weyl `ε`. -/ class IsAntisymmetricForm (κ : K) [S.HasForm κ] : Prop where /-- Swapping the two indices of the form gives minus the form. -/ comm_form : ∀ v, TensorProduct.comm k _ _ (form (S := S) ⟨κ, v⟩) = -form ⟨κ, v⟩ +/-- Contracting with the swapped tensor swaps the two arguments, `u_μ z^νμ v_ν = v_ν z^νμ u_μ`. -/ +lemma contr_toLin_comm (c : Color K) (z : S.V c ⊗[k] S.V c) (u v : S.V c.dual) : + S.contr c.dual (v ⊗ₜ S.toLin (TensorProduct.comm k _ _ z) u) = + S.contr c.dual (u ⊗ₜ S.toLin z v) := by + induction z using TensorProduct.inductionOn with + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] + | tmul x y => + simp only [TensorProduct.comm_tmul, toLin_tmul, tmul_smul, map_smul, smul_eq_mul] + exact mul_comm _ _ + /-- A symmetric form, `g_μν = g_νμ`. -/ lemma form_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : TensorProduct.comm k (S.V c) (S.V c) (form c) = form c := IsSymmetricForm.comm_form c.variance +/-- A symmetric form is symmetric as a bilinear form, Mathlib's `LinearMap.BilinForm.IsSymm`. -/ +lemma isSymm_formBilin (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : + (S.formBilin c).IsSymm := + ⟨fun u v => by rw [formBilin_apply, formBilin_apply, ← contr_toLin_comm, form_symm]⟩ + /-- An antisymmetric form, `g_μν = -g_νμ`. -/ lemma form_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : TensorProduct.comm k (S.V c) (S.V c) (form c) = -form c := IsAntisymmetricForm.comm_form c.variance +/-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's +`LinearMap.IsAlt`. It needs `2 x = 0 → x = 0` in `k`: when `2 = 0` the form `[1]` on `k` is +antisymmetric with `g(1, 1) = 1`. -/ +lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + [NoZeroDivisors k] [CharZero k] : LinearMap.IsAlt (S.formBilin c) := by + refine LinearMap.isAlt_iff_eq_neg_flip.mpr (LinearMap.ext₂ fun u v => ?_) + rw [LinearMap.neg_apply, LinearMap.neg_apply, LinearMap.flip_apply, formBilin_apply, + formBilin_apply, ← contr_toLin_comm, form_antisymm, map_neg, LinearMap.neg_apply, tmul_neg, + map_neg] + /-! ## B. Lowering and raising vectors @@ -194,6 +231,12 @@ lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : /-- Raising is the inverse of lowering. The inverse of `S.toDual c` is `S.toDual c.dual`. -/ lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDual c.dual := rfl +/-- Raising undoes lowering, `(v_ν)^ν = v^ν`. -/ +@[simp] +lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : + S.toDual c.dual (S.toDual c v) = v := + (S.toDual c).symm_apply_apply v + /-- Lowering and raising commute with the group action, `(Λv)_ν = Λ(v_ν)`. -/ lemma toDual_rep (c : Color K) [S.HasForm c.kind] (g : G) (v : S.V c) : S.toDual c (S.rep c g v) = S.rep c.dual g (S.toDual c v) := by @@ -208,7 +251,45 @@ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basis /-! -## C. Lowering and raising at one index +## C. The form as a tensor + +-/ + +variable (S) in +/-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a +lower one. -/ +noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] := + S.twoSlotEquiv c c (form c) + +/-- The form is an invariant tensor. Every group element fixes it, as every Lorentz +transformation fixes the Minkowski metric `η`. -/ +lemma formTensor_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : + Λ • S.formTensor c = S.formTensor c := by + rw [formTensor, ← twoSlotEquiv_map_rep, form_invariant] + +/-- The components of the form tensor are those of the form, `g_μν` at the pair of labels +`(μ 0, μ 1)`. -/ +lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] + (μ : Π j, basisIdx (![c, c] j).kind) : + (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := + tensorBasis_repr_twoSlotEquiv _ _ _ μ + +/-- A symmetric form is symmetric under the slot swap `σ` as a tensor, `g^μν = g^νμ`. -/ +lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] + {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : + S.reindex σ (S.formTensor c) = S.formTensor c := by + rw [formTensor, reindex_twoSlotEquiv hσ, form_symm] + +/-- An antisymmetric form is antisymmetric under the slot swap `σ` as a tensor, +`g^μν = -g^νμ`. -/ +lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : + S.reindex σ (S.formTensor c) = -S.formTensor c := by + rw [formTensor, reindex_twoSlotEquiv hσ, form_antisymm, map_neg] + +/-! + +## D. Lowering and raising at one index -/ @@ -249,36 +330,13 @@ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm).symm from rfl, ← congrAt_symm] exact (S.congrAt i _ h).symm_apply_apply t -/-- Lowering or raising at two different slots commute, `(T_μ)_ν = (T_ν)_μ`. The colors `c₁` and -`c₂` are those after the first step in either order. On literal slots the hypotheses `h₂` and -`h₂'` are found automatically, as the index notation finds them. -/ -lemma toDualAt_comm {c c₁ c₂ c' : ι → Color K} {i j : ι} (hij : i ≠ j) (a b : Color K) - [S.HasForm a.kind] [S.HasForm b.kind] - {ha : c i = a} {hi : c₁ i = a.dual} {h : ∀ x, x ≠ i → c x = c₁ x} - {hb : c₁ j = b} {hj : c' j = b.dual} {h' : ∀ x, x ≠ j → c₁ x = c' x} - (hb₂ : c j = b) (hj₂ : c₂ j = b.dual) (ha₂ : c₂ i = a) (hi₂ : c' i = a.dual) - (h₂ : ∀ x, x ≠ j → c x = c₂ x := by - intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) - (h₂': ∀ x, x ≠ i → c₂ x = c' x := by - intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) (t : S.Tensor c) : - S.toDualAt j b hb hj h' (S.toDualAt i a ha hi h t) = - S.toDualAt i a ha₂ hi₂ h₂' (S.toDualAt j b hb₂ hj₂ h₂ t) := - congrAt_comm hij _ _ _ _ _ _ _ _ - (fun v => by simp only [LinearEquiv.trans_apply, LinearEquiv.cast_apply, cast_cast]) - (fun v => by simp only [LinearEquiv.trans_apply, LinearEquiv.cast_apply, cast_cast]) t - -/-- Lowering or raising the second index of a tensor on two slots, `M^μν ↦ M^μ_ν`, is `toDual` on -the second factor of `M ∈ V a ⊗ V c`. -/ -lemma toDualAt_twoSlotEquiv (a c : Color K) [S.HasForm c.kind] (M : S.V a ⊗[k] S.V c) : - S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 c rfl rfl - (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = - S.twoSlotEquiv a c.dual ((S.toDual c).toLinearMap.lTensor _ M) := by - induction M using TensorProduct.inductionOn with - | add M M' hM hM' => simp only [map_add, hM, hM'] - | tmul x y => - rw [twoSlotEquiv_tmul, LinearMap.lTensor_tmul, twoSlotEquiv_tmul] - exact toDualAt_pure _ _ _ _ _ _ _ rfl - (fun x hx => by fin_cases x <;> first | rfl | exact absurd rfl hx) +/-- Lowering or raising at a slot is `toDual` at that slot, read as a linear map. Chains of +lowerings and raisings at several slots compose slot by slot through `mapSlots_mapSlots`. -/ +lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + S.toDualAt i a ha hi h t = + S.mapAt i (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm).toLinearMap h t := + congrAt_eq_mapAt i _ h t /-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = T^…μ… g_μν`. The labels `φ` of the result are cast to labels of `t`, and the label at `i` is replaced by the summed @@ -313,44 +371,6 @@ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a have hx := Finset.ne_of_mem_erase hx rw [dite_eq_right hx, basis_repr_cast, Function.update_of_ne hx] -/-! - -## D. The form as a tensor - --/ - -variable (S) in -/-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a -lower one. -/ -noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] := - S.twoSlotEquiv c c (form c) - -/-- The form is an invariant tensor. Every group element fixes it, as every Lorentz -transformation fixes the Minkowski metric `η`. -/ -lemma formTensor_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : - Λ • S.formTensor c = S.formTensor c := by - rw [formTensor, ← twoSlotEquiv_map_rep, form_invariant] - -/-- The components of the form tensor are those of the form, `g_μν` at the pair of labels -`(μ 0, μ 1)`. -/ -lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] - (μ : Π j, basisIdx (![c, c] j).kind) : - (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := - tensorBasis_repr_twoSlotEquiv _ _ _ μ - -/-- A symmetric form is symmetric under the slot swap `σ` as a tensor, `g^μν = g^νμ`. -/ -lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] - {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : - S.formTensor c = S.reindex σ (S.formTensor c) := by - rw [formTensor, reindex_twoSlotEquiv hσ, form_symm] - -/-- An antisymmetric form is antisymmetric under the slot swap `σ` as a tensor, -`g^μν = -g^νμ`. -/ -lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : - S.formTensor c = S.reindex σ (-S.formTensor c) := by - rw [formTensor, ← map_neg, reindex_twoSlotEquiv hσ, map_neg, form_antisymm, neg_neg] - /-- Lowering or raising at the slot `i` is contracting that slot with the first index of the form, `T^…_ν… = T^…μ… g_μν`. -/ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) @@ -360,16 +380,30 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i rw [formTensor, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] rfl -/-- Lowering the second index is contracting it with the form, `M^μν g_νρ = M^μ_ρ` at an upper -color `c`, along `ColorEquiv.matMul`. It is `toDualAt_eq_contr` for a tensor on two slots, in the -form of a product of matrices. -/ -lemma toDualAt_eq_contr_formTensor (a c : Color K) [S.HasForm c.kind] (M : S.V a ⊗[k] S.V c) : - S.toDualAt (c := ![a, c]) (c' := ![a, c.dual]) 1 c rfl rfl - (fun x hx => by fin_cases x <;> simp_all) (S.twoSlotEquiv a c M) = - Tensor.contr (ColorEquiv.matMul a c c.dual) - (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.formTensor c.dual)) := by - rw [toDualAt_twoSlotEquiv, formTensor, Tensor.contr_matMul] - rfl +/-! + +## E. Forms on a kind with two labels + +-/ + +/-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`: +`g(u, v) w + g(v, w) u + g(w, u) v = 0`. In two dimensions every alternating form of three vectors +vanishes. -/ +lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] + [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) (u v w : S.V c.dual) : + S.formBilin c u v • w + S.formBilin c v w • u + S.formBilin c w u • v = 0 := by + obtain ⟨i, j, hij, huniv⟩ := Finset.card_eq_two.mp (h2 : Finset.univ.card = 2) + -- Each vector is `x^i e_i + x^j e_j`, and the form vanishes on `e_i, e_i` and `e_j, e_j`. + have hexp (x : S.V c.dual) : x = (S.basis c.dual).repr x i • S.basis c.dual i + + (S.basis c.dual).repr x j • S.basis c.dual j := by + conv_lhs => rw [← (S.basis c.dual).sum_repr x] + rw [huniv, Finset.sum_pair hij] + have H := S.isAlt_formBilin c + have hji := (LinearMap.IsAlt.neg H (S.basis c.dual i) (S.basis c.dual j)).symm + rw [hexp u, hexp v, hexp w] + simp only [map_add, map_smul, LinearMap.add_apply, LinearMap.smul_apply, smul_eq_mul, + LinearMap.IsAlt.self_eq_zero H, hji] + module end Species diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 9fb9248437..190a258b47 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -59,12 +59,12 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. -- `Species.mapAt` applies a linear map at one slot of a tensor and the identity at the others, - with `Species.mapAt_pure`. +- `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_mapSlots` and + `Species.mapSlots_id`, and `Species.mapAt` applies one at a single slot and the identity at the + others. - `Species.congrAt` applies a linear equivalence at one slot of a tensor and the identity at the others, with `Species.congrAt_pure`, `Species.congrAt_smul` and `Species.congrAt_symm`, and - `Species.congrAt_comm` proves that equivalences at two different slots commute, and - `Species.congrAt_eq_mapAt` that `congrAt` is `mapAt` of the underlying map. + `Species.congrAt_eq_mapAt` proves that `congrAt` is `mapAt` of the underlying map. ## iii. Table of contents @@ -299,13 +299,40 @@ lemma congrAt_symm (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x | hsmul r t ht => rw [map_smul, map_smul, ← ht] | hadd x y hx hy => rw [map_add, map_add, ← hx, ← hy] +variable (S) in +/-- A linear map `f x` at every slot `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's +`PiTensorProduct.map`. -/ +noncomputable def mapSlots (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := + (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap + +omit [DecidableEq ι] in +/-- On a pure tensor, `mapSlots` applies `f x` to the vector at each slot `x`. -/ +lemma mapSlots_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : + S.mapSlots f (S.pure p) = S.pure fun x => f x (p x) := by + simp only [mapSlots, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, + toPiTensor_pure, PiTensorProduct.map_tprod, toPiTensor_symm_tprod] + +omit [DecidableEq ι] in +/-- Two slotwise maps in sequence are the slotwise map of the composites. -/ +lemma mapSlots_mapSlots {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) + (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : S.Tensor c) : + S.mapSlots g (S.mapSlots f t) = S.mapSlots (fun x => g x ∘ₗ f x) t := by + simp only [mapSlots, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, + LinearEquiv.apply_symm_apply, ← LinearMap.comp_apply (PiTensorProduct.map g), + ← PiTensorProduct.map_comp] + +omit [DecidableEq ι] in +/-- The slotwise map of the identities is the identity. -/ +lemma mapSlots_id (t : S.Tensor c) : S.mapSlots (fun _ => LinearMap.id) t = t := by + simp [mapSlots, PiTensorProduct.map_id] + variable (S) in /-- The linear map `f` at the slot `i`, and the identity, moved along `h x`, at every other slot -`x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. It is Mathlib's `PiTensorProduct.map` at one slot. -/ +`x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := - (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map (fun x => if hx : x = i then - hx ▸ f else (S.cast (h x hx)).toLinearMap) ∘ₗ (S.toPiTensor c).toLinearMap + S.mapSlots + (Function.update (fun x => if hx : x = i then 0 else (S.cast (h x hx)).toLinearMap) i f) /-- On a pure tensor, `mapAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ @@ -313,8 +340,7 @@ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hqi : q i = f (p i)) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : S.mapAt i f h (S.pure p) = S.pure q := by - simp only [mapAt, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, toPiTensor_pure, - PiTensorProduct.map_tprod, toPiTensor_symm_tprod] + rw [mapAt, mapSlots_pure] congr 1 funext x by_cases hx : x = i @@ -333,33 +359,6 @@ lemma congrAt_eq_mapAt (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, | hsmul r t ht => rw [map_smul, map_smul, ht] | hadd x y hx hy => rw [map_add, map_add, hx, hy] -/-- Linear equivalences at two different slots `i` and `j` commute, `f` and `f'` at `i`, `g` and -`g'` at `j`, when they agree after moving vectors between the intermediate colors `c₁` and `c₂`. -/ -lemma congrAt_comm {c₁ c₂ : ι → Color K} {i j : ι} (hij : i ≠ j) - (f : S.V (c i) ≃ₗ[k] S.V (c₁ i)) (h₁ : ∀ x, x ≠ i → c x = c₁ x) - (g : S.V (c₁ j) ≃ₗ[k] S.V (c' j)) (h₁' : ∀ x, x ≠ j → c₁ x = c' x) - (g' : S.V (c j) ≃ₗ[k] S.V (c₂ j)) (h₂ : ∀ x, x ≠ j → c x = c₂ x) - (f' : S.V (c₂ i) ≃ₗ[k] S.V (c' i)) (h₂' : ∀ x, x ≠ i → c₂ x = c' x) - (hf : ∀ v, f' (S.cast (h₂ i hij) v) = S.cast (h₁' i hij) (f v)) - (hg : ∀ v, g (S.cast (h₁ j hij.symm) v) = S.cast (h₂' j hij.symm) (g' v)) - (t : S.Tensor c) : - S.congrAt j g h₁' (S.congrAt i f h₁ t) = S.congrAt i f' h₂' (S.congrAt j g' h₂ t) := by - induction t using inductionOn_pure with - | h p => - simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, - toPiTensor_symm_tprod] - congr 1 - funext x - by_cases hxi : x = i - · subst hxi - simpa [hij] using (hf (p _)).symm - · by_cases hxj : x = j - · subst hxj - simpa [hxi] using hg (p _) - · simp [hxi, hxj] - | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul, map_smul] - | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] - end end Species From 09a0792886f8384c930f77cc04e823c614683324 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 12:17:43 +0000 Subject: [PATCH 015/242] feat(Elab): tensor_regroup over sums, negatives and scalars; tensor_slots for chains of lowerings and raisings Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 165 ++++++++++++++++++++++++-- 1 file changed, 152 insertions(+), 13 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index a8f312e5b2..7597f4b8a1 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -63,8 +63,9 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. - `tensor_components` : reduces an equation of tensors to an equation of their components. -- `tensor_regroup` : proves an equation of two contractions of the same factors, grouped or - ordered differently. +- `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped + or ordered differently, with the same signs and scalars. +- `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -75,6 +76,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.3. The elaborator - B.4. Components - B.5. Regrouping contractions + - B.6. Chains of lowerings and raisings - C. Examples ## iv. References @@ -553,18 +555,12 @@ private def arrange (target : List Nat) (equal : Nat → Nat → Bool) : TacticM swapFactors q n m (equal labels[m]! labels[m+1]!) labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! -/-- `tensor_regroup` proves an equation `Tensor.contr σ P = Tensor.contr τ Q` of two contractions -of the same factors, grouped or ordered differently, such as -`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. It writes each -side as one contraction of a product, orders the factors of the right side as on the left, and -checks by evaluation that both sides keep the same slots and contract the same pairs of slots +/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors. It writes +each side as one contraction of a product, orders the factors of the right side as on the left, +and checks by evaluation that both sides keep the same slots and contract the same pairs of slots (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of -matching the equal factors is tried in turn, so the number of attempts is the product of `k!` -over the factors that appear `k` times. - -Both sides must be contractions once relabelings are absorbed. A scalar multiple or a side with no -contraction is rejected, and is first brought to that form by hand. -/ -elab "tensor_regroup" : tactic => do +matching the equal factors is tried in turn. -/ +private def regroupContractions : TacticM Unit := do flattenContractions let (p, q) ← contractedProducts unless isLeftNested p && isLeftNested q do @@ -600,6 +596,121 @@ elab "tensor_regroup" : tactic => do throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ of slots. The last attempt failed with\n{lastError}" + +/-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, +contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. +The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a +factor. -/ +private def normalizeSums : TacticM Unit := do + let some (ty, _, _) := (← instantiateMVars (← getMainTarget)).eq? + | throwError "tensor_regroup: the goal is not an equation" + let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! + evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, + TensorProduct.tmul_add, TensorProduct.add_tmul, TensorProduct.tmul_neg, + TensorProduct.neg_tmul, TensorProduct.tmul_sub, TensorProduct.sub_tmul, + TensorProduct.tmul_smul (R' := $k), ← TensorProduct.smul_tmul' (R' := $k), smul_add, + smul_neg, smul_smul, neg_neg, neg_add, sub_eq_add_neg])) + +/-- The summands of a sum, from left to right. -/ +private partial def summands (e : Expr) : List Expr := + let e := e.consumeMData + if e.isAppOfArity ``HAdd.hAdd 6 then summands e.appFn!.appArg! ++ summands e.appArg! else [e] + +/-- Proves an equation of two summands with the same sign and the same scalar, by regrouping the +contractions they multiply. -/ +private partial def regroupSummand : TacticM Unit := do + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + | throwError "tensor_regroup: the goal is not an equation" + let l := l.consumeMData + let r := r.consumeMData + if l.isAppOfArity ``Neg.neg 3 && r.isAppOfArity ``Neg.neg 3 then + evalTactic (← `(tactic| refine congrArg Neg.neg ?_)) + regroupSummand + else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then + let a := l.appFn!.appArg! + unless ← withNewMCtxDepth (isDefEq a r.appFn!.appArg!) do + throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" + evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) + regroupSummand + else + try evalTactic (← `(tactic| with_reducible rfl)) catch _ => regroupContractions + +/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of +the same factors, grouped or ordered differently, such as +`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, +negatives and scalars are first moved out of every product, contraction and relabeling. Each +summand on the left is then matched with a summand on the right with the same sign and scalar, +whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a +factor appears more than once, each way of matching the equal factors is tried in turn, so the +number of attempts is the product of `k!` over the factors that appear `k` times. + +The scalars on the two sides must agree up to definitional equality, as `2` and `2`, and every +summand that is not the same term on both sides must be a contraction once relabelings are +absorbed. -/ +elab "tensor_regroup" : tactic => do + normalizeSums + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + | throwError "tensor_regroup: the goal is not an equation" + let ls := summands l + let rs := summands r + unless ls.length = rs.length do + throwError "tensor_regroup: the two sides have different numbers of summands" + if ls.length = 1 then + regroupSummand + return + -- Each summand on the left is proved equal to an unused summand on the right in its own goal, + -- and the sums then agree up to order. + let main ← getMainGoal + let mut used : List Nat := [] + let mut proofs : Array Term := #[] + for a in ls do + let mut found := false + for j in List.range rs.length do + if used.contains j then continue + let s ← saveState + let g ← mkFreshExprMVar (← mkEq a rs[j]!) + try + setGoals [g.mvarId!] + regroupSummand + unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" + used := j :: used + proofs := proofs.push (← exprToSyntax (← instantiateMVars g)) + found := true + break + catch _ => s.restore + unless found do + throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" + setGoals [main] + -- `conv` closes the goal by `rfl` once the summands are rewritten in order. + for h in proofs do + unless (← getGoals).isEmpty do evalTactic (← `(tactic| conv_lhs => rw [$h:term])) + unless (← getGoals).isEmpty do evalTactic (← `(tactic| abel)) + +/-! + +### B.6. Chains of lowerings and raisings + +-/ + +/-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, +such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain +is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps +are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side +with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be +`Fin n` with a literal `n`. -/ +macro "tensor_slots" : tactic => `(tactic| focus + simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, + Tensors.Species.mapSlots_mapSlots] + first + | refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext fun x => ?_) + | refine (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext fun x => ?_)).trans + (Tensors.Species.mapSlots_id _) + fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> + simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, + LinearMap.id_apply, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, + not_false_eq_true, dite_false, LinearEquiv.coe_coe, LinearEquiv.trans_apply, + LinearEquiv.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual]) + end Tensors.Notation end @@ -719,6 +830,22 @@ example : {T | μ ν ⊗ T | ν ρ = T | ν ρ ⊗ T | μ ν}ᵀ := by example : {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ = U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ ⊗ T | μ ν}ᵀ := by tensor_regroup +/-- `tensor_regroup` moves scalars out of the products, `(r T^μ_ν) U^ν_ρ = r (U^ν_ρ T^μ_ν)`. -/ +example : {(r • T) | μ ν ⊗ U | ν ρ = r • {U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- Negatives, `(-T^μ_ν) U^ν_ρ = -(U^ν_ρ T^μ_ν)`. -/ +example : {-T | μ ν ⊗ U | ν ρ = -{U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- Sums, matched summand by summand in any order, `T^μ_ν U^ν_ρ - W^μ_ρ = -W^μ_ρ + U^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ U | ν ρ + -W | μ ρ = -W | μ ρ + U | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- A sum inside a product is distributed, `T^μ_ν (U + W)^ν_ρ = U^ν_ρ T^μ_ν + W^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ (U | ν ρ + W | ν ρ) = U | ν ρ ⊗ T | μ ν + W | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + section Metric variable [S.HasForm κ] [S.HasForm c.kind] @@ -726,6 +853,18 @@ variable [S.HasForm κ] [S.HasForm c.kind] /-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ +/-- The same by `tensor_slots`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by + tensor_slots + +/-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by + tensor_slots + +/-- Lowerings at two slots commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by + tensor_slots + -- An arrow that does not match the variance of the slot is rejected. /-- error: index notation: ↑μ needs a visibly lower index -/ #guard_msgs in From f663c7ab4a7e32da3b10a2673c694ca51684d335 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 12:30:13 +0000 Subject: [PATCH 016/242] refactor(Tensors): equivariant maps as Representation.Equiv and IntertwiningMap; tensorRep, equivOfPure, intertwiningMapOfPure, congrSlots, equiv_smul, intertwiningMap_smul, cast_apply; ForMathlib Representation.Equiv.tensorCongr and IntertwiningMap.map_invariant; toDual_rep, congrAt_smul, toDualAt_smul, reindex_smul, oneSlotEquiv_rep, twoSlotEquiv_map_rep and contr_smul removed Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../ForMathlib/Representation.lean | 79 ++++++++++++ Physlib/Mathematics/Tensors/Contraction.lean | 45 ++++--- Physlib/Mathematics/Tensors/Elab.lean | 17 +-- Physlib/Mathematics/Tensors/Metric.lean | 37 ++---- Physlib/Mathematics/Tensors/Product.lean | 70 ++++------ Physlib/Mathematics/Tensors/Reindexing.lean | 32 ++--- Physlib/Mathematics/Tensors/Species.lean | 18 ++- Physlib/Mathematics/Tensors/Tensor.lean | 120 +++++++++++++----- Physlib/Mathematics/Tensors/Tensorial.lean | 7 +- .../Tensors/RealTensor/Species.lean | 2 +- 11 files changed, 268 insertions(+), 160 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/Representation.lean diff --git a/Physlib.lean b/Physlib.lean index 2b92ab428f..8e47ee771b 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -172,6 +172,7 @@ public import Physlib.Mathematics.ForMathlib.List.InsertionSort public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Basic public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Unitary public import Physlib.Mathematics.ForMathlib.OrthogonalMatrix +public import Physlib.Mathematics.ForMathlib.Representation public import Physlib.Mathematics.ForMathlib.SchurTriangulation public import Physlib.Mathematics.ForMathlib.SemilinearMaps public import Physlib.Mathematics.ForMathlib.SesquilinearForm diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean new file mode 100644 index 0000000000..ed9269b66a --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -0,0 +1,79 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.RepresentationTheory.Intertwining + +/-! + +# Equivalences of representations and invariant vectors + +The tensor product of two equivalences of representations, and invariant vectors under an +intertwining map. + +## i. Overview + +An intertwining map `φ` from a representation `ρ` to a representation `σ` satisfies +`φ (ρ g v) = σ g (φ v)`. If `v` is invariant, `ρ g v = v`, then so is `φ v`. Invariance of a tensor +built from an invariant vector by intertwining maps is therefore a corollary, as the invariance of +the Minkowski metric as a tensor follows from the invariance of the bilinear form. + +Mathlib has the tensor product of two intertwining maps, `IntertwiningMap.tensor`. Here it is +given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the linear equivalence +`TensorProduct.congr`. + +## ii. Key results + +- `Representation.IntertwiningMap.map_invariant` : an intertwining map sends invariant vectors to + invariant vectors. +- `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. + +## iii. Table of contents + +- A. Invariant vectors +- B. Tensor products of equivalences + +## iv. References + +-/ + +@[expose] public section + +open TensorProduct + +variable {k G V W U X : Type} [CommRing k] [Monoid G] [AddCommMonoid V] [Module k V] + [AddCommMonoid W] [Module k W] [AddCommMonoid U] [Module k U] [AddCommMonoid X] [Module k X] + {ρ : Representation k G V} {σ : Representation k G W} {τ : Representation k G U} + {π : Representation k G X} + +/-! + +## A. Invariant vectors + +-/ + +/-- An intertwining map sends an invariant vector to an invariant vector, +`ρ g v = v → σ g (φ v) = φ v`. -/ +lemma Representation.IntertwiningMap.map_invariant (φ : ρ.IntertwiningMap σ) {v : V} {g : G} + (hv : ρ g v = v) : σ g (φ v) = φ v := by + rw [← Representation.IntertwiningMap.isIntertwining, hv] + +/-! + +## B. Tensor products of equivalences + +-/ + +/-- The tensor product of two equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, Mathlib's +`TensorProduct.congr` with the actions `ρ ⊗ τ` and `σ ⊗ π`. -/ +noncomputable def Representation.Equiv.tensorCongr (φ : ρ.Equiv σ) (ψ : τ.Equiv π) : + (ρ.tprod τ).Equiv (σ.tprod π) := + .mk (TensorProduct.congr φ.toLinearEquiv ψ.toLinearEquiv) + (φ.toIntertwiningMap.tensor ψ.toIntertwiningMap).isIntertwining' + +@[simp] +lemma Representation.Equiv.tensorCongr_tmul (φ : ρ.Equiv σ) (ψ : τ.Equiv π) (v : V) (u : U) : + φ.tensorCongr ψ (v ⊗ₜ u) = φ v ⊗ₜ ψ u := rfl diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 509c11be28..7d3a44b288 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -53,7 +53,7 @@ since the bases of a color and its dual are dual. - `Species.Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`, with the colors `contrColors` of the free indices and the pairs. - `Species.Tensor.contr_pure` gives the contraction of a pure tensor. -- `Species.Tensor.contr_smul` proves that contraction commutes with the group action. +- `Species.Tensor.contr` is an intertwining map, so it commutes with the group action. - `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a relabeling of the slots before or after a contraction. - `ColorEquiv.SamePair` says that two choices of pairs contract the same two slots, and @@ -128,16 +128,21 @@ slot `inr (inl j)` is contracted with the slot `inr (inr j)` by `S.contr (a j)`, `inr (inl j)` as its first argument. The slots `inl x` are the free indices, with the colors `c`. It is the body of `Tensor.contr`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : - S.Tensor (contrColors c a) →ₗ[k] S.Tensor c := + (S.tensorRep (contrColors c a)).IntertwiningMap (S.tensorRep c) := -- The pairs are split into their first halves, of colors `a`, and their second halves, of the -- dual colors. Each vector of a second half is read as a linear form on the first half through -- `S.contr`, and the halves are paired by Mathlib's `PiTensorProduct.dualDistrib`. - (TensorProduct.rid k _).toLinearMap ∘ₗ + intertwiningMapOfPure ((TensorProduct.rid k _).toLinearMap ∘ₗ (lift ((PiTensorProduct.dualDistrib ∘ₗ PiTensorProduct.map (fun j => (curry (S.contr (a j)).toLinearMap).flip) ∘ₗ (S.toPiTensor _).toLinearMap).flip ∘ₗ (S.toPiTensor a).toLinearMap) ∘ₗ (S.tmulEquiv a _).symm.toLinearMap).lTensor _ ∘ₗ - (S.tmulEquiv c _).symm.toLinearMap + (S.tmulEquiv c _).symm.toLinearMap) fun g f => by + simp only [smul_pure, LinearMap.comp_apply, LinearEquiv.coe_coe, + Representation.Equiv.coe_toLinearMap, tmulEquiv_symm_pure, LinearMap.lTensor_tmul, + lift.tmul, LinearMap.flip_apply, toPiTensor_pure, PiTensorProduct.map_tprod, + PiTensorProduct.dualDistrib_apply, TensorProduct.rid_tmul, smul_comm g, smul_pure] + exact congrArg (· • _) (Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) /-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the two vectors of each pair, as in `u ⊗ v ⊗ α ↦ (v^μ α_μ) u`. -/ @@ -160,8 +165,9 @@ lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) `σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The slots `σ.toEquiv.symm (inl x)` are the free indices, with the colors `c'`. -/ noncomputable def contr [Fintype J] {c : ι₁ → Color K} {c' : κ → Color K} - {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) : S.Tensor c →ₗ[k] S.Tensor c' := - contrStd c' a ∘ₗ (S.reindex σ).toLinearMap + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) : + (S.tensorRep c).IntertwiningMap (S.tensorRep c') := + (contrStd c' a).comp (S.reindex σ).toIntertwiningMap variable {c : ι₁ → Color K} {c' : κ → Color K} @@ -176,16 +182,6 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) -/-- Contraction commutes with the group action, `(ΛT)^ρμ_μ = Λ^ρ_λ T^λμ_μ` for the action `Λ` of -`g : G`. -/ -lemma contr_smul [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (g : G) - (t : S.Tensor c) : contr σ (g • t) = g • contr σ t := - map_smul_of_pure (contr σ) g (fun f => by - rw [smul_pure, contr_pure, contr_pure, smul_comm, smul_pure] - simp only [cast_rep] - congr 1 - exact Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) t - /-! ### B.1. Relabeling the slots @@ -196,7 +192,8 @@ lemma contr_smul [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : contr σ (S.reindex τ t) = contr (τ.trans σ) t := by - simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, reindex_reindex] + simp only [contr, Representation.IntertwiningMap.comp_apply, + Representation.Equiv.coe_toIntertwiningMap, reindex_reindex] /-- Relabeling the free indices of a contraction is contracting along the composite relabeling. -/ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → Color K} @@ -208,7 +205,7 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → | h p => rw [contr_pure, map_smul, reindex_pure, contr_pure] congr 1 - exact congrArg S.pure (funext fun x => by simp only [LinearEquiv.cast_apply, cast_cast]; rfl) + exact congrArg S.pure (funext fun x => by simp only [cast_apply, cast_cast]; rfl) /-! @@ -363,7 +360,7 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := by induction t using inductionOn_pure with | h f => - simp only [contr_pure, map_smul, smul_smul, Fintype.prod_sum_type, LinearEquiv.cast_apply, + simp only [contr_pure, map_smul, smul_smul, Fintype.prod_sum_type, cast_apply, cast_cast] rfl | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] @@ -471,7 +468,7 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) contr (ColorEquiv.matMul x y x.dual) (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by - rw [deltaTensor, contr_matMul, (S.twoSlotEquiv _ _).injective.eq_iff, + rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoSlotEquiv _ _)).eq_iff, ← (S.toLin (a := x)).injective.eq_iff, toLin_lTensor, S.snake x.dual] /-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a @@ -562,8 +559,9 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) | hadd t t' ht ht' => rw [map_add, ht, ht'] | h p => refine mapAt_pure i _ _ p p ?_ fun _ _ => rfl - simp only [LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, LinearMap.id_apply, - LinearEquiv.cast_apply, cast_cast] + simp only [LinearMap.coe_comp, Function.comp_apply, + Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, + LinearMap.id_apply, cast_apply, cast_cast] rfl /-! @@ -579,7 +577,8 @@ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := by - simp only [contr, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe] + simp only [contr, Representation.IntertwiningMap.comp_apply, + Representation.Equiv.coe_toIntertwiningMap] generalize S.reindex σ t = u induction u using inductionOn_pure with | hsmul r u hu => simp only [map_smul, Finsupp.smul_apply, hu, smul_eq_mul, Finset.mul_sum] diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 7597f4b8a1..1915bcd425 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -596,7 +596,6 @@ private def regroupContractions : TacticM Unit := do throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ of slots. The last attempt failed with\n{lastError}" - /-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a @@ -708,8 +707,9 @@ macro "tensor_slots" : tactic => `(tactic| focus fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, LinearMap.id_apply, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, - not_false_eq_true, dite_false, LinearEquiv.coe_coe, LinearEquiv.trans_apply, - LinearEquiv.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual]) + not_false_eq_true, dite_false, Representation.IntertwiningMap.coe_toLinearMap, + Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, + Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual]) end Tensors.Notation @@ -870,15 +870,16 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ #guard_msgs in #check {v | ↑μ}ᵀ -/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [toDualAt_smul]`. -/ -example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [toDualAt_smul] +/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ +example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] /-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by - simp only [toDualAt_smul] + simp only [equiv_smul] -/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by `rw [Tensor.contr_smul]`. -/ -example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [Tensor.contr_smul] +/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by +`rw [intertwiningMap_smul]`. -/ +example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] /-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates is `ColorEquiv.matMul`, checked slot by slot. -/ diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 4da20e501c..55204dde4d 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Physlib.Mathematics.Tensors.Contraction -public import Mathlib.Tactic.Module public import Mathlib.LinearAlgebra.Matrix.BilinearForm /-! @@ -220,9 +219,11 @@ lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c. variable (S) in /-- The vector `v` of color `c` contracted with the first index of the form at `c.dual`. It lowers an upper index, `v^μ ↦ v^μ g_μν`, and raises a lower one, `w_μ ↦ w_μ g^μν`. -/ -noncomputable def toDual (c : Color K) [S.HasForm c.kind] : S.V c ≃ₗ[k] S.V c.dual := - LinearEquiv.ofLinearMap (S.toLin (form c.dual)) (S.toLin (form c)) (form_inverse c.dual) - (form_inverse c) +noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.rep c.dual) := + .mk (LinearEquiv.ofLinearMap (S.toLin (form c.dual)) (S.toLin (form c)) (form_inverse c.dual) + (form_inverse c)) fun g => LinearMap.ext fun v => by + change S.toLin (form c.dual) _ = S.rep c.dual g (S.toLin (form c.dual) v) + rw [← S.toLin_map, form_invariant] /-- `toDual` contracts `v` with the first index of the form at `c.dual`. -/ lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : @@ -235,12 +236,7 @@ lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDua @[simp] lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : S.toDual c.dual (S.toDual c v) = v := - (S.toDual c).symm_apply_apply v - -/-- Lowering and raising commute with the group action, `(Λv)_ν = Λ(v_ν)`. -/ -lemma toDual_rep (c : Color K) [S.HasForm c.kind] (g : G) (v : S.V c) : - S.toDual c (S.rep c g v) = S.rep c.dual g (S.toDual c v) := by - rw [toDual_apply, toDual_apply, ← S.toLin_map, form_invariant] + (S.toDual c).toLinearEquiv.symm_apply_apply v /-- Lowering or raising a vector in components, `v_ν = v^μ g_μν`. -/ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : @@ -264,8 +260,8 @@ noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] /-- The form is an invariant tensor. Every group element fixes it, as every Lorentz transformation fixes the Minkowski metric `η`. -/ lemma formTensor_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : - Λ • S.formTensor c = S.formTensor c := by - rw [formTensor, ← twoSlotEquiv_map_rep, form_invariant] + Λ • S.formTensor c = S.formTensor c := + (S.twoSlotEquiv c c).toIntertwiningMap.map_invariant (form_invariant c Λ) /-- The components of the form tensor are those of the form, `g_μν` at the pair of labels `(μ 0, μ 1)`. -/ @@ -302,15 +298,8 @@ at the kind of `a`. The colors `c'` of the result have the dual color at `i` by with `c` elsewhere by `h`. It is `congrAt` of `toDual`. -/ noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) : - S.Tensor c ≃ₗ[k] S.Tensor c' := - S.congrAt i (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm) h - -/-- Lowering or raising at a slot commutes with the group action, `(ΛT)^μ_ν = Λ(T^μ_ν)`. -/ -lemma toDualAt_smul {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) - [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (g : G) - (t : S.Tensor c) : S.toDualAt i a ha hi h (g • t) = g • S.toDualAt i a ha hi h t := - congrAt_smul i _ h (fun g x => by simp only [LinearEquiv.trans_apply, toDual_rep, cast_rep]) - g t + (S.tensorRep c).Equiv (S.tensorRep c') := + S.congrAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)) h /-- On a pure tensor, `toDualAt` applies `toDual` to the vector at `i` and keeps the others. The result `q` is explicit, so that for concrete colors `hqi` and `hq` hold by `rfl`. -/ @@ -326,8 +315,8 @@ lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a.dual hi ha (fun x hx => (h x hx).symm) (S.toDualAt i a ha hi h t) = t := by - rw [toDualAt, toDualAt, show S.cast hi ≪≫ₗ S.toDual a.dual ≪≫ₗ S.cast ha.symm = - (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm).symm from rfl, ← congrAt_symm] + rw [toDualAt, toDualAt, show ((S.cast hi).trans (S.toDual a.dual)).trans (S.cast ha.symm) = + (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).symm from rfl, ← congrAt_symm] exact (S.congrAt i _ h).symm_apply_apply t /-- Lowering or raising at a slot is `toDual` at that slot, read as a linear map. Chains of @@ -335,7 +324,7 @@ lowerings and raisings at several slots compose slot by slot through `mapSlots_m lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a ha hi h t = - S.mapAt i (S.cast ha ≪≫ₗ S.toDual a ≪≫ₗ S.cast hi.symm).toLinearMap h t := + S.mapAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).toLinearMap h t := congrAt_eq_mapAt i _ h t /-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = T^…μ… g_μν`. The diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 1a76b8cb5a..7cbc375df7 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -58,10 +58,9 @@ The components of a product are the products of the components, `(T ⊗ U)^μ_ν - `Species.tmulEquiv_swapLast` proves `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` up to relabeling. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. - `Species.oneSlotEquiv` turns a vector of `V c` into a tensor on one slot, and - `Species.oneSlotEquiv_rep` proves that it commutes with the group action. + it is an equivalence of representations. - `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots, and `Species.deltaTensor` is the delta `δ_μ^ν`. -- `Species.twoSlotEquiv_map_rep` proves that `Λ` acts on a tensor on two slots factor by factor. - `Species.reindex_twoSlotEquiv` proves that swapping the two slots swaps the factors, `M^μν ↦ M^νμ`. - `Species.deltaTensor_invariant` proves that the delta tensor is invariant. @@ -161,9 +160,16 @@ variable (S) in `T` sit on the slots `inl i` and those of `U` on the slots `inr j`. It is Mathlib's `PiTensorProduct.tmulEquivDep`. -/ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : - S.Tensor c₁ ⊗[k] S.Tensor c₂ ≃ₗ[k] S.Tensor (prodColors c₁ c₂) := - TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ - tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm + ((S.tensorRep c₁).tprod (S.tensorRep c₂)).Equiv (S.tensorRep (prodColors c₁ c₂)) := + .mk (TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ + tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm) fun g => + TensorProduct.ext <| ext_pure fun f₁ => ext_pure fun f₂ => by + simp only [LinearMap.compr₂ₛₗ_apply, TensorProduct.mk_apply, LinearMap.comp_apply, + Representation.tprod_apply, TensorProduct.map_tmul, + Representation.ofDistribMulAction_apply_apply, smul_pure, LinearEquiv.coe_coe, + LinearEquiv.trans_apply, TensorProduct.congr_tmul, toPiTensor_pure, tmulEquivDep_apply, + toPiTensor_symm_tprod] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) /-- The product of the pure tensors `u ⊗ α` and `w` is the pure tensor `u ⊗ α ⊗ w`. The vectors `f₁` of the first sit on the slots `inl i` and the vectors `f₂` of the second on the slots @@ -288,17 +294,9 @@ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} { `(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : G) (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : - S.tmulEquiv c₁ c₂ ((g • t₁) ⊗ₜ (g • t₂)) = g • S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) := by - induction t₁ using inductionOn_pure with - | hsmul r t ht => rw [smul_comm, ← smul_tmul', map_smul, ht, ← smul_tmul', map_smul, smul_comm] - | hadd t t' ht ht' => simp only [smul_add, add_tmul, map_add, ht, ht'] - | h f₁ => - induction t₂ using inductionOn_pure with - | hsmul r t ht => rw [smul_comm, tmul_smul, map_smul, ht, tmul_smul, map_smul, smul_comm] - | hadd t t' ht ht' => simp only [smul_add, tmul_add, map_add, ht, ht'] - | h f₂ => - rw [smul_pure, smul_pure, tmulEquiv_pure, tmulEquiv_pure, smul_pure] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + S.tmulEquiv c₁ c₂ ((g • t₁) ⊗ₜ (g • t₂)) = g • S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) := + Representation.IntertwiningMap.isIntertwining _ _ (S.tmulEquiv c₁ c₂).toIntertwiningMap g + (t₁ ⊗ₜ t₂) /-! @@ -319,8 +317,12 @@ tensor `S.deltaTensor c` is the delta seen this way. variable (S) in /-- A vector `v` of `V c` as a tensor with the one slot `![c]`, by Mathlib's `PiTensorProduct.subsingletonEquiv`. -/ -noncomputable def oneSlotEquiv (c : Color K) : S.V c ≃ₗ[k] S.Tensor ![c] := - (subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm +noncomputable def oneSlotEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c]) := + .mk ((subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm) + fun g => LinearMap.ext fun v => by + change S.pure _ = g • S.pure _ + rw [smul_pure] + exact congrArg S.pure (funext fun i => by fin_cases i; rfl) /-- The tensor of `v` is the pure tensor with `v` on its one slot. -/ @[simp] @@ -328,12 +330,6 @@ lemma oneSlotEquiv_apply (c : Color K) (v : S.V c) : S.oneSlotEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := congrArg S.pure (funext fun i => by fin_cases i; rfl) -/-- `oneSlotEquiv` commutes with the group action, `Λv ↦ Λ • v`. -/ -lemma oneSlotEquiv_rep (c : Color K) (g : G) (v : S.V c) : - S.oneSlotEquiv c (S.rep c g v) = g • S.oneSlotEquiv c v := by - rw [oneSlotEquiv_apply, oneSlotEquiv_apply, smul_pure] - exact congrArg S.pure (funext fun i => by fin_cases i; rfl) - /-- The tensor of the basis vector `S.basis c (μ 0)` is the basis tensor with label `μ`. -/ lemma oneSlotEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : S.oneSlotEquiv c (S.basis c (μ 0)) = S.tensorBasis ![c] μ := by @@ -344,30 +340,20 @@ variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` is the tensor `η_μν`. Each factor is a one-slot tensor by `oneSlotEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by `ColorEquiv.ofFinTwo`. -/ -noncomputable def twoSlotEquiv (a b : Color K) : S.V a ⊗[k] S.V b ≃ₗ[k] S.Tensor ![a, b] := - TensorProduct.congr (S.oneSlotEquiv a) (S.oneSlotEquiv b) ≪≫ₗ S.tmulEquiv ![a] ![b] ≪≫ₗ - S.reindex (ColorEquiv.ofFinTwo a b).symm +noncomputable def twoSlotEquiv (a b : Color K) : + ((S.rep a).tprod (S.rep b)).Equiv (S.tensorRep ![a, b]) := + ((S.oneSlotEquiv a).tensorCongr (S.oneSlotEquiv b)).trans <| + (S.tmulEquiv ![a] ![b]).trans (S.reindex (ColorEquiv.ofFinTwo a b).symm) /-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ @[simp] lemma twoSlotEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : S.twoSlotEquiv a b (x ⊗ₜ y) = S.pure (c := ![a, b]) (Fin.cons x (Fin.cons y finZeroElim)) := by - simp only [twoSlotEquiv, LinearEquiv.trans_apply, TensorProduct.congr_tmul, oneSlotEquiv_apply, - tmulEquiv_pure, reindex_pure] + simp only [twoSlotEquiv, Representation.Equiv.trans_apply, + Representation.Equiv.tensorCongr_tmul, oneSlotEquiv_apply, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) -/-- `twoSlotEquiv` commutes with the group action, which acts on `M ∈ V a ⊗ V b` factor by factor, -`Λ (x ⊗ y) = Λx ⊗ Λy`. -/ -lemma twoSlotEquiv_map_rep (a b : Color K) (Λ : G) (M : S.V a ⊗[k] S.V b) : - S.twoSlotEquiv a b (TensorProduct.map (S.rep a Λ) (S.rep b Λ) M) = - Λ • S.twoSlotEquiv a b M := by - induction M using TensorProduct.inductionOn with - | add x y hx hy => simp only [map_add, smul_add, hx, hy] - | tmul x y => - simp only [TensorProduct.map_tmul, twoSlotEquiv_tmul, smul_pure] - exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) - /-- Swapping the two slots of a tensor on two slots swaps the factors, `M^μν ↦ M^νμ`. -/ lemma reindex_twoSlotEquiv {a b : Color K} {σ : ColorEquiv ![a, b] ![b, a]} (hσ : σ.toEquiv = Equiv.swap 0 1) (M : S.V a ⊗[k] S.V b) : @@ -387,8 +373,8 @@ noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := S.twoSlotEquiv c.dual c (S.delta c) /-- The delta tensor is invariant, `Λ δ = δ`. -/ -lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := by - rw [deltaTensor, ← twoSlotEquiv_map_rep, delta_invariant] +lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := + (S.twoSlotEquiv _ c).toIntertwiningMap.map_invariant (S.delta_invariant c Λ) /-! diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 09a2f54ff7..b40b65e9ec 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -50,7 +50,8 @@ their order. tensor. - `Species.reindex_reindex` proves that two relabelings in sequence are their composite, and `Species.reindex_refl` that the identity relabeling does nothing. -- `Species.reindex_smul` proves that relabeling commutes with the group action. +- `Species.reindex` is an equivalence of representations, so relabeling commutes with the group + action. - `Species.tensorBasis_repr_reindex` gives the components of a relabeled tensor, `(Tᵀ)_ν^μ = T^μ_ν`. @@ -128,10 +129,13 @@ variable (S) in holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is Mathlib's `PiTensorProduct.reindex` followed by `PiTensorProduct.congr` of `LinearEquiv.cast`. -/ noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : - S.Tensor c₁ ≃ₗ[k] S.Tensor c₂ := - S.toPiTensor c₁ ≪≫ₗ PiTensorProduct.reindex k _ σ.toEquiv ≪≫ₗ - PiTensorProduct.congr (fun j => S.cast (σ.color_eq j)) ≪≫ₗ - (S.toPiTensor c₂).symm + (S.tensorRep c₁).Equiv (S.tensorRep c₂) := + equivOfPure (S.toPiTensor c₁ ≪≫ₗ PiTensorProduct.reindex k _ σ.toEquiv ≪≫ₗ + PiTensorProduct.congr (fun j => (S.cast (σ.color_eq j)).toLinearEquiv) ≪≫ₗ + (S.toPiTensor c₂).symm) fun g f => by + simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, + PiTensorProduct.reindex_tprod, congr_tprod, toPiTensor_symm_tprod] + exact congrArg S.pure (funext fun j => cast_rep _ _ _) /-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. -/ @@ -150,27 +154,19 @@ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C /-- Relabeling by the identity does nothing. -/ lemma reindex_refl {c₁ : ι₁ → Color K} (t : S.Tensor c₁) : S.reindex (ColorEquiv.refl c₁) t = t := LinearMap.congr_fun (f := (S.reindex (ColorEquiv.refl c₁)).toLinearMap) - (ext_pure (g := LinearMap.id) fun f => by rw [LinearEquiv.coe_coe, reindex_pure]; rfl) t + (ext_pure (g := LinearMap.id) fun f => by + rw [Representation.Equiv.coe_toLinearMap, reindex_pure]; rfl) t /-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`. -/ lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := - LinearMap.congr_fun (f := (S.reindex σ₁ ≪≫ₗ S.reindex σ₂).toLinearMap) + LinearMap.congr_fun (f := ((S.reindex σ₁).trans (S.reindex σ₂)).toLinearMap) (ext_pure (g := (S.reindex (σ₁.trans σ₂)).toLinearMap) fun f => by - simp only [LinearEquiv.coe_coe, LinearEquiv.trans_apply, reindex_pure, - LinearEquiv.cast_apply, cast_cast] + simp only [Representation.Equiv.coe_toLinearMap, Representation.Equiv.trans_apply, + reindex_pure, cast_apply, cast_cast] rfl) t -/-- Relabeling commutes with the group action, as in `(ΛT)ᵀ = Λ Tᵀ`. -/ -lemma reindex_smul {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (g : G) - (t : S.Tensor c₁) : S.reindex σ (g • t) = g • S.reindex σ t := - map_smul_of_pure (S.reindex σ) g (fun f => by - simp only [smul_pure, reindex_pure] - congr 1 - funext j - exact cast_rep _ _ _) t - /-! ## D. Components diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index efa0a31394..bbbab09dbb 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -8,7 +8,6 @@ module public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color public import Mathlib.RepresentationTheory.Intertwining -public import Mathlib.LinearAlgebra.Basis.Bilinear /-! # Tensor species @@ -327,15 +326,20 @@ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) -/ variable (S) in -/-- Moving a vector along an equality of colors `a = a'`, Mathlib's `LinearEquiv.cast`. -/ -protected abbrev cast {a a' : Color K} (p : a = a') : S.V a ≃ₗ[k] S.V a' := - LinearEquiv.cast p +/-- Moving a vector along an equality of colors `a = a'`, Mathlib's `LinearEquiv.cast`, as an +equivalence of representations. -/ +protected def cast {a a' : Color K} (p : a = a') : (S.rep a).Equiv (S.rep a') := + .mk (LinearEquiv.cast p) fun _ => by subst p; rfl + +/-- `S.cast p` is Mathlib's `cast` of the vector. -/ +@[simp] +lemma cast_apply {a a' : Color K} (p : a = a') (x : S.V a) : + S.cast p x = cast (congrArg S.V p) x := rfl /-- Moving a vector along an equality of colors `a = a'` commutes with the group action. -/ lemma cast_rep {a a' : Color K} (p : a = a') (g : G) (x : S.V a) : - S.cast p (S.rep a g x) = S.rep a' g (S.cast p x) := by - subst p - rfl + S.cast p (S.rep a g x) = S.rep a' g (S.cast p x) := + Representation.IntertwiningMap.isIntertwining _ _ (S.cast p).toIntertwiningMap g x /-- Moving a vector along an equality of colors `a = b` keeps its components, with the label cast back to a label at `a`. -/ diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 190a258b47..9a4ab75429 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -7,6 +7,7 @@ module public import Physlib.Mathematics.Tensors.Species public import Mathlib.LinearAlgebra.PiTensorProduct.Basis +public import Physlib.Mathematics.ForMathlib.Representation /-! # Tensors of a species @@ -55,6 +56,10 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.smul_pure` proves that the group acts on a pure tensor slot by slot. - `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure tensors commutes with it on all tensors. +- `Species.tensorRep` is the action on tensors as a Mathlib representation. Maps of tensors that + commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` + of it, built by `Species.equivOfPure` and `Species.intertwiningMapOfPure` from a check on pure + tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. - `Species.smul_eq_self_of_isEmpty` proves that a tensor with no slots is invariant. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. @@ -62,8 +67,9 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_mapSlots` and `Species.mapSlots_id`, and `Species.mapAt` applies one at a single slot and the identity at the others. -- `Species.congrAt` applies a linear equivalence at one slot of a tensor and the identity at the - others, with `Species.congrAt_pure`, `Species.congrAt_smul` and `Species.congrAt_symm`, and +- `Species.congrSlots` applies an equivalence of representations at every slot. +- `Species.congrAt` applies an equivalence of representations at one slot of a tensor and the + identity at the others, with `Species.congrAt_pure` and `Species.congrAt_symm`, and `Species.congrAt_eq_mapAt` proves that `congrAt` is `mapAt` of the underlying map. ## iii. Table of contents @@ -167,6 +173,25 @@ noncomputable instance : SMulCommClass G k (S.Tensor c) where noncomputable instance : SMulCommClass k G (S.Tensor c) := .symm _ _ _ +variable (S) in +/-- The action of the group on tensors as Mathlib's representation, `Λ ↦ (T ↦ Λ • T)`. Maps of +tensors that commute with the action are Mathlib's `Representation.Equiv` and +`Representation.IntertwiningMap` for it. -/ +noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (S.Tensor c) := + Representation.ofDistribMulAction k G (S.Tensor c) + +/-- An intertwining map from or to tensors commutes with the actions, `φ (Λ v) = Λ φ v`. -/ +lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} + (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : S.Tensor c) : + φ (g • t) = g • φ t := + Representation.IntertwiningMap.isIntertwining _ _ φ g t + +/-- An equivalence of representations of tensors commutes with the actions, `φ (Λ T) = Λ φ T`. -/ +lemma equiv_smul {ι' : Type} {c' : ι' → Color K} + (φ : (S.tensorRep c).Equiv (S.tensorRep c')) (g : G) (t : S.Tensor c) : + φ (g • t) = g • φ t := + intertwiningMap_smul φ.toIntertwiningMap g t + /-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes with `Λ` on all tensors. -/ lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} @@ -178,6 +203,33 @@ lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] | hadd x y hx hy => rw [smul_add, map_add, hx, hy, map_add, smul_add] +variable {ι' : Type} {c' : ι' → Color K} in +/-- A linear equivalence of tensors that commutes with `Λ` on pure tensors, as an equivalence of +representations. -/ +noncomputable def equivOfPure (e : S.Tensor c ≃ₗ[k] S.Tensor c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) : + (S.tensorRep c).Equiv (S.tensorRep c') := + .mk e fun g => LinearMap.ext fun t => map_smul_of_pure e g (h g) t + +variable {ι' : Type} {c' : ι' → Color K} in +@[simp] +lemma equivOfPure_apply (e : S.Tensor c ≃ₗ[k] S.Tensor c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) (t : S.Tensor c) : + equivOfPure e h t = e t := rfl + +variable {ι' : Type} {c' : ι' → Color K} in +/-- A linear map of tensors that commutes with `Λ` on pure tensors, as an intertwining map. -/ +noncomputable def intertwiningMapOfPure (f : S.Tensor c →ₗ[k] S.Tensor c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) : + (S.tensorRep c).IntertwiningMap (S.tensorRep c') := + ⟨f, fun g => LinearMap.ext fun t => map_smul_of_pure f g (h g) t⟩ + +variable {ι' : Type} {c' : ι' → Color K} in +@[simp] +lemma intertwiningMapOfPure_apply (f : S.Tensor c →ₗ[k] S.Tensor c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) (t : S.Tensor c) : + intertwiningMapOfPure f h t = f t := rfl + /-- A tensor with no slots is invariant, `Λ s = s`. -/ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = t := by induction t using inductionOn_pure with @@ -244,22 +296,38 @@ section variable [DecidableEq ι] {c' : ι → Color K} variable (S) in -/-- The linear equivalence `f` at the slot `i`, and the identity, moved along `h x`, at every other -slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ -noncomputable def congrAt (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) - (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c ≃ₗ[k] S.Tensor c' := - S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => if hx : x = i then - hx ▸ f else S.cast (h x hx)) ≪≫ₗ (S.toPiTensor c').symm +/-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations +of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ +noncomputable def congrSlots (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : + (S.tensorRep c).Equiv (S.tensorRep c') := + equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => (f x).toLinearEquiv) ≪≫ₗ + (S.toPiTensor c').symm) fun g p => by + simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, + toPiTensor_symm_tprod] + exact congrArg S.pure (funext fun x => + Representation.IntertwiningMap.isIntertwining _ _ (f x).toIntertwiningMap g (p x)) + +omit [DecidableEq ι] in +/-- On a pure tensor, `congrSlots` applies `f x` to the vector at each slot `x`. -/ +lemma congrSlots_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : + S.congrSlots f (S.pure p) = S.pure fun x => f x (p x) := by + simp [congrSlots, congr_tprod] + +variable (S) in +/-- The equivalence of representations `f` at the slot `i`, and the identity, moved along `h x`, +at every other slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ +noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) + (h : ∀ x, x ≠ i → c x = c' x) : (S.tensorRep c).Equiv (S.tensorRep c') := + S.congrSlots fun x => if hx : x = i then hx ▸ f else S.cast (h x hx) /-- On a pure tensor, `congrAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ -lemma congrAt_pure (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) +lemma congrAt_pure (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hqi : q i = f (p i)) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : S.congrAt i f h (S.pure p) = S.pure q := by - simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, - toPiTensor_symm_tprod] + rw [congrAt, congrSlots_pure] congr 1 funext x by_cases hx : x = i @@ -267,29 +335,16 @@ lemma congrAt_pure (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) simp [hqi] · simp [hx, hq x hx] -/-- `congrAt` commutes with the group action when `f` does, `(ΛT)_ν… = Λ(T_ν…)` for lowering -with an invariant metric. -/ -lemma congrAt_smul (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) - (hf : ∀ g x, f (S.rep (c i) g x) = S.rep (c' i) g (f x)) (g : G) (t : S.Tensor c) : - S.congrAt i f h (g • t) = g • S.congrAt i f h t := - map_smul_of_pure (S.congrAt i f h) g (fun p => by - simp only [smul_pure, congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, - toPiTensor_symm_tprod] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp [hf] - · simp only [hx, dite_false, cast_rep]) t - /-- The inverse of `congrAt` of `f` is `congrAt` of `f.symm`. -/ -lemma congrAt_symm (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : +lemma congrAt_symm (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) + (h : ∀ x, x ≠ i → c x = c' x) : (S.congrAt i f h).symm = S.congrAt i f.symm (fun x hx => (h x hx).symm) := by - refine LinearEquiv.ext fun t => (LinearEquiv.symm_apply_eq _).2 ?_ + refine Representation.Equiv.ext (funext fun t => ?_) + refine (S.congrAt i f h).toLinearEquiv.symm_apply_eq.2 ?_ induction t using inductionOn_pure with | h p => - simp only [congrAt, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, - toPiTensor_symm_tprod] + change _ = S.congrSlots _ (S.congrSlots _ (S.pure p)) + rw [congrSlots_pure, congrSlots_pure] congr 1 funext x by_cases hx : x = i @@ -349,8 +404,9 @@ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ · simp [hx, hq x hx] /-- `congrAt` is `mapAt` of the underlying linear map. -/ -lemma congrAt_eq_mapAt (i : ι) (f : S.V (c i) ≃ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) - (t : S.Tensor c) : S.congrAt i f h t = S.mapAt i f.toLinearMap h t := by +lemma congrAt_eq_mapAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) + (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + S.congrAt i f h t = S.mapAt i f.toLinearMap h t := by induction t using inductionOn_pure with | h p => let q : Π x, S.V (c' x) := fun x => if hx : x = i then hx ▸ f (p i) else S.cast (h x hx) (p x) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index cde33a82e7..fb3bcfee1a 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -105,7 +105,7 @@ variable (S) in `S.rep c`. A type that is such a space, such as a Weyl spinor, gets its instance from this. -/ noncomputable abbrev ofIndexSpace (c : Color K) : Tensorial S ![c] (S.V c) where rep := S.rep c - toTensor := .mk (S.oneSlotEquiv c) fun g => LinearMap.ext (S.oneSlotEquiv_rep c g) + toTensor := S.oneSlotEquiv c /-- A type `M` with an action `r` and an equivalence `φ` to a tensorial type `N` commuting with the actions is tensorial with the colors of `N`. -/ @@ -130,10 +130,7 @@ product action `Λ (x ⊗ y) = Λ x ⊗ Λ y` and `x ⊗ y` read as the product noncomputable instance prod [Tensorial S c₁ M] [Tensorial S c₂ N] : Tensorial S (prodColors c₁ c₂) (M ⊗[k] N) where rep := (rep (M := M)).tprod (rep (M := N)) - toTensor := .mk (TensorProduct.congr (toTensor (M := M)).toLinearEquiv - (toTensor (M := N)).toLinearEquiv ≪≫ₗ S.tmulEquiv c₁ c₂) fun g => by - ext x y - simp [Representation.tprod_apply, Species.tmulEquiv_smul] + toTensor := ((toTensor (M := M)).tensorCongr (toTensor (M := N))).trans (S.tmulEquiv c₁ c₂) /-- The group acts on `x ⊗ y` factor by factor, `Λ (x ⊗ y) = Λ x ⊗ Λ y`. -/ @[simp] diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 2c62f97064..8f85af6ae2 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -470,7 +470,7 @@ lemma tensorBasis_repr_toDualAt_contr_vector_tmul (d : ℕ) (F G : Vector d ⊗[ lemma contr_vector_coVector_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) (ω : CoVector d) : {Tensorial.rep Λ v | μ ⊗ Tensorial.rep Λ ω | μ}ᵀ = {v | μ ⊗ ω | μ}ᵀ := by rw [Tensorial.toTensor_rep, Tensorial.toTensor_rep, Species.tmulEquiv_smul, - Species.Tensor.contr_smul, Species.smul_eq_self_of_isEmpty] + Species.intertwiningMap_smul, Species.smul_eq_self_of_isEmpty] end realLorentz From 0229ad471a86d8449cea5efb90daf4b7650d1c39 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 12:43:52 +0000 Subject: [PATCH 017/242] feat(ComplexTensor): ComplexVector.metricVal_invariant, ComplexCoVector.metricVal_invariant, coordPairing_complexVector_coVector, dotProduct_SL2CRep Co-authored-by: Claude Opus 5.5 --- .../Tensors/ComplexTensor/Metrics/Pre.lean | 15 +++++++++++++++ .../Tensors/ComplexTensor/Vector/Basic.lean | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean b/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean index b442ea59a6..1d3303bec0 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean @@ -27,6 +27,13 @@ namespace Lorentz def ComplexVector.metricVal : (ComplexVector ⊗[ℂ] ComplexVector) := contrContrToMatrix.symm ((@minkowskiMatrix 3).map ofRealHom) +/-- The metric `ηᵃᵃ` is invariant under `SL(2,ℂ)`. -/ +lemma ComplexVector.metricVal_invariant (M : SL(2,ℂ)) : + TensorProduct.map (ComplexVector.SL2CRep M) (ComplexVector.SL2CRep M) + ComplexVector.metricVal = ComplexVector.metricVal := by + rw [ComplexVector.metricVal, contrContrToMatrix_ρ_symm, + LorentzGroup.toComplex_mul_minkowskiMatrix_mul_transpose] + /-- The expansion of `ComplexVector.metricVal` into basis vectors. -/ lemma ComplexVector.metricVal_expand_tmul : ComplexVector.metricVal = ComplexVector.basis (Sum.inl 0) ⊗ₜ[ℂ] ComplexVector.basis (Sum.inl 0) @@ -77,6 +84,14 @@ lemma ComplexVector.metric_apply_one : ComplexVector.metric (1 : ℂ) = ComplexV def ComplexCoVector.metricVal : (ComplexCoVector ⊗[ℂ] ComplexCoVector) := coCoToMatrix.symm ((@minkowskiMatrix 3).map ofRealHom) +/-- The metric `ηᵢᵢ` is invariant under `SL(2,ℂ)`. -/ +lemma ComplexCoVector.metricVal_invariant (M : SL(2,ℂ)) : + TensorProduct.map (ComplexCoVector.SL2CRep M) (ComplexCoVector.SL2CRep M) + ComplexCoVector.metricVal = ComplexCoVector.metricVal := by + rw [ComplexCoVector.metricVal, coCoToMatrix_ρ_symm, LorentzGroup.toComplex_inv] + simp only [LorentzGroup.inv_eq_dual, SL2C.toLorentzGroup_apply_coe, + LorentzGroup.toComplex_transpose_mul_minkowskiMatrix_mul_self] + /-- The expansion of `ComplexCoVector.metricVal` into basis vectors. -/ lemma ComplexCoVector.metricVal_expand_tmul : ComplexCoVector.metricVal = ComplexCoVector.basis (Sum.inl 0) ⊗ₜ[ℂ] ComplexCoVector.basis (Sum.inl 0) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean b/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean index c2f1d75159..b54b86c178 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean @@ -5,6 +5,7 @@ Authors: Nikolai Kashcheev, Joseph Tooby-Smith -/ module +public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Relativity.Tensors.ComplexTensor.Vector.Modules /-! @@ -263,5 +264,22 @@ lemma inclCoRealLorentz_ρ (M : SL(2, ℂ)) (v : CoMod 3) : v.toFin1dℝ)) i rw [LorentzGroup.toComplex_mulVec_ofReal] +/-- The coordinate pairing of `ComplexVector.basis` and `ComplexCoVector.basis` is +`ψⁱ φᵢ = ψ ⬝ᵥ φ`. -/ +lemma coordPairing_complexVector_coVector (ψ : ComplexVector) (φ : ComplexCoVector) : + ComplexVector.basis.coordPairing ComplexCoVector.basis (ψ ⊗ₜ φ) = + ψ.toFin13ℂ ⬝ᵥ φ.toFin13ℂ := by + simp [Module.Basis.coordPairing_tmul, dotProduct] + rfl + +/-- The pairing `ψⁱ φᵢ` of a vector and a covector is invariant under `SL(2, ℂ)`. -/ +lemma dotProduct_SL2CRep (M : SL(2, ℂ)) (ψ : ComplexVector) (φ : ComplexCoVector) : + (ComplexVector.SL2CRep M ψ).toFin13ℂ ⬝ᵥ (ComplexCoVector.SL2CRep M φ).toFin13ℂ = + ψ.toFin13ℂ ⬝ᵥ φ.toFin13ℂ := by + change ((LorentzGroup.toComplex (SL2C.toLorentzGroup M)) *ᵥ ψ.toFin13ℂ) ⬝ᵥ + ((LorentzGroup.toComplex (SL2C.toLorentzGroup M))⁻¹ᵀ *ᵥ φ.toFin13ℂ) = _ + rw [dotProduct_mulVec, vecMul_transpose, mulVec_mulVec, + inv_mul_of_invertible (LorentzGroup.toComplex (SL2C.toLorentzGroup M)), one_mulVec] + end Lorentz end From 90e4b651155e57ea56c953db254f13de04479037 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 12:43:52 +0000 Subject: [PATCH 018/242] feat(ComplexTensor): complexLorentz species Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../Tensors/ComplexTensor/Species.lean | 712 ++++++++++++++++++ 2 files changed, 713 insertions(+) create mode 100644 Physlib/Relativity/Tensors/ComplexTensor/Species.lean diff --git a/Physlib.lean b/Physlib.lean index 8e47ee771b..31dead9a09 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -520,6 +520,7 @@ public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Basic public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Lemmas public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre public import Physlib.Relativity.Tensors.ComplexTensor.OfGaussianInt +public import Physlib.Relativity.Tensors.ComplexTensor.Species public import Physlib.Relativity.Tensors.ComplexTensor.Units.Basic public import Physlib.Relativity.Tensors.ComplexTensor.Units.Pre public import Physlib.Relativity.Tensors.ComplexTensor.Units.Symm diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean new file mode 100644 index 0000000000..f1706d46ca --- /dev/null +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -0,0 +1,712 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.Conjugation +public import Physlib.Relativity.Fermions.Weyl.Metric +public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre +public import Physlib.Relativity.Tensors.ComplexTensor.Vector.Basic +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix +/-! + +# The species of complex Lorentz tensors + +Weyl spinors and complex Lorentz vectors as one conjugate species of `SL(2, ℂ)`, with the invariant +forms `ε` and `η`. + +## i. Overview + +Complex Lorentz tensors have three kinds of index, the left-handed kind `left`, as in `ψ_α`, and +the right-handed kind `right`, as in `ψ̄_α̇`, with basis labels `Fin 2`, and the vector kind `vec`, +with labels `Fin 1 ⊕ Fin 3`. Conjugation `star` swaps `left` and `right` and fixes `vec`. With +the two variances this gives six colors, `upL`, `downL`, `upR`, `downR`, `up` and `down`. Under +`open scoped Tensors.complexLorentz`, `ℂT[upL, downR]` is the space of the tensors `T^α_β̇`. + +The species is built on `Fermion.LeftHandedWeyl`, `Fermion.RightHandedWeyl`, their duals, +`Lorentz.ComplexVector` and `Lorentz.ComplexCoVector`, with their representations and bases. An +index contracts with its dual by the coordinate pairing, which is `SL(2, ℂ)`-invariant. +Conjugation conjugates the components, and a label of a kind is the same label of its +conjugate (`idxBar`), a conjugation of the species (`HasConjugation.ofBasis`). The +right-handed representation is the conjugate of the left-handed one, `M ↦ M̄`, and the vector +representation is real, so this conjugation commutes with `SL(2, ℂ)` +(`IsEquivariantConjugation`). + +The invariant form is `ε` at the Weyl colors and `η` at the vector colors. The form `ε` is +antisymmetric and `η` symmetric (`IsAntisymmetricForm`, `IsSymmetricForm`). The form +`ε^αβ` has the matrix `!![0, -1; 1, 0]` and `ε_αβ` the matrix `!![0, 1; -1, 0]`. Lowering contracts +the first index of the form, `ψ_β = ψ^α ε_αβ`. This is Wess and Bagger's `ψ_α = ε_αβ ψ^β`, where +`ε_12 = -1` and `ε^12 = 1`, because their forms are the negatives of `εL'` and `εL`. The +forms are written `εL`, `εL'`, `εR`, `εR'`, `η` and `η'`, and the deltas `δL`, `δL'`, `δR`, `δR'`, +`δ` and `δ'`. Their components are stated through `tensorBasis`, and the antisymmetry +of `ε` and the symmetry of `η` are stated in index notation. + +These spaces are tensors with one slot (`Tensorial`), so a spinor `ψ : LeftHandedWeyl` +enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_α`. + +## ii. Key results + +- `complexLorentz` : the species of complex Lorentz tensors. +- the instances `complexLorentz.HasForm κ` : the forms `ε` and `η`, with `IsAntisymmetricForm` + at the Weyl kinds and `IsSymmetricForm` at the vector kind. +- the instances `complexLorentz.HasConjugation` and `complexLorentz.IsEquivariantConjugation` : + conjugation swaps left- and right-handed indices and commutes with `SL(2, ℂ)`, + and `complexLorentz.tensorBasis_repr_conj` : the components of a conjugate tensor are the + conjugates of the components. +- `complexLorentz.formMatrix_apply` : the components `Species.formMatrix` of the form at any color + are `complexLorentz.formEntries`, for lowering in components. +- `complexLorentz.tensorBasis_repr_formTensor` : the components of `ε` and `η` at any color, and + `complexLorentz.tensorBasis_repr_deltaTensor` those of `δ`. +- `complexLorentz.formTensor_upL_antisymm`, `complexLorentz.formTensor_up_symm` : `ε` is + antisymmetric and `η` symmetric, and `complexLorentz.deltaTensor_swap` : `δ^μ_ν = δ_ν^μ`. +- `complexLorentz.leftHandedWeylTensorial` and the other instances of section F : Weyl spinors and + complex vectors as tensors with one slot. +- `complexLorentz.tensorBasis_repr_toTensor_leftRight` : the components of a tensor in + `LeftHandedWeyl ⊗ RightHandedWeyl` are the entries of `leftRightToMatrix`. + +## iii. Table of contents + +- A. The species +- B. The invariant forms +- C. Conjugation +- D. Components of the forms and the deltas +- E. Symmetry of the forms +- F. Weyl spinors and complex vectors as tensors +- G. Examples + +## iv. References + +- J. Wess and J. Bagger, Supersymmetry and Supergravity, Appendix A. + +-/ + +@[expose] public section + +open TensorProduct Fermion Lorentz MatrixGroups + +namespace Tensors + +/-! + +## A. The species + +-/ + +namespace complexLorentz + +/-- The kinds of complex Lorentz index. -/ +inductive Kind + /-- The left-handed kind, as the index of `ψ_α`. -/ + | left + /-- The right-handed kind, as the index of `ψ̄_α̇`. -/ + | right + /-- The vector kind, its own conjugate. -/ + | vec +deriving DecidableEq + +export Kind (left right vec) + +/-- Conjugation swaps the left- and right-handed kinds and fixes the vector kind. -/ +instance : InvolutiveStar Kind where + star + | left => right + | right => left + | vec => vec + star_involutive κ := by cases κ <;> rfl + +/-- The basis labels of a kind, `Fin 2` for Weyl spinors and `Fin 1 ⊕ Fin 3` for vectors. -/ +abbrev Idx : Kind → Type + | left | right => Fin 2 + | vec => Fin 1 ⊕ Fin 3 + +instance : ∀ κ, Fintype (Idx κ) + | left | right => inferInstanceAs (Fintype (Fin 2)) + | vec => inferInstanceAs (Fintype (Fin 1 ⊕ Fin 3)) + +instance : ∀ κ, DecidableEq (Idx κ) + | left | right => inferInstanceAs (DecidableEq (Fin 2)) + | vec => inferInstanceAs (DecidableEq (Fin 1 ⊕ Fin 3)) + +/-- The vector space of a complex Lorentz index of color `c`. A `def` with its own instances, as +`realLorentz.IndexSpace`. -/ +def IndexSpace : Color Kind → Type + | ⟨left, .up⟩ => LeftHandedWeyl + | ⟨left, .down⟩ => DualLeftHandedWeyl + | ⟨right, .up⟩ => RightHandedWeyl + | ⟨right, .down⟩ => DualRightHandedWeyl + | ⟨vec, .up⟩ => ComplexVector + | ⟨vec, .down⟩ => ComplexCoVector + +noncomputable instance : ∀ c, AddCommGroup (IndexSpace c) + | ⟨left, .up⟩ => inferInstanceAs (AddCommGroup LeftHandedWeyl) + | ⟨left, .down⟩ => inferInstanceAs (AddCommGroup DualLeftHandedWeyl) + | ⟨right, .up⟩ => inferInstanceAs (AddCommGroup RightHandedWeyl) + | ⟨right, .down⟩ => inferInstanceAs (AddCommGroup DualRightHandedWeyl) + | ⟨vec, .up⟩ => inferInstanceAs (AddCommGroup ComplexVector) + | ⟨vec, .down⟩ => inferInstanceAs (AddCommGroup ComplexCoVector) + +noncomputable instance : ∀ c, Module ℂ (IndexSpace c) + | ⟨left, .up⟩ => inferInstanceAs (Module ℂ LeftHandedWeyl) + | ⟨left, .down⟩ => inferInstanceAs (Module ℂ DualLeftHandedWeyl) + | ⟨right, .up⟩ => inferInstanceAs (Module ℂ RightHandedWeyl) + | ⟨right, .down⟩ => inferInstanceAs (Module ℂ DualRightHandedWeyl) + | ⟨vec, .up⟩ => inferInstanceAs (Module ℂ ComplexVector) + | ⟨vec, .down⟩ => inferInstanceAs (Module ℂ ComplexCoVector) + +/-- The representation of `SL(2, ℂ)` on the vector space of a color. -/ +noncomputable def indexRep : (c : Color Kind) → Representation ℂ SL(2, ℂ) (IndexSpace c) + | ⟨left, .up⟩ => LeftHandedWeyl.rep + | ⟨left, .down⟩ => DualLeftHandedWeyl.rep + | ⟨right, .up⟩ => RightHandedWeyl.rep + | ⟨right, .down⟩ => DualRightHandedWeyl.rep + | ⟨vec, .up⟩ => ComplexVector.SL2CRep + | ⟨vec, .down⟩ => ComplexCoVector.SL2CRep + +/-- The basis of the vector space of a color. -/ +noncomputable def indexBasis : + (c : Color Kind) → Module.Basis (Idx c.kind) ℂ (IndexSpace c) + | ⟨left, .up⟩ => LeftHandedWeyl.basis + | ⟨left, .down⟩ => DualLeftHandedWeyl.basis + | ⟨right, .up⟩ => RightHandedWeyl.basis + | ⟨right, .down⟩ => DualRightHandedWeyl.basis + | ⟨vec, .up⟩ => ComplexVector.basis + | ⟨vec, .down⟩ => ComplexCoVector.basis + +/-- The coordinate pairing of a color and its dual is invariant under `SL(2, ℂ)`. -/ +lemma coordPairing_rep : ∀ (c : Color Kind) (g : SL(2, ℂ)) (x : IndexSpace c) + (y : IndexSpace c.dual), + (indexBasis c).coordPairing (indexBasis c.dual) (indexRep c g x ⊗ₜ indexRep c.dual g y) = + (indexBasis c).coordPairing (indexBasis c.dual) (x ⊗ₜ y) + | ⟨left, .up⟩ => Species.invariant_of_contr + LeftHandedWeyl.contr fun μ ν => + (LeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) + | ⟨left, .down⟩ => Species.invariant_of_contr + DualLeftHandedWeyl.contr fun μ ν => + (DualLeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) + | ⟨right, .up⟩ => Species.invariant_of_contr + RightHandedWeyl.contr fun μ ν => + (RightHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) + | ⟨right, .down⟩ => Species.invariant_of_contr + DualRightHandedWeyl.contr fun μ ν => + (DualRightHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) + | ⟨vec, .up⟩ => fun g x y => (coordPairing_complexVector_coVector _ _).trans <| + (dotProduct_SL2CRep g x y).trans (coordPairing_complexVector_coVector x y).symm + | ⟨vec, .down⟩ => fun g x y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| + (coordPairing_complexVector_coVector _ _).trans <| (dotProduct_SL2CRep g y x).trans <| + (coordPairing_complexVector_coVector y x).symm.trans (Module.Basis.coordPairing_comm _ _ _ _) + +end complexLorentz + +open complexLorentz in +/-- Complex Lorentz tensors: Weyl spinors and complex Lorentz vectors with the action of +`SL(2, ℂ)`, on the spaces, representations and bases of `Fermion` and `Lorentz`. -/ +noncomputable abbrev complexLorentz : Species ℂ SL(2, ℂ) Kind Idx where + V := IndexSpace + rep := indexRep + basis := indexBasis + invariant := coordPairing_rep + +namespace complexLorentz + +/-- An upper left-handed index, `ψ^α`. -/ +abbrev upL : Color Kind := ⟨left, .up⟩ + +/-- A lower left-handed index, `ψ_α`. -/ +abbrev downL : Color Kind := ⟨left, .down⟩ + +/-- An upper right-handed index, `ψ̄^α̇`. -/ +abbrev upR : Color Kind := ⟨right, .up⟩ + +/-- A lower right-handed index, `ψ̄_α̇`. -/ +abbrev downR : Color Kind := ⟨right, .down⟩ + +/-- An upper vector index, `v^μ`. -/ +abbrev up : Color Kind := ⟨vec, .up⟩ + +/-- A lower vector index, `w_μ`. -/ +abbrev down : Color Kind := ⟨vec, .down⟩ + +/-- Conjugation swaps `upL` and `upR` and fixes `up`. -/ +example : upL.bar = upR ∧ downR.bar = downL ∧ up.bar = up := ⟨rfl, rfl, rfl⟩ + +/-- The complex Lorentz tensors with the colors listed, as `ℂT[upL, downR]` for `T^α_β̇`. The colors +are read in the namespace `complexLorentz`. The notation is active under +`open scoped Tensors.complexLorentz`. -/ +scoped syntax (name := complexLorentzSyntax) "ℂT[" term,* "]" : term + +macro_rules (kind := complexLorentzSyntax) + | `(ℂT[$cs,*]) => `(Tensors.complexLorentz.Tensor (open Tensors.complexLorentz in ![$cs,*])) + +/-- `ℂT[upL, downR]` is the space of tensors `T^α_β̇`. -/ +example : ℂT[upL, downR] = complexLorentz.Tensor ![upL, downR] := rfl + +end complexLorentz + +/-! + +## B. The invariant forms + +-/ + +namespace complexLorentz + +/-- The invariant form at a color: `ε` at the Weyl colors and `η` at the vector colors. -/ +noncomputable def form : (c : Color Kind) → complexLorentz.V c ⊗[ℂ] complexLorentz.V c + | ⟨left, .up⟩ => LeftHandedWeyl.metricVal + | ⟨left, .down⟩ => DualLeftHandedWeyl.metricVal + | ⟨right, .up⟩ => RightHandedWeyl.metricVal + | ⟨right, .down⟩ => DualRightHandedWeyl.metricVal + | ⟨vec, .up⟩ => ComplexVector.metricVal + | ⟨vec, .down⟩ => ComplexCoVector.metricVal + +/-- The forms are invariant under `SL(2, ℂ)`. -/ +lemma form_invariant : ∀ (c : Color Kind) (g : SL(2, ℂ)), + TensorProduct.map (complexLorentz.rep c g) (complexLorentz.rep c g) (form c) = form c + | ⟨left, .up⟩, g => by + have h := (LinearMap.congr_fun (LeftHandedWeyl.metric.isIntertwining' g) 1).symm + simp [LeftHandedWeyl.metric_apply_one] at h + exact h + | ⟨left, .down⟩, g => by + have h := (LinearMap.congr_fun (DualLeftHandedWeyl.metric.isIntertwining' g) 1).symm + simp [DualLeftHandedWeyl.metric_apply_one] at h + exact h + | ⟨right, .up⟩, g => by + have h := (LinearMap.congr_fun (RightHandedWeyl.metric.isIntertwining' g) 1).symm + simp [RightHandedWeyl.metric_apply_one] at h + exact h + | ⟨right, .down⟩, g => by + have h := (LinearMap.congr_fun (DualRightHandedWeyl.metric.isIntertwining' g) 1).symm + simp [DualRightHandedWeyl.metric_apply_one] at h + exact h + | ⟨vec, .up⟩, g => by + exact ComplexVector.metricVal_invariant g + | ⟨vec, .down⟩, g => by + exact ComplexCoVector.metricVal_invariant g + +/-- The forms at `c` and at `c.dual` are inverse to each other: their matrices are `ε` and +`-ε`, or `η` and `η`. -/ +lemma form_inverse : ∀ c : Color Kind, + complexLorentz.toLin (form c) ∘ₗ complexLorentz.toLin (form c.dual) = LinearMap.id + | ⟨left, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) + (leftLeftToMatrix.apply_symm_apply (-metricRaw))).trans + (by simpa [metricRaw] using Matrix.one_fin_two.symm) + | ⟨left, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) (leftLeftToMatrix.apply_symm_apply (-metricRaw)) + (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw)).trans + (by simpa [metricRaw] using Matrix.one_fin_two.symm) + | ⟨right, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) (dualRightDualRightToMatrix.apply_symm_apply metricRaw) + (rightRightToMatrix.apply_symm_apply (-metricRaw))).trans + (by simpa [metricRaw] using Matrix.one_fin_two.symm) + | ⟨right, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) (rightRightToMatrix.apply_symm_apply (-metricRaw)) + (dualRightDualRightToMatrix.apply_symm_apply metricRaw)).trans + (by simpa [metricRaw] using Matrix.one_fin_two.symm) + | ⟨vec, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) (coCoToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) + (contrContrToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom))).trans + (by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)]) + | ⟨vec, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| + (congrArg₂ (· * ·) + (contrContrToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) + (coCoToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom))).trans + (by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)]) + +end complexLorentz + +/-- The invariant forms, `ε` at the Weyl kinds and `η` at the vector kind. -/ +noncomputable instance (κ : complexLorentz.Kind) : complexLorentz.HasForm κ where + formAt v := complexLorentz.form ⟨κ, v⟩ + formAt_invariant v := complexLorentz.form_invariant ⟨κ, v⟩ + formAt_inverse v := complexLorentz.form_inverse ⟨κ, v⟩ + +namespace complexLorentz + +/-- The form `ε^αβ` on two upper left-handed indices. -/ +scoped notation "εL" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.upL + +/-- The form `ε_αβ` on two lower left-handed indices. -/ +scoped notation "εL'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.downL + +/-- The form `ε^α̇β̇` on two upper right-handed indices. -/ +scoped notation "εR" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.upR + +/-- The form `ε_α̇β̇` on two lower right-handed indices. -/ +scoped notation "εR'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.downR + +/-- The metric `η^μν` on two upper vector indices. -/ +scoped notation "η" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.up + +/-- The metric `η_μν` on two lower vector indices. -/ +scoped notation "η'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.down + +/-- The delta `δ^μ_ν`, on the colors `![up, down]`. -/ +scoped notation "δ" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.down + +/-- The delta `δ_μ^ν`, on the colors `![down, up]`. -/ +scoped notation "δ'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.up + +/-- The delta `δ^α_β`, on the colors `![upL, downL]`. -/ +scoped notation "δL" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.downL + +/-- The delta `δ_α^β`, on the colors `![downL, upL]`. -/ +scoped notation "δL'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.upL + +/-- The delta `δ^α̇_β̇`, on the colors `![upR, downR]`. -/ +scoped notation "δR" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.downR + +/-- The delta `δ_α̇^β̇`, on the colors `![downR, upR]`. -/ +scoped notation "δR'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.upR + +end complexLorentz + +/-! + +## C. Conjugation + +-/ + +namespace complexLorentz + +/-- A label of a kind read as a label of its conjugate `κ'`, the same element of `Fin 2` or of +`Fin 1 ⊕ Fin 3`. -/ +def idxBar {κ κ' : Kind} (h : κ' = star κ) : Idx κ ≃ Idx κ' := + match κ, κ', h with + | left, _, rfl | right, _, rfl | vec, _, rfl => Equiv.refl _ + +/-- Reading a label as a label of the conjugate kind and back gives the label. -/ +lemma idxBar_idxBar {κ κ' : Kind} (h : κ' = star κ) (h' : κ = star κ') (μ : Idx κ) : + idxBar h' (idxBar h μ) = μ := by + subst h + cases κ <;> rfl + +/-- Conjugation swaps left- and right-handed indices and fixes vector indices, conjugating the +components. -/ +noncomputable instance : complexLorentz.HasConjugation := + .ofBasis complexLorentz idxBar idxBar_idxBar + +/-- Conjugation commutes with the action of `SL(2, ℂ)`. The right-handed representation is the +conjugate of the left-handed one, and the vector representation is real. -/ +instance : complexLorentz.IsEquivariantConjugation := + Species.HasConjugation.isEquivariantConjugation_ofBasis idxBar idxBar_idxBar fun κ g => by + match κ with + | left => + exact fun μ ν => (RightHandedWeyl.rep_apply_basis_repr g μ ν).trans + (congrArg star (LeftHandedWeyl.rep_apply_basis_repr g μ ν).symm) + | right => + exact fun μ ν => (LeftHandedWeyl.rep_apply_basis_repr g μ ν).trans <| (star_star _).symm.trans + (congrArg star (RightHandedWeyl.rep_apply_basis_repr g μ ν).symm) + | vec => + intro μ ν + change ComplexVector.basis.repr (ComplexVector.SL2CRep g (ComplexVector.basis μ)) ν = + star (ComplexVector.basis.repr (ComplexVector.SL2CRep g (ComplexVector.basis μ)) ν) + rw [← LinearMap.toMatrix_apply, ComplexVector.basis_ρ_apply, + LorentzGroup.star_toComplex_apply] + +/-- The components of the conjugate of a tensor are the conjugates of its components, at the same +labels read through `idxBar`. -/ +lemma tensorBasis_repr_conj {ι : Type} [Fintype ι] {c c' : ι → Color Kind} + (h : ∀ i, c' i = (c i).bar) (t : complexLorentz.Tensor c) (μ : Π i, Idx (c' i).kind) : + (complexLorentz.tensorBasis c').repr (Species.Tensor.conj h t) μ = + star ((complexLorentz.tensorBasis c).repr t + fun i => (idxBar (congrArg Color.kind (h i))).symm (μ i)) := + Species.Tensor.tensorBasis_repr_conj_of_basis h _ + (fun i => Species.HasConjugation.ofBasis_conjEquiv_basis _ _ (h i)) t μ + +end complexLorentz + +/-! + +## D. Components of the forms and the deltas + +-/ + +namespace complexLorentz + +/-- The entries of the form at each color, `ε^αβ = !![0, -1; 1, 0]` at the upper Weyl colors, +`ε_αβ = !![0, 1; -1, 0]` at the lower ones, and `η` at the vector colors. -/ +noncomputable def formEntries : (c : Color Kind) → Idx c.kind → Idx c.kind → ℂ + | ⟨left, .up⟩ => -metricRaw + | ⟨left, .down⟩ => metricRaw + | ⟨right, .up⟩ => -metricRaw + | ⟨right, .down⟩ => metricRaw + | ⟨vec, _⟩ => fun μ ν => minkowskiMatrix μ ν + +/-- The components of the form at any color are `formEntries`. -/ +lemma formMatrix_apply : ∀ (c : Color Kind) (μ ν : Idx c.kind), + Species.formMatrix complexLorentz c μ ν = formEntries c μ ν + | ⟨left, .up⟩, μ, ν => congrFun (congrFun (leftLeftToMatrix.apply_symm_apply (-metricRaw)) μ) ν + | ⟨left, .down⟩, μ, ν => + congrFun (congrFun (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) μ) ν + | ⟨right, .up⟩, μ, ν => congrFun (congrFun (rightRightToMatrix.apply_symm_apply (-metricRaw)) μ) ν + | ⟨right, .down⟩, μ, ν => + congrFun (congrFun (dualRightDualRightToMatrix.apply_symm_apply metricRaw) μ) ν + | ⟨vec, .up⟩, μ, ν => congrFun (congrFun (contrContrToMatrix.apply_symm_apply + ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν + | ⟨vec, .down⟩, μ, ν => congrFun (congrFun (coCoToMatrix.apply_symm_apply + ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν + +/-- The components of the form tensor at any color are `formEntries`, at the pair of labels +`(μ 0, μ 1)`. The labels `μ` form a dependent family, since the basis labels of a slot depend on +its color. -/ +lemma tensorBasis_repr_formTensor (c : Color Kind) (μ : Π i, Idx (![c, c] i).kind) : + (complexLorentz.tensorBasis ![c, c]).repr (complexLorentz.formTensor c) μ = + formEntries c (μ 0) (μ 1) := + (Species.tensorBasis_repr_formTensor c μ).trans (formMatrix_apply c _ _) + +/-- The components of `ε^αβ` are the matrix `!![0, -1; 1, 0]`. -/ +lemma tensorBasis_repr_formTensor_upL (μ : Π i, Idx (![upL, upL] i).kind) : + (complexLorentz.tensorBasis ![upL, upL]).repr εL μ = (-metricRaw) (μ 0) (μ 1) := + tensorBasis_repr_formTensor upL μ + +/-- The components of `ε_αβ` are the matrix `!![0, 1; -1, 0]`. -/ +lemma tensorBasis_repr_formTensor_downL (μ : Π i, Idx (![downL, downL] i).kind) : + (complexLorentz.tensorBasis ![downL, downL]).repr εL' μ = metricRaw (μ 0) (μ 1) := + tensorBasis_repr_formTensor downL μ + +/-- The components of `ε^α̇β̇` are the matrix `!![0, -1; 1, 0]`. -/ +lemma tensorBasis_repr_formTensor_upR (μ : Π i, Idx (![upR, upR] i).kind) : + (complexLorentz.tensorBasis ![upR, upR]).repr εR μ = (-metricRaw) (μ 0) (μ 1) := + tensorBasis_repr_formTensor upR μ + +/-- The components of `ε_α̇β̇` are the matrix `!![0, 1; -1, 0]`. -/ +lemma tensorBasis_repr_formTensor_downR (μ : Π i, Idx (![downR, downR] i).kind) : + (complexLorentz.tensorBasis ![downR, downR]).repr εR' μ = metricRaw (μ 0) (μ 1) := + tensorBasis_repr_formTensor downR μ + +/-- The components of `η^μν` and `η_μν` are the Minkowski matrix `diag(1, -1, -1, -1)`. -/ +lemma tensorBasis_repr_formTensor_vec (v : Variance) + (μ : Π i, Idx (![⟨vec, v⟩, ⟨vec, v⟩] i : Color Kind).kind) : + (complexLorentz.tensorBasis ![⟨vec, v⟩, ⟨vec, v⟩]).repr (complexLorentz.formTensor ⟨vec, v⟩) + μ = minkowskiMatrix (μ 0) (μ 1) := + tensorBasis_repr_formTensor ⟨vec, v⟩ μ + +/-- The components of the delta are those of the identity matrix, `δ^μ_ν = 1` for `μ = ν` and `0` +otherwise, at every color. -/ +lemma tensorBasis_repr_deltaTensor (c : Color Kind) + (μ : Π i, Idx (![c.dual, c] i).kind) : + (complexLorentz.tensorBasis ![c.dual, c]).repr (complexLorentz.deltaTensor c) μ = + (1 : Matrix (Idx c.kind) (Idx c.kind) ℂ) (μ 0) (μ 1) := by + rw [Species.tensorBasis_repr_deltaTensor_eq_one] + +/-- `η^00 = 1` and `η^11 = -1`: the signature is mostly minus. -/ +example : (complexLorentz.tensorBasis ![up, up]).repr η + (Fin.cons (Sum.inl 0 : Fin 1 ⊕ Fin 3) (Fin.cons (Sum.inl 0 : Fin 1 ⊕ Fin 3) (·.elim0))) = + 1 ∧ + (complexLorentz.tensorBasis ![up, up]).repr η + (Fin.cons (Sum.inr 0 : Fin 1 ⊕ Fin 3) (Fin.cons (Sum.inr 0 : Fin 1 ⊕ Fin 3) (·.elim0))) = + -1 := by + rw [tensorBasis_repr_formTensor_vec .up, tensorBasis_repr_formTensor_vec .up] + change ((minkowskiMatrix (Sum.inl 0) (Sum.inl 0) : ℝ) : ℂ) = 1 ∧ + ((minkowskiMatrix (Sum.inr 0) (Sum.inr 0) : ℝ) : ℂ) = -1 + simp + +end complexLorentz + +/-! + +## E. Symmetry of the forms + +-/ + +namespace complexLorentz + +/-- Swapping the two indices of the form transposes its matrix. -/ +lemma basis_tensorProduct_repr_comm_form (c : Color Kind) (μ ν : Idx c.kind) : + ((indexBasis c).tensorProduct (indexBasis c)).repr + (TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c)) (μ, ν) = + formEntries c ν μ := by + rw [Module.Basis.tensorProduct_repr_comm] + exact formMatrix_apply c ν μ + +/-- The Minkowski metric `η` on the vector kind is symmetric. -/ +instance : complexLorentz.IsSymmetricForm vec where + comm_form v := ((indexBasis ⟨vec, v⟩).tensorProduct (indexBasis ⟨vec, v⟩)).repr.injective + (Finsupp.ext fun ⟨μ, ν⟩ => by + rw [basis_tensorProduct_repr_comm_form] + change _ = Species.formMatrix complexLorentz _ μ ν + rw [formMatrix_apply] + change (minkowskiMatrix ν μ : ℂ) = minkowskiMatrix μ ν + (conv_lhs => rw [← minkowskiMatrix.eq_transpose]); rfl) + +/-- The form `ε` is antisymmetric at the Weyl colors. -/ +lemma comm_form_weyl (c : Color Kind) (h : c.kind ≠ vec) : + TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c) = -Species.form c := + ((indexBasis c).tensorProduct (indexBasis c)).repr.injective + (Finsupp.ext fun ⟨μ, ν⟩ => by + rw [basis_tensorProduct_repr_comm_form, map_neg, Finsupp.neg_apply] + change _ = -Species.formMatrix complexLorentz _ μ ν + rw [formMatrix_apply] + rcases c with ⟨_ | _ | _, _ | _, _ | _, h'⟩ <;> + first | exact absurd rfl h | exact absurd rfl h' | + (fin_cases μ <;> fin_cases ν <;> simp [formEntries, metricRaw])) + +/-- The form `ε` on the left-handed Weyl kind is antisymmetric. -/ +instance : complexLorentz.IsAntisymmetricForm left where + comm_form v := comm_form_weyl ⟨left, v⟩ nofun + +/-- The form `ε` on the right-handed Weyl kind is antisymmetric. -/ +instance : complexLorentz.IsAntisymmetricForm right where + comm_form v := comm_form_weyl ⟨right, v⟩ nofun + +/-- `ε^αβ` is antisymmetric, `ε^αβ = -ε^βα`. -/ +lemma formTensor_upL_antisymm : {εL | α β = -εL | β α}ᵀ := by + rw [map_neg, Species.formTensor_antisymm upL ?_, neg_neg] + exact Equiv.ext (by decide) + +/-- `ε_αβ` is antisymmetric, `ε_αβ = -ε_βα`. -/ +lemma formTensor_downL_antisymm : {εL' | α β = -εL' | β α}ᵀ := by + rw [map_neg, Species.formTensor_antisymm downL ?_, neg_neg] + exact Equiv.ext (by decide) + +/-- `ε^α̇β̇` is antisymmetric, `ε^α̇β̇ = -ε^β̇α̇`. -/ +lemma formTensor_upR_antisymm : {εR | α β = -εR | β α}ᵀ := by + rw [map_neg, Species.formTensor_antisymm upR ?_, neg_neg] + exact Equiv.ext (by decide) + +/-- `ε_α̇β̇` is antisymmetric, `ε_α̇β̇ = -ε_β̇α̇`. -/ +lemma formTensor_downR_antisymm : {εR' | α β = -εR' | β α}ᵀ := by + rw [map_neg, Species.formTensor_antisymm downR ?_, neg_neg] + exact Equiv.ext (by decide) + +/-- `η^μν` is symmetric, `η^μν = η^νμ`. -/ +lemma formTensor_up_symm : {η | μ ν = η | ν μ}ᵀ := by + rw [Species.formTensor_symm up ?_] + exact Equiv.ext (by decide) + +/-- `η_μν` is symmetric, `η_μν = η_νμ`. -/ +lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by + rw [Species.formTensor_symm down ?_] + exact Equiv.ext (by decide) + +/-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ +lemma deltaTensor_swap (c : Color Kind) : + {complexLorentz.deltaTensor c | μ ν = complexLorentz.deltaTensor c.dual | ν μ}ᵀ := by + refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) + rw [Species.tensorBasis_repr_reindex, tensorBasis_repr_deltaTensor, tensorBasis_repr_deltaTensor] + -- The labels on the right are casts along `rfl` of `μ 1` and `μ 0`. + exact congrFun (congrFun (Matrix.transpose_one (n := Idx c.kind) (α := ℂ)) (μ 1)) (μ 0) + +end complexLorentz + +/-! + +## F. Weyl spinors and complex vectors as tensors + +-/ + +namespace complexLorentz + +/-- A left-handed Weyl spinor `ψ^α` is a tensor with one upper left-handed index. -/ +noncomputable instance leftHandedWeylTensorial : Tensorial complexLorentz ![upL] LeftHandedWeyl := + Tensorial.ofIndexSpace complexLorentz upL + +/-- A dual left-handed Weyl spinor `ψ_α` is a tensor with one lower left-handed index. -/ +noncomputable instance dualLeftHandedWeylTensorial : + Tensorial complexLorentz ![downL] DualLeftHandedWeyl := + Tensorial.ofIndexSpace complexLorentz downL + +/-- A right-handed Weyl spinor `ψ̄^α̇` is a tensor with one upper right-handed index. -/ +noncomputable instance rightHandedWeylTensorial : + Tensorial complexLorentz ![upR] RightHandedWeyl := + Tensorial.ofIndexSpace complexLorentz upR + +/-- A dual right-handed Weyl spinor `ψ̄_α̇` is a tensor with one lower right-handed index. -/ +noncomputable instance dualRightHandedWeylTensorial : + Tensorial complexLorentz ![downR] DualRightHandedWeyl := + Tensorial.ofIndexSpace complexLorentz downR + +/-- A complex Lorentz vector `v^μ` is a tensor with one upper vector index. -/ +noncomputable instance complexVectorTensorial : Tensorial complexLorentz ![up] ComplexVector := + Tensorial.ofIndexSpace complexLorentz up + +/-- A complex Lorentz covector `w_μ` is a tensor with one lower vector index. -/ +noncomputable instance complexCoVectorTensorial : + Tensorial complexLorentz ![down] ComplexCoVector := + Tensorial.ofIndexSpace complexLorentz down + +/-- The action on a left-handed Weyl spinor is `LeftHandedWeyl.rep`. -/ +example (g : SL(2, ℂ)) (ψ : LeftHandedWeyl) : Tensorial.rep g ψ = LeftHandedWeyl.rep g ψ := rfl + +/-- The components of the tensor of `ψ^α` are its components in `LeftHandedWeyl.basis`. -/ +example (ψ : LeftHandedWeyl) (μ : Π i, Idx (![upL] i).kind) : + (complexLorentz.tensorBasis ![upL]).repr (Tensorial.toTensor ψ) μ = + LeftHandedWeyl.basis.repr ψ (μ 0) := by + exact Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upL ψ μ + +/-- The components `T^αβ̇` of the tensor of `T` in `LeftHandedWeyl ⊗ RightHandedWeyl` are the +entries of the matrix `leftRightToMatrix T`. -/ +lemma tensorBasis_repr_toTensor_leftRight (T : LeftHandedWeyl ⊗[ℂ] RightHandedWeyl) + (μ : Π i, Idx (prodColors ![upL] ![upR] i).kind) : + (complexLorentz.tensorBasis _).repr (Tensorial.toTensor T) μ = + leftRightToMatrix T (μ (.inl 0)) (μ (.inr 0)) := + Tensorial.tensorBasis_repr_toTensor_prod (S := complexLorentz) (c₁ := ![upL]) (c₂ := ![upR]) + LeftHandedWeyl.basis RightHandedWeyl.basis (fun ν => ν 0) (fun ν => ν 0) + (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upL) + (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upR) T μ + +/-- The pairing `ψ^α φ_α` of a spinor with a dual spinor is invariant. -/ +example (g : SL(2, ℂ)) (ψ : LeftHandedWeyl) (φ : DualLeftHandedWeyl) : + {Tensorial.rep g ψ | α ⊗ Tensorial.rep g φ | α}ᵀ = {ψ | α ⊗ φ | α}ᵀ := by + rw [Tensorial.toTensor_rep, Tensorial.toTensor_rep, Species.tmulEquiv_smul, + Species.intertwiningMap_smul, Species.smul_eq_self_of_isEmpty] + +end complexLorentz + +/-! + +## G. Examples + +-/ + +namespace complexLorentz + +/-- `ε^αβ ε_βγ = δ^α_γ`, by `contr_formTensor_formTensor`. -/ +example : {εL | α β ⊗ εL' | β γ = δL | α γ}ᵀ := by + refine Eq.trans ?_ (Species.contr_formTensor_formTensor upL) + congr 2 + ext x + fin_cases x <;> rfl + +/-- `g^αβ g_βγ = δ^α_γ` at every color: `ε`, `ε'` and `η` with their duals. -/ +example (c : Color Kind) : {complexLorentz.formTensor c | α β ⊗ + complexLorentz.formTensor c.dual | β γ = complexLorentz.deltaTensor c.dual | α γ}ᵀ := by + refine Eq.trans ?_ (Species.contr_formTensor_formTensor c) + congr 2 + ext x + fin_cases x <;> rfl + +/-- The components of `ε^αβ ⊗ ε^α̇β̇` are the products of the components of `εL` and `εR`. -/ +lemma tensorBasis_repr_formTensor_upL_tmul_upR + (μ : Π i, Idx (![upL, upL, upR, upR] i).kind) : + (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εR | α' β'}ᵀ μ = + (-metricRaw) (μ 0) (μ 1) * (-metricRaw) (μ 2) (μ 3) := by + rw [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, + tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] + rfl + +/-- `(ε^αβ ε_βγ)` at `α = γ = 0` is `1`, from the components of `εL` and `εL'`. -/ +example : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εL' | β γ}ᵀ + (Fin.cons (0 : Fin 2) (Fin.cons (0 : Fin 2) fun i => i.elim0)) = 1 := by + rw [Species.Tensor.tensorBasis_repr_contr_one_pair] + simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, + tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor downL] + change ∑ β : Fin 2, (-metricRaw) 0 β * metricRaw β 0 = 1 + simp [metricRaw, Fin.sum_univ_two] + +/-- The conjugate of `ε^αβ` is `ε^α̇β̇`. The conjugate lands on `![upR, upR]` directly. -/ +example : Species.Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by + refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) + rw [tensorBasis_repr_conj, tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] + -- The labels are `idxBar` along `rfl` of `μ 0` and `μ 1`, the identity. + have h (a b : Fin 2) : star ((-metricRaw) a b) = (-metricRaw) a b := by + fin_cases a <;> fin_cases b <;> simp [metricRaw] + exact h _ _ + +end complexLorentz + +end Tensors From 4db6652b10e23b35903c0728bef9e75108a906fc Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:19:11 +0000 Subject: [PATCH 019/242] feat(Elab): tensor_slots as an elab with an rw closing step, identity on either side; tensor_regroup reads equations through metadata Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 56 +++++++++++++++++++-------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 1915bcd425..31b51788a6 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -483,7 +483,7 @@ private def leadingProduct (e : Expr) (m n : Nat) : Expr := Id.run do /-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ private def contractedProducts : TacticM (Expr × Expr) := do - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let arg? (e : Expr) : Option Expr := let e := e.consumeMData @@ -601,7 +601,7 @@ contractions and relabelings are linear, so sums, negatives and scalars of `k` m The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a factor. -/ private def normalizeSums : TacticM Unit := do - let some (ty, _, _) := (← instantiateMVars (← getMainTarget)).eq? + let some (ty, _, _) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, @@ -618,7 +618,7 @@ private partial def summands (e : Expr) : List Expr := /-- Proves an equation of two summands with the same sign and the same scalar, by regrouping the contractions they multiply. -/ private partial def regroupSummand : TacticM Unit := do - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let l := l.consumeMData let r := r.consumeMData @@ -648,7 +648,7 @@ summand that is not the same term on both sides must be a contraction once relab absorbed. -/ elab "tensor_regroup" : tactic => do normalizeSums - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).eq? + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let ls := summands l let rs := summands r @@ -691,25 +691,47 @@ elab "tensor_regroup" : tactic => do -/ +/-- Whether `e` is a slotwise map `S.mapSlots F t` applied to a tensor. -/ +private def isMapSlots (e : Expr) : Bool := + let e := e.consumeMData + e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.mapSlots + /-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be `Fin n` with a literal `n`. -/ -macro "tensor_slots" : tactic => `(tactic| focus - simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, - Tensors.Species.mapSlots_mapSlots] - first - | refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext fun x => ?_) - | refine (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext fun x => ?_)).trans - (Tensors.Species.mapSlots_id _) - fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> - simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, - LinearMap.id_apply, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, - not_false_eq_true, dite_false, Representation.IntertwiningMap.coe_toLinearMap, - Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, - Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual]) +elab "tensor_slots" : tactic => do + evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, + Tensors.Species.mapSlots_mapSlots])) + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? + | throwError "tensor_slots: the goal is not an equation" + -- The branch is chosen from the two sides, since a failed unification with a side that is a + -- definition, such as a named tensor, unfolds it. + match isMapSlots l, isMapSlots r with + | true, true => evalTactic (← `(tactic| + refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_))) + | true, false => evalTactic (← `(tactic| + refine (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)).trans + (Tensors.Species.mapSlots_id _))) + | false, true => evalTactic (← `(tactic| + refine (Tensors.Species.mapSlots_id _).symm.trans + (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)))) + | false, false => throwError "tensor_slots: neither side lowers or raises an index" + evalTactic (← `(tactic| focus + intro x + fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> + simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, + LinearMap.id_apply, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, + not_false_eq_true, dite_false, Representation.IntertwiningMap.coe_toLinearMap, + Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, + Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual] <;> + -- On a species that is an `abbrev`, `simp` cannot rewrite under `toDual`: the types + -- unfold and their instances do not. `rw` checks its motive at default transparency. + (repeat rw [Tensors.Species.cast_apply]) <;> (repeat rw [cast_cast]) <;> + (repeat rw [cast_eq]) <;> (repeat rw [Tensors.Species.toDual_toDual]) <;> + (repeat rw [cast_cast]) <;> rfl)) end Tensors.Notation From 2a0ea6a621ff3e09112a82e119580037fb74ae72 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:19:11 +0000 Subject: [PATCH 020/242] feat(Metric): formMatrix_schouten Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 55204dde4d..df98918d3e 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -7,6 +7,7 @@ module public import Physlib.Mathematics.Tensors.Contraction public import Mathlib.LinearAlgebra.Matrix.BilinearForm +public import Mathlib.Tactic.LinearCombination /-! # Invariant forms, metrics, and lowering and raising indices @@ -68,7 +69,8 @@ cancel in any order. - `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, `T^…_ν… = T^…μ… g_μν`. - `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an - antisymmetric form on a kind with two labels. + antisymmetric form on a kind with two labels, and `Species.formMatrix_schouten` is the same in + components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. ## iii. Table of contents @@ -394,6 +396,25 @@ lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [ LinearMap.IsAlt.self_eq_zero H, hji] module +/-- The Schouten identity in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`: `schouten` at +the basis vectors `e^γ`, `e^ρ` and `e^α'`, read at the label `α`. -/ +lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + [NoZeroDivisors k] [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) + (γ ρ α' α : basisIdx c.kind) : + S.formMatrix c γ ρ * (if α = α' then 1 else 0) = + S.formMatrix c γ α' * (if ρ = α then 1 else 0) - + S.formMatrix c ρ α' * (if γ = α then 1 else 0) := by + have h := congrArg (fun x => (S.basis c.dual).repr x α) + (schouten c h2 (S.basis c.dual γ) (S.basis c.dual ρ) (S.basis c.dual α')) + have hg (μ ν : basisIdx c.kind) : + S.formBilin c (S.basis c.dual μ) (S.basis c.dual ν) = S.formMatrix c μ ν := by + rw [formMatrix_eq_toMatrix, LinearMap.BilinForm.toMatrix_apply] + have hm : S.formMatrix c α' γ = -S.formMatrix c γ α' := by + rw [← hg, ← hg, ← LinearMap.IsAlt.neg (isAlt_formBilin (S := S) c)] + simp only [map_add, map_smul, Finsupp.add_apply, Finsupp.smul_apply, Module.Basis.repr_self, + Finsupp.single_apply, smul_eq_mul, map_zero, Finsupp.zero_apply, hg, @eq_comm _ α' α] at h + linear_combination h - (if ρ = α then 1 else 0) * hm + end Species end Tensors From c4014c10e89eb35ba85c3f99215f150880183a1d Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:19:11 +0000 Subject: [PATCH 021/242] feat(ComplexTensor): schouten_downL, schouten_downR Co-authored-by: Claude Opus 5.5 --- .../Tensors/ComplexTensor/Species.lean | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index f1706d46ca..5b7108f2a2 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -9,7 +9,6 @@ public import Physlib.Mathematics.Tensors.Elab public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Relativity.Fermions.Weyl.Metric public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre -public import Physlib.Relativity.Tensors.ComplexTensor.Vector.Basic public import Physlib.Mathematics.ForMathlib.TensorProductMatrix /-! @@ -62,6 +61,8 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ `complexLorentz.tensorBasis_repr_deltaTensor` those of `δ`. - `complexLorentz.formTensor_upL_antisymm`, `complexLorentz.formTensor_up_symm` : `ε` is antisymmetric and `η` symmetric, and `complexLorentz.deltaTensor_swap` : `δ^μ_ν = δ_ν^μ`. +- `complexLorentz.schouten_downL` and `complexLorentz.schouten_downR` : the Schouten identity of + `ε` in index notation, from `Species.formMatrix_schouten`. - `complexLorentz.leftHandedWeylTensorial` and the other instances of section F : Weyl spinors and complex vectors as tensors with one slot. - `complexLorentz.tensorBasis_repr_toTensor_leftRight` : the components of a tensor in @@ -583,6 +584,27 @@ lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by rw [Species.formTensor_symm down ?_] exact Equiv.ext (by decide) +/-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_γα' δ_ρ^α - ε_ρα' δ_γ^α`. -/ +lemma schouten_downL : + {εL' | γ ρ ⊗ δL | α α' = εL' | γ α' ⊗ δL' | ρ α + -{εL' | ρ α' ⊗ δL' | γ α}ᵀ | ρ α' γ α}ᵀ := by + tensor_components [Species.tensorBasis_repr_formTensor, + fun c μ => (Species.tensorBasis_repr_deltaTensor_eq_one (S := complexLorentz) c μ).trans + Matrix.one_apply] + rw [← sub_eq_add_neg] + exact Species.formMatrix_schouten downL (Fintype.card_fin 2) _ _ _ _ + +/-- The Schouten identity for right-handed indices, +`ε_γ̇ρ̇ δ_α̇^α̇' = ε_γ̇α̇ δ_ρ̇^α̇' - ε_ρ̇α̇ δ_γ̇^α̇'`. -/ +lemma schouten_downR : + {εR' | γ ρ ⊗ δR' | α α' = εR' | γ α ⊗ δR' | ρ α' + -{εR' | ρ α ⊗ δR' | γ α'}ᵀ | ρ α γ α'}ᵀ := by + tensor_components [Species.tensorBasis_repr_formTensor, + fun c μ => (Species.tensorBasis_repr_deltaTensor_eq_one (S := complexLorentz) c μ).trans + Matrix.one_apply] + rw [← sub_eq_add_neg] + refine Eq.trans ?_ (Species.formMatrix_schouten downR (Fintype.card_fin 2) _ _ _ _) + congr 1 + split_ifs with h h' h' <;> first | rfl | exact absurd h.symm h' | exact absurd h'.symm h + /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : {complexLorentz.deltaTensor c | μ ν = complexLorentz.deltaTensor c.dual | ν μ}ᵀ := by From 70d157558827c3dbb6295365a7344c3fb928f9ba Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:19:11 +0000 Subject: [PATCH 022/242] feat(Bispinors): Pauli tensor with structural Fierz and anticommutators; ForMathlib LinearMap.comp_eq_id_comm_of_finrank_eq Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 2 + .../ForMathlib/FiniteDimensional.lean | 61 +++ .../Relativity/Bispinors/Pauli/Tensor.lean | 383 ++++++++++++++++++ 3 files changed, 446 insertions(+) create mode 100644 Physlib/Mathematics/ForMathlib/FiniteDimensional.lean create mode 100644 Physlib/Relativity/Bispinors/Pauli/Tensor.lean diff --git a/Physlib.lean b/Physlib.lean index 31dead9a09..ad2b8666d9 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -162,6 +162,7 @@ public import Physlib.Mathematics.ForMathlib.DataStructures.Matrix.LieTrace public import Physlib.Mathematics.ForMathlib.FDerivCurry public import Physlib.Mathematics.ForMathlib.Fin public import Physlib.Mathematics.ForMathlib.Fin.Involutions +public import Physlib.Mathematics.ForMathlib.FiniteDimensional public import Physlib.Mathematics.ForMathlib.HasTemperateGrowth public import Physlib.Mathematics.ForMathlib.Integration.Basic public import Physlib.Mathematics.ForMathlib.LinearMaps @@ -468,6 +469,7 @@ public import Physlib.QuantumMechanics.SpaceDQuantumSystem public import Physlib.Relativity.Bispinors.Basic public import Physlib.Relativity.Bispinors.Pauli.Basic public import Physlib.Relativity.Bispinors.Pauli.Relations +public import Physlib.Relativity.Bispinors.Pauli.Tensor public import Physlib.Relativity.Bispinors.Pauli.ToTensor public import Physlib.Relativity.Causality.Basic public import Physlib.Relativity.Causality.CausallyFollows diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean new file mode 100644 index 0000000000..ce77c056c9 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -0,0 +1,61 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.FiniteDimensional.Lemmas + +/-! + +# Left and right inverses between spaces of equal dimension + +A left inverse of a linear map between spaces of equal finite dimension is a right inverse. + +## i. Overview + +Mathlib proves `LinearMap.comp_eq_id_comm` for maps from a space to itself. Here the two maps go +between two spaces `V` and `W` of the same finite dimension: `g ∘ f = id` on `V` gives +`f ∘ g = id` on `W`. The map `f` is injective, so it is bijective, and `g` is its inverse. + +## ii. Key results + +- `LinearMap.comp_eq_id_comm_of_finrank_eq` : `f ∘ g = id ↔ g ∘ f = id` between spaces of equal + finite dimension. + +## iii. Table of contents + +- A. Left and right inverses + +## iv. References + +-/ + +@[expose] public section + +open Module + +variable {K V W : Type*} [DivisionRing K] [AddCommGroup V] [Module K V] [AddCommGroup W] + [Module K W] [FiniteDimensional K V] [FiniteDimensional K W] + +/-! + +## A. Left and right inverses + +-/ + +/-- Between spaces of equal finite dimension, a left inverse is a right inverse. -/ +lemma LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq (h : finrank K V = finrank K W) + {f : V →ₗ[K] W} {g : W →ₗ[K] V} (hgf : g ∘ₗ f = LinearMap.id) : f ∘ₗ g = LinearMap.id := by + have hinj : Function.Injective f := fun v v' e => by + simpa [← LinearMap.comp_apply, hgf] using congrArg g e + ext w + obtain ⟨v, rfl⟩ := (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 hinj w + simp [← LinearMap.comp_apply g f, hgf] + +/-- `LinearMap.comp_eq_id_comm` between two spaces of equal finite dimension. -/ +lemma LinearMap.comp_eq_id_comm_of_finrank_eq (h : finrank K V = finrank K W) {f : V →ₗ[K] W} + {g : W →ₗ[K] V} : f ∘ₗ g = LinearMap.id ↔ g ∘ₗ f = LinearMap.id := + ⟨LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h.symm, + LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h⟩ diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean new file mode 100644 index 0000000000..f5bf793e96 --- /dev/null +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -0,0 +1,383 @@ +/- +Copyright (c) 2024 Joseph Tooby-Smith. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Robert Sneiderman, Joseph Tooby-Smith, Andrea Pari +-/ +module + +public import Physlib.Relativity.Bispinors.Pauli.Basic +public import Physlib.Relativity.Tensors.ComplexTensor.Species +public import Physlib.Mathematics.ForMathlib.FiniteDimensional +/-! + +# The Pauli matrices as a complex Lorentz tensor + +The invariant tensor `σT^{μαβ̇}` of `SL(2, ℂ)`, its lowered forms, and its contraction identities. + +## i. Overview + +The Pauli matrices `σ^μ`, with `σ^0 = 1`, are a tensor with a vector index and a left- and a +right-handed Weyl index, `pauliTensor : ℂT[up, upL, upR]`. All three indices are upper, and the +components are `σT^{μαβ̇} = pauliMatrix μ α β`. Index heights in this file are those of the +colors: the numbers that the literature writes `σ^μ_{αβ̇}` sit here at upper Weyl indices. It is +the tensor of `ComplexVector.pauliVal` in `ComplexVector ⊗ (LeftHandedWeyl ⊗ RightHandedWeyl)`, +so it is invariant under `SL(2, ℂ)` because `ComplexVector.pauli` is an intertwining map. + +The lowered forms are written with `↓` in index notation, such as `{pauliTensor | ↓μ α β}ᵀ` for +`σT_μ^{αβ̇}`. Lowering contracts the first index of `η` or `ε` (`Species.toDual`), so +`σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, `{pauliTensor | μ ↓α ↓β}ᵀ` is +`σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix `σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. + +One identity is proved in components, the trace identity `pauliCoDown_trace_pauliCo`, +`tr(σ̄_μ σ_ν) = 2 η_μν`. The others are derived from it at tensor level, by `tensor_regroup` and +`tensor_slots`. The Fierz identity reads the trace identity as `B ∘ A = 2` for the maps `pauliA` +and `pauliB` between tensors with a vector index and tensors with two Weyl indices. Both spaces +have dimension `16`, so `A ∘ B = 2` as well, which is the Fierz identity. The anticommutators +follow from the trace identities and the Schouten identity of `ε`. + +## ii. Key results + +- `complexLorentz.pauliTensor` : the Pauli matrices as a tensor `σT^{μαβ̇}`. +- `complexLorentz.tensorBasis_repr_pauliTensor` : its components are the Pauli matrices. +- `complexLorentz.pauliTensor_invariant` : `σ` is an invariant tensor of `SL(2, ℂ)`. +- `complexLorentz.pauliCoDown_trace_pauliCo`, `complexLorentz.pauliContr_trace_pauliContrDown` + and `complexLorentz.pauliContrDown_trace_pauliContr` : the trace identities + `tr(σ̄_μ σ_ν) = 2 η_μν` and `tr(σ^μ σ̄^ν) = tr(σ̄^μ σ^ν) = 2 η^μν`. +- `complexLorentz.pauliA_pauliB` : the maps `pauliA` and `pauliB` are inverse up to `2`. +- `complexLorentz.pauliCo_contr_pauliContr` : the Fierz identity + `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`. +- `complexLorentz.pauliContr_mul_pauliContrDown_add` and + `complexLorentz.pauliContrDown_pauliContr_mul_add` : the anticommutators + `σ^μ σ̄^ν + σ^ν σ̄^μ = 2 η^μν` and `σ̄^μ σ^ν + σ̄^ν σ^μ = 2 η^μν`. + +## iii. Table of contents + +- A. The Pauli tensor +- B. Lowered indices +- C. Contraction identities + - C.1. Traces + - C.2. The Fierz identity + - C.3. The anticommutators + +## iv. References + +* Dreiner, Haber and Martin, *Two-component spinor techniques and Feynman rules for quantum + field theory and supersymmetry*, arXiv:0812.1594, in the conventions + `g = diag(+1, -1, -1, -1)`. [ref: Dreiner:2008tw] + +-/ + +@[expose] public section + +open TensorProduct Fermion Lorentz MatrixGroups +open PauliMatrix (pauliMatrix pauliBasis pauliSelfAdjoint) + +namespace Tensors.complexLorentz + +/-! + +## A. The Pauli tensor + +-/ + +/-- The Pauli matrices as the complex Lorentz tensor `σT^{μαβ̇}`, the tensor of +`ComplexVector.pauliVal`. -/ +noncomputable def pauliTensor : ℂT[up, upL, upR] := {ComplexVector.pauliVal | μ α β}ᵀ + +/-- `σT` is the Pauli tensor `pauliTensor`, to be distinguished from the Pauli matrices `σ`. -/ +scoped[Tensors.complexLorentz] notation "σT" => pauliTensor + +/-- The components of `σT^{μαβ̇}` are the entries of the Pauli matrices. -/ +lemma tensorBasis_repr_pauliTensor (μ : Π i, Idx (![up, upL, upR] i).kind) : + (complexLorentz.tensorBasis _).repr pauliTensor μ = pauliMatrix (μ 0) (μ 1) (μ 2) := by + rw [pauliTensor, Species.tensorBasis_repr_reindex, ComplexVector.pauliVal, map_sum, map_sum, + Finsupp.finsetSum_apply] + simp only [Tensorial.toTensor_tmul, Species.tensorBasis_repr_tmulEquiv, + tensorBasis_repr_toTensor_leftRight, LinearEquiv.apply_symm_apply] + refine (Finset.sum_congr rfl fun i _ => congrArg (· * _) + (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) up _ _)).trans ?_ + -- The labels are casts along `rfl`, so the identity is stated for plain labels. + have h (a : Fin 1 ⊕ Fin 3) (b c : Fin 2) : + ∑ i, ComplexVector.basis.repr (ComplexVector.basis i) a * (pauliBasis i).1 b c = + pauliMatrix a b c := by + rw [Finset.sum_eq_single a (fun i _ hi => by + rw [Module.Basis.repr_self, Finsupp.single_eq_of_ne hi.symm, zero_mul]) (by simp), + Module.Basis.repr_self, Finsupp.single_eq_same, one_mul] + simp [pauliBasis, pauliSelfAdjoint] + exact h _ _ _ + +/-- `σT^{μαβ̇}` is an invariant tensor of `SL(2, ℂ)`, `g • σT = σT`. -/ +lemma pauliTensor_invariant (g : SL(2, ℂ)) : g • pauliTensor = pauliTensor := by + rw [pauliTensor, ← Species.equiv_smul, ← Tensorial.toTensor_rep, ← ComplexVector.pauli_apply_one] + exact congrArg _ (congrArg _ (ComplexVector.pauli.map_invariant (v := 1) rfl)) + +/-! + +## B. Lowered indices + +-/ + +/-- The components of `σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. -/ +lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).kind) : + (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν α β}ᵀ μ = + minkowskiMatrix (μ 0) (μ 0) * pauliMatrix (μ 0) (μ 1) (μ 2) := by + have h (a : Fin 1 ⊕ Fin 3) (b c : Fin 2) : ∑ x, (minkowskiMatrix x a : ℂ) * pauliMatrix x b c = + minkowskiMatrix a a * pauliMatrix a b c := by + rw [Finset.sum_eq_single a (fun x _ hx => by rw [minkowskiMatrix.off_diag_zero hx]; simp) + (by simp)] + tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, + tensorBasis_repr_pauliTensor] + exact h _ _ _ + +/-- The components of `σT^μ_{αβ̇}`, with both Weyl indices lowered: `η_μμ σT^{μβα}` at the labels +`(μ, α, β)`. -/ +lemma tensorBasis_repr_pauliTensor_downWeyl + (μ : Π i, Idx (![up, downL, downR] i).kind) : + (complexLorentz.tensorBasis _).repr {pauliTensor | ν ↓α ↓β}ᵀ μ = + minkowskiMatrix (μ 0) (μ 0) * pauliMatrix (μ 0) (μ 2) (μ 1) := by + have h (m : Fin 1 ⊕ Fin 3) (a b : Fin 2) : ∑ x, metricRaw x b * ∑ y, metricRaw y a * + pauliMatrix m y x = minkowskiMatrix m m * pauliMatrix m b a := by + rcases m with m | m <;> fin_cases m <;> fin_cases a <;> fin_cases b <;> + simp [Fin.sum_univ_two, metricRaw, pauliMatrix, minkowskiMatrix.inl_0_inl_0, + minkowskiMatrix.inr_i_inr_i] + tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, + tensorBasis_repr_pauliTensor] + exact h _ _ _ + +/-- The components of `σT_{μαβ̇}`, with all indices lowered: `σT^{μβα}` at the labels +`(μ, α, β)`. -/ +lemma tensorBasis_repr_pauliTensor_downAll + (μ : Π i, Idx (![down, downL, downR] i).kind) : + (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν ↓α ↓β}ᵀ μ = + pauliMatrix (μ 0) (μ 2) (μ 1) := by + have h (m : Fin 1 ⊕ Fin 3) (a b : Fin 2) : ∑ x, metricRaw x b * ∑ y, metricRaw y a * + ∑ z, (minkowskiMatrix z m : ℂ) * pauliMatrix z y x = pauliMatrix m b a := by + rcases m with m | m <;> fin_cases m <;> fin_cases a <;> fin_cases b <;> + simp [Fin.sum_univ_two, Fintype.sum_sum_type, Fin.sum_univ_three, metricRaw, pauliMatrix, + minkowskiMatrix.as_diagonal, Matrix.diagonal_apply] + tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, + tensorBasis_repr_pauliTensor] + exact h _ _ _ + +/-- `σT^μ_{αβ̇} = σT_{ραβ̇} η^ρμ`, raising the vector index of the fully lowered Pauli tensor. -/ +lemma pauliContrDown_eq_pauliCoDown_contr : + {pauliTensor | μ ↓α ↓β = {pauliTensor | ↓ρ ↓α ↓β ⊗ η | ρ μ}ᵀ | α β μ}ᵀ := by + -- Write `μ` as lowered and raised again, and the raising as a contraction with `η`. + have h : {pauliTensor | μ ↓α ↓β = {pauliTensor | ↓μ ↓α ↓β}ᵀ | ↑μ α β}ᵀ := by + tensor_slots + rw [h, Species.toDualAt_eq_contr] + tensor_regroup + +/-! + +## C. Contraction identities + +The trace identity `pauliCoDown_trace_pauliCo` is proved in components, from the entries of the +Pauli matrices. The other identities are derived from it at tensor level: the Fierz identity by +reading the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB`, and the +anticommutators by the Schouten identity of `ε`. + +### C.1. Traces + +-/ + +/-- The trace identity `tr(σ̄_μ σ_ν) = 2 η_μν`, as `σT_{μαβ̇} σT_ν^{αβ̇} = 2 η_μν`. -/ +lemma pauliCoDown_trace_pauliCo : + {pauliTensor | ↓μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • η' | μ ν}ᵀ := by + tensor_components [tensorBasis_repr_pauliTensor_downAll, tensorBasis_repr_pauliTensor_down, + tensorBasis_repr_formTensor_vec .down] + generalize φ 0 = m, φ 1 = n + rcases m with m | m <;> rcases n with n | n <;> fin_cases m <;> fin_cases n <;> + simp [Fin.sum_univ_two, pauliMatrix, minkowskiMatrix.inl_0_inl_0, + minkowskiMatrix.inr_i_inr_i, minkowskiMatrix.off_diag_zero] <;> ring_nf + +/-- The trace identity `tr(σ_μ σ̄_ν) = 2 η_μν`, as `σT_μ^{αβ̇} σT_{ναβ̇} = 2 η_μν`. -/ +lemma pauliCo_trace_pauliCoDown : + {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = (2 : ℂ) • η' | μ ν}ᵀ := by + have h : {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = + {pauliTensor | ↓ν ↓α ↓β ⊗ pauliTensor | ↓μ α β}ᵀ | ν μ}ᵀ := by + tensor_regroup + rw [h, pauliCoDown_trace_pauliCo, map_smul, ← formTensor_down_symm] + +/-- The mixed trace identity `tr(σ̄^μ σ_ν) = 2 δ^μ_ν`, as `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ^μ_ν`. -/ +lemma pauliContrDown_trace_pauliCo : + {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • δ' | ν μ}ᵀ := by + have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = + {pauliTensor | ↓ρ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | ρ ν ⊗ η | ρ μ}ᵀ := by + rw [pauliContrDown_eq_pauliCoDown_contr] + tensor_regroup + rw [h, pauliCoDown_trace_pauliCo] + simp only [map_smul, ← TensorProduct.smul_tmul'] + congr 1 + conv_lhs => rw [formTensor_down_symm] + rw [← (Species.contr_formTensor_formTensor (S := complexLorentz) down : + _ = complexLorentz.deltaTensor up)] + tensor_regroup + +/-! + +### C.2. The Fierz identity + +-/ + +/-- `p ↦ σT_μ^{αβ̇} p^{μα'β̇'}`, from a tensor with a vector index to one with two Weyl indices. -/ +noncomputable def pauliA : ℂT[up, upL, upR] →ₗ[ℂ] ℂT[upL, upR, upL, upR] where + toFun p := {pauliTensor | ↓μ α β ⊗ p | μ α' β'}ᵀ + map_add' p q := by simp only [tmul_add, map_add] + map_smul' r p := by simp only [tmul_smul, map_smul, RingHom.id_apply] + +/-- `X ↦ σT^μ_{αβ̇} X^{αβ̇α'β̇'}`, from a tensor with two Weyl indices to one with a vector +index. -/ +noncomputable def pauliB : ℂT[upL, upR, upL, upR] →ₗ[ℂ] ℂT[up, upL, upR] where + toFun X := {pauliTensor | μ ↓α ↓β ⊗ X | α β α' β'}ᵀ + map_add' X Y := by simp only [tmul_add, map_add] + map_smul' r X := by simp only [tmul_smul, map_smul, RingHom.id_apply] + +/-- `pauliB ∘ pauliA = 2`, by the trace identity `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ_ν^μ`. -/ +lemma pauliB_pauliA (p : ℂT[up, upL, upR]) : pauliB (pauliA p) = (2 : ℂ) • p := by + have h : pauliB (pauliA p) = + {{pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | μ ν ⊗ p | ν α' β'}ᵀ := by + simp only [pauliA, pauliB, LinearMap.coe_mk, AddHom.coe_mk] + tensor_regroup + rw [h, pauliContrDown_trace_pauliCo] + conv_rhs => rw [← Species.Tensor.contr_deltaTensor (0 : Fin 3) up rfl p] + tensor_regroup + +/-- `pauliA ∘ pauliB = 2`: both spaces have dimension `16`, so the left inverse `pauliB / 2` of +`pauliA` is a right inverse. -/ +lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : ℂ) • X := by + have := Module.Finite.of_basis (complexLorentz.tensorBasis ![up, upL, upR]) + have := Module.Finite.of_basis (complexLorentz.tensorBasis ![upL, upR, upL, upR]) + have hdim : Module.finrank ℂ ℂT[up, upL, upR] = Module.finrank ℂ ℂT[upL, upR, upL, upR] := by + rw [Module.finrank_eq_card_basis (complexLorentz.tensorBasis _), + Module.finrank_eq_card_basis (complexLorentz.tensorBasis _)] + simp [Fintype.card_pi, Fin.prod_univ_succ] + rfl + have h : ((2 : ℂ)⁻¹ • pauliB) ∘ₗ pauliA = LinearMap.id := LinearMap.ext fun p => by + simp [pauliB_pauliA] + have h' := LinearMap.congr_fun ((LinearMap.comp_eq_id_comm_of_finrank_eq hdim).2 h) X + simp only [LinearMap.comp_apply, LinearMap.smul_apply, map_smul, LinearMap.id_apply] at h' + exact (inv_smul_eq_iff₀ two_ne_zero).1 h' + +/-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ +lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α ↑β}ᵀ = pauliTensor := by + tensor_slots + +/-- `σT^μ_{αβ̇} ε^{αα'} ε^{β̇β̇'} = σT^{μα'β̇'}`. -/ +lemma pauliContrDown_contr_eps : + {pauliTensor | μ ↓α ↓β ⊗ εL | α α' ⊗ εR | β β' = pauliTensor | μ α' β'}ᵀ := by + conv_rhs => rw [← pauliTensor_raise_lower, Species.toDualAt_eq_contr, Species.toDualAt_eq_contr] + tensor_regroup + +/-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is +`pauliA σT = pauliA (pauliB (εL ⊗ εR)) / 2`. -/ +lemma pauliCo_contr_pauliContr : + {pauliTensor | ↓ν α β ⊗ pauliTensor | ν α' β' = + (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by + rw [map_smul, ← pauliA_pauliB] + refine congrArg pauliA ?_ + conv_lhs => rw [← pauliContrDown_contr_eps] + simp only [pauliB, LinearMap.coe_mk, AddHom.coe_mk] + tensor_regroup + +/-! + +### C.3. The anticommutators + +-/ + +/-- `σT^{μαβ̇} = σT_ρ^{αβ̇} η^{ρμ}`, raising the lowered vector index again. -/ +lemma pauliTensor_eq_pauliCo_contr : {pauliTensor | μ α β = pauliTensor | ↓ρ α β ⊗ η | ρ μ}ᵀ := by + have h : {pauliTensor | μ α β = {pauliTensor | ↓μ α β}ᵀ | ↑μ α β}ᵀ := by + tensor_slots + conv_lhs => rw [h, Species.toDualAt_eq_contr] + tensor_regroup + +/-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{αβ̇} = 2 η^μν`. -/ +lemma pauliContr_trace_pauliContrDown : + {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = (2 : ℂ) • η | μ ν}ᵀ := by + have h : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = + {pauliTensor | ν ↓α ↓β ⊗ pauliTensor | ↓ρ α β}ᵀ | ν ρ ⊗ η | ρ μ}ᵀ := by + nth_rw 1 [pauliTensor_eq_pauliCo_contr] + tensor_regroup + rw [h, pauliContrDown_trace_pauliCo] + conv_rhs => rw [formTensor_up_symm, ← Species.Tensor.contr_deltaTensor (0 : Fin 2) up rfl η] + tensor_regroup + +/-- The trace identity `tr(σ̄^μ σ^ν) = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇} = 2 η^μν`. -/ +lemma pauliContrDown_trace_pauliContr : + {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = (2 : ℂ) • η | μ ν}ᵀ := by + have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = + {pauliTensor | ν α β ⊗ pauliTensor | μ ↓α ↓β}ᵀ | ν μ}ᵀ := by + tensor_regroup + rw [h, pauliContr_trace_pauliContrDown, map_smul, ← formTensor_up_symm] + +/-- `σT^{μαβ̇} = σT^{μρβ̇} δ_ρ^α`. -/ +lemma pauliTensor_eq_contr_deltaL : {pauliTensor | μ α β = pauliTensor | μ ρ β ⊗ δL' | ρ α}ᵀ := by + conv_lhs => rw [← Species.Tensor.contr_deltaTensor (1 : Fin 3) upL rfl pauliTensor] + tensor_regroup + +/-- `σT^{μαβ̇} = σT^{μαρ̇} δ_ρ̇^β̇`. -/ +lemma pauliTensor_eq_contr_deltaR : {pauliTensor | μ α β = pauliTensor | μ α r ⊗ δR' | r β}ᵀ := by + conv_lhs => rw [← Species.Tensor.contr_deltaTensor (2 : Fin 3) upR rfl pauliTensor] + tensor_regroup + +/-- `σT^{μρβ̇} σT^{νγδ̇} ε_{δ̇β̇} (ε_{γρ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity +`pauliContr_trace_pauliContrDown` with the lowerings written as contractions with `ε`. -/ +lemma pauliContr_pauliContr_eps_epsL_deltaL : + {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ + {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by + have h : {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ + {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = + {pauliTensor | μ r β ⊗ pauliTensor | ν ↓r ↓β}ᵀ | μ ν ⊗ δL | α α'}ᵀ := by + simp only [Species.toDualAt_eq_contr] + tensor_regroup + rw [h, pauliContr_trace_pauliContrDown, ← smul_tmul', map_smul, map_smul] + +/-- The anticommutator `σ^μ σ̄^ν + σ^ν σ̄^μ = 2 η^μν`, as +`σT^{μαβ̇} σT^ν_{α'β̇} + σT^{ναβ̇} σT^μ_{α'β̇} = 2 η^μν δ^α_α'`. The Schouten identity splits +`ε_γρ δ^α_α'` in `pauliContr_pauliContr_eps_epsL_deltaL` into the two terms. -/ +lemma pauliContr_mul_pauliContrDown_add : + {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α' ↓β + pauliTensor | ν α β ⊗ pauliTensor | μ ↓α' ↓β = + (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by + rw [← pauliContr_pauliContr_eps_epsL_deltaL, schouten_downL, map_add, tmul_add, map_add, + map_add, map_add] + refine congrArg₂ (· + ·) ?_ ?_ + · nth_rw 1 [pauliTensor_eq_contr_deltaL] + simp only [Species.toDualAt_eq_contr] + tensor_regroup + · nth_rw 1 [pauliTensor_eq_contr_deltaL] + rw [formTensor_downR_antisymm] + simp only [Species.toDualAt_eq_contr] + tensor_regroup + +/-- `σT^{μγδ̇} σT^{ναρ̇} ε_{γα} (ε_{δ̇ρ̇} δ_β̇^β̇') = 2 η^μν δ_β̇^β̇'`, the trace identity +`pauliContrDown_trace_pauliContr` with the lowerings written as contractions with `ε`. -/ +lemma pauliContr_pauliContr_eps_epsR_deltaR : + {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ + {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by + have h : {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ + {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = + {pauliTensor | μ ↓a ↓r ⊗ pauliTensor | ν a r}ᵀ | μ ν ⊗ δR' | β β'}ᵀ := by + simp only [Species.toDualAt_eq_contr] + tensor_regroup + rw [h, pauliContrDown_trace_pauliContr, ← smul_tmul', map_smul, map_smul] + +/-- The anticommutator `σ̄^μ σ^ν + σ̄^ν σ^μ = 2 η^μν`, as +`σT^μ_{αβ̇} σT^{ναβ̇'} + σT^ν_{αβ̇} σT^{μαβ̇'} = 2 η^μν δ_β̇^β̇'`. The Schouten identity splits +`ε_δ̇ρ̇ δ_β̇^β̇'` in `pauliContr_pauliContr_eps_epsR_deltaR` into the two terms. -/ +lemma pauliContrDown_pauliContr_mul_add : + {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β' + pauliTensor | ν ↓α ↓β ⊗ pauliTensor | μ α β' = + (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by + rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR, map_add, tmul_add, map_add, + map_add, map_add] + refine congrArg₂ (· + ·) ?_ ?_ + · nth_rw 2 [pauliTensor_eq_contr_deltaR] + simp only [Species.toDualAt_eq_contr] + tensor_regroup + · nth_rw 2 [pauliTensor_eq_contr_deltaR] + rw [formTensor_downL_antisymm] + simp only [Species.toDualAt_eq_contr] + tensor_regroup + +end Tensors.complexLorentz From abdb74727c71a1acd5bbfee36f3aa27d1aaf05e1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:39:07 +0000 Subject: [PATCH 023/242] =?UTF-8?q?feat(Tensor):=20ext=5Fpure=E2=82=82,=20?= =?UTF-8?q?ext=5Fpure=E2=82=83,=20equiv=5Fext=5Fpure,=20intertwiningMap=5F?= =?UTF-8?q?ext=5Fpure;=20remove=20tensorBasis=5Frepr=5Fsmul=5FtensorBasis,?= =?UTF-8?q?=20smul=5FtensorBasis=5Feq=5Fsum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 54 +++++++++++++++++-------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 9a4ab75429..f79dddeef4 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -52,7 +52,10 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.Tensor` and `Species.pure` are the tensors and the pure tensors, and `Species.inductionOn_pure` is induction through pure tensors. -- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. +- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal, + `Species.ext_pure₂` and `Species.ext_pure₃` do the same for maps out of products of two and + three spaces of tensors, and `Species.equiv_ext_pure` and `Species.intertwiningMap_ext_pure` for + maps of representations. - `Species.smul_pure` proves that the group acts on a pure tensor slot by slot. - `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure tensors commutes with it on all tensors. @@ -149,6 +152,23 @@ lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : S.Tensor c →ₗ (h : ∀ p : Π i, S.V (c i), f (S.pure p) = g (S.pure p)) : f = g := PiTensorProduct.ext (MultilinearMap.ext h) +/-- Two linear maps out of `S.Tensor c ⊗ S.Tensor c₂` are equal when they agree on the products +`S.pure p ⊗ₜ S.pure q` of pure tensors. -/ +lemma ext_pure₂ {ι₂ : Type} {c₂ : ι₂ → Color K} {E : Type} [AddCommGroup E] [Module k E] + {f g : S.Tensor c ⊗[k] S.Tensor c₂ →ₗ[k] E} + (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)), + f (S.pure p ⊗ₜ S.pure q) = g (S.pure p ⊗ₜ S.pure q)) : f = g := + TensorProduct.ext <| ext_pure fun p => ext_pure fun q => h p q + +/-- Two linear maps out of `(S.Tensor c ⊗ S.Tensor c₂) ⊗ S.Tensor c₃` are equal when they agree +on the products `(S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r` of pure tensors. -/ +lemma ext_pure₃ {ι₂ ι₃ : Type} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} {E : Type} + [AddCommGroup E] [Module k E] {f g : (S.Tensor c ⊗[k] S.Tensor c₂) ⊗[k] S.Tensor c₃ →ₗ[k] E} + (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)) (r : Π l, S.V (c₃ l)), + f ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r) = g ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r)) : + f = g := + TensorProduct.ext <| ext_pure₂ fun p q => ext_pure fun r => h p q r + /-! ## B. The group action @@ -230,6 +250,20 @@ lemma intertwiningMapOfPure_apply (f : S.Tensor c →ₗ[k] S.Tensor c') (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) (t : S.Tensor c) : intertwiningMapOfPure f h t = f t := rfl +/-- Two equivalences of representations out of tensors are equal when they agree on pure +tensors. -/ +lemma equiv_ext_pure {W : Type} [AddCommGroup W] [Module k W] {ρ : Representation k G W} + {φ ψ : (S.tensorRep c).Equiv ρ} (h : ∀ p : Π i, S.V (c i), φ (S.pure p) = ψ (S.pure p)) : + φ = ψ := + Representation.Equiv.ext <| + funext <| LinearMap.congr_fun (ext_pure (f := φ.toLinearMap) (g := ψ.toLinearMap) h) + +/-- Two intertwining maps out of tensors are equal when they agree on pure tensors. -/ +lemma intertwiningMap_ext_pure {W : Type} [AddCommGroup W] [Module k W] + {ρ : Representation k G W} {φ ψ : (S.tensorRep c).IntertwiningMap ρ} + (h : ∀ p : Π i, S.V (c i), φ (S.pure p) = ψ (S.pure p)) : φ = ψ := + Representation.IntertwiningMap.ext (ext_pure h) + /-- A tensor with no slots is invariant, `Λ s = s`. -/ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = t := by induction t using inductionOn_pure with @@ -260,21 +294,6 @@ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] -/-- The coefficient of `e_ψ` in `g • e_φ` is the product over the indices of the matrix entries of -`g` in the color of that index. -/ -lemma tensorBasis_repr_smul_tensorBasis [Fintype ι] (g : G) (φ ψ : Π i, basisIdx (c i).kind) : - (S.tensorBasis c).repr (g • S.tensorBasis c φ) ψ = - ∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) (S.rep (c i) g) (ψ i) (φ i) := by - simp [tensorBasis_apply, tensorBasis_repr_pure, LinearMap.toMatrix_apply] - -/-- The action of `g` on a basis tensor, `g e_φ = ∑ ψ, (∏ i, g^{ψ i}_{φ i}) e_ψ`, with one matrix -entry of `g` per index, in the color of that index. -/ -lemma smul_tensorBasis_eq_sum [Fintype ι] [DecidableEq ι] (g : G) (φ : Π i, basisIdx (c i).kind) : - g • S.tensorBasis c φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) - (S.rep (c i) g) (ψ i) (φ i)) • S.tensorBasis c ψ := by - conv_lhs => rw [← (S.tensorBasis c).sum_repr (g • S.tensorBasis c φ)] - simp_rw [tensorBasis_repr_smul_tensorBasis] - /-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of `g` per index, in the color of that index. -/ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor c) @@ -283,7 +302,8 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor (S.basis (c i)) (S.rep (c i) g) (φ i) (ψ i)) * (S.tensorBasis c).repr t ψ := by conv_lhs => rw [← (S.tensorBasis c).sum_repr t, Finset.smul_sum] simp_rw [smul_comm g, map_sum, map_smul, Finsupp.coe_finsetSum, Finset.sum_apply, - Finsupp.smul_apply, tensorBasis_repr_smul_tensorBasis, smul_eq_mul, mul_comm] + Finsupp.smul_apply, tensorBasis_apply, smul_pure, tensorBasis_repr_pure, + LinearMap.toMatrix_apply, smul_eq_mul, mul_comm] /-! From d13cc22806e40a6eeca038da942d3952620836a4 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:39:07 +0000 Subject: [PATCH 024/242] refactor(Tensor): slotMapsAt, mapAt_comp, mapAt_id, mapSlots_comp, mapSlots_id_apply; congrAt from mapAt; remove congrAt_pure mapAt is mapSlots of slotMapsAt, and congrAt is the equivalence of representations whose linear map is mapAt, so congrAt_eq_mapAt and congrAt_symm hold by rfl. mapSlots_id is now an equality of maps. toDualAt_pure uses mapAt_pure, and tensor_slots uses the slotMapsAt lemmas. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 9 +- Physlib/Mathematics/Tensors/Metric.lean | 2 +- Physlib/Mathematics/Tensors/Tensor.lean | 177 ++++++++++++++---------- 3 files changed, 109 insertions(+), 79 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 31b51788a6..c50962b852 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -714,17 +714,18 @@ elab "tensor_slots" : tactic => do refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_))) | true, false => evalTactic (← `(tactic| refine (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)).trans - (Tensors.Species.mapSlots_id _))) + (Tensors.Species.mapSlots_id_apply _))) | false, true => evalTactic (← `(tactic| - refine (Tensors.Species.mapSlots_id _).symm.trans + refine (Tensors.Species.mapSlots_id_apply _).symm.trans (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)))) | false, false => throwError "tensor_slots: neither side lowers or raises an index" evalTactic (← `(tactic| focus intro x fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, - LinearMap.id_apply, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, - not_false_eq_true, dite_false, Representation.IntertwiningMap.coe_toLinearMap, + LinearMap.id_apply, Tensors.Species.slotMapsAt_apply_self, + Tensors.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, + Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual] <;> -- On a species that is an `abbrev`, `simp` cannot rewrite under `toDual`: the types diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index df98918d3e..6c80c8f60f 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -311,7 +311,7 @@ lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a (hqi : q i = S.cast hi.symm (S.toDual a (S.cast ha (p i)))) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : S.toDualAt i a ha hi h (S.pure p) = S.pure q := - congrAt_pure i _ h p q hqi hq + mapAt_pure i _ h p q hqi hq /-- Raising at a slot undoes lowering at that slot, `(T^μ_ν)^ν = T^μν`. -/ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index f79dddeef4..4771fc8105 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -67,20 +67,21 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. -- `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_mapSlots` and - `Species.mapSlots_id`, and `Species.mapAt` applies one at a single slot and the identity at the - others. - `Species.congrSlots` applies an equivalence of representations at every slot. -- `Species.congrAt` applies an equivalence of representations at one slot of a tensor and the - identity at the others, with `Species.congrAt_pure` and `Species.congrAt_symm`, and - `Species.congrAt_eq_mapAt` proves that `congrAt` is `mapAt` of the underlying map. +- `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_comp` and + `Species.mapSlots_id`. +- `Species.slotMapsAt` is the family of a linear map at one slot and the identity at the others, + and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and + `Species.mapAt_id`. +- `Species.congrAt` is `mapAt` of an equivalence of representations, as an equivalence of + representations, with `Species.congrAt_symm`. ## iii. Table of contents - A. Tensors - B. The group action - C. The basis of tensors -- D. A linear map at one slot +- D. Linear maps at the slots ## iv. References @@ -307,13 +308,13 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor /-! -## D. A linear map at one slot +## D. Linear maps at the slots -/ section -variable [DecidableEq ι] {c' : ι → Color K} +variable {c' : ι → Color K} variable (S) in /-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations @@ -327,87 +328,78 @@ noncomputable def congrSlots (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : exact congrArg S.pure (funext fun x => Representation.IntertwiningMap.isIntertwining _ _ (f x).toIntertwiningMap g (p x)) -omit [DecidableEq ι] in /-- On a pure tensor, `congrSlots` applies `f x` to the vector at each slot `x`. -/ lemma congrSlots_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : S.congrSlots f (S.pure p) = S.pure fun x => f x (p x) := by simp [congrSlots, congr_tprod] -variable (S) in -/-- The equivalence of representations `f` at the slot `i`, and the identity, moved along `h x`, -at every other slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ -noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) - (h : ∀ x, x ≠ i → c x = c' x) : (S.tensorRep c).Equiv (S.tensorRep c') := - S.congrSlots fun x => if hx : x = i then hx ▸ f else S.cast (h x hx) - -/-- On a pure tensor, `congrAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ -lemma congrAt_pure (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) - (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) - (hqi : q i = f (p i)) - (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : - S.congrAt i f h (S.pure p) = S.pure q := by - rw [congrAt, congrSlots_pure] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp [hqi] - · simp [hx, hq x hx] - -/-- The inverse of `congrAt` of `f` is `congrAt` of `f.symm`. -/ -lemma congrAt_symm (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) - (h : ∀ x, x ≠ i → c x = c' x) : - (S.congrAt i f h).symm = S.congrAt i f.symm (fun x hx => (h x hx).symm) := by - refine Representation.Equiv.ext (funext fun t => ?_) - refine (S.congrAt i f h).toLinearEquiv.symm_apply_eq.2 ?_ - induction t using inductionOn_pure with - | h p => - change _ = S.congrSlots _ (S.congrSlots _ (S.pure p)) - rw [congrSlots_pure, congrSlots_pure] - congr 1 - funext x - by_cases hx : x = i - · subst hx - simp - · simp [hx] - | hsmul r t ht => rw [map_smul, map_smul, ← ht] - | hadd x y hx hy => rw [map_add, map_add, ← hx, ← hy] - variable (S) in /-- A linear map `f x` at every slot `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ noncomputable def mapSlots (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap -omit [DecidableEq ι] in /-- On a pure tensor, `mapSlots` applies `f x` to the vector at each slot `x`. -/ lemma mapSlots_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : S.mapSlots f (S.pure p) = S.pure fun x => f x (p x) := by simp only [mapSlots, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, toPiTensor_pure, PiTensorProduct.map_tprod, toPiTensor_symm_tprod] -omit [DecidableEq ι] in /-- Two slotwise maps in sequence are the slotwise map of the composites. -/ +lemma mapSlots_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) + (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) : + S.mapSlots g ∘ₗ S.mapSlots f = S.mapSlots fun x => g x ∘ₗ f x := + ext_pure fun p => by simp only [LinearMap.comp_apply, mapSlots_pure] + +/-- Two slotwise maps in sequence are the slotwise map of the composites, on a tensor. -/ lemma mapSlots_mapSlots {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : S.Tensor c) : - S.mapSlots g (S.mapSlots f t) = S.mapSlots (fun x => g x ∘ₗ f x) t := by - simp only [mapSlots, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, - LinearEquiv.apply_symm_apply, ← LinearMap.comp_apply (PiTensorProduct.map g), - ← PiTensorProduct.map_comp] + S.mapSlots g (S.mapSlots f t) = S.mapSlots (fun x => g x ∘ₗ f x) t := + LinearMap.congr_fun (mapSlots_comp f g) t -omit [DecidableEq ι] in /-- The slotwise map of the identities is the identity. -/ -lemma mapSlots_id (t : S.Tensor c) : S.mapSlots (fun _ => LinearMap.id) t = t := by - simp [mapSlots, PiTensorProduct.map_id] +lemma mapSlots_id : S.mapSlots (c := c) (fun _ => LinearMap.id) = LinearMap.id := + ext_pure fun p => mapSlots_pure _ p + +/-- The slotwise map of the identities is the identity, on a tensor. -/ +lemma mapSlots_id_apply (t : S.Tensor c) : S.mapSlots (fun _ => LinearMap.id) t = t := + LinearMap.congr_fun mapSlots_id t + +section + +variable [DecidableEq ι] + +variable (S) in +/-- The family of linear maps with `f` at the slot `i` and the identity, moved along `h x`, at +every other slot `x`. `mapAt` applies it slot by slot. -/ +noncomputable def slotMapsAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) : Π x, S.V (c x) →ₗ[k] S.V (c' x) := + Function.update (fun x => if hx : x = i then 0 else (S.cast (h x hx)).toLinearMap) i f + +@[simp] +lemma slotMapsAt_apply_self (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) : S.slotMapsAt i f h i = f := + Function.update_self _ _ _ + +@[simp] +lemma slotMapsAt_apply_ne (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) {x : ι} (hx : x ≠ i) : + S.slotMapsAt i f h x = (S.cast (h x hx)).toLinearMap := by + rw [slotMapsAt, Function.update_of_ne hx, dite_eq_right hx] + +/-- A family with `f` at the slot `i` and the moves along `h x` at the other slots is +`slotMapsAt i f h`. -/ +lemma eq_slotMapsAt {F : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (i : ι) + (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (hi : F i = f) + (hne : ∀ x (hx : x ≠ i), F x = (S.cast (h x hx)).toLinearMap) : F = S.slotMapsAt i f h := + Function.eq_update_iff.2 ⟨hi, fun x hx => by rw [hne x hx, dite_eq_right hx]⟩ variable (S) in /-- The linear map `f` at the slot `i`, and the identity, moved along `h x`, at every other slot `x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := - S.mapSlots - (Function.update (fun x => if hx : x = i then 0 else (S.cast (h x hx)).toLinearMap) i f) + S.mapSlots (S.slotMapsAt i f h) /-- On a pure tensor, `mapAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ @@ -416,24 +408,61 @@ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : S.mapAt i f h (S.pure p) = S.pure q := by rw [mapAt, mapSlots_pure] - congr 1 - funext x + refine congrArg S.pure (funext fun x => ?_) by_cases hx : x = i · subst hx - simp [hqi] - · simp [hx, hq x hx] + rw [slotMapsAt_apply_self, hqi] + · rw [slotMapsAt_apply_ne _ _ _ hx, hq x hx] + rfl + +/-- `mapAt` of `f` and then of `g` at the same slot is `mapAt` of `g ∘ₗ f`. -/ +lemma mapAt_comp {c'' : ι → Color K} (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (g : S.V (c' i) →ₗ[k] S.V (c'' i)) (h : ∀ x, x ≠ i → c x = c' x) + (h' : ∀ x, x ≠ i → c' x = c'' x) : + S.mapAt i g h' ∘ₗ S.mapAt i f h = S.mapAt i (g ∘ₗ f) fun x hx => (h x hx).trans (h' x hx) := by + rw [mapAt, mapAt, mapAt, mapSlots_comp] + refine congrArg _ (eq_slotMapsAt i _ _ (by simp) fun x hx => LinearMap.ext fun v => ?_) + simp [hx] + +/-- `mapAt` of the identity is the identity. -/ +lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : + S.mapAt i LinearMap.id h = LinearMap.id := by + rw [mapAt, ← eq_slotMapsAt (F := fun _ => LinearMap.id) i _ h rfl + fun _ _ => LinearMap.ext fun _ => rfl, mapSlots_id] + +variable (S) in +/-- The equivalence of representations `f` at the slot `i`, and the identity, moved along `h x`, +at every other slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. Its linear map is `mapAt` +of `f`. -/ +noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) + (h : ∀ x, x ≠ i → c x = c' x) : (S.tensorRep c).Equiv (S.tensorRep c') := + equivOfPure (.ofLinearMap (S.mapAt i f.toLinearMap h) + (S.mapAt i f.symm.toLinearMap fun x hx => (h x hx).symm) + (by rw [mapAt_comp, show f.toLinearMap ∘ₗ f.symm.toLinearMap = LinearMap.id from + LinearMap.ext f.apply_symm_apply, mapAt_id]) + (by rw [mapAt_comp, show f.symm.toLinearMap ∘ₗ f.toLinearMap = LinearMap.id from + LinearMap.ext f.symm_apply_apply, mapAt_id])) fun g p => by + simp only [LinearEquiv.coe_ofLinearMap, mapAt, smul_pure, mapSlots_pure] + refine congrArg S.pure (funext fun x => ?_) + by_cases hx : x = i + · subst hx + simp only [slotMapsAt_apply_self, Representation.IntertwiningMap.coe_toLinearMap] + exact Representation.IntertwiningMap.isIntertwining _ _ f.toIntertwiningMap g (p x) + · simp only [slotMapsAt_apply_ne _ _ _ hx, Representation.IntertwiningMap.coe_toLinearMap] + exact cast_rep _ _ _ /-- `congrAt` is `mapAt` of the underlying linear map. -/ lemma congrAt_eq_mapAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : - S.congrAt i f h t = S.mapAt i f.toLinearMap h t := by - induction t using inductionOn_pure with - | h p => - let q : Π x, S.V (c' x) := fun x => if hx : x = i then hx ▸ f (p i) else S.cast (h x hx) (p x) - rw [congrAt_pure i f h p q (by simp [q]) (fun x hx => by simp [q, hx]), - mapAt_pure i _ h p q (by simp [q]) (fun x hx => by simp [q, hx])] - | hsmul r t ht => rw [map_smul, map_smul, ht] - | hadd x y hx hy => rw [map_add, map_add, hx, hy] + S.congrAt i f h t = S.mapAt i f.toLinearMap h t := rfl + +/-- The inverse of `congrAt` of `f` is `congrAt` of `f.symm`. -/ +lemma congrAt_symm (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) + (h : ∀ x, x ≠ i → c x = c' x) : + (S.congrAt i f h).symm = S.congrAt i f.symm (fun x hx => (h x hx).symm) := + Representation.Equiv.ext rfl + +end end From 262817084fc114a08e566d2099d156d279b1288f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:42:04 +0000 Subject: [PATCH 025/242] refactor(Reindexing): permSlots, reindex as permSlots then congrSlots; reindex_trans, reindex_symm, reindex_refl as maps; ColorEquiv.toEquiv_refl, toEquiv_symm, toEquiv_trans, symm_symm reindex_reindex stays pointwise, now from reindex_trans. tensor_components uses ColorEquiv.toEquiv_symm instead of unfolding ColorEquiv.symm. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 2 +- Physlib/Mathematics/Tensors/Elab.lean | 5 +- Physlib/Mathematics/Tensors/Product.lean | 4 +- Physlib/Mathematics/Tensors/Reindexing.lean | 86 ++++++++++++++------ 4 files changed, 68 insertions(+), 29 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 7d3a44b288..a07b4d40b3 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -310,7 +310,7 @@ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] [Invertible (2 : k)] {a : J (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) {t : S.Tensor c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by -- After `π`, each pair `j` of `σ` is its pair `e.symm j`, and the free indices are kept. - have hs (y) : (π.trans σ).toEquiv.symm y = + have hs (y) : π.toEquiv.symm (σ.toEquiv.symm y) = σ.toEquiv.symm (((Equiv.refl κ).sumCongr (e.sumCongr e)).symm y) := congrArg (fun f : Equiv _ _ => f.symm y) hπ have hσ : contr (π.trans σ) t = contr σ t := diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index c50962b852..e00f59014f 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -449,8 +449,9 @@ macro_rules Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are -- evaluated. The option `+instances` reaches those inside instance arguments. - try dsimp +instances only [ColorEquiv.symm, Equiv.symm_symm, Equiv.coe_fn_mk, Sum.elim_inl, - Sum.elim_inr, Matrix.cons_val, Function.comp_apply, Tensors.Notation.reduceCast]) + try dsimp +instances only [ColorEquiv.toEquiv_symm, Equiv.symm_symm, Equiv.coe_fn_mk, + Sum.elim_inl, Sum.elim_inr, Matrix.cons_val, Function.comp_apply, + Tensors.Notation.reduceCast]) /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 7cbc375df7..ea0a09df49 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -258,7 +258,7 @@ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Colo (σ : ColorEquiv c₁ c₁') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = S.reindex (σ.sumCongr (.refl c₂)) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by - rw [reindex_sumCongr_tmulEquiv, reindex_refl] + rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] /-- Relabeling the second factor relabels the product. With `tmulEquiv_reindex_left` it moves relabelings out of products, the form `tensor_regroup` works in. -/ @@ -266,7 +266,7 @@ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color (σ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = S.reindex ((ColorEquiv.refl c₁).sumCongr σ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by - rw [reindex_sumCongr_tmulEquiv, reindex_refl] + rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] /-- Swapping the last two factors of a product, `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` after the slots of `U` are moved before those of `W` by `ColorEquiv.swapLast`. -/ diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index b40b65e9ec..1c9848a1d2 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -46,10 +46,13 @@ their order. ## ii. Key results - `ColorEquiv` is a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. -- `Species.reindex` applies a relabeling to tensors, and `Species.reindex_pure` gives it on a pure +- `Species.permSlots` moves the vectors of a tensor along a bijection of slots, with no colors + involved. It is Mathlib's `PiTensorProduct.reindex`. +- `Species.reindex` applies a relabeling to tensors, `permSlots` followed by `congrSlots` of the + moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. +- `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that relabeling + respects composites, the identity and inverses, and `Species.reindex_reindex` is the first on a tensor. -- `Species.reindex_reindex` proves that two relabelings in sequence are their composite, and - `Species.reindex_refl` that the identity relabeling does nothing. - `Species.reindex` is an equivalence of representations, so relabeling commutes with the group action. - `Species.tensorBasis_repr_reindex` gives the components of a relabeled tensor, @@ -109,6 +112,19 @@ def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := def trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : ColorEquiv c₁ c₃ := ⟨σ₁.toEquiv.trans σ₂.toEquiv, fun j => (σ₁.color_eq _).trans (σ₂.color_eq j)⟩ +@[simp] +lemma toEquiv_refl (c₁ : ι₁ → Color K) : (refl c₁).toEquiv = Equiv.refl ι₁ := rfl + +@[simp] +lemma toEquiv_symm (σ : ColorEquiv c₁ c₂) : σ.symm.toEquiv = σ.toEquiv.symm := rfl + +@[simp] +lemma toEquiv_trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : + (σ₁.trans σ₂).toEquiv = σ₁.toEquiv.trans σ₂.toEquiv := rfl + +@[simp] +lemma symm_symm (σ : ColorEquiv c₁ c₂) : σ.symm.symm = σ := ColorEquiv.ext (Equiv.symm_symm _) + end ColorEquiv namespace Species @@ -124,18 +140,30 @@ variable {ι₁ ι₂ ι₃ : Type} -/ +variable (S) in +/-- The bijection `e` of slots applied to tensors, moving the vector of the old slot `i` to the new +slot `e i`, with the colors `c ∘ e.symm`. It is Mathlib's `PiTensorProduct.reindex`. -/ +noncomputable def permSlots (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : + (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := + equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.reindex k (fun i => S.V (c i)) e ≪≫ₗ + (S.toPiTensor (c ∘ e.symm)).symm) fun g f => by + simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, + PiTensorProduct.reindex_tprod, toPiTensor_symm_tprod] + rfl + +/-- On a pure tensor, the new slot `j` holds the vector of the old slot `e.symm j`. -/ +@[simp] +lemma permSlots_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, S.V (c i)) : + S.permSlots c e (S.pure f) = S.pure fun j => f (e.symm j) := by + simp [permSlots, PiTensorProduct.reindex_tprod] + variable (S) in /-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new slot `j` -holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is Mathlib's -`PiTensorProduct.reindex` followed by `PiTensorProduct.congr` of `LinearEquiv.cast`. -/ +holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is +`permSlots` of `σ.toEquiv` followed by `congrSlots` of the moves. -/ noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : (S.tensorRep c₁).Equiv (S.tensorRep c₂) := - equivOfPure (S.toPiTensor c₁ ≪≫ₗ PiTensorProduct.reindex k _ σ.toEquiv ≪≫ₗ - PiTensorProduct.congr (fun j => (S.cast (σ.color_eq j)).toLinearEquiv) ≪≫ₗ - (S.toPiTensor c₂).symm) fun g f => by - simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, - PiTensorProduct.reindex_tprod, congr_tprod, toPiTensor_symm_tprod] - exact congrArg S.pure (funext fun j => cast_rep _ _ _) + (S.permSlots c₁ σ.toEquiv).trans (S.congrSlots fun j => S.cast (σ.color_eq j)) /-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. -/ @@ -143,7 +171,7 @@ along `σ.color_eq j`. -/ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (f : Π i, S.V (c₁ i)) : S.reindex σ (S.pure f) = S.pure fun j => S.cast (σ.color_eq j) (f (σ.toEquiv.symm j)) := by - simp [reindex, PiTensorProduct.reindex_tprod, congr_tprod] + rw [reindex, Representation.Equiv.trans_apply, permSlots_pure, congrSlots_pure] /-! @@ -151,21 +179,31 @@ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C -/ -/-- Relabeling by the identity does nothing. -/ -lemma reindex_refl {c₁ : ι₁ → Color K} (t : S.Tensor c₁) : S.reindex (ColorEquiv.refl c₁) t = t := - LinearMap.congr_fun (f := (S.reindex (ColorEquiv.refl c₁)).toLinearMap) - (ext_pure (g := LinearMap.id) fun f => by - rw [Representation.Equiv.coe_toLinearMap, reindex_pure]; rfl) t +/-- Relabeling by the identity is the identity. -/ +lemma reindex_refl (c₁ : ι₁ → Color K) : S.reindex (ColorEquiv.refl c₁) = .refl _ := + equiv_ext_pure fun f => by rw [reindex_pure]; rfl + +/-- Relabeling by `σ₁.trans σ₂` is relabeling by `σ₁` and then by `σ₂`. -/ +lemma reindex_trans {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} + (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : + S.reindex (σ₁.trans σ₂) = (S.reindex σ₁).trans (S.reindex σ₂) := + equiv_ext_pure fun f => by + simp only [Representation.Equiv.trans_apply, reindex_pure, cast_apply, cast_cast] + rfl -/-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`. -/ +/-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`, on a tensor. -/ lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : - S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := - LinearMap.congr_fun (f := ((S.reindex σ₁).trans (S.reindex σ₂)).toLinearMap) - (ext_pure (g := (S.reindex (σ₁.trans σ₂)).toLinearMap) fun f => by - simp only [Representation.Equiv.coe_toLinearMap, Representation.Equiv.trans_apply, - reindex_pure, cast_apply, cast_cast] - rfl) t + S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by + rw [reindex_trans, Representation.Equiv.trans_apply] + +/-- The inverse of relabeling by `σ` is relabeling by `σ.symm`. -/ +lemma reindex_symm {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : + (S.reindex σ).symm = S.reindex σ.symm := + Representation.Equiv.ext <| funext fun t => EquivLike.injective (S.reindex σ) <| by + rw [Representation.Equiv.apply_symm_apply, reindex_reindex, + show σ.symm.trans σ = .refl c₂ from ColorEquiv.ext (Equiv.symm_trans_self _), reindex_refl] + rfl /-! From 1fa2e3d0c3f3a3c8425f4478c5612fd9a573303d Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:44:02 +0000 Subject: [PATCH 026/242] =?UTF-8?q?refactor(Product):=20tmulEquiv=5Fassoc,?= =?UTF-8?q?=20tmulEquiv=5Fcomm,=20reindex=5FsumCongr=5FtmulEquiv,=20tmulEq?= =?UTF-8?q?uiv=5FswapLast,=20tensorBasis=5Frepr=5FtmulEquiv=20from=20ext?= =?UTF-8?q?=5Fpure=E2=82=82=20and=20ext=5Fpure=E2=82=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Product.lean | 110 ++++++++++------------- 1 file changed, 47 insertions(+), 63 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index ea0a09df49..87b3b96abe 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -203,54 +203,45 @@ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃) (S.tmulEquiv _ _ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃)) = S.tmulEquiv _ _ (t₁ ⊗ₜ S.tmulEquiv c₂ c₃ (t₂ ⊗ₜ t₃)) := by - induction t₁ using inductionOn_pure with - | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] - | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] - | h f₁ => - induction t₂ using inductionOn_pure with - | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] - | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] - | h f₂ => - induction t₃ using inductionOn_pure with - | hadd t t' ht ht' => simp only [tmul_add, map_add, ht, ht'] - | hsmul r t ht => simp only [tmul_smul, map_smul, ht] - | h f₃ => - simp only [tmulEquiv_pure, reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl) + have h := ext_pure₃ (f := (S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃)).toLinearMap ∘ₗ + (S.tmulEquiv _ c₃).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₂).toLinearMap .id) + (g := (S.tmulEquiv c₁ _).toLinearMap ∘ₗ TensorProduct.map .id (S.tmulEquiv c₂ c₃).toLinearMap ∘ₗ + (TensorProduct.assoc k _ _ _).toLinearMap) fun f₁ f₂ f₃ => by + simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, assoc_tmul, + LinearMap.id_apply, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl) + simpa only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, assoc_tmul, + LinearMap.id_apply, Representation.Equiv.coe_toLinearMap] using + LinearMap.congr_fun h ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) /-- The product is symmetric, `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`, after the indices of `U` are moved in front by `ColorEquiv.sumComm`. -/ lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.reindex (ColorEquiv.sumComm c₁ c₂) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = - S.tmulEquiv c₂ c₁ (t₂ ⊗ₜ t₁) := by - induction t₁ using inductionOn_pure with - | hsmul r t ht => simp only [smul_tmul, tmul_smul, map_smul, ht] - | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] - | h f₁ => - induction t₂ using inductionOn_pure with - | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] - | hadd t t' ht ht' => simp only [tmul_add, add_tmul, map_add, ht, ht'] - | h f₂ => - rw [tmulEquiv_pure, tmulEquiv_pure, reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + S.tmulEquiv c₂ c₁ (t₂ ⊗ₜ t₁) := + LinearMap.congr_fun (f := (S.reindex (ColorEquiv.sumComm c₁ c₂)).toLinearMap ∘ₗ + (S.tmulEquiv c₁ c₂).toLinearMap) + (g := (S.tmulEquiv c₂ c₁).toLinearMap ∘ₗ (TensorProduct.comm k _ _).toLinearMap) + (ext_pure₂ fun f₁ f₂ => by + simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, comm_tmul, + Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ /-- Relabeling the product along `σ.sumCongr τ` is relabeling its factors along `σ` and `τ`. -/ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.reindex (σ.sumCongr τ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = - S.tmulEquiv c₁' c₂' (S.reindex σ t₁ ⊗ₜ S.reindex τ t₂) := by - induction t₁ using inductionOn_pure with - | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] - | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] - | h f₁ => - induction t₂ using inductionOn_pure with - | hsmul r t ht => simp only [tmul_smul, map_smul, ht] - | hadd t t' ht ht' => simp only [tmul_add, map_add, ht, ht'] - | h f₂ => - simp only [tmulEquiv_pure, reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + S.tmulEquiv c₁' c₂' (S.reindex σ t₁ ⊗ₜ S.reindex τ t₂) := + LinearMap.congr_fun (f := (S.reindex (σ.sumCongr τ)).toLinearMap ∘ₗ + (S.tmulEquiv c₁ c₂).toLinearMap) + (g := (S.tmulEquiv c₁' c₂').toLinearMap ∘ₗ + TensorProduct.map (S.reindex σ).toLinearMap (S.reindex τ).toLinearMap) + (ext_pure₂ fun f₁ f₂ => by + simp only [LinearMap.comp_apply, TensorProduct.map_tmul, + Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ /-- Relabeling the first factor relabels the product. With `tmulEquiv_reindex_right` it moves relabelings out of products, the form `tensor_regroup` works in. -/ @@ -275,20 +266,18 @@ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} { S.reindex (ColorEquiv.swapLast c₁ c₂ c₃) (S.tmulEquiv _ c₂ (S.tmulEquiv c₁ c₃ (t₁ ⊗ₜ t₃) ⊗ₜ t₂)) = S.tmulEquiv _ c₃ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃) := by - induction t₁ using inductionOn_pure with - | hadd t t' ht ht' => simp only [add_tmul, map_add, ht, ht'] - | hsmul r t ht => simp only [← smul_tmul', map_smul, ht] - | h f₁ => - induction t₂ using inductionOn_pure with - | hadd t t' ht ht' => simp only [add_tmul, tmul_add, map_add, ht, ht'] - | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] - | h f₂ => - induction t₃ using inductionOn_pure with - | hadd t t' ht ht' => simp only [tmul_add, add_tmul, map_add, ht, ht'] - | hsmul r t ht => simp only [tmul_smul, ← smul_tmul', map_smul, ht] - | h f₃ => - simp only [tmulEquiv_pure, reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with (_ | _) | _ <;> rfl) + have h := ext_pure₃ (f := (S.reindex (ColorEquiv.swapLast c₁ c₂ c₃)).toLinearMap ∘ₗ + (S.tmulEquiv _ c₂).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₃).toLinearMap .id ∘ₗ + (TensorProduct.rightComm k _ _ _).toLinearMap) + (g := (S.tmulEquiv _ c₃).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₂).toLinearMap .id) + fun f₁ f₂ f₃ => by + simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, + rightComm_tmul, LinearMap.id_apply, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, + reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with (_ | _) | _ <;> rfl) + simpa only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, rightComm_tmul, + LinearMap.id_apply, Representation.Equiv.coe_toLinearMap] using + LinearMap.congr_fun h ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) /-- The product commutes with the group action, `Λ T ⊗ Λ U = Λ (T ⊗ U)`, as in `(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ @@ -397,19 +386,14 @@ lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ (y : Π s, basisIdx (prodColors c₁ c₂ s).kind) : (S.tensorBasis (prodColors c₁ c₂)).repr (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) y = (S.tensorBasis c₁).repr t₁ (fun i => y (.inl i)) * - (S.tensorBasis c₂).repr t₂ (fun j => y (.inr j)) := by - induction t₁ using inductionOn_pure with - | hsmul r t ht => - simp only [← smul_tmul', map_smul, Finsupp.smul_apply, ht, smul_eq_mul, mul_assoc] - | hadd t t' ht ht' => simp only [add_tmul, map_add, Finsupp.add_apply, ht, ht', add_mul] - | h f₁ => - induction t₂ using inductionOn_pure with - | hsmul r t ht => - simp only [tmul_smul, map_smul, Finsupp.smul_apply, ht, smul_eq_mul, mul_left_comm] - | hadd t t' ht ht' => simp only [tmul_add, map_add, Finsupp.add_apply, ht, ht', mul_add] - | h f₂ => - rw [tmulEquiv_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, - Fintype.prod_sum_type] + (S.tensorBasis c₂).repr t₂ (fun j => y (.inr j)) := + LinearMap.congr_fun (f := (S.tensorBasis _).coord y ∘ₗ (S.tmulEquiv c₁ c₂).toLinearMap) + (g := LinearMap.mul' k k ∘ₗ TensorProduct.map ((S.tensorBasis c₁).coord fun i => y (.inl i)) + ((S.tensorBasis c₂).coord fun j => y (.inr j))) (ext_pure₂ fun f₁ f₂ => by + simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearMap.mul'_apply, + Module.Basis.coord_apply, Representation.Equiv.coe_toLinearMap] + rw [tmulEquiv_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, + Fintype.prod_sum_type]) _ /-- The components of the tensor of a vector `v` of `V c` are its components `v^μ`. -/ lemma tensorBasis_repr_oneSlotEquiv (c : Color K) (v : S.V c) From 8e2385c41fb2473e43ad90e40c9ee05ed0eca030 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:47:26 +0000 Subject: [PATCH 027/242] refactor(Color): remove Variance.dual_dual, Variance.dual_up, Variance.dual_down, Color.dual_mk Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 98f73f1f4f..877a2b4d5d 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -78,19 +78,10 @@ abbrev up : Variance := ⟨true, false, by decide⟩ @[match_pattern] abbrev down : Variance := ⟨false, true, by decide⟩ --- An abbrev, since the species laws rewrite with `c.dual.dual = c` at reducible transparency. -/-- The dual variance, exchanging up and down. -/ +/-- The dual variance, exchanging up and down. It is an abbrev, since the species laws rewrite with +`c.dual.dual = c` at reducible transparency. -/ abbrev dual (v : Variance) : Variance := ⟨v.isDown, v.isUp, v.ne.symm⟩ -@[simp] -lemma dual_up : up.dual = down := rfl - -@[simp] -lemma dual_down : down.dual = up := rfl - -/-- Taking the dual twice gives back the variance. -/ -lemma dual_dual (v : Variance) : v.dual.dual = v := rfl - end Variance /-! @@ -110,11 +101,6 @@ deriving DecidableEq /-- The dual color, with the same kind and the other variance. -/ abbrev Color.dual {K : Type} (c : Color K) : Color K := ⟨c.kind, c.variance.dual⟩ -/-- The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`. -/ -@[simp] -lemma Color.dual_mk {K : Type} (κ : K) (v : Variance) : - (⟨κ, v⟩ : Color K).dual = ⟨κ, v.dual⟩ := rfl - /-- Taking the dual twice gives back the color. -/ lemma Color.dual_dual {K : Type} (c : Color K) : c.dual.dual = c := rfl From 665efd194e3df8f6e5f46aee69cc86c5fa5e0914 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:49:51 +0000 Subject: [PATCH 028/242] refactor(Product): ColorEquiv.swapLast as a composite, tmulEquiv_swapLast from tmulEquiv_assoc and tmulEquiv_comm; inline ColorEquiv.ofFinTwo into twoSlotEquiv; ColorEquiv.tmulRight via swapLast Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 4 +- Physlib/Mathematics/Tensors/Product.lean | 120 +++++-------------- 2 files changed, 31 insertions(+), 93 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index a07b4d40b3..a5e3d2f491 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -377,9 +377,7 @@ indices of `U` are free and come after the free indices of `T`. -/ noncomputable def _root_.Tensors.ColorEquiv.tmulRight {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := - (σ.sumCongr (.refl d)).trans ((ColorEquiv.sumAssoc _ _ _).trans - (((ColorEquiv.refl c').sumCongr (ColorEquiv.sumComm _ _)).trans - (ColorEquiv.sumAssoc _ _ _).symm)) + (σ.sumCongr (.refl d)).trans (ColorEquiv.swapLast c' d _) /-- Contracting indices of the left factor commutes with the product, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 87b3b96abe..f47d643e9f 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -14,62 +14,28 @@ The product `T ⊗ U` of two tensors has the indices of `T` followed by those of ## i. Overview -Let `T = T^μ_ν e_μ ⊗ e^ν` be a Lorentz tensor with colors `c₁ = ![⟨vec, up⟩, ⟨vec, down⟩]` on the -slots `Fin 2`, and `U = U^ρ e_ρ` a vector with colors `c₂ = ![⟨vec, up⟩]` on the slot `Fin 1`. -Their product is the element +Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the slots `Fin 2` and `U = U^ρ e_ρ` colors `c₂` on +the slot `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the slots `Fin 2 ⊕ Fin 1` +with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on `inr j`, so no slot +is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Mathlib's +`PiTensorProduct.tmulEquivDep`. - `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` of the space `ℝ⁴ ⊗ (ℝ⁴)* ⊗ ℝ⁴`. - -Its slots are labeled by `Fin 2 ⊕ Fin 1`. The slots `inl 0` and `inl 1` carry the indices `μ` and -`ν` of `T`, and the slot `inr 0` carries the index `ρ` of `U`. No slot is renumbered, so no -equality of colors has to be proved. - -- `prodColors c₁ c₂ : ι₁ ⊕ ι₂ → Color K` gives the colors of `T ⊗ U`, those of `T` on the slots - `inl i` and those of `U` on the slots `inr j`. -- `S.tmulEquiv c₁ c₂` sends the element `T ⊗ₜ U` of the space `S.Tensor c₁ ⊗ S.Tensor c₂` to the - tensor `T ⊗ U` in `S.Tensor (prodColors c₁ c₂)`. It is Mathlib's - `PiTensorProduct.tmulEquivDep`. Its inverse splits a tensor on `ι₁ ⊕ ι₂` into its two groups of - indices. - -The product is associative and symmetric up to a relabeling of slots. For a third tensor `W`, -`(T ⊗ U) ⊗ W` on `(ι₁ ⊕ ι₂) ⊕ ι₃` and `T ⊗ (U ⊗ W)` on `ι₁ ⊕ (ι₂ ⊕ ι₃)` both have components -`T^μ_ν U^ρ W^σ`, and they are equal after the relabeling `ColorEquiv.sumAssoc`. A group element -`Λ`, such as a Lorentz transformation, acts on a product factor by factor, `Λ T ⊗ Λ U = Λ (T ⊗ U)`. - -An element `M` of `V a ⊗ V b` is the tensor `S.twoSlotEquiv a b M` with colors `![a, b]`. For -example, the metric `η_μν e^μ ⊗ e^ν` in `(ℝ⁴)* ⊗ (ℝ⁴)*` is the tensor `η_μν`. The delta tensor -`S.deltaTensor c` has colors `![c.dual, c]`. It is `δ_μ^ν` for an upper color `c` and `δ^μ_ν` for -a lower one. - -The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ` -(`tensorBasis_repr_tmulEquiv`). The components of `S.twoSlotEquiv a b M` are the coordinates of -`M` in Mathlib's basis `Basis.tensorProduct` of `V a ⊗ V b` (`tensorBasis_repr_twoSlotEquiv`). +The product is associative and symmetric up to a relabeling of slots, and the group acts on it +factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on +two slots by `twoSlotEquiv`, and the delta `S.delta c` is the tensor `S.deltaTensor c`. The +components of a product are the products of the components. ## ii. Key results - `prodColors` gives the colors of a product. -- `ColorEquiv.sumCongr`, `ColorEquiv.sumAssoc`, `ColorEquiv.sumComm`, `ColorEquiv.swapLast` and - `ColorEquiv.ofFinTwo` relabel the slots of a product. - `Species.tmulEquiv` is the product of two tensors, `T ⊗ₜ U ↦ T ⊗ U`. - `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. -- `Species.reindex_sumCongr_tmulEquiv` proves that relabeling each factor relabels the product, - with `Species.tmulEquiv_reindex_left` and `Species.tmulEquiv_reindex_right` for one factor. -- `Species.tmulEquiv_swapLast` proves `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` up to relabeling. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. -- `Species.oneSlotEquiv` turns a vector of `V c` into a tensor on one slot, and - it is an equivalence of representations. -- `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots, and - `Species.deltaTensor` is the delta `δ_μ^ν`. -- `Species.reindex_twoSlotEquiv` proves that swapping the two slots swaps the factors, - `M^μν ↦ M^νμ`. -- `Species.deltaTensor_invariant` proves that the delta tensor is invariant. +- `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots. - `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. -- `Species.tensorBasis_repr_oneSlotEquiv` and `Species.tensorBasis_repr_twoSlotEquiv` give the - components of a tensor on one and on two slots. -- `Species.tensorBasis_repr_deltaTensor` gives the components of the delta tensor. -- `Species.tensorBasis_repr_deltaTensor_eq_one` proves that in dual bases they form the identity - matrix. +- `Species.tensorBasis_repr_deltaTensor_eq_one` proves that the components of the delta tensor + form the identity matrix. ## iii. Table of contents @@ -98,9 +64,8 @@ namespace Tensors -/ /-- The colors of a product `T ⊗ U`, the colors `c₁` of `T` on the slots `inl i` and the colors -`c₂` of `U` on the slots `inr j`. It equals Mathlib's `Sum.elim c₁ c₂` by `rfl`. It is reducible, -so that `simp` and `rw` see `prodColors c₁ c₂ (.inl i)` as `c₁ i` and can apply Mathlib's lemmas -on `PiTensorProduct.tmulEquivDep`. -/ +`c₂` of `U` on the slots `inr j`. It equals Mathlib's `Sum.elim c₁ c₂` by `rfl`, and it is +reducible so that `prodColors c₁ c₂ (.inl i)` reduces to `c₁ i`. -/ noncomputable abbrev prodColors {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ι₁ ⊕ ι₂ → Color K := fun s => Sum.rec (motive := fun _ => Color K) c₁ c₂ s @@ -129,16 +94,10 @@ def sumComm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : /-- The slots of `(T ⊗ W) ⊗ U` relabeled as those of `(T ⊗ U) ⊗ W`, swapping the last two factors. -/ -def swapLast (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : +noncomputable def swapLast (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : ColorEquiv (prodColors (prodColors c₁ c₃) c₂) (prodColors (prodColors c₁ c₂) c₃) := - ⟨(Equiv.sumAssoc _ _ _).trans ((Equiv.sumCongr (Equiv.refl _) (Equiv.sumComm _ _)).trans - (Equiv.sumAssoc _ _ _).symm), by rintro ((_ | _) | _) <;> rfl⟩ - -/-- The slots `Fin 2` of a tensor `T^μν` relabeled as the slots `Fin 1 ⊕ Fin 1` of a product -`x^μ ⊗ y^ν`. It is Mathlib's `finSumFinEquiv`, reversed. `Species.twoSlotEquiv` relabels by its -inverse. -/ -noncomputable def ofFinTwo (a b : Color K) : ColorEquiv ![a, b] (prodColors ![a] ![b]) := - (ColorEquiv.mk (c₂ := ![a, b]) finSumFinEquiv).symm + (sumAssoc c₁ c₃ c₂).trans (((refl c₁).sumCongr (sumComm c₃ c₂)).trans + (sumAssoc c₁ c₂ c₃).symm) end ColorEquiv @@ -244,7 +203,7 @@ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ /-- Relabeling the first factor relabels the product. With `tmulEquiv_reindex_right` it moves -relabelings out of products, the form `tensor_regroup` works in. -/ +relabelings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₁') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = @@ -252,7 +211,7 @@ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Colo rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] /-- Relabeling the second factor relabels the product. With `tmulEquiv_reindex_left` it moves -relabelings out of products, the form `tensor_regroup` works in. -/ +relabelings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = @@ -266,18 +225,9 @@ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} { S.reindex (ColorEquiv.swapLast c₁ c₂ c₃) (S.tmulEquiv _ c₂ (S.tmulEquiv c₁ c₃ (t₁ ⊗ₜ t₃) ⊗ₜ t₂)) = S.tmulEquiv _ c₃ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃) := by - have h := ext_pure₃ (f := (S.reindex (ColorEquiv.swapLast c₁ c₂ c₃)).toLinearMap ∘ₗ - (S.tmulEquiv _ c₂).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₃).toLinearMap .id ∘ₗ - (TensorProduct.rightComm k _ _ _).toLinearMap) - (g := (S.tmulEquiv _ c₃).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₂).toLinearMap .id) - fun f₁ f₂ f₃ => by - simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, - rightComm_tmul, LinearMap.id_apply, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, - reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with (_ | _) | _ <;> rfl) - simpa only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, rightComm_tmul, - LinearMap.id_apply, Representation.Equiv.coe_toLinearMap] using - LinearMap.congr_fun h ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) + rw [ColorEquiv.swapLast, ← reindex_reindex, ← reindex_reindex, tmulEquiv_assoc, + ← tmulEquiv_reindex_right, tmulEquiv_comm, ← reindex_symm, ← tmulEquiv_assoc] + exact (S.reindex _).symm_apply_apply _ /-- The product commutes with the group action, `Λ T ⊗ Λ U = Λ (T ⊗ U)`, as in `(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ @@ -291,15 +241,9 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : ## D. Tensors on one and two slots -A species gives its objects with two indices, such as the delta `S.delta c`, in Mathlib's binary -tensor product `V a ⊗ V b`, built from pairs `x ⊗ₜ y`. A tensor with two slots lives in -`S.Tensor ![a, b]`, the tensor product of a family of spaces, built from vectors assigned to the -slots `0` and `1`. The two spaces are isomorphic but are different types in Lean, as `ℝ × ℝ` and -`Fin 2 → ℝ` are. `twoSlotEquiv` identifies them, so that such an object can be used as a tensor, -for example in index notation, and its components can be read with Mathlib's -`Basis.tensorProduct`. It is built from the product. Each factor is a tensor on one slot by -`oneSlotEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2`. The delta -tensor `S.deltaTensor c` is the delta seen this way. +A species gives objects with two indices, such as the delta `S.delta c`, in Mathlib's binary +tensor product `V a ⊗ V b`. `twoSlotEquiv` identifies this space with the tensors `S.Tensor ![a, b]` +on two slots, so that such an object can be used in index notation. -/ @@ -328,11 +272,12 @@ lemma oneSlotEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` is the tensor `η_μν`. Each factor is a one-slot tensor by `oneSlotEquiv`, and the slots -`Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by `ColorEquiv.ofFinTwo`. -/ +`Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by Mathlib's `finSumFinEquiv`. -/ noncomputable def twoSlotEquiv (a b : Color K) : ((S.rep a).tprod (S.rep b)).Equiv (S.tensorRep ![a, b]) := ((S.oneSlotEquiv a).tensorCongr (S.oneSlotEquiv b)).trans <| - (S.tmulEquiv ![a] ![b]).trans (S.reindex (ColorEquiv.ofFinTwo a b).symm) + (S.tmulEquiv ![a] ![b]).trans + (S.reindex (ColorEquiv.mk (c₁ := prodColors ![a] ![b]) (c₂ := ![a, b]) finSumFinEquiv)) /-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ @[simp] @@ -369,13 +314,8 @@ lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S. ## E. Components -A component of a tensor with colors `c` chooses one basis label `y j` for each slot `j`, and -`(S.tensorBasis c).repr t y` is the coefficient of `t` on the basis tensor with those labels, as -`T^μ_ν` for `y = (μ, ν)`. The components of a product are the products of the components of its -factors, with the labels `y (inl i)` on the slots of the first factor and `y (inr j)` on those of -the second. The components of a tensor on two slots are the coordinates of the element of -`V a ⊗ V b` in Mathlib's basis `Basis.tensorProduct`, so they can be computed with Mathlib's lemmas -on the binary tensor product. For the delta tensor they form the identity matrix. +The components of a product are the products of the components of its factors. The components +of a tensor on two slots are the coordinates in Mathlib's basis `Basis.tensorProduct`. -/ From 8989809380e9e0acd1a754aa697e8093b40d2159 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:52:38 +0000 Subject: [PATCH 029/242] refactor(Inverse): inverse_eq_iff, inverse_eq_left_inv and inverse_eq_right_inv from it; Species.toLin_map_rep; toLin_form_bijective moved to Metric Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Inverse.lean | 60 ++++++++++-------------- Physlib/Mathematics/Tensors/Metric.lean | 6 +++ Physlib/Mathematics/Tensors/Species.lean | 14 +++--- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 17c06779b8..84f108f84b 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -14,22 +14,13 @@ The inverse `g^νμ` of a tensor `g_μν`, defined through the linear map that ` ## i. Overview -A tensor `g` in `V a ⊗ V b` is also a linear map `S.toLin g : V a.dual → V b`, which contracts a -vector with the first index of `g`, `v^μ ↦ v^μ g_μν` for lower colors `a` and `b`. When this map -is invertible, its inverse is again a tensor, in `V b.dual ⊗ V a.dual`. That is the inverse -`S.inverse g`, with components `g^νμ`. Its first index is the dual of the second index of `g`, so -the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, with its barred index first. +A tensor `g` in `V a ⊗ V b` is also a linear map `S.toLin g : V a.dual → V b`, `v^μ ↦ v^μ g_μν`. +When this map is invertible, its inverse is the tensor `S.inverse g` in `V b.dual ⊗ V a.dual`, +with components `g^νμ`. Otherwise `S.inverse g` is the junk value `0`, so every lemma about it +assumes that `S.toLin g` is bijective. -When `S.toLin g` is not invertible, `S.inverse g` is `0`, as Mathlib's inverse of a singular matrix -is. This junk value makes `inverse` a total function and is the inverse of nothing. Every lemma -about `S.inverse g` assumes that `S.toLin g` is bijective or derives it from its hypotheses. - -The inverse satisfies the contraction laws `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`, and -it is the only tensor whose linear map inverts that of `g`, so `(g⁻¹)⁻¹ = g`. - -The inverse commutes with the group action, `(Λg)⁻¹ = Λ(g⁻¹)`, so the inverse of an invariant -tensor is invariant. The inverse of the form at `c` is the form at `c.dual`, so `g^μν` is the -inverse of `g_μν`. +The inverse satisfies `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`, it is unique, it commutes +with the group action, and the inverse of the form at `c` is the form at `c.dual`. ## ii. Key results @@ -37,13 +28,11 @@ inverse of `g_μν`. linear map. - `Species.contr_mul_inverse` and `Species.contr_inverse_mul` prove `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`. -- `Species.inverse_eq_left_inv`, `Species.inverse_eq_right_inv` and - `Species.inverse_eq_of_contr_eq_deltaTensor` prove that the inverse is unique, and - `Species.inverse_inverse` that `(g⁻¹)⁻¹ = g`. +- `Species.inverse_eq_iff` characterises the inverse by its linear map, and + `Species.inverse_inverse` proves `(g⁻¹)⁻¹ = g`. - `Species.inverse_map_rep` proves `(Λg)⁻¹ = Λ(g⁻¹)`, and `Species.inverse_invariant` that the inverse of an invariant tensor is invariant. -- `Species.inverse_form` proves that the inverse of the form at `c` is the form at `c.dual`, and - `Species.contr_formTensor_formTensor` that `g^μν g_νρ = δ^μ_ρ`. +- `Species.inverse_form` proves that the inverse of the form at `c` is the form at `c.dual`. ## iii. Table of contents @@ -79,9 +68,9 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K variable (S) in open scoped Classical in /-- The inverse `g^νμ ∈ V b.dual ⊗ V a.dual` of `g_μν ∈ V a ⊗ V b`, the tensor whose linear map -inverts that of `g`. When `S.toLin g` is not bijective it is the junk value `0`. Its first index -is the dual of the second index of `g`, so the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, -the usual `g^{IJ̄}` with its indices swapped (`TensorProduct.comm`). -/ +inverts that of `g`, and the junk value `0` when `S.toLin g` is not bijective. Its first index is +the dual of the second index of `g`, so the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, +the usual `g^{IJ̄}` with its indices swapped. -/ noncomputable def inverse (g : S.V a ⊗[k] S.V b) : S.V b.dual ⊗[k] S.V a.dual := if h : Function.Bijective (S.toLin g) then S.toLin.symm (LinearEquiv.ofBijective _ h).symm.toLinearMap @@ -132,20 +121,26 @@ lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi S.deltaTensor b := (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 (comp_toLin_inverse g h) +/-- `N` is the inverse of `g` exactly when its linear map is the inverse of that of `g`. -/ +lemma inverse_eq_iff (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) + (N : S.V b.dual ⊗[k] S.V a.dual) : + S.inverse g = N ↔ S.toLin N = (LinearEquiv.ofBijective _ h).symm.toLinearMap := by + rw [← toLin_inverse g h, S.toLin.injective.eq_iff, eq_comm] + /-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `g`. -/ lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : S.inverse g = N := - Eq.symm <| S.toLin.injective <| by - rw [toLin_inverse g h, ← LinearMap.id_comp (LinearEquiv.ofBijective _ h).symm.toLinearMap] + (inverse_eq_iff g h N).2 <| by + rw [← LinearMap.id_comp (LinearEquiv.ofBijective _ h).symm.toLinearMap] exact (LinearEquiv.eq_comp_toLinearMap_symm _ _).2 hN /-- A tensor `N` with `g ∘ N = id`, as linear maps, is the inverse of `g`. -/ lemma inverse_eq_right_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin g ∘ₗ S.toLin N = LinearMap.id) : S.inverse g = N := - Eq.symm <| S.toLin.injective <| by - rw [toLin_inverse g h, ← LinearMap.comp_id (LinearEquiv.ofBijective _ h).symm.toLinearMap] + (inverse_eq_iff g h N).2 <| by + rw [← LinearMap.comp_id (LinearEquiv.ofBijective _ h).symm.toLinearMap] exact (LinearEquiv.eq_toLinearMap_symm_comp _ _).2 hN /-- A tensor `N` with `g_μν N^νρ = δ_μ^ρ` is the inverse of `g`. -/ @@ -172,10 +167,10 @@ lemma inverse_map_rep (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin S.inverse (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) = TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := by refine inverse_eq_left_inv _ (S.toLin_map_rep_bijective g h Λ) (LinearMap.ext fun x => ?_) - obtain ⟨y, rfl⟩ : ∃ y, S.rep a.dual Λ y = x := ⟨S.rep a.dual Λ⁻¹ x, by simp⟩ - rw [LinearMap.comp_apply, S.toLin_map, S.toLin_map (a := b.dual) _ _ (S.inverse g), + simp only [toLin_map_rep, LinearMap.comp_apply] + rw [show S.rep b.dual.dual Λ⁻¹ = S.rep b Λ⁻¹ from rfl, Representation.inv_self_apply, ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h, LinearMap.id_apply, - LinearMap.id_apply] + Representation.self_inv_apply, LinearMap.id_apply] /-- The inverse of an invariant tensor is invariant, `Λg = g` implies `Λ(g⁻¹) = g⁻¹`. -/ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) @@ -189,11 +184,6 @@ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi -/ -/-- The linear map of the form at `c` is bijective. -/ -lemma toLin_form_bijective (c : Color K) [S.HasForm c.kind] : - Function.Bijective (S.toLin (form c)) := - (S.toDual c.dual).bijective - /-- The inverse of the form at `c` is the form at `c.dual`, as `η^μν` is the inverse of `η_μν`. -/ lemma inverse_form (c : Color K) [S.HasForm c.kind] : S.inverse (form c) = form c.dual := diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 6c80c8f60f..37c98b69b6 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -135,6 +135,12 @@ lemma form_inverse (c : Color K) [S.HasForm c.kind] : S.toLin (form c) ∘ₗ S.toLin (form c.dual) = LinearMap.id := HasForm.formAt_inverse c.variance +/-- The linear map of the form at `c` is bijective. -/ +lemma toLin_form_bijective (c : Color K) [S.HasForm c.kind] : + Function.Bijective (S.toLin (form c)) := + Function.bijective_iff_has_inverse.mpr ⟨S.toLin (form c.dual), + LinearMap.congr_fun (form_inverse c.dual), LinearMap.congr_fun (form_inverse c)⟩ + variable (S) in /-- The form at `c` read as a bilinear form on `V c.dual`, `(u, v) ↦ u_μ g^μν v_ν`, with the first argument contracted with the first index of the form. -/ diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index bbbab09dbb..97fb603b5b 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -267,19 +267,21 @@ lemma delta_invariant (c : Color K) (g : G) : rw [show S.rep c g⁻¹ x = S.rep c.dual.dual g⁻¹ x from rfl, S.toLin_map, S.snake c, LinearMap.id_apply, LinearMap.id_apply, Representation.self_inv_apply] +/-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ +lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : + S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = + S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := + LinearMap.ext fun x => by + rw [LinearMap.comp_apply, LinearMap.comp_apply, ← S.toLin_map, Representation.self_inv_apply] + /-- The linear map of `Λz` is bijective when that of `z` is. -/ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin z)) (g : G) : Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by - -- The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, a composite of bijections. - have e : ⇑(S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) = - S.rep b g ∘ S.toLin z ∘ S.rep a.dual g⁻¹ := by - funext x - rw [Function.comp_apply, Function.comp_apply, ← S.toLin_map, Representation.self_inv_apply] have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := Function.bijective_iff_has_inverse.mpr ⟨S.rep c g⁻¹, Representation.inv_self_apply _ _, Representation.self_inv_apply _ _⟩ - rw [e] + rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) variable {S} From 3af459844826b32483d405f7eeb3767c6387213b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:56:39 +0000 Subject: [PATCH 030/242] feat(Tensorial): tensorBasis_repr_toTensor_ofIndexSpace, tensorBasis_repr_toTensor_prod_ofIndexSpace; toTensor_rep from isIntertwining'; species component lemmas use them Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensorial.lean | 74 +++++++++++-------- .../Tensors/ComplexTensor/Species.lean | 7 +- .../Tensors/RealTensor/Species.lean | 12 +-- 3 files changed, 49 insertions(+), 44 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index fb3bcfee1a..7e5656ca25 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -15,44 +15,33 @@ tensors of a species with given colors, compatibly with the action. ## i. Overview -A Lorentz vector `v` can be stored as its components `v^μ`, a function `Fin 1 ⊕ Fin 3 → ℝ`, on -which Mathlib's analysis acts. The same vector is also a tensor with one upper index, an element of -`S.Tensor c` for a one-slot color list `c`, which is what index notation and contraction act on. -An instance `Tensorial S c M` relates the two. - -The type `M` keeps its own transformation law, the representation `rep`. For a Lorentz vector it is -`Λ v = Λ *ᵥ v`, matrix times column. The identification `toTensor : M → S.Tensor c` respects the -action, `toTensor (Λ v) = Λ • toTensor v`, so `v` transforms as a tensor with colors `c`. This is -the content of the instance. `toTensor` is a Mathlib `Representation.Equiv`, a linear equivalence -that commutes with the action. - -An instance on the space of one index, such as a Lorentz vector or a Weyl spinor, comes from -`ofIndexSpace`. An instance on a type equivalent to a tensorial type, compatibly with the actions, -comes from `ofEquiv`. - -The tensor product `M ⊗ N` of two tensorial types is tensorial (`Tensorial.prod`). Its action is -Mathlib's product representation, `Λ (x ⊗ y) = Λ x ⊗ Λ y`, and `x ⊗ y` is read as the product of -the tensors of `x` and `y` through `Species.tmulEquiv`. A field strength `F^μν` in -`Vector ⊗ Vector` is an example. - -The action on `M` is the field `rep`, not an instance `SMul G M`. If `M` has an instance -`SMul G M`, Mathlib gives `M ⊗ N` an instance `SMul G (M ⊗ N)` that acts on the left factor only, -`Λ • (x ⊗ y) = Λ x ⊗ y`. A diagonal `SMul` instance on `M ⊗ N` would give `Λ • F` two meanings. - -The species and colors are determined by `M`, so a type has one tensor structure. An instance -belongs on a type that always means one physical object, such as `Lorentz.Vector d`, and not on a -type such as `Matrix (Fin 4) (Fin 4) ℝ` that is used for several. +A Lorentz vector `v` can be stored as its components `v^μ`, on which Mathlib's analysis acts, and +is also a tensor with one upper index, on which index notation acts. An instance +`Tensorial S c M` relates the two by a Mathlib `Representation.Equiv` +`toTensor : M → S.Tensor c`, so that `toTensor (Λ v) = Λ • toTensor v`. + +The action on `M` is the field `rep`, not an instance `SMul G M`, since Mathlib would extend such +an instance to `M ⊗ N` acting on the left factor only. The species and colors are determined by +`M`, so an instance belongs on a type that always means one physical object, such as +`Lorentz.Vector d`. + +Instances come from the space of one index (`ofIndexSpace`), from an equivalent tensorial type +(`ofEquiv`), and from tensor products (`prod`), as for a field strength in `Vector ⊗ Vector`. ## ii. Key results - `Tensorial` : a type whose elements are tensors, compatibly with the action of the group. - `Tensorial.toTensor_rep` : `toTensor (Λ m) = Λ • toTensor m`. -- `Tensorial.ofIndexSpace` : the space of one index is tensorial, with one slot. +- `Tensorial.ofIndexSpace` : the space of one index is tensorial, with one slot, and + `Tensorial.tensorBasis_repr_toTensor_ofIndexSpace` gives its components. - `Tensorial.ofEquiv` : a type equivalent to a tensorial type, compatibly with the actions, is tensorial with the same colors. -- `Tensorial.prod` : the tensor product of two tensorial types is tensorial. +- `Tensorial.prod` : the tensor product of two tensorial types is tensorial, with + `Tensorial.rep_tmul` : `Λ (x ⊗ y) = Λ x ⊗ Λ y` and `Tensorial.toTensor_tmul` : the tensor of + `x ⊗ y` is the product of the tensors. - `Tensorial.tensorBasis_repr_toTensor_prod` : the components of the tensor of `T` in `M ⊗ N` are - its coordinates in the product basis. + its coordinates in the product basis, and `Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace` + states it for two spaces of one index. ## iii. Table of contents @@ -98,7 +87,7 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K @[simp] lemma toTensor_rep [Tensorial S c₁ M] (g : G) (m : M) : toTensor (rep g m) = g • toTensor m := - Representation.IntertwiningMap.isIntertwining _ _ (toTensor (M := M)).toIntertwiningMap g m + LinearMap.congr_fun ((toTensor (M := M)).isIntertwining' g) m variable (S) in /-- The space `S.V c` of one index as the tensors with the one slot `![c]`, with the action @@ -107,6 +96,14 @@ noncomputable abbrev ofIndexSpace (c : Color K) : Tensorial S ![c] (S.V c) where rep := S.rep c toTensor := S.oneSlotEquiv c +/-- The components of the tensor of a vector `v` of the space of one index are its components +`v^μ`. -/ +lemma tensorBasis_repr_toTensor_ofIndexSpace (c : Color K) (v : S.V c) + (μ : Π i, basisIdx (![c] i).kind) : + (S.tensorBasis ![c]).repr (toTensor (self := ofIndexSpace S c) v) μ = + (S.basis c).repr v (μ 0) := + Species.tensorBasis_repr_oneSlotEquiv c v μ + /-- A type `M` with an action `r` and an equivalence `φ` to a tensorial type `N` commuting with the actions is tensorial with the colors of `N`. -/ noncomputable abbrev ofEquiv [Tensorial S c₁ N] {r : Representation k G M} @@ -159,6 +156,21 @@ lemma tensorBasis_repr_toTensor_prod [Fintype ι₁] [Fintype ι₂] [Tensorial rw [toTensor_tmul, Species.tensorBasis_repr_tmulEquiv, h₁, h₂, Module.Basis.tensorProduct_repr_tmul_apply, smul_eq_mul, mul_comm] +section ofIndexSpace + +attribute [local instance] ofIndexSpace + +/-- The components `T^μν` of the tensor of `T` in `V a ⊗ V b`, each factor the space of one +index, are the coordinates of `T` in Mathlib's product basis. -/ +lemma tensorBasis_repr_toTensor_prod_ofIndexSpace (a b : Color K) (T : S.V a ⊗[k] S.V b) + (μ : Π s, basisIdx (prodColors ![a] ![b] s).kind) : + (S.tensorBasis (prodColors ![a] ![b])).repr (toTensor T) μ = + ((S.basis a).tensorProduct (S.basis b)).repr T (μ (.inl 0), μ (.inr 0)) := + tensorBasis_repr_toTensor_prod _ _ (fun ν => ν 0) (fun ν => ν 0) + (tensorBasis_repr_toTensor_ofIndexSpace a) (tensorBasis_repr_toTensor_ofIndexSpace b) T μ + +end ofIndexSpace + end Tensorial end Tensors diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 5b7108f2a2..c6478915b8 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -658,7 +658,7 @@ example (g : SL(2, ℂ)) (ψ : LeftHandedWeyl) : Tensorial.rep g ψ = LeftHanded example (ψ : LeftHandedWeyl) (μ : Π i, Idx (![upL] i).kind) : (complexLorentz.tensorBasis ![upL]).repr (Tensorial.toTensor ψ) μ = LeftHandedWeyl.basis.repr ψ (μ 0) := by - exact Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upL ψ μ + exact Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := complexLorentz) upL ψ μ /-- The components `T^αβ̇` of the tensor of `T` in `LeftHandedWeyl ⊗ RightHandedWeyl` are the entries of the matrix `leftRightToMatrix T`. -/ @@ -666,10 +666,7 @@ lemma tensorBasis_repr_toTensor_leftRight (T : LeftHandedWeyl ⊗[ℂ] RightHand (μ : Π i, Idx (prodColors ![upL] ![upR] i).kind) : (complexLorentz.tensorBasis _).repr (Tensorial.toTensor T) μ = leftRightToMatrix T (μ (.inl 0)) (μ (.inr 0)) := - Tensorial.tensorBasis_repr_toTensor_prod (S := complexLorentz) (c₁ := ![upL]) (c₂ := ![upR]) - LeftHandedWeyl.basis RightHandedWeyl.basis (fun ν => ν 0) (fun ν => ν 0) - (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upL) - (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) upR) T μ + Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace (S := complexLorentz) upL upR T μ /-- The pairing `ψ^α φ_α` of a spinor with a dual spinor is invariant. -/ example (g : SL(2, ℂ)) (ψ : LeftHandedWeyl) (φ : DualLeftHandedWeyl) : diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 8f85af6ae2..3e3e1c6dff 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -362,7 +362,7 @@ noncomputable instance coVectorTensorial (d : ℕ) : /-- The components of the tensor of a vector are its components, `v^μ`. -/ lemma tensorBasis_repr_toTensor_vector (d : ℕ) (v : Vector d) (μ : Fin 1 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis ![up]).repr (Tensorial.toTensor v) μ = v (μ 0) := - Species.tensorBasis_repr_oneSlotEquiv (S := realLorentz d) up v μ + Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := realLorentz d) up v μ /-- The vector of a tensor `T` with one upper index has the components of `T`, `v^ν = T^ν`. -/ lemma toTensor_symm_vector_apply (d : ℕ) (T : (realLorentz d).Tensor ![up]) @@ -375,7 +375,7 @@ lemma toTensor_symm_vector_apply (d : ℕ) (T : (realLorentz d).Tensor ![up]) /-- The components of the tensor of a covector are its components, `ω_μ`. -/ lemma tensorBasis_repr_toTensor_coVector (d : ℕ) (ω : CoVector d) (μ : Fin 1 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis ![down]).repr (Tensorial.toTensor ω) μ = ω (μ 0) := - Species.tensorBasis_repr_oneSlotEquiv (S := realLorentz d) down ω μ + Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := realLorentz d) down ω μ /-- The tensor of the basis vector `e_μ` is the basis tensor with label `μ`. -/ lemma toTensor_basis_vector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : @@ -393,9 +393,7 @@ lemma tensorBasis_repr_toTensor_vector_tmul (d : ℕ) (F : Vector d ⊗[ℝ] Vec (μ : Fin 1 ⊕ Fin 1 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis (prodColors ![up] ![up])).repr (Tensorial.toTensor F) μ = (Vector.basis.tensorProduct Vector.basis).repr F (μ (.inl 0), μ (.inr 0)) := - Tensorial.tensorBasis_repr_toTensor_prod (S := realLorentz d) Vector.basis Vector.basis - (fun ν => ν 0) (fun ν => ν 0) (tensorBasis_repr_toTensor_vector d) - (tensorBasis_repr_toTensor_vector d) F μ + Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace (S := realLorentz d) up up F μ /-- The component `F^μν` of `F` in `Vector d ⊗ Vector d`, its coordinate at `(μ, ν)` in Mathlib's product basis `e_μ ⊗ e_ν`. -/ @@ -431,9 +429,7 @@ lemma tensorBasis_repr_toTensor_coVector_tmul (d : ℕ) (T : CoVector d ⊗[ℝ] (μ : Fin 1 ⊕ Fin 1 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis (prodColors ![down] ![up])).repr (Tensorial.toTensor T) μ = (CoVector.basis.tensorProduct Vector.basis).repr T (μ (.inl 0), μ (.inr 0)) := - Tensorial.tensorBasis_repr_toTensor_prod (S := realLorentz d) CoVector.basis Vector.basis - (fun ν => ν 0) (fun ν => ν 0) (tensorBasis_repr_toTensor_coVector d) - (tensorBasis_repr_toTensor_vector d) T μ + Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace (S := realLorentz d) down up T μ /-- The contraction `v^μ ω_μ` of a vector with a covector, a tensor with no slots whose one component is `∑ μ, v^μ ω_μ`. -/ From da13075b786b0527446a73d28772866c97c7811b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:59:47 +0000 Subject: [PATCH 031/242] refactor(Conjugation): sesqForm and its lemmas moved to HermitianForm; ConjLabels for ofBasis; star_contr renamed star_contr_conjEquiv; conj_contr along ColorEquiv.barContr Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Conjugation.lean | 259 ++++-------------- .../Mathematics/Tensors/HermitianForm.lean | 159 +++++++++++ .../Tensors/ComplexTensor/Species.lean | 6 +- 4 files changed, 217 insertions(+), 208 deletions(-) create mode 100644 Physlib/Mathematics/Tensors/HermitianForm.lean diff --git a/Physlib.lean b/Physlib.lean index ad2b8666d9..bc3c97b5c0 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -205,6 +205,7 @@ public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.HermitianForm public import Physlib.Mathematics.Tensors.Inverse public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 7d04ec9bbb..971d1a155a 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -6,8 +6,7 @@ Authors: Andrea Pari module public import Physlib.Mathematics.ForMathlib.SemilinearMaps -public import Physlib.Mathematics.ForMathlib.SesquilinearForm -public import Physlib.Mathematics.Tensors.Inverse +public import Physlib.Mathematics.Tensors.Contraction /-! # Complex conjugation of tensors @@ -18,81 +17,34 @@ The conjugate of a tensor conjugates its components and moves each index to the Conjugation conjugates the components of a tensor and moves each index to the conjugate kind, keeping its variance. A left-handed spinor `ψ_α` becomes a right-handed spinor `ψ̄_α̇`, and a -vector index is its own conjugate, so `v̄^μ` is again a vector. - -A species declares conjugation in two parts. First, its kind type has an instance of Mathlib's -`InvolutiveStar`, whose `star` says which kind is conjugate to which, with `star (star κ) = κ`. -Complex Lorentz lists its kinds and defines `star` by cases. - -```lean -inductive Kind | left | right | vec - -instance : InvolutiveStar Kind where - star | left => right | right => left | vec => vec - star_involutive κ := by cases κ <;> rfl -``` - -The conjugate color `c.bar = ⟨star c.kind, c.variance⟩` has the conjugate kind and the same -variance. Second, the species gives the conjugation maps as data, the class -`Species.HasConjugation`, with two laws. Conjugating twice gives back the vector, and conjugation -preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`. That it commutes with the group action, -`(Λv)‾ = Λ v̄`, is a separate property, `Species.IsEquivariantConjugation`, as symmetry is a -separate property of a form. Conjugation is structure that the species declares, not a consequence -of its bases, so the library assumes nothing about the basis labels of a kind and of its -conjugate. - -The conjugate `S.conjEquiv h v` of a vector `v` of color `c` is a vector of color `c'`, and -`S.conjEquiv h` is a conjugate-linear equivalence `V c ≃ V c'`. The target color is given with -the equation `h : c' = c.bar`, which holds by `rfl` at concrete colors, as in -`S.conjEquiv rfl v`. The laws are stated in this form, so that conjugating twice gives back the -vector (`conjEquiv_conjEquiv`) with no cast along `c.bar.bar = c`. - -Most species conjugate componentwise, `(v̄)^μ = (v^μ)*`. The constructor -`Species.HasConjugation.ofBasis` builds the conjugation from the bases of the species and a label -map from each kind to its conjugate kind. It commutes with the group action when the matrices of -the group at `c.bar` are the conjugates of those at `c` -(`HasConjugation.isEquivariantConjugation_ofBasis`), which is needed at the upper colors only. -The identification of the labels of a kind with those of its conjugate lives in this label map, -which the species gives, and not in the library. For complex Lorentz both kinds of spinor have the -labels `Fin 2` and the map is the identity. - -The conjugate `Tensor.conj h T` of a tensor `T` applies the conjugation to every slot, with the -target colors given by `h`, slot by slot. It is conjugate-linear, an involution -(`Tensor.conj_conj`), commutes with contraction (`Tensor.conj_contr`), and for an equivariant -conjugation with the group action (`Tensor.conj_smul`). Its components are the conjugates of those -of `T` when the species conjugates componentwise (`Tensor.tensorBasis_repr_conj_of_basis`). - -A tensor `g_{IJ̄}` in `V a ⊗ V b` with `b = a.bar` is read as a sesquilinear form on the vectors -(`sesqForm`), so that Hermiticity is Mathlib's `LinearMap.IsSymm` and positive-definiteness is -`LinearMap.IsPosDef` from `ForMathlib.SesquilinearForm`. The inverse `g^{J̄I}` in -`V b.dual ⊗ V a.dual` is again such a form, with `a.dual = b.dual.bar` -(`Color.dual_eq_bar_dual`). The inverse of an invertible Hermitian or positive-definite `g_{IJ̄}` -is again Hermitian or positive-definite, and over a field a positive-definite `g_{IJ̄}` is -invertible. +vector index is its own conjugate. + +The kinds of a species carry Mathlib's `InvolutiveStar`, whose `star` gives the conjugate kind, and +the conjugate color is `c.bar = ⟨star c.kind, c.variance⟩`. The species gives the conjugation maps +`S.conjEquiv h : V c ≃ V c'` as data (`Species.HasConjugation`), with the target color given by an +equation `h : c' = c.bar` that holds by `rfl` at concrete colors. Conjugating twice gives back the +vector and conjugation preserves the contraction. Commuting with the group action is the separate +property `Species.IsEquivariantConjugation`. + +Most species conjugate componentwise, `(v̄)^μ = (v^μ)*`, and get their conjugation from +`Species.HasConjugation.ofBasis` and a map `ConjLabels` from the labels of each kind to those of +its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every slot. ## ii. Key results - `Color.bar` is the conjugate color, with `Color.bar_bar` and `Color.bar_dual`. - `Species.HasConjugation` gives the conjugation of a species with its two laws, and - `Species.conjEquiv` is the conjugate of a vector. + `Species.conjEquiv` is the conjugate of a vector, with its laws `Species.conjEquiv_conjEquiv`, + `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. - `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action, and `Species.rep_dual_of_star_contr` that equivariance at a color gives it at the dual color. -- `Species.conjEquiv_conjEquiv` proves that conjugating twice gives back the vector, - `Species.star_contr` that conjugation preserves the contraction, and `Species.conjEquiv_rep` - that an equivariant conjugation commutes with the group action. -- `Species.HasConjugation.ofBasis` builds a componentwise conjugation from a label map, +- `Species.HasConjugation.ofBasis` builds a componentwise conjugation from `ConjLabels`, and `Species.HasConjugation.isEquivariantConjugation_ofBasis` gives its equivariance from the - matrices of the group, and `Species.basis_repr_conjEquiv_of_basis` gives the components of the - conjugate. + matrices of the group. - `Species.Tensor.conj` is the conjugate of a tensor, with `Species.Tensor.conj_conj`, - `Species.Tensor.conj_pure`, `Species.Tensor.conj_smul` and `Species.Tensor.conj_contr`. + `Species.Tensor.conj_smul` and `Species.Tensor.conj_contr`. - `Species.Tensor.tensorBasis_repr_conj_of_basis` proves that the components of the conjugate are the conjugates of the components. -- `Species.sesqForm` reads a tensor `g_{IJ̄}` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄`. -- `Species.isSymm_sesqForm_inverse` and `Species.isPosDef_sesqForm_inverse` prove that the inverse - of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite. -- `Species.toLin_injective_of_isPosDef` proves that the linear map of a positive-definite `g_{IJ̄}` - is injective, and `Species.toLin_bijective_of_isPosDef` that over a field it is bijective. ## iii. Table of contents @@ -102,8 +54,6 @@ invertible. - D. Conjugation of tensors - D.1. The group action and contraction - D.2. Components -- E. Hermitian forms and their inverses - - E.1. Positive-definite forms over a field ## iv. References @@ -148,6 +98,15 @@ lemma Color.dual_eq_bar_dual {a b : Color K} (hb : b = a.bar) : a.dual = b.dual. -/ +/-- A map reading the labels of a kind `κ` as labels of its conjugate kind `κ'`, given with an +equation `h : κ' = star κ`, that gives back the label when applied twice. -/ +structure ConjLabels (basisIdx : K → Type) where + /-- The labels of `κ` read as labels of its conjugate `κ'`. -/ + equiv {κ κ' : K} (h : κ' = star κ) : basisIdx κ ≃ basisIdx κ' + /-- Reading a label as a label of the conjugate kind and back gives the label. -/ + equiv_equiv {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ) : + equiv h' (equiv h μ) = μ + namespace Species variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} @@ -156,10 +115,8 @@ variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {basisIdx : K variable (S) in /-- A conjugation of the species, a conjugate-linear equivalence `v ↦ v̄` from each color `c` to -its conjugate `c'`. Conjugating twice gives back the vector, and conjugation preserves the -contraction. The conjugate color is given with an equation `h : c' = c.bar`, so that no law moves -a vector along `c.bar.bar = c`. Commuting with the group action is the separate property -`IsEquivariantConjugation`. -/ +its conjugate `c'`, given with an equation `h : c' = c.bar` so that no law moves a vector along +`c.bar.bar = c`. -/ class HasConjugation where /-- The conjugate of a vector of color `c`, a vector of the conjugate color `c'`. -/ conj {c c' : Color K} (h : c' = c.bar) : S.V c ≃ₗ⋆[k] S.V c' @@ -196,7 +153,7 @@ lemma conjEquiv_conjEquiv (h : c' = c.bar) (h' : c = c'.bar) (v : S.V c) : /-- Conjugation preserves the contraction, `(u^μ α_μ)* = ū^μ ᾱ_μ`, for `u` of color `c` and `α` of color `c.dual`. -/ -lemma star_contr (h : c' = c.bar) (u : S.V c) (α : S.V c.dual) : +lemma star_contr_conjEquiv (h : c' = c.bar) (u : S.V c) (α : S.V c.dual) : star (S.contr c (u ⊗ₜ[k] α)) = S.contr c' (S.conjEquiv h u ⊗ₜ[k] S.conjEquiv (congrArg Color.dual h) α) := HasConjugation.star_contr_conj h u α @@ -240,30 +197,25 @@ lemma rep_dual_of_star_contr (f : S.V c ≃ₗ⋆[k] S.V c') (f' : S.V c.dual variable (S) in /-- The componentwise conjugation, `(v̄)^{e μ} = (v^μ)*`, which sends the basis vector `e_μ` of -`c` to the basis vector `e_{e μ}` of the conjugate color. The label map `e h` reads a label of a -kind `κ` as a label of its conjugate `κ'`, given with an equation `h : κ' = star κ`, and reading -it back gives the label (`e_e`). -/ +`c` to the basis vector `e_{e μ}` of the conjugate color, with the labels read by `e`. -/ @[instance_reducible] -noncomputable def HasConjugation.ofBasis - (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') - (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) : +noncomputable def HasConjugation.ofBasis (e : ConjLabels basisIdx) : S.HasConjugation where - conj h := (S.basis _).starEquiv (S.basis _) (e (congrArg Color.kind h)) - conj_conj _ _ v := Module.Basis.starEquiv_starEquiv _ _ _ _ (e_e _ _) v + conj h := (S.basis _).starEquiv (S.basis _) (e.equiv (congrArg Color.kind h)) + conj_conj _ _ v := Module.Basis.starEquiv_starEquiv _ _ _ _ (e.equiv_equiv _ _) v star_contr_conj _ u α := Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α /-- The componentwise conjugation commutes with the group action when the matrix of each `g : G` at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The law is needed at the upper colors only, since it follows at the lower ones (`rep_dual_of_star_contr`). -/ -lemma HasConjugation.isEquivariantConjugation_ofBasis - (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') - (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) +lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) (rep_basis : ∀ (κ : K) (g : G) (μ ν : basisIdx κ), - (S.basis ⟨star κ, .up⟩).repr (S.rep ⟨star κ, .up⟩ g (S.basis ⟨star κ, .up⟩ (e rfl μ))) - (e rfl ν) = star ((S.basis ⟨κ, .up⟩).repr (S.rep ⟨κ, .up⟩ g (S.basis ⟨κ, .up⟩ μ)) ν)) : - letI := ofBasis S e e_e + (S.basis ⟨star κ, .up⟩).repr + (S.rep ⟨star κ, .up⟩ g (S.basis ⟨star κ, .up⟩ (e.equiv rfl μ))) (e.equiv rfl ν) = + star ((S.basis ⟨κ, .up⟩).repr (S.rep ⟨κ, .up⟩ g (S.basis ⟨κ, .up⟩ μ)) ν)) : + letI := ofBasis S e S.IsEquivariantConjugation := by - let := ofBasis S e e_e + let := ofBasis S e refine ⟨fun {c _} h g v => ?_⟩ subst h obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c @@ -276,12 +228,10 @@ lemma HasConjugation.isEquivariantConjugation_ofBasis · exact absurd rfl hv /-- The componentwise conjugate of the basis vector `e_μ` is the basis vector `e_{e μ}`. -/ -lemma HasConjugation.ofBasis_conjEquiv_basis - (e : ∀ {κ κ' : K}, κ' = star κ → basisIdx κ ≃ basisIdx κ') - (e_e : ∀ {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ), e h' (e h μ) = μ) +lemma HasConjugation.ofBasis_conjEquiv_basis (e : ConjLabels basisIdx) (h : c' = c.bar) (μ : basisIdx c.kind) : - letI := ofBasis S e e_e - S.conjEquiv h (S.basis c μ) = S.basis c' (e (congrArg Color.kind h) μ) := + letI := ofBasis S e + S.conjEquiv h (S.basis c μ) = S.basis c' (e.equiv (congrArg Color.kind h) μ) := Module.Basis.starEquiv_apply_self _ _ _ μ /-- The components of the conjugate are the conjugates of the components, @@ -337,25 +287,24 @@ lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : conj h (g • t) = g • conj h t := map_smul_of_pure (conj h) g (fun f => by simp [conjEquiv_rep]) t -variable {κ J : Type} [Fintype J] {b b' : κ → Color K} {a a' : J → Color K} +variable {κ J : Type} [Fintype J] {b : κ → Color K} {a : J → Color K} + +/-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of slots. -/ +def _root_.Tensors.ColorEquiv.barContr {c : ι → Color K} (σ : ColorEquiv c (contrColors b a)) : + ColorEquiv (fun i => (c i).bar) (contrColors (fun x => (b x).bar) fun j => (a j).bar) := + ⟨σ.toEquiv, fun j => by rcases j with _ | _ | _ <;> exact congrArg Color.bar (σ.color_eq _)⟩ /-- Conjugating a contraction is contracting the conjugate along the same pairs of slots, -`(T^ρμ_μ)‾ = T̄^ρμ_μ`. The relabeling `τ` of `T̄` moves the slots as `σ` does (`hτ`). -/ -lemma conj_contr (h : ∀ i, c' i = (c i).bar) (hb : ∀ x, b' x = (b x).bar) - (ha : ∀ j, a' j = (a j).bar) (σ : ColorEquiv c (contrColors b a)) - (τ : ColorEquiv c' (contrColors b' a')) (hτ : τ.toEquiv = σ.toEquiv) (t : S.Tensor c) : - conj hb (contr σ t) = contr τ (conj h t) := by - obtain rfl : c' = fun i => (c i).bar := funext h - obtain rfl : b' = fun x => (b x).bar := funext hb - obtain rfl : a' = fun j => (a j).bar := funext ha - obtain ⟨τ, hτ'⟩ := τ - subst hτ +`(T^ρμ_μ)‾ = T̄^ρμ_μ`. -/ +lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : + conj (fun _ => rfl) (contr σ t) = contr σ.barContr (conj (fun _ => rfl) t) := by induction t using inductionOn_pure with | hsmul r t ht => rw [map_smul, LinearEquiv.map_smulₛₗ, ht, LinearEquiv.map_smulₛₗ, map_smul] | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] | h f => simp only [contr_pure, conj_pure, LinearEquiv.map_smulₛₗ, starRingEnd_apply, star_prod, - star_contr rfl, conjEquiv_cast, cast_conjEquiv] + star_contr_conjEquiv rfl, conjEquiv_cast, cast_conjEquiv] + rfl /-! @@ -378,106 +327,6 @@ lemma tensorBasis_repr_conj_of_basis [Fintype ι] (h : ∀ i, c' i = (c i).bar) end Tensor -/-! - -## E. Hermitian forms and their inverses - --/ - -variable [S.HasConjugation] {a b : Color K} - -variable (S) in -/-- A tensor `g_{IJ̄}` in `V a ⊗ V b` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄` on the -vectors of color `a.dual`. The color `b` is the conjugate of `a` by `hb`, which holds by `rfl` at -concrete colors. The form is conjugate-linear in its first argument, Mathlib's convention, so it is -the physicist's `g(y, x) = g_{IJ̄} y^I x̄^J̄` with the arguments swapped. -/ -noncomputable def sesqForm (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) : - S.V a.dual →ₗ⋆[k] S.V a.dual →ₗ[k] k := - (((TensorProduct.mk k _ _).compr₂ (S.contr b.dual).toLinearMap).compl₂ (S.toLin g)).comp - (S.conjEquiv (congrArg Color.dual hb)).toLinearMap - -/-- The form on `x, y` contracts `x̄` with `g y`, where `g y` is the contraction of `g` with `y`. -/ -lemma sesqForm_apply (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (x y : S.V a.dual) : - S.sesqForm g hb x y = - S.contr b.dual (S.conjEquiv (congrArg Color.dual hb) x ⊗ₜ S.toLin g y) := - rfl - -/-- The form of the inverse on `g x` and `g y` is the conjugate of the form of `g` on `y` and `x`, -`g^{J̄I} (g y)_J̄ (g x)‾_I = (g_{IJ̄} x^I ȳ^J̄)*`. The inverse `g^{J̄I}` acts on barred -covectors, so its form is the conjugate of the usual `g^{IJ̄} w_I w̄'_J̄`. -/ -lemma sesqForm_inverse_toLin (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) - (x y : S.V a.dual) (hb : b = a.bar := by rfl) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - S.sesqForm (S.inverse g) ha (S.toLin g x) (S.toLin g y) = star (S.sesqForm g hb y x) := by - rw [sesqForm_apply, sesqForm_apply, toLin_inverse g h, LinearEquiv.coe_coe, - LinearEquiv.ofBijective_symm_apply_apply, ← S.contr_tmul_symm, star_contr ha, - conjEquiv_conjEquiv] - -/-- The inverse of a Hermitian `g_{IJ̄}` is Hermitian. -/ -lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsSymm := by - refine ⟨fun u w => ?_⟩ - obtain ⟨x, rfl⟩ := h.2 u - obtain ⟨y, rfl⟩ := h.2 w - rw [sesqForm_inverse_toLin g h _ _ hb ha, sesqForm_inverse_toLin g h _ _ hb ha, - starRingEnd_apply, star_star] - exact (hg.eq x y).symm - -/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives -`g(y, y) = 0`. -/ -lemma toLin_injective_of_isPosDef [Preorder k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by - rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] - intro y hy - by_contra hne - simpa [sesqForm_apply, hy] using hg.pos y hne - -/-- The inverse of a positive-definite `g_{IJ̄}` with bijective linear map is positive-definite. -/ -lemma isPosDef_sesqForm_inverse_of_bijective [LT k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsPosDef) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsPosDef := by - refine ⟨isSymm_sesqForm_inverse g h hg.isSymm ha, fun u hu => ?_⟩ - obtain ⟨x, rfl⟩ := h.2 u - rw [sesqForm_inverse_toLin g h _ _ hb ha, ← starRingEnd_apply, hg.isSymm.eq x x] - exact hg.pos x fun hx => hu (by rw [hx, map_zero]) - -end Species - -/-! - -### E.1. Positive-definite forms over a field - --/ - -namespace Species - -variable {k : Type} [Field k] [StarRing k] [Preorder k] {G : Type} [Group G] {basisIdx : K → Type} - [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] - {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} - -/-- A positive-definite `g_{IJ̄}` is invertible. Its linear map `y ↦ g y` is bijective. -/ -lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by - -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. - have hi := toLin_injective_of_isPosDef g hg - refine ⟨hi, ?_⟩ - have : ∀ c, Module.Finite k (S.V c) := fun c => Module.Finite.of_basis (S.basis c) - refine (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi - let f : S.V a.dual ≃ₗ⋆[k] S.V b.dual := S.conjEquiv (congrArg Color.dual hb) - rw [Module.finrank, rank_eq_of_equiv_equiv (starRingEnd k) f.toAddEquiv - (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ f r m), - ← Module.finrank, Module.finrank_eq_card_basis (S.basis b.dual), - Module.finrank_eq_card_basis (S.basis b)] - -/-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ -lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (hg : (S.sesqForm g hb).IsPosDef) (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsPosDef := - isPosDef_sesqForm_inverse_of_bijective g (toLin_bijective_of_isPosDef g hg) hg ha - end Species end Tensors diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean new file mode 100644 index 0000000000..28039fea9b --- /dev/null +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -0,0 +1,159 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.ForMathlib.SesquilinearForm +public import Physlib.Mathematics.Tensors.Conjugation +public import Physlib.Mathematics.Tensors.Inverse +/-! + +# Hermitian forms + +A tensor `g_{IJ̄}` in `V a ⊗ V b` with `b = a.bar` read as a sesquilinear form, and its inverse. + +## i. Overview + +A tensor `g_{IJ̄}` whose second color is the conjugate of the first is a sesquilinear form on the +vectors (`sesqForm`), so that Hermiticity is Mathlib's `LinearMap.IsSymm` and +positive-definiteness is `LinearMap.IsPosDef` from `ForMathlib.SesquilinearForm`. The inverse +`g^{J̄I}` in `V b.dual ⊗ V a.dual` is again such a form, since `a.dual = b.dual.bar` +(`Color.dual_eq_bar_dual`). The inverse of an invertible Hermitian or positive-definite `g_{IJ̄}` +is again Hermitian or positive-definite, and over a field a positive-definite `g_{IJ̄}` is +invertible. + +## ii. Key results + +- `Species.sesqForm` reads a tensor `g_{IJ̄}` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄`. +- `Species.isSymm_sesqForm_inverse` and `Species.isPosDef_sesqForm_inverse` prove that the inverse + of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite. +- `Species.toLin_injective_of_isPosDef` proves that the linear map of a positive-definite `g_{IJ̄}` + is injective, and `Species.toLin_bijective_of_isPosDef` that over a field it is bijective. + +## iii. Table of contents + +- A. Hermitian forms and their inverses +- B. Positive-definite forms over a field + +## iv. References + +None. + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensors + +namespace Species + +variable {K : Type} [InvolutiveStar K] {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] + {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} + +/-! + +## A. Hermitian forms and their inverses + +-/ + +variable (S) in +/-- A tensor `g_{IJ̄}` in `V a ⊗ V b` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄` on the +vectors of color `a.dual`. The color `b` is the conjugate of `a` by `hb`, which holds by `rfl` at +concrete colors. The form is conjugate-linear in its first argument, Mathlib's convention, so it is +the physicist's `g(y, x) = g_{IJ̄} y^I x̄^J̄` with the arguments swapped. -/ +noncomputable def sesqForm (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) : + S.V a.dual →ₗ⋆[k] S.V a.dual →ₗ[k] k := + (((TensorProduct.mk k _ _).compr₂ (S.contr b.dual).toLinearMap).compl₂ (S.toLin g)).comp + (S.conjEquiv (congrArg Color.dual hb)).toLinearMap + +/-- The form on `x, y` contracts `x̄` with `g y`, where `g y` is the contraction of `g` with `y`. -/ +lemma sesqForm_apply (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (x y : S.V a.dual) : + S.sesqForm g hb x y = + S.contr b.dual (S.conjEquiv (congrArg Color.dual hb) x ⊗ₜ S.toLin g y) := + rfl + +/-- The form of the inverse on `g x` and `g y` is the conjugate of the form of `g` on `y` and `x`, +`g^{J̄I} (g y)_J̄ (g x)‾_I = (g_{IJ̄} x^I ȳ^J̄)*`. The inverse `g^{J̄I}` acts on barred +covectors, so its form is the conjugate of the usual `g^{IJ̄} w_I w̄'_J̄`. -/ +lemma sesqForm_inverse_toLin (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) + (x y : S.V a.dual) (hb : b = a.bar := by rfl) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + S.sesqForm (S.inverse g) ha (S.toLin g x) (S.toLin g y) = star (S.sesqForm g hb y x) := by + rw [sesqForm_apply, sesqForm_apply, toLin_inverse g h, LinearEquiv.coe_coe, + LinearEquiv.ofBijective_symm_apply_apply, ← S.contr_tmul_symm, star_contr_conjEquiv ha, + conjEquiv_conjEquiv] + +/-- The inverse of a Hermitian `g_{IJ̄}` is Hermitian. -/ +lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsSymm := by + refine ⟨fun u w => ?_⟩ + obtain ⟨x, rfl⟩ := h.2 u + obtain ⟨y, rfl⟩ := h.2 w + rw [sesqForm_inverse_toLin g h _ _ hb ha, sesqForm_inverse_toLin g h _ _ hb ha, + starRingEnd_apply, star_star] + exact (hg.eq x y).symm + +/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives +`g(y, y) = 0`. -/ +lemma toLin_injective_of_isPosDef [Preorder k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by + rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] + intro y hy + by_contra hne + simpa [sesqForm_apply, hy] using hg.pos y hne + +/-- The inverse of a positive-definite `g_{IJ̄}` with bijective linear map is positive-definite. -/ +lemma isPosDef_sesqForm_inverse_of_bijective [LT k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsPosDef) + (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsPosDef := by + refine ⟨isSymm_sesqForm_inverse g h hg.isSymm ha, fun u hu => ?_⟩ + obtain ⟨x, rfl⟩ := h.2 u + rw [sesqForm_inverse_toLin g h _ _ hb ha, ← starRingEnd_apply, hg.isSymm.eq x x] + exact hg.pos x fun hx => hu (by rw [hx, map_zero]) + +end Species + +/-! + +## B. Positive-definite forms over a field + +-/ + +namespace Species + +variable {K : Type} [InvolutiveStar K] {k : Type} [Field k] [StarRing k] [Preorder k] {G : Type} + [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} + +/-- A positive-definite `g_{IJ̄}` is invertible. Its linear map `y ↦ g y` is bijective. -/ +lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by + -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. + have hi := toLin_injective_of_isPosDef g hg + refine ⟨hi, ?_⟩ + have : ∀ c, Module.Finite k (S.V c) := fun c => Module.Finite.of_basis (S.basis c) + refine (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi + let f : S.V a.dual ≃ₗ⋆[k] S.V b.dual := S.conjEquiv (congrArg Color.dual hb) + rw [Module.finrank, rank_eq_of_equiv_equiv (starRingEnd k) f.toAddEquiv + (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ f r m), + ← Module.finrank, Module.finrank_eq_card_basis (S.basis b.dual), + Module.finrank_eq_card_basis (S.basis b)] + +/-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ +lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} + (hg : (S.sesqForm g hb).IsPosDef) (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : + (S.sesqForm (S.inverse g) ha).IsPosDef := + isPosDef_sesqForm_inverse_of_bijective g (toLin_bijective_of_isPosDef g hg) hg ha + +end Species + +end Tensors diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index c6478915b8..db589a40b6 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -388,12 +388,12 @@ lemma idxBar_idxBar {κ κ' : Kind} (h : κ' = star κ) (h' : κ = star κ') (μ /-- Conjugation swaps left- and right-handed indices and fixes vector indices, conjugating the components. -/ noncomputable instance : complexLorentz.HasConjugation := - .ofBasis complexLorentz idxBar idxBar_idxBar + .ofBasis complexLorentz ⟨idxBar, idxBar_idxBar⟩ /-- Conjugation commutes with the action of `SL(2, ℂ)`. The right-handed representation is the conjugate of the left-handed one, and the vector representation is real. -/ instance : complexLorentz.IsEquivariantConjugation := - Species.HasConjugation.isEquivariantConjugation_ofBasis idxBar idxBar_idxBar fun κ g => by + Species.HasConjugation.isEquivariantConjugation_ofBasis ⟨idxBar, idxBar_idxBar⟩ fun κ g => by match κ with | left => exact fun μ ν => (RightHandedWeyl.rep_apply_basis_repr g μ ν).trans @@ -416,7 +416,7 @@ lemma tensorBasis_repr_conj {ι : Type} [Fintype ι] {c c' : ι → Color Kind} star ((complexLorentz.tensorBasis c).repr t fun i => (idxBar (congrArg Color.kind (h i))).symm (μ i)) := Species.Tensor.tensorBasis_repr_conj_of_basis h _ - (fun i => Species.HasConjugation.ofBasis_conjEquiv_basis _ _ (h i)) t μ + (fun i => Species.HasConjugation.ofBasis_conjEquiv_basis _ (h i)) t μ end complexLorentz From 5f63c7ecb645736c18601be9a5cdc10ed7b90f09 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:59:58 +0000 Subject: [PATCH 032/242] doc(Reindexing): shorten the module overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Reindexing.lean | 35 +++++---------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 1c9848a1d2..0abab7e9e3 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -15,33 +15,14 @@ transpose `T^μ_ν ↦ T_ν^μ`. ## i. Overview -A tensor has one slot for each index, and each slot has a color, such as up or down. `T^μ_ν` has -two slots, the first up and the second down. The transpose `T_ν^μ` has the same components with -the indices written in the other order. Its first slot is down and its second is up. - -A relabeling `σ` is a permutation of the indices. It says which position each index moves to, and -it may only move an index together with its color. For the transpose, `σ` swaps the positions `0` -and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would turn an up -index into a down one is not a relabeling. That operation is raising or lowering, and it needs a -metric. - -Formally `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the slots, where the -labels `ι₁` and `ι₂` may be different types, together with the proof -`σ.color_eq j : c₁ (σ.toEquiv.symm j) = c₂ j` that the new slot `j` has the color of the old slot -it comes from. For concrete colors the proof is found automatically. - -`S.reindex σ : S.Tensor c₁ ≃ₗ[k] S.Tensor c₂` moves each vector to its new slot, so the transpose -sends `u ⊗ α` to `α ⊗ u` and has components `(Tᵀ)_ν^μ = T^μ_ν`. Nothing is computed. Lean writes -the color of the new slot differently from the color of the old one, so each vector is retyped -along `σ.color_eq`, and for concrete colors this retyping is the identity. The name follows -Mathlib's `PiTensorProduct.reindex`. - -Relabelings appear wherever indices are lined up. Index notation relabels `B` in `A^μ_ν + B_ν^μ` -before adding, so the result is the tensor `(A + Bᵀ)^μ_ν` with the slots in the order of `A`. The -symmetry `g_μν = g_νμ` compares `g` with its relabeling. The product `u^μ v^ν` of two vectors, -whose slots are labeled "slot of `u`" and "slot of `v`" (`Fin 1 ⊕ Fin 1`), is relabeled to a -tensor with slots `0, 1` (`Fin 2`). A relabeling can change the type of the labels as well as -their order. +A relabeling `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the slots of a tensor +that keeps their colors, with the proof `σ.color_eq`. For the transpose `T^μ_ν ↦ T_ν^μ` it swaps +the slots `0` and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would +turn an up index into a down one is not a relabeling, since that needs a metric. The labels `ι₁` +and `ι₂` may be different types, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the product `u^μ v^ν`. + +`S.reindex σ` moves each vector of a tensor to its new slot. The name follows Mathlib's +`PiTensorProduct.reindex`. Index notation uses it to line up indices, as in `A^μ_ν + B_ν^μ`. ## ii. Key results From adf87d658e7afe84420405fee53b82bff5a99c67 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:00:24 +0000 Subject: [PATCH 033/242] doc(Metric): shorten the module overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 48 ++++++++----------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 37c98b69b6..bd8392f2f5 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -17,40 +17,20 @@ one. ## i. Overview -An invariant form lowers and raises indices. For Lorentz tensors it is the Minkowski metric, -`η_μν` in `(ℝ⁴)* ⊗ (ℝ⁴)*`, with inverse `η^μν` in `ℝ⁴ ⊗ ℝ⁴`. It lowers `v_ν = v^μ η_μν`, raises -`w^ν = w_μ η^μν`, and the two are inverse, `v^μ η_μν η^νρ = v^ρ`. - -An invariant form on a kind `κ`, the class `S.HasForm κ`, is a chosen invariant invertible -bilinear form. It gives an element `form c` of `V c ⊗ V c` at both colors of the kind, written -`g^μν` at the upper color and `g_μν` at the lower one. It is invariant under the group, and the -forms at `c` and at `c.dual` are inverse to each other. Not every kind needs a form. The SU(3) -species has one on the adjoint and none on the triplet, so it has a `HasForm` instance at the -adjoint only and lowering a triplet index does not typecheck. The form is a choice, since `-η` -serves as well as `η`. The class stores the inverse, checked by the law `form_inverse`, since over -a commutative ring `k` a non-degenerate form need not have an inverse. - -Symmetry is an optional property of the chosen form. The class `S.IsSymmetricForm κ` says that -`g_μν = g_νμ`, as for `η`, and a metric is a symmetric form. The class `S.IsAntisymmetricForm κ` -says that `g_μν = -g_νμ`, as for the Weyl `ε`, which with two labels gives the Schouten identity. -Both are stated on the tensor `g`, and `formBilin` carries them to Mathlib's bilinear forms. A form -on a kind that is a sum of two kinds may be neither, and lowering and raising never ask for either -property. - -`S.toDual c : V c ≃ V c.dual` contracts a vector with the first index of the form at `c.dual`. At -an upper color it lowers the index, `v^μ ↦ v^μ g_μν`, and at a lower color it raises it, -`w_μ ↦ w_μ g^μν`. Its inverse is `S.toDual c.dual`, by `rfl`. For a symmetric metric such as `η` -the first index gives the textbook `v_ν = η_νμ v^μ`. For the antisymmetric `ε` it gives -`ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`. With `ε^12 = ε_21 = 1` this is minus the convention -`ψ_α = ε_αβ ψ^β`, so a Weyl species following that convention stores `-ε`. - -`S.toDualAt i …` lowers or raises the index at the slot `i` of a tensor and keeps the others, as -in `T^μν ↦ T^μ_ν` for `i` the second slot. Lowering at several slots composes slot by slot -(`toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), so a lowering and a raising at the same slot -cancel in any order. - -`S.formTensor c` is the form as a tensor on two slots, `η^μν` at `⟨vec, up⟩` and `η_μν` at -`⟨vec, down⟩`. Lowering an index is contracting it with the form tensor, `T^…_ν… = T^…μ… g_μν`. +For Lorentz tensors the invariant form is the Minkowski metric `η_μν` with inverse `η^μν`. It +lowers `v_ν = v^μ η_μν`, raises `w^ν = w_μ η^μν`, and the two are inverse, +`v^μ η_μν η^νρ = v^ρ`. + +The class `S.HasForm κ` is a chosen invariant form on the kind `κ`, `g^μν` at the upper color and +`g_μν` at the lower one, with the forms at `c` and `c.dual` inverse to each other. Not every kind +has one, as the SU(3) triplet has none. The inverse is stored, since over a commutative ring a +non-degenerate form need not have an inverse. Symmetry, as for `η`, and antisymmetry, as for the +Weyl `ε`, are the optional classes `S.IsSymmetricForm κ` and `S.IsAntisymmetricForm κ`. + +`S.toDual c` contracts a vector with the first index of the form at `c.dual`, lowering an upper +index and raising a lower one, and `S.toDualAt i` does so at the slot `i` of a tensor. For the +antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus the convention +`ψ_α = ε_αβ ψ^β` with `ε^12 = ε_21 = 1`, so a Weyl species following that convention stores `-ε`. ## ii. Key results From 83fc375e23bd24336ee8d6dd92de51f74469213c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:05:31 +0000 Subject: [PATCH 034/242] refactor(Species): complexLorentz and realLorentz as def; basis_tensorProduct_repr_comm_form on complexLorentz.basis; tensor_slots closes by simp Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 7 +------ Physlib/Relativity/Tensors/ComplexTensor/Species.lean | 9 +++++---- Physlib/Relativity/Tensors/RealTensor/Species.lean | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index e00f59014f..e9f691e1e8 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -728,12 +728,7 @@ elab "tensor_slots" : tactic => do Tensors.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, - Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual] <;> - -- On a species that is an `abbrev`, `simp` cannot rewrite under `toDual`: the types - -- unfold and their instances do not. `rw` checks its motive at default transparency. - (repeat rw [Tensors.Species.cast_apply]) <;> (repeat rw [cast_cast]) <;> - (repeat rw [cast_eq]) <;> (repeat rw [Tensors.Species.toDual_toDual]) <;> - (repeat rw [cast_cast]) <;> rfl)) + Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual])) end Tensors.Notation diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index db589a40b6..6f84eeac40 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -204,7 +204,7 @@ end complexLorentz open complexLorentz in /-- Complex Lorentz tensors: Weyl spinors and complex Lorentz vectors with the action of `SL(2, ℂ)`, on the spaces, representations and bases of `Fermion` and `Lorentz`. -/ -noncomputable abbrev complexLorentz : Species ℂ SL(2, ℂ) Kind Idx where +noncomputable def complexLorentz : Species ℂ SL(2, ℂ) Kind Idx where V := IndexSpace rep := indexRep basis := indexBasis @@ -518,7 +518,7 @@ namespace complexLorentz /-- Swapping the two indices of the form transposes its matrix. -/ lemma basis_tensorProduct_repr_comm_form (c : Color Kind) (μ ν : Idx c.kind) : - ((indexBasis c).tensorProduct (indexBasis c)).repr + ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr (TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c)) (μ, ν) = formEntries c ν μ := by rw [Module.Basis.tensorProduct_repr_comm] @@ -526,7 +526,8 @@ lemma basis_tensorProduct_repr_comm_form (c : Color Kind) (μ ν : Idx c.kind) : /-- The Minkowski metric `η` on the vector kind is symmetric. -/ instance : complexLorentz.IsSymmetricForm vec where - comm_form v := ((indexBasis ⟨vec, v⟩).tensorProduct (indexBasis ⟨vec, v⟩)).repr.injective + comm_form v := ((complexLorentz.basis ⟨vec, v⟩).tensorProduct + (complexLorentz.basis ⟨vec, v⟩)).repr.injective (Finsupp.ext fun ⟨μ, ν⟩ => by rw [basis_tensorProduct_repr_comm_form] change _ = Species.formMatrix complexLorentz _ μ ν @@ -537,7 +538,7 @@ instance : complexLorentz.IsSymmetricForm vec where /-- The form `ε` is antisymmetric at the Weyl colors. -/ lemma comm_form_weyl (c : Color Kind) (h : c.kind ≠ vec) : TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c) = -Species.form c := - ((indexBasis c).tensorProduct (indexBasis c)).repr.injective + ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr.injective (Finsupp.ext fun ⟨μ, ν⟩ => by rw [basis_tensorProduct_repr_comm_form, map_neg, Finsupp.neg_apply] change _ = -Species.formMatrix complexLorentz _ μ ν diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 3e3e1c6dff..0acf108000 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -161,7 +161,7 @@ open realLorentz in /-- Real Lorentz tensors in `d + 1` dimensions, on `Vector d` and `CoVector d` with their representations and bases. The labels `Fin 1 ⊕ Fin d` put time first. At a concrete color each field is the `Vector` or `CoVector` one by `rfl`, as in `(realLorentz d).rep up = Vector.rep`. -/ -noncomputable abbrev realLorentz (d : ℕ) : +noncomputable def realLorentz (d : ℕ) : Species ℝ (LorentzGroup d) Kind (fun _ => Fin 1 ⊕ Fin d) where V := IndexSpace d rep := IndexSpace.rep d From 49ab834f5cd20ac1a2a2e1f423ab10307a5ff6c6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 13:31:04 +0000 Subject: [PATCH 035/242] feat(PauliMatrices): trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix; pauliCoDown_trace_pauliCo from it Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 28 ++++++++++--------- .../Relativity/PauliMatrices/SelfAdjoint.lean | 9 ++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index f5bf793e96..e305711aaa 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -28,12 +28,14 @@ The lowered forms are written with `↓` in index notation, such as `{pauliTenso `σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, `{pauliTensor | μ ↓α ↓β}ᵀ` is `σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix `σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. -One identity is proved in components, the trace identity `pauliCoDown_trace_pauliCo`, -`tr(σ̄_μ σ_ν) = 2 η_μν`. The others are derived from it at tensor level, by `tensor_regroup` and -`tensor_slots`. The Fierz identity reads the trace identity as `B ∘ A = 2` for the maps `pauliA` -and `pauliB` between tensors with a vector index and tensors with two Weyl indices. Both spaces -have dimension `16`, so `A ∘ B = 2` as well, which is the Fierz identity. The anticommutators -follow from the trace identities and the Schouten identity of `ε`. +One identity is read off from the Pauli matrices, the trace identity `pauliCoDown_trace_pauliCo`, +`tr(σ̄_μ σ_ν) = 2 η_μν`: its components are the matrix traces of +`PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix`. The others are derived from +it at tensor level, by `tensor_regroup` and `tensor_slots`. The Fierz identity reads the trace +identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB` between tensors with a vector index +and tensors with two Weyl indices. Both spaces have dimension `16`, so `A ∘ B = 2` as well, which +is the Fierz identity. The anticommutators follow from the trace identities and the Schouten +identity of `ε`. ## ii. Key results @@ -172,9 +174,9 @@ lemma pauliContrDown_eq_pauliCoDown_contr : ## C. Contraction identities -The trace identity `pauliCoDown_trace_pauliCo` is proved in components, from the entries of the -Pauli matrices. The other identities are derived from it at tensor level: the Fierz identity by -reading the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB`, and the +The trace identity `pauliCoDown_trace_pauliCo` is proved in components, from the trace identity +of the Pauli matrices. The other identities are derived from it at tensor level: the Fierz +identity by reading the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB`, and the anticommutators by the Schouten identity of `ε`. ### C.1. Traces @@ -186,10 +188,10 @@ lemma pauliCoDown_trace_pauliCo : {pauliTensor | ↓μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • η' | μ ν}ᵀ := by tensor_components [tensorBasis_repr_pauliTensor_downAll, tensorBasis_repr_pauliTensor_down, tensorBasis_repr_formTensor_vec .down] - generalize φ 0 = m, φ 1 = n - rcases m with m | m <;> rcases n with n | n <;> fin_cases m <;> fin_cases n <;> - simp [Fin.sum_univ_two, pauliMatrix, minkowskiMatrix.inl_0_inl_0, - minkowskiMatrix.inr_i_inr_i, minkowskiMatrix.off_diag_zero] <;> ring_nf + -- The components are `tr(σ̄_ν σ_μ)`, written out as a double sum. + exact (Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => mul_comm _ _).trans + ((Matrix.trace_mul_comm _ _).trans + (PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix (φ 0) (φ 1))) /-- The trace identity `tr(σ_μ σ̄_ν) = 2 η_μν`, as `σT_μ^{αβ̇} σT_{ναβ̇} = 2 η_μν`. -/ lemma pauliCo_trace_pauliCoDown : diff --git a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean index 91d2279493..927ebfd2c1 100644 --- a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean +++ b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean @@ -108,6 +108,15 @@ lemma trace_pauliMatrix_mul_pauliMatrix (μ ν : Fin 1 ⊕ Fin 3) : Matrix.trace (pauliMatrix μ * pauliMatrix ν) = ((2 * kroneckerDelta μ ν : ℕ) : ℂ) := by fin_cases μ <;> fin_cases ν <;> simp [kroneckerDelta, pauliMatrix] <;> norm_num +/-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, with `σ̄^ν = η_νν σ^ν = (1, -σ⃗)`. -/ +lemma trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix (μ ν : Fin 1 ⊕ Fin 3) : + trace (pauliMatrix μ * ((minkowskiMatrix ν ν : ℂ) • pauliMatrix ν)) = + 2 * minkowskiMatrix μ ν := by + rw [Matrix.mul_smul, trace_smul, trace_pauliMatrix_mul_pauliMatrix, smul_eq_mul] + rcases eq_or_ne μ ν with rfl | h + · simp [mul_comm] + · simp [eq_zero_of_ne h, minkowskiMatrix.off_diag_zero h] + /-- The four real Pauli coefficients of a self-adjoint `2 × 2` matrix. -/ noncomputable def pauliCoeff (A : selfAdjoint (Matrix (Fin 2) (Fin 2) ℂ)) : From 5204f050250eb3b0a9a59a6ef2d3224935679602 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:14:43 +0000 Subject: [PATCH 036/242] feat(Tensor): finrank_tensor, Module.Finite instance on tensors Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 4771fc8105..b315274609 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -65,7 +65,7 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. - `Species.smul_eq_self_of_isEmpty` proves that a tensor with no slots is invariant. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the - components of a pure tensor. + components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. - `Species.congrSlots` applies an equivalence of representations at every slot. - `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_comp` and @@ -295,6 +295,15 @@ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] +/-- Tensors on finitely many slots are a finite-dimensional space. -/ +noncomputable instance [Fintype ι] : Module.Finite k (S.Tensor c) := + .of_basis (S.tensorBasis c) + +/-- The dimension of tensors is the product of the dimensions of their slots. -/ +lemma finrank_tensor [StrongRankCondition k] [Fintype ι] [DecidableEq ι] (c : ι → Color K) : + Module.finrank k (S.Tensor c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by + rw [Module.finrank_eq_card_basis (S.tensorBasis c), Fintype.card_pi] + /-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of `g` per index, in the color of that index. -/ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor c) From 4018c077083050fcbe756f44add05c335a65b225 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:14:43 +0000 Subject: [PATCH 037/242] feat(Contraction): ColorEquiv.noPairs, contr_noPairs Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index a5e3d2f491..b1fb67095e 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -59,6 +59,8 @@ since the bases of a color and its dual are dual. - `ColorEquiv.SamePair` says that two choices of pairs contract the same two slots, and `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of slots, in any order, are equal. +- `Species.Tensor.contr_noPairs` proves that a contraction with no pairs, along + `ColorEquiv.noPairs`, is the identity. - `Species.Tensor.contr_eq_zero_of_reindex_eq_neg` and `Species.Tensor.contr_tmulEquiv_eq_zero_of_antisymm_symm` prove `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. @@ -182,6 +184,25 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) +/-- The relabeling of a contraction with no pairs, where every slot is free and keeps its label. -/ +def _root_.Tensors.ColorEquiv.noPairs (c : ι₁ → Color K) : + ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where + toEquiv.toFun := .inl + toEquiv.invFun := Sum.elim id (Sum.elim Empty.elim Empty.elim) + toEquiv.left_inv _ := rfl + toEquiv.right_inv := by rintro (x | e | e) <;> first | rfl | exact e.elim + color_eq := by rintro (x | e | e) <;> first | rfl | exact e.elim + +/-- A contraction with no pairs is the identity. Any tensor is thus a contraction, as needed to +compare it with one. -/ +lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := by + induction t using inductionOn_pure with + | hsmul r t ht => rw [map_smul, ht] + | hadd t t' ht ht' => rw [map_add, ht, ht'] + | h p => + rw [contr_pure, Finset.univ_eq_empty, Finset.prod_empty, one_smul] + exact congrArg S.pure (funext fun x => by simp only [cast_apply]; rfl) + /-! ### B.1. Relabeling the slots From c8412d07a964d1278bcb452d0362ae22daf18561 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:14:43 +0000 Subject: [PATCH 038/242] feat(Elab): tensor_regroup inserts deltas, writes a delta as a form and its inverse, swaps symmetric and antisymmetric forms, expands lowerings as a fallback, accepts sides with no contraction Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 320 +++++++++++++++++++++++--- 1 file changed, 287 insertions(+), 33 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index e9f691e1e8..23823af0eb 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -64,7 +64,8 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. - `tensor_components` : reduces an equation of tensors to an equation of their components. - `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped - or ordered differently, with the same signs and scalars. + or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, + swaps of symmetric and antisymmetric forms, and lowerings written as contractions. - `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -556,13 +557,12 @@ private def arrange (target : List Nat) (equal : Nat → Nat → Bool) : TacticM swapFactors q n m (equal labels[m]! labels[m+1]!) labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! -/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors. It writes -each side as one contraction of a product, orders the factors of the right side as on the left, -and checks by evaluation that both sides keep the same slots and contract the same pairs of slots +/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same +factors. It orders the factors of the right side as on the left, and checks by evaluation that both +sides keep the same slots and contract the same pairs of slots (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn. -/ -private def regroupContractions : TacticM Unit := do - flattenContractions +private def matchFactors : TacticM Unit := do let (p, q) ← contractedProducts unless isLeftNested p && isLeftNested q do throwError "tensor_regroup: a side is not a left-nested product after flattening" @@ -597,6 +597,123 @@ private def regroupContractions : TacticM Unit := do throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ of slots. The last attempt failed with\n{lastError}" +/-- Whether `e` is a contraction `Tensor.contr σ P`. -/ +private def isContr (e : Expr) : Bool := + let e := e.consumeMData + e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.Tensor.contr + +/-- The factors among `xs` that are left over once each factor of `ys` takes an equal factor of +`xs`. -/ +private def leftover (xs ys : List Expr) : TacticM (List Expr) := do + let mut rest := xs + for y in ys do + for (x, i) in rest.zipIdx do + if ← withNewMCtxDepth (isDefEq x y) then + rest := rest.eraseIdx i + break + return rest + +/-- The color `a` of a delta `S.deltaTensor a`. -/ +private def deltaColor? (x : Expr) : Option Expr := + let x := x.consumeMData + if x.isAppOf ``Tensors.Species.deltaTensor then some x.appArg! else none + +/-- The color `c` of a form `S.formTensor c`. -/ +private def formColor? (x : Expr) : Option Expr := + let x := x.consumeMData + if x.isAppOf ``Tensors.Species.formTensor then some x.appFn!.appArg! else none + +/-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, +if `lhs`, or of the right side, by `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`). -/ +private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : TacticM Unit := do + let (p, q) ← contractedProducts + let thm ← `(Tensors.Species.Tensor.contr_deltaTensor ($(← slots.elem slot) : $(← slots.type)) + $(← exprToSyntax a) rfl $(← exprToSyntax (if lhs then p else q))) + if lhs then evalTactic (← `(tactic| conv_lhs => rw [← $thm:term])) + else evalTactic (← `(tactic| conv_rhs => rw [← $thm:term])) + +mutual + +/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, at each slot of +the product of color `a` in turn, and proves the goal by `regroupContractions`. -/ +private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) : TacticM Unit := do + let (p, q) ← contractedProducts + let ty ← whnfR (← inferType (if lhs then p else q)) + let args := ty.getAppArgs + let some slots ← Slots.ofType? args[args.size - 2]! + | throwError "tensor_regroup: the slots of a product are not `Fin n` and `⊕`" + let some colors ← colorList? args[args.size - 1]! + | throwError "tensor_regroup: the colors of a product are not a literal list" + let s ← saveState + let mut lastError : MessageData := m!"tensor_regroup: no slot has the color {a} of a delta" + for (col, slot) in colors.zipIdx do + unless ← withNewMCtxDepth (isDefEq col a) do continue + s.restore + try + insertDelta lhs a slots slot + regroupContractions + return + catch e => lastError := e.toMessageData + throw (.error (← getRef) lastError) + +/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to +deltas. A side that is not a contraction is read as one with no pairs +(`Species.Tensor.contr_noPairs`). Each side is flattened to one contraction of a left-nested +product. A delta on one side with no counterpart on the other is inserted on the other side at +each slot of its color in turn, since contracting with a delta is a relabeling. A delta against +two forms `g` and `g⁻¹` on the other side is written as their contraction, and two such forms +with nothing against them are matched by inserting a delta first. The factors are then matched +by `matchFactors`. -/ +private partial def regroupContractions : TacticM Unit := do + let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? + | throwError "tensor_regroup: the goal is not an equation" + unless isContr l do + evalTactic (← `(tactic| refine (Tensors.Species.Tensor.contr_noPairs _).symm.trans ?_)) + unless isContr r do + evalTactic (← `(tactic| refine Eq.trans ?_ (Tensors.Species.Tensor.contr_noPairs _))) + flattenContractions + let (p, q) ← contractedProducts + let extraL ← leftover (factors p) (factors q) + let extraR ← leftover (factors q) (factors p) + -- A delta on one side against two forms on the other is written as the contraction of the + -- forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying the color of each. + -- Against nothing, a delta is inserted first. + for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do + unless gs.length == 2 && gs.all (formColor? · |>.isSome) do continue + let s ← saveState + match ds with + | [d] => + unless (deltaColor? d).isSome do continue + for g in gs do + let some c := formColor? g | continue + s.restore + try + let thm ← `(Tensors.Species.contr_formTensor_formTensor $(← exprToSyntax c)) + if lhs then evalTactic (← `(tactic| conv_lhs => rw [← $thm:term])) + else evalTactic (← `(tactic| conv_rhs => rw [← $thm:term])) + regroupContractions + return + catch _ => pure () + | [] => + for g in gs do + let some c := formColor? g | continue + s.restore + try + insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) + return + catch _ => pure () + | _ => pure () + s.restore + let some extraL := extraL.mapM deltaColor? | matchFactors + let some extraR := extraR.mapM deltaColor? | matchFactors + -- A delta left over on the right is inserted on the left, and conversely. + match extraR, extraL with + | a :: _, _ => insertDeltaAnywhere true a + | [], a :: _ => insertDeltaAnywhere false a + | [], [] => matchFactors + +end + /-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a @@ -616,40 +733,105 @@ private partial def summands (e : Expr) : List Expr := let e := e.consumeMData if e.isAppOfArity ``HAdd.hAdd 6 then summands e.appFn!.appArg! ++ summands e.appArg! else [e] +/-- The forms `S.formTensor c` in `e`, each once. -/ +private def forms (e : Expr) : MetaM (List Expr) := do + let arity ← forallTelescopeReducing (← getConstInfo ``Tensors.Species.formTensor).type + fun xs _ => pure xs.size + let found ← IO.mkRef ([] : List Expr) + -- `forEachExpr` visits each shared subterm once. + forEachExpr e fun x => do + if x.isAppOfArity ``Tensors.Species.formTensor arity then + found.modify fun fs => if fs.contains x then fs else fs ++ [x] + found.get + +/-- Rewrites the `k`-th occurrence of the form `F = S.formTensor c` in the goal by its swap, +`g^μν = -g^νμ` for an antisymmetric form (`Species.formTensor_antisymm`), if `antisym`, and +`g^μν = g^νμ` for a symmetric one (`Species.formTensor_symm`). -/ +private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do + let args := F.getAppArgs + let c ← exprToSyntax args[args.size - 2]! + let S ← exprToSyntax args[args.size - 3]! + let thm ← if antisym then + `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c + (σ := Tensors.ColorEquiv.mk (Equiv.swap 0 1)) rfl).symm) + else + `((Tensors.Species.formTensor_symm (S := $S) $c + (σ := Tensors.ColorEquiv.mk (Equiv.swap 0 1)) rfl).symm) + let kStx : TSyntax `num := quote k + evalTactic (← `(tactic| nth_rw $kStx:num [$thm:term])) + +mutual + /-- Proves an equation of two summands with the same sign and the same scalar, by regrouping the -contractions they multiply. -/ -private partial def regroupSummand : TacticM Unit := do +contractions they multiply. When exactly one side is negated, one antisymmetric form on either +side is swapped first, as in `ε^αβ = -ε^βα`. When the regrouping fails, one symmetric form is +swapped, as in `η^μν = η^νμ`. At most one form is swapped. -/ +private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := do let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let l := l.consumeMData let r := r.consumeMData - if l.isAppOfArity ``Neg.neg 3 && r.isAppOfArity ``Neg.neg 3 then + let negL := l.isAppOfArity ``Neg.neg 3 + let negR := r.isAppOfArity ``Neg.neg 3 + if negL && negR then evalTactic (← `(tactic| refine congrArg Neg.neg ?_)) - regroupSummand + regroupSummand flipped + else if negL != negR && !flipped then + flipOneForm true else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then let a := l.appFn!.appArg! unless ← withNewMCtxDepth (isDefEq a r.appFn!.appArg!) do throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) - regroupSummand + regroupSummand flipped else - try evalTactic (← `(tactic| with_reducible rfl)) catch _ => regroupContractions + try evalTactic (← `(tactic| with_reducible rfl)) catch _ => + let s ← saveState + try regroupContractions catch e => + if flipped then throw e + -- The two sides may differ by the swap of a symmetric form. + s.restore + try flipOneForm false catch _ => s.restore; throw e + +/-- Tries each occurrence of each form in the goal for `flipForm antisym`, then moves a new sign +out and regroups. A form of the other symmetry fails at `flipForm`. -/ +private partial def flipOneForm (antisym : Bool) : TacticM Unit := do + let goal ← instantiateMVars (← getMainTarget) + let s ← saveState + let mut lastError : MessageData := m!"tensor_regroup: no form to swap" + for F in ← forms goal do + -- The occurrences are tried in turn, until `nth_rw` finds no further one. + for k in List.range 16 do + s.restore + try flipForm antisym F (k + 1) catch _ => break + try + normalizeSums + regroupSummand true + return + catch e => lastError := e.toMessageData + throw (.error (← getRef) lastError) -/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of -the same factors, grouped or ordered differently, such as -`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, -negatives and scalars are first moved out of every product, contraction and relabeling. Each -summand on the left is then matched with a summand on the right with the same sign and scalar, -whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a -factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times. +end -The scalars on the two sides must agree up to definitional equality, as `2` and `2`, and every -summand that is not the same term on both sides must be a contraction once relabelings are -absorbed. -/ -elab "tensor_regroup" : tactic => do +/-- A proof of `e = e'` for a sum `e`, where `e'` replaces the summands of `e` from left to right +by the right sides of the proofs `hs`. The proofs left over are returned. -/ +private partial def sumCongr (e : Expr) (hs : List Expr) : MetaM (Expr × List Expr) := do + let e := e.consumeMData + if e.isAppOfArity ``HAdd.hAdd 6 then + let (p, hs) ← sumCongr e.appFn!.appArg! hs + let (q, hs) ← sumCongr e.appArg! hs + return (← mkCongr (← mkCongrArg e.appFn!.appFn! p) q, hs) + match hs with + | h :: hs => return (h, hs) + | [] => throwError "tensor_regroup: internal error, too few summand proofs" + +/-- The body of `tensor_regroup`: moves sums, negatives and scalars out, then proves the equation +summand by summand. -/ +private def regroup : TacticM Unit := do normalizeSums - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? + -- Moving the scalars out may already close the goal. + if (← getGoals).isEmpty then return + let some (ty, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? | throwError "tensor_regroup: the goal is not an equation" let ls := summands l let rs := summands r @@ -662,29 +844,68 @@ elab "tensor_regroup" : tactic => do -- and the sums then agree up to order. let main ← getMainGoal let mut used : List Nat := [] - let mut proofs : Array Term := #[] + let mut proofs : Array Expr := #[] for a in ls do let mut found := false for j in List.range rs.length do if used.contains j then continue let s ← saveState - let g ← mkFreshExprMVar (← mkEq a rs[j]!) + -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. + let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) try setGoals [g.mvarId!] regroupSummand unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" used := j :: used - proofs := proofs.push (← exprToSyntax (← instantiateMVars g)) + proofs := proofs.push (← instantiateMVars g) found := true break catch _ => s.restore unless found do throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" - setGoals [main] - -- `conv` closes the goal by `rfl` once the summands are rewritten in order. - for h in proofs do - unless (← getGoals).isEmpty do evalTactic (← `(tactic| conv_lhs => rw [$h:term])) - unless (← getGoals).isEmpty do evalTactic (← `(tactic| abel)) + -- The summands are replaced by congruence, not by `rw`: a summand can occur inside another one, + -- as `T` inside its relabeling. + let (pf, _) ← sumCongr l proofs.toList + let some (_, _, l') := (← inferType pf).eq? | throwError "tensor_regroup: internal error" + let rest ← mkFreshExprMVar (← mkEq l' r) + main.assign (← mkEqTrans pf rest) + setGoals [rest.mvarId!] + evalTactic (← `(tactic| first | with_reducible rfl | abel)) + +/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of +the same factors, grouped or ordered differently, such as +`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, +negatives and scalars are first moved out of every product, contraction and relabeling. Each +summand on the left is then matched with a summand on the right with the same sign and scalar, +whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a +factor appears more than once, each way of matching the equal factors is tried in turn, so the +number of attempts is the product of `k!` over the factors that appear `k` times. + +Beyond regrouping, it uses these identities: +- a delta on one side with no counterpart on the other is inserted on the other side at a slot of + its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on + the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` + (`Species.contr_formTensor_formTensor`), and two such forms against nothing are matched by + inserting a delta first; +- when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` + (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, + `η^μν = η^νμ` (`Species.formTensor_symm`); +- when no matching is found, lowerings and raisings are expanded into contractions with the form + (`Species.toDualAt_eq_contr`) and the matching is tried again. + +A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs +(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional +equality, as `2` and `2`. -/ +elab "tensor_regroup" : tactic => do + let s ← saveState + try regroup catch e => + -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They + -- are expanded only when needed, since every expanded form is one more factor to match. + s.restore + let before ← instantiateMVars (← getMainTarget) + evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + if (← instantiateMVars (← getMainTarget)) == before then throw e + regroup /-! @@ -865,10 +1086,39 @@ example : {T | μ ν ⊗ U | ν ρ + -W | μ ρ = -W | μ ρ + U | ν ρ ⊗ T | example : {T | μ ν ⊗ (U | ν ρ + W | ν ρ) = U | ν ρ ⊗ T | μ ν + W | ν ρ ⊗ T | μ ν}ᵀ := by tensor_regroup +/-- A delta on one side is inserted on the other, `T^μ_ν = T^μ_ρ δ^ρ_ν`. The left side is a single +tensor, read as a contraction with no pairs. -/ +example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by + tensor_regroup + +/-- A delta inside a sum, `T^μ_ν + U^μ_ρ δ^ρ_ν = U^μ_ν + T^μ_ν`. -/ +example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by + tensor_regroup + section Metric variable [S.HasForm κ] [S.HasForm c.kind] +/-- A lowering is a contraction with the form, `v_μ = v^ν g_νμ`. -/ +example : {v | ↓μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν μ}ᵀ := by + tensor_regroup + +/-- A form and its inverse are matched by inserting a delta, `v^μ = v^ν g_νρ g^ρμ`. -/ +example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ}ᵀ := by + tensor_regroup + +/-- Summands of opposite signs are matched by swapping an antisymmetric form, +`M^μν g_νρ = -(g_ρν M^μν)`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = + -{S.formTensor ⟨κ, .down⟩ | ρ ν ⊗ M | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- A symmetric form is swapped when the regrouping fails, `M^μν g_νρ = M^μν g_ρν`. -/ +example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by + tensor_regroup + /-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ @@ -908,6 +1158,10 @@ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTenso ext x fin_cases x <;> rfl +/-- The same by `tensor_regroup`, which writes the delta as the contraction of the two forms. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + tensor_regroup + end Metric section Tensorial From 70dc1c092b202403755fe0634a1990222a230056 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 14:14:43 +0000 Subject: [PATCH 039/242] refactor(Bispinors): Pauli identities by tensor_regroup with no manual deltas or summand splits; pauliA_pauliB by finrank_tensor; pauliTensor_eq_contr_deltaL and pauliTensor_eq_contr_deltaR removed Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 68 ++++--------------- .../Tensors/ComplexTensor/Species.lean | 5 +- 2 files changed, 16 insertions(+), 57 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index e305711aaa..7f8430e628 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -164,10 +164,6 @@ lemma tensorBasis_repr_pauliTensor_downAll /-- `σT^μ_{αβ̇} = σT_{ραβ̇} η^ρμ`, raising the vector index of the fully lowered Pauli tensor. -/ lemma pauliContrDown_eq_pauliCoDown_contr : {pauliTensor | μ ↓α ↓β = {pauliTensor | ↓ρ ↓α ↓β ⊗ η | ρ μ}ᵀ | α β μ}ᵀ := by - -- Write `μ` as lowered and raised again, and the raising as a contraction with `η`. - have h : {pauliTensor | μ ↓α ↓β = {pauliTensor | ↓μ ↓α ↓β}ᵀ | ↑μ α β}ᵀ := by - tensor_slots - rw [h, Species.toDualAt_eq_contr] tensor_regroup /-! @@ -199,7 +195,8 @@ lemma pauliCo_trace_pauliCoDown : have h : {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = {pauliTensor | ↓ν ↓α ↓β ⊗ pauliTensor | ↓μ α β}ᵀ | ν μ}ᵀ := by tensor_regroup - rw [h, pauliCoDown_trace_pauliCo, map_smul, ← formTensor_down_symm] + rw [h, pauliCoDown_trace_pauliCo] + tensor_regroup /-- The mixed trace identity `tr(σ̄^μ σ_ν) = 2 δ^μ_ν`, as `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ^μ_ν`. -/ lemma pauliContrDown_trace_pauliCo : @@ -209,11 +206,6 @@ lemma pauliContrDown_trace_pauliCo : rw [pauliContrDown_eq_pauliCoDown_contr] tensor_regroup rw [h, pauliCoDown_trace_pauliCo] - simp only [map_smul, ← TensorProduct.smul_tmul'] - congr 1 - conv_lhs => rw [formTensor_down_symm] - rw [← (Species.contr_formTensor_formTensor (S := complexLorentz) down : - _ = complexLorentz.deltaTensor up)] tensor_regroup /-! @@ -242,18 +234,13 @@ lemma pauliB_pauliA (p : ℂT[up, upL, upR]) : pauliB (pauliA p) = (2 : ℂ) • simp only [pauliA, pauliB, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup rw [h, pauliContrDown_trace_pauliCo] - conv_rhs => rw [← Species.Tensor.contr_deltaTensor (0 : Fin 3) up rfl p] tensor_regroup /-- `pauliA ∘ pauliB = 2`: both spaces have dimension `16`, so the left inverse `pauliB / 2` of `pauliA` is a right inverse. -/ lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : ℂ) • X := by - have := Module.Finite.of_basis (complexLorentz.tensorBasis ![up, upL, upR]) - have := Module.Finite.of_basis (complexLorentz.tensorBasis ![upL, upR, upL, upR]) have hdim : Module.finrank ℂ ℂT[up, upL, upR] = Module.finrank ℂ ℂT[upL, upR, upL, upR] := by - rw [Module.finrank_eq_card_basis (complexLorentz.tensorBasis _), - Module.finrank_eq_card_basis (complexLorentz.tensorBasis _)] - simp [Fintype.card_pi, Fin.prod_univ_succ] + rw [Species.finrank_tensor, Species.finrank_tensor] rfl have h : ((2 : ℂ)⁻¹ • pauliB) ∘ₗ pauliA = LinearMap.id := LinearMap.ext fun p => by simp [pauliB_pauliA] @@ -268,7 +255,7 @@ lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α /-- `σT^μ_{αβ̇} ε^{αα'} ε^{β̇β̇'} = σT^{μα'β̇'}`. -/ lemma pauliContrDown_contr_eps : {pauliTensor | μ ↓α ↓β ⊗ εL | α α' ⊗ εR | β β' = pauliTensor | μ α' β'}ᵀ := by - conv_rhs => rw [← pauliTensor_raise_lower, Species.toDualAt_eq_contr, Species.toDualAt_eq_contr] + conv_rhs => rw [← pauliTensor_raise_lower] tensor_regroup /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is @@ -290,9 +277,6 @@ lemma pauliCo_contr_pauliContr : /-- `σT^{μαβ̇} = σT_ρ^{αβ̇} η^{ρμ}`, raising the lowered vector index again. -/ lemma pauliTensor_eq_pauliCo_contr : {pauliTensor | μ α β = pauliTensor | ↓ρ α β ⊗ η | ρ μ}ᵀ := by - have h : {pauliTensor | μ α β = {pauliTensor | ↓μ α β}ᵀ | ↑μ α β}ᵀ := by - tensor_slots - conv_lhs => rw [h, Species.toDualAt_eq_contr] tensor_regroup /-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{αβ̇} = 2 η^μν`. -/ @@ -303,7 +287,6 @@ lemma pauliContr_trace_pauliContrDown : nth_rw 1 [pauliTensor_eq_pauliCo_contr] tensor_regroup rw [h, pauliContrDown_trace_pauliCo] - conv_rhs => rw [formTensor_up_symm, ← Species.Tensor.contr_deltaTensor (0 : Fin 2) up rfl η] tensor_regroup /-- The trace identity `tr(σ̄^μ σ^ν) = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇} = 2 η^μν`. -/ @@ -312,16 +295,7 @@ lemma pauliContrDown_trace_pauliContr : have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = {pauliTensor | ν α β ⊗ pauliTensor | μ ↓α ↓β}ᵀ | ν μ}ᵀ := by tensor_regroup - rw [h, pauliContr_trace_pauliContrDown, map_smul, ← formTensor_up_symm] - -/-- `σT^{μαβ̇} = σT^{μρβ̇} δ_ρ^α`. -/ -lemma pauliTensor_eq_contr_deltaL : {pauliTensor | μ α β = pauliTensor | μ ρ β ⊗ δL' | ρ α}ᵀ := by - conv_lhs => rw [← Species.Tensor.contr_deltaTensor (1 : Fin 3) upL rfl pauliTensor] - tensor_regroup - -/-- `σT^{μαβ̇} = σT^{μαρ̇} δ_ρ̇^β̇`. -/ -lemma pauliTensor_eq_contr_deltaR : {pauliTensor | μ α β = pauliTensor | μ α r ⊗ δR' | r β}ᵀ := by - conv_lhs => rw [← Species.Tensor.contr_deltaTensor (2 : Fin 3) upR rfl pauliTensor] + rw [h, pauliContr_trace_pauliContrDown] tensor_regroup /-- `σT^{μρβ̇} σT^{νγδ̇} ε_{δ̇β̇} (ε_{γρ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity @@ -332,9 +306,9 @@ lemma pauliContr_pauliContr_eps_epsL_deltaL : have h : {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = {pauliTensor | μ r β ⊗ pauliTensor | ν ↓r ↓β}ᵀ | μ ν ⊗ δL | α α'}ᵀ := by - simp only [Species.toDualAt_eq_contr] tensor_regroup - rw [h, pauliContr_trace_pauliContrDown, ← smul_tmul', map_smul, map_smul] + rw [h, pauliContr_trace_pauliContrDown] + tensor_regroup /-- The anticommutator `σ^μ σ̄^ν + σ^ν σ̄^μ = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{α'β̇} + σT^{ναβ̇} σT^μ_{α'β̇} = 2 η^μν δ^α_α'`. The Schouten identity splits @@ -342,16 +316,8 @@ lemma pauliContr_pauliContr_eps_epsL_deltaL : lemma pauliContr_mul_pauliContrDown_add : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α' ↓β + pauliTensor | ν α β ⊗ pauliTensor | μ ↓α' ↓β = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by - rw [← pauliContr_pauliContr_eps_epsL_deltaL, schouten_downL, map_add, tmul_add, map_add, - map_add, map_add] - refine congrArg₂ (· + ·) ?_ ?_ - · nth_rw 1 [pauliTensor_eq_contr_deltaL] - simp only [Species.toDualAt_eq_contr] - tensor_regroup - · nth_rw 1 [pauliTensor_eq_contr_deltaL] - rw [formTensor_downR_antisymm] - simp only [Species.toDualAt_eq_contr] - tensor_regroup + rw [← pauliContr_pauliContr_eps_epsL_deltaL, schouten_downL] + tensor_regroup /-- `σT^{μγδ̇} σT^{ναρ̇} ε_{γα} (ε_{δ̇ρ̇} δ_β̇^β̇') = 2 η^μν δ_β̇^β̇'`, the trace identity `pauliContrDown_trace_pauliContr` with the lowerings written as contractions with `ε`. -/ @@ -361,9 +327,9 @@ lemma pauliContr_pauliContr_eps_epsR_deltaR : have h : {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = {pauliTensor | μ ↓a ↓r ⊗ pauliTensor | ν a r}ᵀ | μ ν ⊗ δR' | β β'}ᵀ := by - simp only [Species.toDualAt_eq_contr] tensor_regroup - rw [h, pauliContrDown_trace_pauliContr, ← smul_tmul', map_smul, map_smul] + rw [h, pauliContrDown_trace_pauliContr] + tensor_regroup /-- The anticommutator `σ̄^μ σ^ν + σ̄^ν σ^μ = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇'} + σT^ν_{αβ̇} σT^{μαβ̇'} = 2 η^μν δ_β̇^β̇'`. The Schouten identity splits @@ -371,15 +337,7 @@ lemma pauliContr_pauliContr_eps_epsR_deltaR : lemma pauliContrDown_pauliContr_mul_add : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β' + pauliTensor | ν ↓α ↓β ⊗ pauliTensor | μ α β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by - rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR, map_add, tmul_add, map_add, - map_add, map_add] - refine congrArg₂ (· + ·) ?_ ?_ - · nth_rw 2 [pauliTensor_eq_contr_deltaR] - simp only [Species.toDualAt_eq_contr] - tensor_regroup - · nth_rw 2 [pauliTensor_eq_contr_deltaR] - rw [formTensor_downL_antisymm] - simp only [Species.toDualAt_eq_contr] - tensor_regroup + rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR] + tensor_regroup end Tensors.complexLorentz diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 6f84eeac40..1e7e250c03 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -603,8 +603,9 @@ lemma schouten_downR : Matrix.one_apply] rw [← sub_eq_add_neg] refine Eq.trans ?_ (Species.formMatrix_schouten downR (Fintype.card_fin 2) _ _ _ _) - congr 1 - split_ifs with h h' h' <;> first | rfl | exact absurd h.symm h' | exact absurd h'.symm h + -- The delta of `formMatrix_schouten` is `δ^α_α'`, read here as `δ_α'^α`. + congr 2 + exact propext eq_comm /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : From 1b183d0af13b3806b9ae72b2edb1febab83441de Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:11:19 +0000 Subject: [PATCH 040/242] refactor(TensorProductMatrix): tensorProductToMatrix_tmul; tensorProductToMatrix_map by induction from it Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/TensorProductMatrix.lean | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean index b6b20e4d9f..b045634eab 100644 --- a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -26,6 +26,8 @@ and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The m - `Module.Basis.tensorProductToMatrix` : the coefficient matrix of a tensor in `M ⊗ N`. - `Module.Basis.tensorProductToMatrix_symm_apply` : the tensor with coefficient matrix `X` is `∑ i j, X i j • b₁ i ⊗ b₂ j`. +- `Module.Basis.tensorProductToMatrix_tmul` : the coefficient matrix of `x ⊗ y` is the outer + product of the coordinates. - `Module.Basis.tensorProductToMatrix_map` : the coefficient matrix of `f ⊗ g` applied to `v`. - `Module.Basis.tensorProductToMatrix_symm_map` : `f ⊗ g` applied to the tensor with coefficient matrix `X`. @@ -70,28 +72,30 @@ noncomputable def tensorProductToMatrix : M ⊗[R] N ≃ₗ[R] Matrix ι κ R := lemma tensorProductToMatrix_apply (v : M ⊗[R] N) (i : ι) (j : κ) : b₁.tensorProductToMatrix b₂ v i j = (b₁.tensorProduct b₂).repr v (i, j) := rfl -variable [DecidableEq ι] [DecidableEq κ] - -/-- The tensor with coefficient matrix `X` is `∑ i j, X i j • b₁ i ⊗ b₂ j`. -/ -lemma tensorProductToMatrix_symm_apply (X : Matrix ι κ R) : - (b₁.tensorProductToMatrix b₂).symm X = ∑ i, ∑ j, X i j • (b₁ i ⊗ₜ[R] b₂ j) := by - rw [LinearEquiv.symm_apply_eq] +/-- The coefficient matrix of `x ⊗ y` is the outer product of the coordinates of `x` and `y`. -/ +lemma tensorProductToMatrix_tmul (x : M) (y : N) : + b₁.tensorProductToMatrix b₂ (x ⊗ₜ y) = vecMulVec (b₁.repr x) (b₂.repr y) := by ext i j - simp [Finsupp.single_apply, Finset.sum_apply] + simp [vecMulVec_apply, mul_comm] + +variable [DecidableEq ι] [DecidableEq κ] /-- The coefficient matrix of `f ⊗ g v` is `A * X * Bᵀ`, with `X` that of `v` and `A` and `B` the matrices of `f` and `g`. -/ lemma tensorProductToMatrix_map (f : M →ₗ[R] M) (g : N →ₗ[R] N) (v : M ⊗[R] N) : b₁.tensorProductToMatrix b₂ (TensorProduct.map f g v) = toMatrix b₁ b₁ f * b₁.tensorProductToMatrix b₂ v * (toMatrix b₂ b₂ g)ᵀ := by + induction v using TensorProduct.inductionOn with + | tmul x y => simp only [map_tmul, tensorProductToMatrix_tmul, mul_vecMulVec, vecMulVec_mul, + vecMul_transpose, toMatrix_mulVec_repr] + | add v w hv hw => simp only [map_add, hv, hw, Matrix.mul_add, Matrix.add_mul] + +/-- The tensor with coefficient matrix `X` is `∑ i j, X i j • b₁ i ⊗ b₂ j`. -/ +lemma tensorProductToMatrix_symm_apply (X : Matrix ι κ R) : + (b₁.tensorProductToMatrix b₂).symm X = ∑ i, ∑ j, X i j • (b₁ i ⊗ₜ[R] b₂ j) := by + rw [LinearEquiv.symm_apply_eq] ext i j - simp only [tensorProductToMatrix_apply, Matrix.mul_apply, Matrix.transpose_apply] - rw [← LinearMap.toMatrix_mulVec_repr (b₁.tensorProduct b₂) (b₁.tensorProduct b₂), - TensorProduct.toMatrix_map] - simp only [Matrix.mulVec, dotProduct, Fintype.sum_prod_type, kroneckerMap_apply, - Finset.sum_mul] - rw [Finset.sum_comm] - exact Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => by ring + simp [Finsupp.single_apply, Finset.sum_apply] /-- `f ⊗ g` sends the tensor with coefficient matrix `X` to the one with coefficient matrix `A * X * Bᵀ`. -/ From 22c244880cac80809f511623a35dd286269164ea Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:11:58 +0000 Subject: [PATCH 041/242] refactor(FiniteDimensional): LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq as a term from Function.LeftInverse.rightInverse_of_surjective Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/FiniteDimensional.lean | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index ce77c056c9..1b0d6a6b5b 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -47,12 +47,10 @@ variable {K V W : Type*} [DivisionRing K] [AddCommGroup V] [Module K V] [AddComm /-- Between spaces of equal finite dimension, a left inverse is a right inverse. -/ lemma LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq (h : finrank K V = finrank K W) - {f : V →ₗ[K] W} {g : W →ₗ[K] V} (hgf : g ∘ₗ f = LinearMap.id) : f ∘ₗ g = LinearMap.id := by - have hinj : Function.Injective f := fun v v' e => by - simpa [← LinearMap.comp_apply, hgf] using congrArg g e - ext w - obtain ⟨v, rfl⟩ := (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 hinj w - simp [← LinearMap.comp_apply g f, hgf] + {f : V →ₗ[K] W} {g : W →ₗ[K] V} (hgf : g ∘ₗ f = LinearMap.id) : f ∘ₗ g = LinearMap.id := + LinearMap.ext <| Function.LeftInverse.rightInverse_of_surjective (f := g) + (LinearMap.congr_fun hgf) <| + (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 <| injective_of_comp_eq_id f g hgf /-- `LinearMap.comp_eq_id_comm` between two spaces of equal finite dimension. -/ lemma LinearMap.comp_eq_id_comm_of_finrank_eq (h : finrank K V = finrank K W) {f : V →ₗ[K] W} From aebfe1e4c9942f295165d4d026d99294478412d2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:14:05 +0000 Subject: [PATCH 042/242] refactor(Contraction): Module.Basis.coordPairing_basis_tmul; coordPairing_comm by simp; lTensor_pairingToLinearMap_coevaluation from coordPairing_basis_tmul Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Contraction.lean | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 00f9d60707..bb482acf76 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -150,8 +150,12 @@ lemma eq_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (p : /-- `b.coordPairing b' (x ⊗ₜ y) = b'.coordPairing b (y ⊗ₜ x)`: swapping slots swaps the bases. -/ lemma coordPairing_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : b.coordPairing b' (x ⊗ₜ[R] y) = b'.coordPairing b (y ⊗ₜ[R] x) := by - rw [coordPairing_tmul, coordPairing_tmul] - exact Finset.sum_congr rfl fun I _ => mul_comm _ _ + simp only [coordPairing_tmul, mul_comm] + +/-- `b.coordPairing b' (b I ⊗ₜ y) = y_I`: pairing with the first basis reads off a coordinate. -/ +lemma coordPairing_basis_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I : ι) (y : N) : + b.coordPairing b' (b I ⊗ₜ[R] y) = b'.equivFun y I := by + rw [coordPairing_comm, coordPairing_tmul_basis] /-- The delta `∑_I b_I ⊗ b'_I ∈ M ⊗ N` of two bases with the same labels. -/ noncomputable def coevaluation (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N := @@ -177,8 +181,6 @@ lemma lTensor_pairingToLinearMap_coevaluation {U : Type*} [AddCommMonoid U] [Mod simp only [coevaluation, map_sum, LinearMap.lTensor_tmul, pairingToLinearMap_tmul, TensorProduct.tmul_smul] conv_rhs => rw [← b'.sum_repr w, TensorProduct.sum_tmul] - exact Finset.sum_congr rfl fun I _ => by - rw [coordPairing_comm, coordPairing_tmul_basis, Module.Basis.equivFun_apply, - TensorProduct.smul_tmul'] + simp only [coordPairing_basis_tmul, Module.Basis.equivFun_apply, TensorProduct.smul_tmul'] end Module.Basis From 4dc610578d68688f954ce20be902426475411121 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:17:13 +0000 Subject: [PATCH 043/242] style(FiniteDimensional): wrap LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq proof to 100 columns Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/FiniteDimensional.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index 1b0d6a6b5b..97ea489999 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -49,8 +49,8 @@ variable {K V W : Type*} [DivisionRing K] [AddCommGroup V] [Module K V] [AddComm lemma LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq (h : finrank K V = finrank K W) {f : V →ₗ[K] W} {g : W →ₗ[K] V} (hgf : g ∘ₗ f = LinearMap.id) : f ∘ₗ g = LinearMap.id := LinearMap.ext <| Function.LeftInverse.rightInverse_of_surjective (f := g) - (LinearMap.congr_fun hgf) <| - (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 <| injective_of_comp_eq_id f g hgf + (LinearMap.congr_fun hgf) <| (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 <| + injective_of_comp_eq_id f g hgf /-- `LinearMap.comp_eq_id_comm` between two spaces of equal finite dimension. -/ lemma LinearMap.comp_eq_id_comm_of_finrank_eq (h : finrank K V = finrank K W) {f : V →ₗ[K] W} From c451a1fa8af991f70bb3a48559ff932021d0e021 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:17:46 +0000 Subject: [PATCH 044/242] refactor(Species): toMatrix_toLin, contr_basis_tmul; toLin_comp_toLin_of_mul_eq_one by matrix multiplication, shorter basis_repr_toLin, toLin_map, delta_invariant, contr_tmul_rep, toLin_map_rep_bijective, ext_contr Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 64 ++++++++++-------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 97fb603b5b..5220b5bf20 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -47,8 +47,9 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. law from its upper colors. - `Species.contr` is the contraction `u^μ α_μ`, with `Species.contr_basis`, `Species.contr_tmul_symm` and `Species.contr_invariant`. -- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, and - `Species.ext_contr` proves that the contraction is nondegenerate. +- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, + `Species.contr_basis_tmul` reads off a component, and `Species.ext_contr` proves that the + contraction is nondegenerate. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. - `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and @@ -56,8 +57,8 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.snake` proves that the delta is the identity, `Species.toLin_map` that `toLin` commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that `(id ⊗ f) z = f ∘ z`. -- `Species.basis_repr_toLin` gives `toLin` in components, and - `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their +- `Species.basis_repr_toLin` gives `toLin` in components, `Species.toMatrix_toLin` its matrix, + and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their matrices are. - `Species.cast` moves a vector along an equality of colors, `Species.cast_rep` proves that this commutes with the group action, and `Species.basis_repr_cast` that it keeps components. @@ -162,17 +163,18 @@ lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := Module.Basis.coordPairing_comm _ _ _ _ +/-- Contracting the basis vector `e_μ` with `α` reads off the component `α_μ`. -/ +lemma contr_basis_tmul (c : Color K) (μ : basisIdx c.kind) (α : S.V c.dual) : + S.contr c (S.basis c μ ⊗ₜ α) = (S.basis c.dual).repr α μ := + (S.contr_tmul_symm c _ α).trans (Module.Basis.coordPairing_tmul_basis _ _ _ _) + variable {S} in /-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the same contraction against every vector of `c` are equal. -/ lemma ext_contr {c : Color K} {α α' : S.V c.dual} (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := (S.basis c.dual).repr.injective <| Finsupp.ext fun μ => by - -- Contracting `e_μ` with `α` reads off the component `α_μ`. - have hα (β : S.V c.dual) : S.contr c (S.basis c μ ⊗ₜ β) = (S.basis c.dual).repr β μ := by - rw [contr_tmul_symm] - exact Module.Basis.coordPairing_tmul_basis _ _ _ _ - simpa [hα] using h (S.basis c μ) + simpa [contr_basis_tmul] using h (S.basis c μ) /-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ lemma contr_invariant (c : Color K) (g : G) (x : S.V c) (y : S.V c.dual) : @@ -182,8 +184,7 @@ lemma contr_invariant (c : Color K) (g : G) (x : S.V c) (y : S.V c.dual) : /-- Contracting `u` with `Λα` is contracting `Λ⁻¹u` with `α`. -/ lemma contr_tmul_rep (c : Color K) (g : G) (u : S.V c) (α : S.V c.dual) : S.contr c (u ⊗ₜ S.rep c.dual g α) = S.contr c (S.rep c g⁻¹ u ⊗ₜ α) := by - conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g u] - exact S.contr_invariant c g _ α + simpa using S.contr_invariant c g (S.rep c g⁻¹ u) α omit S in /-- The law `invariant`, from an invariant pairing `p` for which the bases are dual. It is how a @@ -250,11 +251,7 @@ lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by induction z using TensorProduct.inductionOn with - | tmul w y => - change pairingToLinearMap (S.contr a.dual).toLinearMap (S.rep a g w ⊗ₜ f y) _ = - f (pairingToLinearMap (S.contr a.dual).toLinearMap (w ⊗ₜ y) x) - simp only [pairingToLinearMap_tmul, map_smul, Representation.IntertwiningMap.toLinearMap_apply] - rw [show S.rep a g w = S.rep a.dual.dual g w from rfl, S.contr_invariant] + | tmul w y => simpa using congrArg (· • f y) (S.contr_invariant a.dual g x w) | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] /-- The delta is invariant, `Λ δ = δ`. -/ @@ -263,9 +260,7 @@ lemma delta_invariant (c : Color K) (g : G) : -- Both act as the identity: `(Λδ)(Λx) = Λx` by the snake law. apply S.toLin.injective ext x - conv_lhs => rw [← Representation.self_inv_apply (S.rep c) g x] - rw [show S.rep c g⁻¹ x = S.rep c.dual.dual g⁻¹ x from rfl, S.toLin_map, S.snake c, - LinearMap.id_apply, LinearMap.id_apply, Representation.self_inv_apply] + simpa [snake] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) /-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : @@ -279,8 +274,7 @@ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin z)) (g : G) : Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := - Function.bijective_iff_has_inverse.mpr ⟨S.rep c g⁻¹, Representation.inv_self_apply _ _, - Representation.self_inv_apply _ _⟩ + (Module.End.isUnit_iff _).1 ((Group.isUnit g).map (S.rep c)) rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) @@ -296,12 +290,15 @@ lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, Finset.sum_add_distrib] | tmul x y => - change (S.basis d).repr (pairingToLinearMap (S.contr c.dual).toLinearMap (x ⊗ₜ y) v) ν = _ - rw [pairingToLinearMap_tmul, map_smul, Finsupp.smul_apply, smul_eq_mul] - change S.contr c.dual (v ⊗ₜ x) * _ = _ - simp only [contr_tmul_eq_sum_basis, Module.Basis.tensorProduct_repr_tmul_apply, smul_eq_mul, - Finset.sum_mul] - exact Finset.sum_congr rfl fun _ _ => by ring + simp [contr_tmul_eq_sum_basis, Finset.sum_mul, mul_assoc, mul_comm ((S.basis d).repr y ν)] + +/-- The matrix of `toLin z` is the transpose of the matrix of components of `z`, +`(z v)^ν = z_μ^ν v^μ`. -/ +lemma toMatrix_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) : + LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin z) = + (Matrix.of fun μ ν => ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν)).transpose := by + ext ν μ + simp [LinearMap.toMatrix_apply, basis_repr_toLin, Finsupp.single_apply] /-- `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of `F'` and `F` multiply to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ @@ -310,16 +307,9 @@ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * Matrix.of (fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr F (μ, ν)) = 1) : S.toLin F ∘ₗ S.toLin F' = LinearMap.id := by - refine (S.basis c).ext fun μ => (S.basis c).repr.injective (Finsupp.ext fun ν => ?_) - have hμν := congrFun (congrFun h μ) ν - rw [Matrix.mul_apply, Matrix.one_apply] at hμν - rw [LinearMap.comp_apply, LinearMap.id_apply, basis_repr_toLin, Module.Basis.repr_self, - Finsupp.single_apply, ← hμν] - refine Finset.sum_congr rfl fun ρ _ => congrArg (· * _) ?_ - -- The components of `F'` applied to `e_μ` are the row `μ` of its matrix. - rw [basis_repr_toLin c.dual F' (S.basis c μ)] - erw [Module.Basis.repr_self] - simp [Finsupp.single_apply] + apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective + rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin, toMatrix_toLin c.dual, + ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] /-! From d14db1f7585239003e134534bd3f51d3ef049fd8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:17:46 +0000 Subject: [PATCH 045/242] refactor(Tensor): mapSlots_smul, slotMapsAt_rep; congrSlots, congrSlots_pure and congrAt equivariance from mapSlots_smul Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 63 ++++++++++++++----------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index b315274609..d7feeb3c1b 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -67,9 +67,10 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. -- `Species.congrSlots` applies an equivalence of representations at every slot. - `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_comp` and - `Species.mapSlots_id`. + `Species.mapSlots_id`, and `Species.mapSlots_smul` proves that it commutes with the group when + every slot map does. +- `Species.congrSlots` applies an equivalence of representations at every slot. - `Species.slotMapsAt` is the family of a linear map at one slot and the identity at the others, and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and `Species.mapAt_id`. @@ -325,23 +326,6 @@ section variable {c' : ι → Color K} -variable (S) in -/-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations -of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ -noncomputable def congrSlots (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : - (S.tensorRep c).Equiv (S.tensorRep c') := - equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => (f x).toLinearEquiv) ≪≫ₗ - (S.toPiTensor c').symm) fun g p => by - simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, congr_tprod, - toPiTensor_symm_tprod] - exact congrArg S.pure (funext fun x => - Representation.IntertwiningMap.isIntertwining _ _ (f x).toIntertwiningMap g (p x)) - -/-- On a pure tensor, `congrSlots` applies `f x` to the vector at each slot `x`. -/ -lemma congrSlots_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : - S.congrSlots f (S.pure p) = S.pure fun x => f x (p x) := by - simp [congrSlots, congr_tprod] - variable (S) in /-- A linear map `f x` at every slot `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ @@ -374,6 +358,27 @@ lemma mapSlots_id : S.mapSlots (c := c) (fun _ => LinearMap.id) = LinearMap.id : lemma mapSlots_id_apply (t : S.Tensor c) : S.mapSlots (fun _ => LinearMap.id) t = t := LinearMap.congr_fun mapSlots_id t +/-- Linear maps that commute with the group action at every slot commute with it on tensors, +`f (Λ T) = Λ f T`. -/ +lemma mapSlots_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} + (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : S.Tensor c) : + S.mapSlots f (g • t) = g • S.mapSlots f t := + map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapSlots_pure, hf]) t + +variable (S) in +/-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations +of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ +noncomputable def congrSlots (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : + (S.tensorRep c).Equiv (S.tensorRep c') := + equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => (f x).toLinearEquiv) ≪≫ₗ + (S.toPiTensor c').symm) fun g p => + mapSlots_smul (fun x => (f x).toIntertwiningMap.isIntertwining _ _) g (S.pure p) + +/-- On a pure tensor, `congrSlots` applies `f x` to the vector at each slot `x`. -/ +lemma congrSlots_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : + S.congrSlots f (S.pure p) = S.pure fun x => f x (p x) := + mapSlots_pure (fun x => (f x).toLinearMap) p + section variable [DecidableEq ι] @@ -403,6 +408,15 @@ lemma eq_slotMapsAt {F : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (i : ι) (hne : ∀ x (hx : x ≠ i), F x = (S.cast (h x hx)).toLinearMap) : F = S.slotMapsAt i f h := Function.eq_update_iff.2 ⟨hi, fun x hx => by rw [hne x hx, dite_eq_right hx]⟩ +/-- `slotMapsAt i f h` commutes with the group action at every slot when `f` does. -/ +lemma slotMapsAt_rep (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) + (hf : ∀ g v, f (S.rep (c i) g v) = S.rep (c' i) g (f v)) (x : ι) (g : G) (v : S.V (c x)) : + S.slotMapsAt i f h x (S.rep (c x) g v) = S.rep (c' x) g (S.slotMapsAt i f h x v) := by + by_cases hx : x = i + · subst hx + simpa using hf g v + · simpa [hx] using cast_rep (h x hx) g v + variable (S) in /-- The linear map `f` at the slot `i`, and the identity, moved along `h x`, at every other slot `x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ @@ -450,15 +464,8 @@ noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) (by rw [mapAt_comp, show f.toLinearMap ∘ₗ f.symm.toLinearMap = LinearMap.id from LinearMap.ext f.apply_symm_apply, mapAt_id]) (by rw [mapAt_comp, show f.symm.toLinearMap ∘ₗ f.toLinearMap = LinearMap.id from - LinearMap.ext f.symm_apply_apply, mapAt_id])) fun g p => by - simp only [LinearEquiv.coe_ofLinearMap, mapAt, smul_pure, mapSlots_pure] - refine congrArg S.pure (funext fun x => ?_) - by_cases hx : x = i - · subst hx - simp only [slotMapsAt_apply_self, Representation.IntertwiningMap.coe_toLinearMap] - exact Representation.IntertwiningMap.isIntertwining _ _ f.toIntertwiningMap g (p x) - · simp only [slotMapsAt_apply_ne _ _ _ hx, Representation.IntertwiningMap.coe_toLinearMap] - exact cast_rep _ _ _ + LinearMap.ext f.symm_apply_apply, mapAt_id])) fun g p => + mapSlots_smul (slotMapsAt_rep i _ h (f.toIntertwiningMap.isIntertwining _ _)) g (S.pure p) /-- `congrAt` is `mapAt` of the underlying linear map. -/ lemma congrAt_eq_mapAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) From 0aea29581877f381e4bb04e2b2685103bfe7bd3f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:18:43 +0000 Subject: [PATCH 046/242] refactor(Reindexing): tensorBasis_repr_reindex by basis_repr_cast, without a generalized helper Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Reindexing.lean | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 0abab7e9e3..9f9c7f9bb7 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -206,14 +206,12 @@ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → | hadd t t' ht ht' => simp only [map_add, Finsupp.add_apply, ht, ht'] | h f => rw [reindex_pure, tensorBasis_repr_pure, tensorBasis_repr_pure] - refine (Fintype.prod_equiv σ.toEquiv _ _ fun i => ?_).symm - -- The old slot `σ.toEquiv.symm (σ.toEquiv i)` is `i` only up to a proof, so it is generalized. - have h : ∀ i' (_ : i' = i) (d : Color K) (p : c₁ i' = d) (q : d = c₁ i) (z : basisIdx d.kind), - (S.basis (c₁ i)).repr (f i) (cast (congrArg (basisIdx ∘ Color.kind) q) z) = - (S.basis d).repr (S.cast p (f i')) z := by - rintro _ rfl _ rfl _ _ - rfl - exact h _ (σ.toEquiv.symm_apply_apply i) _ _ (σ.symm.color_eq i) _ + refine Fintype.prod_equiv σ.toEquiv.symm _ _ fun j => ?_ + -- The label `y j` is read at the new slot `σ.toEquiv (σ.toEquiv.symm j)`, which is `j` only + -- up to a proof. + rw [basis_repr_cast] + exact congrArg _ (eq_of_heq ((cast_heq _ _).trans + ((congr_arg_heq y (σ.toEquiv.apply_symm_apply j)).symm.trans (cast_heq _ _).symm))) end Species From d490524f08267886188a05e0936d4c24fe21ef10 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:18:44 +0000 Subject: [PATCH 047/242] =?UTF-8?q?refactor(Product):=20tmulEquiv=5Fassoc?= =?UTF-8?q?=20by=20ext=5Fpure=E2=82=83=20in=20one=20term,=20with=20rTensor?= =?UTF-8?q?=20and=20lTensor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Product.lean | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index f47d643e9f..a219cf37c0 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -161,17 +161,17 @@ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃) (S.tmulEquiv _ _ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃)) = - S.tmulEquiv _ _ (t₁ ⊗ₜ S.tmulEquiv c₂ c₃ (t₂ ⊗ₜ t₃)) := by - have h := ext_pure₃ (f := (S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃)).toLinearMap ∘ₗ - (S.tmulEquiv _ c₃).toLinearMap ∘ₗ TensorProduct.map (S.tmulEquiv c₁ c₂).toLinearMap .id) - (g := (S.tmulEquiv c₁ _).toLinearMap ∘ₗ TensorProduct.map .id (S.tmulEquiv c₂ c₃).toLinearMap ∘ₗ - (TensorProduct.assoc k _ _ _).toLinearMap) fun f₁ f₂ f₃ => by - simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, assoc_tmul, - LinearMap.id_apply, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl) - simpa only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearEquiv.coe_coe, assoc_tmul, - LinearMap.id_apply, Representation.Equiv.coe_toLinearMap] using - LinearMap.congr_fun h ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) + S.tmulEquiv _ _ (t₁ ⊗ₜ S.tmulEquiv c₂ c₃ (t₂ ⊗ₜ t₃)) := + LinearMap.congr_fun (f := (S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃)).toLinearMap ∘ₗ + (S.tmulEquiv _ c₃).toLinearMap ∘ₗ (S.tmulEquiv c₁ c₂).toLinearMap.rTensor _) + (g := (S.tmulEquiv c₁ _).toLinearMap ∘ₗ (S.tmulEquiv c₂ c₃).toLinearMap.lTensor _ ∘ₗ + (TensorProduct.assoc k _ _ _).toLinearMap) + (ext_pure₃ fun f₁ f₂ f₃ => by + simp only [LinearMap.comp_apply, LinearMap.rTensor_tmul, LinearMap.lTensor_tmul, + LinearEquiv.coe_coe, assoc_tmul, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, + reindex_pure] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl)) + ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) /-- The product is symmetric, `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`, after the indices of `U` are moved in front by `ColorEquiv.sumComm`. -/ From 0b14088ef176aa8249dd439f378b0c38f2feb607 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:18:44 +0000 Subject: [PATCH 048/242] refactor(Contraction): contr_noPairs, reindex_contr, contr_eq_of_samePair, contr_contr, tmulEquiv_contr_left, tmulEquiv_contr_right, contr_matMulAt by ext lemmas; contr_deltaTensor by mapAt_id; tensorBasis_repr_contr from new tensorBasis_repr_contrStd Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 159 ++++++++----------- 1 file changed, 69 insertions(+), 90 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index b1fb67095e..466207f3b1 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -75,7 +75,8 @@ since the bases of a color and its dual are dual. - `Species.Tensor.contr_matMulAt` proves that contracting the index `i` with the first index of a tensor `F` on two slots, along `ColorEquiv.matMulAt`, applies `F` at `i`, and `Species.Tensor.contr_deltaTensor` that for the delta this is `T^…μ… δ_μ^ν = T^…ν…`. -- `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction. +- `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction, from + `Species.Tensor.tensorBasis_repr_contrStd` in the standard arrangement of slots. - `Species.Tensor.tensorBasis_repr_contr_one_pair` and `Species.Tensor.tensorBasis_repr_contr_two_pairs` give the same with one and with two pairs. @@ -195,13 +196,10 @@ def _root_.Tensors.ColorEquiv.noPairs (c : ι₁ → Color K) : /-- A contraction with no pairs is the identity. Any tensor is thus a contraction, as needed to compare it with one. -/ -lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := by - induction t using inductionOn_pure with - | hsmul r t ht => rw [map_smul, ht] - | hadd t t' ht ht' => rw [map_add, ht, ht'] - | h p => +lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := + DFunLike.congr_fun (intertwiningMap_ext_pure (ψ := .id _) fun p => by rw [contr_pure, Finset.univ_eq_empty, Finset.prod_empty, one_smul] - exact congrArg S.pure (funext fun x => by simp only [cast_apply]; rfl) + exact congrArg S.pure (funext fun x => by simp only [cast_apply]; rfl)) t /-! @@ -219,14 +217,13 @@ lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J /-- Relabeling the free indices of a contraction is contracting along the composite relabeling. -/ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : S.Tensor c) : - S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := by - induction t using inductionOn_pure with - | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] - | hadd t t' ht ht' => rw [map_add, map_add, ht, ht', map_add] - | h p => - rw [contr_pure, map_smul, reindex_pure, contr_pure] - congr 1 - exact congrArg S.pure (funext fun x => by simp only [cast_apply, cast_cast]; rfl) + S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := + DFunLike.congr_fun (intertwiningMap_ext_pure + (φ := (S.reindex τ).toIntertwiningMap.comp (contr σ)) fun p => by + rw [Representation.IntertwiningMap.comp_apply, Representation.Equiv.coe_toIntertwiningMap, + contr_pure, map_smul, reindex_pure, contr_pure] + congr 1 + exact congrArg S.pure (funext fun x => by simp only [cast_apply, cast_cast]; rfl)) t /-! @@ -299,23 +296,17 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : S.Tensor c) : contr σ t = contr τ t := by - induction t using inductionOn_pure with - | hsmul r t ht => rw [map_smul, map_smul, ht] - | hadd t t' ht ht' => rw [map_add, map_add, ht, ht'] - | h p => - rw [contr_pure, contr_pure] - choose g hg using h₁ - congr 1 - · -- The pair `j` of `σ` is the pair `g j` of `τ`, in the same order or swapped. - refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), - fun j' => (ColorEquiv.exists_samePair_left hfree (fun j => ⟨g j, hg j⟩) j').imp - fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ - rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ - · rw [cast_apply_congr p h, cast_apply_congr p h'] - exact contr_cast_tmul_cast _ _ _ _ _ _ - · rw [cast_apply_congr p h, cast_apply_congr p h'] - exact contr_cast_tmul_cast_symm _ _ _ _ _ _ - · exact congrArg S.pure (funext fun x => cast_apply_congr p (hfree x) _) + refine DFunLike.congr_fun (intertwiningMap_ext_pure fun p => ?_) t + rw [contr_pure, contr_pure] + choose g hg using h₁ + congr 1 + · -- The pair `j` of `σ` is the pair `g j` of `τ`, in the same order or swapped. + refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), + fun j' => (ColorEquiv.exists_samePair_left hfree (fun j => ⟨g j, hg j⟩) j').imp + fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ + rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ <;> rw [cast_apply_congr p h, cast_apply_congr p h'] + exacts [contr_cast_tmul_cast _ _ _ _ _ _, contr_cast_tmul_cast_symm _ _ _ _ _ _] + · exact congrArg S.pure (funext fun x => cast_apply_congr p (hfree x) _) /-! @@ -378,14 +369,11 @@ then `ν` in `T^μ_μ^ν_ν` is contracting both pairs at once. -/ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) (t : S.Tensor c) : - contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := by - induction t using inductionOn_pure with - | h f => - simp only [contr_pure, map_smul, smul_smul, Fintype.prod_sum_type, cast_apply, - cast_cast] - rfl - | hsmul r t ht => rw [map_smul, map_smul, ht, map_smul] - | hadd x y hx hy => rw [map_add, map_add, hx, hy, map_add] + contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := + DFunLike.congr_fun (intertwiningMap_ext_pure (φ := (contr σ₂).comp (contr σ₁)) fun f => by + simp only [Representation.IntertwiningMap.comp_apply, contr_pure, map_smul, smul_smul, + Fintype.prod_sum_type, cast_apply, cast_cast] + rfl) t /-! @@ -405,18 +393,15 @@ noncomputable def _root_.Tensors.ColorEquiv.tmulRight {c : ι₁ → Color K} {c lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (u : S.Tensor d) : S.tmulEquiv c' d (contr σ t ⊗ₜ u) = - contr (σ.tmulRight d) (S.tmulEquiv c d (t ⊗ₜ u)) := by - induction t using inductionOn_pure with - | hsmul r t ht => simp only [map_smul, ← smul_tmul', ht] - | hadd x y hx hy => simp only [map_add, add_tmul, hx, hy] - | h p => - induction u using inductionOn_pure with - | hsmul r u hu => simp only [map_smul, tmul_smul, hu] - | hadd x y hx hy => simp only [map_add, tmul_add, hx, hy] - | h p' => + contr (σ.tmulRight d) (S.tmulEquiv c d (t ⊗ₜ u)) := + LinearMap.congr_fun (f := (S.tmulEquiv c' d).toLinearMap ∘ₗ (contr σ).toLinearMap.rTensor _) + (g := (contr (σ.tmulRight d)).toLinearMap ∘ₗ (S.tmulEquiv c d).toLinearMap) + (ext_pure₂ fun p p' => by + simp only [LinearMap.comp_apply, LinearMap.rTensor_tmul, + Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap] rw [contr_pure, ← smul_tmul', map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] congr 1 - exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) + exact congrArg S.pure (funext fun y => by rcases y with x | m <;> rfl)) (t ⊗ₜ u) /-- The pairs `σ` of `T`, as pairs of `U ⊗ T` for a factor `U` on the left, with colors `d`. The indices of `U` are free and come before the free indices of `T`. -/ @@ -430,18 +415,15 @@ noncomputable def _root_.Tensors.ColorEquiv.tmulLeft {c : ι₁ → Color K} lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color K} (σ : ColorEquiv c (contrColors c' a)) (u : S.Tensor d) (t : S.Tensor c) : S.tmulEquiv d c' (u ⊗ₜ contr σ t) = - contr (σ.tmulLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := by - induction t using inductionOn_pure with - | hsmul r t ht => simp only [map_smul, tmul_smul, ht] - | hadd x y hx hy => simp only [map_add, tmul_add, hx, hy] - | h p => - induction u using inductionOn_pure with - | hsmul r u hu => simp only [map_smul, ← smul_tmul', hu] - | hadd x y hx hy => simp only [map_add, add_tmul, hx, hy] - | h p' => + contr (σ.tmulLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := + LinearMap.congr_fun (f := (S.tmulEquiv d c').toLinearMap ∘ₗ (contr σ).toLinearMap.lTensor _) + (g := (contr (σ.tmulLeft d)).toLinearMap ∘ₗ (S.tmulEquiv d c).toLinearMap) + (ext_pure₂ fun p' p => by + simp only [LinearMap.comp_apply, LinearMap.lTensor_tmul, + Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap] rw [contr_pure, tmul_smul, map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] congr 1 - exact congrArg (PiTensorProduct.tprod k) (funext fun y => by rcases y with x | m <;> rfl) + exact congrArg S.pure (funext fun y => by rcases y with x | m <;> rfl)) (u ⊗ₜ t) /-! @@ -557,14 +539,13 @@ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) contr (ColorEquiv.matMulAt i a b ha hi h) (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoSlotEquiv a.dual b F)) = S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by - induction t using inductionOn_pure with - | hsmul r t ht => rw [← smul_tmul', map_smul, map_smul, ht, map_smul] - | hadd t t' ht ht' => rw [add_tmul, map_add, map_add, ht, ht', map_add] - | h p => - let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) - rw [contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx]), - mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) - (Function.update_self _ _ _) (fun x hx => by simp [q, hx])] + refine LinearMap.congr_fun (f := (contr _).toLinearMap ∘ₗ (S.tmulEquiv c ![a.dual, b]).toLinearMap + ∘ₗ (TensorProduct.mk k _ _).flip (S.twoSlotEquiv a.dual b F)) (ext_pure fun p => ?_) t + -- The vectors of `p` moved along `h` away from `i`. The vector at `i` is replaced below. + let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) + exact (contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx])).trans + (mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) + (Function.update_self _ _ _) (fun x hx => by simp [q, hx])).symm /-- Contracting the index `i` with the first index of the delta is the identity, `T^…μ… δ_μ^ν = T^…ν…`. -/ @@ -572,16 +553,9 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (t : S.Tensor c) : contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by + subst ha rw [deltaTensor, contr_matMulAt, snake] - induction t using inductionOn_pure with - | hsmul r t ht => rw [map_smul, ht] - | hadd t t' ht ht' => rw [map_add, ht, ht'] - | h p => - refine mapAt_pure i _ _ p p ?_ fun _ _ => rfl - simp only [LinearMap.coe_comp, Function.comp_apply, - Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, - LinearMap.id_apply, cast_apply, cast_cast] - rfl + exact LinearMap.congr_fun (mapAt_id i _) t /-! @@ -589,19 +563,15 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) -/ -/-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. The labels `μ` run -over the pairs, and the components of `T` are those of `S.reindex σ T`. -/ -lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] - {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) - (φ : Π x, basisIdx (c' x).kind) : - (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, - (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := by - simp only [contr, Representation.IntertwiningMap.comp_apply, - Representation.Equiv.coe_toIntertwiningMap] - generalize S.reindex σ t = u - induction u using inductionOn_pure with - | hsmul r u hu => simp only [map_smul, Finsupp.smul_apply, hu, smul_eq_mul, Finset.mul_sum] - | hadd u u' hu hu' => simp only [map_add, Finsupp.add_apply, hu, hu', Finset.sum_add_distrib] +/-- The components of the contraction in the standard arrangement of slots, +`(tr T)^ρ = ∑ μ, T^ρμ_μ`. The labels `μ` run over the pairs. -/ +lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : κ → Color K) + (a : J → Color K) (t : S.Tensor (contrColors c' a)) (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (contrStd c' a t) φ = ∑ μ : Π j, basisIdx (a j).kind, + (S.tensorBasis (contrColors c' a)).repr t (Sum.rec φ (Sum.rec μ μ)) := by + induction t using inductionOn_pure with + | hsmul r t ht => simp only [map_smul, Finsupp.smul_apply, ht, smul_eq_mul, Finset.mul_sum] + | hadd t t' ht ht' => simp only [map_add, Finsupp.add_apply, ht, ht', Finset.sum_add_distrib] | h f => -- Each contraction of a pair is a sum over one label, and the product of sums over the -- pairs becomes a sum over the families of labels `μ`. @@ -612,6 +582,15 @@ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] simp only [tensorBasis_repr_pure, Fintype.prod_sum_type, Finset.prod_mul_distrib] ring +/-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. The labels `μ` run +over the pairs, and the components of `T` are those of `S.reindex σ T`. -/ +lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) + (φ : Π x, basisIdx (c' x).kind) : + (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, + (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := + tensorBasis_repr_contrStd c' a _ φ + /-- The components of the contraction of one pair in dual bases, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a single label `μ`. -/ lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} From 383fb3819188a2a03c8a0ea607828a63fd9debb2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:55 +0000 Subject: [PATCH 049/242] feat(Tensor): tensorBasis_repr_mapAt Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index d7feeb3c1b..84a6dd644b 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -453,6 +453,34 @@ lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : rw [mapAt, ← eq_slotMapsAt (F := fun _ => LinearMap.id) i _ h rfl fun _ _ => LinearMap.ext fun _ => rfl, mapSlots_id] +/-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. The +labels `ψ` of `t` away from `i` are the labels `φ` of the result moved along `h`. -/ +lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) + (ψ : Π x, basisIdx (c x).kind) + (hψ : ∀ x (hx : x ≠ i), ψ x = cast (congrArg (basisIdx ∘ Color.kind) (h x hx).symm) (φ x)) : + (S.tensorBasis c').repr (S.mapAt i f h t) φ = ∑ μ : basisIdx (c i).kind, + LinearMap.toMatrix (S.basis (c i)) (S.basis (c' i)) f (φ i) μ * + (S.tensorBasis c).repr t (Function.update ψ i μ) := by + induction t using inductionOn_pure with + | hsmul r t ht => + simp only [map_smul, Finsupp.smul_apply, ht, smul_eq_mul, Finset.mul_sum, mul_left_comm r] + | hadd t t' ht ht' => + simp only [map_add, Finsupp.add_apply, ht, ht', mul_add, Finset.sum_add_distrib] + | h p => + -- Split off the slot `i`, where `f` acts, from the slots where the label is only moved. + rw [mapAt, mapSlots_pure, tensorBasis_repr_pure, + ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), slotMapsAt_apply_self, + ← LinearMap.toMatrix_mulVec_repr (S.basis (c i)) (S.basis (c' i)), Matrix.mulVec, dotProduct, + Finset.sum_mul] + refine Finset.sum_congr rfl fun μ _ => ?_ + rw [tensorBasis_repr_pure, ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), + Function.update_self, mul_assoc] + refine congrArg _ (congrArg _ (Finset.prod_congr rfl fun x hx => ?_)) + have hx := Finset.ne_of_mem_erase hx + rw [slotMapsAt_apply_ne _ _ _ hx, Function.update_of_ne hx, hψ x hx] + exact basis_repr_cast _ _ _ + variable (S) in /-- The equivalence of representations `f` at the slot `i`, and the identity, moved along `h x`, at every other slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. Its linear map is `mapAt` From efa3af14eb55fbc356c9c1c218e8166cadc61e46 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:55 +0000 Subject: [PATCH 050/242] feat(ForMathlib): LinearMap.IsAlt.schouten Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/SesquilinearForm.lean | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index 5c18d593be..f65188fa5e 100644 --- a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean +++ b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.SesquilinearForm.Star +public import Mathlib.Tactic.Module /-! @@ -30,6 +31,7 @@ in a basis it is positive-definite exactly when its matrix is. - `LinearMap.IsPosDef.nondegenerate` : a positive-definite form is nondegenerate. - `LinearMap.isPosDef_iff_posDef_toMatrix` : a form is positive-definite exactly when its matrix in a basis is. +- `LinearMap.IsAlt.schouten` : the Schouten identity for an alternating form in two dimensions. ## iii. Table of contents @@ -37,6 +39,7 @@ in a basis it is positive-definite exactly when its matrix is. - A.1. The definition - A.2. Consequences over a preorder - A.3. The matrix of a positive-definite form +- B. Alternating forms in two dimensions ## iv. References @@ -118,3 +121,29 @@ lemma isPosDef_iff_posDef_toMatrix (b : Basis n R M) : B.IsPosDef ↔ (toMatrix end toMatrix end LinearMap + +/-! + +## B. Alternating forms in two dimensions + +-/ + +namespace LinearMap.IsAlt + +variable {R M ι : Type*} [CommRing R] [AddCommGroup M] [Module R M] [Fintype ι] + [DecidableEq ι] {B : LinearMap.BilinForm R M} + +/-- The Schouten identity, `B(u, v) w + B(v, w) u + B(w, u) v = 0`, for an alternating bilinear +form on a module with a basis of two vectors. -/ +lemma schouten (hB : LinearMap.IsAlt B) (b : Basis ι R M) (h2 : Fintype.card ι = 2) (u v w : M) : + B u v • w + B v w • u + B w u • v = 0 := by + obtain ⟨i, j, hij, huniv⟩ := Finset.card_eq_two.mp (h2 : Finset.univ.card = 2) + -- Each vector is `x^i e_i + x^j e_j`, and `B` vanishes on `e_i, e_i` and on `e_j, e_j`. + have hexp (x : M) : x = b.repr x i • b i + b.repr x j • b j := by + conv_lhs => rw [← b.sum_repr x, huniv, Finset.sum_pair hij] + rw [hexp u, hexp v, hexp w] + simp only [map_add, map_smul, LinearMap.add_apply, LinearMap.smul_apply, smul_eq_mul, + LinearMap.IsAlt.self_eq_zero hB, (LinearMap.IsAlt.neg hB (b i) (b j)).symm] + module + +end LinearMap.IsAlt From 39095abc43f42cc8ead4e918595438119c99a49d Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:55 +0000 Subject: [PATCH 051/242] refactor(Metric): tensorBasis_repr_toDualAt from tensorBasis_repr_mapAt, schouten from LinearMap.IsAlt.schouten; formBilin_basis, formMatrix_symm, formMatrix_antisymm Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 77 +++++++++++-------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index bd8392f2f5..efdd7c4900 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -5,6 +5,8 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.ForMathlib.SesquilinearForm +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Contraction public import Mathlib.LinearAlgebra.Matrix.BilinearForm public import Mathlib.Tactic.LinearCombination @@ -37,7 +39,8 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th - `Species.HasForm` is an invariant form on a kind with its inverse, `g_μν` and `g^μν`, with `Species.form`, `Species.form_invariant` and `Species.form_inverse`. - `Species.IsSymmetricForm` and `Species.IsAntisymmetricForm` say that the form is symmetric or - antisymmetric, `Species.form_symm` and `Species.form_antisymm`. + antisymmetric, `Species.form_symm` and `Species.form_antisymm`, in components + `Species.formMatrix_symm` and `Species.formMatrix_antisymm`. - `Species.formBilin` reads the form as a Mathlib bilinear form, symmetric by `Species.isSymm_formBilin` and alternating by `Species.isAlt_formBilin`. - `Species.toDual` lowers an upper index and raises a lower one, and `Species.toDual_toDual` that @@ -49,8 +52,9 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th - `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, `T^…_ν… = T^…μ… g_μν`. - `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an - antisymmetric form on a kind with two labels, and `Species.formMatrix_schouten` is the same in - components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. + antisymmetric form on a kind with two labels, `LinearMap.IsAlt.schouten` for `formBilin`, and + `Species.formMatrix_schouten` is the same in components, + `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. ## iii. Table of contents @@ -151,6 +155,11 @@ lemma formMatrix_eq_toMatrix (c : Color K) [S.HasForm c.kind] : refine ((S.basis_repr_toLin c (d := c) (form c) _ ν).trans ?_).symm simp [Finsupp.single_apply, formMatrix] +/-- The form on two basis vectors is a component of the form, `g(e^μ, e^ν) = g^μν`. -/ +lemma formBilin_basis (c : Color K) [S.HasForm c.kind] (μ ν : basisIdx c.kind) : + S.formBilin c (S.basis c.dual μ) (S.basis c.dual ν) = S.formMatrix c μ ν := by + rw [formMatrix_eq_toMatrix, LinearMap.BilinForm.toMatrix_apply] + variable (S) in /-- The form on the kind `κ` is symmetric, `g_μν = g_νμ`, as `η`. A metric is a symmetric form. -/ class IsSymmetricForm (κ : K) [S.HasForm κ] : Prop where @@ -178,6 +187,11 @@ lemma form_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : TensorProduct.comm k (S.V c) (S.V c) (form c) = form c := IsSymmetricForm.comm_form c.variance +/-- The components of a symmetric form are symmetric, `g^νμ = g^μν`. -/ +lemma formMatrix_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] + (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = S.formMatrix c μ ν := by + rw [formMatrix, ← Module.Basis.tensorProduct_repr_comm, form_symm, formMatrix] + /-- A symmetric form is symmetric as a bilinear form, Mathlib's `LinearMap.BilinForm.IsSymm`. -/ lemma isSymm_formBilin (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : (S.formBilin c).IsSymm := @@ -188,6 +202,12 @@ lemma form_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.ki TensorProduct.comm k (S.V c) (S.V c) (form c) = -form c := IsAntisymmetricForm.comm_form c.variance +/-- The components of an antisymmetric form are antisymmetric, `g^νμ = -g^μν`. -/ +lemma formMatrix_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = -S.formMatrix c μ ν := by + rw [formMatrix, ← Module.Basis.tensorProduct_repr_comm, form_antisymm, map_neg, + Finsupp.neg_apply, formMatrix] + /-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's `LinearMap.IsAlt`. It needs `2 x = 0 → x = 0` in `k`: when `2 = 0` the form `[1]` on `k` is antisymmetric with `g(1, 1) = 1`. -/ @@ -328,25 +348,13 @@ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a · subst hx; rw [hi, ha] · rw [h x hx]) (φ x)) i (cast (congrArg (basisIdx ∘ Color.kind) ha.symm) μ)) := by subst ha - induction t using inductionOn_pure with - | hsmul r t ht => - simp only [map_smul, Finsupp.smul_apply, ht, smul_eq_mul, Finset.mul_sum] - exact Finset.sum_congr rfl fun _ _ => by ring - | hadd t t' ht ht' => - simp only [map_add, Finsupp.add_apply, ht, ht', mul_add, Finset.sum_add_distrib] - | h p => - rw [toDualAt_pure i _ rfl hi h p (fun x => if hx : x = i then hx ▸ - S.cast hi.symm (S.toDual (c i) (p i)) else S.cast (h x hx) (p x)) - (dite_eq_left rfl) (fun x hx => dite_eq_right hx), tensorBasis_repr_pure, - ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), dite_eq_left rfl, basis_repr_cast, - basis_repr_toDual, Finset.sum_mul] - refine Finset.sum_congr rfl fun μ _ => ?_ - rw [tensorBasis_repr_pure, ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), - Function.update_self, mul_left_comm, mul_assoc] - congr 2 - refine Finset.prod_congr rfl fun x hx => ?_ - have hx := Finset.ne_of_mem_erase hx - rw [dite_eq_right hx, basis_repr_cast, Function.update_of_ne hx] + rw [toDualAt_eq_mapAt] + refine (tensorBasis_repr_mapAt i _ h t φ _ fun _ _ => rfl).trans + (Finset.sum_congr rfl fun μ _ => congrArg (· * _) ?_) + rw [LinearMap.toMatrix_apply] + change (S.basis (c' i)).repr (S.cast hi.symm (S.toDual (c i) (S.basis (c i) μ))) (φ i) = _ + rw [basis_repr_cast, basis_repr_toDual] + simp [Finsupp.single_apply] /-- Lowering or raising at the slot `i` is contracting that slot with the first index of the form, `T^…_ν… = T^…μ… g_μν`. -/ @@ -368,19 +376,8 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i vanishes. -/ lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) (u v w : S.V c.dual) : - S.formBilin c u v • w + S.formBilin c v w • u + S.formBilin c w u • v = 0 := by - obtain ⟨i, j, hij, huniv⟩ := Finset.card_eq_two.mp (h2 : Finset.univ.card = 2) - -- Each vector is `x^i e_i + x^j e_j`, and the form vanishes on `e_i, e_i` and `e_j, e_j`. - have hexp (x : S.V c.dual) : x = (S.basis c.dual).repr x i • S.basis c.dual i + - (S.basis c.dual).repr x j • S.basis c.dual j := by - conv_lhs => rw [← (S.basis c.dual).sum_repr x] - rw [huniv, Finset.sum_pair hij] - have H := S.isAlt_formBilin c - have hji := (LinearMap.IsAlt.neg H (S.basis c.dual i) (S.basis c.dual j)).symm - rw [hexp u, hexp v, hexp w] - simp only [map_add, map_smul, LinearMap.add_apply, LinearMap.smul_apply, smul_eq_mul, - LinearMap.IsAlt.self_eq_zero H, hji] - module + S.formBilin c u v • w + S.formBilin c v w • u + S.formBilin c w u • v = 0 := + (isAlt_formBilin (S := S) c).schouten (S.basis c.dual) h2 u v w /-- The Schouten identity in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`: `schouten` at the basis vectors `e^γ`, `e^ρ` and `e^α'`, read at the label `α`. -/ @@ -392,14 +389,10 @@ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor S.formMatrix c ρ α' * (if γ = α then 1 else 0) := by have h := congrArg (fun x => (S.basis c.dual).repr x α) (schouten c h2 (S.basis c.dual γ) (S.basis c.dual ρ) (S.basis c.dual α')) - have hg (μ ν : basisIdx c.kind) : - S.formBilin c (S.basis c.dual μ) (S.basis c.dual ν) = S.formMatrix c μ ν := by - rw [formMatrix_eq_toMatrix, LinearMap.BilinForm.toMatrix_apply] - have hm : S.formMatrix c α' γ = -S.formMatrix c γ α' := by - rw [← hg, ← hg, ← LinearMap.IsAlt.neg (isAlt_formBilin (S := S) c)] simp only [map_add, map_smul, Finsupp.add_apply, Finsupp.smul_apply, Module.Basis.repr_self, - Finsupp.single_apply, smul_eq_mul, map_zero, Finsupp.zero_apply, hg, @eq_comm _ α' α] at h - linear_combination h - (if ρ = α then 1 else 0) * hm + Finsupp.single_apply, smul_eq_mul, map_zero, Finsupp.zero_apply, formBilin_basis, + @eq_comm _ α' α] at h + linear_combination h - (if ρ = α then 1 else 0) * formMatrix_antisymm (S := S) c γ α' end Species From 9d20533b8e4140785a3426d9ec08f83ad70c8884 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:56 +0000 Subject: [PATCH 052/242] refactor(Inverse): inverse_eq_left_inv and inverse_eq_right_inv by LinearMap.cancel_right/left; toLin_inverse_comp, comp_toLin_inverse, inverse_map_rep golfed Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Inverse.lean | 28 +++++++++--------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 84f108f84b..51255d1507 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -95,17 +95,15 @@ lemma toLin_inverse_bijective (g : S.V a ⊗[k] S.V b) (h : Function.Bijective ( /-- Inverting after `g` is the identity, as linear maps. -/ lemma toLin_inverse_comp (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.toLin (S.inverse g) ∘ₗ S.toLin g = LinearMap.id := - LinearMap.ext fun x => by - rw [toLin_inverse g h] - exact (LinearEquiv.ofBijective _ h).symm_apply_apply x + S.toLin (S.inverse g) ∘ₗ S.toLin g = LinearMap.id := by + rw [toLin_inverse g h] + exact (LinearEquiv.ofBijective _ h).symm_comp /-- `g` after inverting is the identity, as linear maps. -/ lemma comp_toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.toLin g ∘ₗ S.toLin (S.inverse g) = LinearMap.id := - LinearMap.ext fun x => by - rw [toLin_inverse g h] - exact (LinearEquiv.ofBijective _ h).apply_symm_apply x + S.toLin g ∘ₗ S.toLin (S.inverse g) = LinearMap.id := by + rw [toLin_inverse g h] + exact (LinearEquiv.ofBijective _ h).comp_symm /-- `g_μν g^νρ = δ_μ^ρ`, the contraction of `g` with its inverse on the adjacent indices. -/ lemma contr_mul_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : @@ -131,17 +129,13 @@ lemma inverse_eq_iff (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : S.inverse g = N := - (inverse_eq_iff g h N).2 <| by - rw [← LinearMap.id_comp (LinearEquiv.ofBijective _ h).symm.toLinearMap] - exact (LinearEquiv.eq_comp_toLinearMap_symm _ _).2 hN + S.toLin.injective <| (LinearMap.cancel_right h.2).1 <| by rw [toLin_inverse_comp g h, hN] /-- A tensor `N` with `g ∘ N = id`, as linear maps, is the inverse of `g`. -/ lemma inverse_eq_right_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin g ∘ₗ S.toLin N = LinearMap.id) : S.inverse g = N := - (inverse_eq_iff g h N).2 <| by - rw [← LinearMap.comp_id (LinearEquiv.ofBijective _ h).symm.toLinearMap] - exact (LinearEquiv.eq_toLinearMap_symm_comp _ _).2 hN + S.toLin.injective <| (LinearMap.cancel_left h.1).1 <| by rw [comp_toLin_inverse g h, hN] /-- A tensor `N` with `g_μν N^νρ = δ_μ^ρ` is the inverse of `g`. -/ lemma inverse_eq_of_contr_eq_deltaTensor (g : S.V a ⊗[k] S.V b) @@ -167,10 +161,8 @@ lemma inverse_map_rep (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin S.inverse (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) = TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := by refine inverse_eq_left_inv _ (S.toLin_map_rep_bijective g h Λ) (LinearMap.ext fun x => ?_) - simp only [toLin_map_rep, LinearMap.comp_apply] - rw [show S.rep b.dual.dual Λ⁻¹ = S.rep b Λ⁻¹ from rfl, Representation.inv_self_apply, - ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h, LinearMap.id_apply, - Representation.self_inv_apply, LinearMap.id_apply] + simp [toLin_map_rep, Representation.inv_self_apply, Representation.self_inv_apply, + ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h] /-- The inverse of an invariant tensor is invariant, `Λg = g` implies `Λ(g⁻¹) = g⁻¹`. -/ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) From 8ae243d550bd197803c8bbe3911a04063f865a93 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:56 +0000 Subject: [PATCH 053/242] feat(Conjugation): finrank_conj; isEquivariantConjugation_ofBasis drops the impossible variance cases in one step Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 971d1a155a..204c0d1087 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -175,6 +175,12 @@ lemma conjEquiv_cast {b b' : Color K} (p : b = b') (h : c' = b'.bar) (x : S.V b) subst p rfl +/-- Conjugation keeps the dimension, as `v ↦ v̄` is a conjugate-linear equivalence. -/ +lemma finrank_conj (h : c' = c.bar) : Module.finrank k (S.V c') = Module.finrank k (S.V c) := by + rw [Module.finrank, ← rank_eq_of_equiv_equiv (starRingEnd k) (S.conjEquiv h).toAddEquiv + (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ (S.conjEquiv h) r m), + ← Module.finrank] + end omit [InvolutiveStar K] in @@ -218,14 +224,12 @@ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) let := ofBasis S e refine ⟨fun {c _} h g v => ?_⟩ subst h - obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c - · exact absurd rfl hv + obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c <;> try exact absurd rfl hv · -- A lower color is the dual of `⟨κ, up⟩`, where the law holds. exact rep_dual_of_star_contr (c := ⟨κ, .up⟩) (c' := ⟨star κ, .up⟩) _ _ (fun u α => Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α) (fun g => Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g)) g v · exact Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g) v - · exact absurd rfl hv /-- The componentwise conjugate of the basis vector `e_μ` is the basis vector `e_{e μ}`. -/ lemma HasConjugation.ofBasis_conjEquiv_basis (e : ConjLabels basisIdx) From 072af6fd1e4bc34ba5ef9ad93a6c44d5bf60d351 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:25:56 +0000 Subject: [PATCH 054/242] refactor(HermitianForm): toLin_bijective_of_isPosDef by finrank_conj Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/HermitianForm.lean | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 28039fea9b..1218e2fcc1 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -139,13 +139,9 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. have hi := toLin_injective_of_isPosDef g hg - refine ⟨hi, ?_⟩ have : ∀ c, Module.Finite k (S.V c) := fun c => Module.Finite.of_basis (S.basis c) - refine (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi - let f : S.V a.dual ≃ₗ⋆[k] S.V b.dual := S.conjEquiv (congrArg Color.dual hb) - rw [Module.finrank, rank_eq_of_equiv_equiv (starRingEnd k) f.toAddEquiv - (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ f r m), - ← Module.finrank, Module.finrank_eq_card_basis (S.basis b.dual), + refine ⟨hi, (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi⟩ + rw [← finrank_conj (congrArg Color.dual hb), Module.finrank_eq_card_basis (S.basis b.dual), Module.finrank_eq_card_basis (S.basis b)] /-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ From c3da2d5af6bec1422d0c632a5b8a5c6123977fe1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:34:05 +0000 Subject: [PATCH 055/242] feat(ForMathlib/Representation): IntertwiningMap.map_one_invariant Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index ed9269b66a..6216b27a90 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -28,7 +28,8 @@ given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the l ## ii. Key results - `Representation.IntertwiningMap.map_invariant` : an intertwining map sends invariant vectors to - invariant vectors. + invariant vectors, and `Representation.IntertwiningMap.map_one_invariant` : an intertwining map + from the trivial representation picks out an invariant vector. - `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. ## iii. Table of contents @@ -61,6 +62,13 @@ lemma Representation.IntertwiningMap.map_invariant (φ : ρ.IntertwiningMap σ) (hv : ρ g v = v) : σ g (φ v) = φ v := by rw [← Representation.IntertwiningMap.isIntertwining, hv] +/-- An intertwining map `φ` from the trivial representation picks out an invariant vector, its +value `φ 1`, as an invariant form picks out its tensor. -/ +lemma Representation.IntertwiningMap.map_one_invariant + (φ : (Representation.trivial k G k).IntertwiningMap ρ) {v : V} (hv : φ 1 = v) (g : G) : + ρ g v = v := + hv ▸ φ.map_invariant rfl + /-! ## B. Tensor products of equivalences From dad6a7a14ae949d60be6c64007a2fe2788aca45b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:34:05 +0000 Subject: [PATCH 056/242] feat(Metric): Species.comm_form_eq_smul Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index efdd7c4900..cf1b4a9987 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -40,7 +40,8 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th `Species.form`, `Species.form_invariant` and `Species.form_inverse`. - `Species.IsSymmetricForm` and `Species.IsAntisymmetricForm` say that the form is symmetric or antisymmetric, `Species.form_symm` and `Species.form_antisymm`, in components - `Species.formMatrix_symm` and `Species.formMatrix_antisymm`. + `Species.formMatrix_symm` and `Species.formMatrix_antisymm`, and `Species.comm_form_eq_smul` + gives either from the components of the form. - `Species.formBilin` reads the form as a Mathlib bilinear form, symmetric by `Species.isSymm_formBilin` and alternating by `Species.isAlt_formBilin`. - `Species.toDual` lowers an upper index and raises a lower one, and `Species.toDual_toDual` that @@ -208,6 +209,15 @@ lemma formMatrix_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor rw [formMatrix, ← Module.Basis.tensorProduct_repr_comm, form_antisymm, map_neg, Finsupp.neg_apply, formMatrix] +/-- Swapping the two indices of the form scales it by `r` when its components do, +`g^νμ = r g^μν`. With `r = 1` it gives `IsSymmetricForm`, with `r = -1` `IsAntisymmetricForm`. -/ +lemma comm_form_eq_smul (c : Color K) [S.HasForm c.kind] (r : k) + (h : ∀ μ ν, S.formMatrix c ν μ = r * S.formMatrix c μ ν) : + TensorProduct.comm k (S.V c) (S.V c) (form c) = r • form c := + ((S.basis c).tensorProduct (S.basis c)).repr.injective <| Finsupp.ext fun ⟨μ, ν⟩ => by + rw [Module.Basis.tensorProduct_repr_comm, map_smul, Finsupp.smul_apply, smul_eq_mul] + exact h μ ν + /-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's `LinearMap.IsAlt`. It needs `2 x = 0 → x = 0` in `k`: when `2 = 0` the form `[1]` on `k` is antisymmetric with `g(1, 1) = 1`. -/ From 4db531a58b236106367be3bab1f79d803aaa205d Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:34:05 +0000 Subject: [PATCH 057/242] refactor(ComplexTensor/Species): coordPairing_rep by invariant_of_up; form_invariant by map_one_invariant; form_inverse from basis_tensorProduct_repr_form and formEntries_dual_mul; symmetry instances by comm_form_eq_smul with formEntries_antisymm; formTensor_*_symm/antisymm and deltaTensor_swap by tensor_regroup; basis_tensorProduct_repr_comm_form removed Co-authored-by: Claude Opus 5.5 --- .../Tensors/ComplexTensor/Species.lean | 230 +++++++----------- 1 file changed, 89 insertions(+), 141 deletions(-) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 1e7e250c03..da2d86c334 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -176,28 +176,19 @@ noncomputable def indexBasis : | ⟨vec, .up⟩ => ComplexVector.basis | ⟨vec, .down⟩ => ComplexCoVector.basis -/-- The coordinate pairing of a color and its dual is invariant under `SL(2, ℂ)`. -/ +/-- The coordinate pairing of a color and its dual is invariant under `SL(2, ℂ)`. At a lower color +it is the pairing at the upper color with its factors swapped (`Species.invariant_of_up`). -/ lemma coordPairing_rep : ∀ (c : Color Kind) (g : SL(2, ℂ)) (x : IndexSpace c) (y : IndexSpace c.dual), (indexBasis c).coordPairing (indexBasis c.dual) (indexRep c g x ⊗ₜ indexRep c.dual g y) = - (indexBasis c).coordPairing (indexBasis c.dual) (x ⊗ₜ y) - | ⟨left, .up⟩ => Species.invariant_of_contr - LeftHandedWeyl.contr fun μ ν => + (indexBasis c).coordPairing (indexBasis c.dual) (x ⊗ₜ y) := + Species.invariant_of_up indexRep indexBasis fun + | left => Species.invariant_of_contr LeftHandedWeyl.contr fun μ ν => (LeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) - | ⟨left, .down⟩ => Species.invariant_of_contr - DualLeftHandedWeyl.contr fun μ ν => - (DualLeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) - | ⟨right, .up⟩ => Species.invariant_of_contr - RightHandedWeyl.contr fun μ ν => + | right => Species.invariant_of_contr RightHandedWeyl.contr fun μ ν => (RightHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) - | ⟨right, .down⟩ => Species.invariant_of_contr - DualRightHandedWeyl.contr fun μ ν => - (DualRightHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) - | ⟨vec, .up⟩ => fun g x y => (coordPairing_complexVector_coVector _ _).trans <| - (dotProduct_SL2CRep g x y).trans (coordPairing_complexVector_coVector x y).symm - | ⟨vec, .down⟩ => fun g x y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| - (coordPairing_complexVector_coVector _ _).trans <| (dotProduct_SL2CRep g y x).trans <| - (coordPairing_complexVector_coVector y x).symm.trans (Module.Basis.coordPairing_comm _ _ _ _) + | vec => fun g x y => (coordPairing_complexVector_coVector _ _).trans <| + (dotProduct_SL2CRep g x y).trans (coordPairing_complexVector_coVector x y).symm end complexLorentz @@ -263,59 +254,60 @@ noncomputable def form : (c : Color Kind) → complexLorentz.V c ⊗[ℂ] comple | ⟨vec, .up⟩ => ComplexVector.metricVal | ⟨vec, .down⟩ => ComplexCoVector.metricVal -/-- The forms are invariant under `SL(2, ℂ)`. -/ +/-- The forms are invariant under `SL(2, ℂ)`, each the value at `1` of an intertwining map from +the trivial representation. -/ lemma form_invariant : ∀ (c : Color Kind) (g : SL(2, ℂ)), TensorProduct.map (complexLorentz.rep c g) (complexLorentz.rep c g) (form c) = form c - | ⟨left, .up⟩, g => by - have h := (LinearMap.congr_fun (LeftHandedWeyl.metric.isIntertwining' g) 1).symm - simp [LeftHandedWeyl.metric_apply_one] at h - exact h - | ⟨left, .down⟩, g => by - have h := (LinearMap.congr_fun (DualLeftHandedWeyl.metric.isIntertwining' g) 1).symm - simp [DualLeftHandedWeyl.metric_apply_one] at h - exact h - | ⟨right, .up⟩, g => by - have h := (LinearMap.congr_fun (RightHandedWeyl.metric.isIntertwining' g) 1).symm - simp [RightHandedWeyl.metric_apply_one] at h - exact h - | ⟨right, .down⟩, g => by - have h := (LinearMap.congr_fun (DualRightHandedWeyl.metric.isIntertwining' g) 1).symm - simp [DualRightHandedWeyl.metric_apply_one] at h - exact h - | ⟨vec, .up⟩, g => by - exact ComplexVector.metricVal_invariant g - | ⟨vec, .down⟩, g => by - exact ComplexCoVector.metricVal_invariant g - -/-- The forms at `c` and at `c.dual` are inverse to each other: their matrices are `ε` and -`-ε`, or `η` and `η`. -/ -lemma form_inverse : ∀ c : Color Kind, - complexLorentz.toLin (form c) ∘ₗ complexLorentz.toLin (form c.dual) = LinearMap.id - | ⟨left, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) - (leftLeftToMatrix.apply_symm_apply (-metricRaw))).trans - (by simpa [metricRaw] using Matrix.one_fin_two.symm) - | ⟨left, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) (leftLeftToMatrix.apply_symm_apply (-metricRaw)) - (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw)).trans - (by simpa [metricRaw] using Matrix.one_fin_two.symm) - | ⟨right, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) (dualRightDualRightToMatrix.apply_symm_apply metricRaw) - (rightRightToMatrix.apply_symm_apply (-metricRaw))).trans - (by simpa [metricRaw] using Matrix.one_fin_two.symm) - | ⟨right, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) (rightRightToMatrix.apply_symm_apply (-metricRaw)) - (dualRightDualRightToMatrix.apply_symm_apply metricRaw)).trans - (by simpa [metricRaw] using Matrix.one_fin_two.symm) - | ⟨vec, .up⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) (coCoToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) - (contrContrToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom))).trans - (by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)]) - | ⟨vec, .down⟩ => Species.toLin_comp_toLin_of_mul_eq_one _ _ <| - (congrArg₂ (· * ·) - (contrContrToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) - (coCoToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom))).trans - (by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)]) + | ⟨left, .up⟩ => LeftHandedWeyl.metric.map_one_invariant LeftHandedWeyl.metric_apply_one + | ⟨left, .down⟩ => + DualLeftHandedWeyl.metric.map_one_invariant DualLeftHandedWeyl.metric_apply_one + | ⟨right, .up⟩ => RightHandedWeyl.metric.map_one_invariant RightHandedWeyl.metric_apply_one + | ⟨right, .down⟩ => + DualRightHandedWeyl.metric.map_one_invariant DualRightHandedWeyl.metric_apply_one + | ⟨vec, .up⟩ => ComplexVector.metric.map_one_invariant ComplexVector.metric_apply_one + | ⟨vec, .down⟩ => ComplexCoVector.metric.map_one_invariant ComplexCoVector.metric_apply_one + +/-- The entries of the form at each color, `ε^αβ = !![0, -1; 1, 0]` at the upper Weyl colors, +`ε_αβ = !![0, 1; -1, 0]` at the lower ones, and `η` at the vector colors. -/ +noncomputable def formEntries : (c : Color Kind) → Idx c.kind → Idx c.kind → ℂ + | ⟨left, .up⟩ => -metricRaw + | ⟨left, .down⟩ => metricRaw + | ⟨right, .up⟩ => -metricRaw + | ⟨right, .down⟩ => metricRaw + | ⟨vec, _⟩ => fun μ ν => minkowskiMatrix μ ν + +/-- The coordinates of the form in Mathlib's product basis are `formEntries`: each form is defined +by its matrix. -/ +lemma basis_tensorProduct_repr_form : ∀ (c : Color Kind) (μ ν : Idx c.kind), + ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr (form c) (μ, ν) = + formEntries c μ ν + | ⟨left, .up⟩, μ, ν => congrFun (congrFun (leftLeftToMatrix.apply_symm_apply (-metricRaw)) μ) ν + | ⟨left, .down⟩, μ, ν => + congrFun (congrFun (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) μ) ν + | ⟨right, .up⟩, μ, ν => congrFun (congrFun (rightRightToMatrix.apply_symm_apply (-metricRaw)) μ) ν + | ⟨right, .down⟩, μ, ν => + congrFun (congrFun (dualRightDualRightToMatrix.apply_symm_apply metricRaw) μ) ν + | ⟨vec, .up⟩, μ, ν => congrFun (congrFun (contrContrToMatrix.apply_symm_apply + ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν + | ⟨vec, .down⟩, μ, ν => congrFun (congrFun (coCoToMatrix.apply_symm_apply + ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν + +/-- The matrices of the forms at `c.dual` and at `c` are inverse, `ε (-ε) = 1` and `η η = 1`. -/ +lemma formEntries_dual_mul : ∀ c : Color Kind, + Matrix.of (formEntries c.dual) * Matrix.of (formEntries c) = 1 + | ⟨left, .up⟩ | ⟨right, .up⟩ => show metricRaw * -metricRaw = 1 by + simpa [metricRaw] using Matrix.one_fin_two.symm + | ⟨left, .down⟩ | ⟨right, .down⟩ => show -metricRaw * metricRaw = 1 by + simpa [metricRaw] using Matrix.one_fin_two.symm + | ⟨vec, _⟩ => show (@minkowskiMatrix 3).map Complex.ofRealHom * + (@minkowskiMatrix 3).map Complex.ofRealHom = 1 by + rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)] + +/-- The forms at `c` and at `c.dual` are inverse to each other, since their matrices are. -/ +lemma form_inverse (c : Color Kind) : + complexLorentz.toLin (form c) ∘ₗ complexLorentz.toLin (form c.dual) = LinearMap.id := + Species.toLin_comp_toLin_of_mul_eq_one _ _ <| by + simpa only [basis_tensorProduct_repr_form] using formEntries_dual_mul c end complexLorentz @@ -428,28 +420,10 @@ end complexLorentz namespace complexLorentz -/-- The entries of the form at each color, `ε^αβ = !![0, -1; 1, 0]` at the upper Weyl colors, -`ε_αβ = !![0, 1; -1, 0]` at the lower ones, and `η` at the vector colors. -/ -noncomputable def formEntries : (c : Color Kind) → Idx c.kind → Idx c.kind → ℂ - | ⟨left, .up⟩ => -metricRaw - | ⟨left, .down⟩ => metricRaw - | ⟨right, .up⟩ => -metricRaw - | ⟨right, .down⟩ => metricRaw - | ⟨vec, _⟩ => fun μ ν => minkowskiMatrix μ ν - /-- The components of the form at any color are `formEntries`. -/ -lemma formMatrix_apply : ∀ (c : Color Kind) (μ ν : Idx c.kind), - Species.formMatrix complexLorentz c μ ν = formEntries c μ ν - | ⟨left, .up⟩, μ, ν => congrFun (congrFun (leftLeftToMatrix.apply_symm_apply (-metricRaw)) μ) ν - | ⟨left, .down⟩, μ, ν => - congrFun (congrFun (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) μ) ν - | ⟨right, .up⟩, μ, ν => congrFun (congrFun (rightRightToMatrix.apply_symm_apply (-metricRaw)) μ) ν - | ⟨right, .down⟩, μ, ν => - congrFun (congrFun (dualRightDualRightToMatrix.apply_symm_apply metricRaw) μ) ν - | ⟨vec, .up⟩, μ, ν => congrFun (congrFun (contrContrToMatrix.apply_symm_apply - ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν - | ⟨vec, .down⟩, μ, ν => congrFun (congrFun (coCoToMatrix.apply_symm_apply - ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν +lemma formMatrix_apply (c : Color Kind) (μ ν : Idx c.kind) : + Species.formMatrix complexLorentz c μ ν = formEntries c μ ν := + basis_tensorProduct_repr_form c μ ν /-- The components of the form tensor at any color are `formEntries`, at the pair of labels `(μ 0, μ 1)`. The labels `μ` form a dependent family, since the basis labels of a slot depend on @@ -516,36 +490,26 @@ end complexLorentz namespace complexLorentz -/-- Swapping the two indices of the form transposes its matrix. -/ -lemma basis_tensorProduct_repr_comm_form (c : Color Kind) (μ ν : Idx c.kind) : - ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr - (TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c)) (μ, ν) = - formEntries c ν μ := by - rw [Module.Basis.tensorProduct_repr_comm] - exact formMatrix_apply c ν μ - -/-- The Minkowski metric `η` on the vector kind is symmetric. -/ +/-- The Minkowski metric `η` on the vector kind is symmetric, `η^νμ = η^μν`. -/ instance : complexLorentz.IsSymmetricForm vec where - comm_form v := ((complexLorentz.basis ⟨vec, v⟩).tensorProduct - (complexLorentz.basis ⟨vec, v⟩)).repr.injective - (Finsupp.ext fun ⟨μ, ν⟩ => by - rw [basis_tensorProduct_repr_comm_form] - change _ = Species.formMatrix complexLorentz _ μ ν - rw [formMatrix_apply] - change (minkowskiMatrix ν μ : ℂ) = minkowskiMatrix μ ν - (conv_lhs => rw [← minkowskiMatrix.eq_transpose]); rfl) + comm_form v := (Species.comm_form_eq_smul _ 1 fun μ ν => by + rw [formMatrix_apply, formMatrix_apply, one_mul] + exact congrArg Complex.ofReal (congrFun (congrFun minkowskiMatrix.eq_transpose μ) ν)).trans + (one_smul _ _) + +/-- The entries of `ε` are antisymmetric, `ε^βα = -ε^αβ`. -/ +lemma formEntries_antisymm : ∀ c : Color Kind, c.kind ≠ vec → ∀ μ ν, + formEntries c ν μ = -1 * formEntries c μ ν + | ⟨left, .up⟩, _ | ⟨left, .down⟩, _ | ⟨right, .up⟩, _ | ⟨right, .down⟩, _ => fun μ ν => by + fin_cases μ <;> fin_cases ν <;> simp [formEntries, metricRaw] + | ⟨vec, _⟩, h => absurd rfl h /-- The form `ε` is antisymmetric at the Weyl colors. -/ lemma comm_form_weyl (c : Color Kind) (h : c.kind ≠ vec) : TensorProduct.comm ℂ _ _ (Species.form (S := complexLorentz) c) = -Species.form c := - ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr.injective - (Finsupp.ext fun ⟨μ, ν⟩ => by - rw [basis_tensorProduct_repr_comm_form, map_neg, Finsupp.neg_apply] - change _ = -Species.formMatrix complexLorentz _ μ ν - rw [formMatrix_apply] - rcases c with ⟨_ | _ | _, _ | _, _ | _, h'⟩ <;> - first | exact absurd rfl h | exact absurd rfl h' | - (fin_cases μ <;> fin_cases ν <;> simp [formEntries, metricRaw])) + (Species.comm_form_eq_smul c (-1) fun μ ν => by + rw [formMatrix_apply, formMatrix_apply] + exact formEntries_antisymm c h μ ν).trans (neg_one_smul _ _) /-- The form `ε` on the left-handed Weyl kind is antisymmetric. -/ instance : complexLorentz.IsAntisymmetricForm left where @@ -557,40 +521,33 @@ instance : complexLorentz.IsAntisymmetricForm right where /-- `ε^αβ` is antisymmetric, `ε^αβ = -ε^βα`. -/ lemma formTensor_upL_antisymm : {εL | α β = -εL | β α}ᵀ := by - rw [map_neg, Species.formTensor_antisymm upL ?_, neg_neg] - exact Equiv.ext (by decide) + tensor_regroup /-- `ε_αβ` is antisymmetric, `ε_αβ = -ε_βα`. -/ lemma formTensor_downL_antisymm : {εL' | α β = -εL' | β α}ᵀ := by - rw [map_neg, Species.formTensor_antisymm downL ?_, neg_neg] - exact Equiv.ext (by decide) + tensor_regroup /-- `ε^α̇β̇` is antisymmetric, `ε^α̇β̇ = -ε^β̇α̇`. -/ lemma formTensor_upR_antisymm : {εR | α β = -εR | β α}ᵀ := by - rw [map_neg, Species.formTensor_antisymm upR ?_, neg_neg] - exact Equiv.ext (by decide) + tensor_regroup /-- `ε_α̇β̇` is antisymmetric, `ε_α̇β̇ = -ε_β̇α̇`. -/ lemma formTensor_downR_antisymm : {εR' | α β = -εR' | β α}ᵀ := by - rw [map_neg, Species.formTensor_antisymm downR ?_, neg_neg] - exact Equiv.ext (by decide) + tensor_regroup /-- `η^μν` is symmetric, `η^μν = η^νμ`. -/ lemma formTensor_up_symm : {η | μ ν = η | ν μ}ᵀ := by - rw [Species.formTensor_symm up ?_] - exact Equiv.ext (by decide) + tensor_regroup /-- `η_μν` is symmetric, `η_μν = η_νμ`. -/ lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by - rw [Species.formTensor_symm down ?_] - exact Equiv.ext (by decide) + tensor_regroup /-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_γα' δ_ρ^α - ε_ρα' δ_γ^α`. -/ lemma schouten_downL : {εL' | γ ρ ⊗ δL | α α' = εL' | γ α' ⊗ δL' | ρ α + -{εL' | ρ α' ⊗ δL' | γ α}ᵀ | ρ α' γ α}ᵀ := by tensor_components [Species.tensorBasis_repr_formTensor, - fun c μ => (Species.tensorBasis_repr_deltaTensor_eq_one (S := complexLorentz) c μ).trans - Matrix.one_apply] + Species.tensorBasis_repr_deltaTensor_eq_one, Matrix.one_apply] rw [← sub_eq_add_neg] exact Species.formMatrix_schouten downL (Fintype.card_fin 2) _ _ _ _ @@ -610,10 +567,7 @@ lemma schouten_downR : /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : {complexLorentz.deltaTensor c | μ ν = complexLorentz.deltaTensor c.dual | ν μ}ᵀ := by - refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) - rw [Species.tensorBasis_repr_reindex, tensorBasis_repr_deltaTensor, tensorBasis_repr_deltaTensor] - -- The labels on the right are casts along `rfl` of `μ 1` and `μ 0`. - exact congrFun (congrFun (Matrix.transpose_one (n := Idx c.kind) (α := ℂ)) (μ 1)) (μ 0) + tensor_regroup end complexLorentz @@ -686,20 +640,14 @@ end complexLorentz namespace complexLorentz -/-- `ε^αβ ε_βγ = δ^α_γ`, by `contr_formTensor_formTensor`. -/ +/-- `ε^αβ ε_βγ = δ^α_γ`, by `tensor_regroup` through `contr_formTensor_formTensor`. -/ example : {εL | α β ⊗ εL' | β γ = δL | α γ}ᵀ := by - refine Eq.trans ?_ (Species.contr_formTensor_formTensor upL) - congr 2 - ext x - fin_cases x <;> rfl + tensor_regroup /-- `g^αβ g_βγ = δ^α_γ` at every color: `ε`, `ε'` and `η` with their duals. -/ example (c : Color Kind) : {complexLorentz.formTensor c | α β ⊗ complexLorentz.formTensor c.dual | β γ = complexLorentz.deltaTensor c.dual | α γ}ᵀ := by - refine Eq.trans ?_ (Species.contr_formTensor_formTensor c) - congr 2 - ext x - fin_cases x <;> rfl + tensor_regroup /-- The components of `ε^αβ ⊗ ε^α̇β̇` are the products of the components of `εL` and `εR`. -/ lemma tensorBasis_repr_formTensor_upL_tmul_upR From 3908486f19318a8668c851de8c0f7c56703fc70a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:34:05 +0000 Subject: [PATCH 058/242] refactor(ComplexTensor/Metrics/Pre): ComplexVector.metricVal_invariant and ComplexCoVector.metricVal_invariant removed Co-authored-by: Claude Opus 5.5 --- .../Tensors/ComplexTensor/Metrics/Pre.lean | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean b/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean index 1d3303bec0..b442ea59a6 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Metrics/Pre.lean @@ -27,13 +27,6 @@ namespace Lorentz def ComplexVector.metricVal : (ComplexVector ⊗[ℂ] ComplexVector) := contrContrToMatrix.symm ((@minkowskiMatrix 3).map ofRealHom) -/-- The metric `ηᵃᵃ` is invariant under `SL(2,ℂ)`. -/ -lemma ComplexVector.metricVal_invariant (M : SL(2,ℂ)) : - TensorProduct.map (ComplexVector.SL2CRep M) (ComplexVector.SL2CRep M) - ComplexVector.metricVal = ComplexVector.metricVal := by - rw [ComplexVector.metricVal, contrContrToMatrix_ρ_symm, - LorentzGroup.toComplex_mul_minkowskiMatrix_mul_transpose] - /-- The expansion of `ComplexVector.metricVal` into basis vectors. -/ lemma ComplexVector.metricVal_expand_tmul : ComplexVector.metricVal = ComplexVector.basis (Sum.inl 0) ⊗ₜ[ℂ] ComplexVector.basis (Sum.inl 0) @@ -84,14 +77,6 @@ lemma ComplexVector.metric_apply_one : ComplexVector.metric (1 : ℂ) = ComplexV def ComplexCoVector.metricVal : (ComplexCoVector ⊗[ℂ] ComplexCoVector) := coCoToMatrix.symm ((@minkowskiMatrix 3).map ofRealHom) -/-- The metric `ηᵢᵢ` is invariant under `SL(2,ℂ)`. -/ -lemma ComplexCoVector.metricVal_invariant (M : SL(2,ℂ)) : - TensorProduct.map (ComplexCoVector.SL2CRep M) (ComplexCoVector.SL2CRep M) - ComplexCoVector.metricVal = ComplexCoVector.metricVal := by - rw [ComplexCoVector.metricVal, coCoToMatrix_ρ_symm, LorentzGroup.toComplex_inv] - simp only [LorentzGroup.inv_eq_dual, SL2C.toLorentzGroup_apply_coe, - LorentzGroup.toComplex_transpose_mul_minkowskiMatrix_mul_self] - /-- The expansion of `ComplexCoVector.metricVal` into basis vectors. -/ lemma ComplexCoVector.metricVal_expand_tmul : ComplexCoVector.metricVal = ComplexCoVector.basis (Sum.inl 0) ⊗ₜ[ℂ] ComplexCoVector.basis (Sum.inl 0) From 5dc72162ceef7c419186af1ee63a114378711bf9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:34:05 +0000 Subject: [PATCH 059/242] refactor(RealTensor/Species): coordPairing_rep by invariant_of_up; metric_inverse by toLin_comp_toLin_of_mul_eq_one; toLin_metric_basis removed Co-authored-by: Claude Opus 5.5 --- .../Tensors/RealTensor/Species.lean | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 0acf108000..1105cbc8ff 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -143,17 +143,16 @@ lemma dotProduct_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) (ω : CoVect rw [dotProduct_mulVec, vecMul_transpose, mulVec_mulVec, LorentzGroup.coe_inv, LorentzGroup.subtype_inv_mul, one_mulVec] -/-- The coordinate pairing of an index with its dual is Lorentz invariant. -/ +/-- The coordinate pairing of an index with its dual is Lorentz invariant. At a lower index it is +the pairing at the upper index with its factors swapped (`Species.invariant_of_up`). -/ lemma coordPairing_rep (d : ℕ) : ∀ (c : Color Kind) (Λ : LorentzGroup d) (x : IndexSpace d c) (y : IndexSpace d c.dual), (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) (IndexSpace.rep d c Λ x ⊗ₜ IndexSpace.rep d c.dual Λ y) = - (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) (x ⊗ₜ y) - | ⟨_, .up⟩, Λ, x, y => (coordPairing_vector_coVector d _ _).trans <| - (dotProduct_rep d Λ x y).trans (coordPairing_vector_coVector d x y).symm - | ⟨_, .down⟩, Λ, x, y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| - (coordPairing_vector_coVector d _ _).trans <| (dotProduct_rep d Λ y x).trans <| - (coordPairing_vector_coVector d y x).symm.trans (Module.Basis.coordPairing_comm _ _ _ _) + (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) (x ⊗ₜ y) := + Species.invariant_of_up (basisIdx := fun _ => Fin 1 ⊕ Fin d) _ _ fun _ Λ x y => + (coordPairing_vector_coVector d _ _).trans <| + (dotProduct_rep d Λ x y).trans (coordPairing_vector_coVector d x y).symm end realLorentz @@ -232,26 +231,13 @@ lemma metric_invariant (d : ℕ) (c : Color Kind) (Λ : LorentzGroup d) : Matrix.transpose_transpose, ← LorentzGroup.coe_inv, LorentzGroup.transpose_mul_minkowskiMatrix_mul_self] -/-- Contracting the metric at `c` with a basis vector of `c.dual` gives `η_νν e_ν`. -/ -lemma toLin_metric_basis (d : ℕ) (c : Color Kind) (ν : Fin 1 ⊕ Fin d) : - (realLorentz d).toLin (metric d c) ((realLorentz d).basis c.dual ν) = - minkowskiMatrix ν ν • (realLorentz d).basis c ν := by - refine ((realLorentz d).basis c).repr.injective (Finsupp.ext fun ρ => ?_) - rw [Species.basis_repr_toLin, map_smul] - simp only [Module.Basis.repr_self, Finsupp.single_apply, ite_mul, one_mul, zero_mul, - Finset.sum_ite_eq, Finset.mem_univ, ite_true, Finsupp.smul_apply, smul_eq_mul, mul_ite, mul_one, - mul_zero] - refine (basis_tensorProduct_repr_metric d c ν ρ).trans ?_ - by_cases h : ν = ρ <;> simp [h] - -/-- The metrics at a color and at its dual are inverse, `η^μν η_νρ = δ^μ_ρ`: `η` is diagonal with -`η_μμ² = 1`. -/ +/-- The metrics at a color and at its dual are inverse, `η^μν η_νρ = δ^μ_ρ`, since `η² = 1`. -/ lemma metric_inverse (d : ℕ) (c : Color Kind) : (realLorentz d).toLin (metric d c) ∘ₗ (realLorentz d).toLin (metric d c.dual) = LinearMap.id := - ((realLorentz d).basis c).ext fun ν => by - rw [LinearMap.comp_apply, toLin_metric_basis d c.dual ν, map_smul, toLin_metric_basis d c ν, - smul_smul, minkowskiMatrix.η_apply_mul_η_apply_diag, one_smul, LinearMap.id_apply] + Species.toLin_comp_toLin_of_mul_eq_one _ _ <| by + simp only [basis_tensorProduct_repr_metric] + exact minkowskiMatrix.sq end realLorentz From 79785a2de97a3580bb086d4e50e8f00956b14022 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:17:13 +0000 Subject: [PATCH 060/242] refactor(Elab): Node.toDualAt, tensorOf, indexName and literalTensorType? split out of leaf and buildTo Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 112 ++++++++++++++------------ 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 23823af0eb..5d6aadb604 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -199,6 +199,17 @@ partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do def colorsTerm (cs : List Expr) : TermElabM Term := do `(![$((← cs.toArray.mapM exprToSyntax)),*]) +/-- The slots, the color family `c` and its entries of a tensor type `S.Tensor c` with literal slots +and colors. -/ +def literalTensorType? (ty : Expr) : MetaM (Option (Slots × Expr × List Expr)) := do + let ty ← whnfR (← instantiateMVars ty) + unless ty.isAppOf ``Tensors.Species.Tensor do return none + let args := ty.getAppArgs + let some slots ← Slots.ofType? args[args.size - 2]! | return none + let c ← instantiateMVars args[args.size - 1]! + let some cs ← colorList? c | return none + return (slots, c, cs) + /-! ### B.2. The steps of an expression @@ -232,24 +243,55 @@ def Node.toFin (a : Node) : TermElabM Node := do if a.slots == .fin n then return a return { a with slots := .fin n, tm := ← a.reindex (List.range n) a.colors } -/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ` and raised at each `↑μ`. -/ -def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do - let mut e ← instantiateMVars (← elabTerm T none) +/-- The expression on `Fin n` with its slot `i` moved to the dual color by `S.toDualAt`. -/ +def Node.toDualAt (a : Node) (i : Nat) : TermElabM Node := do + let colors := a.colors.set i (← mkAppM ``Tensors.Color.dual #[a.colors[i]!]) + -- The color `col` of the slot is passed explicitly, so the form is found at the kind of `col` and + -- every lowering at that kind uses the same instance. The proofs `c i = col` and + -- `c' i = col.dual` are `id rfl`, since a bare `rfl` has the type `c' i = c' i`, which `rw` + -- matches only after unfolding `![…] i`. Away from `i` the colors agree by cases. + let tm ← `(Tensors.Species.toDualAt $(← exprToSyntax a.species) (c' := $(← colorsTerm colors)) + ($(quote i) : Fin $(quote a.names.length)) $(← exprToSyntax a.colors[i]!) (id rfl) (id rfl) + (by intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) $a.tm) + return { a with colors, tm } + +/-- The term `T` and its type, a tensor type. A term that is not a tensor is read as +`Tensorial.toTensor T` through its `Tensorial` instance. -/ +def tensorOf (T : Term) : TermElabM (Expr × Expr) := do + let e ← instantiateMVars (← elabTerm T none) if e.hasSyntheticSorry then throwAbortTerm - let mut ty ← whnfR (← instantiateMVars (← inferType e)) + let ty ← whnfR (← instantiateMVars (← inferType e)) if ty.isMVar then throwError "index notation: the type of {T} is not known; give it, as `({T} : S.Tensor ![…])`" - unless ty.isAppOf ``Tensors.Species.Tensor do - -- Not a tensor: read it through its `Tensorial` instance, if it has one. - try - e ← withoutErrToSorry do - let e ← elabTerm (← `(Tensors.Tensorial.toTensor $(← exprToSyntax e))) none - synthesizeSyntheticMVarsNoPostponing - instantiateMVars e - ty ← whnfR (← instantiateMVars (← inferType e)) - catch ex => - throwError "index notation: {T} is not a tensor and has no `Tensorial` instance:\n\ - {ex.toMessageData}" + if ty.isAppOf ``Tensors.Species.Tensor then return (e, ty) + try + let e ← withoutErrToSorry do + let e ← elabTerm (← `(Tensors.Tensorial.toTensor $(← exprToSyntax e))) none + synthesizeSyntheticMVarsNoPostponing + instantiateMVars e + return (e, ← whnfR (← instantiateMVars (← inferType e))) + catch ex => + throwError "index notation: {T} is not a tensor and has no `Tensorial` instance:\n\ + {ex.toMessageData}" + +/-- The name of the index `i` on a slot of color `col`, and whether it is lowered or raised, after +checking that its arrow matches the variance of `col`. -/ +def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) := do + let arrow (x : Ident) (up : Bool) : TermElabM (Name × Bool) := do + let isUp ← whnfD (← mkAppM ``Tensors.Variance.isUp #[← mkAppM ``Tensors.Color.variance #[col]]) + unless isUp.isConstOf (if up then ``Bool.true else ``Bool.false) do + throwError "index notation: {if up then "↓" else "↑"}{x.getId} needs a visibly \ + {if up then "upper" else "lower"} index" + pure (x.getId, true) + match i with + | `(tensorIndex| $x:ident) => pure (x.getId, false) + | `(tensorIndex| ↓$x:ident) => arrow x true + | `(tensorIndex| ↑$x:ident) => arrow x false + | _ => throwError "index notation: unsupported index {i}" + +/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ` and raised at each `↑μ`. -/ +def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do + let (e, ty) ← tensorOf T let args := ty.getAppArgs let some slots ← Slots.ofType? args[args.size - 2]! | throwError "index notation: the slots of {T} must be `Fin n` and `⊕`" @@ -257,37 +299,10 @@ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do | throwError "index notation: the colors of {T} must be a literal list" unless colors.length = idxs.size do throwError "index notation: {T} has {colors.length} slots, {idxs.size} indices given" - -- Each index with whether its slot moves to the dual color, after checking the arrow. - let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => do - let isUp ← whnfD (← mkAppM ``Tensors.Variance.isUp #[← mkAppM ``Tensors.Color.variance #[col]]) - let check (x : Ident) (up : Bool) (arrow : String) : TermElabM (Name × Bool) := do - unless isUp.isConstOf (if up then ``Bool.true else ``Bool.false) do - throwError "index notation: {arrow}{x.getId} needs a visibly \ - {if up then "upper" else "lower"} index" - pure (x.getId, true) - match i with - | `(tensorIndex| $x:ident) => pure (x.getId, false) - | `(tensorIndex| ↓$x:ident) => check x true "↓" - | `(tensorIndex| ↑$x:ident) => check x false "↑" - | _ => throwError "index notation: unsupported index {i}" - let mut a : Node := ⟨kinds.map (·.1), colors, slots, ← exprToSyntax e, args[args.size - 3]!⟩ + let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => indexName i col + let a : Node := ⟨kinds.map (·.1), colors, slots, ← exprToSyntax e, args[args.size - 3]!⟩ if kinds.all (!·.2) then return a - a ← a.toFin - let n := colors.length - for ((_, d), i) in kinds.zipIdx do - if d then - let colors := a.colors.set i (← mkAppM ``Tensors.Color.dual #[a.colors[i]!]) - -- The color `col` of the slot is passed explicitly, so the form is found at the kind of - -- `col` and every lowering at that kind uses the same instance. The proofs `c i = col` and - -- `c' i = col.dual` are `id rfl`, since a bare `rfl` has the type `c' i = c' i`, which - -- `rw` matches only after unfolding `![…] i`. Away from `i` the colors agree by cases. - let sp ← exprToSyntax a.species - let col ← exprToSyntax a.colors[i]! - let tm ← `(Tensors.Species.toDualAt $sp (c' := $(← colorsTerm colors)) - ($(quote i) : Fin $(quote n)) $col (id rfl) (id rfl) - (by intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) $a.tm) - a := { a with colors, tm } - return a + kinds.zipIdx.foldlM (init := ← a.toFin) fun a ((_, d), i) => if d then a.toDualAt i else pure a /-- Contracts every repeated name in one `Tensor.contr`, and numbers the free slots in the order written. -/ @@ -368,12 +383,7 @@ end /-- The tensor of `e` on the slots and colors of the tensor type `target`, its free indices in the order written. It is `none` when `target` is not a tensor type with literal slots and colors. -/ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do - let target ← whnfR (← instantiateMVars target) - unless target.isAppOf ``Tensors.Species.Tensor do return none - let args := target.getAppArgs - let some slots ← Slots.ofType? args[args.size - 2]! | return none - let c ← instantiateMVars args[args.size - 1]! - let some cs ← colorList? c | return none + let some (slots, c, cs) ← literalTensorType? target | return none let u ← uncontracted e -- A product with no contraction is already on the slots of its factors. let a ← if u.slots == slots && u.names.eraseDups.length == u.names.length then pure u From 1a7afe0d02a0272ae9086878306a28b3eda6f1e9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:17:33 +0000 Subject: [PATCH 061/242] refactor(Elab): tensor_regroup and tensor_slots on shared goalEq, bundledArg?, wrapSides, firstSuccess and rewriteSubterm; matchFactors tries at most 12 matchings rewriteSubterm replaces the conv/nth_rw rewrites of swaps, deltas, form pairs and form flips. It finds the subterm syntactically (form flips by tensorLeaves) and skips the type check of the whole goal, so a false goal with equal factors fails within the heartbeats instead of timing out, and its error names the factor, the summand or the mismatched pairs of slots. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 487 ++++++++++++++------------ 1 file changed, 265 insertions(+), 222 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 5d6aadb604..6425887f94 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -76,8 +76,11 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.2. The steps of an expression - B.3. The elaborator - B.4. Components - - B.5. Regrouping contractions - - B.6. Chains of lowerings and raisings + - B.5. Reading and rewriting the goal + - B.6. Matching the factors of two contractions + - B.7. Deltas and forms + - B.8. Sums, signs and scalars + - B.9. Chains of lowerings and raisings - C. Examples ## iv. References @@ -466,19 +469,117 @@ macro_rules /-! -### B.5. Regrouping contractions +### B.5. Reading and rewriting the goal -/ open Tactic -/-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ -private def productFactors? (e : Expr) : Option (Expr × Expr) := +/-- The type and the two sides of the goal of the tactic `tac`, an equation. -/ +private def goalEq (tac : String := "tensor_regroup") : TacticM (Expr × Expr × Expr) := do + let some (ty, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? + | throwError "{tac}: the goal is not an equation" + return (ty, l.consumeMData, r.consumeMData) + +/-- The argument `x` of `F x`, for a bundled map `F` whose head is the constant `f`, such as +`Tensor.contr σ` for `f = Tensor.contr`. -/ +private def bundledArg? (f : Name) (e : Expr) : Option Expr := let e := e.consumeMData - if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.tmulEquiv then - let x := e.appArg!.consumeMData - if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none - else none + if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf f then some e.appArg! else none + +/-- Writes each side `x` of the goal on which `p` fails as `F x`, by the term `h : F x = x`. -/ +private def wrapSides (tac : String) (p : Expr → Bool) (h : Term) : TacticM Unit := do + let (_, l, r) ← goalEq tac + unless p l do evalTactic (← `(tactic| refine (Eq.symm $h).trans ?_)) + unless p r do evalTactic (← `(tactic| refine Eq.trans ?_ $h)) + +/-- Whether `a` and `b` are definitionally equal, without assigning metavariables of the goal. -/ +private def isDefEqGoal (a b : Expr) : MetaM Bool := withNewMCtxDepth (isDefEq a b) + +/-- Runs `f x` for each `x` of `xs` in turn, each from the current state, and returns the result of +the first that succeeds. When every one fails it throws the last error, and `err` when `xs` is +empty. -/ +private def firstSuccess {α β : Type} (xs : List α) (err : MessageData) (f : α → TacticM β) : + TacticM β := do + let s ← saveState + let mut err := err + for x in xs do + try return ← f x + catch e => s.restore; err := e.toMessageData + throwError err + +/-- The number of trailing arguments of `e` that are the tensors it is built from: the argument of +a bundled map, the two factors of `T ⊗ₜ U`, the two summands of a sum, and the tensor of a negative +or of a scalar multiple. -/ +private def tensorArgs (e : Expr) : Nat := + if e.isAppOf ``TensorProduct.tmul || e.isAppOfArity ``HAdd.hAdd 6 then 2 + else if e.isAppOfArity ``DFunLike.coe 6 || e.isAppOfArity ``Neg.neg 3 || + e.isAppOfArity ``HSMul.hSMul 6 then 1 + else 0 + +/-- The tensors that `e` is built from, from left to right, through the arguments of +`tensorArgs`. -/ +private partial def tensorLeaves (e : Expr) : List Expr := + let e := e.consumeMData + let args := e.getAppArgs + match tensorArgs e with + | 0 => [e] + | n => (args.toList.drop (args.size - n)).flatMap tensorLeaves + +/-- The expression `e` with its leaf `k` of `tensorLeaves e` replaced by `v` if it is `x`, and `k` +less the number of leaves of `e`. -/ +private partial def replaceLeaf (x v e : Expr) (k : Int) : Expr × Int := + let e := e.consumeMData + let n := tensorArgs e + if k < 0 then (e, k) + else if n == 0 then (if k == 0 && e == x then v else e, k - 1) + else + let args := e.getAppArgs + let (args, k) := (List.range n).foldl (init := (args, k)) fun (args, k) i => + let j := args.size - n + i + let (a, k) := replaceLeaf x v args[j]! k + (args.set! j a, k) + (mkAppN e.getAppFn args, k) + +/-- Rewrites the subterm `x` of the goal by the term `thm : x' = y`, where `x'` is `x` up to +unfolding instances, as in `rw`: on the left side only if `lhs = some true`, on the right side only +if `lhs = some false`, and only at the leaf `k` of the tensor leaves of the two sides +(`tensorLeaves`) if `k` is given. Unlike `rw`, it finds `x` by syntactic equality and does not check +the type of the whole goal, which on large products is slow. -/ +private def rewriteSubterm (x : Expr) (thm : Term) (lhs : Option Bool := none) + (k : Option Nat := none) : TacticM Unit := do + let (_, l, r) ← goalEq + -- As in `rw`, the arguments of `thm` left open, such as `rfl`, are elaborated after `x` fixes + -- the implicit arguments. + let h ← Tactic.elabTerm thm none (mayPostpone := true) + let some (_, x', _) := (← instantiateMVars (← inferType h)).eq? + | throwError "tensor_regroup: internal error, {thm} is not an equation" + unless ← withTransparency .instances (isDefEq x x') do + throwError "tensor_regroup: {thm} does not rewrite{indentExpr x}" + Term.synthesizeSyntheticMVarsNoPostponing + let motive ← withLocalDeclD `x (← inferType x) fun v => do + let (l, r) := match k with + | none => + let replace (e : Expr) := e.replace fun e => if e == x then some v else none + (if lhs != some false then replace l else l, if lhs != some true then replace r else r) + | some k => + let (l', k) := if lhs != some false then replaceLeaf x v l k else (l, k) + (l', if lhs != some true then (replaceLeaf x v r k).1 else r) + mkLambdaFVars #[v] (← mkEq l r) + let pf ← mkCongrArg motive (← instantiateMVars h) + let some (_, _, target) := (← inferType pf).eq? | throwError "tensor_regroup: internal error" + replaceMainGoal [← (← getMainGoal).replaceTargetEq target.headBeta pf] + +/-! + +### B.6. Matching the factors of two contractions + +-/ + +/-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ +private def productFactors? (e : Expr) : Option (Expr × Expr) := do + let x := (← bundledArg? ``Tensors.Species.tmulEquiv e).consumeMData + if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none /-- The factors of a product, from left to right. -/ private partial def factors (e : Expr) : List Expr := @@ -493,28 +594,28 @@ private def leadingProduct (e : Expr) (m n : Nat) : Expr := Id.run do if let some (a, _) := productFactors? e then e := a return e -/-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ -private def contractedProducts : TacticM (Expr × Expr) := do - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_regroup: the goal is not an equation" - let arg? (e : Expr) : Option Expr := - let e := e.consumeMData - if e.isAppOfArity ``DFunLike.coe 6 && - e.appFn!.appArg!.isAppOf ``Tensors.Species.Tensor.contr then some e.appArg! else none - let some p := arg? l | throwError "tensor_regroup: the left side is not a contraction" - let some q := arg? r | throwError "tensor_regroup: the right side is not a contraction" - return (p, q) - /-- Whether every product inside the product `e` is in its left factor. -/ private partial def isLeftNested (e : Expr) : Bool := match productFactors? e with | some (a, b) => (productFactors? b).isNone && isLeftNested a | none => true +/-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ +private def contractedProducts : TacticM (Expr × Expr) := do + let (_, l, r) ← goalEq + let some p := bundledArg? ``Tensors.Species.Tensor.contr l + | throwError "tensor_regroup: the left side is not a contraction" + let some q := bundledArg? ``Tensors.Species.Tensor.contr r + | throwError "tensor_regroup: the right side is not a contraction" + return (p, q) + /-- Writes both sides of the goal as one contraction of a left-nested product, repeating until the -goal no longer changes, at most ten times. The three steps run separately, since together their -lemmas loop. -/ +goal no longer changes, at most ten times. A side that is not a contraction is read as one with no +pairs (`Species.Tensor.contr_noPairs`). The three steps run separately, since together their lemmas +loop. -/ private def flattenContractions : TacticM Unit := do + wrapSides "tensor_regroup" (bundledArg? ``Tensors.Species.Tensor.contr · |>.isSome) + (← `(Tensors.Species.Tensor.contr_noPairs _)) for _ in [0:10] do let before ← instantiateMVars (← getMainTarget) evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr_left, @@ -527,9 +628,9 @@ private def flattenContractions : TacticM Unit := do throwError "tensor_regroup: flattening the products did not stop after ten rounds" /-- Swaps the factors `m` and `m + 1` of the product `q`, of `n` factors, on the right side of the -goal, and absorbs the relabeling into the contraction. For two equal factors the two sides of the -swap lemma agree up to the relabeling, so it is rewritten once with `rw` rather than `simp`. -/ -private def swapFactors (q : Expr) (n m : Nat) (equal : Bool) : TacticM Unit := do +goal, by `Species.tmulEquiv_comm` or `Species.tmulEquiv_swapLast`, and absorbs the relabeling into +the contraction. -/ +private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do let fs := factors q let thm ← if m = 0 then `((Tensors.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) @@ -537,14 +638,9 @@ private def swapFactors (q : Expr) (n m : Nat) (equal : Bool) : TacticM Unit := else `((Tensors.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) - if equal then - evalTactic (← `(tactic| conv_rhs => rw [$thm:term])) - evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, - Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) - else - evalTactic (← `(tactic| conv_rhs => simp only [$thm:term, - Tensors.Species.tmulEquiv_reindex_left, Tensors.Species.reindex_reindex, - Tensors.Species.Tensor.contr_reindex])) + rewriteSubterm (leadingProduct q n (m + 2)) thm (some false) + evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, + Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) /-- The ways to match each factor on the left with a different factor on the right, given for each factor on the left the factors on the right equal to it. The matching in order comes first. -/ @@ -554,24 +650,25 @@ private partial def matchings : List (List Nat) → List Nat → List (List Nat) (c.filter (!used.contains ·)).flatMap fun j => (matchings cs (j :: used)).map (j :: ·) /-- Orders the factors on the right side of the goal so that the factor at each position `k` is -the one that started at position `target[k]`, by swapping neighbours. `equal i j` says whether -the factors that started at `i` and `j` are equal. -/ -private def arrange (target : List Nat) (equal : Nat → Nat → Bool) : TacticM Unit := do +the one that started at position `target[k]`, by swapping neighbours. -/ +private def arrange (target : List Nat) : TacticM Unit := do let n := target.length -- The starting positions of the factors in their current order. let mut labels := List.range n for pos in (List.range n).reverse do - let k := labels.idxOf target[pos]! - for m in [k:pos] do - let (_, q) ← contractedProducts - swapFactors q n m (equal labels[m]! labels[m+1]!) + for m in [labels.idxOf target[pos]!:pos] do + swapFactors (← contractedProducts).2 n m labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! +/-- The largest number of matchings of equal factors that `matchFactors` tries, so that a false goal +fails within the default heartbeats. -/ +private def maxMatchings : Nat := 12 + /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same factors. It orders the factors of the right side as on the left, and checks by evaluation that both sides keep the same slots and contract the same pairs of slots (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of -matching the equal factors is tried in turn. -/ +matching the equal factors is tried in turn, at most `maxMatchings` of them. -/ private def matchFactors : TacticM Unit := do let (p, q) ← contractedProducts unless isLeftNested p && isLeftNested q do @@ -580,47 +677,38 @@ private def matchFactors : TacticM Unit := do let qs := factors q unless ps.length = qs.length do throwError "tensor_regroup: the two sides have different numbers of factors" - let n := ps.length - -- Factors are compared once, without assigning metavariables of the goal. - let same (a b : Expr) : TacticM Bool := withNewMCtxDepth (isDefEq a b) - let candidates ← ps.mapM fun x => (List.range n).filterM fun j => same x qs[j]! - let table ← (List.range n).toArray.mapM fun i => - (List.range n).toArray.mapM fun j => same qs[i]! qs[j]! - let equal : Nat → Nat → Bool := fun i j => (table[i]!)[j]! + let candidates ← ps.mapM fun x => (List.range qs.length).filterM fun j => isDefEqGoal x qs[j]! + if let some (x, _) := (ps.zip candidates).find? (·.2.isEmpty) then + throwError "tensor_regroup: the factor{indentExpr x}\nhas no equal factor on the right" let targets := matchings candidates [] if targets.isEmpty then - throwError "tensor_regroup: some factor on the left has no equal factor on the right" - let s ← saveState - let mut lastError : MessageData := m!"" - for target in targets do - s.restore - try - arrange target equal - -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The - -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free - -- color variables of the goal although the bijections of slots do not depend on them. - evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in - exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) - (of_decide_eq_true rfl) _)) - return - catch e => lastError := e.toMessageData - throwError "tensor_regroup: no matching of the factors gives two contractions of the same pairs \ - of slots. The last attempt failed with\n{lastError}" - -/-- Whether `e` is a contraction `Tensor.contr σ P`. -/ -private def isContr (e : Expr) : Bool := - let e := e.consumeMData - e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.Tensor.contr + throwError "tensor_regroup: a factor appears more often on the left than on the right" + let tried := if targets.length ≤ maxMatchings then m!"each way" else + m!"the first {maxMatchings} of the {targets.length} ways" + firstSuccess (targets.take maxMatchings) m!"" fun target => do + arrange target + -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The + -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free + -- color variables of the goal although the bijections of slots do not depend on them. + try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in + exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) + (of_decide_eq_true rfl) _)) + catch _ => throwError "tensor_regroup: the two sides contract different pairs of slots, for \ + {tried} of matching their equal factors" + +/-! + +### B.7. Deltas and forms + +-/ /-- The factors among `xs` that are left over once each factor of `ys` takes an equal factor of `xs`. -/ -private def leftover (xs ys : List Expr) : TacticM (List Expr) := do +private def leftover (xs ys : List Expr) : MetaM (List Expr) := do let mut rest := xs for y in ys do - for (x, i) in rest.zipIdx do - if ← withNewMCtxDepth (isDefEq x y) then - rest := rest.eraseIdx i - break + if let some i ← (List.range rest.length).findM? fun i => isDefEqGoal rest[i]! y then + rest := rest.eraseIdx i return rest /-- The color `a` of a delta `S.deltaTensor a`. -/ @@ -628,19 +716,22 @@ private def deltaColor? (x : Expr) : Option Expr := let x := x.consumeMData if x.isAppOf ``Tensors.Species.deltaTensor then some x.appArg! else none -/-- The color `c` of a form `S.formTensor c`. -/ -private def formColor? (x : Expr) : Option Expr := +/-- The species `S` and the color `c` of a form `S.formTensor c`. -/ +private def formArgs? (x : Expr) : Option (Expr × Expr) := let x := x.consumeMData - if x.isAppOf ``Tensors.Species.formTensor then some x.appFn!.appArg! else none + if x.isAppOf ``Tensors.Species.formTensor then + let args := x.getAppArgs + some (args[args.size - 3]!, args[args.size - 2]!) + else none /-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, if `lhs`, or of the right side, by `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`). -/ private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : TacticM Unit := do let (p, q) ← contractedProducts - let thm ← `(Tensors.Species.Tensor.contr_deltaTensor ($(← slots.elem slot) : $(← slots.type)) - $(← exprToSyntax a) rfl $(← exprToSyntax (if lhs then p else q))) - if lhs then evalTactic (← `(tactic| conv_lhs => rw [← $thm:term])) - else evalTactic (← `(tactic| conv_rhs => rw [← $thm:term])) + let P := if lhs then p else q + let thm ← `((Tensors.Species.Tensor.contr_deltaTensor + ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) + rewriteSubterm P thm (some lhs) mutual @@ -648,89 +739,67 @@ mutual the product of color `a` in turn, and proves the goal by `regroupContractions`. -/ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) : TacticM Unit := do let (p, q) ← contractedProducts - let ty ← whnfR (← inferType (if lhs then p else q)) - let args := ty.getAppArgs - let some slots ← Slots.ofType? args[args.size - 2]! - | throwError "tensor_regroup: the slots of a product are not `Fin n` and `⊕`" - let some colors ← colorList? args[args.size - 1]! - | throwError "tensor_regroup: the colors of a product are not a literal list" - let s ← saveState - let mut lastError : MessageData := m!"tensor_regroup: no slot has the color {a} of a delta" - for (col, slot) in colors.zipIdx do - unless ← withNewMCtxDepth (isDefEq col a) do continue - s.restore - try + let some (slots, _, colors) ← literalTensorType? (← inferType (if lhs then p else q)) + | throwError "tensor_regroup: the slots or colors of a product are not literal" + let slotsOfColor ← colors.zipIdx.filterM fun (col, _) => isDefEqGoal col a + firstSuccess (slotsOfColor.map (·.2)) m!"tensor_regroup: no slot has the color {a} of a delta" + fun slot => do insertDelta lhs a slots slot regroupContractions - return - catch e => lastError := e.toMessageData - throw (.error (← getRef) lastError) + +/-- Matches two forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors `ds` +left over on the left side, if `lhs`, or on the right side. A delta `ds = [δ]` is written as the +contraction of the forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying the +color of each form. With `ds = []`, a delta is inserted first. -/ +private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Unit := do + let fs := gs.filterMap formArgs? + unless gs.length == 2 && fs.length == 2 do + throwError "tensor_regroup: the factors left over are not two forms" + match ds with + | [d] => + unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" + firstSuccess fs m!"" fun (S, c) => do + let thm ← `((Tensors.Species.contr_formTensor_formTensor (S := $(← exprToSyntax S)) + $(← exprToSyntax c)).symm) + rewriteSubterm d thm (some lhs) + regroupContractions + | [] => firstSuccess fs m!"" fun (_, c) => do + insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) + | _ => throwError "tensor_regroup: more than one factor is left over against two forms" /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to -deltas. A side that is not a contraction is read as one with no pairs -(`Species.Tensor.contr_noPairs`). Each side is flattened to one contraction of a left-nested -product. A delta on one side with no counterpart on the other is inserted on the other side at -each slot of its color in turn, since contracting with a delta is a relabeling. A delta against -two forms `g` and `g⁻¹` on the other side is written as their contraction, and two such forms -with nothing against them are matched by inserting a delta first. The factors are then matched -by `matchFactors`. -/ +deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested +product. Two forms on one side with a delta or nothing left over on the other are matched by +`matchFormPair`. A delta on one side with no counterpart on the other is inserted on the other side +at each slot of its color in turn, since contracting with a delta is a relabeling. The factors are +then matched by `matchFactors`. -/ private partial def regroupContractions : TacticM Unit := do - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_regroup: the goal is not an equation" - unless isContr l do - evalTactic (← `(tactic| refine (Tensors.Species.Tensor.contr_noPairs _).symm.trans ?_)) - unless isContr r do - evalTactic (← `(tactic| refine Eq.trans ?_ (Tensors.Species.Tensor.contr_noPairs _))) flattenContractions let (p, q) ← contractedProducts let extraL ← leftover (factors p) (factors q) let extraR ← leftover (factors q) (factors p) - -- A delta on one side against two forms on the other is written as the contraction of the - -- forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying the color of each. - -- Against nothing, a delta is inserted first. for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do - unless gs.length == 2 && gs.all (formColor? · |>.isSome) do continue - let s ← saveState - match ds with - | [d] => - unless (deltaColor? d).isSome do continue - for g in gs do - let some c := formColor? g | continue - s.restore - try - let thm ← `(Tensors.Species.contr_formTensor_formTensor $(← exprToSyntax c)) - if lhs then evalTactic (← `(tactic| conv_lhs => rw [← $thm:term])) - else evalTactic (← `(tactic| conv_rhs => rw [← $thm:term])) - regroupContractions - return - catch _ => pure () - | [] => - for g in gs do - let some c := formColor? g | continue - s.restore - try - insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) - return - catch _ => pure () - | _ => pure () - s.restore - let some extraL := extraL.mapM deltaColor? | matchFactors - let some extraR := extraR.mapM deltaColor? | matchFactors + try matchFormPair lhs ds gs; return catch _ => pure () -- A delta left over on the right is inserted on the left, and conversely. - match extraR, extraL with - | a :: _, _ => insertDeltaAnywhere true a - | [], a :: _ => insertDeltaAnywhere false a - | [], [] => matchFactors + match extraL.mapM deltaColor?, extraR.mapM deltaColor? with + | some _, some (a :: _) => insertDeltaAnywhere true a + | some (a :: _), some [] => insertDeltaAnywhere false a + | _, _ => matchFactors end +/-! + +### B.8. Sums, signs and scalars + +-/ + /-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a factor. -/ private def normalizeSums : TacticM Unit := do - let some (ty, _, _) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_regroup: the goal is not an equation" + let (ty, _, _) ← goalEq let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, TensorProduct.tmul_add, TensorProduct.add_tmul, TensorProduct.tmul_neg, @@ -743,32 +812,17 @@ private partial def summands (e : Expr) : List Expr := let e := e.consumeMData if e.isAppOfArity ``HAdd.hAdd 6 then summands e.appFn!.appArg! ++ summands e.appArg! else [e] -/-- The forms `S.formTensor c` in `e`, each once. -/ -private def forms (e : Expr) : MetaM (List Expr) := do - let arity ← forallTelescopeReducing (← getConstInfo ``Tensors.Species.formTensor).type - fun xs _ => pure xs.size - let found ← IO.mkRef ([] : List Expr) - -- `forEachExpr` visits each shared subterm once. - forEachExpr e fun x => do - if x.isAppOfArity ``Tensors.Species.formTensor arity then - found.modify fun fs => if fs.contains x then fs else fs ++ [x] - found.get - -/-- Rewrites the `k`-th occurrence of the form `F = S.formTensor c` in the goal by its swap, +/-- Rewrites the form `F = S.formTensor c` at the tensor leaf `k` of the goal by its swap, `g^μν = -g^νμ` for an antisymmetric form (`Species.formTensor_antisymm`), if `antisym`, and `g^μν = g^νμ` for a symmetric one (`Species.formTensor_symm`). -/ private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do - let args := F.getAppArgs - let c ← exprToSyntax args[args.size - 2]! - let S ← exprToSyntax args[args.size - 3]! + let some (S, c) := formArgs? F | throwError "tensor_regroup: internal error, {F} is not a form" + let (S, c) := (← exprToSyntax S, ← exprToSyntax c) + let σ ← `(Tensors.ColorEquiv.mk (Equiv.swap 0 1)) let thm ← if antisym then - `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c - (σ := Tensors.ColorEquiv.mk (Equiv.swap 0 1)) rfl).symm) - else - `((Tensors.Species.formTensor_symm (S := $S) $c - (σ := Tensors.ColorEquiv.mk (Equiv.swap 0 1)) rfl).symm) - let kStx : TSyntax `num := quote k - evalTactic (← `(tactic| nth_rw $kStx:num [$thm:term])) + `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c (σ := $σ) rfl).symm) + else `((Tensors.Species.formTensor_symm (S := $S) $c (σ := $σ) rfl).symm) + rewriteSubterm F thm (k := some k) mutual @@ -777,10 +831,7 @@ contractions they multiply. When exactly one side is negated, one antisymmetric side is swapped first, as in `ε^αβ = -ε^βα`. When the regrouping fails, one symmetric form is swapped, as in `η^μν = η^νμ`. At most one form is swapped. -/ private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := do - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_regroup: the goal is not an equation" - let l := l.consumeMData - let r := r.consumeMData + let (_, l, r) ← goalEq let negL := l.isAppOfArity ``Neg.neg 3 let negR := r.isAppOfArity ``Neg.neg 3 if negL && negR then @@ -790,7 +841,7 @@ private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := d flipOneForm true else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then let a := l.appFn!.appArg! - unless ← withNewMCtxDepth (isDefEq a r.appFn!.appArg!) do + unless ← isDefEqGoal a r.appFn!.appArg! do throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) regroupSummand flipped @@ -803,23 +854,24 @@ private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := d s.restore try flipOneForm false catch _ => s.restore; throw e -/-- Tries each occurrence of each form in the goal for `flipForm antisym`, then moves a new sign -out and regroups. A form of the other symmetry fails at `flipForm`. -/ +/-- Tries each form among the tensor leaves of the goal for `flipForm antisym`, then moves a new +sign out and regroups. A form of the other symmetry fails at `flipForm`. -/ private partial def flipOneForm (antisym : Bool) : TacticM Unit := do - let goal ← instantiateMVars (← getMainTarget) + let (_, l, r) ← goalEq + let leaves := tensorLeaves l ++ tensorLeaves r let s ← saveState let mut lastError : MessageData := m!"tensor_regroup: no form to swap" - for F in ← forms goal do - -- The occurrences are tried in turn, until `nth_rw` finds no further one. - for k in List.range 16 do + for F in (leaves.filter (formArgs? · |>.isSome)).eraseDups do + for k in (List.range leaves.length).filter (leaves[·]! == F) do s.restore - try flipForm antisym F (k + 1) catch _ => break + try flipForm antisym F k catch _ => break try normalizeSums regroupSummand true return catch e => lastError := e.toMessageData - throw (.error (← getRef) lastError) + s.restore + throwError lastError end @@ -835,14 +887,29 @@ private partial def sumCongr (e : Expr) (hs : List Expr) : MetaM (Expr × List E | h :: hs => return (h, hs) | [] => throwError "tensor_regroup: internal error, too few summand proofs" +/-- The position among `rs`, outside `used`, of a summand equal to `a` of type `ty` by +`regroupSummand`, with the proof of the equation. -/ +private def matchSummand (ty a : Expr) (rs : List Expr) (used : List Nat) : + TacticM (Nat × Expr) := do + let js := (List.range rs.length).filter (!used.contains ·) + try + firstSuccess js m!"" fun j => do + -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. + let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) + setGoals [g.mvarId!] + regroupSummand + unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" + return (j, ← instantiateMVars g) + catch _ => + throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" + /-- The body of `tensor_regroup`: moves sums, negatives and scalars out, then proves the equation summand by summand. -/ private def regroup : TacticM Unit := do normalizeSums -- Moving the scalars out may already close the goal. if (← getGoals).isEmpty then return - let some (ty, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_regroup: the goal is not an equation" + let (ty, l, r) ← goalEq let ls := summands l let rs := summands r unless ls.length = rs.length do @@ -856,23 +923,9 @@ private def regroup : TacticM Unit := do let mut used : List Nat := [] let mut proofs : Array Expr := #[] for a in ls do - let mut found := false - for j in List.range rs.length do - if used.contains j then continue - let s ← saveState - -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. - let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) - try - setGoals [g.mvarId!] - regroupSummand - unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" - used := j :: used - proofs := proofs.push (← instantiateMVars g) - found := true - break - catch _ => s.restore - unless found do - throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" + let (j, pf) ← matchSummand ty a rs used + used := j :: used + proofs := proofs.push pf -- The summands are replaced by congruence, not by `rw`: a summand can occur inside another one, -- as `T` inside its relabeling. let (pf, _) ← sumCongr l proofs.toList @@ -889,7 +942,8 @@ negatives and scalars are first moved out of every product, contraction and rela summand on the left is then matched with a summand on the right with the same sign and scalar, whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times. +number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 +are tried. Beyond regrouping, it uses these identities: - a delta on one side with no counterpart on the other is inserted on the other side at a slot of @@ -919,15 +973,10 @@ elab "tensor_regroup" : tactic => do /-! -### B.6. Chains of lowerings and raisings +### B.9. Chains of lowerings and raisings -/ -/-- Whether `e` is a slotwise map `S.mapSlots F t` applied to a tensor. -/ -private def isMapSlots (e : Expr) : Bool := - let e := e.consumeMData - e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf ``Tensors.Species.mapSlots - /-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps @@ -935,23 +984,17 @@ are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be `Fin n` with a literal `n`. -/ elab "tensor_slots" : tactic => do - evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, + evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, Tensors.Species.mapSlots_mapSlots])) - let some (_, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "tensor_slots: the goal is not an equation" - -- The branch is chosen from the two sides, since a failed unification with a side that is a + let isMapSlots (e : Expr) := (bundledArg? ``Tensors.Species.mapSlots e).isSome + let (_, l, r) ← goalEq "tensor_slots" + unless isMapSlots l || isMapSlots r do + throwError "tensor_slots: neither side lowers or raises an index" + -- Each side is checked before it is unified, since a failed unification with a side that is a -- definition, such as a named tensor, unfolds it. - match isMapSlots l, isMapSlots r with - | true, true => evalTactic (← `(tactic| - refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_))) - | true, false => evalTactic (← `(tactic| - refine (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)).trans - (Tensors.Species.mapSlots_id_apply _))) - | false, true => evalTactic (← `(tactic| - refine (Tensors.Species.mapSlots_id_apply _).symm.trans - (congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_)))) - | false, false => throwError "tensor_slots: neither side lowers or raises an index" + wrapSides "tensor_slots" isMapSlots (← `(Tensors.Species.mapSlots_id_apply _)) evalTactic (← `(tactic| focus + refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_) intro x fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, From ec1141a9ebd9d3159d43a6ee5807a81c46db19a0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:17:36 +0000 Subject: [PATCH 062/242] test(Elab): tensor_regroup with a delta on either side, three equal factors and false goals; tensor_slots with the chain on the right and with no chain Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 6425887f94..1b903d6078 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -1148,6 +1148,38 @@ example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by tensor_regroup +/-- A delta on the left side is inserted on the right, `T^μ_ρ δ^ρ_ν = T^μ_ν`. -/ +example : {T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = T | μ ν}ᵀ := by + tensor_regroup + +/-- Three equal factors, matched in one of the `3!` ways, +`T^μ_ν T^ν_ρ T^ρ_σ = T^ρ_σ T^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ T | ν ρ ⊗ T | ρ σ = T | ρ σ ⊗ T | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +-- A factor with no counterpart on the other side is named. +/-- error: tensor_regroup: the factor + U +has no equal factor on the right -/ +#guard_msgs in +example : {T | μ ν ⊗ U | ν ρ = W | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +-- The same factors contracted along other pairs of slots, `T U W ≠ T W U`, are rejected. +/-- error: tensor_regroup: the two sides contract different pairs of slots, for each way of +matching their equal factors -/ +#guard_msgs in +example : {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ = T | μ ν ⊗ W | ν ρ ⊗ U | ρ σ}ᵀ := by + tensor_regroup + +-- A summand with no counterpart on the other side is named. +/-- error: tensor_regroup: the summand + W +has no matching summand on the right -/ +#guard_msgs in +example : {T | μ ν + W | μ ν = U | μ ν + T | μ ν}ᵀ := by + tensor_regroup + section Metric variable [S.HasForm κ] [S.HasForm c.kind] @@ -1179,6 +1211,16 @@ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by tensor_slots +/-- `tensor_slots` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ +example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by + tensor_slots + +-- An equation with no lowering or raising is rejected. +/-- error: tensor_slots: neither side lowers or raises an index -/ +#guard_msgs in +example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by + tensor_slots + /-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by tensor_slots @@ -1215,6 +1257,10 @@ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTenso example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by tensor_regroup +/-- The delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ +example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by + tensor_regroup + end Metric section Tensorial From d9f004a9c272181acfcb1053177b1387c1dd1849 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:36:20 +0000 Subject: [PATCH 063/242] feat(Pauli): adjugate_pauliMatrix, adjugate_minkowskiMatrix_smul_pauliMatrix, sum_metricRaw_mul_sum_metricRaw_mul; tensorBasis_repr_pauliTensor_down, _downAll and pauliCoDown_trace_pauliCo without fin_cases; tensorBasis_repr_pauliTensor_downWeyl removed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lowering both Weyl indices of a matrix with ε gives its adjugate (Weyl/Metric), and adj σ^μ = σ̄^μ (PauliMatrices/SelfAdjoint), so the components of the lowered Pauli tensors are matrix identities. The trace components are tr(σ^μᵀ σ̄^νᵀ), closed by Matrix.trace_transpose_mul. Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 46 ++++++------------- Physlib/Relativity/Fermions/Weyl/Metric.lean | 6 +++ .../Relativity/PauliMatrices/SelfAdjoint.lean | 12 +++++ 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 7f8430e628..658b3a9876 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -27,6 +27,8 @@ The lowered forms are written with `↓` in index notation, such as `{pauliTenso `σT_μ^{αβ̇}`. Lowering contracts the first index of `η` or `ε` (`Species.toDual`), so `σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, `{pauliTensor | μ ↓α ↓β}ᵀ` is `σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix `σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. +Lowering both indices of a matrix with `ε` gives its adjugate, transposed, and +`adj σ^μ = σ̄^μ`, so the components of the lowered forms are matrix identities. One identity is read off from the Pauli matrices, the trace identity `pauliCoDown_trace_pauliCo`, `tr(σ̄_μ σ_ν) = 2 η_μν`: its components are the matrix traces of @@ -123,43 +125,22 @@ lemma pauliTensor_invariant (g : SL(2, ℂ)) : g • pauliTensor = pauliTensor : lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).kind) : (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν α β}ᵀ μ = minkowskiMatrix (μ 0) (μ 0) * pauliMatrix (μ 0) (μ 1) (μ 2) := by - have h (a : Fin 1 ⊕ Fin 3) (b c : Fin 2) : ∑ x, (minkowskiMatrix x a : ℂ) * pauliMatrix x b c = - minkowskiMatrix a a * pauliMatrix a b c := by - rw [Finset.sum_eq_single a (fun x _ hx => by rw [minkowskiMatrix.off_diag_zero hx]; simp) - (by simp)] tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, tensorBasis_repr_pauliTensor] - exact h _ _ _ + -- Only the diagonal entry `η_μμ` of `η` contributes. + exact Fintype.sum_eq_single _ fun x hx => by simp [formEntries, minkowskiMatrix.off_diag_zero hx] -/-- The components of `σT^μ_{αβ̇}`, with both Weyl indices lowered: `η_μμ σT^{μβα}` at the labels -`(μ, α, β)`. -/ -lemma tensorBasis_repr_pauliTensor_downWeyl - (μ : Π i, Idx (![up, downL, downR] i).kind) : - (complexLorentz.tensorBasis _).repr {pauliTensor | ν ↓α ↓β}ᵀ μ = - minkowskiMatrix (μ 0) (μ 0) * pauliMatrix (μ 0) (μ 2) (μ 1) := by - have h (m : Fin 1 ⊕ Fin 3) (a b : Fin 2) : ∑ x, metricRaw x b * ∑ y, metricRaw y a * - pauliMatrix m y x = minkowskiMatrix m m * pauliMatrix m b a := by - rcases m with m | m <;> fin_cases m <;> fin_cases a <;> fin_cases b <;> - simp [Fin.sum_univ_two, metricRaw, pauliMatrix, minkowskiMatrix.inl_0_inl_0, - minkowskiMatrix.inr_i_inr_i] - tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, - tensorBasis_repr_pauliTensor] - exact h _ _ _ - -/-- The components of `σT_{μαβ̇}`, with all indices lowered: `σT^{μβα}` at the labels -`(μ, α, β)`. -/ +/-- The components of `σT_{μαβ̇}`, with all indices lowered, are the entries of the adjugate of +`σ̄^μ = η_μμ σ^μ`, transposed: `σT^{μβα}` at the labels `(μ, α, β)`. -/ lemma tensorBasis_repr_pauliTensor_downAll (μ : Π i, Idx (![down, downL, downR] i).kind) : (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν ↓α ↓β}ᵀ μ = pauliMatrix (μ 0) (μ 2) (μ 1) := by - have h (m : Fin 1 ⊕ Fin 3) (a b : Fin 2) : ∑ x, metricRaw x b * ∑ y, metricRaw y a * - ∑ z, (minkowskiMatrix z m : ℂ) * pauliMatrix z y x = pauliMatrix m b a := by - rcases m with m | m <;> fin_cases m <;> fin_cases a <;> fin_cases b <;> - simp [Fin.sum_univ_two, Fintype.sum_sum_type, Fin.sum_univ_three, metricRaw, pauliMatrix, - minkowskiMatrix.as_diagonal, Matrix.diagonal_apply] + -- `↓` unfolds the lowered vector index by `tensorBasis_repr_pauliTensor_down` first. tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, - tensorBasis_repr_pauliTensor] - exact h _ _ _ + ↓tensorBasis_repr_pauliTensor_down] + exact (sum_metricRaw_mul_sum_metricRaw_mul _ _ _).trans + (congrFun₂ (PauliMatrix.adjugate_minkowskiMatrix_smul_pauliMatrix _) _ _) /-- `σT^μ_{αβ̇} = σT_{ραβ̇} η^ρμ`, raising the vector index of the fully lowered Pauli tensor. -/ lemma pauliContrDown_eq_pauliCoDown_contr : @@ -184,10 +165,9 @@ lemma pauliCoDown_trace_pauliCo : {pauliTensor | ↓μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • η' | μ ν}ᵀ := by tensor_components [tensorBasis_repr_pauliTensor_downAll, tensorBasis_repr_pauliTensor_down, tensorBasis_repr_formTensor_vec .down] - -- The components are `tr(σ̄_ν σ_μ)`, written out as a double sum. - exact (Finset.sum_congr rfl fun _ _ => Finset.sum_congr rfl fun _ _ => mul_comm _ _).trans - ((Matrix.trace_mul_comm _ _).trans - (PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix (φ 0) (φ 1))) + -- The components are `tr(σ^μᵀ σ̄^νᵀ) = tr(σ^μ σ̄^ν)`, written out as a double sum. + exact (Matrix.trace_transpose_mul _ _).trans + (PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix _ _) /-- The trace identity `tr(σ_μ σ̄_ν) = 2 η_μν`, as `σT_μ^{αβ̇} σT_{ναβ̇} = 2 η_μν`. -/ lemma pauliCo_trace_pauliCoDown : diff --git a/Physlib/Relativity/Fermions/Weyl/Metric.lean b/Physlib/Relativity/Fermions/Weyl/Metric.lean index 5e83c791ae..50d2f4aa00 100644 --- a/Physlib/Relativity/Fermions/Weyl/Metric.lean +++ b/Physlib/Relativity/Fermions/Weyl/Metric.lean @@ -75,6 +75,12 @@ lemma metricRaw_comm_star (M : SL(2,ℂ)) : metricRaw * M.1.map star = ((M.1)⁻ rw [eta_fin_two (!![M.1 0 0, M.1 0 1; M.1 1 0, M.1 1 1].map star)] simp +/-- Lowering both indices of a `2 × 2` matrix with the metric gives its adjugate, transposed: +`∑ x y, ε_{xb} ε_{ya} M^{yx} = (adj M)_{ba}`, the components of `ε Mᵀ εᵀ = adj M`. -/ +lemma sum_metricRaw_mul_sum_metricRaw_mul (M : Matrix (Fin 2) (Fin 2) ℂ) (a b : Fin 2) : + ∑ x, metricRaw x b * ∑ y, metricRaw y a * M y x = adjugate M b a := by + fin_cases a <;> fin_cases b <;> simp [Fin.sum_univ_two, metricRaw, adjugate_fin_two] + /-! ## B. The metric for left-handed Weyl fermions diff --git a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean index 927ebfd2c1..513d858418 100644 --- a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean +++ b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean @@ -117,6 +117,18 @@ lemma trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix (μ ν : Fin 1 ⊕ · simp [mul_comm] · simp [eq_zero_of_ne h, minkowskiMatrix.off_diag_zero h] +/-- `σ̄^μ = η_μμ σ^μ` is the adjugate of `σ^μ`. -/ +lemma adjugate_pauliMatrix (μ : Fin 1 ⊕ Fin 3) : + adjugate (pauliMatrix μ) = (minkowskiMatrix μ μ : ℂ) • pauliMatrix μ := by + rcases μ with μ | μ <;> fin_cases μ <;> + simp [adjugate_fin_two, pauliMatrix, minkowskiMatrix.inl_0_inl_0, minkowskiMatrix.inr_i_inr_i] + +/-- `σ^μ` is the adjugate of `σ̄^μ = η_μμ σ^μ`. -/ +lemma adjugate_minkowskiMatrix_smul_pauliMatrix (μ : Fin 1 ⊕ Fin 3) : + adjugate ((minkowskiMatrix μ μ : ℂ) • pauliMatrix μ) = pauliMatrix μ := by + rw [← adjugate_pauliMatrix, adjugate_adjugate'] + simp + /-- The four real Pauli coefficients of a self-adjoint `2 × 2` matrix. -/ noncomputable def pauliCoeff (A : selfAdjoint (Matrix (Fin 2) (Fin 2) ℂ)) : From 62278789925697d2f2653392119cde49532e1fb4 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:36:40 +0000 Subject: [PATCH 064/242] refactor(Bispinors): tensorBasis_repr_pauliTensor from tensorBasis_repr_toTensor_prod and pauliVal_repr Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 658b3a9876..c6059aead8 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -74,7 +74,7 @@ identity of `ε`. @[expose] public section open TensorProduct Fermion Lorentz MatrixGroups -open PauliMatrix (pauliMatrix pauliBasis pauliSelfAdjoint) +open PauliMatrix (pauliMatrix) namespace Tensors.complexLorentz @@ -94,21 +94,13 @@ scoped[Tensors.complexLorentz] notation "σT" => pauliTensor /-- The components of `σT^{μαβ̇}` are the entries of the Pauli matrices. -/ lemma tensorBasis_repr_pauliTensor (μ : Π i, Idx (![up, upL, upR] i).kind) : (complexLorentz.tensorBasis _).repr pauliTensor μ = pauliMatrix (μ 0) (μ 1) (μ 2) := by - rw [pauliTensor, Species.tensorBasis_repr_reindex, ComplexVector.pauliVal, map_sum, map_sum, - Finsupp.finsetSum_apply] - simp only [Tensorial.toTensor_tmul, Species.tensorBasis_repr_tmulEquiv, - tensorBasis_repr_toTensor_leftRight, LinearEquiv.apply_symm_apply] - refine (Finset.sum_congr rfl fun i _ => congrArg (· * _) - (Species.tensorBasis_repr_oneSlotEquiv (S := complexLorentz) up _ _)).trans ?_ - -- The labels are casts along `rfl`, so the identity is stated for plain labels. - have h (a : Fin 1 ⊕ Fin 3) (b c : Fin 2) : - ∑ i, ComplexVector.basis.repr (ComplexVector.basis i) a * (pauliBasis i).1 b c = - pauliMatrix a b c := by - rw [Finset.sum_eq_single a (fun i _ hi => by - rw [Module.Basis.repr_self, Finsupp.single_eq_of_ne hi.symm, zero_mul]) (by simp), - Module.Basis.repr_self, Finsupp.single_eq_same, one_mul] - simp [pauliBasis, pauliSelfAdjoint] - exact h _ _ _ + rw [pauliTensor, Species.tensorBasis_repr_reindex] + -- The components of `pauliVal` in the product basis `e_μ ⊗ e_α ⊗ e_β̇` are `pauliVal_repr`. + exact (Tensorial.tensorBasis_repr_toTensor_prod (S := complexLorentz) (c₁ := ![up]) + ComplexVector.basis (LeftHandedWeyl.basis.tensorProduct RightHandedWeyl.basis) (· 0) + (fun ν => (ν (.inl 0), ν (.inr 0))) + (Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := complexLorentz) up) + tensorBasis_repr_toTensor_leftRight _ _).trans (ComplexVector.pauliVal_repr _ _ _) /-- `σT^{μαβ̇}` is an invariant tensor of `SL(2, ℂ)`, `g • σT = σT`. -/ lemma pauliTensor_invariant (g : SL(2, ℂ)) : g • pauliTensor = pauliTensor := by From 97309c41ce906c2583130d9dffdcc0ca39bca327 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 15:36:43 +0000 Subject: [PATCH 065/242] refactor(Bispinors): pauliCo_contr_pauliContr by pauliTensor_raise_lower; pauliContrDown_contr_eps removed Co-authored-by: Claude Opus 5.5 --- Physlib/Relativity/Bispinors/Pauli/Tensor.lean | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index c6059aead8..038f66620b 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -224,12 +224,6 @@ lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α ↑β}ᵀ = pauliTensor := by tensor_slots -/-- `σT^μ_{αβ̇} ε^{αα'} ε^{β̇β̇'} = σT^{μα'β̇'}`. -/ -lemma pauliContrDown_contr_eps : - {pauliTensor | μ ↓α ↓β ⊗ εL | α α' ⊗ εR | β β' = pauliTensor | μ α' β'}ᵀ := by - conv_rhs => rw [← pauliTensor_raise_lower] - tensor_regroup - /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is `pauliA σT = pauliA (pauliB (εL ⊗ εR)) / 2`. -/ lemma pauliCo_contr_pauliContr : @@ -237,7 +231,7 @@ lemma pauliCo_contr_pauliContr : (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by rw [map_smul, ← pauliA_pauliB] refine congrArg pauliA ?_ - conv_lhs => rw [← pauliContrDown_contr_eps] + rw [← pauliTensor_raise_lower] simp only [pauliB, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup From 695b209b729afdfa327ae15e9c601f6bec66c1c8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:32:09 +0000 Subject: [PATCH 066/242] chore: remove transitive imports in SesquilinearForm, HermitianForm, Metric and both species Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/SesquilinearForm.lean | 1 - Physlib/Mathematics/Tensors/HermitianForm.lean | 1 - Physlib/Mathematics/Tensors/Metric.lean | 1 - Physlib/Relativity/Tensors/ComplexTensor/Species.lean | 1 - Physlib/Relativity/Tensors/RealTensor/Species.lean | 1 - 5 files changed, 5 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index f65188fa5e..e62a39c2dd 100644 --- a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean +++ b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.SesquilinearForm.Star -public import Mathlib.Tactic.Module /-! diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 1218e2fcc1..7984340039 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.ForMathlib.SesquilinearForm public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Inverse /-! diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index cf1b4a9987..1848320eaf 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -8,7 +8,6 @@ module public import Physlib.Mathematics.ForMathlib.SesquilinearForm public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Contraction -public import Mathlib.LinearAlgebra.Matrix.BilinearForm public import Mathlib.Tactic.LinearCombination /-! diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index da2d86c334..55ea4ced6f 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -9,7 +9,6 @@ public import Physlib.Mathematics.Tensors.Elab public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Relativity.Fermions.Weyl.Metric public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre -public import Physlib.Mathematics.ForMathlib.TensorProductMatrix /-! # The species of complex Lorentz tensors diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 1105cbc8ff..6efcea5f11 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Elab public import Physlib.Relativity.Tensors.RealTensor.CoVector.Representation public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct From 9b0e037de99a10afe780e804f86366a7420b6b1f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:45:52 +0000 Subject: [PATCH 067/242] feat(ForMathlib): LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq; pauliA_pauliB from it Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/FiniteDimensional.lean | 19 +++++++++++++++++++ .../Relativity/Bispinors/Pauli/Tensor.lean | 12 +++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index 97ea489999..2429a2ff91 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -23,6 +23,8 @@ between two spaces `V` and `W` of the same finite dimension: `g ∘ f = id` on ` - `LinearMap.comp_eq_id_comm_of_finrank_eq` : `f ∘ g = id ↔ g ∘ f = id` between spaces of equal finite dimension. +- `LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq` : the same up to a nonzero + scalar, `g (f v) = c • v` for all `v` gives `f (g w) = c • w` for all `w`. ## iii. Table of contents @@ -57,3 +59,20 @@ lemma LinearMap.comp_eq_id_comm_of_finrank_eq (h : finrank K V = finrank K W) {f {g : W →ₗ[K] V} : f ∘ₗ g = LinearMap.id ↔ g ∘ₗ f = LinearMap.id := ⟨LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h.symm, LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h⟩ + +section Field + +variable {K V W : Type*} [Field K] [AddCommGroup V] [Module K V] [AddCommGroup W] [Module K W] + [FiniteDimensional K V] [FiniteDimensional K W] + +/-- Between spaces of equal finite dimension, `g (f v) = c • v` for all `v` and `c ≠ 0` give +`f (g w) = c • w` for all `w`. -/ +lemma LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq + (h : finrank K V = finrank K W) {f : V →ₗ[K] W} {g : W →ₗ[K] V} {c : K} (hc : c ≠ 0) + (hgf : ∀ v, g (f v) = c • v) (w : W) : f (g w) = c • w := by + have := LinearMap.congr_fun (LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h + (f := f) (g := c⁻¹ • g) (LinearMap.ext fun v => by simp [hgf, smul_smul, hc])) w + rw [← inv_smul_eq_iff₀ hc] + simpa [eq_comm] using this + +end Field diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 038f66620b..f86860653a 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -210,15 +210,9 @@ lemma pauliB_pauliA (p : ℂT[up, upL, upR]) : pauliB (pauliA p) = (2 : ℂ) • /-- `pauliA ∘ pauliB = 2`: both spaces have dimension `16`, so the left inverse `pauliB / 2` of `pauliA` is a right inverse. -/ -lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : ℂ) • X := by - have hdim : Module.finrank ℂ ℂT[up, upL, upR] = Module.finrank ℂ ℂT[upL, upR, upL, upR] := by - rw [Species.finrank_tensor, Species.finrank_tensor] - rfl - have h : ((2 : ℂ)⁻¹ • pauliB) ∘ₗ pauliA = LinearMap.id := LinearMap.ext fun p => by - simp [pauliB_pauliA] - have h' := LinearMap.congr_fun ((LinearMap.comp_eq_id_comm_of_finrank_eq hdim).2 h) X - simp only [LinearMap.comp_apply, LinearMap.smul_apply, map_smul, LinearMap.id_apply] at h' - exact (inv_smul_eq_iff₀ two_ne_zero).1 h' +lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : ℂ) • X := + LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq + (by rw [Species.finrank_tensor, Species.finrank_tensor]; rfl) two_ne_zero pauliB_pauliA X /-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α ↑β}ᵀ = pauliTensor := by From e60d0e3d6c53876a63a8bf7fab67a02788981653 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:46:31 +0000 Subject: [PATCH 068/242] feat(Reindexing): ColorEquiv.swapTwo; reindex_twoSlotEquiv, formTensor_symm and formTensor_antisymm stated with it Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 5 ++--- Physlib/Mathematics/Tensors/Metric.lean | 18 ++++++++---------- Physlib/Mathematics/Tensors/Product.lean | 8 +++----- Physlib/Mathematics/Tensors/Reindexing.lean | 7 +++++++ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 1b903d6078..1fef43ffd8 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -818,10 +818,9 @@ private partial def summands (e : Expr) : List Expr := private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do let some (S, c) := formArgs? F | throwError "tensor_regroup: internal error, {F} is not a form" let (S, c) := (← exprToSyntax S, ← exprToSyntax c) - let σ ← `(Tensors.ColorEquiv.mk (Equiv.swap 0 1)) let thm ← if antisym then - `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c (σ := $σ) rfl).symm) - else `((Tensors.Species.formTensor_symm (S := $S) $c (σ := $σ) rfl).symm) + `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c).symm) + else `((Tensors.Species.formTensor_symm (S := $S) $c).symm) rewriteSubterm F thm (k := some k) mutual diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 1848320eaf..5f50aac14a 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -287,18 +287,16 @@ lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := tensorBasis_repr_twoSlotEquiv _ _ _ μ -/-- A symmetric form is symmetric under the slot swap `σ` as a tensor, `g^μν = g^νμ`. -/ -lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] - {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : - S.reindex σ (S.formTensor c) = S.formTensor c := by - rw [formTensor, reindex_twoSlotEquiv hσ, form_symm] +/-- A symmetric form is symmetric under the slot swap as a tensor, `g^μν = g^νμ`. -/ +lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : + S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = S.formTensor c := by + rw [formTensor, reindex_twoSlotEquiv, form_symm] -/-- An antisymmetric form is antisymmetric under the slot swap `σ` as a tensor, +/-- An antisymmetric form is antisymmetric under the slot swap as a tensor, `g^μν = -g^νμ`. -/ -lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - {σ : ColorEquiv ![c, c] ![c, c]} (hσ : σ.toEquiv = Equiv.swap 0 1) : - S.reindex σ (S.formTensor c) = -S.formTensor c := by - rw [formTensor, reindex_twoSlotEquiv hσ, form_antisymm, map_neg] +lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : + S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = -S.formTensor c := by + rw [formTensor, reindex_twoSlotEquiv, form_antisymm, map_neg] /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index a219cf37c0..244ef16473 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -289,11 +289,9 @@ lemma twoSlotEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) /-- Swapping the two slots of a tensor on two slots swaps the factors, `M^μν ↦ M^νμ`. -/ -lemma reindex_twoSlotEquiv {a b : Color K} {σ : ColorEquiv ![a, b] ![b, a]} - (hσ : σ.toEquiv = Equiv.swap 0 1) (M : S.V a ⊗[k] S.V b) : - S.reindex σ (S.twoSlotEquiv a b M) = S.twoSlotEquiv b a (TensorProduct.comm k _ _ M) := by - obtain ⟨e, he⟩ := σ - subst hσ +lemma reindex_twoSlotEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : + S.reindex (ColorEquiv.swapTwo a b) (S.twoSlotEquiv a b M) = + S.twoSlotEquiv b a (TensorProduct.comm k _ _ M) := by induction M using TensorProduct.inductionOn with | add x y hx hy => simp only [map_add, hx, hy] | tmul x y => diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 9f9c7f9bb7..89fb9817db 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -93,6 +93,13 @@ def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := def trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : ColorEquiv c₁ c₃ := ⟨σ₁.toEquiv.trans σ₂.toEquiv, fun j => (σ₁.color_eq _).trans (σ₂.color_eq j)⟩ +/-- The relabeling that swaps the two slots of a tensor on two slots, `![a, b] → ![b, a]`. -/ +def swapTwo (a b : Color K) : ColorEquiv (K := K) ![a, b] ![b, a] := ⟨Equiv.swap 0 1, by + intro j; fin_cases j <;> rfl⟩ + +@[simp] +lemma toEquiv_swapTwo (a b : Color K) : (swapTwo a b).toEquiv = Equiv.swap 0 1 := rfl + @[simp] lemma toEquiv_refl (c₁ : ι₁ → Color K) : (refl c₁).toEquiv = Equiv.refl ι₁ := rfl From 99136cfe97f1540d61995c338cf93ba5dba0cf18 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:50:53 +0000 Subject: [PATCH 069/242] feat(Metric): formMatrix_schouten' for the other delta orientation; schouten_downR by it Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 13 ++++++++++++- .../Relativity/Tensors/ComplexTensor/Species.lean | 8 ++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 5f50aac14a..ac612a1aa2 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -54,7 +54,8 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th - `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an antisymmetric form on a kind with two labels, `LinearMap.IsAlt.schouten` for `formBilin`, and `Species.formMatrix_schouten` is the same in components, - `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. + `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`, and `Species.formMatrix_schouten'` with the delta + read the other way, `g^γρ δ_α^α' = g^γα δ_ρ^α' - g^ρα δ_γ^α'`. ## iii. Table of contents @@ -401,6 +402,16 @@ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor @eq_comm _ α' α] at h linear_combination h - (if ρ = α then 1 else 0) * formMatrix_antisymm (S := S) c γ α' +/-- The Schouten identity in components with the delta read the other way, +`g^γρ δ_α^α' = g^γα δ_ρ^α' - g^ρα δ_γ^α'`. -/ +lemma formMatrix_schouten' (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + [NoZeroDivisors k] [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) + (γ ρ α α' : basisIdx c.kind) : + S.formMatrix c γ ρ * (if α = α' then 1 else 0) = + S.formMatrix c γ α * (if ρ = α' then 1 else 0) - + S.formMatrix c ρ α * (if γ = α' then 1 else 0) := by + simpa only [@eq_comm _ α' α] using formMatrix_schouten c h2 γ ρ α α' + end Species end Tensors diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 55ea4ced6f..a8c098f55e 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -555,13 +555,9 @@ lemma schouten_downL : lemma schouten_downR : {εR' | γ ρ ⊗ δR' | α α' = εR' | γ α ⊗ δR' | ρ α' + -{εR' | ρ α ⊗ δR' | γ α'}ᵀ | ρ α γ α'}ᵀ := by tensor_components [Species.tensorBasis_repr_formTensor, - fun c μ => (Species.tensorBasis_repr_deltaTensor_eq_one (S := complexLorentz) c μ).trans - Matrix.one_apply] + Species.tensorBasis_repr_deltaTensor_eq_one, Matrix.one_apply] rw [← sub_eq_add_neg] - refine Eq.trans ?_ (Species.formMatrix_schouten downR (Fintype.card_fin 2) _ _ _ _) - -- The delta of `formMatrix_schouten` is `δ^α_α'`, read here as `δ_α'^α`. - congr 2 - exact propext eq_comm + exact Species.formMatrix_schouten' downR (Fintype.card_fin 2) _ _ _ _ /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : From af13b70b10e6e94b0730240df51b998303cfa7cb Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:52:48 +0000 Subject: [PATCH 070/242] feat(Elab): schouten_formTensor_deltaTensor and schouten_formTensor_deltaTensor_dual; schouten_downL/R as instances Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 49 +++++++++++++++++-- .../Tensors/ComplexTensor/Species.lean | 17 +++---- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 1fef43ffd8..763c924b6b 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -57,7 +57,7 @@ value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is -`contr_formTensor_formTensor` (section C). +`contr_formTensor_formTensor` (section D). ## ii. Key results @@ -67,6 +67,9 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, swaps of symmetric and antisymmetric forms, and lowerings written as contractions. - `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. +- `Species.schouten_formTensor_deltaTensor` and `Species.schouten_formTensor_deltaTensor_dual` : + the Schouten identity of an antisymmetric form on a kind with two labels, for both orientations + of the delta. ## iii. Table of contents @@ -81,7 +84,8 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.7. Deltas and forms - B.8. Sums, signs and scalars - B.9. Chains of lowerings and raisings -- C. Examples +- C. The Schouten identity +- D. Examples ## iv. References @@ -1009,7 +1013,46 @@ end /-! -## C. Examples +## C. The Schouten identity + +-/ + +public section + +namespace Tensors.Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] + [CharZero k] + +/-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`, +`g_γρ δ^α_α' = g_γα' δ_ρ^α - g_ρα' δ_γ^α`. -/ +lemma schouten_formTensor_deltaTensor (h2 : Fintype.card (basisIdx c.kind) = 2) : + {S.formTensor c | γ ρ ⊗ S.deltaTensor c | α α' = + S.formTensor c | γ α' ⊗ S.deltaTensor c.dual | ρ α + + -{S.formTensor c | ρ α' ⊗ S.deltaTensor c.dual | γ α}ᵀ | ρ α' γ α}ᵀ := by + tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] + rw [← sub_eq_add_neg] + exact formMatrix_schouten c h2 _ _ _ _ + +/-- The Schouten identity with the delta read the other way, +`g_γρ δ_α^α' = g_γα δ_ρ^α' - g_ρα δ_γ^α'`. -/ +lemma schouten_formTensor_deltaTensor_dual (h2 : Fintype.card (basisIdx c.kind) = 2) : + {S.formTensor c | γ ρ ⊗ S.deltaTensor c.dual | α α' = + S.formTensor c | γ α ⊗ S.deltaTensor c.dual | ρ α' + + -{S.formTensor c | ρ α ⊗ S.deltaTensor c.dual | γ α'}ᵀ | ρ α γ α'}ᵀ := by + tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] + rw [← sub_eq_add_neg] + exact formMatrix_schouten' c h2 _ _ _ _ + +end Tensors.Species + +end + +/-! + +## D. Examples -/ diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index a8c098f55e..f166864d3e 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -61,7 +61,8 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ - `complexLorentz.formTensor_upL_antisymm`, `complexLorentz.formTensor_up_symm` : `ε` is antisymmetric and `η` symmetric, and `complexLorentz.deltaTensor_swap` : `δ^μ_ν = δ_ν^μ`. - `complexLorentz.schouten_downL` and `complexLorentz.schouten_downR` : the Schouten identity of - `ε` in index notation, from `Species.formMatrix_schouten`. + `ε` in index notation, from `Species.schouten_formTensor_deltaTensor` and + `Species.schouten_formTensor_deltaTensor_dual`. - `complexLorentz.leftHandedWeylTensorial` and the other instances of section F : Weyl spinors and complex vectors as tensors with one slot. - `complexLorentz.tensorBasis_repr_toTensor_leftRight` : the components of a tensor in @@ -544,20 +545,14 @@ lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by /-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_γα' δ_ρ^α - ε_ρα' δ_γ^α`. -/ lemma schouten_downL : - {εL' | γ ρ ⊗ δL | α α' = εL' | γ α' ⊗ δL' | ρ α + -{εL' | ρ α' ⊗ δL' | γ α}ᵀ | ρ α' γ α}ᵀ := by - tensor_components [Species.tensorBasis_repr_formTensor, - Species.tensorBasis_repr_deltaTensor_eq_one, Matrix.one_apply] - rw [← sub_eq_add_neg] - exact Species.formMatrix_schouten downL (Fintype.card_fin 2) _ _ _ _ + {εL' | γ ρ ⊗ δL | α α' = εL' | γ α' ⊗ δL' | ρ α + -{εL' | ρ α' ⊗ δL' | γ α}ᵀ | ρ α' γ α}ᵀ := + Species.schouten_formTensor_deltaTensor downL (Fintype.card_fin 2) /-- The Schouten identity for right-handed indices, `ε_γ̇ρ̇ δ_α̇^α̇' = ε_γ̇α̇ δ_ρ̇^α̇' - ε_ρ̇α̇ δ_γ̇^α̇'`. -/ lemma schouten_downR : - {εR' | γ ρ ⊗ δR' | α α' = εR' | γ α ⊗ δR' | ρ α' + -{εR' | ρ α ⊗ δR' | γ α'}ᵀ | ρ α γ α'}ᵀ := by - tensor_components [Species.tensorBasis_repr_formTensor, - Species.tensorBasis_repr_deltaTensor_eq_one, Matrix.one_apply] - rw [← sub_eq_add_neg] - exact Species.formMatrix_schouten' downR (Fintype.card_fin 2) _ _ _ _ + {εR' | γ ρ ⊗ δR' | α α' = εR' | γ α ⊗ δR' | ρ α' + -{εR' | ρ α ⊗ δR' | γ α'}ᵀ | ρ α γ α'}ᵀ := + Species.schouten_formTensor_deltaTensor_dual downR (Fintype.card_fin 2) /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : From d0278dd681030458bfd43f2f6f231078147860ea Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:17:52 +0000 Subject: [PATCH 071/242] refactor(Schouten): Species.schouten_formTensor_deltaTensor and _dual moved to Tensors/Schouten; LinearMap.IsAlt.schouten moved to ForMathlib/Schouten Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 2 + Physlib/Mathematics/ForMathlib/Schouten.lean | 64 ++++++++++++++++ .../ForMathlib/SesquilinearForm.lean | 28 ------- Physlib/Mathematics/Tensors/Elab.lean | 49 +------------ Physlib/Mathematics/Tensors/Metric.lean | 1 + Physlib/Mathematics/Tensors/Schouten.lean | 73 +++++++++++++++++++ .../Tensors/ComplexTensor/Species.lean | 2 +- 7 files changed, 144 insertions(+), 75 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/Schouten.lean create mode 100644 Physlib/Mathematics/Tensors/Schouten.lean diff --git a/Physlib.lean b/Physlib.lean index bc3c97b5c0..c52fddb3c1 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -174,6 +174,7 @@ public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Basic public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Unitary public import Physlib.Mathematics.ForMathlib.OrthogonalMatrix public import Physlib.Mathematics.ForMathlib.Representation +public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SchurTriangulation public import Physlib.Mathematics.ForMathlib.SemilinearMaps public import Physlib.Mathematics.ForMathlib.SesquilinearForm @@ -210,6 +211,7 @@ public import Physlib.Mathematics.Tensors.Inverse public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing +public import Physlib.Mathematics.Tensors.Schouten public import Physlib.Mathematics.Tensors.Species public import Physlib.Mathematics.Tensors.Tensor public import Physlib.Mathematics.Tensors.Tensorial diff --git a/Physlib/Mathematics/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean new file mode 100644 index 0000000000..001d2ec4a2 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/Schouten.lean @@ -0,0 +1,64 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.SesquilinearForm.Basic +public import Mathlib.Tactic.Module + +/-! + +# The Schouten identity + +An alternating bilinear form on a module with a basis of two vectors satisfies the Schouten +identity. + +## i. Overview + +For an alternating bilinear form `B` on a module with a basis of two vectors, +`B(u, v) w + B(v, w) u + B(w, u) v = 0` for all `u`, `v`, `w`. Expanding each vector in the basis, +`B` vanishes on equal basis vectors and is antisymmetric on the other two, so the sum cancels. + +## ii. Key results + +- `LinearMap.IsAlt.schouten` : the Schouten identity for an alternating form in two dimensions. + +## iii. Table of contents + +- A. Alternating forms in two dimensions + +## iv. References + +-/ + +@[expose] public section + +open Module + +/-! + +## A. Alternating forms in two dimensions + +-/ + +namespace LinearMap.IsAlt + +variable {R M ι : Type*} [CommRing R] [AddCommGroup M] [Module R M] [Fintype ι] + [DecidableEq ι] {B : LinearMap.BilinForm R M} + +/-- The Schouten identity, `B(u, v) w + B(v, w) u + B(w, u) v = 0`, for an alternating bilinear +form on a module with a basis of two vectors. -/ +lemma schouten (hB : LinearMap.IsAlt B) (b : Basis ι R M) (h2 : Fintype.card ι = 2) (u v w : M) : + B u v • w + B v w • u + B w u • v = 0 := by + obtain ⟨i, j, hij, huniv⟩ := Finset.card_eq_two.mp (h2 : Finset.univ.card = 2) + -- Each vector is `x^i e_i + x^j e_j`, and `B` vanishes on `e_i, e_i` and on `e_j, e_j`. + have hexp (x : M) : x = b.repr x i • b i + b.repr x j • b j := by + conv_lhs => rw [← b.sum_repr x, huniv, Finset.sum_pair hij] + rw [hexp u, hexp v, hexp w] + simp only [map_add, map_smul, LinearMap.add_apply, LinearMap.smul_apply, smul_eq_mul, + LinearMap.IsAlt.self_eq_zero hB, (LinearMap.IsAlt.neg hB (b i) (b j)).symm] + module + +end LinearMap.IsAlt diff --git a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index e62a39c2dd..5c18d593be 100644 --- a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean +++ b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean @@ -30,7 +30,6 @@ in a basis it is positive-definite exactly when its matrix is. - `LinearMap.IsPosDef.nondegenerate` : a positive-definite form is nondegenerate. - `LinearMap.isPosDef_iff_posDef_toMatrix` : a form is positive-definite exactly when its matrix in a basis is. -- `LinearMap.IsAlt.schouten` : the Schouten identity for an alternating form in two dimensions. ## iii. Table of contents @@ -38,7 +37,6 @@ in a basis it is positive-definite exactly when its matrix is. - A.1. The definition - A.2. Consequences over a preorder - A.3. The matrix of a positive-definite form -- B. Alternating forms in two dimensions ## iv. References @@ -120,29 +118,3 @@ lemma isPosDef_iff_posDef_toMatrix (b : Basis n R M) : B.IsPosDef ↔ (toMatrix end toMatrix end LinearMap - -/-! - -## B. Alternating forms in two dimensions - --/ - -namespace LinearMap.IsAlt - -variable {R M ι : Type*} [CommRing R] [AddCommGroup M] [Module R M] [Fintype ι] - [DecidableEq ι] {B : LinearMap.BilinForm R M} - -/-- The Schouten identity, `B(u, v) w + B(v, w) u + B(w, u) v = 0`, for an alternating bilinear -form on a module with a basis of two vectors. -/ -lemma schouten (hB : LinearMap.IsAlt B) (b : Basis ι R M) (h2 : Fintype.card ι = 2) (u v w : M) : - B u v • w + B v w • u + B w u • v = 0 := by - obtain ⟨i, j, hij, huniv⟩ := Finset.card_eq_two.mp (h2 : Finset.univ.card = 2) - -- Each vector is `x^i e_i + x^j e_j`, and `B` vanishes on `e_i, e_i` and on `e_j, e_j`. - have hexp (x : M) : x = b.repr x i • b i + b.repr x j • b j := by - conv_lhs => rw [← b.sum_repr x, huniv, Finset.sum_pair hij] - rw [hexp u, hexp v, hexp w] - simp only [map_add, map_smul, LinearMap.add_apply, LinearMap.smul_apply, smul_eq_mul, - LinearMap.IsAlt.self_eq_zero hB, (LinearMap.IsAlt.neg hB (b i) (b j)).symm] - module - -end LinearMap.IsAlt diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 763c924b6b..1fef43ffd8 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -57,7 +57,7 @@ value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is -`contr_formTensor_formTensor` (section D). +`contr_formTensor_formTensor` (section C). ## ii. Key results @@ -67,9 +67,6 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, swaps of symmetric and antisymmetric forms, and lowerings written as contractions. - `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. -- `Species.schouten_formTensor_deltaTensor` and `Species.schouten_formTensor_deltaTensor_dual` : - the Schouten identity of an antisymmetric form on a kind with two labels, for both orientations - of the delta. ## iii. Table of contents @@ -84,8 +81,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.7. Deltas and forms - B.8. Sums, signs and scalars - B.9. Chains of lowerings and raisings -- C. The Schouten identity -- D. Examples +- C. Examples ## iv. References @@ -1013,46 +1009,7 @@ end /-! -## C. The Schouten identity - --/ - -public section - -namespace Tensors.Species - -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} - [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} - (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] - [CharZero k] - -/-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`, -`g_γρ δ^α_α' = g_γα' δ_ρ^α - g_ρα' δ_γ^α`. -/ -lemma schouten_formTensor_deltaTensor (h2 : Fintype.card (basisIdx c.kind) = 2) : - {S.formTensor c | γ ρ ⊗ S.deltaTensor c | α α' = - S.formTensor c | γ α' ⊗ S.deltaTensor c.dual | ρ α + - -{S.formTensor c | ρ α' ⊗ S.deltaTensor c.dual | γ α}ᵀ | ρ α' γ α}ᵀ := by - tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] - rw [← sub_eq_add_neg] - exact formMatrix_schouten c h2 _ _ _ _ - -/-- The Schouten identity with the delta read the other way, -`g_γρ δ_α^α' = g_γα δ_ρ^α' - g_ρα δ_γ^α'`. -/ -lemma schouten_formTensor_deltaTensor_dual (h2 : Fintype.card (basisIdx c.kind) = 2) : - {S.formTensor c | γ ρ ⊗ S.deltaTensor c.dual | α α' = - S.formTensor c | γ α ⊗ S.deltaTensor c.dual | ρ α' + - -{S.formTensor c | ρ α ⊗ S.deltaTensor c.dual | γ α'}ᵀ | ρ α γ α'}ᵀ := by - tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] - rw [← sub_eq_add_neg] - exact formMatrix_schouten' c h2 _ _ _ _ - -end Tensors.Species - -end - -/-! - -## D. Examples +## C. Examples -/ diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index ac612a1aa2..9e4916304c 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SesquilinearForm public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Contraction diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean new file mode 100644 index 0000000000..96bce2de29 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -0,0 +1,73 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Elab + +/-! + +# The Schouten identity in index notation + +The Schouten identity of an antisymmetric form on a kind with two labels, as an equation of +tensors. + +## i. Overview + +For an antisymmetric form `g` on a kind with two labels, such as the Weyl `ε`, the Schouten +identity `g_γρ δ^α_α' = g_γα' δ_ρ^α - g_ρα' δ_γ^α` lets one exchange an index of the form with +an index of a delta. It is `Species.formMatrix_schouten` read in components, here stated in index +notation for both orientations of the delta. + +## ii. Key results + +- `Species.schouten_formTensor_deltaTensor` and `Species.schouten_formTensor_deltaTensor_dual` : + the Schouten identity of an antisymmetric form on a kind with two labels, for both orientations + of the delta. + +## iii. Table of contents + +- A. The Schouten identity + +## iv. References + +-/ + +@[expose] public section + +/-! + +## A. The Schouten identity + +-/ + +namespace Tensors.Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] + [CharZero k] + +/-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`, +`g_γρ δ^α_α' = g_γα' δ_ρ^α - g_ρα' δ_γ^α`. -/ +lemma schouten_formTensor_deltaTensor (h2 : Fintype.card (basisIdx c.kind) = 2) : + {S.formTensor c | γ ρ ⊗ S.deltaTensor c | α α' = + S.formTensor c | γ α' ⊗ S.deltaTensor c.dual | ρ α + + -{S.formTensor c | ρ α' ⊗ S.deltaTensor c.dual | γ α}ᵀ | ρ α' γ α}ᵀ := by + tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] + rw [← sub_eq_add_neg] + exact formMatrix_schouten c h2 _ _ _ _ + +/-- The Schouten identity with the delta read the other way, +`g_γρ δ_α^α' = g_γα δ_ρ^α' - g_ρα δ_γ^α'`. -/ +lemma schouten_formTensor_deltaTensor_dual (h2 : Fintype.card (basisIdx c.kind) = 2) : + {S.formTensor c | γ ρ ⊗ S.deltaTensor c.dual | α α' = + S.formTensor c | γ α ⊗ S.deltaTensor c.dual | ρ α' + + -{S.formTensor c | ρ α ⊗ S.deltaTensor c.dual | γ α'}ᵀ | ρ α γ α'}ᵀ := by + tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] + rw [← sub_eq_add_neg] + exact formMatrix_schouten' c h2 _ _ _ _ + +end Tensors.Species diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index f166864d3e..5b34c4bb54 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.Schouten public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Relativity.Fermions.Weyl.Metric public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre From 22f9c7ebb60bf094e19bbc187666563ee1537181 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:56:33 +0000 Subject: [PATCH 072/242] refactor(Pauli): pauliContrDown_trace_pauliCo and pauliContr_trace_pauliContrDown by plain tensor_regroup; pauliContrDown_eq_pauliCoDown_contr and pauliTensor_eq_pauliCo_contr removed Co-authored-by: Claude Opus 5.5 --- Physlib/Relativity/Bispinors/Pauli/Tensor.lean | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index f86860653a..5b92f05825 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -134,11 +134,6 @@ lemma tensorBasis_repr_pauliTensor_downAll exact (sum_metricRaw_mul_sum_metricRaw_mul _ _ _).trans (congrFun₂ (PauliMatrix.adjugate_minkowskiMatrix_smul_pauliMatrix _) _ _) -/-- `σT^μ_{αβ̇} = σT_{ραβ̇} η^ρμ`, raising the vector index of the fully lowered Pauli tensor. -/ -lemma pauliContrDown_eq_pauliCoDown_contr : - {pauliTensor | μ ↓α ↓β = {pauliTensor | ↓ρ ↓α ↓β ⊗ η | ρ μ}ᵀ | α β μ}ᵀ := by - tensor_regroup - /-! ## C. Contraction identities @@ -175,7 +170,6 @@ lemma pauliContrDown_trace_pauliCo : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • δ' | ν μ}ᵀ := by have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = {pauliTensor | ↓ρ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | ρ ν ⊗ η | ρ μ}ᵀ := by - rw [pauliContrDown_eq_pauliCoDown_contr] tensor_regroup rw [h, pauliCoDown_trace_pauliCo] tensor_regroup @@ -235,16 +229,11 @@ lemma pauliCo_contr_pauliContr : -/ -/-- `σT^{μαβ̇} = σT_ρ^{αβ̇} η^{ρμ}`, raising the lowered vector index again. -/ -lemma pauliTensor_eq_pauliCo_contr : {pauliTensor | μ α β = pauliTensor | ↓ρ α β ⊗ η | ρ μ}ᵀ := by - tensor_regroup - /-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{αβ̇} = 2 η^μν`. -/ lemma pauliContr_trace_pauliContrDown : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = (2 : ℂ) • η | μ ν}ᵀ := by have h : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = {pauliTensor | ν ↓α ↓β ⊗ pauliTensor | ↓ρ α β}ᵀ | ν ρ ⊗ η | ρ μ}ᵀ := by - nth_rw 1 [pauliTensor_eq_pauliCo_contr] tensor_regroup rw [h, pauliContrDown_trace_pauliCo] tensor_regroup From b04dd33267aba159aed96db0451ea713f1752f68 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:50:57 +0000 Subject: [PATCH 073/242] feat(Elab): tensor_regroup cancels several pairs of inverse forms in matchFormPair Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 1fef43ffd8..ea1d948015 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -747,25 +747,31 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) : TacticM Unit : insertDelta lhs a slots slot regroupContractions -/-- Matches two forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors `ds` -left over on the left side, if `lhs`, or on the right side. A delta `ds = [δ]` is written as the -contraction of the forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying the -color of each form. With `ds = []`, a delta is inserted first. -/ +/-- Matches pairs of forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors +`ds` left over on the left side, if `lhs`, or on the right side. A delta `ds = [δ]` is written as +the contraction of two forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying +the color of each form whose dual form is also left over. With `ds = []`, a delta is inserted +first. Each pair cancelled this way, as a lowering `g_μν` against a raising form `g^νρ`, leaves the +other pairs to the next round of `regroupContractions`. -/ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Unit := do let fs := gs.filterMap formArgs? - unless gs.length == 2 && fs.length == 2 do - throwError "tensor_regroup: the factors left over are not two forms" + unless 2 ≤ gs.length && gs.length % 2 == 0 && fs.length == gs.length do + throwError "tensor_regroup: the factors left over are not pairs of forms" + -- Only the colors whose dual form is also left over can cancel, each tried once. + let fs ← fs.eraseDups.filterM fun (_, c) => do + let cd ← mkAppM ``Tensors.Color.dual #[c] + fs.anyM fun (_, c') => isDefEqGoal c' cd match ds with | [d] => unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" - firstSuccess fs m!"" fun (S, c) => do + firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (S, c) => do let thm ← `((Tensors.Species.contr_formTensor_formTensor (S := $(← exprToSyntax S)) $(← exprToSyntax c)).symm) rewriteSubterm d thm (some lhs) regroupContractions - | [] => firstSuccess fs m!"" fun (_, c) => do + | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) - | _ => throwError "tensor_regroup: more than one factor is left over against two forms" + | _ => throwError "tensor_regroup: more than one factor is left over against forms" /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested From cad0fa165f15a9efe98476efac9addb8c40e35fe Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 16:58:45 +0000 Subject: [PATCH 074/242] feat(Elab): tensor_regroup cancels lowerings against inverse forms; pauliCo_contr_pauliContr without the round trip Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 88 +++++++++++++++++-- .../Relativity/Bispinors/Pauli/Tensor.lean | 1 - 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index ea1d948015..bd9b496645 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -736,12 +736,23 @@ private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : T mutual /-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, at each slot of -the product of color `a` in turn, and proves the goal by `regroupContractions`. -/ -private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) : TacticM Unit := do +the product of color `a` in turn, and proves the goal by `regroupContractions`. With `skipForms`, +the slots of forms are left out when another factor has a slot: inserting the delta anywhere along a +chain of contracted forms gives the same factors, so trying each of their slots only repeats the +search. -/ +private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Bool := false) : + TacticM Unit := do let (p, q) ← contractedProducts - let some (slots, _, colors) ← literalTensorType? (← inferType (if lhs then p else q)) + let P := if lhs then p else q + let some (slots, _, colors) ← literalTensorType? (← inferType P) | throwError "tensor_regroup: the slots or colors of a product are not literal" - let slotsOfColor ← colors.zipIdx.filterM fun (col, _) => isDefEqGoal col a + -- Whether each slot belongs to a form, from the number of slots of each factor in turn. + let inForm ← (factors P).flatMapM fun f => do + let n := ((← literalTensorType? (← inferType f)).map (·.2.2.length)).getD 0 + return List.replicate n (skipForms && (formArgs? f).isSome) + let inForm := if inForm.all id then inForm.map fun _ => false else inForm + let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => + return !inForm.getD i false && (← isDefEqGoal col a) firstSuccess (slotsOfColor.map (·.2)) m!"tensor_regroup: no slot has the color {a} of a delta" fun slot => do insertDelta lhs a slots slot @@ -770,7 +781,7 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni rewriteSubterm d thm (some lhs) regroupContractions | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do - insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) + insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) (skipForms := true) | _ => throwError "tensor_regroup: more than one factor is left over against forms" /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to @@ -784,13 +795,18 @@ private partial def regroupContractions : TacticM Unit := do let (p, q) ← contractedProducts let extraL ← leftover (factors p) (factors q) let extraR ← leftover (factors q) (factors p) + -- The error reported in place of the count of factors when forms are left over on one side. + let mut formErr : Option MessageData := none for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do - try matchFormPair lhs ds gs; return catch _ => pure () + try matchFormPair lhs ds gs; return catch _ => + if 2 ≤ gs.length && gs.all (formArgs? · |>.isSome) then + formErr := m!"tensor_regroup: the forms left over on one side do not cancel in inverse pairs \ + against the other side{indentD (MessageData.joinSep (gs.map toMessageData) "\n")}" -- A delta left over on the right is inserted on the left, and conversely. match extraL.mapM deltaColor?, extraR.mapM deltaColor? with | some _, some (a :: _) => insertDeltaAnywhere true a | some (a :: _), some [] => insertDeltaAnywhere false a - | _, _ => matchFactors + | _, _ => try matchFactors catch e => throwError (formErr.getD e.toMessageData) end @@ -954,8 +970,9 @@ Beyond regrouping, it uses these identities: - a delta on one side with no counterpart on the other is inserted on the other side at a slot of its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` - (`Species.contr_formTensor_formTensor`), and two such forms against nothing are matched by - inserting a delta first; + (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering + `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta + first; - when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, `η^μν = η^νμ` (`Species.formTensor_symm`); @@ -1197,6 +1214,59 @@ example : {v | ↓μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν μ}ᵀ := by example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ}ᵀ := by tensor_regroup +/-- A lowering cancels against the inverse form, `M^μ_ν g^νρ = M^μρ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + tensor_regroup + +/-- Two lowerings cancel against two inverse forms, one pair at a time, +`M_μν g^μρ g^νσ = M^ρσ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | ρ σ}ᵀ := by + tensor_regroup + +/-- The same with the pairs on the right side and on two kinds, `N^ρσ = N_μν g^μρ g'^νσ`. -/ +example (κ' : K) [S.HasForm κ'] (N : S.Tensor ![⟨κ, .up⟩, ⟨κ', .up⟩]) : + {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ', .up⟩ | ν σ}ᵀ := by + tensor_regroup + +/-- A lowering contracted with the second index of a symmetric form, `M^μ_ν g^ρν = M^μρ`. -/ +example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + tensor_regroup + +/-- With an antisymmetric form the first index cancels with no sign, `M^μ_ν ε^νρ = M^μρ`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + tensor_regroup + +/-- With an antisymmetric form the second index cancels with a sign, `M^μ_ν ε^ρν = -M^μρ`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = -M | μ ρ}ᵀ := by + tensor_regroup + +-- The sign of an antisymmetric form is not dropped, `M^μ_ν ε^ρν ≠ M^μρ`. +/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against +the other side + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up } -/ +#guard_msgs in +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + tensor_regroup + +-- Cancelling the pairs does not swap the free indices, `M_μν g^μρ g^νσ ≠ M^σρ`. +/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against +the other side + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up } + S.formTensor { kind := κ, variance := Variance.up } -/ +#guard_msgs in +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | σ ρ}ᵀ := by + tensor_regroup + /-- Summands of opposite signs are matched by swapping an antisymmetric form, `M^μν g_νρ = -(g_ρν M^μν)`. -/ example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 5b92f05825..847fa742d0 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -219,7 +219,6 @@ lemma pauliCo_contr_pauliContr : (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by rw [map_smul, ← pauliA_pauliB] refine congrArg pauliA ?_ - rw [← pauliTensor_raise_lower] simp only [pauliB, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup From 7b09790d3cb23ebcba1bb992d009908a29e9c202 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:08:50 +0000 Subject: [PATCH 075/242] doc(Elab): note the limit of skipping form slots in insertDeltaAnywhere Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index bd9b496645..47c1b08034 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -750,6 +750,8 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Boo let inForm ← (factors P).flatMapM fun f => do let n := ((← literalTensorType? (← inferType f)).map (·.2.2.length)).getD 0 return List.replicate n (skipForms && (formArgs? f).isSome) + -- shortcut: misses a chain of forms that reaches no other factor, as `T^μ_ν g^αβ` against + -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the slots of forms after the others would cover it. let inForm := if inForm.all id then inForm.map fun _ => false else inForm let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => return !inForm.getD i false && (← isDefEqGoal col a) From c4692c656ebd278f09a12cf554563b9ec850fa74 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:08:22 +0000 Subject: [PATCH 076/242] =?UTF-8?q?feat(Elab):=20tensor=5Fregroup=20using?= =?UTF-8?q?=20e=E2=82=81,=20=E2=80=A6,=20e=E2=82=99;=20Pauli=20trace=20and?= =?UTF-8?q?=20eps=20lemmas=20rewritten=20with=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spike: tensor_regroup using rewrites a sub-product of the left side by each identity before regrouping. flattenContractions and regroupContractions now stop when simp closes the goal. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 276 ++++++++++++++++-- .../Relativity/Bispinors/Pauli/Tensor.lean | 34 +-- 2 files changed, 254 insertions(+), 56 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 47c1b08034..e1e638aad6 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -65,7 +65,8 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - `tensor_components` : reduces an equation of tensors to an equation of their components. - `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, - swaps of symmetric and antisymmetric forms, and lowerings written as contractions. + swaps of symmetric and antisymmetric forms, and lowerings written as contractions. With + `using e₁, …`, it first rewrites a sub-product of the left side by each identity `eᵢ`. - `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -81,6 +82,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.7. Deltas and forms - B.8. Sums, signs and scalars - B.9. Chains of lowerings and raisings + - B.10. Rewriting a sub-product by an identity - C. Examples ## iv. References @@ -624,6 +626,8 @@ private def flattenContractions : TacticM Unit := do evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, Tensors.Species.tmulEquiv_reindex_right, Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex, Tensors.Species.Tensor.reindex_contr])) + -- The two sides may become equal, and `simp` then closes the goal. + if (← getGoals).isEmpty then return if (← instantiateMVars (← getMainTarget)) == before then return throwError "tensor_regroup: flattening the products did not stop after ten rounds" @@ -794,6 +798,7 @@ at each slot of its color in turn, since contracting with a delta is a relabelin then matched by `matchFactors`. -/ private partial def regroupContractions : TacticM Unit := do flattenContractions + if (← getGoals).isEmpty then return let (p, q) ← contractedProducts let extraL ← leftover (factors p) (factors q) let extraR ← leftover (factors q) (factors p) @@ -958,33 +963,9 @@ private def regroup : TacticM Unit := do setGoals [rest.mvarId!] evalTactic (← `(tactic| first | with_reducible rfl | abel)) -/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of -the same factors, grouped or ordered differently, such as -`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, -negatives and scalars are first moved out of every product, contraction and relabeling. Each -summand on the left is then matched with a summand on the right with the same sign and scalar, -whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a -factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 -are tried. - -Beyond regrouping, it uses these identities: -- a delta on one side with no counterpart on the other is inserted on the other side at a slot of - its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on - the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` - (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering - `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta - first; -- when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` - (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, - `η^μν = η^νμ` (`Species.formTensor_symm`); -- when no matching is found, lowerings and raisings are expanded into contractions with the form - (`Species.toDualAt_eq_contr`) and the matching is tried again. - -A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs -(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional -equality, as `2` and `2`. -/ -elab "tensor_regroup" : tactic => do +/-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as +contractions with the form (`Species.toDualAt_eq_contr`) when it fails. -/ +private def regroupWithLowerings : TacticM Unit := do let s ← saveState try regroup catch e => -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They @@ -1028,6 +1009,216 @@ elab "tensor_slots" : tactic => do Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual])) +/-! + +### B.10. Rewriting a sub-product by an identity + +-/ + +/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of +`P` are numbered in order, the slots of each factor after those of the factors before it. -/ +private structure ReadContraction where + /-- The factors of `P`, from left to right. -/ + factors : List Expr + /-- The slot of `P` at which each factor starts. -/ + offsets : List Nat + /-- The slot of `P` of each free index, in order. -/ + free : List Nat + /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ + pairs : List (Nat × Nat) + +/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in +slot order. -/ +private partial def slotElems (ι : Expr) : MetaM (List Expr) := do + let ι ← whnfR ι + if ι.isAppOfArity ``Fin 1 then + let some n ← (evalNat ι.appArg!).run + | throwError "tensor_regroup: the slot type {ι} is not literal" + (List.range n).mapM fun i => do + mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] + else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] + else if ι.isConstOf ``Empty then return [] + else if ι.isAppOfArity ``Sum 2 then + let (a, b) := (ι.getArg! 0, ι.getArg! 1) + return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ + (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) + else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" + +/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ +private partial def slotNumber : Slots → Expr → MetaM Nat + | .fin _, x => do + let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run + | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + return i + | .unit, _ => return 0 + | .sum a b, x => do + let x ← whnfD x + if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! + else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) + else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + +/-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each +free index and at each slot of each pair. -/ +private def readContraction (e : Expr) : MetaM ReadContraction := do + let some P := bundledArg? ``Tensors.Species.Tensor.contr e + | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" + let τ := e.consumeMData.appFn!.appArg!.appArg! + let some (slots, _, _) ← literalTensorType? (← inferType P) + | throwError "tensor_regroup: the slots or colors of a product are not literal" + let fs := factors P + let sizes ← fs.mapM fun f => do + let some (s, _, _) ← literalTensorType? (← inferType f) + | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" + return s.size + -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. + let args := (← whnfR (← inferType τ)).getAppArgs + let cargs := args[args.size - 1]!.getAppArgs + let (κ, J) := (cargs[1]!, cargs[2]!) + let JJ ← mkAppM ``Sum #[J, J] + let at_ (y : Expr) : MetaM Nat := do + slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) + let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) + let pairs ← (← slotElems J).mapM fun j => do + let half (left : Bool) := do + at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) + #[J, J, j]]) + return (← half true, ← half false) + return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ + +/-- The ways of sending the slots of `A` to slots of `G`, factor by factor, each factor of `A` to an +equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list of +images of the slots of `A`, together with the factors of `G` that are hit. -/ +private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List Nat)) := do + let candidates ← A.factors.mapM fun x => + (List.range G.factors.length).filterM fun j => isDefEqGoal x G.factors[j]! + let isPair (p q : Nat) := G.pairs.contains (p, q) || G.pairs.contains (q, p) + return (matchings candidates []).filterMap fun target => + let φ := (target.zipIdx).flatMap fun (j, i) => + (List.range (A.offsets[i+1]! - A.offsets[i]!)).map (G.offsets[j]! + ·) + if A.pairs.all fun (p, q) => isPair φ[p]! φ[q]! then some (φ, target) else none + +/-- The function `x ↦ {x | … ⊗ g | … ⊗ …}ᵀ`, for `x` of the type of `A`, with its free indices +in the order of `L`, the contraction read as `G`. The factors `g` of `G` not hit by `target` follow +`x`, and every slot of `G` is named after its free index `fₖ` or its pair `pⱼ`. The slots of `x` +are named as the free indices of `A` are sent by `φ`. -/ +private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : List Nat) : + TermElabM Expr := do + let name (s : Nat) : TSyntax `tensorIndex := Unhygienic.run do + let n := match G.free.idxOf? s with + | some k => s!"f{k}" + | none => s!"p{(G.pairs.findIdx? fun (p, q) => p == s || q == s).getD 0}" + `(tensorIndex| $(mkIdent (Name.mkSimple n)):ident) + let leaf (T : Expr) (ss : List Nat) : TermElabM (TSyntax `tensorIndexExpr) := do + `(tensorIndexExpr| $(← exprToSyntax T):term | $((ss.map name).toArray)*) + let rest ← ((List.range G.factors.length).filter (!target.contains ·)).mapM fun j => + leaf G.factors[j]! ((List.range (G.offsets[j+1]! - G.offsets[j]!)).map (G.offsets[j]! + ·)) + withLocalDeclD `x (← inferType A) fun x => do + let rhs ← rest.foldlM (init := ← leaf x (Aread.free.map (φ[·]!))) fun a b => + `(tensorIndexExpr| $a ⊗ $b) + -- The equation `{L | f₀ f₁ … = …}ᵀ` relabels its right side to the order of `L`. + let eq ← Term.elabType (← `({ $(← leaf L G.free) = $rhs }ᵀ)) + Term.synthesizeSyntheticMVarsNoPostponing + let some (_, _, M) := (← instantiateMVars eq).eq? + | throwError "tensor_regroup: internal error, not an equation" + mkLambdaFVars #[x] M + +/-- Proves the goal `ty` by `tac`, aside from the main goal. -/ +private def proveAside (ty : Expr) (tac : TacticM Unit) : TacticM Expr := do + let g ← mkFreshExprMVar ty + let gs ← getGoals + setGoals [g.mvarId!] + try + tac + unless (← getGoals).isEmpty do throwError "tensor_regroup: internal error, a goal is left" + finally setGoals gs + instantiateMVars g + +/-- The side `A`, flattened to one contraction of a left-nested product, with lowerings and +raisings first written as contractions with the form if `expand`. -/ +private def flattenTerm (A : Expr) (expand : Bool) : TacticM Expr := do + let g ← mkFreshExprMVar (← mkEq A (← mkAppOptM ``Zero.zero #[← inferType A, none])) + let gs ← getGoals + setGoals [g.mvarId!] + try + if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + flattenContractions + return (← goalEq).2.1 + finally setGoals gs + +/-- The body of `tensor_regroup using e₁, …, eₙ`. For `e₁ : A = B`, the left side of the goal is +regrouped by `regroupWithLowerings` into a product `M A` of `A` with the factors not in `A`, which +`congrArg M e₁` rewrites to `M B`, and the remaining identities are used in the same way. Each way +of finding the factors of `A` among the factors of the goal is tried in turn, first as written and +then with lowerings and raisings written as contractions with the form. -/ +private partial def regroupUsing : List Term → TacticM Unit + | [] => regroupWithLowerings + | e :: es => do + let h ← instantiateMVars (← Tactic.elabTerm e none) + let some (_, A, B) := (← instantiateMVars (← inferType h)).eq? + | throwError "tensor_regroup: {e} is not an equation" + let mut err := m!"tensor_regroup: no factors of the left side are contracted as in the left \ + side of {e}" + for expand in [false, true] do + let s ← saveState + if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + flattenContractions + let (_, L, _) ← goalEq + let G ← readContraction L + let Aread ← readContraction (← flattenTerm A expand) + for (φ, target) in (← embeddings Aread G).take maxMatchings do + let s' ← saveState + try + -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. + let M ← regroupedFun L A Aread G φ target + let h₁ ← proveAside (← mkEq L (M.beta #[A])) regroupWithLowerings + let pf ← mkEqTrans h₁ (← mkCongrArg M h) + let (_, _, R) ← goalEq + let rest ← mkFreshExprSyntheticOpaqueMVar (← mkEq (M.beta #[B]) R) + (← getMainGoal).assign (← mkEqTrans pf rest) + replaceMainGoal [rest.mvarId!] + regroupUsing es + return + catch ex => s'.restore; err := ex.toMessageData + s.restore + throwError err + +/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of +the same factors, grouped or ordered differently, such as +`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, +negatives and scalars are first moved out of every product, contraction and relabeling. Each +summand on the left is then matched with a summand on the right with the same sign and scalar, +whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a +factor appears more than once, each way of matching the equal factors is tried in turn, so the +number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 +are tried. + +Beyond regrouping, it uses these identities: +- a delta on one side with no counterpart on the other is inserted on the other side at a slot of + its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on + the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` + (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering + `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta + first; +- when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` + (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, + `η^μν = η^νμ` (`Species.formTensor_symm`); +- when no matching is found, lowerings and raisings are expanded into contractions with the form + (`Species.toDualAt_eq_contr`) and the matching is tried again. + +A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs +(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional +equality, as `2` and `2`. + +`tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, +the factors of `Aᵢ` are found among the factors of the left side of the goal, contracted along the +same pairs, and the left side is regrouped as `Aᵢ` times the other factors and rewritten to `Bᵢ`. +The equations must be closed terms. -/ +syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic + +elab_rules : tactic + | `(tactic| tensor_regroup) => regroupWithLowerings + | `(tactic| tensor_regroup using $es,*) => regroupUsing es.getElems.toList + end Tensors.Notation end @@ -1204,6 +1395,35 @@ has no matching summand on the right -/ example : {T | μ ν + W | μ ν = U | μ ν + T | μ ν}ᵀ := by tensor_regroup +/-- `tensor_regroup using h` finds `T^μ_ν U^ν_ρ` among the factors, rewrites it by `h` and +regroups, `U^ν_ρ W^ρ_σ T^μ_ν = W^μ_ρ W^ρ_σ`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : + {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = W | μ ρ ⊗ W | ρ σ}ᵀ := by + tensor_regroup using h + +/-- Two identities in turn, `U^ν_ρ W^ρ_σ T^μ_ν = (T U) W = W W = T`. -/ +example (h₁ : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) (h₂ : {W | μ ν ⊗ W | ν ρ = T | μ ρ}ᵀ) : + {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = T | μ σ}ᵀ := by + tensor_regroup using h₁, h₂ + +/-- The free indices of the identity may be contracted with each other, `T^μ_ν U^ν_μ = W^μ_μ`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν μ ⊗ T | μ ν = W | μ μ}ᵀ := by + tensor_regroup using h + +-- The factors `T` and `U` are contracted as `U^ν_ρ T^ρ_μ`, not as `T^μ_ν U^ν_ρ`. +/-- error: tensor_regroup: no factors of the left side are contracted as in the left side of h -/ +#guard_msgs in +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν ρ ⊗ T | ρ μ = W | ν μ}ᵀ := by + tensor_regroup using h + +-- After the rewrite the two sides still differ, `W ≠ U`. +/-- error: tensor_regroup: the factor + W +has no equal factor on the right -/ +#guard_msgs in +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {T | μ ν ⊗ U | ν ρ = U | μ ρ}ᵀ := by + tensor_regroup using h + section Metric variable [S.HasForm κ] [S.HasForm c.kind] diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 847fa742d0..ed809d39c1 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -159,11 +159,7 @@ lemma pauliCoDown_trace_pauliCo : /-- The trace identity `tr(σ_μ σ̄_ν) = 2 η_μν`, as `σT_μ^{αβ̇} σT_{ναβ̇} = 2 η_μν`. -/ lemma pauliCo_trace_pauliCoDown : {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = (2 : ℂ) • η' | μ ν}ᵀ := by - have h : {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = - {pauliTensor | ↓ν ↓α ↓β ⊗ pauliTensor | ↓μ α β}ᵀ | ν μ}ᵀ := by - tensor_regroup - rw [h, pauliCoDown_trace_pauliCo] - tensor_regroup + tensor_regroup using pauliCoDown_trace_pauliCo /-- The mixed trace identity `tr(σ̄^μ σ_ν) = 2 δ^μ_ν`, as `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ^μ_ν`. -/ lemma pauliContrDown_trace_pauliCo : @@ -195,12 +191,8 @@ noncomputable def pauliB : ℂT[upL, upR, upL, upR] →ₗ[ℂ] ℂT[up, upL, up /-- `pauliB ∘ pauliA = 2`, by the trace identity `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ_ν^μ`. -/ lemma pauliB_pauliA (p : ℂT[up, upL, upR]) : pauliB (pauliA p) = (2 : ℂ) • p := by - have h : pauliB (pauliA p) = - {{pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | μ ν ⊗ p | ν α' β'}ᵀ := by - simp only [pauliA, pauliB, LinearMap.coe_mk, AddHom.coe_mk] - tensor_regroup - rw [h, pauliContrDown_trace_pauliCo] - tensor_regroup + simp only [pauliA, pauliB, LinearMap.coe_mk, AddHom.coe_mk] + tensor_regroup using pauliContrDown_trace_pauliCo /-- `pauliA ∘ pauliB = 2`: both spaces have dimension `16`, so the left inverse `pauliB / 2` of `pauliA` is a right inverse. -/ @@ -240,23 +232,14 @@ lemma pauliContr_trace_pauliContrDown : /-- The trace identity `tr(σ̄^μ σ^ν) = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇} = 2 η^μν`. -/ lemma pauliContrDown_trace_pauliContr : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = (2 : ℂ) • η | μ ν}ᵀ := by - have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = - {pauliTensor | ν α β ⊗ pauliTensor | μ ↓α ↓β}ᵀ | ν μ}ᵀ := by - tensor_regroup - rw [h, pauliContr_trace_pauliContrDown] - tensor_regroup + tensor_regroup using pauliContr_trace_pauliContrDown /-- `σT^{μρβ̇} σT^{νγδ̇} ε_{δ̇β̇} (ε_{γρ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity `pauliContr_trace_pauliContrDown` with the lowerings written as contractions with `ε`. -/ lemma pauliContr_pauliContr_eps_epsL_deltaL : {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by - have h : {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ - {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = - {pauliTensor | μ r β ⊗ pauliTensor | ν ↓r ↓β}ᵀ | μ ν ⊗ δL | α α'}ᵀ := by - tensor_regroup - rw [h, pauliContr_trace_pauliContrDown] - tensor_regroup + tensor_regroup using pauliContr_trace_pauliContrDown /-- The anticommutator `σ^μ σ̄^ν + σ^ν σ̄^μ = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{α'β̇} + σT^{ναβ̇} σT^μ_{α'β̇} = 2 η^μν δ^α_α'`. The Schouten identity splits @@ -272,12 +255,7 @@ lemma pauliContr_mul_pauliContrDown_add : lemma pauliContr_pauliContr_eps_epsR_deltaR : {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by - have h : {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ - {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = - {pauliTensor | μ ↓a ↓r ⊗ pauliTensor | ν a r}ᵀ | μ ν ⊗ δR' | β β'}ᵀ := by - tensor_regroup - rw [h, pauliContrDown_trace_pauliContr] - tensor_regroup + tensor_regroup using pauliContrDown_trace_pauliContr /-- The anticommutator `σ̄^μ σ^ν + σ̄^ν σ^μ = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇'} + σT^ν_{αβ̇} σT^{μαβ̇'} = 2 η^μν δ_β̇^β̇'`. The Schouten identity splits From 43e6dcd57c15f395c0e7d136b488a30aca4eb2c9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:26:34 +0000 Subject: [PATCH 077/242] feat(Elab): tensor_regroup using searches the right side of the goal when the left has no match Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 71 ++++++++++++++++----------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index e1e638aad6..8a28e3bf94 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -66,7 +66,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, swaps of symmetric and antisymmetric forms, and lowerings written as contractions. With - `using e₁, …`, it first rewrites a sub-product of the left side by each identity `eᵢ`. + `using e₁, …`, it first rewrites a sub-product of either side by each identity `eᵢ`. - `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -1149,37 +1149,45 @@ private def flattenTerm (A : Expr) (expand : Bool) : TacticM Expr := do regrouped by `regroupWithLowerings` into a product `M A` of `A` with the factors not in `A`, which `congrArg M e₁` rewrites to `M B`, and the remaining identities are used in the same way. Each way of finding the factors of `A` among the factors of the goal is tried in turn, first as written and -then with lowerings and raisings written as contractions with the form. -/ +then with lowerings and raisings written as contractions with the form, first on the left side of +the goal and then on the right. -/ private partial def regroupUsing : List Term → TacticM Unit | [] => regroupWithLowerings | e :: es => do let h ← instantiateMVars (← Tactic.elabTerm e none) let some (_, A, B) := (← instantiateMVars (← inferType h)).eq? | throwError "tensor_regroup: {e} is not an equation" - let mut err := m!"tensor_regroup: no factors of the left side are contracted as in the left \ + let mut err := m!"tensor_regroup: no factors of either side are contracted as in the left \ side of {e}" - for expand in [false, true] do - let s ← saveState - if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) - flattenContractions - let (_, L, _) ← goalEq - let G ← readContraction L - let Aread ← readContraction (← flattenTerm A expand) - for (φ, target) in (← embeddings Aread G).take maxMatchings do - let s' ← saveState - try - -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. - let M ← regroupedFun L A Aread G φ target - let h₁ ← proveAside (← mkEq L (M.beta #[A])) regroupWithLowerings - let pf ← mkEqTrans h₁ (← mkCongrArg M h) - let (_, _, R) ← goalEq - let rest ← mkFreshExprSyntheticOpaqueMVar (← mkEq (M.beta #[B]) R) - (← getMainGoal).assign (← mkEqTrans pf rest) - replaceMainGoal [rest.mvarId!] - regroupUsing es - return - catch ex => s'.restore; err := ex.toMessageData - s.restore + -- The right side is searched by first turning the goal around. + for flip in [false, true] do + for expand in [false, true] do + let s ← saveState + -- A side that is not a contraction has no factors to search. + let read? ← try + if flip then replaceMainGoal [← (← getMainGoal).applySymm] + if expand then + evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + flattenContractions + let (_, L, _) ← goalEq + pure (some (L, ← readContraction L, ← readContraction (← flattenTerm A expand))) + catch _ => pure none + if let some (L, G, Aread) := read? then + for (φ, target) in (← embeddings Aread G).take maxMatchings do + let s' ← saveState + try + -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. + let M ← regroupedFun L A Aread G φ target + let h₁ ← proveAside (← mkEq L (M.beta #[A])) regroupWithLowerings + let pf ← mkEqTrans h₁ (← mkCongrArg M h) + let (_, _, R) ← goalEq + let rest ← mkFreshExprSyntheticOpaqueMVar (← mkEq (M.beta #[B]) R) + (← getMainGoal).assign (← mkEqTrans pf rest) + replaceMainGoal [rest.mvarId!] + regroupUsing es + return + catch ex => s'.restore; err := ex.toMessageData + s.restore throwError err /-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of @@ -1210,9 +1218,9 @@ A side that is not a contraction, such as a single tensor, is read as a contract equality, as `2` and `2`. `tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, -the factors of `Aᵢ` are found among the factors of the left side of the goal, contracted along the -same pairs, and the left side is regrouped as `Aᵢ` times the other factors and rewritten to `Bᵢ`. -The equations must be closed terms. -/ +the factors of `Aᵢ` are found among the factors of the left side of the goal, or else of the right +side, contracted along the same pairs, and that side is regrouped as `Aᵢ` times the other factors +and rewritten to `Bᵢ`. The equations must be closed terms. -/ syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic elab_rules : tactic @@ -1401,6 +1409,11 @@ example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = W | μ ρ ⊗ W | ρ σ}ᵀ := by tensor_regroup using h +/-- The identity is found on the right side, `W^μ_ρ W^ρ_σ = U^ν_ρ W^ρ_σ T^μ_ν`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : + {W | μ ρ ⊗ W | ρ σ = U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν}ᵀ := by + tensor_regroup using h + /-- Two identities in turn, `U^ν_ρ W^ρ_σ T^μ_ν = (T U) W = W W = T`. -/ example (h₁ : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) (h₂ : {W | μ ν ⊗ W | ν ρ = T | μ ρ}ᵀ) : {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = T | μ σ}ᵀ := by @@ -1411,7 +1424,7 @@ example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν μ ⊗ T | μ tensor_regroup using h -- The factors `T` and `U` are contracted as `U^ν_ρ T^ρ_μ`, not as `T^μ_ν U^ν_ρ`. -/-- error: tensor_regroup: no factors of the left side are contracted as in the left side of h -/ +/-- error: tensor_regroup: no factors of either side are contracted as in the left side of h -/ #guard_msgs in example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν ρ ⊗ T | ρ μ = W | ν μ}ᵀ := by tensor_regroup using h From f6a84856911b77360768b6163a248b181b580749 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:26:34 +0000 Subject: [PATCH 078/242] perf(Elab): tensor_regroup expands lowerings only when the goal has one; clearer error for false goals Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 31 +++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 8a28e3bf94..24aa55d683 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -963,18 +963,27 @@ private def regroup : TacticM Unit := do setGoals [rest.mvarId!] evalTactic (← `(tactic| first | with_reducible rfl | abel)) +/-- Whether `e` contains a lowering or raising `S.toDualAt`. -/ +private def hasLowering (e : Expr) : Bool := + (e.find? (·.isConstOf ``Tensors.Species.toDualAt)).isSome + /-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as -contractions with the form (`Species.toDualAt_eq_contr`) when it fails. -/ +contractions with the form (`Species.toDualAt_eq_contr`) when it fails and the goal has one. When +both fail, the first error is reported if it is about the summands or scalars, and the second +otherwise. -/ private def regroupWithLowerings : TacticM Unit := do + unless hasLowering (← instantiateMVars (← getMainTarget)) do regroup; return let s ← saveState try regroup catch e => -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They -- are expanded only when needed, since every expanded form is one more factor to match. s.restore - let before ← instantiateMVars (← getMainTarget) - evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) - if (← instantiateMVars (← getMainTarget)) == before then throw e - regroup + evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_contr])) + try regroup catch e' => + -- The expansion changes the factors, not the summands or scalars, so an error about those + -- reads better before it. + let msg ← e.toMessageData.toString + throw <| if msg.contains "summand" || msg.contains "scalars" then e else e' /-! @@ -1159,9 +1168,11 @@ private partial def regroupUsing : List Term → TacticM Unit | throwError "tensor_regroup: {e} is not an equation" let mut err := m!"tensor_regroup: no factors of either side are contracted as in the left \ side of {e}" + let expands := if hasLowering (← instantiateMVars (← getMainTarget)) || hasLowering A then + [false, true] else [false] -- The right side is searched by first turning the goal around. for flip in [false, true] do - for expand in [false, true] do + for expand in expands do let s ← saveState -- A side that is not a contraction has no factors to search. let read? ← try @@ -1502,6 +1513,14 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | σ ρ}ᵀ := by tensor_regroup +-- A false goal with lowerings reports the summand as written, not with the lowerings expanded. +/-- error: tensor_regroup: the summand + (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w +has no matching summand on the right -/ +#guard_msgs in +example (w : S.Tensor ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by + tensor_regroup + /-- Summands of opposite signs are matched by swapping an antisymmetric form, `M^μν g_νρ = -(g_ρν M^μν)`. -/ example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : From 4c08faa165dae6d562b044c09ebc926ee9c42db1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:29:58 +0000 Subject: [PATCH 079/242] doc(Pauli): overview names tensor_regroup using Co-authored-by: Claude Opus 5.5 --- Physlib/Relativity/Bispinors/Pauli/Tensor.lean | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index ed809d39c1..b8132afe1b 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -33,11 +33,11 @@ Lowering both indices of a matrix with `ε` gives its adjugate, transposed, and One identity is read off from the Pauli matrices, the trace identity `pauliCoDown_trace_pauliCo`, `tr(σ̄_μ σ_ν) = 2 η_μν`: its components are the matrix traces of `PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix`. The others are derived from -it at tensor level, by `tensor_regroup` and `tensor_slots`. The Fierz identity reads the trace -identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB` between tensors with a vector index -and tensors with two Weyl indices. Both spaces have dimension `16`, so `A ∘ B = 2` as well, which -is the Fierz identity. The anticommutators follow from the trace identities and the Schouten -identity of `ε`. +it at tensor level, mostly by `tensor_regroup using` an identity proved before. The Fierz identity +reads the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB` between tensors with a +vector index and tensors with two Weyl indices. Both spaces have dimension `16`, so `A ∘ B = 2` as +well, which is the Fierz identity. The anticommutators follow from the trace identities and the +Schouten identity of `ε`. ## ii. Key results From b8b866b6aff39a6a5612e8de8f690d999a04a5e1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:36:43 +0000 Subject: [PATCH 080/242] chore: transitive Mathlib.Tactic.Module import removed from ForMathlib/Schouten Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Schouten.lean | 1 - 1 file changed, 1 deletion(-) diff --git a/Physlib/Mathematics/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean index 001d2ec4a2..6dd41965b5 100644 --- a/Physlib/Mathematics/ForMathlib/Schouten.lean +++ b/Physlib/Mathematics/ForMathlib/Schouten.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.SesquilinearForm.Basic -public import Mathlib.Tactic.Module /-! From c8cc5c5723d23d3ac0f3af1de5adafe1bda425ae Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 17:36:43 +0000 Subject: [PATCH 081/242] refactor(Elab): examples moved to Tensors/ElabExamples; regroupedFun antiquotations bound to names Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Elab.lean | 394 +---------------- Physlib/Mathematics/Tensors/ElabExamples.lean | 416 ++++++++++++++++++ 3 files changed, 423 insertions(+), 388 deletions(-) create mode 100644 Physlib/Mathematics/Tensors/ElabExamples.lean diff --git a/Physlib.lean b/Physlib.lean index c52fddb3c1..ba046a6bb9 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -206,6 +206,7 @@ public import Physlib.Mathematics.Tensors.Color public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.ElabExamples public import Physlib.Mathematics.Tensors.HermitianForm public import Physlib.Mathematics.Tensors.Inverse public import Physlib.Mathematics.Tensors.Metric diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 24aa55d683..822806490c 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -57,7 +57,8 @@ value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is -`contr_formTensor_formTensor` (section C). +`contr_formTensor_formTensor`. Examples of the notation and its tactics are in +`Physlib.Mathematics.Tensors.ElabExamples`. ## ii. Key results @@ -83,7 +84,6 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.8. Sums, signs and scalars - B.9. Chains of lowerings and raisings - B.10. Rewriting a sub-product by an identity -- C. Examples ## iv. References @@ -1116,9 +1116,11 @@ private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : L let n := match G.free.idxOf? s with | some k => s!"f{k}" | none => s!"p{(G.pairs.findIdx? fun (p, q) => p == s || q == s).getD 0}" - `(tensorIndex| $(mkIdent (Name.mkSimple n)):ident) + let i := mkIdent (Name.mkSimple n) + `(tensorIndex| $i:ident) let leaf (T : Expr) (ss : List Nat) : TermElabM (TSyntax `tensorIndexExpr) := do - `(tensorIndexExpr| $(← exprToSyntax T):term | $((ss.map name).toArray)*) + let t ← exprToSyntax T + `(tensorIndexExpr| $t:term | $((ss.map name).toArray)*) let rest ← ((List.range G.factors.length).filter (!target.contains ·)).mapM fun j => leaf G.factors[j]! ((List.range (G.offsets[j+1]! - G.offsets[j]!)).map (G.offsets[j]! + ·)) withLocalDeclD `x (← inferType A) fun x => do @@ -1241,387 +1243,3 @@ elab_rules : tactic end Tensors.Notation end - -/-! - -## C. Examples - --/ - -namespace Tensors.Notation - -open Species - -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} - [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} - (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) - (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) - -/-- info: T : S.Tensor ![a, a.dual] -/ -#guard_msgs in -#check {T | μ ν}ᵀ - -/-- The matrix product `T^μ_ν U^ν_ρ`. -/ -noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ - -/-- A product given as one term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the -product in the notation. -/ -example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl - -/-- An equation with a tensor `s` with no slots, `T^μ_μ = s`. The space after the bar keeps -Mathlib's absolute value `|x|` from reading it. -/ -example (s : S.Tensor (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl - -/-- An equation in parentheses is the same equation. -/ -example : {(T | μ ν = U | μ ν)}ᵀ ↔ {T | μ ν = U | μ ν}ᵀ := Iff.rfl - -/-- An equation whose right side lists its indices in the other order, `T^μ_ν = T'_ν^μ`. -/ -example : Prop := {T | μ ν = T' | ν μ}ᵀ - -/-- The trace of a product of four matrices, `T^μ_ν U^ν_ρ T^ρ_σ U^σ_μ`. -/ -noncomputable example : S.Tensor (![] : Fin 0 → Color K) := - {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ}ᵀ - -/-- The trace of a product of five matrices, `T^μ_ν U^ν_ρ W^ρ_σ T^σ_τ U^τ_μ`. -/ -noncomputable example : S.Tensor (![] : Fin 0 → Color K) := - {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ ⊗ T | σ τ ⊗ U | τ μ}ᵀ - -/-- Sums, negatives and scalar multiples, `-T^μ_ν + r U^μ_ν`. -/ -example : {-T | μ ν + r • U | μ ν}ᵀ = -T + r • U := rfl - -/-- A scalar multiple of a contraction, `r T^μ_ν U^ν_ρ`. -/ -example : {r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ}ᵀ = r • {T | μ ν ⊗ U | ν ρ}ᵀ := rfl - -/-- Each side of a sum is contracted first, so `μ` is free again in the product, -`(T^λ_λ + U^λ_λ) T^μ_ν`. -/ -example : {(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ = {({T | μ μ}ᵀ + {U | μ μ}ᵀ) | ⊗ T | μ ν}ᵀ := rfl - --- The two sides of a sum or an equation must agree in the colors of each name. -/-- error: index notation: ν has the color a.dual on the left and a on the right -/ -#guard_msgs in -#check {T | μ ν = T | ν μ}ᵀ - --- A term that is neither a tensor nor tensorial is rejected, with the error of the instance search. -/-- error: index notation: r is not a tensor and has no `Tensorial` instance: -failed to synthesize instance of type class - Tensorial -/ -#guard_msgs (substring := true) in -#check {r | μ}ᵀ - --- A term whose type is not known, such as a bare numeral, is rejected. -/-- error: index notation: the type of 0 is not known; give it, as `(0 : S.Tensor ![…])` -/ -#guard_msgs in -#check {0 | μ ν}ᵀ - -/-- An expression in parentheses is contracted first, so `μ` is free again in the product, -`(T^λ_λ) U^μ_ρ`. -/ -example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ := rfl - --- A name appearing three times in one product chain is rejected. -/-- error: index notation: μ appears more than twice -/ -#guard_msgs in -#check {T | μ μ ⊗ U | μ ρ}ᵀ - --- A tensor whose colors are not a literal list is rejected. -/-- error: index notation: the colors of t must be a literal list -/ -#guard_msgs in -example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ - --- A pair of slots whose colors are not dual is rejected. -/-- error: index notation: μ joins the colors a and a, which are not dual -/ -#guard_msgs in -#check {T | μ ν ⊗ T | μ ρ}ᵀ - -/-- Contracting `ν` and then `ρ` is contracting both at once, `(T U)^μ_ρ W^ρ_τ = T^μ_ν U^ν_ρ W^ρ_τ`, -by `tmulEquiv_contr_left` and `contr_contr`. -/ -example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U | ν ρ ⊗ W | ρ τ}ᵀ := by - rw [Tensor.tmulEquiv_contr_left, Tensor.contr_contr] - congr 2 - ext x - fin_cases x <;> rfl - -/-- The same by `tensor_regroup`, with the other grouping on the right, -`(T U)^μ_ρ W^ρ_τ = T^μ_ν (U W)^ν_τ`. -/ -example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ := by - tensor_regroup - -/-- `tensor_regroup` reorders the factors, `T^μ_ν U^ν_ρ = U^ν_ρ T^μ_ν`. -/ -example : {T | μ ν ⊗ U | ν ρ = U | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - -/-- A factor that appears twice, `T^μ_ν T^ν_ρ = T^ν_ρ T^μ_ν`. -/ -example : {T | μ ν ⊗ T | ν ρ = T | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - -/-- The trace is cyclic, `tr(T U T U) = tr(U T U T)`. -/ -example : {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ = U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ ⊗ T | μ ν}ᵀ := by - tensor_regroup - -/-- `tensor_regroup` moves scalars out of the products, `(r T^μ_ν) U^ν_ρ = r (U^ν_ρ T^μ_ν)`. -/ -example : {(r • T) | μ ν ⊗ U | ν ρ = r • {U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by - tensor_regroup - -/-- Negatives, `(-T^μ_ν) U^ν_ρ = -(U^ν_ρ T^μ_ν)`. -/ -example : {-T | μ ν ⊗ U | ν ρ = -{U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by - tensor_regroup - -/-- Sums, matched summand by summand in any order, `T^μ_ν U^ν_ρ - W^μ_ρ = -W^μ_ρ + U^ν_ρ T^μ_ν`. -/ -example : {T | μ ν ⊗ U | ν ρ + -W | μ ρ = -W | μ ρ + U | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - -/-- A sum inside a product is distributed, `T^μ_ν (U + W)^ν_ρ = U^ν_ρ T^μ_ν + W^ν_ρ T^μ_ν`. -/ -example : {T | μ ν ⊗ (U | ν ρ + W | ν ρ) = U | ν ρ ⊗ T | μ ν + W | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - -/-- A delta on one side is inserted on the other, `T^μ_ν = T^μ_ρ δ^ρ_ν`. The left side is a single -tensor, read as a contraction with no pairs. -/ -example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by - tensor_regroup - -/-- A delta inside a sum, `T^μ_ν + U^μ_ρ δ^ρ_ν = U^μ_ν + T^μ_ν`. -/ -example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by - tensor_regroup - -/-- A delta on the left side is inserted on the right, `T^μ_ρ δ^ρ_ν = T^μ_ν`. -/ -example : {T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = T | μ ν}ᵀ := by - tensor_regroup - -/-- Three equal factors, matched in one of the `3!` ways, -`T^μ_ν T^ν_ρ T^ρ_σ = T^ρ_σ T^ν_ρ T^μ_ν`. -/ -example : {T | μ ν ⊗ T | ν ρ ⊗ T | ρ σ = T | ρ σ ⊗ T | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - --- A factor with no counterpart on the other side is named. -/-- error: tensor_regroup: the factor - U -has no equal factor on the right -/ -#guard_msgs in -example : {T | μ ν ⊗ U | ν ρ = W | ν ρ ⊗ T | μ ν}ᵀ := by - tensor_regroup - --- The same factors contracted along other pairs of slots, `T U W ≠ T W U`, are rejected. -/-- error: tensor_regroup: the two sides contract different pairs of slots, for each way of -matching their equal factors -/ -#guard_msgs in -example : {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ = T | μ ν ⊗ W | ν ρ ⊗ U | ρ σ}ᵀ := by - tensor_regroup - --- A summand with no counterpart on the other side is named. -/-- error: tensor_regroup: the summand - W -has no matching summand on the right -/ -#guard_msgs in -example : {T | μ ν + W | μ ν = U | μ ν + T | μ ν}ᵀ := by - tensor_regroup - -/-- `tensor_regroup using h` finds `T^μ_ν U^ν_ρ` among the factors, rewrites it by `h` and -regroups, `U^ν_ρ W^ρ_σ T^μ_ν = W^μ_ρ W^ρ_σ`. -/ -example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : - {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = W | μ ρ ⊗ W | ρ σ}ᵀ := by - tensor_regroup using h - -/-- The identity is found on the right side, `W^μ_ρ W^ρ_σ = U^ν_ρ W^ρ_σ T^μ_ν`. -/ -example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : - {W | μ ρ ⊗ W | ρ σ = U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν}ᵀ := by - tensor_regroup using h - -/-- Two identities in turn, `U^ν_ρ W^ρ_σ T^μ_ν = (T U) W = W W = T`. -/ -example (h₁ : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) (h₂ : {W | μ ν ⊗ W | ν ρ = T | μ ρ}ᵀ) : - {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = T | μ σ}ᵀ := by - tensor_regroup using h₁, h₂ - -/-- The free indices of the identity may be contracted with each other, `T^μ_ν U^ν_μ = W^μ_μ`. -/ -example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν μ ⊗ T | μ ν = W | μ μ}ᵀ := by - tensor_regroup using h - --- The factors `T` and `U` are contracted as `U^ν_ρ T^ρ_μ`, not as `T^μ_ν U^ν_ρ`. -/-- error: tensor_regroup: no factors of either side are contracted as in the left side of h -/ -#guard_msgs in -example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν ρ ⊗ T | ρ μ = W | ν μ}ᵀ := by - tensor_regroup using h - --- After the rewrite the two sides still differ, `W ≠ U`. -/-- error: tensor_regroup: the factor - W -has no equal factor on the right -/ -#guard_msgs in -example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {T | μ ν ⊗ U | ν ρ = U | μ ρ}ᵀ := by - tensor_regroup using h - -section Metric - -variable [S.HasForm κ] [S.HasForm c.kind] - -/-- A lowering is a contraction with the form, `v_μ = v^ν g_νμ`. -/ -example : {v | ↓μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν μ}ᵀ := by - tensor_regroup - -/-- A form and its inverse are matched by inserting a delta, `v^μ = v^ν g_νρ g^ρμ`. -/ -example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ}ᵀ := by - tensor_regroup - -/-- A lowering cancels against the inverse form, `M^μ_ν g^νρ = M^μρ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by - tensor_regroup - -/-- Two lowerings cancel against two inverse forms, one pair at a time, -`M_μν g^μρ g^νσ = M^ρσ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | ρ σ}ᵀ := by - tensor_regroup - -/-- The same with the pairs on the right side and on two kinds, `N^ρσ = N_μν g^μρ g'^νσ`. -/ -example (κ' : K) [S.HasForm κ'] (N : S.Tensor ![⟨κ, .up⟩, ⟨κ', .up⟩]) : - {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ', .up⟩ | ν σ}ᵀ := by - tensor_regroup - -/-- A lowering contracted with the second index of a symmetric form, `M^μ_ν g^ρν = M^μρ`. -/ -example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by - tensor_regroup - -/-- With an antisymmetric form the first index cancels with no sign, `M^μ_ν ε^νρ = M^μρ`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by - tensor_regroup - -/-- With an antisymmetric form the second index cancels with a sign, `M^μ_ν ε^ρν = -M^μρ`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = -M | μ ρ}ᵀ := by - tensor_regroup - --- The sign of an antisymmetric form is not dropped, `M^μ_ν ε^ρν ≠ M^μρ`. -/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against -the other side - S.formTensor { kind := κ, variance := Variance.up }.dual - S.formTensor { kind := κ, variance := Variance.up } -/ -#guard_msgs in -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by - tensor_regroup - --- Cancelling the pairs does not swap the free indices, `M_μν g^μρ g^νσ ≠ M^σρ`. -/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against -the other side - S.formTensor { kind := κ, variance := Variance.up }.dual - S.formTensor { kind := κ, variance := Variance.up }.dual - S.formTensor { kind := κ, variance := Variance.up } - S.formTensor { kind := κ, variance := Variance.up } -/ -#guard_msgs in -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | σ ρ}ᵀ := by - tensor_regroup - --- A false goal with lowerings reports the summand as written, not with the lowerings expanded. -/-- error: tensor_regroup: the summand - (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w -has no matching summand on the right -/ -#guard_msgs in -example (w : S.Tensor ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by - tensor_regroup - -/-- Summands of opposite signs are matched by swapping an antisymmetric form, -`M^μν g_νρ = -(g_ρν M^μν)`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = - -{S.formTensor ⟨κ, .down⟩ | ρ ν ⊗ M | μ ν}ᵀ | ρ μ}ᵀ := by - tensor_regroup - -/-- A symmetric form is swapped when the regrouping fails, `M^μν g_νρ = M^μν g_ρν`. -/ -example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by - tensor_regroup - -/-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ -example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ - -/-- The same by `tensor_slots`. -/ -example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by - tensor_slots - -/-- `tensor_slots` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ -example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by - tensor_slots - --- An equation with no lowering or raising is rejected. -/-- error: tensor_slots: neither side lowers or raises an index -/ -#guard_msgs in -example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by - tensor_slots - -/-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by - tensor_slots - -/-- Lowerings at two slots commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by - tensor_slots - --- An arrow that does not match the variance of the slot is rejected. -/-- error: index notation: ↑μ needs a visibly lower index -/ -#guard_msgs in -#check {v | ↑μ}ᵀ - -/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ -example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] - -/-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by - simp only [equiv_smul] - -/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by -`rw [intertwiningMap_smul]`. -/ -example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] - -/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates -is `ColorEquiv.matMul`, checked slot by slot. -/ -example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by - rw [← contr_formTensor_formTensor] - congr 2 - ext x - fin_cases x <;> rfl - -/-- The same by `tensor_regroup`, which writes the delta as the contraction of the two forms. -/ -example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by - tensor_regroup - -/-- The delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ -example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by - tensor_regroup - -end Metric - -section Tensorial - -variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) - -/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ -#guard_msgs in -#check {m | μ ν}ᵀ - -/-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ -noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ - -/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ -noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ - -/-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ -noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ - -open TensorProduct in -/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ -example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by - rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] - -/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ -noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ - -/-- error: index notation: the free index μ has the color a, but its slot is expected to have the -color a.dual -/ -#guard_msgs in -example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ - -end Tensorial - -end Tensors.Notation diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean new file mode 100644 index 0000000000..30e2b1236c --- /dev/null +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -0,0 +1,416 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Elab + +/-! + +# Examples of index notation + +Examples and tests of the index notation `{…}ᵀ` and of the tactics `tensor_components`, +`tensor_regroup` and `tensor_slots`. + +## i. Overview + +Each example states an equation in index notation and proves it by `rfl` or by one of the tactics. +The `#guard_msgs` tests fix the elaborated terms and the error messages of false goals. + +## ii. Key results + +There are no declarations, only examples. + +## iii. Table of contents + +- A. Examples + +## iv. References + +-/ + +/-! + +## A. Examples + +-/ + +namespace Tensors.Notation + +open Species + +variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} + (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) + (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) + +/-- info: T : S.Tensor ![a, a.dual] -/ +#guard_msgs in +#check {T | μ ν}ᵀ + +/-- The matrix product `T^μ_ν U^ν_ρ`. -/ +noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ + +/-- A product given as one term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the +product in the notation. -/ +example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl + +/-- An equation with a tensor `s` with no slots, `T^μ_μ = s`. The space after the bar keeps +Mathlib's absolute value `|x|` from reading it. -/ +example (s : S.Tensor (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl + +/-- An equation in parentheses is the same equation. -/ +example : {(T | μ ν = U | μ ν)}ᵀ ↔ {T | μ ν = U | μ ν}ᵀ := Iff.rfl + +/-- An equation whose right side lists its indices in the other order, `T^μ_ν = T'_ν^μ`. -/ +example : Prop := {T | μ ν = T' | ν μ}ᵀ + +/-- The trace of a product of four matrices, `T^μ_ν U^ν_ρ T^ρ_σ U^σ_μ`. -/ +noncomputable example : S.Tensor (![] : Fin 0 → Color K) := + {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ}ᵀ + +/-- The trace of a product of five matrices, `T^μ_ν U^ν_ρ W^ρ_σ T^σ_τ U^τ_μ`. -/ +noncomputable example : S.Tensor (![] : Fin 0 → Color K) := + {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ ⊗ T | σ τ ⊗ U | τ μ}ᵀ + +/-- Sums, negatives and scalar multiples, `-T^μ_ν + r U^μ_ν`. -/ +example : {-T | μ ν + r • U | μ ν}ᵀ = -T + r • U := rfl + +/-- A scalar multiple of a contraction, `r T^μ_ν U^ν_ρ`. -/ +example : {r • {T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ}ᵀ = r • {T | μ ν ⊗ U | ν ρ}ᵀ := rfl + +/-- Each side of a sum is contracted first, so `μ` is free again in the product, +`(T^λ_λ + U^λ_λ) T^μ_ν`. -/ +example : {(T | μ μ + U | μ μ) ⊗ T | μ ν}ᵀ = {({T | μ μ}ᵀ + {U | μ μ}ᵀ) | ⊗ T | μ ν}ᵀ := rfl + +-- The two sides of a sum or an equation must agree in the colors of each name. +/-- error: index notation: ν has the color a.dual on the left and a on the right -/ +#guard_msgs in +#check {T | μ ν = T | ν μ}ᵀ + +-- A term that is neither a tensor nor tensorial is rejected, with the error of the instance search. +/-- error: index notation: r is not a tensor and has no `Tensorial` instance: +failed to synthesize instance of type class + Tensorial -/ +#guard_msgs (substring := true) in +#check {r | μ}ᵀ + +-- A term whose type is not known, such as a bare numeral, is rejected. +/-- error: index notation: the type of 0 is not known; give it, as `(0 : S.Tensor ![…])` -/ +#guard_msgs in +#check {0 | μ ν}ᵀ + +/-- An expression in parentheses is contracted first, so `μ` is free again in the product, +`(T^λ_λ) U^μ_ρ`. -/ +example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ := rfl + +-- A name appearing three times in one product chain is rejected. +/-- error: index notation: μ appears more than twice -/ +#guard_msgs in +#check {T | μ μ ⊗ U | μ ρ}ᵀ + +-- A tensor whose colors are not a literal list is rejected. +/-- error: index notation: the colors of t must be a literal list -/ +#guard_msgs in +example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ + +-- A pair of slots whose colors are not dual is rejected. +/-- error: index notation: μ joins the colors a and a, which are not dual -/ +#guard_msgs in +#check {T | μ ν ⊗ T | μ ρ}ᵀ + +/-- Contracting `ν` and then `ρ` is contracting both at once, `(T U)^μ_ρ W^ρ_τ = T^μ_ν U^ν_ρ W^ρ_τ`, +by `tmulEquiv_contr_left` and `contr_contr`. -/ +example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U | ν ρ ⊗ W | ρ τ}ᵀ := by + rw [Tensor.tmulEquiv_contr_left, Tensor.contr_contr] + congr 2 + ext x + fin_cases x <;> rfl + +/-- The same by `tensor_regroup`, with the other grouping on the right, +`(T U)^μ_ρ W^ρ_τ = T^μ_ν (U W)^ν_τ`. -/ +example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ := by + tensor_regroup + +/-- `tensor_regroup` reorders the factors, `T^μ_ν U^ν_ρ = U^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ U | ν ρ = U | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- A factor that appears twice, `T^μ_ν T^ν_ρ = T^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ T | ν ρ = T | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- The trace is cyclic, `tr(T U T U) = tr(U T U T)`. -/ +example : {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ = U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- `tensor_regroup` moves scalars out of the products, `(r T^μ_ν) U^ν_ρ = r (U^ν_ρ T^μ_ν)`. -/ +example : {(r • T) | μ ν ⊗ U | ν ρ = r • {U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- Negatives, `(-T^μ_ν) U^ν_ρ = -(U^ν_ρ T^μ_ν)`. -/ +example : {-T | μ ν ⊗ U | ν ρ = -{U | ν ρ ⊗ T | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- Sums, matched summand by summand in any order, `T^μ_ν U^ν_ρ - W^μ_ρ = -W^μ_ρ + U^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ U | ν ρ + -W | μ ρ = -W | μ ρ + U | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- A sum inside a product is distributed, `T^μ_ν (U + W)^ν_ρ = U^ν_ρ T^μ_ν + W^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ (U | ν ρ + W | ν ρ) = U | ν ρ ⊗ T | μ ν + W | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +/-- A delta on one side is inserted on the other, `T^μ_ν = T^μ_ρ δ^ρ_ν`. The left side is a single +tensor, read as a contraction with no pairs. -/ +example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by + tensor_regroup + +/-- A delta inside a sum, `T^μ_ν + U^μ_ρ δ^ρ_ν = U^μ_ν + T^μ_ν`. -/ +example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by + tensor_regroup + +/-- A delta on the left side is inserted on the right, `T^μ_ρ δ^ρ_ν = T^μ_ν`. -/ +example : {T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = T | μ ν}ᵀ := by + tensor_regroup + +/-- Three equal factors, matched in one of the `3!` ways, +`T^μ_ν T^ν_ρ T^ρ_σ = T^ρ_σ T^ν_ρ T^μ_ν`. -/ +example : {T | μ ν ⊗ T | ν ρ ⊗ T | ρ σ = T | ρ σ ⊗ T | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +-- A factor with no counterpart on the other side is named. +/-- error: tensor_regroup: the factor + U +has no equal factor on the right -/ +#guard_msgs in +example : {T | μ ν ⊗ U | ν ρ = W | ν ρ ⊗ T | μ ν}ᵀ := by + tensor_regroup + +-- The same factors contracted along other pairs of slots, `T U W ≠ T W U`, are rejected. +/-- error: tensor_regroup: the two sides contract different pairs of slots, for each way of +matching their equal factors -/ +#guard_msgs in +example : {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ = T | μ ν ⊗ W | ν ρ ⊗ U | ρ σ}ᵀ := by + tensor_regroup + +-- A summand with no counterpart on the other side is named. +/-- error: tensor_regroup: the summand + W +has no matching summand on the right -/ +#guard_msgs in +example : {T | μ ν + W | μ ν = U | μ ν + T | μ ν}ᵀ := by + tensor_regroup + +/-- `tensor_regroup using h` finds `T^μ_ν U^ν_ρ` among the factors, rewrites it by `h` and +regroups, `U^ν_ρ W^ρ_σ T^μ_ν = W^μ_ρ W^ρ_σ`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : + {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = W | μ ρ ⊗ W | ρ σ}ᵀ := by + tensor_regroup using h + +/-- The identity is found on the right side, `W^μ_ρ W^ρ_σ = U^ν_ρ W^ρ_σ T^μ_ν`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : + {W | μ ρ ⊗ W | ρ σ = U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν}ᵀ := by + tensor_regroup using h + +/-- Two identities in turn, `U^ν_ρ W^ρ_σ T^μ_ν = (T U) W = W W = T`. -/ +example (h₁ : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) (h₂ : {W | μ ν ⊗ W | ν ρ = T | μ ρ}ᵀ) : + {U | ν ρ ⊗ W | ρ σ ⊗ T | μ ν = T | μ σ}ᵀ := by + tensor_regroup using h₁, h₂ + +/-- The free indices of the identity may be contracted with each other, `T^μ_ν U^ν_μ = W^μ_μ`. -/ +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν μ ⊗ T | μ ν = W | μ μ}ᵀ := by + tensor_regroup using h + +-- The factors `T` and `U` are contracted as `U^ν_ρ T^ρ_μ`, not as `T^μ_ν U^ν_ρ`. +/-- error: tensor_regroup: no factors of either side are contracted as in the left side of h -/ +#guard_msgs in +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {U | ν ρ ⊗ T | ρ μ = W | ν μ}ᵀ := by + tensor_regroup using h + +-- After the rewrite the two sides still differ, `W ≠ U`. +/-- error: tensor_regroup: the factor + W +has no equal factor on the right -/ +#guard_msgs in +example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {T | μ ν ⊗ U | ν ρ = U | μ ρ}ᵀ := by + tensor_regroup using h + +section Metric + +variable [S.HasForm κ] [S.HasForm c.kind] + +/-- A lowering is a contraction with the form, `v_μ = v^ν g_νμ`. -/ +example : {v | ↓μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν μ}ᵀ := by + tensor_regroup + +/-- A form and its inverse are matched by inserting a delta, `v^μ = v^ν g_νρ g^ρμ`. -/ +example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ}ᵀ := by + tensor_regroup + +/-- A lowering cancels against the inverse form, `M^μ_ν g^νρ = M^μρ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + tensor_regroup + +/-- Two lowerings cancel against two inverse forms, one pair at a time, +`M_μν g^μρ g^νσ = M^ρσ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | ρ σ}ᵀ := by + tensor_regroup + +/-- The same with the pairs on the right side and on two kinds, `N^ρσ = N_μν g^μρ g'^νσ`. -/ +example (κ' : K) [S.HasForm κ'] (N : S.Tensor ![⟨κ, .up⟩, ⟨κ', .up⟩]) : + {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ', .up⟩ | ν σ}ᵀ := by + tensor_regroup + +/-- A lowering contracted with the second index of a symmetric form, `M^μ_ν g^ρν = M^μρ`. -/ +example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + tensor_regroup + +/-- With an antisymmetric form the first index cancels with no sign, `M^μ_ν ε^νρ = M^μρ`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + tensor_regroup + +/-- With an antisymmetric form the second index cancels with a sign, `M^μ_ν ε^ρν = -M^μρ`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = -M | μ ρ}ᵀ := by + tensor_regroup + +-- The sign of an antisymmetric form is not dropped, `M^μ_ν ε^ρν ≠ M^μρ`. +/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against +the other side + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up } -/ +#guard_msgs in +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + tensor_regroup + +-- Cancelling the pairs does not swap the free indices, `M_μν g^μρ g^νσ ≠ M^σρ`. +/-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against +the other side + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up }.dual + S.formTensor { kind := κ, variance := Variance.up } + S.formTensor { kind := κ, variance := Variance.up } -/ +#guard_msgs in +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | σ ρ}ᵀ := by + tensor_regroup + +-- A false goal with lowerings reports the summand as written, not with the lowerings expanded. +/-- error: tensor_regroup: the summand + (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w +has no matching summand on the right -/ +#guard_msgs in +example (w : S.Tensor ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by + tensor_regroup + +/-- Summands of opposite signs are matched by swapping an antisymmetric form, +`M^μν g_νρ = -(g_ρν M^μν)`. -/ +example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = + -{S.formTensor ⟨κ, .down⟩ | ρ ν ⊗ M | μ ν}ᵀ | ρ μ}ᵀ := by + tensor_regroup + +/-- A symmetric form is swapped when the regrouping fails, `M^μν g_νρ = M^μν g_ρν`. -/ +example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by + tensor_regroup + +/-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ + +/-- The same by `tensor_slots`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by + tensor_slots + +/-- `tensor_slots` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ +example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by + tensor_slots + +-- An equation with no lowering or raising is rejected. +/-- error: tensor_slots: neither side lowers or raises an index -/ +#guard_msgs in +example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by + tensor_slots + +/-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by + tensor_slots + +/-- Lowerings at two slots commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by + tensor_slots + +-- An arrow that does not match the variance of the slot is rejected. +/-- error: index notation: ↑μ needs a visibly lower index -/ +#guard_msgs in +#check {v | ↑μ}ᵀ + +/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ +example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] + +/-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by + simp only [equiv_smul] + +/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by +`rw [intertwiningMap_smul]`. -/ +example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] + +/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates +is `ColorEquiv.matMul`, checked slot by slot. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + rw [← contr_formTensor_formTensor] + congr 2 + ext x + fin_cases x <;> rfl + +/-- The same by `tensor_regroup`, which writes the delta as the contraction of the two forms. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + tensor_regroup + +/-- The delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ +example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by + tensor_regroup + +end Metric + +section Tensorial + +variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) + +/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ +#guard_msgs in +#check {m | μ ν}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ +noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ + +/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ +noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ +noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ + +open TensorProduct in +/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ +example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by + rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] + +/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ +noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ + +/-- error: index notation: the free index μ has the color a, but its slot is expected to have the +color a.dual -/ +#guard_msgs in +example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ + +end Tensorial + +end Tensors.Notation From ae2a3ca734b819b08e345c82326ea6b43d1e06d4 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sat, 7 Nov 2026 19:19:28 +0000 Subject: [PATCH 082/242] refactor(Pauli): pauliA, pauliB renamed vectorToBispinor, bispinorToVector; pauliB_pauliA, pauliA_pauliB renamed bispinorToVector_vectorToBispinor, vectorToBispinor_bispinorToVector Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index b8132afe1b..ef208ac36c 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -33,11 +33,11 @@ Lowering both indices of a matrix with `ε` gives its adjugate, transposed, and One identity is read off from the Pauli matrices, the trace identity `pauliCoDown_trace_pauliCo`, `tr(σ̄_μ σ_ν) = 2 η_μν`: its components are the matrix traces of `PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix`. The others are derived from -it at tensor level, mostly by `tensor_regroup using` an identity proved before. The Fierz identity -reads the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB` between tensors with a -vector index and tensors with two Weyl indices. Both spaces have dimension `16`, so `A ∘ B = 2` as -well, which is the Fierz identity. The anticommutators follow from the trace identities and the -Schouten identity of `ε`. +it at tensor level, mostly by `tensor_regroup using` an identity proved before. Contracting with +`σ` gives a map `vectorToBispinor` from tensors with a vector index to tensors with two Weyl indices +and a map `bispinorToVector` back. The trace identity says that going there and back is `2`. Both +spaces have dimension `16`, so going back and there is `2` as well, which is the Fierz identity. +The anticommutators follow from the trace identities and the Schouten identity of `ε`. ## ii. Key results @@ -47,7 +47,8 @@ Schouten identity of `ε`. - `complexLorentz.pauliCoDown_trace_pauliCo`, `complexLorentz.pauliContr_trace_pauliContrDown` and `complexLorentz.pauliContrDown_trace_pauliContr` : the trace identities `tr(σ̄_μ σ_ν) = 2 η_μν` and `tr(σ^μ σ̄^ν) = tr(σ̄^μ σ^ν) = 2 η^μν`. -- `complexLorentz.pauliA_pauliB` : the maps `pauliA` and `pauliB` are inverse up to `2`. +- `complexLorentz.vectorToBispinor_bispinorToVector` : the maps `vectorToBispinor` and + `bispinorToVector` are inverse up to `2`. - `complexLorentz.pauliCo_contr_pauliContr` : the Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`. - `complexLorentz.pauliContr_mul_pauliContrDown_add` and @@ -140,7 +141,7 @@ lemma tensorBasis_repr_pauliTensor_downAll The trace identity `pauliCoDown_trace_pauliCo` is proved in components, from the trace identity of the Pauli matrices. The other identities are derived from it at tensor level: the Fierz -identity by reading the trace identity as `B ∘ A = 2` for the maps `pauliA` and `pauliB`, and the +identity by reading the trace identity as `bispinorToVector ∘ vectorToBispinor = 2`, and the anticommutators by the Schouten identity of `ε`. ### C.1. Traces @@ -176,42 +177,47 @@ lemma pauliContrDown_trace_pauliCo : -/ -/-- `p ↦ σT_μ^{αβ̇} p^{μα'β̇'}`, from a tensor with a vector index to one with two Weyl indices. -/ -noncomputable def pauliA : ℂT[up, upL, upR] →ₗ[ℂ] ℂT[upL, upR, upL, upR] where +/-- `p ↦ σT_μ^{αβ̇} p^{μα'β̇'}`, from a tensor with a vector index to one with two Weyl indices. +On a vector, with no `α'β̇'`, it is the bispinor `p^{αβ̇}` of `p^μ`. -/ +noncomputable def vectorToBispinor : ℂT[up, upL, upR] →ₗ[ℂ] ℂT[upL, upR, upL, upR] where toFun p := {pauliTensor | ↓μ α β ⊗ p | μ α' β'}ᵀ map_add' p q := by simp only [tmul_add, map_add] map_smul' r p := by simp only [tmul_smul, map_smul, RingHom.id_apply] /-- `X ↦ σT^μ_{αβ̇} X^{αβ̇α'β̇'}`, from a tensor with two Weyl indices to one with a vector -index. -/ -noncomputable def pauliB : ℂT[upL, upR, upL, upR] →ₗ[ℂ] ℂT[up, upL, upR] where +index, the inverse of `vectorToBispinor` up to `2`. -/ +noncomputable def bispinorToVector : ℂT[upL, upR, upL, upR] →ₗ[ℂ] ℂT[up, upL, upR] where toFun X := {pauliTensor | μ ↓α ↓β ⊗ X | α β α' β'}ᵀ map_add' X Y := by simp only [tmul_add, map_add] map_smul' r X := by simp only [tmul_smul, map_smul, RingHom.id_apply] -/-- `pauliB ∘ pauliA = 2`, by the trace identity `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ_ν^μ`. -/ -lemma pauliB_pauliA (p : ℂT[up, upL, upR]) : pauliB (pauliA p) = (2 : ℂ) • p := by - simp only [pauliA, pauliB, LinearMap.coe_mk, AddHom.coe_mk] +/-- `bispinorToVector ∘ vectorToBispinor = 2`, by the trace identity +`σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ_ν^μ`. -/ +lemma bispinorToVector_vectorToBispinor (p : ℂT[up, upL, upR]) : + bispinorToVector (vectorToBispinor p) = (2 : ℂ) • p := by + simp only [vectorToBispinor, bispinorToVector, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup using pauliContrDown_trace_pauliCo -/-- `pauliA ∘ pauliB = 2`: both spaces have dimension `16`, so the left inverse `pauliB / 2` of -`pauliA` is a right inverse. -/ -lemma pauliA_pauliB (X : ℂT[upL, upR, upL, upR]) : pauliA (pauliB X) = (2 : ℂ) • X := +/-- `vectorToBispinor ∘ bispinorToVector = 2`: both spaces have dimension `16`, so the left inverse +`bispinorToVector / 2` of `vectorToBispinor` is a right inverse. -/ +lemma vectorToBispinor_bispinorToVector (X : ℂT[upL, upR, upL, upR]) : + vectorToBispinor (bispinorToVector X) = (2 : ℂ) • X := LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq - (by rw [Species.finrank_tensor, Species.finrank_tensor]; rfl) two_ne_zero pauliB_pauliA X + (by rw [Species.finrank_tensor, Species.finrank_tensor]; rfl) two_ne_zero + bispinorToVector_vectorToBispinor X /-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α ↑β}ᵀ = pauliTensor := by tensor_slots /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is -`pauliA σT = pauliA (pauliB (εL ⊗ εR)) / 2`. -/ +`vectorToBispinor σT = vectorToBispinor (bispinorToVector (εL ⊗ εR)) / 2`. -/ lemma pauliCo_contr_pauliContr : {pauliTensor | ↓ν α β ⊗ pauliTensor | ν α' β' = (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by - rw [map_smul, ← pauliA_pauliB] - refine congrArg pauliA ?_ - simp only [pauliB, LinearMap.coe_mk, AddHom.coe_mk] + rw [map_smul, ← vectorToBispinor_bispinorToVector] + refine congrArg vectorToBispinor ?_ + simp only [bispinorToVector, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup /-! From 97e179f4eea93a40b4b9098f3fbeae32bacb1b3e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 8 Nov 2026 18:41:45 +0000 Subject: [PATCH 083/242] refactor(Tensors): tensor_components, tensor_regroup, tensor_slots moved from Elab to ElabTactics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Elab keeps the index notation {…}ᵀ and elabTensorIndex. ElabExamples is grouped into notation examples (A–C) and tactic examples (D–F). Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/Elab.lean | 821 +---------------- Physlib/Mathematics/Tensors/ElabExamples.lean | 201 ++-- Physlib/Mathematics/Tensors/ElabTactics.lean | 858 ++++++++++++++++++ Physlib/Mathematics/Tensors/Schouten.lean | 2 +- .../Tensors/RealTensor/Species.lean | 2 +- 6 files changed, 987 insertions(+), 898 deletions(-) create mode 100644 Physlib/Mathematics/Tensors/ElabTactics.lean diff --git a/Physlib.lean b/Physlib.lean index ba046a6bb9..b4a355f9af 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -207,6 +207,7 @@ public import Physlib.Mathematics.Tensors.Conjugation public import Physlib.Mathematics.Tensors.Contraction public import Physlib.Mathematics.Tensors.Elab public import Physlib.Mathematics.Tensors.ElabExamples +public import Physlib.Mathematics.Tensors.ElabTactics public import Physlib.Mathematics.Tensors.HermitianForm public import Physlib.Mathematics.Tensors.Inverse public import Physlib.Mathematics.Tensors.Metric diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 822806490c..2050a18e2d 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -57,18 +57,12 @@ value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written `{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is -`contr_formTensor_formTensor`. Examples of the notation and its tactics are in +`contr_formTensor_formTensor`. Examples of the notation are in `Physlib.Mathematics.Tensors.ElabExamples`. ## ii. Key results - `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. -- `tensor_components` : reduces an equation of tensors to an equation of their components. -- `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped - or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, - swaps of symmetric and antisymmetric forms, and lowerings written as contractions. With - `using e₁, …`, it first rewrites a sub-product of either side by each identity `eᵢ`. -- `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -77,13 +71,6 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - B.1. Slot types - B.2. The steps of an expression - B.3. The elaborator - - B.4. Components - - B.5. Reading and rewriting the goal - - B.6. Matching the factors of two contractions - - B.7. Deltas and forms - - B.8. Sums, signs and scalars - - B.9. Chains of lowerings and raisings - - B.10. Rewriting a sub-product by an identity ## iv. References @@ -434,812 +421,6 @@ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do | `({ $e:tensorIndexExpr }ᵀ) => elabTerm (← buildFor e expectedType?) expectedType? | _ => throwUnsupportedSyntax -/-! - -### B.4. Components - --/ - -/-- Removes `cast h a` when the two types agree at reducible transparency. -/ -dsimproc_decl reduceCast (cast _ _) := fun e => do - let_expr cast α β _ a := e | return .continue - if ← withReducible (isDefEq α β) then return .done a else return .continue - -/-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, -using the component lemmas `lᵢ` as well. It unfolds products, relabelings, sums, negatives, zeros, -scalar multiples and contractions of one or two pairs. For more pairs, pass -`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal slot that -`tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a -goal that is already a component the first step is skipped. -/ -syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.simpArg,* "]" : - tactic - -macro_rules - | `(tactic| tensor_components [$ls,*]) => `(tactic| focus - try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) - -- `Function.update_of_ne` proves `j ≠ i` for literal slots by `ne_eq` and `Fin.reduceEq`. - simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, - Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_one_pair, - Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, - Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, - Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] - -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are - -- evaluated. The option `+instances` reaches those inside instance arguments. - try dsimp +instances only [ColorEquiv.toEquiv_symm, Equiv.symm_symm, Equiv.coe_fn_mk, - Sum.elim_inl, Sum.elim_inr, Matrix.cons_val, Function.comp_apply, - Tensors.Notation.reduceCast]) - -/-! - -### B.5. Reading and rewriting the goal - --/ - -open Tactic - -/-- The type and the two sides of the goal of the tactic `tac`, an equation. -/ -private def goalEq (tac : String := "tensor_regroup") : TacticM (Expr × Expr × Expr) := do - let some (ty, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? - | throwError "{tac}: the goal is not an equation" - return (ty, l.consumeMData, r.consumeMData) - -/-- The argument `x` of `F x`, for a bundled map `F` whose head is the constant `f`, such as -`Tensor.contr σ` for `f = Tensor.contr`. -/ -private def bundledArg? (f : Name) (e : Expr) : Option Expr := - let e := e.consumeMData - if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf f then some e.appArg! else none - -/-- Writes each side `x` of the goal on which `p` fails as `F x`, by the term `h : F x = x`. -/ -private def wrapSides (tac : String) (p : Expr → Bool) (h : Term) : TacticM Unit := do - let (_, l, r) ← goalEq tac - unless p l do evalTactic (← `(tactic| refine (Eq.symm $h).trans ?_)) - unless p r do evalTactic (← `(tactic| refine Eq.trans ?_ $h)) - -/-- Whether `a` and `b` are definitionally equal, without assigning metavariables of the goal. -/ -private def isDefEqGoal (a b : Expr) : MetaM Bool := withNewMCtxDepth (isDefEq a b) - -/-- Runs `f x` for each `x` of `xs` in turn, each from the current state, and returns the result of -the first that succeeds. When every one fails it throws the last error, and `err` when `xs` is -empty. -/ -private def firstSuccess {α β : Type} (xs : List α) (err : MessageData) (f : α → TacticM β) : - TacticM β := do - let s ← saveState - let mut err := err - for x in xs do - try return ← f x - catch e => s.restore; err := e.toMessageData - throwError err - -/-- The number of trailing arguments of `e` that are the tensors it is built from: the argument of -a bundled map, the two factors of `T ⊗ₜ U`, the two summands of a sum, and the tensor of a negative -or of a scalar multiple. -/ -private def tensorArgs (e : Expr) : Nat := - if e.isAppOf ``TensorProduct.tmul || e.isAppOfArity ``HAdd.hAdd 6 then 2 - else if e.isAppOfArity ``DFunLike.coe 6 || e.isAppOfArity ``Neg.neg 3 || - e.isAppOfArity ``HSMul.hSMul 6 then 1 - else 0 - -/-- The tensors that `e` is built from, from left to right, through the arguments of -`tensorArgs`. -/ -private partial def tensorLeaves (e : Expr) : List Expr := - let e := e.consumeMData - let args := e.getAppArgs - match tensorArgs e with - | 0 => [e] - | n => (args.toList.drop (args.size - n)).flatMap tensorLeaves - -/-- The expression `e` with its leaf `k` of `tensorLeaves e` replaced by `v` if it is `x`, and `k` -less the number of leaves of `e`. -/ -private partial def replaceLeaf (x v e : Expr) (k : Int) : Expr × Int := - let e := e.consumeMData - let n := tensorArgs e - if k < 0 then (e, k) - else if n == 0 then (if k == 0 && e == x then v else e, k - 1) - else - let args := e.getAppArgs - let (args, k) := (List.range n).foldl (init := (args, k)) fun (args, k) i => - let j := args.size - n + i - let (a, k) := replaceLeaf x v args[j]! k - (args.set! j a, k) - (mkAppN e.getAppFn args, k) - -/-- Rewrites the subterm `x` of the goal by the term `thm : x' = y`, where `x'` is `x` up to -unfolding instances, as in `rw`: on the left side only if `lhs = some true`, on the right side only -if `lhs = some false`, and only at the leaf `k` of the tensor leaves of the two sides -(`tensorLeaves`) if `k` is given. Unlike `rw`, it finds `x` by syntactic equality and does not check -the type of the whole goal, which on large products is slow. -/ -private def rewriteSubterm (x : Expr) (thm : Term) (lhs : Option Bool := none) - (k : Option Nat := none) : TacticM Unit := do - let (_, l, r) ← goalEq - -- As in `rw`, the arguments of `thm` left open, such as `rfl`, are elaborated after `x` fixes - -- the implicit arguments. - let h ← Tactic.elabTerm thm none (mayPostpone := true) - let some (_, x', _) := (← instantiateMVars (← inferType h)).eq? - | throwError "tensor_regroup: internal error, {thm} is not an equation" - unless ← withTransparency .instances (isDefEq x x') do - throwError "tensor_regroup: {thm} does not rewrite{indentExpr x}" - Term.synthesizeSyntheticMVarsNoPostponing - let motive ← withLocalDeclD `x (← inferType x) fun v => do - let (l, r) := match k with - | none => - let replace (e : Expr) := e.replace fun e => if e == x then some v else none - (if lhs != some false then replace l else l, if lhs != some true then replace r else r) - | some k => - let (l', k) := if lhs != some false then replaceLeaf x v l k else (l, k) - (l', if lhs != some true then (replaceLeaf x v r k).1 else r) - mkLambdaFVars #[v] (← mkEq l r) - let pf ← mkCongrArg motive (← instantiateMVars h) - let some (_, _, target) := (← inferType pf).eq? | throwError "tensor_regroup: internal error" - replaceMainGoal [← (← getMainGoal).replaceTargetEq target.headBeta pf] - -/-! - -### B.6. Matching the factors of two contractions - --/ - -/-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ -private def productFactors? (e : Expr) : Option (Expr × Expr) := do - let x := (← bundledArg? ``Tensors.Species.tmulEquiv e).consumeMData - if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none - -/-- The factors of a product, from left to right. -/ -private partial def factors (e : Expr) : List Expr := - match productFactors? e with - | some (a, b) => factors a ++ factors b - | none => [e] - -/-- The product of the first `n` factors of a left-nested product `e` of `m` factors. -/ -private def leadingProduct (e : Expr) (m n : Nat) : Expr := Id.run do - let mut e := e - for _ in [0:m - n] do - if let some (a, _) := productFactors? e then e := a - return e - -/-- Whether every product inside the product `e` is in its left factor. -/ -private partial def isLeftNested (e : Expr) : Bool := - match productFactors? e with - | some (a, b) => (productFactors? b).isNone && isLeftNested a - | none => true - -/-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ -private def contractedProducts : TacticM (Expr × Expr) := do - let (_, l, r) ← goalEq - let some p := bundledArg? ``Tensors.Species.Tensor.contr l - | throwError "tensor_regroup: the left side is not a contraction" - let some q := bundledArg? ``Tensors.Species.Tensor.contr r - | throwError "tensor_regroup: the right side is not a contraction" - return (p, q) - -/-- Writes both sides of the goal as one contraction of a left-nested product, repeating until the -goal no longer changes, at most ten times. A side that is not a contraction is read as one with no -pairs (`Species.Tensor.contr_noPairs`). The three steps run separately, since together their lemmas -loop. -/ -private def flattenContractions : TacticM Unit := do - wrapSides "tensor_regroup" (bundledArg? ``Tensors.Species.Tensor.contr · |>.isSome) - (← `(Tensors.Species.Tensor.contr_noPairs _)) - for _ in [0:10] do - let before ← instantiateMVars (← getMainTarget) - evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr_left, - Tensors.Species.Tensor.tmulEquiv_contr_right, Tensors.Species.Tensor.contr_contr])) - evalTactic (← `(tactic| try simp only [← Tensors.Species.tmulEquiv_assoc])) - evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, - Tensors.Species.tmulEquiv_reindex_right, Tensors.Species.reindex_reindex, - Tensors.Species.Tensor.contr_reindex, Tensors.Species.Tensor.reindex_contr])) - -- The two sides may become equal, and `simp` then closes the goal. - if (← getGoals).isEmpty then return - if (← instantiateMVars (← getMainTarget)) == before then return - throwError "tensor_regroup: flattening the products did not stop after ten rounds" - -/-- Swaps the factors `m` and `m + 1` of the product `q`, of `n` factors, on the right side of the -goal, by `Species.tmulEquiv_comm` or `Species.tmulEquiv_swapLast`, and absorbs the relabeling into -the contraction. -/ -private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do - let fs := factors q - let thm ← if m = 0 then - `((Tensors.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) - $(← exprToSyntax fs[0]!)).symm) - else - `((Tensors.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) - $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) - rewriteSubterm (leadingProduct q n (m + 2)) thm (some false) - evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, - Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) - -/-- The ways to match each factor on the left with a different factor on the right, given for -each factor on the left the factors on the right equal to it. The matching in order comes first. -/ -private partial def matchings : List (List Nat) → List Nat → List (List Nat) - | [], _ => [[]] - | c :: cs, used => - (c.filter (!used.contains ·)).flatMap fun j => (matchings cs (j :: used)).map (j :: ·) - -/-- Orders the factors on the right side of the goal so that the factor at each position `k` is -the one that started at position `target[k]`, by swapping neighbours. -/ -private def arrange (target : List Nat) : TacticM Unit := do - let n := target.length - -- The starting positions of the factors in their current order. - let mut labels := List.range n - for pos in (List.range n).reverse do - for m in [labels.idxOf target[pos]!:pos] do - swapFactors (← contractedProducts).2 n m - labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! - -/-- The largest number of matchings of equal factors that `matchFactors` tries, so that a false goal -fails within the default heartbeats. -/ -private def maxMatchings : Nat := 12 - -/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same -factors. It orders the factors of the right side as on the left, and checks by evaluation that both -sides keep the same slots and contract the same pairs of slots -(`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of -matching the equal factors is tried in turn, at most `maxMatchings` of them. -/ -private def matchFactors : TacticM Unit := do - let (p, q) ← contractedProducts - unless isLeftNested p && isLeftNested q do - throwError "tensor_regroup: a side is not a left-nested product after flattening" - let ps := factors p - let qs := factors q - unless ps.length = qs.length do - throwError "tensor_regroup: the two sides have different numbers of factors" - let candidates ← ps.mapM fun x => (List.range qs.length).filterM fun j => isDefEqGoal x qs[j]! - if let some (x, _) := (ps.zip candidates).find? (·.2.isEmpty) then - throwError "tensor_regroup: the factor{indentExpr x}\nhas no equal factor on the right" - let targets := matchings candidates [] - if targets.isEmpty then - throwError "tensor_regroup: a factor appears more often on the left than on the right" - let tried := if targets.length ≤ maxMatchings then m!"each way" else - m!"the first {maxMatchings} of the {targets.length} ways" - firstSuccess (targets.take maxMatchings) m!"" fun target => do - arrange target - -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The - -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free - -- color variables of the goal although the bijections of slots do not depend on them. - try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in - exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) - (of_decide_eq_true rfl) _)) - catch _ => throwError "tensor_regroup: the two sides contract different pairs of slots, for \ - {tried} of matching their equal factors" - -/-! - -### B.7. Deltas and forms - --/ - -/-- The factors among `xs` that are left over once each factor of `ys` takes an equal factor of -`xs`. -/ -private def leftover (xs ys : List Expr) : MetaM (List Expr) := do - let mut rest := xs - for y in ys do - if let some i ← (List.range rest.length).findM? fun i => isDefEqGoal rest[i]! y then - rest := rest.eraseIdx i - return rest - -/-- The color `a` of a delta `S.deltaTensor a`. -/ -private def deltaColor? (x : Expr) : Option Expr := - let x := x.consumeMData - if x.isAppOf ``Tensors.Species.deltaTensor then some x.appArg! else none - -/-- The species `S` and the color `c` of a form `S.formTensor c`. -/ -private def formArgs? (x : Expr) : Option (Expr × Expr) := - let x := x.consumeMData - if x.isAppOf ``Tensors.Species.formTensor then - let args := x.getAppArgs - some (args[args.size - 3]!, args[args.size - 2]!) - else none - -/-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, -if `lhs`, or of the right side, by `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`). -/ -private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : TacticM Unit := do - let (p, q) ← contractedProducts - let P := if lhs then p else q - let thm ← `((Tensors.Species.Tensor.contr_deltaTensor - ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) - rewriteSubterm P thm (some lhs) - -mutual - -/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, at each slot of -the product of color `a` in turn, and proves the goal by `regroupContractions`. With `skipForms`, -the slots of forms are left out when another factor has a slot: inserting the delta anywhere along a -chain of contracted forms gives the same factors, so trying each of their slots only repeats the -search. -/ -private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Bool := false) : - TacticM Unit := do - let (p, q) ← contractedProducts - let P := if lhs then p else q - let some (slots, _, colors) ← literalTensorType? (← inferType P) - | throwError "tensor_regroup: the slots or colors of a product are not literal" - -- Whether each slot belongs to a form, from the number of slots of each factor in turn. - let inForm ← (factors P).flatMapM fun f => do - let n := ((← literalTensorType? (← inferType f)).map (·.2.2.length)).getD 0 - return List.replicate n (skipForms && (formArgs? f).isSome) - -- shortcut: misses a chain of forms that reaches no other factor, as `T^μ_ν g^αβ` against - -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the slots of forms after the others would cover it. - let inForm := if inForm.all id then inForm.map fun _ => false else inForm - let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => - return !inForm.getD i false && (← isDefEqGoal col a) - firstSuccess (slotsOfColor.map (·.2)) m!"tensor_regroup: no slot has the color {a} of a delta" - fun slot => do - insertDelta lhs a slots slot - regroupContractions - -/-- Matches pairs of forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors -`ds` left over on the left side, if `lhs`, or on the right side. A delta `ds = [δ]` is written as -the contraction of two forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying -the color of each form whose dual form is also left over. With `ds = []`, a delta is inserted -first. Each pair cancelled this way, as a lowering `g_μν` against a raising form `g^νρ`, leaves the -other pairs to the next round of `regroupContractions`. -/ -private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Unit := do - let fs := gs.filterMap formArgs? - unless 2 ≤ gs.length && gs.length % 2 == 0 && fs.length == gs.length do - throwError "tensor_regroup: the factors left over are not pairs of forms" - -- Only the colors whose dual form is also left over can cancel, each tried once. - let fs ← fs.eraseDups.filterM fun (_, c) => do - let cd ← mkAppM ``Tensors.Color.dual #[c] - fs.anyM fun (_, c') => isDefEqGoal c' cd - match ds with - | [d] => - unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" - firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (S, c) => do - let thm ← `((Tensors.Species.contr_formTensor_formTensor (S := $(← exprToSyntax S)) - $(← exprToSyntax c)).symm) - rewriteSubterm d thm (some lhs) - regroupContractions - | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do - insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) (skipForms := true) - | _ => throwError "tensor_regroup: more than one factor is left over against forms" - -/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to -deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested -product. Two forms on one side with a delta or nothing left over on the other are matched by -`matchFormPair`. A delta on one side with no counterpart on the other is inserted on the other side -at each slot of its color in turn, since contracting with a delta is a relabeling. The factors are -then matched by `matchFactors`. -/ -private partial def regroupContractions : TacticM Unit := do - flattenContractions - if (← getGoals).isEmpty then return - let (p, q) ← contractedProducts - let extraL ← leftover (factors p) (factors q) - let extraR ← leftover (factors q) (factors p) - -- The error reported in place of the count of factors when forms are left over on one side. - let mut formErr : Option MessageData := none - for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do - try matchFormPair lhs ds gs; return catch _ => - if 2 ≤ gs.length && gs.all (formArgs? · |>.isSome) then - formErr := m!"tensor_regroup: the forms left over on one side do not cancel in inverse pairs \ - against the other side{indentD (MessageData.joinSep (gs.map toMessageData) "\n")}" - -- A delta left over on the right is inserted on the left, and conversely. - match extraL.mapM deltaColor?, extraR.mapM deltaColor? with - | some _, some (a :: _) => insertDeltaAnywhere true a - | some (a :: _), some [] => insertDeltaAnywhere false a - | _, _ => try matchFactors catch e => throwError (formErr.getD e.toMessageData) - -end - -/-! - -### B.8. Sums, signs and scalars - --/ - -/-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, -contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. -The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a -factor. -/ -private def normalizeSums : TacticM Unit := do - let (ty, _, _) ← goalEq - let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! - evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, - TensorProduct.tmul_add, TensorProduct.add_tmul, TensorProduct.tmul_neg, - TensorProduct.neg_tmul, TensorProduct.tmul_sub, TensorProduct.sub_tmul, - TensorProduct.tmul_smul (R' := $k), ← TensorProduct.smul_tmul' (R' := $k), smul_add, - smul_neg, smul_smul, neg_neg, neg_add, sub_eq_add_neg])) - -/-- The summands of a sum, from left to right. -/ -private partial def summands (e : Expr) : List Expr := - let e := e.consumeMData - if e.isAppOfArity ``HAdd.hAdd 6 then summands e.appFn!.appArg! ++ summands e.appArg! else [e] - -/-- Rewrites the form `F = S.formTensor c` at the tensor leaf `k` of the goal by its swap, -`g^μν = -g^νμ` for an antisymmetric form (`Species.formTensor_antisymm`), if `antisym`, and -`g^μν = g^νμ` for a symmetric one (`Species.formTensor_symm`). -/ -private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do - let some (S, c) := formArgs? F | throwError "tensor_regroup: internal error, {F} is not a form" - let (S, c) := (← exprToSyntax S, ← exprToSyntax c) - let thm ← if antisym then - `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c).symm) - else `((Tensors.Species.formTensor_symm (S := $S) $c).symm) - rewriteSubterm F thm (k := some k) - -mutual - -/-- Proves an equation of two summands with the same sign and the same scalar, by regrouping the -contractions they multiply. When exactly one side is negated, one antisymmetric form on either -side is swapped first, as in `ε^αβ = -ε^βα`. When the regrouping fails, one symmetric form is -swapped, as in `η^μν = η^νμ`. At most one form is swapped. -/ -private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := do - let (_, l, r) ← goalEq - let negL := l.isAppOfArity ``Neg.neg 3 - let negR := r.isAppOfArity ``Neg.neg 3 - if negL && negR then - evalTactic (← `(tactic| refine congrArg Neg.neg ?_)) - regroupSummand flipped - else if negL != negR && !flipped then - flipOneForm true - else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then - let a := l.appFn!.appArg! - unless ← isDefEqGoal a r.appFn!.appArg! do - throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" - evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) - regroupSummand flipped - else - try evalTactic (← `(tactic| with_reducible rfl)) catch _ => - let s ← saveState - try regroupContractions catch e => - if flipped then throw e - -- The two sides may differ by the swap of a symmetric form. - s.restore - try flipOneForm false catch _ => s.restore; throw e - -/-- Tries each form among the tensor leaves of the goal for `flipForm antisym`, then moves a new -sign out and regroups. A form of the other symmetry fails at `flipForm`. -/ -private partial def flipOneForm (antisym : Bool) : TacticM Unit := do - let (_, l, r) ← goalEq - let leaves := tensorLeaves l ++ tensorLeaves r - let s ← saveState - let mut lastError : MessageData := m!"tensor_regroup: no form to swap" - for F in (leaves.filter (formArgs? · |>.isSome)).eraseDups do - for k in (List.range leaves.length).filter (leaves[·]! == F) do - s.restore - try flipForm antisym F k catch _ => break - try - normalizeSums - regroupSummand true - return - catch e => lastError := e.toMessageData - s.restore - throwError lastError - -end - -/-- A proof of `e = e'` for a sum `e`, where `e'` replaces the summands of `e` from left to right -by the right sides of the proofs `hs`. The proofs left over are returned. -/ -private partial def sumCongr (e : Expr) (hs : List Expr) : MetaM (Expr × List Expr) := do - let e := e.consumeMData - if e.isAppOfArity ``HAdd.hAdd 6 then - let (p, hs) ← sumCongr e.appFn!.appArg! hs - let (q, hs) ← sumCongr e.appArg! hs - return (← mkCongr (← mkCongrArg e.appFn!.appFn! p) q, hs) - match hs with - | h :: hs => return (h, hs) - | [] => throwError "tensor_regroup: internal error, too few summand proofs" - -/-- The position among `rs`, outside `used`, of a summand equal to `a` of type `ty` by -`regroupSummand`, with the proof of the equation. -/ -private def matchSummand (ty a : Expr) (rs : List Expr) (used : List Nat) : - TacticM (Nat × Expr) := do - let js := (List.range rs.length).filter (!used.contains ·) - try - firstSuccess js m!"" fun j => do - -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. - let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) - setGoals [g.mvarId!] - regroupSummand - unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" - return (j, ← instantiateMVars g) - catch _ => - throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" - -/-- The body of `tensor_regroup`: moves sums, negatives and scalars out, then proves the equation -summand by summand. -/ -private def regroup : TacticM Unit := do - normalizeSums - -- Moving the scalars out may already close the goal. - if (← getGoals).isEmpty then return - let (ty, l, r) ← goalEq - let ls := summands l - let rs := summands r - unless ls.length = rs.length do - throwError "tensor_regroup: the two sides have different numbers of summands" - if ls.length = 1 then - regroupSummand - return - -- Each summand on the left is proved equal to an unused summand on the right in its own goal, - -- and the sums then agree up to order. - let main ← getMainGoal - let mut used : List Nat := [] - let mut proofs : Array Expr := #[] - for a in ls do - let (j, pf) ← matchSummand ty a rs used - used := j :: used - proofs := proofs.push pf - -- The summands are replaced by congruence, not by `rw`: a summand can occur inside another one, - -- as `T` inside its relabeling. - let (pf, _) ← sumCongr l proofs.toList - let some (_, _, l') := (← inferType pf).eq? | throwError "tensor_regroup: internal error" - let rest ← mkFreshExprMVar (← mkEq l' r) - main.assign (← mkEqTrans pf rest) - setGoals [rest.mvarId!] - evalTactic (← `(tactic| first | with_reducible rfl | abel)) - -/-- Whether `e` contains a lowering or raising `S.toDualAt`. -/ -private def hasLowering (e : Expr) : Bool := - (e.find? (·.isConstOf ``Tensors.Species.toDualAt)).isSome - -/-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as -contractions with the form (`Species.toDualAt_eq_contr`) when it fails and the goal has one. When -both fail, the first error is reported if it is about the summands or scalars, and the second -otherwise. -/ -private def regroupWithLowerings : TacticM Unit := do - unless hasLowering (← instantiateMVars (← getMainTarget)) do regroup; return - let s ← saveState - try regroup catch e => - -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They - -- are expanded only when needed, since every expanded form is one more factor to match. - s.restore - evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_contr])) - try regroup catch e' => - -- The expansion changes the factors, not the summands or scalars, so an error about those - -- reads better before it. - let msg ← e.toMessageData.toString - throw <| if msg.contains "summand" || msg.contains "scalars" then e else e' - -/-! - -### B.9. Chains of lowerings and raisings - --/ - -/-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, -such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain -is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps -are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side -with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be -`Fin n` with a literal `n`. -/ -elab "tensor_slots" : tactic => do - evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, - Tensors.Species.mapSlots_mapSlots])) - let isMapSlots (e : Expr) := (bundledArg? ``Tensors.Species.mapSlots e).isSome - let (_, l, r) ← goalEq "tensor_slots" - unless isMapSlots l || isMapSlots r do - throwError "tensor_slots: neither side lowers or raises an index" - -- Each side is checked before it is unified, since a failed unification with a side that is a - -- definition, such as a named tensor, unfolds it. - wrapSides "tensor_slots" isMapSlots (← `(Tensors.Species.mapSlots_id_apply _)) - evalTactic (← `(tactic| focus - refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_) - intro x - fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> - simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, - LinearMap.id_apply, Tensors.Species.slotMapsAt_apply_self, - Tensors.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, - Representation.IntertwiningMap.coe_toLinearMap, - Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, - Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual])) - -/-! - -### B.10. Rewriting a sub-product by an identity - --/ - -/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of -`P` are numbered in order, the slots of each factor after those of the factors before it. -/ -private structure ReadContraction where - /-- The factors of `P`, from left to right. -/ - factors : List Expr - /-- The slot of `P` at which each factor starts. -/ - offsets : List Nat - /-- The slot of `P` of each free index, in order. -/ - free : List Nat - /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ - pairs : List (Nat × Nat) - -/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in -slot order. -/ -private partial def slotElems (ι : Expr) : MetaM (List Expr) := do - let ι ← whnfR ι - if ι.isAppOfArity ``Fin 1 then - let some n ← (evalNat ι.appArg!).run - | throwError "tensor_regroup: the slot type {ι} is not literal" - (List.range n).mapM fun i => do - mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] - else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] - else if ι.isConstOf ``Empty then return [] - else if ι.isAppOfArity ``Sum 2 then - let (a, b) := (ι.getArg! 0, ι.getArg! 1) - return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ - (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) - else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" - -/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ -private partial def slotNumber : Slots → Expr → MetaM Nat - | .fin _, x => do - let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run - | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" - return i - | .unit, _ => return 0 - | .sum a b, x => do - let x ← whnfD x - if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! - else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) - else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" - -/-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each -free index and at each slot of each pair. -/ -private def readContraction (e : Expr) : MetaM ReadContraction := do - let some P := bundledArg? ``Tensors.Species.Tensor.contr e - | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" - let τ := e.consumeMData.appFn!.appArg!.appArg! - let some (slots, _, _) ← literalTensorType? (← inferType P) - | throwError "tensor_regroup: the slots or colors of a product are not literal" - let fs := factors P - let sizes ← fs.mapM fun f => do - let some (s, _, _) ← literalTensorType? (← inferType f) - | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" - return s.size - -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. - let args := (← whnfR (← inferType τ)).getAppArgs - let cargs := args[args.size - 1]!.getAppArgs - let (κ, J) := (cargs[1]!, cargs[2]!) - let JJ ← mkAppM ``Sum #[J, J] - let at_ (y : Expr) : MetaM Nat := do - slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) - let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) - let pairs ← (← slotElems J).mapM fun j => do - let half (left : Bool) := do - at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) - #[J, J, j]]) - return (← half true, ← half false) - return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ - -/-- The ways of sending the slots of `A` to slots of `G`, factor by factor, each factor of `A` to an -equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list of -images of the slots of `A`, together with the factors of `G` that are hit. -/ -private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List Nat)) := do - let candidates ← A.factors.mapM fun x => - (List.range G.factors.length).filterM fun j => isDefEqGoal x G.factors[j]! - let isPair (p q : Nat) := G.pairs.contains (p, q) || G.pairs.contains (q, p) - return (matchings candidates []).filterMap fun target => - let φ := (target.zipIdx).flatMap fun (j, i) => - (List.range (A.offsets[i+1]! - A.offsets[i]!)).map (G.offsets[j]! + ·) - if A.pairs.all fun (p, q) => isPair φ[p]! φ[q]! then some (φ, target) else none - -/-- The function `x ↦ {x | … ⊗ g | … ⊗ …}ᵀ`, for `x` of the type of `A`, with its free indices -in the order of `L`, the contraction read as `G`. The factors `g` of `G` not hit by `target` follow -`x`, and every slot of `G` is named after its free index `fₖ` or its pair `pⱼ`. The slots of `x` -are named as the free indices of `A` are sent by `φ`. -/ -private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : List Nat) : - TermElabM Expr := do - let name (s : Nat) : TSyntax `tensorIndex := Unhygienic.run do - let n := match G.free.idxOf? s with - | some k => s!"f{k}" - | none => s!"p{(G.pairs.findIdx? fun (p, q) => p == s || q == s).getD 0}" - let i := mkIdent (Name.mkSimple n) - `(tensorIndex| $i:ident) - let leaf (T : Expr) (ss : List Nat) : TermElabM (TSyntax `tensorIndexExpr) := do - let t ← exprToSyntax T - `(tensorIndexExpr| $t:term | $((ss.map name).toArray)*) - let rest ← ((List.range G.factors.length).filter (!target.contains ·)).mapM fun j => - leaf G.factors[j]! ((List.range (G.offsets[j+1]! - G.offsets[j]!)).map (G.offsets[j]! + ·)) - withLocalDeclD `x (← inferType A) fun x => do - let rhs ← rest.foldlM (init := ← leaf x (Aread.free.map (φ[·]!))) fun a b => - `(tensorIndexExpr| $a ⊗ $b) - -- The equation `{L | f₀ f₁ … = …}ᵀ` relabels its right side to the order of `L`. - let eq ← Term.elabType (← `({ $(← leaf L G.free) = $rhs }ᵀ)) - Term.synthesizeSyntheticMVarsNoPostponing - let some (_, _, M) := (← instantiateMVars eq).eq? - | throwError "tensor_regroup: internal error, not an equation" - mkLambdaFVars #[x] M - -/-- Proves the goal `ty` by `tac`, aside from the main goal. -/ -private def proveAside (ty : Expr) (tac : TacticM Unit) : TacticM Expr := do - let g ← mkFreshExprMVar ty - let gs ← getGoals - setGoals [g.mvarId!] - try - tac - unless (← getGoals).isEmpty do throwError "tensor_regroup: internal error, a goal is left" - finally setGoals gs - instantiateMVars g - -/-- The side `A`, flattened to one contraction of a left-nested product, with lowerings and -raisings first written as contractions with the form if `expand`. -/ -private def flattenTerm (A : Expr) (expand : Bool) : TacticM Expr := do - let g ← mkFreshExprMVar (← mkEq A (← mkAppOptM ``Zero.zero #[← inferType A, none])) - let gs ← getGoals - setGoals [g.mvarId!] - try - if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) - flattenContractions - return (← goalEq).2.1 - finally setGoals gs - -/-- The body of `tensor_regroup using e₁, …, eₙ`. For `e₁ : A = B`, the left side of the goal is -regrouped by `regroupWithLowerings` into a product `M A` of `A` with the factors not in `A`, which -`congrArg M e₁` rewrites to `M B`, and the remaining identities are used in the same way. Each way -of finding the factors of `A` among the factors of the goal is tried in turn, first as written and -then with lowerings and raisings written as contractions with the form, first on the left side of -the goal and then on the right. -/ -private partial def regroupUsing : List Term → TacticM Unit - | [] => regroupWithLowerings - | e :: es => do - let h ← instantiateMVars (← Tactic.elabTerm e none) - let some (_, A, B) := (← instantiateMVars (← inferType h)).eq? - | throwError "tensor_regroup: {e} is not an equation" - let mut err := m!"tensor_regroup: no factors of either side are contracted as in the left \ - side of {e}" - let expands := if hasLowering (← instantiateMVars (← getMainTarget)) || hasLowering A then - [false, true] else [false] - -- The right side is searched by first turning the goal around. - for flip in [false, true] do - for expand in expands do - let s ← saveState - -- A side that is not a contraction has no factors to search. - let read? ← try - if flip then replaceMainGoal [← (← getMainGoal).applySymm] - if expand then - evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) - flattenContractions - let (_, L, _) ← goalEq - pure (some (L, ← readContraction L, ← readContraction (← flattenTerm A expand))) - catch _ => pure none - if let some (L, G, Aread) := read? then - for (φ, target) in (← embeddings Aread G).take maxMatchings do - let s' ← saveState - try - -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. - let M ← regroupedFun L A Aread G φ target - let h₁ ← proveAside (← mkEq L (M.beta #[A])) regroupWithLowerings - let pf ← mkEqTrans h₁ (← mkCongrArg M h) - let (_, _, R) ← goalEq - let rest ← mkFreshExprSyntheticOpaqueMVar (← mkEq (M.beta #[B]) R) - (← getMainGoal).assign (← mkEqTrans pf rest) - replaceMainGoal [rest.mvarId!] - regroupUsing es - return - catch ex => s'.restore; err := ex.toMessageData - s.restore - throwError err - -/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of -the same factors, grouped or ordered differently, such as -`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, -negatives and scalars are first moved out of every product, contraction and relabeling. Each -summand on the left is then matched with a summand on the right with the same sign and scalar, -whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a -factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 -are tried. - -Beyond regrouping, it uses these identities: -- a delta on one side with no counterpart on the other is inserted on the other side at a slot of - its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on - the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` - (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering - `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta - first; -- when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` - (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, - `η^μν = η^νμ` (`Species.formTensor_symm`); -- when no matching is found, lowerings and raisings are expanded into contractions with the form - (`Species.toDualAt_eq_contr`) and the matching is tried again. - -A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs -(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional -equality, as `2` and `2`. - -`tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, -the factors of `Aᵢ` are found among the factors of the left side of the goal, or else of the right -side, contracted along the same pairs, and that side is regrouped as `Aᵢ` times the other factors -and rewritten to `Bᵢ`. The equations must be closed terms. -/ -syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic - -elab_rules : tactic - | `(tactic| tensor_regroup) => regroupWithLowerings - | `(tactic| tensor_regroup using $es,*) => regroupUsing es.getElems.toList - end Tensors.Notation end diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 30e2b1236c..c2de9529c3 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.ElabTactics /-! @@ -25,18 +25,17 @@ There are no declarations, only examples. ## iii. Table of contents -- A. Examples +- A. Products, sums and contractions +- B. Lowering and raising +- C. Tensorial types +- D. Regrouping contractions +- E. Regrouping with forms +- F. Chains of lowerings and raisings ## iv. References -/ -/-! - -## A. Examples - --/ - namespace Tensors.Notation open Species @@ -46,6 +45,13 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) + +/-! + +## A. Products, sums and contractions + +-/ + /-- info: T : S.Tensor ![a, a.dual] -/ #guard_msgs in #check {T | μ ν}ᵀ @@ -129,7 +135,90 @@ example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ U ext x fin_cases x <;> rfl -/-- The same by `tensor_regroup`, with the other grouping on the right, +/-! + +## B. Lowering and raising + +-/ + +section Metric + +variable [S.HasForm κ] [S.HasForm c.kind] + +/-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ + +-- An arrow that does not match the variance of the slot is rejected. +/-- error: index notation: ↑μ needs a visibly lower index -/ +#guard_msgs in +#check {v | ↑μ}ᵀ + +/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ +example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] + +/-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ +example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by + simp only [equiv_smul] + +/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by +`rw [intertwiningMap_smul]`. -/ +example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] + +/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates +is `ColorEquiv.matMul`, checked slot by slot. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + rw [← contr_formTensor_formTensor] + congr 2 + ext x + fin_cases x <;> rfl + +end Metric + +/-! + +## C. Tensorial types + +-/ + +section Tensorial + +variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) + +/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ +#guard_msgs in +#check {m | μ ν}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ +noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ + +/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ +noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ + +/-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ +noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ + +open TensorProduct in +/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ +example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by + rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] + +/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ +noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ + +/-- error: index notation: the free index μ has the color a, but its slot is expected to have the +color a.dual -/ +#guard_msgs in +example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ + +end Tensorial + +/-! + +## D. Regrouping contractions + +-/ + +/-- By `tensor_regroup`, with the other grouping on the right, `(T U)^μ_ρ W^ρ_τ = T^μ_ν (U W)^ν_τ`. -/ example : {{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ := by tensor_regroup @@ -237,6 +326,12 @@ has no equal factor on the right -/ example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {T | μ ν ⊗ U | ν ρ = U | μ ρ}ᵀ := by tensor_regroup using h +/-! + +## E. Regrouping with forms + +-/ + section Metric variable [S.HasForm κ] [S.HasForm c.kind] @@ -322,10 +417,28 @@ example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by tensor_regroup -/-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ -example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ +/-- `g^μν g_νρ = δ^μ_ρ` by `tensor_regroup`, which writes the delta as the contraction of the two +forms. -/ +example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by + tensor_regroup -/-- The same by `tensor_slots`. -/ +/-- The delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ +example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by + tensor_regroup + +end Metric + +/-! + +## F. Chains of lowerings and raisings + +-/ + +section Metric + +variable [S.HasForm κ] [S.HasForm c.kind] + +/-- Lowering and then raising by `tensor_slots`, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by tensor_slots @@ -347,70 +460,6 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by tensor_slots --- An arrow that does not match the variance of the slot is rejected. -/-- error: index notation: ↑μ needs a visibly lower index -/ -#guard_msgs in -#check {v | ↑μ}ᵀ - -/-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ -example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] - -/-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by - simp only [equiv_smul] - -/-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by -`rw [intertwiningMap_smul]`. -/ -example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] - -/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates -is `ColorEquiv.matMul`, checked slot by slot. -/ -example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by - rw [← contr_formTensor_formTensor] - congr 2 - ext x - fin_cases x <;> rfl - -/-- The same by `tensor_regroup`, which writes the delta as the contraction of the two forms. -/ -example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by - tensor_regroup - -/-- The delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ -example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by - tensor_regroup - end Metric -section Tensorial - -variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) - -/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ -#guard_msgs in -#check {m | μ ν}ᵀ - -/-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ -noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ - -/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ -noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ - -/-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ -noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ - -open TensorProduct in -/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ -example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by - rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] - -/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ -noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ - -/-- error: index notation: the free index μ has the color a, but its slot is expected to have the -color a.dual -/ -#guard_msgs in -example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ - -end Tensorial - end Tensors.Notation diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean new file mode 100644 index 0000000000..5715911711 --- /dev/null +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -0,0 +1,858 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Elab +/-! + +# Tactics for index notation + +Tactics that prove equations of tensors written in the index notation `{…}ᵀ`. + +## i. Overview + +Three tactics prove equations written in index notation. `tensor_components` works on the +components, `tensor_regroup` on the contractions and `tensor_slots` on the lowerings and raisings. +Examples are in `Physlib.Mathematics.Tensors.ElabExamples`. + +## ii. Key results + +- `tensor_components` : reduces an equation of tensors to an equation of their components. +- `tensor_regroup` : proves an equation of two sums of contractions of the same factors, grouped + or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, + swaps of symmetric and antisymmetric forms, and lowerings written as contractions. With + `using e₁, …`, it first rewrites a sub-product of either side by each identity `eᵢ`. +- `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. + +## iii. Table of contents + +- A. Components +- B. Reading and rewriting the goal +- C. Matching the factors of two contractions +- D. Deltas and forms +- E. Sums, signs and scalars +- F. Chains of lowerings and raisings +- G. Rewriting a sub-product by an identity + +## iv. References + +-/ + +public meta section + +open Lean Meta Elab Term + +namespace Tensors.Notation + +/-! + +## A. Components + +-/ + +/-- Removes `cast h a` when the two types agree at reducible transparency. -/ +dsimproc_decl reduceCast (cast _ _) := fun e => do + let_expr cast α β _ a := e | return .continue + if ← withReducible (isDefEq α β) then return .done a else return .continue + +/-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, +using the component lemmas `lᵢ` as well. It unfolds products, relabelings, sums, negatives, zeros, +scalar multiples and contractions of one or two pairs. For more pairs, pass +`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal slot that +`tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a +goal that is already a component the first step is skipped. -/ +syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.simpArg,* "]" : + tactic + +macro_rules + | `(tactic| tensor_components [$ls,*]) => `(tactic| focus + try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) + -- `Function.update_of_ne` proves `j ≠ i` for literal slots by `ne_eq` and `Fin.reduceEq`. + simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, + Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_one_pair, + Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, + Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, + Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] + -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are + -- evaluated. The option `+instances` reaches those inside instance arguments. + try dsimp +instances only [ColorEquiv.toEquiv_symm, Equiv.symm_symm, Equiv.coe_fn_mk, + Sum.elim_inl, Sum.elim_inr, Matrix.cons_val, Function.comp_apply, + Tensors.Notation.reduceCast]) + +/-! + +## B. Reading and rewriting the goal + +-/ + +open Tactic + +/-- The type and the two sides of the goal of the tactic `tac`, an equation. -/ +private def goalEq (tac : String := "tensor_regroup") : TacticM (Expr × Expr × Expr) := do + let some (ty, l, r) := (← instantiateMVars (← getMainTarget)).consumeMData.eq? + | throwError "{tac}: the goal is not an equation" + return (ty, l.consumeMData, r.consumeMData) + +/-- The argument `x` of `F x`, for a bundled map `F` whose head is the constant `f`, such as +`Tensor.contr σ` for `f = Tensor.contr`. -/ +private def bundledArg? (f : Name) (e : Expr) : Option Expr := + let e := e.consumeMData + if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf f then some e.appArg! else none + +/-- Writes each side `x` of the goal on which `p` fails as `F x`, by the term `h : F x = x`. -/ +private def wrapSides (tac : String) (p : Expr → Bool) (h : Term) : TacticM Unit := do + let (_, l, r) ← goalEq tac + unless p l do evalTactic (← `(tactic| refine (Eq.symm $h).trans ?_)) + unless p r do evalTactic (← `(tactic| refine Eq.trans ?_ $h)) + +/-- Whether `a` and `b` are definitionally equal, without assigning metavariables of the goal. -/ +private def isDefEqGoal (a b : Expr) : MetaM Bool := withNewMCtxDepth (isDefEq a b) + +/-- Runs `f x` for each `x` of `xs` in turn, each from the current state, and returns the result of +the first that succeeds. When every one fails it throws the last error, and `err` when `xs` is +empty. -/ +private def firstSuccess {α β : Type} (xs : List α) (err : MessageData) (f : α → TacticM β) : + TacticM β := do + let s ← saveState + let mut err := err + for x in xs do + try return ← f x + catch e => s.restore; err := e.toMessageData + throwError err + +/-- The number of trailing arguments of `e` that are the tensors it is built from: the argument of +a bundled map, the two factors of `T ⊗ₜ U`, the two summands of a sum, and the tensor of a negative +or of a scalar multiple. -/ +private def tensorArgs (e : Expr) : Nat := + if e.isAppOf ``TensorProduct.tmul || e.isAppOfArity ``HAdd.hAdd 6 then 2 + else if e.isAppOfArity ``DFunLike.coe 6 || e.isAppOfArity ``Neg.neg 3 || + e.isAppOfArity ``HSMul.hSMul 6 then 1 + else 0 + +/-- The tensors that `e` is built from, from left to right, through the arguments of +`tensorArgs`. -/ +private partial def tensorLeaves (e : Expr) : List Expr := + let e := e.consumeMData + let args := e.getAppArgs + match tensorArgs e with + | 0 => [e] + | n => (args.toList.drop (args.size - n)).flatMap tensorLeaves + +/-- The expression `e` with its leaf `k` of `tensorLeaves e` replaced by `v` if it is `x`, and `k` +less the number of leaves of `e`. -/ +private partial def replaceLeaf (x v e : Expr) (k : Int) : Expr × Int := + let e := e.consumeMData + let n := tensorArgs e + if k < 0 then (e, k) + else if n == 0 then (if k == 0 && e == x then v else e, k - 1) + else + let args := e.getAppArgs + let (args, k) := (List.range n).foldl (init := (args, k)) fun (args, k) i => + let j := args.size - n + i + let (a, k) := replaceLeaf x v args[j]! k + (args.set! j a, k) + (mkAppN e.getAppFn args, k) + +/-- Rewrites the subterm `x` of the goal by the term `thm : x' = y`, where `x'` is `x` up to +unfolding instances, as in `rw`: on the left side only if `lhs = some true`, on the right side only +if `lhs = some false`, and only at the leaf `k` of the tensor leaves of the two sides +(`tensorLeaves`) if `k` is given. Unlike `rw`, it finds `x` by syntactic equality and does not check +the type of the whole goal, which on large products is slow. -/ +private def rewriteSubterm (x : Expr) (thm : Term) (lhs : Option Bool := none) + (k : Option Nat := none) : TacticM Unit := do + let (_, l, r) ← goalEq + -- As in `rw`, the arguments of `thm` left open, such as `rfl`, are elaborated after `x` fixes + -- the implicit arguments. + let h ← Tactic.elabTerm thm none (mayPostpone := true) + let some (_, x', _) := (← instantiateMVars (← inferType h)).eq? + | throwError "tensor_regroup: internal error, {thm} is not an equation" + unless ← withTransparency .instances (isDefEq x x') do + throwError "tensor_regroup: {thm} does not rewrite{indentExpr x}" + Term.synthesizeSyntheticMVarsNoPostponing + let motive ← withLocalDeclD `x (← inferType x) fun v => do + let (l, r) := match k with + | none => + let replace (e : Expr) := e.replace fun e => if e == x then some v else none + (if lhs != some false then replace l else l, if lhs != some true then replace r else r) + | some k => + let (l', k) := if lhs != some false then replaceLeaf x v l k else (l, k) + (l', if lhs != some true then (replaceLeaf x v r k).1 else r) + mkLambdaFVars #[v] (← mkEq l r) + let pf ← mkCongrArg motive (← instantiateMVars h) + let some (_, _, target) := (← inferType pf).eq? | throwError "tensor_regroup: internal error" + replaceMainGoal [← (← getMainGoal).replaceTargetEq target.headBeta pf] + +/-! + +## C. Matching the factors of two contractions + +-/ + +/-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ +private def productFactors? (e : Expr) : Option (Expr × Expr) := do + let x := (← bundledArg? ``Tensors.Species.tmulEquiv e).consumeMData + if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none + +/-- The factors of a product, from left to right. -/ +private partial def factors (e : Expr) : List Expr := + match productFactors? e with + | some (a, b) => factors a ++ factors b + | none => [e] + +/-- The product of the first `n` factors of a left-nested product `e` of `m` factors. -/ +private def leadingProduct (e : Expr) (m n : Nat) : Expr := Id.run do + let mut e := e + for _ in [0:m - n] do + if let some (a, _) := productFactors? e then e := a + return e + +/-- Whether every product inside the product `e` is in its left factor. -/ +private partial def isLeftNested (e : Expr) : Bool := + match productFactors? e with + | some (a, b) => (productFactors? b).isNone && isLeftNested a + | none => true + +/-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ +private def contractedProducts : TacticM (Expr × Expr) := do + let (_, l, r) ← goalEq + let some p := bundledArg? ``Tensors.Species.Tensor.contr l + | throwError "tensor_regroup: the left side is not a contraction" + let some q := bundledArg? ``Tensors.Species.Tensor.contr r + | throwError "tensor_regroup: the right side is not a contraction" + return (p, q) + +/-- Writes both sides of the goal as one contraction of a left-nested product, repeating until the +goal no longer changes, at most ten times. A side that is not a contraction is read as one with no +pairs (`Species.Tensor.contr_noPairs`). The three steps run separately, since together their lemmas +loop. -/ +private def flattenContractions : TacticM Unit := do + wrapSides "tensor_regroup" (bundledArg? ``Tensors.Species.Tensor.contr · |>.isSome) + (← `(Tensors.Species.Tensor.contr_noPairs _)) + for _ in [0:10] do + let before ← instantiateMVars (← getMainTarget) + evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr_left, + Tensors.Species.Tensor.tmulEquiv_contr_right, Tensors.Species.Tensor.contr_contr])) + evalTactic (← `(tactic| try simp only [← Tensors.Species.tmulEquiv_assoc])) + evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, + Tensors.Species.tmulEquiv_reindex_right, Tensors.Species.reindex_reindex, + Tensors.Species.Tensor.contr_reindex, Tensors.Species.Tensor.reindex_contr])) + -- The two sides may become equal, and `simp` then closes the goal. + if (← getGoals).isEmpty then return + if (← instantiateMVars (← getMainTarget)) == before then return + throwError "tensor_regroup: flattening the products did not stop after ten rounds" + +/-- Swaps the factors `m` and `m + 1` of the product `q`, of `n` factors, on the right side of the +goal, by `Species.tmulEquiv_comm` or `Species.tmulEquiv_swapLast`, and absorbs the relabeling into +the contraction. -/ +private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do + let fs := factors q + let thm ← if m = 0 then + `((Tensors.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) + $(← exprToSyntax fs[0]!)).symm) + else + `((Tensors.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) + $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) + rewriteSubterm (leadingProduct q n (m + 2)) thm (some false) + evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, + Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) + +/-- The ways to match each factor on the left with a different factor on the right, given for +each factor on the left the factors on the right equal to it. The matching in order comes first. -/ +private partial def matchings : List (List Nat) → List Nat → List (List Nat) + | [], _ => [[]] + | c :: cs, used => + (c.filter (!used.contains ·)).flatMap fun j => (matchings cs (j :: used)).map (j :: ·) + +/-- Orders the factors on the right side of the goal so that the factor at each position `k` is +the one that started at position `target[k]`, by swapping neighbours. -/ +private def arrange (target : List Nat) : TacticM Unit := do + let n := target.length + -- The starting positions of the factors in their current order. + let mut labels := List.range n + for pos in (List.range n).reverse do + for m in [labels.idxOf target[pos]!:pos] do + swapFactors (← contractedProducts).2 n m + labels := (labels.set m labels[m+1]!).set (m+1) labels[m]! + +/-- The largest number of matchings of equal factors that `matchFactors` tries, so that a false goal +fails within the default heartbeats. -/ +private def maxMatchings : Nat := 12 + +/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same +factors. It orders the factors of the right side as on the left, and checks by evaluation that both +sides keep the same slots and contract the same pairs of slots +(`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of +matching the equal factors is tried in turn, at most `maxMatchings` of them. -/ +private def matchFactors : TacticM Unit := do + let (p, q) ← contractedProducts + unless isLeftNested p && isLeftNested q do + throwError "tensor_regroup: a side is not a left-nested product after flattening" + let ps := factors p + let qs := factors q + unless ps.length = qs.length do + throwError "tensor_regroup: the two sides have different numbers of factors" + let candidates ← ps.mapM fun x => (List.range qs.length).filterM fun j => isDefEqGoal x qs[j]! + if let some (x, _) := (ps.zip candidates).find? (·.2.isEmpty) then + throwError "tensor_regroup: the factor{indentExpr x}\nhas no equal factor on the right" + let targets := matchings candidates [] + if targets.isEmpty then + throwError "tensor_regroup: a factor appears more often on the left than on the right" + let tried := if targets.length ≤ maxMatchings then m!"each way" else + m!"the first {maxMatchings} of the {targets.length} ways" + firstSuccess (targets.take maxMatchings) m!"" fun target => do + arrange target + -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The + -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free + -- color variables of the goal although the bijections of slots do not depend on them. + try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in + exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) + (of_decide_eq_true rfl) _)) + catch _ => throwError "tensor_regroup: the two sides contract different pairs of slots, for \ + {tried} of matching their equal factors" + +/-! + +## D. Deltas and forms + +-/ + +/-- The factors among `xs` that are left over once each factor of `ys` takes an equal factor of +`xs`. -/ +private def leftover (xs ys : List Expr) : MetaM (List Expr) := do + let mut rest := xs + for y in ys do + if let some i ← (List.range rest.length).findM? fun i => isDefEqGoal rest[i]! y then + rest := rest.eraseIdx i + return rest + +/-- The color `a` of a delta `S.deltaTensor a`. -/ +private def deltaColor? (x : Expr) : Option Expr := + let x := x.consumeMData + if x.isAppOf ``Tensors.Species.deltaTensor then some x.appArg! else none + +/-- The species `S` and the color `c` of a form `S.formTensor c`. -/ +private def formArgs? (x : Expr) : Option (Expr × Expr) := + let x := x.consumeMData + if x.isAppOf ``Tensors.Species.formTensor then + let args := x.getAppArgs + some (args[args.size - 3]!, args[args.size - 2]!) + else none + +/-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, +if `lhs`, or of the right side, by `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`). -/ +private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : TacticM Unit := do + let (p, q) ← contractedProducts + let P := if lhs then p else q + let thm ← `((Tensors.Species.Tensor.contr_deltaTensor + ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) + rewriteSubterm P thm (some lhs) + +mutual + +/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, at each slot of +the product of color `a` in turn, and proves the goal by `regroupContractions`. With `skipForms`, +the slots of forms are left out when another factor has a slot: inserting the delta anywhere along a +chain of contracted forms gives the same factors, so trying each of their slots only repeats the +search. -/ +private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Bool := false) : + TacticM Unit := do + let (p, q) ← contractedProducts + let P := if lhs then p else q + let some (slots, _, colors) ← literalTensorType? (← inferType P) + | throwError "tensor_regroup: the slots or colors of a product are not literal" + -- Whether each slot belongs to a form, from the number of slots of each factor in turn. + let inForm ← (factors P).flatMapM fun f => do + let n := ((← literalTensorType? (← inferType f)).map (·.2.2.length)).getD 0 + return List.replicate n (skipForms && (formArgs? f).isSome) + -- shortcut: misses a chain of forms that reaches no other factor, as `T^μ_ν g^αβ` against + -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the slots of forms after the others would cover it. + let inForm := if inForm.all id then inForm.map fun _ => false else inForm + let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => + return !inForm.getD i false && (← isDefEqGoal col a) + firstSuccess (slotsOfColor.map (·.2)) m!"tensor_regroup: no slot has the color {a} of a delta" + fun slot => do + insertDelta lhs a slots slot + regroupContractions + +/-- Matches pairs of forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors +`ds` left over on the left side, if `lhs`, or on the right side. A delta `ds = [δ]` is written as +the contraction of two forms, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), trying +the color of each form whose dual form is also left over. With `ds = []`, a delta is inserted +first. Each pair cancelled this way, as a lowering `g_μν` against a raising form `g^νρ`, leaves the +other pairs to the next round of `regroupContractions`. -/ +private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Unit := do + let fs := gs.filterMap formArgs? + unless 2 ≤ gs.length && gs.length % 2 == 0 && fs.length == gs.length do + throwError "tensor_regroup: the factors left over are not pairs of forms" + -- Only the colors whose dual form is also left over can cancel, each tried once. + let fs ← fs.eraseDups.filterM fun (_, c) => do + let cd ← mkAppM ``Tensors.Color.dual #[c] + fs.anyM fun (_, c') => isDefEqGoal c' cd + match ds with + | [d] => + unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" + firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (S, c) => do + let thm ← `((Tensors.Species.contr_formTensor_formTensor (S := $(← exprToSyntax S)) + $(← exprToSyntax c)).symm) + rewriteSubterm d thm (some lhs) + regroupContractions + | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do + insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) (skipForms := true) + | _ => throwError "tensor_regroup: more than one factor is left over against forms" + +/-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to +deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested +product. Two forms on one side with a delta or nothing left over on the other are matched by +`matchFormPair`. A delta on one side with no counterpart on the other is inserted on the other side +at each slot of its color in turn, since contracting with a delta is a relabeling. The factors are +then matched by `matchFactors`. -/ +private partial def regroupContractions : TacticM Unit := do + flattenContractions + if (← getGoals).isEmpty then return + let (p, q) ← contractedProducts + let extraL ← leftover (factors p) (factors q) + let extraR ← leftover (factors q) (factors p) + -- The error reported in place of the count of factors when forms are left over on one side. + let mut formErr : Option MessageData := none + for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do + try matchFormPair lhs ds gs; return catch _ => + if 2 ≤ gs.length && gs.all (formArgs? · |>.isSome) then + formErr := m!"tensor_regroup: the forms left over on one side do not cancel in inverse pairs \ + against the other side{indentD (MessageData.joinSep (gs.map toMessageData) "\n")}" + -- A delta left over on the right is inserted on the left, and conversely. + match extraL.mapM deltaColor?, extraR.mapM deltaColor? with + | some _, some (a :: _) => insertDeltaAnywhere true a + | some (a :: _), some [] => insertDeltaAnywhere false a + | _, _ => try matchFactors catch e => throwError (formErr.getD e.toMessageData) + +end + +/-! + +## E. Sums, signs and scalars + +-/ + +/-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, +contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. +The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a +factor. -/ +private def normalizeSums : TacticM Unit := do + let (ty, _, _) ← goalEq + let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! + evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, + TensorProduct.tmul_add, TensorProduct.add_tmul, TensorProduct.tmul_neg, + TensorProduct.neg_tmul, TensorProduct.tmul_sub, TensorProduct.sub_tmul, + TensorProduct.tmul_smul (R' := $k), ← TensorProduct.smul_tmul' (R' := $k), smul_add, + smul_neg, smul_smul, neg_neg, neg_add, sub_eq_add_neg])) + +/-- The summands of a sum, from left to right. -/ +private partial def summands (e : Expr) : List Expr := + let e := e.consumeMData + if e.isAppOfArity ``HAdd.hAdd 6 then summands e.appFn!.appArg! ++ summands e.appArg! else [e] + +/-- Rewrites the form `F = S.formTensor c` at the tensor leaf `k` of the goal by its swap, +`g^μν = -g^νμ` for an antisymmetric form (`Species.formTensor_antisymm`), if `antisym`, and +`g^μν = g^νμ` for a symmetric one (`Species.formTensor_symm`). -/ +private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do + let some (S, c) := formArgs? F | throwError "tensor_regroup: internal error, {F} is not a form" + let (S, c) := (← exprToSyntax S, ← exprToSyntax c) + let thm ← if antisym then + `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c).symm) + else `((Tensors.Species.formTensor_symm (S := $S) $c).symm) + rewriteSubterm F thm (k := some k) + +mutual + +/-- Proves an equation of two summands with the same sign and the same scalar, by regrouping the +contractions they multiply. When exactly one side is negated, one antisymmetric form on either +side is swapped first, as in `ε^αβ = -ε^βα`. When the regrouping fails, one symmetric form is +swapped, as in `η^μν = η^νμ`. At most one form is swapped. -/ +private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := do + let (_, l, r) ← goalEq + let negL := l.isAppOfArity ``Neg.neg 3 + let negR := r.isAppOfArity ``Neg.neg 3 + if negL && negR then + evalTactic (← `(tactic| refine congrArg Neg.neg ?_)) + regroupSummand flipped + else if negL != negR && !flipped then + flipOneForm true + else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then + let a := l.appFn!.appArg! + unless ← isDefEqGoal a r.appFn!.appArg! do + throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" + evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) + regroupSummand flipped + else + try evalTactic (← `(tactic| with_reducible rfl)) catch _ => + let s ← saveState + try regroupContractions catch e => + if flipped then throw e + -- The two sides may differ by the swap of a symmetric form. + s.restore + try flipOneForm false catch _ => s.restore; throw e + +/-- Tries each form among the tensor leaves of the goal for `flipForm antisym`, then moves a new +sign out and regroups. A form of the other symmetry fails at `flipForm`. -/ +private partial def flipOneForm (antisym : Bool) : TacticM Unit := do + let (_, l, r) ← goalEq + let leaves := tensorLeaves l ++ tensorLeaves r + let s ← saveState + let mut lastError : MessageData := m!"tensor_regroup: no form to swap" + for F in (leaves.filter (formArgs? · |>.isSome)).eraseDups do + for k in (List.range leaves.length).filter (leaves[·]! == F) do + s.restore + try flipForm antisym F k catch _ => break + try + normalizeSums + regroupSummand true + return + catch e => lastError := e.toMessageData + s.restore + throwError lastError + +end + +/-- A proof of `e = e'` for a sum `e`, where `e'` replaces the summands of `e` from left to right +by the right sides of the proofs `hs`. The proofs left over are returned. -/ +private partial def sumCongr (e : Expr) (hs : List Expr) : MetaM (Expr × List Expr) := do + let e := e.consumeMData + if e.isAppOfArity ``HAdd.hAdd 6 then + let (p, hs) ← sumCongr e.appFn!.appArg! hs + let (q, hs) ← sumCongr e.appArg! hs + return (← mkCongr (← mkCongrArg e.appFn!.appFn! p) q, hs) + match hs with + | h :: hs => return (h, hs) + | [] => throwError "tensor_regroup: internal error, too few summand proofs" + +/-- The position among `rs`, outside `used`, of a summand equal to `a` of type `ty` by +`regroupSummand`, with the proof of the equation. -/ +private def matchSummand (ty a : Expr) (rs : List Expr) (used : List Nat) : + TacticM (Nat × Expr) := do + let js := (List.range rs.length).filter (!used.contains ·) + try + firstSuccess js m!"" fun j => do + -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. + let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) + setGoals [g.mvarId!] + regroupSummand + unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" + return (j, ← instantiateMVars g) + catch _ => + throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" + +/-- The body of `tensor_regroup`: moves sums, negatives and scalars out, then proves the equation +summand by summand. -/ +private def regroup : TacticM Unit := do + normalizeSums + -- Moving the scalars out may already close the goal. + if (← getGoals).isEmpty then return + let (ty, l, r) ← goalEq + let ls := summands l + let rs := summands r + unless ls.length = rs.length do + throwError "tensor_regroup: the two sides have different numbers of summands" + if ls.length = 1 then + regroupSummand + return + -- Each summand on the left is proved equal to an unused summand on the right in its own goal, + -- and the sums then agree up to order. + let main ← getMainGoal + let mut used : List Nat := [] + let mut proofs : Array Expr := #[] + for a in ls do + let (j, pf) ← matchSummand ty a rs used + used := j :: used + proofs := proofs.push pf + -- The summands are replaced by congruence, not by `rw`: a summand can occur inside another one, + -- as `T` inside its relabeling. + let (pf, _) ← sumCongr l proofs.toList + let some (_, _, l') := (← inferType pf).eq? | throwError "tensor_regroup: internal error" + let rest ← mkFreshExprMVar (← mkEq l' r) + main.assign (← mkEqTrans pf rest) + setGoals [rest.mvarId!] + evalTactic (← `(tactic| first | with_reducible rfl | abel)) + +/-- Whether `e` contains a lowering or raising `S.toDualAt`. -/ +private def hasLowering (e : Expr) : Bool := + (e.find? (·.isConstOf ``Tensors.Species.toDualAt)).isSome + +/-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as +contractions with the form (`Species.toDualAt_eq_contr`) when it fails and the goal has one. When +both fail, the first error is reported if it is about the summands or scalars, and the second +otherwise. -/ +private def regroupWithLowerings : TacticM Unit := do + unless hasLowering (← instantiateMVars (← getMainTarget)) do regroup; return + let s ← saveState + try regroup catch e => + -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They + -- are expanded only when needed, since every expanded form is one more factor to match. + s.restore + evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_contr])) + try regroup catch e' => + -- The expansion changes the factors, not the summands or scalars, so an error about those + -- reads better before it. + let msg ← e.toMessageData.toString + throw <| if msg.contains "summand" || msg.contains "scalars" then e else e' + +/-! + +## F. Chains of lowerings and raisings + +-/ + +/-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, +such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain +is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps +are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side +with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be +`Fin n` with a literal `n`. -/ +elab "tensor_slots" : tactic => do + evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, + Tensors.Species.mapSlots_mapSlots])) + let isMapSlots (e : Expr) := (bundledArg? ``Tensors.Species.mapSlots e).isSome + let (_, l, r) ← goalEq "tensor_slots" + unless isMapSlots l || isMapSlots r do + throwError "tensor_slots: neither side lowers or raises an index" + -- Each side is checked before it is unified, since a failed unification with a side that is a + -- definition, such as a named tensor, unfolds it. + wrapSides "tensor_slots" isMapSlots (← `(Tensors.Species.mapSlots_id_apply _)) + evalTactic (← `(tactic| focus + refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_) + intro x + fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> + simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, + LinearMap.id_apply, Tensors.Species.slotMapsAt_apply_self, + Tensors.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, + Representation.IntertwiningMap.coe_toLinearMap, + Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, + Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual])) + +/-! + +## G. Rewriting a sub-product by an identity + +-/ + +/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of +`P` are numbered in order, the slots of each factor after those of the factors before it. -/ +private structure ReadContraction where + /-- The factors of `P`, from left to right. -/ + factors : List Expr + /-- The slot of `P` at which each factor starts. -/ + offsets : List Nat + /-- The slot of `P` of each free index, in order. -/ + free : List Nat + /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ + pairs : List (Nat × Nat) + +/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in +slot order. -/ +private partial def slotElems (ι : Expr) : MetaM (List Expr) := do + let ι ← whnfR ι + if ι.isAppOfArity ``Fin 1 then + let some n ← (evalNat ι.appArg!).run + | throwError "tensor_regroup: the slot type {ι} is not literal" + (List.range n).mapM fun i => do + mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] + else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] + else if ι.isConstOf ``Empty then return [] + else if ι.isAppOfArity ``Sum 2 then + let (a, b) := (ι.getArg! 0, ι.getArg! 1) + return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ + (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) + else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" + +/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ +private partial def slotNumber : Slots → Expr → MetaM Nat + | .fin _, x => do + let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run + | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + return i + | .unit, _ => return 0 + | .sum a b, x => do + let x ← whnfD x + if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! + else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) + else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + +/-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each +free index and at each slot of each pair. -/ +private def readContraction (e : Expr) : MetaM ReadContraction := do + let some P := bundledArg? ``Tensors.Species.Tensor.contr e + | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" + let τ := e.consumeMData.appFn!.appArg!.appArg! + let some (slots, _, _) ← literalTensorType? (← inferType P) + | throwError "tensor_regroup: the slots or colors of a product are not literal" + let fs := factors P + let sizes ← fs.mapM fun f => do + let some (s, _, _) ← literalTensorType? (← inferType f) + | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" + return s.size + -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. + let args := (← whnfR (← inferType τ)).getAppArgs + let cargs := args[args.size - 1]!.getAppArgs + let (κ, J) := (cargs[1]!, cargs[2]!) + let JJ ← mkAppM ``Sum #[J, J] + let at_ (y : Expr) : MetaM Nat := do + slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) + let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) + let pairs ← (← slotElems J).mapM fun j => do + let half (left : Bool) := do + at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) + #[J, J, j]]) + return (← half true, ← half false) + return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ + +/-- The ways of sending the slots of `A` to slots of `G`, factor by factor, each factor of `A` to an +equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list of +images of the slots of `A`, together with the factors of `G` that are hit. -/ +private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List Nat)) := do + let candidates ← A.factors.mapM fun x => + (List.range G.factors.length).filterM fun j => isDefEqGoal x G.factors[j]! + let isPair (p q : Nat) := G.pairs.contains (p, q) || G.pairs.contains (q, p) + return (matchings candidates []).filterMap fun target => + let φ := (target.zipIdx).flatMap fun (j, i) => + (List.range (A.offsets[i+1]! - A.offsets[i]!)).map (G.offsets[j]! + ·) + if A.pairs.all fun (p, q) => isPair φ[p]! φ[q]! then some (φ, target) else none + +/-- The function `x ↦ {x | … ⊗ g | … ⊗ …}ᵀ`, for `x` of the type of `A`, with its free indices +in the order of `L`, the contraction read as `G`. The factors `g` of `G` not hit by `target` follow +`x`, and every slot of `G` is named after its free index `fₖ` or its pair `pⱼ`. The slots of `x` +are named as the free indices of `A` are sent by `φ`. -/ +private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : List Nat) : + TermElabM Expr := do + let name (s : Nat) : TSyntax `tensorIndex := Unhygienic.run do + let n := match G.free.idxOf? s with + | some k => s!"f{k}" + | none => s!"p{(G.pairs.findIdx? fun (p, q) => p == s || q == s).getD 0}" + let i := mkIdent (Name.mkSimple n) + `(tensorIndex| $i:ident) + let leaf (T : Expr) (ss : List Nat) : TermElabM (TSyntax `tensorIndexExpr) := do + let t ← exprToSyntax T + `(tensorIndexExpr| $t:term | $((ss.map name).toArray)*) + let rest ← ((List.range G.factors.length).filter (!target.contains ·)).mapM fun j => + leaf G.factors[j]! ((List.range (G.offsets[j+1]! - G.offsets[j]!)).map (G.offsets[j]! + ·)) + withLocalDeclD `x (← inferType A) fun x => do + let rhs ← rest.foldlM (init := ← leaf x (Aread.free.map (φ[·]!))) fun a b => + `(tensorIndexExpr| $a ⊗ $b) + -- The equation `{L | f₀ f₁ … = …}ᵀ` relabels its right side to the order of `L`. + let eq ← Term.elabType (← `({ $(← leaf L G.free) = $rhs }ᵀ)) + Term.synthesizeSyntheticMVarsNoPostponing + let some (_, _, M) := (← instantiateMVars eq).eq? + | throwError "tensor_regroup: internal error, not an equation" + mkLambdaFVars #[x] M + +/-- Proves the goal `ty` by `tac`, aside from the main goal. -/ +private def proveAside (ty : Expr) (tac : TacticM Unit) : TacticM Expr := do + let g ← mkFreshExprMVar ty + let gs ← getGoals + setGoals [g.mvarId!] + try + tac + unless (← getGoals).isEmpty do throwError "tensor_regroup: internal error, a goal is left" + finally setGoals gs + instantiateMVars g + +/-- The side `A`, flattened to one contraction of a left-nested product, with lowerings and +raisings first written as contractions with the form if `expand`. -/ +private def flattenTerm (A : Expr) (expand : Bool) : TacticM Expr := do + let g ← mkFreshExprMVar (← mkEq A (← mkAppOptM ``Zero.zero #[← inferType A, none])) + let gs ← getGoals + setGoals [g.mvarId!] + try + if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + flattenContractions + return (← goalEq).2.1 + finally setGoals gs + +/-- The body of `tensor_regroup using e₁, …, eₙ`. For `e₁ : A = B`, the left side of the goal is +regrouped by `regroupWithLowerings` into a product `M A` of `A` with the factors not in `A`, which +`congrArg M e₁` rewrites to `M B`, and the remaining identities are used in the same way. Each way +of finding the factors of `A` among the factors of the goal is tried in turn, first as written and +then with lowerings and raisings written as contractions with the form, first on the left side of +the goal and then on the right. -/ +private partial def regroupUsing : List Term → TacticM Unit + | [] => regroupWithLowerings + | e :: es => do + let h ← instantiateMVars (← Tactic.elabTerm e none) + let some (_, A, B) := (← instantiateMVars (← inferType h)).eq? + | throwError "tensor_regroup: {e} is not an equation" + let mut err := m!"tensor_regroup: no factors of either side are contracted as in the left \ + side of {e}" + let expands := if hasLowering (← instantiateMVars (← getMainTarget)) || hasLowering A then + [false, true] else [false] + -- The right side is searched by first turning the goal around. + for flip in [false, true] do + for expand in expands do + let s ← saveState + -- A side that is not a contraction has no factors to search. + let read? ← try + if flip then replaceMainGoal [← (← getMainGoal).applySymm] + if expand then + evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + flattenContractions + let (_, L, _) ← goalEq + pure (some (L, ← readContraction L, ← readContraction (← flattenTerm A expand))) + catch _ => pure none + if let some (L, G, Aread) := read? then + for (φ, target) in (← embeddings Aread G).take maxMatchings do + let s' ← saveState + try + -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. + let M ← regroupedFun L A Aread G φ target + let h₁ ← proveAside (← mkEq L (M.beta #[A])) regroupWithLowerings + let pf ← mkEqTrans h₁ (← mkCongrArg M h) + let (_, _, R) ← goalEq + let rest ← mkFreshExprSyntheticOpaqueMVar (← mkEq (M.beta #[B]) R) + (← getMainGoal).assign (← mkEqTrans pf rest) + replaceMainGoal [rest.mvarId!] + regroupUsing es + return + catch ex => s'.restore; err := ex.toMessageData + s.restore + throwError err + +/-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of +the same factors, grouped or ordered differently, such as +`{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, +negatives and scalars are first moved out of every product, contraction and relabeling. Each +summand on the left is then matched with a summand on the right with the same sign and scalar, +whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a +factor appears more than once, each way of matching the equal factors is tried in turn, so the +number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 +are tried. + +Beyond regrouping, it uses these identities: +- a delta on one side with no counterpart on the other is inserted on the other side at a slot of + its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on + the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` + (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering + `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta + first; +- when the signs of two summands differ, one antisymmetric form is swapped, `ε^αβ = -ε^βα` + (`Species.formTensor_antisymm`), and when the regrouping fails, one symmetric form is swapped, + `η^μν = η^νμ` (`Species.formTensor_symm`); +- when no matching is found, lowerings and raisings are expanded into contractions with the form + (`Species.toDualAt_eq_contr`) and the matching is tried again. + +A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs +(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional +equality, as `2` and `2`. + +`tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, +the factors of `Aᵢ` are found among the factors of the left side of the goal, or else of the right +side, contracted along the same pairs, and that side is regrouped as `Aᵢ` times the other factors +and rewritten to `Bᵢ`. The equations must be closed terms. -/ +syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic + +elab_rules : tactic + | `(tactic| tensor_regroup) => regroupWithLowerings + | `(tactic| tensor_regroup using $es,*) => regroupUsing es.getElems.toList + +end Tensors.Notation + +end diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index 96bce2de29..2ef748a083 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.ElabTactics /-! diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 6efcea5f11..579d13ee8d 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.Elab +public import Physlib.Mathematics.Tensors.ElabTactics public import Physlib.Relativity.Tensors.RealTensor.CoVector.Representation public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct public import Physlib.Relativity.Tensors.RealTensor.Vector.Representation From cc99951a6a7f7c815690389bea003bd2ddaa52d3 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Sun, 8 Nov 2026 18:53:52 +0000 Subject: [PATCH 084/242] style(Tensors): ElabExamples drops a double empty line Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/ElabExamples.lean | 1 - 1 file changed, 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index c2de9529c3..7e629d38e6 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -45,7 +45,6 @@ variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) - /-! ## A. Products, sums and contractions From 0ac977fedffd1e13fc270f453c1e971c54fac3bf Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 20:48:43 +0000 Subject: [PATCH 085/242] doc(Tensors): Species module doc says why the law invariant is stated at every color Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 5220b5bf20..15516854b3 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -31,8 +31,9 @@ structure, not a field, so that a conjugate species can give conjugate kinds the The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, -which makes `V c.dual` the dual of `V c`. The law is stated at every color for a uniform interface. -At a lower color it follows from the upper one, and `invariant_of_up` gives it so. +which makes `V c.dual` the dual of `V c`. The law is stated at every color, so that lemmas about a +general color `c` use it directly. At a lower color it is the law at the upper color with the +factors swapped, and `invariant_of_up` proves it so. The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in `V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction From df3e007e004c0b78550291e315e6a5e64710d0c8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 20:56:59 +0000 Subject: [PATCH 086/242] feat(Tensors): Basis.curry_coordPairing, Basis.isPerfPair_coordPairing and Species.isPerfPair_contr Co-authored-by: Claude Opus 5.5 --- .../Mathematics/ForMathlib/Contraction.lean | 21 +++++++++++++++++++ Physlib/Mathematics/Tensors/Species.lean | 13 +++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index bb482acf76..a2e72dcb25 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -7,6 +7,7 @@ module public import Mathlib.LinearAlgebra.Contraction public import Mathlib.LinearAlgebra.Matrix.SesquilinearForm +public import Mathlib.LinearAlgebra.PerfectPairing.Basic /-! @@ -42,6 +43,8 @@ has components `z_μ^A`, where `A` labels a basis of `U`, and reads as the map ` `∑_I (b x)_I (b' y)_I` of two bases with the same labels, and the delta `∑_I b_I ⊗ b'_I`. - `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and `Module.Basis.eq_coordPairing` : it is the only such pairing. +- `Module.Basis.isPerfPair_coordPairing` : the coordinate pairing is a perfect pairing, through + `Module.Basis.curry_coordPairing` : it is Mathlib's `toDualEquiv` of the second basis. - `Module.Basis.lTensor_pairingToLinearMap_coevaluation` : a tensor is recovered from its linear map through the delta. - `Module.Basis.pairingToLinearMap_coordPairing_coevaluation` : the delta read through the @@ -157,6 +160,24 @@ lemma coordPairing_basis_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R b.coordPairing b' (b I ⊗ₜ[R] y) = b'.equivFun y I := by rw [coordPairing_comm, coordPairing_tmul_basis] +/-- The coordinate pairing, curried, sends `x` to the dual vector `b'.toDual` of the vector with +the same coordinates in `b'`. -/ +lemma curry_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + TensorProduct.curry (b.coordPairing b') = + ((b.equiv b' (Equiv.refl ι)).trans b'.toDualEquiv).toLinearMap := + b.ext fun I => b'.ext fun J => by + simp [coordPairing_basis_basis, Module.Basis.toDualEquiv_apply, Module.Basis.toDual_apply] + +/-- The coordinate pairing of two bases with the same labels is a perfect pairing. -/ +instance isPerfPair_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + (TensorProduct.curry (b.coordPairing b')).IsPerfPair where + bijective_left := by rw [curry_coordPairing]; exact LinearEquiv.bijective _ + bijective_right := by + have : (TensorProduct.curry (b.coordPairing b')).flip = + TensorProduct.curry (b'.coordPairing b) := + LinearMap.ext₂ fun y x => coordPairing_comm b b' x y + rw [this, curry_coordPairing]; exact LinearEquiv.bijective _ + /-- The delta `∑_I b_I ⊗ b'_I ∈ M ⊗ N` of two bases with the same labels. -/ noncomputable def coevaluation (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N := ∑ I, b I ⊗ₜ[R] b' I diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 15516854b3..828aa8f63f 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -31,9 +31,9 @@ structure, not a field, so that a conjugate species can give conjugate kinds the The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, -which makes `V c.dual` the dual of `V c`. The law is stated at every color, so that lemmas about a -general color `c` use it directly. At a lower color it is the law at the upper color with the -factors swapped, and `invariant_of_up` proves it so. +which makes `V c.dual` the dual of `V c` (`isPerfPair_contr`). The law is stated at every color, +so that lemmas about a general color `c` use it directly. At a lower color it is the law at the +upper color with the factors swapped, and `invariant_of_up` proves it so. The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in `V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction @@ -51,6 +51,8 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, `Species.contr_basis_tmul` reads off a component, and `Species.ext_contr` proves that the contraction is nondegenerate. +- `Species.isPerfPair_contr` : the contraction is a perfect pairing, so `V c.dual` is the dual of + `V c`. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. - `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and @@ -177,6 +179,11 @@ lemma ext_contr {c : Color K} {α α' : S.V c.dual} (S.basis c.dual).repr.injective <| Finsupp.ext fun μ => by simpa [contr_basis_tmul] using h (S.basis c μ) +/-- The contraction is a perfect pairing, so `V c.dual` is the dual of `V c`. -/ +instance isPerfPair_contr (c : Color K) : + (TensorProduct.curry (S.contr c).toLinearMap).IsPerfPair := + Module.Basis.isPerfPair_coordPairing _ _ + /-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ lemma contr_invariant (c : Color K) (g : G) (x : S.V c) (y : S.V c.dual) : S.contr c (S.rep c g x ⊗ₜ[k] S.rep c.dual g y) = S.contr c (x ⊗ₜ[k] y) := From a7e953e898c403a555dd7c0dc09de8a0ea8917c0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:05:46 +0000 Subject: [PATCH 087/242] doc(Tensors): contr_basis index labels and the proof comment of delta_invariant Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 828aa8f63f..456f4cd74d 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -150,7 +150,7 @@ noncomputable def contr (c : Color K) : ⟨(S.basis c).coordPairing (S.basis c.dual), fun g => TensorProduct.ext' fun x y => S.invariant c g x y⟩ -/-- The bases of a color and its dual are dual, `e^μ(e_ν) = δ^μ_ν`. -/ +/-- The bases of a color and its dual are dual, `e^ν(e_μ) = δ_μ^ν`. -/ lemma contr_basis (c : Color K) (μ ν : basisIdx c.kind) : S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) = if μ = ν then 1 else 0 := Module.Basis.coordPairing_basis_basis _ _ _ _ @@ -265,7 +265,7 @@ lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U /-- The delta is invariant, `Λ δ = δ`. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- Both act as the identity: `(Λδ)(Λx) = Λx` by the snake law. + -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `snake`. apply S.toLin.injective ext x simpa [snake] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) From 40cbc45b11c31a35f31891e663017baaeb08821e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:09:42 +0000 Subject: [PATCH 088/242] feat(Tensors): Species.dualEquiv, V c.dual as the dual representation of V c Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 456f4cd74d..aa7cca72b0 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -31,7 +31,7 @@ structure, not a field, so that a conjugate species can give conjugate kinds the The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, -which makes `V c.dual` the dual of `V c` (`isPerfPair_contr`). The law is stated at every color, +which makes `V c.dual` the dual of `V c` (`dualEquiv`). The law is stated at every color, so that lemmas about a general color `c` use it directly. At a lower color it is the law at the upper color with the factors swapped, and `invariant_of_up` proves it so. @@ -51,8 +51,8 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, `Species.contr_basis_tmul` reads off a component, and `Species.ext_contr` proves that the contraction is nondegenerate. -- `Species.isPerfPair_contr` : the contraction is a perfect pairing, so `V c.dual` is the dual of - `V c`. +- `Species.isPerfPair_contr` : the contraction is a perfect pairing, and `Species.dualEquiv` + identifies `V c.dual` with the dual representation of `V c`. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. - `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and @@ -194,6 +194,18 @@ lemma contr_tmul_rep (c : Color K) (g : G) (u : S.V c) (α : S.V c.dual) : S.contr c (u ⊗ₜ S.rep c.dual g α) = S.contr c (S.rep c g⁻¹ u ⊗ₜ α) := by simpa using S.contr_invariant c g (S.rep c g⁻¹ u) α +/-- The space of `c.dual` is the dual representation of the space of `c`, a vector `α` read as the +linear form `u ↦ u^μ α_μ`. -/ +noncomputable def dualEquiv (c : Color K) : (S.rep c.dual).Equiv (S.rep c).dual := + .mk (TensorProduct.curry (S.contr c).toLinearMap).flip.toPerfPair fun g => + LinearMap.ext fun α => LinearMap.ext fun u => S.contr_tmul_rep c g u α + +/-- `dualEquiv c α` is the linear form `u ↦ u^μ α_μ`. -/ +@[simp] +lemma dualEquiv_apply (c : Color K) (α : S.V c.dual) (u : S.V c) : + S.dualEquiv c α u = S.contr c (u ⊗ₜ α) := + rfl + omit S in /-- The law `invariant`, from an invariant pairing `p` for which the bases are dual. It is how a species is built from a pairing that is known to be invariant. -/ From 1269399c1a7e28b26e72f710e3bad2f7423d0ae0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:18:21 +0000 Subject: [PATCH 089/242] refactor(Tensors): Species.toLin from Mathlib's dualTensorHomEquivOfBasis; pairingToLinearMap and Basis.coevaluation removed Co-authored-by: Claude Opus 5.5 --- .../Mathematics/ForMathlib/Contraction.lean | 114 ++---------------- Physlib/Mathematics/Tensors/Species.lean | 24 ++-- 2 files changed, 22 insertions(+), 116 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index a2e72dcb25..6d171c3b34 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -5,55 +5,36 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Contraction public import Mathlib.LinearAlgebra.Matrix.SesquilinearForm public import Mathlib.LinearAlgebra.PerfectPairing.Basic /-! -# A tensor read as a linear map through a pairing +# The coordinate pairing of two bases -A pairing `p : V ⊗ W → R` reads a tensor `z` in `W ⊗ U` as a linear map from `V` to `U`. +Two bases with the same labels pair two modules by their coordinates. ## i. Overview -Here `V`, `W` and `U` are spaces, and `x`, `w`, `y` and `z` are elements. The pairing `p` contracts -an element `x` of `V` with an element `w` of `W` to the scalar `p (x ⊗ w)`. A tensor `z` in `W ⊗ U` -reads as the linear map from `V` to `U` that contracts its input with the first factor of `z` and -keeps the second. On a pure tensor this is `x ↦ p (x ⊗ w) • y` for `z = w ⊗ y`. +Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by their +coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`). The two bases are dual for this +pairing, and it is a perfect pairing in Mathlib's sense, so `N` is the dual of `M`. -For Lorentz tensors take `V = ℝ⁴` the vectors `x^μ`, `W = (ℝ⁴)*` the covectors `w_μ`, and -`p (x ⊗ w) = x^μ w_μ`. In the standard basis of `ℝ⁴` and its dual basis, a tensor `z` in `W ⊗ U` -has components `z_μ^A`, where `A` labels a basis of `U`, and reads as the map `x^μ ↦ x^μ z_μ^A`. - -- With `U = V` and `z` the Kronecker delta `δ_μ^ν`, the map is `x^μ ↦ x^μ δ_μ^ν = x^ν`, the - identity. -- With `U = W` and `z` the Minkowski metric `η_μν`, the map is `x^μ ↦ x^μ η_μν = x_ν`, lowering - the index. +For Lorentz tensors take `M = ℝ⁴` the vectors `x^μ` with the standard basis, and `N` the covectors +`w_μ` with the dual basis. The coordinate pairing is the contraction `x^μ w_μ`. ## ii. Key results -- `TensorProduct.pairingToLinearMap` : a tensor in `W ⊗ U` as a linear map `V → U` through `p`. -- `TensorProduct.pairingToLinearMap_tmul` : its value on a pure tensor `w ⊗ y`. -- `TensorProduct.pairingToLinearMap_lTensor` : a linear map applied to the second factor of `z` is - applied to the values. -- `TensorProduct.pairingToLinearMap_apply` : the value at `x` as `x ⊗ z` with its first two factors - contracted. -- `Module.Basis.coordPairing`, `Module.Basis.coevaluation` : the coordinate pairing - `∑_I (b x)_I (b' y)_I` of two bases with the same labels, and the delta `∑_I b_I ⊗ b'_I`. +- `Module.Basis.coordPairing` : the coordinate pairing `∑_I (b x)_I (b' y)_I` of two bases with + the same labels. - `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and `Module.Basis.eq_coordPairing` : it is the only such pairing. - `Module.Basis.isPerfPair_coordPairing` : the coordinate pairing is a perfect pairing, through `Module.Basis.curry_coordPairing` : it is Mathlib's `toDualEquiv` of the second basis. -- `Module.Basis.lTensor_pairingToLinearMap_coevaluation` : a tensor is recovered from its linear - map through the delta. -- `Module.Basis.pairingToLinearMap_coordPairing_coevaluation` : the delta read through the - coordinate pairing is the identity. ## iii. Table of contents -- A. Reading a tensor as a linear map -- B. The coordinate pairing of two bases +- A. The coordinate pairing of two bases ## iv. References @@ -63,60 +44,13 @@ There are no known references for the material in this module. @[expose] public section -namespace TensorProduct - -variable {R V W U U' : Type*} [CommSemiring R] [AddCommMonoid V] [Module R V] [AddCommMonoid W] - [Module R W] [AddCommMonoid U] [Module R U] [AddCommMonoid U'] [Module R U'] - -/-! - -## A. Reading a tensor as a linear map - --/ - -/-- The tensor `z` in `W ⊗ U` read through the pairing `p` as a linear map from `V` to `U`. The -pairing takes the input on the left, `p (x ⊗ w)`, and contracts it with the first factor of `z`. -It is Mathlib's `dualTensorHom R V U` with each `w` read as the form -`x ↦ p (x ⊗ w)`. -/ -noncomputable def pairingToLinearMap (p : V ⊗[R] W →ₗ[R] R) : W ⊗[R] U →ₗ[R] V →ₗ[R] U := - dualTensorHom R V U ∘ₗ (TensorProduct.curry p).flip.rTensor U - -/-- On a pure tensor, `(w ⊗ y)(x) = p (x ⊗ w) • y`. -/ -@[simp] -lemma pairingToLinearMap_tmul (p : V ⊗[R] W →ₗ[R] R) (w : W) (y : U) (x : V) : - pairingToLinearMap p (w ⊗ₜ y) x = p (x ⊗ₜ w) • y := rfl - -/-- Applying a linear map `f : U → U'` to the second factor of `z` composes the map of `z` with -`f`. -/ -lemma pairingToLinearMap_lTensor (p : V ⊗[R] W →ₗ[R] R) (f : U →ₗ[R] U') (z : W ⊗[R] U) : - pairingToLinearMap p (f.lTensor W z) = f ∘ₗ pairingToLinearMap p z := by - induction z using TensorProduct.inductionOn with - | tmul w y => ext x; simp - | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] - -/-- The value at `x` is `x ⊗ z` with its first two factors contracted by `p`, -`lid (p.rTensor U (assoc.symm (x ⊗ z)))`. This is the form of a contraction built from Mathlib's -associator `TensorProduct.assoc` and unitor `TensorProduct.lid`. -/ -lemma pairingToLinearMap_apply (p : V ⊗[R] W →ₗ[R] R) (z : W ⊗[R] U) (x : V) : - pairingToLinearMap p z x = - TensorProduct.lid R U (p.rTensor U ((TensorProduct.assoc R V W U).symm (x ⊗ₜ z))) := by - induction z using TensorProduct.inductionOn with - | tmul w y => rfl - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] - -end TensorProduct - namespace Module.Basis open TensorProduct /-! -## B. The coordinate pairing of two bases - -Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by -their coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`), with delta `∑_I b_I ⊗ b'_I` -(`coevaluation`). +## A. The coordinate pairing of two bases -/ @@ -178,30 +112,4 @@ instance isPerfPair_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι LinearMap.ext₂ fun y x => coordPairing_comm b b' x y rw [this, curry_coordPairing]; exact LinearEquiv.bijective _ -/-- The delta `∑_I b_I ⊗ b'_I ∈ M ⊗ N` of two bases with the same labels. -/ -noncomputable def coevaluation (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N := - ∑ I, b I ⊗ₜ[R] b' I - -/-- The snake law: contracting `x ∈ M` with the first factor of `∑_I b'_I ⊗ b_I` gives `x`. -/ -lemma pairingToLinearMap_coordPairing_coevaluation (b : Module.Basis ι R M) - (b' : Module.Basis ι R N) : - pairingToLinearMap (b.coordPairing b') (b'.coevaluation b) = LinearMap.id := by - refine LinearMap.ext fun x => ?_ - rw [coevaluation, map_sum, LinearMap.sum_apply] - simp only [pairingToLinearMap_tmul, coordPairing_tmul_basis, LinearMap.id_apply] - exact b.sum_equivFun x - -/-- The other half of the snake law: a tensor `z ∈ N ⊗ U` is recovered from its linear map -`x ↦ z x` by applying the map to the `M` factor of `∑_I b'_I ⊗ b_I`, `z = b'_I ⊗ z(b_I)`. -/ -lemma lTensor_pairingToLinearMap_coevaluation {U : Type*} [AddCommMonoid U] [Module R U] - (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (z : N ⊗[R] U) : - (pairingToLinearMap (b.coordPairing b') z).lTensor N (b'.coevaluation b) = z := by - induction z using TensorProduct.inductionOn with - | add z₁ z₂ h₁ h₂ => simp only [map_add, LinearMap.lTensor_add, LinearMap.add_apply, h₁, h₂] - | tmul w y => - simp only [coevaluation, map_sum, LinearMap.lTensor_tmul, pairingToLinearMap_tmul, - TensorProduct.tmul_smul] - conv_rhs => rw [← b'.sum_repr w, TensorProduct.sum_tmul] - simp only [coordPairing_basis_tmul, Module.Basis.equivFun_apply, TensorProduct.smul_tmul'] - end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index aa7cca72b0..54afb247ab 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Mathlib.LinearAlgebra.Contraction public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color public import Mathlib.RepresentationTheory.Intertwining @@ -227,26 +228,21 @@ lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommG /-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of the dual color is first. -/ noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := - (S.basis c.dual).coevaluation (S.basis c) + ∑ μ, S.basis c.dual μ ⊗ₜ S.basis c μ /-- The delta of `c` is the delta of `c.dual ∈ V c ⊗ V c.dual` with its factors swapped. -/ lemma delta_eq_comm_delta_dual (c : Color K) : S.delta c = TensorProduct.comm k _ _ (S.delta c.dual) := by - simp only [delta, Module.Basis.coevaluation, map_sum, TensorProduct.comm_tmul] + simp only [delta, map_sum, TensorProduct.comm_tmul] /-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. Every linear map arises so. -/ noncomputable def toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := - -- The inverse sends `f` to `(id ⊗ f) δ`. Both composites are the identity, by the two halves of - -- the snake law for dual bases. - LinearEquiv.ofLinearMap (pairingToLinearMap (S.contr a.dual).toLinearMap) - ((LinearMap.lTensorHom (S.V a.dual.dual)).flip (S.delta a.dual)) - (LinearMap.ext fun f => by - simp [pairingToLinearMap_lTensor, delta, contr, - Module.Basis.pairingToLinearMap_coordPairing_coevaluation]) - (LinearMap.ext fun z => Module.Basis.lTensor_pairingToLinearMap_coevaluation _ _ z) + -- Mathlib's `dualTensorHomEquivOfBasis`, after reading `V a` as the dual of `V a.dual`. + (TensorProduct.congr (S.dualEquiv a.dual).toLinearEquiv (LinearEquiv.refl k U)).trans + (dualTensorHomEquivOfBasis (S.basis a.dual)) /-- A pure tensor `x ⊗ y` acts as `v ↦ (v^μ x_μ) y`, contracting `v` with its first factor. -/ @[simp] @@ -258,12 +254,14 @@ lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V `(id ⊗ f) z = f ∘ z`. -/ lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) : - S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := - pairingToLinearMap_lTensor _ f z + S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := by + induction z using TensorProduct.inductionOn with + | tmul x y => ext v; simp + | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] /-- The snake law `u^μ δ_μ^ν = u^ν`, the delta read as a linear map is the identity. -/ lemma snake (c : Color K) : S.toLin (S.delta c) = LinearMap.id := - Module.Basis.pairingToLinearMap_coordPairing_coevaluation _ _ + LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] /-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ From 6efc8e913c72f027227210319143ffa3bd8a68bd Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:22:47 +0000 Subject: [PATCH 090/242] chore(Tensors): Species drops a transitive import Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 1 - 1 file changed, 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 54afb247ab..11927bff41 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Contraction public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color public import Mathlib.RepresentationTheory.Intertwining From 94db1bc5ac8810a74dba44291b55772a14badd52 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:33:14 +0000 Subject: [PATCH 091/242] refactor(Tensors): Species.ext_contr from dualEquiv; Species.contr_basis_tmul and Species.delta_eq_comm_delta_dual removed Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 33 +++++++----------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 11927bff41..b0d7b2c0d1 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -48,15 +48,13 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. law from its upper colors. - `Species.contr` is the contraction `u^μ α_μ`, with `Species.contr_basis`, `Species.contr_tmul_symm` and `Species.contr_invariant`. -- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, - `Species.contr_basis_tmul` reads off a component, and `Species.ext_contr` proves that the - contraction is nondegenerate. +- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, and + `Species.ext_contr` proves that the contraction is nondegenerate. - `Species.isPerfPair_contr` : the contraction is a perfect pairing, and `Species.dualEquiv` identifies `V c.dual` with the dual representation of `V c`. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. -- `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_eq_comm_delta_dual` and - `Species.delta_invariant`. +- `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_invariant`. - `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.snake` proves that the delta is the identity, `Species.toLin_map` that `toLin` commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that `(id ⊗ f) z = f ∘ z`. @@ -166,19 +164,6 @@ lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := Module.Basis.coordPairing_comm _ _ _ _ -/-- Contracting the basis vector `e_μ` with `α` reads off the component `α_μ`. -/ -lemma contr_basis_tmul (c : Color K) (μ : basisIdx c.kind) (α : S.V c.dual) : - S.contr c (S.basis c μ ⊗ₜ α) = (S.basis c.dual).repr α μ := - (S.contr_tmul_symm c _ α).trans (Module.Basis.coordPairing_tmul_basis _ _ _ _) - -variable {S} in -/-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the -same contraction against every vector of `c` are equal. -/ -lemma ext_contr {c : Color K} {α α' : S.V c.dual} - (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := - (S.basis c.dual).repr.injective <| Finsupp.ext fun μ => by - simpa [contr_basis_tmul] using h (S.basis c μ) - /-- The contraction is a perfect pairing, so `V c.dual` is the dual of `V c`. -/ instance isPerfPair_contr (c : Color K) : (TensorProduct.curry (S.contr c).toLinearMap).IsPerfPair := @@ -206,6 +191,13 @@ lemma dualEquiv_apply (c : Color K) (α : S.V c.dual) (u : S.V c) : S.dualEquiv c α u = S.contr c (u ⊗ₜ α) := rfl +variable {S} in +/-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the +same contraction against every vector of `c` are equal. -/ +lemma ext_contr {c : Color K} {α α' : S.V c.dual} + (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := + (S.dualEquiv c).toLinearEquiv.injective (LinearMap.ext h) + omit S in /-- The law `invariant`, from an invariant pairing `p` for which the bases are dual. It is how a species is built from a pairing that is known to be invariant. -/ @@ -229,11 +221,6 @@ the dual color is first. -/ noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := ∑ μ, S.basis c.dual μ ⊗ₜ S.basis c μ -/-- The delta of `c` is the delta of `c.dual ∈ V c ⊗ V c.dual` with its factors swapped. -/ -lemma delta_eq_comm_delta_dual (c : Color K) : - S.delta c = TensorProduct.comm k _ _ (S.delta c.dual) := by - simp only [delta, map_sum, TensorProduct.comm_tmul] - /-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. Every linear map arises so. -/ From 1f9218205bc882caad4d4c9f377fec732482f79b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:49:41 +0000 Subject: [PATCH 092/242] doc(Tensors): Color and Species module docs corrected, with section overviews Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 18 +++++---- Physlib/Mathematics/Tensors/Species.lean | 47 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 877a2b4d5d..a7ee81d440 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -18,17 +18,16 @@ variance. A color is the type of an index, made of a kind `κ : K` and a variance, `up` or `down`. A species (`Species.lean`) assigns a vector space to each color. The kind fixes the basis labels, and the variance says whether the index is that of a vector (`up`) or a covector (`down`). Lorentz -tensors have one kind, `vec`. The vector `v^μ` has color `⟨vec, up⟩` with space `ℝ⁴`, and the -covector `w_μ` has color `⟨vec, down⟩` with space `(ℝ⁴)*`. +tensors have one kind, `vec`. The vector `v^μ` has color `⟨vec, up⟩` and the covector `w_μ` has +color `⟨vec, down⟩`. The slots of a tensor are labelled by colors, not by spaces, so that matching a slot with its dual is a decidable equality of labels, not an equality of types. The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, and a contraction such as `v^μ w_μ` pairs a slot of color `c` with a slot of color `c.dual`. -`Variance` is two booleans, and `dual` swaps them. Swapping twice gives back the same structure, -so `c.dual.dual = c` holds by `rfl` for a variable color `c`. `Color K` is a structure, rather -than the product `K × Variance`, so that `c.dual` works as dot notation. +`Color K` is a structure, rather than the product `K × Variance`, so that `c.dual` works as dot +notation. ## ii. Key results @@ -44,7 +43,7 @@ than the product `K × Variance`, so that `c.dual` works as dot notation. ## iv. References -None. +There are no known references for the material in this module. -/ @@ -56,6 +55,9 @@ namespace Tensors ## A. Up and down +`Variance` is two booleans rather than an inductive with two constructors. `dual` swaps them, so +`c.dual.dual` is `c` by `rfl` for a variable color `c`. With an inductive this needs a case split. + -/ /-- The variance of an index, up (contravariant, `v^μ`) or down (covariant, `w_μ`). -/ @@ -78,8 +80,8 @@ abbrev up : Variance := ⟨true, false, by decide⟩ @[match_pattern] abbrev down : Variance := ⟨false, true, by decide⟩ -/-- The dual variance, exchanging up and down. It is an abbrev, since the species laws rewrite with -`c.dual.dual = c` at reducible transparency. -/ +/-- The dual variance, exchanging up and down. It is an abbrev, so that `simp` and `rw` see +`c.dual.dual` as `c`. -/ abbrev dual (v : Variance) : Variance := ⟨v.isDown, v.isUp, v.ne.symm⟩ end Variance diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index b0d7b2c0d1..8247d6f98a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -22,23 +22,17 @@ representation `S.rep c` of `G` and a basis `S.basis c`. The data is indexed by one definition covers both variances. The contraction `S.contr c : V c ⊗ V c.dual → k` is `u^μ α_μ` for `c = ⟨vec, up⟩` and `α_μ u^μ` for `c = ⟨vec, down⟩`. -The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an index of that kind -runs over. For Lorentz tensors `basisIdx vec = Fin 1 ⊕ Fin d`, one time label and `d` space -labels, and the number of labels is the dimension. The labels depend on the kind alone, so an -index and its dual share them, as `μ` in `v^μ` and `α_μ`. `basisIdx` is a parameter of the -structure, not a field, so that a conjugate species can give conjugate kinds the same labels. - The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that -existing types such as `CoVector d` serve as covectors. The one law of a species (`invariant`) says -that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` is invariant, -which makes `V c.dual` the dual of `V c` (`dualEquiv`). The law is stated at every color, -so that lemmas about a general color `c` use it directly. At a lower color it is the law at the -upper color with the factors swapped, and `invariant_of_up` proves it so. +existing types such as `CoVector d` serve as covectors. The coordinate pairing +`u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` makes `V c.dual` the dual of `V c`. The +one law of a species (`invariant`) says that this pairing is invariant, which makes `V c.dual` the +dual representation (`dualEquiv`). The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in `V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction -is symmetric (`contr_tmul_symm`). A tensor `z` in `V a ⊗ U` is read as the linear map -`S.toLin z : V a.dual → U` that contracts a vector with the first factor of `z`, and the delta is +is symmetric (`contr_tmul_symm`). A tensor `z` in `V a ⊗ U`, for a color `a` and any module `U`, +is read as the linear map `S.toLin z : V a.dual → U` that contracts a vector with the first factor +of `z`, and the delta is then the identity, `u^μ δ_μ^ν = u^ν` (`snake`). Indices name slots, as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. @@ -75,7 +69,7 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## iv. References -None. +There are no known references for the material in this module. -/ @@ -89,6 +83,12 @@ namespace Tensors ## A. Species +The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an index of that kind +runs over, as `Fin 1 ⊕ Fin d` for a Lorentz index. A color and its dual share labels, as `μ` in +`v^μ` and `α_μ`. `basisIdx` is a parameter, not a field, so that a conjugate species can give +conjugate kinds the same labels. The law is stated at every color, so that lemmas about a general +color `c` use it directly. `invariant_of_up` proves it from the upper colors. + -/ /-- A tensor species, which gives each color a representation with a basis such that the @@ -137,6 +137,12 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, ## B. The contraction +The contraction is defined as the coordinate pairing of the bases, not stored as data. The bases +of `c` and `c.dual` are then dual by construction, and components need no dual-basis matching. Its +invariance is the law of the species. `dualEquiv` identifies `V c.dual` with Mathlib's dual +representation of `V c`, and `invariant_of_contr` builds the law from a pairing known to be +invariant. + -/ variable (S : Species k G K basisIdx) @@ -214,6 +220,12 @@ lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommG ## C. The delta, and tensors of two factors as linear maps +A tensor in `V a ⊗ U`, for a color `a` and any module `U`, is read as a linear map `V a.dual → U` +(`toLin`), Mathlib's `dualTensorHomEquivOfBasis`. A metric `g_μν` read this way is the map +`v^μ ↦ v^μ g_μν`, and its inverse `g^μν` is the tensor of the inverse map (`Species.inverse`, in a +later file). The delta is the tensor of the identity map (`snake`), so it is invariant +(`delta_invariant`). + -/ /-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of @@ -284,7 +296,8 @@ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) variable {S} -/-- `toLin` in components, `(z v)^ν = v^μ z_μ^ν` for `z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ +/-- `toLin` in components. For a lower color `c` and an upper color `d`, `(z v)^ν = v^μ z_μ^ν` for +`z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : S.V c.dual) (ν : basisIdx d.kind) : (S.basis d).repr (S.toLin z v) ν = @@ -319,6 +332,10 @@ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) ## D. Moving a vector along an equality of colors +A relabeling of slots matches colors only through a proof, `c (σ.symm j) = c' j`, not by +definition. A vector of one color is then read at an equal color through `S.cast`. The lemmas say +that this keeps the group action, the components and the contraction. + -/ variable (S) in From 916f8c82118a1ff4d442aea51c6728705eba0ea1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:58:23 +0000 Subject: [PATCH 093/242] =?UTF-8?q?doc(Tensors):=20Species=20=C2=A7B=20ove?= =?UTF-8?q?rview=20says=20the=20contraction=20is=20not=20a=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 8247d6f98a..c4b546f8a8 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -137,9 +137,9 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, ## B. The contraction -The contraction is defined as the coordinate pairing of the bases, not stored as data. The bases -of `c` and `c.dual` are then dual by construction, and components need no dual-basis matching. Its -invariance is the law of the species. `dualEquiv` identifies `V c.dual` with Mathlib's dual +The contraction is not a field of `Species`. It is defined from the bases, as their coordinate +pairing. The bases of `c` and `c.dual` are then dual by construction, and components need no +dual-basis matching. Its invariance is the law of the species. `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of `V c`, and `invariant_of_contr` builds the law from a pairing known to be invariant. From 75ae8e0605d874a4b721f52c9047414b6e1009ba Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 21:59:15 +0000 Subject: [PATCH 094/242] =?UTF-8?q?style(Tensors):=20Species=20=C2=A7B=20o?= =?UTF-8?q?verview=20reflowed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index c4b546f8a8..5e6e5a6042 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -139,9 +139,9 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, The contraction is not a field of `Species`. It is defined from the bases, as their coordinate pairing. The bases of `c` and `c.dual` are then dual by construction, and components need no -dual-basis matching. Its invariance is the law of the species. `dualEquiv` identifies `V c.dual` with Mathlib's dual -representation of `V c`, and `invariant_of_contr` builds the law from a pairing known to be -invariant. +dual-basis matching. Its invariance is the law of the species. `dualEquiv` identifies `V c.dual` +with Mathlib's dual representation of `V c`, and `invariant_of_contr` builds the law from a pairing +known to be invariant. -/ From 7f42333bfbaae1b54902731b1311b89f4ce017c3 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:05:45 +0000 Subject: [PATCH 095/242] refactor(Tensors): Species.snake renamed Species.toLin_delta Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 4 ++-- Physlib/Mathematics/Tensors/Product.lean | 2 +- Physlib/Mathematics/Tensors/Species.lean | 22 ++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 466207f3b1..53a3976af9 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -470,7 +470,7 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoSlotEquiv _ _)).eq_iff, - ← (S.toLin (a := x)).injective.eq_iff, toLin_lTensor, S.snake x.dual] + ← (S.toLin (a := x)).injective.eq_iff, toLin_lTensor, S.toLin_delta x.dual] /-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is @@ -554,7 +554,7 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by subst ha - rw [deltaTensor, contr_matMulAt, snake] + rw [deltaTensor, contr_matMulAt, toLin_delta] exact LinearMap.congr_fun (mapAt_id i _) t /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 244ef16473..4f50876e8c 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -373,7 +373,7 @@ lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) intro a b have h := basis_repr_toLin c.dual (S.delta c) (S.basis c a) b - rw [S.snake, LinearMap.id_apply, Module.Basis.repr_self] at h + rw [S.toLin_delta, LinearMap.id_apply, Module.Basis.repr_self] at h simp only [Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true] at h rw [Matrix.one_apply, ← h] diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 5e6e5a6042..f9560964e5 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -32,9 +32,8 @@ The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ `V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction is symmetric (`contr_tmul_symm`). A tensor `z` in `V a ⊗ U`, for a color `a` and any module `U`, is read as the linear map `S.toLin z : V a.dual → U` that contracts a vector with the first factor -of `z`, and the delta is -then the identity, `u^μ δ_μ^ν = u^ν` (`snake`). Indices name slots, as in abstract index notation, -so `u^μ α_μ` is `S.contr c (u ⊗ α)`. +of `z`, and the delta is then the identity, `u^μ δ_μ^ν = u^ν` (`toLin_delta`). Indices name slots, +as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## ii. Key results @@ -49,9 +48,10 @@ so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. - `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_invariant`. -- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.snake` proves - that the delta is the identity, `Species.toLin_map` that `toLin` commutes with the group - action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that `(id ⊗ f) z = f ∘ z`. +- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, + `Species.toLin_delta` proves that the delta is the identity, `Species.toLin_map` that `toLin` + commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that + `(id ⊗ f) z = f ∘ z`. - `Species.basis_repr_toLin` gives `toLin` in components, `Species.toMatrix_toLin` its matrix, and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their matrices are. @@ -223,7 +223,7 @@ lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommG A tensor in `V a ⊗ U`, for a color `a` and any module `U`, is read as a linear map `V a.dual → U` (`toLin`), Mathlib's `dualTensorHomEquivOfBasis`. A metric `g_μν` read this way is the map `v^μ ↦ v^μ g_μν`, and its inverse `g^μν` is the tensor of the inverse map (`Species.inverse`, in a -later file). The delta is the tensor of the identity map (`snake`), so it is invariant +later file). The delta is the tensor of the identity map (`toLin_delta`), so it is invariant (`delta_invariant`). -/ @@ -257,8 +257,8 @@ lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [A | tmul x y => ext v; simp | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] -/-- The snake law `u^μ δ_μ^ν = u^ν`, the delta read as a linear map is the identity. -/ -lemma snake (c : Color K) : S.toLin (S.delta c) = LinearMap.id := +/-- The delta read as a linear map is the identity, `u^μ δ_μ^ν = u^ν`. -/ +lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] /-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and @@ -273,10 +273,10 @@ lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U /-- The delta is invariant, `Λ δ = δ`. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `snake`. + -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. apply S.toLin.injective ext x - simpa [snake] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) + simpa [toLin_delta] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) /-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : From bca76c727734cfc33c0ac8e99c0aaae8227613f9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:12:19 +0000 Subject: [PATCH 096/242] refactor(Tensors): Species.toLin and Species.delta moved to Tensors/LinearMap Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + Physlib/Mathematics/Tensors/LinearMap.lean | 181 +++++++++++++++++++++ Physlib/Mathematics/Tensors/Product.lean | 1 + Physlib/Mathematics/Tensors/Species.lean | 132 +-------------- 4 files changed, 188 insertions(+), 127 deletions(-) create mode 100644 Physlib/Mathematics/Tensors/LinearMap.lean diff --git a/Physlib.lean b/Physlib.lean index b4a355f9af..c1b52feb0d 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -210,6 +210,7 @@ public import Physlib.Mathematics.Tensors.ElabExamples public import Physlib.Mathematics.Tensors.ElabTactics public import Physlib.Mathematics.Tensors.HermitianForm public import Physlib.Mathematics.Tensors.Inverse +public import Physlib.Mathematics.Tensors.LinearMap public import Physlib.Mathematics.Tensors.Metric public import Physlib.Mathematics.Tensors.Product public import Physlib.Mathematics.Tensors.Reindexing diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean new file mode 100644 index 0000000000..74b9083fc5 --- /dev/null +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -0,0 +1,181 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Physlib.Mathematics.Tensors.Species +/-! + +# Tensors of two factors as linear maps + +A tensor in `V a ⊗ U` is read as a linear map `V a.dual → U`, and the delta is the identity map. + +## i. Overview + +A tensor `z` in `V a ⊗ U`, for a color `a` of a species `S` and any module `U`, is read as the +linear map `S.toLin z : V a.dual → U` that contracts a vector with the first factor of `z`. On a +pure tensor this is `v ↦ (v · x) y` for `z = x ⊗ y`. Every linear map arises so. `toLin` is +Mathlib's `dualTensorHomEquivOfBasis`, after `Species.dualEquiv` reads `V a` as the dual of +`V a.dual`. + +A metric `g_μν` read this way is the map `v^μ ↦ v^μ g_μν`, and its inverse `g^μν` is the tensor of +the inverse map (`Species.inverse`, in a later file). The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the +tensor of the identity map, `u^μ δ_μ^ν = u^ν`, so it is invariant. + +## ii. Key results + +- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.toLin_map` + proves that it commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` + that `(id ⊗ f) z = f ∘ z`. +- `Species.delta` is the delta `δ_μ^ν`, `Species.toLin_delta` proves that it is the identity map, + and `Species.delta_invariant` that it is invariant. +- `Species.basis_repr_toLin` gives `toLin` in components, `Species.toMatrix_toLin` its matrix, + and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their + matrices are. + +## iii. Table of contents + +- A. Tensors of two factors as linear maps +- B. The delta +- C. Components + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensors + +namespace Species + +variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : Species k G K basisIdx) + +/-! + +## A. Tensors of two factors as linear maps + +-/ + +/-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the +first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. +Every linear map arises so. -/ +noncomputable def toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : + S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := + -- Mathlib's `dualTensorHomEquivOfBasis`, after reading `V a` as the dual of `V a.dual`. + (TensorProduct.congr (S.dualEquiv a.dual).toLinearEquiv (LinearEquiv.refl k U)).trans + (dualTensorHomEquivOfBasis (S.basis a.dual)) + +/-- A pure tensor `x ⊗ y` acts as `v ↦ (v^μ x_μ) y`, contracting `v` with its first factor. -/ +@[simp] +lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V a) (y : U) + (v : S.V a.dual) : S.toLin (x ⊗ₜ y) v = S.contr a.dual (v ⊗ₜ x) • y := + rfl + +/-- Applying a linear map `f` to the second factor of `z` composes the linear map of `z` with `f`, +`(id ⊗ f) z = f ∘ z`. -/ +lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] + [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) : + S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := by + induction z using TensorProduct.inductionOn with + | tmul x y => ext v; simp + | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] + +/-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and +linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ +lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U] + [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : + S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by + induction z using TensorProduct.inductionOn with + | tmul w y => simpa using congrArg (· • f y) (S.contr_invariant a.dual g x w) + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] + +/-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ +lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : + S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = + S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := + LinearMap.ext fun x => by + rw [LinearMap.comp_apply, LinearMap.comp_apply, ← S.toLin_map, Representation.self_inv_apply] + +/-- The linear map of `Λz` is bijective when that of `z` is. -/ +lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) + (h : Function.Bijective (S.toLin z)) (g : G) : + Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by + have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := + (Module.End.isUnit_iff _).1 ((Group.isUnit g).map (S.rep c)) + rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] + exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) + +/-! + +## B. The delta + +-/ + +/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of +the dual color is first. -/ +noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := + ∑ μ, S.basis c.dual μ ⊗ₜ S.basis c μ + +/-- The delta read as a linear map is the identity, `u^μ δ_μ^ν = u^ν`. -/ +lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := + LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] + +/-- The delta is invariant, `Λ δ = δ`. -/ +lemma delta_invariant (c : Color K) (g : G) : + TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by + -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. + apply S.toLin.injective + ext x + simpa [toLin_delta] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) + +/-! + +## C. Components + +-/ + +variable {S} + +/-- `toLin` in components. For a lower color `c` and an upper color `d`, `(z v)^ν = v^μ z_μ^ν` for +`z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ +lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : S.V c.dual) + (ν : basisIdx d.kind) : + (S.basis d).repr (S.toLin z v) ν = + ∑ μ, (S.basis c.dual).repr v μ * ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν) := by + induction z using TensorProduct.inductionOn with + | add x y hx hy => + simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, + Finset.sum_add_distrib] + | tmul x y => + simp [contr_tmul_eq_sum_basis, Finset.sum_mul, mul_assoc, mul_comm ((S.basis d).repr y ν)] + +/-- The matrix of `toLin z` is the transpose of the matrix of components of `z`, +`(z v)^ν = z_μ^ν v^μ`. -/ +lemma toMatrix_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) : + LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin z) = + (Matrix.of fun μ ν => ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν)).transpose := by + ext ν μ + simp [LinearMap.toMatrix_apply, basis_repr_toLin, Finsupp.single_apply] + +/-- `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of `F'` and `F` multiply +to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ +lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) + (F' : S.V c.dual ⊗[k] S.V c.dual) + (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * + Matrix.of (fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr F (μ, ν)) = 1) : + S.toLin F ∘ₗ S.toLin F' = LinearMap.id := by + apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective + rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin, toMatrix_toLin c.dual, + ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] + +end Species + +end Tensors diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 4f50876e8c..4078578a91 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.Tensors.LinearMap public import Physlib.Mathematics.Tensors.Reindexing /-! diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index f9560964e5..d43339f90d 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -28,12 +28,9 @@ existing types such as `CoVector d` serve as covectors. The coordinate pairing one law of a species (`invariant`) says that this pairing is invariant, which makes `V c.dual` the dual representation (`dualEquiv`). -The contraction `S.contr c` is this pairing, and the delta `S.delta c` is `∑ μ, e^μ ⊗ e_μ` in -`V c.dual ⊗ V c`. The bases of `c` and `c.dual` are then dual (`contr_basis`) and the contraction -is symmetric (`contr_tmul_symm`). A tensor `z` in `V a ⊗ U`, for a color `a` and any module `U`, -is read as the linear map `S.toLin z : V a.dual → U` that contracts a vector with the first factor -of `z`, and the delta is then the identity, `u^μ δ_μ^ν = u^ν` (`toLin_delta`). Indices name slots, -as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. +The contraction `S.contr c` is this pairing. The bases of `c` and `c.dual` are then dual +(`contr_basis`) and the contraction is symmetric (`contr_tmul_symm`). Indices name slots, as in +abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## ii. Key results @@ -47,14 +44,6 @@ as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. identifies `V c.dual` with the dual representation of `V c`. - `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the bases are dual. -- `Species.delta` is the delta `δ_μ^ν`, with `Species.delta_invariant`. -- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, - `Species.toLin_delta` proves that the delta is the identity, `Species.toLin_map` that `toLin` - commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` that - `(id ⊗ f) z = f ∘ z`. -- `Species.basis_repr_toLin` gives `toLin` in components, `Species.toMatrix_toLin` its matrix, - and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their - matrices are. - `Species.cast` moves a vector along an equality of colors, `Species.cast_rep` proves that this commutes with the group action, and `Species.basis_repr_cast` that it keeps components. - `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` prove that the @@ -64,8 +53,7 @@ as in abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - A. Species - B. The contraction -- C. The delta, and tensors of two factors as linear maps -- D. Moving a vector along an equality of colors +- C. Moving a vector along an equality of colors ## iv. References @@ -216,121 +204,11 @@ lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommG rw [← b.eq_coordPairing b' p.toLinearMap h] simpa using Representation.IntertwiningMap.isIntertwining _ _ p g (x ⊗ₜ y) -/-! - -## C. The delta, and tensors of two factors as linear maps - -A tensor in `V a ⊗ U`, for a color `a` and any module `U`, is read as a linear map `V a.dual → U` -(`toLin`), Mathlib's `dualTensorHomEquivOfBasis`. A metric `g_μν` read this way is the map -`v^μ ↦ v^μ g_μν`, and its inverse `g^μν` is the tensor of the inverse map (`Species.inverse`, in a -later file). The delta is the tensor of the identity map (`toLin_delta`), so it is invariant -(`delta_invariant`). - --/ - -/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of -the dual color is first. -/ -noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := - ∑ μ, S.basis c.dual μ ⊗ₜ S.basis c μ - -/-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the -first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. -Every linear map arises so. -/ -noncomputable def toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : - S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := - -- Mathlib's `dualTensorHomEquivOfBasis`, after reading `V a` as the dual of `V a.dual`. - (TensorProduct.congr (S.dualEquiv a.dual).toLinearEquiv (LinearEquiv.refl k U)).trans - (dualTensorHomEquivOfBasis (S.basis a.dual)) - -/-- A pure tensor `x ⊗ y` acts as `v ↦ (v^μ x_μ) y`, contracting `v` with its first factor. -/ -@[simp] -lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V a) (y : U) - (v : S.V a.dual) : S.toLin (x ⊗ₜ y) v = S.contr a.dual (v ⊗ₜ x) • y := - rfl - -/-- Applying a linear map `f` to the second factor of `z` composes the linear map of `z` with `f`, -`(id ⊗ f) z = f ∘ z`. -/ -lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] - [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) : - S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := by - induction z using TensorProduct.inductionOn with - | tmul x y => ext v; simp - | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] - -/-- The delta read as a linear map is the identity, `u^μ δ_μ^ν = u^ν`. -/ -lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := - LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] - -/-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and -linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ -lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U] - [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : - S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by - induction z using TensorProduct.inductionOn with - | tmul w y => simpa using congrArg (· • f y) (S.contr_invariant a.dual g x w) - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] - -/-- The delta is invariant, `Λ δ = δ`. -/ -lemma delta_invariant (c : Color K) (g : G) : - TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. - apply S.toLin.injective - ext x - simpa [toLin_delta] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) - -/-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ -lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : - S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = - S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := - LinearMap.ext fun x => by - rw [LinearMap.comp_apply, LinearMap.comp_apply, ← S.toLin_map, Representation.self_inv_apply] - -/-- The linear map of `Λz` is bijective when that of `z` is. -/ -lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) - (h : Function.Bijective (S.toLin z)) (g : G) : - Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by - have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := - (Module.End.isUnit_iff _).1 ((Group.isUnit g).map (S.rep c)) - rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] - exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) - variable {S} -/-- `toLin` in components. For a lower color `c` and an upper color `d`, `(z v)^ν = v^μ z_μ^ν` for -`z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ -lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : S.V c.dual) - (ν : basisIdx d.kind) : - (S.basis d).repr (S.toLin z v) ν = - ∑ μ, (S.basis c.dual).repr v μ * ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν) := by - induction z using TensorProduct.inductionOn with - | add x y hx hy => - simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, - Finset.sum_add_distrib] - | tmul x y => - simp [contr_tmul_eq_sum_basis, Finset.sum_mul, mul_assoc, mul_comm ((S.basis d).repr y ν)] - -/-- The matrix of `toLin z` is the transpose of the matrix of components of `z`, -`(z v)^ν = z_μ^ν v^μ`. -/ -lemma toMatrix_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) : - LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin z) = - (Matrix.of fun μ ν => ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν)).transpose := by - ext ν μ - simp [LinearMap.toMatrix_apply, basis_repr_toLin, Finsupp.single_apply] - -/-- `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of `F'` and `F` multiply -to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ -lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) - (F' : S.V c.dual ⊗[k] S.V c.dual) - (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * - Matrix.of (fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr F (μ, ν)) = 1) : - S.toLin F ∘ₗ S.toLin F' = LinearMap.id := by - apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective - rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin, toMatrix_toLin c.dual, - ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] - /-! -## D. Moving a vector along an equality of colors +## C. Moving a vector along an equality of colors A relabeling of slots matches colors only through a proof, `c (σ.symm j) = c' j`, not by definition. A vector of one color is then read at an equal color through `S.cast`. The lemmas say From 1b2d8a505cd3eed54baae04a972fc29c52dd3065 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:20:32 +0000 Subject: [PATCH 097/242] doc(Tensors): Species overview states that a repeated index is the contraction Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index d43339f90d..fb36fa00e1 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -29,8 +29,8 @@ one law of a species (`invariant`) says that this pairing is invariant, which ma dual representation (`dualEquiv`). The contraction `S.contr c` is this pairing. The bases of `c` and `c.dual` are then dual -(`contr_basis`) and the contraction is symmetric (`contr_tmul_symm`). Indices name slots, as in -abstract index notation, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. +(`contr_basis`) and the contraction is symmetric (`contr_tmul_symm`). A repeated index is +contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ` (`contr_tmul_eq_sum_basis`). ## ii. Key results From 9e1db5471775b04e9980f51dc1f0e1b20342c021 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:30:24 +0000 Subject: [PATCH 098/242] doc(Tensors): Color, Species and LinearMap docs rewritten for a first-time reader Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 31 +++---- Physlib/Mathematics/Tensors/LinearMap.lean | 37 ++++---- Physlib/Mathematics/Tensors/Species.lean | 102 ++++++++++++--------- 3 files changed, 92 insertions(+), 78 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index a7ee81d440..6c981cc493 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -10,31 +10,30 @@ public import Mathlib.Init # Colors of tensor slots -A color is a kind of index with a variance, up or down. Its dual has the same kind and the other -variance. +A color says what one index of a tensor is: its kind and whether it is up or down. ## i. Overview -A color is the type of an index, made of a kind `κ : K` and a variance, `up` or `down`. A -species (`Species.lean`) assigns a vector space to each color. The kind fixes the basis labels, -and the variance says whether the index is that of a vector (`up`) or a covector (`down`). Lorentz -tensors have one kind, `vec`. The vector `v^μ` has color `⟨vec, up⟩` and the covector `w_μ` has -color `⟨vec, down⟩`. +A color `⟨κ, v⟩` is a kind of index `κ : K` and a variance `v`, `up` or `down`. For Lorentz +tensors there is one kind, `vec`. The vector index of `v^μ` has color `⟨vec, up⟩` and the +covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds: +`left` and `right` for the indices of left- and right-handed Weyl spinors, and `vec`. -The slots of a tensor are labelled by colors, not by spaces, so that matching a slot with its -dual is a decidable equality of labels, not an equality of types. The dual of `⟨κ, v⟩` is -`⟨κ, v.dual⟩`, and a contraction such as `v^μ w_μ` pairs a slot of color `c` with a slot of -color `c.dual`. +A tensor has one color per slot. A species (`Species.lean`) gives each color a space, and a +tensor is an element of the tensor product of the spaces of its slots. Slots are labelled by +colors rather than by spaces, so that checking that two slots can be contracted is an equality of +labels, decidable, not an equality of types. -`Color K` is a structure, rather than the product `K × Variance`, so that `c.dual` works as dot -notation. +The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, the same kind with the other variance. A contraction such +as `v^μ w_μ` pairs a slot of color `c` with a slot of color `c.dual`. `Color K` is a structure +rather than the product `K × Variance`, so that `c.dual` works as dot notation. ## ii. Key results -- `Variance` is the variance of an index, with `Variance.up`, `Variance.down` and +- `Variance` : the variance of an index, with `Variance.up`, `Variance.down` and `Variance.dual`. -- `Color` is a kind of index and a variance. -- `Color.dual` is the dual color, and `Color.dual_dual` proves `c.dual.dual = c` by `rfl`. +- `Color` : a kind of index and a variance. +- `Color.dual` : the dual color, and `Color.dual_dual` : `c.dual.dual = c` by `rfl`. ## iii. Table of contents diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 74b9083fc5..fc40c6cc62 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -15,25 +15,26 @@ A tensor in `V a ⊗ U` is read as a linear map `V a.dual → U`, and the delta ## i. Overview A tensor `z` in `V a ⊗ U`, for a color `a` of a species `S` and any module `U`, is read as the -linear map `S.toLin z : V a.dual → U` that contracts a vector with the first factor of `z`. On a -pure tensor this is `v ↦ (v · x) y` for `z = x ⊗ y`. Every linear map arises so. `toLin` is -Mathlib's `dualTensorHomEquivOfBasis`, after `Species.dualEquiv` reads `V a` as the dual of -`V a.dual`. +linear map `S.toLin z : V a.dual → U` that contracts its input with the first factor of `z`. On a +pure tensor `z = x ⊗ y` it is `v ↦ (v^μ x_μ) y`. Every linear map `V a.dual → U` is `toLin z` for +exactly one `z`. `toLin` is Mathlib's `dualTensorHomEquivOfBasis`, after `Species.dualEquiv` +reads `V a` as the dual of `V a.dual`. -A metric `g_μν` read this way is the map `v^μ ↦ v^μ g_μν`, and its inverse `g^μν` is the tensor of -the inverse map (`Species.inverse`, in a later file). The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the -tensor of the identity map, `u^μ δ_μ^ν = u^ν`, so it is invariant. +This is how two-index tensors act as maps. A metric `g_μν` is the map `v^μ ↦ v^μ g_μν`, and the +inverse metric `g^μν` is the tensor of the inverse map. The delta +`S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν` +(`toLin_delta`). The identity commutes with every group element, so the delta is invariant +(`delta_invariant`). ## ii. Key results -- `Species.toLin` identifies `V a ⊗ U` with the linear maps `V a.dual → U`, `Species.toLin_map` - proves that it commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` - that `(id ⊗ f) z = f ∘ z`. -- `Species.delta` is the delta `δ_μ^ν`, `Species.toLin_delta` proves that it is the identity map, - and `Species.delta_invariant` that it is invariant. -- `Species.basis_repr_toLin` gives `toLin` in components, `Species.toMatrix_toLin` its matrix, - and `Species.toLin_comp_toLin_of_mul_eq_one` proves that two such maps are inverse when their - matrices are. +- `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with `Species.toLin_map` : it + commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` : + `(id ⊗ f) z = f ∘ z`. +- `Species.delta` : the delta `δ_μ^ν`, with `Species.toLin_delta` : it is the identity map, and + `Species.delta_invariant` : it is invariant. +- `Species.basis_repr_toLin` : `toLin` in components, `Species.toMatrix_toLin` : its matrix, and + `Species.toLin_comp_toLin_of_mul_eq_one` : two such maps are inverse when their matrices are. ## iii. Table of contents @@ -128,7 +129,7 @@ noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] -/-- The delta is invariant, `Λ δ = δ`. -/ +/-- The delta is invariant: `Λ` acting on both factors gives `Λ δ = δ`. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. @@ -165,8 +166,8 @@ lemma toMatrix_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) : ext ν μ simp [LinearMap.toMatrix_apply, basis_repr_toLin, Finsupp.single_apply] -/-- `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of `F'` and `F` multiply -to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ +/-- For a lower color `c`, `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of +`F'` and `F` multiply to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) (F' : S.V c.dual ⊗[k] S.V c.dual) (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index fb36fa00e1..c12825a481 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -12,42 +12,49 @@ public import Mathlib.RepresentationTheory.Intertwining # Tensor species -A species assigns to each color a representation with a basis whose coordinate pairing with the -basis of the dual color is invariant. +A species gives each color of index a space, an action of the symmetry group and a basis. Its +tensors are built from these. ## i. Overview -A species `S : Species k G K basisIdx` assigns to each color `c` a `k`-module `S.V c`, a -representation `S.rep c` of `G` and a basis `S.basis c`. The data is indexed by colors, so that -one definition covers both variances. The contraction `S.contr c : V c ⊗ V c.dual → k` is -`u^μ α_μ` for `c = ⟨vec, up⟩` and `α_μ u^μ` for `c = ⟨vec, down⟩`. +A species `S : Species k G K basisIdx` gives each color `c` a `k`-module `S.V c`, a +representation `S.rep c` of the group `G` on it, and a basis `S.basis c`. A tensor of `S` with +slot colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with the action of `G` on each +factor. -The space of a lower index is data of the species, not `Module.Dual` of the upper one, so that -existing types such as `CoVector d` serve as covectors. The coordinate pairing -`u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of `c` and `c.dual` makes `V c.dual` the dual of `V c`. The -one law of a species (`invariant`) says that this pairing is invariant, which makes `V c.dual` the -dual representation (`dualEquiv`). +For real Lorentz tensors in `1 + d` dimensions, `k = ℝ`, `G` is the Lorentz group, and there is +one kind `vec`, whose basis labels are `basisIdx vec = Fin 1 ⊕ Fin d`. The space of `⟨vec, up⟩` +is `Vector d` with the action `v^μ ↦ Λ^μ_ν v^ν` and the standard basis `e_μ`. The space of +`⟨vec, down⟩` is `CoVector d` with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. -The contraction `S.contr c` is this pairing. The bases of `c` and `c.dual` are then dual -(`contr_basis`) and the contraction is symmetric (`contr_tmul_symm`). A repeated index is -contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ` (`contr_tmul_eq_sum_basis`). +The space of a lower index is given by the species, as `CoVector d` is, rather than built as +`Module.Dual` of the upper space. Existing types for covectors and spinors are then the spaces +of the species. That the two spaces are dual is the content of the bases and of the one law: + +- The contraction `S.contr c : V c ⊗ V c.dual → k` is defined from the bases, as + `u ⊗ α ↦ ∑ μ, u^μ α_μ`. This makes the two bases dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and + `V c.dual` the dual space of `V c`. +- The law of a species (`invariant`) says that the contraction is invariant, + `(Λu)^μ (Λα)_μ = u^μ α_μ`. This makes `V c.dual` the dual representation of `V c` + (`dualEquiv`). + +A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## ii. Key results -- `Species` is the data and the law of a tensor species, and `Species.invariant_of_up` gives the - law from its upper colors. -- `Species.contr` is the contraction `u^μ α_μ`, with `Species.contr_basis`, - `Species.contr_tmul_symm` and `Species.contr_invariant`. -- `Species.contr_tmul_eq_sum_basis` gives the contraction of two vectors in components, and - `Species.ext_contr` proves that the contraction is nondegenerate. -- `Species.isPerfPair_contr` : the contraction is a perfect pairing, and `Species.dualEquiv` - identifies `V c.dual` with the dual representation of `V c`. -- `Species.invariant_of_contr` gives the law `invariant` from an invariant pairing for which the - bases are dual. -- `Species.cast` moves a vector along an equality of colors, `Species.cast_rep` proves that this - commutes with the group action, and `Species.basis_repr_cast` that it keeps components. -- `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` prove that the - contraction does not depend on the colors its two vectors are moved to. +- `Species` : the data and the law of a tensor species, and `Species.invariant_of_up` : the law + from the upper colors alone. +- `Species.contr` : the contraction `u^μ α_μ`, with `Species.contr_tmul_eq_sum_basis` : its + formula in components, `Species.contr_basis` : the bases are dual, `Species.contr_tmul_symm` : + it is symmetric, and `Species.contr_invariant` : it is invariant. +- `Species.isPerfPair_contr` : the contraction is a perfect pairing, `Species.dualEquiv` : + `V c.dual` is the dual representation of `V c`, and `Species.ext_contr` : a vector of + `V c.dual` is determined by its contractions. +- `Species.invariant_of_contr` : the law from a pairing known to be invariant. +- `Species.cast` : a vector moved along an equality of colors, with `Species.cast_rep`, + `Species.basis_repr_cast`, `Species.contr_cast_tmul_cast` and + `Species.contr_cast_tmul_cast_symm` : this keeps the action, the components and the + contraction. ## iii. Table of contents @@ -72,10 +79,13 @@ namespace Tensors ## A. Species The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an index of that kind -runs over, as `Fin 1 ⊕ Fin d` for a Lorentz index. A color and its dual share labels, as `μ` in -`v^μ` and `α_μ`. `basisIdx` is a parameter, not a field, so that a conjugate species can give -conjugate kinds the same labels. The law is stated at every color, so that lemmas about a general -color `c` use it directly. `invariant_of_up` proves it from the upper colors. +runs over. A color and its dual share labels, as `μ` in `v^μ` and `α_μ`. `basisIdx` is a +parameter rather than a field, so that species built from one another, such as a species and +its complex conjugate, have the same labels by definition. + +The law is stated at every color, so that lemmas about a general color `c` use it directly. At a +lower color it is the law at the upper color with the factors swapped, and `invariant_of_up` +proves it from the upper colors. -/ @@ -125,11 +135,12 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, ## B. The contraction -The contraction is not a field of `Species`. It is defined from the bases, as their coordinate -pairing. The bases of `c` and `c.dual` are then dual by construction, and components need no -dual-basis matching. Its invariance is the law of the species. `dualEquiv` identifies `V c.dual` -with Mathlib's dual representation of `V c`, and `invariant_of_contr` builds the law from a pairing -known to be invariant. +The contraction is not a field of `Species`. It is defined from the bases, as +`u ⊗ α ↦ ∑ μ, u^μ α_μ`, so the bases of `c` and `c.dual` are dual by definition +(`contr_basis`). Its invariance is the law of the species. It is a perfect pairing in Mathlib's +sense, and `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of `V c`, so +Mathlib's results on duals apply. `invariant_of_contr` gives the law when an invariant pairing is +already known, as for Weyl spinors. -/ @@ -142,7 +153,7 @@ noncomputable def contr (c : Color K) : ⟨(S.basis c).coordPairing (S.basis c.dual), fun g => TensorProduct.ext' fun x y => S.invariant c g x y⟩ -/-- The bases of a color and its dual are dual, `e^ν(e_μ) = δ_μ^ν`. -/ +/-- The bases of a color and its dual are dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`. -/ lemma contr_basis (c : Color K) (μ ν : basisIdx c.kind) : S.contr c (S.basis c μ ⊗ₜ S.basis c.dual ν) = if μ = ν then 1 else 0 := Module.Basis.coordPairing_basis_basis _ _ _ _ @@ -153,7 +164,8 @@ lemma contr_tmul_eq_sum_basis (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ y) = ∑ μ, (S.basis c).repr x μ * (S.basis c.dual).repr y μ := Module.Basis.coordPairing_tmul _ _ x y -/-- The contraction is symmetric, `u^μ α_μ = α_μ u^μ`. -/ +/-- The contraction is symmetric: `u^μ α_μ` at `c` is `α_μ u^μ` at `c.dual`, with the factors +swapped. -/ lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := Module.Basis.coordPairing_comm _ _ _ _ @@ -210,9 +222,11 @@ variable {S} ## C. Moving a vector along an equality of colors -A relabeling of slots matches colors only through a proof, `c (σ.symm j) = c' j`, not by -definition. A vector of one color is then read at an equal color through `S.cast`. The lemmas say -that this keeps the group action, the components and the contraction. +`S.V a` and `S.V a'` are different types unless `a` and `a'` are equal by definition. The colors +of two slots are often equal only through a proof: after a relabeling of slots, for example, the +color of a new slot equals the color of the old slot it came from. A vector is moved between the +two spaces by `S.cast`, which is the identity on the vector. The lemmas say that this keeps the +group action, the components and the contraction, so that proofs can remove the casts. -/ @@ -240,8 +254,8 @@ lemma basis_repr_cast {a b : Color K} (p : a = b) (v : S.V a) (μ : basisIdx b.k subst p rfl -/-- Moving the vector of a family `f` at the slot `s` depends only on the slot, so it may be read -at any slot `s'` equal to `s`. -/ +/-- For a family `f` of vectors on slots, moving `f s` to the color `b` is moving `f s'`, for any +slot `s'` equal to `s`. -/ lemma cast_apply_congr {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} (e : s = s') {b : Color K} (p : d s = b) : S.cast p (f s) = S.cast (e ▸ p) (f s') := by subst e From 20fedafb59280c7236a3c33d100c40d1da5aa07c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:38:24 +0000 Subject: [PATCH 099/242] doc(Tensors): no prose colons in Color, Species, LinearMap and ForMathlib/Contraction docs Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Contraction.lean | 9 +++++---- Physlib/Mathematics/Tensors/Color.lean | 4 ++-- Physlib/Mathematics/Tensors/LinearMap.lean | 2 +- Physlib/Mathematics/Tensors/Species.lean | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 6d171c3b34..d5915d1ff9 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -68,12 +68,12 @@ lemma coordPairing_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x b.coordPairing b' (x ⊗ₜ[R] y) = ∑ I, b.equivFun x I * b'.equivFun y I := by simp [coordPairing, Matrix.toLinearMap₂_apply, Matrix.one_apply] -/-- `b.coordPairing b' (x ⊗ₜ b' J) = x_J`: pairing with the second basis reads off a coordinate. -/ +/-- Pairing with the second basis reads off a coordinate, `b.coordPairing b' (x ⊗ₜ b' J) = x_J`. -/ lemma coordPairing_tmul_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (J : ι) : b.coordPairing b' (x ⊗ₜ[R] b' J) = b.equivFun x J := by simp [coordPairing_tmul, Module.Basis.equivFun_self] -/-- `b.coordPairing b' (b I ⊗ₜ b' J) = δ_{IJ}`: the two bases are dual. -/ +/-- The two bases are dual, `b.coordPairing b' (b I ⊗ₜ b' J) = δ_{IJ}`. -/ lemma coordPairing_basis_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I J : ι) : b.coordPairing b' (b I ⊗ₜ[R] b' J) = if I = J then 1 else 0 := by rw [coordPairing_tmul_basis, Module.Basis.equivFun_self] @@ -84,12 +84,13 @@ lemma eq_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (p : (b.tensorProduct b').ext fun IJ => by rw [Module.Basis.tensorProduct_apply, h, coordPairing_basis_basis] -/-- `b.coordPairing b' (x ⊗ₜ y) = b'.coordPairing b (y ⊗ₜ x)`: swapping slots swaps the bases. -/ +/-- Swapping the factors swaps the bases, +`b.coordPairing b' (x ⊗ₜ y) = b'.coordPairing b (y ⊗ₜ x)`. -/ lemma coordPairing_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : b.coordPairing b' (x ⊗ₜ[R] y) = b'.coordPairing b (y ⊗ₜ[R] x) := by simp only [coordPairing_tmul, mul_comm] -/-- `b.coordPairing b' (b I ⊗ₜ y) = y_I`: pairing with the first basis reads off a coordinate. -/ +/-- Pairing with the first basis reads off a coordinate, `b.coordPairing b' (b I ⊗ₜ y) = y_I`. -/ lemma coordPairing_basis_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I : ι) (y : N) : b.coordPairing b' (b I ⊗ₜ[R] y) = b'.equivFun y I := by rw [coordPairing_comm, coordPairing_tmul_basis] diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 6c981cc493..c45c682877 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -10,13 +10,13 @@ public import Mathlib.Init # Colors of tensor slots -A color says what one index of a tensor is: its kind and whether it is up or down. +A color gives the kind of an index of a tensor and whether the index is up or down. ## i. Overview A color `⟨κ, v⟩` is a kind of index `κ : K` and a variance `v`, `up` or `down`. For Lorentz tensors there is one kind, `vec`. The vector index of `v^μ` has color `⟨vec, up⟩` and the -covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds: +covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds, `left` and `right` for the indices of left- and right-handed Weyl spinors, and `vec`. A tensor has one color per slot. A species (`Species.lean`) gives each color a space, and a diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index fc40c6cc62..d573294fdf 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -129,7 +129,7 @@ noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] -/-- The delta is invariant: `Λ` acting on both factors gives `Λ δ = δ`. -/ +/-- The delta is invariant, `Λ δ = δ`, with `Λ` acting on both factors. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index c12825a481..d9f49c415f 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -164,7 +164,7 @@ lemma contr_tmul_eq_sum_basis (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ y) = ∑ μ, (S.basis c).repr x μ * (S.basis c.dual).repr y μ := Module.Basis.coordPairing_tmul _ _ x y -/-- The contraction is symmetric: `u^μ α_μ` at `c` is `α_μ u^μ` at `c.dual`, with the factors +/-- The contraction is symmetric. `u^μ α_μ` at `c` is `α_μ u^μ` at `c.dual`, with the factors swapped. -/ lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := @@ -223,7 +223,7 @@ variable {S} ## C. Moving a vector along an equality of colors `S.V a` and `S.V a'` are different types unless `a` and `a'` are equal by definition. The colors -of two slots are often equal only through a proof: after a relabeling of slots, for example, the +of two slots are often equal only through a proof. After a relabeling of slots, for example, the color of a new slot equals the color of the old slot it came from. A vector is moved between the two spaces by `S.cast`, which is the identity on the vector. The lemmas say that this keeps the group action, the components and the contraction, so that proofs can remove the casts. From c373e676580b53a906e916f36d60fcf6582c6f08 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:43:22 +0000 Subject: [PATCH 100/242] =?UTF-8?q?doc(Tensors):=20Color=20=C2=A7A=20state?= =?UTF-8?q?s=20the=20gain=20of=20the=20Bool=20variance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index c45c682877..659440dc9f 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -54,8 +54,8 @@ namespace Tensors ## A. Up and down -`Variance` is two booleans rather than an inductive with two constructors. `dual` swaps them, so -`c.dual.dual` is `c` by `rfl` for a variable color `c`. With an inductive this needs a case split. +`Variance` is two booleans, and `dual` swaps them. So `c.dual.dual` is `c` by definition, also for +a variable color `c`, and the spaces `S.V c.dual.dual` and `S.V c` of a species are the same type. -/ From cf18ab860a1b3a08a04b193f0816d17d71ca8d14 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:44:50 +0000 Subject: [PATCH 101/242] doc(Tensors): Color section A titled Variance Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 659440dc9f..8ff476670a 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -37,7 +37,7 @@ rather than the product `K × Variance`, so that `c.dual` works as dot notation. ## iii. Table of contents -- A. Up and down +- A. Variance - B. Colors ## iv. References @@ -52,7 +52,7 @@ namespace Tensors /-! -## A. Up and down +## A. Variance `Variance` is two booleans, and `dual` swaps them. So `c.dual.dual` is `c` by definition, also for a variable color `c`, and the spaces `S.V c.dual.dual` and `S.V c` of a species are the same type. From c592c7a6785c09bf024ec90b4ceb1195ab3e6ade Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:47:09 +0000 Subject: [PATCH 102/242] doc(Tensors): Species overview gives the number of components of Vector d and CoVector d Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index d9f49c415f..3590b5f7a8 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -24,8 +24,9 @@ factor. For real Lorentz tensors in `1 + d` dimensions, `k = ℝ`, `G` is the Lorentz group, and there is one kind `vec`, whose basis labels are `basisIdx vec = Fin 1 ⊕ Fin d`. The space of `⟨vec, up⟩` -is `Vector d` with the action `v^μ ↦ Λ^μ_ν v^ν` and the standard basis `e_μ`. The space of -`⟨vec, down⟩` is `CoVector d` with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. +is `Vector d`, the vectors with `1 + d` components, with the action `v^μ ↦ Λ^μ_ν v^ν` and the +standard basis `e_μ`. The space of `⟨vec, down⟩` is `CoVector d`, also with `1 + d` components, +with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. The space of a lower index is given by the species, as `CoVector d` is, rather than built as `Module.Dual` of the upper space. Existing types for covectors and spinors are then the spaces From e2ccb997bd87626cc344e0fe1afa776891c3f5a1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 22:59:48 +0000 Subject: [PATCH 103/242] doc(Tensors): Species overview states why lower spaces are given by the species Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 3590b5f7a8..cc2b2380be 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -28,9 +28,9 @@ is `Vector d`, the vectors with `1 + d` components, with the action `v^μ ↦ Λ standard basis `e_μ`. The space of `⟨vec, down⟩` is `CoVector d`, also with `1 + d` components, with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. -The space of a lower index is given by the species, as `CoVector d` is, rather than built as -`Module.Dual` of the upper space. Existing types for covectors and spinors are then the spaces -of the species. That the two spaces are dual is the content of the bases and of the one law: +The space of a lower index is given by the species rather than built as `Module.Dual` of the +upper space, so that a covector is an element of `CoVector d` itself, not a linear form on +`Vector d`. That the two spaces are dual is the content of the bases and of the one law: - The contraction `S.contr c : V c ⊗ V c.dual → k` is defined from the bases, as `u ⊗ α ↦ ∑ μ, u^μ α_μ`. This makes the two bases dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and From 9a37b88b1c2fc00dad75ac8227d595d17ce90952 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:01:44 +0000 Subject: [PATCH 104/242] doc(Tensors): Species overview separates the roles of the bases and the law Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index cc2b2380be..c5883dfcaa 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -28,9 +28,9 @@ is `Vector d`, the vectors with `1 + d` components, with the action `v^μ ↦ Λ standard basis `e_μ`. The space of `⟨vec, down⟩` is `CoVector d`, also with `1 + d` components, with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. -The space of a lower index is given by the species rather than built as `Module.Dual` of the -upper space, so that a covector is an element of `CoVector d` itself, not a linear form on -`Vector d`. That the two spaces are dual is the content of the bases and of the one law: +Each lower space is given by the species, not built as `Module.Dual` of the upper space, so that +a covector is an element of `CoVector d` itself rather than a linear form on `Vector d`. The bases +make the two spaces dual, and the law of the species makes them dual representations: - The contraction `S.contr c : V c ⊗ V c.dual → k` is defined from the bases, as `u ⊗ α ↦ ∑ μ, u^μ α_μ`. This makes the two bases dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and From 3532f2e62b4ff50420ea0c1d734a32c917f61aa5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:05:11 +0000 Subject: [PATCH 105/242] doc(Tensors): "law" replaced by "condition", "hypothesis" or "properties" Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 16 +++++----- Physlib/Mathematics/Tensors/Contraction.lean | 4 +-- Physlib/Mathematics/Tensors/Elab.lean | 4 +-- Physlib/Mathematics/Tensors/Inverse.lean | 4 +-- Physlib/Mathematics/Tensors/Species.lean | 32 ++++++++++---------- Physlib/Mathematics/Tensors/Tensor.lean | 2 +- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 204c0d1087..74ef95da6c 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -33,9 +33,9 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every slot ## ii. Key results - `Color.bar` is the conjugate color, with `Color.bar_bar` and `Color.bar_dual`. -- `Species.HasConjugation` gives the conjugation of a species with its two laws, and - `Species.conjEquiv` is the conjugate of a vector, with its laws `Species.conjEquiv_conjEquiv`, - `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. +- `Species.HasConjugation` gives the conjugation of a species with its two conditions, and + `Species.conjEquiv` is the conjugate of a vector, with its properties + `Species.conjEquiv_conjEquiv`, `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. - `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action, and `Species.rep_dual_of_star_contr` that equivariance at a color gives it at the dual color. - `Species.HasConjugation.ofBasis` builds a componentwise conjugation from `ConjLabels`, and @@ -115,8 +115,8 @@ variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {basisIdx : K variable (S) in /-- A conjugation of the species, a conjugate-linear equivalence `v ↦ v̄` from each color `c` to -its conjugate `c'`, given with an equation `h : c' = c.bar` so that no law moves a vector along -`c.bar.bar = c`. -/ +its conjugate `c'`, given with an equation `h : c' = c.bar` so that none of its conditions moves a +vector along `c.bar.bar = c`. -/ class HasConjugation where /-- The conjugate of a vector of color `c`, a vector of the conjugate color `c'`. -/ conj {c c' : Color K} (h : c' = c.bar) : S.V c ≃ₗ⋆[k] S.V c' @@ -212,8 +212,8 @@ noncomputable def HasConjugation.ofBasis (e : ConjLabels basisIdx) : star_contr_conj _ u α := Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α /-- The componentwise conjugation commutes with the group action when the matrix of each `g : G` -at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The law is needed at the -upper colors only, since it follows at the lower ones (`rep_dual_of_star_contr`). -/ +at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The hypothesis is needed at +the upper colors only, since it follows at the lower ones (`rep_dual_of_star_contr`). -/ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) (rep_basis : ∀ (κ : K) (g : G) (μ ν : basisIdx κ), (S.basis ⟨star κ, .up⟩).repr @@ -225,7 +225,7 @@ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) refine ⟨fun {c _} h g v => ?_⟩ subst h obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c <;> try exact absurd rfl hv - · -- A lower color is the dual of `⟨κ, up⟩`, where the law holds. + · -- A lower color is the dual of `⟨κ, up⟩`, where `rep_basis` gives equivariance. exact rep_dual_of_star_contr (c := ⟨κ, .up⟩) (c' := ⟨star κ, .up⟩) _ _ (fun u α => Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α) (fun g => Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g)) g v diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 53a3976af9..7968613cce 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -12,7 +12,7 @@ public import Mathlib.LinearAlgebra.PiTensorProduct.Dual # Contraction of tensors The contraction of pairs of indices, an index with its dual in each pair, as in the trace -`T^ρμ_μ` or the full contraction `T^μν U_μν`, and its laws. +`T^ρμ_μ` or the full contraction `T^μν U_μν`, and its properties. ## i. Overview @@ -30,7 +30,7 @@ each pair by `S.contr (a j)`, with the first index of the pair as its first argu Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the product `T ⊗ U` has four slots, which form two pairs and leave no slot, `κ = Empty`. The following -laws let a contraction of several tensors be computed one step at a time. +properties let a contraction of several tensors be computed one step at a time. - Only the pairs of indices matter. Two contractions of the same pairs of indices are equal, in whatever order the pairs are listed and whichever index of a pair comes first. - A tensor that changes sign when its pairs are permuted contracts to zero, since its contraction diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 2050a18e2d..73fc7f2997 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -170,8 +170,8 @@ partial def Slots.ofType? (ι : Expr) : MetaM (Option Slots) := do else return none /-- The bijection from `dom` to `cod` sending the slot `pre[j]` of `dom` to the slot `j` of `cod`. -The list `pre` names every slot of `dom` once. The inverse laws are proved slot by slot by -`fin_cases`. -/ +The list `pre` names every slot of `dom` once. Its inverse identities are proved slot by slot +by `fin_cases`. -/ def slotEquiv (dom cod : Slots) (pre : List Nat) : TermElabM Term := do let toFun ← dom.fn ``Sum.elim (← (List.range dom.size).mapM fun i => cod.elem (pre.idxOf i)) let invFun ← cod.fn ``Sum.elim (← pre.mapM dom.elem) diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 51255d1507..ec31b099af 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -37,7 +37,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual ## iii. Table of contents - A. The inverse -- B. The contraction laws and uniqueness +- B. The contraction identities and uniqueness - C. The inverse commutes with the group action - D. The inverse of an invariant form @@ -89,7 +89,7 @@ lemma toLin_inverse_bijective (g : S.V a ⊗[k] S.V b) (h : Function.Bijective ( /-! -## B. The contraction laws and uniqueness +## B. The contraction identities and uniqueness -/ diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index c5883dfcaa..2d81fa8bbf 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -30,12 +30,12 @@ with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. Each lower space is given by the species, not built as `Module.Dual` of the upper space, so that a covector is an element of `CoVector d` itself rather than a linear form on `Vector d`. The bases -make the two spaces dual, and the law of the species makes them dual representations: +make the two spaces dual, and the condition on the species makes them dual representations: - The contraction `S.contr c : V c ⊗ V c.dual → k` is defined from the bases, as `u ⊗ α ↦ ∑ μ, u^μ α_μ`. This makes the two bases dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and `V c.dual` the dual space of `V c`. -- The law of a species (`invariant`) says that the contraction is invariant, +- The condition on a species (`invariant`) says that the contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ`. This makes `V c.dual` the dual representation of `V c` (`dualEquiv`). @@ -43,15 +43,15 @@ A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. ## ii. Key results -- `Species` : the data and the law of a tensor species, and `Species.invariant_of_up` : the law - from the upper colors alone. +- `Species` : the data of a tensor species and the condition on it, and + `Species.invariant_of_up` : the condition from the upper colors alone. - `Species.contr` : the contraction `u^μ α_μ`, with `Species.contr_tmul_eq_sum_basis` : its formula in components, `Species.contr_basis` : the bases are dual, `Species.contr_tmul_symm` : it is symmetric, and `Species.contr_invariant` : it is invariant. - `Species.isPerfPair_contr` : the contraction is a perfect pairing, `Species.dualEquiv` : `V c.dual` is the dual representation of `V c`, and `Species.ext_contr` : a vector of `V c.dual` is determined by its contractions. -- `Species.invariant_of_contr` : the law from a pairing known to be invariant. +- `Species.invariant_of_contr` : the condition from a pairing known to be invariant. - `Species.cast` : a vector moved along an equality of colors, with `Species.cast_rep`, `Species.basis_repr_cast`, `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` : this keeps the action, the components and the @@ -84,9 +84,9 @@ runs over. A color and its dual share labels, as `μ` in `v^μ` and `α_μ`. `ba parameter rather than a field, so that species built from one another, such as a species and its complex conjugate, have the same labels by definition. -The law is stated at every color, so that lemmas about a general color `c` use it directly. At a -lower color it is the law at the upper color with the factors swapped, and `invariant_of_up` -proves it from the upper colors. +The condition is stated at every color, so that lemmas about a general color `c` use it directly. +At a lower color it is the condition at the upper color with the factors swapped, and +`invariant_of_up` proves it from the upper colors. -/ @@ -118,8 +118,8 @@ namespace Species variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] -/-- The law `invariant` at every color from the law at the upper colors. At a lower color the -pairing is the pairing at the dual, upper color with its factors swapped. -/ +/-- The condition `invariant` at every color from the condition at the upper colors. At a lower +color the pairing is the pairing at the dual, upper color with its factors swapped. -/ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, Module k (V c)] (rep : (c : Color K) → Representation k G (V c)) (basis : (c : Color K) → Module.Basis (basisIdx c.kind) k (V c)) @@ -138,10 +138,10 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, The contraction is not a field of `Species`. It is defined from the bases, as `u ⊗ α ↦ ∑ μ, u^μ α_μ`, so the bases of `c` and `c.dual` are dual by definition -(`contr_basis`). Its invariance is the law of the species. It is a perfect pairing in Mathlib's -sense, and `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of `V c`, so -Mathlib's results on duals apply. `invariant_of_contr` gives the law when an invariant pairing is -already known, as for Weyl spinors. +(`contr_basis`). Its invariance is the condition on the species. It is a perfect pairing in +Mathlib's sense, and `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of +`V c`, so Mathlib's results on duals apply. `invariant_of_contr` gives the condition when an +invariant pairing is already known, as for Weyl spinors. -/ @@ -206,8 +206,8 @@ lemma ext_contr {c : Color K} {α α' : S.V c.dual} (S.dualEquiv c).toLinearEquiv.injective (LinearMap.ext h) omit S in -/-- The law `invariant`, from an invariant pairing `p` for which the bases are dual. It is how a -species is built from a pairing that is known to be invariant. -/ +/-- The condition `invariant`, from an invariant pairing `p` for which the bases are dual. It is how +a species is built from a pairing that is known to be invariant. -/ lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommGroup M] [Module k M] [AddCommGroup N] [Module k N] {ρ : Representation k G M} {ρ' : Representation k G N} {b : Module.Basis ι k M} {b' : Module.Basis ι k N} diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 84a6dd644b..ce924634fe 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -28,7 +28,7 @@ Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). The group action, relabeling, products and contraction are each given by -their value on pure tensors, and their laws are proved there first. `S.pure f` is Mathlib's +their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's `⨂ₜ[k] i, f i` as an element of `S.Tensor c`. A group element `Λ` acts on every slot by the representation of its color, From 2ff0a7f161e21a845d82b91a1e5c5ee6c2dce558 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:07:01 +0000 Subject: [PATCH 106/242] doc(Tensors): Species overview says how the contraction is computed Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 2d81fa8bbf..291045e4c8 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -32,9 +32,9 @@ Each lower space is given by the species, not built as `Module.Dual` of the uppe a covector is an element of `CoVector d` itself rather than a linear form on `Vector d`. The bases make the two spaces dual, and the condition on the species makes them dual representations: -- The contraction `S.contr c : V c ⊗ V c.dual → k` is defined from the bases, as - `u ⊗ α ↦ ∑ μ, u^μ α_μ`. This makes the two bases dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and - `V c.dual` the dual space of `V c`. +- The contraction `S.contr c : V c ⊗ V c.dual → k` multiplies the components of its two arguments + in the bases of `c` and `c.dual` and adds them up, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. The two + bases are then dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and `V c.dual` is the dual space of `V c`. - The condition on a species (`invariant`) says that the contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ`. This makes `V c.dual` the dual representation of `V c` (`dualEquiv`). From bb91ece199d0002b693650f5920567a8c823507a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:09:12 +0000 Subject: [PATCH 107/242] doc(Tensors): Species overview gives why the bases and the spaces are dual Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 291045e4c8..34bcff260b 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -33,8 +33,10 @@ a covector is an element of `CoVector d` itself rather than a linear form on `Ve make the two spaces dual, and the condition on the species makes them dual representations: - The contraction `S.contr c : V c ⊗ V c.dual → k` multiplies the components of its two arguments - in the bases of `c` and `c.dual` and adds them up, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. The two - bases are then dual, `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν`, and `V c.dual` is the dual space of `V c`. + in the bases of `c` and `c.dual` and adds them up, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. A basis + vector has one component equal to `1` and the others `0`, so `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν` and + the two bases are dual. Every linear form on `V c` is `u ↦ u^μ α_μ` for exactly one `α`, with + `α_μ` its value on `e_μ`, so `V c.dual` is the dual space of `V c` (`isPerfPair_contr`). - The condition on a species (`invariant`) says that the contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ`. This makes `V c.dual` the dual representation of `V c` (`dualEquiv`). From ac9508a06650e8c636ab5292c6910d22fe4d4948 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:11:12 +0000 Subject: [PATCH 108/242] doc(Tensors): Species overview gives why V c.dual is the dual representation Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 34bcff260b..d2e63a7e0d 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -38,8 +38,9 @@ make the two spaces dual, and the condition on the species makes them dual repre the two bases are dual. Every linear form on `V c` is `u ↦ u^μ α_μ` for exactly one `α`, with `α_μ` its value on `e_μ`, so `V c.dual` is the dual space of `V c` (`isPerfPair_contr`). - The condition on a species (`invariant`) says that the contraction is invariant, - `(Λu)^μ (Λα)_μ = u^μ α_μ`. This makes `V c.dual` the dual representation of `V c` - (`dualEquiv`). + `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. Equivalently `u^μ (Λα)_μ = (Λ⁻¹u)^μ α_μ` + (`contr_tmul_rep`), which is how `G` acts on linear forms on `V c`. So `G` acts on `V c.dual` as + on the linear forms on `V c`, and `V c.dual` is the dual representation of `V c` (`dualEquiv`). A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. From 4e377b433bb4a97d3f4ef105291fd2e30beaed97 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:13:57 +0000 Subject: [PATCH 109/242] doc(Tensors): the contraction identifies V c.dual with the dual space, rather than the bases making the spaces dual Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Contraction.lean | 2 +- Physlib/Mathematics/Tensors/Species.lean | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index d5915d1ff9..392fa561a8 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -18,7 +18,7 @@ Two bases with the same labels pair two modules by their coordinates. Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by their coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`). The two bases are dual for this -pairing, and it is a perfect pairing in Mathlib's sense, so `N` is the dual of `M`. +pairing, and it is a perfect pairing in Mathlib's sense, so it identifies `N` with the dual of `M`. For Lorentz tensors take `M = ℝ⁴` the vectors `x^μ` with the standard basis, and `N` the covectors `w_μ` with the dual basis. The coordinate pairing is the contraction `x^μ w_μ`. diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index d2e63a7e0d..82696018c8 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -30,17 +30,19 @@ with the action `w_μ ↦ w_ν (Λ⁻¹)^ν_μ` and the dual basis `e^μ`. Each lower space is given by the species, not built as `Module.Dual` of the upper space, so that a covector is an element of `CoVector d` itself rather than a linear form on `Vector d`. The bases -make the two spaces dual, and the condition on the species makes them dual representations: +fix the contraction, which identifies `V c.dual` with the dual space of `V c`, and the condition on +the species makes this identification respect the group action: - The contraction `S.contr c : V c ⊗ V c.dual → k` multiplies the components of its two arguments in the bases of `c` and `c.dual` and adds them up, `S.contr c (u ⊗ α) = ∑ μ, u^μ α_μ`. A basis vector has one component equal to `1` and the others `0`, so `S.contr c (e_μ ⊗ e^ν) = δ_μ^ν` and the two bases are dual. Every linear form on `V c` is `u ↦ u^μ α_μ` for exactly one `α`, with - `α_μ` its value on `e_μ`, so `V c.dual` is the dual space of `V c` (`isPerfPair_contr`). + `α_μ` its value on `e_μ`, so the contraction identifies `V c.dual` with the dual space of `V c` + (`isPerfPair_contr`). - The condition on a species (`invariant`) says that the contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. Equivalently `u^μ (Λα)_μ = (Λ⁻¹u)^μ α_μ` - (`contr_tmul_rep`), which is how `G` acts on linear forms on `V c`. So `G` acts on `V c.dual` as - on the linear forms on `V c`, and `V c.dual` is the dual representation of `V c` (`dualEquiv`). + (`contr_tmul_rep`), which is how `G` acts on linear forms on `V c`. So the identification + respects the action, and `V c.dual` is the dual representation of `V c` (`dualEquiv`). A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. @@ -174,7 +176,7 @@ lemma contr_tmul_symm (c : Color K) (x : S.V c) (y : S.V c.dual) : S.contr c (x ⊗ₜ[k] y) = S.contr c.dual (y ⊗ₜ[k] x) := Module.Basis.coordPairing_comm _ _ _ _ -/-- The contraction is a perfect pairing, so `V c.dual` is the dual of `V c`. -/ +/-- The contraction is a perfect pairing, so it identifies `V c.dual` with the dual of `V c`. -/ instance isPerfPair_contr (c : Color K) : (TensorProduct.curry (S.contr c).toLinearMap).IsPerfPair := Module.Basis.isPerfPair_coordPairing _ _ From 73d247b3b91a9ec3cc8671981bae8813cdd47dd9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:16:08 +0000 Subject: [PATCH 110/242] =?UTF-8?q?doc(Tensors):=20Species=20=C2=A7A=20dro?= =?UTF-8?q?ps=20a=20stale=20reason=20for=20basisIdx=20being=20a=20paramete?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 82696018c8..7289a1103c 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -86,8 +86,7 @@ namespace Tensors The basis of a color `c` is labelled by `basisIdx c.kind`, the values that an index of that kind runs over. A color and its dual share labels, as `μ` in `v^μ` and `α_μ`. `basisIdx` is a -parameter rather than a field, so that species built from one another, such as a species and -its complex conjugate, have the same labels by definition. +parameter of `Species`. The condition is stated at every color, so that lemmas about a general color `c` use it directly. At a lower color it is the condition at the upper color with the factors swapped, and From 739f2bb3a6b76b0aac1eb17df997e15d4a072a39 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:20:00 +0000 Subject: [PATCH 111/242] doc(Tensors): Color and Species docs drop references to later pieces Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 6 +++--- Physlib/Mathematics/Tensors/Species.lean | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 8ff476670a..fd2b3b4e0b 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -16,8 +16,8 @@ A color gives the kind of an index of a tensor and whether the index is up or do A color `⟨κ, v⟩` is a kind of index `κ : K` and a variance `v`, `up` or `down`. For Lorentz tensors there is one kind, `vec`. The vector index of `v^μ` has color `⟨vec, up⟩` and the -covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds, -`left` and `right` for the indices of left- and right-handed Weyl spinors, and `vec`. +covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds, one +for each of the two types of Weyl spinor index and one for the vector index. A tensor has one color per slot. A species (`Species.lean`) gives each color a space, and a tensor is an element of the tensor product of the spaces of its slots. Slots are labelled by @@ -93,7 +93,7 @@ end Variance /-- A color `⟨κ, v⟩`, made of a kind of index `κ : K` and its variance `v`. -/ structure Color (K : Type) where - /-- The kind of index, such as the Lorentz vector index `vec` or a left-handed Weyl index. -/ + /-- The kind of index, such as a Lorentz vector index or a left-handed Weyl spinor index. -/ kind : K /-- Whether the index is up or down. -/ variance : Variance diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 7289a1103c..8f17d5bba9 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -145,7 +145,7 @@ The contraction is not a field of `Species`. It is defined from the bases, as (`contr_basis`). Its invariance is the condition on the species. It is a perfect pairing in Mathlib's sense, and `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of `V c`, so Mathlib's results on duals apply. `invariant_of_contr` gives the condition when an -invariant pairing is already known, as for Weyl spinors. +invariant pairing is already known. -/ @@ -228,10 +228,11 @@ variable {S} ## C. Moving a vector along an equality of colors `S.V a` and `S.V a'` are different types unless `a` and `a'` are equal by definition. The colors -of two slots are often equal only through a proof. After a relabeling of slots, for example, the -color of a new slot equals the color of the old slot it came from. A vector is moved between the -two spaces by `S.cast`, which is the identity on the vector. The lemmas say that this keeps the -group action, the components and the contraction, so that proofs can remove the casts. +of two slots are often equal only through a proof. For a family of colors `c : ι → Color K` and +two slots with `s = s'`, the colors `c s` and `c s'` are equal through that proof. A vector is +moved between the two spaces by `S.cast`, which is the identity on the vector. The lemmas say that +this keeps the group action, the components and the contraction, so that proofs can remove the +casts. -/ From 0b7bb7b484cc932737c92b2c9882cf9a058ece51 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:36:08 +0000 Subject: [PATCH 112/242] refactor(ForMathlib): Basis.coordPairing defined as toDual after Basis.equiv; Basis.curry_coordPairing and Basis.coordPairing_basis_tmul removed Co-authored-by: Claude Opus 5.5 --- .../Mathematics/ForMathlib/Contraction.lean | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 392fa561a8..50ef63364a 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -5,7 +5,7 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Matrix.SesquilinearForm +public import Mathlib.LinearAlgebra.Dual.Basis public import Mathlib.LinearAlgebra.PerfectPairing.Basic /-! @@ -17,7 +17,8 @@ Two bases with the same labels pair two modules by their coordinates. ## i. Overview Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by their -coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`). The two bases are dual for this +coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`). It is Mathlib's `b'.toDual`, applied +after the equivalence `b.equiv b'` that sends `b I` to `b' I`. The two bases are dual for this pairing, and it is a perfect pairing in Mathlib's sense, so it identifies `N` with the dual of `M`. For Lorentz tensors take `M = ℝ⁴` the vectors `x^μ` with the standard basis, and `N` the covectors @@ -29,8 +30,7 @@ For Lorentz tensors take `M = ℝ⁴` the vectors `x^μ` with the standard basis the same labels. - `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and `Module.Basis.eq_coordPairing` : it is the only such pairing. -- `Module.Basis.isPerfPair_coordPairing` : the coordinate pairing is a perfect pairing, through - `Module.Basis.curry_coordPairing` : it is Mathlib's `toDualEquiv` of the second basis. +- `Module.Basis.isPerfPair_coordPairing` : the coordinate pairing is a perfect pairing. ## iii. Table of contents @@ -57,21 +57,22 @@ open TensorProduct variable {R ι M N : Type*} [CommSemiring R] [Fintype ι] [DecidableEq ι] [AddCommMonoid M] [Module R M] [AddCommMonoid N] [Module R N] -/-- The coordinate pairing `M ⊗ N → R` of two bases with the same labels, Mathlib's -`Matrix.toLinearMap₂ b b'` of the identity matrix. -/ +/-- The coordinate pairing `M ⊗ N → R` of two bases with the same labels. It sends `x ⊗ y` to +`b'.toDual x' y`, where `x'` is the vector of `N` with the coordinates of `x`. -/ noncomputable def coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N →ₗ[R] R := - TensorProduct.lift (Matrix.toLinearMap₂ b b' 1) - -/-- `b.coordPairing b' (x ⊗ₜ y) = ∑_I (b x)_I (b' y)_I`. -/ -lemma coordPairing_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : - b.coordPairing b' (x ⊗ₜ[R] y) = ∑ I, b.equivFun x I * b'.equivFun y I := by - simp [coordPairing, Matrix.toLinearMap₂_apply, Matrix.one_apply] + TensorProduct.lift (b'.toDual ∘ₗ (b.equiv b' (Equiv.refl ι)).toLinearMap) /-- Pairing with the second basis reads off a coordinate, `b.coordPairing b' (x ⊗ₜ b' J) = x_J`. -/ lemma coordPairing_tmul_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (J : ι) : b.coordPairing b' (x ⊗ₜ[R] b' J) = b.equivFun x J := by - simp [coordPairing_tmul, Module.Basis.equivFun_self] + simp [coordPairing, Module.Basis.toDual_eq_repr, Module.Basis.equiv] + +/-- `b.coordPairing b' (x ⊗ₜ y) = ∑_I (b x)_I (b' y)_I`. -/ +lemma coordPairing_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : N) : + b.coordPairing b' (x ⊗ₜ[R] y) = ∑ I, b.equivFun x I * b'.equivFun y I := by + conv_lhs => rw [← b'.sum_equivFun y, TensorProduct.tmul_sum, map_sum] + simp_rw [TensorProduct.tmul_smul, map_smul, coordPairing_tmul_basis, smul_eq_mul, mul_comm] /-- The two bases are dual, `b.coordPairing b' (b I ⊗ₜ b' J) = δ_{IJ}`. -/ lemma coordPairing_basis_basis (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I J : ι) : @@ -90,27 +91,19 @@ lemma coordPairing_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x b.coordPairing b' (x ⊗ₜ[R] y) = b'.coordPairing b (y ⊗ₜ[R] x) := by simp only [coordPairing_tmul, mul_comm] -/-- Pairing with the first basis reads off a coordinate, `b.coordPairing b' (b I ⊗ₜ y) = y_I`. -/ -lemma coordPairing_basis_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (I : ι) (y : N) : - b.coordPairing b' (b I ⊗ₜ[R] y) = b'.equivFun y I := by - rw [coordPairing_comm, coordPairing_tmul_basis] - -/-- The coordinate pairing, curried, sends `x` to the dual vector `b'.toDual` of the vector with -the same coordinates in `b'`. -/ -lemma curry_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : - TensorProduct.curry (b.coordPairing b') = - ((b.equiv b' (Equiv.refl ι)).trans b'.toDualEquiv).toLinearMap := - b.ext fun I => b'.ext fun J => by - simp [coordPairing_basis_basis, Module.Basis.toDualEquiv_apply, Module.Basis.toDual_apply] - /-- The coordinate pairing of two bases with the same labels is a perfect pairing. -/ instance isPerfPair_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : (TensorProduct.curry (b.coordPairing b')).IsPerfPair where - bijective_left := by rw [curry_coordPairing]; exact LinearEquiv.bijective _ + -- Curried, the pairing is `b'.toDualEquiv` after matching the bases, an equivalence. + bijective_left := by + rw [show TensorProduct.curry (b.coordPairing b') = + ((b.equiv b' (Equiv.refl ι)).trans b'.toDualEquiv).toLinearMap from + LinearMap.ext₂ fun _ _ => rfl] + exact LinearEquiv.bijective _ bijective_right := by - have : (TensorProduct.curry (b.coordPairing b')).flip = - TensorProduct.curry (b'.coordPairing b) := - LinearMap.ext₂ fun y x => coordPairing_comm b b' x y - rw [this, curry_coordPairing]; exact LinearEquiv.bijective _ + rw [show (TensorProduct.curry (b.coordPairing b')).flip = + ((b'.equiv b (Equiv.refl ι)).trans b.toDualEquiv).toLinearMap from + LinearMap.ext₂ fun y x => (coordPairing_comm b b' x y).trans rfl] + exact LinearEquiv.bijective _ end Module.Basis From 8688d39cb18645b4c0d51b09c411842cc76ec30a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Mon, 9 Nov 2026 23:40:55 +0000 Subject: [PATCH 113/242] doc(ForMathlib): Contraction overview says why the coordinate pairing is needed; drop a transitive import Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Contraction.lean | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 50ef63364a..4f0a1a0ed3 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Dual.Basis public import Mathlib.LinearAlgebra.PerfectPairing.Basic /-! @@ -16,13 +15,16 @@ Two bases with the same labels pair two modules by their coordinates. ## i. Overview -Two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type `ι` pair by their -coordinates, `(x, y) ↦ ∑_I (b x)_I (b' y)_I` (`coordPairing`). It is Mathlib's `b'.toDual`, applied -after the equivalence `b.equiv b'` that sends `b I` to `b' I`. The two bases are dual for this -pairing, and it is a perfect pairing in Mathlib's sense, so it identifies `N` with the dual of `M`. +Mathlib pairs a module `M` with its own dual. In physics the space paired with `M` is often a +type of its own with its own basis, not `Module.Dual M`. Covectors `w_μ`, with the basis `e^μ`, +pair with vectors `x^μ`, with the basis `e_μ`, as `x^μ w_μ = ∑ μ, x^μ w_μ`. This file gives that +pairing for any two modules `M` and `N` with bases `b` and `b'` labelled by the same finite type +`ι`, and shows that it identifies `N` with the dual of `M`, so Mathlib's results on duals apply. -For Lorentz tensors take `M = ℝ⁴` the vectors `x^μ` with the standard basis, and `N` the covectors -`w_μ` with the dual basis. The coordinate pairing is the contraction `x^μ w_μ`. +The coordinate pairing `coordPairing` sends `x ⊗ y` to `∑_I (b x)_I (b' y)_I`. It is Mathlib's +`b'.toDual`, applied after the equivalence `b.equiv b'` that sends `b I` to `b' I`. The two bases +are dual for it, it is the only pairing for which they are, and it is a perfect pairing in +Mathlib's sense. ## ii. Key results From 619e76361bc2f5a0ad5045a4dbdefc3bb2cf7e47 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 08:28:19 +0000 Subject: [PATCH 114/242] fix(Tensors/ElabTactics): tensor_regroup compares factors up to instances; using accepts local hypotheses - isEqFactor: factors are compared at instances transparency, so a named tensor is never unfolded into the contraction that defines it. - regroupUsing runs in the context of the main goal, so tensor_regroup using h accepts a local hypothesis h. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/ElabTactics.lean | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 5715911711..691bcd078b 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -111,6 +111,11 @@ private def wrapSides (tac : String) (p : Expr → Bool) (h : Term) : TacticM Un /-- Whether `a` and `b` are definitionally equal, without assigning metavariables of the goal. -/ private def isDefEqGoal (a b : Expr) : MetaM Bool := withNewMCtxDepth (isDefEq a b) +/-- Whether the factors `a` and `b` are equal up to instances, as in `rw`, so that a named tensor +is never unfolded into the contraction that defines it. -/ +private def isEqFactor (a b : Expr) : MetaM Bool := + withNewMCtxDepth (withTransparency .instances (isDefEq a b)) + /-- Runs `f x` for each `x` of `xs` in turn, each from the current state, and returns the result of the first that succeeds. When every one fails it throws the last error, and `err` when `xs` is empty. -/ @@ -294,7 +299,7 @@ private def matchFactors : TacticM Unit := do let qs := factors q unless ps.length = qs.length do throwError "tensor_regroup: the two sides have different numbers of factors" - let candidates ← ps.mapM fun x => (List.range qs.length).filterM fun j => isDefEqGoal x qs[j]! + let candidates ← ps.mapM fun x => (List.range qs.length).filterM fun j => isEqFactor x qs[j]! if let some (x, _) := (ps.zip candidates).find? (·.2.isEmpty) then throwError "tensor_regroup: the factor{indentExpr x}\nhas no equal factor on the right" let targets := matchings candidates [] @@ -324,7 +329,7 @@ private def matchFactors : TacticM Unit := do private def leftover (xs ys : List Expr) : MetaM (List Expr) := do let mut rest := xs for y in ys do - if let some i ← (List.range rest.length).findM? fun i => isDefEqGoal rest[i]! y then + if let some i ← (List.range rest.length).findM? fun i => isEqFactor rest[i]! y then rest := rest.eraseIdx i return rest @@ -712,7 +717,7 @@ equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each wa images of the slots of `A`, together with the factors of `G` that are hit. -/ private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List Nat)) := do let candidates ← A.factors.mapM fun x => - (List.range G.factors.length).filterM fun j => isDefEqGoal x G.factors[j]! + (List.range G.factors.length).filterM fun j => isEqFactor x G.factors[j]! let isPair (p q : Nat) := G.pairs.contains (p, q) || G.pairs.contains (q, p) return (matchings candidates []).filterMap fun target => let φ := (target.zipIdx).flatMap fun (j, i) => @@ -777,7 +782,7 @@ then with lowerings and raisings written as contractions with the form, first on the goal and then on the right. -/ private partial def regroupUsing : List Term → TacticM Unit | [] => regroupWithLowerings - | e :: es => do + | e :: es => withMainContext do let h ← instantiateMVars (← Tactic.elabTerm e none) let some (_, A, B) := (← instantiateMVars (← inferType h)).eq? | throwError "tensor_regroup: {e} is not an equation" @@ -846,7 +851,7 @@ equality, as `2` and `2`. `tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, the factors of `Aᵢ` are found among the factors of the left side of the goal, or else of the right side, contracted along the same pairs, and that side is regrouped as `Aᵢ` times the other factors -and rewritten to `Bᵢ`. The equations must be closed terms. -/ +and rewritten to `Bᵢ`. -/ syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic elab_rules : tactic From eb4beae8063aaa27c174f46f94a736ed4e1f9035 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 08:28:36 +0000 Subject: [PATCH 115/242] =?UTF-8?q?feat(Tensors/Elab):=20=CF=84(=CE=BC)=20?= =?UTF-8?q?flips=20the=20variance=20of=20an=20index,=20next=20to=20?= =?UTF-8?q?=E2=86=93=CE=BC=20and=20=E2=86=91=CE=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 13 ++++++++++--- Physlib/Mathematics/Tensors/ElabExamples.lean | 7 +++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 73fc7f2997..63fbcdc65e 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -33,6 +33,8 @@ names are bound only inside the braces. Each construct is an operation of the sp - `↓μ` lowers an upper index and `↑μ` raises a lower one, by `S.toDualAt`, so `{v | ↓ν}ᵀ` is `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the slot, and a visible variance, as in `⟨κ, .up⟩`. +- `τ(μ)` flips the variance of an index by `S.toDualAt` whichever way it points, so it needs no + visible variance. - A term `m : M` with an instance `Tensorial S c M`, such as a Lorentz vector, is read as `Tensorial.toTensor m`. - Scalars, group elements and negatives are ordinary terms in front of `|`, as in `r • T | μ ν`, @@ -96,6 +98,9 @@ syntax ident : tensorIndex syntax "↓" ident : tensorIndex /-- A named lower index raised with the invariant form, as `α^μ` from `α_μ`. -/ syntax "↑" ident : tensorIndex +/-- A named index whose variance is flipped with the invariant form, lowered if upper and raised if +lower. -/ +syntax "τ(" ident ")" : tensorIndex /-- A tensor expression in index notation. -/ declare_syntax_cat tensorIndexExpr @@ -266,8 +271,8 @@ def tensorOf (T : Term) : TermElabM (Expr × Expr) := do throwError "index notation: {T} is not a tensor and has no `Tensorial` instance:\n\ {ex.toMessageData}" -/-- The name of the index `i` on a slot of color `col`, and whether it is lowered or raised, after -checking that its arrow matches the variance of `col`. -/ +/-- The name of the index `i` on a slot of color `col`, and whether its variance is flipped, after +checking that an arrow matches the variance of `col`. -/ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) := do let arrow (x : Ident) (up : Bool) : TermElabM (Name × Bool) := do let isUp ← whnfD (← mkAppM ``Tensors.Variance.isUp #[← mkAppM ``Tensors.Color.variance #[col]]) @@ -279,9 +284,11 @@ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) | `(tensorIndex| $x:ident) => pure (x.getId, false) | `(tensorIndex| ↓$x:ident) => arrow x true | `(tensorIndex| ↑$x:ident) => arrow x false + | `(tensorIndex| τ($x:ident)) => pure (x.getId, true) | _ => throwError "index notation: unsupported index {i}" -/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ` and raised at each `↑μ`. -/ +/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ`, raised at each `↑μ` +and flipped at each `τ(μ)`. -/ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do let (e, ty) ← tensorOf T let args := ty.getAppArgs diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 7e629d38e6..cc1e582a19 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -152,6 +152,13 @@ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ #guard_msgs in #check {v | ↑μ}ᵀ +/-- `τ` lowers an upper index, as `↓` does. -/ +example : {v | τ(μ)}ᵀ = {v | ↓μ}ᵀ := rfl + +/-- `τ` needs no visible variance, so it flips an index of any color, `(w_μ)^μ = w^μ`. -/ +example (w : S.Tensor ![c]) : {{w | τ(μ)}ᵀ | τ(μ)}ᵀ = w := by + tensor_slots + /-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] From 3fbd470a17008c3207a275b33e6a5aa7a0fe1bfc Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 10:22:44 +0000 Subject: [PATCH 116/242] =?UTF-8?q?refactor(Tensors):=20lowering=20contrac?= =?UTF-8?q?ts=20the=20second=20index=20of=20the=20form,=20v=5F=CE=BD=20=3D?= =?UTF-8?q?=20g=5F=CE=BD=CE=BC=20v^=CE=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Metric: toDual is toLin of the swapped form; basis_repr_toDual, tensorBasis_repr_toDualAt and toDualAt_eq_contr read the second index. - LinearMap: toMatrix_toLin_comm, toLin_comm_comp_toLin_comm, comm_delta. - Contraction: contr_deltaTensor_swap, a delta contracted on its second index. - Inverse: reindex_contr_formTensor_formTensor, g^μν g_νρ with the delta swapped. - ElabTactics: tensor_regroup inserts and matches deltas on either index, choosing the side from how the forms are contracted (deltaOrientations); flipForm fails instead of logging when the form has no symmetry. - ComplexTensor/Species: the Weyl ε has ε^12 = 1 and ε_12 = -1, so that ψ_α = ε_αβ ψ^β as in Wess and Bagger; schouten_downL and schouten_downR have the free index first in each ε. - Weyl/Metric: sum_metricRaw_mul_sum_metricRaw_mul with the lower ε. - Pauli/Tensor: component proofs, η raising and explicit ε in the new convention; the Fierz identity by tensor_regroup using the ε antisymmetry. - ElabExamples: the metric examples restated in the new convention, and examples of a delta contracted on its second index. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 11 + Physlib/Mathematics/Tensors/ElabExamples.lean | 40 ++-- Physlib/Mathematics/Tensors/ElabTactics.lean | 211 ++++++++++-------- Physlib/Mathematics/Tensors/Inverse.lean | 9 + Physlib/Mathematics/Tensors/LinearMap.lean | 29 +++ Physlib/Mathematics/Tensors/Metric.lean | 41 ++-- .../Relativity/Bispinors/Pauli/Tensor.lean | 26 ++- Physlib/Relativity/Fermions/Weyl/Metric.lean | 6 +- .../Tensors/ComplexTensor/Species.lean | 111 +++++---- 9 files changed, 301 insertions(+), 183 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 7968613cce..1470ce05b0 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -557,6 +557,17 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) rw [deltaTensor, contr_matMulAt, toLin_delta] exact LinearMap.congr_fun (mapAt_id i _) t +/-- Contracting the index `i` with the second index of the delta is the identity, +`δ^ν_μ T^…μ… = T^…ν…`. -/ +lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) + (ha : c i = a) (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) + (S.tmulEquiv c ![a.dual, a] + (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by + rw [deltaTensor, reindex_twoSlotEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a + from S.comm_delta a.dual] + exact contr_deltaTensor i a ha t + /-! ## D. Components diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index cc1e582a19..c5aaf01b91 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -262,6 +262,14 @@ tensor, read as a contraction with no pairs. -/ example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by tensor_regroup +/-- A delta contracted at its second slot must be inserted at a slot of the dual color. -/ +example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a | ν ρ}ᵀ := by + tensor_regroup + +/-- Matching a delta at its second slot also works when the delta is on the left. -/ +example : {T | μ ρ ⊗ S.deltaTensor a | ν ρ = T | μ ν}ᵀ := by + tensor_regroup + /-- A delta inside a sum, `T^μ_ν + U^μ_ρ δ^ρ_ν = U^μ_ν + T^μ_ν`. -/ example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by tensor_regroup @@ -342,53 +350,53 @@ section Metric variable [S.HasForm κ] [S.HasForm c.kind] -/-- A lowering is a contraction with the form, `v_μ = v^ν g_νμ`. -/ -example : {v | ↓μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν μ}ᵀ := by +/-- A lowering is a contraction with the form, `v_μ = g_μν v^ν`. -/ +example : {v | ↓μ = S.formTensor ⟨κ, .down⟩ | μ ν ⊗ v | ν}ᵀ := by tensor_regroup /-- A form and its inverse are matched by inserting a delta, `v^μ = v^ν g_νρ g^ρμ`. -/ example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ}ᵀ := by tensor_regroup -/-- A lowering cancels against the inverse form, `M^μ_ν g^νρ = M^μρ`. -/ +/-- A lowering cancels against the inverse form, `g^ρν M^μ_ν = M^μρ`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by tensor_regroup /-- Two lowerings cancel against two inverse forms, one pair at a time, -`M_μν g^μρ g^νσ = M^ρσ`. -/ +`g^ρμ g^σν M_μν = M^ρσ`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | ρ σ}ᵀ := by + {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ ⊗ S.formTensor ⟨κ, .up⟩ | σ ν = M | ρ σ}ᵀ := by tensor_regroup -/-- The same with the pairs on the right side and on two kinds, `N^ρσ = N_μν g^μρ g'^νσ`. -/ +/-- The same with the pairs on the right side and on two kinds, `N^ρσ = g^ρμ g'^σν N_μν`. -/ example (κ' : K) [S.HasForm κ'] (N : S.Tensor ![⟨κ, .up⟩, ⟨κ', .up⟩]) : - {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ', .up⟩ | ν σ}ᵀ := by + {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ ⊗ S.formTensor ⟨κ', .up⟩ | σ ν}ᵀ := by tensor_regroup -/-- A lowering contracted with the second index of a symmetric form, `M^μ_ν g^ρν = M^μρ`. -/ +/-- A lowering contracted with the first index of a symmetric form, `g^νρ M^μ_ν = M^μρ`. -/ example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by tensor_regroup -/-- With an antisymmetric form the first index cancels with no sign, `M^μ_ν ε^νρ = M^μρ`. -/ +/-- With an antisymmetric form the second index cancels with no sign, `ε^ρν M^μ_ν = M^μρ`. -/ example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by tensor_regroup -/-- With an antisymmetric form the second index cancels with a sign, `M^μ_ν ε^ρν = -M^μρ`. -/ +/-- With an antisymmetric form the first index cancels with a sign, `ε^νρ M^μ_ν = -M^μρ`. -/ example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = -M | μ ρ}ᵀ := by + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = -M | μ ρ}ᵀ := by tensor_regroup --- The sign of an antisymmetric form is not dropped, `M^μ_ν ε^ρν ≠ M^μρ`. +-- The sign of an antisymmetric form is not dropped, `ε^νρ M^μ_ν ≠ M^μρ`. /-- error: tensor_regroup: the forms left over on one side do not cancel in inverse pairs against the other side S.formTensor { kind := κ, variance := Variance.up }.dual S.formTensor { kind := κ, variance := Variance.up } -/ #guard_msgs in example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : - {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by + {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by tensor_regroup -- Cancelling the pairs does not swap the free indices, `M_μν g^μρ g^νσ ≠ M^σρ`. diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 691bcd078b..fa90d068f0 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -346,22 +346,112 @@ private def formArgs? (x : Expr) : Option (Expr × Expr) := some (args[args.size - 3]!, args[args.size - 2]!) else none +/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of +`P` are numbered in order, the slots of each factor after those of the factors before it. -/ +private structure ReadContraction where + /-- The factors of `P`, from left to right. -/ + factors : List Expr + /-- The slot of `P` at which each factor starts. -/ + offsets : List Nat + /-- The slot of `P` of each free index, in order. -/ + free : List Nat + /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ + pairs : List (Nat × Nat) + +/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in +slot order. -/ +private partial def slotElems (ι : Expr) : MetaM (List Expr) := do + let ι ← whnfR ι + if ι.isAppOfArity ``Fin 1 then + let some n ← (evalNat ι.appArg!).run + | throwError "tensor_regroup: the slot type {ι} is not literal" + (List.range n).mapM fun i => do + mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] + else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] + else if ι.isConstOf ``Empty then return [] + else if ι.isAppOfArity ``Sum 2 then + let (a, b) := (ι.getArg! 0, ι.getArg! 1) + return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ + (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) + else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" + +/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ +private partial def slotNumber : Slots → Expr → MetaM Nat + | .fin _, x => do + let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run + | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + return i + | .unit, _ => return 0 + | .sum a b, x => do + let x ← whnfD x + if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! + else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) + else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + +/-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each +free index and at each slot of each pair. -/ +private def readContraction (e : Expr) : MetaM ReadContraction := do + let some P := bundledArg? ``Tensors.Species.Tensor.contr e + | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" + let τ := e.consumeMData.appFn!.appArg!.appArg! + let some (slots, _, _) ← literalTensorType? (← inferType P) + | throwError "tensor_regroup: the slots or colors of a product are not literal" + let fs := factors P + let sizes ← fs.mapM fun f => do + let some (s, _, _) ← literalTensorType? (← inferType f) + | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" + return s.size + -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. + let args := (← whnfR (← inferType τ)).getAppArgs + let cargs := args[args.size - 1]!.getAppArgs + let (κ, J) := (cargs[1]!, cargs[2]!) + let JJ ← mkAppM ``Sum #[J, J] + let at_ (y : Expr) : MetaM Nat := do + slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) + let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) + let pairs ← (← slotElems J).mapM fun j => do + let half (left : Bool) := do + at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) + #[J, J, j]]) + return (← half true, ← half false) + return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ + +/-- The ways to insert a delta against the side `e` of the goal, a contraction: `false` for +`T^…μ… δ_μ^ν` and `true` for `δ^ν_μ T^…μ…`. A form or a delta of `e` whose first slot is contracted +with another factor cancels against a delta inserted the first way, and one whose second slot is, +the second way. Both are returned when no form or delta is contracted with another factor. -/ +private def deltaOrientations (e : Expr) : MetaM (List Bool) := do + let rc ← readContraction e + let isFormOrDelta := rc.factors.map fun f => (formArgs? f).isSome || (deltaColor? f).isSome + let factorOf (s : Nat) : Nat := (rc.offsets.findIdx? (s < ·)).getD 1 - 1 + let side (s t : Nat) : Option Bool := + let (i, j) := (factorOf s, factorOf t) + if isFormOrDelta.getD i false && !isFormOrDelta.getD j false then + some (s - rc.offsets[i]! == 1) + else none + let found := (rc.pairs.filterMap fun (s, t) => (side s t).orElse fun _ => side t s).eraseDups + return if found.isEmpty then [false, true] else found + /-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, -if `lhs`, or of the right side, by `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`). -/ -private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) : TacticM Unit := do +if `lhs`, or of the right side, by `Species.Tensor.contr_deltaTensor`. With `second`, uses +`Species.Tensor.contr_deltaTensor_swap` instead, inserting a delta of color `a.dual`. -/ +private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) (second : Bool) : + TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q - let thm ← `((Tensors.Species.Tensor.contr_deltaTensor + let lem := mkIdent <| if second then ``Tensors.Species.Tensor.contr_deltaTensor_swap + else ``Tensors.Species.Tensor.contr_deltaTensor + let thm ← `(($lem ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) rewriteSubterm P thm (some lhs) mutual -/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, at each slot of -the product of color `a` in turn, and proves the goal by `regroupContractions`. With `skipForms`, -the slots of forms are left out when another factor has a slot: inserting the delta anywhere along a -chain of contracted forms gives the same factors, so trying each of their slots only repeats the -search. -/ +/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, trying slots of +color `a` for its first index and `a.dual` for its second index, and proves the goal by +`regroupContractions`. With `skipForms`, the slots of forms are left out when another factor has a +slot: inserting the delta anywhere along a chain of contracted forms gives the same factors, so +trying each of their slots only repeats the search. -/ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Bool := false) : TacticM Unit := do let (p, q) ← contractedProducts @@ -375,11 +465,18 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Boo -- shortcut: misses a chain of forms that reaches no other factor, as `T^μ_ν g^αβ` against -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the slots of forms after the others would cover it. let inForm := if inForm.all id then inForm.map fun _ => false else inForm - let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => - return !inForm.getD i false && (← isDefEqGoal col a) - firstSuccess (slotsOfColor.map (·.2)) m!"tensor_regroup: no slot has the color {a} of a delta" - fun slot => do - insertDelta lhs a slots slot + let (_, l, r) ← goalEq + let orientations ← deltaOrientations (if lhs then r else l) + let tries ← orientations.flatMapM fun second => do + -- The swapped identity inserts `deltaTensor b.dual` at a slot of color `b`. + -- To insert the requested `deltaTensor a`, that slot must therefore have color `a.dual`. + let b ← if second then mkAppM ``Tensors.Color.dual #[a] else pure a + let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => + return !inForm.getD i false && (← isDefEqGoal col b) + return slotsOfColor.map fun (_, i) => (i, second, b) + firstSuccess tries m!"tensor_regroup: no slot has the color {a} of a delta" + fun (slot, second, b) => do + insertDelta lhs b slots slot second regroupContractions /-- Matches pairs of forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors @@ -399,9 +496,18 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni match ds with | [d] => unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" - firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (S, c) => do - let thm ← `((Tensors.Species.contr_formTensor_formTensor (S := $(← exprToSyntax S)) - $(← exprToSyntax c)).symm) + -- The delta is `g^μν g_νρ` read with its slots in order, when the slot of the delta and the + -- slot of the forms contracted with other factors agree, or swapped. + let (_, l, r) ← goalEq + let swaps := match ← deltaOrientations (if lhs then l else r), + ← deltaOrientations (if lhs then r else l) with + | [x], [y] => [x != y] + | _, _ => [false, true] + let tries := swaps.flatMap fun sw => fs.map (·, sw) + firstSuccess tries m!"tensor_regroup: no two forms left over are inverse" fun ((S, c), sw) => do + let lem := mkIdent <| if sw then ``Tensors.Species.reindex_contr_formTensor_formTensor + else ``Tensors.Species.contr_formTensor_formTensor + let thm ← `(($lem (S := $(← exprToSyntax S)) $(← exprToSyntax c)).symm) rewriteSubterm d thm (some lhs) regroupContractions | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do @@ -468,7 +574,8 @@ private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do let thm ← if antisym then `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c).symm) else `((Tensors.Species.formTensor_symm (S := $S) $c).symm) - rewriteSubterm F thm (k := some k) + -- A form with no instance of the symmetry fails here, instead of logging an error. + Term.withoutErrToSorry <| rewriteSubterm F thm (k := some k) mutual @@ -642,76 +749,6 @@ elab "tensor_slots" : tactic => do -/ -/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of -`P` are numbered in order, the slots of each factor after those of the factors before it. -/ -private structure ReadContraction where - /-- The factors of `P`, from left to right. -/ - factors : List Expr - /-- The slot of `P` at which each factor starts. -/ - offsets : List Nat - /-- The slot of `P` of each free index, in order. -/ - free : List Nat - /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ - pairs : List (Nat × Nat) - -/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in -slot order. -/ -private partial def slotElems (ι : Expr) : MetaM (List Expr) := do - let ι ← whnfR ι - if ι.isAppOfArity ``Fin 1 then - let some n ← (evalNat ι.appArg!).run - | throwError "tensor_regroup: the slot type {ι} is not literal" - (List.range n).mapM fun i => do - mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] - else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] - else if ι.isConstOf ``Empty then return [] - else if ι.isAppOfArity ``Sum 2 then - let (a, b) := (ι.getArg! 0, ι.getArg! 1) - return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ - (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) - else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" - -/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ -private partial def slotNumber : Slots → Expr → MetaM Nat - | .fin _, x => do - let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run - | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" - return i - | .unit, _ => return 0 - | .sum a b, x => do - let x ← whnfD x - if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! - else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) - else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" - -/-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each -free index and at each slot of each pair. -/ -private def readContraction (e : Expr) : MetaM ReadContraction := do - let some P := bundledArg? ``Tensors.Species.Tensor.contr e - | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" - let τ := e.consumeMData.appFn!.appArg!.appArg! - let some (slots, _, _) ← literalTensorType? (← inferType P) - | throwError "tensor_regroup: the slots or colors of a product are not literal" - let fs := factors P - let sizes ← fs.mapM fun f => do - let some (s, _, _) ← literalTensorType? (← inferType f) - | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" - return s.size - -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. - let args := (← whnfR (← inferType τ)).getAppArgs - let cargs := args[args.size - 1]!.getAppArgs - let (κ, J) := (cargs[1]!, cargs[2]!) - let JJ ← mkAppM ``Sum #[J, J] - let at_ (y : Expr) : MetaM Nat := do - slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) - let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) - let pairs ← (← slotElems J).mapM fun j => do - let half (left : Bool) := do - at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) - #[J, J, j]]) - return (← half true, ← half false) - return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ - /-- The ways of sending the slots of `A` to slots of `G`, factor by factor, each factor of `A` to an equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list of images of the slots of `A`, together with the factors of `G` that are hit. -/ diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index ec31b099af..a942f70070 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -190,6 +190,15 @@ lemma contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : rw [formTensor, formTensor, ← inverse_form c] exact contr_mul_inverse _ (toLin_form_bijective c) +/-- `g^μν g_νρ = δ_ρ^μ` at an upper color `c`, with the delta read with its slots swapped. -/ +lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : + S.reindex (ColorEquiv.swapTwo c c.dual) (Tensor.contr (ColorEquiv.matMul c c c.dual) + (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual))) = + S.deltaTensor c := by + rw [contr_formTensor_formTensor, deltaTensor, reindex_twoSlotEquiv, + show TensorProduct.comm k _ _ (S.delta c.dual) = S.delta c from S.comm_delta c.dual] + rfl + end Species end Tensors diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index d573294fdf..0f8670f09e 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Species /-! @@ -129,6 +130,10 @@ noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] +/-- Swapping the factors of the delta at `c` gives the delta at `c.dual`, `δ_μ^ν = δ^ν_μ`. -/ +lemma comm_delta (c : Color K) : TensorProduct.comm k _ _ (S.delta c) = S.delta c.dual := by + simp only [delta, map_sum, TensorProduct.comm_tmul] + /-- The delta is invariant, `Λ δ = δ`, with `Λ` acting on both factors. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by @@ -177,6 +182,30 @@ lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin, toMatrix_toLin c.dual, ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] +/-- `toLin` of the swapped tensor reads `z` along its second index, `v^μ ↦ z_νμ v^μ`, so its matrix +is the matrix of components of `z`. -/ +lemma toMatrix_toLin_comm (c : Color K) {d : Color K} (z : S.V d ⊗[k] S.V c) : + LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin (TensorProduct.comm k _ _ z)) = + Matrix.of fun ν μ => ((S.basis d).tensorProduct (S.basis c)).repr z (ν, μ) := by + rw [toMatrix_toLin] + ext ν μ + simp [Module.Basis.tensorProduct_repr_comm] + +/-- Swapping the indices of `F` and `F'` swaps the order of composition, `F F' = 1` read along the +second indices from `F' F = 1` read along the first. -/ +lemma toLin_comm_comp_toLin_comm {c : Color K} (F : S.V c ⊗[k] S.V c) + (F' : S.V c.dual ⊗[k] S.V c.dual) (h : S.toLin F' ∘ₗ S.toLin F = LinearMap.id) : + S.toLin (TensorProduct.comm k _ _ F) ∘ₗ S.toLin (TensorProduct.comm k _ _ F') = + LinearMap.id := by + have h := congrArg (LinearMap.toMatrix (S.basis c.dual) (S.basis c.dual)) h + rw [LinearMap.toMatrix_comp _ (S.basis c), toMatrix_toLin, toMatrix_toLin c.dual, + ← Matrix.transpose_mul, LinearMap.toMatrix_id, ← Matrix.transpose_one, + Matrix.transpose_inj] at h + apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective + rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin_comm, toMatrix_toLin_comm c.dual, + LinearMap.toMatrix_id] + exact h + end Species end Tensors diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 9e4916304c..6759f9f4fc 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -7,7 +7,6 @@ module public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SesquilinearForm -public import Physlib.Mathematics.ForMathlib.TensorProductMatrix public import Physlib.Mathematics.Tensors.Contraction public import Mathlib.Tactic.LinearCombination /-! @@ -236,17 +235,20 @@ lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c. -/ variable (S) in -/-- The vector `v` of color `c` contracted with the first index of the form at `c.dual`. It lowers -an upper index, `v^μ ↦ v^μ g_μν`, and raises a lower one, `w_μ ↦ w_μ g^μν`. -/ +/-- The vector `v` of color `c` contracted with the second index of the form at `c.dual`. It +lowers an upper index, `v^μ ↦ g_νμ v^μ`, and raises a lower one, `w_μ ↦ g^νμ w_μ`. -/ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.rep c.dual) := - .mk (LinearEquiv.ofLinearMap (S.toLin (form c.dual)) (S.toLin (form c)) (form_inverse c.dual) - (form_inverse c)) fun g => LinearMap.ext fun v => by - change S.toLin (form c.dual) _ = S.rep c.dual g (S.toLin (form c.dual) v) - rw [← S.toLin_map, form_invariant] - -/-- `toDual` contracts `v` with the first index of the form at `c.dual`. -/ + .mk (LinearEquiv.ofLinearMap (S.toLin (TensorProduct.comm k _ _ (form c.dual))) + (S.toLin (TensorProduct.comm k _ _ (form c))) + (toLin_comm_comp_toLin_comm _ _ (form_inverse c)) + (toLin_comm_comp_toLin_comm _ _ (form_inverse c.dual))) fun g => LinearMap.ext fun v => by + change S.toLin (TensorProduct.comm k _ _ (form c.dual)) _ = + S.rep c.dual g (S.toLin (TensorProduct.comm k _ _ (form c.dual)) v) + rw [← S.toLin_map c.dual g, TensorProduct.map_comm, form_invariant] + +/-- `toDual` contracts `v` with the second index of the form at `c.dual`. -/ lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : - S.toDual c v = S.toLin (form c.dual) v := rfl + S.toDual c v = S.toLin (TensorProduct.comm k _ _ (form c.dual)) v := rfl /-- Raising is the inverse of lowering. The inverse of `S.toDual c` is `S.toDual c.dual`. -/ lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDual c.dual := rfl @@ -257,12 +259,12 @@ lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : S.toDual c.dual (S.toDual c v) = v := (S.toDual c).toLinearEquiv.symm_apply_apply v -/-- Lowering or raising a vector in components, `v_ν = v^μ g_μν`. -/ +/-- Lowering or raising a vector in components, `v_ν = g_νμ v^μ`. -/ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : (S.basis c.dual).repr (S.toDual c v) ν = ∑ μ, (S.basis c).repr v μ * - S.formMatrix c.dual μ ν := by + S.formMatrix c.dual ν μ := by rw [toDual_apply, basis_repr_toLin] - rfl + simp [formMatrix, Module.Basis.tensorProduct_repr_comm] /-! @@ -344,14 +346,14 @@ lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i S.mapAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).toLinearMap h t := congrAt_eq_mapAt i _ h t -/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = T^…μ… g_μν`. The +/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = g_νμ T^…μ…`. The labels `φ` of the result are cast to labels of `t`, and the label at `i` is replaced by the summed `μ`. For concrete colors the casts are along `rfl`. -/ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (S.toDualAt i a ha hi h t) φ = - ∑ μ, S.formMatrix a.dual μ (cast (congrArg (basisIdx ∘ Color.kind) hi) (φ i)) * + ∑ μ, S.formMatrix a.dual (cast (congrArg (basisIdx ∘ Color.kind) hi) (φ i)) μ * (S.tensorBasis c).repr t (Function.update (fun x => cast (by by_cases hx : x = i · subst hx; rw [hi, ha] @@ -365,13 +367,14 @@ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a rw [basis_repr_cast, basis_repr_toDual] simp [Finsupp.single_apply] -/-- Lowering or raising at the slot `i` is contracting that slot with the first index of the form, -`T^…_ν… = T^…μ… g_μν`. -/ +/-- Lowering or raising at the slot `i` is contracting that slot with the second index of the +form, `T^…_ν… = g_νμ T^…μ…`. -/ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) - (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.formTensor a.dual)) := by - rw [formTensor, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] + (S.tmulEquiv c ![a.dual, a.dual] + (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a.dual a.dual) (S.formTensor a.dual))) := by + rw [formTensor, reindex_twoSlotEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] rfl /-! diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index ef208ac36c..f6d779f614 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -121,7 +121,8 @@ lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).ki tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, tensorBasis_repr_pauliTensor] -- Only the diagonal entry `η_μμ` of `η` contributes. - exact Fintype.sum_eq_single _ fun x hx => by simp [formEntries, minkowskiMatrix.off_diag_zero hx] + exact Fintype.sum_eq_single _ fun x hx => by + simp [formEntries, minkowskiMatrix.off_diag_zero (Ne.symm hx)] /-- The components of `σT_{μαβ̇}`, with all indices lowered, are the entries of the adjugate of `σ̄^μ = η_μμ σ^μ`, transposed: `σT^{μβα}` at the labels `(μ, α, β)`. -/ @@ -166,7 +167,7 @@ lemma pauliCo_trace_pauliCoDown : lemma pauliContrDown_trace_pauliCo : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • δ' | ν μ}ᵀ := by have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = - {pauliTensor | ↓ρ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | ρ ν ⊗ η | ρ μ}ᵀ := by + {pauliTensor | ↓ρ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | ρ ν ⊗ η | μ ρ}ᵀ := by tensor_regroup rw [h, pauliCoDown_trace_pauliCo] tensor_regroup @@ -218,7 +219,8 @@ lemma pauliCo_contr_pauliContr : rw [map_smul, ← vectorToBispinor_bispinorToVector] refine congrArg vectorToBispinor ?_ simp only [bispinorToVector, LinearMap.coe_mk, AddHom.coe_mk] - tensor_regroup + -- Raising with `ε` contracts its second index, so both forms are swapped, with signs that cancel. + tensor_regroup using formTensor_upL_antisymm, formTensor_upR_antisymm /-! @@ -230,7 +232,7 @@ lemma pauliCo_contr_pauliContr : lemma pauliContr_trace_pauliContrDown : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = (2 : ℂ) • η | μ ν}ᵀ := by have h : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = - {pauliTensor | ν ↓α ↓β ⊗ pauliTensor | ↓ρ α β}ᵀ | ν ρ ⊗ η | ρ μ}ᵀ := by + {pauliTensor | ν ↓α ↓β ⊗ pauliTensor | ↓ρ α β}ᵀ | ν ρ ⊗ η | μ ρ}ᵀ := by tensor_regroup rw [h, pauliContrDown_trace_pauliCo] tensor_regroup @@ -240,32 +242,32 @@ lemma pauliContrDown_trace_pauliContr : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = (2 : ℂ) • η | μ ν}ᵀ := by tensor_regroup using pauliContr_trace_pauliContrDown -/-- `σT^{μρβ̇} σT^{νγδ̇} ε_{δ̇β̇} (ε_{γρ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity +/-- `σT^{μρβ̇} σT^{νγδ̇} ε_{β̇δ̇} (ε_{ργ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity `pauliContr_trace_pauliContrDown` with the lowerings written as contractions with `ε`. -/ lemma pauliContr_pauliContr_eps_epsL_deltaL : - {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | d β ⊗ - {εL' | g r ⊗ δL | α α'}ᵀ | g r α α' = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by + {pauliTensor | μ r β ⊗ pauliTensor | ν g d ⊗ εR' | β d ⊗ + {εL' | r g ⊗ δL | α α'}ᵀ | r g α α' = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by tensor_regroup using pauliContr_trace_pauliContrDown /-- The anticommutator `σ^μ σ̄^ν + σ^ν σ̄^μ = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{α'β̇} + σT^{ναβ̇} σT^μ_{α'β̇} = 2 η^μν δ^α_α'`. The Schouten identity splits -`ε_γρ δ^α_α'` in `pauliContr_pauliContr_eps_epsL_deltaL` into the two terms. -/ +`ε_ργ δ^α_α'` in `pauliContr_pauliContr_eps_epsL_deltaL` into the two terms. -/ lemma pauliContr_mul_pauliContrDown_add : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α' ↓β + pauliTensor | ν α β ⊗ pauliTensor | μ ↓α' ↓β = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by rw [← pauliContr_pauliContr_eps_epsL_deltaL, schouten_downL] tensor_regroup -/-- `σT^{μγδ̇} σT^{ναρ̇} ε_{γα} (ε_{δ̇ρ̇} δ_β̇^β̇') = 2 η^μν δ_β̇^β̇'`, the trace identity +/-- `σT^{μγδ̇} σT^{ναρ̇} ε_{αγ} (ε_{ρ̇δ̇} δ_β̇^β̇') = 2 η^μν δ_β̇^β̇'`, the trace identity `pauliContrDown_trace_pauliContr` with the lowerings written as contractions with `ε`. -/ lemma pauliContr_pauliContr_eps_epsR_deltaR : - {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | g a ⊗ - {εR' | d r ⊗ δR' | β β'}ᵀ | d r β β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by + {pauliTensor | μ g d ⊗ pauliTensor | ν a r ⊗ εL' | a g ⊗ + {εR' | r d ⊗ δR' | β β'}ᵀ | r d β β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by tensor_regroup using pauliContrDown_trace_pauliContr /-- The anticommutator `σ̄^μ σ^ν + σ̄^ν σ^μ = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇'} + σT^ν_{αβ̇} σT^{μαβ̇'} = 2 η^μν δ_β̇^β̇'`. The Schouten identity splits -`ε_δ̇ρ̇ δ_β̇^β̇'` in `pauliContr_pauliContr_eps_epsR_deltaR` into the two terms. -/ +`ε_ρ̇δ̇ δ_β̇^β̇'` in `pauliContr_pauliContr_eps_epsR_deltaR` into the two terms. -/ lemma pauliContrDown_pauliContr_mul_add : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β' + pauliTensor | ν ↓α ↓β ⊗ pauliTensor | μ α β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by diff --git a/Physlib/Relativity/Fermions/Weyl/Metric.lean b/Physlib/Relativity/Fermions/Weyl/Metric.lean index 50d2f4aa00..891fb4584e 100644 --- a/Physlib/Relativity/Fermions/Weyl/Metric.lean +++ b/Physlib/Relativity/Fermions/Weyl/Metric.lean @@ -75,10 +75,10 @@ lemma metricRaw_comm_star (M : SL(2,ℂ)) : metricRaw * M.1.map star = ((M.1)⁻ rw [eta_fin_two (!![M.1 0 0, M.1 0 1; M.1 1 0, M.1 1 1].map star)] simp -/-- Lowering both indices of a `2 × 2` matrix with the metric gives its adjugate, transposed: -`∑ x y, ε_{xb} ε_{ya} M^{yx} = (adj M)_{ba}`, the components of `ε Mᵀ εᵀ = adj M`. -/ +/-- Lowering both indices of a `2 × 2` matrix with the metric `ε_ab = -metricRaw a b` gives its +adjugate, transposed: `∑ x y, ε_{bx} ε_{ay} M^{yx} = (adj M)_{ba}`. -/ lemma sum_metricRaw_mul_sum_metricRaw_mul (M : Matrix (Fin 2) (Fin 2) ℂ) (a b : Fin 2) : - ∑ x, metricRaw x b * ∑ y, metricRaw y a * M y x = adjugate M b a := by + ∑ x, (-metricRaw) b x * ∑ y, (-metricRaw) a y * M y x = adjugate M b a := by fin_cases a <;> fin_cases b <;> simp [Fin.sum_univ_two, metricRaw, adjugate_fin_two] /-! diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 5b34c4bb54..fc48a9a5ad 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -35,9 +35,9 @@ representation is real, so this conjugation commutes with `SL(2, ℂ)` The invariant form is `ε` at the Weyl colors and `η` at the vector colors. The form `ε` is antisymmetric and `η` symmetric (`IsAntisymmetricForm`, `IsSymmetricForm`). The form -`ε^αβ` has the matrix `!![0, -1; 1, 0]` and `ε_αβ` the matrix `!![0, 1; -1, 0]`. Lowering contracts -the first index of the form, `ψ_β = ψ^α ε_αβ`. This is Wess and Bagger's `ψ_α = ε_αβ ψ^β`, where -`ε_12 = -1` and `ε^12 = 1`, because their forms are the negatives of `εL'` and `εL`. The +`ε^αβ` has the matrix `!![0, 1; -1, 0]` and `ε_αβ` the matrix `!![0, -1; 1, 0]`, so `ε^12 = 1` and +`ε_12 = -1`. Lowering contracts the second index of the form, `ψ_α = ε_αβ ψ^β`, as in Wess and +Bagger. The forms are written `εL`, `εL'`, `εR`, `εR'`, `η` and `η'`, and the deltas `δL`, `δL'`, `δR`, `δR'`, `δ` and `δ'`. Their components are stated through `tensorBasis`, and the antisymmetry of `ε` and the symmetry of `η` are stated in index notation. @@ -245,12 +245,13 @@ end complexLorentz namespace complexLorentz -/-- The invariant form at a color: `ε` at the Weyl colors and `η` at the vector colors. -/ +/-- The invariant form at a color: `ε` at the Weyl colors and `η` at the vector colors. At the Weyl +colors it is the negative of the metric of the Weyl representation, so that `ε^12 = 1`. -/ noncomputable def form : (c : Color Kind) → complexLorentz.V c ⊗[ℂ] complexLorentz.V c - | ⟨left, .up⟩ => LeftHandedWeyl.metricVal - | ⟨left, .down⟩ => DualLeftHandedWeyl.metricVal - | ⟨right, .up⟩ => RightHandedWeyl.metricVal - | ⟨right, .down⟩ => DualRightHandedWeyl.metricVal + | ⟨left, .up⟩ => -LeftHandedWeyl.metricVal + | ⟨left, .down⟩ => -DualLeftHandedWeyl.metricVal + | ⟨right, .up⟩ => -RightHandedWeyl.metricVal + | ⟨right, .down⟩ => -DualRightHandedWeyl.metricVal | ⟨vec, .up⟩ => ComplexVector.metricVal | ⟨vec, .down⟩ => ComplexCoVector.metricVal @@ -258,22 +259,24 @@ noncomputable def form : (c : Color Kind) → complexLorentz.V c ⊗[ℂ] comple the trivial representation. -/ lemma form_invariant : ∀ (c : Color Kind) (g : SL(2, ℂ)), TensorProduct.map (complexLorentz.rep c g) (complexLorentz.rep c g) (form c) = form c - | ⟨left, .up⟩ => LeftHandedWeyl.metric.map_one_invariant LeftHandedWeyl.metric_apply_one - | ⟨left, .down⟩ => - DualLeftHandedWeyl.metric.map_one_invariant DualLeftHandedWeyl.metric_apply_one - | ⟨right, .up⟩ => RightHandedWeyl.metric.map_one_invariant RightHandedWeyl.metric_apply_one - | ⟨right, .down⟩ => - DualRightHandedWeyl.metric.map_one_invariant DualRightHandedWeyl.metric_apply_one + | ⟨left, .up⟩ => fun g => (map_neg _ _).trans <| congrArg Neg.neg <| + LeftHandedWeyl.metric.map_one_invariant LeftHandedWeyl.metric_apply_one g + | ⟨left, .down⟩ => fun g => (map_neg _ _).trans <| congrArg Neg.neg <| + DualLeftHandedWeyl.metric.map_one_invariant DualLeftHandedWeyl.metric_apply_one g + | ⟨right, .up⟩ => fun g => (map_neg _ _).trans <| congrArg Neg.neg <| + RightHandedWeyl.metric.map_one_invariant RightHandedWeyl.metric_apply_one g + | ⟨right, .down⟩ => fun g => (map_neg _ _).trans <| congrArg Neg.neg <| + DualRightHandedWeyl.metric.map_one_invariant DualRightHandedWeyl.metric_apply_one g | ⟨vec, .up⟩ => ComplexVector.metric.map_one_invariant ComplexVector.metric_apply_one | ⟨vec, .down⟩ => ComplexCoVector.metric.map_one_invariant ComplexCoVector.metric_apply_one -/-- The entries of the form at each color, `ε^αβ = !![0, -1; 1, 0]` at the upper Weyl colors, -`ε_αβ = !![0, 1; -1, 0]` at the lower ones, and `η` at the vector colors. -/ +/-- The entries of the form at each color, `ε^αβ = !![0, 1; -1, 0]` at the upper Weyl colors, +`ε_αβ = !![0, -1; 1, 0]` at the lower ones, and `η` at the vector colors. -/ noncomputable def formEntries : (c : Color Kind) → Idx c.kind → Idx c.kind → ℂ - | ⟨left, .up⟩ => -metricRaw - | ⟨left, .down⟩ => metricRaw - | ⟨right, .up⟩ => -metricRaw - | ⟨right, .down⟩ => metricRaw + | ⟨left, .up⟩ => metricRaw + | ⟨left, .down⟩ => -metricRaw + | ⟨right, .up⟩ => metricRaw + | ⟨right, .down⟩ => -metricRaw | ⟨vec, _⟩ => fun μ ν => minkowskiMatrix μ ν /-- The coordinates of the form in Mathlib's product basis are `formEntries`: each form is defined @@ -281,23 +284,35 @@ by its matrix. -/ lemma basis_tensorProduct_repr_form : ∀ (c : Color Kind) (μ ν : Idx c.kind), ((complexLorentz.basis c).tensorProduct (complexLorentz.basis c)).repr (form c) (μ, ν) = formEntries c μ ν - | ⟨left, .up⟩, μ, ν => congrFun (congrFun (leftLeftToMatrix.apply_symm_apply (-metricRaw)) μ) ν - | ⟨left, .down⟩, μ, ν => - congrFun (congrFun (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) μ) ν - | ⟨right, .up⟩, μ, ν => congrFun (congrFun (rightRightToMatrix.apply_symm_apply (-metricRaw)) μ) ν - | ⟨right, .down⟩, μ, ν => - congrFun (congrFun (dualRightDualRightToMatrix.apply_symm_apply metricRaw) μ) ν + | ⟨left, .up⟩, μ, ν => (DFunLike.congr_fun (map_neg _ (LeftHandedWeyl.metricVal : + complexLorentz.V ⟨left, .up⟩ ⊗[ℂ] complexLorentz.V ⟨left, .up⟩)) (μ, ν)).trans <| + neg_eq_iff_eq_neg.mpr + (congrFun (congrFun (leftLeftToMatrix.apply_symm_apply (-metricRaw)) μ) ν) + | ⟨left, .down⟩, μ, ν => (DFunLike.congr_fun (map_neg _ (DualLeftHandedWeyl.metricVal : + complexLorentz.V ⟨left, .down⟩ ⊗[ℂ] complexLorentz.V ⟨left, .down⟩)) (μ, ν)).trans <| + neg_eq_iff_eq_neg.mpr + ((congrFun (congrFun (dualLeftdualLeftToMatrix.apply_symm_apply metricRaw) μ) ν).trans + (neg_neg _).symm) + | ⟨right, .up⟩, μ, ν => (DFunLike.congr_fun (map_neg _ (RightHandedWeyl.metricVal : + complexLorentz.V ⟨right, .up⟩ ⊗[ℂ] complexLorentz.V ⟨right, .up⟩)) (μ, ν)).trans <| + neg_eq_iff_eq_neg.mpr + (congrFun (congrFun (rightRightToMatrix.apply_symm_apply (-metricRaw)) μ) ν) + | ⟨right, .down⟩, μ, ν => (DFunLike.congr_fun (map_neg _ (DualRightHandedWeyl.metricVal : + complexLorentz.V ⟨right, .down⟩ ⊗[ℂ] complexLorentz.V ⟨right, .down⟩)) (μ, ν)).trans <| + neg_eq_iff_eq_neg.mpr + ((congrFun (congrFun (dualRightDualRightToMatrix.apply_symm_apply metricRaw) μ) ν).trans + (neg_neg _).symm) | ⟨vec, .up⟩, μ, ν => congrFun (congrFun (contrContrToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν | ⟨vec, .down⟩, μ, ν => congrFun (congrFun (coCoToMatrix.apply_symm_apply ((@minkowskiMatrix 3).map Complex.ofRealHom)) μ) ν -/-- The matrices of the forms at `c.dual` and at `c` are inverse, `ε (-ε) = 1` and `η η = 1`. -/ +/-- The matrices of the forms at `c.dual` and at `c` are inverse, `(-ε) ε = 1` and `η η = 1`. -/ lemma formEntries_dual_mul : ∀ c : Color Kind, Matrix.of (formEntries c.dual) * Matrix.of (formEntries c) = 1 - | ⟨left, .up⟩ | ⟨right, .up⟩ => show metricRaw * -metricRaw = 1 by + | ⟨left, .up⟩ | ⟨right, .up⟩ => show -metricRaw * metricRaw = 1 by simpa [metricRaw] using Matrix.one_fin_two.symm - | ⟨left, .down⟩ | ⟨right, .down⟩ => show -metricRaw * metricRaw = 1 by + | ⟨left, .down⟩ | ⟨right, .down⟩ => show metricRaw * -metricRaw = 1 by simpa [metricRaw] using Matrix.one_fin_two.symm | ⟨vec, _⟩ => show (@minkowskiMatrix 3).map Complex.ofRealHom * (@minkowskiMatrix 3).map Complex.ofRealHom = 1 by @@ -433,24 +448,24 @@ lemma tensorBasis_repr_formTensor (c : Color Kind) (μ : Π i, Idx (![c, c] i).k formEntries c (μ 0) (μ 1) := (Species.tensorBasis_repr_formTensor c μ).trans (formMatrix_apply c _ _) -/-- The components of `ε^αβ` are the matrix `!![0, -1; 1, 0]`. -/ +/-- The components of `ε^αβ` are the matrix `!![0, 1; -1, 0]`. -/ lemma tensorBasis_repr_formTensor_upL (μ : Π i, Idx (![upL, upL] i).kind) : - (complexLorentz.tensorBasis ![upL, upL]).repr εL μ = (-metricRaw) (μ 0) (μ 1) := + (complexLorentz.tensorBasis ![upL, upL]).repr εL μ = metricRaw (μ 0) (μ 1) := tensorBasis_repr_formTensor upL μ -/-- The components of `ε_αβ` are the matrix `!![0, 1; -1, 0]`. -/ +/-- The components of `ε_αβ` are the matrix `!![0, -1; 1, 0]`. -/ lemma tensorBasis_repr_formTensor_downL (μ : Π i, Idx (![downL, downL] i).kind) : - (complexLorentz.tensorBasis ![downL, downL]).repr εL' μ = metricRaw (μ 0) (μ 1) := + (complexLorentz.tensorBasis ![downL, downL]).repr εL' μ = (-metricRaw) (μ 0) (μ 1) := tensorBasis_repr_formTensor downL μ -/-- The components of `ε^α̇β̇` are the matrix `!![0, -1; 1, 0]`. -/ +/-- The components of `ε^α̇β̇` are the matrix `!![0, 1; -1, 0]`. -/ lemma tensorBasis_repr_formTensor_upR (μ : Π i, Idx (![upR, upR] i).kind) : - (complexLorentz.tensorBasis ![upR, upR]).repr εR μ = (-metricRaw) (μ 0) (μ 1) := + (complexLorentz.tensorBasis ![upR, upR]).repr εR μ = metricRaw (μ 0) (μ 1) := tensorBasis_repr_formTensor upR μ -/-- The components of `ε_α̇β̇` are the matrix `!![0, 1; -1, 0]`. -/ +/-- The components of `ε_α̇β̇` are the matrix `!![0, -1; 1, 0]`. -/ lemma tensorBasis_repr_formTensor_downR (μ : Π i, Idx (![downR, downR] i).kind) : - (complexLorentz.tensorBasis ![downR, downR]).repr εR' μ = metricRaw (μ 0) (μ 1) := + (complexLorentz.tensorBasis ![downR, downR]).repr εR' μ = (-metricRaw) (μ 0) (μ 1) := tensorBasis_repr_formTensor downR μ /-- The components of `η^μν` and `η_μν` are the Minkowski matrix `diag(1, -1, -1, -1)`. -/ @@ -543,16 +558,20 @@ lemma formTensor_up_symm : {η | μ ν = η | ν μ}ᵀ := by lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by tensor_regroup -/-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_γα' δ_ρ^α - ε_ρα' δ_γ^α`. -/ +/-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_α'ρ δ_γ^α - ε_α'γ δ_ρ^α`. The +free lower index `α'` is the first index of each `ε`, the one that lowering contracts. -/ lemma schouten_downL : - {εL' | γ ρ ⊗ δL | α α' = εL' | γ α' ⊗ δL' | ρ α + -{εL' | ρ α' ⊗ δL' | γ α}ᵀ | ρ α' γ α}ᵀ := - Species.schouten_formTensor_deltaTensor downL (Fintype.card_fin 2) + {εL' | γ ρ ⊗ δL | α α' = εL' | α' ρ ⊗ δL' | γ α + -{εL' | α' γ ⊗ δL' | ρ α}ᵀ | α' γ ρ α}ᵀ := by + tensor_regroup using Species.schouten_formTensor_deltaTensor (S := complexLorentz) downL + (Fintype.card_fin 2) /-- The Schouten identity for right-handed indices, -`ε_γ̇ρ̇ δ_α̇^α̇' = ε_γ̇α̇ δ_ρ̇^α̇' - ε_ρ̇α̇ δ_γ̇^α̇'`. -/ +`ε_γ̇ρ̇ δ_α̇^α̇' = ε_α̇ρ̇ δ_γ̇^α̇' - ε_α̇γ̇ δ_ρ̇^α̇'`. The free lower index `α̇` is the first +index of each `ε`. -/ lemma schouten_downR : - {εR' | γ ρ ⊗ δR' | α α' = εR' | γ α ⊗ δR' | ρ α' + -{εR' | ρ α ⊗ δR' | γ α'}ᵀ | ρ α γ α'}ᵀ := - Species.schouten_formTensor_deltaTensor_dual downR (Fintype.card_fin 2) + {εR' | γ ρ ⊗ δR' | α α' = εR' | α ρ ⊗ δR' | γ α' + -{εR' | α γ ⊗ δR' | ρ α'}ᵀ | α γ ρ α'}ᵀ := by + tensor_regroup using Species.schouten_formTensor_deltaTensor_dual (S := complexLorentz) downR + (Fintype.card_fin 2) /-- Swapping the indices of the delta at `c` gives the delta at `c.dual`, as `δ^μ_ν = δ_ν^μ`. -/ lemma deltaTensor_swap (c : Color Kind) : @@ -643,7 +662,7 @@ example (c : Color Kind) : {complexLorentz.formTensor c | α β ⊗ lemma tensorBasis_repr_formTensor_upL_tmul_upR (μ : Π i, Idx (![upL, upL, upR, upR] i).kind) : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εR | α' β'}ᵀ μ = - (-metricRaw) (μ 0) (μ 1) * (-metricRaw) (μ 2) (μ 3) := by + metricRaw (μ 0) (μ 1) * metricRaw (μ 2) (μ 3) := by rw [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] rfl @@ -654,7 +673,7 @@ example : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εL' | β γ}ᵀ rw [Species.Tensor.tensorBasis_repr_contr_one_pair] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor downL] - change ∑ β : Fin 2, (-metricRaw) 0 β * metricRaw β 0 = 1 + change ∑ β : Fin 2, metricRaw 0 β * (-metricRaw) β 0 = 1 simp [metricRaw, Fin.sum_univ_two] /-- The conjugate of `ε^αβ` is `ε^α̇β̇`. The conjugate lands on `![upR, upR]` directly. -/ @@ -662,7 +681,7 @@ example : Species.Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) rw [tensorBasis_repr_conj, tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] -- The labels are `idxBar` along `rfl` of `μ 0` and `μ 1`, the identity. - have h (a b : Fin 2) : star ((-metricRaw) a b) = (-metricRaw) a b := by + have h (a b : Fin 2) : star (metricRaw a b) = metricRaw a b := by fin_cases a <;> fin_cases b <;> simp [metricRaw] exact h _ _ From 76a5bd1e31de35d1ab94052e43eb46bb1670d4ab Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 16:43:48 +0000 Subject: [PATCH 117/242] =?UTF-8?q?refactor(Pauli):=20state=20the=20lowere?= =?UTF-8?q?d=20Pauli=20tensors=20with=20=CF=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The statements in Pauli/Tensor.lean write lowered indices as τ(μ) instead of ↓μ. The module doc states the second-index lowering convention, σT_μ = η_μν σT^ν. Co-authored-by: Claude Opus 5.5 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index f6d779f614..6b71372604 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -23,10 +23,11 @@ colors: the numbers that the literature writes `σ^μ_{αβ̇}` sit here at uppe the tensor of `ComplexVector.pauliVal` in `ComplexVector ⊗ (LeftHandedWeyl ⊗ RightHandedWeyl)`, so it is invariant under `SL(2, ℂ)` because `ComplexVector.pauli` is an intertwining map. -The lowered forms are written with `↓` in index notation, such as `{pauliTensor | ↓μ α β}ᵀ` for -`σT_μ^{αβ̇}`. Lowering contracts the first index of `η` or `ε` (`Species.toDual`), so -`σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, `{pauliTensor | μ ↓α ↓β}ᵀ` is -`σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix `σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. +The lowered forms are written with `τ` in index notation, such as `{pauliTensor | τ(μ) α β}ᵀ` for +`σT_μ^{αβ̇}`. Lowering contracts the second index of `η` or `ε` (`Species.toDual`), so +`σT_μ^{αβ̇} = η_μν σT^{ναβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, +`{pauliTensor | μ τ(α) τ(β)}ᵀ` is `σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix +`σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. Lowering both indices of a matrix with `ε` gives its adjugate, transposed, and `adj σ^μ = σ̄^μ`, so the components of the lowered forms are matrix identities. @@ -116,7 +117,7 @@ lemma pauliTensor_invariant (g : SL(2, ℂ)) : g • pauliTensor = pauliTensor : /-- The components of `σT_μ^{αβ̇} = η_μμ σT^{μαβ̇}`. -/ lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).kind) : - (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν α β}ᵀ μ = + (complexLorentz.tensorBasis _).repr {pauliTensor | τ(ν) α β}ᵀ μ = minkowskiMatrix (μ 0) (μ 0) * pauliMatrix (μ 0) (μ 1) (μ 2) := by tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, tensorBasis_repr_pauliTensor] @@ -128,7 +129,7 @@ lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).ki `σ̄^μ = η_μμ σ^μ`, transposed: `σT^{μβα}` at the labels `(μ, α, β)`. -/ lemma tensorBasis_repr_pauliTensor_downAll (μ : Π i, Idx (![down, downL, downR] i).kind) : - (complexLorentz.tensorBasis _).repr {pauliTensor | ↓ν ↓α ↓β}ᵀ μ = + (complexLorentz.tensorBasis _).repr {pauliTensor | τ(ν) τ(α) τ(β)}ᵀ μ = pauliMatrix (μ 0) (μ 2) (μ 1) := by -- `↓` unfolds the lowered vector index by `tensorBasis_repr_pauliTensor_down` first. tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, @@ -151,7 +152,7 @@ anticommutators by the Schouten identity of `ε`. /-- The trace identity `tr(σ̄_μ σ_ν) = 2 η_μν`, as `σT_{μαβ̇} σT_ν^{αβ̇} = 2 η_μν`. -/ lemma pauliCoDown_trace_pauliCo : - {pauliTensor | ↓μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • η' | μ ν}ᵀ := by + {pauliTensor | τ(μ) τ(α) τ(β) ⊗ pauliTensor | τ(ν) α β = (2 : ℂ) • η' | μ ν}ᵀ := by tensor_components [tensorBasis_repr_pauliTensor_downAll, tensorBasis_repr_pauliTensor_down, tensorBasis_repr_formTensor_vec .down] -- The components are `tr(σ^μᵀ σ̄^νᵀ) = tr(σ^μ σ̄^ν)`, written out as a double sum. @@ -160,14 +161,14 @@ lemma pauliCoDown_trace_pauliCo : /-- The trace identity `tr(σ_μ σ̄_ν) = 2 η_μν`, as `σT_μ^{αβ̇} σT_{ναβ̇} = 2 η_μν`. -/ lemma pauliCo_trace_pauliCoDown : - {pauliTensor | ↓μ α β ⊗ pauliTensor | ↓ν ↓α ↓β = (2 : ℂ) • η' | μ ν}ᵀ := by + {pauliTensor | τ(μ) α β ⊗ pauliTensor | τ(ν) τ(α) τ(β) = (2 : ℂ) • η' | μ ν}ᵀ := by tensor_regroup using pauliCoDown_trace_pauliCo /-- The mixed trace identity `tr(σ̄^μ σ_ν) = 2 δ^μ_ν`, as `σT^μ_{αβ̇} σT_ν^{αβ̇} = 2 δ^μ_ν`. -/ lemma pauliContrDown_trace_pauliCo : - {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = (2 : ℂ) • δ' | ν μ}ᵀ := by - have h : {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ↓ν α β = - {pauliTensor | ↓ρ ↓α ↓β ⊗ pauliTensor | ↓ν α β}ᵀ | ρ ν ⊗ η | μ ρ}ᵀ := by + {pauliTensor | μ τ(α) τ(β) ⊗ pauliTensor | τ(ν) α β = (2 : ℂ) • δ' | ν μ}ᵀ := by + have h : {pauliTensor | μ τ(α) τ(β) ⊗ pauliTensor | τ(ν) α β = + {pauliTensor | τ(ρ) τ(α) τ(β) ⊗ pauliTensor | τ(ν) α β}ᵀ | ρ ν ⊗ η | μ ρ}ᵀ := by tensor_regroup rw [h, pauliCoDown_trace_pauliCo] tensor_regroup @@ -181,14 +182,14 @@ lemma pauliContrDown_trace_pauliCo : /-- `p ↦ σT_μ^{αβ̇} p^{μα'β̇'}`, from a tensor with a vector index to one with two Weyl indices. On a vector, with no `α'β̇'`, it is the bispinor `p^{αβ̇}` of `p^μ`. -/ noncomputable def vectorToBispinor : ℂT[up, upL, upR] →ₗ[ℂ] ℂT[upL, upR, upL, upR] where - toFun p := {pauliTensor | ↓μ α β ⊗ p | μ α' β'}ᵀ + toFun p := {pauliTensor | τ(μ) α β ⊗ p | μ α' β'}ᵀ map_add' p q := by simp only [tmul_add, map_add] map_smul' r p := by simp only [tmul_smul, map_smul, RingHom.id_apply] /-- `X ↦ σT^μ_{αβ̇} X^{αβ̇α'β̇'}`, from a tensor with two Weyl indices to one with a vector index, the inverse of `vectorToBispinor` up to `2`. -/ noncomputable def bispinorToVector : ℂT[upL, upR, upL, upR] →ₗ[ℂ] ℂT[up, upL, upR] where - toFun X := {pauliTensor | μ ↓α ↓β ⊗ X | α β α' β'}ᵀ + toFun X := {pauliTensor | μ τ(α) τ(β) ⊗ X | α β α' β'}ᵀ map_add' X Y := by simp only [tmul_add, map_add] map_smul' r X := by simp only [tmul_smul, map_smul, RingHom.id_apply] @@ -208,13 +209,13 @@ lemma vectorToBispinor_bispinorToVector (X : ℂT[upL, upR, upL, upR]) : bispinorToVector_vectorToBispinor X /-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ -lemma pauliTensor_raise_lower : {{pauliTensor | μ ↓α ↓β}ᵀ | μ ↑α ↑β}ᵀ = pauliTensor := by +lemma pauliTensor_raise_lower : {{pauliTensor | μ τ(α) τ(β)}ᵀ | μ τ(α) τ(β)}ᵀ = pauliTensor := by tensor_slots /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is `vectorToBispinor σT = vectorToBispinor (bispinorToVector (εL ⊗ εR)) / 2`. -/ lemma pauliCo_contr_pauliContr : - {pauliTensor | ↓ν α β ⊗ pauliTensor | ν α' β' = + {pauliTensor | τ(ν) α β ⊗ pauliTensor | ν α' β' = (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by rw [map_smul, ← vectorToBispinor_bispinorToVector] refine congrArg vectorToBispinor ?_ @@ -230,16 +231,16 @@ lemma pauliCo_contr_pauliContr : /-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{αβ̇} = 2 η^μν`. -/ lemma pauliContr_trace_pauliContrDown : - {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = (2 : ℂ) • η | μ ν}ᵀ := by - have h : {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α ↓β = - {pauliTensor | ν ↓α ↓β ⊗ pauliTensor | ↓ρ α β}ᵀ | ν ρ ⊗ η | μ ρ}ᵀ := by + {pauliTensor | μ α β ⊗ pauliTensor | ν τ(α) τ(β) = (2 : ℂ) • η | μ ν}ᵀ := by + have h : {pauliTensor | μ α β ⊗ pauliTensor | ν τ(α) τ(β) = + {pauliTensor | ν τ(α) τ(β) ⊗ pauliTensor | τ(ρ) α β}ᵀ | ν ρ ⊗ η | μ ρ}ᵀ := by tensor_regroup rw [h, pauliContrDown_trace_pauliCo] tensor_regroup /-- The trace identity `tr(σ̄^μ σ^ν) = 2 η^μν`, as `σT^μ_{αβ̇} σT^{ναβ̇} = 2 η^μν`. -/ lemma pauliContrDown_trace_pauliContr : - {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β = (2 : ℂ) • η | μ ν}ᵀ := by + {pauliTensor | μ τ(α) τ(β) ⊗ pauliTensor | ν α β = (2 : ℂ) • η | μ ν}ᵀ := by tensor_regroup using pauliContr_trace_pauliContrDown /-- `σT^{μρβ̇} σT^{νγδ̇} ε_{β̇δ̇} (ε_{ργ} δ^α_α') = 2 η^μν δ^α_α'`, the trace identity @@ -253,7 +254,8 @@ lemma pauliContr_pauliContr_eps_epsL_deltaL : `σT^{μαβ̇} σT^ν_{α'β̇} + σT^{ναβ̇} σT^μ_{α'β̇} = 2 η^μν δ^α_α'`. The Schouten identity splits `ε_ργ δ^α_α'` in `pauliContr_pauliContr_eps_epsL_deltaL` into the two terms. -/ lemma pauliContr_mul_pauliContrDown_add : - {pauliTensor | μ α β ⊗ pauliTensor | ν ↓α' ↓β + pauliTensor | ν α β ⊗ pauliTensor | μ ↓α' ↓β = + {pauliTensor | μ α β ⊗ pauliTensor | ν τ(α') τ(β) + + pauliTensor | ν α β ⊗ pauliTensor | μ τ(α') τ(β) = (2 : ℂ) • {η | μ ν ⊗ δL | α α'}ᵀ | μ ν α α'}ᵀ := by rw [← pauliContr_pauliContr_eps_epsL_deltaL, schouten_downL] tensor_regroup @@ -269,7 +271,8 @@ lemma pauliContr_pauliContr_eps_epsR_deltaR : `σT^μ_{αβ̇} σT^{ναβ̇'} + σT^ν_{αβ̇} σT^{μαβ̇'} = 2 η^μν δ_β̇^β̇'`. The Schouten identity splits `ε_ρ̇δ̇ δ_β̇^β̇'` in `pauliContr_pauliContr_eps_epsR_deltaR` into the two terms. -/ lemma pauliContrDown_pauliContr_mul_add : - {pauliTensor | μ ↓α ↓β ⊗ pauliTensor | ν α β' + pauliTensor | ν ↓α ↓β ⊗ pauliTensor | μ α β' = + {pauliTensor | μ τ(α) τ(β) ⊗ pauliTensor | ν α β' + + pauliTensor | ν τ(α) τ(β) ⊗ pauliTensor | μ α β' = (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR] tensor_regroup From db1f4b0dc6e2ce50a49c8e5d7897ab5977708c1d Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 15:51:04 +0000 Subject: [PATCH 118/242] refactor(Tensors): toLin and the delta from Module.Basis.coordTensorHomEquiv; deltaTensor and contr_matMul move to LinearMap, after Contraction New ForMathlib/CoordTensorHom: coordTensorHomEquiv, coordTensorHomEquiv_tmul, coordTensorHomEquiv_lTensor, coordDelta, coordTensorHomEquiv_coordDelta, comm_coordDelta, repr_coordTensorHomEquiv, toMatrix_coordTensorHomEquiv, coordTensorHomEquiv_comp_of_mul_eq_one, toMatrix_coordTensorHomEquiv_comm and coordTensorHomEquiv_comm_comp_comm, for two bases with the same labels. Species.toLin and Species.delta are abbrevs for them; the Species lemmas with only basis content (toLin_tmul, toLin_lTensor, toLin_delta, comm_delta, basis_repr_toLin, toMatrix_toLin, toLin_comp_toLin_of_mul_eq_one, toMatrix_toLin_comm, toLin_comm_comp_toLin_comm) are removed, and Species.coordPairing_basis folds the pairing back into the contraction. LinearMap now imports Contraction and holds deltaTensor, deltaTensor_invariant, tensorBasis_repr_deltaTensor, tensorBasis_repr_deltaTensor_eq_one (from Product) and ColorEquiv.matMul, contr_matMul, contr_matMul_eq_deltaTensor_iff, ColorEquiv.matMulAt, contr_matMulAt_pure, contr_matMulAt, contr_deltaTensor, contr_deltaTensor_swap (from Contraction), so Product and Contraction no longer depend on toLin. Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../ForMathlib/CoordTensorHom.lean | 164 +++++++++ Physlib/Mathematics/Tensors/Contraction.lean | 158 +-------- Physlib/Mathematics/Tensors/Inverse.lean | 3 +- Physlib/Mathematics/Tensors/LinearMap.lean | 329 ++++++++++++------ Physlib/Mathematics/Tensors/Metric.lean | 15 +- Physlib/Mathematics/Tensors/Product.lean | 42 +-- .../Tensors/ComplexTensor/Species.lean | 2 +- .../Tensors/RealTensor/Species.lean | 2 +- 9 files changed, 409 insertions(+), 307 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/CoordTensorHom.lean diff --git a/Physlib.lean b/Physlib.lean index c1b52feb0d..9ccd8e6380 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -158,6 +158,7 @@ public import Physlib.Mathematics.Distribution.PowMul public import Physlib.Mathematics.Distribution.TemperedDistribution public import Physlib.Mathematics.ForMathlib.ComplexLinear public import Physlib.Mathematics.ForMathlib.Contraction +public import Physlib.Mathematics.ForMathlib.CoordTensorHom public import Physlib.Mathematics.ForMathlib.DataStructures.Matrix.LieTrace public import Physlib.Mathematics.ForMathlib.FDerivCurry public import Physlib.Mathematics.ForMathlib.Fin diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean new file mode 100644 index 0000000000..8654f149b2 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -0,0 +1,164 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.Contraction +public import Physlib.Mathematics.ForMathlib.Contraction +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix + +/-! + +# Tensors as linear maps through the coordinate pairing + +Two bases with the same labels read a tensor of `M ⊗ U` as a linear map `N → U`. + +## i. Overview + +Mathlib reads a tensor of `Module.Dual R N ⊗ U` as a linear map `N → U` +(`dualTensorHomEquivOfBasis`). When `M` is paired with `N` by the coordinate pairing of two bases +`b` and `b'` with the same labels (`Module.Basis.coordPairing`), `M` plays the role of the dual of +`N`, so a tensor of `M ⊗ U` is a linear map `N → U`. For a covector index this reads a metric +`g_μν` as the map `v^μ ↦ v^μ g_μν`. + +The delta `∑ I, b I ⊗ b' I` of the two bases is the tensor of the identity map. The matrix of the +linear map of `z` is the transpose of the matrix of components of `z`, so two tensors give inverse +maps when their matrices of components are inverse. + +## ii. Key results + +- `Module.Basis.coordTensorHomEquiv` : a tensor of `M ⊗ U` as a linear map `N → U`. +- `Module.Basis.coordDelta` : the delta `∑ I, b I ⊗ b' I`, and + `Module.Basis.coordTensorHomEquiv_coordDelta` : it is the identity map. +- `Module.Basis.toMatrix_coordTensorHomEquiv` : the matrix of the linear map is the transpose of + the matrix of components. +- `Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one` : two tensors give inverse maps when their + matrices of components are inverse. + +## iii. Table of contents + +- A. Tensors as linear maps through the coordinate pairing + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +namespace Module.Basis + +open TensorProduct + +/-! + +## A. Tensors as linear maps through the coordinate pairing + +-/ + +variable {R ι M N U U' : Type*} [CommRing R] [Fintype ι] [DecidableEq ι] [AddCommGroup M] + [Module R M] [AddCommGroup N] [Module R N] [AddCommGroup U] [Module R U] [AddCommGroup U'] + [Module R U'] + +/-- `M ⊗ U` as the linear maps `N → U`, reading `M` as the dual of `N` through the coordinate +pairing. It sends `x ⊗ y` to `v ↦ b'.coordPairing b (v ⊗ x) • y`. It is Mathlib's +`dualTensorHomEquivOfBasis`, after the coordinate pairing identifies `M` with the dual of `N`. -/ +noncomputable def coordTensorHomEquiv (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + M ⊗[R] U ≃ₗ[R] (N →ₗ[R] U) := + (TensorProduct.congr (TensorProduct.curry (b'.coordPairing b)).flip.toPerfPair + (LinearEquiv.refl R U)).trans (dualTensorHomEquivOfBasis b') + +/-- A pure tensor `x ⊗ y` acts as `v ↦ b'.coordPairing b (v ⊗ x) • y`. -/ +@[simp] +lemma coordTensorHomEquiv_tmul (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (x : M) (y : U) + (v : N) : b.coordTensorHomEquiv b' (x ⊗ₜ[R] y) v = b'.coordPairing b (v ⊗ₜ[R] x) • y := + rfl + +/-- Applying `f` to the second factor composes with `f`, `(id ⊗ f) z = f ∘ z`. -/ +lemma coordTensorHomEquiv_lTensor (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (f : U →ₗ[R] U') (z : M ⊗[R] U) : + b.coordTensorHomEquiv b' (f.lTensor M z) = f ∘ₗ b.coordTensorHomEquiv b' z := by + induction z using TensorProduct.inductionOn with + | tmul x y => ext v; simp + | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] + +/-- The delta `∑ I, b I ⊗ b' I` of two bases with the same labels. -/ +noncomputable def coordDelta (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] N := + ∑ I, b I ⊗ₜ b' I + +/-- The delta read as a linear map is the identity. -/ +lemma coordTensorHomEquiv_coordDelta (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + b.coordTensorHomEquiv b' (b.coordDelta b') = LinearMap.id := + b'.ext fun J => by + simp [coordDelta, coordPairing_basis_basis, Finset.sum_ite_eq] + +omit [DecidableEq ι] in +/-- Swapping the factors of the delta swaps the bases, +`comm (∑ I, b I ⊗ b' I) = ∑ I, b' I ⊗ b I`. -/ +lemma comm_coordDelta (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : + TensorProduct.comm R M N (b.coordDelta b') = b'.coordDelta b := by + simp only [coordDelta, map_sum, TensorProduct.comm_tmul] + +/-- `coordTensorHomEquiv` in components, `(z v)_J = ∑ I, v_I z_{IJ}`. -/ +lemma repr_coordTensorHomEquiv {κ N' : Type*} [AddCommGroup N'] [Module R N'] + (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (d : Module.Basis κ R N') + (z : M ⊗[R] N') (v : N) (J : κ) : + d.repr (b.coordTensorHomEquiv b' z v) J = + ∑ I, b'.repr v I * (b.tensorProduct d).repr z (I, J) := by + induction z using TensorProduct.inductionOn with + | add x y hx hy => + simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, + Finset.sum_add_distrib] + | tmul x y => + simp [coordPairing_tmul, Finset.sum_mul, mul_assoc, mul_comm (d.repr y J)] + +/-- The matrix of `coordTensorHomEquiv z` is the transpose of the matrix of components of `z`. -/ +lemma toMatrix_coordTensorHomEquiv {κ N' : Type*} [Fintype κ] [AddCommGroup N'] [Module R N'] + (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (d : Module.Basis κ R N') (z : M ⊗[R] N') : + LinearMap.toMatrix b' d (b.coordTensorHomEquiv b' z) = + (Matrix.of fun I J => (b.tensorProduct d).repr z (I, J)).transpose := by + ext J I + simp [LinearMap.toMatrix_apply, repr_coordTensorHomEquiv, Finsupp.single_apply] + +/-- Two tensors of `M ⊗ M` and `N ⊗ N` give inverse linear maps when their matrices of +components multiply to the identity. -/ +lemma coordTensorHomEquiv_comp_of_mul_eq_one (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (F : M ⊗[R] M) (F' : N ⊗[R] N) + (h : Matrix.of (fun I J => (b'.tensorProduct b').repr F' (I, J)) * + Matrix.of (fun I J => (b.tensorProduct b).repr F (I, J)) = 1) : + b.coordTensorHomEquiv b' F ∘ₗ b'.coordTensorHomEquiv b F' = LinearMap.id := by + apply (LinearMap.toMatrix b b).injective + rw [LinearMap.toMatrix_comp _ b', toMatrix_coordTensorHomEquiv, toMatrix_coordTensorHomEquiv, + ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] + +/-- Read along its second factor, a tensor of `N' ⊗ M` has the matrix of its components, +`(comm z) v = ∑ I, z_{JI} v_I`. -/ +lemma toMatrix_coordTensorHomEquiv_comm {κ N' : Type*} [Fintype κ] [AddCommGroup N'] + [Module R N'] (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (d : Module.Basis κ R N') (z : N' ⊗[R] M) : + LinearMap.toMatrix b' d (b.coordTensorHomEquiv b' (TensorProduct.comm R N' M z)) = + Matrix.of fun J I => (d.tensorProduct b).repr z (J, I) := by + rw [toMatrix_coordTensorHomEquiv] + ext J I + simp [Module.Basis.tensorProduct_repr_comm] + +/-- Swapping the factors of `F` and `F'` swaps the order of composition, `F F' = 1` read along the +second factors from `F' F = 1` read along the first. -/ +lemma coordTensorHomEquiv_comm_comp_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (F : M ⊗[R] M) (F' : N ⊗[R] N) + (h : b'.coordTensorHomEquiv b F' ∘ₗ b.coordTensorHomEquiv b' F = LinearMap.id) : + b.coordTensorHomEquiv b' (TensorProduct.comm R M M F) ∘ₗ + b'.coordTensorHomEquiv b (TensorProduct.comm R N N F') = LinearMap.id := by + have h := congrArg (LinearMap.toMatrix b' b') h + rw [LinearMap.toMatrix_comp _ b, toMatrix_coordTensorHomEquiv, toMatrix_coordTensorHomEquiv, + ← Matrix.transpose_mul, LinearMap.toMatrix_id, ← Matrix.transpose_one, + Matrix.transpose_inj] at h + apply (LinearMap.toMatrix b b).injective + rw [LinearMap.toMatrix_comp _ b', toMatrix_coordTensorHomEquiv_comm, + toMatrix_coordTensorHomEquiv_comm, LinearMap.toMatrix_id] + exact h + +end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 1470ce05b0..95c8803f9b 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -41,10 +41,6 @@ properties let a contraction of several tensors be computed one step at a time. - A factor that takes no part in a contraction can be multiplied before or after it, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -For tensors on two slots, the contraction `M^μ_ν N^ν_ρ` of the inner indices is the product of -matrices. Contracting an index of any tensor with the first index of a tensor `F` on two slots -applies `F`, read as a linear map, at that index, and for the delta this is the identity. - The components of a contraction are sums of components of the tensor, `(tr T)^ρ = ∑ μ, T^ρμ_μ`, since the bases of a color and its dual are dual. @@ -69,12 +65,6 @@ since the bases of a color and its dual are dual. - `Species.Tensor.tmulEquiv_contr_left` and `Species.Tensor.tmulEquiv_contr_right` prove that contracting a factor commutes with the product, along `ColorEquiv.tmulRight` and `ColorEquiv.tmulLeft`. -- `Species.Tensor.contr_matMul` gives the contraction `M^μ_ν N^ν_ρ` of two tensors on two slots, - and `Species.Tensor.contr_matMul_eq_deltaTensor_iff` proves that it is the delta exactly when - their linear maps compose to the identity. -- `Species.Tensor.contr_matMulAt` proves that contracting the index `i` with the first index of a - tensor `F` on two slots, along `ColorEquiv.matMulAt`, applies `F` at `i`, and - `Species.Tensor.contr_deltaTensor` that for the delta this is `T^…μ… δ_μ^ν = T^…ν…`. - `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction, from `Species.Tensor.tensorBasis_repr_contrStd` in the standard arrangement of slots. - `Species.Tensor.tensorBasis_repr_contr_one_pair` and @@ -89,8 +79,7 @@ since the bases of a color and its dual are dual. - B.3. Symmetric against antisymmetric - B.4. Two contractions are one - B.5. Products with a tensor that is not contracted -- C. Products with a tensor on two slots -- D. Components +- C. Components ## iv. References @@ -427,150 +416,7 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color /-! -## C. Products with a tensor on two slots - --/ - -/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two slots that contracts the -second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are -`0` and `1`. -/ -def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : - ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where - toEquiv.toFun - | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 - toEquiv.invFun - | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 - toEquiv.left_inv := by decide - toEquiv.right_inv := by decide - -/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots is `M` with its second factor mapped -by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ -lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : - contr (ColorEquiv.matMul a c b) - (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.twoSlotEquiv _ b N)) = - S.twoSlotEquiv a b ((S.toLin N).lTensor _ M) := by - induction M using TensorProduct.inductionOn with - | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] - | tmul m₁ m₂ => - induction N using TensorProduct.inductionOn with - | add N N' hN hN' => - simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] - | tmul n₁ n₂ => - simp only [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, - LinearMap.lTensor_tmul, toLin_tmul, tmul_smul, map_smul] - congr 2 - funext j - fin_cases j <;> rfl - -/-- Two tensors on two slots contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear -maps compose to the identity, `Y ∘ X = id`. -/ -lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) - (Y : S.V y.dual ⊗[k] S.V x.dual) : - contr (ColorEquiv.matMul x y x.dual) - (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ - S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by - rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoSlotEquiv _ _)).eq_iff, - ← (S.toLin (a := x)).injective.eq_iff, toLin_lTensor, S.toLin_delta x.dual] - -/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a -tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is -`ColorEquiv.matMul` at any slot of a tensor on any slots. -/ -def _root_.Tensors.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) - (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : - ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where - toEquiv := - { toFun := Sum.elim (fun x => if x = i then .inr (.inl ()) else .inl x) - (fun j => if j = 0 then .inr (.inr ()) else .inl i) - invFun := Sum.elim (fun x => if x = i then .inr 1 else .inl x) - (Sum.elim (fun _ => .inl i) fun _ => .inr 0) - left_inv := by - rintro (x | j) - · by_cases hx : x = i <;> simp [hx] - · fin_cases j <;> simp - right_inv := by - rintro (x | (⟨⟩ | ⟨⟩)) - · by_cases hx : x = i <;> simp [hx] - · simp - · simp } - color_eq := by - rintro (x | (⟨⟩ | ⟨⟩)) - · by_cases hx : x = i - · subst hx - simp [hi] - · simp [hx, h x hx] - · simp [ha] - · simp - -/-- On a pure tensor, contracting the slot `i` with the first slot of `F` applies `F`, read as a -linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other slots are -those of `p`, moved along `h`. -/ -lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) - (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) - (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) - (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : - contr (ColorEquiv.matMulAt i a b ha hi h) - (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoSlotEquiv a.dual b F)) = - S.pure (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) := by - induction F using TensorProduct.inductionOn with - | add F F' hF hF' => - simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] - rw [pure, pure, pure, MultilinearMap.map_update_add, map_add] - | tmul x y => - rw [toLin_tmul, map_smul] - conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] - rw [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] - congr 2 - funext z - by_cases hz : z = i - · subst hz - rw [Function.update_self, cast_apply_congr _ (show - (ColorEquiv.matMulAt z a b ha hi h).toEquiv.symm (.inl z) = .inr 1 by - simp [ColorEquiv.matMulAt])] - rfl - · rw [Function.update_of_ne hz, cast_apply_congr _ (show - (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by - simp [ColorEquiv.matMulAt, hz]), hq z hz] - -/-- Contracting the index `i` of `t` with the first index of a tensor `F` on two slots applies `F`, -read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ -lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) - (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) - (t : S.Tensor c) : - contr (ColorEquiv.matMulAt i a b ha hi h) - (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoSlotEquiv a.dual b F)) = - S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by - refine LinearMap.congr_fun (f := (contr _).toLinearMap ∘ₗ (S.tmulEquiv c ![a.dual, b]).toLinearMap - ∘ₗ (TensorProduct.mk k _ _).flip (S.twoSlotEquiv a.dual b F)) (ext_pure fun p => ?_) t - -- The vectors of `p` moved along `h` away from `i`. The vector at `i` is replaced below. - let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) - exact (contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx])).trans - (mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) - (Function.update_self _ _ _) (fun x hx => by simp [q, hx])).symm - -/-- Contracting the index `i` with the first index of the delta is the identity, -`T^…μ… δ_μ^ν = T^…ν…`. -/ -lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) - (t : S.Tensor c) : - contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) - (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by - subst ha - rw [deltaTensor, contr_matMulAt, toLin_delta] - exact LinearMap.congr_fun (mapAt_id i _) t - -/-- Contracting the index `i` with the second index of the delta is the identity, -`δ^ν_μ T^…μ… = T^…ν…`. -/ -lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) - (ha : c i = a) (t : S.Tensor c) : - contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) - (S.tmulEquiv c ![a.dual, a] - (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by - rw [deltaTensor, reindex_twoSlotEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a - from S.comm_delta a.dual] - exact contr_deltaTensor i a ha t - -/-! - -## D. Components +## C. Components -/ diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index a942f70070..a403544fe5 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -196,7 +196,8 @@ lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual))) = S.deltaTensor c := by rw [contr_formTensor_formTensor, deltaTensor, reindex_twoSlotEquiv, - show TensorProduct.comm k _ _ (S.delta c.dual) = S.delta c from S.comm_delta c.dual] + show TensorProduct.comm k _ _ (S.delta c.dual) = S.delta c from + Module.Basis.comm_coordDelta _ _] rfl end Species diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 0f8670f09e..17bc0047a3 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -5,43 +5,57 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.ForMathlib.TensorProductMatrix -public import Physlib.Mathematics.Tensors.Species +public import Physlib.Mathematics.ForMathlib.CoordTensorHom +public import Physlib.Mathematics.Tensors.Contraction /-! # Tensors of two factors as linear maps -A tensor in `V a ⊗ U` is read as a linear map `V a.dual → U`, and the delta is the identity map. +A tensor in `V a ⊗ U` is read as a linear map `V a.dual → U`. The delta is the identity map, and +contracting an index with a tensor of two factors applies its linear map. ## i. Overview A tensor `z` in `V a ⊗ U`, for a color `a` of a species `S` and any module `U`, is read as the linear map `S.toLin z : V a.dual → U` that contracts its input with the first factor of `z`. On a pure tensor `z = x ⊗ y` it is `v ↦ (v^μ x_μ) y`. Every linear map `V a.dual → U` is `toLin z` for -exactly one `z`. `toLin` is Mathlib's `dualTensorHomEquivOfBasis`, after `Species.dualEquiv` -reads `V a` as the dual of `V a.dual`. +exactly one `z`. `toLin` is `Module.Basis.coordTensorHomEquiv` for the bases of `a` and `a.dual`, +which is Mathlib's `dualTensorHomEquivOfBasis` once the contraction identifies `V a` with the dual +of `V a.dual`. The results that need only the bases, such as the matrix of `toLin z`, are stated +there. This is how two-index tensors act as maps. A metric `g_μν` is the map `v^μ ↦ v^μ g_μν`, and the -inverse metric `g^μν` is the tensor of the inverse map. The delta -`S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν` -(`toLin_delta`). The identity commutes with every group element, so the delta is invariant -(`delta_invariant`). +inverse metric `g^μν` is the tensor of the inverse map. The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. + +The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν`. The +identity commutes with every group element, so the delta is invariant. `S.deltaTensor c` is the +delta as a tensor on two slots, and its components form the identity matrix. + +Contracting the inner indices of two tensors on two slots, `M^μ_ν N^ν_ρ`, applies the linear map +of `N` to the second factor of `M`, and the result is the delta exactly when the two linear maps +are inverse. More generally, contracting an index of any tensor with the first index of a tensor +`F` on two slots applies the linear map of `F` at that index, and for the delta this is the +identity. ## ii. Key results - `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with `Species.toLin_map` : it - commutes with the group action, `(Λz)(Λv) = Λ(z v)`, and `Species.toLin_lTensor` : - `(id ⊗ f) z = f ∘ z`. -- `Species.delta` : the delta `δ_μ^ν`, with `Species.toLin_delta` : it is the identity map, and - `Species.delta_invariant` : it is invariant. -- `Species.basis_repr_toLin` : `toLin` in components, `Species.toMatrix_toLin` : its matrix, and - `Species.toLin_comp_toLin_of_mul_eq_one` : two such maps are inverse when their matrices are. + commutes with the group action, `(Λz)(Λv) = Λ(z v)`. +- `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. +- `Species.deltaTensor` : the delta as a tensor on two slots, with + `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. +- `Species.Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, and + `Species.Tensor.contr_matMul_eq_deltaTensor_iff` : it is the delta exactly when the linear maps + of `M` and `N` are inverse. +- `Species.Tensor.contr_matMulAt` : contracting the index `i` with the first index of `F` applies + `F` at `i`, and `Species.Tensor.contr_deltaTensor` : for the delta this is + `T^…μ… δ_μ^ν = T^…ν…`. ## iii. Table of contents - A. Tensors of two factors as linear maps - B. The delta -- C. Components +- C. Products with a tensor on two slots ## iv. References @@ -69,34 +83,26 @@ variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K /-- An element `z` of `V a ⊗ U` as the linear map `V a.dual → U` that contracts a vector with the first factor of `z`. For a lower color `a` and `z = g` in `V a ⊗ V a` it is `v^μ ↦ v^μ g_μν`. Every linear map arises so. -/ -noncomputable def toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : +noncomputable abbrev toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := - -- Mathlib's `dualTensorHomEquivOfBasis`, after reading `V a` as the dual of `V a.dual`. - (TensorProduct.congr (S.dualEquiv a.dual).toLinearEquiv (LinearEquiv.refl k U)).trans - (dualTensorHomEquivOfBasis (S.basis a.dual)) + (S.basis a).coordTensorHomEquiv (S.basis a.dual) -/-- A pure tensor `x ⊗ y` acts as `v ↦ (v^μ x_μ) y`, contracting `v` with its first factor. -/ +/-- The coordinate pairing of the bases of `c` and `c.dual` is the contraction. -/ @[simp] -lemma toLin_tmul {a : Color K} {U : Type} [AddCommGroup U] [Module k U] (x : S.V a) (y : U) - (v : S.V a.dual) : S.toLin (x ⊗ₜ y) v = S.contr a.dual (v ⊗ₜ x) • y := +lemma coordPairing_basis (c : Color K) {x : S.V c} {y : S.V c.dual} : + (S.basis c).coordPairing (S.basis c.dual) (x ⊗ₜ y) = S.contr c (x ⊗ₜ y) := rfl -/-- Applying a linear map `f` to the second factor of `z` composes the linear map of `z` with `f`, -`(id ⊗ f) z = f ∘ z`. -/ -lemma toLin_lTensor {a : Color K} {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] - [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) : - S.toLin (f.lTensor (S.V a) z) = f ∘ₗ S.toLin z := by - induction z using TensorProduct.inductionOn with - | tmul x y => ext v; simp - | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.comp_add] - /-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U] [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by induction z using TensorProduct.inductionOn with - | tmul w y => simpa using congrArg (· • f y) (S.contr_invariant a.dual g x w) + | tmul w y => + rw [TensorProduct.map_tmul, Module.Basis.coordTensorHomEquiv_tmul, + Module.Basis.coordTensorHomEquiv_tmul, map_smul] + exact congrArg (· • f y) (S.contr_invariant a.dual g x w) | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] /-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ @@ -123,88 +129,207 @@ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) /-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of the dual color is first. -/ -noncomputable def delta (c : Color K) : S.V c.dual ⊗[k] S.V c := - ∑ μ, S.basis c.dual μ ⊗ₜ S.basis c μ - -/-- The delta read as a linear map is the identity, `u^μ δ_μ^ν = u^ν`. -/ -lemma toLin_delta (c : Color K) : S.toLin (S.delta c) = LinearMap.id := - LinearMap.ext fun v => by simp [delta, contr, Module.Basis.coordPairing_tmul_basis] - -/-- Swapping the factors of the delta at `c` gives the delta at `c.dual`, `δ_μ^ν = δ^ν_μ`. -/ -lemma comm_delta (c : Color K) : TensorProduct.comm k _ _ (S.delta c) = S.delta c.dual := by - simp only [delta, map_sum, TensorProduct.comm_tmul] +noncomputable abbrev delta (c : Color K) : S.V c.dual ⊗[k] S.V c := + (S.basis c.dual).coordDelta (S.basis c) /-- The delta is invariant, `Λ δ = δ`, with `Λ` acting on both factors. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `toLin_delta`. + -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `coordTensorHomEquiv_coordDelta`. apply S.toLin.injective ext x - simpa [toLin_delta] using S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) + simpa [Module.Basis.coordTensorHomEquiv_coordDelta] using + S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) + +variable {S} + +variable (S) in +/-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper +color `c` and `δ^μ_ν` for a lower one. -/ +noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := + S.twoSlotEquiv c.dual c (S.delta c) + +/-- The delta tensor is invariant, `Λ δ = δ`. -/ +lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := + (S.twoSlotEquiv _ c).toIntertwiningMap.map_invariant (S.delta_invariant c Λ) + +/-- The components of the delta tensor are the coordinates of `S.delta c` in Mathlib's product +basis, at the pair of labels `(μ 0, μ 1)`. -/ +lemma tensorBasis_repr_deltaTensor (c : Color K) (μ : Π j, basisIdx (![c.dual, c] j).kind) : + (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = + ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (μ 0, μ 1) := + tensorBasis_repr_twoSlotEquiv _ _ _ μ + +/-- The components of the delta tensor form the identity matrix, `δ_μ^ν = 1` for `μ = ν` and `0` +otherwise. -/ +lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) + (μ : Π j, basisIdx (![c.dual, c] j).kind) : + (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = + (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) (μ 0) (μ 1) := by + -- The labels `μ 0` and `μ 1` are labels at `c` only after unfolding, so they are generalized. + suffices h : ∀ a b, ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (a, b) = + (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) a b from + (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) + intro a b + have h := Module.Basis.repr_coordTensorHomEquiv (S.basis c.dual) (S.basis c) (S.basis c) + (S.delta c) (S.basis c a) b + rw [Module.Basis.coordTensorHomEquiv_coordDelta, LinearMap.id_apply, Module.Basis.repr_self] at h + simp only [Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, + ite_true] at h + rw [Matrix.one_apply, ← h] + +namespace Tensor + +open TensorProduct PiTensorProduct + +variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} /-! -## C. Components +## C. Products with a tensor on two slots -/ -variable {S} - -/-- `toLin` in components. For a lower color `c` and an upper color `d`, `(z v)^ν = v^μ z_μ^ν` for -`z = z_μ^ν e^μ ⊗ e_ν` in `V c ⊗ V d`. -/ -lemma basis_repr_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) (v : S.V c.dual) - (ν : basisIdx d.kind) : - (S.basis d).repr (S.toLin z v) ν = - ∑ μ, (S.basis c.dual).repr v μ * ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν) := by - induction z using TensorProduct.inductionOn with - | add x y hx hy => - simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, - Finset.sum_add_distrib] +/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two slots that contracts the +second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are +`0` and `1`. -/ +def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : + ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where + toEquiv.toFun + | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 + toEquiv.invFun + | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 + toEquiv.left_inv := by decide + toEquiv.right_inv := by decide + +/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots is `M` with its second factor mapped +by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ +lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : + contr (ColorEquiv.matMul a c b) + (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.twoSlotEquiv _ b N)) = + S.twoSlotEquiv a b ((S.toLin N).lTensor _ M) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] + | tmul m₁ m₂ => + induction N using TensorProduct.inductionOn with + | add N N' hN hN' => + simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] + | tmul n₁ n₂ => + simp only [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, + LinearMap.lTensor_tmul, Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, tmul_smul, + map_smul] + congr 2 + funext j + fin_cases j <;> rfl + +/-- Two tensors on two slots contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear +maps compose to the identity, `Y ∘ X = id`. -/ +lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) + (Y : S.V y.dual ⊗[k] S.V x.dual) : + contr (ColorEquiv.matMul x y x.dual) + (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ + S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by + rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoSlotEquiv _ _)).eq_iff, + ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, + Module.Basis.coordTensorHomEquiv_coordDelta] + +/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a +tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is +`ColorEquiv.matMul` at any slot of a tensor on any slots. -/ +def _root_.Tensors.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) + (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : + ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where + toEquiv := + { toFun := Sum.elim (fun x => if x = i then .inr (.inl ()) else .inl x) + (fun j => if j = 0 then .inr (.inr ()) else .inl i) + invFun := Sum.elim (fun x => if x = i then .inr 1 else .inl x) + (Sum.elim (fun _ => .inl i) fun _ => .inr 0) + left_inv := by + rintro (x | j) + · by_cases hx : x = i <;> simp [hx] + · fin_cases j <;> simp + right_inv := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i <;> simp [hx] + · simp + · simp } + color_eq := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i + · subst hx + simp [hi] + · simp [hx, h x hx] + · simp [ha] + · simp + +/-- On a pure tensor, contracting the slot `i` with the first slot of `F` applies `F`, read as a +linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other slots are +those of `p`, moved along `h`. -/ +lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) + (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) + (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) + (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : + contr (ColorEquiv.matMulAt i a b ha hi h) + (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoSlotEquiv a.dual b F)) = + S.pure (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) := by + induction F using TensorProduct.inductionOn with + | add F F' hF hF' => + simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] + rw [pure, pure, pure, MultilinearMap.map_update_add, map_add] | tmul x y => - simp [contr_tmul_eq_sum_basis, Finset.sum_mul, mul_assoc, mul_comm ((S.basis d).repr y ν)] - -/-- The matrix of `toLin z` is the transpose of the matrix of components of `z`, -`(z v)^ν = z_μ^ν v^μ`. -/ -lemma toMatrix_toLin (c : Color K) {d : Color K} (z : S.V c ⊗[k] S.V d) : - LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin z) = - (Matrix.of fun μ ν => ((S.basis c).tensorProduct (S.basis d)).repr z (μ, ν)).transpose := by - ext ν μ - simp [LinearMap.toMatrix_apply, basis_repr_toLin, Finsupp.single_apply] - -/-- For a lower color `c`, `toLin F ∘ₗ toLin F'` is the identity when the matrices of components of -`F'` and `F` multiply to the identity, `F'^μρ F_ρν = δ^μ_ν`. -/ -lemma toLin_comp_toLin_of_mul_eq_one {c : Color K} (F : S.V c ⊗[k] S.V c) - (F' : S.V c.dual ⊗[k] S.V c.dual) - (h : Matrix.of (fun μ ν => ((S.basis c.dual).tensorProduct (S.basis c.dual)).repr F' (μ, ν)) * - Matrix.of (fun μ ν => ((S.basis c).tensorProduct (S.basis c)).repr F (μ, ν)) = 1) : - S.toLin F ∘ₗ S.toLin F' = LinearMap.id := by - apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective - rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin, toMatrix_toLin c.dual, - ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] - -/-- `toLin` of the swapped tensor reads `z` along its second index, `v^μ ↦ z_νμ v^μ`, so its matrix -is the matrix of components of `z`. -/ -lemma toMatrix_toLin_comm (c : Color K) {d : Color K} (z : S.V d ⊗[k] S.V c) : - LinearMap.toMatrix (S.basis c.dual) (S.basis d) (S.toLin (TensorProduct.comm k _ _ z)) = - Matrix.of fun ν μ => ((S.basis d).tensorProduct (S.basis c)).repr z (ν, μ) := by - rw [toMatrix_toLin] - ext ν μ - simp [Module.Basis.tensorProduct_repr_comm] - -/-- Swapping the indices of `F` and `F'` swaps the order of composition, `F F' = 1` read along the -second indices from `F' F = 1` read along the first. -/ -lemma toLin_comm_comp_toLin_comm {c : Color K} (F : S.V c ⊗[k] S.V c) - (F' : S.V c.dual ⊗[k] S.V c.dual) (h : S.toLin F' ∘ₗ S.toLin F = LinearMap.id) : - S.toLin (TensorProduct.comm k _ _ F) ∘ₗ S.toLin (TensorProduct.comm k _ _ F') = - LinearMap.id := by - have h := congrArg (LinearMap.toMatrix (S.basis c.dual) (S.basis c.dual)) h - rw [LinearMap.toMatrix_comp _ (S.basis c), toMatrix_toLin, toMatrix_toLin c.dual, - ← Matrix.transpose_mul, LinearMap.toMatrix_id, ← Matrix.transpose_one, - Matrix.transpose_inj] at h - apply (LinearMap.toMatrix (S.basis c) (S.basis c)).injective - rw [LinearMap.toMatrix_comp _ (S.basis c.dual), toMatrix_toLin_comm, toMatrix_toLin_comm c.dual, - LinearMap.toMatrix_id] - exact h + rw [Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, map_smul] + conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] + rw [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] + congr 2 + funext z + by_cases hz : z = i + · subst hz + rw [Function.update_self, cast_apply_congr _ (show + (ColorEquiv.matMulAt z a b ha hi h).toEquiv.symm (.inl z) = .inr 1 by + simp [ColorEquiv.matMulAt])] + rfl + · rw [Function.update_of_ne hz, cast_apply_congr _ (show + (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by + simp [ColorEquiv.matMulAt, hz]), hq z hz] + +/-- Contracting the index `i` of `t` with the first index of a tensor `F` on two slots applies `F`, +read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ +lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) + (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) + (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a b ha hi h) + (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoSlotEquiv a.dual b F)) = + S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by + refine LinearMap.congr_fun (f := (contr _).toLinearMap ∘ₗ (S.tmulEquiv c ![a.dual, b]).toLinearMap + ∘ₗ (TensorProduct.mk k _ _).flip (S.twoSlotEquiv a.dual b F)) (ext_pure fun p => ?_) t + -- The vectors of `p` moved along `h` away from `i`. The vector at `i` is replaced below. + let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) + exact (contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx])).trans + (mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) + (Function.update_self _ _ _) (fun x hx => by simp [q, hx])).symm + +/-- Contracting the index `i` with the first index of the delta is the identity, +`T^…μ… δ_μ^ν = T^…ν…`. -/ +lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) + (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) + (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by + subst ha + rw [deltaTensor, contr_matMulAt, Module.Basis.coordTensorHomEquiv_coordDelta] + exact LinearMap.congr_fun (mapAt_id i _) t + +/-- Contracting the index `i` with the second index of the delta is the identity, +`δ^ν_μ T^…μ… = T^…ν…`. -/ +lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) + (ha : c i = a) (t : S.Tensor c) : + contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) + (S.tmulEquiv c ![a.dual, a] + (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by + rw [deltaTensor, reindex_twoSlotEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a + from Module.Basis.comm_coordDelta _ _] + exact contr_deltaTensor i a ha t + +end Tensor end Species diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 6759f9f4fc..d3984c361a 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -7,7 +7,7 @@ module public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SesquilinearForm -public import Physlib.Mathematics.Tensors.Contraction +public import Physlib.Mathematics.Tensors.LinearMap public import Mathlib.Tactic.LinearCombination /-! @@ -153,7 +153,8 @@ lemma formMatrix_eq_toMatrix (c : Color K) [S.HasForm c.kind] : rw [LinearMap.BilinForm.toMatrix_apply, formBilin_apply, contr_tmul_eq_sum_basis] simp only [Module.Basis.repr_self, Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, ite_true] - refine ((S.basis_repr_toLin c (d := c) (form c) _ ν).trans ?_).symm + refine ((Module.Basis.repr_coordTensorHomEquiv (S.basis c) (S.basis c.dual) (S.basis c) (form c) + _ ν).trans ?_).symm simp [Finsupp.single_apply, formMatrix] /-- The form on two basis vectors is a component of the form, `g(e^μ, e^ν) = g^μν`. -/ @@ -180,7 +181,8 @@ lemma contr_toLin_comm (c : Color K) (z : S.V c ⊗[k] S.V c) (u v : S.V c.dual) induction z using TensorProduct.inductionOn with | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] | tmul x y => - simp only [TensorProduct.comm_tmul, toLin_tmul, tmul_smul, map_smul, smul_eq_mul] + simp only [TensorProduct.comm_tmul, Module.Basis.coordTensorHomEquiv_tmul, tmul_smul, map_smul, + smul_eq_mul] exact mul_comm _ _ /-- A symmetric form, `g_μν = g_νμ`. -/ @@ -240,8 +242,9 @@ lowers an upper index, `v^μ ↦ g_νμ v^μ`, and raises a lower one, `w_μ ↦ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.rep c.dual) := .mk (LinearEquiv.ofLinearMap (S.toLin (TensorProduct.comm k _ _ (form c.dual))) (S.toLin (TensorProduct.comm k _ _ (form c))) - (toLin_comm_comp_toLin_comm _ _ (form_inverse c)) - (toLin_comm_comp_toLin_comm _ _ (form_inverse c.dual))) fun g => LinearMap.ext fun v => by + (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c)) + (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c.dual))) + fun g => LinearMap.ext fun v => by change S.toLin (TensorProduct.comm k _ _ (form c.dual)) _ = S.rep c.dual g (S.toLin (TensorProduct.comm k _ _ (form c.dual)) v) rw [← S.toLin_map c.dual g, TensorProduct.map_comm, form_invariant] @@ -263,7 +266,7 @@ lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : (S.basis c.dual).repr (S.toDual c v) ν = ∑ μ, (S.basis c).repr v μ * S.formMatrix c.dual ν μ := by - rw [toDual_apply, basis_repr_toLin] + rw [toDual_apply, Module.Basis.repr_coordTensorHomEquiv] simp [formMatrix, Module.Basis.tensorProduct_repr_comm] /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 4078578a91..fdb010ed77 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Physlib.Mathematics.Tensors.LinearMap public import Physlib.Mathematics.Tensors.Reindexing /-! @@ -23,8 +22,7 @@ is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Ma The product is associative and symmetric up to a relabeling of slots, and the group acts on it factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on -two slots by `twoSlotEquiv`, and the delta `S.delta c` is the tensor `S.deltaTensor c`. The -components of a product are the products of the components. +two slots by `twoSlotEquiv`. The components of a product are the products of the components. ## ii. Key results @@ -35,8 +33,6 @@ components of a product are the products of the components. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. - `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots. - `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. -- `Species.tensorBasis_repr_deltaTensor_eq_one` proves that the components of the delta tensor - form the identity matrix. ## iii. Table of contents @@ -242,7 +238,7 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : ## D. Tensors on one and two slots -A species gives objects with two indices, such as the delta `S.delta c`, in Mathlib's binary +A species gives objects with two indices, such as a metric `η_μν`, in Mathlib's binary tensor product `V a ⊗ V b`. `twoSlotEquiv` identifies this space with the tensors `S.Tensor ![a, b]` on two slots, so that such an object can be used in index notation. @@ -299,16 +295,6 @@ lemma reindex_twoSlotEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : simp only [twoSlotEquiv_tmul, reindex_pure, TensorProduct.comm_tmul] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) -variable (S) in -/-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper -color `c` and `δ^μ_ν` for a lower one. -/ -noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := - S.twoSlotEquiv c.dual c (S.delta c) - -/-- The delta tensor is invariant, `Λ δ = δ`. -/ -lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := - (S.twoSlotEquiv _ c).toIntertwiningMap.map_invariant (S.delta_invariant c Λ) - /-! ## E. Components @@ -355,30 +341,6 @@ lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) -- Mathlib's lemma, and `exact` checks it up to unfolding. exact ((Module.Basis.tensorProduct_repr_tmul_apply _ _ _ _ _ _).trans (smul_eq_mul _ _)).symm -/-- The components of the delta tensor are the coordinates of `S.delta c` in Mathlib's product -basis, at the pair of labels `(μ 0, μ 1)`. -/ -lemma tensorBasis_repr_deltaTensor (c : Color K) (μ : Π j, basisIdx (![c.dual, c] j).kind) : - (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = - ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (μ 0, μ 1) := - tensorBasis_repr_twoSlotEquiv _ _ _ μ - -/-- The components of the delta tensor form the identity matrix, `δ_μ^ν = 1` for `μ = ν` and `0` -otherwise. -/ -lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) - (μ : Π j, basisIdx (![c.dual, c] j).kind) : - (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = - (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) (μ 0) (μ 1) := by - -- The labels `μ 0` and `μ 1` are labels at `c` only after unfolding, so they are generalized. - suffices h : ∀ a b, ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (a, b) = - (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) a b from - (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) - intro a b - have h := basis_repr_toLin c.dual (S.delta c) (S.basis c a) b - rw [S.toLin_delta, LinearMap.id_apply, Module.Basis.repr_self] at h - simp only [Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, - ite_true] at h - rw [Matrix.one_apply, ← h] - end Species end Tensors diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index fc48a9a5ad..a5c0bf437b 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -321,7 +321,7 @@ lemma formEntries_dual_mul : ∀ c : Color Kind, /-- The forms at `c` and at `c.dual` are inverse to each other, since their matrices are. -/ lemma form_inverse (c : Color Kind) : complexLorentz.toLin (form c) ∘ₗ complexLorentz.toLin (form c.dual) = LinearMap.id := - Species.toLin_comp_toLin_of_mul_eq_one _ _ <| by + Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one _ _ _ _ <| by simpa only [basis_tensorProduct_repr_form] using formEntries_dual_mul c end complexLorentz diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 579d13ee8d..c4ae6c596f 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -234,7 +234,7 @@ lemma metric_invariant (d : ℕ) (c : Color Kind) (Λ : LorentzGroup d) : lemma metric_inverse (d : ℕ) (c : Color Kind) : (realLorentz d).toLin (metric d c) ∘ₗ (realLorentz d).toLin (metric d c.dual) = LinearMap.id := - Species.toLin_comp_toLin_of_mul_eq_one _ _ <| by + Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one _ _ _ _ <| by simp only [basis_tensorProduct_repr_metric] exact minkowskiMatrix.sq From 1d0a362be1147b976ff90bc1f2bd50cf6946af87 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 16:41:58 +0000 Subject: [PATCH 119/242] refactor(Tensors): golf the CoordTensorHom and LinearMap proofs; add tensorProduct_repr_coordDelta, drop toMatrix_coordTensorHomEquiv_comm tensorProduct_repr_coordDelta gives the components of the delta, so tensorBasis_repr_deltaTensor_eq_one is a term. coordTensorHomEquiv_comm_comp_comm follows from coordTensorHomEquiv_comp_of_mul_eq_one, so toMatrix_coordTensorHomEquiv_comm is no longer needed. toLin_map_rep_bijective uses Mathlib's Representation.apply_bijective. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 33 +++++++------------ Physlib/Mathematics/Tensors/LinearMap.lean | 23 +++---------- 2 files changed, 16 insertions(+), 40 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 8654f149b2..a122ed78a9 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -102,6 +102,11 @@ lemma comm_coordDelta (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : TensorProduct.comm R M N (b.coordDelta b') = b'.coordDelta b := by simp only [coordDelta, map_sum, TensorProduct.comm_tmul] +/-- The components of the delta form the identity matrix. -/ +lemma tensorProduct_repr_coordDelta (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (I J : ι) : (b.tensorProduct b').repr (b.coordDelta b') (I, J) = if I = J then 1 else 0 := by + simp [coordDelta, Finsupp.single_apply] + /-- `coordTensorHomEquiv` in components, `(z v)_J = ∑ I, v_I z_{IJ}`. -/ lemma repr_coordTensorHomEquiv {κ N' : Type*} [AddCommGroup N'] [Module R N'] (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (d : Module.Basis κ R N') @@ -109,11 +114,8 @@ lemma repr_coordTensorHomEquiv {κ N' : Type*} [AddCommGroup N'] [Module R N'] d.repr (b.coordTensorHomEquiv b' z v) J = ∑ I, b'.repr v I * (b.tensorProduct d).repr z (I, J) := by induction z using TensorProduct.inductionOn with - | add x y hx hy => - simp only [map_add, LinearMap.add_apply, Finsupp.add_apply, hx, hy, mul_add, - Finset.sum_add_distrib] - | tmul x y => - simp [coordPairing_tmul, Finset.sum_mul, mul_assoc, mul_comm (d.repr y J)] + | add x y hx hy => simp [hx, hy, mul_add, Finset.sum_add_distrib] + | tmul x y => simp [coordPairing_tmul, Finset.sum_mul, mul_assoc, mul_comm (d.repr y J)] /-- The matrix of `coordTensorHomEquiv z` is the transpose of the matrix of components of `z`. -/ lemma toMatrix_coordTensorHomEquiv {κ N' : Type*} [Fintype κ] [AddCommGroup N'] [Module R N'] @@ -134,17 +136,6 @@ lemma coordTensorHomEquiv_comp_of_mul_eq_one (b : Module.Basis ι R M) (b' : Mod rw [LinearMap.toMatrix_comp _ b', toMatrix_coordTensorHomEquiv, toMatrix_coordTensorHomEquiv, ← Matrix.transpose_mul, h, Matrix.transpose_one, LinearMap.toMatrix_id] -/-- Read along its second factor, a tensor of `N' ⊗ M` has the matrix of its components, -`(comm z) v = ∑ I, z_{JI} v_I`. -/ -lemma toMatrix_coordTensorHomEquiv_comm {κ N' : Type*} [Fintype κ] [AddCommGroup N'] - [Module R N'] (b : Module.Basis ι R M) (b' : Module.Basis ι R N) - (d : Module.Basis κ R N') (z : N' ⊗[R] M) : - LinearMap.toMatrix b' d (b.coordTensorHomEquiv b' (TensorProduct.comm R N' M z)) = - Matrix.of fun J I => (d.tensorProduct b).repr z (J, I) := by - rw [toMatrix_coordTensorHomEquiv] - ext J I - simp [Module.Basis.tensorProduct_repr_comm] - /-- Swapping the factors of `F` and `F'` swaps the order of composition, `F F' = 1` read along the second factors from `F' F = 1` read along the first. -/ lemma coordTensorHomEquiv_comm_comp_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) @@ -152,13 +143,11 @@ lemma coordTensorHomEquiv_comm_comp_comm (b : Module.Basis ι R M) (b' : Module. (h : b'.coordTensorHomEquiv b F' ∘ₗ b.coordTensorHomEquiv b' F = LinearMap.id) : b.coordTensorHomEquiv b' (TensorProduct.comm R M M F) ∘ₗ b'.coordTensorHomEquiv b (TensorProduct.comm R N N F') = LinearMap.id := by + -- The matrices of `comm F'` and `comm F` are the transposes of those in `h`. + refine coordTensorHomEquiv_comp_of_mul_eq_one b b' _ _ ?_ have h := congrArg (LinearMap.toMatrix b' b') h rw [LinearMap.toMatrix_comp _ b, toMatrix_coordTensorHomEquiv, toMatrix_coordTensorHomEquiv, - ← Matrix.transpose_mul, LinearMap.toMatrix_id, ← Matrix.transpose_one, - Matrix.transpose_inj] at h - apply (LinearMap.toMatrix b b).injective - rw [LinearMap.toMatrix_comp _ b', toMatrix_coordTensorHomEquiv_comm, - toMatrix_coordTensorHomEquiv_comm, LinearMap.toMatrix_id] - exact h + LinearMap.toMatrix_id] at h + convert h using 2 <;> ext <;> simp [tensorProduct_repr_comm] end Module.Basis diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 17bc0047a3..f3d7aedf7b 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -100,9 +100,7 @@ lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by induction z using TensorProduct.inductionOn with | tmul w y => - rw [TensorProduct.map_tmul, Module.Basis.coordTensorHomEquiv_tmul, - Module.Basis.coordTensorHomEquiv_tmul, map_smul] - exact congrArg (· • f y) (S.contr_invariant a.dual g x w) + exact (congrArg (· • f y) (S.contr_invariant a.dual g x w)).trans (map_smul f _ y).symm | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] /-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ @@ -116,10 +114,8 @@ lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin z)) (g : G) : Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by - have hrep (c : Color K) (g : G) : Function.Bijective (S.rep c g) := - (Module.End.isUnit_iff _).1 ((Group.isUnit g).map (S.rep c)) rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] - exact (hrep b g).comp (h.comp (hrep a.dual g⁻¹)) + exact ((S.rep b).apply_bijective g).comp (h.comp ((S.rep a.dual).apply_bijective g⁻¹)) /-! @@ -165,18 +161,9 @@ otherwise. -/ lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) (μ : Π j, basisIdx (![c.dual, c] j).kind) : (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = - (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) (μ 0) (μ 1) := by - -- The labels `μ 0` and `μ 1` are labels at `c` only after unfolding, so they are generalized. - suffices h : ∀ a b, ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (a, b) = - (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) a b from - (tensorBasis_repr_deltaTensor c μ).trans (h (μ 0) (μ 1)) - intro a b - have h := Module.Basis.repr_coordTensorHomEquiv (S.basis c.dual) (S.basis c) (S.basis c) - (S.delta c) (S.basis c a) b - rw [Module.Basis.coordTensorHomEquiv_coordDelta, LinearMap.id_apply, Module.Basis.repr_self] at h - simp only [Finsupp.single_apply, ite_mul, one_mul, zero_mul, Finset.sum_ite_eq, Finset.mem_univ, - ite_true] at h - rw [Matrix.one_apply, ← h] + (1 : Matrix (basisIdx c.kind) (basisIdx c.kind) k) (μ 0) (μ 1) := + (tensorBasis_repr_deltaTensor c μ).trans + ((Module.Basis.tensorProduct_repr_coordDelta _ _ _ _).trans Matrix.one_apply.symm) namespace Tensor From 60fca39c854f3e6a08cbb6dae04bec14336a7e40 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 16:55:01 +0000 Subject: [PATCH 120/242] refactor(Tensors): isIntertwiningMap_toLin_iff from Mathlib's linHom invariants; delta_invariant and toDual through it; drop toLin_map; coordPairing_basis_dual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit isIntertwiningMap_toLin_iff: a tensor of V a ⊗ V b is invariant exactly when its linear map commutes with the action, from Representation.mem_linHom_invariants_iff_isIntertwining and toLin_map_rep. delta_invariant (the identity commutes with the action) and the equivariance of toDual (an invariant form gives an intertwining map) are now instances of it. toLin_map_rep is proved directly, so the general toLin_map is removed. coordPairing_basis_dual folds the pairing into the contraction in the other order, so simp's form is S.contr in both. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/LinearMap.lean | 45 +++++++++++++--------- Physlib/Mathematics/Tensors/Metric.lean | 8 ++-- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index f3d7aedf7b..8b7bd2f273 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Physlib.Mathematics.ForMathlib.CoordTensorHom +public import Mathlib.RepresentationTheory.Invariants public import Physlib.Mathematics.Tensors.Contraction /-! @@ -25,7 +26,9 @@ of `V a.dual`. The results that need only the bases, such as the matrix of `toLi there. This is how two-index tensors act as maps. A metric `g_μν` is the map `v^μ ↦ v^μ g_μν`, and the -inverse metric `g^μν` is the tensor of the inverse map. The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. +inverse metric `g^μν` is the tensor of the inverse map. The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, so `z` is +invariant exactly when its linear map commutes with the group action. This is Mathlib's +`Representation.mem_linHom_invariants_iff_isIntertwining`, read through `toLin`. The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν`. The identity commutes with every group element, so the delta is invariant. `S.deltaTensor c` is the @@ -39,8 +42,9 @@ identity. ## ii. Key results -- `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with `Species.toLin_map` : it - commutes with the group action, `(Λz)(Λv) = Λ(z v)`. +- `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with `Species.toLin_map_rep` : + the linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, and `Species.isIntertwiningMap_toLin_iff` : `z` is + invariant exactly when its linear map commutes with the group action. - `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. - `Species.deltaTensor` : the delta as a tensor on two slots, with `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. @@ -93,22 +97,21 @@ lemma coordPairing_basis (c : Color K) {x : S.V c} {y : S.V c.dual} : (S.basis c).coordPairing (S.basis c.dual) (x ⊗ₜ y) = S.contr c (x ⊗ₜ y) := rfl -/-- `toLin` commutes with the group action, `((Λ ⊗ f) z)(Λx) = f (z x)` for every `Λ : G` and -linear map `f` on `U`. For `f = Λ` this is the equivariance `(Λz)(Λx) = Λ(z x)`. -/ -lemma toLin_map (a : Color K) (g : G) {U U' : Type} [AddCommGroup U] [Module k U] - [AddCommGroup U'] [Module k U'] (f : U →ₗ[k] U') (z : S.V a ⊗[k] U) (x : S.V a.dual) : - S.toLin (TensorProduct.map (S.rep a g) f z) (S.rep a.dual g x) = f (S.toLin z x) := by - induction z using TensorProduct.inductionOn with - | tmul w y => - exact (congrArg (· • f y) (S.contr_invariant a.dual g x w)).trans (map_smul f _ y).symm - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz'] +/-- The coordinate pairing of the bases of `c.dual` and `c` is the contraction at `c.dual`. -/ +@[simp] +lemma coordPairing_basis_dual (c : Color K) {x : S.V c.dual} {y : S.V c} : + (S.basis c.dual).coordPairing (S.basis c) (x ⊗ₜ y) = S.contr c.dual (x ⊗ₜ y) := + rfl /-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := LinearMap.ext fun x => by - rw [LinearMap.comp_apply, LinearMap.comp_apply, ← S.toLin_map, Representation.self_inv_apply] + induction z using TensorProduct.inductionOn with + | tmul w y => + simpa using congrArg (· • S.rep b g y) (S.contr_invariant a.dual g (S.rep a.dual g⁻¹ x) w) + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz', LinearMap.comp_apply] /-- The linear map of `Λz` is bijective when that of `z` is. -/ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) @@ -117,6 +120,13 @@ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] exact ((S.rep b).apply_bijective g).comp (h.comp ((S.rep a.dual).apply_bijective g⁻¹)) +/-- A tensor is invariant exactly when its linear map commutes with the group action. -/ +lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : + (S.rep a.dual).IsIntertwiningMap (S.rep b) (S.toLin z) ↔ + ∀ g, TensorProduct.map (S.rep a g) (S.rep b g) z = z := by + rw [← Representation.mem_linHom_invariants_iff_isIntertwining] + exact forall_congr' fun g => by rw [← toLin_map_rep, S.toLin.injective.eq_iff] + /-! ## B. The delta @@ -131,11 +141,10 @@ noncomputable abbrev delta (c : Color K) : S.V c.dual ⊗[k] S.V c := /-- The delta is invariant, `Λ δ = δ`, with `Λ` acting on both factors. -/ lemma delta_invariant (c : Color K) (g : G) : TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- `(Λδ)(Λx) = Λ(δx)` by `toLin_map`, and `δx = x` by `coordTensorHomEquiv_coordDelta`. - apply S.toLin.injective - ext x - simpa [Module.Basis.coordTensorHomEquiv_coordDelta] using - S.toLin_map c.dual g (S.rep c g) (S.delta c) (S.rep c g⁻¹ x) + -- The linear map of the delta is the identity, which commutes with the action. + refine (S.isIntertwiningMap_toLin_iff (S.delta c)).1 ?_ g + rw [Module.Basis.coordTensorHomEquiv_coordDelta] + exact ⟨fun _ _ => rfl⟩ variable {S} diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index d3984c361a..013c1afdcf 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -8,7 +8,6 @@ module public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SesquilinearForm public import Physlib.Mathematics.Tensors.LinearMap -public import Mathlib.Tactic.LinearCombination /-! # Invariant forms, metrics, and lowering and raising indices @@ -244,10 +243,9 @@ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.r (S.toLin (TensorProduct.comm k _ _ (form c))) (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c)) (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c.dual))) - fun g => LinearMap.ext fun v => by - change S.toLin (TensorProduct.comm k _ _ (form c.dual)) _ = - S.rep c.dual g (S.toLin (TensorProduct.comm k _ _ (form c.dual)) v) - rw [← S.toLin_map c.dual g, TensorProduct.map_comm, form_invariant] + fun g => LinearMap.ext <| + ((S.isIntertwiningMap_toLin_iff (a := c.dual) (b := c.dual) _).2 fun g => by + rw [TensorProduct.map_comm, form_invariant]).isIntertwining g /-- `toDual` contracts `v` with the second index of the form at `c.dual`. -/ lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : From 8e3cf3869b71a68623f14433ba060b5757d48e39 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Tue, 10 Nov 2026 17:05:58 +0000 Subject: [PATCH 121/242] spike: species over a field, [CommRing k] to [Field k] in the core, Species.finiteDimensional Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 2 +- Physlib/Mathematics/Tensors/Contraction.lean | 2 +- Physlib/Mathematics/Tensors/ElabExamples.lean | 2 +- Physlib/Mathematics/Tensors/HermitianForm.lean | 12 +++++------- Physlib/Mathematics/Tensors/Inverse.lean | 2 +- Physlib/Mathematics/Tensors/LinearMap.lean | 2 +- Physlib/Mathematics/Tensors/Metric.lean | 2 +- Physlib/Mathematics/Tensors/Product.lean | 2 +- Physlib/Mathematics/Tensors/Reindexing.lean | 2 +- Physlib/Mathematics/Tensors/Schouten.lean | 2 +- Physlib/Mathematics/Tensors/Species.lean | 9 +++++++-- Physlib/Mathematics/Tensors/Tensor.lean | 4 ++-- Physlib/Mathematics/Tensors/Tensorial.lean | 4 ++-- 13 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 74ef95da6c..fd93630089 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -109,7 +109,7 @@ structure ConjLabels (basisIdx : K → Type) where namespace Species -variable {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {c c' : Color K} diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 95c8803f9b..f5fe90ca54 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -95,7 +95,7 @@ namespace Tensors namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ κ κ' κ₁ J J' J₁ J₂ : Type} diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index c5aaf01b91..82a011d73a 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -40,7 +40,7 @@ namespace Tensors.Notation open Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 7984340039..64657e4771 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -20,8 +20,7 @@ vectors (`sesqForm`), so that Hermiticity is Mathlib's `LinearMap.IsSymm` and positive-definiteness is `LinearMap.IsPosDef` from `ForMathlib.SesquilinearForm`. The inverse `g^{J̄I}` in `V b.dual ⊗ V a.dual` is again such a form, since `a.dual = b.dual.bar` (`Color.dual_eq_bar_dual`). The inverse of an invertible Hermitian or positive-definite `g_{IJ̄}` -is again Hermitian or positive-definite, and over a field a positive-definite `g_{IJ̄}` is -invertible. +is again Hermitian or positive-definite, and a positive-definite `g_{IJ̄}` is invertible. ## ii. Key results @@ -29,12 +28,12 @@ invertible. - `Species.isSymm_sesqForm_inverse` and `Species.isPosDef_sesqForm_inverse` prove that the inverse of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite. - `Species.toLin_injective_of_isPosDef` proves that the linear map of a positive-definite `g_{IJ̄}` - is injective, and `Species.toLin_bijective_of_isPosDef` that over a field it is bijective. + is injective, and `Species.toLin_bijective_of_isPosDef` that it is bijective. ## iii. Table of contents - A. Hermitian forms and their inverses -- B. Positive-definite forms over a field +- B. Positive-definite forms are invertible ## iv. References @@ -50,7 +49,7 @@ namespace Tensors namespace Species -variable {K : Type} [InvolutiveStar K] {k : Type} [CommRing k] [StarRing k] {G : Type} [Group G] +variable {K : Type} [InvolutiveStar K] {k : Type} [Field k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} @@ -122,7 +121,7 @@ end Species /-! -## B. Positive-definite forms over a field +## B. Positive-definite forms are invertible -/ @@ -138,7 +137,6 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. have hi := toLin_injective_of_isPosDef g hg - have : ∀ c, Module.Finite k (S.V c) := fun c => Module.Finite.of_basis (S.basis c) refine ⟨hi, (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi⟩ rw [← finrank_conj (congrArg Color.dual hb), Module.finrank_eq_card_basis (S.basis b.dual), Module.finrank_eq_card_basis (S.basis b)] diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index a403544fe5..8b2a266bbb 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -55,7 +55,7 @@ namespace Tensors namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {a b : Color K} diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 8b7bd2f273..6440db6c4d 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -75,7 +75,7 @@ namespace Tensors namespace Species -variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} +variable {k : Type} [Field k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : Species k G K basisIdx) /-! diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 013c1afdcf..83c8cf774a 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -78,7 +78,7 @@ namespace Tensors namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index fdb010ed77..208be2b576 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -100,7 +100,7 @@ end ColorEquiv namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 89fb9817db..4311bc4908 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -117,7 +117,7 @@ end ColorEquiv namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ ι₃ : Type} diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index 2ef748a083..a99e415439 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -45,7 +45,7 @@ notation for both orientations of the delta. namespace Tensors.Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] [CharZero k] diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 8f17d5bba9..2cbe24474a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -98,7 +98,7 @@ At a lower color it is the condition at the upper color with the factors swapped coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` with the basis of the dual color is invariant. The basis of a color of kind `κ` is labelled by `basisIdx κ`, the values that an index of kind `κ` runs over, and a color and its dual share these labels. -/ -structure Species (k : Type) [CommRing k] (G : Type) [Group G] (K : Type) +structure Species (k : Type) [Field k] (G : Type) [Group G] (K : Type) (basisIdx : K → Type) [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] where /-- The vector space of a color. -/ V : Color K → Type @@ -119,7 +119,7 @@ attribute [instance] Species.addCommGroup Species.module namespace Species -variable {k : Type} [CommRing k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} +variable {k : Type} [Field k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-- The condition `invariant` at every color from the condition at the upper colors. At a lower @@ -136,6 +136,11 @@ lemma invariant_of_up {V : Color K → Type} [∀ c, AddCommGroup (V c)] [∀ c, | ⟨κ, .down⟩, g, x, y => (Module.Basis.coordPairing_comm _ _ _ _).trans <| (h κ g y x).trans (Module.Basis.coordPairing_comm _ _ _ _) +/-- The space of each color is finite-dimensional, since it has a finite basis. -/ +instance finiteDimensional (S : Species k G K basisIdx) (c : Color K) : + FiniteDimensional k (S.V c) := + Module.Finite.of_basis (S.basis c) + /-! ## B. The contraction diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index ce924634fe..a801ee5905 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -98,7 +98,7 @@ namespace Tensors namespace Species -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-! @@ -301,7 +301,7 @@ noncomputable instance [Fintype ι] : Module.Finite k (S.Tensor c) := .of_basis (S.tensorBasis c) /-- The dimension of tensors is the product of the dimensions of their slots. -/ -lemma finrank_tensor [StrongRankCondition k] [Fintype ι] [DecidableEq ι] (c : ι → Color K) : +lemma finrank_tensor [Fintype ι] [DecidableEq ι] (c : ι → Color K) : Module.finrank k (S.Tensor c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by rw [Module.finrank_eq_card_basis (S.tensorBasis c), Fintype.card_pi] diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 7e5656ca25..41fa568115 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -66,7 +66,7 @@ namespace Tensors /-- A type `M` whose elements are the tensors of `S` with colors `c`. `M` has its own action of the group, `rep`, and `toTensor` identifies `M` with `S.Tensor c` compatibly with it. -/ -class Tensorial {k : outParam Type} [CommRing k] {G : outParam Type} [Group G] {K : outParam Type} +class Tensorial {k : outParam Type} [Field k] {G : outParam Type} [Group G] {K : outParam Type} {basisIdx : outParam (K → Type)} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : outParam (Species k G K basisIdx)) {ι : outParam Type} (c : outParam (ι → Color K)) (M : Type) [AddCommMonoid M] [Module k M] where @@ -77,7 +77,7 @@ class Tensorial {k : outParam Type} [CommRing k] {G : outParam Type} [Group G] { namespace Tensorial -variable {k : Type} [CommRing k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {ι₁ ι₂ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {M N : Type} [AddCommMonoid M] [Module k M] [AddCommMonoid N] [Module k N] From 018ae94e3900853b93bbce04852536c5690bd15a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:08:28 +0000 Subject: [PATCH 122/242] spike: RCLike scalars for the tensor core Species and the core take [RCLike k] in place of [Field k]. Drop the binders RCLike provides: [StarRing k] (Conjugation, HermitianForm), [Preorder k]/[LT k] (HermitianForm, now open scoped ComplexOrder), [NoZeroDivisors k] [CharZero k] (Metric isAlt_formBilin, schouten, formMatrix_schouten, formMatrix_schouten'; Schouten), and [Invertible (2 : k)] (Contraction contr_eq_zero_of_reindex_eq_neg, contr_tmulEquiv_eq_zero_of_antisymm_symm, closed with two_ne_zero). Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 2 +- Physlib/Mathematics/Tensors/Contraction.lean | 8 +++--- Physlib/Mathematics/Tensors/ElabExamples.lean | 2 +- .../Mathematics/Tensors/HermitianForm.lean | 9 ++++--- Physlib/Mathematics/Tensors/Inverse.lean | 2 +- Physlib/Mathematics/Tensors/LinearMap.lean | 2 +- Physlib/Mathematics/Tensors/Metric.lean | 25 ++++++++----------- Physlib/Mathematics/Tensors/Product.lean | 2 +- Physlib/Mathematics/Tensors/Reindexing.lean | 2 +- Physlib/Mathematics/Tensors/Schouten.lean | 5 ++-- Physlib/Mathematics/Tensors/Species.lean | 13 +++++----- Physlib/Mathematics/Tensors/Tensor.lean | 2 +- Physlib/Mathematics/Tensors/Tensorial.lean | 4 +-- 13 files changed, 38 insertions(+), 40 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index fd93630089..218220b49f 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -109,7 +109,7 @@ structure ConjLabels (basisIdx : K → Type) where namespace Species -variable {k : Type} [Field k] [StarRing k] {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {c c' : Color K} diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index f5fe90ca54..80e6f469bc 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -95,7 +95,7 @@ namespace Tensors namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ κ κ' κ₁ J J' J₁ J₂ : Type} @@ -306,7 +306,7 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' /-- A contraction of a tensor that changes sign under a relabeling `π` of its slots is zero, when `π` only permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the form of the index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ -lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] [Invertible (2 : k)] {a : J → Color K} +lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) {t : S.Tensor c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by @@ -320,13 +320,13 @@ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] [Invertible (2 : k)] {a : J have h : contr σ t + contr σ t = 0 := by conv_lhs => enter [1]; rw [ht] rw [contr_reindex, map_neg, hσ, neg_add_cancel] - rw [← one_smul k (contr σ t), ← invOf_mul_self (2 : k), mul_smul, two_smul, h, smul_zero] + exact (smul_eq_zero.1 ((two_smul k _).trans h)).resolve_left two_ne_zero /-- `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. The contraction of `A ⊗ B` is zero when `π₁` reverses the sign of `A`, `π₂` fixes `B`, and together they only permute the pairs. The hypotheses have the form of the index notation, `{A | μ ν = -A | ν μ}ᵀ` and `{B | μ ν = B | ν μ}ᵀ`. -/ -lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] [Invertible (2 : k)] +lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv (prodColors c₁ c₂) (contrColors c' a)) (π₁ : ColorEquiv c₁ c₁) (π₂ : ColorEquiv c₂ c₂) (e : J ≃ J) diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 82a011d73a..2d4bdc0aeb 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -40,7 +40,7 @@ namespace Tensors.Notation open Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 64657e4771..b817e11eca 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -44,12 +44,13 @@ None. @[expose] public section open TensorProduct +open scoped ComplexOrder namespace Tensors namespace Species -variable {K : Type} [InvolutiveStar K] {k : Type} [Field k] [StarRing k] {G : Type} [Group G] +variable {K : Type} [InvolutiveStar K] {k : Type} [RCLike k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} @@ -100,7 +101,7 @@ lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} /-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives `g(y, y) = 0`. -/ -lemma toLin_injective_of_isPosDef [Preorder k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} +lemma toLin_injective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] intro y hy @@ -108,7 +109,7 @@ lemma toLin_injective_of_isPosDef [Preorder k] (g : S.V a ⊗[k] S.V b) {hb : b simpa [sesqForm_apply, hy] using hg.pos y hne /-- The inverse of a positive-definite `g_{IJ̄}` with bijective linear map is positive-definite. -/ -lemma isPosDef_sesqForm_inverse_of_bijective [LT k] (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} +lemma isPosDef_sesqForm_inverse_of_bijective (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsPosDef) (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : (S.sesqForm (S.inverse g) ha).IsPosDef := by @@ -127,7 +128,7 @@ end Species namespace Species -variable {K : Type} [InvolutiveStar K] {k : Type} [Field k] [StarRing k] [Preorder k] {G : Type} +variable {K : Type} [InvolutiveStar K] {k : Type} [RCLike k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 8b2a266bbb..1891ebdc8a 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -55,7 +55,7 @@ namespace Tensors namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {a b : Color K} diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 6440db6c4d..863a60bd64 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -75,7 +75,7 @@ namespace Tensors namespace Species -variable {k : Type} [Field k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} +variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : Species k G K basisIdx) /-! diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 83c8cf774a..61692eb080 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -23,9 +23,9 @@ lowers `v_ν = v^μ η_μν`, raises `w^ν = w_μ η^μν`, and the two are inve The class `S.HasForm κ` is a chosen invariant form on the kind `κ`, `g^μν` at the upper color and `g_μν` at the lower one, with the forms at `c` and `c.dual` inverse to each other. Not every kind -has one, as the SU(3) triplet has none. The inverse is stored, since over a commutative ring a -non-degenerate form need not have an inverse. Symmetry, as for `η`, and antisymmetry, as for the -Weyl `ε`, are the optional classes `S.IsSymmetricForm κ` and `S.IsAntisymmetricForm κ`. +has one, as the SU(3) triplet has none. The inverse is stored with the form. Symmetry, as for +`η`, and antisymmetry, as for the Weyl `ε`, are the optional classes `S.IsSymmetricForm κ` and +`S.IsAntisymmetricForm κ`. `S.toDual c` contracts a vector with the first index of the form at `c.dual`, lowering an upper index and raising a lower one, and `S.toDualAt i` does so at the slot `i` of a tensor. For the @@ -78,7 +78,7 @@ namespace Tensors namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-! @@ -220,10 +220,9 @@ lemma comm_form_eq_smul (c : Color K) [S.HasForm c.kind] (r : k) exact h μ ν /-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's -`LinearMap.IsAlt`. It needs `2 x = 0 → x = 0` in `k`: when `2 = 0` the form `[1]` on `k` is -antisymmetric with `g(1, 1) = 1`. -/ -lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - [NoZeroDivisors k] [CharZero k] : LinearMap.IsAlt (S.formBilin c) := by +`LinearMap.IsAlt`. It uses that `k` has characteristic zero. -/ +lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : + LinearMap.IsAlt (S.formBilin c) := by refine LinearMap.isAlt_iff_eq_neg_flip.mpr (LinearMap.ext₂ fun u v => ?_) rw [LinearMap.neg_apply, LinearMap.neg_apply, LinearMap.flip_apply, formBilin_apply, formBilin_apply, ← contr_toLin_comm, form_antisymm, map_neg, LinearMap.neg_apply, tmul_neg, @@ -387,16 +386,15 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i /-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`: `g(u, v) w + g(v, w) u + g(w, u) v = 0`. In two dimensions every alternating form of three vectors vanishes. -/ -lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] - [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) (u v w : S.V c.dual) : +lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] + (h2 : Fintype.card (basisIdx c.kind) = 2) (u v w : S.V c.dual) : S.formBilin c u v • w + S.formBilin c v w • u + S.formBilin c w u • v = 0 := (isAlt_formBilin (S := S) c).schouten (S.basis c.dual) h2 u v w /-- The Schouten identity in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`: `schouten` at the basis vectors `e^γ`, `e^ρ` and `e^α'`, read at the label `α`. -/ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - [NoZeroDivisors k] [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) - (γ ρ α' α : basisIdx c.kind) : + (h2 : Fintype.card (basisIdx c.kind) = 2) (γ ρ α' α : basisIdx c.kind) : S.formMatrix c γ ρ * (if α = α' then 1 else 0) = S.formMatrix c γ α' * (if ρ = α then 1 else 0) - S.formMatrix c ρ α' * (if γ = α then 1 else 0) := by @@ -410,8 +408,7 @@ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor /-- The Schouten identity in components with the delta read the other way, `g^γρ δ_α^α' = g^γα δ_ρ^α' - g^ρα δ_γ^α'`. -/ lemma formMatrix_schouten' (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - [NoZeroDivisors k] [CharZero k] (h2 : Fintype.card (basisIdx c.kind) = 2) - (γ ρ α α' : basisIdx c.kind) : + (h2 : Fintype.card (basisIdx c.kind) = 2) (γ ρ α α' : basisIdx c.kind) : S.formMatrix c γ ρ * (if α = α' then 1 else 0) = S.formMatrix c γ α * (if ρ = α' then 1 else 0) - S.formMatrix c ρ α * (if γ = α' then 1 else 0) := by diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 208be2b576..cd2c3d3b55 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -100,7 +100,7 @@ end ColorEquiv namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 4311bc4908..25a6a7732e 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -117,7 +117,7 @@ end ColorEquiv namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ ι₃ : Type} diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index a99e415439..38cbc7a3a7 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -45,10 +45,9 @@ notation for both orientations of the delta. namespace Tensors.Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} - (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] [NoZeroDivisors k] - [CharZero k] + (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] /-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`, `g_γρ δ^α_α' = g_γα' δ_ρ^α - g_ρα' δ_γ^α`. -/ diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 2cbe24474a..de06d9d03a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Mathlib.Analysis.RCLike.Basic public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color public import Mathlib.RepresentationTheory.Intertwining @@ -17,10 +18,10 @@ tensors are built from these. ## i. Overview -A species `S : Species k G K basisIdx` gives each color `c` a `k`-module `S.V c`, a -representation `S.rep c` of the group `G` on it, and a basis `S.basis c`. A tensor of `S` with -slot colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with the action of `G` on each -factor. +A species `S : Species k G K basisIdx` over `k = ℝ` or `ℂ` (`RCLike k`) gives each color `c` a +`k`-vector space `S.V c`, a representation `S.rep c` of the group `G` on it, and a basis +`S.basis c`. A tensor of `S` with slot colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with +the action of `G` on each factor. For real Lorentz tensors in `1 + d` dimensions, `k = ℝ`, `G` is the Lorentz group, and there is one kind `vec`, whose basis labels are `basisIdx vec = Fin 1 ⊕ Fin d`. The space of `⟨vec, up⟩` @@ -98,7 +99,7 @@ At a lower color it is the condition at the upper color with the factors swapped coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` with the basis of the dual color is invariant. The basis of a color of kind `κ` is labelled by `basisIdx κ`, the values that an index of kind `κ` runs over, and a color and its dual share these labels. -/ -structure Species (k : Type) [Field k] (G : Type) [Group G] (K : Type) +structure Species (k : Type) [RCLike k] (G : Type) [Group G] (K : Type) (basisIdx : K → Type) [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] where /-- The vector space of a color. -/ V : Color K → Type @@ -119,7 +120,7 @@ attribute [instance] Species.addCommGroup Species.module namespace Species -variable {k : Type} [Field k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} +variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-- The condition `invariant` at every color from the condition at the upper colors. At a lower diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index a801ee5905..373b6e95c9 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -98,7 +98,7 @@ namespace Tensors namespace Species -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] /-! diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 41fa568115..eaaeabe1c2 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -66,7 +66,7 @@ namespace Tensors /-- A type `M` whose elements are the tensors of `S` with colors `c`. `M` has its own action of the group, `rep`, and `toTensor` identifies `M` with `S.Tensor c` compatibly with it. -/ -class Tensorial {k : outParam Type} [Field k] {G : outParam Type} [Group G] {K : outParam Type} +class Tensorial {k : outParam Type} [RCLike k] {G : outParam Type} [Group G] {K : outParam Type} {basisIdx : outParam (K → Type)} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : outParam (Species k G K basisIdx)) {ι : outParam Type} (c : outParam (ι → Color K)) (M : Type) [AddCommMonoid M] [Module k M] where @@ -77,7 +77,7 @@ class Tensorial {k : outParam Type} [Field k] {G : outParam Type} [Group G] {K : namespace Tensorial -variable {k : Type} [Field k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {ι₁ ι₂ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {M N : Type} [AddCommMonoid M] [Module k M] [AddCommMonoid N] [Module k N] From 9ec9f6c19179b6fb1e429341829ce3d5c1e9f806 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:28:34 +0000 Subject: [PATCH 123/242] refactor(Tensors): namespace Tensor for the new tensor library The new library's declarations move from the namespace Tensors to Tensor: Tensor.Species, Tensor.Color, Tensor.ColorEquiv, Tensor.Notation, Tensor.realLorentz, Tensor.complexLorentz. Module paths are unchanged. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 4 +- Physlib/Mathematics/Tensors/Conjugation.lean | 6 +- Physlib/Mathematics/Tensors/Contraction.lean | 22 ++--- Physlib/Mathematics/Tensors/Elab.lean | 44 +++++----- Physlib/Mathematics/Tensors/ElabExamples.lean | 4 +- Physlib/Mathematics/Tensors/ElabTactics.lean | 88 +++++++++---------- .../Mathematics/Tensors/HermitianForm.lean | 4 +- Physlib/Mathematics/Tensors/Inverse.lean | 4 +- Physlib/Mathematics/Tensors/LinearMap.lean | 8 +- Physlib/Mathematics/Tensors/Metric.lean | 4 +- Physlib/Mathematics/Tensors/Product.lean | 4 +- Physlib/Mathematics/Tensors/Reindexing.lean | 4 +- Physlib/Mathematics/Tensors/Schouten.lean | 4 +- Physlib/Mathematics/Tensors/Species.lean | 4 +- Physlib/Mathematics/Tensors/Tensor.lean | 4 +- Physlib/Mathematics/Tensors/Tensorial.lean | 4 +- .../Relativity/Bispinors/Pauli/Tensor.lean | 6 +- .../Tensors/ComplexTensor/Species.lean | 34 +++---- .../Tensors/RealTensor/Species.lean | 12 +-- 19 files changed, 132 insertions(+), 132 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index fd2b3b4e0b..d51eecead3 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -48,7 +48,7 @@ There are no known references for the material in this module. @[expose] public section -namespace Tensors +namespace Tensor /-! @@ -105,4 +105,4 @@ abbrev Color.dual {K : Type} (c : Color K) : Color K := ⟨c.kind, c.variance.du /-- Taking the dual twice gives back the color. -/ lemma Color.dual_dual {K : Type} (c : Color K) : c.dual.dual = c := rfl -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 218220b49f..29d36281c0 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -65,7 +65,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor /-! @@ -294,7 +294,7 @@ lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : variable {κ J : Type} [Fintype J] {b : κ → Color K} {a : J → Color K} /-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of slots. -/ -def _root_.Tensors.ColorEquiv.barContr {c : ι → Color K} (σ : ColorEquiv c (contrColors b a)) : +def _root_.Tensor.ColorEquiv.barContr {c : ι → Color K} (σ : ColorEquiv c (contrColors b a)) : ColorEquiv (fun i => (c i).bar) (contrColors (fun x => (b x).bar) fun j => (a j).bar) := ⟨σ.toEquiv, fun j => by rcases j with _ | _ | _ <;> exact congrArg Color.bar (σ.color_eq _)⟩ @@ -333,4 +333,4 @@ end Tensor end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 80e6f469bc..7e1c77b4f8 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -91,7 +91,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor namespace Species @@ -111,7 +111,7 @@ namespace Tensor /-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of slots. The free index `inl x` has the color `c x`, and the two slots `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors `a j` and `(a j).dual`. -/ -noncomputable abbrev _root_.Tensors.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : +noncomputable abbrev _root_.Tensor.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : κ ⊕ (J ⊕ J) → Color K := prodColors c (prodColors a fun j => (a j).dual) @@ -175,7 +175,7 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) /-- The relabeling of a contraction with no pairs, where every slot is free and keeps its label. -/ -def _root_.Tensors.ColorEquiv.noPairs (c : ι₁ → Color K) : +def _root_.Tensor.ColorEquiv.noPairs (c : ι₁ → Color K) : ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where toEquiv.toFun := .inl toEquiv.invFun := Sum.elim id (Sum.elim Empty.elim Empty.elim) @@ -221,7 +221,7 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → -/ /-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two slots, in either order. -/ -def _root_.Tensors.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} +def _root_.Tensor.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) (j' : J') : Prop := (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inl j')) ∧ @@ -236,7 +236,7 @@ instance [DecidableEq ι₁] {a : J → Color K} {a' : J' → Color K} infer_instance /-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ -lemma _root_.Tensors.ColorEquiv.SamePair.left_unique {a : J → Color K} {a' : J' → Color K} +lemma _root_.Tensor.ColorEquiv.SamePair.left_unique {a : J → Color K} {a' : J' → Color K} {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j₁ j₂ : J} {j' : J'} (h₁ : σ.SamePair τ j₁ j') (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by have hσ := σ.toEquiv.symm.injective @@ -247,7 +247,7 @@ lemma _root_.Tensors.ColorEquiv.SamePair.left_unique {a : J → Color K} {a' : J · simpa using hσ (h₁.trans h₂.symm) /-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ -lemma _root_.Tensors.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : J' → Color K} +lemma _root_.Tensor.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : J' → Color K} {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j : J} {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by have hτ := τ.toEquiv.symm.injective @@ -256,7 +256,7 @@ lemma _root_.Tensors.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : /-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair of `τ` is a pair of `σ`. The slots of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ -lemma _root_.Tensors.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' → Color K} +lemma _root_.Tensor.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' → Color K} {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by @@ -345,7 +345,7 @@ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] /-- The pairs of two contractions in sequence, `σ₁` and then `σ₂`, as one choice of pairs `J₁ ⊕ J₂`, the pairs `J₁` of `σ₁` followed by the pairs `J₂` of `σ₂`. -/ -noncomputable def _root_.Tensors.ColorEquiv.chain {c : ι₁ → Color K} {c₁ : κ₁ → Color K} +noncomputable def _root_.Tensor.ColorEquiv.chain {c : ι₁ → Color K} {c₁ : κ₁ → Color K} {c' : κ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) : ColorEquiv c (contrColors c' (prodColors a₁ a₂)) := @@ -372,7 +372,7 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ /-- The pairs `σ` of `T`, as pairs of `T ⊗ U` for a factor `U` on the right, with colors `d`. The indices of `U` are free and come after the free indices of `T`. -/ -noncomputable def _root_.Tensors.ColorEquiv.tmulRight {c : ι₁ → Color K} {c' : κ → Color K} +noncomputable def _root_.Tensor.ColorEquiv.tmulRight {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := (σ.sumCongr (.refl d)).trans (ColorEquiv.swapLast c' d _) @@ -394,7 +394,7 @@ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color /-- The pairs `σ` of `T`, as pairs of `U ⊗ T` for a factor `U` on the left, with colors `d`. The indices of `U` are free and come before the free indices of `T`. -/ -noncomputable def _root_.Tensors.ColorEquiv.tmulLeft {c : ι₁ → Color K} +noncomputable def _root_.Tensor.ColorEquiv.tmulLeft {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : ColorEquiv (prodColors d c) (contrColors (prodColors d c') a) := ((ColorEquiv.refl d).sumCongr σ).trans (ColorEquiv.sumAssoc _ _ _).symm @@ -475,4 +475,4 @@ end Tensor end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 63fbcdc65e..f1e75da263 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -82,7 +82,7 @@ public meta section open Lean Meta Elab Term -namespace Tensors.Notation +namespace Tensor.Notation /-! @@ -187,7 +187,7 @@ def slotEquiv (dom cod : Slots) (pre : List Nat) : TermElabM Term := do /-- The entries of a literal color family `![a, b, …]`, or of `prodColors` of literal families. -/ partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do - if c.isAppOfArity ``Tensors.prodColors 5 then + if c.isAppOfArity ``Tensor.prodColors 5 then return (· ++ ·) <$> (← colorList? (c.getArg! 3)) <*> (← colorList? (c.getArg! 4)) let (cs, _, tail) ← Matrix.matchVecConsPrefix (mkNatLit 0) c return if tail.isAppOfArity ``Matrix.vecEmpty 1 then some cs else none @@ -200,7 +200,7 @@ def colorsTerm (cs : List Expr) : TermElabM Term := do and colors. -/ def literalTensorType? (ty : Expr) : MetaM (Option (Slots × Expr × List Expr)) := do let ty ← whnfR (← instantiateMVars ty) - unless ty.isAppOf ``Tensors.Species.Tensor do return none + unless ty.isAppOf ``Tensor.Species.Tensor do return none let args := ty.getAppArgs let some slots ← Slots.ofType? args[args.size - 2]! | return none let c ← instantiateMVars args[args.size - 1]! @@ -230,8 +230,8 @@ structure Node where /-- The tensor of `a` relabeled onto `Fin pre.length` with the colors `cs`, its slot `pre[j]` becoming the slot `j`. -/ def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term := do - `(Tensors.Species.reindex $(← exprToSyntax a.species) - (Tensors.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) $(← slotEquiv a.slots (.fin pre.length) pre)) + `(Tensor.Species.reindex $(← exprToSyntax a.species) + (Tensor.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) $(← slotEquiv a.slots (.fin pre.length) pre)) $a.tm) /-- The expression relabeled to `Fin n`, its slots numbered in order. -/ @@ -242,12 +242,12 @@ def Node.toFin (a : Node) : TermElabM Node := do /-- The expression on `Fin n` with its slot `i` moved to the dual color by `S.toDualAt`. -/ def Node.toDualAt (a : Node) (i : Nat) : TermElabM Node := do - let colors := a.colors.set i (← mkAppM ``Tensors.Color.dual #[a.colors[i]!]) + let colors := a.colors.set i (← mkAppM ``Tensor.Color.dual #[a.colors[i]!]) -- The color `col` of the slot is passed explicitly, so the form is found at the kind of `col` and -- every lowering at that kind uses the same instance. The proofs `c i = col` and -- `c' i = col.dual` are `id rfl`, since a bare `rfl` has the type `c' i = c' i`, which `rw` -- matches only after unfolding `![…] i`. Away from `i` the colors agree by cases. - let tm ← `(Tensors.Species.toDualAt $(← exprToSyntax a.species) (c' := $(← colorsTerm colors)) + let tm ← `(Tensor.Species.toDualAt $(← exprToSyntax a.species) (c' := $(← colorsTerm colors)) ($(quote i) : Fin $(quote a.names.length)) $(← exprToSyntax a.colors[i]!) (id rfl) (id rfl) (by intro x hx; fin_cases x <;> first | rfl | exact absurd rfl hx) $a.tm) return { a with colors, tm } @@ -260,10 +260,10 @@ def tensorOf (T : Term) : TermElabM (Expr × Expr) := do let ty ← whnfR (← instantiateMVars (← inferType e)) if ty.isMVar then throwError "index notation: the type of {T} is not known; give it, as `({T} : S.Tensor ![…])`" - if ty.isAppOf ``Tensors.Species.Tensor then return (e, ty) + if ty.isAppOf ``Tensor.Species.Tensor then return (e, ty) try let e ← withoutErrToSorry do - let e ← elabTerm (← `(Tensors.Tensorial.toTensor $(← exprToSyntax e))) none + let e ← elabTerm (← `(Tensor.Tensorial.toTensor $(← exprToSyntax e))) none synthesizeSyntheticMVarsNoPostponing instantiateMVars e return (e, ← whnfR (← instantiateMVars (← inferType e))) @@ -275,7 +275,7 @@ def tensorOf (T : Term) : TermElabM (Expr × Expr) := do checking that an arrow matches the variance of `col`. -/ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) := do let arrow (x : Ident) (up : Bool) : TermElabM (Name × Bool) := do - let isUp ← whnfD (← mkAppM ``Tensors.Variance.isUp #[← mkAppM ``Tensors.Color.variance #[col]]) + let isUp ← whnfD (← mkAppM ``Tensor.Variance.isUp #[← mkAppM ``Tensor.Color.variance #[col]]) unless isUp.isConstOf (if up then ``Bool.true else ``Bool.false) do throwError "index notation: {if up then "↓" else "↑"}{x.getId} needs a visibly \ {if up then "upper" else "lower"} index" @@ -314,7 +314,7 @@ def contract (a : Node) : TermElabM Node := do | [_] => free := free ++ [i] | [(_, i₁), (_, i₂)] => if i == i₁ then - unless ← isDefEq a.colors[i₂]! (← mkAppM ``Tensors.Color.dual #[a.colors[i₁]!]) do + unless ← isDefEq a.colors[i₂]! (← mkAppM ``Tensor.Color.dual #[a.colors[i₁]!]) do throwError "index notation: {nm} joins the colors {a.colors[i₁]!} and \ {a.colors[i₂]!}, which are not dual" pairs := pairs ++ [(i₁, i₂)] @@ -328,10 +328,10 @@ def contract (a : Node) : TermElabM Node := do -- slot to `inr (inr j)`. let σ ← slotEquiv a.slots (.sum (.fin m) (.sum J J)) (free ++ pairs.map (·.1) ++ pairs.map (·.2)) - let pairColors ← J.fn ``Tensors.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) - let tm ← `(Tensors.Species.Tensor.contr (S := $(← exprToSyntax a.species)) - (Tensors.ColorEquiv.mk - (c₂ := Tensors.contrColors $(← colorsTerm (free.map (a.colors[·]!))) $pairColors) $σ) + let pairColors ← J.fn ``Tensor.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) + let tm ← `(Tensor.Species.Tensor.contr (S := $(← exprToSyntax a.species)) + (Tensor.ColorEquiv.mk + (c₂ := Tensor.contrColors $(← colorsTerm (free.map (a.colors[·]!))) $pairColors) $σ) $a.tm) return ⟨free.map (a.names[·]!), free.map (a.colors[·]!), .fin m, tm, a.species⟩ @@ -363,7 +363,7 @@ partial def uncontracted (stx : Syntax) : TermElabM Node := do | `(tensorIndexExpr| $a:tensorIndexExpr ⊗ $b:tensorIndexExpr) => do let a ← uncontracted a let b ← uncontracted b - let tm ← `(Tensors.Species.tmulEquiv $(← exprToSyntax a.species) _ _ + let tm ← `(Tensor.Species.tmulEquiv $(← exprToSyntax a.species) _ _ (TensorProduct.tmul _ $a.tm $b.tm)) return ⟨a.names ++ b.names, a.colors ++ b.colors, .sum a.slots b.slots, tm, a.species⟩ | `(tensorIndexExpr| ($a:tensorIndexExpr)) => build a @@ -394,8 +394,8 @@ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do throwError "index notation: the free index {a.names[i]!} has the color {col}, but its slot \ is expected to have the color {cs[i]!}" if a.slots == slots then return a.tm - `(Tensors.Species.reindex $(← exprToSyntax a.species) - (Tensors.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) + `(Tensor.Species.reindex $(← exprToSyntax a.species) + (Tensor.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) $(← slotEquiv a.slots slots (List.range slots.size))) $a.tm) /-- The term of `e` for the expected type. It is relabeled onto the slots of a tensor type, and read @@ -404,19 +404,19 @@ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do let some ty := expectedType? | return (← build e).tm let ty ← instantiateMVars ty if ty.getAppFn.isMVar || ty.isSort then return (← build e).tm - if (← whnfR ty).isAppOf ``Tensors.Species.Tensor then + if (← whnfR ty).isAppOf ``Tensor.Species.Tensor then return (← buildTo e ty).getD (← build e).tm -- For a `Tensorial` type, the tensor type is the codomain of `toTensor`. let M ← exprToSyntax ty let s ← saveState let some tensorTy ← try withoutErrToSorry do - let f ← elabTerm (← `(fun m : $M => Tensors.Tensorial.toTensor m)) none + let f ← elabTerm (← `(fun m : $M => Tensor.Tensorial.toTensor m)) none synthesizeSyntheticMVarsNoPostponing pure (some (← inferType (← instantiateMVars f)).bindingBody!) catch _ => s.restore; pure none | return (← build e).tm let some tm ← buildTo e tensorTy | return (← build e).tm - `((Tensors.Tensorial.toTensor (M := $M)).symm $tm) + `((Tensor.Tensorial.toTensor (M := $M)).symm $tm) /-- Elaborates index notation. An equation is allowed only at the top, possibly in parentheses. -/ @[term_elab tensorIndexSyntax] def elabTensorIndex : TermElab := fun stx expectedType? => do @@ -428,6 +428,6 @@ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do | `({ $e:tensorIndexExpr }ᵀ) => elabTerm (← buildFor e expectedType?) expectedType? | _ => throwUnsupportedSyntax -end Tensors.Notation +end Tensor.Notation end diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 2d4bdc0aeb..7f023a83a3 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -36,7 +36,7 @@ There are no declarations, only examples. -/ -namespace Tensors.Notation +namespace Tensor.Notation open Species @@ -476,4 +476,4 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ end Metric -end Tensors.Notation +end Tensor.Notation diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index fa90d068f0..272f1f1a2d 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -45,7 +45,7 @@ public meta section open Lean Meta Elab Term -namespace Tensors.Notation +namespace Tensor.Notation /-! @@ -80,7 +80,7 @@ macro_rules -- evaluated. The option `+instances` reaches those inside instance arguments. try dsimp +instances only [ColorEquiv.toEquiv_symm, Equiv.symm_symm, Equiv.coe_fn_mk, Sum.elim_inl, Sum.elim_inr, Matrix.cons_val, Function.comp_apply, - Tensors.Notation.reduceCast]) + Tensor.Notation.reduceCast]) /-! @@ -198,7 +198,7 @@ private def rewriteSubterm (x : Expr) (thm : Term) (lhs : Option Bool := none) /-- The two factors `T` and `U` of a product `S.tmulEquiv c₁ c₂ (T ⊗ₜ U)`. -/ private def productFactors? (e : Expr) : Option (Expr × Expr) := do - let x := (← bundledArg? ``Tensors.Species.tmulEquiv e).consumeMData + let x := (← bundledArg? ``Tensor.Species.tmulEquiv e).consumeMData if x.isAppOf ``TensorProduct.tmul then some (x.appFn!.appArg!, x.appArg!) else none /-- The factors of a product, from left to right. -/ @@ -223,9 +223,9 @@ private partial def isLeftNested (e : Expr) : Bool := /-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ private def contractedProducts : TacticM (Expr × Expr) := do let (_, l, r) ← goalEq - let some p := bundledArg? ``Tensors.Species.Tensor.contr l + let some p := bundledArg? ``Tensor.Species.Tensor.contr l | throwError "tensor_regroup: the left side is not a contraction" - let some q := bundledArg? ``Tensors.Species.Tensor.contr r + let some q := bundledArg? ``Tensor.Species.Tensor.contr r | throwError "tensor_regroup: the right side is not a contraction" return (p, q) @@ -234,16 +234,16 @@ goal no longer changes, at most ten times. A side that is not a contraction is r pairs (`Species.Tensor.contr_noPairs`). The three steps run separately, since together their lemmas loop. -/ private def flattenContractions : TacticM Unit := do - wrapSides "tensor_regroup" (bundledArg? ``Tensors.Species.Tensor.contr · |>.isSome) - (← `(Tensors.Species.Tensor.contr_noPairs _)) + wrapSides "tensor_regroup" (bundledArg? ``Tensor.Species.Tensor.contr · |>.isSome) + (← `(Tensor.Species.Tensor.contr_noPairs _)) for _ in [0:10] do let before ← instantiateMVars (← getMainTarget) - evalTactic (← `(tactic| try simp only [Tensors.Species.Tensor.tmulEquiv_contr_left, - Tensors.Species.Tensor.tmulEquiv_contr_right, Tensors.Species.Tensor.contr_contr])) - evalTactic (← `(tactic| try simp only [← Tensors.Species.tmulEquiv_assoc])) - evalTactic (← `(tactic| try simp only [Tensors.Species.tmulEquiv_reindex_left, - Tensors.Species.tmulEquiv_reindex_right, Tensors.Species.reindex_reindex, - Tensors.Species.Tensor.contr_reindex, Tensors.Species.Tensor.reindex_contr])) + evalTactic (← `(tactic| try simp only [Tensor.Species.Tensor.tmulEquiv_contr_left, + Tensor.Species.Tensor.tmulEquiv_contr_right, Tensor.Species.Tensor.contr_contr])) + evalTactic (← `(tactic| try simp only [← Tensor.Species.tmulEquiv_assoc])) + evalTactic (← `(tactic| try simp only [Tensor.Species.tmulEquiv_reindex_left, + Tensor.Species.tmulEquiv_reindex_right, Tensor.Species.reindex_reindex, + Tensor.Species.Tensor.contr_reindex, Tensor.Species.Tensor.reindex_contr])) -- The two sides may become equal, and `simp` then closes the goal. if (← getGoals).isEmpty then return if (← instantiateMVars (← getMainTarget)) == before then return @@ -255,14 +255,14 @@ the contraction. -/ private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do let fs := factors q let thm ← if m = 0 then - `((Tensors.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) + `((Tensor.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) $(← exprToSyntax fs[0]!)).symm) else - `((Tensors.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) + `((Tensor.Species.tmulEquiv_swapLast $(← exprToSyntax (leadingProduct q n m)) $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) rewriteSubterm (leadingProduct q n (m + 2)) thm (some false) - evalTactic (← `(tactic| conv_rhs => simp only [Tensors.Species.tmulEquiv_reindex_left, - Tensors.Species.reindex_reindex, Tensors.Species.Tensor.contr_reindex])) + evalTactic (← `(tactic| conv_rhs => simp only [Tensor.Species.tmulEquiv_reindex_left, + Tensor.Species.reindex_reindex, Tensor.Species.Tensor.contr_reindex])) /-- The ways to match each factor on the left with a different factor on the right, given for each factor on the left the factors on the right equal to it. The matching in order comes first. -/ @@ -313,7 +313,7 @@ private def matchFactors : TacticM Unit := do -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free -- color variables of the goal although the bijections of slots do not depend on them. try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in - exact Tensors.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) + exact Tensor.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) (of_decide_eq_true rfl) _)) catch _ => throwError "tensor_regroup: the two sides contract different pairs of slots, for \ {tried} of matching their equal factors" @@ -336,12 +336,12 @@ private def leftover (xs ys : List Expr) : MetaM (List Expr) := do /-- The color `a` of a delta `S.deltaTensor a`. -/ private def deltaColor? (x : Expr) : Option Expr := let x := x.consumeMData - if x.isAppOf ``Tensors.Species.deltaTensor then some x.appArg! else none + if x.isAppOf ``Tensor.Species.deltaTensor then some x.appArg! else none /-- The species `S` and the color `c` of a form `S.formTensor c`. -/ private def formArgs? (x : Expr) : Option (Expr × Expr) := let x := x.consumeMData - if x.isAppOf ``Tensors.Species.formTensor then + if x.isAppOf ``Tensor.Species.formTensor then let args := x.getAppArgs some (args[args.size - 3]!, args[args.size - 2]!) else none @@ -391,7 +391,7 @@ private partial def slotNumber : Slots → Expr → MetaM Nat /-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each free index and at each slot of each pair. -/ private def readContraction (e : Expr) : MetaM ReadContraction := do - let some P := bundledArg? ``Tensors.Species.Tensor.contr e + let some P := bundledArg? ``Tensor.Species.Tensor.contr e | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" let τ := e.consumeMData.appFn!.appArg!.appArg! let some (slots, _, _) ← literalTensorType? (← inferType P) @@ -407,7 +407,7 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do let (κ, J) := (cargs[1]!, cargs[2]!) let JJ ← mkAppM ``Sum #[J, J] let at_ (y : Expr) : MetaM Nat := do - slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensors.ColorEquiv.toEquiv #[τ], y]) + slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensor.ColorEquiv.toEquiv #[τ], y]) let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) let pairs ← (← slotElems J).mapM fun j => do let half (left : Bool) := do @@ -439,8 +439,8 @@ private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) (se TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q - let lem := mkIdent <| if second then ``Tensors.Species.Tensor.contr_deltaTensor_swap - else ``Tensors.Species.Tensor.contr_deltaTensor + let lem := mkIdent <| if second then ``Tensor.Species.Tensor.contr_deltaTensor_swap + else ``Tensor.Species.Tensor.contr_deltaTensor let thm ← `(($lem ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) rewriteSubterm P thm (some lhs) @@ -470,7 +470,7 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Boo let tries ← orientations.flatMapM fun second => do -- The swapped identity inserts `deltaTensor b.dual` at a slot of color `b`. -- To insert the requested `deltaTensor a`, that slot must therefore have color `a.dual`. - let b ← if second then mkAppM ``Tensors.Color.dual #[a] else pure a + let b ← if second then mkAppM ``Tensor.Color.dual #[a] else pure a let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => return !inForm.getD i false && (← isDefEqGoal col b) return slotsOfColor.map fun (_, i) => (i, second, b) @@ -491,7 +491,7 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni throwError "tensor_regroup: the factors left over are not pairs of forms" -- Only the colors whose dual form is also left over can cancel, each tried once. let fs ← fs.eraseDups.filterM fun (_, c) => do - let cd ← mkAppM ``Tensors.Color.dual #[c] + let cd ← mkAppM ``Tensor.Color.dual #[c] fs.anyM fun (_, c') => isDefEqGoal c' cd match ds with | [d] => @@ -505,13 +505,13 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni | _, _ => [false, true] let tries := swaps.flatMap fun sw => fs.map (·, sw) firstSuccess tries m!"tensor_regroup: no two forms left over are inverse" fun ((S, c), sw) => do - let lem := mkIdent <| if sw then ``Tensors.Species.reindex_contr_formTensor_formTensor - else ``Tensors.Species.contr_formTensor_formTensor + let lem := mkIdent <| if sw then ``Tensor.Species.reindex_contr_formTensor_formTensor + else ``Tensor.Species.contr_formTensor_formTensor let thm ← `(($lem (S := $(← exprToSyntax S)) $(← exprToSyntax c)).symm) rewriteSubterm d thm (some lhs) regroupContractions | [] => firstSuccess fs m!"tensor_regroup: no two forms left over are inverse" fun (_, c) => do - insertDeltaAnywhere lhs (← mkAppM ``Tensors.Color.dual #[c]) (skipForms := true) + insertDeltaAnywhere lhs (← mkAppM ``Tensor.Color.dual #[c]) (skipForms := true) | _ => throwError "tensor_regroup: more than one factor is left over against forms" /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two contractions of the same factors, up to @@ -572,8 +572,8 @@ private def flipForm (antisym : Bool) (F : Expr) (k : Nat) : TacticM Unit := do let some (S, c) := formArgs? F | throwError "tensor_regroup: internal error, {F} is not a form" let (S, c) := (← exprToSyntax S, ← exprToSyntax c) let thm ← if antisym then - `(neg_eq_iff_eq_neg.mp (Tensors.Species.formTensor_antisymm (S := $S) $c).symm) - else `((Tensors.Species.formTensor_symm (S := $S) $c).symm) + `(neg_eq_iff_eq_neg.mp (Tensor.Species.formTensor_antisymm (S := $S) $c).symm) + else `((Tensor.Species.formTensor_symm (S := $S) $c).symm) -- A form with no instance of the symmetry fails here, instead of logging an error. Term.withoutErrToSorry <| rewriteSubterm F thm (k := some k) @@ -690,7 +690,7 @@ private def regroup : TacticM Unit := do /-- Whether `e` contains a lowering or raising `S.toDualAt`. -/ private def hasLowering (e : Expr) : Bool := - (e.find? (·.isConstOf ``Tensors.Species.toDualAt)).isSome + (e.find? (·.isConstOf ``Tensor.Species.toDualAt)).isSome /-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as contractions with the form (`Species.toDualAt_eq_contr`) when it fails and the goal has one. When @@ -703,7 +703,7 @@ private def regroupWithLowerings : TacticM Unit := do -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They -- are expanded only when needed, since every expanded form is one more factor to match. s.restore - evalTactic (← `(tactic| simp only [Tensors.Species.toDualAt_eq_contr])) + evalTactic (← `(tactic| simp only [Tensor.Species.toDualAt_eq_contr])) try regroup catch e' => -- The expansion changes the factors, not the summands or scalars, so an error about those -- reads better before it. @@ -723,25 +723,25 @@ are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be `Fin n` with a literal `n`. -/ elab "tensor_slots" : tactic => do - evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_mapAt, Tensors.Species.mapAt, - Tensors.Species.mapSlots_mapSlots])) - let isMapSlots (e : Expr) := (bundledArg? ``Tensors.Species.mapSlots e).isSome + evalTactic (← `(tactic| try simp only [Tensor.Species.toDualAt_eq_mapAt, Tensor.Species.mapAt, + Tensor.Species.mapSlots_mapSlots])) + let isMapSlots (e : Expr) := (bundledArg? ``Tensor.Species.mapSlots e).isSome let (_, l, r) ← goalEq "tensor_slots" unless isMapSlots l || isMapSlots r do throwError "tensor_slots: neither side lowers or raises an index" -- Each side is checked before it is unified, since a failed unification with a side that is a -- definition, such as a named tensor, unfolds it. - wrapSides "tensor_slots" isMapSlots (← `(Tensors.Species.mapSlots_id_apply _)) + wrapSides "tensor_slots" isMapSlots (← `(Tensor.Species.mapSlots_id_apply _)) evalTactic (← `(tactic| focus - refine congrArg (fun F => Tensors.Species.mapSlots _ F _) (funext ?_) + refine congrArg (fun F => Tensor.Species.mapSlots _ F _) (funext ?_) intro x fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, - LinearMap.id_apply, Tensors.Species.slotMapsAt_apply_self, - Tensors.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, + LinearMap.id_apply, Tensor.Species.slotMapsAt_apply_self, + Tensor.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, - Tensors.Species.cast_apply, cast_cast, cast_eq, Tensors.Species.toDual_toDual])) + Tensor.Species.cast_apply, cast_cast, cast_eq, Tensor.Species.toDual_toDual])) /-! @@ -806,7 +806,7 @@ private def flattenTerm (A : Expr) (expand : Bool) : TacticM Expr := do let gs ← getGoals setGoals [g.mvarId!] try - if expand then evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + if expand then evalTactic (← `(tactic| try simp only [Tensor.Species.toDualAt_eq_contr])) flattenContractions return (← goalEq).2.1 finally setGoals gs @@ -835,7 +835,7 @@ private partial def regroupUsing : List Term → TacticM Unit let read? ← try if flip then replaceMainGoal [← (← getMainGoal).applySymm] if expand then - evalTactic (← `(tactic| try simp only [Tensors.Species.toDualAt_eq_contr])) + evalTactic (← `(tactic| try simp only [Tensor.Species.toDualAt_eq_contr])) flattenContractions let (_, L, _) ← goalEq pure (some (L, ← readContraction L, ← readContraction (← flattenTerm A expand))) @@ -895,6 +895,6 @@ elab_rules : tactic | `(tactic| tensor_regroup) => regroupWithLowerings | `(tactic| tensor_regroup using $es,*) => regroupUsing es.getElems.toList -end Tensors.Notation +end Tensor.Notation end diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index b817e11eca..965ef672dd 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -46,7 +46,7 @@ None. open TensorProduct open scoped ComplexOrder -namespace Tensors +namespace Tensor namespace Species @@ -150,4 +150,4 @@ lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 1891ebdc8a..2c817ef7ae 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -51,7 +51,7 @@ None. open TensorProduct -namespace Tensors +namespace Tensor namespace Species @@ -202,4 +202,4 @@ lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 863a60bd64..56183d1ace 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -71,7 +71,7 @@ There are no known references for the material in this module. open TensorProduct -namespace Tensors +namespace Tensor namespace Species @@ -189,7 +189,7 @@ variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} /-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two slots that contracts the second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are `0` and `1`. -/ -def _root_.Tensors.ColorEquiv.matMul (a c b : Color K) : +def _root_.Tensor.ColorEquiv.matMul (a c b : Color K) : ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where toEquiv.toFun | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 @@ -232,7 +232,7 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) /-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is `ColorEquiv.matMul` at any slot of a tensor on any slots. -/ -def _root_.Tensors.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) +def _root_.Tensor.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where toEquiv := @@ -329,4 +329,4 @@ end Tensor end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 61692eb080..d09bdf78ef 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -74,7 +74,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor namespace Species @@ -416,4 +416,4 @@ lemma formMatrix_schouten' (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFo end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index cd2c3d3b55..162841202f 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -52,7 +52,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor /-! @@ -343,4 +343,4 @@ lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 25a6a7732e..784764f511 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -56,7 +56,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor /-! @@ -222,4 +222,4 @@ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index 38cbc7a3a7..b933773d0f 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -43,7 +43,7 @@ notation for both orientations of the delta. -/ -namespace Tensors.Species +namespace Tensor.Species variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} @@ -69,4 +69,4 @@ lemma schouten_formTensor_deltaTensor_dual (h2 : Fintype.card (basisIdx c.kind) rw [← sub_eq_add_neg] exact formMatrix_schouten' c h2 _ _ _ _ -end Tensors.Species +end Tensor.Species diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index de06d9d03a..2728eb031a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -79,7 +79,7 @@ There are no known references for the material in this module. open TensorProduct -namespace Tensors +namespace Tensor /-! @@ -291,4 +291,4 @@ lemma contr_cast_tmul_cast_symm {d e a b : Color K} (x : S.V d) (y : S.V e) (p : end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 373b6e95c9..5ab750f3f6 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -94,7 +94,7 @@ None. open TensorProduct PiTensorProduct -namespace Tensors +namespace Tensor namespace Species @@ -512,4 +512,4 @@ end end Species -end Tensors +end Tensor diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index eaaeabe1c2..c561a595b1 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -56,7 +56,7 @@ Instances come from the space of one index (`ofIndexSpace`), from an equivalent open TensorProduct -namespace Tensors +namespace Tensor /-! @@ -173,4 +173,4 @@ end ofIndexSpace end Tensorial -end Tensors +end Tensor diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 6b71372604..0d1242a1e3 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -78,7 +78,7 @@ The anticommutators follow from the trace identities and the Schouten identity o open TensorProduct Fermion Lorentz MatrixGroups open PauliMatrix (pauliMatrix) -namespace Tensors.complexLorentz +namespace Tensor.complexLorentz /-! @@ -91,7 +91,7 @@ namespace Tensors.complexLorentz noncomputable def pauliTensor : ℂT[up, upL, upR] := {ComplexVector.pauliVal | μ α β}ᵀ /-- `σT` is the Pauli tensor `pauliTensor`, to be distinguished from the Pauli matrices `σ`. -/ -scoped[Tensors.complexLorentz] notation "σT" => pauliTensor +scoped[Tensor.complexLorentz] notation "σT" => pauliTensor /-- The components of `σT^{μαβ̇}` are the entries of the Pauli matrices. -/ lemma tensorBasis_repr_pauliTensor (μ : Π i, Idx (![up, upL, upR] i).kind) : @@ -277,4 +277,4 @@ lemma pauliContrDown_pauliContr_mul_add : rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR] tensor_regroup -end Tensors.complexLorentz +end Tensor.complexLorentz diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index a5c0bf437b..4d8eeb6d60 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -22,7 +22,7 @@ Complex Lorentz tensors have three kinds of index, the left-handed kind `left`, the right-handed kind `right`, as in `ψ̄_α̇`, with basis labels `Fin 2`, and the vector kind `vec`, with labels `Fin 1 ⊕ Fin 3`. Conjugation `star` swaps `left` and `right` and fixes `vec`. With the two variances this gives six colors, `upL`, `downL`, `upR`, `downR`, `up` and `down`. Under -`open scoped Tensors.complexLorentz`, `ℂT[upL, downR]` is the space of the tensors `T^α_β̇`. +`open scoped Tensor.complexLorentz`, `ℂT[upL, downR]` is the space of the tensors `T^α_β̇`. The species is built on `Fermion.LeftHandedWeyl`, `Fermion.RightHandedWeyl`, their duals, `Lorentz.ComplexVector` and `Lorentz.ComplexCoVector`, with their representations and bases. An @@ -88,7 +88,7 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ open TensorProduct Fermion Lorentz MatrixGroups -namespace Tensors +namespace Tensor /-! @@ -226,11 +226,11 @@ example : upL.bar = upR ∧ downR.bar = downL ∧ up.bar = up := ⟨rfl, rfl, rf /-- The complex Lorentz tensors with the colors listed, as `ℂT[upL, downR]` for `T^α_β̇`. The colors are read in the namespace `complexLorentz`. The notation is active under -`open scoped Tensors.complexLorentz`. -/ +`open scoped Tensor.complexLorentz`. -/ scoped syntax (name := complexLorentzSyntax) "ℂT[" term,* "]" : term macro_rules (kind := complexLorentzSyntax) - | `(ℂT[$cs,*]) => `(Tensors.complexLorentz.Tensor (open Tensors.complexLorentz in ![$cs,*])) + | `(ℂT[$cs,*]) => `(Tensor.complexLorentz.Tensor (open Tensor.complexLorentz in ![$cs,*])) /-- `ℂT[upL, downR]` is the space of tensors `T^α_β̇`. -/ example : ℂT[upL, downR] = complexLorentz.Tensor ![upL, downR] := rfl @@ -335,40 +335,40 @@ noncomputable instance (κ : complexLorentz.Kind) : complexLorentz.HasForm κ wh namespace complexLorentz /-- The form `ε^αβ` on two upper left-handed indices. -/ -scoped notation "εL" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.upL +scoped notation "εL" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.upL /-- The form `ε_αβ` on two lower left-handed indices. -/ -scoped notation "εL'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.downL +scoped notation "εL'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.downL /-- The form `ε^α̇β̇` on two upper right-handed indices. -/ -scoped notation "εR" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.upR +scoped notation "εR" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.upR /-- The form `ε_α̇β̇` on two lower right-handed indices. -/ -scoped notation "εR'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.downR +scoped notation "εR'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.downR /-- The metric `η^μν` on two upper vector indices. -/ -scoped notation "η" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.up +scoped notation "η" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.up /-- The metric `η_μν` on two lower vector indices. -/ -scoped notation "η'" => Species.formTensor Tensors.complexLorentz Tensors.complexLorentz.down +scoped notation "η'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.down /-- The delta `δ^μ_ν`, on the colors `![up, down]`. -/ -scoped notation "δ" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.down +scoped notation "δ" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.down /-- The delta `δ_μ^ν`, on the colors `![down, up]`. -/ -scoped notation "δ'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.up +scoped notation "δ'" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.up /-- The delta `δ^α_β`, on the colors `![upL, downL]`. -/ -scoped notation "δL" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.downL +scoped notation "δL" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.downL /-- The delta `δ_α^β`, on the colors `![downL, upL]`. -/ -scoped notation "δL'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.upL +scoped notation "δL'" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.upL /-- The delta `δ^α̇_β̇`, on the colors `![upR, downR]`. -/ -scoped notation "δR" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.downR +scoped notation "δR" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.downR /-- The delta `δ_α̇^β̇`, on the colors `![downR, upR]`. -/ -scoped notation "δR'" => Species.deltaTensor Tensors.complexLorentz Tensors.complexLorentz.upR +scoped notation "δR'" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.upR end complexLorentz @@ -687,4 +687,4 @@ example : Species.Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by end complexLorentz -end Tensors +end Tensor diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index c4ae6c596f..0b0db3ec56 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -26,7 +26,7 @@ labels `μ` run over `Fin 1 ⊕ Fin d`, with `Sum.inl 0` the time direction and directions. A Lorentz transformation `Λ : LorentzGroup d` acts on an upper index by `(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by `(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named `realLorentz.up` and `realLorentz.down`, and `ℝT[d, up, down]` is the space of the tensors -`T^μ_ν`, under `open scoped Tensors.realLorentz`. +`T^μ_ν`, under `open scoped Tensor.realLorentz`. The contraction `u^μ w_μ` is the coordinate pairing `∑ μ, u^μ w_μ` of the two bases and the delta is `δ_μ^ν = ∑ μ, e^μ ⊗ e_μ`. The one physical input is that the pairing is Lorentz invariant @@ -85,7 +85,7 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ open TensorProduct Lorentz Matrix -namespace Tensors +namespace Tensor /-! @@ -176,12 +176,12 @@ abbrev down : Color Kind := ⟨.vec, .down⟩ /-- The real Lorentz tensors in `d + 1` dimensions with the colors listed, as `ℝT[d, up, down]` for `T^μ_ν`. The colors are read in the namespace `realLorentz`. The notation is active under -`open scoped Tensors.realLorentz`. -/ +`open scoped Tensor.realLorentz`. -/ scoped syntax (name := realLorentzSyntax) "ℝT[" term,+ "]" : term macro_rules (kind := realLorentzSyntax) - | `(ℝT[$d]) => `((Tensors.realLorentz $d).Tensor ![]) - | `(ℝT[$d, $cs,*]) => `((Tensors.realLorentz $d).Tensor (open Tensors.realLorentz in ![$cs,*])) + | `(ℝT[$d]) => `((Tensor.realLorentz $d).Tensor ![]) + | `(ℝT[$d, $cs,*]) => `((Tensor.realLorentz $d).Tensor (open Tensor.realLorentz in ![$cs,*])) end realLorentz @@ -455,4 +455,4 @@ lemma contr_vector_coVector_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) ( end realLorentz -end Tensors +end Tensor From 628803d5353887ef34496e9a6aa8c1aca4a10779 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:30:56 +0000 Subject: [PATCH 124/242] refactor(Tensors): index for slot in declaration names and the tactic oneSlotEquiv, twoSlotEquiv, mapSlots, permSlots, congrSlots and slotMapsAt become oneIndexEquiv, twoIndexEquiv, mapIndices, permIndices, congrIndices and indexMapsAt, with their lemmas. The tactic tensor_slots becomes tensor_indices, and the elaborator's Slots, slotEquiv, slotElems and slotNumber become Indices, indexEquiv, indexElems and indexNumber. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 53 ++++----- Physlib/Mathematics/Tensors/ElabExamples.lean | 20 ++-- Physlib/Mathematics/Tensors/ElabTactics.lean | 64 +++++------ Physlib/Mathematics/Tensors/Inverse.lean | 8 +- Physlib/Mathematics/Tensors/LinearMap.lean | 26 ++--- Physlib/Mathematics/Tensors/Metric.lean | 14 +-- Physlib/Mathematics/Tensors/Product.lean | 54 ++++----- Physlib/Mathematics/Tensors/Reindexing.lean | 18 +-- Physlib/Mathematics/Tensors/Tensor.lean | 104 +++++++++--------- Physlib/Mathematics/Tensors/Tensorial.lean | 4 +- .../Relativity/Bispinors/Pauli/Tensor.lean | 2 +- .../Tensors/RealTensor/Species.lean | 6 +- 12 files changed, 187 insertions(+), 186 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index f1e75da263..fc97697769 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -127,29 +127,29 @@ syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term -/ /-- A slot type built from `Fin n`, `Unit` and `⊕`. Its slots are numbered `0, 1, …` in order. -/ -inductive Slots where +inductive Indices where /-- `Fin n`. -/ | fin (n : Nat) /-- `Unit`. -/ | unit /-- `a ⊕ b`. -/ - | sum (a b : Slots) + | sum (a b : Indices) deriving BEq /-- The number of slots. -/ -def Slots.size : Slots → Nat +def Indices.size : Indices → Nat | .fin n => n | .unit => 1 | .sum a b => a.size + b.size /-- The slot type as a term. -/ -def Slots.type : Slots → TermElabM Term +def Indices.type : Indices → TermElabM Term | .fin n => `(Fin $(quote n)) | .unit => `(Unit) | .sum a b => do `($(← a.type) ⊕ $(← b.type)) /-- The slot number `i` as a term. -/ -def Slots.elem : Slots → Nat → TermElabM Term +def Indices.elem : Indices → Nat → TermElabM Term | .fin n, i => `(($(quote i) : Fin $(quote n))) | .unit, _ => `(()) | .sum a b, i => @@ -157,27 +157,27 @@ def Slots.elem : Slots → Nat → TermElabM Term /-- The function on a slot type taking the values `vs` in slot order, built from vector literals on `Fin n`, constants on `Unit` and `comb` on `⊕`. -/ -def Slots.fn (comb : Name) : Slots → List Term → TermElabM Term +def Indices.fn (comb : Name) : Indices → List Term → TermElabM Term | .fin _, vs => `(![$(vs.toArray),*]) | .unit, vs => `(fun _ : Unit => $(vs[0]!)) | .sum a b, vs => do `($(mkIdent comb) $(← a.fn comb (vs.take a.size)) $(← b.fn comb (vs.drop a.size))) /-- The slot type of a type built from `Fin n` and `⊕` with a literal `n`. -/ -partial def Slots.ofType? (ι : Expr) : MetaM (Option Slots) := do +partial def Indices.ofType? (ι : Expr) : MetaM (Option Indices) := do let ι ← whnfR ι if ι.isAppOfArity ``Fin 1 then return (← (evalNat ι.appArg!).run).map .fin else if ι.isAppOfArity ``Sum 2 then - let some a ← Slots.ofType? (ι.getArg! 0) | return none - let some b ← Slots.ofType? (ι.getArg! 1) | return none + let some a ← Indices.ofType? (ι.getArg! 0) | return none + let some b ← Indices.ofType? (ι.getArg! 1) | return none return some (.sum a b) else return none /-- The bijection from `dom` to `cod` sending the slot `pre[j]` of `dom` to the slot `j` of `cod`. The list `pre` names every slot of `dom` once. Its inverse identities are proved slot by slot by `fin_cases`. -/ -def slotEquiv (dom cod : Slots) (pre : List Nat) : TermElabM Term := do +def indexEquiv (dom cod : Indices) (pre : List Nat) : TermElabM Term := do let toFun ← dom.fn ``Sum.elim (← (List.range dom.size).mapM fun i => cod.elem (pre.idxOf i)) let invFun ← cod.fn ``Sum.elim (← pre.mapM dom.elem) -- `decide` would need a `DecidableEq` instance on the whole slot type, which exceeds the @@ -198,14 +198,14 @@ def colorsTerm (cs : List Expr) : TermElabM Term := do /-- The slots, the color family `c` and its entries of a tensor type `S.Tensor c` with literal slots and colors. -/ -def literalTensorType? (ty : Expr) : MetaM (Option (Slots × Expr × List Expr)) := do +def literalTensorType? (ty : Expr) : MetaM (Option (Indices × Expr × List Expr)) := do let ty ← whnfR (← instantiateMVars ty) unless ty.isAppOf ``Tensor.Species.Tensor do return none let args := ty.getAppArgs - let some slots ← Slots.ofType? args[args.size - 2]! | return none + let some indices ← Indices.ofType? args[args.size - 2]! | return none let c ← instantiateMVars args[args.size - 1]! let some cs ← colorList? c | return none - return (slots, c, cs) + return (indices, c, cs) /-! @@ -221,7 +221,7 @@ structure Node where /-- The color of each slot. -/ colors : List Expr /-- The slot type. -/ - slots : Slots + indices : Indices /-- The tensor. -/ tm : Term /-- The species. -/ @@ -231,14 +231,15 @@ structure Node where becoming the slot `j`. -/ def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term := do `(Tensor.Species.reindex $(← exprToSyntax a.species) - (Tensor.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) $(← slotEquiv a.slots (.fin pre.length) pre)) + (Tensor.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) + $(← indexEquiv a.indices (.fin pre.length) pre)) $a.tm) /-- The expression relabeled to `Fin n`, its slots numbered in order. -/ def Node.toFin (a : Node) : TermElabM Node := do let n := a.names.length - if a.slots == .fin n then return a - return { a with slots := .fin n, tm := ← a.reindex (List.range n) a.colors } + if a.indices == .fin n then return a + return { a with indices := .fin n, tm := ← a.reindex (List.range n) a.colors } /-- The expression on `Fin n` with its slot `i` moved to the dual color by `S.toDualAt`. -/ def Node.toDualAt (a : Node) (i : Nat) : TermElabM Node := do @@ -292,14 +293,14 @@ and flipped at each `τ(μ)`. -/ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do let (e, ty) ← tensorOf T let args := ty.getAppArgs - let some slots ← Slots.ofType? args[args.size - 2]! + let some indices ← Indices.ofType? args[args.size - 2]! | throwError "index notation: the slots of {T} must be `Fin n` and `⊕`" let some colors ← colorList? args[args.size - 1]! | throwError "index notation: the colors of {T} must be a literal list" unless colors.length = idxs.size do throwError "index notation: {T} has {colors.length} slots, {idxs.size} indices given" let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => indexName i col - let a : Node := ⟨kinds.map (·.1), colors, slots, ← exprToSyntax e, args[args.size - 3]!⟩ + let a : Node := ⟨kinds.map (·.1), colors, indices, ← exprToSyntax e, args[args.size - 3]!⟩ if kinds.all (!·.2) then return a kinds.zipIdx.foldlM (init := ← a.toFin) fun a ((_, d), i) => if d then a.toDualAt i else pure a @@ -323,10 +324,10 @@ def contract (a : Node) : TermElabM Node := do let m := free.length -- The pairs are labeled by `Unit` for one pair, as in `ColorEquiv.matMul`, and by -- `(… ⊕ Unit) ⊕ Unit` for more, as `ColorEquiv.chain` combines two contractions in sequence. - let J := (List.range (pairs.length - 1)).foldl (fun J _ => Slots.sum J .unit) .unit + let J := (List.range (pairs.length - 1)).foldl (fun J _ => Indices.sum J .unit) .unit -- The free slots go to `inl k`, the first slot of the pair `j` to `inr (inl j)` and its second -- slot to `inr (inr j)`. - let σ ← slotEquiv a.slots (.sum (.fin m) (.sum J J)) + let σ ← indexEquiv a.indices (.sum (.fin m) (.sum J J)) (free ++ pairs.map (·.1) ++ pairs.map (·.2)) let pairColors ← J.fn ``Tensor.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) let tm ← `(Tensor.Species.Tensor.contr (S := $(← exprToSyntax a.species)) @@ -365,7 +366,7 @@ partial def uncontracted (stx : Syntax) : TermElabM Node := do let b ← uncontracted b let tm ← `(Tensor.Species.tmulEquiv $(← exprToSyntax a.species) _ _ (TensorProduct.tmul _ $a.tm $b.tm)) - return ⟨a.names ++ b.names, a.colors ++ b.colors, .sum a.slots b.slots, tm, a.species⟩ + return ⟨a.names ++ b.names, a.colors ++ b.colors, .sum a.indices b.indices, tm, a.species⟩ | `(tensorIndexExpr| ($a:tensorIndexExpr)) => build a | `(tensorIndexExpr| $a:tensorIndexExpr + $b:tensorIndexExpr) => do let l ← build a @@ -382,10 +383,10 @@ end /-- The tensor of `e` on the slots and colors of the tensor type `target`, its free indices in the order written. It is `none` when `target` is not a tensor type with literal slots and colors. -/ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do - let some (slots, c, cs) ← literalTensorType? target | return none + let some (indices, c, cs) ← literalTensorType? target | return none let u ← uncontracted e -- A product with no contraction is already on the slots of its factors. - let a ← if u.slots == slots && u.names.eraseDups.length == u.names.length then pure u + let a ← if u.indices == indices && u.names.eraseDups.length == u.names.length then pure u else contract u unless cs.length == a.colors.length do throwError "index notation: {a.colors.length} free indices, but {cs.length} slots expected" @@ -393,10 +394,10 @@ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do unless ← isDefEq col cs[i]! do throwError "index notation: the free index {a.names[i]!} has the color {col}, but its slot \ is expected to have the color {cs[i]!}" - if a.slots == slots then return a.tm + if a.indices == indices then return a.tm `(Tensor.Species.reindex $(← exprToSyntax a.species) (Tensor.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) - $(← slotEquiv a.slots slots (List.range slots.size))) $a.tm) + $(← indexEquiv a.indices indices (List.range indices.size))) $a.tm) /-- The term of `e` for the expected type. It is relabeled onto the slots of a tensor type, and read back by `Tensorial.toTensor.symm` for a `Tensorial` type. -/ diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 7f023a83a3..7c0bfd200b 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -12,7 +12,7 @@ public import Physlib.Mathematics.Tensors.ElabTactics # Examples of index notation Examples and tests of the index notation `{…}ᵀ` and of the tactics `tensor_components`, -`tensor_regroup` and `tensor_slots`. +`tensor_regroup` and `tensor_indices`. ## i. Overview @@ -157,7 +157,7 @@ example : {v | τ(μ)}ᵀ = {v | ↓μ}ᵀ := rfl /-- `τ` needs no visible variance, so it flips an index of any color, `(w_μ)^μ = w^μ`. -/ example (w : S.Tensor ![c]) : {{w | τ(μ)}ᵀ | τ(μ)}ᵀ = w := by - tensor_slots + tensor_indices /-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] @@ -452,27 +452,27 @@ section Metric variable [S.HasForm κ] [S.HasForm c.kind] -/-- Lowering and then raising by `tensor_slots`, `(v_μ)^μ = v^μ`. -/ +/-- Lowering and then raising by `tensor_indices`, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by - tensor_slots + tensor_indices -/-- `tensor_slots` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ +/-- `tensor_indices` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by - tensor_slots + tensor_indices -- An equation with no lowering or raising is rejected. -/-- error: tensor_slots: neither side lowers or raises an index -/ +/-- error: tensor_indices: neither side lowers or raises an index -/ #guard_msgs in example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by - tensor_slots + tensor_indices /-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by - tensor_slots + tensor_indices /-- Lowerings at two slots commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by - tensor_slots + tensor_indices end Metric diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 272f1f1a2d..f278279465 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -15,7 +15,7 @@ Tactics that prove equations of tensors written in the index notation `{…}ᵀ` ## i. Overview Three tactics prove equations written in index notation. `tensor_components` works on the -components, `tensor_regroup` on the contractions and `tensor_slots` on the lowerings and raisings. +components, `tensor_regroup` on the contractions and `tensor_indices` on the lowerings and raisings. Examples are in `Physlib.Mathematics.Tensors.ElabExamples`. ## ii. Key results @@ -25,7 +25,7 @@ Examples are in `Physlib.Mathematics.Tensors.ElabExamples`. or ordered differently, with the same signs and scalars, up to deltas, inverse pairs of forms, swaps of symmetric and antisymmetric forms, and lowerings written as contractions. With `using e₁, …`, it first rewrites a sub-product of either side by each identity `eᵢ`. -- `tensor_slots` : proves an equation of two chains of lowerings and raisings of the same tensor. +- `tensor_indices` : proves an equation of two chains of lowerings and raisings of the same tensor. ## iii. Table of contents @@ -360,7 +360,7 @@ private structure ReadContraction where /-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in slot order. -/ -private partial def slotElems (ι : Expr) : MetaM (List Expr) := do +private partial def indexElems (ι : Expr) : MetaM (List Expr) := do let ι ← whnfR ι if ι.isAppOfArity ``Fin 1 then let some n ← (evalNat ι.appArg!).run @@ -371,12 +371,12 @@ private partial def slotElems (ι : Expr) : MetaM (List Expr) := do else if ι.isConstOf ``Empty then return [] else if ι.isAppOfArity ``Sum 2 then let (a, b) := (ι.getArg! 0, ι.getArg! 1) - return (← (← slotElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ - (← (← slotElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) + return (← (← indexElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ + (← (← indexElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" /-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ -private partial def slotNumber : Slots → Expr → MetaM Nat +private partial def indexNumber : Indices → Expr → MetaM Nat | .fin _, x => do let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" @@ -384,8 +384,8 @@ private partial def slotNumber : Slots → Expr → MetaM Nat | .unit, _ => return 0 | .sum a b, x => do let x ← whnfD x - if x.isAppOfArity ``Sum.inl 3 then slotNumber a x.appArg! - else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← slotNumber b x.appArg!) + if x.isAppOfArity ``Sum.inl 3 then indexNumber a x.appArg! + else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← indexNumber b x.appArg!) else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" /-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each @@ -394,7 +394,7 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do let some P := bundledArg? ``Tensor.Species.Tensor.contr e | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" let τ := e.consumeMData.appFn!.appArg!.appArg! - let some (slots, _, _) ← literalTensorType? (← inferType P) + let some (indices, _, _) ← literalTensorType? (← inferType P) | throwError "tensor_regroup: the slots or colors of a product are not literal" let fs := factors P let sizes ← fs.mapM fun f => do @@ -407,9 +407,9 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do let (κ, J) := (cargs[1]!, cargs[2]!) let JJ ← mkAppM ``Sum #[J, J] let at_ (y : Expr) : MetaM Nat := do - slotNumber slots (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensor.ColorEquiv.toEquiv #[τ], y]) - let free ← (← slotElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) - let pairs ← (← slotElems J).mapM fun j => do + indexNumber indices (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensor.ColorEquiv.toEquiv #[τ], y]) + let free ← (← indexElems κ).mapM fun x => do at_ (← mkAppOptM ``Sum.inl #[κ, JJ, x]) + let pairs ← (← indexElems J).mapM fun j => do let half (left : Bool) := do at_ (← mkAppOptM ``Sum.inr #[κ, JJ, ← mkAppOptM (if left then ``Sum.inl else ``Sum.inr) #[J, J, j]]) @@ -435,14 +435,14 @@ private def deltaOrientations (e : Expr) : MetaM (List Bool) := do /-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, if `lhs`, or of the right side, by `Species.Tensor.contr_deltaTensor`. With `second`, uses `Species.Tensor.contr_deltaTensor_swap` instead, inserting a delta of color `a.dual`. -/ -private def insertDelta (lhs : Bool) (a : Expr) (slots : Slots) (slot : Nat) (second : Bool) : +private def insertDelta (lhs : Bool) (a : Expr) (indices : Indices) (index : Nat) (second : Bool) : TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q let lem := mkIdent <| if second then ``Tensor.Species.Tensor.contr_deltaTensor_swap else ``Tensor.Species.Tensor.contr_deltaTensor let thm ← `(($lem - ($(← slots.elem slot) : $(← slots.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) + ($(← indices.elem index) : $(← indices.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) rewriteSubterm P thm (some lhs) mutual @@ -456,7 +456,7 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Boo TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q - let some (slots, _, colors) ← literalTensorType? (← inferType P) + let some (indices, _, colors) ← literalTensorType? (← inferType P) | throwError "tensor_regroup: the slots or colors of a product are not literal" -- Whether each slot belongs to a form, from the number of slots of each factor in turn. let inForm ← (factors P).flatMapM fun f => do @@ -471,12 +471,12 @@ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Boo -- The swapped identity inserts `deltaTensor b.dual` at a slot of color `b`. -- To insert the requested `deltaTensor a`, that slot must therefore have color `a.dual`. let b ← if second then mkAppM ``Tensor.Color.dual #[a] else pure a - let slotsOfColor ← colors.zipIdx.filterM fun (col, i) => + let indicesOfColor ← colors.zipIdx.filterM fun (col, i) => return !inForm.getD i false && (← isDefEqGoal col b) - return slotsOfColor.map fun (_, i) => (i, second, b) + return indicesOfColor.map fun (_, i) => (i, second, b) firstSuccess tries m!"tensor_regroup: no slot has the color {a} of a delta" - fun (slot, second, b) => do - insertDelta lhs b slots slot second + fun (index, second, b) => do + insertDelta lhs b indices index second regroupContractions /-- Matches pairs of forms `g` and `g⁻¹`, the factors `gs` left over on one side, with the factors @@ -716,29 +716,29 @@ private def regroupWithLowerings : TacticM Unit := do -/ -/-- `tensor_slots` proves an equation of two chains of lowerings and raisings of the same tensor, +/-- `tensor_indices` proves an equation of two chains of lowerings and raisings of the same tensor, such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain -is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapSlots_mapSlots`), and the two maps +is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapIndices_mapIndices`), and the two maps are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side -with no lowering or raising is the slotwise identity (`Species.mapSlots_id`). The slots must be +with no lowering or raising is the slotwise identity (`Species.mapIndices_id`). The slots must be `Fin n` with a literal `n`. -/ -elab "tensor_slots" : tactic => do +elab "tensor_indices" : tactic => do evalTactic (← `(tactic| try simp only [Tensor.Species.toDualAt_eq_mapAt, Tensor.Species.mapAt, - Tensor.Species.mapSlots_mapSlots])) - let isMapSlots (e : Expr) := (bundledArg? ``Tensor.Species.mapSlots e).isSome - let (_, l, r) ← goalEq "tensor_slots" - unless isMapSlots l || isMapSlots r do - throwError "tensor_slots: neither side lowers or raises an index" + Tensor.Species.mapIndices_mapIndices])) + let isMapIndices (e : Expr) := (bundledArg? ``Tensor.Species.mapIndices e).isSome + let (_, l, r) ← goalEq "tensor_indices" + unless isMapIndices l || isMapIndices r do + throwError "tensor_indices: neither side lowers or raises an index" -- Each side is checked before it is unified, since a failed unification with a side that is a -- definition, such as a named tensor, unfolds it. - wrapSides "tensor_slots" isMapSlots (← `(Tensor.Species.mapSlots_id_apply _)) + wrapSides "tensor_indices" isMapIndices (← `(Tensor.Species.mapIndices_id_apply _)) evalTactic (← `(tactic| focus - refine congrArg (fun F => Tensor.Species.mapSlots _ F _) (funext ?_) + refine congrArg (fun F => Tensor.Species.mapIndices _ F _) (funext ?_) intro x fin_cases x <;> refine LinearMap.ext fun v => ?_ <;> simp only [Fin.zero_eta, Fin.isValue, Fin.mk_one, Fin.reduceFinMk, LinearMap.comp_apply, - LinearMap.id_apply, Tensor.Species.slotMapsAt_apply_self, - Tensor.Species.slotMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, + LinearMap.id_apply, Tensor.Species.indexMapsAt_apply_self, + Tensor.Species.indexMapsAt_apply_ne, ne_eq, Fin.reduceEq, not_false_eq_true, Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap, Representation.Equiv.trans_apply, Tensor.Species.cast_apply, cast_cast, cast_eq, Tensor.Species.toDual_toDual])) diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 2c817ef7ae..a481c6c79b 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -108,14 +108,14 @@ lemma comp_toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toL /-- `g_μν g^νρ = δ_μ^ρ`, the contraction of `g` with its inverse on the adjacent indices. -/ lemma contr_mul_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : Tensor.contr (ColorEquiv.matMul a b a.dual) - (S.tmulEquiv _ _ (S.twoSlotEquiv a b g ⊗ₜ S.twoSlotEquiv _ _ (S.inverse g))) = + (S.tmulEquiv _ _ (S.twoIndexEquiv a b g ⊗ₜ S.twoIndexEquiv _ _ (S.inverse g))) = S.deltaTensor a.dual := (Tensor.contr_matMul_eq_deltaTensor_iff g (S.inverse g)).2 (toLin_inverse_comp g h) /-- `g^νμ g_μρ = δ^ν_ρ`, the contraction of the inverse with `g` on the adjacent indices. -/ lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : Tensor.contr (ColorEquiv.matMul b.dual a.dual b) - (S.tmulEquiv _ _ (S.twoSlotEquiv _ _ (S.inverse g) ⊗ₜ S.twoSlotEquiv a b g)) = + (S.tmulEquiv _ _ (S.twoIndexEquiv _ _ (S.inverse g) ⊗ₜ S.twoIndexEquiv a b g)) = S.deltaTensor b := (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 (comp_toLin_inverse g h) @@ -141,7 +141,7 @@ lemma inverse_eq_right_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.t lemma inverse_eq_of_contr_eq_deltaTensor (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : Tensor.contr (ColorEquiv.matMul a b a.dual) - (S.tmulEquiv _ _ (S.twoSlotEquiv a b g ⊗ₜ S.twoSlotEquiv _ _ N)) = + (S.tmulEquiv _ _ (S.twoIndexEquiv a b g ⊗ₜ S.twoIndexEquiv _ _ N)) = S.deltaTensor a.dual) : S.inverse g = N := inverse_eq_left_inv g h ((Tensor.contr_matMul_eq_deltaTensor_iff g N).1 hN) @@ -195,7 +195,7 @@ lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : S.reindex (ColorEquiv.swapTwo c c.dual) (Tensor.contr (ColorEquiv.matMul c c c.dual) (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual))) = S.deltaTensor c := by - rw [contr_formTensor_formTensor, deltaTensor, reindex_twoSlotEquiv, + rw [contr_formTensor_formTensor, deltaTensor, reindex_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta c.dual) = S.delta c from Module.Basis.comm_coordDelta _ _] rfl diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 56183d1ace..509c83e8fe 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -152,18 +152,18 @@ variable (S) in /-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper color `c` and `δ^μ_ν` for a lower one. -/ noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := - S.twoSlotEquiv c.dual c (S.delta c) + S.twoIndexEquiv c.dual c (S.delta c) /-- The delta tensor is invariant, `Λ δ = δ`. -/ lemma deltaTensor_invariant (c : Color K) (Λ : G) : Λ • S.deltaTensor c = S.deltaTensor c := - (S.twoSlotEquiv _ c).toIntertwiningMap.map_invariant (S.delta_invariant c Λ) + (S.twoIndexEquiv _ c).toIntertwiningMap.map_invariant (S.delta_invariant c Λ) /-- The components of the delta tensor are the coordinates of `S.delta c` in Mathlib's product basis, at the pair of labels `(μ 0, μ 1)`. -/ lemma tensorBasis_repr_deltaTensor (c : Color K) (μ : Π j, basisIdx (![c.dual, c] j).kind) : (S.tensorBasis ![c.dual, c]).repr (S.deltaTensor c) μ = ((S.basis c.dual).tensorProduct (S.basis c)).repr (S.delta c) (μ 0, μ 1) := - tensorBasis_repr_twoSlotEquiv _ _ _ μ + tensorBasis_repr_twoIndexEquiv _ _ _ μ /-- The components of the delta tensor form the identity matrix, `δ_μ^ν = 1` for `μ = ν` and `0` otherwise. -/ @@ -202,8 +202,8 @@ def _root_.Tensor.ColorEquiv.matMul (a c b : Color K) : by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : contr (ColorEquiv.matMul a c b) - (S.tmulEquiv _ _ (S.twoSlotEquiv a c M ⊗ₜ S.twoSlotEquiv _ b N)) = - S.twoSlotEquiv a b ((S.toLin N).lTensor _ M) := by + (S.tmulEquiv _ _ (S.twoIndexEquiv a c M ⊗ₜ S.twoIndexEquiv _ b N)) = + S.twoIndexEquiv a b ((S.toLin N).lTensor _ M) := by induction M using TensorProduct.inductionOn with | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] | tmul m₁ m₂ => @@ -211,7 +211,7 @@ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual | add N N' hN hN' => simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] | tmul n₁ n₂ => - simp only [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, + simp only [twoIndexEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, LinearMap.lTensor_tmul, Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, tmul_smul, map_smul] congr 2 @@ -223,9 +223,9 @@ maps compose to the identity, `Y ∘ X = id`. -/ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) (Y : S.V y.dual ⊗[k] S.V x.dual) : contr (ColorEquiv.matMul x y x.dual) - (S.tmulEquiv _ _ (S.twoSlotEquiv x y X ⊗ₜ S.twoSlotEquiv _ _ Y)) = S.deltaTensor x.dual ↔ + (S.tmulEquiv _ _ (S.twoIndexEquiv x y X ⊗ₜ S.twoIndexEquiv _ _ Y)) = S.deltaTensor x.dual ↔ S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by - rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoSlotEquiv _ _)).eq_iff, + rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoIndexEquiv _ _)).eq_iff, ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, Module.Basis.coordTensorHomEquiv_coordDelta] @@ -266,7 +266,7 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : contr (ColorEquiv.matMulAt i a b ha hi h) - (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoSlotEquiv a.dual b F)) = + (S.tmulEquiv c ![a.dual, b] (S.pure p ⊗ₜ S.twoIndexEquiv a.dual b F)) = S.pure (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) := by induction F using TensorProduct.inductionOn with | add F F' hF hF' => @@ -275,7 +275,7 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι | tmul x y => rw [Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, map_smul] conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] - rw [twoSlotEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] + rw [twoIndexEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] congr 2 funext z by_cases hz : z = i @@ -294,10 +294,10 @@ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) (t : S.Tensor c) : contr (ColorEquiv.matMulAt i a b ha hi h) - (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoSlotEquiv a.dual b F)) = + (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoIndexEquiv a.dual b F)) = S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by refine LinearMap.congr_fun (f := (contr _).toLinearMap ∘ₗ (S.tmulEquiv c ![a.dual, b]).toLinearMap - ∘ₗ (TensorProduct.mk k _ _).flip (S.twoSlotEquiv a.dual b F)) (ext_pure fun p => ?_) t + ∘ₗ (TensorProduct.mk k _ _).flip (S.twoIndexEquiv a.dual b F)) (ext_pure fun p => ?_) t -- The vectors of `p` moved along `h` away from `i`. The vector at `i` is replaced below. let q : Π x, S.V (c' x) := fun x => if hx : x = i then 0 else S.cast (h x hx) (p x) exact (contr_matMulAt_pure i a b ha hi h F p q (fun x hx => by simp [q, hx])).trans @@ -321,7 +321,7 @@ lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by - rw [deltaTensor, reindex_twoSlotEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a + rw [deltaTensor, reindex_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a from Module.Basis.comm_coordDelta _ _] exact contr_deltaTensor i a ha t diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index d09bdf78ef..3630e14c6f 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -276,31 +276,31 @@ variable (S) in /-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a lower one. -/ noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] := - S.twoSlotEquiv c c (form c) + S.twoIndexEquiv c c (form c) /-- The form is an invariant tensor. Every group element fixes it, as every Lorentz transformation fixes the Minkowski metric `η`. -/ lemma formTensor_invariant (c : Color K) [S.HasForm c.kind] (Λ : G) : Λ • S.formTensor c = S.formTensor c := - (S.twoSlotEquiv c c).toIntertwiningMap.map_invariant (form_invariant c Λ) + (S.twoIndexEquiv c c).toIntertwiningMap.map_invariant (form_invariant c Λ) /-- The components of the form tensor are those of the form, `g_μν` at the pair of labels `(μ 0, μ 1)`. -/ lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] (μ : Π j, basisIdx (![c, c] j).kind) : (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := - tensorBasis_repr_twoSlotEquiv _ _ _ μ + tensorBasis_repr_twoIndexEquiv _ _ _ μ /-- A symmetric form is symmetric under the slot swap as a tensor, `g^μν = g^νμ`. -/ lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = S.formTensor c := by - rw [formTensor, reindex_twoSlotEquiv, form_symm] + rw [formTensor, reindex_twoIndexEquiv, form_symm] /-- An antisymmetric form is antisymmetric under the slot swap as a tensor, `g^μν = -g^νμ`. -/ lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = -S.formTensor c := by - rw [formTensor, reindex_twoSlotEquiv, form_antisymm, map_neg] + rw [formTensor, reindex_twoIndexEquiv, form_antisymm, map_neg] /-! @@ -339,7 +339,7 @@ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i exact (S.congrAt i _ h).symm_apply_apply t /-- Lowering or raising at a slot is `toDual` at that slot, read as a linear map. Chains of -lowerings and raisings at several slots compose slot by slot through `mapSlots_mapSlots`. -/ +lowerings and raisings at several slots compose slot by slot through `mapIndices_mapIndices`. -/ lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a ha hi h t = @@ -374,7 +374,7 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a.dual a.dual) (S.formTensor a.dual))) := by - rw [formTensor, reindex_twoSlotEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] + rw [formTensor, reindex_twoIndexEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] rfl /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 162841202f..51938cd73a 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -22,7 +22,7 @@ is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Ma The product is associative and symmetric up to a relabeling of slots, and the group acts on it factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on -two slots by `twoSlotEquiv`. The components of a product are the products of the components. +two slots by `twoIndexEquiv`. The components of a product are the products of the components. ## ii. Key results @@ -31,7 +31,7 @@ two slots by `twoSlotEquiv`. The components of a product are the products of the - `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. -- `Species.twoSlotEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots. +- `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots. - `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. ## iii. Table of contents @@ -239,15 +239,15 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : ## D. Tensors on one and two slots A species gives objects with two indices, such as a metric `η_μν`, in Mathlib's binary -tensor product `V a ⊗ V b`. `twoSlotEquiv` identifies this space with the tensors `S.Tensor ![a, b]` -on two slots, so that such an object can be used in index notation. +tensor product `V a ⊗ V b`. `twoIndexEquiv` identifies this space with the tensors +`S.Tensor ![a, b]` on two slots, so that such an object can be used in index notation. -/ variable (S) in /-- A vector `v` of `V c` as a tensor with the one slot `![c]`, by Mathlib's `PiTensorProduct.subsingletonEquiv`. -/ -noncomputable def oneSlotEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c]) := +noncomputable def oneIndexEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c]) := .mk ((subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm) fun g => LinearMap.ext fun v => by change S.pure _ = g • S.pure _ @@ -256,43 +256,43 @@ noncomputable def oneSlotEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c] /-- The tensor of `v` is the pure tensor with `v` on its one slot. -/ @[simp] -lemma oneSlotEquiv_apply (c : Color K) (v : S.V c) : - S.oneSlotEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := +lemma oneIndexEquiv_apply (c : Color K) (v : S.V c) : + S.oneIndexEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := congrArg S.pure (funext fun i => by fin_cases i; rfl) /-- The tensor of the basis vector `S.basis c (μ 0)` is the basis tensor with label `μ`. -/ -lemma oneSlotEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : - S.oneSlotEquiv c (S.basis c (μ 0)) = S.tensorBasis ![c] μ := by - rw [oneSlotEquiv_apply, tensorBasis_apply] +lemma oneIndexEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : + S.oneIndexEquiv c (S.basis c (μ 0)) = S.tensorBasis ![c] μ := by + rw [oneIndexEquiv_apply, tensorBasis_apply] exact congrArg S.pure (funext fun j => by fin_cases j; rfl) variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` -is the tensor `η_μν`. Each factor is a one-slot tensor by `oneSlotEquiv`, and the slots +is the tensor `η_μν`. Each factor is a one-slot tensor by `oneIndexEquiv`, and the slots `Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by Mathlib's `finSumFinEquiv`. -/ -noncomputable def twoSlotEquiv (a b : Color K) : +noncomputable def twoIndexEquiv (a b : Color K) : ((S.rep a).tprod (S.rep b)).Equiv (S.tensorRep ![a, b]) := - ((S.oneSlotEquiv a).tensorCongr (S.oneSlotEquiv b)).trans <| + ((S.oneIndexEquiv a).tensorCongr (S.oneIndexEquiv b)).trans <| (S.tmulEquiv ![a] ![b]).trans (S.reindex (ColorEquiv.mk (c₁ := prodColors ![a] ![b]) (c₂ := ![a, b]) finSumFinEquiv)) /-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ @[simp] -lemma twoSlotEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : - S.twoSlotEquiv a b (x ⊗ₜ y) = +lemma twoIndexEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : + S.twoIndexEquiv a b (x ⊗ₜ y) = S.pure (c := ![a, b]) (Fin.cons x (Fin.cons y finZeroElim)) := by - simp only [twoSlotEquiv, Representation.Equiv.trans_apply, - Representation.Equiv.tensorCongr_tmul, oneSlotEquiv_apply, tmulEquiv_pure, reindex_pure] + simp only [twoIndexEquiv, Representation.Equiv.trans_apply, + Representation.Equiv.tensorCongr_tmul, oneIndexEquiv_apply, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) /-- Swapping the two slots of a tensor on two slots swaps the factors, `M^μν ↦ M^νμ`. -/ -lemma reindex_twoSlotEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : - S.reindex (ColorEquiv.swapTwo a b) (S.twoSlotEquiv a b M) = - S.twoSlotEquiv b a (TensorProduct.comm k _ _ M) := by +lemma reindex_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : + S.reindex (ColorEquiv.swapTwo a b) (S.twoIndexEquiv a b M) = + S.twoIndexEquiv b a (TensorProduct.comm k _ _ M) := by induction M using TensorProduct.inductionOn with | add x y hx hy => simp only [map_add, hx, hy] | tmul x y => - simp only [twoSlotEquiv_tmul, reindex_pure, TensorProduct.comm_tmul] + simp only [twoIndexEquiv_tmul, reindex_pure, TensorProduct.comm_tmul] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) /-! @@ -321,22 +321,22 @@ lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ Fintype.prod_sum_type]) _ /-- The components of the tensor of a vector `v` of `V c` are its components `v^μ`. -/ -lemma tensorBasis_repr_oneSlotEquiv (c : Color K) (v : S.V c) +lemma tensorBasis_repr_oneIndexEquiv (c : Color K) (v : S.V c) (μ : Π i, basisIdx (![c] i).kind) : - (S.tensorBasis ![c]).repr (S.oneSlotEquiv c v) μ = (S.basis c).repr v (μ 0) := by - rw [oneSlotEquiv_apply, tensorBasis_repr_pure, Fin.prod_univ_one] + (S.tensorBasis ![c]).repr (S.oneIndexEquiv c v) μ = (S.basis c).repr v (μ 0) := by + rw [oneIndexEquiv_apply, tensorBasis_repr_pure, Fin.prod_univ_one] rfl /-- The components of the tensor of `M` in `V a ⊗ V b` are the coordinates of `M` in Mathlib's product basis `Basis.tensorProduct`. -/ -lemma tensorBasis_repr_twoSlotEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) +lemma tensorBasis_repr_twoIndexEquiv (a b : Color K) (M : S.V a ⊗[k] S.V b) (y : Π j, basisIdx (![a, b] j).kind) : - (S.tensorBasis ![a, b]).repr (S.twoSlotEquiv a b M) y = + (S.tensorBasis ![a, b]).repr (S.twoIndexEquiv a b M) y = ((S.basis a).tensorProduct (S.basis b)).repr M (y 0, y 1) := by induction M using TensorProduct.inductionOn with | add M M' hM hM' => simp only [map_add, Finsupp.add_apply, hM, hM'] | tmul x x' => - rw [twoSlotEquiv_tmul, tensorBasis_repr_pure, Fin.prod_univ_two, mul_comm] + rw [twoIndexEquiv_tmul, tensorBasis_repr_pure, Fin.prod_univ_two, mul_comm] -- `y 0` is a label at `a` only after unfolding `![a, b] 0`, so `rw` and `simp` cannot use -- Mathlib's lemma, and `exact` checks it up to unfolding. exact ((Module.Basis.tensorProduct_repr_tmul_apply _ _ _ _ _ _).trans (smul_eq_mul _ _)).symm diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 784764f511..ee64e4ebb4 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -27,9 +27,9 @@ and `ι₂` may be different types, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the pro ## ii. Key results - `ColorEquiv` is a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. -- `Species.permSlots` moves the vectors of a tensor along a bijection of slots, with no colors +- `Species.permIndices` moves the vectors of a tensor along a bijection of slots, with no colors involved. It is Mathlib's `PiTensorProduct.reindex`. -- `Species.reindex` applies a relabeling to tensors, `permSlots` followed by `congrSlots` of the +- `Species.reindex` applies a relabeling to tensors, `permIndices` followed by `congrIndices` of the moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. - `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that relabeling respects composites, the identity and inverses, and `Species.reindex_reindex` is the first on a @@ -131,7 +131,7 @@ variable {ι₁ ι₂ ι₃ : Type} variable (S) in /-- The bijection `e` of slots applied to tensors, moving the vector of the old slot `i` to the new slot `e i`, with the colors `c ∘ e.symm`. It is Mathlib's `PiTensorProduct.reindex`. -/ -noncomputable def permSlots (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : +noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.reindex k (fun i => S.V (c i)) e ≪≫ₗ (S.toPiTensor (c ∘ e.symm)).symm) fun g f => by @@ -141,17 +141,17 @@ noncomputable def permSlots (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : /-- On a pure tensor, the new slot `j` holds the vector of the old slot `e.symm j`. -/ @[simp] -lemma permSlots_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, S.V (c i)) : - S.permSlots c e (S.pure f) = S.pure fun j => f (e.symm j) := by - simp [permSlots, PiTensorProduct.reindex_tprod] +lemma permIndices_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, S.V (c i)) : + S.permIndices c e (S.pure f) = S.pure fun j => f (e.symm j) := by + simp [permIndices, PiTensorProduct.reindex_tprod] variable (S) in /-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is -`permSlots` of `σ.toEquiv` followed by `congrSlots` of the moves. -/ +`permIndices` of `σ.toEquiv` followed by `congrIndices` of the moves. -/ noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : (S.tensorRep c₁).Equiv (S.tensorRep c₂) := - (S.permSlots c₁ σ.toEquiv).trans (S.congrSlots fun j => S.cast (σ.color_eq j)) + (S.permIndices c₁ σ.toEquiv).trans (S.congrIndices fun j => S.cast (σ.color_eq j)) /-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. -/ @@ -159,7 +159,7 @@ along `σ.color_eq j`. -/ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (f : Π i, S.V (c₁ i)) : S.reindex σ (S.pure f) = S.pure fun j => S.cast (σ.color_eq j) (f (σ.toEquiv.symm j)) := by - rw [reindex, Representation.Equiv.trans_apply, permSlots_pure, congrSlots_pure] + rw [reindex, Representation.Equiv.trans_apply, permIndices_pure, congrIndices_pure] /-! diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 5ab750f3f6..e5708c2d60 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -67,11 +67,11 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. -- `Species.mapSlots` applies a linear map at every slot, with `Species.mapSlots_comp` and - `Species.mapSlots_id`, and `Species.mapSlots_smul` proves that it commutes with the group when +- `Species.mapIndices` applies a linear map at every slot, with `Species.mapIndices_comp` and + `Species.mapIndices_id`, and `Species.mapIndices_smul` proves that it commutes with the group when every slot map does. -- `Species.congrSlots` applies an equivalence of representations at every slot. -- `Species.slotMapsAt` is the family of a linear map at one slot and the identity at the others, +- `Species.congrIndices` applies an equivalence of representations at every slot. +- `Species.indexMapsAt` is the family of a linear map at one slot and the identity at the others, and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and `Species.mapAt_id`. - `Species.congrAt` is `mapAt` of an equivalence of representations, as an equivalence of @@ -329,55 +329,55 @@ variable {c' : ι → Color K} variable (S) in /-- A linear map `f x` at every slot `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ -noncomputable def mapSlots (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := +noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap -/-- On a pure tensor, `mapSlots` applies `f x` to the vector at each slot `x`. -/ -lemma mapSlots_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : - S.mapSlots f (S.pure p) = S.pure fun x => f x (p x) := by - simp only [mapSlots, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, +/-- On a pure tensor, `mapIndices` applies `f x` to the vector at each slot `x`. -/ +lemma mapIndices_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : + S.mapIndices f (S.pure p) = S.pure fun x => f x (p x) := by + simp only [mapIndices, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, toPiTensor_pure, PiTensorProduct.map_tprod, toPiTensor_symm_tprod] /-- Two slotwise maps in sequence are the slotwise map of the composites. -/ -lemma mapSlots_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) +lemma mapIndices_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) : - S.mapSlots g ∘ₗ S.mapSlots f = S.mapSlots fun x => g x ∘ₗ f x := - ext_pure fun p => by simp only [LinearMap.comp_apply, mapSlots_pure] + S.mapIndices g ∘ₗ S.mapIndices f = S.mapIndices fun x => g x ∘ₗ f x := + ext_pure fun p => by simp only [LinearMap.comp_apply, mapIndices_pure] /-- Two slotwise maps in sequence are the slotwise map of the composites, on a tensor. -/ -lemma mapSlots_mapSlots {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) +lemma mapIndices_mapIndices {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : S.Tensor c) : - S.mapSlots g (S.mapSlots f t) = S.mapSlots (fun x => g x ∘ₗ f x) t := - LinearMap.congr_fun (mapSlots_comp f g) t + S.mapIndices g (S.mapIndices f t) = S.mapIndices (fun x => g x ∘ₗ f x) t := + LinearMap.congr_fun (mapIndices_comp f g) t /-- The slotwise map of the identities is the identity. -/ -lemma mapSlots_id : S.mapSlots (c := c) (fun _ => LinearMap.id) = LinearMap.id := - ext_pure fun p => mapSlots_pure _ p +lemma mapIndices_id : S.mapIndices (c := c) (fun _ => LinearMap.id) = LinearMap.id := + ext_pure fun p => mapIndices_pure _ p /-- The slotwise map of the identities is the identity, on a tensor. -/ -lemma mapSlots_id_apply (t : S.Tensor c) : S.mapSlots (fun _ => LinearMap.id) t = t := - LinearMap.congr_fun mapSlots_id t +lemma mapIndices_id_apply (t : S.Tensor c) : S.mapIndices (fun _ => LinearMap.id) t = t := + LinearMap.congr_fun mapIndices_id t /-- Linear maps that commute with the group action at every slot commute with it on tensors, `f (Λ T) = Λ f T`. -/ -lemma mapSlots_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} +lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : S.Tensor c) : - S.mapSlots f (g • t) = g • S.mapSlots f t := - map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapSlots_pure, hf]) t + S.mapIndices f (g • t) = g • S.mapIndices f t := + map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapIndices_pure, hf]) t variable (S) in /-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ -noncomputable def congrSlots (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : +noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : (S.tensorRep c).Equiv (S.tensorRep c') := equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => (f x).toLinearEquiv) ≪≫ₗ (S.toPiTensor c').symm) fun g p => - mapSlots_smul (fun x => (f x).toIntertwiningMap.isIntertwining _ _) g (S.pure p) + mapIndices_smul (fun x => (f x).toIntertwiningMap.isIntertwining _ _) g (S.pure p) -/-- On a pure tensor, `congrSlots` applies `f x` to the vector at each slot `x`. -/ -lemma congrSlots_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : - S.congrSlots f (S.pure p) = S.pure fun x => f x (p x) := - mapSlots_pure (fun x => (f x).toLinearMap) p +/-- On a pure tensor, `congrIndices` applies `f x` to the vector at each slot `x`. -/ +lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : + S.congrIndices f (S.pure p) = S.pure fun x => f x (p x) := + mapIndices_pure (fun x => (f x).toLinearMap) p section @@ -386,32 +386,32 @@ variable [DecidableEq ι] variable (S) in /-- The family of linear maps with `f` at the slot `i` and the identity, moved along `h x`, at every other slot `x`. `mapAt` applies it slot by slot. -/ -noncomputable def slotMapsAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) +noncomputable def indexMapsAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : Π x, S.V (c x) →ₗ[k] S.V (c' x) := Function.update (fun x => if hx : x = i then 0 else (S.cast (h x hx)).toLinearMap) i f @[simp] -lemma slotMapsAt_apply_self (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) - (h : ∀ x, x ≠ i → c x = c' x) : S.slotMapsAt i f h i = f := +lemma indexMapsAt_apply_self (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) + (h : ∀ x, x ≠ i → c x = c' x) : S.indexMapsAt i f h i = f := Function.update_self _ _ _ @[simp] -lemma slotMapsAt_apply_ne (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) +lemma indexMapsAt_apply_ne (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) {x : ι} (hx : x ≠ i) : - S.slotMapsAt i f h x = (S.cast (h x hx)).toLinearMap := by - rw [slotMapsAt, Function.update_of_ne hx, dite_eq_right hx] + S.indexMapsAt i f h x = (S.cast (h x hx)).toLinearMap := by + rw [indexMapsAt, Function.update_of_ne hx, dite_eq_right hx] /-- A family with `f` at the slot `i` and the moves along `h x` at the other slots is -`slotMapsAt i f h`. -/ -lemma eq_slotMapsAt {F : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (i : ι) +`indexMapsAt i f h`. -/ +lemma eq_indexMapsAt {F : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (hi : F i = f) - (hne : ∀ x (hx : x ≠ i), F x = (S.cast (h x hx)).toLinearMap) : F = S.slotMapsAt i f h := + (hne : ∀ x (hx : x ≠ i), F x = (S.cast (h x hx)).toLinearMap) : F = S.indexMapsAt i f h := Function.eq_update_iff.2 ⟨hi, fun x hx => by rw [hne x hx, dite_eq_right hx]⟩ -/-- `slotMapsAt i f h` commutes with the group action at every slot when `f` does. -/ -lemma slotMapsAt_rep (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) +/-- `indexMapsAt i f h` commutes with the group action at every slot when `f` does. -/ +lemma indexMapsAt_rep (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (hf : ∀ g v, f (S.rep (c i) g v) = S.rep (c' i) g (f v)) (x : ι) (g : G) (v : S.V (c x)) : - S.slotMapsAt i f h x (S.rep (c x) g v) = S.rep (c' x) g (S.slotMapsAt i f h x v) := by + S.indexMapsAt i f h x (S.rep (c x) g v) = S.rep (c' x) g (S.indexMapsAt i f h x v) := by by_cases hx : x = i · subst hx simpa using hf g v @@ -422,7 +422,7 @@ variable (S) in `x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := - S.mapSlots (S.slotMapsAt i f h) + S.mapIndices (S.indexMapsAt i f h) /-- On a pure tensor, `mapAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ @@ -430,12 +430,12 @@ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hqi : q i = f (p i)) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : S.mapAt i f h (S.pure p) = S.pure q := by - rw [mapAt, mapSlots_pure] + rw [mapAt, mapIndices_pure] refine congrArg S.pure (funext fun x => ?_) by_cases hx : x = i · subst hx - rw [slotMapsAt_apply_self, hqi] - · rw [slotMapsAt_apply_ne _ _ _ hx, hq x hx] + rw [indexMapsAt_apply_self, hqi] + · rw [indexMapsAt_apply_ne _ _ _ hx, hq x hx] rfl /-- `mapAt` of `f` and then of `g` at the same slot is `mapAt` of `g ∘ₗ f`. -/ @@ -443,15 +443,15 @@ lemma mapAt_comp {c'' : ι → Color K} (i : ι) (f : S.V (c i) →ₗ[k] S.V (c (g : S.V (c' i) →ₗ[k] S.V (c'' i)) (h : ∀ x, x ≠ i → c x = c' x) (h' : ∀ x, x ≠ i → c' x = c'' x) : S.mapAt i g h' ∘ₗ S.mapAt i f h = S.mapAt i (g ∘ₗ f) fun x hx => (h x hx).trans (h' x hx) := by - rw [mapAt, mapAt, mapAt, mapSlots_comp] - refine congrArg _ (eq_slotMapsAt i _ _ (by simp) fun x hx => LinearMap.ext fun v => ?_) + rw [mapAt, mapAt, mapAt, mapIndices_comp] + refine congrArg _ (eq_indexMapsAt i _ _ (by simp) fun x hx => LinearMap.ext fun v => ?_) simp [hx] /-- `mapAt` of the identity is the identity. -/ lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : S.mapAt i LinearMap.id h = LinearMap.id := by - rw [mapAt, ← eq_slotMapsAt (F := fun _ => LinearMap.id) i _ h rfl - fun _ _ => LinearMap.ext fun _ => rfl, mapSlots_id] + rw [mapAt, ← eq_indexMapsAt (F := fun _ => LinearMap.id) i _ h rfl + fun _ _ => LinearMap.ext fun _ => rfl, mapIndices_id] /-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. The labels `ψ` of `t` away from `i` are the labels `φ` of the result moved along `h`. -/ @@ -469,8 +469,8 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V simp only [map_add, Finsupp.add_apply, ht, ht', mul_add, Finset.sum_add_distrib] | h p => -- Split off the slot `i`, where `f` acts, from the slots where the label is only moved. - rw [mapAt, mapSlots_pure, tensorBasis_repr_pure, - ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), slotMapsAt_apply_self, + rw [mapAt, mapIndices_pure, tensorBasis_repr_pure, + ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), indexMapsAt_apply_self, ← LinearMap.toMatrix_mulVec_repr (S.basis (c i)) (S.basis (c' i)), Matrix.mulVec, dotProduct, Finset.sum_mul] refine Finset.sum_congr rfl fun μ _ => ?_ @@ -478,7 +478,7 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V Function.update_self, mul_assoc] refine congrArg _ (congrArg _ (Finset.prod_congr rfl fun x hx => ?_)) have hx := Finset.ne_of_mem_erase hx - rw [slotMapsAt_apply_ne _ _ _ hx, Function.update_of_ne hx, hψ x hx] + rw [indexMapsAt_apply_ne _ _ _ hx, Function.update_of_ne hx, hψ x hx] exact basis_repr_cast _ _ _ variable (S) in @@ -493,7 +493,7 @@ noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) LinearMap.ext f.apply_symm_apply, mapAt_id]) (by rw [mapAt_comp, show f.symm.toLinearMap ∘ₗ f.toLinearMap = LinearMap.id from LinearMap.ext f.symm_apply_apply, mapAt_id])) fun g p => - mapSlots_smul (slotMapsAt_rep i _ h (f.toIntertwiningMap.isIntertwining _ _)) g (S.pure p) + mapIndices_smul (indexMapsAt_rep i _ h (f.toIntertwiningMap.isIntertwining _ _)) g (S.pure p) /-- `congrAt` is `mapAt` of the underlying linear map. -/ lemma congrAt_eq_mapAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index c561a595b1..9057ff6bfd 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -94,7 +94,7 @@ variable (S) in `S.rep c`. A type that is such a space, such as a Weyl spinor, gets its instance from this. -/ noncomputable abbrev ofIndexSpace (c : Color K) : Tensorial S ![c] (S.V c) where rep := S.rep c - toTensor := S.oneSlotEquiv c + toTensor := S.oneIndexEquiv c /-- The components of the tensor of a vector `v` of the space of one index are its components `v^μ`. -/ @@ -102,7 +102,7 @@ lemma tensorBasis_repr_toTensor_ofIndexSpace (c : Color K) (v : S.V c) (μ : Π i, basisIdx (![c] i).kind) : (S.tensorBasis ![c]).repr (toTensor (self := ofIndexSpace S c) v) μ = (S.basis c).repr v (μ 0) := - Species.tensorBasis_repr_oneSlotEquiv c v μ + Species.tensorBasis_repr_oneIndexEquiv c v μ /-- A type `M` with an action `r` and an equivalence `φ` to a tensorial type `N` commuting with the actions is tensorial with the colors of `N`. -/ diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 0d1242a1e3..39771976da 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -210,7 +210,7 @@ lemma vectorToBispinor_bispinorToVector (X : ℂT[upL, upR, upL, upR]) : /-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ lemma pauliTensor_raise_lower : {{pauliTensor | μ τ(α) τ(β)}ᵀ | μ τ(α) τ(β)}ᵀ = pauliTensor := by - tensor_slots + tensor_indices /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is `vectorToBispinor σT = vectorToBispinor (bispinorToVector (εL ⊗ εR)) / 2`. -/ diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 0b0db3ec56..2f7c4c0c15 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -269,7 +269,7 @@ lemma formMatrix_apply (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : /-- The components of the metric are the Minkowski matrix `diag(1, -1, …, -1)`: at the labels `![α, β]` they are `η^αβ` at an upper color and `η_αβ` at a lower one. The labels are any -`μ : Fin 2 → Fin 1 ⊕ Fin d`, as in `Species.tensorBasis_repr_twoSlotEquiv`. -/ +`μ : Fin 2 → Fin 1 ⊕ Fin d`, as in `Species.tensorBasis_repr_twoIndexEquiv`. -/ lemma tensorBasis_repr_formTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis ![c, c]).repr ((realLorentz d).formTensor c) μ = minkowskiMatrix (μ 0) (μ 1) := by @@ -365,12 +365,12 @@ lemma tensorBasis_repr_toTensor_coVector (d : ℕ) (ω : CoVector d) (μ : Fin 1 /-- The tensor of the basis vector `e_μ` is the basis tensor with label `μ`. -/ lemma toTensor_basis_vector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : Tensorial.toTensor (Vector.basis μ) = (realLorentz d).tensorBasis ![up] fun _ => μ := - Species.oneSlotEquiv_basis (S := realLorentz d) up fun _ => μ + Species.oneIndexEquiv_basis (S := realLorentz d) up fun _ => μ /-- The tensor of the basis covector `e^μ` is the basis tensor with label `μ`. -/ lemma toTensor_basis_coVector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : Tensorial.toTensor (CoVector.basis μ) = (realLorentz d).tensorBasis ![down] fun _ => μ := - Species.oneSlotEquiv_basis (S := realLorentz d) down fun _ => μ + Species.oneIndexEquiv_basis (S := realLorentz d) down fun _ => μ /-- The components `F^μν` of the tensor of `F` in `Vector d ⊗ Vector d` are its coordinates in Mathlib's product basis `Basis.tensorProduct`. -/ From 354a622fb5d1abb5ca1616278bb6ae24fe4c48c6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:34:16 +0000 Subject: [PATCH 125/242] doc(Tensors): index for slot in the prose of the tensor library MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings, module docs, comments and error messages say index for a position of a tensor such as the two of T^μ_ν, and label for the values an index runs over. Paragraphs touched are rewrapped to 100 columns. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 12 +-- Physlib/Mathematics/Tensors/Conjugation.lean | 14 +-- Physlib/Mathematics/Tensors/Contraction.lean | 58 +++++------ Physlib/Mathematics/Tensors/Elab.lean | 98 +++++++++---------- Physlib/Mathematics/Tensors/ElabExamples.lean | 24 ++--- Physlib/Mathematics/Tensors/ElabTactics.lean | 92 ++++++++--------- Physlib/Mathematics/Tensors/Inverse.lean | 8 +- Physlib/Mathematics/Tensors/LinearMap.lean | 32 +++--- Physlib/Mathematics/Tensors/Metric.lean | 26 ++--- Physlib/Mathematics/Tensors/Product.lean | 54 +++++----- Physlib/Mathematics/Tensors/Reindexing.lean | 48 ++++----- Physlib/Mathematics/Tensors/Species.lean | 10 +- Physlib/Mathematics/Tensors/Tensor.lean | 78 +++++++-------- Physlib/Mathematics/Tensors/Tensorial.lean | 8 +- .../Tensors/ComplexTensor/Species.lean | 6 +- .../Tensors/RealTensor/Species.lean | 6 +- 16 files changed, 287 insertions(+), 287 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index d51eecead3..894d5a3e76 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -8,7 +8,7 @@ module public import Mathlib.Init /-! -# Colors of tensor slots +# Colors of tensor indices A color gives the kind of an index of a tensor and whether the index is up or down. @@ -19,13 +19,13 @@ tensors there is one kind, `vec`. The vector index of `v^μ` has color `⟨vec, covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors have three kinds, one for each of the two types of Weyl spinor index and one for the vector index. -A tensor has one color per slot. A species (`Species.lean`) gives each color a space, and a -tensor is an element of the tensor product of the spaces of its slots. Slots are labelled by -colors rather than by spaces, so that checking that two slots can be contracted is an equality of -labels, decidable, not an equality of types. +A tensor has one color per index. A species (`Species.lean`) gives each color a space, and a +tensor is an element of the tensor product of the spaces of its indices. Indices carry colors +rather than spaces, so that checking that two indices can be contracted is an equality of colors, +decidable, not an equality of types. The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, the same kind with the other variance. A contraction such -as `v^μ w_μ` pairs a slot of color `c` with a slot of color `c.dual`. `Color K` is a structure +as `v^μ w_μ` pairs an index of color `c` with an index of color `c.dual`. `Color K` is a structure rather than the product `K × Variance`, so that `c.dual` works as dot notation. ## ii. Key results diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 29d36281c0..302e59f560 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -28,7 +28,7 @@ property `Species.IsEquivariantConjugation`. Most species conjugate componentwise, `(v̄)^μ = (v^μ)*`, and get their conjugation from `Species.HasConjugation.ofBasis` and a map `ConjLabels` from the labels of each kind to those of -its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every slot. +its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every index. ## ii. Key results @@ -258,9 +258,9 @@ namespace Tensor variable [S.HasConjugation] {ι : Type} {c c' : ι → Color K} -/-- The conjugate `T̄` of a tensor `T`, with every slot conjugated and moved to the conjugate color. -The colors `c'` of the result are the conjugates of `c` by `h`. For concrete colors `h` holds by -`rfl` slot by slot, `fun i => by fin_cases i <;> rfl`. It is conjugate-linear. -/ +/-- The conjugate `T̄` of a tensor `T`, with every index conjugated and moved to the conjugate +color. The colors `c'` of the result are the conjugates of `c` by `h`. For concrete colors `h` holds +by `rfl` index by index, `fun i => by fin_cases i <;> rfl`. It is conjugate-linear. -/ noncomputable def conj (h : ∀ i, c' i = (c i).bar) : S.Tensor c ≃ₗ⋆[k] S.Tensor c' := (S.toPiTensor c).trans <| (PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i)).trans (S.toPiTensor c').symm @@ -293,12 +293,12 @@ lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : variable {κ J : Type} [Fintype J] {b : κ → Color K} {a : J → Color K} -/-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of slots. -/ +/-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of indices. -/ def _root_.Tensor.ColorEquiv.barContr {c : ι → Color K} (σ : ColorEquiv c (contrColors b a)) : ColorEquiv (fun i => (c i).bar) (contrColors (fun x => (b x).bar) fun j => (a j).bar) := ⟨σ.toEquiv, fun j => by rcases j with _ | _ | _ <;> exact congrArg Color.bar (σ.color_eq _)⟩ -/-- Conjugating a contraction is contracting the conjugate along the same pairs of slots, +/-- Conjugating a contraction is contracting the conjugate along the same pairs of indices, `(T^ρμ_μ)‾ = T̄^ρμ_μ`. -/ lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : conj (fun _ => rfl) (contr σ t) = contr σ.barContr (conj (fun _ => rfl) t) := by @@ -317,7 +317,7 @@ lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : -/ /-- The components of the conjugate are the conjugates of the components, -`(T̄)^μν = (T^{e⁻¹ μ, e⁻¹ ν})*`, when conjugation sends each basis vector `e_μ` at the slot `i` to +`(T̄)^μν = (T^{e⁻¹ μ, e⁻¹ ν})*`, when conjugation sends each basis vector `e_μ` at the index `i` to the basis vector `e_{e i μ}`. -/ lemma tensorBasis_repr_conj_of_basis [Fintype ι] (h : ∀ i, c' i = (c i).bar) (e : ∀ i, basisIdx (c i).kind ≃ basisIdx (c' i).kind) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 7e1c77b4f8..b957ea3cfe 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -17,20 +17,20 @@ The contraction of pairs of indices, an index with its dual in each pair, as in ## i. Overview A contraction pairs indices of dual colors and sums over their common label, as in the trace -`(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the slots `Fin 3`. On a pure tensor +`(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the indices `Fin 3`. On a pure tensor `u ⊗ v ⊗ α` the trace is `(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. The pairs to contract are chosen by a relabeling `σ : ColorEquiv c (contrColors c' a)` of the -slots. It sends each free index to a slot `inl x`, of color `c' x`, and the two indices of a pair -`j` to the slots `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the trace, -`σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`, and the index notation +indices. It sends each free index to an index `inl x`, of color `c' x`, and the two indices of a +pair `j` to the indices `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the +trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`, and the index notation `{T | ρ μ μ}ᵀ` builds it from the repeated name. Since `σ` keeps colors, the two indices of each pair are dual without a separate hypothesis. `Tensor.contr σ` relabels by `σ` and then contracts each pair by `S.contr (a j)`, with the first index of the pair as its first argument. Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the -product `T ⊗ U` has four slots, which form two pairs and leave no slot, `κ = Empty`. The following -properties let a contraction of several tensors be computed one step at a time. +product `T ⊗ U` has four indices, which form two pairs and leave no index, `κ = Empty`. The +following properties let a contraction of several tensors be computed one step at a time. - Only the pairs of indices matter. Two contractions of the same pairs of indices are equal, in whatever order the pairs are listed and whichever index of a pair comes first. - A tensor that changes sign when its pairs are permuted contracts to zero, since its contraction @@ -51,9 +51,9 @@ since the bases of a color and its dual are dual. - `Species.Tensor.contr_pure` gives the contraction of a pure tensor. - `Species.Tensor.contr` is an intertwining map, so it commutes with the group action. - `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a relabeling of the - slots before or after a contraction. -- `ColorEquiv.SamePair` says that two choices of pairs contract the same two slots, and - `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of slots, + indices before or after a contraction. +- `ColorEquiv.SamePair` says that two choices of pairs contract the same two indices, and + `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, in any order, are equal. - `Species.Tensor.contr_noPairs` proves that a contraction with no pairs, along `ColorEquiv.noPairs`, is the identity. @@ -66,15 +66,15 @@ since the bases of a color and its dual are dual. contracting a factor commutes with the product, along `ColorEquiv.tmulRight` and `ColorEquiv.tmulLeft`. - `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction, from - `Species.Tensor.tensorBasis_repr_contrStd` in the standard arrangement of slots. + `Species.Tensor.tensorBasis_repr_contrStd` in the standard arrangement of indices. - `Species.Tensor.tensorBasis_repr_contr_one_pair` and `Species.Tensor.tensorBasis_repr_contr_two_pairs` give the same with one and with two pairs. ## iii. Table of contents -- A. Contraction in the standard arrangement of slots +- A. Contraction in the standard arrangement of indices - B. Contraction of chosen pairs of indices - - B.1. Relabeling the slots + - B.1. Relabeling the indices - B.2. Contractions of the same pairs of indices - B.3. Symmetric against antisymmetric - B.4. Two contractions are one @@ -104,20 +104,20 @@ namespace Tensor /-! -## A. Contraction in the standard arrangement of slots +## A. Contraction in the standard arrangement of indices -/ -/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of slots. The free index `inl x` has -the color `c x`, and the two slots `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors +/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of indices. The free index `inl x` has +the color `c x`, and the two indices `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors `a j` and `(a j).dual`. -/ noncomputable abbrev _root_.Tensor.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : κ ⊕ (J ⊕ J) → Color K := prodColors c (prodColors a fun j => (a j).dual) /-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the -slot `inr (inl j)` is contracted with the slot `inr (inr j)` by `S.contr (a j)`, with the slot -`inr (inl j)` as its first argument. The slots `inl x` are the free indices, with the colors +index `inr (inl j)` is contracted with the index `inr (inr j)` by `S.contr (a j)`, with the index +`inr (inl j)` as its first argument. The indices `inl x` are the free indices, with the colors `c`. It is the body of `Tensor.contr`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : (S.tensorRep (contrColors c a)).IntertwiningMap (S.tensorRep c) := @@ -152,9 +152,9 @@ lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) -/ /-- The contraction of the pairs of indices chosen by `σ`, as in `T^ρμ_ν ↦ T^ρμ_μ`. It is -`S.reindex σ` followed by `Tensor.contrStd c' a`. For each pair `j` the slot -`σ.toEquiv.symm (inr (inl j))`, of color `a j`, is contracted with the slot -`σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The slots `σ.toEquiv.symm (inl x)` are the +`S.reindex σ` followed by `Tensor.contrStd c' a`. For each pair `j` the index +`σ.toEquiv.symm (inr (inl j))`, of color `a j`, is contracted with the index +`σ.toEquiv.symm (inr (inr j))`, of color `(a j).dual`. The indices `σ.toEquiv.symm (inl x)` are the free indices, with the colors `c'`. -/ noncomputable def contr [Fintype J] {c : ι₁ → Color K} {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) : @@ -164,7 +164,7 @@ noncomputable def contr [Fintype J] {c : ι₁ → Color K} {c' : κ → Color K variable {c : ι₁ → Color K} {c' : κ → Color K} /-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the -two vectors of each pair. Each vector is moved along `σ.color_eq` to the color of its slot in the +two vectors of each pair. Each vector is moved along `σ.color_eq` to the color of its index in the standard arrangement. -/ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (f : Π x, S.V (c x)) : @@ -174,7 +174,7 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) -/-- The relabeling of a contraction with no pairs, where every slot is free and keeps its label. -/ +/-- The relabeling of a contraction with no pairs, where every index is free and stays in place. -/ def _root_.Tensor.ColorEquiv.noPairs (c : ι₁ → Color K) : ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where toEquiv.toFun := .inl @@ -192,11 +192,11 @@ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := /-! -### B.1. Relabeling the slots +### B.1. Relabeling the indices -/ -/-- Relabeling the slots and then contracting is contracting along the composite relabeling. -/ +/-- Relabeling the indices and then contracting is contracting along the composite relabeling. -/ lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : contr σ (S.reindex τ t) = contr (τ.trans σ) t := by @@ -220,7 +220,7 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → -/ -/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two slots, in either order. -/ +/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two indices, in either order. -/ def _root_.Tensor.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) (j' : J') : Prop := @@ -255,7 +255,7 @@ lemma _root_.Tensor.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : J simpa using hτ (h₁.symm.trans h₂) /-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair -of `τ` is a pair of `σ`. The slots of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ +of `τ` is a pair of `σ`. The indices of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ lemma _root_.Tensor.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' → Color K} {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) @@ -278,7 +278,7 @@ lemma _root_.Tensor.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' exact ⟨j, .inr h⟩ /-- Two contractions are equal when they keep the same free indices in the same order and contract -the same pairs of indices, in any order and with either index of a pair first. On literal slots +the same pairs of indices, in any order and with either index of a pair first. On literal indices the hypotheses are checked by evaluation. -/ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) @@ -303,7 +303,7 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' -/ -/-- A contraction of a tensor that changes sign under a relabeling `π` of its slots is zero, when +/-- A contraction of a tensor that changes sign under a relabeling `π` of its indices is zero, when `π` only permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the form of the index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} @@ -420,7 +420,7 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color -/ -/-- The components of the contraction in the standard arrangement of slots, +/-- The components of the contraction in the standard arrangement of indices, `(tr T)^ρ = ∑ μ, T^ρμ_μ`. The labels `μ` run over the pairs. -/ lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : κ → Color K) (a : J → Color K) (t : S.Tensor (contrColors c' a)) (φ : Π x, basisIdx (c' x).kind) : diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index fc97697769..e2aeba1151 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -16,7 +16,7 @@ Tensor expressions with named indices, such as `{T | μ ν ⊗ U | ν ρ}ᵀ` fo ## i. Overview A tensor `T` of a species `S` with colors `![c₀, …, cₙ₋₁]` is written `T | μ₀ … μₙ₋₁`, one name -per slot. The heights of the indices are not written, since the colors fix them. A real Lorentz +per index. The heights of the indices are not written, since the colors fix them. A real Lorentz tensor `T : S.Tensor ![⟨vec, up⟩, ⟨vec, down⟩]`, with components `T^μ_ν`, is written `T | μ ν`. An expression `{…}ᵀ` is a tensor in `S.Tensor ![…]`, or a proposition if it is an equation. The @@ -24,14 +24,14 @@ names are bound only inside the braces. Each construct is an operation of the sp - `A ⊗ B` is `S.tmulEquiv _ _ (A ⊗ₜ B)`. - A name written twice in a product is contracted. All pairs of the product are contracted in one - `Tensor.contr σ`, with the relabeling `σ` generated from the names. The two slots of a pair must + `Tensor.contr σ`, with the relabeling `σ` generated from the names. The two indices of a pair must have dual colors, and the first one written is the first argument of `S.contr`. - The free indices are numbered `0, 1, …` in the order written. The result is a tensor on `Fin m` with a literal color list `![…]`. - `A + B` and `A = B` relabel `B` by `S.reindex` when its names come in another order. Each name must have the same color on both sides. - `↓μ` lowers an upper index and `↑μ` raises a lower one, by `S.toDualAt`, so `{v | ↓ν}ᵀ` is - `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the slot, and a visible + `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the index, and a visible variance, as in `⟨κ, .up⟩`. - `τ(μ)` flips the variance of an index by `S.toDualAt` whichever way it points, so it needs no visible variance. @@ -45,16 +45,16 @@ Parentheses, each side of a sum and a nested `{…}ᵀ` are contracted before th product, so the names they contract are free again outside. Thus `{(T | μ μ) ⊗ U | μ ρ}ᵀ` is `(T^λ_λ) U^μ_ρ`. Within one product a name appears at most twice. -When the expected type is known, the result is fitted to it. On a tensor type with other slots, -such as `Fin 1 ⊕ Fin 1` for `Fin 2`, the free indices are placed on those slots in the order +When the expected type is known, the result is fitted to it. On a tensor type with another index +type, such as `Fin 1 ⊕ Fin 1` for `Fin 2`, the free indices are placed on its indices in the order written. On a type `M` with an instance `Tensorial S c M`, the result is read back by `Tensorial.toTensor.symm`. So `({v | ↓μ}ᵀ : CoVector d)` is the covector `v_μ`, and `({x | μ ⊗ y | ν}ᵀ : Vector d ⊗ Vector d)` is `x ⊗ₜ y`. -Every tensor needs literal slots and colors. Its slot type is built from `Fin n` and `⊕` with a +Every tensor needs literal indices and colors. Its index type is built from `Fin n` and `⊕` with a literal `n`, and its colors are a literal list `![a, b, …]` or `prodColors` of such lists. General lemmas over a variable color family are therefore stated without the notation. A tensor `s` with -no slots is written `s |` with a space after the bar, since Mathlib reads `|x|` as an absolute +no indices is written `s |` with a space after the bar, since Mathlib reads `|x|` as an absolute value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written @@ -70,7 +70,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written - A. Syntax - B. Elaboration - - B.1. Slot types + - B.1. Index types - B.2. The steps of an expression - B.3. The elaborator @@ -104,7 +104,7 @@ syntax "τ(" ident ")" : tensorIndex /-- A tensor expression in index notation. -/ declare_syntax_cat tensorIndexExpr -/-- A tensor with a name on each slot. -/ +/-- A tensor with a name on each index. -/ syntax term "|" (ppSpace tensorIndex)* : tensorIndexExpr /-- An equation. -/ syntax:40 tensorIndexExpr "=" tensorIndexExpr:41 : tensorIndexExpr @@ -122,12 +122,12 @@ syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term ## B. Elaboration -### B.1. Slot types +### B.1. Index types -/ -/-- A slot type built from `Fin n`, `Unit` and `⊕`. Its slots are numbered `0, 1, …` in order. -/ -inductive Indices where +/-- An index type built from `Fin n`, `Unit` and `⊕`. Its indices are numbered `0, 1, …` in order. +-/ inductive Indices where /-- `Fin n`. -/ | fin (n : Nat) /-- `Unit`. -/ @@ -136,34 +136,34 @@ inductive Indices where | sum (a b : Indices) deriving BEq -/-- The number of slots. -/ +/-- The number of indices. -/ def Indices.size : Indices → Nat | .fin n => n | .unit => 1 | .sum a b => a.size + b.size -/-- The slot type as a term. -/ +/-- The index type as a term. -/ def Indices.type : Indices → TermElabM Term | .fin n => `(Fin $(quote n)) | .unit => `(Unit) | .sum a b => do `($(← a.type) ⊕ $(← b.type)) -/-- The slot number `i` as a term. -/ +/-- The index `i` as a term. -/ def Indices.elem : Indices → Nat → TermElabM Term | .fin n, i => `(($(quote i) : Fin $(quote n))) | .unit, _ => `(()) | .sum a b, i => if i < a.size then do `(Sum.inl $(← a.elem i)) else do `(Sum.inr $(← b.elem (i - a.size))) -/-- The function on a slot type taking the values `vs` in slot order, built from vector literals on -`Fin n`, constants on `Unit` and `comb` on `⊕`. -/ +/-- The function on an index type taking the values `vs` in index order, built from vector literals +on `Fin n`, constants on `Unit` and `comb` on `⊕`. -/ def Indices.fn (comb : Name) : Indices → List Term → TermElabM Term | .fin _, vs => `(![$(vs.toArray),*]) | .unit, vs => `(fun _ : Unit => $(vs[0]!)) | .sum a b, vs => do `($(mkIdent comb) $(← a.fn comb (vs.take a.size)) $(← b.fn comb (vs.drop a.size))) -/-- The slot type of a type built from `Fin n` and `⊕` with a literal `n`. -/ +/-- The index type of a type built from `Fin n` and `⊕` with a literal `n`. -/ partial def Indices.ofType? (ι : Expr) : MetaM (Option Indices) := do let ι ← whnfR ι if ι.isAppOfArity ``Fin 1 then @@ -174,13 +174,13 @@ partial def Indices.ofType? (ι : Expr) : MetaM (Option Indices) := do return some (.sum a b) else return none -/-- The bijection from `dom` to `cod` sending the slot `pre[j]` of `dom` to the slot `j` of `cod`. -The list `pre` names every slot of `dom` once. Its inverse identities are proved slot by slot +/-- The bijection from `dom` to `cod` sending the index `pre[j]` of `dom` to the index `j` of `cod`. +The list `pre` names every index of `dom` once. Its inverse identities are proved index by index by `fin_cases`. -/ def indexEquiv (dom cod : Indices) (pre : List Nat) : TermElabM Term := do let toFun ← dom.fn ``Sum.elim (← (List.range dom.size).mapM fun i => cod.elem (pre.idxOf i)) let invFun ← cod.fn ``Sum.elim (← pre.mapM dom.elem) - -- `decide` would need a `DecidableEq` instance on the whole slot type, which exceeds the + -- `decide` would need a `DecidableEq` instance on the whole index type, which exceeds the -- instance size limit from four contracted pairs on. `((Equiv.mk $toFun $invFun (by intro x; fin_cases x <;> rfl) (by intro x; fin_cases x <;> rfl) : $(← dom.type) ≃ $(← cod.type))) @@ -196,8 +196,8 @@ partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do def colorsTerm (cs : List Expr) : TermElabM Term := do `(![$((← cs.toArray.mapM exprToSyntax)),*]) -/-- The slots, the color family `c` and its entries of a tensor type `S.Tensor c` with literal slots -and colors. -/ +/-- The index type, the color family `c` and its entries of a tensor type `S.Tensor c` with literal +indices and colors. -/ def literalTensorType? (ty : Expr) : MetaM (Option (Indices × Expr × List Expr)) := do let ty ← whnfR (← instantiateMVars ty) unless ty.isAppOf ``Tensor.Species.Tensor do return none @@ -213,39 +213,39 @@ def literalTensorType? (ty : Expr) : MetaM (Option (Indices × Expr × List Expr -/ -/-- A partly elaborated expression, with the name and color of each slot, its slot type, its tensor -and its species. -/ +/-- A partly elaborated expression, with the name and color of each index, its index type, its +tensor and its species. -/ structure Node where - /-- The index name of each slot. -/ + /-- The name of each index. -/ names : List Name - /-- The color of each slot. -/ + /-- The color of each index. -/ colors : List Expr - /-- The slot type. -/ + /-- The index type. -/ indices : Indices /-- The tensor. -/ tm : Term /-- The species. -/ species : Expr -/-- The tensor of `a` relabeled onto `Fin pre.length` with the colors `cs`, its slot `pre[j]` -becoming the slot `j`. -/ +/-- The tensor of `a` relabeled onto `Fin pre.length` with the colors `cs`, its index `pre[j]` +becoming the index `j`. -/ def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term := do `(Tensor.Species.reindex $(← exprToSyntax a.species) (Tensor.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) $(← indexEquiv a.indices (.fin pre.length) pre)) $a.tm) -/-- The expression relabeled to `Fin n`, its slots numbered in order. -/ +/-- The expression relabeled to `Fin n`, its indices numbered in order. -/ def Node.toFin (a : Node) : TermElabM Node := do let n := a.names.length if a.indices == .fin n then return a return { a with indices := .fin n, tm := ← a.reindex (List.range n) a.colors } -/-- The expression on `Fin n` with its slot `i` moved to the dual color by `S.toDualAt`. -/ +/-- The expression on `Fin n` with its index `i` moved to the dual color by `S.toDualAt`. -/ def Node.toDualAt (a : Node) (i : Nat) : TermElabM Node := do let colors := a.colors.set i (← mkAppM ``Tensor.Color.dual #[a.colors[i]!]) - -- The color `col` of the slot is passed explicitly, so the form is found at the kind of `col` and - -- every lowering at that kind uses the same instance. The proofs `c i = col` and + -- The color `col` of the index is passed explicitly, so the form is found at the kind of `col` + -- and every lowering at that kind uses the same instance. The proofs `c i = col` and -- `c' i = col.dual` are `id rfl`, since a bare `rfl` has the type `c' i = c' i`, which `rw` -- matches only after unfolding `![…] i`. Away from `i` the colors agree by cases. let tm ← `(Tensor.Species.toDualAt $(← exprToSyntax a.species) (c' := $(← colorsTerm colors)) @@ -272,7 +272,7 @@ def tensorOf (T : Term) : TermElabM (Expr × Expr) := do throwError "index notation: {T} is not a tensor and has no `Tensorial` instance:\n\ {ex.toMessageData}" -/-- The name of the index `i` on a slot of color `col`, and whether its variance is flipped, after +/-- The name of the written index `i`, of color `col`, and whether its variance is flipped, after checking that an arrow matches the variance of `col`. -/ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) := do let arrow (x : Ident) (up : Bool) : TermElabM (Name × Bool) := do @@ -288,23 +288,23 @@ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) | `(tensorIndex| τ($x:ident)) => pure (x.getId, true) | _ => throwError "index notation: unsupported index {i}" -/-- The tensor `T | i₁ i₂ …` with its slots named, lowered at each `↓μ`, raised at each `↑μ` +/-- The tensor `T | i₁ i₂ …` with its indices named, lowered at each `↓μ`, raised at each `↑μ` and flipped at each `τ(μ)`. -/ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do let (e, ty) ← tensorOf T let args := ty.getAppArgs let some indices ← Indices.ofType? args[args.size - 2]! - | throwError "index notation: the slots of {T} must be `Fin n` and `⊕`" + | throwError "index notation: the index type of {T} must be built from `Fin n` and `⊕`" let some colors ← colorList? args[args.size - 1]! | throwError "index notation: the colors of {T} must be a literal list" unless colors.length = idxs.size do - throwError "index notation: {T} has {colors.length} slots, {idxs.size} indices given" + throwError "index notation: {T} has {colors.length} indices, but {idxs.size} names are given" let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => indexName i col let a : Node := ⟨kinds.map (·.1), colors, indices, ← exprToSyntax e, args[args.size - 3]!⟩ if kinds.all (!·.2) then return a kinds.zipIdx.foldlM (init := ← a.toFin) fun a ((_, d), i) => if d then a.toDualAt i else pure a -/-- Contracts every repeated name in one `Tensor.contr`, and numbers the free slots in the order +/-- Contracts every repeated name in one `Tensor.contr`, and numbers the free indices in the order written. -/ def contract (a : Node) : TermElabM Node := do let idx := a.names.zipIdx @@ -325,8 +325,8 @@ def contract (a : Node) : TermElabM Node := do -- The pairs are labeled by `Unit` for one pair, as in `ColorEquiv.matMul`, and by -- `(… ⊕ Unit) ⊕ Unit` for more, as `ColorEquiv.chain` combines two contractions in sequence. let J := (List.range (pairs.length - 1)).foldl (fun J _ => Indices.sum J .unit) .unit - -- The free slots go to `inl k`, the first slot of the pair `j` to `inr (inl j)` and its second - -- slot to `inr (inr j)`. + -- The free indices go to `inl k`, the first index of the pair `j` to `inr (inl j)` and its second + -- index to `inr (inr j)`. let σ ← indexEquiv a.indices (.sum (.fin m) (.sum J J)) (free ++ pairs.map (·.1) ++ pairs.map (·.2)) let pairColors ← J.fn ``Tensor.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) @@ -380,27 +380,27 @@ partial def build (stx : Syntax) : TermElabM Node := do contract (← uncontract end -/-- The tensor of `e` on the slots and colors of the tensor type `target`, its free indices in the -order written. It is `none` when `target` is not a tensor type with literal slots and colors. -/ +/-- The tensor of `e` on the indices and colors of the tensor type `target`, its free indices in the +order written. It is `none` when `target` is not a tensor type with literal indices and colors. -/ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do let some (indices, c, cs) ← literalTensorType? target | return none let u ← uncontracted e - -- A product with no contraction is already on the slots of its factors. + -- A product with no contraction is already on the indices of its factors. let a ← if u.indices == indices && u.names.eraseDups.length == u.names.length then pure u else contract u unless cs.length == a.colors.length do - throwError "index notation: {a.colors.length} free indices, but {cs.length} slots expected" + throwError "index notation: {a.colors.length} free indices, but {cs.length} indices expected" for (col, i) in a.colors.zipIdx do unless ← isDefEq col cs[i]! do - throwError "index notation: the free index {a.names[i]!} has the color {col}, but its slot \ - is expected to have the color {cs[i]!}" + throwError "index notation: the free index {a.names[i]!} has the color {col}, but the \ + expected index has the color {cs[i]!}" if a.indices == indices then return a.tm `(Tensor.Species.reindex $(← exprToSyntax a.species) (Tensor.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) $(← indexEquiv a.indices indices (List.range indices.size))) $a.tm) -/-- The term of `e` for the expected type. It is relabeled onto the slots of a tensor type, and read -back by `Tensorial.toTensor.symm` for a `Tensorial` type. -/ +/-- The term of `e` for the expected type. It is relabeled onto the indices of a tensor type, and +read back by `Tensorial.toTensor.symm` for a `Tensorial` type. -/ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do let some ty := expectedType? | return (← build e).tm let ty ← instantiateMVars ty diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 7c0bfd200b..ccbe91318f 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -62,7 +62,7 @@ noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ product in the notation. -/ example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl -/-- An equation with a tensor `s` with no slots, `T^μ_μ = s`. The space after the bar keeps +/-- An equation with a tensor `s` with no indices, `T^μ_μ = s`. The space after the bar keeps Mathlib's absolute value `|x|` from reading it. -/ example (s : S.Tensor (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl @@ -121,7 +121,7 @@ example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ : #guard_msgs in example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ --- A pair of slots whose colors are not dual is rejected. +-- A pair of indices whose colors are not dual is rejected. /-- error: index notation: μ joins the colors a and a, which are not dual -/ #guard_msgs in #check {T | μ ν ⊗ T | μ ρ}ᵀ @@ -147,7 +147,7 @@ variable [S.HasForm κ] [S.HasForm c.kind] /-- Lowering and then raising an index gives the vector back, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ --- An arrow that does not match the variance of the slot is rejected. +-- An arrow that does not match the variance of the index is rejected. /-- error: index notation: ↑μ needs a visibly lower index -/ #guard_msgs in #check {v | ↑μ}ᵀ @@ -171,7 +171,7 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] /-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates -is `ColorEquiv.matMul`, checked slot by slot. -/ +is `ColorEquiv.matMul`, checked index by index. -/ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by rw [← contr_formTensor_formTensor] congr 2 @@ -208,11 +208,11 @@ open TensorProduct in example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] -/-- A tensor on `Fin 2` relabeled onto the slots `Fin 1 ⊕ Fin 1` of a product. -/ +/-- A tensor on `Fin 2` relabeled onto the indices `Fin 1 ⊕ Fin 1` of a product. -/ noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ -/-- error: index notation: the free index μ has the color a, but its slot is expected to have the -color a.dual -/ +/-- error: index notation: the free index μ has the color a, but the expected index has the color +a.dual -/ #guard_msgs in example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ @@ -262,11 +262,11 @@ tensor, read as a contraction with no pairs. -/ example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν}ᵀ := by tensor_regroup -/-- A delta contracted at its second slot must be inserted at a slot of the dual color. -/ +/-- A delta contracted at its second index must be inserted at an index of the dual color. -/ example : {T | μ ν = T | μ ρ ⊗ S.deltaTensor a | ν ρ}ᵀ := by tensor_regroup -/-- Matching a delta at its second slot also works when the delta is on the left. -/ +/-- Matching a delta at its second index also works when the delta is on the left. -/ example : {T | μ ρ ⊗ S.deltaTensor a | ν ρ = T | μ ν}ᵀ := by tensor_regroup @@ -291,8 +291,8 @@ has no equal factor on the right -/ example : {T | μ ν ⊗ U | ν ρ = W | ν ρ ⊗ T | μ ν}ᵀ := by tensor_regroup --- The same factors contracted along other pairs of slots, `T U W ≠ T W U`, are rejected. -/-- error: tensor_regroup: the two sides contract different pairs of slots, for each way of +-- The same factors contracted along other pairs of indices, `T U W ≠ T W U`, are rejected. +/-- error: tensor_regroup: the two sides contract different pairs of indices, for each way of matching their equal factors -/ #guard_msgs in example : {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ = T | μ ν ⊗ W | ν ρ ⊗ U | ρ σ}ᵀ := by @@ -470,7 +470,7 @@ example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by tensor_indices -/-- Lowerings at two slots commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ +/-- Lowerings at two indices commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by tensor_indices diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index f278279465..5d5ba900b6 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -61,7 +61,7 @@ dsimproc_decl reduceCast (cast _ _) := fun e => do /-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, using the component lemmas `lᵢ` as well. It unfolds products, relabelings, sums, negatives, zeros, scalar multiples and contractions of one or two pairs. For more pairs, pass -`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal slot that +`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal index that `tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a goal that is already a component the first step is skipped. -/ syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.simpArg,* "]" : @@ -70,7 +70,7 @@ syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.si macro_rules | `(tactic| tensor_components [$ls,*]) => `(tactic| focus try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) - -- `Function.update_of_ne` proves `j ≠ i` for literal slots by `ne_eq` and `Fin.reduceEq`. + -- `Function.update_of_ne` proves `j ≠ i` for literal indices by `ne_eq` and `Fin.reduceEq`. simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_one_pair, Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, @@ -288,7 +288,7 @@ private def maxMatchings : Nat := 12 /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same factors. It orders the factors of the right side as on the left, and checks by evaluation that both -sides keep the same slots and contract the same pairs of slots +sides keep the same indices and contract the same pairs of indices (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, at most `maxMatchings` of them. -/ private def matchFactors : TacticM Unit := do @@ -309,13 +309,13 @@ private def matchFactors : TacticM Unit := do m!"the first {maxMatchings} of the {targets.length} ways" firstSuccess (targets.take maxMatchings) m!"" fun target => do arrange target - -- The slots are a nested sum, whose `DecidableEq` instance exceeds the default size. The + -- The indices are a nested sum, whose `DecidableEq` instance exceeds the default size. The -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free - -- color variables of the goal although the bijections of slots do not depend on them. + -- color variables of the goal although the bijections of indices do not depend on them. try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in exact Tensor.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) (of_decide_eq_true rfl) _)) - catch _ => throwError "tensor_regroup: the two sides contract different pairs of slots, for \ + catch _ => throwError "tensor_regroup: the two sides contract different pairs of indices, for \ {tried} of matching their equal factors" /-! @@ -346,25 +346,25 @@ private def formArgs? (x : Expr) : Option (Expr × Expr) := some (args[args.size - 3]!, args[args.size - 2]!) else none -/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read slot by slot. The slots of -`P` are numbered in order, the slots of each factor after those of the factors before it. -/ +/-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read index by index. The indices +of `P` are numbered in order, the indices of each factor after those of the factors before it. -/ private structure ReadContraction where /-- The factors of `P`, from left to right. -/ factors : List Expr - /-- The slot of `P` at which each factor starts. -/ + /-- The index of `P` at which each factor starts. -/ offsets : List Nat - /-- The slot of `P` of each free index, in order. -/ + /-- The index of `P` of each free index, in order. -/ free : List Nat - /-- The two slots of `P` of each pair, the first argument of `S.contr` first. -/ + /-- The two indices of `P` of each pair, the first argument of `S.contr` first. -/ pairs : List (Nat × Nat) -/-- The elements of a slot type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in -slot order. -/ +/-- The elements of an index type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in +index order. -/ private partial def indexElems (ι : Expr) : MetaM (List Expr) := do let ι ← whnfR ι if ι.isAppOfArity ``Fin 1 then let some n ← (evalNat ι.appArg!).run - | throwError "tensor_regroup: the slot type {ι} is not literal" + | throwError "tensor_regroup: the index type {ι} is not literal" (List.range n).mapM fun i => do mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] @@ -373,33 +373,33 @@ private partial def indexElems (ι : Expr) : MetaM (List Expr) := do let (a, b) := (ι.getArg! 0, ι.getArg! 1) return (← (← indexElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ (← (← indexElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) - else throwError "tensor_regroup: the slot type {ι} is not built from `Fin n` and `⊕`" + else throwError "tensor_regroup: the index type {ι} is not built from `Fin n` and `⊕`" -/-- The number of the slot `x` of the slot type `slots`, after evaluating `x`. -/ +/-- The number of the index `x` of the index type `indices`, after evaluating `x`. -/ private partial def indexNumber : Indices → Expr → MetaM Nat | .fin _, x => do let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run - | throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + | throwError "tensor_regroup: internal error, the index {x} does not evaluate" return i | .unit, _ => return 0 | .sum a b, x => do let x ← whnfD x if x.isAppOfArity ``Sum.inl 3 then indexNumber a x.appArg! else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← indexNumber b x.appArg!) - else throwError "tensor_regroup: internal error, the slot {x} does not evaluate" + else throwError "tensor_regroup: internal error, the index {x} does not evaluate" /-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each -free index and at each slot of each pair. -/ +free index and at each index of each pair. -/ private def readContraction (e : Expr) : MetaM ReadContraction := do let some P := bundledArg? ``Tensor.Species.Tensor.contr e | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" let τ := e.consumeMData.appFn!.appArg!.appArg! let some (indices, _, _) ← literalTensorType? (← inferType P) - | throwError "tensor_regroup: the slots or colors of a product are not literal" + | throwError "tensor_regroup: the indices or colors of a product are not literal" let fs := factors P let sizes ← fs.mapM fun f => do let some (s, _, _) ← literalTensorType? (← inferType f) - | throwError "tensor_regroup: the slots or colors of{indentExpr f}\nare not literal" + | throwError "tensor_regroup: the indices or colors of{indentExpr f}\nare not literal" return s.size -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. let args := (← whnfR (← inferType τ)).getAppArgs @@ -417,8 +417,8 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do return ⟨fs, sizes.scanl (· + ·) 0, free, pairs⟩ /-- The ways to insert a delta against the side `e` of the goal, a contraction: `false` for -`T^…μ… δ_μ^ν` and `true` for `δ^ν_μ T^…μ…`. A form or a delta of `e` whose first slot is contracted -with another factor cancels against a delta inserted the first way, and one whose second slot is, +`T^…μ… δ_μ^ν` and `true` for `δ^ν_μ T^…μ…`. A form or a delta of `e` whose first index is contracted +with another factor cancels against a delta inserted the first way, and one whose second index is, the second way. Both are returned when no form or delta is contracted with another factor. -/ private def deltaOrientations (e : Expr) : MetaM (List Bool) := do let rc ← readContraction e @@ -432,7 +432,7 @@ private def deltaOrientations (e : Expr) : MetaM (List Bool) := do let found := (rc.pairs.filterMap fun (s, t) => (side s t).orElse fun _ => side t s).eraseDups return if found.isEmpty then [false, true] else found -/-- Inserts a delta of color `a` at the slot `slot` of the product of the left side of the goal, +/-- Inserts a delta of color `a` at the index `index` of the product of the left side of the goal, if `lhs`, or of the right side, by `Species.Tensor.contr_deltaTensor`. With `second`, uses `Species.Tensor.contr_deltaTensor_swap` instead, inserting a delta of color `a.dual`. -/ private def insertDelta (lhs : Bool) (a : Expr) (indices : Indices) (index : Nat) (second : Bool) : @@ -447,34 +447,34 @@ private def insertDelta (lhs : Bool) (a : Expr) (indices : Indices) (index : Nat mutual -/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, trying slots of +/-- Inserts a delta of color `a` on the left side, if `lhs`, or on the right side, trying indices of color `a` for its first index and `a.dual` for its second index, and proves the goal by -`regroupContractions`. With `skipForms`, the slots of forms are left out when another factor has a -slot: inserting the delta anywhere along a chain of contracted forms gives the same factors, so -trying each of their slots only repeats the search. -/ +`regroupContractions`. With `skipForms`, the indices of forms are left out when another factor has +an index: inserting the delta anywhere along a chain of contracted forms gives the same factors, so +trying each of their indices only repeats the search. -/ private partial def insertDeltaAnywhere (lhs : Bool) (a : Expr) (skipForms : Bool := false) : TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q let some (indices, _, colors) ← literalTensorType? (← inferType P) - | throwError "tensor_regroup: the slots or colors of a product are not literal" - -- Whether each slot belongs to a form, from the number of slots of each factor in turn. + | throwError "tensor_regroup: the indices or colors of a product are not literal" + -- Whether each index belongs to a form, from the number of indices of each factor in turn. let inForm ← (factors P).flatMapM fun f => do let n := ((← literalTensorType? (← inferType f)).map (·.2.2.length)).getD 0 return List.replicate n (skipForms && (formArgs? f).isSome) -- shortcut: misses a chain of forms that reaches no other factor, as `T^μ_ν g^αβ` against - -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the slots of forms after the others would cover it. + -- `T^μ_ν g^αγ g_γδ g^δβ`; trying the indices of forms after the others would cover it. let inForm := if inForm.all id then inForm.map fun _ => false else inForm let (_, l, r) ← goalEq let orientations ← deltaOrientations (if lhs then r else l) let tries ← orientations.flatMapM fun second => do - -- The swapped identity inserts `deltaTensor b.dual` at a slot of color `b`. - -- To insert the requested `deltaTensor a`, that slot must therefore have color `a.dual`. + -- The swapped identity inserts `deltaTensor b.dual` at an index of color `b`. + -- To insert the requested `deltaTensor a`, that index must therefore have color `a.dual`. let b ← if second then mkAppM ``Tensor.Color.dual #[a] else pure a let indicesOfColor ← colors.zipIdx.filterM fun (col, i) => return !inForm.getD i false && (← isDefEqGoal col b) return indicesOfColor.map fun (_, i) => (i, second, b) - firstSuccess tries m!"tensor_regroup: no slot has the color {a} of a delta" + firstSuccess tries m!"tensor_regroup: no index has the color {a} of a delta" fun (index, second, b) => do insertDelta lhs b indices index second regroupContractions @@ -496,8 +496,8 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni match ds with | [d] => unless (deltaColor? d).isSome do throwError "tensor_regroup: {d} is not a delta" - -- The delta is `g^μν g_νρ` read with its slots in order, when the slot of the delta and the - -- slot of the forms contracted with other factors agree, or swapped. + -- The delta is `g^μν g_νρ` read with its indices in order, when the index of the delta and the + -- index of the forms contracted with other factors agree, or swapped. let (_, l, r) ← goalEq let swaps := match ← deltaOrientations (if lhs then l else r), ← deltaOrientations (if lhs then r else l) with @@ -518,7 +518,7 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested product. Two forms on one side with a delta or nothing left over on the other are matched by `matchFormPair`. A delta on one side with no counterpart on the other is inserted on the other side -at each slot of its color in turn, since contracting with a delta is a relabeling. The factors are +at each index of its color in turn, since contracting with a delta is a relabeling. The factors are then matched by `matchFactors`. -/ private partial def regroupContractions : TacticM Unit := do flattenContractions @@ -718,10 +718,10 @@ private def regroupWithLowerings : TacticM Unit := do /-- `tensor_indices` proves an equation of two chains of lowerings and raisings of the same tensor, such as `{{T | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = T` or `{{T | ↓μ ν}ᵀ | μ ↓ν = {T | μ ↓ν}ᵀ | ↓μ ν}ᵀ`. Each chain -is one slotwise map (`Species.toDualAt_eq_mapAt`, `Species.mapIndices_mapIndices`), and the two maps -are compared slot by slot, where `Species.toDual_toDual` cancels a lowering with a raising. A side -with no lowering or raising is the slotwise identity (`Species.mapIndices_id`). The slots must be -`Fin n` with a literal `n`. -/ +is one index-wise map (`Species.toDualAt_eq_mapAt`, `Species.mapIndices_mapIndices`), and the two +maps are compared index by index, where `Species.toDual_toDual` cancels a lowering with a raising. A +side with no lowering or raising is the index-wise identity (`Species.mapIndices_id`). The indices +must be `Fin n` with a literal `n`. -/ elab "tensor_indices" : tactic => do evalTactic (← `(tactic| try simp only [Tensor.Species.toDualAt_eq_mapAt, Tensor.Species.mapAt, Tensor.Species.mapIndices_mapIndices])) @@ -749,9 +749,9 @@ elab "tensor_indices" : tactic => do -/ -/-- The ways of sending the slots of `A` to slots of `G`, factor by factor, each factor of `A` to an -equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list of -images of the slots of `A`, together with the factors of `G` that are hit. -/ +/-- The ways of sending the indices of `A` to indices of `G`, factor by factor, each factor of `A` +to an equal factor of `G`, so that every pair of `A` is sent to a pair of `G`. Each way is the list +of images of the indices of `A`, together with the factors of `G` that are hit. -/ private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List Nat)) := do let candidates ← A.factors.mapM fun x => (List.range G.factors.length).filterM fun j => isEqFactor x G.factors[j]! @@ -763,7 +763,7 @@ private def embeddings (A G : ReadContraction) : MetaM (List (List Nat × List N /-- The function `x ↦ {x | … ⊗ g | … ⊗ …}ᵀ`, for `x` of the type of `A`, with its free indices in the order of `L`, the contraction read as `G`. The factors `g` of `G` not hit by `target` follow -`x`, and every slot of `G` is named after its free index `fₖ` or its pair `pⱼ`. The slots of `x` +`x`, and every index of `G` is named after its free index `fₖ` or its pair `pⱼ`. The indices of `x` are named as the free indices of `A` are sent by `φ`. -/ private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : List Nat) : TermElabM Expr := do @@ -869,7 +869,7 @@ number of attempts is the product of `k!` over the factors that appear `k` times are tried. Beyond regrouping, it uses these identities: -- a delta on one side with no counterpart on the other is inserted on the other side at a slot of +- a delta on one side with no counterpart on the other is inserted on the other side at an index of its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index a481c6c79b..b294edf818 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -8,7 +8,7 @@ module public import Physlib.Mathematics.Tensors.Metric /-! -# The inverse of a tensor on two slots +# The inverse of a tensor on two indices The inverse `g^νμ` of a tensor `g_μν`, defined through the linear map that `g` defines. @@ -181,8 +181,8 @@ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi lemma inverse_form (c : Color K) [S.HasForm c.kind] : S.inverse (form c) = form c.dual := inverse_eq_left_inv _ (toLin_form_bijective c) (form_inverse c.dual) -/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`. The second slot of the form at `c` contracted -with the first slot of the form at `c.dual` is the delta. -/ +/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`. The second index of the form at `c` contracted +with the first index of the form at `c.dual` is the delta. -/ lemma contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : Tensor.contr (ColorEquiv.matMul c c c.dual) (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual)) = @@ -190,7 +190,7 @@ lemma contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : rw [formTensor, formTensor, ← inverse_form c] exact contr_mul_inverse _ (toLin_form_bijective c) -/-- `g^μν g_νρ = δ_ρ^μ` at an upper color `c`, with the delta read with its slots swapped. -/ +/-- `g^μν g_νρ = δ_ρ^μ` at an upper color `c`, with the delta read with its indices swapped. -/ lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : S.reindex (ColorEquiv.swapTwo c c.dual) (Tensor.contr (ColorEquiv.matMul c c c.dual) (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual))) = diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 509c83e8fe..3f5053df13 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -32,12 +32,12 @@ invariant exactly when its linear map commutes with the group action. This is Ma The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν`. The identity commutes with every group element, so the delta is invariant. `S.deltaTensor c` is the -delta as a tensor on two slots, and its components form the identity matrix. +delta as a tensor on two indices, and its components form the identity matrix. -Contracting the inner indices of two tensors on two slots, `M^μ_ν N^ν_ρ`, applies the linear map +Contracting the inner indices of two tensors on two indices, `M^μ_ν N^ν_ρ`, applies the linear map of `N` to the second factor of `M`, and the result is the delta exactly when the two linear maps are inverse. More generally, contracting an index of any tensor with the first index of a tensor -`F` on two slots applies the linear map of `F` at that index, and for the delta this is the +`F` on two indices applies the linear map of `F` at that index, and for the delta this is the identity. ## ii. Key results @@ -46,7 +46,7 @@ identity. the linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, and `Species.isIntertwiningMap_toLin_iff` : `z` is invariant exactly when its linear map commutes with the group action. - `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. -- `Species.deltaTensor` : the delta as a tensor on two slots, with +- `Species.deltaTensor` : the delta as a tensor on two indices, with `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. - `Species.Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, and `Species.Tensor.contr_matMul_eq_deltaTensor_iff` : it is the delta exactly when the linear maps @@ -59,7 +59,7 @@ identity. - A. Tensors of two factors as linear maps - B. The delta -- C. Products with a tensor on two slots +- C. Products with a tensor on two indices ## iv. References @@ -182,11 +182,11 @@ variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} /-! -## C. Products with a tensor on two slots +## C. Products with a tensor on two indices -/ -/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two slots that contracts the +/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are `0` and `1`. -/ def _root_.Tensor.ColorEquiv.matMul (a c b : Color K) : @@ -198,7 +198,7 @@ def _root_.Tensor.ColorEquiv.matMul (a c b : Color K) : toEquiv.left_inv := by decide toEquiv.right_inv := by decide -/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two slots is `M` with its second factor mapped +/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two indices is `M` with its second factor mapped by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : contr (ColorEquiv.matMul a c b) @@ -218,7 +218,7 @@ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual funext j fin_cases j <;> rfl -/-- Two tensors on two slots contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear +/-- Two tensors on two indices contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear maps compose to the identity, `Y ∘ X = id`. -/ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) (Y : S.V y.dual ⊗[k] S.V x.dual) : @@ -229,9 +229,9 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, Module.Basis.coordTensorHomEquiv_coordDelta] -/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the slot `i` of `T` with the first slot of a -tensor `F` on two slots and puts the second slot of `F` in the place of `i`. It is -`ColorEquiv.matMul` at any slot of a tensor on any slots. -/ +/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a +tensor `F` on two indices and puts the second index of `F` in the place of `i`. It is +`ColorEquiv.matMul` at any index of a tensor on any index type. -/ def _root_.Tensor.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where @@ -258,8 +258,8 @@ def _root_.Tensor.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Colo · simp [ha] · simp -/-- On a pure tensor, contracting the slot `i` with the first slot of `F` applies `F`, read as a -linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other slots are +/-- On a pure tensor, contracting the index `i` with the first index of `F` applies `F`, read as a +linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other indices are those of `p`, moved along `h`. -/ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) @@ -288,8 +288,8 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι (ColorEquiv.matMulAt i a b ha hi h).toEquiv.symm (.inl z) = .inl z by simp [ColorEquiv.matMulAt, hz]), hq z hz] -/-- Contracting the index `i` of `t` with the first index of a tensor `F` on two slots applies `F`, -read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ +/-- Contracting the index `i` of `t` with the first index of a tensor `F` on two indices applies +`F`, read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) (t : S.Tensor c) : diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 3630e14c6f..aecd3ebfb1 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -28,7 +28,7 @@ has one, as the SU(3) triplet has none. The inverse is stored with the form. Sym `S.IsAntisymmetricForm κ`. `S.toDual c` contracts a vector with the first index of the form at `c.dual`, lowering an upper -index and raising a lower one, and `S.toDualAt i` does so at the slot `i` of a tensor. For the +index and raising a lower one, and `S.toDualAt i` does so at the index `i` of a tensor. For the antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus the convention `ψ_α = ε_αβ ψ^β` with `ε^12 = ε_21 = 1`, so a Weyl species following that convention stores `-ε`. @@ -44,8 +44,8 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th `Species.isSymm_formBilin` and alternating by `Species.isAlt_formBilin`. - `Species.toDual` lowers an upper index and raises a lower one, and `Species.toDual_toDual` that raising undoes lowering. -- `Species.formTensor` is the form as a tensor on two slots. -- `Species.toDualAt` lowers or raises the index at one slot of a tensor, with +- `Species.formTensor` is the form as a tensor on two indices. +- `Species.toDualAt` lowers or raises one index of a tensor, with `Species.toDualAt_toDualAt`, `Species.toDualAt_eq_mapAt` and, in components, `Species.tensorBasis_repr_toDualAt`. - `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, @@ -291,12 +291,12 @@ lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] (S.tensorBasis ![c, c]).repr (S.formTensor c) μ = S.formMatrix c (μ 0) (μ 1) := tensorBasis_repr_twoIndexEquiv _ _ _ μ -/-- A symmetric form is symmetric under the slot swap as a tensor, `g^μν = g^νμ`. -/ +/-- A symmetric form is symmetric under the index swap as a tensor, `g^μν = g^νμ`. -/ lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = S.formTensor c := by rw [formTensor, reindex_twoIndexEquiv, form_symm] -/-- An antisymmetric form is antisymmetric under the slot swap as a tensor, +/-- An antisymmetric form is antisymmetric under the index swap as a tensor, `g^μν = -g^νμ`. -/ lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = -S.formTensor c := by @@ -311,9 +311,9 @@ lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor variable {ι : Type} [DecidableEq ι] variable (S) in -/-- `S.toDual` at the slot `i` of a tensor and the identity at the other slots, as in -`T^μν ↦ T^μ_ν` for `i` the second slot. The slot has the color `a` by `ha`, and the form is the one -at the kind of `a`. The colors `c'` of the result have the dual color at `i` by `hi` and agree +/-- `S.toDual` at the index `i` of a tensor and the identity at the other indices, as in +`T^μν ↦ T^μ_ν` for `i` the second index. The index has the color `a` by `ha`, and the form is the +one at the kind of `a`. The colors `c'` of the result have the dual color at `i` by `hi` and agree with `c` elsewhere by `h`. It is `congrAt` of `toDual`. -/ noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) : @@ -330,7 +330,7 @@ lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a S.toDualAt i a ha hi h (S.pure p) = S.pure q := mapAt_pure i _ h p q hqi hq -/-- Raising at a slot undoes lowering at that slot, `(T^μ_ν)^ν = T^μν`. -/ +/-- Raising at an index undoes lowering at that index, `(T^μ_ν)^ν = T^μν`. -/ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a.dual hi ha (fun x hx => (h x hx).symm) (S.toDualAt i a ha hi h t) = t := by @@ -338,15 +338,15 @@ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).symm from rfl, ← congrAt_symm] exact (S.congrAt i _ h).symm_apply_apply t -/-- Lowering or raising at a slot is `toDual` at that slot, read as a linear map. Chains of -lowerings and raisings at several slots compose slot by slot through `mapIndices_mapIndices`. -/ +/-- Lowering or raising at an index is `toDual` at that index, read as a linear map. Chains of +lowerings and raisings at several indices compose index by index through `mapIndices_mapIndices`. -/ lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : S.toDualAt i a ha hi h t = S.mapAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).toLinearMap h t := congrAt_eq_mapAt i _ h t -/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = g_νμ T^…μ…`. The +/-- Lowering or raising the index `i` in components, `T^…_ν… = g_νμ T^…μ…`. The labels `φ` of the result are cast to labels of `t`, and the label at `i` is replaced by the summed `μ`. For concrete colors the casts are along `rfl`. -/ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a : Color K) @@ -367,7 +367,7 @@ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a rw [basis_repr_cast, basis_repr_toDual] simp [Finsupp.single_apply] -/-- Lowering or raising at the slot `i` is contracting that slot with the second index of the +/-- Lowering or raising at the index `i` is contracting that index with the second index of the form, `T^…_ν… = g_νμ T^…μ…`. -/ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 51938cd73a..2ac6195471 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -14,15 +14,15 @@ The product `T ⊗ U` of two tensors has the indices of `T` followed by those of ## i. Overview -Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the slots `Fin 2` and `U = U^ρ e_ρ` colors `c₂` on -the slot `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the slots `Fin 2 ⊕ Fin 1` -with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on `inr j`, so no slot +Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the indices `Fin 2` and `U = U^ρ e_ρ` colors `c₂` on +the index `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the indices `Fin 2 ⊕ Fin 1` +with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on `inr j`, so no index is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Mathlib's `PiTensorProduct.tmulEquivDep`. -The product is associative and symmetric up to a relabeling of slots, and the group acts on it +The product is associative and symmetric up to a relabeling of indices, and the group acts on it factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on -two slots by `twoIndexEquiv`. The components of a product are the products of the components. +two indices by `twoIndexEquiv`. The components of a product are the products of the components. ## ii. Key results @@ -31,7 +31,7 @@ two slots by `twoIndexEquiv`. The components of a product are the products of th - `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. -- `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two slots. +- `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two indices. - `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. ## iii. Table of contents @@ -39,7 +39,7 @@ two slots by `twoIndexEquiv`. The components of a product are the products of th - A. The colors and relabelings of a product - B. The product - C. Associativity, symmetry and equivariance -- D. Tensors on one and two slots +- D. Tensors on one and two indices - E. Components ## iv. References @@ -60,8 +60,8 @@ namespace Tensor -/ -/-- The colors of a product `T ⊗ U`, the colors `c₁` of `T` on the slots `inl i` and the colors -`c₂` of `U` on the slots `inr j`. It equals Mathlib's `Sum.elim c₁ c₂` by `rfl`, and it is +/-- The colors of a product `T ⊗ U`, the colors `c₁` of `T` on the indices `inl i` and the colors +`c₂` of `U` on the indices `inr j`. It equals Mathlib's `Sum.elim c₁ c₂` by `rfl`, and it is reducible so that `prodColors c₁ c₂ (.inl i)` reduces to `c₁ i`. -/ noncomputable abbrev prodColors {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ι₁ ⊕ ι₂ → Color K := @@ -71,25 +71,25 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} -/-- The relabeling `σ` of the slots of `T` and `τ` of the slots of `U`, as a relabeling of the -slots of `T ⊗ U`. It is Mathlib's `Equiv.sumCongr`. -/ +/-- The relabeling `σ` of the indices of `T` and `τ` of the indices of `U`, as a relabeling of the +indices of `T ⊗ U`. It is Mathlib's `Equiv.sumCongr`. -/ def sumCongr {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') : ColorEquiv (prodColors c₁ c₂) (prodColors c₁' c₂') := ⟨Equiv.sumCongr σ.toEquiv τ.toEquiv, by rintro (j | j); exacts [σ.color_eq j, τ.color_eq j]⟩ -/-- The slots of `(T ⊗ U) ⊗ W` relabeled as those of `T ⊗ (U ⊗ W)`. It is Mathlib's +/-- The indices of `(T ⊗ U) ⊗ W` relabeled as those of `T ⊗ (U ⊗ W)`. It is Mathlib's `Equiv.sumAssoc`. -/ def sumAssoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : ColorEquiv (prodColors (prodColors c₁ c₂) c₃) (prodColors c₁ (prodColors c₂ c₃)) := ⟨Equiv.sumAssoc ι₁ ι₂ ι₃, by rintro (_ | _ | _) <;> rfl⟩ -/-- The slots of `T ⊗ U` relabeled as those of `U ⊗ T`. It is Mathlib's `Equiv.sumComm`. -/ +/-- The indices of `T ⊗ U` relabeled as those of `U ⊗ T`. It is Mathlib's `Equiv.sumComm`. -/ def sumComm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ColorEquiv (prodColors c₁ c₂) (prodColors c₂ c₁) := ⟨Equiv.sumComm ι₁ ι₂, by rintro (_ | _) <;> rfl⟩ -/-- The slots of `(T ⊗ W) ⊗ U` relabeled as those of `(T ⊗ U) ⊗ W`, swapping the last two +/-- The indices of `(T ⊗ W) ⊗ U` relabeled as those of `(T ⊗ U) ⊗ W`, swapping the last two factors. -/ noncomputable def swapLast (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : ColorEquiv (prodColors (prodColors c₁ c₃) c₂) (prodColors (prodColors c₁ c₂) c₃) := @@ -113,7 +113,7 @@ variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} variable (S) in /-- The product of tensors, `T ⊗ₜ U ↦ T ⊗ U` with `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. The indices of -`T` sit on the slots `inl i` and those of `U` on the slots `inr j`. It is Mathlib's +`T` sit on the indices `inl i` and those of `U` on the indices `inr j`. It is Mathlib's `PiTensorProduct.tmulEquivDep`. -/ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ((S.tensorRep c₁).tprod (S.tensorRep c₂)).Equiv (S.tensorRep (prodColors c₁ c₂)) := @@ -128,7 +128,7 @@ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) /-- The product of the pure tensors `u ⊗ α` and `w` is the pure tensor `u ⊗ α ⊗ w`. The vectors -`f₁` of the first sit on the slots `inl i` and the vectors `f₂` of the second on the slots +`f₁` of the first sit on the indices `inl i` and the vectors `f₂` of the second on the indices `inr j`. -/ @[simp] lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ : Π i, S.V (c₁ i)) @@ -138,7 +138,7 @@ lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ simp [tmulEquiv, tmulEquivDep_apply] /-- The inverse of the product splits a pure tensor into the pure tensors of its two groups of -slots, `u ⊗ α ⊗ w ↦ (u ⊗ α) ⊗ₜ w`. -/ +indices, `u ⊗ α ⊗ w ↦ (u ⊗ α) ⊗ₜ w`. -/ @[simp] lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f : Π s, S.V (prodColors c₁ c₂ s)) : @@ -152,7 +152,7 @@ lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) -/ -/-- The product is associative, `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)`, after the slots of the left side +/-- The product is associative, `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)`, after the indices of the left side are regrouped by `ColorEquiv.sumAssoc`. -/ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : @@ -215,7 +215,7 @@ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color S.reindex ((ColorEquiv.refl c₁).sumCongr σ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] -/-- Swapping the last two factors of a product, `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` after the slots of +/-- Swapping the last two factors of a product, `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` after the indices of `U` are moved before those of `W` by `ColorEquiv.swapLast`. -/ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : @@ -236,16 +236,16 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : /-! -## D. Tensors on one and two slots +## D. Tensors on one and two indices A species gives objects with two indices, such as a metric `η_μν`, in Mathlib's binary tensor product `V a ⊗ V b`. `twoIndexEquiv` identifies this space with the tensors -`S.Tensor ![a, b]` on two slots, so that such an object can be used in index notation. +`S.Tensor ![a, b]` on two indices, so that such an object can be used in index notation. -/ variable (S) in -/-- A vector `v` of `V c` as a tensor with the one slot `![c]`, by Mathlib's +/-- A vector `v` of `V c` as a tensor with the one index `![c]`, by Mathlib's `PiTensorProduct.subsingletonEquiv`. -/ noncomputable def oneIndexEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c]) := .mk ((subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm) @@ -254,7 +254,7 @@ noncomputable def oneIndexEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c rw [smul_pure] exact congrArg S.pure (funext fun i => by fin_cases i; rfl) -/-- The tensor of `v` is the pure tensor with `v` on its one slot. -/ +/-- The tensor of `v` is the pure tensor with `v` on its one index. -/ @[simp] lemma oneIndexEquiv_apply (c : Color K) (v : S.V c) : S.oneIndexEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := @@ -268,7 +268,7 @@ lemma oneIndexEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` -is the tensor `η_μν`. Each factor is a one-slot tensor by `oneIndexEquiv`, and the slots +is the tensor `η_μν`. Each factor is a one-index tensor by `oneIndexEquiv`, and the indices `Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by Mathlib's `finSumFinEquiv`. -/ noncomputable def twoIndexEquiv (a b : Color K) : ((S.rep a).tprod (S.rep b)).Equiv (S.tensorRep ![a, b]) := @@ -276,7 +276,7 @@ noncomputable def twoIndexEquiv (a b : Color K) : (S.tmulEquiv ![a] ![b]).trans (S.reindex (ColorEquiv.mk (c₁ := prodColors ![a] ![b]) (c₂ := ![a, b]) finSumFinEquiv)) -/-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the slot `0` and `y` on the slot `1`. -/ +/-- The tensor of `x ⊗ₜ y` is the pure tensor with `x` on the index `0` and `y` on the index `1`. -/ @[simp] lemma twoIndexEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : S.twoIndexEquiv a b (x ⊗ₜ y) = @@ -285,7 +285,7 @@ lemma twoIndexEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : Representation.Equiv.tensorCongr_tmul, oneIndexEquiv_apply, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) -/-- Swapping the two slots of a tensor on two slots swaps the factors, `M^μν ↦ M^νμ`. -/ +/-- Swapping the two indices of a tensor on two indices swaps the factors, `M^μν ↦ M^νμ`. -/ lemma reindex_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : S.reindex (ColorEquiv.swapTwo a b) (S.twoIndexEquiv a b M) = S.twoIndexEquiv b a (TensorProduct.comm k _ _ M) := by @@ -300,7 +300,7 @@ lemma reindex_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : ## E. Components The components of a product are the products of the components of its factors. The components -of a tensor on two slots are the coordinates in Mathlib's basis `Basis.tensorProduct`. +of a tensor on two indices are the coordinates in Mathlib's basis `Basis.tensorProduct`. -/ diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index ee64e4ebb4..375be8e286 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -8,26 +8,26 @@ module public import Physlib.Mathematics.Tensors.Tensor /-! -# Relabeling the slots of a tensor +# Relabeling the indices of a tensor -A bijection of the slots of a tensor that keeps their colors, and its action on tensors, as in the +A bijection of the indices of a tensor that keeps their colors, and its action on tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. ## i. Overview -A relabeling `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the slots of a tensor +A relabeling `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the indices of a tensor that keeps their colors, with the proof `σ.color_eq`. For the transpose `T^μ_ν ↦ T_ν^μ` it swaps -the slots `0` and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would +the indices `0` and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would turn an up index into a down one is not a relabeling, since that needs a metric. The labels `ι₁` and `ι₂` may be different types, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the product `u^μ v^ν`. -`S.reindex σ` moves each vector of a tensor to its new slot. The name follows Mathlib's +`S.reindex σ` moves each vector of a tensor to its new index. The name follows Mathlib's `PiTensorProduct.reindex`. Index notation uses it to line up indices, as in `A^μ_ν + B_ν^μ`. ## ii. Key results -- `ColorEquiv` is a bijection of slots that keeps their colors, with `refl`, `symm` and `trans`. -- `Species.permIndices` moves the vectors of a tensor along a bijection of slots, with no colors +- `ColorEquiv` is a bijection of indices that keeps their colors, with `refl`, `symm` and `trans`. +- `Species.permIndices` moves the vectors of a tensor along a bijection of indices, with no colors involved. It is Mathlib's `PiTensorProduct.reindex`. - `Species.reindex` applies a relabeling to tensors, `permIndices` followed by `congrIndices` of the moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. @@ -64,16 +64,16 @@ namespace Tensor -/ -/-- A relabeling of slots that keeps their colors, made of a bijection of slots and the proof that -each new slot has the color of the old slot it comes from. The proof is stated at the new labels -`j`, so that `c₂ j` reduces at labels such as `inl x` or `0`. For finitely many concrete slots it -is found by `rfl` or by cases. Two relabelings are equal when their bijections are +/-- A relabeling of indices that keeps their colors, made of a bijection of indices and the proof +that each new index has the color of the old index it comes from. The proof is stated at the new +indices `j`, so that `c₂ j` reduces at indices such as `inl x` or `0`. For finitely many concrete +indices it is found by `rfl` or by cases. Two relabelings are equal when their bijections are (`ColorEquiv.ext`). -/ @[ext] structure ColorEquiv {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) where - /-- The bijection of slots, old slot `i` to new slot `toEquiv i`. -/ + /-- The bijection of indices, old index `i` to new index `toEquiv i`. -/ toEquiv : ι₁ ≃ ι₂ - /-- The new slot `j` has the color of the old slot `toEquiv.symm j`. -/ + /-- The new index `j` has the color of the old index `toEquiv.symm j`. -/ color_eq : ∀ j, c₁ (toEquiv.symm j) = c₂ j := by first | exact fun _ => rfl | (intro j; fin_cases j <;> rfl) @@ -82,10 +82,10 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ ι₃ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} -/-- The identity relabeling, where every slot keeps its label. -/ +/-- The identity relabeling, where every index stays in place. -/ def refl (c₁ : ι₁ → Color K) : ColorEquiv c₁ c₁ := ⟨Equiv.refl ι₁, fun _ => rfl⟩ -/-- The inverse relabeling, where every slot gets back its old label. -/ +/-- The inverse relabeling, where every index returns to its old position. -/ def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := ⟨σ.toEquiv.symm, fun i => by simpa using (σ.color_eq (σ.toEquiv i)).symm⟩ @@ -93,7 +93,7 @@ def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := def trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : ColorEquiv c₁ c₃ := ⟨σ₁.toEquiv.trans σ₂.toEquiv, fun j => (σ₁.color_eq _).trans (σ₂.color_eq j)⟩ -/-- The relabeling that swaps the two slots of a tensor on two slots, `![a, b] → ![b, a]`. -/ +/-- The relabeling that swaps the two indices of a tensor on two indices, `![a, b] → ![b, a]`. -/ def swapTwo (a b : Color K) : ColorEquiv (K := K) ![a, b] ![b, a] := ⟨Equiv.swap 0 1, by intro j; fin_cases j <;> rfl⟩ @@ -129,8 +129,8 @@ variable {ι₁ ι₂ ι₃ : Type} -/ variable (S) in -/-- The bijection `e` of slots applied to tensors, moving the vector of the old slot `i` to the new -slot `e i`, with the colors `c ∘ e.symm`. It is Mathlib's `PiTensorProduct.reindex`. -/ +/-- The bijection `e` of indices applied to tensors, moving the vector of the old index `i` to the +new index `e i`, with the colors `c ∘ e.symm`. It is Mathlib's `PiTensorProduct.reindex`. -/ noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.reindex k (fun i => S.V (c i)) e ≪≫ₗ @@ -139,21 +139,21 @@ noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : PiTensorProduct.reindex_tprod, toPiTensor_symm_tprod] rfl -/-- On a pure tensor, the new slot `j` holds the vector of the old slot `e.symm j`. -/ +/-- On a pure tensor, the new index `j` holds the vector of the old index `e.symm j`. -/ @[simp] lemma permIndices_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, S.V (c i)) : S.permIndices c e (S.pure f) = S.pure fun j => f (e.symm j) := by simp [permIndices, PiTensorProduct.reindex_tprod] variable (S) in -/-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new slot `j` -holds the vector of the old slot `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is +/-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new index `j` +holds the vector of the old index `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is `permIndices` of `σ.toEquiv` followed by `congrIndices` of the moves. -/ noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : (S.tensorRep c₁).Equiv (S.tensorRep c₂) := (S.permIndices c₁ σ.toEquiv).trans (S.congrIndices fun j => S.cast (σ.color_eq j)) -/-- On a pure tensor, the new slot `j` holds the vector of the old slot `σ.toEquiv.symm j`, moved +/-- On a pure tensor, the new index `j` holds the vector of the old index `σ.toEquiv.symm j`, moved along `σ.color_eq j`. -/ @[simp] lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) @@ -200,7 +200,7 @@ lemma reindex_symm {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C -/ /-- The components of a relabeled tensor are those of the tensor, as in `(Tᵀ)_ν^μ = T^μ_ν`. The -old slot `i` takes the label `y (σ.toEquiv i)` of its new slot, cast along the equality of their +old index `i` takes the label `y (σ.toEquiv i)` of its new index, cast along the equality of their colors. For concrete colors and labels the cast is between equal types, and `rfl` removes it. -/ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (t : S.Tensor c₁) @@ -214,7 +214,7 @@ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → | h f => rw [reindex_pure, tensorBasis_repr_pure, tensorBasis_repr_pure] refine Fintype.prod_equiv σ.toEquiv.symm _ _ fun j => ?_ - -- The label `y j` is read at the new slot `σ.toEquiv (σ.toEquiv.symm j)`, which is `j` only + -- The label `y j` is read at the new index `σ.toEquiv (σ.toEquiv.symm j)`, which is `j` only -- up to a proof. rw [basis_repr_cast] exact congrArg _ (eq_of_heq ((cast_heq _ _).trans diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 2728eb031a..2e68c17c7c 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -20,7 +20,7 @@ tensors are built from these. A species `S : Species k G K basisIdx` over `k = ℝ` or `ℂ` (`RCLike k`) gives each color `c` a `k`-vector space `S.V c`, a representation `S.rep c` of the group `G` on it, and a basis -`S.basis c`. A tensor of `S` with slot colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with +`S.basis c`. A tensor of `S` with index colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with the action of `G` on each factor. For real Lorentz tensors in `1 + d` dimensions, `k = ℝ`, `G` is the Lorentz group, and there is @@ -234,8 +234,8 @@ variable {S} ## C. Moving a vector along an equality of colors `S.V a` and `S.V a'` are different types unless `a` and `a'` are equal by definition. The colors -of two slots are often equal only through a proof. For a family of colors `c : ι → Color K` and -two slots with `s = s'`, the colors `c s` and `c s'` are equal through that proof. A vector is +of two indices are often equal only through a proof. For a family of colors `c : ι → Color K` and +two indices with `s = s'`, the colors `c s` and `c s'` are equal through that proof. A vector is moved between the two spaces by `S.cast`, which is the identity on the vector. The lemmas say that this keeps the group action, the components and the contraction, so that proofs can remove the casts. @@ -266,8 +266,8 @@ lemma basis_repr_cast {a b : Color K} (p : a = b) (v : S.V a) (μ : basisIdx b.k subst p rfl -/-- For a family `f` of vectors on slots, moving `f s` to the color `b` is moving `f s'`, for any -slot `s'` equal to `s`. -/ +/-- For a family `f` of vectors on indices, moving `f s` to the color `b` is moving `f s'`, for any +index `s'` equal to `s`. -/ lemma cast_apply_congr {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} (e : s = s') {b : Color K} (p : d s = b) : S.cast p (f s) = S.cast (e ▸ p) (f s') := by subst e diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index e5708c2d60..1f6429b31f 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -13,16 +13,16 @@ public import Physlib.Mathematics.ForMathlib.Representation # Tensors of a species The tensors of a species with colors `c : ι → Color K` form Mathlib's `⨂[k] i, V (c i)`, for any -slot type `ι`, and the group acts on them slot by slot. +index type `ι`, and the group acts on them index by index. ## i. Overview -A tensor of a species `S` has one slot for each label `i : ι`, and slot `i` has the color `c i`. +A tensor of a species `S` has one index `i` for each `i : ι`, and the index `i` has the color `c i`. The tensors with colors `c : ι → Color K` form the space `S.Tensor c`, which is Mathlib's -`⨂[k] i, V (c i)`. The slots `ι` can be any type, so later files label slots by `Fin n`, sums or -options without renumbering. +`⨂[k] i, V (c i)`. The index type `ι` can be any type, so later files take `Fin n`, sums or options +for it without renumbering. -A pure tensor takes one vector in each slot, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in +A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in `V (c 1)` and `β` in `V (c 2)`. Its components are products, `(u ⊗ α ⊗ β)^μ_νρ = u^μ α_ν β_ρ`. Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its @@ -31,7 +31,7 @@ values on pure tensors (`ext_pure`), and a property of all tensors follows from their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's `⨂ₜ[k] i, f i` as an element of `S.Tensor c`. -A group element `Λ` acts on every slot by the representation of its color, +A group element `Λ` acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). A map that commutes with `Λ` on pure tensors commutes with it on all tensors (`map_smul_of_pure`). @@ -45,7 +45,7 @@ as `(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ` (`tensor transform with `Λ⁻¹`, which keeps `u^μ α_μ` invariant. A linear equivalence `f` from the space of one color to that of another acts on tensors through a -single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the other slots, +single index. `S.congrAt i f` applies `f` at the index `i` and the identity at the other indices, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`, as lowering one index of a tensor does. ## ii. Key results @@ -56,22 +56,22 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the `Species.ext_pure₂` and `Species.ext_pure₃` do the same for maps out of products of two and three spaces of tensors, and `Species.equiv_ext_pure` and `Species.intertwiningMap_ext_pure` for maps of representations. -- `Species.smul_pure` proves that the group acts on a pure tensor slot by slot. +- `Species.smul_pure` proves that the group acts on a pure tensor index by index. - `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure tensors commutes with it on all tensors. - `Species.tensorRep` is the action on tensors as a Mathlib representation. Maps of tensors that commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` of it, built by `Species.equivOfPure` and `Species.intertwiningMapOfPure` from a check on pure tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. -- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no slots is invariant. +- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. - `Species.tensorBasis_repr_smul` gives the components of `g • t`. -- `Species.mapIndices` applies a linear map at every slot, with `Species.mapIndices_comp` and +- `Species.mapIndices` applies a linear map at every index, with `Species.mapIndices_comp` and `Species.mapIndices_id`, and `Species.mapIndices_smul` proves that it commutes with the group when - every slot map does. -- `Species.congrIndices` applies an equivalence of representations at every slot. -- `Species.indexMapsAt` is the family of a linear map at one slot and the identity at the others, + every index map does. +- `Species.congrIndices` applies an equivalence of representations at every index. +- `Species.indexMapsAt` is the family of a linear map at one index and the identity at the others, and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and `Species.mapAt_id`. - `Species.congrAt` is `mapAt` of an equivalence of representations, as an equivalence of @@ -82,7 +82,7 @@ single slot. `S.congrAt i f` applies `f` at the slot `i` and the identity at the - A. Tensors - B. The group action - C. The basis of tensors -- D. Linear maps at the slots +- D. Linear maps at the indices ## iv. References @@ -107,7 +107,7 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → -/ -/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the slots `ι`. It is a `def`, not +/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not an `abbrev`, so that the type `S.Tensor c` keeps the species and the colors, which index notation reads off the type of a tensor. -/ def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := @@ -129,7 +129,7 @@ noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : S.Tensor c ≃ₗ[k] ⨂[k] i, S.V (c i) := LinearEquiv.refl k _ -/-- The pure tensor of a family of vectors, one per slot, such as `u ⊗ α ⊗ β` with components +/-- The pure tensor of a family of vectors, one per index, such as `u ⊗ α ⊗ β` with components `u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : S.Tensor c := (S.toPiTensor c).symm (⨂ₜ[k] i, f i) @@ -177,7 +177,7 @@ lemma ext_pure₃ {ι₂ ι₃ : Type} {c₂ : ι₂ → Color K} {c₃ : ι₃ -/ -/-- The group acts on every slot by the representation of its color, +/-- The group acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. -/ noncomputable instance : DistribMulAction G (S.Tensor c) := DistribMulAction.compHom (M := Module.End k (S.Tensor c)) _ @@ -266,7 +266,7 @@ lemma intertwiningMap_ext_pure {W : Type} [AddCommGroup W] [Module k W] (h : ∀ p : Π i, S.V (c i), φ (S.pure p) = ψ (S.pure p)) : φ = ψ := Representation.IntertwiningMap.ext (ext_pure h) -/-- A tensor with no slots is invariant, `Λ s = s`. -/ +/-- A tensor with no indices is invariant, `Λ s = s`. -/ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = t := by induction t using inductionOn_pure with | h f => rw [smul_pure]; exact congrArg _ (funext isEmptyElim) @@ -296,11 +296,11 @@ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] -/-- Tensors on finitely many slots are a finite-dimensional space. -/ +/-- Tensors on finitely many indices are a finite-dimensional space. -/ noncomputable instance [Fintype ι] : Module.Finite k (S.Tensor c) := .of_basis (S.tensorBasis c) -/-- The dimension of tensors is the product of the dimensions of their slots. -/ +/-- The dimension of tensors is the product of the dimensions of their indices. -/ lemma finrank_tensor [Fintype ι] [DecidableEq ι] (c : ι → Color K) : Module.finrank k (S.Tensor c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by rw [Module.finrank_eq_card_basis (S.tensorBasis c), Fintype.card_pi] @@ -318,7 +318,7 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor /-! -## D. Linear maps at the slots +## D. Linear maps at the indices -/ @@ -327,38 +327,38 @@ section variable {c' : ι → Color K} variable (S) in -/-- A linear map `f x` at every slot `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's +/-- A linear map `f x` at every index `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap -/-- On a pure tensor, `mapIndices` applies `f x` to the vector at each slot `x`. -/ +/-- On a pure tensor, `mapIndices` applies `f x` to the vector at each index `x`. -/ lemma mapIndices_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : S.mapIndices f (S.pure p) = S.pure fun x => f x (p x) := by simp only [mapIndices, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, toPiTensor_pure, PiTensorProduct.map_tprod, toPiTensor_symm_tprod] -/-- Two slotwise maps in sequence are the slotwise map of the composites. -/ +/-- Two index-wise maps in sequence are the index-wise map of the composites. -/ lemma mapIndices_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) : S.mapIndices g ∘ₗ S.mapIndices f = S.mapIndices fun x => g x ∘ₗ f x := ext_pure fun p => by simp only [LinearMap.comp_apply, mapIndices_pure] -/-- Two slotwise maps in sequence are the slotwise map of the composites, on a tensor. -/ +/-- Two index-wise maps in sequence are the index-wise map of the composites, on a tensor. -/ lemma mapIndices_mapIndices {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : S.Tensor c) : S.mapIndices g (S.mapIndices f t) = S.mapIndices (fun x => g x ∘ₗ f x) t := LinearMap.congr_fun (mapIndices_comp f g) t -/-- The slotwise map of the identities is the identity. -/ +/-- The index-wise map of the identities is the identity. -/ lemma mapIndices_id : S.mapIndices (c := c) (fun _ => LinearMap.id) = LinearMap.id := ext_pure fun p => mapIndices_pure _ p -/-- The slotwise map of the identities is the identity, on a tensor. -/ +/-- The index-wise map of the identities is the identity, on a tensor. -/ lemma mapIndices_id_apply (t : S.Tensor c) : S.mapIndices (fun _ => LinearMap.id) t = t := LinearMap.congr_fun mapIndices_id t -/-- Linear maps that commute with the group action at every slot commute with it on tensors, +/-- Linear maps that commute with the group action at every index commute with it on tensors, `f (Λ T) = Λ f T`. -/ lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : S.Tensor c) : @@ -366,7 +366,7 @@ lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapIndices_pure, hf]) t variable (S) in -/-- Equivalences of representations `f x` at every slot `x`, as an equivalence of representations +/-- Equivalences of representations `f x` at every index `x`, as an equivalence of representations of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : (S.tensorRep c).Equiv (S.tensorRep c') := @@ -374,7 +374,7 @@ noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : (S.toPiTensor c').symm) fun g p => mapIndices_smul (fun x => (f x).toIntertwiningMap.isIntertwining _ _) g (S.pure p) -/-- On a pure tensor, `congrIndices` applies `f x` to the vector at each slot `x`. -/ +/-- On a pure tensor, `congrIndices` applies `f x` to the vector at each index `x`. -/ lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : S.congrIndices f (S.pure p) = S.pure fun x => f x (p x) := mapIndices_pure (fun x => (f x).toLinearMap) p @@ -384,8 +384,8 @@ section variable [DecidableEq ι] variable (S) in -/-- The family of linear maps with `f` at the slot `i` and the identity, moved along `h x`, at -every other slot `x`. `mapAt` applies it slot by slot. -/ +/-- The family of linear maps with `f` at the index `i` and the identity, moved along `h x`, at +every other index `x`. `mapAt` applies it index by index. -/ noncomputable def indexMapsAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : Π x, S.V (c x) →ₗ[k] S.V (c' x) := Function.update (fun x => if hx : x = i then 0 else (S.cast (h x hx)).toLinearMap) i f @@ -401,14 +401,14 @@ lemma indexMapsAt_apply_ne (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) S.indexMapsAt i f h x = (S.cast (h x hx)).toLinearMap := by rw [indexMapsAt, Function.update_of_ne hx, dite_eq_right hx] -/-- A family with `f` at the slot `i` and the moves along `h x` at the other slots is +/-- A family with `f` at the index `i` and the moves along `h x` at the other indices is `indexMapsAt i f h`. -/ lemma eq_indexMapsAt {F : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (hi : F i = f) (hne : ∀ x (hx : x ≠ i), F x = (S.cast (h x hx)).toLinearMap) : F = S.indexMapsAt i f h := Function.eq_update_iff.2 ⟨hi, fun x hx => by rw [hne x hx, dite_eq_right hx]⟩ -/-- `indexMapsAt i f h` commutes with the group action at every slot when `f` does. -/ +/-- `indexMapsAt i f h` commutes with the group action at every index when `f` does. -/ lemma indexMapsAt_rep (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (hf : ∀ g v, f (S.rep (c i) g v) = S.rep (c' i) g (f v)) (x : ι) (g : G) (v : S.V (c x)) : S.indexMapsAt i f h x (S.rep (c x) g v) = S.rep (c' x) g (S.indexMapsAt i f h x v) := by @@ -418,7 +418,7 @@ lemma indexMapsAt_rep (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, · simpa [hx] using cast_rep (h x hx) g v variable (S) in -/-- The linear map `f` at the slot `i`, and the identity, moved along `h x`, at every other slot +/-- The linear map `f` at the index `i`, and the identity, moved along `h x`, at every other index `x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := @@ -438,7 +438,7 @@ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ · rw [indexMapsAt_apply_ne _ _ _ hx, hq x hx] rfl -/-- `mapAt` of `f` and then of `g` at the same slot is `mapAt` of `g ∘ₗ f`. -/ +/-- `mapAt` of `f` and then of `g` at the same index is `mapAt` of `g ∘ₗ f`. -/ lemma mapAt_comp {c'' : ι → Color K} (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (g : S.V (c' i) →ₗ[k] S.V (c'' i)) (h : ∀ x, x ≠ i → c x = c' x) (h' : ∀ x, x ≠ i → c' x = c'' x) : @@ -468,7 +468,7 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V | hadd t t' ht ht' => simp only [map_add, Finsupp.add_apply, ht, ht', mul_add, Finset.sum_add_distrib] | h p => - -- Split off the slot `i`, where `f` acts, from the slots where the label is only moved. + -- Split off the index `i`, where `f` acts, from the indices where the label is only moved. rw [mapAt, mapIndices_pure, tensorBasis_repr_pure, ← Finset.mul_prod_erase _ _ (Finset.mem_univ i), indexMapsAt_apply_self, ← LinearMap.toMatrix_mulVec_repr (S.basis (c i)) (S.basis (c' i)), Matrix.mulVec, dotProduct, @@ -482,8 +482,8 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V exact basis_repr_cast _ _ _ variable (S) in -/-- The equivalence of representations `f` at the slot `i`, and the identity, moved along `h x`, -at every other slot `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. Its linear map is `mapAt` +/-- The equivalence of representations `f` at the index `i`, and the identity, moved along `h x`, +at every other index `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. Its linear map is `mapAt` of `f`. -/ noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) (h : ∀ x, x ≠ i → c x = c' x) : (S.tensorRep c).Equiv (S.tensorRep c') := diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 9057ff6bfd..9eba85e482 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -32,8 +32,8 @@ Instances come from the space of one index (`ofIndexSpace`), from an equivalent - `Tensorial` : a type whose elements are tensors, compatibly with the action of the group. - `Tensorial.toTensor_rep` : `toTensor (Λ m) = Λ • toTensor m`. -- `Tensorial.ofIndexSpace` : the space of one index is tensorial, with one slot, and - `Tensorial.tensorBasis_repr_toTensor_ofIndexSpace` gives its components. +- `Tensorial.ofIndexSpace` : the space of one index is tensorial, as the tensors with that index, + and `Tensorial.tensorBasis_repr_toTensor_ofIndexSpace` gives its components. - `Tensorial.ofEquiv` : a type equivalent to a tensorial type, compatibly with the actions, is tensorial with the same colors. - `Tensorial.prod` : the tensor product of two tensorial types is tensorial, with @@ -90,7 +90,7 @@ lemma toTensor_rep [Tensorial S c₁ M] (g : G) (m : M) : LinearMap.congr_fun ((toTensor (M := M)).isIntertwining' g) m variable (S) in -/-- The space `S.V c` of one index as the tensors with the one slot `![c]`, with the action +/-- The space `S.V c` of one index as the tensors with the one index `![c]`, with the action `S.rep c`. A type that is such a space, such as a Weyl spinor, gets its instance from this. -/ noncomputable abbrev ofIndexSpace (c : Color K) : Tensorial S ![c] (S.V c) where rep := S.rep c @@ -121,7 +121,7 @@ lemma toTensor_ofEquiv [Tensorial S c₁ N] {r : Representation k G M} -/ -/-- The tensor product `M ⊗ N` of two tensorial types is tensorial on the slots of both, with the +/-- The tensor product `M ⊗ N` of two tensorial types is tensorial on the indices of both, with the product action `Λ (x ⊗ y) = Λ x ⊗ Λ y` and `x ⊗ y` read as the product of the tensors of `x` and `y`. -/ noncomputable instance prod [Tensorial S c₁ M] [Tensorial S c₂ N] : diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 4d8eeb6d60..e4dd9a8ccf 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -42,7 +42,7 @@ forms are written `εL`, `εL'`, `εR`, `εR'`, `η` and `η'`, and the deltas ` `δ` and `δ'`. Their components are stated through `tensorBasis`, and the antisymmetry of `ε` and the symmetry of `η` are stated in index notation. -These spaces are tensors with one slot (`Tensorial`), so a spinor `ψ : LeftHandedWeyl` +These spaces are tensors with one index (`Tensorial`), so a spinor `ψ : LeftHandedWeyl` enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_α`. ## ii. Key results @@ -64,7 +64,7 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ `ε` in index notation, from `Species.schouten_formTensor_deltaTensor` and `Species.schouten_formTensor_deltaTensor_dual`. - `complexLorentz.leftHandedWeylTensorial` and the other instances of section F : Weyl spinors and - complex vectors as tensors with one slot. + complex vectors as tensors with one index. - `complexLorentz.tensorBasis_repr_toTensor_leftRight` : the components of a tensor in `LeftHandedWeyl ⊗ RightHandedWeyl` are the entries of `leftRightToMatrix`. @@ -441,7 +441,7 @@ lemma formMatrix_apply (c : Color Kind) (μ ν : Idx c.kind) : basis_tensorProduct_repr_form c μ ν /-- The components of the form tensor at any color are `formEntries`, at the pair of labels -`(μ 0, μ 1)`. The labels `μ` form a dependent family, since the basis labels of a slot depend on +`(μ 0, μ 1)`. The labels `μ` form a dependent family, since the basis labels of an index depend on its color. -/ lemma tensorBasis_repr_formTensor (c : Color Kind) (μ : Π i, Idx (![c, c] i).kind) : (complexLorentz.tensorBasis ![c, c]).repr (complexLorentz.formTensor c) μ = diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 2f7c4c0c15..54b2e430b0 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -291,7 +291,7 @@ lemma basis_repr_toDual (d : ℕ) (c : Color Kind) (v : (realLorentz d).V c) simp [Species.basis_repr_toDual c v ν, formMatrix_apply, minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, mul_comm] -/-- Lowering or raising the index at the slot `i` in components, `T^…_ν… = η_νν T^…ν…`: the +/-- Lowering or raising the index `i` in components, `T^…_ν… = η_νν T^…ν…`: the Minkowski metric is diagonal. -/ lemma tensorBasis_repr_toDualAt (d : ℕ) {ι : Type} [Fintype ι] [DecidableEq ι] {c c' : ι → Color Kind} (i : ι) (a : Color Kind) (ha : c i = a) (hi : c' i = a.dual) @@ -416,7 +416,7 @@ lemma tensorBasis_repr_toTensor_coVector_tmul (d : ℕ) (T : CoVector d ⊗[ℝ] (CoVector.basis.tensorProduct Vector.basis).repr T (μ (.inl 0), μ (.inr 0)) := Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace (S := realLorentz d) down up T μ -/-- The contraction `v^μ ω_μ` of a vector with a covector, a tensor with no slots whose one +/-- The contraction `v^μ ω_μ` of a vector with a covector, a tensor with no indices whose one component is `∑ μ, v^μ ω_μ`. -/ lemma tensorBasis_repr_contr_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : ((realLorentz d).tensorBasis ![]).repr {v | μ ⊗ ω | μ}ᵀ (fun i => i.elim0) = @@ -426,7 +426,7 @@ lemma tensorBasis_repr_contr_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVe tensorBasis_repr_toTensor_coVector] open scoped Lorentz.Vector in -/-- The Minkowski product is the contraction `η_μν v^μ w^ν`, a tensor with no slots whose one +/-- The Minkowski product is the contraction `η_μν v^μ w^ν`, a tensor with no indices whose one component is `⟪v, w⟫ₘ`. -/ lemma tensorBasis_repr_formTensor_contr_vector (d : ℕ) (v w : Vector d) : ((realLorentz d).tensorBasis ![]).repr From 5d9db56dc1998d42476689ecffa7ede85de44925 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:38:12 +0000 Subject: [PATCH 126/242] doc(Tensors): Color and Species docs as in #2049 Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 6 +++--- Physlib/Mathematics/Tensors/Species.lean | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index 894d5a3e76..e919aaa665 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -20,9 +20,9 @@ covector index of `w_μ` has color `⟨vec, down⟩`. Complex Lorentz tensors ha for each of the two types of Weyl spinor index and one for the vector index. A tensor has one color per index. A species (`Species.lean`) gives each color a space, and a -tensor is an element of the tensor product of the spaces of its indices. Indices carry colors -rather than spaces, so that checking that two indices can be contracted is an equality of colors, -decidable, not an equality of types. +tensor is an element of the tensor product of the spaces of its indices. Each index has a color +rather than a space, so that checking that two indices can be contracted is an equality of +colors, which is decidable, not an equality of types. The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, the same kind with the other variance. A contraction such as `v^μ w_μ` pairs an index of color `c` with an index of color `c.dual`. `Color K` is a structure diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 2e68c17c7c..27571a138a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -20,8 +20,8 @@ tensors are built from these. A species `S : Species k G K basisIdx` over `k = ℝ` or `ℂ` (`RCLike k`) gives each color `c` a `k`-vector space `S.V c`, a representation `S.rep c` of the group `G` on it, and a basis -`S.basis c`. A tensor of `S` with index colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, with -the action of `G` on each factor. +`S.basis c`. A tensor of `S` with index colors `c₁, …, cₙ` is an element of `V c₁ ⊗ ⋯ ⊗ V cₙ`, +with the action of `G` on each factor. For real Lorentz tensors in `1 + d` dimensions, `k = ℝ`, `G` is the Lorentz group, and there is one kind `vec`, whose basis labels are `basisIdx vec = Fin 1 ⊕ Fin d`. The space of `⟨vec, up⟩` @@ -266,8 +266,8 @@ lemma basis_repr_cast {a b : Color K} (p : a = b) (v : S.V a) (μ : basisIdx b.k subst p rfl -/-- For a family `f` of vectors on indices, moving `f s` to the color `b` is moving `f s'`, for any -index `s'` equal to `s`. -/ +/-- For a family `f` of vectors on indices, moving `f s` to the color `b` is moving `f s'`, for +any index `s'` equal to `s`. -/ lemma cast_apply_congr {ι : Type} {d : ι → Color K} (f : Π s, S.V (d s)) {s s' : ι} (e : s = s') {b : Color K} (p : d s = b) : S.cast p (f s) = S.cast (e ▸ p) (f s') := by subst e From e66bf06fa9957dfd3f730920a5a45c67b944d52c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:50:13 +0000 Subject: [PATCH 127/242] doc(Tensors): toDual and toDualAt_eq_contr lower with the second index of the form MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Metric overview and the docstring of complexLorentz.schouten_downL said that lowering contracts the first index of the form. It contracts the second, v_ν = g_νμ v^μ, so the Weyl convention ψ_α = ε_αβ ψ^β holds as stored. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Metric.lean | 13 +++++++------ .../Relativity/Tensors/ComplexTensor/Species.lean | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index aecd3ebfb1..801d7415b7 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -18,7 +18,7 @@ one. ## i. Overview For Lorentz tensors the invariant form is the Minkowski metric `η_μν` with inverse `η^μν`. It -lowers `v_ν = v^μ η_μν`, raises `w^ν = w_μ η^μν`, and the two are inverse, +lowers `v_ν = η_νμ v^μ`, raises `w^ν = η^νμ w_μ`, and the two are inverse, `v^μ η_μν η^νρ = v^ρ`. The class `S.HasForm κ` is a chosen invariant form on the kind `κ`, `g^μν` at the upper color and @@ -27,10 +27,11 @@ has one, as the SU(3) triplet has none. The inverse is stored with the form. Sym `η`, and antisymmetry, as for the Weyl `ε`, are the optional classes `S.IsSymmetricForm κ` and `S.IsAntisymmetricForm κ`. -`S.toDual c` contracts a vector with the first index of the form at `c.dual`, lowering an upper -index and raising a lower one, and `S.toDualAt i` does so at the index `i` of a tensor. For the -antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus the convention -`ψ_α = ε_αβ ψ^β` with `ε^12 = ε_21 = 1`, so a Weyl species following that convention stores `-ε`. +`S.toDual c` contracts a vector with the second index of the form at `c.dual`, lowering an upper +index, `v_ν = g_νμ v^μ`, and raising a lower one, `w^ν = g^νμ w_μ`, and `S.toDualAt i` does so at +the index `i` of a tensor. For the antisymmetric `ε`, where the order of the indices matters, +lowering gives `ψ_α = ε_αβ ψ^β`, the convention of Wess and Bagger and of Dreiner, Haber and +Martin. ## ii. Key results @@ -49,7 +50,7 @@ antisymmetric `ε` this gives `ψ_β = ψ^α ε_αβ = -ε_βα ψ^α`, minus th `Species.toDualAt_toDualAt`, `Species.toDualAt_eq_mapAt` and, in components, `Species.tensorBasis_repr_toDualAt`. - `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, - `T^…_ν… = T^…μ… g_μν`. + `T^…_ν… = g_νμ T^…μ…`. - `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an antisymmetric form on a kind with two labels, `LinearMap.IsAlt.schouten` for `formBilin`, and `Species.formMatrix_schouten` is the same in components, diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index e4dd9a8ccf..b7c0afb601 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -559,7 +559,7 @@ lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by tensor_regroup /-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_α'ρ δ_γ^α - ε_α'γ δ_ρ^α`. The -free lower index `α'` is the first index of each `ε`, the one that lowering contracts. -/ +free lower index `α'` is the first index of each `ε`. -/ lemma schouten_downL : {εL' | γ ρ ⊗ δL | α α' = εL' | α' ρ ⊗ δL' | γ α + -{εL' | α' γ ⊗ δL' | ρ α}ᵀ | α' γ ρ α}ᵀ := by tensor_regroup using Species.schouten_formTensor_deltaTensor (S := complexLorentz) downL From c13dff5e9afcd0fdacafda5eb065177276572812 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:52:02 +0000 Subject: [PATCH 128/242] refactor(Tensors): remove toDual_symm, toDualAt_pure, Color.bar_dual and other unused restatements Removed, none has a caller: - realLorentz.toTensor_basis_vector, realLorentz.toTensor_basis_coVector (oneIndexEquiv_basis) - Species.toDual_symm (rfl), Species.toDualAt_pure (mapAt_pure) - Color.bar_dual (rfl) - complexLorentz.pauliTensor_raise_lower - LinearMap.comp_eq_id_comm_of_finrank_eq - complexLorentz.tensorBasis_repr_formTensor_upL, _downL, _upR, _downR (instances of complexLorentz.tensorBasis_repr_formTensor, which the examples now use) - Species.inverse_eq_iff, Species.inverse_eq_right_inv, Species.inverse_eq_of_contr_eq_deltaTensor (Species.inverse_eq_left_inv stays) Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/FiniteDimensional.lean | 10 ++----- Physlib/Mathematics/Tensors/Conjugation.lean | 5 +--- Physlib/Mathematics/Tensors/Inverse.lean | 22 +--------------- Physlib/Mathematics/Tensors/Metric.lean | 13 ---------- .../Relativity/Bispinors/Pauli/Tensor.lean | 4 --- .../Tensors/ComplexTensor/Species.lean | 26 +++---------------- .../Tensors/RealTensor/Species.lean | 12 +-------- 7 files changed, 8 insertions(+), 84 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index 2429a2ff91..5017fc4b7e 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -21,8 +21,8 @@ between two spaces `V` and `W` of the same finite dimension: `g ∘ f = id` on ` ## ii. Key results -- `LinearMap.comp_eq_id_comm_of_finrank_eq` : `f ∘ g = id ↔ g ∘ f = id` between spaces of equal - finite dimension. +- `LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq` : `g ∘ f = id` gives `f ∘ g = id` between + spaces of equal finite dimension. - `LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq` : the same up to a nonzero scalar, `g (f v) = c • v` for all `v` gives `f (g w) = c • w` for all `w`. @@ -54,12 +54,6 @@ lemma LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq (h : finrank K V = finran (LinearMap.congr_fun hgf) <| (LinearMap.injective_iff_surjective_of_finrank_eq_finrank h).1 <| injective_of_comp_eq_id f g hgf -/-- `LinearMap.comp_eq_id_comm` between two spaces of equal finite dimension. -/ -lemma LinearMap.comp_eq_id_comm_of_finrank_eq (h : finrank K V = finrank K W) {f : V →ₗ[K] W} - {g : W →ₗ[K] V} : f ∘ₗ g = LinearMap.id ↔ g ∘ₗ f = LinearMap.id := - ⟨LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h.symm, - LinearMap.comp_eq_id_of_comp_eq_id_of_finrank_eq h⟩ - section Field variable {K V W : Type*} [Field K] [AddCommGroup V] [Module K V] [AddCommGroup W] [Module K W] diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 302e59f560..2d1c2cadec 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -32,7 +32,7 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde ## ii. Key results -- `Color.bar` is the conjugate color, with `Color.bar_bar` and `Color.bar_dual`. +- `Color.bar` is the conjugate color, with `Color.bar_bar`. - `Species.HasConjugation` gives the conjugation of a species with its two conditions, and `Species.conjEquiv` is the conjugate of a vector, with its properties `Species.conjEquiv_conjEquiv`, `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. @@ -83,9 +83,6 @@ abbrev Color.bar (c : Color K) : Color K := ⟨star c.kind, c.variance⟩ lemma Color.bar_bar (c : Color K) : c.bar.bar = c := by simp [Color.bar] -/-- Conjugation commutes with duality, by `rfl`. -/ -lemma Color.bar_dual (c : Color K) : c.dual.bar = c.bar.dual := rfl - /-- If `b` is the conjugate of `a`, then `a.dual` is the conjugate of `b.dual`. This is the color equation of the inverse of a form in `V a ⊗ V b`. -/ lemma Color.dual_eq_bar_dual {a b : Color K} (hb : b = a.bar) : a.dual = b.dual.bar := by diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index b294edf818..8fbebcbc85 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -28,7 +28,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual linear map. - `Species.contr_mul_inverse` and `Species.contr_inverse_mul` prove `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`. -- `Species.inverse_eq_iff` characterises the inverse by its linear map, and +- `Species.inverse_eq_left_inv` characterises the inverse as a left inverse, and `Species.inverse_inverse` proves `(g⁻¹)⁻¹ = g`. - `Species.inverse_map_rep` proves `(Λg)⁻¹ = Λ(g⁻¹)`, and `Species.inverse_invariant` that the inverse of an invariant tensor is invariant. @@ -119,32 +119,12 @@ lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi S.deltaTensor b := (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 (comp_toLin_inverse g h) -/-- `N` is the inverse of `g` exactly when its linear map is the inverse of that of `g`. -/ -lemma inverse_eq_iff (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) - (N : S.V b.dual ⊗[k] S.V a.dual) : - S.inverse g = N ↔ S.toLin N = (LinearEquiv.ofBijective _ h).symm.toLinearMap := by - rw [← toLin_inverse g h, S.toLin.injective.eq_iff, eq_comm] - /-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `g`. -/ lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : S.inverse g = N := S.toLin.injective <| (LinearMap.cancel_right h.2).1 <| by rw [toLin_inverse_comp g h, hN] -/-- A tensor `N` with `g ∘ N = id`, as linear maps, is the inverse of `g`. -/ -lemma inverse_eq_right_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) - {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin g ∘ₗ S.toLin N = LinearMap.id) : - S.inverse g = N := - S.toLin.injective <| (LinearMap.cancel_left h.1).1 <| by rw [comp_toLin_inverse g h, hN] - -/-- A tensor `N` with `g_μν N^νρ = δ_μ^ρ` is the inverse of `g`. -/ -lemma inverse_eq_of_contr_eq_deltaTensor (g : S.V a ⊗[k] S.V b) - (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} - (hN : Tensor.contr (ColorEquiv.matMul a b a.dual) - (S.tmulEquiv _ _ (S.twoIndexEquiv a b g ⊗ₜ S.twoIndexEquiv _ _ N)) = - S.deltaTensor a.dual) : S.inverse g = N := - inverse_eq_left_inv g h ((Tensor.contr_matMul_eq_deltaTensor_iff g N).1 hN) - /-- The inverse of the inverse is the tensor itself, `(g⁻¹)⁻¹ = g`. -/ lemma inverse_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : S.inverse (S.inverse g) = g := diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 801d7415b7..a59640b9e0 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -251,9 +251,6 @@ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.r lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : S.toDual c v = S.toLin (TensorProduct.comm k _ _ (form c.dual)) v := rfl -/-- Raising is the inverse of lowering. The inverse of `S.toDual c` is `S.toDual c.dual`. -/ -lemma toDual_symm (c : Color K) [S.HasForm c.kind] : (S.toDual c).symm = S.toDual c.dual := rfl - /-- Raising undoes lowering, `(v_ν)^ν = v^ν`. -/ @[simp] lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : @@ -321,16 +318,6 @@ noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : (S.tensorRep c).Equiv (S.tensorRep c') := S.congrAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)) h -/-- On a pure tensor, `toDualAt` applies `toDual` to the vector at `i` and keeps the others. The -result `q` is explicit, so that for concrete colors `hqi` and `hq` hold by `rfl`. -/ -lemma toDualAt_pure {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) - [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) - (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) - (hqi : q i = S.cast hi.symm (S.toDual a (S.cast ha (p i)))) - (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : - S.toDualAt i a ha hi h (S.pure p) = S.pure q := - mapAt_pure i _ h p q hqi hq - /-- Raising at an index undoes lowering at that index, `(T^μ_ν)^ν = T^μν`. -/ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 39771976da..f0c7768498 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -208,10 +208,6 @@ lemma vectorToBispinor_bispinorToVector (X : ℂT[upL, upR, upL, upR]) : (by rw [Species.finrank_tensor, Species.finrank_tensor]; rfl) two_ne_zero bispinorToVector_vectorToBispinor X -/-- Raising both Weyl indices of `σT^μ_{αβ̇}` gives `σT^{μαβ̇}`. -/ -lemma pauliTensor_raise_lower : {{pauliTensor | μ τ(α) τ(β)}ᵀ | μ τ(α) τ(β)}ᵀ = pauliTensor := by - tensor_indices - /-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is `vectorToBispinor σT = vectorToBispinor (bispinorToVector (εL ⊗ εR)) / 2`. -/ lemma pauliCo_contr_pauliContr : diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index b7c0afb601..53aaf45105 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -448,26 +448,6 @@ lemma tensorBasis_repr_formTensor (c : Color Kind) (μ : Π i, Idx (![c, c] i).k formEntries c (μ 0) (μ 1) := (Species.tensorBasis_repr_formTensor c μ).trans (formMatrix_apply c _ _) -/-- The components of `ε^αβ` are the matrix `!![0, 1; -1, 0]`. -/ -lemma tensorBasis_repr_formTensor_upL (μ : Π i, Idx (![upL, upL] i).kind) : - (complexLorentz.tensorBasis ![upL, upL]).repr εL μ = metricRaw (μ 0) (μ 1) := - tensorBasis_repr_formTensor upL μ - -/-- The components of `ε_αβ` are the matrix `!![0, -1; 1, 0]`. -/ -lemma tensorBasis_repr_formTensor_downL (μ : Π i, Idx (![downL, downL] i).kind) : - (complexLorentz.tensorBasis ![downL, downL]).repr εL' μ = (-metricRaw) (μ 0) (μ 1) := - tensorBasis_repr_formTensor downL μ - -/-- The components of `ε^α̇β̇` are the matrix `!![0, 1; -1, 0]`. -/ -lemma tensorBasis_repr_formTensor_upR (μ : Π i, Idx (![upR, upR] i).kind) : - (complexLorentz.tensorBasis ![upR, upR]).repr εR μ = metricRaw (μ 0) (μ 1) := - tensorBasis_repr_formTensor upR μ - -/-- The components of `ε_α̇β̇` are the matrix `!![0, -1; 1, 0]`. -/ -lemma tensorBasis_repr_formTensor_downR (μ : Π i, Idx (![downR, downR] i).kind) : - (complexLorentz.tensorBasis ![downR, downR]).repr εR' μ = (-metricRaw) (μ 0) (μ 1) := - tensorBasis_repr_formTensor downR μ - /-- The components of `η^μν` and `η_μν` are the Minkowski matrix `diag(1, -1, -1, -1)`. -/ lemma tensorBasis_repr_formTensor_vec (v : Variance) (μ : Π i, Idx (![⟨vec, v⟩, ⟨vec, v⟩] i : Color Kind).kind) : @@ -664,7 +644,7 @@ lemma tensorBasis_repr_formTensor_upL_tmul_upR (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εR | α' β'}ᵀ μ = metricRaw (μ 0) (μ 1) * metricRaw (μ 2) (μ 3) := by rw [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, - tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] + tensorBasis_repr_formTensor upL, tensorBasis_repr_formTensor upR] rfl /-- `(ε^αβ ε_βγ)` at `α = γ = 0` is `1`, from the components of `εL` and `εL'`. -/ @@ -672,14 +652,14 @@ example : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εL' | β γ}ᵀ (Fin.cons (0 : Fin 2) (Fin.cons (0 : Fin 2) fun i => i.elim0)) = 1 := by rw [Species.Tensor.tensorBasis_repr_contr_one_pair] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, - tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor downL] + tensorBasis_repr_formTensor upL, tensorBasis_repr_formTensor downL] change ∑ β : Fin 2, metricRaw 0 β * (-metricRaw) β 0 = 1 simp [metricRaw, Fin.sum_univ_two] /-- The conjugate of `ε^αβ` is `ε^α̇β̇`. The conjugate lands on `![upR, upR]` directly. -/ example : Species.Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) - rw [tensorBasis_repr_conj, tensorBasis_repr_formTensor_upL, tensorBasis_repr_formTensor_upR] + rw [tensorBasis_repr_conj, tensorBasis_repr_formTensor upL, tensorBasis_repr_formTensor upR] -- The labels are `idxBar` along `rfl` of `μ 0` and `μ 1`, the identity. have h (a b : Fin 2) : star (metricRaw a b) = metricRaw a b := by fin_cases a <;> fin_cases b <;> simp [metricRaw] diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 54b2e430b0..2ae0acf410 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -55,7 +55,7 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ - `realLorentz.tensorBasis_repr_toTensor_vector` and `realLorentz.tensorBasis_repr_toTensor_coVector` : the components of the tensor of a vector or a covector are its components, and `realLorentz.toTensor_symm_vector_apply` : the converse for a - vector. `realLorentz.toTensor_basis_vector` : a basis vector is a basis tensor. + vector. - `realLorentz.tensorBasis_repr_toTensor_vector_tmul` and `realLorentz.tensorBasis_repr_toTensor_coVector_tmul` : the components of the tensor of an element of `Vector d ⊗ Vector d` or `CoVector d ⊗ Vector d` are its coordinates in the product @@ -362,16 +362,6 @@ lemma tensorBasis_repr_toTensor_coVector (d : ℕ) (ω : CoVector d) (μ : Fin 1 ((realLorentz d).tensorBasis ![down]).repr (Tensorial.toTensor ω) μ = ω (μ 0) := Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := realLorentz d) down ω μ -/-- The tensor of the basis vector `e_μ` is the basis tensor with label `μ`. -/ -lemma toTensor_basis_vector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : - Tensorial.toTensor (Vector.basis μ) = (realLorentz d).tensorBasis ![up] fun _ => μ := - Species.oneIndexEquiv_basis (S := realLorentz d) up fun _ => μ - -/-- The tensor of the basis covector `e^μ` is the basis tensor with label `μ`. -/ -lemma toTensor_basis_coVector (d : ℕ) (μ : Fin 1 ⊕ Fin d) : - Tensorial.toTensor (CoVector.basis μ) = (realLorentz d).tensorBasis ![down] fun _ => μ := - Species.oneIndexEquiv_basis (S := realLorentz d) down fun _ => μ - /-- The components `F^μν` of the tensor of `F` in `Vector d ⊗ Vector d` are its coordinates in Mathlib's product basis `Basis.tensorProduct`. -/ lemma tensorBasis_repr_toTensor_vector_tmul (d : ℕ) (F : Vector d ⊗[ℝ] Vector d) From e3fecdc11592511f7d7b0f2b6fe83bc3c1785207 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:54:42 +0000 Subject: [PATCH 129/242] refactor(Tensors): declare contrColors, ColorEquiv.noPairs, SamePair, chain, tmulRight, tmulLeft, matMul, matMulAt and barContr in namespace ColorEquiv The species-free choices of pairs are declared at the top of the file that first needs them, in namespace Tensor.ColorEquiv, instead of with _root_ from inside Species.Tensor. Contraction and LinearMap gain a section A for them. Fully qualified names are unchanged. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 11 +- Physlib/Mathematics/Tensors/Contraction.lean | 240 ++++++++++--------- Physlib/Mathematics/Tensors/LinearMap.lean | 107 +++++---- 3 files changed, 193 insertions(+), 165 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 2d1c2cadec..1c8fb00077 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -89,6 +89,12 @@ lemma Color.dual_eq_bar_dual {a b : Color K} (hb : b = a.bar) : a.dual = b.dual. subst hb simp [Color.bar] +/-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of indices. -/ +def ColorEquiv.barContr {ι κ J : Type} {c : ι → Color K} {b : κ → Color K} {a : J → Color K} + (σ : ColorEquiv c (contrColors b a)) : + ColorEquiv (fun i => (c i).bar) (contrColors (fun x => (b x).bar) fun j => (a j).bar) := + ⟨σ.toEquiv, fun j => by rcases j with _ | _ | _ <;> exact congrArg Color.bar (σ.color_eq _)⟩ + /-! ## B. Species with a conjugation @@ -290,11 +296,6 @@ lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : variable {κ J : Type} [Fintype J] {b : κ → Color K} {a : J → Color K} -/-- The pairs `σ` of `T`, as pairs of its conjugate `T̄`, with the same bijection of indices. -/ -def _root_.Tensor.ColorEquiv.barContr {c : ι → Color K} (σ : ColorEquiv c (contrColors b a)) : - ColorEquiv (fun i => (c i).bar) (contrColors (fun x => (b x).bar) fun j => (a j).bar) := - ⟨σ.toEquiv, fun j => by rcases j with _ | _ | _ <;> exact congrArg Color.bar (σ.color_eq _)⟩ - /-- Conjugating a contraction is contracting the conjugate along the same pairs of indices, `(T^ρμ_μ)‾ = T̄^ρμ_μ`. -/ lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index b957ea3cfe..9cd002837a 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -72,14 +72,15 @@ since the bases of a color and its dual are dual. ## iii. Table of contents -- A. Contraction in the standard arrangement of indices -- B. Contraction of chosen pairs of indices - - B.1. Relabeling the indices - - B.2. Contractions of the same pairs of indices - - B.3. Symmetric against antisymmetric - - B.4. Two contractions are one - - B.5. Products with a tensor that is not contracted -- C. Components +- A. Choices of pairs of indices +- B. Contraction in the standard arrangement of indices +- C. Contraction of chosen pairs of indices + - C.1. Relabeling the indices + - C.2. Contractions of the same pairs of indices + - C.3. Symmetric against antisymmetric + - C.4. Two contractions are one + - C.5. Products with a tensor that is not contracted +- D. Components ## iv. References @@ -93,6 +94,116 @@ open TensorProduct PiTensorProduct namespace Tensor +/-! + +## A. Choices of pairs of indices + +-/ + +/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of indices. The free index `inl x` has +the color `c x`, and the two indices `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors +`a j` and `(a j).dual`. -/ +noncomputable abbrev contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : + κ ⊕ (J ⊕ J) → Color K := + prodColors c (prodColors a fun j => (a j).dual) + +namespace ColorEquiv + +variable {K : Type} {ι₁ ι₂ κ κ₁ J J' J₁ J₂ : Type} + +/-- The relabeling of a contraction with no pairs, where every index is free and stays in place. -/ +def noPairs (c : ι₁ → Color K) : + ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where + toEquiv.toFun := .inl + toEquiv.invFun := Sum.elim id (Sum.elim Empty.elim Empty.elim) + toEquiv.left_inv _ := rfl + toEquiv.right_inv := by rintro (x | e | e) <;> first | rfl | exact e.elim + color_eq := by rintro (x | e | e) <;> first | rfl | exact e.elim + +variable {c : ι₁ → Color K} {c' : κ → Color K} + +/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two indices, in either order. -/ +def SamePair {a : J → Color K} {a' : J' → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) + (j' : J') : Prop := + (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inl j')) ∧ + σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inr j'))) ∨ + (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inr j')) ∧ + σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inl j'))) + +instance [DecidableEq ι₁] {a : J → Color K} {a' : J' → Color K} + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) + (j' : J') : Decidable (σ.SamePair τ j j') := by + unfold SamePair + infer_instance + +/-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ +lemma SamePair.left_unique {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j₁ j₂ : J} + {j' : J'} (h₁ : σ.SamePair τ j₁ j') (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by + have hσ := σ.toEquiv.symm.injective + rcases h₁ with ⟨h₁, h₁'⟩ | ⟨h₁, h₁'⟩ <;> rcases h₂ with ⟨h₂, h₂'⟩ | ⟨h₂, h₂'⟩ + · simpa using hσ (h₁.trans h₂.symm) + · simpa using hσ (h₁'.trans h₂.symm) + · simpa using hσ (h₁.trans h₂'.symm) + · simpa using hσ (h₁.trans h₂.symm) + +/-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ +lemma SamePair.right_unique {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j : J} + {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by + have hτ := τ.toEquiv.symm.injective + rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> + simpa using hτ (h₁.symm.trans h₂) + +/-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair +of `τ` is a pair of `σ`. The indices of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ +lemma exists_samePair_left {a : J → Color K} {a' : J' → Color K} + {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} + (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by + have hτ := τ.toEquiv.symm.injective + rcases hs : σ.toEquiv (τ.toEquiv.symm (.inr (.inl j'))) with x | j | j <;> + rw [← Equiv.eq_symm_apply] at hs + · rw [hfree] at hs + have := hτ hs + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · obtain rfl : j'' = j' := by simpa using hτ (h.1.symm.trans hs.symm) + exact ⟨j, .inl h⟩ + · have := hτ (h.1.symm.trans hs.symm) + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · have := hτ (h.2.symm.trans hs.symm) + simp at this + · obtain rfl : j'' = j' := by simpa using hτ (h.2.symm.trans hs.symm) + exact ⟨j, .inr h⟩ + +/-- The pairs of two contractions in sequence, `σ₁` and then `σ₂`, as one choice of pairs +`J₁ ⊕ J₂`, the pairs `J₁` of `σ₁` followed by the pairs `J₂` of `σ₂`. -/ +noncomputable def chain {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} + (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) : + ColorEquiv c (contrColors c' (prodColors a₁ a₂)) := + σ₁.trans ((σ₂.sumCongr (.refl _)).trans ⟨(Equiv.sumAssoc _ _ _).trans + (Equiv.sumCongr (Equiv.refl _) ((Equiv.sumComm _ _).trans (Equiv.sumSumSumComm _ _ _ _))), + by rintro (_ | (_ | _) | (_ | _)) <;> rfl⟩) + +/-- The pairs `σ` of `T`, as pairs of `T ⊗ U` for a factor `U` on the right, with colors `d`. The +indices of `U` are free and come after the free indices of `T`. -/ +noncomputable def tmulRight {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) + (d : ι₂ → Color K) : + ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := + (σ.sumCongr (.refl d)).trans (swapLast c' d _) + +/-- The pairs `σ` of `T`, as pairs of `U ⊗ T` for a factor `U` on the left, with colors `d`. The +indices of `U` are free and come before the free indices of `T`. -/ +noncomputable def tmulLeft {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) + (d : ι₂ → Color K) : + ColorEquiv (prodColors d c) (contrColors (prodColors d c') a) := + ((refl d).sumCongr σ).trans (sumAssoc _ _ _).symm + +end ColorEquiv + namespace Species variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} @@ -104,17 +215,10 @@ namespace Tensor /-! -## A. Contraction in the standard arrangement of indices +## B. Contraction in the standard arrangement of indices -/ -/-- The colors of the standard arrangement `κ ⊕ (J ⊕ J)` of indices. The free index `inl x` has -the color `c x`, and the two indices `inr (inl j)` and `inr (inr j)` of the pair `j` have the colors -`a j` and `(a j).dual`. -/ -noncomputable abbrev _root_.Tensor.contrColors {K κ J : Type} (c : κ → Color K) (a : J → Color K) : - κ ⊕ (J ⊕ J) → Color K := - prodColors c (prodColors a fun j => (a j).dual) - /-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the index `inr (inl j)` is contracted with the index `inr (inr j)` by `S.contr (a j)`, with the index `inr (inl j)` as its first argument. The indices `inl x` are the free indices, with the colors @@ -147,7 +251,7 @@ lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) /-! -## B. Contraction of chosen pairs of indices +## C. Contraction of chosen pairs of indices -/ @@ -174,15 +278,6 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) -/-- The relabeling of a contraction with no pairs, where every index is free and stays in place. -/ -def _root_.Tensor.ColorEquiv.noPairs (c : ι₁ → Color K) : - ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where - toEquiv.toFun := .inl - toEquiv.invFun := Sum.elim id (Sum.elim Empty.elim Empty.elim) - toEquiv.left_inv _ := rfl - toEquiv.right_inv := by rintro (x | e | e) <;> first | rfl | exact e.elim - color_eq := by rintro (x | e | e) <;> first | rfl | exact e.elim - /-- A contraction with no pairs is the identity. Any tensor is thus a contraction, as needed to compare it with one. -/ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := @@ -192,7 +287,7 @@ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := /-! -### B.1. Relabeling the indices +### C.1. Relabeling the indices -/ @@ -216,67 +311,10 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → /-! -### B.2. Contractions of the same pairs of indices +### C.2. Contractions of the same pairs of indices -/ -/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two indices, in either order. -/ -def _root_.Tensor.ColorEquiv.SamePair {a : J → Color K} {a' : J' → Color K} - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) - (j' : J') : Prop := - (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inl j')) ∧ - σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inr j'))) ∨ - (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inr j')) ∧ - σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inl j'))) - -instance [DecidableEq ι₁] {a : J → Color K} {a' : J' → Color K} - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) - (j' : J') : Decidable (σ.SamePair τ j j') := by - unfold ColorEquiv.SamePair - infer_instance - -/-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ -lemma _root_.Tensor.ColorEquiv.SamePair.left_unique {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j₁ j₂ : J} - {j' : J'} (h₁ : σ.SamePair τ j₁ j') (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by - have hσ := σ.toEquiv.symm.injective - rcases h₁ with ⟨h₁, h₁'⟩ | ⟨h₁, h₁'⟩ <;> rcases h₂ with ⟨h₂, h₂'⟩ | ⟨h₂, h₂'⟩ - · simpa using hσ (h₁.trans h₂.symm) - · simpa using hσ (h₁'.trans h₂.symm) - · simpa using hσ (h₁.trans h₂'.symm) - · simpa using hσ (h₁.trans h₂.symm) - -/-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ -lemma _root_.Tensor.ColorEquiv.SamePair.right_unique {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j : J} - {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by - have hτ := τ.toEquiv.symm.injective - rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> - simpa using hτ (h₁.symm.trans h₂) - -/-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair -of `τ` is a pair of `σ`. The indices of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ -lemma _root_.Tensor.ColorEquiv.exists_samePair_left {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} - (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) - (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by - have hτ := τ.toEquiv.symm.injective - rcases hs : σ.toEquiv (τ.toEquiv.symm (.inr (.inl j'))) with x | j | j <;> - rw [← Equiv.eq_symm_apply] at hs - · rw [hfree] at hs - have := hτ hs - simp at this - · obtain ⟨j'', h | h⟩ := h₁ j - · obtain rfl : j'' = j' := by simpa using hτ (h.1.symm.trans hs.symm) - exact ⟨j, .inl h⟩ - · have := hτ (h.1.symm.trans hs.symm) - simp at this - · obtain ⟨j'', h | h⟩ := h₁ j - · have := hτ (h.2.symm.trans hs.symm) - simp at this - · obtain rfl : j'' = j' := by simpa using hτ (h.2.symm.trans hs.symm) - exact ⟨j, .inr h⟩ - /-- Two contractions are equal when they keep the same free indices in the same order and contract the same pairs of indices, in any order and with either index of a pair first. On literal indices the hypotheses are checked by evaluation. -/ @@ -299,7 +337,7 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' /-! -### B.3. Symmetric against antisymmetric +### C.3. Symmetric against antisymmetric -/ @@ -339,20 +377,10 @@ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] /-! -### B.4. Two contractions are one +### C.4. Two contractions are one -/ -/-- The pairs of two contractions in sequence, `σ₁` and then `σ₂`, as one choice of pairs -`J₁ ⊕ J₂`, the pairs `J₁` of `σ₁` followed by the pairs `J₂` of `σ₂`. -/ -noncomputable def _root_.Tensor.ColorEquiv.chain {c : ι₁ → Color K} {c₁ : κ₁ → Color K} - {c' : κ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} - (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) : - ColorEquiv c (contrColors c' (prodColors a₁ a₂)) := - σ₁.trans ((σ₂.sumCongr (.refl _)).trans ⟨(Equiv.sumAssoc _ _ _).trans - (Equiv.sumCongr (Equiv.refl _) ((Equiv.sumComm _ _).trans (Equiv.sumSumSumComm _ _ _ _))), - by rintro (_ | (_ | _) | (_ | _)) <;> rfl⟩) - /-- Two contractions in sequence are one contraction, along `σ₁.chain σ₂`. Contracting `μ` and then `ν` in `T^μ_μ^ν_ν` is contracting both pairs at once. -/ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} @@ -366,17 +394,10 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ /-! -### B.5. Products with a tensor that is not contracted +### C.5. Products with a tensor that is not contracted -/ -/-- The pairs `σ` of `T`, as pairs of `T ⊗ U` for a factor `U` on the right, with colors `d`. The -indices of `U` are free and come after the free indices of `T`. -/ -noncomputable def _root_.Tensor.ColorEquiv.tmulRight {c : ι₁ → Color K} {c' : κ → Color K} - {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : - ColorEquiv (prodColors c d) (contrColors (prodColors c' d) a) := - (σ.sumCongr (.refl d)).trans (ColorEquiv.swapLast c' d _) - /-- Contracting indices of the left factor commutes with the product, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color K} @@ -392,13 +413,6 @@ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color congr 1 exact congrArg S.pure (funext fun y => by rcases y with x | m <;> rfl)) (t ⊗ₜ u) -/-- The pairs `σ` of `T`, as pairs of `U ⊗ T` for a factor `U` on the left, with colors `d`. The -indices of `U` are free and come before the free indices of `T`. -/ -noncomputable def _root_.Tensor.ColorEquiv.tmulLeft {c : ι₁ → Color K} - {c' : κ → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (d : ι₂ → Color K) : - ColorEquiv (prodColors d c) (contrColors (prodColors d c') a) := - ((ColorEquiv.refl d).sumCongr σ).trans (ColorEquiv.sumAssoc _ _ _).symm - /-- Contracting indices of the right factor commutes with the product, `U^ρ (T^μ_μν) = (U ⊗ T)^ρμ_μν`. -/ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color K} @@ -416,7 +430,7 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color /-! -## C. Components +## D. Components -/ diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 3f5053df13..03e96899c6 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -57,9 +57,10 @@ identity. ## iii. Table of contents -- A. Tensors of two factors as linear maps -- B. The delta -- C. Products with a tensor on two indices +- A. Choices of pairs for products with a tensor on two indices +- B. Tensors of two factors as linear maps +- C. The delta +- D. Products with a tensor on two indices ## iv. References @@ -73,6 +74,59 @@ open TensorProduct namespace Tensor +/-! + +## A. Choices of pairs for products with a tensor on two indices + +-/ + +namespace ColorEquiv + +variable {K ι₁ : Type} + +/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the +second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are +`0` and `1`. -/ +def matMul (a c b : Color K) : + ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where + toEquiv.toFun + | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 + toEquiv.invFun + | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 + toEquiv.left_inv := by decide + toEquiv.right_inv := by decide + +/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a +tensor `F` on two indices and puts the second index of `F` in the place of `i`. It is +`matMul` at any index of a tensor on any index type. -/ +def matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) + (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : + ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where + toEquiv := + { toFun := Sum.elim (fun x => if x = i then .inr (.inl ()) else .inl x) + (fun j => if j = 0 then .inr (.inr ()) else .inl i) + invFun := Sum.elim (fun x => if x = i then .inr 1 else .inl x) + (Sum.elim (fun _ => .inl i) fun _ => .inr 0) + left_inv := by + rintro (x | j) + · by_cases hx : x = i <;> simp [hx] + · fin_cases j <;> simp + right_inv := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i <;> simp [hx] + · simp + · simp } + color_eq := by + rintro (x | (⟨⟩ | ⟨⟩)) + · by_cases hx : x = i + · subst hx + simp [hi] + · simp [hx, h x hx] + · simp [ha] + · simp + +end ColorEquiv + namespace Species variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} @@ -80,7 +134,7 @@ variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → /-! -## A. Tensors of two factors as linear maps +## B. Tensors of two factors as linear maps -/ @@ -129,7 +183,7 @@ lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : /-! -## B. The delta +## C. The delta -/ @@ -182,22 +236,10 @@ variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} /-! -## C. Products with a tensor on two indices +## D. Products with a tensor on two indices -/ -/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the -second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are -`0` and `1`. -/ -def _root_.Tensor.ColorEquiv.matMul (a c b : Color K) : - ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where - toEquiv.toFun - | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 - toEquiv.invFun - | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 - toEquiv.left_inv := by decide - toEquiv.right_inv := by decide - /-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two indices is `M` with its second factor mapped by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : @@ -229,35 +271,6 @@ lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, Module.Basis.coordTensorHomEquiv_coordDelta] -/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a -tensor `F` on two indices and puts the second index of `F` in the place of `i`. It is -`ColorEquiv.matMul` at any index of a tensor on any index type. -/ -def _root_.Tensor.ColorEquiv.matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) - (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : - ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where - toEquiv := - { toFun := Sum.elim (fun x => if x = i then .inr (.inl ()) else .inl x) - (fun j => if j = 0 then .inr (.inr ()) else .inl i) - invFun := Sum.elim (fun x => if x = i then .inr 1 else .inl x) - (Sum.elim (fun _ => .inl i) fun _ => .inr 0) - left_inv := by - rintro (x | j) - · by_cases hx : x = i <;> simp [hx] - · fin_cases j <;> simp - right_inv := by - rintro (x | (⟨⟩ | ⟨⟩)) - · by_cases hx : x = i <;> simp [hx] - · simp - · simp } - color_eq := by - rintro (x | (⟨⟩ | ⟨⟩)) - · by_cases hx : x = i - · subst hx - simp [hi] - · simp [hx, h x hx] - · simp [ha] - · simp - /-- On a pure tensor, contracting the index `i` with the first index of `F` applies `F`, read as a linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other indices are those of `p`, moved along `h`. -/ From b56bf0ae6d26df411dfab52beb858f8340c19b0b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:55:20 +0000 Subject: [PATCH 130/242] refactor(Tensors): sesqForm_inverse_toLin, isSymm_sesqForm_inverse and isPosDef_sesqForm_inverse take hb explicitly hb comes before x y, and the form of the inverse is stated at Color.dual_eq_bar_dual hb instead of taking ha. isPosDef_sesqForm_inverse_of_bijective, used once, is inlined into isPosDef_sesqForm_inverse. HermitianForm has one namespace block. Co-authored-by: Claude Opus 5.5 --- .../Mathematics/Tensors/HermitianForm.lean | 63 +++++++------------ 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 965ef672dd..432e1de8ae 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -80,25 +80,29 @@ lemma sesqForm_apply (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (x y : S.V a.dual `g^{J̄I} (g y)_J̄ (g x)‾_I = (g_{IJ̄} x^I ȳ^J̄)*`. The inverse `g^{J̄I}` acts on barred covectors, so its form is the conjugate of the usual `g^{IJ̄} w_I w̄'_J̄`. -/ lemma sesqForm_inverse_toLin (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) - (x y : S.V a.dual) (hb : b = a.bar := by rfl) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - S.sesqForm (S.inverse g) ha (S.toLin g x) (S.toLin g y) = star (S.sesqForm g hb y x) := by + (hb : b = a.bar) (x y : S.V a.dual) : + S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb) (S.toLin g x) (S.toLin g y) = + star (S.sesqForm g hb y x) := by rw [sesqForm_apply, sesqForm_apply, toLin_inverse g h, LinearEquiv.coe_coe, - LinearEquiv.ofBijective_symm_apply_apply, ← S.contr_tmul_symm, star_contr_conjEquiv ha, - conjEquiv_conjEquiv] + LinearEquiv.ofBijective_symm_apply_apply, ← S.contr_tmul_symm, + star_contr_conjEquiv (Color.dual_eq_bar_dual hb), conjEquiv_conjEquiv] /-- The inverse of a Hermitian `g_{IJ̄}` is Hermitian. -/ -lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsSymm := by +lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) + (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) : + (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsSymm := by refine ⟨fun u w => ?_⟩ obtain ⟨x, rfl⟩ := h.2 u obtain ⟨y, rfl⟩ := h.2 w - rw [sesqForm_inverse_toLin g h _ _ hb ha, sesqForm_inverse_toLin g h _ _ hb ha, - starRingEnd_apply, star_star] + rw [sesqForm_inverse_toLin g h hb, sesqForm_inverse_toLin g h hb, starRingEnd_apply, star_star] exact (hg.eq x y).symm +/-! + +## B. Positive-definite forms are invertible + +-/ + /-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives `g(y, y) = 0`. -/ lemma toLin_injective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} @@ -108,31 +112,6 @@ lemma toLin_injective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} by_contra hne simpa [sesqForm_apply, hy] using hg.pos y hne -/-- The inverse of a positive-definite `g_{IJ̄}` with bijective linear map is positive-definite. -/ -lemma isPosDef_sesqForm_inverse_of_bijective (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsPosDef) - (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsPosDef := by - refine ⟨isSymm_sesqForm_inverse g h hg.isSymm ha, fun u hu => ?_⟩ - obtain ⟨x, rfl⟩ := h.2 u - rw [sesqForm_inverse_toLin g h _ _ hb ha, ← starRingEnd_apply, hg.isSymm.eq x x] - exact hg.pos x fun hx => hu (by rw [hx, map_zero]) - -end Species - -/-! - -## B. Positive-definite forms are invertible - --/ - -namespace Species - -variable {K : Type} [InvolutiveStar K] {k : Type} [RCLike k] {G : Type} - [Group G] {basisIdx : K → Type} - [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] - {S : Species k G K basisIdx} [S.HasConjugation] {a b : Color K} - /-- A positive-definite `g_{IJ̄}` is invertible. Its linear map `y ↦ g y` is bijective. -/ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Bijective (S.toLin g) := by @@ -143,10 +122,14 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} Module.finrank_eq_card_basis (S.basis b)] /-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ -lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} - (hg : (S.sesqForm g hb).IsPosDef) (ha : a.dual = b.dual.bar := Color.dual_eq_bar_dual hb) : - (S.sesqForm (S.inverse g) ha).IsPosDef := - isPosDef_sesqForm_inverse_of_bijective g (toLin_bijective_of_isPosDef g hg) hg ha +lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) + (hg : (S.sesqForm g hb).IsPosDef) : + (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsPosDef := by + have h := toLin_bijective_of_isPosDef g hg + refine ⟨isSymm_sesqForm_inverse g hb h hg.isSymm, fun u hu => ?_⟩ + obtain ⟨x, rfl⟩ := h.2 u + rw [sesqForm_inverse_toLin g h hb, ← starRingEnd_apply, hg.isSymm.eq x x] + exact hg.pos x fun hx => hu (by rw [hx, map_zero]) end Species From af35efa3d61a3a562adb971e1a4630bc61480260 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 12:03:08 +0000 Subject: [PATCH 131/242] doc(Tensors): reindexing in prose, Pauli conventions, contr_toLin_comm to LinearMap, formMatrix_schouten' inlined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docs: - "relabeling" becomes "reindexing" in the prose of Reindexing, Product, Contraction, LinearMap and Tensor, matching Species.reindex and PiTensorProduct.reindex. - Stale or vague docstrings fixed in Product (tmulEquiv, tmulEquiv_pure, tmulEquiv_symm_pure), Contraction, Tensor (mapAt_pure, tensorBasis_repr_mapAt), Inverse (contr_formTensor_formTensor), CoordTensorHom (coordTensorHomEquiv), Conjugation (title) and the complex and real species overviews. - Pauli/Tensor states the Dreiner-Haber-Martin conventions, the index-height dictionary at the Fierz identity, the matrix-product convention of the anticommutators, and the PauliMatrix trace identities it reproduces. tensorBasis_repr_pauliTensor_downAll has a clearer docstring. - Every module without references says so in one sentence. - Docstrings for Lorentz.Vector.tensorComponent_apply and Lorentz.Vector.tensorComponent_tmul. Tidies: - Species.contr_toLin_comm moves from Metric to LinearMap. - Species.formMatrix_schouten' is removed, inlined into its one caller Species.schouten_formTensor_deltaTensor_dual. - contr_reindex and reindex_contr no longer shadow the variables ι₂ and κ'. - A non-terminal simp in coordPairing_complexVector_coVector, a redundant open Matrix, and Type* in Representation. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 3 +- .../ForMathlib/FiniteDimensional.lean | 2 + .../ForMathlib/Representation.lean | 4 +- Physlib/Mathematics/ForMathlib/Schouten.lean | 2 + .../ForMathlib/SemilinearMaps.lean | 2 +- Physlib/Mathematics/Tensors/Conjugation.lean | 4 +- Physlib/Mathematics/Tensors/Contraction.lean | 54 +++++++++---------- .../Mathematics/Tensors/HermitianForm.lean | 2 +- Physlib/Mathematics/Tensors/Inverse.lean | 6 +-- Physlib/Mathematics/Tensors/LinearMap.lean | 16 +++++- Physlib/Mathematics/Tensors/Metric.lean | 25 +-------- Physlib/Mathematics/Tensors/Product.lean | 51 +++++++++--------- Physlib/Mathematics/Tensors/Reindexing.lean | 50 ++++++++--------- Physlib/Mathematics/Tensors/Schouten.lean | 9 +++- Physlib/Mathematics/Tensors/Tensor.lean | 14 +++-- Physlib/Mathematics/Tensors/Tensorial.lean | 2 + .../Relativity/Bispinors/Pauli/Tensor.lean | 36 +++++++++---- .../Tensors/ComplexTensor/Species.lean | 22 ++++---- .../Tensors/ComplexTensor/Vector/Basic.lean | 2 +- .../Tensors/RealTensor/Species.lean | 24 +++++---- 20 files changed, 174 insertions(+), 156 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index a122ed78a9..6ccef265b6 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -64,8 +64,7 @@ variable {R ι M N U U' : Type*} [CommRing R] [Fintype ι] [DecidableEq ι] [Add [Module R U'] /-- `M ⊗ U` as the linear maps `N → U`, reading `M` as the dual of `N` through the coordinate -pairing. It sends `x ⊗ y` to `v ↦ b'.coordPairing b (v ⊗ x) • y`. It is Mathlib's -`dualTensorHomEquivOfBasis`, after the coordinate pairing identifies `M` with the dual of `N`. -/ +pairing. It sends `x ⊗ y` to `v ↦ b'.coordPairing b (v ⊗ x) • y`. -/ noncomputable def coordTensorHomEquiv (b : Module.Basis ι R M) (b' : Module.Basis ι R N) : M ⊗[R] U ≃ₗ[R] (N →ₗ[R] U) := (TensorProduct.congr (TensorProduct.curry (b'.coordPairing b)).flip.toPerfPair diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index 5017fc4b7e..e08721ad99 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -32,6 +32,8 @@ between two spaces `V` and `W` of the same finite dimension: `g ∘ f = id` on ` ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 6216b27a90..6cc31a6ad7 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -39,13 +39,15 @@ given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the l ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section open TensorProduct -variable {k G V W U X : Type} [CommRing k] [Monoid G] [AddCommMonoid V] [Module k V] +variable {k G V W U X : Type*} [CommRing k] [Monoid G] [AddCommMonoid V] [Module k V] [AddCommMonoid W] [Module k W] [AddCommMonoid U] [Module k U] [AddCommMonoid X] [Module k X] {ρ : Representation k G V} {σ : Representation k G W} {τ : Representation k G U} {π : Representation k G X} diff --git a/Physlib/Mathematics/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean index 6dd41965b5..e41b9ffe9b 100644 --- a/Physlib/Mathematics/ForMathlib/Schouten.lean +++ b/Physlib/Mathematics/ForMathlib/Schouten.lean @@ -30,6 +30,8 @@ For an alternating bilinear form `B` on a module with a basis of two vectors, ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index 0f9f39ff8a..3e97c79d06 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -46,7 +46,7 @@ factors along the same labels conjugates the coordinate pairing of two bases. ## iv. References -None. +There are no known references for the material in this module. -/ diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 1c8fb00077..02e69a77ac 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -9,7 +9,7 @@ public import Physlib.Mathematics.ForMathlib.SemilinearMaps public import Physlib.Mathematics.Tensors.Contraction /-! -# Complex conjugation of tensors +# Conjugation of tensors The conjugate of a tensor conjugates its components and moves each index to the conjugate color. @@ -57,7 +57,7 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde ## iv. References -None. +There are no known references for the material in this module. -/ diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 9cd002837a..78619e0a67 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -17,15 +17,15 @@ The contraction of pairs of indices, an index with its dual in each pair, as in ## i. Overview A contraction pairs indices of dual colors and sums over their common label, as in the trace -`(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the indices `Fin 3`. On a pure tensor +`(tr T)^ρ = T^ρμ_μ` of a Lorentz tensor `T^ρμ_ν` on the index type `Fin 3`. On a pure tensor `u ⊗ v ⊗ α` the trace is `(v^μ α_μ) u`, where `v^μ α_μ` is the species contraction `S.contr`. -The pairs to contract are chosen by a relabeling `σ : ColorEquiv c (contrColors c' a)` of the +The pairs to contract are chosen by a reindexing `σ : ColorEquiv c (contrColors c' a)` of the indices. It sends each free index to an index `inl x`, of color `c' x`, and the two indices of a pair `j` to the indices `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`, and the index notation `{T | ρ μ μ}ᵀ` builds it from the repeated name. Since `σ` keeps colors, the two indices of each -pair are dual without a separate hypothesis. `Tensor.contr σ` relabels by `σ` and then contracts +pair are dual without a separate hypothesis. `Tensor.contr σ` reindexes by `σ` and then contracts each pair by `S.contr (a j)`, with the first index of the pair as its first argument. Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the @@ -50,8 +50,8 @@ since the bases of a color and its dual are dual. colors `contrColors` of the free indices and the pairs. - `Species.Tensor.contr_pure` gives the contraction of a pure tensor. - `Species.Tensor.contr` is an intertwining map, so it commutes with the group action. -- `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a relabeling of the - indices before or after a contraction. +- `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a reindexing before + or after a contraction. - `ColorEquiv.SamePair` says that two choices of pairs contract the same two indices, and `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, in any order, are equal. @@ -75,7 +75,7 @@ since the bases of a color and its dual are dual. - A. Choices of pairs of indices - B. Contraction in the standard arrangement of indices - C. Contraction of chosen pairs of indices - - C.1. Relabeling the indices + - C.1. Reindexing - C.2. Contractions of the same pairs of indices - C.3. Symmetric against antisymmetric - C.4. Two contractions are one @@ -84,7 +84,7 @@ since the bases of a color and its dual are dual. ## iv. References -None. +There are no known references for the material in this module. -/ @@ -111,7 +111,7 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ κ κ₁ J J' J₁ J₂ : Type} -/-- The relabeling of a contraction with no pairs, where every index is free and stays in place. -/ +/-- The reindexing of a contraction with no pairs, where every index is free and stays in place. -/ def noPairs (c : ι₁ → Color K) : ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where toEquiv.toFun := .inl @@ -222,7 +222,7 @@ namespace Tensor /-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the index `inr (inl j)` is contracted with the index `inr (inr j)` by `S.contr (a j)`, with the index `inr (inl j)` as its first argument. The indices `inl x` are the free indices, with the colors -`c`. It is the body of `Tensor.contr`. -/ +`c`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : (S.tensorRep (contrColors c a)).IntertwiningMap (S.tensorRep c) := -- The pairs are split into their first halves, of colors `a`, and their second halves, of the @@ -278,8 +278,7 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors S.pure (c := c') fun x => S.cast (σ.color_eq (.inl x)) (f (σ.toEquiv.symm (.inl x))) := (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) -/-- A contraction with no pairs is the identity. Any tensor is thus a contraction, as needed to -compare it with one. -/ +/-- A contraction with no pairs is the identity. -/ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := DFunLike.congr_fun (intertwiningMap_ext_pure (ψ := .id _) fun p => by rw [contr_pure, Finset.univ_eq_empty, Finset.prod_empty, one_smul] @@ -287,19 +286,19 @@ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := /-! -### C.1. Relabeling the indices +### C.1. Reindexing -/ -/-- Relabeling the indices and then contracting is contracting along the composite relabeling. -/ -lemma contr_reindex {ι₂ : Type} [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} +/-- Reindexing and then contracting is contracting along the composite reindexing. -/ +lemma contr_reindex [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : contr σ (S.reindex τ t) = contr (τ.trans σ) t := by simp only [contr, Representation.IntertwiningMap.comp_apply, Representation.Equiv.coe_toIntertwiningMap, reindex_reindex] -/-- Relabeling the free indices of a contraction is contracting along the composite relabeling. -/ -lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → Color K} +/-- Reindexing the free indices of a contraction is contracting along the composite reindexing. -/ +lemma reindex_contr [Fintype J] {c'' : κ' → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : S.Tensor c) : S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := DFunLike.congr_fun (intertwiningMap_ext_pure @@ -316,8 +315,7 @@ lemma reindex_contr {κ' : Type} [Fintype J] {c'' : κ' → Color K} {a : J → -/ /-- Two contractions are equal when they keep the same free indices in the same order and contract -the same pairs of indices, in any order and with either index of a pair first. On literal indices -the hypotheses are checked by evaluation. -/ +the same pairs of indices, in any order and with either index of a pair first. -/ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) @@ -341,9 +339,9 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' -/ -/-- A contraction of a tensor that changes sign under a relabeling `π` of its indices is zero, when -`π` only permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the -form of the index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ +/-- A contraction of a tensor that changes sign under a reindexing `π` is zero, when `π` only +permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the form of the +index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) @@ -435,7 +433,7 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color -/ /-- The components of the contraction in the standard arrangement of indices, -`(tr T)^ρ = ∑ μ, T^ρμ_μ`. The labels `μ` run over the pairs. -/ +`(tr T)^ρ = ∑ μ, T^ρμ_μ`, with one label `μ j` per pair `j`. -/ lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : κ → Color K) (a : J → Color K) (t : S.Tensor (contrColors c' a)) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contrStd c' a t) φ = ∑ μ : Π j, basisIdx (a j).kind, @@ -453,8 +451,8 @@ lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : simp only [tensorBasis_repr_pure, Fintype.prod_sum_type, Finset.prod_mul_distrib] ring -/-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace. The labels `μ` run -over the pairs, and the components of `T` are those of `S.reindex σ T`. -/ +/-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace, with one label `μ j` +per pair `j`. The components of `T` are those of `S.reindex σ T`. -/ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : @@ -462,8 +460,8 @@ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := tensorBasis_repr_contrStd c' a _ φ -/-- The components of the contraction of one pair in dual bases, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a -single label `μ`. -/ +/-- The components of the contraction of one pair, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a single label +`μ`. -/ lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a ()).kind, @@ -472,8 +470,8 @@ lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} rw [tensorBasis_repr_contr] exact Fintype.sum_equiv (Equiv.funUnique Unit (basisIdx (a ()).kind)) _ _ fun _ => rfl -/-- The components of the contraction of two pairs in dual bases, `(tr T)^ρ = ∑ μ ν, T^ρμν_μν` -with one label per pair. -/ +/-- The components of the contraction of two pairs, `(tr T)^ρ = ∑ μ ν, T^ρμν_μν`, with one label +per pair. -/ lemma tensorBasis_repr_contr_two_pairs [Fintype κ] {a : Unit ⊕ Unit → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 432e1de8ae..cc9d80f887 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -37,7 +37,7 @@ is again Hermitian or positive-definite, and a positive-definite `g_{IJ̄}` is i ## iv. References -None. +There are no known references for the material in this module. -/ diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 8fbebcbc85..7434b6ed1e 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -43,7 +43,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual ## iv. References -None. +There are no known references for the material in this module. -/ @@ -161,8 +161,8 @@ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi lemma inverse_form (c : Color K) [S.HasForm c.kind] : S.inverse (form c) = form c.dual := inverse_eq_left_inv _ (toLin_form_bijective c) (form_inverse c.dual) -/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`. The second index of the form at `c` contracted -with the first index of the form at `c.dual` is the delta. -/ +/-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`, the delta at `c.dual`. The second index of the form +at `c` contracted with the first index of the form at `c.dual` is the delta. -/ lemma contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : Tensor.contr (ColorEquiv.matMul c c c.dual) (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual)) = diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 03e96899c6..10b0052d7c 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -84,7 +84,7 @@ namespace ColorEquiv variable {K ι₁ : Type} -/-- The relabeling of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the +/-- The reindexing of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are `0` and `1`. -/ def matMul (a c b : Color K) : @@ -96,7 +96,7 @@ def matMul (a c b : Color K) : toEquiv.left_inv := by decide toEquiv.right_inv := by decide -/-- The relabeling of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a +/-- The reindexing of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a tensor `F` on two indices and puts the second index of `F` in the place of `i`. It is `matMul` at any index of a tensor on any index type. -/ def matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) @@ -181,6 +181,18 @@ lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : rw [← Representation.mem_linHom_invariants_iff_isIntertwining] exact forall_congr' fun g => by rw [← toLin_map_rep, S.toLin.injective.eq_iff] +variable {S} in +/-- Contracting with the swapped tensor swaps the two arguments, `u_μ z^νμ v_ν = v_ν z^νμ u_μ`. -/ +lemma contr_toLin_comm (c : Color K) (z : S.V c ⊗[k] S.V c) (u v : S.V c.dual) : + S.contr c.dual (v ⊗ₜ S.toLin (TensorProduct.comm k _ _ z) u) = + S.contr c.dual (u ⊗ₜ S.toLin z v) := by + induction z using TensorProduct.inductionOn with + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] + | tmul x y => + simp only [TensorProduct.comm_tmul, Module.Basis.coordTensorHomEquiv_tmul, tmul_smul, map_smul, + smul_eq_mul] + exact mul_comm _ _ + /-! ## C. The delta diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index a59640b9e0..46b9c50ef0 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -54,8 +54,7 @@ Martin. - `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an antisymmetric form on a kind with two labels, `LinearMap.IsAlt.schouten` for `formBilin`, and `Species.formMatrix_schouten` is the same in components, - `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`, and `Species.formMatrix_schouten'` with the delta - read the other way, `g^γρ δ_α^α' = g^γα δ_ρ^α' - g^ρα δ_γ^α'`. + `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. ## iii. Table of contents @@ -67,7 +66,7 @@ Martin. ## iv. References -None. +There are no known references for the material in this module. -/ @@ -174,17 +173,6 @@ class IsAntisymmetricForm (κ : K) [S.HasForm κ] : Prop where /-- Swapping the two indices of the form gives minus the form. -/ comm_form : ∀ v, TensorProduct.comm k _ _ (form (S := S) ⟨κ, v⟩) = -form ⟨κ, v⟩ -/-- Contracting with the swapped tensor swaps the two arguments, `u_μ z^νμ v_ν = v_ν z^νμ u_μ`. -/ -lemma contr_toLin_comm (c : Color K) (z : S.V c ⊗[k] S.V c) (u v : S.V c.dual) : - S.contr c.dual (v ⊗ₜ S.toLin (TensorProduct.comm k _ _ z) u) = - S.contr c.dual (u ⊗ₜ S.toLin z v) := by - induction z using TensorProduct.inductionOn with - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] - | tmul x y => - simp only [TensorProduct.comm_tmul, Module.Basis.coordTensorHomEquiv_tmul, tmul_smul, map_smul, - smul_eq_mul] - exact mul_comm _ _ - /-- A symmetric form, `g_μν = g_νμ`. -/ lemma form_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : TensorProduct.comm k (S.V c) (S.V c) (form c) = form c := @@ -393,15 +381,6 @@ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricFor @eq_comm _ α' α] at h linear_combination h - (if ρ = α then 1 else 0) * formMatrix_antisymm (S := S) c γ α' -/-- The Schouten identity in components with the delta read the other way, -`g^γρ δ_α^α' = g^γα δ_ρ^α' - g^ρα δ_γ^α'`. -/ -lemma formMatrix_schouten' (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - (h2 : Fintype.card (basisIdx c.kind) = 2) (γ ρ α α' : basisIdx c.kind) : - S.formMatrix c γ ρ * (if α = α' then 1 else 0) = - S.formMatrix c γ α * (if ρ = α' then 1 else 0) - - S.formMatrix c ρ α * (if γ = α' then 1 else 0) := by - simpa only [@eq_comm _ α' α] using formMatrix_schouten c h2 γ ρ α α' - end Species end Tensor diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 2ac6195471..adbae55076 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -14,13 +14,13 @@ The product `T ⊗ U` of two tensors has the indices of `T` followed by those of ## i. Overview -Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the indices `Fin 2` and `U = U^ρ e_ρ` colors `c₂` on -the index `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the indices `Fin 2 ⊕ Fin 1` -with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on `inr j`, so no index -is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Mathlib's -`PiTensorProduct.tmulEquivDep`. +Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the indices `Fin 2` and `U = U^ρ e_ρ` have colors +`c₂` on the index `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the indices +`Fin 2 ⊕ Fin 1` with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on +`inr j`, so no index is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Mathlib's +`PiTensorProduct.tmulEquivDep` transported along `toPiTensor`. -The product is associative and symmetric up to a relabeling of indices, and the group acts on it +The product is associative and symmetric up to a reindexing, and the group acts on it factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on two indices by `twoIndexEquiv`. The components of a product are the products of the components. @@ -36,7 +36,7 @@ two indices by `twoIndexEquiv`. The components of a product are the products of ## iii. Table of contents -- A. The colors and relabelings of a product +- A. The colors and reindexings of a product - B. The product - C. Associativity, symmetry and equivariance - D. Tensors on one and two indices @@ -44,7 +44,7 @@ two indices by `twoIndexEquiv`. The components of a product are the products of ## iv. References -None. +There are no known references for the material in this module. -/ @@ -56,7 +56,7 @@ namespace Tensor /-! -## A. The colors and relabelings of a product +## A. The colors and reindexings of a product -/ @@ -71,25 +71,25 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} -/-- The relabeling `σ` of the indices of `T` and `τ` of the indices of `U`, as a relabeling of the +/-- The reindexing `σ` of the indices of `T` and `τ` of the indices of `U`, as a reindexing of the indices of `T ⊗ U`. It is Mathlib's `Equiv.sumCongr`. -/ def sumCongr {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') : ColorEquiv (prodColors c₁ c₂) (prodColors c₁' c₂') := ⟨Equiv.sumCongr σ.toEquiv τ.toEquiv, by rintro (j | j); exacts [σ.color_eq j, τ.color_eq j]⟩ -/-- The indices of `(T ⊗ U) ⊗ W` relabeled as those of `T ⊗ (U ⊗ W)`. It is Mathlib's +/-- The indices of `(T ⊗ U) ⊗ W` reindexed as those of `T ⊗ (U ⊗ W)`. It is Mathlib's `Equiv.sumAssoc`. -/ def sumAssoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : ColorEquiv (prodColors (prodColors c₁ c₂) c₃) (prodColors c₁ (prodColors c₂ c₃)) := ⟨Equiv.sumAssoc ι₁ ι₂ ι₃, by rintro (_ | _ | _) <;> rfl⟩ -/-- The indices of `T ⊗ U` relabeled as those of `U ⊗ T`. It is Mathlib's `Equiv.sumComm`. -/ +/-- The indices of `T ⊗ U` reindexed as those of `U ⊗ T`. It is Mathlib's `Equiv.sumComm`. -/ def sumComm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ColorEquiv (prodColors c₁ c₂) (prodColors c₂ c₁) := ⟨Equiv.sumComm ι₁ ι₂, by rintro (_ | _) <;> rfl⟩ -/-- The indices of `(T ⊗ W) ⊗ U` relabeled as those of `(T ⊗ U) ⊗ W`, swapping the last two +/-- The indices of `(T ⊗ W) ⊗ U` reindexed as those of `(T ⊗ U) ⊗ W`, swapping the last two factors. -/ noncomputable def swapLast (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) : ColorEquiv (prodColors (prodColors c₁ c₃) c₂) (prodColors (prodColors c₁ c₂) c₃) := @@ -113,8 +113,8 @@ variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} variable (S) in /-- The product of tensors, `T ⊗ₜ U ↦ T ⊗ U` with `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. The indices of -`T` sit on the indices `inl i` and those of `U` on the indices `inr j`. It is Mathlib's -`PiTensorProduct.tmulEquivDep`. -/ +`T` become the indices `inl i` and those of `U` the indices `inr j`. It is Mathlib's +`PiTensorProduct.tmulEquivDep` transported along `toPiTensor`. -/ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ((S.tensorRep c₁).tprod (S.tensorRep c₂)).Equiv (S.tensorRep (prodColors c₁ c₂)) := .mk (TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ @@ -127,9 +127,8 @@ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K toPiTensor_symm_tprod] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) -/-- The product of the pure tensors `u ⊗ α` and `w` is the pure tensor `u ⊗ α ⊗ w`. The vectors -`f₁` of the first sit on the indices `inl i` and the vectors `f₂` of the second on the indices -`inr j`. -/ +/-- The product of the pure tensors of the vectors `f₁` and `f₂` is the pure tensor with the vectors +`f₁` at the indices `inl i` and the vectors `f₂` at the indices `inr j`. -/ @[simp] lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ : Π i, S.V (c₁ i)) (f₂ : Π j, S.V (c₂ j)) : @@ -137,8 +136,8 @@ lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ S.pure fun s => Sum.rec (motive := fun s => S.V (prodColors c₁ c₂ s)) f₁ f₂ s := by simp [tmulEquiv, tmulEquivDep_apply] -/-- The inverse of the product splits a pure tensor into the pure tensors of its two groups of -indices, `u ⊗ α ⊗ w ↦ (u ⊗ α) ⊗ₜ w`. -/ +/-- The inverse of the product splits a pure tensor into the pure tensor of its vectors at the +indices `inl i` and the pure tensor of its vectors at the indices `inr j`. -/ @[simp] lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f : Π s, S.V (prodColors c₁ c₂ s)) : @@ -184,7 +183,7 @@ lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ -/-- Relabeling the product along `σ.sumCongr τ` is relabeling its factors along `σ` and `τ`. -/ +/-- Reindexing the product along `σ.sumCongr τ` is reindexing its factors along `σ` and `τ`. -/ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : @@ -199,16 +198,16 @@ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ -/-- Relabeling the first factor relabels the product. With `tmulEquiv_reindex_right` it moves -relabelings out of products, the form used by index notation. -/ +/-- Reindexing the first factor reindexes the product. With `tmulEquiv_reindex_right` it moves +reindexings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₁') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = S.reindex (σ.sumCongr (.refl c₂)) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] -/-- Relabeling the second factor relabels the product. With `tmulEquiv_reindex_left` it moves -relabelings out of products, the form used by index notation. -/ +/-- Reindexing the second factor reindexes the product. With `tmulEquiv_reindex_left` it moves +reindexings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = @@ -269,7 +268,7 @@ lemma oneIndexEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` is the tensor `η_μν`. Each factor is a one-index tensor by `oneIndexEquiv`, and the indices -`Fin 1 ⊕ Fin 1` of their product are relabeled `Fin 2` by Mathlib's `finSumFinEquiv`. -/ +`Fin 1 ⊕ Fin 1` of their product become `Fin 2` by Mathlib's `finSumFinEquiv`. -/ noncomputable def twoIndexEquiv (a b : Color K) : ((S.rep a).tprod (S.rep b)).Equiv (S.tensorRep ![a, b]) := ((S.oneIndexEquiv a).tensorCongr (S.oneIndexEquiv b)).trans <| diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 375be8e286..06c0f7f59d 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -8,18 +8,18 @@ module public import Physlib.Mathematics.Tensors.Tensor /-! -# Relabeling the indices of a tensor +# Reindexing tensors A bijection of the indices of a tensor that keeps their colors, and its action on tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. ## i. Overview -A relabeling `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the indices of a tensor +A reindexing `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the indices of a tensor that keeps their colors, with the proof `σ.color_eq`. For the transpose `T^μ_ν ↦ T_ν^μ` it swaps the indices `0` and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would -turn an up index into a down one is not a relabeling, since that needs a metric. The labels `ι₁` -and `ι₂` may be different types, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the product `u^μ v^ν`. +turn an up index into a down one is not a reindexing, since that needs a metric. The index types +`ι₁` and `ι₂` may be different, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the product `u^μ v^ν`. `S.reindex σ` moves each vector of a tensor to its new index. The name follows Mathlib's `PiTensorProduct.reindex`. Index notation uses it to line up indices, as in `A^μ_ν + B_ν^μ`. @@ -29,26 +29,26 @@ and `ι₂` may be different types, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the pro - `ColorEquiv` is a bijection of indices that keeps their colors, with `refl`, `symm` and `trans`. - `Species.permIndices` moves the vectors of a tensor along a bijection of indices, with no colors involved. It is Mathlib's `PiTensorProduct.reindex`. -- `Species.reindex` applies a relabeling to tensors, `permIndices` followed by `congrIndices` of the +- `Species.reindex` applies a reindexing to tensors, `permIndices` followed by `congrIndices` of the moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. -- `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that relabeling +- `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that reindexing respects composites, the identity and inverses, and `Species.reindex_reindex` is the first on a tensor. -- `Species.reindex` is an equivalence of representations, so relabeling commutes with the group +- `Species.reindex` is an equivalence of representations, so reindexing commutes with the group action. -- `Species.tensorBasis_repr_reindex` gives the components of a relabeled tensor, +- `Species.tensorBasis_repr_reindex` gives the components of a reindexed tensor, `(Tᵀ)_ν^μ = T^μ_ν`. ## iii. Table of contents -- A. Relabelings that keep colors -- B. Relabeling tensors +- A. Reindexings that keep colors +- B. Reindexing tensors - C. Composition and equivariance - D. Components ## iv. References -None. +There are no known references for the material in this module. -/ @@ -60,14 +60,14 @@ namespace Tensor /-! -## A. Relabelings that keep colors +## A. Reindexings that keep colors -/ -/-- A relabeling of indices that keeps their colors, made of a bijection of indices and the proof +/-- A reindexing that keeps the colors of the indices, made of a bijection of indices and the proof that each new index has the color of the old index it comes from. The proof is stated at the new indices `j`, so that `c₂ j` reduces at indices such as `inl x` or `0`. For finitely many concrete -indices it is found by `rfl` or by cases. Two relabelings are equal when their bijections are +indices it is found by `rfl` or by cases. Two reindexings are equal when their bijections are (`ColorEquiv.ext`). -/ @[ext] structure ColorEquiv {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) where @@ -82,18 +82,18 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ ι₃ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} -/-- The identity relabeling, where every index stays in place. -/ +/-- The identity reindexing, where every index stays in place. -/ def refl (c₁ : ι₁ → Color K) : ColorEquiv c₁ c₁ := ⟨Equiv.refl ι₁, fun _ => rfl⟩ -/-- The inverse relabeling, where every index returns to its old position. -/ +/-- The inverse reindexing, where every index returns to its old position. -/ def symm (σ : ColorEquiv c₁ c₂) : ColorEquiv c₂ c₁ := ⟨σ.toEquiv.symm, fun i => by simpa using (σ.color_eq (σ.toEquiv i)).symm⟩ -/-- The relabeling `σ₁` followed by `σ₂`. -/ +/-- The reindexing `σ₁` followed by `σ₂`. -/ def trans (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : ColorEquiv c₁ c₃ := ⟨σ₁.toEquiv.trans σ₂.toEquiv, fun j => (σ₁.color_eq _).trans (σ₂.color_eq j)⟩ -/-- The relabeling that swaps the two indices of a tensor on two indices, `![a, b] → ![b, a]`. -/ +/-- The reindexing that swaps the two indices of a tensor on two indices, `![a, b] → ![b, a]`. -/ def swapTwo (a b : Color K) : ColorEquiv (K := K) ![a, b] ![b, a] := ⟨Equiv.swap 0 1, by intro j; fin_cases j <;> rfl⟩ @@ -124,7 +124,7 @@ variable {ι₁ ι₂ ι₃ : Type} /-! -## B. Relabeling tensors +## B. Reindexing tensors -/ @@ -146,7 +146,7 @@ lemma permIndices_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, simp [permIndices, PiTensorProduct.reindex_tprod] variable (S) in -/-- The relabeling `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new index `j` +/-- The reindexing `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new index `j` holds the vector of the old index `σ.toEquiv.symm j`, moved along `σ.color_eq j`. It is `permIndices` of `σ.toEquiv` followed by `congrIndices` of the moves. -/ noncomputable def reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : @@ -167,11 +167,11 @@ lemma reindex_pure {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C -/ -/-- Relabeling by the identity is the identity. -/ +/-- Reindexing by the identity is the identity. -/ lemma reindex_refl (c₁ : ι₁ → Color K) : S.reindex (ColorEquiv.refl c₁) = .refl _ := equiv_ext_pure fun f => by rw [reindex_pure]; rfl -/-- Relabeling by `σ₁.trans σ₂` is relabeling by `σ₁` and then by `σ₂`. -/ +/-- Reindexing by `σ₁.trans σ₂` is reindexing by `σ₁` and then by `σ₂`. -/ lemma reindex_trans {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) : S.reindex (σ₁.trans σ₂) = (S.reindex σ₁).trans (S.reindex σ₂) := @@ -179,13 +179,13 @@ lemma reindex_trans {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ simp only [Representation.Equiv.trans_apply, reindex_pure, cast_apply, cast_cast] rfl -/-- Relabeling by `σ₁` and then by `σ₂` is relabeling by `σ₁.trans σ₂`, on a tensor. -/ +/-- Reindexing by `σ₁` and then by `σ₂` is reindexing by `σ₁.trans σ₂`, on a tensor. -/ lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by rw [reindex_trans, Representation.Equiv.trans_apply] -/-- The inverse of relabeling by `σ` is relabeling by `σ.symm`. -/ +/-- The inverse of reindexing by `σ` is reindexing by `σ.symm`. -/ lemma reindex_symm {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) : (S.reindex σ).symm = S.reindex σ.symm := Representation.Equiv.ext <| funext fun t => EquivLike.injective (S.reindex σ) <| by @@ -199,7 +199,7 @@ lemma reindex_symm {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C -/ -/-- The components of a relabeled tensor are those of the tensor, as in `(Tᵀ)_ν^μ = T^μ_ν`. The +/-- The components of a reindexed tensor are those of the tensor, as in `(Tᵀ)_ν^μ = T^μ_ν`. The old index `i` takes the label `y (σ.toEquiv i)` of its new index, cast along the equality of their colors. For concrete colors and labels the cast is between equal types, and `rfl` removes it. -/ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → Color K} diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index b933773d0f..1b187f10e8 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -33,6 +33,8 @@ notation for both orientations of the delta. ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section @@ -67,6 +69,11 @@ lemma schouten_formTensor_deltaTensor_dual (h2 : Fintype.card (basisIdx c.kind) -{S.formTensor c | ρ α ⊗ S.deltaTensor c.dual | γ α'}ᵀ | ρ α γ α'}ᵀ := by tensor_components [tensorBasis_repr_formTensor, tensorBasis_repr_deltaTensor_eq_one] rw [← sub_eq_add_neg] - exact formMatrix_schouten' c h2 _ _ _ _ + -- `formMatrix_schouten` with the two labels of the delta swapped. + have h (γ ρ α α' : basisIdx c.kind) : S.formMatrix c γ ρ * (if α = α' then 1 else 0) = + S.formMatrix c γ α * (if ρ = α' then 1 else 0) - + S.formMatrix c ρ α * (if γ = α' then 1 else 0) := by + simpa only [@eq_comm _ α' α] using formMatrix_schouten c h2 γ ρ α α' + exact h _ _ _ _ end Tensor.Species diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 1f6429b31f..4ad1e19205 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -19,15 +19,14 @@ index type `ι`, and the group acts on them index by index. A tensor of a species `S` has one index `i` for each `i : ι`, and the index `i` has the color `c i`. The tensors with colors `c : ι → Color K` form the space `S.Tensor c`, which is Mathlib's -`⨂[k] i, V (c i)`. The index type `ι` can be any type, so later files take `Fin n`, sums or options -for it without renumbering. +`⨂[k] i, V (c i)`. The index type `ι` can be any type, such as `Fin n` or a sum `ι₁ ⊕ ι₂`. A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in `V (c 1)` and `β` in `V (c 2)`. Its components are products, `(u ⊗ α ⊗ β)^μ_νρ = u^μ α_ν β_ρ`. Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case -(`inductionOn_pure`). The group action, relabeling, products and contraction are each given by +(`inductionOn_pure`). The group action, reindexing, products and contraction are each given by their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's `⨂ₜ[k] i, f i` as an element of `S.Tensor c`. @@ -86,7 +85,7 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t ## iv. References -None. +There are no known references for the material in this module. -/ @@ -424,8 +423,8 @@ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := S.mapIndices (S.indexMapsAt i f h) -/-- On a pure tensor, `mapAt` applies `f` to the vector at `i`, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. The -result is given by its vectors `q`, `f (p i)` at `i` and `p x` moved along `h x` elsewhere. -/ +/-- On a pure tensor, `mapAt` applies `f` to the vector at `i` and keeps the others, +`u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ lemma mapAt_pure (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (p : Π x, S.V (c x)) (q : Π x, S.V (c' x)) (hqi : q i = f (p i)) (hq : ∀ x (hx : x ≠ i), q x = S.cast (h x hx) (p x)) : @@ -453,8 +452,7 @@ lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : rw [mapAt, ← eq_indexMapsAt (F := fun _ => LinearMap.id) i _ h rfl fun _ _ => LinearMap.ext fun _ => rfl, mapIndices_id] -/-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. The -labels `ψ` of `t` away from `i` are the labels `φ` of the result moved along `h`. -/ +/-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. -/ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) (ψ : Π x, basisIdx (c x).kind) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 9eba85e482..762bc6fbae 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -50,6 +50,8 @@ Instances come from the space of one index (`ofIndexSpace`), from an equivalent ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index f0c7768498..91b0f2707b 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -23,21 +23,29 @@ colors: the numbers that the literature writes `σ^μ_{αβ̇}` sit here at uppe the tensor of `ComplexVector.pauliVal` in `ComplexVector ⊗ (LeftHandedWeyl ⊗ RightHandedWeyl)`, so it is invariant under `SL(2, ℂ)` because `ComplexVector.pauli` is an intertwining map. +The conventions are those of Dreiner, Haber and Martin: `η = diag(1, -1, -1, -1)`, and `ε^12 = 1`, +`ε_12 = -1` (`complexLorentz.formEntries`). + The lowered forms are written with `τ` in index notation, such as `{pauliTensor | τ(μ) α β}ᵀ` for `σT_μ^{αβ̇}`. Lowering contracts the second index of `η` or `ε` (`Species.toDual`), so `σT_μ^{αβ̇} = η_μν σT^{ναβ̇} = η_μμ σT^{μαβ̇}`. With both Weyl indices lowered, `{pauliTensor | μ τ(α) τ(β)}ᵀ` is `σT^μ_{αβ̇}`: with rows `β̇` and columns `α` it is the matrix -`σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. -Lowering both indices of a matrix with `ε` gives its adjugate, transposed, and -`adj σ^μ = σ̄^μ`, so the components of the lowered forms are matrix identities. +`σ̄^μ = η_μμ σ^μ = (1, -σ⃗)`. Lowering both indices of a matrix with `ε` gives its adjugate, +transposed, and `adj σ^μ = σ̄^μ`, so the components of the lowered forms are matrix identities. One identity is read off from the Pauli matrices, the trace identity `pauliCoDown_trace_pauliCo`, `tr(σ̄_μ σ_ν) = 2 η_μν`: its components are the matrix traces of `PauliMatrix.trace_pauliMatrix_mul_minkowskiMatrix_smul_pauliMatrix`. The others are derived from -it at tensor level, mostly by `tensor_regroup using` an identity proved before. Contracting with +it at tensor level, mostly by `tensor_regroup using` an identity proved before. The trace +identities `pauliCoDown_trace_pauliCo` and `pauliCo_trace_pauliCoDown` reproduce +`PauliMatrix.pauliCoDown_trace_pauliCo` and `PauliMatrix.pauliCo_trace_pauliCoDown` with proofs at +tensor level. Contracting with `σ` gives a map `vectorToBispinor` from tensors with a vector index to tensors with two Weyl indices and a map `bispinorToVector` back. The trace identity says that going there and back is `2`. Both -spaces have dimension `16`, so going back and there is `2` as well, which is the Fierz identity. +spaces have dimension `16`, so going back and there is `2` as well, which is the Fierz identity +`σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`. An upper Weyl index of `σT` is a lower one in the +literature, so this is `σ^μ_{αβ̇} σ_{μα'β̇'} = 2 ε_{αα'} ε_{β̇β̇'}`, where the sign of each `ε` +changes and the two signs cancel. The anticommutators follow from the trace identities and the Schouten identity of `ε`. ## ii. Key results @@ -45,9 +53,13 @@ The anticommutators follow from the trace identities and the Schouten identity o - `complexLorentz.pauliTensor` : the Pauli matrices as a tensor `σT^{μαβ̇}`. - `complexLorentz.tensorBasis_repr_pauliTensor` : its components are the Pauli matrices. - `complexLorentz.pauliTensor_invariant` : `σ` is an invariant tensor of `SL(2, ℂ)`. -- `complexLorentz.pauliCoDown_trace_pauliCo`, `complexLorentz.pauliContr_trace_pauliContrDown` - and `complexLorentz.pauliContrDown_trace_pauliContr` : the trace identities - `tr(σ̄_μ σ_ν) = 2 η_μν` and `tr(σ^μ σ̄^ν) = tr(σ̄^μ σ^ν) = 2 η^μν`. +- `complexLorentz.tensorBasis_repr_pauliTensor_down` and + `complexLorentz.tensorBasis_repr_pauliTensor_downAll` : the components of the lowered forms. +- `complexLorentz.pauliCoDown_trace_pauliCo`, `complexLorentz.pauliCo_trace_pauliCoDown`, + `complexLorentz.pauliContr_trace_pauliContrDown` and + `complexLorentz.pauliContrDown_trace_pauliContr` : the trace identities + `tr(σ̄_μ σ_ν) = tr(σ_μ σ̄_ν) = 2 η_μν` and `tr(σ^μ σ̄^ν) = tr(σ̄^μ σ^ν) = 2 η^μν`, and + `complexLorentz.pauliContrDown_trace_pauliCo` : the mixed trace `tr(σ̄^μ σ_ν) = 2 δ^μ_ν`. - `complexLorentz.vectorToBispinor_bispinorToVector` : the maps `vectorToBispinor` and `bispinorToVector` are inverse up to `2`. - `complexLorentz.pauliCo_contr_pauliContr` : the Fierz identity @@ -125,8 +137,9 @@ lemma tensorBasis_repr_pauliTensor_down (μ : Π i, Idx (![down, upL, upR] i).ki exact Fintype.sum_eq_single _ fun x hx => by simp [formEntries, minkowskiMatrix.off_diag_zero (Ne.symm hx)] -/-- The components of `σT_{μαβ̇}`, with all indices lowered, are the entries of the adjugate of -`σ̄^μ = η_μμ σ^μ`, transposed: `σT^{μβα}` at the labels `(μ, α, β)`. -/ +/-- The components of `σT_{μαβ̇}`, with all three indices lowered, are the entries of `σ^μ` at the +row `β̇` and the column `α`. Lowering `μ` gives `σ̄^μ = η_μμ σ^μ`, and lowering both Weyl indices +takes it to its adjugate, transposed, with `adj σ̄^μ = σ^μ`. -/ lemma tensorBasis_repr_pauliTensor_downAll (μ : Π i, Idx (![down, downL, downR] i).kind) : (complexLorentz.tensorBasis _).repr {pauliTensor | τ(ν) τ(α) τ(β)}ᵀ μ = @@ -223,6 +236,9 @@ lemma pauliCo_contr_pauliContr : ### C.3. The anticommutators +A matrix product contracts the column of the first factor with the row of the second, so +`(σ^μ σ̄^ν)_{αα'}` is `σT^{μαβ̇} σT^ν_{α'β̇}`, summed over `β̇`. + -/ /-- The trace identity `tr(σ^μ σ̄^ν) = 2 η^μν`, as `σT^{μαβ̇} σT^ν_{αβ̇} = 2 η^μν`. -/ diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 53aaf45105..1955d2758a 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -18,11 +18,12 @@ forms `ε` and `η`. ## i. Overview -Complex Lorentz tensors have three kinds of index, the left-handed kind `left`, as in `ψ_α`, and -the right-handed kind `right`, as in `ψ̄_α̇`, with basis labels `Fin 2`, and the vector kind `vec`, -with labels `Fin 1 ⊕ Fin 3`. Conjugation `star` swaps `left` and `right` and fixes `vec`. With -the two variances this gives six colors, `upL`, `downL`, `upR`, `downR`, `up` and `down`. Under -`open scoped Tensor.complexLorentz`, `ℂT[upL, downR]` is the space of the tensors `T^α_β̇`. +Complex Lorentz tensors have three kinds of index. The left-handed kind `left`, as in `ψ_α`, and +the right-handed kind `right`, as in `ψ̄_α̇`, have the labels `Fin 2`. The vector kind `vec`, as in +`v^μ`, has the labels `Fin 1 ⊕ Fin 3`, so the space dimension is fixed at `d = 3`. Conjugation +`star` swaps `left` and `right` and fixes `vec`. With the two variances this gives six colors, +`upL`, `downL`, `upR`, `downR`, `up` and `down`. Under `open scoped Tensor.complexLorentz`, +`ℂT[upL, downR]` is the space of the tensors `T^α_β̇`. The species is built on `Fermion.LeftHandedWeyl`, `Fermion.RightHandedWeyl`, their duals, `Lorentz.ComplexVector` and `Lorentz.ComplexCoVector`, with their representations and bases. An @@ -34,13 +35,12 @@ representation is real, so this conjugation commutes with `SL(2, ℂ)` (`IsEquivariantConjugation`). The invariant form is `ε` at the Weyl colors and `η` at the vector colors. The form `ε` is -antisymmetric and `η` symmetric (`IsAntisymmetricForm`, `IsSymmetricForm`). The form -`ε^αβ` has the matrix `!![0, 1; -1, 0]` and `ε_αβ` the matrix `!![0, -1; 1, 0]`, so `ε^12 = 1` and +antisymmetric and `η` symmetric (`IsAntisymmetricForm`, `IsSymmetricForm`). The form `ε^αβ` has +the matrix `!![0, 1; -1, 0]` and `ε_αβ` the matrix `!![0, -1; 1, 0]`, so `ε^12 = 1` and `ε_12 = -1`. Lowering contracts the second index of the form, `ψ_α = ε_αβ ψ^β`, as in Wess and -Bagger. The -forms are written `εL`, `εL'`, `εR`, `εR'`, `η` and `η'`, and the deltas `δL`, `δL'`, `δR`, `δR'`, -`δ` and `δ'`. Their components are stated through `tensorBasis`, and the antisymmetry -of `ε` and the symmetry of `η` are stated in index notation. +Bagger. The forms are written `εL`, `εL'`, `εR`, `εR'`, `η` and `η'`, and the deltas `δL`, `δL'`, +`δR`, `δR'`, `δ` and `δ'`. Their components are stated through `tensorBasis`, and the +antisymmetry of `ε` and the symmetry of `η` are stated in index notation. These spaces are tensors with one index (`Tensorial`), so a spinor `ψ : LeftHandedWeyl` enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_α`. diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean b/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean index b54b86c178..3383d1d626 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean @@ -269,7 +269,7 @@ lemma inclCoRealLorentz_ρ (M : SL(2, ℂ)) (v : CoMod 3) : lemma coordPairing_complexVector_coVector (ψ : ComplexVector) (φ : ComplexCoVector) : ComplexVector.basis.coordPairing ComplexCoVector.basis (ψ ⊗ₜ φ) = ψ.toFin13ℂ ⬝ᵥ φ.toFin13ℂ := by - simp [Module.Basis.coordPairing_tmul, dotProduct] + rw [Module.Basis.coordPairing_tmul] rfl /-- The pairing `ψⁱ φᵢ` of a vector and a covector is invariant under `SL(2, ℂ)`. -/ diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 2ae0acf410..929ed8ee7f 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -18,15 +18,15 @@ Real Lorentz tensors in `d + 1` dimensions as a species, with the Minkowski metr ## i. Overview Real Lorentz tensors have one kind of index, the vector index `vec`. An upper index has the color -`⟨vec, up⟩` and a lower index the color `⟨vec, down⟩`. A color is a label. The species assigns to -it a space: `V ⟨vec, up⟩ = Lorentz.Vector d`, the contravariant vectors `v^μ` with basis `e_μ`, -and `V ⟨vec, down⟩ = Lorentz.CoVector d`, the covariant vectors `w_μ` with basis `e^μ`. Both are -functions `Fin 1 ⊕ Fin d → ℝ` with Mathlib's analysis, so `V ⟨vec, up⟩ = ℝ⁴` for `d = 3`. The -labels `μ` run over `Fin 1 ⊕ Fin d`, with `Sum.inl 0` the time direction and `Sum.inr i` the space -directions. A Lorentz transformation `Λ : LorentzGroup d` acts on an upper index by -`(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by `(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named -`realLorentz.up` and `realLorentz.down`, and `ℝT[d, up, down]` is the space of the tensors -`T^μ_ν`, under `open scoped Tensor.realLorentz`. +`⟨vec, up⟩` and a lower index the color `⟨vec, down⟩`. A color is the kind and variance of an +index. The species assigns to it a space: `V ⟨vec, up⟩ = Lorentz.Vector d`, the contravariant +vectors `v^μ` with basis `e_μ`, and `V ⟨vec, down⟩ = Lorentz.CoVector d`, the covariant vectors +`w_μ` with basis `e^μ`. Both are functions `Fin 1 ⊕ Fin d → ℝ` with Mathlib's analysis, so +`V ⟨vec, up⟩ = ℝ⁴` for `d = 3`. The labels `μ` run over `Fin 1 ⊕ Fin d`, with `Sum.inl 0` the +time direction and `Sum.inr i` the space directions. A Lorentz transformation +`Λ : LorentzGroup d` acts on an upper index by `(Λv)^μ = Λ^μ_ν v^ν` and on a lower index by +`(Λw)_μ = (Λ⁻¹)^ν_μ w_ν`. The two colors are named `realLorentz.up` and `realLorentz.down`, and +`ℝT[d, up, down]` is the space of the tensors `T^μ_ν`, under `open scoped Tensor.realLorentz`. The contraction `u^μ w_μ` is the coordinate pairing `∑ μ, u^μ w_μ` of the two bases and the delta is `δ_μ^ν = ∑ μ, e^μ ⊗ e_μ`. The one physical input is that the pairing is Lorentz invariant @@ -79,6 +79,8 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ ## iv. References +There are no known references for the material in this module. + -/ @[expose] public section @@ -331,8 +333,6 @@ end realLorentz namespace realLorentz -open Matrix - /-- A Lorentz vector `v^μ` is a tensor with one upper index, the space of the index. The Lorentz group acts by `Vector.rep`, `Λ v = Λ *ᵥ v`. -/ noncomputable instance vectorTensorial (d : ℕ) : Tensorial (realLorentz d) ![up] (Vector d) := @@ -376,10 +376,12 @@ noncomputable def _root_.Lorentz.Vector.tensorComponent {d : ℕ} (μ ν : Fin 1 Vector d ⊗[ℝ] Vector d →ₗ[ℝ] ℝ := Finsupp.lapply (μ, ν) ∘ₗ (Vector.basis.tensorProduct Vector.basis).repr.toLinearMap +/-- The component `F^μν` is the coordinate of `F` at `(μ, ν)` in Mathlib's product basis. -/ lemma _root_.Lorentz.Vector.tensorComponent_apply {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (F : Vector d ⊗[ℝ] Vector d) : Vector.tensorComponent μ ν F = (Vector.basis.tensorProduct Vector.basis).repr F (μ, ν) := rfl +/-- The components of `v ⊗ w` are the products of components, `(v ⊗ w)^μν = v^μ w^ν`. -/ @[simp] lemma _root_.Lorentz.Vector.tensorComponent_tmul {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (v w : Vector d) : Vector.tensorComponent μ ν (v ⊗ₜ w) = v μ * w ν := by From 463f12d50a60c2a9658b224b1672079f1979c551 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:56:22 +0000 Subject: [PATCH 132/242] fix(Tensors): tensor_regroup errors and buildFor fallback - `goalField`: `tensor_regroup` on a goal that is not an equation of tensors fails with an error instead of a panic. - `matchSummand` keeps the reason a summand has no match. - `throwSummandError`/`isSummandError`: `regroupWithLowerings` picks the error by a message tag, not by the message text. - `buildFor` elaborates the fallback only when `buildTo` fails. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 4 +- Physlib/Mathematics/Tensors/ElabExamples.lean | 16 +++++++- Physlib/Mathematics/Tensors/ElabTactics.lean | 41 ++++++++++++++----- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index e2aeba1151..5f1bfdf1d9 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -406,7 +406,9 @@ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do let ty ← instantiateMVars ty if ty.getAppFn.isMVar || ty.isSort then return (← build e).tm if (← whnfR ty).isAppOf ``Tensor.Species.Tensor then - return (← buildTo e ty).getD (← build e).tm + match ← buildTo e ty with + | some tm => return tm + | none => return (← build e).tm -- For a `Tensorial` type, the tensor type is the codomain of `toTensor`. let M ← exprToSyntax ty let s ← saveState diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index ccbe91318f..9c58ab8768 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -301,11 +301,20 @@ example : {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ = T | μ ν ⊗ W | ν ρ ⊗ U -- A summand with no counterpart on the other side is named. /-- error: tensor_regroup: the summand W -has no matching summand on the right -/ +has no matching summand on the right. Against the last one tried: + tensor_regroup: the factor + W + has no equal factor on the right -/ #guard_msgs in example : {T | μ ν + W | μ ν = U | μ ν + T | μ ν}ᵀ := by tensor_regroup +-- A goal that is not an equation of tensors is rejected. +/-- error: tensor_regroup: the goal is not an equation of tensors -/ +#guard_msgs in +example (x y : k) : x = y := by + tensor_regroup + /-- `tensor_regroup using h` finds `T^μ_ν U^ν_ρ` among the factors, rewrites it by `h` and regroups, `U^ν_ρ W^ρ_σ T^μ_ν = W^μ_ρ W^ρ_σ`. -/ example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : @@ -414,7 +423,10 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : -- A false goal with lowerings reports the summand as written, not with the lowerings expanded. /-- error: tensor_regroup: the summand (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w -has no matching summand on the right -/ +has no matching summand on the right. Against the last one tried: + tensor_regroup: the factor + (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w + has no equal factor on the right -/ #guard_msgs in example (w : S.Tensor ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by tensor_regroup diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 5d5ba900b6..905ae1b9ae 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -96,6 +96,24 @@ private def goalEq (tac : String := "tensor_regroup") : TacticM (Expr × Expr × | throwError "{tac}: the goal is not an equation" return (ty, l.consumeMData, r.consumeMData) +/-- The field `k` of the tensors of the goal of `tensor_regroup`, an equation in `S.Tensor c`. -/ +private def goalField : TacticM Expr := do + let (ty, _, _) ← goalEq + let_expr Tensor.Species.Tensor k _ _ _ _ _ _ _ _ _ _ := (← whnfR ty) + | throwError "tensor_regroup: the goal is not an equation of tensors" + return k + +/-- The tag of the errors of `tensor_regroup` about the summands or scalars of the goal. -/ +private def summandErrorTag : Name := `Tensor.Notation.summandError + +/-- Throws the error `msg`, tagged by `summandErrorTag`. -/ +private def throwSummandError {α : Type} (msg : MessageData) : TacticM α := + throwError (MessageData.tagged summandErrorTag msg) + +/-- Whether `e` is an error thrown by `throwSummandError`. -/ +private def isSummandError (e : Exception) : Bool := + e.toMessageData.hasTag (· == summandErrorTag) + /-- The argument `x` of `F x`, for a bundled map `F` whose head is the constant `f`, such as `Tensor.contr σ` for `f = Tensor.contr`. -/ private def bundledArg? (f : Name) (e : Expr) : Option Expr := @@ -552,8 +570,7 @@ contractions and relabelings are linear, so sums, negatives and scalars of `k` m The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a factor. -/ private def normalizeSums : TacticM Unit := do - let (ty, _, _) ← goalEq - let k ← exprToSyntax (← whnfR ty).getAppArgs[0]! + let k ← exprToSyntax (← goalField) evalTactic (← `(tactic| try simp only [map_add, map_neg, map_sub, map_smul, TensorProduct.tmul_add, TensorProduct.add_tmul, TensorProduct.tmul_neg, TensorProduct.neg_tmul, TensorProduct.tmul_sub, TensorProduct.sub_tmul, @@ -595,7 +612,7 @@ private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := d else if l.isAppOfArity ``HSMul.hSMul 6 && r.isAppOfArity ``HSMul.hSMul 6 then let a := l.appFn!.appArg! unless ← isDefEqGoal a r.appFn!.appArg! do - throwError "tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" + throwSummandError m!"tensor_regroup: the scalars {a} and {r.appFn!.appArg!} differ" evalTactic (← `(tactic| refine congrArg (HSMul.hSMul $(← exprToSyntax a)) ?_)) regroupSummand flipped else @@ -653,8 +670,9 @@ private def matchSummand (ty a : Expr) (rs : List Expr) (used : List Nat) : regroupSummand unless (← getGoals).isEmpty do throwError "tensor_regroup: a summand is not closed" return (j, ← instantiateMVars g) - catch _ => - throwError "tensor_regroup: the summand{indentExpr a}\nhas no matching summand on the right" + catch e => + throwSummandError m!"tensor_regroup: the summand{indentExpr a}\nhas no matching summand on \ + the right. Against the last one tried:{indentD e.toMessageData}" /-- The body of `tensor_regroup`: moves sums, negatives and scalars out, then proves the equation summand by summand. -/ @@ -666,7 +684,7 @@ private def regroup : TacticM Unit := do let ls := summands l let rs := summands r unless ls.length = rs.length do - throwError "tensor_regroup: the two sides have different numbers of summands" + throwSummandError m!"tensor_regroup: the two sides have different numbers of summands" if ls.length = 1 then regroupSummand return @@ -694,8 +712,8 @@ private def hasLowering (e : Expr) : Bool := /-- The body of `tensor_regroup`: `regroup`, and again after writing lowerings and raisings as contractions with the form (`Species.toDualAt_eq_contr`) when it fails and the goal has one. When -both fail, the first error is reported if it is about the summands or scalars, and the second -otherwise. -/ +both fail, the first error is reported if it is about the summands or scalars +(`isSummandError`), and the second otherwise. -/ private def regroupWithLowerings : TacticM Unit := do unless hasLowering (← instantiateMVars (← getMainTarget)) do regroup; return let s ← saveState @@ -707,8 +725,7 @@ private def regroupWithLowerings : TacticM Unit := do try regroup catch e' => -- The expansion changes the factors, not the summands or scalars, so an error about those -- reads better before it. - let msg ← e.toMessageData.toString - throw <| if msg.contains "summand" || msg.contains "scalars" then e else e' + throw <| if isSummandError e then e else e' /-! @@ -893,7 +910,9 @@ syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic elab_rules : tactic | `(tactic| tensor_regroup) => regroupWithLowerings - | `(tactic| tensor_regroup using $es,*) => regroupUsing es.getElems.toList + | `(tactic| tensor_regroup using $es,*) => do + discard goalField + regroupUsing es.getElems.toList end Tensor.Notation From 03c219b8c9adb7527b2137d0e5b3bcd22c9045f9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 11:59:30 +0000 Subject: [PATCH 133/242] refactor(Tensors): Indices.empty, Indices.elems and let_expr patterns - `Indices` gains `empty`, and `Indices.ofType?` reads `Unit` and `Empty` as its docstring says. - `Indices.toExpr` and `Indices.elems` give the indices as expressions. `indexElems` in ElabTactics now goes through them. - `tensorTypeArgs?` reads the species, index type and colors of `S.Tensor c` by `let_expr`. `colorList?`, `bundledArg?`, `deltaColor?`, `formArgs?` and `readContraction` match by `let_expr` or `match_expr` instead of argument positions. - The error on a non-literal index type names the type and says that `n` must be a literal. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 82 +++++++++++++------ Physlib/Mathematics/Tensors/ElabExamples.lean | 6 ++ Physlib/Mathematics/Tensors/ElabTactics.lean | 50 ++++------- 3 files changed, 81 insertions(+), 57 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 5f1bfdf1d9..7018c3363c 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -126,12 +126,15 @@ syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term -/ -/-- An index type built from `Fin n`, `Unit` and `⊕`. Its indices are numbered `0, 1, …` in order. --/ inductive Indices where +/-- An index type built from `Fin n`, `Unit`, `Empty` and `⊕`. Its indices are numbered `0, 1, …` in +order. -/ +inductive Indices where /-- `Fin n`. -/ | fin (n : Nat) /-- `Unit`. -/ | unit + /-- `Empty`. -/ + | empty /-- `a ⊕ b`. -/ | sum (a b : Indices) deriving BEq @@ -140,39 +143,61 @@ deriving BEq def Indices.size : Indices → Nat | .fin n => n | .unit => 1 + | .empty => 0 | .sum a b => a.size + b.size /-- The index type as a term. -/ def Indices.type : Indices → TermElabM Term | .fin n => `(Fin $(quote n)) | .unit => `(Unit) + | .empty => `(Empty) | .sum a b => do `($(← a.type) ⊕ $(← b.type)) +/-- The index type as an expression. -/ +def Indices.toExpr : Indices → Expr + | .fin n => mkApp (mkConst ``Fin) (mkNatLit n) + | .unit => mkConst ``Unit + | .empty => mkConst ``Empty + | .sum a b => mkApp2 (mkConst ``Sum [.zero, .zero]) a.toExpr b.toExpr + /-- The index `i` as a term. -/ def Indices.elem : Indices → Nat → TermElabM Term | .fin n, i => `(($(quote i) : Fin $(quote n))) | .unit, _ => `(()) + | .empty, _ => throwError "index notation: internal error, `Empty` has no index" | .sum a b, i => if i < a.size then do `(Sum.inl $(← a.elem i)) else do `(Sum.inr $(← b.elem (i - a.size))) +/-- The indices `0, 1, …` in order, as elements of the index type `toExpr`. -/ +def Indices.elems : Indices → MetaM (List Expr) + | .fin n => (List.range n).mapM fun i => do + mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, mkNatLit n])] + | .unit => return [mkConst ``Unit.unit] + | .empty => return [] + | .sum a b => do + let inj (f : Name) := mkApp3 (mkConst f [.zero, .zero]) a.toExpr b.toExpr + return (← a.elems).map (inj ``Sum.inl) ++ (← b.elems).map (inj ``Sum.inr) + /-- The function on an index type taking the values `vs` in index order, built from vector literals -on `Fin n`, constants on `Unit` and `comb` on `⊕`. -/ +on `Fin n`, constants on `Unit`, `Empty.elim` on `Empty` and `comb` on `⊕`. -/ def Indices.fn (comb : Name) : Indices → List Term → TermElabM Term | .fin _, vs => `(![$(vs.toArray),*]) | .unit, vs => `(fun _ : Unit => $(vs[0]!)) + | .empty, _ => `(Empty.elim) | .sum a b, vs => do `($(mkIdent comb) $(← a.fn comb (vs.take a.size)) $(← b.fn comb (vs.drop a.size))) -/-- The index type of a type built from `Fin n` and `⊕` with a literal `n`. -/ +/-- The index type of a type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`. -/ partial def Indices.ofType? (ι : Expr) : MetaM (Option Indices) := do - let ι ← whnfR ι - if ι.isAppOfArity ``Fin 1 then - return (← (evalNat ι.appArg!).run).map .fin - else if ι.isAppOfArity ``Sum 2 then - let some a ← Indices.ofType? (ι.getArg! 0) | return none - let some b ← Indices.ofType? (ι.getArg! 1) | return none + match_expr (← whnfR ι) with + | Fin n => return (← (evalNat n).run).map .fin + | Sum a b => + let some a ← Indices.ofType? a | return none + let some b ← Indices.ofType? b | return none return some (.sum a b) - else return none + | PUnit => return some .unit + | Empty => return some .empty + | _ => return none /-- The bijection from `dom` to `cod` sending the index `pre[j]` of `dom` to the index `j` of `cod`. The list `pre` names every index of `dom` once. Its inverse identities are proved index by index @@ -187,23 +212,28 @@ def indexEquiv (dom cod : Indices) (pre : List Nat) : TermElabM Term := do /-- The entries of a literal color family `![a, b, …]`, or of `prodColors` of literal families. -/ partial def colorList? (c : Expr) : MetaM (Option (List Expr)) := do - if c.isAppOfArity ``Tensor.prodColors 5 then - return (· ++ ·) <$> (← colorList? (c.getArg! 3)) <*> (← colorList? (c.getArg! 4)) - let (cs, _, tail) ← Matrix.matchVecConsPrefix (mkNatLit 0) c - return if tail.isAppOfArity ``Matrix.vecEmpty 1 then some cs else none + match_expr c with + | Tensor.prodColors _ _ _ c₁ c₂ => return (· ++ ·) <$> (← colorList? c₁) <*> (← colorList? c₂) + | _ => + let (cs, _, tail) ← Matrix.matchVecConsPrefix (mkNatLit 0) c + return if tail.isAppOfArity ``Matrix.vecEmpty 1 then some cs else none /-- The literal color family `![a, b, …]`. -/ def colorsTerm (cs : List Expr) : TermElabM Term := do `(![$((← cs.toArray.mapM exprToSyntax)),*]) +/-- The species `S`, the index type `ι` and the color family `c` of a tensor type +`S.Tensor (ι := ι) c`. -/ +def tensorTypeArgs? (ty : Expr) : Option (Expr × Expr × Expr) := + let_expr Tensor.Species.Tensor _ _ _ _ _ _ _ _ S ι c := ty | none + some (S, ι, c) + /-- The index type, the color family `c` and its entries of a tensor type `S.Tensor c` with literal indices and colors. -/ def literalTensorType? (ty : Expr) : MetaM (Option (Indices × Expr × List Expr)) := do - let ty ← whnfR (← instantiateMVars ty) - unless ty.isAppOf ``Tensor.Species.Tensor do return none - let args := ty.getAppArgs - let some indices ← Indices.ofType? args[args.size - 2]! | return none - let c ← instantiateMVars args[args.size - 1]! + let some (_, ι, c) := tensorTypeArgs? (← whnfR (← instantiateMVars ty)) | return none + let some indices ← Indices.ofType? ι | return none + let c ← instantiateMVars c let some cs ← colorList? c | return none return (indices, c, cs) @@ -292,15 +322,17 @@ def indexName (i : TSyntax `tensorIndex) (col : Expr) : TermElabM (Name × Bool) and flipped at each `τ(μ)`. -/ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do let (e, ty) ← tensorOf T - let args := ty.getAppArgs - let some indices ← Indices.ofType? args[args.size - 2]! - | throwError "index notation: the index type of {T} must be built from `Fin n` and `⊕`" - let some colors ← colorList? args[args.size - 1]! + let some (S, ι, c) := tensorTypeArgs? ty + | throwError "index notation: internal error, the type of {T} is not a tensor type" + let some indices ← Indices.ofType? ι + | throwError "index notation: the index type {ι} of {T} must be built from `Fin n` with a \ + literal `n`, `Unit`, `Empty` and `⊕`" + let some colors ← colorList? c | throwError "index notation: the colors of {T} must be a literal list" unless colors.length = idxs.size do throwError "index notation: {T} has {colors.length} indices, but {idxs.size} names are given" let kinds ← (idxs.toList.zip colors).mapM fun (i, col) => indexName i col - let a : Node := ⟨kinds.map (·.1), colors, indices, ← exprToSyntax e, args[args.size - 3]!⟩ + let a : Node := ⟨kinds.map (·.1), colors, indices, ← exprToSyntax e, S⟩ if kinds.all (!·.2) then return a kinds.zipIdx.foldlM (init := ← a.toFin) fun a ((_, d), i) => if d then a.toDualAt i else pure a diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 9c58ab8768..c7a8c01c75 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -121,6 +121,12 @@ example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ : #guard_msgs in example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ +-- A tensor whose index type is not literal is rejected. +/-- error: index notation: the index type Fin n of t must be built from `Fin n` with a literal `n`, +`Unit`, `Empty` and `⊕` -/ +#guard_msgs (whitespace := lax) in +example (n : ℕ) (f : Fin n → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ}ᵀ + -- A pair of indices whose colors are not dual is rejected. /-- error: index notation: μ joins the colors a and a, which are not dual -/ #guard_msgs in diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 905ae1b9ae..c6169fb992 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -117,8 +117,8 @@ private def isSummandError (e : Exception) : Bool := /-- The argument `x` of `F x`, for a bundled map `F` whose head is the constant `f`, such as `Tensor.contr σ` for `f = Tensor.contr`. -/ private def bundledArg? (f : Name) (e : Expr) : Option Expr := - let e := e.consumeMData - if e.isAppOfArity ``DFunLike.coe 6 && e.appFn!.appArg!.isAppOf f then some e.appArg! else none + let_expr DFunLike.coe _ _ _ _ F x := e | none + if F.isAppOf f then some x else none /-- Writes each side `x` of the goal on which `p` fails as `F x`, by the term `h : F x = x`. -/ private def wrapSides (tac : String) (p : Expr → Bool) (h : Term) : TacticM Unit := do @@ -353,16 +353,13 @@ private def leftover (xs ys : List Expr) : MetaM (List Expr) := do /-- The color `a` of a delta `S.deltaTensor a`. -/ private def deltaColor? (x : Expr) : Option Expr := - let x := x.consumeMData - if x.isAppOf ``Tensor.Species.deltaTensor then some x.appArg! else none + let_expr Tensor.Species.deltaTensor _ _ _ _ _ _ _ _ _ a := x | none + some a /-- The species `S` and the color `c` of a form `S.formTensor c`. -/ private def formArgs? (x : Expr) : Option (Expr × Expr) := - let x := x.consumeMData - if x.isAppOf ``Tensor.Species.formTensor then - let args := x.getAppArgs - some (args[args.size - 3]!, args[args.size - 2]!) - else none + let_expr Tensor.Species.formTensor _ _ _ _ _ _ _ _ S c _ := x | none + some (S, c) /-- A contraction `Tensor.contr τ P` of a left-nested product `P`, read index by index. The indices of `P` are numbered in order, the indices of each factor after those of the factors before it. -/ @@ -376,22 +373,12 @@ private structure ReadContraction where /-- The two indices of `P` of each pair, the first argument of `S.contr` first. -/ pairs : List (Nat × Nat) -/-- The elements of an index type built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕`, in -index order. -/ -private partial def indexElems (ι : Expr) : MetaM (List Expr) := do - let ι ← whnfR ι - if ι.isAppOfArity ``Fin 1 then - let some n ← (evalNat ι.appArg!).run - | throwError "tensor_regroup: the index type {ι} is not literal" - (List.range n).mapM fun i => do - mkAppM ``Fin.mk #[mkNatLit i, ← mkDecideProof (← mkAppM ``LT.lt #[mkNatLit i, ι.appArg!])] - else if let .const ``PUnit [u] := ι then return [mkConst ``PUnit.unit [u]] - else if ι.isConstOf ``Empty then return [] - else if ι.isAppOfArity ``Sum 2 then - let (a, b) := (ι.getArg! 0, ι.getArg! 1) - return (← (← indexElems a).mapM fun x => mkAppOptM ``Sum.inl #[a, b, x]) ++ - (← (← indexElems b).mapM fun x => mkAppOptM ``Sum.inr #[a, b, x]) - else throwError "tensor_regroup: the index type {ι} is not built from `Fin n` and `⊕`" +/-- The elements of the index type `ι`, in index order (`Indices.elems`). -/ +private def indexElems (ι : Expr) : MetaM (List Expr) := do + let some indices ← Indices.ofType? ι + | throwError "tensor_regroup: the index type {ι} is not built from `Fin n` with a literal `n`, \ + `Unit`, `Empty` and `⊕`" + indices.elems /-- The number of the index `x` of the index type `indices`, after evaluating `x`. -/ private partial def indexNumber : Indices → Expr → MetaM Nat @@ -400,6 +387,7 @@ private partial def indexNumber : Indices → Expr → MetaM Nat | throwError "tensor_regroup: internal error, the index {x} does not evaluate" return i | .unit, _ => return 0 + | .empty, x => throwError "tensor_regroup: internal error, the index {x} does not evaluate" | .sum a b, x => do let x ← whnfD x if x.isAppOfArity ``Sum.inl 3 then indexNumber a x.appArg! @@ -409,9 +397,11 @@ private partial def indexNumber : Indices → Expr → MetaM Nat /-- Reads a contraction `Tensor.contr τ P` of a left-nested product `P`, evaluating `τ` at each free index and at each index of each pair. -/ private def readContraction (e : Expr) : MetaM ReadContraction := do - let some P := bundledArg? ``Tensor.Species.Tensor.contr e - | throwError "tensor_regroup: the side{indentExpr e}\nis not a contraction" - let τ := e.consumeMData.appFn!.appArg!.appArg! + -- `e` is `Tensor.contr τ P`, with `τ : ColorEquiv c (contrColors c' a)`, `c' : κ → Color K` and + -- `a : J → Color K`. + let notContr := m!"tensor_regroup: the side{indentExpr e}\nis not a contraction" + let_expr DFunLike.coe _ _ _ _ F P := e | throwError notContr + let_expr Tensor.Species.Tensor.contr _ _ _ _ _ _ _ _ _ _ κ J _ _ _ _ τ := F | throwError notContr let some (indices, _, _) ← literalTensorType? (← inferType P) | throwError "tensor_regroup: the indices or colors of a product are not literal" let fs := factors P @@ -419,10 +409,6 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do let some (s, _, _) ← literalTensorType? (← inferType f) | throwError "tensor_regroup: the indices or colors of{indentExpr f}\nare not literal" return s.size - -- `τ : ColorEquiv c (contrColors c' a)`, with `c' : κ → Color K` and `a : J → Color K`. - let args := (← whnfR (← inferType τ)).getAppArgs - let cargs := args[args.size - 1]!.getAppArgs - let (κ, J) := (cargs[1]!, cargs[2]!) let JJ ← mkAppM ``Sum #[J, J] let at_ (y : Expr) : MetaM Nat := do indexNumber indices (← mkAppM ``Equiv.invFun #[← mkAppM ``Tensor.ColorEquiv.toEquiv #[τ], y]) From 6e85eda9e61c378f58696e548b52c4ccd949b172 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 12:01:00 +0000 Subject: [PATCH 134/242] chore(Tensors): tidy the index-notation tactics and examples - Drop the `saveState`/`restore` pairs that `TacticM`'s `catch` already covers, in `firstSuccess`, `regroupSummand`, `regroupWithLowerings` and `regroupUsing`. - Comment the blocks that continue inside `catch _ =>`, and refer to `maxMatchings` in the `tensor_regroup` docstring. - ElabExamples: drop the duplicate delta-insertion example, one `section Metric` for sections B to F. - References sentence in the three module docs, and "reindexing" for "relabeling" in the prose. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Elab.lean | 14 ++++--- Physlib/Mathematics/Tensors/ElabExamples.lean | 24 +++--------- Physlib/Mathematics/Tensors/ElabTactics.lean | 37 +++++++++---------- 3 files changed, 31 insertions(+), 44 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 7018c3363c..6d5b9c740c 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -24,11 +24,11 @@ names are bound only inside the braces. Each construct is an operation of the sp - `A ⊗ B` is `S.tmulEquiv _ _ (A ⊗ₜ B)`. - A name written twice in a product is contracted. All pairs of the product are contracted in one - `Tensor.contr σ`, with the relabeling `σ` generated from the names. The two indices of a pair must + `Tensor.contr σ`, with the reindexing `σ` generated from the names. The two indices of a pair must have dual colors, and the first one written is the first argument of `S.contr`. - The free indices are numbered `0, 1, …` in the order written. The result is a tensor on `Fin m` with a literal color list `![…]`. -- `A + B` and `A = B` relabel `B` by `S.reindex` when its names come in another order. Each name +- `A + B` and `A = B` reindex `B` by `S.reindex` when its names come in another order. Each name must have the same color on both sides. - `↓μ` lowers an upper index and `↑μ` raises a lower one, by `S.toDualAt`, so `{v | ↓ν}ᵀ` is `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the index, and a visible @@ -76,6 +76,8 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written ## iv. References +There are no known references for the material in this module. + -/ public meta section @@ -257,7 +259,7 @@ structure Node where /-- The species. -/ species : Expr -/-- The tensor of `a` relabeled onto `Fin pre.length` with the colors `cs`, its index `pre[j]` +/-- The tensor of `a` reindexed onto `Fin pre.length` with the colors `cs`, its index `pre[j]` becoming the index `j`. -/ def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term := do `(Tensor.Species.reindex $(← exprToSyntax a.species) @@ -265,7 +267,7 @@ def Node.reindex (a : Node) (pre : List Nat) (cs : List Expr) : TermElabM Term : $(← indexEquiv a.indices (.fin pre.length) pre)) $a.tm) -/-- The expression relabeled to `Fin n`, its indices numbered in order. -/ +/-- The expression reindexed to `Fin n`, its indices numbered in order. -/ def Node.toFin (a : Node) : TermElabM Node := do let n := a.names.length if a.indices == .fin n then return a @@ -368,7 +370,7 @@ def contract (a : Node) : TermElabM Node := do $a.tm) return ⟨free.map (a.names[·]!), free.map (a.colors[·]!), .fin m, tm, a.species⟩ -/-- The right side relabeled to the order of the left side's names, after checking that each name +/-- The right side reindexed to the order of the left side's names, after checking that each name has the same color on both sides. -/ def reorder (l r : Node) : TermElabM Term := do unless l.names.length == r.names.length && l.names.all (r.names.contains ·) do @@ -431,7 +433,7 @@ def buildTo (e : Syntax) (target : Expr) : TermElabM (Option Term) := do (Tensor.ColorEquiv.mk (c₂ := $(← exprToSyntax c)) $(← indexEquiv a.indices indices (List.range indices.size))) $a.tm) -/-- The term of `e` for the expected type. It is relabeled onto the indices of a tensor type, and +/-- The term of `e` for the expected type. It is reindexed onto the indices of a tensor type, and read back by `Tensorial.toTensor.symm` for a `Tensorial` type. -/ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do let some ty := expectedType? | return (← build e).tm diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index c7a8c01c75..4734fb7073 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -34,6 +34,8 @@ There are no declarations, only examples. ## iv. References +There are no known references for the material in this module. + -/ namespace Tensor.Notation @@ -176,7 +178,7 @@ example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ `rw [intertwiningMap_smul]`. -/ example : {(Λ • T) | μ μ}ᵀ = Λ • {T | μ μ}ᵀ := by rw [intertwiningMap_smul] -/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The relabeling the notation generates +/-- `g^μν g_νρ = δ^μ_ρ`, by `contr_formTensor_formTensor`. The reindexing the notation generates is `ColorEquiv.matMul`, checked index by index. -/ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ := by rw [← contr_formTensor_formTensor] @@ -184,8 +186,6 @@ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTenso ext x fin_cases x <;> rfl -end Metric - /-! ## C. Tensorial types @@ -210,11 +210,11 @@ noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ} noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ open TensorProduct in -/-- A product with no contraction is read back as `m ⊗ₜ m`, with no relabeling. -/ +/-- A product with no contraction is read back as `m ⊗ₜ m`, with no reindexing. -/ example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] -/-- A tensor on `Fin 2` relabeled onto the indices `Fin 1 ⊕ Fin 1` of a product. -/ +/-- A tensor on `Fin 2` reindexed onto the indices `Fin 1 ⊕ Fin 1` of a product. -/ noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ /-- error: index notation: the free index μ has the color a, but the expected index has the color @@ -280,10 +280,6 @@ example : {T | μ ρ ⊗ S.deltaTensor a | ν ρ = T | μ ν}ᵀ := by example : {T | μ ν + U | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = U | μ ν + T | μ ν}ᵀ := by tensor_regroup -/-- A delta on the left side is inserted on the right, `T^μ_ρ δ^ρ_ν = T^μ_ν`. -/ -example : {T | μ ρ ⊗ S.deltaTensor a.dual | ρ ν = T | μ ν}ᵀ := by - tensor_regroup - /-- Three equal factors, matched in one of the `3!` ways, `T^μ_ν T^ν_ρ T^ρ_σ = T^ρ_σ T^ν_ρ T^μ_ν`. -/ example : {T | μ ν ⊗ T | ν ρ ⊗ T | ρ σ = T | ρ σ ⊗ T | ν ρ ⊗ T | μ ν}ᵀ := by @@ -361,10 +357,6 @@ example (h : {T | μ ν ⊗ U | ν ρ = W | μ ρ}ᵀ) : {T | μ ν ⊗ U | ν -/ -section Metric - -variable [S.HasForm κ] [S.HasForm c.kind] - /-- A lowering is a contraction with the form, `v_μ = g_μν v^ν`. -/ example : {v | ↓μ = S.formTensor ⟨κ, .down⟩ | μ ν ⊗ v | ν}ᵀ := by tensor_regroup @@ -458,18 +450,12 @@ example : {S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTenso example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by tensor_regroup -end Metric - /-! ## F. Chains of lowerings and raisings -/ -section Metric - -variable [S.HasForm κ] [S.HasForm c.kind] - /-- Lowering and then raising by `tensor_indices`, `(v_μ)^μ = v^μ`. -/ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by tensor_indices diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index c6169fb992..b12e466570 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -39,6 +39,8 @@ Examples are in `Physlib.Mathematics.Tensors.ElabExamples`. ## iv. References +There are no known references for the material in this module. + -/ public meta section @@ -59,7 +61,7 @@ dsimproc_decl reduceCast (cast _ _) := fun e => do if ← withReducible (isDefEq α β) then return .done a else return .continue /-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, -using the component lemmas `lᵢ` as well. It unfolds products, relabelings, sums, negatives, zeros, +using the component lemmas `lᵢ` as well. It unfolds products, reindexings, sums, negatives, zeros, scalar multiples and contractions of one or two pairs. For more pairs, pass `Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal index that `tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a @@ -76,7 +78,7 @@ macro_rules Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] - -- The casts of `tensorBasis_repr_reindex` agree once the relabeling and the color lists are + -- The casts of `tensorBasis_repr_reindex` agree once the reindexing and the color lists are -- evaluated. The option `+instances` reaches those inside instance arguments. try dsimp +instances only [ColorEquiv.toEquiv_symm, Equiv.symm_symm, Equiv.coe_fn_mk, Sum.elim_inl, Sum.elim_inr, Matrix.cons_val, Function.comp_apply, @@ -139,11 +141,10 @@ the first that succeeds. When every one fails it throws the last error, and `err empty. -/ private def firstSuccess {α β : Type} (xs : List α) (err : MessageData) (f : α → TacticM β) : TacticM β := do - let s ← saveState let mut err := err for x in xs do try return ← f x - catch e => s.restore; err := e.toMessageData + catch e => err := e.toMessageData throwError err /-- The number of trailing arguments of `e` that are the tensors it is built from: the argument of @@ -268,7 +269,7 @@ private def flattenContractions : TacticM Unit := do throwError "tensor_regroup: flattening the products did not stop after ten rounds" /-- Swaps the factors `m` and `m + 1` of the product `q`, of `n` factors, on the right side of the -goal, by `Species.tmulEquiv_comm` or `Species.tmulEquiv_swapLast`, and absorbs the relabeling into +goal, by `Species.tmulEquiv_comm` or `Species.tmulEquiv_swapLast`, and absorbs the reindexing into the contraction. -/ private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do let fs := factors q @@ -522,7 +523,7 @@ private partial def matchFormPair (lhs : Bool) (ds gs : List Expr) : TacticM Uni deltas and inverse pairs of forms. Each side is flattened to one contraction of a left-nested product. Two forms on one side with a delta or nothing left over on the other are matched by `matchFormPair`. A delta on one side with no counterpart on the other is inserted on the other side -at each index of its color in turn, since contracting with a delta is a relabeling. The factors are +at each index of its color in turn, since contracting with a delta is a reindexing. The factors are then matched by `matchFactors`. -/ private partial def regroupContractions : TacticM Unit := do flattenContractions @@ -534,6 +535,7 @@ private partial def regroupContractions : TacticM Unit := do let mut formErr : Option MessageData := none for (lhs, ds, gs) in [(true, extraL, extraR), (false, extraR, extraL)] do try matchFormPair lhs ds gs; return catch _ => + -- When the forms do not cancel, the rest of the loop body records why. if 2 ≤ gs.length && gs.all (formArgs? · |>.isSome) then formErr := m!"tensor_regroup: the forms left over on one side do not cancel in inverse pairs \ against the other side{indentD (MessageData.joinSep (gs.map toMessageData) "\n")}" @@ -552,7 +554,7 @@ end -/ /-- Brings both sides of the goal to sums of signed scalar multiples of contractions. Products, -contractions and relabelings are linear, so sums, negatives and scalars of `k` move out of them. +contractions and reindexings are linear, so sums, negatives and scalars of `k` move out of them. The scalar lemmas are given the field `k` of the species, so a group element `Λ • T` stays a factor. -/ private def normalizeSums : TacticM Unit := do @@ -603,12 +605,11 @@ private partial def regroupSummand (flipped : Bool := false) : TacticM Unit := d regroupSummand flipped else try evalTactic (← `(tactic| with_reducible rfl)) catch _ => - let s ← saveState + -- When `rfl` fails, the rest of the block regroups. try regroupContractions catch e => if flipped then throw e -- The two sides may differ by the swap of a symmetric form. - s.restore - try flipOneForm false catch _ => s.restore; throw e + try flipOneForm false catch _ => throw e /-- Tries each form among the tensor leaves of the goal for `flipForm antisym`, then moves a new sign out and regroups. A form of the other symmetry fails at `flipForm`. -/ @@ -619,6 +620,7 @@ private partial def flipOneForm (antisym : Bool) : TacticM Unit := do let mut lastError : MessageData := m!"tensor_regroup: no form to swap" for F in (leaves.filter (formArgs? · |>.isSome)).eraseDups do for k in (List.range leaves.length).filter (leaves[·]! == F) do + -- Undoes a swap whose regrouping failed in the previous round. s.restore try flipForm antisym F k catch _ => break try @@ -684,7 +686,7 @@ private def regroup : TacticM Unit := do used := j :: used proofs := proofs.push pf -- The summands are replaced by congruence, not by `rw`: a summand can occur inside another one, - -- as `T` inside its relabeling. + -- as `T` inside its reindexing. let (pf, _) ← sumCongr l proofs.toList let some (_, _, l') := (← inferType pf).eq? | throwError "tensor_regroup: internal error" let rest ← mkFreshExprMVar (← mkEq l' r) @@ -702,11 +704,9 @@ both fail, the first error is reported if it is about the summands or scalars (`isSummandError`), and the second otherwise. -/ private def regroupWithLowerings : TacticM Unit := do unless hasLowering (← instantiateMVars (← getMainTarget)) do regroup; return - let s ← saveState try regroup catch e => -- Lowerings and raisings are contractions with the form (`Species.toDualAt_eq_contr`). They -- are expanded only when needed, since every expanded form is one more factor to match. - s.restore evalTactic (← `(tactic| simp only [Tensor.Species.toDualAt_eq_contr])) try regroup catch e' => -- The expansion changes the factors, not the summands or scalars, so an error about those @@ -784,7 +784,7 @@ private def regroupedFun (L A : Expr) (Aread G : ReadContraction) (φ target : L withLocalDeclD `x (← inferType A) fun x => do let rhs ← rest.foldlM (init := ← leaf x (Aread.free.map (φ[·]!))) fun a b => `(tensorIndexExpr| $a ⊗ $b) - -- The equation `{L | f₀ f₁ … = …}ᵀ` relabels its right side to the order of `L`. + -- The equation `{L | f₀ f₁ … = …}ᵀ` reindexes its right side to the order of `L`. let eq ← Term.elabType (← `({ $(← leaf L G.free) = $rhs }ᵀ)) Term.synthesizeSyntheticMVarsNoPostponing let some (_, _, M) := (← instantiateMVars eq).eq? @@ -845,7 +845,6 @@ private partial def regroupUsing : List Term → TacticM Unit catch _ => pure none if let some (L, G, Aread) := read? then for (φ, target) in (← embeddings Aread G).take maxMatchings do - let s' ← saveState try -- `L = M A` by regrouping, and `M A = M B` by `congrArg M h`. let M ← regroupedFun L A Aread G φ target @@ -857,19 +856,19 @@ private partial def regroupUsing : List Term → TacticM Unit replaceMainGoal [rest.mvarId!] regroupUsing es return - catch ex => s'.restore; err := ex.toMessageData + catch ex => err := ex.toMessageData s.restore throwError err /-- `tensor_regroup` proves an equation of two sums of signed scalar multiples of contractions of the same factors, grouped or ordered differently, such as `{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, -negatives and scalars are first moved out of every product, contraction and relabeling. Each +negatives and scalars are first moved out of every product, contraction and reindexing. Each summand on the left is then matched with a summand on the right with the same sign and scalar, whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 -are tried. +number of attempts is the product of `k!` over the factors that appear `k` times, and at most +`maxMatchings` are tried. Beyond regrouping, it uses these identities: - a delta on one side with no counterpart on the other is inserted on the other side at an index of From 225a1fd72edbd81f729d96eb83496430936ba40a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 12:10:41 +0000 Subject: [PATCH 135/242] doc(Tensors): tensor_regroup docstring states the limit of 12 matchings Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/ElabTactics.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index b12e466570..9856498d53 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -867,8 +867,8 @@ negatives and scalars are first moved out of every product, contraction and rein summand on the left is then matched with a summand on the right with the same sign and scalar, whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, so the -number of attempts is the product of `k!` over the factors that appear `k` times, and at most -`maxMatchings` are tried. +number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 +are tried (`maxMatchings`). Beyond regrouping, it uses these identities: - a delta on one side with no counterpart on the other is inserted on the other side at an index of From 4e290e67c8b2b4717e0abcb6f45bc95e9a7d7482 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 15:03:24 +0000 Subject: [PATCH 136/242] doc(Tensors): Color and Species say when colors are equal by computation and when S.cast moves vectors Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Color.lean | 6 ++++-- Physlib/Mathematics/Tensors/Species.lean | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Color.lean b/Physlib/Mathematics/Tensors/Color.lean index e919aaa665..d5505c21ff 100644 --- a/Physlib/Mathematics/Tensors/Color.lean +++ b/Physlib/Mathematics/Tensors/Color.lean @@ -21,8 +21,10 @@ for each of the two types of Weyl spinor index and one for the vector index. A tensor has one color per index. A species (`Species.lean`) gives each color a space, and a tensor is an element of the tensor product of the spaces of its indices. Each index has a color -rather than a space, so that checking that two indices can be contracted is an equality of -colors, which is decidable, not an equality of types. +rather than a space, so that whether two indices can be contracted is an equation of colors, +`c j = (c i).dual`, which Lean checks by computation when the colors are known. When two colors +are equal only through a proof, vectors are moved between their spaces by `S.cast` +(`Species.lean`, §C). The dual of `⟨κ, v⟩` is `⟨κ, v.dual⟩`, the same kind with the other variance. A contraction such as `v^μ w_μ` pairs an index of color `c` with an index of color `c.dual`. `Color K` is a structure diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 27571a138a..bcd003a676 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -233,12 +233,12 @@ variable {S} ## C. Moving a vector along an equality of colors -`S.V a` and `S.V a'` are different types unless `a` and `a'` are equal by definition. The colors -of two indices are often equal only through a proof. For a family of colors `c : ι → Color K` and -two indices with `s = s'`, the colors `c s` and `c s'` are equal through that proof. A vector is -moved between the two spaces by `S.cast`, which is the identity on the vector. The lemmas say that -this keeps the group action, the components and the contraction, so that proofs can remove the -casts. +When two colors are equal by computation, as `c.dual.dual` and `c`, their spaces are the same +type. When they are equal only through a proof `p : a = a'`, Lean cannot use a vector of `S.V a` +where one of `S.V a'` is expected. This happens for a hypothesis such as `c i = a`, and for the +colors `c s` and `c s'` of two indices with `s = s'`. `S.cast p` moves the vector from `S.V a` to +`S.V a'` without changing it. The lemmas below say that `S.cast` keeps the group action, the +components and the contraction, so that proofs can remove it. -/ From fb263207dbfbf5462495170588ccd5402668a3e6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 15:22:45 +0000 Subject: [PATCH 137/242] doc(Tensors): Species docstring names each field and the one condition Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index bcd003a676..8142829b1a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -95,10 +95,10 @@ At a lower color it is the condition at the upper color with the factors swapped -/ -/-- A tensor species, which gives each color a representation with a basis such that the -coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` with the basis of the dual color is invariant. The basis -of a color of kind `κ` is labelled by `basisIdx κ`, the values that an index of kind `κ` runs -over, and a color and its dual share these labels. -/ +/-- A tensor species gives each color `c` a space `V c`, a representation `rep c` of `G` on it and +a basis `basis c` labelled by `basisIdx c.kind`, so that a color and its dual share labels. Its one +condition, `invariant`, says that the coordinate pairing `u ⊗ α ↦ ∑ μ, u^μ α_μ` of the bases of +`c` and `c.dual` is invariant. -/ structure Species (k : Type) [RCLike k] (G : Type) [Group G] (K : Type) (basisIdx : K → Type) [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] where /-- The vector space of a color. -/ From 51ceb1a0d7f92ee881c1ad861bd933efe1268e1a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 15:29:20 +0000 Subject: [PATCH 138/242] doc(Tensors): Tensor docs without references to later pieces Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 4ad1e19205..f805f1af4e 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -26,8 +26,8 @@ A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case -(`inductionOn_pure`). The group action, reindexing, products and contraction are each given by -their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's +(`inductionOn_pure`). Maps of tensors, starting with the group action, are given by their value on +pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's `⨂ₜ[k] i, f i` as an element of `S.Tensor c`. A group element `Λ` acts on every index by the representation of its color, @@ -45,7 +45,7 @@ transform with `Λ⁻¹`, which keeps `u^μ α_μ` invariant. A linear equivalence `f` from the space of one color to that of another acts on tensors through a single index. `S.congrAt i f` applies `f` at the index `i` and the identity at the other indices, -`u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`, as lowering one index of a tensor does. +`u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. ## ii. Key results @@ -107,8 +107,8 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → -/ /-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not -an `abbrev`, so that the type `S.Tensor c` keeps the species and the colors, which index notation -reads off the type of a tensor. -/ +an `abbrev`, so that the type `S.Tensor c` keeps the species and the colors instead of unfolding to +`⨂[k] i, S.V (c i)`. -/ def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := ⨂[k] i, S.V (c i) From 3c0d4ba7c3c31c77481a48d44ab96aecfcaec889 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 15:42:20 +0000 Subject: [PATCH 139/242] refactor(Tensors): Tensor S c is the tensor type, tensor lemmas in namespace Tensor The tensor type is the root-level `Tensor S c`, whose namespace is the library namespace, as for Mathlib's `TensorProduct`. The declarations of `Tensor.Species.Tensor` move to `Tensor` (`Tensor.contr`, `Tensor.conj`, ...), which silences the `dupNamespace` linter. Uses, notations, the elaborator and the docs follow. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 28 +++--- Physlib/Mathematics/Tensors/Contraction.lean | 64 ++++++------- Physlib/Mathematics/Tensors/Elab.lean | 18 ++-- Physlib/Mathematics/Tensors/ElabExamples.lean | 62 ++++++------- Physlib/Mathematics/Tensors/ElabTactics.lean | 50 +++++------ Physlib/Mathematics/Tensors/LinearMap.lean | 34 ++++--- Physlib/Mathematics/Tensors/Metric.lean | 10 +-- Physlib/Mathematics/Tensors/Product.lean | 22 ++--- Physlib/Mathematics/Tensors/Reindexing.lean | 4 +- Physlib/Mathematics/Tensors/Tensor.lean | 90 +++++++++---------- Physlib/Mathematics/Tensors/Tensorial.lean | 6 +- .../Tensors/ComplexTensor/Species.lean | 14 +-- .../Tensors/RealTensor/Species.lean | 14 +-- 13 files changed, 203 insertions(+), 213 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 02e69a77ac..abaedf0703 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -41,9 +41,9 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde - `Species.HasConjugation.ofBasis` builds a componentwise conjugation from `ConjLabels`, and `Species.HasConjugation.isEquivariantConjugation_ofBasis` gives its equivariance from the matrices of the group. -- `Species.Tensor.conj` is the conjugate of a tensor, with `Species.Tensor.conj_conj`, - `Species.Tensor.conj_smul` and `Species.Tensor.conj_contr`. -- `Species.Tensor.tensorBasis_repr_conj_of_basis` proves that the components of the conjugate are +- `Tensor.conj` is the conjugate of a tensor, with `Tensor.conj_conj`, + `Tensor.conj_smul` and `Tensor.conj_contr`. +- `Tensor.tensorBasis_repr_conj_of_basis` proves that the components of the conjugate are the conjugates of the components. ## iii. Table of contents @@ -110,12 +110,12 @@ structure ConjLabels (basisIdx : K → Type) where equiv_equiv {κ κ' : K} (h : κ' = star κ) (h' : κ = star κ') (μ : basisIdx κ) : equiv h' (equiv h μ) = μ -namespace Species - variable {k : Type} [RCLike k] {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} {c c' : Color K} +namespace Species + variable (S) in /-- A conjugation of the species, a conjugate-linear equivalence `v ↦ v̄` from each color `c` to its conjugate `c'`, given with an equation `h : c' = c.bar` so that none of its conditions moves a @@ -257,14 +257,16 @@ lemma basis_repr_conjEquiv_of_basis [S.HasConjugation] (h : c' = c.bar) -/ -namespace Tensor +end Species + +open Species variable [S.HasConjugation] {ι : Type} {c c' : ι → Color K} /-- The conjugate `T̄` of a tensor `T`, with every index conjugated and moved to the conjugate color. The colors `c'` of the result are the conjugates of `c` by `h`. For concrete colors `h` holds by `rfl` index by index, `fun i => by fin_cases i <;> rfl`. It is conjugate-linear. -/ -noncomputable def conj (h : ∀ i, c' i = (c i).bar) : S.Tensor c ≃ₗ⋆[k] S.Tensor c' := +noncomputable def conj (h : ∀ i, c' i = (c i).bar) : Tensor S c ≃ₗ⋆[k] Tensor S c' := (S.toPiTensor c).trans <| (PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i)).trans (S.toPiTensor c').symm @@ -275,7 +277,7 @@ lemma conj_pure (h : ∀ i, c' i = (c i).bar) (f : Π i, S.V (c i)) : congrₛₗ_tprod _ f /-- Conjugating twice gives back the tensor. -/ -lemma conj_conj (h : ∀ i, c' i = (c i).bar) (h' : ∀ i, c i = (c' i).bar) (t : S.Tensor c) : +lemma conj_conj (h : ∀ i, c' i = (c i).bar) (h' : ∀ i, c i = (c' i).bar) (t : Tensor S c) : conj h' (conj h t) = t := by induction t using inductionOn_pure with | h f => simp @@ -290,7 +292,7 @@ lemma conj_conj (h : ∀ i, c' i = (c i).bar) (h' : ∀ i, c i = (c' i).bar) (t /-- Conjugation commutes with the action `Λ` of `g : G`, `(ΛT)‾ = Λ T̄`. -/ lemma conj_smul [S.IsEquivariantConjugation] (h : ∀ i, c' i = (c i).bar) (g : G) - (t : S.Tensor c) : + (t : Tensor S c) : conj h (g • t) = g • conj h t := map_smul_of_pure (conj h) g (fun f => by simp [conjEquiv_rep]) t @@ -298,7 +300,7 @@ variable {κ J : Type} [Fintype J] {b : κ → Color K} {a : J → Color K} /-- Conjugating a contraction is contracting the conjugate along the same pairs of indices, `(T^ρμ_μ)‾ = T̄^ρμ_μ`. -/ -lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : +lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : Tensor S c) : conj (fun _ => rfl) (contr σ t) = contr σ.barContr (conj (fun _ => rfl) t) := by induction t using inductionOn_pure with | hsmul r t ht => rw [map_smul, LinearEquiv.map_smulₛₗ, ht, LinearEquiv.map_smulₛₗ, map_smul] @@ -319,7 +321,7 @@ lemma conj_contr (σ : ColorEquiv c (contrColors b a)) (t : S.Tensor c) : the basis vector `e_{e i μ}`. -/ lemma tensorBasis_repr_conj_of_basis [Fintype ι] (h : ∀ i, c' i = (c i).bar) (e : ∀ i, basisIdx (c i).kind ≃ basisIdx (c' i).kind) - (he : ∀ i μ, S.conjEquiv (h i) (S.basis (c i) μ) = S.basis (c' i) (e i μ)) (t : S.Tensor c) + (he : ∀ i μ, S.conjEquiv (h i) (S.basis (c i) μ) = S.basis (c' i) (e i μ)) (t : Tensor S c) (μ : Π i, basisIdx (c' i).kind) : (S.tensorBasis c').repr (conj h t) μ = star ((S.tensorBasis c).repr t fun i => (e i).symm (μ i)) := by @@ -328,7 +330,3 @@ lemma tensorBasis_repr_conj_of_basis [Fintype ι] (h : ∀ i, c' i = (c i).bar) rfl end Tensor - -end Species - -end Tensor diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 78619e0a67..1358929a0a 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -46,29 +46,29 @@ since the bases of a color and its dual are dual. ## ii. Key results -- `Species.Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`, with the +- `Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`, with the colors `contrColors` of the free indices and the pairs. -- `Species.Tensor.contr_pure` gives the contraction of a pure tensor. -- `Species.Tensor.contr` is an intertwining map, so it commutes with the group action. -- `Species.Tensor.contr_reindex` and `Species.Tensor.reindex_contr` absorb a reindexing before +- `Tensor.contr_pure` gives the contraction of a pure tensor. +- `Tensor.contr` is an intertwining map, so it commutes with the group action. +- `Tensor.contr_reindex` and `Tensor.reindex_contr` absorb a reindexing before or after a contraction. - `ColorEquiv.SamePair` says that two choices of pairs contract the same two indices, and - `Species.Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, + `Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, in any order, are equal. -- `Species.Tensor.contr_noPairs` proves that a contraction with no pairs, along +- `Tensor.contr_noPairs` proves that a contraction with no pairs, along `ColorEquiv.noPairs`, is the identity. -- `Species.Tensor.contr_eq_zero_of_reindex_eq_neg` and - `Species.Tensor.contr_tmulEquiv_eq_zero_of_antisymm_symm` prove `A^μν B_μν = 0` for `A` +- `Tensor.contr_eq_zero_of_reindex_eq_neg` and + `Tensor.contr_tmulEquiv_eq_zero_of_antisymm_symm` prove `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. -- `Species.Tensor.contr_contr` proves that two contractions in sequence are one, along +- `Tensor.contr_contr` proves that two contractions in sequence are one, along `ColorEquiv.chain`. -- `Species.Tensor.tmulEquiv_contr_left` and `Species.Tensor.tmulEquiv_contr_right` prove that +- `Tensor.tmulEquiv_contr_left` and `Tensor.tmulEquiv_contr_right` prove that contracting a factor commutes with the product, along `ColorEquiv.tmulRight` and `ColorEquiv.tmulLeft`. -- `Species.Tensor.tensorBasis_repr_contr` gives the components of a contraction, from - `Species.Tensor.tensorBasis_repr_contrStd` in the standard arrangement of indices. -- `Species.Tensor.tensorBasis_repr_contr_one_pair` and - `Species.Tensor.tensorBasis_repr_contr_two_pairs` give the same with one and with two pairs. +- `Tensor.tensorBasis_repr_contr` gives the components of a contraction, from + `Tensor.tensorBasis_repr_contrStd` in the standard arrangement of indices. +- `Tensor.tensorBasis_repr_contr_one_pair` and + `Tensor.tensorBasis_repr_contr_two_pairs` give the same with one and with two pairs. ## iii. Table of contents @@ -204,14 +204,12 @@ noncomputable def tmulLeft {a : J → Color K} (σ : ColorEquiv c (contrColors c end ColorEquiv -namespace Species - variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} variable {ι₁ ι₂ κ κ' κ₁ J J' J₁ J₂ : Type} -namespace Tensor +open Species /-! @@ -279,7 +277,7 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors (congrArg (contrStd c' a) (reindex_pure σ f)).trans (contrStd_pure c' a _) /-- A contraction with no pairs is the identity. -/ -lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := +lemma contr_noPairs (t : Tensor S c) : contr (ColorEquiv.noPairs c) t = t := DFunLike.congr_fun (intertwiningMap_ext_pure (ψ := .id _) fun p => by rw [contr_pure, Finset.univ_eq_empty, Finset.prod_empty, one_smul] exact congrArg S.pure (funext fun x => by simp only [cast_apply]; rfl)) t @@ -292,14 +290,14 @@ lemma contr_noPairs (t : S.Tensor c) : contr (ColorEquiv.noPairs c) t = t := /-- Reindexing and then contracting is contracting along the composite reindexing. -/ lemma contr_reindex [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} - (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : S.Tensor c) : + (σ : ColorEquiv c₂ (contrColors c' a)) (τ : ColorEquiv c c₂) (t : Tensor S c) : contr σ (S.reindex τ t) = contr (τ.trans σ) t := by simp only [contr, Representation.IntertwiningMap.comp_apply, - Representation.Equiv.coe_toIntertwiningMap, reindex_reindex] + Representation.Equiv.coe_toIntertwiningMap, Species.reindex_reindex] /-- Reindexing the free indices of a contraction is contracting along the composite reindexing. -/ lemma reindex_contr [Fintype J] {c'' : κ' → Color K} {a : J → Color K} - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : S.Tensor c) : + (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : Tensor S c) : S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := DFunLike.congr_fun (intertwiningMap_ext_pure (φ := (S.reindex τ).toIntertwiningMap.comp (contr σ)) fun p => by @@ -319,7 +317,7 @@ the same pairs of indices, in any order and with either index of a pair first. - lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) - (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : S.Tensor c) : + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : Tensor S c) : contr σ t = contr τ t := by refine DFunLike.congr_fun (intertwiningMap_ext_pure fun p => ?_) t rw [contr_pure, contr_pure] @@ -345,7 +343,7 @@ index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) - {t : S.Tensor c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by + {t : Tensor S c} (ht : t = S.reindex π (-t)) : contr σ t = 0 := by -- After `π`, each pair `j` of `σ` is its pair `e.symm j`, and the free indices are kept. have hs (y) : π.toEquiv.symm (σ.toEquiv.symm y) = σ.toEquiv.symm (((Equiv.refl κ).sumCongr (e.sumCongr e)).symm y) := @@ -368,7 +366,7 @@ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] (π₂ : ColorEquiv c₂ c₂) (e : J ≃ J) (hπ : (π₁.sumCongr π₂).toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) - {A : S.Tensor c₁} {B : S.Tensor c₂} (hA : A = S.reindex π₁ (-A)) (hB : B = S.reindex π₂ B) : + {A : Tensor S c₁} {B : Tensor S c₂} (hA : A = S.reindex π₁ (-A)) (hB : B = S.reindex π₂ B) : contr σ (S.tmulEquiv c₁ c₂ (A ⊗ₜ B)) = 0 := contr_eq_zero_of_reindex_eq_neg σ _ e hπ (by rw [← map_neg, ← neg_tmul, reindex_sumCongr_tmulEquiv, ← hA, ← hB]) @@ -383,7 +381,7 @@ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] then `ν` in `T^μ_μ^ν_ν` is contracting both pairs at once. -/ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} (σ₁ : ColorEquiv c (contrColors c₁ a₁)) - (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) (t : S.Tensor c) : + (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) (t : Tensor S c) : contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := DFunLike.congr_fun (intertwiningMap_ext_pure (φ := (contr σ₂).comp (contr σ₁)) fun f => by simp only [Representation.IntertwiningMap.comp_apply, contr_pure, map_smul, smul_smul, @@ -399,7 +397,7 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ /-- Contracting indices of the left factor commutes with the product, `(T^μ_μν) U^ρ = (T ⊗ U)^μ_μν^ρ`. -/ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color K} - (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (u : S.Tensor d) : + (σ : ColorEquiv c (contrColors c' a)) (t : Tensor S c) (u : Tensor S d) : S.tmulEquiv c' d (contr σ t ⊗ₜ u) = contr (σ.tmulRight d) (S.tmulEquiv c d (t ⊗ₜ u)) := LinearMap.congr_fun (f := (S.tmulEquiv c' d).toLinearMap ∘ₗ (contr σ).toLinearMap.rTensor _) @@ -414,7 +412,7 @@ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color /-- Contracting indices of the right factor commutes with the product, `U^ρ (T^μ_μν) = (U ⊗ T)^ρμ_μν`. -/ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color K} - (σ : ColorEquiv c (contrColors c' a)) (u : S.Tensor d) (t : S.Tensor c) : + (σ : ColorEquiv c (contrColors c' a)) (u : Tensor S d) (t : Tensor S c) : S.tmulEquiv d c' (u ⊗ₜ contr σ t) = contr (σ.tmulLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := LinearMap.congr_fun (f := (S.tmulEquiv d c').toLinearMap ∘ₗ (contr σ).toLinearMap.lTensor _) @@ -435,7 +433,7 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color /-- The components of the contraction in the standard arrangement of indices, `(tr T)^ρ = ∑ μ, T^ρμ_μ`, with one label `μ j` per pair `j`. -/ lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : κ → Color K) - (a : J → Color K) (t : S.Tensor (contrColors c' a)) (φ : Π x, basisIdx (c' x).kind) : + (a : J → Color K) (t : Tensor S (contrColors c' a)) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contrStd c' a t) φ = ∑ μ : Π j, basisIdx (a j).kind, (S.tensorBasis (contrColors c' a)).repr t (Sum.rec φ (Sum.rec μ μ)) := by induction t using inductionOn_pure with @@ -454,7 +452,7 @@ lemma tensorBasis_repr_contrStd [Fintype κ] [Fintype J] [DecidableEq J] (c' : /-- The components of a contraction, as `(tr T)^ρ = ∑ μ, T^ρμ_μ` for a trace, with one label `μ j` per pair `j`. The components of `T` are those of `S.reindex σ T`. -/ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] - {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) + {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : Π j, basisIdx (a j).kind, (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec μ μ)) := @@ -463,7 +461,7 @@ lemma tensorBasis_repr_contr [Fintype κ] [Fintype J] [DecidableEq J] /-- The components of the contraction of one pair, `(tr T)^ρ = ∑ μ, T^ρμ_μ` with a single label `μ`. -/ lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} - (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (σ : ColorEquiv c (contrColors c' a)) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a ()).kind, (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ (Sum.rec (fun _ => μ) (fun _ => μ))) := by @@ -473,7 +471,7 @@ lemma tensorBasis_repr_contr_one_pair [Fintype κ] {a : Unit → Color K} /-- The components of the contraction of two pairs, `(tr T)^ρ = ∑ μ ν, T^ρμν_μν`, with one label per pair. -/ lemma tensorBasis_repr_contr_two_pairs [Fintype κ] {a : Unit ⊕ Unit → Color K} - (σ : ColorEquiv c (contrColors c' a)) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (σ : ColorEquiv c (contrColors c' a)) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (contr σ t) φ = ∑ μ : basisIdx (a (.inl ())).kind, ∑ ν : basisIdx (a (.inr ())).kind, (S.tensorBasis (contrColors c' a)).repr (S.reindex σ t) (Sum.rec φ @@ -484,7 +482,3 @@ lemma tensorBasis_repr_contr_two_pairs [Fintype κ] {a : Unit ⊕ Unit → Color rcases j with _ | (⟨⟨⟩⟩ | ⟨⟨⟩⟩) | (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> rfl) end Tensor - -end Species - -end Tensor diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 6d5b9c740c..9038f70a83 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -17,9 +17,9 @@ Tensor expressions with named indices, such as `{T | μ ν ⊗ U | ν ρ}ᵀ` fo A tensor `T` of a species `S` with colors `![c₀, …, cₙ₋₁]` is written `T | μ₀ … μₙ₋₁`, one name per index. The heights of the indices are not written, since the colors fix them. A real Lorentz -tensor `T : S.Tensor ![⟨vec, up⟩, ⟨vec, down⟩]`, with components `T^μ_ν`, is written `T | μ ν`. +tensor `T : Tensor S ![⟨vec, up⟩, ⟨vec, down⟩]`, with components `T^μ_ν`, is written `T | μ ν`. -An expression `{…}ᵀ` is a tensor in `S.Tensor ![…]`, or a proposition if it is an equation. The +An expression `{…}ᵀ` is a tensor in `Tensor S ![…]`, or a proposition if it is an equation. The names are bound only inside the braces. Each construct is an operation of the species. - `A ⊗ B` is `S.tmulEquiv _ _ (A ⊗ₜ B)`. @@ -225,12 +225,12 @@ def colorsTerm (cs : List Expr) : TermElabM Term := do `(![$((← cs.toArray.mapM exprToSyntax)),*]) /-- The species `S`, the index type `ι` and the color family `c` of a tensor type -`S.Tensor (ι := ι) c`. -/ +`Tensor S (ι := ι) c`. -/ def tensorTypeArgs? (ty : Expr) : Option (Expr × Expr × Expr) := - let_expr Tensor.Species.Tensor _ _ _ _ _ _ _ _ S ι c := ty | none + let_expr Tensor _ _ _ _ _ _ _ _ S ι c := ty | none some (S, ι, c) -/-- The index type, the color family `c` and its entries of a tensor type `S.Tensor c` with literal +/-- The index type, the color family `c` and its entries of a tensor type `Tensor S c` with literal indices and colors. -/ def literalTensorType? (ty : Expr) : MetaM (Option (Indices × Expr × List Expr)) := do let some (_, ι, c) := tensorTypeArgs? (← whnfR (← instantiateMVars ty)) | return none @@ -292,8 +292,8 @@ def tensorOf (T : Term) : TermElabM (Expr × Expr) := do if e.hasSyntheticSorry then throwAbortTerm let ty ← whnfR (← instantiateMVars (← inferType e)) if ty.isMVar then - throwError "index notation: the type of {T} is not known; give it, as `({T} : S.Tensor ![…])`" - if ty.isAppOf ``Tensor.Species.Tensor then return (e, ty) + throwError "index notation: the type of {T} is not known; give it, as `({T} : Tensor S ![…])`" + if ty.isAppOf ``Tensor then return (e, ty) try let e ← withoutErrToSorry do let e ← elabTerm (← `(Tensor.Tensorial.toTensor $(← exprToSyntax e))) none @@ -364,7 +364,7 @@ def contract (a : Node) : TermElabM Node := do let σ ← indexEquiv a.indices (.sum (.fin m) (.sum J J)) (free ++ pairs.map (·.1) ++ pairs.map (·.2)) let pairColors ← J.fn ``Tensor.prodColors (← pairs.mapM fun (i, _) => exprToSyntax a.colors[i]!) - let tm ← `(Tensor.Species.Tensor.contr (S := $(← exprToSyntax a.species)) + let tm ← `(Tensor.contr (S := $(← exprToSyntax a.species)) (Tensor.ColorEquiv.mk (c₂ := Tensor.contrColors $(← colorsTerm (free.map (a.colors[·]!))) $pairColors) $σ) $a.tm) @@ -439,7 +439,7 @@ def buildFor (e : Syntax) (expectedType? : Option Expr) : TermElabM Term := do let some ty := expectedType? | return (← build e).tm let ty ← instantiateMVars ty if ty.getAppFn.isMVar || ty.isSort then return (← build e).tm - if (← whnfR ty).isAppOf ``Tensor.Species.Tensor then + if (← whnfR ty).isAppOf ``Tensor then match ← buildTo e ty with | some tm => return tm | none => return (← build e).tm diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index 4734fb7073..b0aeda97f3 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -44,8 +44,8 @@ open Species variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} - (a c : Color K) (T U W : S.Tensor ![a, a.dual]) (T' : S.Tensor ![a.dual, a]) - (κ : K) (v : S.Tensor ![⟨κ, .up⟩]) (r : k) (Λ : G) + (a c : Color K) (T U W : Tensor S ![a, a.dual]) (T' : Tensor S ![a.dual, a]) + (κ : K) (v : Tensor S ![⟨κ, .up⟩]) (r : k) (Λ : G) /-! @@ -53,12 +53,12 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → -/ -/-- info: T : S.Tensor ![a, a.dual] -/ +/-- info: T : Tensor S ![a, a.dual] -/ #guard_msgs in #check {T | μ ν}ᵀ /-- The matrix product `T^μ_ν U^ν_ρ`. -/ -noncomputable example : S.Tensor ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ +noncomputable example : Tensor S ![a, a.dual] := {T | μ ν ⊗ U | ν ρ}ᵀ /-- A product given as one term, with the colors `prodColors ![a, a.dual] ![a, a.dual]`, is the product in the notation. -/ @@ -66,7 +66,7 @@ example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ} /-- An equation with a tensor `s` with no indices, `T^μ_μ = s`. The space after the bar keeps Mathlib's absolute value `|x|` from reading it. -/ -example (s : S.Tensor (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl +example (s : Tensor S (![] : Fin 0 → Color K)) : {T | μ μ = s | }ᵀ ↔ {T | μ μ}ᵀ = s := Iff.rfl /-- An equation in parentheses is the same equation. -/ example : {(T | μ ν = U | μ ν)}ᵀ ↔ {T | μ ν = U | μ ν}ᵀ := Iff.rfl @@ -75,11 +75,11 @@ example : {(T | μ ν = U | μ ν)}ᵀ ↔ {T | μ ν = U | μ ν}ᵀ := Iff.rfl example : Prop := {T | μ ν = T' | ν μ}ᵀ /-- The trace of a product of four matrices, `T^μ_ν U^ν_ρ T^ρ_σ U^σ_μ`. -/ -noncomputable example : S.Tensor (![] : Fin 0 → Color K) := +noncomputable example : Tensor S (![] : Fin 0 → Color K) := {T | μ ν ⊗ U | ν ρ ⊗ T | ρ σ ⊗ U | σ μ}ᵀ /-- The trace of a product of five matrices, `T^μ_ν U^ν_ρ W^ρ_σ T^σ_τ U^τ_μ`. -/ -noncomputable example : S.Tensor (![] : Fin 0 → Color K) := +noncomputable example : Tensor S (![] : Fin 0 → Color K) := {T | μ ν ⊗ U | ν ρ ⊗ W | ρ σ ⊗ T | σ τ ⊗ U | τ μ}ᵀ /-- Sums, negatives and scalar multiples, `-T^μ_ν + r U^μ_ν`. -/ @@ -105,7 +105,7 @@ failed to synthesize instance of type class #check {r | μ}ᵀ -- A term whose type is not known, such as a bare numeral, is rejected. -/-- error: index notation: the type of 0 is not known; give it, as `(0 : S.Tensor ![…])` -/ +/-- error: index notation: the type of 0 is not known; give it, as `(0 : Tensor S ![…])` -/ #guard_msgs in #check {0 | μ ν}ᵀ @@ -121,13 +121,13 @@ example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ : -- A tensor whose colors are not a literal list is rejected. /-- error: index notation: the colors of t must be a literal list -/ #guard_msgs in -example (f : Fin 2 → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ ν}ᵀ +example (f : Fin 2 → Color K) (t : Tensor S f) : Tensor S f := {t | μ ν}ᵀ -- A tensor whose index type is not literal is rejected. /-- error: index notation: the index type Fin n of t must be built from `Fin n` with a literal `n`, `Unit`, `Empty` and `⊕` -/ #guard_msgs (whitespace := lax) in -example (n : ℕ) (f : Fin n → Color K) (t : S.Tensor f) : S.Tensor f := {t | μ}ᵀ +example (n : ℕ) (f : Fin n → Color K) (t : Tensor S f) : Tensor S f := {t | μ}ᵀ -- A pair of indices whose colors are not dual is rejected. /-- error: index notation: μ joins the colors a and a, which are not dual -/ @@ -164,14 +164,14 @@ example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := toDualAt_toDualAt _ _ _ _ _ _ example : {v | τ(μ)}ᵀ = {v | ↓μ}ᵀ := rfl /-- `τ` needs no visible variance, so it flips an index of any color, `(w_μ)^μ = w^μ`. -/ -example (w : S.Tensor ![c]) : {{w | τ(μ)}ᵀ | τ(μ)}ᵀ = w := by +example (w : Tensor S ![c]) : {{w | τ(μ)}ᵀ | τ(μ)}ᵀ = w := by tensor_indices /-- Lowering commutes with the group action, `(Λ v)_μ = Λ (v_μ)`, by `rw [equiv_smul]`. -/ example : {(Λ • v) | ↓μ}ᵀ = Λ • {v | ↓μ}ᵀ := by rw [equiv_smul] /-- Lowering both indices commutes with the group action, `(Λ M)_μν = Λ (M_μν)`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {(Λ • M) | ↓μ ↓ν}ᵀ = Λ • {M | ↓μ ↓ν}ᵀ := by simp only [equiv_smul] /-- A trace commutes with the group action, `(Λ T)^μ_μ = Λ (T^μ_μ)`, by @@ -196,15 +196,15 @@ section Tensorial variable {M : Type} [AddCommGroup M] [Module k M] [Tensorial S ![a, a.dual] M] (m : M) -/-- info: Tensorial.toTensor m : S.Tensor ![a, a.dual] -/ +/-- info: Tensorial.toTensor m : Tensor S ![a, a.dual] -/ #guard_msgs in #check {m | μ ν}ᵀ /-- The contraction `m^μ_ν T^ν_ρ` of an element of a tensorial type with a tensor. -/ -noncomputable example : S.Tensor ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ +noncomputable example : Tensor S ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ /-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ -noncomputable example : S.Tensor ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ +noncomputable example : Tensor S ![a, a.dual] := {(m ⊗ₜ[k] m) | μ ν ν ρ}ᵀ /-- The contraction `m^μ_ν T^ν_ρ` read back as an element of `M`. -/ noncomputable example : M := {m | μ ν ⊗ T | ν ρ}ᵀ @@ -215,12 +215,12 @@ example : ({m | μ ν ⊗ m | ρ σ}ᵀ : M ⊗[k] M) = m ⊗ₜ m := by rw [← Tensorial.toTensor_tmul, Representation.Equiv.symm_apply_apply] /-- A tensor on `Fin 2` reindexed onto the indices `Fin 1 ⊕ Fin 1` of a product. -/ -noncomputable example : S.Tensor (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ +noncomputable example : Tensor S (prodColors ![a] ![a.dual]) := {T | μ ν}ᵀ /-- error: index notation: the free index μ has the color a, but the expected index has the color a.dual -/ #guard_msgs in -example : S.Tensor (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ +example : Tensor S (prodColors ![a.dual] ![a]) := {T | μ ν}ᵀ end Tensorial @@ -366,33 +366,33 @@ example : {v | μ = v | ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ ⊗ S.formTe tensor_regroup /-- A lowering cancels against the inverse form, `g^ρν M^μ_ν = M^μρ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by tensor_regroup /-- Two lowerings cancel against two inverse forms, one pair at a time, `g^ρμ g^σν M_μν = M^ρσ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ ⊗ S.formTensor ⟨κ, .up⟩ | σ ν = M | ρ σ}ᵀ := by tensor_regroup /-- The same with the pairs on the right side and on two kinds, `N^ρσ = g^ρμ g'^σν N_μν`. -/ -example (κ' : K) [S.HasForm κ'] (N : S.Tensor ![⟨κ, .up⟩, ⟨κ', .up⟩]) : +example (κ' : K) [S.HasForm κ'] (N : Tensor S ![⟨κ, .up⟩, ⟨κ', .up⟩]) : {N | ρ σ = N | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ μ ⊗ S.formTensor ⟨κ', .up⟩ | σ ν}ᵀ := by tensor_regroup /-- A lowering contracted with the first index of a symmetric form, `g^νρ M^μ_ν = M^μρ`. -/ -example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsSymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by tensor_regroup /-- With an antisymmetric form the second index cancels with no sign, `ε^ρν M^μ_ν = M^μρ`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsAntisymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ρ ν = M | μ ρ}ᵀ := by tensor_regroup /-- With an antisymmetric form the first index cancels with a sign, `ε^νρ M^μ_ν = -M^μρ`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsAntisymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = -M | μ ρ}ᵀ := by tensor_regroup @@ -402,7 +402,7 @@ the other side S.formTensor { kind := κ, variance := Variance.up }.dual S.formTensor { kind := κ, variance := Variance.up } -/ #guard_msgs in -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsAntisymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | ν ρ = M | μ ρ}ᵀ := by tensor_regroup @@ -414,7 +414,7 @@ the other side S.formTensor { kind := κ, variance := Variance.up } S.formTensor { kind := κ, variance := Variance.up } -/ #guard_msgs in -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | ↓μ ↓ν ⊗ S.formTensor ⟨κ, .up⟩ | μ ρ ⊗ S.formTensor ⟨κ, .up⟩ | ν σ = M | σ ρ}ᵀ := by tensor_regroup @@ -426,18 +426,18 @@ has no matching summand on the right. Against the last one tried: (S.toDualAt 0 { kind := κ, variance := Variance.up } ⋯ ⋯ ⋯) w has no equal factor on the right -/ #guard_msgs in -example (w : S.Tensor ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by +example (w : Tensor S ![⟨κ, .up⟩]) : {v | ↓μ + w | ↓μ = v | ↓μ + v | ↓μ}ᵀ := by tensor_regroup /-- Summands of opposite signs are matched by swapping an antisymmetric form, `M^μν g_νρ = -(g_ρν M^μν)`. -/ -example [S.IsAntisymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsAntisymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = -{S.formTensor ⟨κ, .down⟩ | ρ ν ⊗ M | μ ν}ᵀ | ρ μ}ᵀ := by tensor_regroup /-- A symmetric form is swapped when the regrouping fails, `M^μν g_νρ = M^μν g_ρν`. -/ -example [S.IsSymmetricForm κ] (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : +example [S.IsSymmetricForm κ] (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by tensor_regroup @@ -467,15 +467,15 @@ example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by -- An equation with no lowering or raising is rejected. /-- error: tensor_indices: neither side lowers or raises an index -/ #guard_msgs in -example (w : S.Tensor ![⟨κ, .up⟩]) : v = w := by +example (w : Tensor S ![⟨κ, .up⟩]) : v = w := by tensor_indices /-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by tensor_indices /-- Lowerings at two indices commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ -example (M : S.Tensor ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by tensor_indices end Metric diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 9856498d53..7090c82902 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -63,7 +63,7 @@ dsimproc_decl reduceCast (cast _ _) := fun e => do /-- `tensor_components [l₁, …]` reduces an equation of tensors to its components at a label `φ`, using the component lemmas `lᵢ` as well. It unfolds products, reindexings, sums, negatives, zeros, scalar multiples and contractions of one or two pairs. For more pairs, pass -`Species.Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal index that +`Tensor.tensorBasis_repr_contr`. It evaluates the `Function.update` at a literal index that `tensorBasis_repr_toDualAt` produces, and removes the casts on labels once their types agree. On a goal that is already a component the first step is skipped. -/ syntax (name := tensorComponents) "tensor_components" " [" Lean.Parser.Tactic.simpArg,* "]" : @@ -74,8 +74,8 @@ macro_rules try refine (Species.tensorBasis _ _).repr.injective (Finsupp.ext fun $(mkIdent `φ) => ?_) -- `Function.update_of_ne` proves `j ≠ i` for literal indices by `ne_eq` and `Fin.reduceEq`. simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, - Finsupp.zero_apply, Species.Tensor.tensorBasis_repr_contr_one_pair, - Species.Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, + Finsupp.zero_apply, Tensor.tensorBasis_repr_contr_one_pair, + Tensor.tensorBasis_repr_contr_two_pairs, Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, map_smul, Finsupp.smul_apply, smul_eq_mul, Function.update_self, Function.update_of_ne, ne_eq, Fin.reduceEq, not_false_eq_true, $ls,*] -- The casts of `tensorBasis_repr_reindex` agree once the reindexing and the color lists are @@ -98,10 +98,10 @@ private def goalEq (tac : String := "tensor_regroup") : TacticM (Expr × Expr × | throwError "{tac}: the goal is not an equation" return (ty, l.consumeMData, r.consumeMData) -/-- The field `k` of the tensors of the goal of `tensor_regroup`, an equation in `S.Tensor c`. -/ +/-- The field `k` of the tensors of the goal of `tensor_regroup`, an equation in `Tensor S c`. -/ private def goalField : TacticM Expr := do let (ty, _, _) ← goalEq - let_expr Tensor.Species.Tensor k _ _ _ _ _ _ _ _ _ _ := (← whnfR ty) + let_expr Tensor k _ _ _ _ _ _ _ _ _ _ := (← whnfR ty) | throwError "tensor_regroup: the goal is not an equation of tensors" return k @@ -242,27 +242,27 @@ private partial def isLeftNested (e : Expr) : Bool := /-- The products `P` and `Q` of a goal `Tensor.contr σ P = Tensor.contr τ Q`. -/ private def contractedProducts : TacticM (Expr × Expr) := do let (_, l, r) ← goalEq - let some p := bundledArg? ``Tensor.Species.Tensor.contr l + let some p := bundledArg? ``Tensor.contr l | throwError "tensor_regroup: the left side is not a contraction" - let some q := bundledArg? ``Tensor.Species.Tensor.contr r + let some q := bundledArg? ``Tensor.contr r | throwError "tensor_regroup: the right side is not a contraction" return (p, q) /-- Writes both sides of the goal as one contraction of a left-nested product, repeating until the goal no longer changes, at most ten times. A side that is not a contraction is read as one with no -pairs (`Species.Tensor.contr_noPairs`). The three steps run separately, since together their lemmas +pairs (`Tensor.contr_noPairs`). The three steps run separately, since together their lemmas loop. -/ private def flattenContractions : TacticM Unit := do - wrapSides "tensor_regroup" (bundledArg? ``Tensor.Species.Tensor.contr · |>.isSome) - (← `(Tensor.Species.Tensor.contr_noPairs _)) + wrapSides "tensor_regroup" (bundledArg? ``Tensor.contr · |>.isSome) + (← `(Tensor.contr_noPairs _)) for _ in [0:10] do let before ← instantiateMVars (← getMainTarget) - evalTactic (← `(tactic| try simp only [Tensor.Species.Tensor.tmulEquiv_contr_left, - Tensor.Species.Tensor.tmulEquiv_contr_right, Tensor.Species.Tensor.contr_contr])) + evalTactic (← `(tactic| try simp only [Tensor.tmulEquiv_contr_left, + Tensor.tmulEquiv_contr_right, Tensor.contr_contr])) evalTactic (← `(tactic| try simp only [← Tensor.Species.tmulEquiv_assoc])) evalTactic (← `(tactic| try simp only [Tensor.Species.tmulEquiv_reindex_left, Tensor.Species.tmulEquiv_reindex_right, Tensor.Species.reindex_reindex, - Tensor.Species.Tensor.contr_reindex, Tensor.Species.Tensor.reindex_contr])) + Tensor.contr_reindex, Tensor.reindex_contr])) -- The two sides may become equal, and `simp` then closes the goal. if (← getGoals).isEmpty then return if (← instantiateMVars (← getMainTarget)) == before then return @@ -281,7 +281,7 @@ private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do $(← exprToSyntax fs[m]!) $(← exprToSyntax fs[m+1]!)).symm) rewriteSubterm (leadingProduct q n (m + 2)) thm (some false) evalTactic (← `(tactic| conv_rhs => simp only [Tensor.Species.tmulEquiv_reindex_left, - Tensor.Species.reindex_reindex, Tensor.Species.Tensor.contr_reindex])) + Tensor.Species.reindex_reindex, Tensor.contr_reindex])) /-- The ways to match each factor on the left with a different factor on the right, given for each factor on the left the factors on the right equal to it. The matching in order comes first. -/ @@ -308,7 +308,7 @@ private def maxMatchings : Nat := 12 /-- Proves `Tensor.contr σ P = Tensor.contr τ Q` for two flattened contractions of the same factors. It orders the factors of the right side as on the left, and checks by evaluation that both sides keep the same indices and contract the same pairs of indices -(`Species.Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of +(`Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, at most `maxMatchings` of them. -/ private def matchFactors : TacticM Unit := do let (p, q) ← contractedProducts @@ -332,7 +332,7 @@ private def matchFactors : TacticM Unit := do -- checks are run by `of_decide_eq_true rfl` rather than `decide`, which rejects the free -- color variables of the goal although the bijections of indices do not depend on them. try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in - exact Tensor.Species.Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) + exact Tensor.contr_eq_of_samePair _ _ (of_decide_eq_true rfl) (of_decide_eq_true rfl) _)) catch _ => throwError "tensor_regroup: the two sides contract different pairs of indices, for \ {tried} of matching their equal factors" @@ -402,7 +402,7 @@ private def readContraction (e : Expr) : MetaM ReadContraction := do -- `a : J → Color K`. let notContr := m!"tensor_regroup: the side{indentExpr e}\nis not a contraction" let_expr DFunLike.coe _ _ _ _ F P := e | throwError notContr - let_expr Tensor.Species.Tensor.contr _ _ _ _ _ _ _ _ _ _ κ J _ _ _ _ τ := F | throwError notContr + let_expr Tensor.contr _ _ _ _ _ _ _ _ _ _ κ J _ _ _ _ τ := F | throwError notContr let some (indices, _, _) ← literalTensorType? (← inferType P) | throwError "tensor_regroup: the indices or colors of a product are not literal" let fs := factors P @@ -438,14 +438,14 @@ private def deltaOrientations (e : Expr) : MetaM (List Bool) := do return if found.isEmpty then [false, true] else found /-- Inserts a delta of color `a` at the index `index` of the product of the left side of the goal, -if `lhs`, or of the right side, by `Species.Tensor.contr_deltaTensor`. With `second`, uses -`Species.Tensor.contr_deltaTensor_swap` instead, inserting a delta of color `a.dual`. -/ +if `lhs`, or of the right side, by `Tensor.contr_deltaTensor`. With `second`, uses +`Tensor.contr_deltaTensor_swap` instead, inserting a delta of color `a.dual`. -/ private def insertDelta (lhs : Bool) (a : Expr) (indices : Indices) (index : Nat) (second : Bool) : TacticM Unit := do let (p, q) ← contractedProducts let P := if lhs then p else q - let lem := mkIdent <| if second then ``Tensor.Species.Tensor.contr_deltaTensor_swap - else ``Tensor.Species.Tensor.contr_deltaTensor + let lem := mkIdent <| if second then ``Tensor.contr_deltaTensor_swap + else ``Tensor.contr_deltaTensor let thm ← `(($lem ($(← indices.elem index) : $(← indices.type)) $(← exprToSyntax a) rfl $(← exprToSyntax P)).symm) rewriteSubterm P thm (some lhs) @@ -652,7 +652,7 @@ private def matchSummand (ty a : Expr) (rs : List Expr) (used : List Nat) : let js := (List.range rs.length).filter (!used.contains ·) try firstSuccess js m!"" fun j => do - -- The type of the tensors is taken from the goal, where it is `S.Tensor c`. + -- The type of the tensors is taken from the goal, where it is `Tensor S c`. let g ← mkFreshExprMVar (mkApp3 (.const ``Eq [← getLevel ty]) ty a rs[j]!) setGoals [g.mvarId!] regroupSummand @@ -865,14 +865,14 @@ the same factors, grouped or ordered differently, such as `{{T | μ ν ⊗ U | ν ρ}ᵀ | μ ρ ⊗ W | ρ τ = T | μ ν ⊗ {U | ν ρ ⊗ W | ρ τ}ᵀ | ν τ}ᵀ`. Sums, negatives and scalars are first moved out of every product, contraction and reindexing. Each summand on the left is then matched with a summand on the right with the same sign and scalar, -whose contraction is the same up to regrouping (`Species.Tensor.contr_eq_of_samePair`). When a +whose contraction is the same up to regrouping (`Tensor.contr_eq_of_samePair`). When a factor appears more than once, each way of matching the equal factors is tried in turn, so the number of attempts is the product of `k!` over the factors that appear `k` times, and at most 12 are tried (`maxMatchings`). Beyond regrouping, it uses these identities: - a delta on one side with no counterpart on the other is inserted on the other side at an index of - its color, `T^…μ… = T^…ν… δ_ν^μ` (`Species.Tensor.contr_deltaTensor`), or, against two forms on + its color, `T^…μ… = T^…ν… δ_ν^μ` (`Tensor.contr_deltaTensor`), or, against two forms on the other side, written as their contraction, `g^μν g_νρ = δ^μ_ρ` (`Species.contr_formTensor_formTensor`), and pairs of such forms against nothing, as a lowering `T_…ν… = T^…μ… g_μν` contracted with `g^νρ`, are matched one pair at a time by inserting a delta @@ -884,7 +884,7 @@ Beyond regrouping, it uses these identities: (`Species.toDualAt_eq_contr`) and the matching is tried again. A side that is not a contraction, such as a single tensor, is read as a contraction with no pairs -(`Species.Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional +(`Tensor.contr_noPairs`). The scalars on the two sides must agree up to definitional equality, as `2` and `2`. `tensor_regroup using e₁, …, eₙ` first rewrites by the equations `eᵢ : Aᵢ = Bᵢ`, in turn. For each, diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 10b0052d7c..ecef273211 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -48,11 +48,11 @@ identity. - `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. - `Species.deltaTensor` : the delta as a tensor on two indices, with `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. -- `Species.Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, and - `Species.Tensor.contr_matMul_eq_deltaTensor_iff` : it is the delta exactly when the linear maps +- `Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, and + `Tensor.contr_matMul_eq_deltaTensor_iff` : it is the delta exactly when the linear maps of `M` and `N` are inverse. -- `Species.Tensor.contr_matMulAt` : contracting the index `i` with the first index of `F` applies - `F` at `i`, and `Species.Tensor.contr_deltaTensor` : for the delta this is +- `Tensor.contr_matMulAt` : contracting the index `i` with the first index of `F` applies + `F` at `i`, and `Tensor.contr_deltaTensor` : for the delta this is `T^…μ… δ_μ^ν = T^…ν…`. ## iii. Table of contents @@ -127,11 +127,11 @@ def matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) end ColorEquiv -namespace Species - variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : Species k G K basisIdx) +namespace Species + /-! ## B. Tensors of two factors as linear maps @@ -217,7 +217,7 @@ variable {S} variable (S) in /-- The delta `S.delta c` as a tensor with colors `![c.dual, c]`. It is `δ_μ^ν` for an upper color `c` and `δ^μ_ν` for a lower one. -/ -noncomputable def deltaTensor (c : Color K) : S.Tensor ![c.dual, c] := +noncomputable def deltaTensor (c : Color K) : Tensor S ![c.dual, c] := S.twoIndexEquiv c.dual c (S.delta c) /-- The delta tensor is invariant, `Λ δ = δ`. -/ @@ -240,9 +240,11 @@ lemma tensorBasis_repr_deltaTensor_eq_one (c : Color K) (tensorBasis_repr_deltaTensor c μ).trans ((Module.Basis.tensorProduct_repr_coordDelta _ _ _ _).trans Matrix.one_apply.symm) -namespace Tensor +end Species -open TensorProduct PiTensorProduct +open Species PiTensorProduct + +variable {S} variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} @@ -296,10 +298,10 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι induction F using TensorProduct.inductionOn with | add F F' hF hF' => simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] - rw [pure, pure, pure, MultilinearMap.map_update_add, map_add] + rw [Species.pure, Species.pure, Species.pure, MultilinearMap.map_update_add, map_add] | tmul x y => rw [Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, map_smul] - conv_rhs => rw [pure, MultilinearMap.map_update_smul, map_smul, ← pure] + conv_rhs => rw [Species.pure, MultilinearMap.map_update_smul, map_smul, ← Species.pure] rw [twoIndexEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] congr 2 funext z @@ -317,7 +319,7 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι `F`, read as a linear map by `Species.toLin`, at `i`, as in `T^…μ… F_μ^ν`. -/ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) (F : S.V a.dual ⊗[k] S.V b) - (t : S.Tensor c) : + (t : Tensor S c) : contr (ColorEquiv.matMulAt i a b ha hi h) (S.tmulEquiv c ![a.dual, b] (t ⊗ₜ S.twoIndexEquiv a.dual b F)) = S.mapAt i ((S.cast hi.symm).toLinearMap ∘ₗ S.toLin F ∘ₗ (S.cast ha).toLinearMap) h t := by @@ -332,7 +334,7 @@ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) /-- Contracting the index `i` with the first index of the delta is the identity, `T^…μ… δ_μ^ν = T^…ν…`. -/ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) - (t : S.Tensor c) : + (t : Tensor S c) : contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.deltaTensor a)) = t := by subst ha @@ -342,7 +344,7 @@ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) /-- Contracting the index `i` with the second index of the delta is the identity, `δ^ν_μ T^…μ… = T^…ν…`. -/ lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) - (ha : c i = a) (t : S.Tensor c) : + (ha : c i = a) (t : Tensor S c) : contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by @@ -351,7 +353,3 @@ lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι exact contr_deltaTensor i a ha t end Tensor - -end Species - -end Tensor diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 46b9c50ef0..40e52e95d8 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -261,7 +261,7 @@ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basis variable (S) in /-- The form at `c` as a tensor with colors `![c, c]`, `g^μν` at an upper color and `g_μν` at a lower one. -/ -noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : S.Tensor ![c, c] := +noncomputable def formTensor (c : Color K) [S.HasForm c.kind] : Tensor S ![c, c] := S.twoIndexEquiv c c (form c) /-- The form is an invariant tensor. Every group element fixes it, as every Lorentz @@ -308,7 +308,7 @@ noncomputable def toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : /-- Raising at an index undoes lowering at that index, `(T^μ_ν)^ν = T^μν`. -/ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) - [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) : S.toDualAt i a.dual hi ha (fun x hx => (h x hx).symm) (S.toDualAt i a ha hi h t) = t := by rw [toDualAt, toDualAt, show ((S.cast hi).trans (S.toDual a.dual)).trans (S.cast ha.symm) = (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).symm from rfl, ← congrAt_symm] @@ -317,7 +317,7 @@ lemma toDualAt_toDualAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i /-- Lowering or raising at an index is `toDual` at that index, read as a linear map. Chains of lowerings and raisings at several indices compose index by index through `mapIndices_mapIndices`. -/ lemma toDualAt_eq_mapAt {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) - [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) : S.toDualAt i a ha hi h t = S.mapAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)).toLinearMap h t := congrAt_eq_mapAt i _ h t @@ -327,7 +327,7 @@ labels `φ` of the result are cast to labels of `t`, and the label at `i` is rep `μ`. For concrete colors the casts are along `rfl`. -/ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) - (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) : + (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) : (S.tensorBasis c').repr (S.toDualAt i a ha hi h t) φ = ∑ μ, S.formMatrix a.dual (cast (congrArg (basisIdx ∘ Color.kind) hi) (φ i)) μ * (S.tensorBasis c).repr t (Function.update (fun x => cast (by @@ -346,7 +346,7 @@ lemma tensorBasis_repr_toDualAt [Fintype ι] {c c' : ι → Color K} (i : ι) (a /-- Lowering or raising at the index `i` is contracting that index with the second index of the form, `T^…_ν… = g_νμ T^…μ…`. -/ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i = a) - [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) : S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a.dual a.dual) (S.formTensor a.dual))) := by diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index adbae55076..1eb6c465ff 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -154,7 +154,7 @@ lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) /-- The product is associative, `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)`, after the indices of the left side are regrouped by `ColorEquiv.sumAssoc`. -/ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) - (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : + (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) (t₃ : Tensor S c₃) : S.reindex (ColorEquiv.sumAssoc c₁ c₂ c₃) (S.tmulEquiv _ _ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃)) = S.tmulEquiv _ _ (t₁ ⊗ₜ S.tmulEquiv c₂ c₃ (t₂ ⊗ₜ t₃)) := @@ -171,8 +171,8 @@ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c /-- The product is symmetric, `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`, after the indices of `U` are moved in front by `ColorEquiv.sumComm`. -/ -lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ : S.Tensor c₁) - (t₂ : S.Tensor c₂) : +lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ : Tensor S c₁) + (t₂ : Tensor S c₂) : S.reindex (ColorEquiv.sumComm c₁ c₂) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = S.tmulEquiv c₂ c₁ (t₂ ⊗ₜ t₁) := LinearMap.congr_fun (f := (S.reindex (ColorEquiv.sumComm c₁ c₂)).toLinearMap ∘ₗ @@ -186,7 +186,7 @@ lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ /-- Reindexing the product along `σ.sumCongr τ` is reindexing its factors along `σ` and `τ`. -/ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₁ c₁') (τ : ColorEquiv c₂ c₂') - (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) : S.reindex (σ.sumCongr τ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) = S.tmulEquiv c₁' c₂' (S.reindex σ t₁ ⊗ₜ S.reindex τ t₂) := LinearMap.congr_fun (f := (S.reindex (σ.sumCongr τ)).toLinearMap ∘ₗ @@ -201,7 +201,7 @@ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → /-- Reindexing the first factor reindexes the product. With `tmulEquiv_reindex_right` it moves reindexings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} - (σ : ColorEquiv c₁ c₁') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + (σ : ColorEquiv c₁ c₁') (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) : S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = S.reindex (σ.sumCongr (.refl c₂)) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] @@ -209,7 +209,7 @@ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Colo /-- Reindexing the second factor reindexes the product. With `tmulEquiv_reindex_left` it moves reindexings out of products, the form used by index notation. -/ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} - (σ : ColorEquiv c₂ c₂') (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) : + (σ : ColorEquiv c₂ c₂') (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) : S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = S.reindex ((ColorEquiv.refl c₁).sumCongr σ) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] @@ -217,7 +217,7 @@ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color /-- Swapping the last two factors of a product, `(T ⊗ W) ⊗ U = (T ⊗ U) ⊗ W` after the indices of `U` are moved before those of `W` by `ColorEquiv.swapLast`. -/ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} - (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) (t₃ : S.Tensor c₃) : + (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) (t₃ : Tensor S c₃) : S.reindex (ColorEquiv.swapLast c₁ c₂ c₃) (S.tmulEquiv _ c₂ (S.tmulEquiv c₁ c₃ (t₁ ⊗ₜ t₃) ⊗ₜ t₂)) = S.tmulEquiv _ c₃ (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) ⊗ₜ t₃) := by @@ -227,8 +227,8 @@ lemma tmulEquiv_swapLast {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} { /-- The product commutes with the group action, `Λ T ⊗ Λ U = Λ (T ⊗ U)`, as in `(Λ^μ_α T^α) (Λ^ρ_β U^β) = Λ^μ_α Λ^ρ_β (T ⊗ U)^αβ`. -/ -lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : G) (t₁ : S.Tensor c₁) - (t₂ : S.Tensor c₂) : +lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : G) (t₁ : Tensor S c₁) + (t₂ : Tensor S c₂) : S.tmulEquiv c₁ c₂ ((g • t₁) ⊗ₜ (g • t₂)) = g • S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂) := Representation.IntertwiningMap.isIntertwining _ _ (S.tmulEquiv c₁ c₂).toIntertwiningMap g (t₁ ⊗ₜ t₂) @@ -239,7 +239,7 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : A species gives objects with two indices, such as a metric `η_μν`, in Mathlib's binary tensor product `V a ⊗ V b`. `twoIndexEquiv` identifies this space with the tensors -`S.Tensor ![a, b]` on two indices, so that such an object can be used in index notation. +`Tensor S ![a, b]` on two indices, so that such an object can be used in index notation. -/ @@ -306,7 +306,7 @@ of a tensor on two indices are the coordinates in Mathlib's basis `Basis.tensorP /-- The components of a product are the products of the components, `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. The labels `y (inl i)` are those of `T` and the labels `y (inr j)` those of `U`. -/ lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ → Color K) - (c₂ : ι₂ → Color K) (t₁ : S.Tensor c₁) (t₂ : S.Tensor c₂) + (c₂ : ι₂ → Color K) (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) (y : Π s, basisIdx (prodColors c₁ c₂ s).kind) : (S.tensorBasis (prodColors c₁ c₂)).repr (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) y = (S.tensorBasis c₁).repr t₁ (fun i => y (.inl i)) * diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 06c0f7f59d..0ee028429a 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -181,7 +181,7 @@ lemma reindex_trans {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ /-- Reindexing by `σ₁` and then by `σ₂` is reindexing by `σ₁.trans σ₂`, on a tensor. -/ lemma reindex_reindex {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} - (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : S.Tensor c₁) : + (σ₁ : ColorEquiv c₁ c₂) (σ₂ : ColorEquiv c₂ c₃) (t : Tensor S c₁) : S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by rw [reindex_trans, Representation.Equiv.trans_apply] @@ -203,7 +203,7 @@ lemma reindex_symm {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} (σ : C old index `i` takes the label `y (σ.toEquiv i)` of its new index, cast along the equality of their colors. For concrete colors and labels the cast is between equal types, and `rfl` removes it. -/ lemma tensorBasis_repr_reindex [Fintype ι₁] [Fintype ι₂] {c₁ : ι₁ → Color K} - {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (t : S.Tensor c₁) + {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₂) (t : Tensor S c₁) (y : Π j, basisIdx (c₂ j).kind) : (S.tensorBasis c₂).repr (S.reindex σ t) y = (S.tensorBasis c₁).repr t fun i => diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index f805f1af4e..82f924adf6 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -18,7 +18,7 @@ index type `ι`, and the group acts on them index by index. ## i. Overview A tensor of a species `S` has one index `i` for each `i : ι`, and the index `i` has the color `c i`. -The tensors with colors `c : ι → Color K` form the space `S.Tensor c`, which is Mathlib's +The tensors with colors `c : ι → Color K` form the space `Tensor S c`, which is Mathlib's `⨂[k] i, V (c i)`. The index type `ι` can be any type, such as `Fin n` or a sum `ι₁ ⊕ ι₂`. A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in @@ -28,7 +28,7 @@ a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). Maps of tensors, starting with the group action, are given by their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's -`⨂ₜ[k] i, f i` as an element of `S.Tensor c`. +`⨂ₜ[k] i, f i` as an element of `Tensor S c`. A group element `Λ` acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). A map that commutes with `Λ` on pure tensors @@ -49,7 +49,7 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t ## ii. Key results -- `Species.Tensor` and `Species.pure` are the tensors and the pure tensors, and +- `Tensor` and `Species.pure` are the tensors and the pure tensors, and `Species.inductionOn_pure` is induction through pure tensors. - `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal, `Species.ext_pure₂` and `Species.ext_pure₃` do the same for maps out of products of two and @@ -107,30 +107,30 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → -/ /-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not -an `abbrev`, so that the type `S.Tensor c` keeps the species and the colors instead of unfolding to +an `abbrev`, so that the type `Tensor S c` keeps the species and the colors instead of unfolding to `⨂[k] i, S.V (c i)`. -/ -def Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := +def _root_.Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := ⨂[k] i, S.V (c i) noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : - AddCommGroup (S.Tensor c) := + AddCommGroup (Tensor S c) := inferInstanceAs (AddCommGroup (⨂[k] i, S.V (c i))) noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : - Module k (S.Tensor c) := + Module k (Tensor S c) := inferInstanceAs (Module k (⨂[k] i, S.V (c i))) variable {ι : Type} {c : ι → Color K} -/-- The identity from `S.Tensor c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. +/-- The identity from `Tensor S c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. Mathlib's constructions on `⨂` are composed with it. -/ noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : - S.Tensor c ≃ₗ[k] ⨂[k] i, S.V (c i) := + Tensor S c ≃ₗ[k] ⨂[k] i, S.V (c i) := LinearEquiv.refl k _ /-- The pure tensor of a family of vectors, one per index, such as `u ⊗ α ⊗ β` with components `u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ -noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : S.Tensor c := +noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : Tensor S c := (S.toPiTensor c).symm (⨂ₜ[k] i, f i) variable {S : Species k G K basisIdx} @@ -143,28 +143,28 @@ lemma toPiTensor_symm_tprod (f : Π i, S.V (c i)) : (S.toPiTensor c).symm (⨂ₜ[k] i, f i) = S.pure f := rfl /-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ -lemma inductionOn_pure {P : S.Tensor c → Prop} +lemma inductionOn_pure {P : Tensor S c → Prop} (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) - (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : S.Tensor c) : P t := + (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : Tensor S c) : P t := PiTensorProduct.induction_on (motive := P) t (fun r f => hsmul r _ (h f)) hadd /-- Two linear maps out of tensors are equal when they agree on pure tensors. -/ -lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : S.Tensor c →ₗ[k] E} +lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : Tensor S c →ₗ[k] E} (h : ∀ p : Π i, S.V (c i), f (S.pure p) = g (S.pure p)) : f = g := PiTensorProduct.ext (MultilinearMap.ext h) -/-- Two linear maps out of `S.Tensor c ⊗ S.Tensor c₂` are equal when they agree on the products +/-- Two linear maps out of `Tensor S c ⊗ Tensor S c₂` are equal when they agree on the products `S.pure p ⊗ₜ S.pure q` of pure tensors. -/ lemma ext_pure₂ {ι₂ : Type} {c₂ : ι₂ → Color K} {E : Type} [AddCommGroup E] [Module k E] - {f g : S.Tensor c ⊗[k] S.Tensor c₂ →ₗ[k] E} + {f g : Tensor S c ⊗[k] Tensor S c₂ →ₗ[k] E} (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)), f (S.pure p ⊗ₜ S.pure q) = g (S.pure p ⊗ₜ S.pure q)) : f = g := TensorProduct.ext <| ext_pure fun p => ext_pure fun q => h p q -/-- Two linear maps out of `(S.Tensor c ⊗ S.Tensor c₂) ⊗ S.Tensor c₃` are equal when they agree +/-- Two linear maps out of `(Tensor S c ⊗ Tensor S c₂) ⊗ Tensor S c₃` are equal when they agree on the products `(S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r` of pure tensors. -/ lemma ext_pure₃ {ι₂ ι₃ : Type} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} {E : Type} - [AddCommGroup E] [Module k E] {f g : (S.Tensor c ⊗[k] S.Tensor c₂) ⊗[k] S.Tensor c₃ →ₗ[k] E} + [AddCommGroup E] [Module k E] {f g : (Tensor S c ⊗[k] Tensor S c₂) ⊗[k] Tensor S c₃ →ₗ[k] E} (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)) (r : Π l, S.V (c₃ l)), f ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r) = g ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r)) : f = g := @@ -178,8 +178,8 @@ lemma ext_pure₃ {ι₂ ι₃ : Type} {c₂ : ι₂ → Color K} {c₃ : ι₃ /-- The group acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. -/ -noncomputable instance : DistribMulAction G (S.Tensor c) := - DistribMulAction.compHom (M := Module.End k (S.Tensor c)) _ +noncomputable instance : DistribMulAction G (Tensor S c) := + DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ (PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi fun i => S.rep (c i))) @[simp] @@ -187,38 +187,38 @@ lemma smul_pure (g : G) (f : Π i, S.V (c i)) : g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := PiTensorProduct.map_tprod _ _ -noncomputable instance : SMulCommClass G k (S.Tensor c) where +noncomputable instance : SMulCommClass G k (Tensor S c) where smul_comm g r t := by change PiTensorProduct.map (fun i => S.rep (c i) g) (r • t) = r • PiTensorProduct.map _ t exact map_smul _ r t -noncomputable instance : SMulCommClass k G (S.Tensor c) := .symm _ _ _ +noncomputable instance : SMulCommClass k G (Tensor S c) := .symm _ _ _ variable (S) in /-- The action of the group on tensors as Mathlib's representation, `Λ ↦ (T ↦ Λ • T)`. Maps of tensors that commute with the action are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` for it. -/ -noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (S.Tensor c) := - Representation.ofDistribMulAction k G (S.Tensor c) +noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor S c) := + Representation.ofDistribMulAction k G (Tensor S c) /-- An intertwining map from or to tensors commutes with the actions, `φ (Λ v) = Λ φ v`. -/ lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} - (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : S.Tensor c) : + (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := Representation.IntertwiningMap.isIntertwining _ _ φ g t /-- An equivalence of representations of tensors commutes with the actions, `φ (Λ T) = Λ φ T`. -/ lemma equiv_smul {ι' : Type} {c' : ι' → Color K} - (φ : (S.tensorRep c).Equiv (S.tensorRep c')) (g : G) (t : S.Tensor c) : + (φ : (S.tensorRep c).Equiv (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := intertwiningMap_smul φ.toIntertwiningMap g t /-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes with `Λ` on all tensors. -/ lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} - [FunLike F (S.Tensor c) (S.Tensor c')] [SemilinearMapClass F σ (S.Tensor c) (S.Tensor c')] + [FunLike F (Tensor S c) (Tensor S c')] [SemilinearMapClass F σ (Tensor S c) (Tensor S c')] (f : F) (g : G) (h : ∀ p : Π i, S.V (c i), f (g • S.pure p) = g • f (S.pure p)) - (t : S.Tensor c) : f (g • t) = g • f t := by + (t : Tensor S c) : f (g • t) = g • f t := by induction t using inductionOn_pure with | h p => exact h p | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] @@ -227,28 +227,28 @@ lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : variable {ι' : Type} {c' : ι' → Color K} in /-- A linear equivalence of tensors that commutes with `Λ` on pure tensors, as an equivalence of representations. -/ -noncomputable def equivOfPure (e : S.Tensor c ≃ₗ[k] S.Tensor c') +noncomputable def equivOfPure (e : Tensor S c ≃ₗ[k] Tensor S c') (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) : (S.tensorRep c).Equiv (S.tensorRep c') := .mk e fun g => LinearMap.ext fun t => map_smul_of_pure e g (h g) t variable {ι' : Type} {c' : ι' → Color K} in @[simp] -lemma equivOfPure_apply (e : S.Tensor c ≃ₗ[k] S.Tensor c') - (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) (t : S.Tensor c) : +lemma equivOfPure_apply (e : Tensor S c ≃ₗ[k] Tensor S c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) (t : Tensor S c) : equivOfPure e h t = e t := rfl variable {ι' : Type} {c' : ι' → Color K} in /-- A linear map of tensors that commutes with `Λ` on pure tensors, as an intertwining map. -/ -noncomputable def intertwiningMapOfPure (f : S.Tensor c →ₗ[k] S.Tensor c') +noncomputable def intertwiningMapOfPure (f : Tensor S c →ₗ[k] Tensor S c') (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) : (S.tensorRep c).IntertwiningMap (S.tensorRep c') := ⟨f, fun g => LinearMap.ext fun t => map_smul_of_pure f g (h g) t⟩ variable {ι' : Type} {c' : ι' → Color K} in @[simp] -lemma intertwiningMapOfPure_apply (f : S.Tensor c →ₗ[k] S.Tensor c') - (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) (t : S.Tensor c) : +lemma intertwiningMapOfPure_apply (f : Tensor S c →ₗ[k] Tensor S c') + (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) (t : Tensor S c) : intertwiningMapOfPure f h t = f t := rfl /-- Two equivalences of representations out of tensors are equal when they agree on pure @@ -266,7 +266,7 @@ lemma intertwiningMap_ext_pure {W : Type} [AddCommGroup W] [Module k W] Representation.IntertwiningMap.ext (ext_pure h) /-- A tensor with no indices is invariant, `Λ s = s`. -/ -lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = t := by +lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = t := by induction t using inductionOn_pure with | h f => rw [smul_pure]; exact congrArg _ (funext isEmptyElim) | hsmul r t ht => rw [smul_comm, ht] @@ -281,7 +281,7 @@ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : S.Tensor c) : g • t = /-- The basis `e_μ ⊗ e^ν ⊗ …` of tensors, Mathlib's `Basis.piTensorProduct` of the bases of the colors. -/ noncomputable def tensorBasis (S : Species k G K basisIdx) [Fintype ι] (c : ι → Color K) : - Module.Basis (Π i, basisIdx (c i).kind) k (S.Tensor c) := + Module.Basis (Π i, basisIdx (c i).kind) k (Tensor S c) := (Basis.piTensorProduct fun i => S.basis (c i)).map (S.toPiTensor c).symm /-- A basis tensor is the pure tensor of basis vectors, `e_μ ⊗ e^ν ⊗ …`. -/ @@ -296,17 +296,17 @@ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] /-- Tensors on finitely many indices are a finite-dimensional space. -/ -noncomputable instance [Fintype ι] : Module.Finite k (S.Tensor c) := +noncomputable instance [Fintype ι] : Module.Finite k (Tensor S c) := .of_basis (S.tensorBasis c) /-- The dimension of tensors is the product of the dimensions of their indices. -/ lemma finrank_tensor [Fintype ι] [DecidableEq ι] (c : ι → Color K) : - Module.finrank k (S.Tensor c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by + Module.finrank k (Tensor S c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by rw [Module.finrank_eq_card_basis (S.tensorBasis c), Fintype.card_pi] /-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of `g` per index, in the color of that index. -/ -lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : S.Tensor c) +lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S c) (φ : Π i, basisIdx (c i).kind) : (S.tensorBasis c).repr (g • t) φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) (S.rep (c i) g) (φ i) (ψ i)) * (S.tensorBasis c).repr t ψ := by @@ -328,7 +328,7 @@ variable {c' : ι → Color K} variable (S) in /-- A linear map `f x` at every index `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ -noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : S.Tensor c →ₗ[k] S.Tensor c' := +noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : Tensor S c →ₗ[k] Tensor S c' := (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap /-- On a pure tensor, `mapIndices` applies `f x` to the vector at each index `x`. -/ @@ -345,7 +345,7 @@ lemma mapIndices_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V /-- Two index-wise maps in sequence are the index-wise map of the composites, on a tensor. -/ lemma mapIndices_mapIndices {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) - (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : S.Tensor c) : + (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) (t : Tensor S c) : S.mapIndices g (S.mapIndices f t) = S.mapIndices (fun x => g x ∘ₗ f x) t := LinearMap.congr_fun (mapIndices_comp f g) t @@ -354,13 +354,13 @@ lemma mapIndices_id : S.mapIndices (c := c) (fun _ => LinearMap.id) = LinearMap. ext_pure fun p => mapIndices_pure _ p /-- The index-wise map of the identities is the identity, on a tensor. -/ -lemma mapIndices_id_apply (t : S.Tensor c) : S.mapIndices (fun _ => LinearMap.id) t = t := +lemma mapIndices_id_apply (t : Tensor S c) : S.mapIndices (fun _ => LinearMap.id) t = t := LinearMap.congr_fun mapIndices_id t /-- Linear maps that commute with the group action at every index commute with it on tensors, `f (Λ T) = Λ f T`. -/ lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} - (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : S.Tensor c) : + (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : Tensor S c) : S.mapIndices f (g • t) = g • S.mapIndices f t := map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapIndices_pure, hf]) t @@ -420,7 +420,7 @@ variable (S) in /-- The linear map `f` at the index `i`, and the identity, moved along `h x`, at every other index `x`, as in `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. -/ noncomputable def mapAt (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) - (h : ∀ x, x ≠ i → c x = c' x) : S.Tensor c →ₗ[k] S.Tensor c' := + (h : ∀ x, x ≠ i → c x = c' x) : Tensor S c →ₗ[k] Tensor S c' := S.mapIndices (S.indexMapsAt i f h) /-- On a pure tensor, `mapAt` applies `f` to the vector at `i` and keeps the others, @@ -454,7 +454,7 @@ lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : /-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. -/ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) - (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) (φ : Π x, basisIdx (c' x).kind) + (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) (ψ : Π x, basisIdx (c x).kind) (hψ : ∀ x (hx : x ≠ i), ψ x = cast (congrArg (basisIdx ∘ Color.kind) (h x hx).symm) (φ x)) : (S.tensorBasis c').repr (S.mapAt i f h t) φ = ∑ μ : basisIdx (c i).kind, @@ -495,7 +495,7 @@ noncomputable def congrAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) /-- `congrAt` is `mapAt` of the underlying linear map. -/ lemma congrAt_eq_mapAt (i : ι) (f : (S.rep (c i)).Equiv (S.rep (c' i))) - (h : ∀ x, x ≠ i → c x = c' x) (t : S.Tensor c) : + (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) : S.congrAt i f h t = S.mapAt i f.toLinearMap h t := rfl /-- The inverse of `congrAt` of `f` is `congrAt` of `f.symm`. -/ diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 762bc6fbae..08323ea30c 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -18,7 +18,7 @@ tensors of a species with given colors, compatibly with the action. A Lorentz vector `v` can be stored as its components `v^μ`, on which Mathlib's analysis acts, and is also a tensor with one upper index, on which index notation acts. An instance `Tensorial S c M` relates the two by a Mathlib `Representation.Equiv` -`toTensor : M → S.Tensor c`, so that `toTensor (Λ v) = Λ • toTensor v`. +`toTensor : M → Tensor S c`, so that `toTensor (Λ v) = Λ • toTensor v`. The action on `M` is the field `rep`, not an instance `SMul G M`, since Mathlib would extend such an instance to `M ⊗ N` acting on the left factor only. The species and colors are determined by @@ -67,7 +67,7 @@ namespace Tensor -/ /-- A type `M` whose elements are the tensors of `S` with colors `c`. `M` has its own action of -the group, `rep`, and `toTensor` identifies `M` with `S.Tensor c` compatibly with it. -/ +the group, `rep`, and `toTensor` identifies `M` with `Tensor S c` compatibly with it. -/ class Tensorial {k : outParam Type} [RCLike k] {G : outParam Type} [Group G] {K : outParam Type} {basisIdx : outParam (K → Type)} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : outParam (Species k G K basisIdx)) {ι : outParam Type} @@ -75,7 +75,7 @@ class Tensorial {k : outParam Type} [RCLike k] {G : outParam Type} [Group G] {K /-- The action of the group on `M`, such as `Λ v = Λ *ᵥ v` on Lorentz vectors. -/ rep : Representation k G M /-- The elements of `M` as tensors, compatibly with the action. -/ - toTensor : rep.Equiv (Representation.ofDistribMulAction k G (S.Tensor c)) + toTensor : rep.Equiv (Representation.ofDistribMulAction k G (Tensor S c)) namespace Tensorial diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 1955d2758a..f5ceafe5b2 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -230,10 +230,10 @@ are read in the namespace `complexLorentz`. The notation is active under scoped syntax (name := complexLorentzSyntax) "ℂT[" term,* "]" : term macro_rules (kind := complexLorentzSyntax) - | `(ℂT[$cs,*]) => `(Tensor.complexLorentz.Tensor (open Tensor.complexLorentz in ![$cs,*])) + | `(ℂT[$cs,*]) => `(Tensor Tensor.complexLorentz (open Tensor.complexLorentz in ![$cs,*])) /-- `ℂT[upL, downR]` is the space of tensors `T^α_β̇`. -/ -example : ℂT[upL, downR] = complexLorentz.Tensor ![upL, downR] := rfl +example : ℂT[upL, downR] = Tensor complexLorentz ![upL, downR] := rfl end complexLorentz @@ -418,11 +418,11 @@ instance : complexLorentz.IsEquivariantConjugation := /-- The components of the conjugate of a tensor are the conjugates of its components, at the same labels read through `idxBar`. -/ lemma tensorBasis_repr_conj {ι : Type} [Fintype ι] {c c' : ι → Color Kind} - (h : ∀ i, c' i = (c i).bar) (t : complexLorentz.Tensor c) (μ : Π i, Idx (c' i).kind) : - (complexLorentz.tensorBasis c').repr (Species.Tensor.conj h t) μ = + (h : ∀ i, c' i = (c i).bar) (t : Tensor complexLorentz c) (μ : Π i, Idx (c' i).kind) : + (complexLorentz.tensorBasis c').repr (Tensor.conj h t) μ = star ((complexLorentz.tensorBasis c).repr t fun i => (idxBar (congrArg Color.kind (h i))).symm (μ i)) := - Species.Tensor.tensorBasis_repr_conj_of_basis h _ + Tensor.tensorBasis_repr_conj_of_basis h _ (fun i => Species.HasConjugation.ofBasis_conjEquiv_basis _ (h i)) t μ end complexLorentz @@ -650,14 +650,14 @@ lemma tensorBasis_repr_formTensor_upL_tmul_upR /-- `(ε^αβ ε_βγ)` at `α = γ = 0` is `1`, from the components of `εL` and `εL'`. -/ example : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εL' | β γ}ᵀ (Fin.cons (0 : Fin 2) (Fin.cons (0 : Fin 2) fun i => i.elim0)) = 1 := by - rw [Species.Tensor.tensorBasis_repr_contr_one_pair] + rw [Tensor.tensorBasis_repr_contr_one_pair] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor upL, tensorBasis_repr_formTensor downL] change ∑ β : Fin 2, metricRaw 0 β * (-metricRaw) β 0 = 1 simp [metricRaw, Fin.sum_univ_two] /-- The conjugate of `ε^αβ` is `ε^α̇β̇`. The conjugate lands on `![upR, upR]` directly. -/ -example : Species.Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by +example : Tensor.conj (fun i => by fin_cases i <;> rfl) εL = εR := by refine (complexLorentz.tensorBasis _).repr.injective (Finsupp.ext fun μ => ?_) rw [tensorBasis_repr_conj, tensorBasis_repr_formTensor upL, tensorBasis_repr_formTensor upR] -- The labels are `idxBar` along `rfl` of `μ 0` and `μ 1`, the identity. diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 929ed8ee7f..db664064da 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -182,8 +182,8 @@ abbrev down : Color Kind := ⟨.vec, .down⟩ scoped syntax (name := realLorentzSyntax) "ℝT[" term,+ "]" : term macro_rules (kind := realLorentzSyntax) - | `(ℝT[$d]) => `((Tensor.realLorentz $d).Tensor ![]) - | `(ℝT[$d, $cs,*]) => `((Tensor.realLorentz $d).Tensor (open Tensor.realLorentz in ![$cs,*])) + | `(ℝT[$d]) => `(Tensor (Tensor.realLorentz $d) ![]) + | `(ℝT[$d, $cs,*]) => `(Tensor (Tensor.realLorentz $d) (open Tensor.realLorentz in ![$cs,*])) end realLorentz @@ -297,7 +297,7 @@ lemma basis_repr_toDual (d : ℕ) (c : Color Kind) (v : (realLorentz d).V c) Minkowski metric is diagonal. -/ lemma tensorBasis_repr_toDualAt (d : ℕ) {ι : Type} [Fintype ι] [DecidableEq ι] {c c' : ι → Color Kind} (i : ι) (a : Color Kind) (ha : c i = a) (hi : c' i = a.dual) - (h : ∀ x, x ≠ i → c x = c' x) (t : (realLorentz d).Tensor c) (φ : ι → Fin 1 ⊕ Fin d) : + (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor (realLorentz d) c) (φ : ι → Fin 1 ⊕ Fin d) : ((realLorentz d).tensorBasis c').repr ((realLorentz d).toDualAt i a ha hi h t) φ = minkowskiMatrix (φ i) (φ i) * ((realLorentz d).tensorBasis c).repr t φ := by subst ha @@ -316,13 +316,13 @@ of dimensions. -/ example (d : ℕ) : ((realLorentz d).tensorBasis ![]).repr {(realLorentz d).formTensor down | μ ν ⊗ (realLorentz d).formTensor up | μ ν}ᵀ (fun i => i.elim0) = d + 1 := by - rw [Species.Tensor.tensorBasis_repr_contr_two_pairs] + rw [Tensor.tensorBasis_repr_contr_two_pairs] simp only [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_formTensor] simp [minkowskiMatrix.as_diagonal, Matrix.diagonal_apply, Fintype.sum_sum_type, add_comm] /-- `ℝT[3, up, down]` is the space of tensors `T^μ_ν` in four dimensions. -/ -example : ℝT[3, up, down] = (realLorentz 3).Tensor ![up, down] := rfl +example : ℝT[3, up, down] = Tensor (realLorentz 3) ![up, down] := rfl end realLorentz /-! @@ -350,7 +350,7 @@ lemma tensorBasis_repr_toTensor_vector (d : ℕ) (v : Vector d) (μ : Fin 1 → Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := realLorentz d) up v μ /-- The vector of a tensor `T` with one upper index has the components of `T`, `v^ν = T^ν`. -/ -lemma toTensor_symm_vector_apply (d : ℕ) (T : (realLorentz d).Tensor ![up]) +lemma toTensor_symm_vector_apply (d : ℕ) (T : Tensor (realLorentz d) ![up]) (ν : Fin 1 ⊕ Fin d) : (Tensorial.toTensor (M := Vector d)).symm T ν = ((realLorentz d).tensorBasis ![up]).repr T fun _ => ν := by @@ -413,7 +413,7 @@ component is `∑ μ, v^μ ω_μ`. -/ lemma tensorBasis_repr_contr_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : ((realLorentz d).tensorBasis ![]).repr {v | μ ⊗ ω | μ}ᵀ (fun i => i.elim0) = ∑ μ, v μ * ω μ := by - simp [Species.Tensor.tensorBasis_repr_contr_one_pair, Species.tensorBasis_repr_reindex, + simp [Tensor.tensorBasis_repr_contr_one_pair, Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, tensorBasis_repr_toTensor_vector, tensorBasis_repr_toTensor_coVector] From 47655628bcc0fb8ed2f3a380d02e4d0f89d71ae8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 15:58:14 +0000 Subject: [PATCH 140/242] =?UTF-8?q?refactor(Tensors):=20ext=5Fpure=20is=20?= =?UTF-8?q?an=20ext=20lemma,=20remove=20ext=5Fpure=E2=82=82,=20ext=5Fpure?= =?UTF-8?q?=E2=82=83,=20intertwiningMap=5Fext=5Fpure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ext_pure` is tagged `@[ext]`, so `ext p q` reduces maps out of products of tensors and intertwining maps out of tensors to pure tensors. The call sites in Product.lean and Contraction.lean use `ext`. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Contraction.lean | 39 +++++++++++------ Physlib/Mathematics/Tensors/Product.lean | 46 ++++++++++++-------- Physlib/Mathematics/Tensors/Tensor.lean | 32 +++----------- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 1358929a0a..600664589f 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -278,7 +278,9 @@ lemma contr_pure [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors /-- A contraction with no pairs is the identity. -/ lemma contr_noPairs (t : Tensor S c) : contr (ColorEquiv.noPairs c) t = t := - DFunLike.congr_fun (intertwiningMap_ext_pure (ψ := .id _) fun p => by + DFunLike.congr_fun (g := Representation.IntertwiningMap.id _) (by + ext p + simp only [Representation.IntertwiningMap.coe_toLinearMap] rw [contr_pure, Finset.univ_eq_empty, Finset.prod_empty, one_smul] exact congrArg S.pure (funext fun x => by simp only [cast_apply]; rfl)) t @@ -299,12 +301,13 @@ lemma contr_reindex [Fintype J] {c₂ : ι₂ → Color K} {a : J → Color K} lemma reindex_contr [Fintype J] {c'' : κ' → Color K} {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c' c'') (t : Tensor S c) : S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) t := - DFunLike.congr_fun (intertwiningMap_ext_pure - (φ := (S.reindex τ).toIntertwiningMap.comp (contr σ)) fun p => by - rw [Representation.IntertwiningMap.comp_apply, Representation.Equiv.coe_toIntertwiningMap, - contr_pure, map_smul, reindex_pure, contr_pure] - congr 1 - exact congrArg S.pure (funext fun x => by simp only [cast_apply, cast_cast]; rfl)) t + DFunLike.congr_fun (f := (S.reindex τ).toIntertwiningMap.comp (contr σ)) (by + ext p + simp only [Representation.IntertwiningMap.coe_toLinearMap] + rw [Representation.IntertwiningMap.comp_apply, Representation.Equiv.coe_toIntertwiningMap, + contr_pure, map_smul, reindex_pure, contr_pure] + congr 1 + exact congrArg S.pure (funext fun x => by simp only [cast_apply, cast_cast]; rfl)) t /-! @@ -319,7 +322,9 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : Tensor S c) : contr σ t = contr τ t := by - refine DFunLike.congr_fun (intertwiningMap_ext_pure fun p => ?_) t + refine DFunLike.congr_fun (f := contr σ) (g := contr τ) ?_ t + ext p + simp only [Representation.IntertwiningMap.coe_toLinearMap] rw [contr_pure, contr_pure] choose g hg using h₁ congr 1 @@ -383,8 +388,10 @@ lemma contr_contr [Fintype J₁] [Fintype J₂] {c₁ : κ₁ → Color K} {a₁ {a₂ : J₂ → Color K} (σ₁ : ColorEquiv c (contrColors c₁ a₁)) (σ₂ : ColorEquiv c₁ (contrColors c' a₂)) (t : Tensor S c) : contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := - DFunLike.congr_fun (intertwiningMap_ext_pure (φ := (contr σ₂).comp (contr σ₁)) fun f => by - simp only [Representation.IntertwiningMap.comp_apply, contr_pure, map_smul, smul_smul, + DFunLike.congr_fun (f := (contr σ₂).comp (contr σ₁)) (by + ext f + simp only [Representation.IntertwiningMap.coe_toLinearMap, + Representation.IntertwiningMap.comp_apply, contr_pure, map_smul, smul_smul, Fintype.prod_sum_type, cast_apply, cast_cast] rfl) t @@ -402,8 +409,10 @@ lemma tmulEquiv_contr_left [Fintype J] {a : J → Color K} {d : ι₂ → Color contr (σ.tmulRight d) (S.tmulEquiv c d (t ⊗ₜ u)) := LinearMap.congr_fun (f := (S.tmulEquiv c' d).toLinearMap ∘ₗ (contr σ).toLinearMap.rTensor _) (g := (contr (σ.tmulRight d)).toLinearMap ∘ₗ (S.tmulEquiv c d).toLinearMap) - (ext_pure₂ fun p p' => by - simp only [LinearMap.comp_apply, LinearMap.rTensor_tmul, + (by + ext p p' + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, LinearMap.rTensor_tmul, Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap] rw [contr_pure, ← smul_tmul', map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] congr 1 @@ -417,8 +426,10 @@ lemma tmulEquiv_contr_right [Fintype J] {a : J → Color K} {d : ι₂ → Color contr (σ.tmulLeft d) (S.tmulEquiv d c (u ⊗ₜ t)) := LinearMap.congr_fun (f := (S.tmulEquiv d c').toLinearMap ∘ₗ (contr σ).toLinearMap.lTensor _) (g := (contr (σ.tmulLeft d)).toLinearMap ∘ₗ (S.tmulEquiv d c).toLinearMap) - (ext_pure₂ fun p' p => by - simp only [LinearMap.comp_apply, LinearMap.lTensor_tmul, + (by + ext p' p + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, LinearMap.lTensor_tmul, Representation.IntertwiningMap.coe_toLinearMap, Representation.Equiv.coe_toIntertwiningMap] rw [contr_pure, tmul_smul, map_smul, tmulEquiv_pure, tmulEquiv_pure, contr_pure] congr 1 diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 1eb6c465ff..63f277e2da 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -118,14 +118,14 @@ variable (S) in noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ((S.tensorRep c₁).tprod (S.tensorRep c₂)).Equiv (S.tensorRep (prodColors c₁ c₂)) := .mk (TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ - tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm) fun g => - TensorProduct.ext <| ext_pure fun f₁ => ext_pure fun f₂ => by - simp only [LinearMap.compr₂ₛₗ_apply, TensorProduct.mk_apply, LinearMap.comp_apply, - Representation.tprod_apply, TensorProduct.map_tmul, - Representation.ofDistribMulAction_apply_apply, smul_pure, LinearEquiv.coe_coe, - LinearEquiv.trans_apply, TensorProduct.congr_tmul, toPiTensor_pure, tmulEquivDep_apply, - toPiTensor_symm_tprod] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm) fun g => by + ext f₁ f₂ + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, Representation.tprod_apply, + TensorProduct.map_tmul, Representation.ofDistribMulAction_apply_apply, smul_pure, + LinearEquiv.coe_coe, LinearEquiv.trans_apply, TensorProduct.congr_tmul, toPiTensor_pure, + tmulEquivDep_apply, toPiTensor_symm_tprod] + exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) /-- The product of the pure tensors of the vectors `f₁` and `f₂` is the pure tensor with the vectors `f₁` at the indices `inl i` and the vectors `f₂` at the indices `inr j`. -/ @@ -162,10 +162,12 @@ lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c (S.tmulEquiv _ c₃).toLinearMap ∘ₗ (S.tmulEquiv c₁ c₂).toLinearMap.rTensor _) (g := (S.tmulEquiv c₁ _).toLinearMap ∘ₗ (S.tmulEquiv c₂ c₃).toLinearMap.lTensor _ ∘ₗ (TensorProduct.assoc k _ _ _).toLinearMap) - (ext_pure₃ fun f₁ f₂ f₃ => by - simp only [LinearMap.comp_apply, LinearMap.rTensor_tmul, LinearMap.lTensor_tmul, - LinearEquiv.coe_coe, assoc_tmul, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, - reindex_pure] + (by + ext f₁ f₂ f₃ + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, LinearMap.rTensor_tmul, + LinearMap.lTensor_tmul, LinearEquiv.coe_coe, assoc_tmul, + Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ | _ <;> rfl)) ((t₁ ⊗ₜ t₂) ⊗ₜ t₃) @@ -178,8 +180,10 @@ lemma tmulEquiv_comm (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (t₁ LinearMap.congr_fun (f := (S.reindex (ColorEquiv.sumComm c₁ c₂)).toLinearMap ∘ₗ (S.tmulEquiv c₁ c₂).toLinearMap) (g := (S.tmulEquiv c₂ c₁).toLinearMap ∘ₗ (TensorProduct.comm k _ _).toLinearMap) - (ext_pure₂ fun f₁ f₂ => by - simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, comm_tmul, + (by + ext f₁ f₂ + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, LinearEquiv.coe_coe, comm_tmul, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ @@ -193,8 +197,10 @@ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → (S.tmulEquiv c₁ c₂).toLinearMap) (g := (S.tmulEquiv c₁' c₂').toLinearMap ∘ₗ TensorProduct.map (S.reindex σ).toLinearMap (S.reindex τ).toLinearMap) - (ext_pure₂ fun f₁ f₂ => by - simp only [LinearMap.comp_apply, TensorProduct.map_tmul, + (by + ext f₁ f₂ + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, TensorProduct.map_tmul, Representation.Equiv.coe_toLinearMap, tmulEquiv_pure, reindex_pure] exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ @@ -313,9 +319,11 @@ lemma tensorBasis_repr_tmulEquiv [Fintype ι₁] [Fintype ι₂] (c₁ : ι₁ (S.tensorBasis c₂).repr t₂ (fun j => y (.inr j)) := LinearMap.congr_fun (f := (S.tensorBasis _).coord y ∘ₗ (S.tmulEquiv c₁ c₂).toLinearMap) (g := LinearMap.mul' k k ∘ₗ TensorProduct.map ((S.tensorBasis c₁).coord fun i => y (.inl i)) - ((S.tensorBasis c₂).coord fun j => y (.inr j))) (ext_pure₂ fun f₁ f₂ => by - simp only [LinearMap.comp_apply, TensorProduct.map_tmul, LinearMap.mul'_apply, - Module.Basis.coord_apply, Representation.Equiv.coe_toLinearMap] + ((S.tensorBasis c₂).coord fun j => y (.inr j))) (by + ext f₁ f₂ + simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, + LinearMap.coe_restrictScalars, LinearMap.comp_apply, TensorProduct.map_tmul, + LinearMap.mul'_apply, Module.Basis.coord_apply, Representation.Equiv.coe_toLinearMap] rw [tmulEquiv_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, tensorBasis_repr_pure, Fintype.prod_sum_type]) _ diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 82f924adf6..99e3854fb1 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -51,10 +51,10 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t - `Tensor` and `Species.pure` are the tensors and the pure tensors, and `Species.inductionOn_pure` is induction through pure tensors. -- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal, - `Species.ext_pure₂` and `Species.ext_pure₃` do the same for maps out of products of two and - three spaces of tensors, and `Species.equiv_ext_pure` and `Species.intertwiningMap_ext_pure` for - maps of representations. +- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. It is + an `ext` lemma, so `ext p q` also reduces maps out of products `Tensor S c ⊗ Tensor S c₂`, and + intertwining maps, to pure tensors. `Species.equiv_ext_pure` does the same for equivalences of + representations. - `Species.smul_pure` proves that the group acts on a pure tensor index by index. - `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure tensors commutes with it on all tensors. @@ -149,27 +149,11 @@ lemma inductionOn_pure {P : Tensor S c → Prop} PiTensorProduct.induction_on (motive := P) t (fun r f => hsmul r _ (h f)) hadd /-- Two linear maps out of tensors are equal when they agree on pure tensors. -/ +@[ext] lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : Tensor S c →ₗ[k] E} (h : ∀ p : Π i, S.V (c i), f (S.pure p) = g (S.pure p)) : f = g := PiTensorProduct.ext (MultilinearMap.ext h) -/-- Two linear maps out of `Tensor S c ⊗ Tensor S c₂` are equal when they agree on the products -`S.pure p ⊗ₜ S.pure q` of pure tensors. -/ -lemma ext_pure₂ {ι₂ : Type} {c₂ : ι₂ → Color K} {E : Type} [AddCommGroup E] [Module k E] - {f g : Tensor S c ⊗[k] Tensor S c₂ →ₗ[k] E} - (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)), - f (S.pure p ⊗ₜ S.pure q) = g (S.pure p ⊗ₜ S.pure q)) : f = g := - TensorProduct.ext <| ext_pure fun p => ext_pure fun q => h p q - -/-- Two linear maps out of `(Tensor S c ⊗ Tensor S c₂) ⊗ Tensor S c₃` are equal when they agree -on the products `(S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r` of pure tensors. -/ -lemma ext_pure₃ {ι₂ ι₃ : Type} {c₂ : ι₂ → Color K} {c₃ : ι₃ → Color K} {E : Type} - [AddCommGroup E] [Module k E] {f g : (Tensor S c ⊗[k] Tensor S c₂) ⊗[k] Tensor S c₃ →ₗ[k] E} - (h : ∀ (p : Π i, S.V (c i)) (q : Π j, S.V (c₂ j)) (r : Π l, S.V (c₃ l)), - f ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r) = g ((S.pure p ⊗ₜ S.pure q) ⊗ₜ S.pure r)) : - f = g := - TensorProduct.ext <| ext_pure₂ fun p q => ext_pure fun r => h p q r - /-! ## B. The group action @@ -259,12 +243,6 @@ lemma equiv_ext_pure {W : Type} [AddCommGroup W] [Module k W] {ρ : Representati Representation.Equiv.ext <| funext <| LinearMap.congr_fun (ext_pure (f := φ.toLinearMap) (g := ψ.toLinearMap) h) -/-- Two intertwining maps out of tensors are equal when they agree on pure tensors. -/ -lemma intertwiningMap_ext_pure {W : Type} [AddCommGroup W] [Module k W] - {ρ : Representation k G W} {φ ψ : (S.tensorRep c).IntertwiningMap ρ} - (h : ∀ p : Π i, S.V (c i), φ (S.pure p) = ψ (S.pure p)) : φ = ψ := - Representation.IntertwiningMap.ext (ext_pure h) - /-- A tensor with no indices is invariant, `Λ s = s`. -/ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = t := by induction t using inductionOn_pure with From 5735a00bc89e3afbccc3796846900ffb1e7cbc79 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 16:00:06 +0000 Subject: [PATCH 141/242] feat(ForMathlib): Representation.piTprod, IsIntertwiningMap.piTensorMap, Equiv.piTensorCongr Add the tensor product of a family of representations `Representation.piTprod` with `piTprod_apply` and `piTprod_tprod`, the lemma `Representation.IsIntertwiningMap.piTensorMap` that `PiTensorProduct.map` of intertwining maps is intertwining, and `Representation.Equiv.piTensorCongr` with `piTensorCongr_tprod`. The file's ring is relaxed to `CommSemiring`. The action on `Tensor S c` is `piTprod` of the color representations, `smul_pure`, `mapIndices_smul` and `congrIndices` (with `congrIndices_pure`) are these lemmas and definitions on `Tensor S c`. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 72 +++++++++++++++++-- Physlib/Mathematics/Tensors/Tensor.lean | 27 ++++--- 2 files changed, 81 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 6cc31a6ad7..854ab0ce6c 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -6,13 +6,14 @@ Authors: Andrea Pari module public import Mathlib.RepresentationTheory.Intertwining +public import Mathlib.LinearAlgebra.PiTensorProduct.Basic /-! -# Equivalences of representations and invariant vectors +# Tensor products of representations and invariant vectors -The tensor product of two equivalences of representations, and invariant vectors under an -intertwining map. +Tensor products of equivalences of representations, the tensor product of a family of +representations, and invariant vectors under an intertwining map. ## i. Overview @@ -25,17 +26,26 @@ Mathlib has the tensor product of two intertwining maps, `IntertwiningMap.tensor given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the linear equivalence `TensorProduct.congr`. +A family of representations `ρ i` on `M i` gives the representation `piTprod ρ` on +`⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g`, on Mathlib's `PiTensorProduct.map`. Maps `f i` that intertwine +`ρ i` and `σ i` give `PiTensorProduct.map f`, which intertwines `piTprod ρ` and `piTprod σ`. + ## ii. Key results - `Representation.IntertwiningMap.map_invariant` : an intertwining map sends invariant vectors to invariant vectors, and `Representation.IntertwiningMap.map_one_invariant` : an intertwining map from the trivial representation picks out an invariant vector. - `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. +- `Representation.piTprod` : the tensor product of a family of representations, with + `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is + intertwining, and `Representation.Equiv.piTensorCongr` : the tensor product of a family of + equivalences of representations. ## iii. Table of contents - A. Invariant vectors - B. Tensor products of equivalences +- C. Tensor products of families ## iv. References @@ -47,7 +57,7 @@ There are no known references for the material in this module. open TensorProduct -variable {k G V W U X : Type*} [CommRing k] [Monoid G] [AddCommMonoid V] [Module k V] +variable {k G V W U X : Type*} [CommSemiring k] [Monoid G] [AddCommMonoid V] [Module k V] [AddCommMonoid W] [Module k W] [AddCommMonoid U] [Module k U] [AddCommMonoid X] [Module k X] {ρ : Representation k G V} {σ : Representation k G W} {τ : Representation k G U} {π : Representation k G X} @@ -87,3 +97,57 @@ noncomputable def Representation.Equiv.tensorCongr (φ : ρ.Equiv σ) (ψ : τ.E @[simp] lemma Representation.Equiv.tensorCongr_tmul (φ : ρ.Equiv σ) (ψ : τ.Equiv π) (v : V) (u : U) : φ.tensorCongr ψ (v ⊗ₜ u) = φ v ⊗ₜ ψ u := rfl + +/-! + +## C. Tensor products of families + +-/ + +section + +variable {ι : Type*} {M N : ι → Type*} [∀ i, AddCommMonoid (M i)] [∀ i, Module k (M i)] + [∀ i, AddCommMonoid (N i)] [∀ i, Module k (N i)] + +/-- The tensor product of a family of representations, `g ↦ ⨂ i, ρ i g` on `⨂[k] i, M i`. It is +the n-ary `Representation.tprod`. -/ +noncomputable def Representation.piTprod (ρ : Π i, Representation k G (M i)) : + Representation k G (⨂[k] i, M i) := + PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi ρ) + +lemma Representation.piTprod_apply (ρ : Π i, Representation k G (M i)) (g : G) : + Representation.piTprod ρ g = PiTensorProduct.map fun i => ρ i g := rfl + +@[simp] +lemma Representation.piTprod_tprod (ρ : Π i, Representation k G (M i)) (g : G) (m : Π i, M i) : + Representation.piTprod ρ g (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, ρ i g (m i) := + PiTensorProduct.map_tprod _ _ + +/-- `PiTensorProduct.map f` intertwines `piTprod ρ` and `piTprod σ` when every `f i` intertwines +`ρ i` and `σ i`. -/ +lemma Representation.IsIntertwiningMap.piTensorMap {ρ : Π i, Representation k G (M i)} + {σ : Π i, Representation k G (N i)} {f : Π i, M i →ₗ[k] N i} + (hf : ∀ i, (ρ i).IsIntertwiningMap (σ i) (f i)) : + (Representation.piTprod ρ).IsIntertwiningMap (Representation.piTprod σ) + (PiTensorProduct.map f) where + isIntertwining g v := by + rw [Representation.piTprod_apply, Representation.piTprod_apply, ← LinearMap.comp_apply, + ← PiTensorProduct.map_comp, ← LinearMap.comp_apply, ← PiTensorProduct.map_comp] + exact congr(PiTensorProduct.map $(funext fun i => LinearMap.ext ((hf i).isIntertwining g)) v) + +/-- The tensor product of a family of equivalences of representations, `⨂ i, v i ↦ ⨂ i, φ i (v i)`. +It is Mathlib's `PiTensorProduct.congr` with the actions `piTprod ρ` and `piTprod σ`. -/ +noncomputable def Representation.Equiv.piTensorCongr {ρ : Π i, Representation k G (M i)} + {σ : Π i, Representation k G (N i)} (φ : Π i, (ρ i).Equiv (σ i)) : + (Representation.piTprod ρ).Equiv (Representation.piTprod σ) := + .mk (PiTensorProduct.congr fun i => (φ i).toLinearEquiv) fun g => LinearMap.ext + ((Representation.IsIntertwiningMap.piTensorMap fun i => + ⟨(φ i).toIntertwiningMap.isIntertwining⟩).isIntertwining g) + +@[simp] +lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M i)} + {σ : Π i, Representation k G (N i)} (φ : Π i, (ρ i).Equiv (σ i)) (m : Π i, M i) : + Representation.Equiv.piTensorCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := + PiTensorProduct.congr_tprod _ _ + +end diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 99e3854fb1..7d0a57ee01 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -31,7 +31,8 @@ pure tensors, and their properties are proved there first. `S.pure f` is Mathlib `⨂ₜ[k] i, f i` as an element of `Tensor S c`. A group element `Λ` acts on every index by the representation of its color, -`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). A map that commutes with `Λ` on pure tensors +`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation +`Representation.piTprod` on `⨂[k] i, V (c i)`. A map that commutes with `Λ` on pure tensors commutes with it on all tensors (`map_smul_of_pure`). The components `T^μ_νρ` of a tensor `T` are its coordinates in the basis `e_μ ⊗ e^ν ⊗ e^ρ` @@ -69,7 +70,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t - `Species.mapIndices` applies a linear map at every index, with `Species.mapIndices_comp` and `Species.mapIndices_id`, and `Species.mapIndices_smul` proves that it commutes with the group when every index map does. -- `Species.congrIndices` applies an equivalence of representations at every index. +- `Species.congrIndices` applies an equivalence of representations at every index. It is + `Representation.Equiv.piTensorCongr`. - `Species.indexMapsAt` is the family of a linear map at one index and the identity at the others, and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and `Species.mapAt_id`. @@ -161,20 +163,19 @@ lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : Tensor S c →ₗ -/ /-- The group acts on every index by the representation of its color, -`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. -/ +`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. It is `Representation.piTprod` of the representations of the +colors. -/ noncomputable instance : DistribMulAction G (Tensor S c) := DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ - (PiTensorProduct.mapMonoidHom.comp (MonoidHom.pi fun i => S.rep (c i))) + (Representation.piTprod fun i => S.rep (c i)) @[simp] lemma smul_pure (g : G) (f : Π i, S.V (c i)) : g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := - PiTensorProduct.map_tprod _ _ + Representation.piTprod_tprod _ _ _ noncomputable instance : SMulCommClass G k (Tensor S c) where - smul_comm g r t := by - change PiTensorProduct.map (fun i => S.rep (c i) g) (r • t) = r • PiTensorProduct.map _ t - exact map_smul _ r t + smul_comm g r t := map_smul (Representation.piTprod (fun i => S.rep (c i)) g) r t noncomputable instance : SMulCommClass k G (Tensor S c) := .symm _ _ _ @@ -340,21 +341,19 @@ lemma mapIndices_id_apply (t : Tensor S c) : S.mapIndices (fun _ => LinearMap.id lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : Tensor S c) : S.mapIndices f (g • t) = g • S.mapIndices f t := - map_smul_of_pure _ g (fun p => by simp only [smul_pure, mapIndices_pure, hf]) t + (Representation.IsIntertwiningMap.piTensorMap fun x => ⟨hf x⟩).isIntertwining g t variable (S) in /-- Equivalences of representations `f x` at every index `x`, as an equivalence of representations -of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.congr`. -/ +of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is `Representation.Equiv.piTensorCongr`. -/ noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : (S.tensorRep c).Equiv (S.tensorRep c') := - equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.congr (fun x => (f x).toLinearEquiv) ≪≫ₗ - (S.toPiTensor c').symm) fun g p => - mapIndices_smul (fun x => (f x).toIntertwiningMap.isIntertwining _ _) g (S.pure p) + Representation.Equiv.piTensorCongr f /-- On a pure tensor, `congrIndices` applies `f x` to the vector at each index `x`. -/ lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : S.congrIndices f (S.pure p) = S.pure fun x => f x (p x) := - mapIndices_pure (fun x => (f x).toLinearMap) p + Representation.Equiv.piTensorCongr_tprod f p section From a880ad7ce9b55e6b4fcd8209126596829937a4a6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 16:03:05 +0000 Subject: [PATCH 142/242] feat(ForMathlib): PiTensorProduct.toMatrix_map_apply Add `PiTensorProduct.toMatrix_map_apply`, the matrix of `PiTensorProduct.map f` in the product bases as the n-ary Kronecker product of the matrices of the `f i`, in the new file ForMathlib/PiTensorProduct.lean. `tensorBasis_repr_smul` is now its corollary. Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../ForMathlib/PiTensorProduct.lean | 62 +++++++++++++++++++ Physlib/Mathematics/Tensors/Tensor.lean | 12 ++-- 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/PiTensorProduct.lean diff --git a/Physlib.lean b/Physlib.lean index 9ccd8e6380..bc53dc7bf0 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -174,6 +174,7 @@ public import Physlib.Mathematics.ForMathlib.List.InsertionSort public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Basic public import Physlib.Mathematics.ForMathlib.OneParameterSubgroups.Unitary public import Physlib.Mathematics.ForMathlib.OrthogonalMatrix +public import Physlib.Mathematics.ForMathlib.PiTensorProduct public import Physlib.Mathematics.ForMathlib.Representation public import Physlib.Mathematics.ForMathlib.Schouten public import Physlib.Mathematics.ForMathlib.SchurTriangulation diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean new file mode 100644 index 0000000000..5c12d3245f --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -0,0 +1,62 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.LinearAlgebra.PiTensorProduct.Basis +public import Mathlib.LinearAlgebra.Matrix.ToLin + +/-! + +# Matrices of `PiTensorProduct.map` + +The matrix of a family of linear maps in the product basis of a `PiTensorProduct`. + +## i. Overview + +`PiTensorProduct.map f` applies `f i` at every index `i`. In the product bases +`Basis.piTensorProduct b`, its matrix is the n-ary Kronecker product of the matrices of the `f i`: +its entry at `(φ, ψ)` is the product of the entries at `(φ i, ψ i)`. Mathlib has the binary case, +`TensorProduct.toMatrix_map`. + +## ii. Key results + +- `PiTensorProduct.toMatrix_map_apply` : the matrix of `PiTensorProduct.map f` in the product + bases is the product of the matrices of the `f i`. + +## iii. Table of contents + +- A. Matrices in the product basis + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +variable {ι R : Type*} {M N : ι → Type*} + +/-! + +## A. Matrices in the product basis + +-/ + +/-- The matrix of `PiTensorProduct.map f` in the product bases is the n-ary Kronecker product of +the matrices of the `f i`. -/ +lemma PiTensorProduct.toMatrix_map_apply [CommSemiring R] [∀ i, AddCommMonoid (M i)] + [∀ i, Module R (M i)] [∀ i, AddCommMonoid (N i)] [∀ i, Module R (N i)] [Fintype ι] + [DecidableEq ι] {κ κ' : ι → Type*} [∀ i, Fintype (κ i)] [∀ i, DecidableEq (κ i)] + [∀ i, Fintype (κ' i)] + (b : Π i, Module.Basis (κ i) R (M i)) (b' : Π i, Module.Basis (κ' i) R (N i)) + (f : Π i, M i →ₗ[R] N i) (φ : Π i, κ' i) (ψ : Π i, κ i) : + LinearMap.toMatrix (Basis.piTensorProduct b) (Basis.piTensorProduct b') + (PiTensorProduct.map f) φ ψ = ∏ i, LinearMap.toMatrix (b i) (b' i) (f i) (φ i) (ψ i) := by + simp [LinearMap.toMatrix_apply, Basis.piTensorProduct_apply, + Basis.piTensorProduct_repr_tprod_apply] diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 7d0a57ee01..e2f3eb2d47 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -6,7 +6,7 @@ Authors: Andrea Pari module public import Physlib.Mathematics.Tensors.Species -public import Mathlib.LinearAlgebra.PiTensorProduct.Basis +public import Physlib.Mathematics.ForMathlib.PiTensorProduct public import Physlib.Mathematics.ForMathlib.Representation /-! @@ -66,7 +66,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t - `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. -- `Species.tensorBasis_repr_smul` gives the components of `g • t`. +- `Species.tensorBasis_repr_smul` gives the components of `g • t`, from the matrix of + `PiTensorProduct.map` in `PiTensorProduct.toMatrix_map_apply`. - `Species.mapIndices` applies a linear map at every index, with `Species.mapIndices_comp` and `Species.mapIndices_id`, and `Species.mapIndices_smul` proves that it commutes with the group when every index map does. @@ -289,10 +290,9 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S (φ : Π i, basisIdx (c i).kind) : (S.tensorBasis c).repr (g • t) φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) (S.basis (c i)) (S.rep (c i) g) (φ i) (ψ i)) * (S.tensorBasis c).repr t ψ := by - conv_lhs => rw [← (S.tensorBasis c).sum_repr t, Finset.smul_sum] - simp_rw [smul_comm g, map_sum, map_smul, Finsupp.coe_finsetSum, Finset.sum_apply, - Finsupp.smul_apply, tensorBasis_apply, smul_pure, tensorBasis_repr_pure, - LinearMap.toMatrix_apply, smul_eq_mul, mul_comm] + simp_rw [← PiTensorProduct.toMatrix_map_apply] + exact congrFun (LinearMap.toMatrix_mulVec_repr (Basis.piTensorProduct fun i => S.basis (c i)) _ + (Representation.piTprod (fun i => S.rep (c i)) g) t).symm φ /-! From 7935f07036e640bcacc19dc1906e4501be4b0b1e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 16:04:25 +0000 Subject: [PATCH 143/242] feat(ForMathlib): Module.finrank_piTensorProduct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `Module.finrank_piTensorProduct`, the dimension of `⨂[R] i, M i` as the product of the dimensions of free finite `M i`. `finrank_tensor` is now its corollary and no longer needs `DecidableEq ι`. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 27 +++++++++++++++++-- Physlib/Mathematics/Tensors/Tensor.lean | 10 ++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 5c12d3245f..2c7ad606b7 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -7,12 +7,14 @@ module public import Mathlib.LinearAlgebra.PiTensorProduct.Basis public import Mathlib.LinearAlgebra.Matrix.ToLin +public import Mathlib.LinearAlgebra.Dimension.Free /-! -# Matrices of `PiTensorProduct.map` +# Matrices of `PiTensorProduct.map` and dimension of `PiTensorProduct` -The matrix of a family of linear maps in the product basis of a `PiTensorProduct`. +The matrix of a family of linear maps in the product basis of a `PiTensorProduct`, and the +dimension of a `PiTensorProduct`. ## i. Overview @@ -21,14 +23,19 @@ The matrix of a family of linear maps in the product basis of a `PiTensorProduct its entry at `(φ, ψ)` is the product of the entries at `(φ i, ψ i)`. Mathlib has the binary case, `TensorProduct.toMatrix_map`. +The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`, as +`Module.finrank_tensorProduct` for two factors. + ## ii. Key results - `PiTensorProduct.toMatrix_map_apply` : the matrix of `PiTensorProduct.map f` in the product bases is the product of the matrices of the `f i`. +- `Module.finrank_piTensorProduct` : the dimension of `⨂[R] i, M i` is `∏ i, finrank R (M i)`. ## iii. Table of contents - A. Matrices in the product basis +- B. Dimension ## iv. References @@ -60,3 +67,19 @@ lemma PiTensorProduct.toMatrix_map_apply [CommSemiring R] [∀ i, AddCommMonoid (PiTensorProduct.map f) φ ψ = ∏ i, LinearMap.toMatrix (b i) (b' i) (f i) (φ i) (ψ i) := by simp [LinearMap.toMatrix_apply, Basis.piTensorProduct_apply, Basis.piTensorProduct_repr_tprod_apply] + +/-! + +## B. Dimension + +-/ + +/-- The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`. -/ +lemma Module.finrank_piTensorProduct [CommRing R] [StrongRankCondition R] [Fintype ι] + [∀ i, AddCommGroup (M i)] [∀ i, Module R (M i)] [∀ i, Module.Free R (M i)] + [∀ i, Module.Finite R (M i)] : + Module.finrank R (⨂[R] i, M i) = ∏ i, Module.finrank R (M i) := by + classical + rw [Module.finrank_eq_card_basis (Basis.piTensorProduct fun i => Module.Free.chooseBasis R (M i)), + Fintype.card_pi] + simp [Module.finrank_eq_card_chooseBasisIndex] diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index e2f3eb2d47..70880c5435 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -65,7 +65,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. - `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the - components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors. + components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors, from + `Module.finrank_piTensorProduct`. - `Species.tensorBasis_repr_smul` gives the components of `g • t`, from the matrix of `PiTensorProduct.map` in `PiTensorProduct.toMatrix_map_apply`. - `Species.mapIndices` applies a linear map at every index, with `Species.mapIndices_comp` and @@ -280,9 +281,10 @@ noncomputable instance [Fintype ι] : Module.Finite k (Tensor S c) := .of_basis (S.tensorBasis c) /-- The dimension of tensors is the product of the dimensions of their indices. -/ -lemma finrank_tensor [Fintype ι] [DecidableEq ι] (c : ι → Color K) : - Module.finrank k (Tensor S c) = ∏ i, Fintype.card (basisIdx (c i).kind) := by - rw [Module.finrank_eq_card_basis (S.tensorBasis c), Fintype.card_pi] +lemma finrank_tensor [Fintype ι] (c : ι → Color K) : + Module.finrank k (Tensor S c) = ∏ i, Fintype.card (basisIdx (c i).kind) := + Module.finrank_piTensorProduct.trans + (Finset.prod_congr rfl fun i _ => Module.finrank_eq_card_basis (S.basis (c i))) /-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of `g` per index, in the color of that index. -/ From 27975dd4c3154ca1a558493d5e65c4b5ff0a6e5a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 16:05:56 +0000 Subject: [PATCH 144/242] feat(ForMathlib): PiTensorProduct.map_eq_id_of_isEmpty, map_smul_ofDistribMulAction Add `PiTensorProduct.map_eq_id_of_isEmpty`, and `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and `Representation.Equiv.map_smul_ofDistribMulAction` for representations given by actions. `smul_eq_self_of_isEmpty`, `intertwiningMap_smul` and `equiv_smul` are their corollaries, and `mapIndices_pure`, `mapIndices_comp` and `mapIndices_id` are Mathlib's `PiTensorProduct.map_tprod`, `PiTensorProduct.map_comp` and `PiTensorProduct.map_id`. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 21 +++++++++-- .../ForMathlib/Representation.lean | 35 ++++++++++++++++++- Physlib/Mathematics/Tensors/Tensor.lean | 23 ++++++------ 3 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 2c7ad606b7..51bd38bda0 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -13,8 +13,8 @@ public import Mathlib.LinearAlgebra.Dimension.Free # Matrices of `PiTensorProduct.map` and dimension of `PiTensorProduct` -The matrix of a family of linear maps in the product basis of a `PiTensorProduct`, and the -dimension of a `PiTensorProduct`. +The matrix of a family of linear maps in the product basis of a `PiTensorProduct`, the dimension +of a `PiTensorProduct`, and `PiTensorProduct.map` on an empty index type. ## i. Overview @@ -26,16 +26,20 @@ its entry at `(φ, ψ)` is the product of the entries at `(φ i, ψ i)`. Mathlib The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`, as `Module.finrank_tensorProduct` for two factors. +With no indices, `⨂[R] i, M i` is `R` and `PiTensorProduct.map f` is the identity. + ## ii. Key results - `PiTensorProduct.toMatrix_map_apply` : the matrix of `PiTensorProduct.map f` in the product bases is the product of the matrices of the `f i`. - `Module.finrank_piTensorProduct` : the dimension of `⨂[R] i, M i` is `∏ i, finrank R (M i)`. +- `PiTensorProduct.map_eq_id_of_isEmpty` : with no indices, `PiTensorProduct.map f` is the identity. ## iii. Table of contents - A. Matrices in the product basis - B. Dimension +- C. Empty index types ## iv. References @@ -83,3 +87,16 @@ lemma Module.finrank_piTensorProduct [CommRing R] [StrongRankCondition R] [Finty rw [Module.finrank_eq_card_basis (Basis.piTensorProduct fun i => Module.Free.chooseBasis R (M i)), Fintype.card_pi] simp [Module.finrank_eq_card_chooseBasisIndex] + +/-! + +## C. Empty index types + +-/ + +/-- With no indices, `PiTensorProduct.map f` is the identity. -/ +lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoid (M i)] + [∀ i, Module R (M i)] [IsEmpty ι] (f : Π i, M i →ₗ[R] M i) : + PiTensorProduct.map f = LinearMap.id := by + ext x + simp [Subsingleton.elim (fun i => f i (x i)) x] diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 854ab0ce6c..d8a2dfadd7 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -13,7 +13,8 @@ public import Mathlib.LinearAlgebra.PiTensorProduct.Basic # Tensor products of representations and invariant vectors Tensor products of equivalences of representations, the tensor product of a family of -representations, and invariant vectors under an intertwining map. +representations, invariant vectors under an intertwining map, and maps of representations given +by actions. ## i. Overview @@ -40,12 +41,16 @@ A family of representations `ρ i` on `M i` gives the representation `piTprod ρ `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is intertwining, and `Representation.Equiv.piTensorCongr` : the tensor product of a family of equivalences of representations. +- `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and + `Representation.Equiv.map_smul_ofDistribMulAction` : maps of representations given by actions + commute with the actions, `φ (g • v) = g • φ v`. ## iii. Table of contents - A. Invariant vectors - B. Tensor products of equivalences - C. Tensor products of families +- D. Representations given by actions ## iv. References @@ -151,3 +156,31 @@ lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M PiTensorProduct.congr_tprod _ _ end + +/-! + +## D. Representations given by actions + +-/ + +section + +variable [DistribMulAction G V] [SMulCommClass G k V] [DistribMulAction G W] [SMulCommClass G k W] + +/-- An intertwining map of representations given by actions commutes with the actions, +`φ (g • v) = g • φ v`. -/ +lemma Representation.IntertwiningMap.map_smul_ofDistribMulAction + (φ : (Representation.ofDistribMulAction k G V).IntertwiningMap + (Representation.ofDistribMulAction k G W)) (g : G) (v : V) : + φ (g • v) = g • φ v := + Representation.IntertwiningMap.isIntertwining _ _ φ g v + +/-- An equivalence of representations given by actions commutes with the actions, +`φ (g • v) = g • φ v`. -/ +lemma Representation.Equiv.map_smul_ofDistribMulAction + (φ : (Representation.ofDistribMulAction k G V).Equiv + (Representation.ofDistribMulAction k G W)) (g : G) (v : V) : + φ (g • v) = g • φ v := + φ.toIntertwiningMap.map_smul_ofDistribMulAction g v + +end diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 70880c5435..f67d94d91e 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -63,7 +63,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` of it, built by `Species.equivOfPure` and `Species.intertwiningMapOfPure` from a check on pure tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. -- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant. +- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant, from + `PiTensorProduct.map_eq_id_of_isEmpty`. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors, from `Module.finrank_piTensorProduct`. @@ -192,13 +193,13 @@ noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := - Representation.IntertwiningMap.isIntertwining _ _ φ g t + φ.map_smul_ofDistribMulAction g t /-- An equivalence of representations of tensors commutes with the actions, `φ (Λ T) = Λ φ T`. -/ lemma equiv_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).Equiv (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := - intertwiningMap_smul φ.toIntertwiningMap g t + φ.map_smul_ofDistribMulAction g t /-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes with `Λ` on all tensors. -/ @@ -247,11 +248,8 @@ lemma equiv_ext_pure {W : Type} [AddCommGroup W] [Module k W] {ρ : Representati funext <| LinearMap.congr_fun (ext_pure (f := φ.toLinearMap) (g := ψ.toLinearMap) h) /-- A tensor with no indices is invariant, `Λ s = s`. -/ -lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = t := by - induction t using inductionOn_pure with - | h f => rw [smul_pure]; exact congrArg _ (funext isEmptyElim) - | hsmul r t ht => rw [smul_comm, ht] - | hadd t t' ht ht' => rw [smul_add, ht, ht'] +lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = t := + LinearMap.congr_fun (PiTensorProduct.map_eq_id_of_isEmpty _) t /-! @@ -314,15 +312,14 @@ noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : Tensor /-- On a pure tensor, `mapIndices` applies `f x` to the vector at each index `x`. -/ lemma mapIndices_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : - S.mapIndices f (S.pure p) = S.pure fun x => f x (p x) := by - simp only [mapIndices, LinearMap.coe_comp, Function.comp_apply, LinearEquiv.coe_coe, - toPiTensor_pure, PiTensorProduct.map_tprod, toPiTensor_symm_tprod] + S.mapIndices f (S.pure p) = S.pure fun x => f x (p x) := + PiTensorProduct.map_tprod _ _ /-- Two index-wise maps in sequence are the index-wise map of the composites. -/ lemma mapIndices_comp {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (g : Π x, S.V (c' x) →ₗ[k] S.V (c'' x)) : S.mapIndices g ∘ₗ S.mapIndices f = S.mapIndices fun x => g x ∘ₗ f x := - ext_pure fun p => by simp only [LinearMap.comp_apply, mapIndices_pure] + (PiTensorProduct.map_comp g f).symm /-- Two index-wise maps in sequence are the index-wise map of the composites, on a tensor. -/ lemma mapIndices_mapIndices {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) @@ -332,7 +329,7 @@ lemma mapIndices_mapIndices {c'' : ι → Color K} (f : Π x, S.V (c x) →ₗ[k /-- The index-wise map of the identities is the identity. -/ lemma mapIndices_id : S.mapIndices (c := c) (fun _ => LinearMap.id) = LinearMap.id := - ext_pure fun p => mapIndices_pure _ p + PiTensorProduct.map_id /-- The index-wise map of the identities is the identity, on a tensor. -/ lemma mapIndices_id_apply (t : Tensor S c) : S.mapIndices (fun _ => LinearMap.id) t = t := From 496295d11b9230ded2c7b7da9c10252229e709f5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:29:06 +0000 Subject: [PATCH 145/242] refactor(ForMathlib): coordPairing_coordTensorHomEquiv_comm, isSymm/isAlt of the bilinear form of a tensor in ForMathlib/CoordTensorHom Species.contr_toLin_comm is removed; Species.isSymm_formBilin and Species.isAlt_formBilin are proved from the general lemmas. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 50 +++++++++++++++++++ Physlib/Mathematics/Tensors/LinearMap.lean | 12 ----- Physlib/Mathematics/Tensors/Metric.lean | 9 ++-- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 6ccef265b6..8369ad5f1b 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.Contraction +public import Mathlib.LinearAlgebra.BilinearForm.Properties public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.ForMathlib.TensorProductMatrix @@ -27,6 +28,10 @@ The delta `∑ I, b I ⊗ b' I` of the two bases is the tensor of the identity m linear map of `z` is the transpose of the matrix of components of `z`, so two tensors give inverse maps when their matrices of components are inverse. +A tensor `z` of `M ⊗ M` gives the bilinear form `(u, v) ↦ ⟨v, z u⟩` on `N`. Swapping the factors +of `z` swaps its arguments, so the form is symmetric when `z` is and alternating when `z` is +antisymmetric. + ## ii. Key results - `Module.Basis.coordTensorHomEquiv` : a tensor of `M ⊗ U` as a linear map `N → U`. @@ -36,10 +41,14 @@ maps when their matrices of components are inverse. the matrix of components. - `Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one` : two tensors give inverse maps when their matrices of components are inverse. +- `Module.Basis.isSymm_coordTensorHomEquiv_of_comm_eq` and + `Module.Basis.isAlt_coordTensorHomEquiv_of_comm_eq_neg` : the bilinear form `(u, v) ↦ ⟨v, z u⟩` + is symmetric when `comm z = z` and alternating when `comm z = -z`. ## iii. Table of contents - A. Tensors as linear maps through the coordinate pairing +- B. The bilinear form of a tensor ## iv. References @@ -149,4 +158,45 @@ lemma coordTensorHomEquiv_comm_comp_comm (b : Module.Basis ι R M) (b' : Module. LinearMap.toMatrix_id] at h convert h using 2 <;> ext <;> simp [tensorProduct_repr_comm] +/-! + +## B. The bilinear form of a tensor + +-/ + +/-- Swapping the factors of `z` swaps the arguments of `(u, v) ↦ ⟨v, z u⟩`. -/ +lemma coordPairing_coordTensorHomEquiv_comm (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (z : M ⊗[R] M) (u v : N) : + b'.coordPairing b (v ⊗ₜ b.coordTensorHomEquiv b' (TensorProduct.comm R M M z) u) = + b'.coordPairing b (u ⊗ₜ b.coordTensorHomEquiv b' z v) := by + induction z using TensorProduct.inductionOn with + | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] + | tmul x y => + simp only [TensorProduct.comm_tmul, coordTensorHomEquiv_tmul, tmul_smul, map_smul, + smul_eq_mul] + exact mul_comm _ _ + +/-- The bilinear form `(u, v) ↦ ⟨v, z u⟩` is symmetric when `comm z = z`. -/ +lemma isSymm_coordTensorHomEquiv_of_comm_eq (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + {z : M ⊗[R] M} (hz : TensorProduct.comm R M M z = z) : + LinearMap.BilinForm.IsSymm + ((TensorProduct.curry (b'.coordPairing b)).flip ∘ₗ b.coordTensorHomEquiv b' z) := + ⟨fun u v => by + change _ = b'.coordPairing b (u ⊗ₜ b.coordTensorHomEquiv b' z v) + rw [← coordPairing_coordTensorHomEquiv_comm, hz] + rfl⟩ + +/-- The bilinear form `(u, v) ↦ ⟨v, z u⟩` is alternating when `comm z = -z`, over a ring of +characteristic zero. -/ +lemma isAlt_coordTensorHomEquiv_of_comm_eq_neg [NoZeroDivisors R] [CharZero R] + (b : Module.Basis ι R M) (b' : Module.Basis ι R N) {z : M ⊗[R] M} + (hz : TensorProduct.comm R M M z = -z) : + LinearMap.IsAlt ((TensorProduct.curry (b'.coordPairing b)).flip ∘ₗ + b.coordTensorHomEquiv b' z) := by + refine LinearMap.isAlt_iff_eq_neg_flip.mpr (LinearMap.ext₂ fun u v => ?_) + change b'.coordPairing b (v ⊗ₜ b.coordTensorHomEquiv b' z u) = + -b'.coordPairing b (u ⊗ₜ b.coordTensorHomEquiv b' z v) + rw [← coordPairing_coordTensorHomEquiv_comm, hz, map_neg, LinearMap.neg_apply, tmul_neg, + map_neg] + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index ecef273211..073678336c 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -181,18 +181,6 @@ lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : rw [← Representation.mem_linHom_invariants_iff_isIntertwining] exact forall_congr' fun g => by rw [← toLin_map_rep, S.toLin.injective.eq_iff] -variable {S} in -/-- Contracting with the swapped tensor swaps the two arguments, `u_μ z^νμ v_ν = v_ν z^νμ u_μ`. -/ -lemma contr_toLin_comm (c : Color K) (z : S.V c ⊗[k] S.V c) (u v : S.V c.dual) : - S.contr c.dual (v ⊗ₜ S.toLin (TensorProduct.comm k _ _ z) u) = - S.contr c.dual (u ⊗ₜ S.toLin z v) := by - induction z using TensorProduct.inductionOn with - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, tmul_add, hz, hz'] - | tmul x y => - simp only [TensorProduct.comm_tmul, Module.Basis.coordTensorHomEquiv_tmul, tmul_smul, map_smul, - smul_eq_mul] - exact mul_comm _ _ - /-! ## C. The delta diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 40e52e95d8..c9f98e01ef 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -186,7 +186,7 @@ lemma formMatrix_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind /-- A symmetric form is symmetric as a bilinear form, Mathlib's `LinearMap.BilinForm.IsSymm`. -/ lemma isSymm_formBilin (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : (S.formBilin c).IsSymm := - ⟨fun u v => by rw [formBilin_apply, formBilin_apply, ← contr_toLin_comm, form_symm]⟩ + (S.basis c).isSymm_coordTensorHomEquiv_of_comm_eq (S.basis c.dual) (form_symm c) /-- An antisymmetric form, `g_μν = -g_νμ`. -/ lemma form_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : @@ -211,11 +211,8 @@ lemma comm_form_eq_smul (c : Color K) [S.HasForm c.kind] (r : k) /-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's `LinearMap.IsAlt`. It uses that `k` has characteristic zero. -/ lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : - LinearMap.IsAlt (S.formBilin c) := by - refine LinearMap.isAlt_iff_eq_neg_flip.mpr (LinearMap.ext₂ fun u v => ?_) - rw [LinearMap.neg_apply, LinearMap.neg_apply, LinearMap.flip_apply, formBilin_apply, - formBilin_apply, ← contr_toLin_comm, form_antisymm, map_neg, LinearMap.neg_apply, tmul_neg, - map_neg] + LinearMap.IsAlt (S.formBilin c) := + (S.basis c).isAlt_coordTensorHomEquiv_of_comm_eq_neg (S.basis c.dual) (form_antisymm c) /-! From 938094dc6e31ddc3800070ed46003a12cc5f9c91 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:30:11 +0000 Subject: [PATCH 146/242] =?UTF-8?q?refactor(ForMathlib):=20LinearEquiv.fin?= =?UTF-8?q?rank=5Feq=E2=82=9B=E2=82=97=20in=20ForMathlib/SemilinearMaps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A semilinear equivalence keeps the finite rank. Species.finrank_conj is removed and its caller uses the general lemma on conjEquiv. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/SemilinearMaps.lean | 25 +++++++++++++++++++ Physlib/Mathematics/Tensors/Conjugation.lean | 6 ----- .../Mathematics/Tensors/HermitianForm.lean | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index 3e97c79d06..566a9425e8 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Mathlib.LinearAlgebra.Dimension.Finrank public import Mathlib.LinearAlgebra.PiTensorProduct.Basic public import Physlib.Mathematics.ForMathlib.Contraction @@ -29,6 +30,9 @@ coordinates of a vector in `b` and reads them in `b'`, the coordinate at `μ` mo conjugate-linear map that sends each `b μ` to `b' (e μ)` is this equivalence. Conjugating both factors along the same labels conjugates the coordinate pairing of two bases. +A semilinear equivalence keeps the finite rank, so a module and its conjugate have the same +dimension. + ## ii. Key results - `PiTensorProduct.mapₛₗ` applies a family of semilinear maps to a tensor product, and @@ -38,11 +42,13 @@ factors along the same labels conjugates the coordinate pairing of two bases. `b' (e μ)`. - `Module.Basis.star_coordPairing_starEquiv` proves that conjugating both factors conjugates the coordinate pairing. +- `LinearEquiv.finrank_eqₛₗ` : a semilinear equivalence keeps the finite rank. ## iii. Table of contents - A. Semilinear maps of tensor products - B. Conjugating the coordinates of a basis +- C. The rank under a semilinear equivalence ## iv. References @@ -184,3 +190,22 @@ lemma star_coordPairing_starEquiv [Fintype ι] [DecidableEq ι] [Fintype ι'] [D exact Fintype.sum_equiv e _ _ fun μ => by simp end Module.Basis + +/-! + +## C. The rank under a semilinear equivalence + +-/ + +namespace LinearEquiv + +variable {R M M' : Type*} [Semiring R] [AddCommMonoid M] [Module R M] [AddCommMonoid M'] + [Module R M'] {σ σ' : R →+* R} [RingHomInvPair σ σ'] [RingHomInvPair σ' σ] + +/-- A semilinear equivalence keeps the finite rank, as `v ↦ v̄` does. -/ +lemma finrank_eqₛₗ (f : M ≃ₛₗ[σ] M') : Module.finrank R M = Module.finrank R M' := by + simpa [Module.finrank] using congr(Cardinal.toNat $(lift_rank_eq_of_equiv_equiv σ f.toAddEquiv + (Function.bijective_iff_has_inverse.mpr ⟨σ', fun _ => RingHomInvPair.comp_apply_eq, + fun _ => RingHomInvPair.comp_apply_eq₂⟩) (map_smulₛₗ f))) + +end LinearEquiv diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index abaedf0703..21e5936cb2 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -178,12 +178,6 @@ lemma conjEquiv_cast {b b' : Color K} (p : b = b') (h : c' = b'.bar) (x : S.V b) subst p rfl -/-- Conjugation keeps the dimension, as `v ↦ v̄` is a conjugate-linear equivalence. -/ -lemma finrank_conj (h : c' = c.bar) : Module.finrank k (S.V c') = Module.finrank k (S.V c) := by - rw [Module.finrank, ← rank_eq_of_equiv_equiv (starRingEnd k) (S.conjEquiv h).toAddEquiv - (Function.Involutive.bijective fun r => by simp) (fun r m => map_smulₛₗ (S.conjEquiv h) r m), - ← Module.finrank] - end omit [InvolutiveStar K] in diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index cc9d80f887..a84c8c9e7c 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -118,7 +118,7 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. have hi := toLin_injective_of_isPosDef g hg refine ⟨hi, (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi⟩ - rw [← finrank_conj (congrArg Color.dual hb), Module.finrank_eq_card_basis (S.basis b.dual), + rw [(S.conjEquiv (congrArg Color.dual hb)).finrank_eqₛₗ, Module.finrank_eq_card_basis (S.basis b.dual), Module.finrank_eq_card_basis (S.basis b)] /-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ From 7f62af61633a356acf1543c9a44326c0ef797fa1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:31:43 +0000 Subject: [PATCH 147/242] refactor(ForMathlib): Module.Basis.comm_eq_smul_iff in ForMathlib/TensorProductMatrix Swapping the factors scales a tensor exactly when its coefficients do. Species.formMatrix_symm, formMatrix_antisymm and comm_form_eq_smul are proved from it. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/TensorProductMatrix.lean | 10 ++++++++++ Physlib/Mathematics/Tensors/Metric.lean | 15 +++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean index b045634eab..e245735589 100644 --- a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -33,6 +33,9 @@ and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The m matrix `X`. - `Module.Basis.tensorProduct_repr_comm` : swapping the factors of a tensor transposes its coefficients. +- `Module.Basis.comm_eq_smul_iff` : swapping the factors scales a tensor of `M ⊗ M` by `r` exactly + when its coefficient matrix satisfies `Xᵀ = r X`, symmetric for `r = 1` and antisymmetric for + `r = -1`. ## iii. Table of contents @@ -124,4 +127,11 @@ lemma tensorProduct_repr_comm (b₁ : Basis ι R M) (b₂ : Basis κ R N) (v : M | tmul x y => simp [mul_comm] | add x y hx hy => simp [hx, hy] +/-- Swapping the factors scales `v` by `r` exactly when its coefficients do, `X j i = r X i j`. -/ +lemma comm_eq_smul_iff (b : Basis ι R M) (v : M ⊗[R] M) (r : R) : + TensorProduct.comm R M M v = r • v ↔ + ∀ i j, (b.tensorProduct b).repr v (j, i) = r * (b.tensorProduct b).repr v (i, j) := by + rw [← (b.tensorProduct b).repr.injective.eq_iff, Finsupp.ext_iff, Prod.forall] + simp only [tensorProduct_repr_comm, map_smul, Finsupp.smul_apply, smul_eq_mul] + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index c9f98e01ef..af52e8164c 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -180,8 +180,9 @@ lemma form_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : /-- The components of a symmetric form are symmetric, `g^νμ = g^μν`. -/ lemma formMatrix_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] - (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = S.formMatrix c μ ν := by - rw [formMatrix, ← Module.Basis.tensorProduct_repr_comm, form_symm, formMatrix] + (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = S.formMatrix c μ ν := + (((S.basis c).comm_eq_smul_iff _ 1).1 ((form_symm c).trans (one_smul k _).symm) μ ν).trans + (one_mul _) /-- A symmetric form is symmetric as a bilinear form, Mathlib's `LinearMap.BilinForm.IsSymm`. -/ lemma isSymm_formBilin (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : @@ -195,18 +196,16 @@ lemma form_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.ki /-- The components of an antisymmetric form are antisymmetric, `g^νμ = -g^μν`. -/ lemma formMatrix_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = -S.formMatrix c μ ν := by - rw [formMatrix, ← Module.Basis.tensorProduct_repr_comm, form_antisymm, map_neg, - Finsupp.neg_apply, formMatrix] + (μ ν : basisIdx c.kind) : S.formMatrix c ν μ = -S.formMatrix c μ ν := + (((S.basis c).comm_eq_smul_iff _ (-1)).1 + ((form_antisymm c).trans (neg_one_smul k _).symm) μ ν).trans (neg_one_mul _) /-- Swapping the two indices of the form scales it by `r` when its components do, `g^νμ = r g^μν`. With `r = 1` it gives `IsSymmetricForm`, with `r = -1` `IsAntisymmetricForm`. -/ lemma comm_form_eq_smul (c : Color K) [S.HasForm c.kind] (r : k) (h : ∀ μ ν, S.formMatrix c ν μ = r * S.formMatrix c μ ν) : TensorProduct.comm k (S.V c) (S.V c) (form c) = r • form c := - ((S.basis c).tensorProduct (S.basis c)).repr.injective <| Finsupp.ext fun ⟨μ, ν⟩ => by - rw [Module.Basis.tensorProduct_repr_comm, map_smul, Finsupp.smul_apply, smul_eq_mul] - exact h μ ν + ((S.basis c).comm_eq_smul_iff _ r).2 h /-- An antisymmetric form is alternating as a bilinear form, `g(u, u) = 0`, Mathlib's `LinearMap.IsAlt`. It uses that `k` has characteristic zero. -/ From 895ee8e68d13a7b0b8bc56a78b93a137ab8464b9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:33:45 +0000 Subject: [PATCH 148/242] refactor(ForMathlib): LinearMap.IsAlt.schouten_basis in ForMathlib/Schouten The Schouten identity on basis vectors, read at a label. Species.schouten is removed and Species.formMatrix_schouten is proved from the general lemma. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Schouten.lean | 14 ++++++++++ Physlib/Mathematics/Tensors/Metric.lean | 27 +++++--------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean index e41b9ffe9b..4274eb10d4 100644 --- a/Physlib/Mathematics/ForMathlib/Schouten.lean +++ b/Physlib/Mathematics/ForMathlib/Schouten.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.SesquilinearForm.Basic +import Mathlib.Tactic.LinearCombination /-! @@ -19,10 +20,12 @@ identity. For an alternating bilinear form `B` on a module with a basis of two vectors, `B(u, v) w + B(v, w) u + B(w, u) v = 0` for all `u`, `v`, `w`. Expanding each vector in the basis, `B` vanishes on equal basis vectors and is antisymmetric on the other two, so the sum cancels. +Read at a label on three basis vectors, it relates the entries of the matrix of `B`. ## ii. Key results - `LinearMap.IsAlt.schouten` : the Schouten identity for an alternating form in two dimensions. +- `LinearMap.IsAlt.schouten_basis` : the same on basis vectors, read at a label. ## iii. Table of contents @@ -62,4 +65,15 @@ lemma schouten (hB : LinearMap.IsAlt B) (b : Basis ι R M) (h2 : Fintype.card ι LinearMap.IsAlt.self_eq_zero hB, (LinearMap.IsAlt.neg hB (b i) (b j)).symm] module +/-- The Schouten identity on the basis vectors `e_i`, `e_j` and `e_k`, read at the label `l`, +`B(e_i, e_j) δ_lk = B(e_i, e_k) δ_jl - B(e_j, e_k) δ_il`. -/ +lemma schouten_basis (hB : LinearMap.IsAlt B) (b : Basis ι R M) (h2 : Fintype.card ι = 2) + (i j k l : ι) : + B (b i) (b j) * (if l = k then 1 else 0) = + B (b i) (b k) * (if j = l then 1 else 0) - B (b j) (b k) * (if i = l then 1 else 0) := by + have h := congrArg (fun x => b.repr x l) (hB.schouten b h2 (b i) (b j) (b k)) + simp only [map_add, map_smul, Finsupp.add_apply, Finsupp.smul_apply, Basis.repr_self, + Finsupp.single_apply, smul_eq_mul, map_zero, Finsupp.zero_apply, @eq_comm _ k l] at h + linear_combination h + (if j = l then 1 else 0) * LinearMap.IsAlt.neg hB (b k) (b i) + end LinearMap.IsAlt diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index af52e8164c..acf2c1d0f0 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -51,10 +51,9 @@ Martin. `Species.tensorBasis_repr_toDualAt`. - `Species.toDualAt_eq_contr` proves that lowering is contracting with the form, `T^…_ν… = g_νμ T^…μ…`. -- `Species.schouten` is the Schouten identity `g(u, v) w + g(v, w) u + g(w, u) v = 0` for an - antisymmetric form on a kind with two labels, `LinearMap.IsAlt.schouten` for `formBilin`, and - `Species.formMatrix_schouten` is the same in components, - `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`. +- `Species.formMatrix_schouten` is the Schouten identity for an antisymmetric form on a kind with + two labels in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`, + `LinearMap.IsAlt.schouten_basis` for `formBilin`. ## iii. Table of contents @@ -355,27 +354,15 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i -/ -/-- The Schouten identity for an antisymmetric form on a kind with two labels, such as the Weyl `ε`: -`g(u, v) w + g(v, w) u + g(w, u) v = 0`. In two dimensions every alternating form of three vectors -vanishes. -/ -lemma schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] - (h2 : Fintype.card (basisIdx c.kind) = 2) (u v w : S.V c.dual) : - S.formBilin c u v • w + S.formBilin c v w • u + S.formBilin c w u • v = 0 := - (isAlt_formBilin (S := S) c).schouten (S.basis c.dual) h2 u v w - -/-- The Schouten identity in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`: `schouten` at -the basis vectors `e^γ`, `e^ρ` and `e^α'`, read at the label `α`. -/ +/-- The Schouten identity in components, `g^γρ δ^α_α' = g^γα' δ^α_ρ - g^ρα' δ^α_γ`, for an +antisymmetric form on a kind with two labels, such as the Weyl `ε`. -/ lemma formMatrix_schouten (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] (h2 : Fintype.card (basisIdx c.kind) = 2) (γ ρ α' α : basisIdx c.kind) : S.formMatrix c γ ρ * (if α = α' then 1 else 0) = S.formMatrix c γ α' * (if ρ = α then 1 else 0) - S.formMatrix c ρ α' * (if γ = α then 1 else 0) := by - have h := congrArg (fun x => (S.basis c.dual).repr x α) - (schouten c h2 (S.basis c.dual γ) (S.basis c.dual ρ) (S.basis c.dual α')) - simp only [map_add, map_smul, Finsupp.add_apply, Finsupp.smul_apply, Module.Basis.repr_self, - Finsupp.single_apply, smul_eq_mul, map_zero, Finsupp.zero_apply, formBilin_basis, - @eq_comm _ α' α] at h - linear_combination h - (if ρ = α then 1 else 0) * formMatrix_antisymm (S := S) c γ α' + simpa only [formBilin_basis] using + (isAlt_formBilin (S := S) c).schouten_basis (S.basis c.dual) h2 γ ρ α' α end Species From 1c46fb81d9b00cb52e6a386a253309fd9ef90edb Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:38:27 +0000 Subject: [PATCH 149/242] refactor(Relativity): tensorComponent to Lorentz.Vector, Vector.coordPairing_basis_tmul, tensorProductToMatrix_symm_diagonal Module.Basis.tensorProductToMatrix_symm_diagonal (ForMathlib): the tensor with a diagonal coefficient matrix. The tensorComponent family moves to Lorentz.Vector.Basic, with ext and eq-sum from Basis.ext_elem and tensorProductToMatrix_symm_apply. realLorentz.coordPairing_vector_coVector and realLorentz.dotProduct_rep are replaced by Vector.coordPairing_basis_tmul and the existing Vector.contract_rep. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/TensorProductMatrix.lean | 7 ++ .../Representation/Contraction.lean | 7 ++ .../Tensors/RealTensor/Species.lean | 67 ++++--------------- .../Tensors/RealTensor/Vector/Basic.lean | 29 ++++++++ 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean index e245735589..8fd2f232ce 100644 --- a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -28,6 +28,8 @@ and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The m `∑ i j, X i j • b₁ i ⊗ b₂ j`. - `Module.Basis.tensorProductToMatrix_tmul` : the coefficient matrix of `x ⊗ y` is the outer product of the coordinates. +- `Module.Basis.tensorProductToMatrix_symm_diagonal` : the tensor with the diagonal coefficient + matrix `diagonal D` is `∑ i, D i • b i ⊗ b i`. - `Module.Basis.tensorProductToMatrix_map` : the coefficient matrix of `f ⊗ g` applied to `v`. - `Module.Basis.tensorProductToMatrix_symm_map` : `f ⊗ g` applied to the tensor with coefficient matrix `X`. @@ -100,6 +102,11 @@ lemma tensorProductToMatrix_symm_apply (X : Matrix ι κ R) : ext i j simp [Finsupp.single_apply, Finset.sum_apply] +/-- The tensor with the diagonal coefficient matrix `diagonal D` is `∑ i, D i • b i ⊗ b i`. -/ +lemma tensorProductToMatrix_symm_diagonal (D : ι → R) : + (b₁.tensorProductToMatrix b₁).symm (diagonal D) = ∑ i, D i • (b₁ i ⊗ₜ[R] b₁ i) := by + simp [tensorProductToMatrix_symm_apply, diagonal_apply, ite_smul] + /-- `f ⊗ g` sends the tensor with coefficient matrix `X` to the one with coefficient matrix `A * X * Bᵀ`. -/ lemma tensorProductToMatrix_symm_map (f : M →ₗ[R] M) (g : N →ₗ[R] N) (X : Matrix ι κ R) : diff --git a/Physlib/Relativity/Tensors/RealTensor/Representation/Contraction.lean b/Physlib/Relativity/Tensors/RealTensor/Representation/Contraction.lean index 4993bb5bc8..ef2e92bd45 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Representation/Contraction.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Representation/Contraction.lean @@ -8,6 +8,7 @@ module public import Physlib.Relativity.Tensors.RealTensor.Vector.Representation public import Physlib.Relativity.Tensors.RealTensor.CoVector.Representation public import Mathlib.RepresentationTheory.Intertwining +public import Physlib.Mathematics.ForMathlib.Contraction /-! # Contraction of Real Lorentz Vectors and Covectors @@ -108,6 +109,12 @@ lemma Vector.contract_eq_coVector_contract (φ : Vector d) (ψ : CoVector d) : Vector.contract (φ ⊗ₜ ψ) = CoVector.contract (ψ ⊗ₜ φ) := by simp_rw [Vector.contract_tmul, CoVector.contract_tmul, mul_comm] +/-- The contraction `φ^μ ψ_μ` is the coordinate pairing of `Vector.basis` and `CoVector.basis`. -/ +lemma Vector.coordPairing_basis_tmul (φ : Vector d) (ψ : CoVector d) : + Vector.basis.coordPairing CoVector.basis (φ ⊗ₜ ψ) = Vector.contract (φ ⊗ₜ ψ) := by + simp [Module.Basis.coordPairing_tmul, Vector.basis_repr_apply, CoVector.basis_repr_apply, + Vector.contract_tmul] + lemma Vector.contract_rep (Λ : LorentzGroup d) (φ : Vector d) (ψ : CoVector d) : Vector.contract ((Vector.rep Λ φ) ⊗ₜ (CoVector.rep Λ ψ)) = Vector.contract (φ ⊗ₜ ψ) := by convert! Vector.contract.isIntertwining _ _ Λ (φ ⊗ₜ ψ) diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index db664064da..8b9150dab0 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -6,9 +6,10 @@ Authors: Andrea Pari module public import Physlib.Mathematics.Tensors.ElabTactics -public import Physlib.Relativity.Tensors.RealTensor.CoVector.Representation +public import Physlib.Relativity.Tensors.RealTensor.Representation.Contraction public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct public import Physlib.Relativity.Tensors.RealTensor.Vector.Representation +import Mathlib.LinearAlgebra.Matrix.IsDiag /-! # The species of real Lorentz tensors @@ -66,7 +67,6 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ contraction `η_μν v^μ w^ν`. - `realLorentz.basis_repr_toDual` and `realLorentz.tensorBasis_repr_toDualAt` : lowering or raising an index multiplies its component by `η_νν`. -- `Lorentz.Vector.tensorComponent` : the component `F^μν` of `F` in `Vector d ⊗ Vector d`. - `realLorentz.tensorBasis_repr_toDualAt_contr_vector_tmul` : the contraction `F_μν G^μν` in components. @@ -130,20 +130,6 @@ noncomputable abbrev IndexSpace.basis (d : ℕ) : | ⟨_, .up⟩ => Vector.basis | ⟨_, .down⟩ => CoVector.basis -/-- The coordinate pairing of `Vector.basis` and `CoVector.basis` is `v^μ ω_μ = v ⬝ᵥ ω`. -/ -lemma coordPairing_vector_coVector (d : ℕ) (v : Vector d) (ω : CoVector d) : - Vector.basis.coordPairing CoVector.basis (v ⊗ₜ ω) = (v : Fin 1 ⊕ Fin d → ℝ) ⬝ᵥ ω := by - simp [Module.Basis.coordPairing_tmul, Vector.basis_repr_apply, CoVector.basis_repr_apply, - dotProduct] - -/-- The pairing `v^μ ω_μ` of a vector and a covector is Lorentz invariant, -`(Λ v) ⬝ᵥ ((Λ⁻¹)ᵀ ω) = v ⬝ᵥ ω`. -/ -lemma dotProduct_rep (d : ℕ) (Λ : LorentzGroup d) (v : Vector d) (ω : CoVector d) : - (Vector.rep Λ v : Fin 1 ⊕ Fin d → ℝ) ⬝ᵥ CoVector.rep Λ ω = v ⬝ᵥ ω := by - change (Λ.1 *ᵥ v) ⬝ᵥ ((Λ⁻¹).1ᵀ *ᵥ ω) = _ - rw [dotProduct_mulVec, vecMul_transpose, mulVec_mulVec, LorentzGroup.coe_inv, - LorentzGroup.subtype_inv_mul, one_mulVec] - /-- The coordinate pairing of an index with its dual is Lorentz invariant. At a lower index it is the pairing at the upper index with its factors swapped (`Species.invariant_of_up`). -/ lemma coordPairing_rep (d : ℕ) : ∀ (c : Color Kind) (Λ : LorentzGroup d) (x : IndexSpace d c) @@ -152,8 +138,8 @@ lemma coordPairing_rep (d : ℕ) : ∀ (c : Color Kind) (Λ : LorentzGroup d) (x (IndexSpace.rep d c Λ x ⊗ₜ IndexSpace.rep d c.dual Λ y) = (IndexSpace.basis d c).coordPairing (IndexSpace.basis d c.dual) (x ⊗ₜ y) := Species.invariant_of_up (basisIdx := fun _ => Fin 1 ⊕ Fin d) _ _ fun _ Λ x y => - (coordPairing_vector_coVector d _ _).trans <| - (dotProduct_rep d Λ x y).trans (coordPairing_vector_coVector d x y).symm + (Vector.coordPairing_basis_tmul _ _).trans <| + (Vector.contract_rep Λ x y).trans (Vector.coordPairing_basis_tmul x y).symm end realLorentz @@ -204,13 +190,14 @@ noncomputable def metric (d : ℕ) (c : Color Kind) : (realLorentz d).V c ⊗[ lemma basis_tensorProduct_repr_metric (d : ℕ) (c : Color Kind) (μ ν : Fin 1 ⊕ Fin d) : (((realLorentz d).basis c).tensorProduct ((realLorentz d).basis c)).repr (metric d c) (μ, ν) = minkowskiMatrix μ ν := by - rw [metric, map_sum, Finsupp.finsetSum_apply, Finset.sum_eq_single μ - (fun x _ hx => by simp [Module.Basis.tensorProduct_repr_tmul_apply, Finsupp.single_apply, hx]) - (by simp)] - by_cases h : μ = ν - · subst h - simp [Module.Basis.tensorProduct_repr_tmul_apply] - · simp [Module.Basis.tensorProduct_repr_tmul_apply, h] + let b := (realLorentz d).basis c + have h : metric d c = (b.tensorProductToMatrix b).symm minkowskiMatrix := by + rw [← (show Matrix.IsDiag (@minkowskiMatrix d) by + rw [minkowskiMatrix.as_diagonal]; exact Matrix.isDiag_diagonal _).diagonal_diag, + Module.Basis.tensorProductToMatrix_symm_diagonal] + rfl + rw [h] + exact congrFun₂ ((b.tensorProductToMatrix b).apply_symm_apply minkowskiMatrix) μ ν /-- The metric is Lorentz invariant, `Λ η Λᵀ = η` at an upper color and `Λ⁻¹ᵀ η Λ⁻¹ = η` at a lower one. -/ @@ -370,36 +357,6 @@ lemma tensorBasis_repr_toTensor_vector_tmul (d : ℕ) (F : Vector d ⊗[ℝ] Vec (Vector.basis.tensorProduct Vector.basis).repr F (μ (.inl 0), μ (.inr 0)) := Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace (S := realLorentz d) up up F μ -/-- The component `F^μν` of `F` in `Vector d ⊗ Vector d`, its coordinate at `(μ, ν)` in Mathlib's -product basis `e_μ ⊗ e_ν`. -/ -noncomputable def _root_.Lorentz.Vector.tensorComponent {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) : - Vector d ⊗[ℝ] Vector d →ₗ[ℝ] ℝ := - Finsupp.lapply (μ, ν) ∘ₗ (Vector.basis.tensorProduct Vector.basis).repr.toLinearMap - -/-- The component `F^μν` is the coordinate of `F` at `(μ, ν)` in Mathlib's product basis. -/ -lemma _root_.Lorentz.Vector.tensorComponent_apply {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) - (F : Vector d ⊗[ℝ] Vector d) : - Vector.tensorComponent μ ν F = (Vector.basis.tensorProduct Vector.basis).repr F (μ, ν) := rfl - -/-- The components of `v ⊗ w` are the products of components, `(v ⊗ w)^μν = v^μ w^ν`. -/ -@[simp] -lemma _root_.Lorentz.Vector.tensorComponent_tmul {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (v w : Vector d) : - Vector.tensorComponent μ ν (v ⊗ₜ w) = v μ * w ν := by - simp [Vector.tensorComponent_apply, Module.Basis.tensorProduct_repr_tmul_apply, - Vector.basis_repr_apply, mul_comm] - -/-- Two elements of `Vector d ⊗ Vector d` with the same components are equal. -/ -lemma _root_.Lorentz.Vector.tensorComponent_ext {d : ℕ} {F G : Vector d ⊗[ℝ] Vector d} - (h : ∀ μ ν, Vector.tensorComponent μ ν F = Vector.tensorComponent μ ν G) : F = G := - (Vector.basis.tensorProduct Vector.basis).repr.injective (Finsupp.ext fun μν => h μν.1 μν.2) - -/-- An element of `Vector d ⊗ Vector d` is the sum `F^μν e_μ ⊗ e_ν` of its components. -/ -lemma _root_.Lorentz.Vector.eq_sum_tensorComponent {d : ℕ} (F : Vector d ⊗[ℝ] Vector d) : - F = ∑ μ, ∑ ν, Vector.tensorComponent μ ν F • Vector.basis μ ⊗ₜ[ℝ] Vector.basis ν := by - simp only [Vector.tensorComponent_apply, ← Fintype.sum_prod_type'] - exact ((Vector.basis.tensorProduct Vector.basis).sum_repr _).symm.trans - (Finset.sum_congr rfl fun _ _ => by rw [Module.Basis.tensorProduct_apply]) - /-- The components `T_μ^ν` of the tensor of `T` in `CoVector d ⊗ Vector d` are its coordinates in Mathlib's product basis `Basis.tensorProduct`. -/ lemma tensorBasis_repr_toTensor_coVector_tmul (d : ℕ) (T : CoVector d ⊗[ℝ] Vector d) diff --git a/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean b/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean index 7855965de8..a8a8994af0 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean @@ -7,6 +7,7 @@ module public import Mathlib.Analysis.InnerProductSpace.PiL2 public import Mathlib.Geometry.Manifold.IsManifold.Basic +public import Physlib.Mathematics.ForMathlib.TensorProductMatrix /-! # Lorentz Vectors @@ -324,6 +325,34 @@ lemma sum_inl_inr_basis_eq_zero_iff {d : ℕ} (f₀ : ℝ) (f : Fin d → ℝ) : rw [h1, sum_basis_eq_zero_iff] simp [f'] +/-- The component `F^μν` of `F` in `Vector d ⊗ Vector d`, its coordinate at `(μ, ν)` in Mathlib's +product basis `e_μ ⊗ e_ν`. -/ +def tensorComponent {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) : Vector d ⊗[ℝ] Vector d →ₗ[ℝ] ℝ := + Finsupp.lapply (μ, ν) ∘ₗ (basis.tensorProduct basis).repr.toLinearMap + +/-- The component `F^μν` is the coordinate of `F` at `(μ, ν)` in Mathlib's product basis. -/ +lemma tensorComponent_apply {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (F : Vector d ⊗[ℝ] Vector d) : + tensorComponent μ ν F = (basis.tensorProduct basis).repr F (μ, ν) := rfl + +/-- The components of `v ⊗ w` are the products of components, `(v ⊗ w)^μν = v^μ w^ν`. -/ +@[simp] +lemma tensorComponent_tmul {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (v w : Vector d) : + tensorComponent μ ν (v ⊗ₜ w) = v μ * w ν := by + simp [tensorComponent_apply, Module.Basis.tensorProduct_repr_tmul_apply, basis_repr_apply, + mul_comm] + +/-- Two elements of `Vector d ⊗ Vector d` with the same components are equal. -/ +lemma tensorComponent_ext {d : ℕ} {F G : Vector d ⊗[ℝ] Vector d} + (h : ∀ μ ν, tensorComponent μ ν F = tensorComponent μ ν G) : F = G := + (basis.tensorProduct basis).ext_elem fun μν => h μν.1 μν.2 + +/-- An element of `Vector d ⊗ Vector d` is the sum `F^μν e_μ ⊗ e_ν` of its components. -/ +lemma eq_sum_tensorComponent {d : ℕ} (F : Vector d ⊗[ℝ] Vector d) : + F = ∑ μ, ∑ ν, tensorComponent μ ν F • basis μ ⊗ₜ[ℝ] basis ν := by + conv_lhs => rw [← (basis.tensorProductToMatrix basis).symm_apply_apply F, + Module.Basis.tensorProductToMatrix_symm_apply] + rfl + /-! ## C. The Spatial part From 6919d5a95bd45d93b6560974a5913bb0c286db2b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:39:56 +0000 Subject: [PATCH 150/242] refactor(Relativity): transpose_metricRaw, neg_metricRaw_mul_metricRaw, metricRaw_mul_neg_metricRaw in Fermions/Weyl/Metric complexLorentz.formEntries_dual_mul and formEntries_antisymm are proved from them. Co-authored-by: Claude Opus 5.5 --- Physlib/Relativity/Fermions/Weyl/Metric.lean | 13 +++++++++++++ .../Relativity/Tensors/ComplexTensor/Species.lean | 9 ++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Physlib/Relativity/Fermions/Weyl/Metric.lean b/Physlib/Relativity/Fermions/Weyl/Metric.lean index 891fb4584e..ab75912db9 100644 --- a/Physlib/Relativity/Fermions/Weyl/Metric.lean +++ b/Physlib/Relativity/Fermions/Weyl/Metric.lean @@ -36,6 +36,19 @@ open CategoryTheory.MonoidalCategory /-- The raw `2x2` matrix corresponding to the metric for fermions. -/ def metricRaw : Matrix (Fin 2) (Fin 2) ℂ := !![0, 1; -1, 0] +/-- The metric is antisymmetric, `εᵀ = -ε`. -/ +lemma transpose_metricRaw : metricRawᵀ = -metricRaw := by + ext i j + fin_cases i <;> fin_cases j <;> simp [metricRaw] + +/-- The metric with its indices raised is the inverse of the metric, `(-ε) ε = 1`. -/ +lemma neg_metricRaw_mul_metricRaw : -metricRaw * metricRaw = 1 := by + simpa [metricRaw] using Matrix.one_fin_two.symm + +/-- The metric is the inverse of its negative, `ε (-ε) = 1`. -/ +lemma metricRaw_mul_neg_metricRaw : metricRaw * -metricRaw = 1 := by + simpa [metricRaw] using Matrix.one_fin_two.symm + /-- Multiplying an element of `SL(2, ℂ)` on the left with the metric `𝓔` is equivalent to multiplying the inverse-transpose of that element on the right with the metric. -/ lemma comm_metricRaw (M : SL(2,ℂ)) : M.1 * metricRaw = metricRaw * (M.1⁻¹)ᵀ := by diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index f5ceafe5b2..1f8a8f39b2 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -310,10 +310,8 @@ lemma basis_tensorProduct_repr_form : ∀ (c : Color Kind) (μ ν : Idx c.kind), /-- The matrices of the forms at `c.dual` and at `c` are inverse, `(-ε) ε = 1` and `η η = 1`. -/ lemma formEntries_dual_mul : ∀ c : Color Kind, Matrix.of (formEntries c.dual) * Matrix.of (formEntries c) = 1 - | ⟨left, .up⟩ | ⟨right, .up⟩ => show -metricRaw * metricRaw = 1 by - simpa [metricRaw] using Matrix.one_fin_two.symm - | ⟨left, .down⟩ | ⟨right, .down⟩ => show metricRaw * -metricRaw = 1 by - simpa [metricRaw] using Matrix.one_fin_two.symm + | ⟨left, .up⟩ | ⟨right, .up⟩ => neg_metricRaw_mul_metricRaw + | ⟨left, .down⟩ | ⟨right, .down⟩ => metricRaw_mul_neg_metricRaw | ⟨vec, _⟩ => show (@minkowskiMatrix 3).map Complex.ofRealHom * (@minkowskiMatrix 3).map Complex.ofRealHom = 1 by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)] @@ -496,7 +494,8 @@ instance : complexLorentz.IsSymmetricForm vec where lemma formEntries_antisymm : ∀ c : Color Kind, c.kind ≠ vec → ∀ μ ν, formEntries c ν μ = -1 * formEntries c μ ν | ⟨left, .up⟩, _ | ⟨left, .down⟩, _ | ⟨right, .up⟩, _ | ⟨right, .down⟩, _ => fun μ ν => by - fin_cases μ <;> fin_cases ν <;> simp [formEntries, metricRaw] + have h : metricRaw ν μ = -metricRaw μ ν := congrFun₂ transpose_metricRaw μ ν + simp [formEntries, h] | ⟨vec, _⟩, h => absurd rfl h /-- The form `ε` is antisymmetric at the Weyl colors. -/ From be7852292c00a243d348f3b60c9f7a4c8d0db2d3 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:41:44 +0000 Subject: [PATCH 151/242] refactor(ForMathlib): Equiv.SamePair in ForMathlib/EquivPairs, noPairs from Equiv.sumEmpty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Equiv.SamePair, SamePair.left_unique, SamePair.right_unique, exists_samePair_left and SamePair.bijective are stated for equivalences ι ≃ κ ⊕ (J ⊕ J). ColorEquiv.SamePair and its lemmas are removed, and Tensor.contr_eq_of_samePair takes σ.toEquiv.SamePair τ.toEquiv. ColorEquiv.noPairs uses Mathlib's Equiv.sumEmpty. Co-authored-by: Claude Opus 5.5 --- Physlib.lean | 1 + .../Mathematics/ForMathlib/EquivPairs.lean | 121 ++++++++++++++++++ Physlib/Mathematics/Tensors/Contraction.lean | 77 ++--------- 3 files changed, 130 insertions(+), 69 deletions(-) create mode 100644 Physlib/Mathematics/ForMathlib/EquivPairs.lean diff --git a/Physlib.lean b/Physlib.lean index bc53dc7bf0..9e5823abaf 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -160,6 +160,7 @@ public import Physlib.Mathematics.ForMathlib.ComplexLinear public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.ForMathlib.CoordTensorHom public import Physlib.Mathematics.ForMathlib.DataStructures.Matrix.LieTrace +public import Physlib.Mathematics.ForMathlib.EquivPairs public import Physlib.Mathematics.ForMathlib.FDerivCurry public import Physlib.Mathematics.ForMathlib.Fin public import Physlib.Mathematics.ForMathlib.Fin.Involutions diff --git a/Physlib/Mathematics/ForMathlib/EquivPairs.lean b/Physlib/Mathematics/ForMathlib/EquivPairs.lean new file mode 100644 index 0000000000..68aebf4037 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/EquivPairs.lean @@ -0,0 +1,121 @@ +/- +Copyright (c) 2026 Andrea Pari. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Andrea Pari +-/ +module + +public import Mathlib.Logic.Equiv.Defs +public import Mathlib.Logic.Function.Basic + +/-! + +# Pairs of elements chosen by an equivalence + +An equivalence `σ : ι ≃ κ ⊕ (J ⊕ J)` chooses disjoint pairs of elements of `ι`, one for each `j`. + +## i. Overview + +An equivalence `σ : ι ≃ κ ⊕ (J ⊕ J)` splits `ι` into the elements `σ.symm (inl x)`, one for each +`x : κ`, and the pairs `σ.symm (inr (inl j))` and `σ.symm (inr (inr j))`, one for each `j : J`. A +contraction of tensor indices is chosen this way, as in `T^ρμ_μ`, where `ρ` is left alone and the +two indices named `μ` form a pair. + +Two such equivalences `σ` and `τ` may list the same pairs in another order, or with the two +elements of a pair swapped. `Equiv.SamePair σ τ j j'` says that the pair `j` of `σ` and the pair +`j'` of `τ` are the same two elements, in either order. A pair of one is the same pair as at most +one pair of the other. When `σ` and `τ` agree on `κ` and every pair of `σ` is a pair of `τ`, the +matching of pairs is a bijection `J → J'`. + +## ii. Key results + +- `Equiv.SamePair` : the pair `j` of `σ` and the pair `j'` of `τ` are the same two elements. +- `Equiv.SamePair.left_unique` and `Equiv.SamePair.right_unique` : the matching of pairs is + injective in each argument. +- `Equiv.SamePair.bijective` : when `σ` and `τ` agree on `κ`, a matching of every pair of `σ` with + a pair of `τ` is a bijection. + +## iii. Table of contents + +- A. The same pair of two equivalences + +## iv. References + +There are no known references for the material in this module. + +-/ + +@[expose] public section + +/-! + +## A. The same pair of two equivalences + +-/ + +namespace Equiv + +variable {ι κ J J' : Type*} + +/-- The pair `j` of `σ` and the pair `j'` of `τ` are the same two elements, in either order. -/ +def SamePair (σ : ι ≃ κ ⊕ (J ⊕ J)) (τ : ι ≃ κ ⊕ (J' ⊕ J')) (j : J) (j' : J') : Prop := + (σ.symm (.inr (.inl j)) = τ.symm (.inr (.inl j')) ∧ + σ.symm (.inr (.inr j)) = τ.symm (.inr (.inr j'))) ∨ + (σ.symm (.inr (.inl j)) = τ.symm (.inr (.inr j')) ∧ + σ.symm (.inr (.inr j)) = τ.symm (.inr (.inl j'))) + +instance [DecidableEq ι] (σ : ι ≃ κ ⊕ (J ⊕ J)) (τ : ι ≃ κ ⊕ (J' ⊕ J')) (j : J) (j' : J') : + Decidable (σ.SamePair τ j j') := by + unfold SamePair + infer_instance + +variable {σ : ι ≃ κ ⊕ (J ⊕ J)} {τ : ι ≃ κ ⊕ (J' ⊕ J')} + +/-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ +lemma SamePair.left_unique {j₁ j₂ : J} {j' : J'} (h₁ : σ.SamePair τ j₁ j') + (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by + have hσ := σ.symm.injective + rcases h₁ with ⟨h₁, h₁'⟩ | ⟨h₁, h₁'⟩ <;> rcases h₂ with ⟨h₂, h₂'⟩ | ⟨h₂, h₂'⟩ + · simpa using hσ (h₁.trans h₂.symm) + · simpa using hσ (h₁'.trans h₂.symm) + · simpa using hσ (h₁.trans h₂'.symm) + · simpa using hσ (h₁.trans h₂.symm) + +/-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ +lemma SamePair.right_unique {j : J} {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) + (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by + have hτ := τ.symm.injective + rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> + simpa using hτ (h₁.symm.trans h₂) + +/-- When `σ` and `τ` agree on `κ` and every pair of `σ` is a pair of `τ`, every pair of `τ` is a +pair of `σ`. -/ +lemma exists_samePair_left (hfree : ∀ x, σ.symm (.inl x) = τ.symm (.inl x)) + (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by + have hτ := τ.symm.injective + -- The elements of the pair `j'` of `τ` are not in `κ`, so they lie in a pair of `σ`. + rcases hs : σ (τ.symm (.inr (.inl j'))) with x | j | j <;> + rw [← Equiv.eq_symm_apply] at hs + · rw [hfree] at hs + have := hτ hs + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · obtain rfl : j'' = j' := by simpa using hτ (h.1.symm.trans hs.symm) + exact ⟨j, .inl h⟩ + · have := hτ (h.1.symm.trans hs.symm) + simp at this + · obtain ⟨j'', h | h⟩ := h₁ j + · have := hτ (h.2.symm.trans hs.symm) + simp at this + · obtain rfl : j'' = j' := by simpa using hτ (h.2.symm.trans hs.symm) + exact ⟨j, .inr h⟩ + +/-- When `σ` and `τ` agree on `κ`, a matching `g` of every pair of `σ` with the same pair of `τ` is +a bijection. -/ +lemma SamePair.bijective (hfree : ∀ x, σ.symm (.inl x) = τ.symm (.inl x)) {g : J → J'} + (hg : ∀ j, σ.SamePair τ j (g j)) : Function.Bijective g := + ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), + fun j' => (exists_samePair_left hfree (fun j => ⟨g j, hg j⟩) j').imp + fun j hj => (hg j).right_unique hj⟩ + +end Equiv diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 600664589f..b4927b78ac 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Physlib.Mathematics.ForMathlib.EquivPairs public import Physlib.Mathematics.Tensors.Product public import Mathlib.LinearAlgebra.PiTensorProduct.Dual /-! @@ -52,9 +53,8 @@ since the bases of a color and its dual are dual. - `Tensor.contr` is an intertwining map, so it commutes with the group action. - `Tensor.contr_reindex` and `Tensor.reindex_contr` absorb a reindexing before or after a contraction. -- `ColorEquiv.SamePair` says that two choices of pairs contract the same two indices, and - `Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, - in any order, are equal. +- `Tensor.contr_eq_of_samePair` proves that two contractions of the same pairs of indices, in any + order, are equal, the pairs matched by `Equiv.SamePair`. - `Tensor.contr_noPairs` proves that a contraction with no pairs, along `ColorEquiv.noPairs`, is the identity. - `Tensor.contr_eq_zero_of_reindex_eq_neg` and @@ -111,74 +111,15 @@ namespace ColorEquiv variable {K : Type} {ι₁ ι₂ κ κ₁ J J' J₁ J₂ : Type} -/-- The reindexing of a contraction with no pairs, where every index is free and stays in place. -/ +/-- The reindexing of a contraction with no pairs, where every index is free and stays in place. +It is the inverse of Mathlib's `Equiv.sumEmpty`. -/ def noPairs (c : ι₁ → Color K) : ColorEquiv c (contrColors c (Empty.elim : Empty → Color K)) where - toEquiv.toFun := .inl - toEquiv.invFun := Sum.elim id (Sum.elim Empty.elim Empty.elim) - toEquiv.left_inv _ := rfl - toEquiv.right_inv := by rintro (x | e | e) <;> first | rfl | exact e.elim + toEquiv := (Equiv.sumEmpty ι₁ (Empty ⊕ Empty)).symm color_eq := by rintro (x | e | e) <;> first | rfl | exact e.elim variable {c : ι₁ → Color K} {c' : κ → Color K} -/-- The pair `j` of `σ` and the pair `j'` of `τ` contract the same two indices, in either order. -/ -def SamePair {a : J → Color K} {a' : J' → Color K} - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) - (j' : J') : Prop := - (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inl j')) ∧ - σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inr j'))) ∨ - (σ.toEquiv.symm (.inr (.inl j)) = τ.toEquiv.symm (.inr (.inr j')) ∧ - σ.toEquiv.symm (.inr (.inr j)) = τ.toEquiv.symm (.inr (.inl j'))) - -instance [DecidableEq ι₁] {a : J → Color K} {a' : J' → Color K} - (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (j : J) - (j' : J') : Decidable (σ.SamePair τ j j') := by - unfold SamePair - infer_instance - -/-- A pair of `τ` is the same pair as at most one pair of `σ`. -/ -lemma SamePair.left_unique {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j₁ j₂ : J} - {j' : J'} (h₁ : σ.SamePair τ j₁ j') (h₂ : σ.SamePair τ j₂ j') : j₁ = j₂ := by - have hσ := σ.toEquiv.symm.injective - rcases h₁ with ⟨h₁, h₁'⟩ | ⟨h₁, h₁'⟩ <;> rcases h₂ with ⟨h₂, h₂'⟩ | ⟨h₂, h₂'⟩ - · simpa using hσ (h₁.trans h₂.symm) - · simpa using hσ (h₁'.trans h₂.symm) - · simpa using hσ (h₁.trans h₂'.symm) - · simpa using hσ (h₁.trans h₂.symm) - -/-- A pair of `σ` is the same pair as at most one pair of `τ`. -/ -lemma SamePair.right_unique {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} {j : J} - {j₁ j₂ : J'} (h₁ : σ.SamePair τ j j₁) (h₂ : σ.SamePair τ j j₂) : j₁ = j₂ := by - have hτ := τ.toEquiv.symm.injective - rcases h₁ with ⟨h₁, -⟩ | ⟨h₁, -⟩ <;> rcases h₂ with ⟨h₂, -⟩ | ⟨h₂, -⟩ <;> - simpa using hτ (h₁.symm.trans h₂) - -/-- When `σ` and `τ` keep the same free indices and every pair of `σ` is a pair of `τ`, every pair -of `τ` is a pair of `σ`. The indices of a pair of `τ` are not free, so they lie in a pair of `σ`. -/ -lemma exists_samePair_left {a : J → Color K} {a' : J' → Color K} - {σ : ColorEquiv c (contrColors c' a)} {τ : ColorEquiv c (contrColors c' a')} - (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) - (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (j' : J') : ∃ j, σ.SamePair τ j j' := by - have hτ := τ.toEquiv.symm.injective - rcases hs : σ.toEquiv (τ.toEquiv.symm (.inr (.inl j'))) with x | j | j <;> - rw [← Equiv.eq_symm_apply] at hs - · rw [hfree] at hs - have := hτ hs - simp at this - · obtain ⟨j'', h | h⟩ := h₁ j - · obtain rfl : j'' = j' := by simpa using hτ (h.1.symm.trans hs.symm) - exact ⟨j, .inl h⟩ - · have := hτ (h.1.symm.trans hs.symm) - simp at this - · obtain ⟨j'', h | h⟩ := h₁ j - · have := hτ (h.2.symm.trans hs.symm) - simp at this - · obtain rfl : j'' = j' := by simpa using hτ (h.2.symm.trans hs.symm) - exact ⟨j, .inr h⟩ - /-- The pairs of two contractions in sequence, `σ₁` and then `σ₂`, as one choice of pairs `J₁ ⊕ J₂`, the pairs `J₁` of `σ₁` followed by the pairs `J₂` of `σ₂`. -/ noncomputable def chain {c₁ : κ₁ → Color K} {a₁ : J₁ → Color K} {a₂ : J₂ → Color K} @@ -320,7 +261,7 @@ the same pairs of indices, in any order and with either index of a pair first. - lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' → Color K} (σ : ColorEquiv c (contrColors c' a)) (τ : ColorEquiv c (contrColors c' a')) (hfree : ∀ x, σ.toEquiv.symm (.inl x) = τ.toEquiv.symm (.inl x)) - (h₁ : ∀ j, ∃ j', σ.SamePair τ j j') (t : Tensor S c) : + (h₁ : ∀ j, ∃ j', σ.toEquiv.SamePair τ.toEquiv j j') (t : Tensor S c) : contr σ t = contr τ t := by refine DFunLike.congr_fun (f := contr σ) (g := contr τ) ?_ t ext p @@ -329,9 +270,7 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' choose g hg using h₁ congr 1 · -- The pair `j` of `σ` is the pair `g j` of `τ`, in the same order or swapped. - refine Fintype.prod_bijective g ⟨fun j₁ j₂ hj => (hg j₁).left_unique (hj ▸ hg j₂), - fun j' => (ColorEquiv.exists_samePair_left hfree (fun j => ⟨g j, hg j⟩) j').imp - fun j hj => (hg j).right_unique hj⟩ _ _ fun j => ?_ + refine Fintype.prod_bijective g (Equiv.SamePair.bijective hfree hg) _ _ fun j => ?_ rcases hg j with ⟨h, h'⟩ | ⟨h, h'⟩ <;> rw [cast_apply_congr p h, cast_apply_congr p h'] exacts [contr_cast_tmul_cast _ _ _ _ _ _, contr_cast_tmul_cast_symm _ _ _ _ _ _] · exact congrArg S.pure (funext fun x => cast_apply_congr p (hfree x) _) From bfc507ac08535152cbad0e1c6c52a6bb4c4376f5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:42:04 +0000 Subject: [PATCH 152/242] =?UTF-8?q?style(Tensors):=20wrap=20the=20finrank?= =?UTF-8?q?=5Feq=E2=82=9B=E2=82=97=20rewrite=20in=20toLin=5Fbijective=5Fof?= =?UTF-8?q?=5FisPosDef?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/HermitianForm.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index a84c8c9e7c..a2f82e1b73 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -118,8 +118,8 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} -- Surjective since the dimensions agree, through the conjugation `V a.dual ≃ V b.dual`. have hi := toLin_injective_of_isPosDef g hg refine ⟨hi, (LinearMap.injective_iff_surjective_of_finrank_eq_finrank ?_).1 hi⟩ - rw [(S.conjEquiv (congrArg Color.dual hb)).finrank_eqₛₗ, Module.finrank_eq_card_basis (S.basis b.dual), - Module.finrank_eq_card_basis (S.basis b)] + rw [(S.conjEquiv (congrArg Color.dual hb)).finrank_eqₛₗ, + Module.finrank_eq_card_basis (S.basis b.dual), Module.finrank_eq_card_basis (S.basis b)] /-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) From 1adc697bb98bbf235ecaed2a5a221585e1ed15bb Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:55:13 +0000 Subject: [PATCH 153/242] chore: remove transitive imports in EquivPairs, SemilinearMaps and RealTensor/Species Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/EquivPairs.lean | 1 - Physlib/Mathematics/ForMathlib/SemilinearMaps.lean | 1 - Physlib/Relativity/Tensors/RealTensor/Species.lean | 1 - 3 files changed, 3 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/EquivPairs.lean b/Physlib/Mathematics/ForMathlib/EquivPairs.lean index 68aebf4037..2d6350099a 100644 --- a/Physlib/Mathematics/ForMathlib/EquivPairs.lean +++ b/Physlib/Mathematics/ForMathlib/EquivPairs.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Mathlib.Logic.Equiv.Defs -public import Mathlib.Logic.Function.Basic /-! diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index 566a9425e8..b826e1195a 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Dimension.Finrank public import Mathlib.LinearAlgebra.PiTensorProduct.Basic public import Physlib.Mathematics.ForMathlib.Contraction diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 8b9150dab0..6449363874 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -8,7 +8,6 @@ module public import Physlib.Mathematics.Tensors.ElabTactics public import Physlib.Relativity.Tensors.RealTensor.Representation.Contraction public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct -public import Physlib.Relativity.Tensors.RealTensor.Vector.Representation import Mathlib.LinearAlgebra.Matrix.IsDiag /-! From 6d67370ec41ec5f2e713983a7594ce9baca479f5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:33:24 +0000 Subject: [PATCH 154/242] refactor(ForMathlib): Module.Basis.coordPairing_invariant replaces Species.invariant_of_contr The lemma mentions only bases and representations, so it moves to ForMathlib/Contraction.lean next to eq_coordPairing, over a commutative semiring and a monoid. Co-authored-by: Claude Opus 5.5 --- .../Mathematics/ForMathlib/Contraction.lean | 23 ++++++++++++++++++- Physlib/Mathematics/Tensors/Species.lean | 17 ++------------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Contraction.lean b/Physlib/Mathematics/ForMathlib/Contraction.lean index 4f0a1a0ed3..76985877e4 100644 --- a/Physlib/Mathematics/ForMathlib/Contraction.lean +++ b/Physlib/Mathematics/ForMathlib/Contraction.lean @@ -6,6 +6,7 @@ Authors: Andrea Pari module public import Mathlib.LinearAlgebra.PerfectPairing.Basic +public import Mathlib.RepresentationTheory.Intertwining /-! @@ -24,7 +25,8 @@ pairing for any two modules `M` and `N` with bases `b` and `b'` labelled by the The coordinate pairing `coordPairing` sends `x ⊗ y` to `∑_I (b x)_I (b' y)_I`. It is Mathlib's `b'.toDual`, applied after the equivalence `b.equiv b'` that sends `b I` to `b' I`. The two bases are dual for it, it is the only pairing for which they are, and it is a perfect pairing in -Mathlib's sense. +Mathlib's sense. When the two modules carry representations and some invariant pairing makes the +bases dual, the coordinate pairing is that pairing, so it is invariant. ## ii. Key results @@ -33,10 +35,13 @@ Mathlib's sense. - `Module.Basis.coordPairing_basis_basis` : the two bases are dual for the coordinate pairing, and `Module.Basis.eq_coordPairing` : it is the only such pairing. - `Module.Basis.isPerfPair_coordPairing` : the coordinate pairing is a perfect pairing. +- `Module.Basis.coordPairing_invariant` : the coordinate pairing is invariant when an invariant + pairing makes the bases dual. ## iii. Table of contents - A. The coordinate pairing of two bases +- B. Invariance of the coordinate pairing ## iv. References @@ -108,4 +113,20 @@ instance isPerfPair_coordPairing (b : Module.Basis ι R M) (b' : Module.Basis ι LinearMap.ext₂ fun y x => (coordPairing_comm b b' x y).trans rfl] exact LinearEquiv.bijective _ +/-! + +## B. Invariance of the coordinate pairing + +-/ + +/-- The coordinate pairing is invariant when an invariant pairing `p` makes the bases dual, since +then `p` is the coordinate pairing. -/ +lemma coordPairing_invariant {G : Type*} [Monoid G] {ρ : Representation R G M} + {ρ' : Representation R G N} {b : Module.Basis ι R M} {b' : Module.Basis ι R N} + (p : (ρ.tprod ρ').IntertwiningMap (Representation.trivial R G R)) + (h : ∀ I J, p (b I ⊗ₜ b' J) = if I = J then 1 else 0) (g : G) (x : M) (y : N) : + b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y) := by + rw [← b.eq_coordPairing b' p.toLinearMap h] + simpa using Representation.IntertwiningMap.isIntertwining _ _ p g (x ⊗ₜ y) + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 8142829b1a..bc69af6f6a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -57,7 +57,6 @@ A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.isPerfPair_contr` : the contraction is a perfect pairing, `Species.dualEquiv` : `V c.dual` is the dual representation of `V c`, and `Species.ext_contr` : a vector of `V c.dual` is determined by its contractions. -- `Species.invariant_of_contr` : the condition from a pairing known to be invariant. - `Species.cast` : a vector moved along an equality of colors, with `Species.cast_rep`, `Species.basis_repr_cast`, `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` : this keeps the action, the components and the @@ -150,8 +149,8 @@ The contraction is not a field of `Species`. It is defined from the bases, as `u ⊗ α ↦ ∑ μ, u^μ α_μ`, so the bases of `c` and `c.dual` are dual by definition (`contr_basis`). Its invariance is the condition on the species. It is a perfect pairing in Mathlib's sense, and `dualEquiv` identifies `V c.dual` with Mathlib's dual representation of -`V c`, so Mathlib's results on duals apply. `invariant_of_contr` gives the condition when an -invariant pairing is already known. +`V c`, so Mathlib's results on duals apply. When an invariant pairing for which the bases are +dual is already known, `Module.Basis.coordPairing_invariant` gives the condition. -/ @@ -215,18 +214,6 @@ lemma ext_contr {c : Color K} {α α' : S.V c.dual} (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := (S.dualEquiv c).toLinearEquiv.injective (LinearMap.ext h) -omit S in -/-- The condition `invariant`, from an invariant pairing `p` for which the bases are dual. It is how -a species is built from a pairing that is known to be invariant. -/ -lemma invariant_of_contr {ι M N : Type} [Fintype ι] [DecidableEq ι] [AddCommGroup M] - [Module k M] [AddCommGroup N] [Module k N] {ρ : Representation k G M} - {ρ' : Representation k G N} {b : Module.Basis ι k M} {b' : Module.Basis ι k N} - (p : (ρ.tprod ρ').IntertwiningMap (Representation.trivial k G k)) - (h : ∀ I J, p (b I ⊗ₜ b' J) = if I = J then 1 else 0) (g : G) (x : M) (y : N) : - b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y) := by - rw [← b.eq_coordPairing b' p.toLinearMap h] - simpa using Representation.IntertwiningMap.isIntertwining _ _ p g (x ⊗ₜ y) - variable {S} /-! From 1f7d98b6862110070789f00bd749171a8010b682 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 17:59:11 +0000 Subject: [PATCH 155/242] refactor(Relativity): complexLorentz species uses Module.Basis.coordPairing_invariant Co-authored-by: Claude Opus 5.5 --- Physlib/Relativity/Tensors/ComplexTensor/Species.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 1f8a8f39b2..15aeb78d0c 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -183,9 +183,9 @@ lemma coordPairing_rep : ∀ (c : Color Kind) (g : SL(2, ℂ)) (x : IndexSpace c (indexBasis c).coordPairing (indexBasis c.dual) (indexRep c g x ⊗ₜ indexRep c.dual g y) = (indexBasis c).coordPairing (indexBasis c.dual) (x ⊗ₜ y) := Species.invariant_of_up indexRep indexBasis fun - | left => Species.invariant_of_contr LeftHandedWeyl.contr fun μ ν => + | left => Module.Basis.coordPairing_invariant LeftHandedWeyl.contr fun μ ν => (LeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) - | right => Species.invariant_of_contr RightHandedWeyl.contr fun μ ν => + | right => Module.Basis.coordPairing_invariant RightHandedWeyl.contr fun μ ν => (RightHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) | vec => fun g x y => (coordPairing_complexVector_coVector _ _).trans <| (dotProduct_SL2CRep g x y).trans (coordPairing_complexVector_coVector x y).symm From 43d8ada30493a2ad1cd704c85cc2e490a25bcf07 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:03:48 +0000 Subject: [PATCH 156/242] refactor(ForMathlib): Module.Basis.coordTensorHomEquiv_map_rep, isIntertwiningMap_coordTensorHomEquiv_iff, coordDelta_invariant; Species.toLin_map_rep, delta_invariant as their transports Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 65 +++++++++++++++++++ Physlib/Mathematics/Tensors/LinearMap.lean | 28 +++----- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 8369ad5f1b..d71d91ed23 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -7,6 +7,7 @@ module public import Mathlib.LinearAlgebra.Contraction public import Mathlib.LinearAlgebra.BilinearForm.Properties +public import Mathlib.RepresentationTheory.Invariants public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.ForMathlib.TensorProductMatrix @@ -32,6 +33,10 @@ A tensor `z` of `M ⊗ M` gives the bilinear form `(u, v) ↦ ⟨v, z u⟩` on ` of `z` swaps its arguments, so the form is symmetric when `z` is and alternating when `z` is antisymmetric. +When the coordinate pairing is invariant under representations of a group, the linear map of +`Λz` is `Λ ∘ z ∘ Λ⁻¹`, so `z` is invariant exactly when its linear map is an intertwining map, +and the delta is invariant. + ## ii. Key results - `Module.Basis.coordTensorHomEquiv` : a tensor of `M ⊗ U` as a linear map `N → U`. @@ -44,11 +49,16 @@ antisymmetric. - `Module.Basis.isSymm_coordTensorHomEquiv_of_comm_eq` and `Module.Basis.isAlt_coordTensorHomEquiv_of_comm_eq_neg` : the bilinear form `(u, v) ↦ ⟨v, z u⟩` is symmetric when `comm z = z` and alternating when `comm z = -z`. +- `Module.Basis.coordTensorHomEquiv_map_rep` : the linear map of `(Λ ⊗ f) z` is + `f ∘ z ∘ Λ⁻¹`, and `Module.Basis.isIntertwiningMap_coordTensorHomEquiv_iff` : `z` is invariant + exactly when its linear map is an intertwining map. +- `Module.Basis.coordDelta_invariant` : the delta is invariant. ## iii. Table of contents - A. Tensors as linear maps through the coordinate pairing - B. The bilinear form of a tensor +- C. Equivariance ## iv. References @@ -199,4 +209,59 @@ lemma isAlt_coordTensorHomEquiv_of_comm_eq_neg [NoZeroDivisors R] [CharZero R] rw [← coordPairing_coordTensorHomEquiv_comm, hz, map_neg, LinearMap.neg_apply, tmul_neg, map_neg] +/-! + +## C. Equivariance + +When the coordinate pairing of `b` and `b'` is invariant under representations `ρ` on `M` and `ρ'` +on `N`, the group acts on a tensor of `M ⊗ U` as `Λ ∘ z ∘ Λ⁻¹` on its linear map. + +-/ + +section + +variable {G : Type*} [Group G] {ρ : Representation R G M} {ρ' : Representation R G N} + {b : Module.Basis ι R M} {b' : Module.Basis ι R N} + +/-- The linear map of `(Λ ⊗ f) z` is `f ∘ z ∘ Λ⁻¹` when the coordinate pairing is invariant. -/ +lemma coordTensorHomEquiv_map_rep + (hρ : ∀ g x y, b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y)) + (f : U →ₗ[R] U') (z : M ⊗[R] U) (g : G) : + b.coordTensorHomEquiv b' (TensorProduct.map (ρ g) f z) = + f ∘ₗ b.coordTensorHomEquiv b' z ∘ₗ ρ' g⁻¹ := by + induction z using TensorProduct.inductionOn with + | add z z' hz hz' => simp only [map_add, hz, hz', LinearMap.add_comp, LinearMap.comp_add] + | tmul x y => + ext v + simpa [coordPairing_comm b' b] using + congrArg (· • f y) (hρ g x (ρ' g⁻¹ v)) + +/-- The linear map of `(Λ ⊗ f) z` is bijective when those of `f` and `z` are. -/ +lemma bijective_coordTensorHomEquiv_map_rep + (hρ : ∀ g x y, b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y)) + {f : U →ₗ[R] U'} (hf : Function.Bijective f) {z : M ⊗[R] U} + (hz : Function.Bijective (b.coordTensorHomEquiv b' z)) (g : G) : + Function.Bijective (b.coordTensorHomEquiv b' (TensorProduct.map (ρ g) f z)) := by + rw [coordTensorHomEquiv_map_rep hρ, LinearMap.coe_comp, LinearMap.coe_comp] + exact hf.comp (hz.comp (ρ'.apply_bijective g⁻¹)) + +/-- A tensor is invariant exactly when its linear map is an intertwining map. -/ +lemma isIntertwiningMap_coordTensorHomEquiv_iff + (hρ : ∀ g x y, b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y)) + {σ : Representation R G U} (z : M ⊗[R] U) : + ρ'.IsIntertwiningMap σ (b.coordTensorHomEquiv b' z) ↔ + ∀ g, TensorProduct.map (ρ g) (σ g) z = z := by + rw [← Representation.mem_linHom_invariants_iff_isIntertwining] + exact forall_congr' fun g => by + rw [← (b.coordTensorHomEquiv b').injective.eq_iff, coordTensorHomEquiv_map_rep hρ] + +/-- The delta is invariant when the coordinate pairing is. -/ +lemma coordDelta_invariant + (hρ : ∀ g x y, b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y)) (g : G) : + TensorProduct.map (ρ g) (ρ' g) (b.coordDelta b') = b.coordDelta b' := + (isIntertwiningMap_coordTensorHomEquiv_iff hρ _).1 + (by rw [coordTensorHomEquiv_coordDelta]; exact ⟨fun _ _ => rfl⟩) g + +end + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 073678336c..3de67ab1ce 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -22,8 +22,8 @@ linear map `S.toLin z : V a.dual → U` that contracts its input with the first pure tensor `z = x ⊗ y` it is `v ↦ (v^μ x_μ) y`. Every linear map `V a.dual → U` is `toLin z` for exactly one `z`. `toLin` is `Module.Basis.coordTensorHomEquiv` for the bases of `a` and `a.dual`, which is Mathlib's `dualTensorHomEquivOfBasis` once the contraction identifies `V a` with the dual -of `V a.dual`. The results that need only the bases, such as the matrix of `toLin z`, are stated -there. +of `V a.dual`. The results that need only the bases and the invariance of the contraction, such as +the matrix of `toLin z` and its equivariance, are stated there. This is how two-index tensors act as maps. A metric `g_μν` is the map `v^μ ↦ v^μ g_μν`, and the inverse metric `g^μν` is the tensor of the inverse map. The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, so `z` is @@ -161,25 +161,20 @@ lemma coordPairing_basis_dual (c : Color K) {x : S.V c.dual} {y : S.V c} : lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := - LinearMap.ext fun x => by - induction z using TensorProduct.inductionOn with - | tmul w y => - simpa using congrArg (· • S.rep b g y) (S.contr_invariant a.dual g (S.rep a.dual g⁻¹ x) w) - | add z z' hz hz' => simp only [map_add, LinearMap.add_apply, hz, hz', LinearMap.comp_apply] + Module.Basis.coordTensorHomEquiv_map_rep (S.invariant a) _ z g /-- The linear map of `Λz` is bijective when that of `z` is. -/ lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin z)) (g : G) : - Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := by - rw [toLin_map_rep, LinearMap.coe_comp, LinearMap.coe_comp] - exact ((S.rep b).apply_bijective g).comp (h.comp ((S.rep a.dual).apply_bijective g⁻¹)) + Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := + Module.Basis.bijective_coordTensorHomEquiv_map_rep (S.invariant a) + ((S.rep b).apply_bijective g) h g /-- A tensor is invariant exactly when its linear map commutes with the group action. -/ lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : (S.rep a.dual).IsIntertwiningMap (S.rep b) (S.toLin z) ↔ - ∀ g, TensorProduct.map (S.rep a g) (S.rep b g) z = z := by - rw [← Representation.mem_linHom_invariants_iff_isIntertwining] - exact forall_congr' fun g => by rw [← toLin_map_rep, S.toLin.injective.eq_iff] + ∀ g, TensorProduct.map (S.rep a g) (S.rep b g) z = z := + Module.Basis.isIntertwiningMap_coordTensorHomEquiv_iff (S.invariant a) z /-! @@ -194,11 +189,8 @@ noncomputable abbrev delta (c : Color K) : S.V c.dual ⊗[k] S.V c := /-- The delta is invariant, `Λ δ = δ`, with `Λ` acting on both factors. -/ lemma delta_invariant (c : Color K) (g : G) : - TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := by - -- The linear map of the delta is the identity, which commutes with the action. - refine (S.isIntertwiningMap_toLin_iff (S.delta c)).1 ?_ g - rw [Module.Basis.coordTensorHomEquiv_coordDelta] - exact ⟨fun _ _ => rfl⟩ + TensorProduct.map (S.rep c.dual g) (S.rep c g) (S.delta c) = S.delta c := + Module.Basis.coordDelta_invariant (S.invariant c.dual) g variable {S} From 110ceabea2bb4b903d2ff88eb2584bd4d77e73ed Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:05:41 +0000 Subject: [PATCH 157/242] refactor(ForMathlib): Module.Basis.coordInverse with its algebra, Species.inverse as its transport Removes Species.toLin_inverse_bijective, toLin_inverse_comp, comp_toLin_inverse, inverse_inverse, toLin_map_rep and toLin_map_rep_bijective, which restated the general lemmas with no caller left. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 101 ++++++++++++++++++ Physlib/Mathematics/Tensors/Inverse.lean | 54 +++------- Physlib/Mathematics/Tensors/LinearMap.lean | 22 +--- 3 files changed, 120 insertions(+), 57 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index d71d91ed23..4f1f385da0 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -37,6 +37,11 @@ When the coordinate pairing is invariant under representations of a group, the l `Λz` is `Λ ∘ z ∘ Λ⁻¹`, so `z` is invariant exactly when its linear map is an intertwining map, and the delta is invariant. +A tensor `z` of `M ⊗ P` whose linear map `N → P` is bijective has an inverse, the tensor of +`Q ⊗ N` whose linear map `P → N` inverts it, for bases `d` of `P` and `d'` of `Q`. The inverse is +unique, involutive, and commutes with the group action. When the linear map of `z` is not +bijective the inverse is the junk value `0`, so every lemma about it assumes bijectivity. + ## ii. Key results - `Module.Basis.coordTensorHomEquiv` : a tensor of `M ⊗ U` as a linear map `N → U`. @@ -53,12 +58,19 @@ and the delta is invariant. `f ∘ z ∘ Λ⁻¹`, and `Module.Basis.isIntertwiningMap_coordTensorHomEquiv_iff` : `z` is invariant exactly when its linear map is an intertwining map. - `Module.Basis.coordDelta_invariant` : the delta is invariant. +- `Module.Basis.coordInverse` : the inverse of a tensor, with + `Module.Basis.coordTensorHomEquiv_coordInverse_comp` and + `Module.Basis.comp_coordTensorHomEquiv_coordInverse` : it is a left and a right inverse, + `Module.Basis.coordInverse_eq_of_comp_eq_id` : it is unique, + `Module.Basis.coordInverse_coordInverse` : `(z⁻¹)⁻¹ = z`, and + `Module.Basis.coordInverse_map_rep` : `(Λz)⁻¹ = Λ(z⁻¹)`. ## iii. Table of contents - A. Tensors as linear maps through the coordinate pairing - B. The bilinear form of a tensor - C. Equivariance +- D. The inverse ## iv. References @@ -264,4 +276,93 @@ lemma coordDelta_invariant end +/-! + +## D. The inverse + +A tensor `z` of `M ⊗ P` is a linear map `N → P`. Its inverse is the tensor of `Q ⊗ N` whose linear +map `P → N` inverts it, read through the bases `d` of `P` and `d'` of `Q`. + +-/ + +section + +variable {κ P Q : Type*} [Fintype κ] [DecidableEq κ] [AddCommGroup P] [Module R P] + [AddCommGroup Q] [Module R Q] (b : Module.Basis ι R M) (b' : Module.Basis ι R N) + (d : Module.Basis κ R P) (d' : Module.Basis κ R Q) + +open scoped Classical in +/-- The inverse of `z : M ⊗ P`, the tensor of `Q ⊗ N` whose linear map `P → N` inverts that of `z`, +and the junk value `0` when the linear map of `z` is not bijective. -/ +noncomputable def coordInverse (z : M ⊗[R] P) : Q ⊗[R] N := + if h : Function.Bijective (b.coordTensorHomEquiv b' z) then + (d'.coordTensorHomEquiv d).symm (LinearEquiv.ofBijective _ h).symm.toLinearMap + else 0 + +variable {b b' d d'} + +/-- The linear map of the inverse is the inverse of the linear map. -/ +lemma coordTensorHomEquiv_coordInverse {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) : + d'.coordTensorHomEquiv d (b.coordInverse b' d d' z) = + (LinearEquiv.ofBijective _ h).symm.toLinearMap := by + simp [coordInverse, h] + +/-- The linear map of the inverse is bijective. -/ +lemma bijective_coordTensorHomEquiv_coordInverse {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) : + Function.Bijective (d'.coordTensorHomEquiv d (b.coordInverse b' d d' z)) := by + rw [coordTensorHomEquiv_coordInverse h] + exact (LinearEquiv.ofBijective _ h).symm.bijective + +/-- The inverse is a left inverse, as linear maps. -/ +lemma coordTensorHomEquiv_coordInverse_comp {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) : + d'.coordTensorHomEquiv d (b.coordInverse b' d d' z) ∘ₗ b.coordTensorHomEquiv b' z = + LinearMap.id := by + rw [coordTensorHomEquiv_coordInverse h] + exact (LinearEquiv.ofBijective _ h).symm_comp + +/-- The inverse is a right inverse, as linear maps. -/ +lemma comp_coordTensorHomEquiv_coordInverse {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) : + b.coordTensorHomEquiv b' z ∘ₗ d'.coordTensorHomEquiv d (b.coordInverse b' d d' z) = + LinearMap.id := by + rw [coordTensorHomEquiv_coordInverse h] + exact (LinearEquiv.ofBijective _ h).comp_symm + +/-- A tensor `w` whose linear map is a left inverse of that of `z` is the inverse of `z`. -/ +lemma coordInverse_eq_of_comp_eq_id {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) {w : Q ⊗[R] N} + (hw : d'.coordTensorHomEquiv d w ∘ₗ b.coordTensorHomEquiv b' z = LinearMap.id) : + b.coordInverse b' d d' z = w := + (d'.coordTensorHomEquiv d).injective <| (LinearMap.cancel_right h.2).1 <| by + rw [coordTensorHomEquiv_coordInverse_comp h, hw] + +/-- The inverse of the inverse is the tensor itself. -/ +lemma coordInverse_coordInverse {z : M ⊗[R] P} + (h : Function.Bijective (b.coordTensorHomEquiv b' z)) : + d'.coordInverse d b' b (b.coordInverse b' d d' z) = z := + coordInverse_eq_of_comp_eq_id (bijective_coordTensorHomEquiv_coordInverse h) + (comp_coordTensorHomEquiv_coordInverse h) + +/-- The inverse commutes with the group action, `(Λz)⁻¹ = Λ(z⁻¹)`, when the coordinate pairings of +`b` and `b'` and of `d` and `d'` are invariant. -/ +lemma coordInverse_map_rep {G : Type*} [Group G] {ρ : Representation R G M} + {ρ' : Representation R G N} {σ : Representation R G P} {σ' : Representation R G Q} + (hρ : ∀ g x y, b.coordPairing b' (ρ g x ⊗ₜ ρ' g y) = b.coordPairing b' (x ⊗ₜ y)) + (hσ : ∀ g x y, d.coordPairing d' (σ g x ⊗ₜ σ' g y) = d.coordPairing d' (x ⊗ₜ y)) + {z : M ⊗[R] P} (h : Function.Bijective (b.coordTensorHomEquiv b' z)) (g : G) : + b.coordInverse b' d d' (TensorProduct.map (ρ g) (σ g) z) = + TensorProduct.map (σ' g) (ρ' g) (b.coordInverse b' d d' z) := by + have hσ' : ∀ g x y, d'.coordPairing d (σ' g x ⊗ₜ σ g y) = d'.coordPairing d (x ⊗ₜ y) := + fun g x y => by rw [coordPairing_comm, hσ, coordPairing_comm] + refine coordInverse_eq_of_comp_eq_id + (bijective_coordTensorHomEquiv_map_rep hρ (σ.apply_bijective g) h g) (LinearMap.ext fun x => ?_) + simp [coordTensorHomEquiv_map_rep hρ, coordTensorHomEquiv_map_rep hσ', + Representation.inv_self_apply, Representation.self_inv_apply, + ← LinearMap.comp_apply (d'.coordTensorHomEquiv d _), coordTensorHomEquiv_coordInverse_comp h] + +end + end Module.Basis diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 7434b6ed1e..5ee50035f4 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -17,7 +17,8 @@ The inverse `g^νμ` of a tensor `g_μν`, defined through the linear map that ` A tensor `g` in `V a ⊗ V b` is also a linear map `S.toLin g : V a.dual → V b`, `v^μ ↦ v^μ g_μν`. When this map is invertible, its inverse is the tensor `S.inverse g` in `V b.dual ⊗ V a.dual`, with components `g^νμ`. Otherwise `S.inverse g` is the junk value `0`, so every lemma about it -assumes that `S.toLin g` is bijective. +assumes that `S.toLin g` is bijective. It is `Module.Basis.coordInverse` for the bases, where its +algebra is proved, including `(g⁻¹)⁻¹ = g` (`Module.Basis.coordInverse_coordInverse`). The inverse satisfies `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`, it is unique, it commutes with the group action, and the inverse of the form at `c` is the form at `c.dual`. @@ -28,8 +29,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual linear map. - `Species.contr_mul_inverse` and `Species.contr_inverse_mul` prove `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`. -- `Species.inverse_eq_left_inv` characterises the inverse as a left inverse, and - `Species.inverse_inverse` proves `(g⁻¹)⁻¹ = g`. +- `Species.inverse_eq_left_inv` characterises the inverse as a left inverse. - `Species.inverse_map_rep` proves `(Λg)⁻¹ = Λ(g⁻¹)`, and `Species.inverse_invariant` that the inverse of an invariant tensor is invariant. - `Species.inverse_form` proves that the inverse of the form at `c` is the form at `c.dual`. @@ -66,26 +66,17 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → -/ variable (S) in -open scoped Classical in /-- The inverse `g^νμ ∈ V b.dual ⊗ V a.dual` of `g_μν ∈ V a ⊗ V b`, the tensor whose linear map inverts that of `g`, and the junk value `0` when `S.toLin g` is not bijective. Its first index is the dual of the second index of `g`, so the inverse of a Hermitian metric `g_{IJ̄}` is `g^{J̄I}`, -the usual `g^{IJ̄}` with its indices swapped. -/ +the usual `g^{IJ̄}` with its indices swapped. It is `Module.Basis.coordInverse` for the bases. -/ noncomputable def inverse (g : S.V a ⊗[k] S.V b) : S.V b.dual ⊗[k] S.V a.dual := - if h : Function.Bijective (S.toLin g) then - S.toLin.symm (LinearEquiv.ofBijective _ h).symm.toLinearMap - else 0 + (S.basis a).coordInverse (S.basis a.dual) (S.basis b) (S.basis b.dual) g /-- The linear map of the inverse is the inverse of the linear map. -/ lemma toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.toLin (S.inverse g) = (LinearEquiv.ofBijective _ h).symm.toLinearMap := by - simp [inverse, h] - -/-- The linear map of the inverse is bijective. -/ -lemma toLin_inverse_bijective (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - Function.Bijective (S.toLin (S.inverse g)) := by - rw [toLin_inverse g h] - exact (LinearEquiv.ofBijective _ h).symm.bijective + S.toLin (S.inverse g) = (LinearEquiv.ofBijective _ h).symm.toLinearMap := + Module.Basis.coordTensorHomEquiv_coordInverse h /-! @@ -93,42 +84,27 @@ lemma toLin_inverse_bijective (g : S.V a ⊗[k] S.V b) (h : Function.Bijective ( -/ -/-- Inverting after `g` is the identity, as linear maps. -/ -lemma toLin_inverse_comp (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.toLin (S.inverse g) ∘ₗ S.toLin g = LinearMap.id := by - rw [toLin_inverse g h] - exact (LinearEquiv.ofBijective _ h).symm_comp - -/-- `g` after inverting is the identity, as linear maps. -/ -lemma comp_toLin_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.toLin g ∘ₗ S.toLin (S.inverse g) = LinearMap.id := by - rw [toLin_inverse g h] - exact (LinearEquiv.ofBijective _ h).comp_symm - /-- `g_μν g^νρ = δ_μ^ρ`, the contraction of `g` with its inverse on the adjacent indices. -/ lemma contr_mul_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : Tensor.contr (ColorEquiv.matMul a b a.dual) (S.tmulEquiv _ _ (S.twoIndexEquiv a b g ⊗ₜ S.twoIndexEquiv _ _ (S.inverse g))) = S.deltaTensor a.dual := - (Tensor.contr_matMul_eq_deltaTensor_iff g (S.inverse g)).2 (toLin_inverse_comp g h) + (Tensor.contr_matMul_eq_deltaTensor_iff g (S.inverse g)).2 + (Module.Basis.coordTensorHomEquiv_coordInverse_comp h) /-- `g^νμ g_μρ = δ^ν_ρ`, the contraction of the inverse with `g` on the adjacent indices. -/ lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : Tensor.contr (ColorEquiv.matMul b.dual a.dual b) (S.tmulEquiv _ _ (S.twoIndexEquiv _ _ (S.inverse g) ⊗ₜ S.twoIndexEquiv a b g)) = S.deltaTensor b := - (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 (comp_toLin_inverse g h) + (Tensor.contr_matMul_eq_deltaTensor_iff (S.inverse g) g).2 + (Module.Basis.comp_coordTensorHomEquiv_coordInverse h) /-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `g`. -/ lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : S.inverse g = N := - S.toLin.injective <| (LinearMap.cancel_right h.2).1 <| by rw [toLin_inverse_comp g h, hN] - -/-- The inverse of the inverse is the tensor itself, `(g⁻¹)⁻¹ = g`. -/ -lemma inverse_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) : - S.inverse (S.inverse g) = g := - inverse_eq_left_inv _ (toLin_inverse_bijective g h) (comp_toLin_inverse g h) + Module.Basis.coordInverse_eq_of_comp_eq_id h hN /-! @@ -139,10 +115,8 @@ lemma inverse_inverse (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin /-- The inverse of `Λg` is `Λ` applied to the inverse of `g`, `(Λg)⁻¹ = Λ(g⁻¹)`. -/ lemma inverse_map_rep (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) : S.inverse (TensorProduct.map (S.rep a Λ) (S.rep b Λ) g) = - TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := by - refine inverse_eq_left_inv _ (S.toLin_map_rep_bijective g h Λ) (LinearMap.ext fun x => ?_) - simp [toLin_map_rep, Representation.inv_self_apply, Representation.self_inv_apply, - ← LinearMap.comp_apply (S.toLin (S.inverse g)), toLin_inverse_comp g h] + TensorProduct.map (S.rep b.dual Λ) (S.rep a.dual Λ) (S.inverse g) := + Module.Basis.coordInverse_map_rep (S.invariant a) (S.invariant b) h Λ /-- The inverse of an invariant tensor is invariant, `Λg = g` implies `Λ(g⁻¹) = g⁻¹`. -/ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) (Λ : G) diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 3de67ab1ce..74a5f8f5a8 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -27,7 +27,8 @@ the matrix of `toLin z` and its equivariance, are stated there. This is how two-index tensors act as maps. A metric `g_μν` is the map `v^μ ↦ v^μ g_μν`, and the inverse metric `g^μν` is the tensor of the inverse map. The map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, so `z` is -invariant exactly when its linear map commutes with the group action. This is Mathlib's +invariant exactly when its linear map commutes with the group action +(`Module.Basis.coordTensorHomEquiv_map_rep`). This is Mathlib's `Representation.mem_linHom_invariants_iff_isIntertwining`, read through `toLin`. The delta `S.delta c = ∑ μ, e^μ ⊗ e_μ` is the tensor of the identity map, `u^μ δ_μ^ν = u^ν`. The @@ -42,9 +43,9 @@ identity. ## ii. Key results -- `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with `Species.toLin_map_rep` : - the linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`, and `Species.isIntertwiningMap_toLin_iff` : `z` is - invariant exactly when its linear map commutes with the group action. +- `Species.toLin` : `V a ⊗ U` as the linear maps `V a.dual → U`, with + `Species.isIntertwiningMap_toLin_iff` : `z` is invariant exactly when its linear map commutes + with the group action. - `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. - `Species.deltaTensor` : the delta as a tensor on two indices, with `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. @@ -157,19 +158,6 @@ lemma coordPairing_basis_dual (c : Color K) {x : S.V c.dual} {y : S.V c} : (S.basis c.dual).coordPairing (S.basis c) (x ⊗ₜ y) = S.contr c.dual (x ⊗ₜ y) := rfl -/-- The linear map of `Λz` is `Λ ∘ z ∘ Λ⁻¹`. -/ -lemma toLin_map_rep {a b : Color K} (z : S.V a ⊗[k] S.V b) (g : G) : - S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z) = - S.rep b g ∘ₗ S.toLin z ∘ₗ S.rep a.dual g⁻¹ := - Module.Basis.coordTensorHomEquiv_map_rep (S.invariant a) _ z g - -/-- The linear map of `Λz` is bijective when that of `z` is. -/ -lemma toLin_map_rep_bijective {a b : Color K} (z : S.V a ⊗[k] S.V b) - (h : Function.Bijective (S.toLin z)) (g : G) : - Function.Bijective (S.toLin (TensorProduct.map (S.rep a g) (S.rep b g) z)) := - Module.Basis.bijective_coordTensorHomEquiv_map_rep (S.invariant a) - ((S.rep b).apply_bijective g) h g - /-- A tensor is invariant exactly when its linear map commutes with the group action. -/ lemma isIntertwiningMap_toLin_iff {a b : Color K} (z : S.V a ⊗[k] S.V b) : (S.rep a.dual).IsIntertwiningMap (S.rep b) (S.toLin z) ↔ From 12c9495d858553fddee442ace1357f884c5744e4 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:06:51 +0000 Subject: [PATCH 158/242] refactor(ForMathlib): Module.Basis.coordToDual, repr_coordToDual; Species.toDual, basis_repr_toDual as their transports Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/CoordTensorHom.lean | 28 ++++++++++++++++++- Physlib/Mathematics/Tensors/Metric.lean | 13 ++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 4f1f385da0..3dbd1cccb1 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -27,7 +27,8 @@ Mathlib reads a tensor of `Module.Dual R N ⊗ U` as a linear map `N → U` The delta `∑ I, b I ⊗ b' I` of the two bases is the tensor of the identity map. The matrix of the linear map of `z` is the transpose of the matrix of components of `z`, so two tensors give inverse -maps when their matrices of components are inverse. +maps when their matrices of components are inverse. Contracting with the second factor of a tensor +`F'` of `N ⊗ N` with such an inverse is then an equivalence `M ≃ N`, as a metric lowers an index. A tensor `z` of `M ⊗ M` gives the bilinear form `(u, v) ↦ ⟨v, z u⟩` on `N`. Swapping the factors of `z` swaps its arguments, so the form is symmetric when `z` is and alternating when `z` is @@ -51,6 +52,8 @@ bijective the inverse is the junk value `0`, so every lemma about it assumes bij the matrix of components. - `Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one` : two tensors give inverse maps when their matrices of components are inverse. +- `Module.Basis.coordToDual` : the equivalence `M ≃ N` that contracts with the second factor of a + tensor `F'` with inverse `F`, and `Module.Basis.repr_coordToDual` : it in components. - `Module.Basis.isSymm_coordTensorHomEquiv_of_comm_eq` and `Module.Basis.isAlt_coordTensorHomEquiv_of_comm_eq_neg` : the bilinear form `(u, v) ↦ ⟨v, z u⟩` is symmetric when `comm z = z` and alternating when `comm z = -z`. @@ -180,6 +183,29 @@ lemma coordTensorHomEquiv_comm_comp_comm (b : Module.Basis ι R M) (b' : Module. LinearMap.toMatrix_id] at h convert h using 2 <;> ext <;> simp [tensorProduct_repr_comm] +/-- The equivalence `M ≃ N` that contracts a vector with the second factor of `F'`, when the linear +maps of `F` and `F'` are inverse. Its inverse contracts with the second factor of `F`. For a metric +`F' = g_μν` it is `v^μ ↦ g_νμ v^μ`. -/ +noncomputable def coordToDual (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (F : M ⊗[R] M) + (F' : N ⊗[R] N) + (h : b.coordTensorHomEquiv b' F ∘ₗ b'.coordTensorHomEquiv b F' = LinearMap.id) + (h' : b'.coordTensorHomEquiv b F' ∘ₗ b.coordTensorHomEquiv b' F = LinearMap.id) : + M ≃ₗ[R] N := + .ofLinearMap (b'.coordTensorHomEquiv b (TensorProduct.comm R N N F')) + (b.coordTensorHomEquiv b' (TensorProduct.comm R M M F)) + (coordTensorHomEquiv_comm_comp_comm b' b F' F h) (coordTensorHomEquiv_comm_comp_comm b b' F F' h') + +/-- `coordToDual` in components, `v_ν = F'_νμ v^μ`. -/ +lemma repr_coordToDual (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (F : M ⊗[R] M) + (F' : N ⊗[R] N) + (h : b.coordTensorHomEquiv b' F ∘ₗ b'.coordTensorHomEquiv b F' = LinearMap.id) + (h' : b'.coordTensorHomEquiv b F' ∘ₗ b.coordTensorHomEquiv b' F = LinearMap.id) (v : M) + (ν : ι) : + b'.repr (coordToDual b b' F F' h h' v) ν = + ∑ μ, b.repr v μ * (b'.tensorProduct b').repr F' (ν, μ) := by + change b'.repr (b'.coordTensorHomEquiv b (TensorProduct.comm R N N F') v) ν = _ + simp [repr_coordTensorHomEquiv, tensorProduct_repr_comm] + /-! ## B. The bilinear form of a tensor diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index acf2c1d0f0..59d92acbc1 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -29,7 +29,8 @@ has one, as the SU(3) triplet has none. The inverse is stored with the form. Sym `S.toDual c` contracts a vector with the second index of the form at `c.dual`, lowering an upper index, `v_ν = g_νμ v^μ`, and raising a lower one, `w^ν = g^νμ w_μ`, and `S.toDualAt i` does so at -the index `i` of a tensor. For the antisymmetric `ε`, where the order of the indices matters, +the index `i` of a tensor. `S.toDual c` is `Module.Basis.coordToDual` for the bases of `c` and +`c.dual`. For the antisymmetric `ε`, where the order of the indices matters, lowering gives `ψ_α = ε_αβ ψ^β`, the convention of Wess and Bagger and of Dreiner, Haber and Martin. @@ -222,10 +223,7 @@ variable (S) in /-- The vector `v` of color `c` contracted with the second index of the form at `c.dual`. It lowers an upper index, `v^μ ↦ g_νμ v^μ`, and raises a lower one, `w_μ ↦ g^νμ w_μ`. -/ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.rep c.dual) := - .mk (LinearEquiv.ofLinearMap (S.toLin (TensorProduct.comm k _ _ (form c.dual))) - (S.toLin (TensorProduct.comm k _ _ (form c))) - (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c)) - (Module.Basis.coordTensorHomEquiv_comm_comp_comm _ _ _ _ (form_inverse c.dual))) + .mk ((S.basis c).coordToDual (S.basis c.dual) _ _ (form_inverse c) (form_inverse c.dual)) fun g => LinearMap.ext <| ((S.isIntertwiningMap_toLin_iff (a := c.dual) (b := c.dual) _).2 fun g => by rw [TensorProduct.map_comm, form_invariant]).isIntertwining g @@ -243,9 +241,8 @@ lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : /-- Lowering or raising a vector in components, `v_ν = g_νμ v^μ`. -/ lemma basis_repr_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) (ν : basisIdx c.kind) : (S.basis c.dual).repr (S.toDual c v) ν = ∑ μ, (S.basis c).repr v μ * - S.formMatrix c.dual ν μ := by - rw [toDual_apply, Module.Basis.repr_coordTensorHomEquiv] - simp [formMatrix, Module.Basis.tensorProduct_repr_comm] + S.formMatrix c.dual ν μ := + Module.Basis.repr_coordToDual _ _ _ _ (form_inverse c) (form_inverse c.dual) v ν /-! From 8c368710b54f107dddd3a21cbc637da69e1011dd Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:08:53 +0000 Subject: [PATCH 159/242] refactor(ForMathlib): PiTensorProduct.contrPairs, contrPairs_tprod, IsIntertwiningMap.contrPairs; Tensor.contrStd as their transport Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 46 ++++++++++++++++++- .../ForMathlib/Representation.lean | 25 ++++++++-- Physlib/Mathematics/Tensors/Contraction.lean | 28 ++++------- 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 51bd38bda0..0e18e511ba 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -8,13 +8,15 @@ module public import Mathlib.LinearAlgebra.PiTensorProduct.Basis public import Mathlib.LinearAlgebra.Matrix.ToLin public import Mathlib.LinearAlgebra.Dimension.Free +public import Mathlib.LinearAlgebra.PiTensorProduct.Dual /-! -# Matrices of `PiTensorProduct.map` and dimension of `PiTensorProduct` +# Matrices of `PiTensorProduct.map`, dimension and contraction of `PiTensorProduct` The matrix of a family of linear maps in the product basis of a `PiTensorProduct`, the dimension -of a `PiTensorProduct`, and `PiTensorProduct.map` on an empty index type. +of a `PiTensorProduct`, `PiTensorProduct.map` on an empty index type, and the contraction of pairs +of factors. ## i. Overview @@ -28,18 +30,25 @@ The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`, With no indices, `⨂[R] i, M i` is `R` and `PiTensorProduct.map f` is the identity. +On the index type `κ ⊕ (J ⊕ J)`, a family of pairings `p j` of the factors at `inr (inl j)` and +`inr (inr j)` contracts `⨂[R] i, M i` to the tensor product of the factors at `inl x`, as a trace +`T^ρμ_μ` pairs an upper and a lower index. + ## ii. Key results - `PiTensorProduct.toMatrix_map_apply` : the matrix of `PiTensorProduct.map f` in the product bases is the product of the matrices of the `f i`. - `Module.finrank_piTensorProduct` : the dimension of `⨂[R] i, M i` is `∏ i, finrank R (M i)`. - `PiTensorProduct.map_eq_id_of_isEmpty` : with no indices, `PiTensorProduct.map f` is the identity. +- `PiTensorProduct.contrPairs` : the contraction of pairs of factors by a family of pairings, with + `PiTensorProduct.contrPairs_tprod` : its value on a pure tensor. ## iii. Table of contents - A. Matrices in the product basis - B. Dimension - C. Empty index types +- D. Contraction of pairs of factors ## iv. References @@ -100,3 +109,36 @@ lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoi PiTensorProduct.map f = LinearMap.id := by ext x simp [Subsingleton.elim (fun i => f i (x i)) x] + +/-! + +## D. Contraction of pairs of factors + +-/ + +section + +variable [CommSemiring R] {κ J : Type*} [Fintype J] {M : κ ⊕ (J ⊕ J) → Type*} + [∀ i, AddCommMonoid (M i)] [∀ i, Module R (M i)] + +/-- The contraction of `⨂ i, M i` on `κ ⊕ (J ⊕ J)` that pairs the factors at `inr (inl j)` and +`inr (inr j)` by `p j` and keeps the factors at `inl x`. -/ +noncomputable def PiTensorProduct.contrPairs + (p : Π j, M (.inr (.inl j)) ⊗[R] M (.inr (.inr j)) →ₗ[R] R) : + (⨂[R] i, M i) →ₗ[R] ⨂[R] x, M (.inl x) := + -- The factors of each pair are split into two tensor products, and the second is read as a + -- linear form on the first through the `p j` and Mathlib's `PiTensorProduct.dualDistrib`. + (TensorProduct.rid R _).toLinearMap ∘ₗ + (TensorProduct.lift (PiTensorProduct.dualDistrib ∘ₗ + PiTensorProduct.map fun j => (TensorProduct.curry (p j)).flip).flip ∘ₗ + (tmulEquivDep R fun i => M (.inr i)).symm.toLinearMap).lTensor _ ∘ₗ + (tmulEquivDep R M).symm.toLinearMap + +/-- On a pure tensor the contraction multiplies the kept factors by the pairings of each pair. -/ +lemma PiTensorProduct.contrPairs_tprod + (p : Π j, M (.inr (.inl j)) ⊗[R] M (.inr (.inr j)) →ₗ[R] R) (f : Π i, M i) : + contrPairs p (⨂ₜ[R] i, f i) = + (∏ j, p j (f (.inr (.inl j)) ⊗ₜ f (.inr (.inr j)))) • ⨂ₜ[R] x, f (.inl x) := by + simp [contrPairs, tmulEquivDep_symm_apply] + +end diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index d8a2dfadd7..80ea8deeea 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -6,7 +6,7 @@ Authors: Andrea Pari module public import Mathlib.RepresentationTheory.Intertwining -public import Mathlib.LinearAlgebra.PiTensorProduct.Basic +public import Physlib.Mathematics.ForMathlib.PiTensorProduct /-! @@ -30,6 +30,8 @@ given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the l A family of representations `ρ i` on `M i` gives the representation `piTprod ρ` on `⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g`, on Mathlib's `PiTensorProduct.map`. Maps `f i` that intertwine `ρ i` and `σ i` give `PiTensorProduct.map f`, which intertwines `piTprod ρ` and `piTprod σ`. +Contracting pairs of factors by invariant pairings, `PiTensorProduct.contrPairs`, is also +intertwining. ## ii. Key results @@ -39,8 +41,9 @@ A family of representations `ρ i` on `M i` gives the representation `piTprod ρ - `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. - `Representation.piTprod` : the tensor product of a family of representations, with `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is - intertwining, and `Representation.Equiv.piTensorCongr` : the tensor product of a family of - equivalences of representations. + intertwining, `Representation.Equiv.piTensorCongr` : the tensor product of a family of + equivalences of representations, and `Representation.IsIntertwiningMap.contrPairs` : the + contraction of invariantly paired factors is intertwining. - `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and `Representation.Equiv.map_smul_ofDistribMulAction` : maps of representations given by actions commute with the actions, `φ (g • v) = g • φ v`. @@ -155,6 +158,22 @@ lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M Representation.Equiv.piTensorCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ +/-- `PiTensorProduct.contrPairs p` intertwines `piTprod ρ` and `piTprod` of the kept factors when +every pairing `p j` is invariant. -/ +lemma Representation.IsIntertwiningMap.contrPairs {κ J : Type*} [Fintype J] + {M : κ ⊕ (J ⊕ J) → Type*} [∀ i, AddCommMonoid (M i)] [∀ i, Module k (M i)] + {ρ : Π i, Representation k G (M i)} + {p : Π j, M (.inr (.inl j)) ⊗[k] M (.inr (.inr j)) →ₗ[k] k} + (hp : ∀ j g x y, p j (ρ _ g x ⊗ₜ ρ _ g y) = p j (x ⊗ₜ y)) : + (Representation.piTprod ρ).IsIntertwiningMap (Representation.piTprod fun x => ρ (.inl x)) + (PiTensorProduct.contrPairs p) where + isIntertwining g v := by + induction v using PiTensorProduct.induction_on with + | smul_tprod r f => + simp only [map_smul, Representation.piTprod_tprod, PiTensorProduct.contrPairs_tprod, hp, + smul_comm r] + | add x y hx hy => simp only [map_add, hx, hy] + end /-! diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index b4927b78ac..3db1639681 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -7,7 +7,6 @@ module public import Physlib.Mathematics.ForMathlib.EquivPairs public import Physlib.Mathematics.Tensors.Product -public import Mathlib.LinearAlgebra.PiTensorProduct.Dual /-! # Contraction of tensors @@ -161,23 +160,16 @@ open Species /-- The contraction of a tensor in the standard arrangement `κ ⊕ (J ⊕ J)`. For each pair `j` the index `inr (inl j)` is contracted with the index `inr (inr j)` by `S.contr (a j)`, with the index `inr (inl j)` as its first argument. The indices `inl x` are the free indices, with the colors -`c`. -/ +`c`. It is `PiTensorProduct.contrPairs` of the contractions `S.contr (a j)`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : (S.tensorRep (contrColors c a)).IntertwiningMap (S.tensorRep c) := - -- The pairs are split into their first halves, of colors `a`, and their second halves, of the - -- dual colors. Each vector of a second half is read as a linear form on the first half through - -- `S.contr`, and the halves are paired by Mathlib's `PiTensorProduct.dualDistrib`. - intertwiningMapOfPure ((TensorProduct.rid k _).toLinearMap ∘ₗ - (lift ((PiTensorProduct.dualDistrib ∘ₗ - PiTensorProduct.map (fun j => (curry (S.contr (a j)).toLinearMap).flip) ∘ₗ - (S.toPiTensor _).toLinearMap).flip ∘ₗ (S.toPiTensor a).toLinearMap) ∘ₗ - (S.tmulEquiv a _).symm.toLinearMap).lTensor _ ∘ₗ - (S.tmulEquiv c _).symm.toLinearMap) fun g f => by - simp only [smul_pure, LinearMap.comp_apply, LinearEquiv.coe_coe, - Representation.Equiv.coe_toLinearMap, tmulEquiv_symm_pure, LinearMap.lTensor_tmul, - lift.tmul, LinearMap.flip_apply, toPiTensor_pure, PiTensorProduct.map_tprod, - PiTensorProduct.dualDistrib_apply, TensorProduct.rid_tmul, smul_comm g, smul_pure] - exact congrArg (· • _) (Finset.prod_congr rfl fun j _ => S.contr_invariant (a j) g _ _) + ⟨(S.toPiTensor c).symm.toLinearMap ∘ₗ + PiTensorProduct.contrPairs (M := fun i => S.V (contrColors c a i)) + (fun j => (S.contr (a j)).toLinearMap) ∘ₗ (S.toPiTensor _).toLinearMap, + fun g => LinearMap.ext fun t => + congrArg (S.toPiTensor c).symm ((Representation.IsIntertwiningMap.contrPairs + (ρ := fun i => S.rep (contrColors c a i)) fun j => S.contr_invariant (a j)).isIntertwining + g t)⟩ /-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the two vectors of each pair, as in `u ⊗ v ⊗ α ↦ (v^μ α_μ) u`. -/ @@ -185,8 +177,8 @@ lemma contrStd_pure [Fintype J] (c : κ → Color K) (a : J → Color K) (f : Π x, S.V (contrColors c a x)) : contrStd c a (S.pure f) = (∏ j, S.contr (a j) (f (.inr (.inl j)) ⊗ₜ f (.inr (.inr j)))) • - S.pure (c := c) fun x => f (.inl x) := by - simp [contrStd, PiTensorProduct.map_tprod, PiTensorProduct.dualDistrib_apply] + S.pure (c := c) fun x => f (.inl x) := + PiTensorProduct.contrPairs_tprod _ f /-! From b8b8a17aebf478d4b47f74e0d828ba0e5a8d5e0f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:10:01 +0000 Subject: [PATCH 160/242] style(ForMathlib): wrap the definition of Module.Basis.coordToDual Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/CoordTensorHom.lean | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 3dbd1cccb1..23f0e07192 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -193,7 +193,8 @@ noncomputable def coordToDual (b : Module.Basis ι R M) (b' : Module.Basis ι R M ≃ₗ[R] N := .ofLinearMap (b'.coordTensorHomEquiv b (TensorProduct.comm R N N F')) (b.coordTensorHomEquiv b' (TensorProduct.comm R M M F)) - (coordTensorHomEquiv_comm_comp_comm b' b F' F h) (coordTensorHomEquiv_comm_comp_comm b b' F F' h') + (coordTensorHomEquiv_comm_comp_comm b' b F' F h) + (coordTensorHomEquiv_comm_comp_comm b b' F F' h') /-- `coordToDual` in components, `v_ν = F'_νμ v^μ`. -/ lemma repr_coordToDual (b : Module.Basis ι R M) (b' : Module.Basis ι R N) (F : M ⊗[R] M) From e1374bbb49517768a8c7a71b89aae1958d283ac1 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:10:01 +0000 Subject: [PATCH 161/242] refactor(ForMathlib): Representation.map_piTprod_of_tprod, Species.map_smul_of_pure as its transport Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 12 ++++++++++++ Physlib/Mathematics/Tensors/Tensor.lean | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 80ea8deeea..fb25e8227c 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -44,6 +44,8 @@ intertwining. intertwining, `Representation.Equiv.piTensorCongr` : the tensor product of a family of equivalences of representations, and `Representation.IsIntertwiningMap.contrPairs` : the contraction of invariantly paired factors is intertwining. +- `Representation.map_piTprod_of_tprod` : a semilinear map that commutes with `piTprod ρ` on pure + tensors commutes with it on all tensors. - `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and `Representation.Equiv.map_smul_ofDistribMulAction` : maps of representations given by actions commute with the actions, `φ (g • v) = g • φ v`. @@ -158,6 +160,16 @@ lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M Representation.Equiv.piTensorCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ +/-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes +with it on all tensors. -/ +lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} + (f : (⨂[k] i, M i) →ₛₗ[φ] U) (g : G) + (h : ∀ m : Π i, M i, f (Representation.piTprod ρ g (⨂ₜ[k] i, m i)) = τ g (f (⨂ₜ[k] i, m i))) + (t : ⨂[k] i, M i) : f (Representation.piTprod ρ g t) = τ g (f t) := by + induction t using PiTensorProduct.induction_on with + | smul_tprod r m => rw [map_smul, map_smulₛₗ, h, map_smulₛₗ, map_smul] + | add x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] + /-- `PiTensorProduct.contrPairs p` intertwines `piTprod ρ` and `piTprod` of the kept factors when every pairing `p j` is invariant. -/ lemma Representation.IsIntertwiningMap.contrPairs {κ J : Type*} [Fintype J] diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index f67d94d91e..a3c30fb0ea 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -33,7 +33,7 @@ pure tensors, and their properties are proved there first. `S.pure f` is Mathlib A group element `Λ` acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation `Representation.piTprod` on `⨂[k] i, V (c i)`. A map that commutes with `Λ` on pure tensors -commutes with it on all tensors (`map_smul_of_pure`). +commutes with it on all tensors (`map_smul_of_pure`, from `Representation.map_piTprod_of_tprod`). The components `T^μ_νρ` of a tensor `T` are its coordinates in the basis `e_μ ⊗ e^ν ⊗ e^ρ` (`tensorBasis`), which is Mathlib's `Basis.piTensorProduct`. Each basis tensor is a pure tensor of @@ -206,11 +206,9 @@ with `Λ` on all tensors. -/ lemma map_smul_of_pure {ι' : Type} {c' : ι' → Color K} {σ : k →+* k} {F : Type} [FunLike F (Tensor S c) (Tensor S c')] [SemilinearMapClass F σ (Tensor S c) (Tensor S c')] (f : F) (g : G) (h : ∀ p : Π i, S.V (c i), f (g • S.pure p) = g • f (S.pure p)) - (t : Tensor S c) : f (g • t) = g • f t := by - induction t using inductionOn_pure with - | h p => exact h p - | hsmul r t ht => rw [smul_comm, map_smulₛₗ, ht, map_smulₛₗ, smul_comm] - | hadd x y hx hy => rw [smul_add, map_add, hx, hy, map_add, smul_add] + (t : Tensor S c) : f (g • t) = g • f t := + Representation.map_piTprod_of_tprod (τ := Representation.piTprod fun i => S.rep (c' i)) + (f : Tensor S c →ₛₗ[σ] Tensor S c') g h t variable {ι' : Type} {c' : ι' → Color K} in /-- A linear equivalence of tensors that commutes with `Λ` on pure tensors, as an equivalence of From e13520221be637da037c6580604617dfcba3884a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:11:21 +0000 Subject: [PATCH 162/242] =?UTF-8?q?refactor(ForMathlib):=20PiTensorProduct?= =?UTF-8?q?.congr=E2=82=9B=E2=82=97=5Fcongr=E2=82=9B=E2=82=97,=20Module.Ba?= =?UTF-8?q?sis.map=5Frep=5Fof=5Fstar=5FcoordPairing;=20Tensor.conj=5Fconj,?= =?UTF-8?q?=20Species.rep=5Fdual=5Fof=5Fstar=5Fcontr=20as=20their=20transp?= =?UTF-8?q?orts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/SemilinearMaps.lean | 48 +++++++++++++++++-- Physlib/Mathematics/Tensors/Conjugation.lean | 11 ++--- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index b826e1195a..f2334384e3 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -27,7 +27,9 @@ A basis `b` of `M`, a basis `b'` of `M'` and an equivalence `e` of their labels conjugate-linear equivalence `Module.Basis.starEquiv b b' e : M ≃ₗ⋆[R] M'`. It conjugates the coordinates of a vector in `b` and reads them in `b'`, the coordinate at `μ` moving to `e μ`. A conjugate-linear map that sends each `b μ` to `b' (e μ)` is this equivalence. Conjugating both -factors along the same labels conjugates the coordinate pairing of two bases. +factors along the same labels conjugates the coordinate pairing of two bases. When conjugate-linear +maps of the two factors conjugate one invariant coordinate pairing into another, the map of the +second factor commutes with the group action when the map of the first does. A semilinear equivalence keeps the finite rank, so a module and its conjugate have the same dimension. @@ -35,12 +37,15 @@ dimension. ## ii. Key results - `PiTensorProduct.mapₛₗ` applies a family of semilinear maps to a tensor product, and - `PiTensorProduct.congrₛₗ` a family of semilinear equivalences. + `PiTensorProduct.congrₛₗ` a family of semilinear equivalences, with + `PiTensorProduct.congrₛₗ_congrₛₗ` : maps that undo the `f i` undo `congrₛₗ f`. - `Module.Basis.starEquiv` conjugates the coordinates of `b` and reads them in `b'`, and `Module.Basis.eq_starEquiv` proves that it is the only conjugate-linear map sending `b μ` to `b' (e μ)`. - `Module.Basis.star_coordPairing_starEquiv` proves that conjugating both factors conjugates the - coordinate pairing. + coordinate pairing, and `Module.Basis.map_rep_of_star_coordPairing` that a conjugation of + invariant coordinate pairings commutes with the group action on the second factor when it does + on the first. - `LinearEquiv.finrank_eqₛₗ` : a semilinear equivalence keeps the finite rank. ## iii. Table of contents @@ -116,6 +121,18 @@ lemma congrₛₗ_tprod {σ σ' : R →+* R} [RingHomInvPair σ σ'] [RingHomInv congrₛₗ f (tprod R x) = tprod R fun i => f i (x i) := mapₛₗ_tprod _ x +/-- Applying `congrₛₗ f` and then `congrₛₗ g`, for maps `g i` that undo the `f i`, gives back the +tensor. -/ +lemma congrₛₗ_congrₛₗ {σ σ' : R →+* R} [RingHomInvPair σ σ'] [RingHomInvPair σ' σ] + (f : Π i, s i ≃ₛₗ[σ] t i) (g : Π i, t i ≃ₛₗ[σ'] s i) (h : ∀ i x, g i (f i x) = x) + (x : ⨂[R] i, s i) : congrₛₗ g (congrₛₗ f x) = x := by + induction x using PiTensorProduct.induction_on with + | smul_tprod r x => + rw [LinearEquiv.map_smulₛₗ, LinearEquiv.map_smulₛₗ, congrₛₗ_tprod, congrₛₗ_tprod, + RingHomInvPair.comp_apply_eq] + simp only [h] + | add x y hx hy => rw [map_add, map_add, hx, hy] + end PiTensorProduct /-! @@ -188,6 +205,31 @@ lemma star_coordPairing_starEquiv [Fintype ι] [DecidableEq ι] [Fintype ι'] [D simp only [star_sum, star_mul'] exact Fintype.sum_equiv e _ _ fun μ => by simp +/-- Let conjugate-linear maps `f` of `M` and `f'` of `N` conjugate the coordinate pairing of `b₁` and +`b₂` into that of `b₁'` and `b₂'`, both invariant. If `f` commutes with the group action, so does +`f'`. -/ +lemma map_rep_of_star_coordPairing [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] + {N N' G : Type*} [AddCommMonoid N] [Module R N] [AddCommMonoid N'] [Module R N'] [Group G] + {ρ₁ : Representation R G M} {ρ₂ : Representation R G N} {ρ₁' : Representation R G M'} + {ρ₂' : Representation R G N'} {b₁ : Basis ι R M} {b₂ : Basis ι R N} {b₁' : Basis ι' R M'} + {b₂' : Basis ι' R N'} + (h : ∀ g x y, b₁.coordPairing b₂ (ρ₁ g x ⊗ₜ ρ₂ g y) = b₁.coordPairing b₂ (x ⊗ₜ y)) + (h' : ∀ g x y, b₁'.coordPairing b₂' (ρ₁' g x ⊗ₜ ρ₂' g y) = b₁'.coordPairing b₂' (x ⊗ₜ y)) + (f : M ≃ₗ⋆[R] M') (f' : N ≃ₗ⋆[R] N') + (hf' : ∀ u α, star (b₁.coordPairing b₂ (u ⊗ₜ α)) = b₁'.coordPairing b₂' (f u ⊗ₜ f' α)) + (hf : ∀ g v, f (ρ₁ g v) = ρ₁' g (f v)) (g : G) (α : N) : + f' (ρ₂ g α) = ρ₂' g (f' α) := by + have hr (u : M) (α : N) : b₁.coordPairing b₂ (u ⊗ₜ ρ₂ g α) = + b₁.coordPairing b₂ (ρ₁ g⁻¹ u ⊗ₜ α) := by + simpa using h g (ρ₁ g⁻¹ u) α + have hr' (u : M') (α : N') : b₁'.coordPairing b₂' (u ⊗ₜ ρ₂' g α) = + b₁'.coordPairing b₂' (ρ₁' g⁻¹ u ⊗ₜ α) := by + simpa using h' g (ρ₁' g⁻¹ u) α + refine (isPerfPair_coordPairing b₁' b₂').bijective_right.1 (LinearMap.ext fun u' => ?_) + obtain ⟨u, rfl⟩ := f.surjective u' + change b₁'.coordPairing b₂' (f u ⊗ₜ f' (ρ₂ g α)) = b₁'.coordPairing b₂' (f u ⊗ₜ ρ₂' g (f' α)) + rw [← hf', hr, hf', hf, hr'] + end Module.Basis /-! diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 21e5936cb2..ea82f11fee 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -188,9 +188,7 @@ lemma rep_dual_of_star_contr (f : S.V c ≃ₗ⋆[k] S.V c') (f' : S.V c.dual (hf' : ∀ u α, star (S.contr c (u ⊗ₜ α)) = S.contr c' (f u ⊗ₜ f' α)) (hf : ∀ g v, f (S.rep c g v) = S.rep c' g (f v)) (g : G) (α : S.V c.dual) : f' (S.rep c.dual g α) = S.rep c'.dual g (f' α) := - ext_contr fun u' => by - obtain ⟨u, rfl⟩ := f.surjective u' - rw [← hf', contr_tmul_rep, contr_tmul_rep, ← hf, hf'] + Module.Basis.map_rep_of_star_coordPairing (S.invariant c) (S.invariant c') f f' hf' hf g α /-! @@ -272,11 +270,8 @@ lemma conj_pure (h : ∀ i, c' i = (c i).bar) (f : Π i, S.V (c i)) : /-- Conjugating twice gives back the tensor. -/ lemma conj_conj (h : ∀ i, c' i = (c i).bar) (h' : ∀ i, c i = (c' i).bar) (t : Tensor S c) : - conj h' (conj h t) = t := by - induction t using inductionOn_pure with - | h f => simp - | hsmul r t ht => rw [LinearEquiv.map_smulₛₗ, LinearEquiv.map_smulₛₗ, ht, starRingEnd_self_apply] - | hadd x y hx hy => rw [map_add, map_add, hx, hy] + conj h' (conj h t) = t := + PiTensorProduct.congrₛₗ_congrₛₗ _ _ (fun i => conjEquiv_conjEquiv (h i) (h' i)) t /-! From 992359b70eedb057fb747b2bfce085588bba565a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:16:22 +0000 Subject: [PATCH 163/242] style(ForMathlib): wrap the docstring of Module.Basis.map_rep_of_star_coordPairing Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/SemilinearMaps.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index f2334384e3..1341fc0fdd 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -205,9 +205,9 @@ lemma star_coordPairing_starEquiv [Fintype ι] [DecidableEq ι] [Fintype ι'] [D simp only [star_sum, star_mul'] exact Fintype.sum_equiv e _ _ fun μ => by simp -/-- Let conjugate-linear maps `f` of `M` and `f'` of `N` conjugate the coordinate pairing of `b₁` and -`b₂` into that of `b₁'` and `b₂'`, both invariant. If `f` commutes with the group action, so does -`f'`. -/ +/-- Let conjugate-linear maps `f` of `M` and `f'` of `N` conjugate the coordinate pairing of `b₁` +and `b₂` into that of `b₁'` and `b₂'`, both invariant. If `f` commutes with the group action, so +does `f'`. -/ lemma map_rep_of_star_coordPairing [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] {N N' G : Type*} [AddCommMonoid N] [Module R N] [AddCommMonoid N'] [Module R N'] [Group G] {ρ₁ : Representation R G M} {ρ₂ : Representation R G N} {ρ₁' : Representation R G M'} From 3b6b36e711c7ef8d0999ecae06cbda6019550c33 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:22:14 +0000 Subject: [PATCH 164/242] chore: remove transitive imports in CoordTensorHom, PiTensorProduct, LinearMap, Species and Tensor; drop unused Fintype in map_rep_of_star_coordPairing Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/CoordTensorHom.lean | 1 - Physlib/Mathematics/ForMathlib/PiTensorProduct.lean | 1 - Physlib/Mathematics/ForMathlib/SemilinearMaps.lean | 2 +- Physlib/Mathematics/Tensors/LinearMap.lean | 1 - Physlib/Mathematics/Tensors/Species.lean | 1 - Physlib/Mathematics/Tensors/Tensor.lean | 1 - 6 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 23f0e07192..3bf54fe716 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.Contraction public import Mathlib.LinearAlgebra.BilinearForm.Properties public import Mathlib.RepresentationTheory.Invariants public import Physlib.Mathematics.ForMathlib.Contraction diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 0e18e511ba..ed1eaf9ed7 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -5,7 +5,6 @@ Authors: Andrea Pari -/ module -public import Mathlib.LinearAlgebra.PiTensorProduct.Basis public import Mathlib.LinearAlgebra.Matrix.ToLin public import Mathlib.LinearAlgebra.Dimension.Free public import Mathlib.LinearAlgebra.PiTensorProduct.Dual diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index 1341fc0fdd..3591b2a918 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -208,7 +208,7 @@ lemma star_coordPairing_starEquiv [Fintype ι] [DecidableEq ι] [Fintype ι'] [D /-- Let conjugate-linear maps `f` of `M` and `f'` of `N` conjugate the coordinate pairing of `b₁` and `b₂` into that of `b₁'` and `b₂'`, both invariant. If `f` commutes with the group action, so does `f'`. -/ -lemma map_rep_of_star_coordPairing [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] +lemma map_rep_of_star_coordPairing [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] {N N' G : Type*} [AddCommMonoid N] [Module R N] [AddCommMonoid N'] [Module R N'] [Group G] {ρ₁ : Representation R G M} {ρ₂ : Representation R G N} {ρ₁' : Representation R G M'} {ρ₂' : Representation R G N'} {b₁ : Basis ι R M} {b₂ : Basis ι R N} {b₁' : Basis ι' R M'} diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 74a5f8f5a8..c1a32ab775 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Physlib.Mathematics.ForMathlib.CoordTensorHom -public import Mathlib.RepresentationTheory.Invariants public import Physlib.Mathematics.Tensors.Contraction /-! diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index bc69af6f6a..34c57806d0 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -8,7 +8,6 @@ module public import Mathlib.Analysis.RCLike.Basic public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color -public import Mathlib.RepresentationTheory.Intertwining /-! # Tensor species diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index a3c30fb0ea..978a81f804 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -6,7 +6,6 @@ Authors: Andrea Pari module public import Physlib.Mathematics.Tensors.Species -public import Physlib.Mathematics.ForMathlib.PiTensorProduct public import Physlib.Mathematics.ForMathlib.Representation /-! From a02cdf050c496829ff5ad3be2d65b007b66c0139 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:26:35 +0000 Subject: [PATCH 165/242] chore(Tensors): keep the Intertwining import of Species.lean as in #2049 Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 1 + 1 file changed, 1 insertion(+) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index 34c57806d0..bc69af6f6a 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -8,6 +8,7 @@ module public import Mathlib.Analysis.RCLike.Basic public import Physlib.Mathematics.ForMathlib.Contraction public import Physlib.Mathematics.Tensors.Color +public import Mathlib.RepresentationTheory.Intertwining /-! # Tensor species From 3bca110830cbee835c383a471bf19d26fa7aca9e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Wed, 11 Nov 2026 18:06:03 +0000 Subject: [PATCH 166/242] refactor(Tensors): Species.ext_contr next to the contraction, from isPerfPair_contr Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Species.lean | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Species.lean b/Physlib/Mathematics/Tensors/Species.lean index bc69af6f6a..f81557fba2 100644 --- a/Physlib/Mathematics/Tensors/Species.lean +++ b/Physlib/Mathematics/Tensors/Species.lean @@ -54,9 +54,9 @@ A repeated index is contracted, so `u^μ α_μ` is `S.contr c (u ⊗ α)`. - `Species.contr` : the contraction `u^μ α_μ`, with `Species.contr_tmul_eq_sum_basis` : its formula in components, `Species.contr_basis` : the bases are dual, `Species.contr_tmul_symm` : it is symmetric, and `Species.contr_invariant` : it is invariant. -- `Species.isPerfPair_contr` : the contraction is a perfect pairing, `Species.dualEquiv` : - `V c.dual` is the dual representation of `V c`, and `Species.ext_contr` : a vector of - `V c.dual` is determined by its contractions. +- `Species.isPerfPair_contr` : the contraction is a perfect pairing, `Species.ext_contr` : a + vector of `V c.dual` is determined by its contractions, and `Species.dualEquiv` : `V c.dual` is + the dual representation of `V c`. - `Species.cast` : a vector moved along an equality of colors, with `Species.cast_rep`, `Species.basis_repr_cast`, `Species.contr_cast_tmul_cast` and `Species.contr_cast_tmul_cast_symm` : this keeps the action, the components and the @@ -185,6 +185,14 @@ instance isPerfPair_contr (c : Color K) : (TensorProduct.curry (S.contr c).toLinearMap).IsPerfPair := Module.Basis.isPerfPair_coordPairing _ _ +variable {S} in +/-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the +same contraction against every vector of `c` are equal. -/ +lemma ext_contr {c : Color K} {α α' : S.V c.dual} + (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := + (LinearMap.IsPerfPair.bijective_right + (p := TensorProduct.curry (S.contr c).toLinearMap)).injective (LinearMap.ext h) + /-- The contraction is invariant, `(Λu)^μ (Λα)_μ = u^μ α_μ` for every `Λ : G`. -/ lemma contr_invariant (c : Color K) (g : G) (x : S.V c) (y : S.V c.dual) : S.contr c (S.rep c g x ⊗ₜ[k] S.rep c.dual g y) = S.contr c (x ⊗ₜ[k] y) := @@ -207,13 +215,6 @@ lemma dualEquiv_apply (c : Color K) (α : S.V c.dual) (u : S.V c) : S.dualEquiv c α u = S.contr c (u ⊗ₜ α) := rfl -variable {S} in -/-- The contraction is nondegenerate in its second argument. Two vectors of `c.dual` with the -same contraction against every vector of `c` are equal. -/ -lemma ext_contr {c : Color K} {α α' : S.V c.dual} - (h : ∀ u, S.contr c (u ⊗ₜ α) = S.contr c (u ⊗ₜ α')) : α = α' := - (S.dualEquiv c).toLinearEquiv.injective (LinearMap.ext h) - variable {S} /-! From 717b30788665e6debd338eaa5b9ea22329594d99 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:14:17 +0000 Subject: [PATCH 167/242] feat(ForMathlib): Representation.Equiv.piTensorReindex, permIndices as its specialisation Add Representation.Equiv.piTensorReindex and piTensorReindex_tprod on Mathlib's PiTensorProduct.reindex. Species.permIndices and permIndices_pure become one-line specialisations. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 20 ++++++++++++++++++- Physlib/Mathematics/Tensors/Reindexing.lean | 14 +++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index fb25e8227c..b68a3b72f2 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -30,6 +30,8 @@ given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the l A family of representations `ρ i` on `M i` gives the representation `piTprod ρ` on `⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g`, on Mathlib's `PiTensorProduct.map`. Maps `f i` that intertwine `ρ i` and `σ i` give `PiTensorProduct.map f`, which intertwines `piTprod ρ` and `piTprod σ`. +Reindexing the factors along `e : ι ≃ ι'`, Mathlib's `PiTensorProduct.reindex`, is an +equivalence of representations. Contracting pairs of factors by invariant pairings, `PiTensorProduct.contrPairs`, is also intertwining. @@ -42,7 +44,8 @@ intertwining. - `Representation.piTprod` : the tensor product of a family of representations, with `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is intertwining, `Representation.Equiv.piTensorCongr` : the tensor product of a family of - equivalences of representations, and `Representation.IsIntertwiningMap.contrPairs` : the + equivalences of representations, `Representation.Equiv.piTensorReindex` : reindexing the factors, + and `Representation.IsIntertwiningMap.contrPairs` : the contraction of invariantly paired factors is intertwining. - `Representation.map_piTprod_of_tprod` : a semilinear map that commutes with `piTprod ρ` on pure tensors commutes with it on all tensors. @@ -160,6 +163,21 @@ lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M Representation.Equiv.piTensorCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ +/-- Reindexing the factors of a tensor product of representations along `e : ι ≃ ι'`, +`⨂ i, v i ↦ ⨂ j, v (e.symm j)`. It is Mathlib's `PiTensorProduct.reindex` with the actions +`piTprod ρ` and `piTprod (ρ ∘ e.symm)`. -/ +noncomputable def Representation.Equiv.piTensorReindex {ι' : Type*} + (ρ : Π i, Representation k G (M i)) (e : ι ≃ ι') : + (Representation.piTprod ρ).Equiv (Representation.piTprod fun j => ρ (e.symm j)) := + .mk (PiTensorProduct.reindex k M e) fun g => PiTensorProduct.ext <| MultilinearMap.ext fun m => by + simp [PiTensorProduct.reindex_tprod] + +@[simp] +lemma Representation.Equiv.piTensorReindex_tprod {ι' : Type*} (ρ : Π i, Representation k G (M i)) + (e : ι ≃ ι') (m : Π i, M i) : + Representation.Equiv.piTensorReindex ρ e (⨂ₜ[k] i, m i) = ⨂ₜ[k] j, m (e.symm j) := + PiTensorProduct.reindex_tprod _ _ + /-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes with it on all tensors. -/ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 0ee028429a..b6159c4268 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -28,7 +28,7 @@ turn an up index into a down one is not a reindexing, since that needs a metric. - `ColorEquiv` is a bijection of indices that keeps their colors, with `refl`, `symm` and `trans`. - `Species.permIndices` moves the vectors of a tensor along a bijection of indices, with no colors - involved. It is Mathlib's `PiTensorProduct.reindex`. + involved. It is `Representation.Equiv.piTensorReindex`, on Mathlib's `PiTensorProduct.reindex`. - `Species.reindex` applies a reindexing to tensors, `permIndices` followed by `congrIndices` of the moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. - `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that reindexing @@ -130,20 +130,16 @@ variable {ι₁ ι₂ ι₃ : Type} variable (S) in /-- The bijection `e` of indices applied to tensors, moving the vector of the old index `i` to the -new index `e i`, with the colors `c ∘ e.symm`. It is Mathlib's `PiTensorProduct.reindex`. -/ +new index `e i`, with the colors `c ∘ e.symm`. It is `Representation.Equiv.piTensorReindex`. -/ noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := - equivOfPure (S.toPiTensor c ≪≫ₗ PiTensorProduct.reindex k (fun i => S.V (c i)) e ≪≫ₗ - (S.toPiTensor (c ∘ e.symm)).symm) fun g f => by - simp only [smul_pure, LinearEquiv.trans_apply, toPiTensor_pure, - PiTensorProduct.reindex_tprod, toPiTensor_symm_tprod] - rfl + Representation.Equiv.piTensorReindex (fun i => S.rep (c i)) e /-- On a pure tensor, the new index `j` holds the vector of the old index `e.symm j`. -/ @[simp] lemma permIndices_pure (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) (f : Π i, S.V (c i)) : - S.permIndices c e (S.pure f) = S.pure fun j => f (e.symm j) := by - simp [permIndices, PiTensorProduct.reindex_tprod] + S.permIndices c e (S.pure f) = S.pure fun j => f (e.symm j) := + PiTensorProduct.reindex_tprod _ _ variable (S) in /-- The reindexing `σ` applied to tensors, as in the transpose `T^μ_ν ↦ T_ν^μ`. The new index `j` From 28cef1f5146a3594a71978157d24537b06c85b3a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:18:17 +0000 Subject: [PATCH 168/242] feat(ForMathlib): Representation.Equiv.piTprodSum and piTprodOfSubsingleton Add Representation.Equiv.piTprodSum (on PiTensorProduct.tmulEquivDep), piTprodSum_tmul, and Representation.Equiv.piTprodOfSubsingleton (on PiTensorProduct.subsingletonEquiv). Species.tmulEquiv and Species.oneIndexEquiv become one-line specialisations, and tmulEquiv_pure, tmulEquiv_symm_pure become term proofs. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 48 +++++++++++++++++-- Physlib/Mathematics/Tensors/Product.lean | 36 +++++--------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index b68a3b72f2..a25b883da3 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -30,8 +30,10 @@ given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the l A family of representations `ρ i` on `M i` gives the representation `piTprod ρ` on `⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g`, on Mathlib's `PiTensorProduct.map`. Maps `f i` that intertwine `ρ i` and `σ i` give `PiTensorProduct.map f`, which intertwines `piTprod ρ` and `piTprod σ`. -Reindexing the factors along `e : ι ≃ ι'`, Mathlib's `PiTensorProduct.reindex`, is an -equivalence of representations. +Three of Mathlib's linear equivalences of `⨂` are equivalences of representations: +`PiTensorProduct.reindex` along `e : ι ≃ ι'`, `PiTensorProduct.tmulEquivDep`, which splits a +family over `ι₁ ⊕ ι₂` into its two halves, and `PiTensorProduct.subsingletonEquiv`, for a family +over a subsingleton index type. Contracting pairs of factors by invariant pairings, `PiTensorProduct.contrPairs`, is also intertwining. @@ -45,8 +47,10 @@ intertwining. `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is intertwining, `Representation.Equiv.piTensorCongr` : the tensor product of a family of equivalences of representations, `Representation.Equiv.piTensorReindex` : reindexing the factors, - and `Representation.IsIntertwiningMap.contrPairs` : the - contraction of invariantly paired factors is intertwining. + `Representation.Equiv.piTprodSum` : the product of the two halves of a family over `ι₁ ⊕ ι₂`, + `Representation.Equiv.piTprodOfSubsingleton` : a family over a subsingleton index type, and + `Representation.IsIntertwiningMap.contrPairs` : the contraction of invariantly paired factors is + intertwining. - `Representation.map_piTprod_of_tprod` : a semilinear map that commutes with `piTprod ρ` on pure tensors commutes with it on all tensors. - `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and @@ -178,6 +182,42 @@ lemma Representation.Equiv.piTensorReindex_tprod {ι' : Type*} (ρ : Π i, Repre Representation.Equiv.piTensorReindex ρ e (⨂ₜ[k] i, m i) = ⨂ₜ[k] j, m (e.symm j) := PiTensorProduct.reindex_tprod _ _ +/-- The tensor product of the two halves of a family of representations indexed by `ι₁ ⊕ ι₂`, +`(⨂ i, v i) ⊗ (⨂ j, w j) ↦ ⨂ s, Sum.rec v w s`. It is Mathlib's `PiTensorProduct.tmulEquivDep` +with the actions. -/ +noncomputable def Representation.Equiv.piTprodSum {ι₁ ι₂ : Type*} {M : ι₁ ⊕ ι₂ → Type*} + [∀ i, AddCommMonoid (M i)] [∀ i, Module k (M i)] (ρ : Π i, Representation k G (M i)) : + ((Representation.piTprod fun i => ρ (.inl i)).tprod + (Representation.piTprod fun j => ρ (.inr j))).Equiv (Representation.piTprod ρ) := + .mk (PiTensorProduct.tmulEquivDep k M) fun g => by + ext f₁ f₂ + simp only [Representation.tprod_apply, LinearMap.compMultilinearMap_apply, + TensorProduct.AlgebraTensorModule.curry_apply, LinearMap.restrictScalars_self, + TensorProduct.curry_apply, LinearMap.coe_comp, LinearEquiv.coe_coe, Function.comp_apply, + TensorProduct.map_tmul, Representation.piTprod_tprod, PiTensorProduct.tmulEquivDep_apply] + exact congrArg (PiTensorProduct.tprod k) (funext fun s => by rcases s with _ | _ <;> rfl) + +@[simp] +lemma Representation.Equiv.piTprodSum_tmul {ι₁ ι₂ : Type*} {M : ι₁ ⊕ ι₂ → Type*} + [∀ i, AddCommMonoid (M i)] [∀ i, Module k (M i)] (ρ : Π i, Representation k G (M i)) + (f₁ : Π i, M (.inl i)) (f₂ : Π j, M (.inr j)) : + Representation.Equiv.piTprodSum ρ ((⨂ₜ[k] i, f₁ i) ⊗ₜ (⨂ₜ[k] j, f₂ j)) = + ⨂ₜ[k] s, Sum.rec f₁ f₂ s := + PiTensorProduct.tmulEquivDep_apply k M f₁ f₂ + +/-- A representation `ρ i₀` as the tensor product of a family `ρ` over a subsingleton index type, +`v ↦ ⨂ i, v`. It is Mathlib's `PiTensorProduct.subsingletonEquiv` with the actions. -/ +noncomputable def Representation.Equiv.piTprodOfSubsingleton [Subsingleton ι] + (ρ : Π i, Representation k G (M i)) (i₀ : ι) : + (ρ i₀).Equiv (Representation.piTprod ρ) := + .mk (PiTensorProduct.subsingletonEquiv i₀).symm fun g => LinearMap.ext fun v => by + simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, + PiTensorProduct.subsingletonEquiv_symm_apply, Representation.piTprod_tprod] + congr 1 + ext i + obtain rfl := Subsingleton.elim i i₀ + simp + /-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes with it on all tensors. -/ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 63f277e2da..57f76491fd 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -17,8 +17,8 @@ The product `T ⊗ U` of two tensors has the indices of `T` followed by those of Let `T = T^μ_ν e_μ ⊗ e^ν` have colors `c₁` on the indices `Fin 2` and `U = U^ρ e_ρ` have colors `c₂` on the index `Fin 1`. Their product `T ⊗ U = T^μ_ν U^ρ e_μ ⊗ e^ν ⊗ e_ρ` has the indices `Fin 2 ⊕ Fin 1` with colors `prodColors c₁ c₂`, those of `T` on `inl i` and those of `U` on -`inr j`, so no index is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is Mathlib's -`PiTensorProduct.tmulEquivDep` transported along `toPiTensor`. +`inr j`, so no index is renumbered. `S.tmulEquiv c₁ c₂` sends `T ⊗ₜ U` to `T ⊗ U`. It is +`Representation.Equiv.piTprodSum`, on Mathlib's `PiTensorProduct.tmulEquivDep`. The product is associative and symmetric up to a reindexing, and the group acts on it factor by factor. An element of `V a ⊗ V b`, such as the metric `η_μν e^μ ⊗ e^ν`, is a tensor on @@ -113,19 +113,11 @@ variable {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} variable (S) in /-- The product of tensors, `T ⊗ₜ U ↦ T ⊗ U` with `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. The indices of -`T` become the indices `inl i` and those of `U` the indices `inr j`. It is Mathlib's -`PiTensorProduct.tmulEquivDep` transported along `toPiTensor`. -/ +`T` become the indices `inl i` and those of `U` the indices `inr j`. It is +`Representation.Equiv.piTprodSum`. -/ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) : ((S.tensorRep c₁).tprod (S.tensorRep c₂)).Equiv (S.tensorRep (prodColors c₁ c₂)) := - .mk (TensorProduct.congr (S.toPiTensor c₁) (S.toPiTensor c₂) ≪≫ₗ - tmulEquivDep k (fun s => S.V (prodColors c₁ c₂ s)) ≪≫ₗ (S.toPiTensor _).symm) fun g => by - ext f₁ f₂ - simp only [TensorProduct.AlgebraTensorModule.curry_apply, TensorProduct.curry_apply, - LinearMap.coe_restrictScalars, LinearMap.comp_apply, Representation.tprod_apply, - TensorProduct.map_tmul, Representation.ofDistribMulAction_apply_apply, smul_pure, - LinearEquiv.coe_coe, LinearEquiv.trans_apply, TensorProduct.congr_tmul, toPiTensor_pure, - tmulEquivDep_apply, toPiTensor_symm_tprod] - exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl) + Representation.Equiv.piTprodSum fun s => S.rep (prodColors c₁ c₂ s) /-- The product of the pure tensors of the vectors `f₁` and `f₂` is the pure tensor with the vectors `f₁` at the indices `inl i` and the vectors `f₂` at the indices `inr j`. -/ @@ -133,8 +125,8 @@ noncomputable def tmulEquiv (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ : Π i, S.V (c₁ i)) (f₂ : Π j, S.V (c₂ j)) : S.tmulEquiv c₁ c₂ (S.pure f₁ ⊗ₜ S.pure f₂) = - S.pure fun s => Sum.rec (motive := fun s => S.V (prodColors c₁ c₂ s)) f₁ f₂ s := by - simp [tmulEquiv, tmulEquivDep_apply] + S.pure fun s => Sum.rec (motive := fun s => S.V (prodColors c₁ c₂ s)) f₁ f₂ s := + Representation.Equiv.piTprodSum_tmul (fun s => S.rep (prodColors c₁ c₂ s)) f₁ f₂ /-- The inverse of the product splits a pure tensor into the pure tensor of its vectors at the indices `inl i` and the pure tensor of its vectors at the indices `inr j`. -/ @@ -142,8 +134,8 @@ indices `inl i` and the pure tensor of its vectors at the indices `inr j`. -/ lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f : Π s, S.V (prodColors c₁ c₂ s)) : (S.tmulEquiv c₁ c₂).symm (S.pure f) = - S.pure (c := c₁) (fun i => f (.inl i)) ⊗ₜ S.pure (c := c₂) fun j => f (.inr j) := by - simp [tmulEquiv, tmulEquivDep_symm_apply] + S.pure (c := c₁) (fun i => f (.inl i)) ⊗ₜ S.pure (c := c₂) fun j => f (.inr j) := + tmulEquivDep_symm_apply _ _ f /-! @@ -250,14 +242,10 @@ tensor product `V a ⊗ V b`. `twoIndexEquiv` identifies this space with the ten -/ variable (S) in -/-- A vector `v` of `V c` as a tensor with the one index `![c]`, by Mathlib's -`PiTensorProduct.subsingletonEquiv`. -/ +/-- A vector `v` of `V c` as a tensor with the one index `![c]`. It is +`Representation.Equiv.piTprodOfSubsingleton`. -/ noncomputable def oneIndexEquiv (c : Color K) : (S.rep c).Equiv (S.tensorRep ![c]) := - .mk ((subsingletonEquiv (s := fun i => S.V (![c] i)) 0).symm ≪≫ₗ (S.toPiTensor _).symm) - fun g => LinearMap.ext fun v => by - change S.pure _ = g • S.pure _ - rw [smul_pure] - exact congrArg S.pure (funext fun i => by fin_cases i; rfl) + Representation.Equiv.piTprodOfSubsingleton (fun i => S.rep (![c] i)) 0 /-- The tensor of `v` is the pure tensor with `v` on its one index. -/ @[simp] From 021c9da7b5c3e2e61cdc43a0dcff399cecf36cad Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:19:26 +0000 Subject: [PATCH 169/242] feat(ForMathlib): LinearMap.IsSymm.of_comp_star and IsPosDef.of_comp_star Transport symmetry and positive-definiteness of a sesquilinear form along a surjective or bijective linear map with B' (A x) (A y) = star (B y x). Species.isSymm_sesqForm_inverse and Species.isPosDef_sesqForm_inverse become one-line corollaries. Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/SesquilinearForm.lean | 44 +++++++++++++++++-- .../Mathematics/Tensors/HermitianForm.lean | 18 +++----- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index 5c18d593be..5904ee30ca 100644 --- a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean +++ b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean @@ -21,13 +21,18 @@ positive-definite matrices (`Matrix.PosDef`), but no positive-definite sesquilin `LinearMap.IsPosDef` fills this gap with the shape of `LinearMap.IsPosSemidef`: a structure extending `LinearMap.IsSymm`. For a form `M →ₗ⋆[R] M →ₗ[R] R` symmetry is Hermiticity, `star (B x y) = B y x`. A positive-definite form is positive-semidefinite and nondegenerate, and -in a basis it is positive-definite exactly when its matrix is. +in a basis it is positive-definite exactly when its matrix is. Symmetry and positive-definiteness +transfer along a surjective or bijective linear map that turns one form into the conjugate of the +other, as for the inverse of a Hermitian metric. ## ii. Key results - `LinearMap.IsPosDef` : a sesquilinear form is symmetric and `0 < B x x` for `x ≠ 0`. - `LinearMap.IsPosDef.isPosSemidef` : a positive-definite form is positive-semidefinite. - `LinearMap.IsPosDef.nondegenerate` : a positive-definite form is nondegenerate. +- `LinearMap.IsSymm.of_comp_star` and `LinearMap.IsPosDef.of_comp_star` : a form `B'` with + `B' (A x) (A y) = star (B y x)` for a surjective or bijective `A` is symmetric or + positive-definite when `B` is. - `LinearMap.isPosDef_iff_posDef_toMatrix` : a form is positive-definite exactly when its matrix in a basis is. @@ -36,7 +41,8 @@ in a basis it is positive-definite exactly when its matrix is. - A. Positive-definite sesquilinear forms - A.1. The definition - A.2. Consequences over a preorder - - A.3. The matrix of a positive-definite form + - A.3. Transport along a linear map + - A.4. The matrix of a positive-definite form ## iv. References @@ -97,7 +103,39 @@ end IsPosDef /-! -### A.3. The matrix of a positive-definite form +### A.3. Transport along a linear map + +-/ + +section comp + +variable {R M N : Type*} [CommSemiring R] [StarRing R] [AddCommMonoid M] [Module R M] + [AddCommMonoid N] [Module R N] {B : M →ₗ⋆[R] M →ₗ[R] R} {B' : N →ₗ⋆[R] N →ₗ[R] R} + {A : M →ₗ[R] N} + +/-- If `B' (A x) (A y) = star (B y x)` for a surjective `A`, then `B'` is symmetric when `B` is. -/ +lemma IsSymm.of_comp_star (hA : Function.Surjective A) (hB : B.IsSymm) + (h : ∀ x y, B' (A x) (A y) = star (B y x)) : B'.IsSymm := by + refine ⟨fun u w => ?_⟩ + obtain ⟨x, rfl⟩ := hA u + obtain ⟨y, rfl⟩ := hA w + rw [h, h, starRingEnd_apply, star_star] + exact (hB.eq x y).symm + +/-- If `B' (A x) (A y) = star (B y x)` for a bijective `A`, then `B'` is positive-definite when `B` +is. -/ +lemma IsPosDef.of_comp_star [LT R] (hA : Function.Bijective A) (hB : B.IsPosDef) + (h : ∀ x y, B' (A x) (A y) = star (B y x)) : B'.IsPosDef := by + refine ⟨hB.isSymm.of_comp_star hA.2 h, fun u hu => ?_⟩ + obtain ⟨x, rfl⟩ := hA.2 u + rw [h, ← starRingEnd_apply, hB.isSymm.eq x x] + exact hB.pos x fun hx => hu (by rw [hx, map_zero]) + +end comp + +/-! + +### A.4. The matrix of a positive-definite form -/ diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index a2f82e1b73..3ae3169712 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -26,7 +26,8 @@ is again Hermitian or positive-definite, and a positive-definite `g_{IJ̄}` is i - `Species.sesqForm` reads a tensor `g_{IJ̄}` as the sesquilinear form `x, y ↦ g_{IJ̄} y^I x̄^J̄`. - `Species.isSymm_sesqForm_inverse` and `Species.isPosDef_sesqForm_inverse` prove that the inverse - of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite. + of an invertible Hermitian or positive-definite `g_{IJ̄}` is Hermitian or positive-definite, from + `LinearMap.IsSymm.of_comp_star` and `LinearMap.IsPosDef.of_comp_star`. - `Species.toLin_injective_of_isPosDef` proves that the linear map of a positive-definite `g_{IJ̄}` is injective, and `Species.toLin_bijective_of_isPosDef` that it is bijective. @@ -90,12 +91,8 @@ lemma sesqForm_inverse_toLin (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S /-- The inverse of a Hermitian `g_{IJ̄}` is Hermitian. -/ lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (h : Function.Bijective (S.toLin g)) (hg : (S.sesqForm g hb).IsSymm) : - (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsSymm := by - refine ⟨fun u w => ?_⟩ - obtain ⟨x, rfl⟩ := h.2 u - obtain ⟨y, rfl⟩ := h.2 w - rw [sesqForm_inverse_toLin g h hb, sesqForm_inverse_toLin g h hb, starRingEnd_apply, star_star] - exact (hg.eq x y).symm + (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsSymm := + hg.of_comp_star h.2 (sesqForm_inverse_toLin g h hb) /-! @@ -124,12 +121,9 @@ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} /-- The inverse of a positive-definite `g_{IJ̄}` is positive-definite. -/ lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (hg : (S.sesqForm g hb).IsPosDef) : - (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsPosDef := by + (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsPosDef := have h := toLin_bijective_of_isPosDef g hg - refine ⟨isSymm_sesqForm_inverse g hb h hg.isSymm, fun u hu => ?_⟩ - obtain ⟨x, rfl⟩ := h.2 u - rw [sesqForm_inverse_toLin g h hb, ← starRingEnd_apply, hg.isSymm.eq x x] - exact hg.pos x fun hx => hu (by rw [hx, map_zero]) + hg.of_comp_star h (sesqForm_inverse_toLin g h hb) end Species From e6b9bf6c971a31979ebb7350fe68744d3678bf54 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:19:38 +0000 Subject: [PATCH 170/242] refactor(Tensors): toLin_injective_of_isPosDef via IsPosDef.nondegenerate Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/HermitianForm.lean | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 3ae3169712..a18591eaea 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -100,14 +100,12 @@ lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) -/ -/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since `g y = 0` gives -`g(y, y) = 0`. -/ +/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since the form is +nondegenerate. -/ lemma toLin_injective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] - intro y hy - by_contra hne - simpa [sesqForm_apply, hy] using hg.pos y hne + exact fun y hy => hg.nondegenerate.2 y fun x => by simp [sesqForm_apply, hy] /-- A positive-definite `g_{IJ̄}` is invertible. Its linear map `y ↦ g y` is bijective. -/ lemma toLin_bijective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} From 278e509d5c493c6f15eaf401cf1fb5dc6a262877 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:22:41 +0000 Subject: [PATCH 171/242] refactor(Tensors): remove Species.toPiTensor, toPiTensor_pure, toPiTensor_symm_tprod MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tensor S c unfolds to ⨂[k] i, S.V (c i), so pure, tensorBasis, mapIndices, contrStd and conj are the Mathlib terms directly. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 3 +- Physlib/Mathematics/Tensors/Contraction.lean | 11 +++----- Physlib/Mathematics/Tensors/LinearMap.lean | 4 +-- Physlib/Mathematics/Tensors/Tensor.lean | 29 ++++++-------------- 4 files changed, 15 insertions(+), 32 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index ea82f11fee..aba64b40f2 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -259,8 +259,7 @@ variable [S.HasConjugation] {ι : Type} {c c' : ι → Color K} color. The colors `c'` of the result are the conjugates of `c` by `h`. For concrete colors `h` holds by `rfl` index by index, `fun i => by fin_cases i <;> rfl`. It is conjugate-linear. -/ noncomputable def conj (h : ∀ i, c' i = (c i).bar) : Tensor S c ≃ₗ⋆[k] Tensor S c' := - (S.toPiTensor c).trans <| (PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i)).trans - (S.toPiTensor c').symm + PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i) /-- The conjugate of a pure tensor conjugates each factor, `(u ⊗ α)‾ = ū ⊗ ᾱ`. -/ @[simp] diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 3db1639681..ec60021b06 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -163,13 +163,10 @@ index `inr (inl j)` is contracted with the index `inr (inr j)` by `S.contr (a j) `c`. It is `PiTensorProduct.contrPairs` of the contractions `S.contr (a j)`. -/ noncomputable def contrStd [Fintype J] (c : κ → Color K) (a : J → Color K) : (S.tensorRep (contrColors c a)).IntertwiningMap (S.tensorRep c) := - ⟨(S.toPiTensor c).symm.toLinearMap ∘ₗ - PiTensorProduct.contrPairs (M := fun i => S.V (contrColors c a i)) - (fun j => (S.contr (a j)).toLinearMap) ∘ₗ (S.toPiTensor _).toLinearMap, - fun g => LinearMap.ext fun t => - congrArg (S.toPiTensor c).symm ((Representation.IsIntertwiningMap.contrPairs - (ρ := fun i => S.rep (contrColors c a i)) fun j => S.contr_invariant (a j)).isIntertwining - g t)⟩ + ⟨PiTensorProduct.contrPairs (M := fun i => S.V (contrColors c a i)) + (fun j => (S.contr (a j)).toLinearMap), + fun g => LinearMap.ext <| (Representation.IsIntertwiningMap.contrPairs + (ρ := fun i => S.rep (contrColors c a i)) fun j => S.contr_invariant (a j)).isIntertwining g⟩ /-- On a pure tensor the contraction multiplies the remaining vectors by the contraction of the two vectors of each pair, as in `u ⊗ v ⊗ α ↦ (v^μ α_μ) u`. -/ diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index c1a32ab775..0a0ecb2281 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -265,10 +265,10 @@ lemma contr_matMulAt_pure [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι induction F using TensorProduct.inductionOn with | add F F' hF hF' => simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] - rw [Species.pure, Species.pure, Species.pure, MultilinearMap.map_update_add, map_add] + exact ((PiTensorProduct.tprod k).map_update_add q i _ _).symm | tmul x y => rw [Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, map_smul] - conv_rhs => rw [Species.pure, MultilinearMap.map_update_smul, map_smul, ← Species.pure] + conv_rhs => rw [Species.pure, MultilinearMap.map_update_smul, ← Species.pure] rw [twoIndexEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique] congr 2 funext z diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 978a81f804..d76ef8d3ff 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -112,7 +112,7 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → /-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not an `abbrev`, so that the type `Tensor S c` keeps the species and the colors instead of unfolding to -`⨂[k] i, S.V (c i)`. -/ +`⨂[k] i, S.V (c i)`. Mathlib's constructions on `⨂` apply to it by definitional unfolding. -/ def _root_.Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := ⨂[k] i, S.V (c i) @@ -126,26 +126,13 @@ noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Colo variable {ι : Type} {c : ι → Color K} -/-- The identity from `Tensor S c` to Mathlib's `⨂[k] i, S.V (c i)`, as a linear equivalence. -Mathlib's constructions on `⨂` are composed with it. -/ -noncomputable def toPiTensor (S : Species k G K basisIdx) (c : ι → Color K) : - Tensor S c ≃ₗ[k] ⨂[k] i, S.V (c i) := - LinearEquiv.refl k _ - /-- The pure tensor of a family of vectors, one per index, such as `u ⊗ α ⊗ β` with components `u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : Tensor S c := - (S.toPiTensor c).symm (⨂ₜ[k] i, f i) + ⨂ₜ[k] i, f i variable {S : Species k G K basisIdx} -@[simp] -lemma toPiTensor_pure (f : Π i, S.V (c i)) : S.toPiTensor c (S.pure f) = ⨂ₜ[k] i, f i := rfl - -@[simp] -lemma toPiTensor_symm_tprod (f : Π i, S.V (c i)) : - (S.toPiTensor c).symm (⨂ₜ[k] i, f i) = S.pure f := rfl - /-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ lemma inductionOn_pure {P : Tensor S c → Prop} (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) @@ -258,18 +245,18 @@ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = colors. -/ noncomputable def tensorBasis (S : Species k G K basisIdx) [Fintype ι] (c : ι → Color K) : Module.Basis (Π i, basisIdx (c i).kind) k (Tensor S c) := - (Basis.piTensorProduct fun i => S.basis (c i)).map (S.toPiTensor c).symm + Basis.piTensorProduct fun i => S.basis (c i) /-- A basis tensor is the pure tensor of basis vectors, `e_μ ⊗ e^ν ⊗ …`. -/ lemma tensorBasis_apply [Fintype ι] (x : Π i, basisIdx (c i).kind) : - S.tensorBasis c x = S.pure fun i => S.basis (c i) (x i) := by - simp [tensorBasis, Basis.piTensorProduct_apply] + S.tensorBasis c x = S.pure fun i => S.basis (c i) (x i) := + Basis.piTensorProduct_apply _ _ /-- The components of a pure tensor are products, `(u ⊗ α)^μ_ν = u^μ α_ν`. -/ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) (x : Π i, basisIdx (c i).kind) : - (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := by - simp [tensorBasis, Basis.piTensorProduct_repr_tprod_apply] + (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := + Basis.piTensorProduct_repr_tprod_apply _ _ _ /-- Tensors on finitely many indices are a finite-dimensional space. -/ noncomputable instance [Fintype ι] : Module.Finite k (Tensor S c) := @@ -305,7 +292,7 @@ variable (S) in /-- A linear map `f x` at every index `x`, as in `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`. -/ noncomputable def mapIndices (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) : Tensor S c →ₗ[k] Tensor S c' := - (S.toPiTensor c').symm.toLinearMap ∘ₗ PiTensorProduct.map f ∘ₗ (S.toPiTensor c).toLinearMap + PiTensorProduct.map f /-- On a pure tensor, `mapIndices` applies `f x` to the vector at each index `x`. -/ lemma mapIndices_pure (f : Π x, S.V (c x) →ₗ[k] S.V (c' x)) (p : Π x, S.V (c x)) : From 39e9f7e3d30162870d2017c4c8a514df5c36f3ce Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:23:58 +0000 Subject: [PATCH 172/242] refactor(Tensors): Module.Finite from PiTensorProduct.finite, ext_pure and equiv_ext_pure at Type* MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Module.Finite instance on Tensor S c needs only [Finite ι] and is Mathlib's PiTensorProduct.finite. ext_pure and equiv_ext_pure take targets in any universe with AddCommMonoid. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index d76ef8d3ff..1667bbf49c 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -5,6 +5,7 @@ Authors: Andrea Pari -/ module +public import Mathlib.LinearAlgebra.PiTensorProduct.Finite public import Physlib.Mathematics.Tensors.Species public import Physlib.Mathematics.ForMathlib.Representation /-! @@ -141,7 +142,7 @@ lemma inductionOn_pure {P : Tensor S c → Prop} /-- Two linear maps out of tensors are equal when they agree on pure tensors. -/ @[ext] -lemma ext_pure {E : Type} [AddCommGroup E] [Module k E] {f g : Tensor S c →ₗ[k] E} +lemma ext_pure {E : Type*} [AddCommMonoid E] [Module k E] {f g : Tensor S c →ₗ[k] E} (h : ∀ p : Π i, S.V (c i), f (S.pure p) = g (S.pure p)) : f = g := PiTensorProduct.ext (MultilinearMap.ext h) @@ -225,7 +226,7 @@ lemma intertwiningMapOfPure_apply (f : Tensor S c →ₗ[k] Tensor S c') /-- Two equivalences of representations out of tensors are equal when they agree on pure tensors. -/ -lemma equiv_ext_pure {W : Type} [AddCommGroup W] [Module k W] {ρ : Representation k G W} +lemma equiv_ext_pure {W : Type*} [AddCommMonoid W] [Module k W] {ρ : Representation k G W} {φ ψ : (S.tensorRep c).Equiv ρ} (h : ∀ p : Π i, S.V (c i), φ (S.pure p) = ψ (S.pure p)) : φ = ψ := Representation.Equiv.ext <| @@ -258,9 +259,10 @@ lemma tensorBasis_repr_pure [Fintype ι] (f : Π i, S.V (c i)) (S.tensorBasis c).repr (S.pure f) x = ∏ i, (S.basis (c i)).repr (f i) (x i) := Basis.piTensorProduct_repr_tprod_apply _ _ _ -/-- Tensors on finitely many indices are a finite-dimensional space. -/ -noncomputable instance [Fintype ι] : Module.Finite k (Tensor S c) := - .of_basis (S.tensorBasis c) +/-- Tensors on finitely many indices are a finite-dimensional space, from Mathlib's +`PiTensorProduct.finite`. -/ +instance [Finite ι] : Module.Finite k (Tensor S c) := + inferInstanceAs (Module.Finite k (⨂[k] i, S.V (c i))) /-- The dimension of tensors is the product of the dimensions of their indices. -/ lemma finrank_tensor [Fintype ι] (c : ι → Color K) : From f15547440da0a3280add267e807f7c0ff41a64e2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:25:10 +0000 Subject: [PATCH 173/242] refactor(Tensors): delete unused Species.intertwiningMapOfPure, intertwiningMapOfPure_apply Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 1667bbf49c..2d1152303e 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -61,8 +61,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t tensors commutes with it on all tensors. - `Species.tensorRep` is the action on tensors as a Mathlib representation. Maps of tensors that commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` - of it, built by `Species.equivOfPure` and `Species.intertwiningMapOfPure` from a check on pure - tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. + of it. `Species.equivOfPure` builds an equivalence from a check on pure tensors, and + `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. - `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant, from `PiTensorProduct.map_eq_id_of_isEmpty`. - `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the @@ -211,19 +211,6 @@ lemma equivOfPure_apply (e : Tensor S c ≃ₗ[k] Tensor S c') (h : ∀ (g : G) (p : Π i, S.V (c i)), e (g • S.pure p) = g • e (S.pure p)) (t : Tensor S c) : equivOfPure e h t = e t := rfl -variable {ι' : Type} {c' : ι' → Color K} in -/-- A linear map of tensors that commutes with `Λ` on pure tensors, as an intertwining map. -/ -noncomputable def intertwiningMapOfPure (f : Tensor S c →ₗ[k] Tensor S c') - (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) : - (S.tensorRep c).IntertwiningMap (S.tensorRep c') := - ⟨f, fun g => LinearMap.ext fun t => map_smul_of_pure f g (h g) t⟩ - -variable {ι' : Type} {c' : ι' → Color K} in -@[simp] -lemma intertwiningMapOfPure_apply (f : Tensor S c →ₗ[k] Tensor S c') - (h : ∀ (g : G) (p : Π i, S.V (c i)), f (g • S.pure p) = g • f (S.pure p)) (t : Tensor S c) : - intertwiningMapOfPure f h t = f t := rfl - /-- Two equivalences of representations out of tensors are equal when they agree on pure tensors. -/ lemma equiv_ext_pure {W : Type*} [AddCommMonoid W] [Module k W] {ρ : Representation k G W} From ec3381e5b91532669683a06c5a2d05184f51bd8c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:25:58 +0000 Subject: [PATCH 174/242] refactor(Tensors): Tensorial and its lemmas at M N : Type* Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensorial.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 08323ea30c..4097647516 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -71,7 +71,7 @@ the group, `rep`, and `toTensor` identifies `M` with `Tensor S c` compatibly wit class Tensorial {k : outParam Type} [RCLike k] {G : outParam Type} [Group G] {K : outParam Type} {basisIdx : outParam (K → Type)} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : outParam (Species k G K basisIdx)) {ι : outParam Type} - (c : outParam (ι → Color K)) (M : Type) [AddCommMonoid M] [Module k M] where + (c : outParam (ι → Color K)) (M : Type*) [AddCommMonoid M] [Module k M] where /-- The action of the group on `M`, such as `Λ v = Λ *ᵥ v` on Lorentz vectors. -/ rep : Representation k G M /-- The elements of `M` as tensors, compatibly with the action. -/ @@ -81,7 +81,7 @@ namespace Tensorial variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] {S : Species k G K basisIdx} - {ι₁ ι₂ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {M N : Type} [AddCommMonoid M] + {ι₁ ι₂ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {M N : Type*} [AddCommMonoid M] [Module k M] [AddCommMonoid N] [Module k N] /-- Transforming `m` and then reading it as a tensor is transforming the tensor, From 2d07cecbf07a5288e09595e491afe9ff483230dc Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:26:42 +0000 Subject: [PATCH 175/242] refactor(Tensors): delete unused Tensorial.ofEquiv, toTensor_ofEquiv Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensorial.lean | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 4097647516..8d5ba903ea 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -25,8 +25,8 @@ an instance to `M ⊗ N` acting on the left factor only. The species and colors `M`, so an instance belongs on a type that always means one physical object, such as `Lorentz.Vector d`. -Instances come from the space of one index (`ofIndexSpace`), from an equivalent tensorial type -(`ofEquiv`), and from tensor products (`prod`), as for a field strength in `Vector ⊗ Vector`. +Instances come from the space of one index (`ofIndexSpace`) and from tensor products (`prod`), as +for a field strength in `Vector ⊗ Vector`. ## ii. Key results @@ -34,8 +34,6 @@ Instances come from the space of one index (`ofIndexSpace`), from an equivalent - `Tensorial.toTensor_rep` : `toTensor (Λ m) = Λ • toTensor m`. - `Tensorial.ofIndexSpace` : the space of one index is tensorial, as the tensors with that index, and `Tensorial.tensorBasis_repr_toTensor_ofIndexSpace` gives its components. -- `Tensorial.ofEquiv` : a type equivalent to a tensorial type, compatibly with the actions, is - tensorial with the same colors. - `Tensorial.prod` : the tensor product of two tensorial types is tensorial, with `Tensorial.rep_tmul` : `Λ (x ⊗ y) = Λ x ⊗ Λ y` and `Tensorial.toTensor_tmul` : the tensor of `x ⊗ y` is the product of the tensors. @@ -106,17 +104,6 @@ lemma tensorBasis_repr_toTensor_ofIndexSpace (c : Color K) (v : S.V c) (S.basis c).repr v (μ 0) := Species.tensorBasis_repr_oneIndexEquiv c v μ -/-- A type `M` with an action `r` and an equivalence `φ` to a tensorial type `N` commuting with -the actions is tensorial with the colors of `N`. -/ -noncomputable abbrev ofEquiv [Tensorial S c₁ N] {r : Representation k G M} - (φ : r.Equiv (rep (M := N))) : Tensorial S c₁ M where - rep := r - toTensor := φ.trans (toTensor (M := N)) - -/-- The tensor of `m` is the tensor of `φ m`. -/ -lemma toTensor_ofEquiv [Tensorial S c₁ N] {r : Representation k G M} - (φ : r.Equiv (rep (M := N))) (m : M) : toTensor (self := ofEquiv φ) m = toTensor (φ m) := rfl - /-! ## B. Tensor products of tensorial types From 7fd6b550a85ef6223ccd91f6ff200eb11b677881 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:28:27 +0000 Subject: [PATCH 176/242] refactor(Tensors): ColorEquiv.matMul as matMulAt 1, contr_matMul from contr_matMulAt Add Species.mapAt_one_twoIndexEquiv: a linear map at the index 1 of a tensor on two indices acts on the second factor. ColorEquiv.matMul is matMulAt at the index 1 of Fin 2, and contr_matMul is contr_matMulAt followed by mapAt_one_twoIndexEquiv. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/LinearMap.lean | 76 +++++++++------------- Physlib/Mathematics/Tensors/Product.lean | 19 +++++- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 0a0ecb2281..1f9c39d418 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -48,7 +48,7 @@ identity. - `Species.delta` : the delta `δ_μ^ν`, with `Species.delta_invariant` : it is invariant. - `Species.deltaTensor` : the delta as a tensor on two indices, with `Species.tensorBasis_repr_deltaTensor_eq_one` : its components form the identity matrix. -- `Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, and +- `Tensor.contr_matMul` : the contraction `M^μ_ν N^ν_ρ`, the case `i = 1` of `contr_matMulAt`, and `Tensor.contr_matMul_eq_deltaTensor_iff` : it is the delta exactly when the linear maps of `M` and `N` are inverse. - `Tensor.contr_matMulAt` : contracting the index `i` with the first index of `F` applies @@ -84,21 +84,8 @@ namespace ColorEquiv variable {K ι₁ : Type} -/-- The reindexing of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the -second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are -`0` and `1`. -/ -def matMul (a c b : Color K) : - ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) where - toEquiv.toFun - | .inl 0 => .inl 0 | .inl 1 => .inr (.inl ()) | .inr 0 => .inr (.inr ()) | .inr 1 => .inl 1 - toEquiv.invFun - | .inl 0 => .inl 0 | .inl 1 => .inr 1 | .inr (.inl ()) => .inl 1 | .inr (.inr ()) => .inr 0 - toEquiv.left_inv := by decide - toEquiv.right_inv := by decide - /-- The reindexing of `T^…μ… F_μ^ν`, which contracts the index `i` of `T` with the first index of a -tensor `F` on two indices and puts the second index of `F` in the place of `i`. It is -`matMul` at any index of a tensor on any index type. -/ +tensor `F` on two indices and puts the second index of `F` in the place of `i`. -/ def matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (a b : Color K) (ha : c i = a) (hi : c' i = b) (h : ∀ x, x ≠ i → c x = c' x) : ColorEquiv (prodColors c ![a.dual, b]) (contrColors c' fun _ : Unit => a) where @@ -125,6 +112,13 @@ def matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) · simp [ha] · simp +/-- The reindexing of the product `M^μ_ν N^ν_ρ` of two tensors on two indices that contracts the +second index of `M`, of color `c`, with the first index of `N`. The free indices `μ` and `ρ` are +`0` and `1`. It is `matMulAt` at the index `1`. -/ +def matMul (a c b : Color K) : + ColorEquiv (prodColors ![a, c] ![c.dual, b]) (contrColors ![a, b] fun _ : Unit => c) := + matMulAt (c := ![a, c]) (c' := ![a, b]) 1 c b rfl rfl fun x hx => by fin_cases x <;> simp_all + end ColorEquiv variable {k : Type} [RCLike k] {G : Type} [Group G] {K : Type} {basisIdx : K → Type} @@ -221,37 +215,6 @@ variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} -/ -/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two indices is `M` with its second factor mapped -by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ -lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : - contr (ColorEquiv.matMul a c b) - (S.tmulEquiv _ _ (S.twoIndexEquiv a c M ⊗ₜ S.twoIndexEquiv _ b N)) = - S.twoIndexEquiv a b ((S.toLin N).lTensor _ M) := by - induction M using TensorProduct.inductionOn with - | add M M' hM hM' => simp only [map_add, add_tmul, hM, hM'] - | tmul m₁ m₂ => - induction N using TensorProduct.inductionOn with - | add N N' hN hN' => - simp only [map_add, tmul_add, LinearMap.lTensor_add, LinearMap.add_apply, hN, hN'] - | tmul n₁ n₂ => - simp only [twoIndexEquiv_tmul, tmulEquiv_pure, contr_pure, Fintype.prod_unique, - LinearMap.lTensor_tmul, Module.Basis.coordTensorHomEquiv_tmul, coordPairing_basis, tmul_smul, - map_smul] - congr 2 - funext j - fin_cases j <;> rfl - -/-- Two tensors on two indices contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear -maps compose to the identity, `Y ∘ X = id`. -/ -lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) - (Y : S.V y.dual ⊗[k] S.V x.dual) : - contr (ColorEquiv.matMul x y x.dual) - (S.tmulEquiv _ _ (S.twoIndexEquiv x y X ⊗ₜ S.twoIndexEquiv _ _ Y)) = S.deltaTensor x.dual ↔ - S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by - rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoIndexEquiv _ _)).eq_iff, - ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, - Module.Basis.coordTensorHomEquiv_coordDelta] - /-- On a pure tensor, contracting the index `i` with the first index of `F` applies `F`, read as a linear map by `Species.toLin`, to the vector at `i`. The vectors `q` at the other indices are those of `p`, moved along `h`. -/ @@ -298,6 +261,27 @@ lemma contr_matMulAt [DecidableEq ι₁] {c c' : ι₁ → Color K} (i : ι₁) (mapAt_pure i _ h p (Function.update q i (S.cast hi.symm (S.toLin F (S.cast ha (p i))))) (Function.update_self _ _ _) (fun x hx => by simp [q, hx])).symm +/-- The contraction `M^μ_ν N^ν_ρ` of two tensors on two indices is `M` with its second factor mapped +by `S.toLin N`, the linear map `v_ν ↦ v_ν N^ν_ρ`. -/ +lemma contr_matMul (a c b : Color K) (M : S.V a ⊗[k] S.V c) (N : S.V c.dual ⊗[k] S.V b) : + contr (ColorEquiv.matMul a c b) + (S.tmulEquiv _ _ (S.twoIndexEquiv a c M ⊗ₜ S.twoIndexEquiv _ b N)) = + S.twoIndexEquiv a b ((S.toLin N).lTensor _ M) := + (contr_matMulAt (S := S) (c := ![a, c]) (c' := ![a, b]) 1 c b rfl rfl + (fun x hx => by fin_cases x <;> simp_all) N (S.twoIndexEquiv a c M)).trans + (mapAt_one_twoIndexEquiv a c b (S.toLin N) M _) + +/-- Two tensors on two indices contract to the delta, `X_μν Y^νρ = δ_μ^ρ`, exactly when their linear +maps compose to the identity, `Y ∘ X = id`. -/ +lemma contr_matMul_eq_deltaTensor_iff {x y : Color K} (X : S.V x ⊗[k] S.V y) + (Y : S.V y.dual ⊗[k] S.V x.dual) : + contr (ColorEquiv.matMul x y x.dual) + (S.tmulEquiv _ _ (S.twoIndexEquiv x y X ⊗ₜ S.twoIndexEquiv _ _ Y)) = S.deltaTensor x.dual ↔ + S.toLin Y ∘ₗ S.toLin X = LinearMap.id := by + rw [deltaTensor, contr_matMul, (EquivLike.injective (S.twoIndexEquiv _ _)).eq_iff, + ← (S.toLin (a := x)).injective.eq_iff, Module.Basis.coordTensorHomEquiv_lTensor, + Module.Basis.coordTensorHomEquiv_coordDelta] + /-- Contracting the index `i` with the first index of the delta is the identity, `T^…μ… δ_μ^ν = T^…ν…`. -/ lemma contr_deltaTensor [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι₁) (a : Color K) (ha : c i = a) diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index 57f76491fd..f71baa39d0 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -31,7 +31,9 @@ two indices by `twoIndexEquiv`. The components of a product are the products of - `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. -- `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two indices. +- `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two indices, and + `Species.mapAt_one_twoIndexEquiv` proves that a linear map at its index `1` acts on the second + factor. - `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. ## iii. Table of contents @@ -288,6 +290,21 @@ lemma reindex_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : simp only [twoIndexEquiv_tmul, reindex_pure, TensorProduct.comm_tmul] exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) +/-- A linear map `f` at the index `1` of the tensor of `M` in `V a ⊗ V c` is `f` on the second +factor of `M`, `M^μν ↦ M^μρ f_ρ^ν`. -/ +lemma mapAt_one_twoIndexEquiv (a c b : Color K) (f : S.V c →ₗ[k] S.V b) (M : S.V a ⊗[k] S.V c) + (h : ∀ x, x ≠ (1 : Fin 2) → (![a, c] : Fin 2 → Color K) x = (![a, b] : Fin 2 → Color K) x) : + S.mapAt (c := ![a, c]) (c' := ![a, b]) 1 f h (S.twoIndexEquiv a c M) = + S.twoIndexEquiv a b (f.lTensor _ M) := by + induction M using TensorProduct.inductionOn with + | add M M' hM hM' => simp only [map_add, hM, hM'] + | tmul m₁ m₂ => + rw [twoIndexEquiv_tmul, LinearMap.lTensor_tmul, twoIndexEquiv_tmul] + refine mapAt_pure (c := ![a, c]) (c' := ![a, b]) 1 f h _ _ rfl fun x hx => ?_ + fin_cases x + · rfl + · exact absurd rfl hx + /-! ## E. Components From 2e39e377030d629c23a2869dc99911920a908ebe Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 10:29:07 +0000 Subject: [PATCH 177/242] refactor(Tensors): delete Species.rep_dual_of_star_contr Use Module.Basis.map_rep_of_star_coordPairing directly at its one use in HasConjugation.isEquivariantConjugation_ofBasis. Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Conjugation.lean | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index aba64b40f2..4c8f2293e1 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -36,8 +36,7 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde - `Species.HasConjugation` gives the conjugation of a species with its two conditions, and `Species.conjEquiv` is the conjugate of a vector, with its properties `Species.conjEquiv_conjEquiv`, `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. -- `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action, and - `Species.rep_dual_of_star_contr` that equivariance at a color gives it at the dual color. +- `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action. - `Species.HasConjugation.ofBasis` builds a componentwise conjugation from `ConjLabels`, and `Species.HasConjugation.isEquivariantConjugation_ofBasis` gives its equivariance from the matrices of the group. @@ -180,16 +179,6 @@ lemma conjEquiv_cast {b b' : Color K} (p : b = b') (h : c' = b'.bar) (x : S.V b) end -omit [InvolutiveStar K] in -/-- If conjugate-linear maps `f` at `c` and `f'` at `c.dual` preserve the contraction and `f` -commutes with the group action, then so does `f'`. Equivariance at a dual color follows from -equivariance at the color. -/ -lemma rep_dual_of_star_contr (f : S.V c ≃ₗ⋆[k] S.V c') (f' : S.V c.dual ≃ₗ⋆[k] S.V c'.dual) - (hf' : ∀ u α, star (S.contr c (u ⊗ₜ α)) = S.contr c' (f u ⊗ₜ f' α)) - (hf : ∀ g v, f (S.rep c g v) = S.rep c' g (f v)) (g : G) (α : S.V c.dual) : - f' (S.rep c.dual g α) = S.rep c'.dual g (f' α) := - Module.Basis.map_rep_of_star_coordPairing (S.invariant c) (S.invariant c') f f' hf' hf g α - /-! ## C. Componentwise conjugation @@ -208,7 +197,8 @@ noncomputable def HasConjugation.ofBasis (e : ConjLabels basisIdx) : /-- The componentwise conjugation commutes with the group action when the matrix of each `g : G` at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The hypothesis is needed at -the upper colors only, since it follows at the lower ones (`rep_dual_of_star_contr`). -/ +the upper colors only, since it follows at the lower ones +(`Module.Basis.map_rep_of_star_coordPairing`). -/ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) (rep_basis : ∀ (κ : K) (g : G) (μ ν : basisIdx κ), (S.basis ⟨star κ, .up⟩).repr @@ -221,7 +211,8 @@ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) subst h obtain ⟨κ, ⟨_ | _, _ | _, hv⟩⟩ := c <;> try exact absurd rfl hv · -- A lower color is the dual of `⟨κ, up⟩`, where `rep_basis` gives equivariance. - exact rep_dual_of_star_contr (c := ⟨κ, .up⟩) (c' := ⟨star κ, .up⟩) _ _ + exact Module.Basis.map_rep_of_star_coordPairing (S.invariant ⟨κ, .up⟩) + (S.invariant ⟨star κ, .up⟩) _ _ (fun u α => Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α) (fun g => Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g)) g v · exact Module.Basis.starEquiv_map_of_repr _ _ _ _ _ (rep_basis κ g) v From 69eb3d6c9c757a187a2fc8dd094e4381d5217672 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:21:20 +0000 Subject: [PATCH 178/242] refactor(Tensors): Species.tensorRep as Representation.piTprod, scoped Species.distribMulAction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The action of the group on tensors is the representation S.tensorRep c. The instance for Λ • T is scoped to Tensor, with high priority, since Mathlib gives Circle and ℂˣ a scalar action on every ℂ-module. Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Tensor.lean | 58 ++++++++++++++++--------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 2d1152303e..89f1ae9dbd 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -31,17 +31,19 @@ pure tensors, and their properties are proved there first. `S.pure f` is Mathlib `⨂ₜ[k] i, f i` as an element of `Tensor S c`. A group element `Λ` acts on every index by the representation of its color, -`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation -`Representation.piTprod` on `⨂[k] i, V (c i)`. A map that commutes with `Λ` on pure tensors -commutes with it on all tensors (`map_smul_of_pure`, from `Representation.map_piTprod_of_tprod`). +`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation `S.tensorRep c`, +`Representation.piTprod` on `⨂[k] i, V (c i)`, written `Λ • T` under `open scoped Tensor`. A map +that commutes with `Λ` on pure tensors commutes with it on all tensors (`map_smul_of_pure`, from +`Representation.map_piTprod_of_tprod`). The components `T^μ_νρ` of a tensor `T` are its coordinates in the basis `e_μ ⊗ e^ν ⊗ e^ρ` (`tensorBasis`), which is Mathlib's `Basis.piTensorProduct`. Each basis tensor is a pure tensor of basis vectors. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, down⟩, ⟨vec, down⟩]` give the space -`ℝ⁴ ⊗ (ℝ⁴)* ⊗ (ℝ⁴)*` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. A Lorentz transformation `Λ` acts -as `(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ` (`tensorBasis_repr_smul`). Lower indices +`Vector 3 ⊗ CoVector 3 ⊗ CoVector 3` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. A Lorentz +transformation `Λ` acts as +`(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ` (`tensorBasis_repr_smul`). Lower indices transform with `Λ⁻¹`, which keeps `u^μ α_μ` invariant. A linear equivalence `f` from the space of one color to that of another acts on tensors through a @@ -59,7 +61,8 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t - `Species.smul_pure` proves that the group acts on a pure tensor index by index. - `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure tensors commutes with it on all tensors. -- `Species.tensorRep` is the action on tensors as a Mathlib representation. Maps of tensors that +- `Species.tensorRep` is the action on tensors as a Mathlib representation, and + `Species.distribMulAction` is the scoped instance for `Λ • T`. Maps of tensors that commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` of it. `Species.equivOfPure` builds an equivalence from a check on pure tensors, and `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. @@ -152,29 +155,44 @@ lemma ext_pure {E : Type*} [AddCommMonoid E] [Module k E] {f g : Tensor S c → -/ -/-- The group acts on every index by the representation of its color, +variable (S) in +/-- The action of the group on tensors, on every index by the representation of its color, `Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. It is `Representation.piTprod` of the representations of the -colors. -/ -noncomputable instance : DistribMulAction G (Tensor S c) := - DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ - (Representation.piTprod fun i => S.rep (c i)) +colors. Maps of tensors that commute with the action are Mathlib's `Representation.Equiv` and +`Representation.IntertwiningMap` for it. -/ +noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor S c) := + Representation.piTprod fun i => S.rep (c i) + +/-- The action `Λ • T` of the group on tensors, `S.tensorRep c Λ T`. The instance is scoped to +`Tensor`, and has high priority there, since Mathlib gives some groups, such as `Circle` and `ℂˣ`, +a scalar action on every `ℂ`-module. -/ +noncomputable abbrev distribMulAction : DistribMulAction G (Tensor S c) := + DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ (S.tensorRep c) + +scoped[Tensor] attribute [instance 1100] Tensor.Species.distribMulAction + +/-- The scalar multiplication of `distribMulAction`, found before any scalar action of Mathlib. -/ +noncomputable abbrev smul : SMul G (Tensor S c) := distribMulAction.toSMul + +scoped[Tensor] attribute [instance 1100] Tensor.Species.smul @[simp] lemma smul_pure (g : G) (f : Π i, S.V (c i)) : g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := Representation.piTprod_tprod _ _ _ -noncomputable instance : SMulCommClass G k (Tensor S c) where - smul_comm g r t := map_smul (Representation.piTprod (fun i => S.rep (c i)) g) r t +/-- The action of the group commutes with the scalars. -/ +lemma smulCommClass : SMulCommClass G k (Tensor S c) where + smul_comm g r t := map_smul (S.tensorRep c g) r t -noncomputable instance : SMulCommClass k G (Tensor S c) := .symm _ _ _ +scoped[Tensor] attribute [instance] Tensor.Species.smulCommClass -variable (S) in -/-- The action of the group on tensors as Mathlib's representation, `Λ ↦ (T ↦ Λ • T)`. Maps of -tensors that commute with the action are Mathlib's `Representation.Equiv` and -`Representation.IntertwiningMap` for it. -/ -noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor S c) := - Representation.ofDistribMulAction k G (Tensor S c) +/-- The scalars commute with the action of the group. -/ +lemma smulCommClass' : SMulCommClass k G (Tensor S c) := + haveI := smulCommClass (S := S) (c := c) + .symm _ _ _ + +scoped[Tensor] attribute [instance] Tensor.Species.smulCommClass' /-- An intertwining map from or to tensors commutes with the actions, `φ (Λ v) = Λ φ v`. -/ lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} From 7cb316e1d4c24bf901a5007335b644b536e51306 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:23:30 +0000 Subject: [PATCH 179/242] docs(Tensors): Reindexing and Contraction module docs without index notation Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Contraction.lean | 12 +++++------- Physlib/Mathematics/Tensors/Reindexing.lean | 10 ++++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index ec60021b06..969f469ced 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -23,9 +23,8 @@ A contraction pairs indices of dual colors and sums over their common label, as The pairs to contract are chosen by a reindexing `σ : ColorEquiv c (contrColors c' a)` of the indices. It sends each free index to an index `inl x`, of color `c' x`, and the two indices of a pair `j` to the indices `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the -trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`, and the index notation -`{T | ρ μ μ}ᵀ` builds it from the repeated name. Since `σ` keeps colors, the two indices of each -pair are dual without a separate hypothesis. `Tensor.contr σ` reindexes by `σ` and then contracts +trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`. Since `σ` keeps colors, +the two indices of each pair are dual without a separate hypothesis. `Tensor.contr σ` reindexes by `σ` and then contracts each pair by `S.contr (a j)`, with the first index of the pair as its first argument. Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the @@ -271,8 +270,8 @@ lemma contr_eq_of_samePair [Fintype J] [Fintype J'] {a : J → Color K} {a' : J' -/ /-- A contraction of a tensor that changes sign under a reindexing `π` is zero, when `π` only -permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` has the form of the -index notation `{T | μ ν = -T | ν μ}ᵀ`. -/ +permutes the pairs along `e` and keeps the free indices. The hypothesis `ht` says `T^μν = -T^νμ`, +for `π` the swap. -/ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} (σ : ColorEquiv c (contrColors c' a)) (π : ColorEquiv c c) (e : J ≃ J) (hπ : π.toEquiv.trans σ.toEquiv = σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) @@ -291,8 +290,7 @@ lemma contr_eq_zero_of_reindex_eq_neg [Fintype J] {a : J → Color K} /-- `A^μν B_μν = 0` for `A` antisymmetric and `B` symmetric. The contraction of `A ⊗ B` is zero when `π₁` reverses the sign of `A`, `π₂` fixes `B`, and together they only permute the pairs. The -hypotheses have the form of the index notation, `{A | μ ν = -A | ν μ}ᵀ` and -`{B | μ ν = B | ν μ}ᵀ`. -/ +hypotheses say `A^μν = -A^νμ` and `B_μν = B_νμ`, for `π₁` and `π₂` the swaps. -/ lemma contr_tmulEquiv_eq_zero_of_antisymm_symm [Fintype J] {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {a : J → Color K} (σ : ColorEquiv (prodColors c₁ c₂) (contrColors c' a)) (π₁ : ColorEquiv c₁ c₁) diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index b6159c4268..232c4d27c8 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -17,12 +17,13 @@ transpose `T^μ_ν ↦ T_ν^μ`. A reindexing `σ : ColorEquiv c₁ c₂` is a bijection `σ.toEquiv : ι₁ ≃ ι₂` of the indices of a tensor that keeps their colors, with the proof `σ.color_eq`. For the transpose `T^μ_ν ↦ T_ν^μ` it swaps -the indices `0` and `1`, and the colors `![up, down]` become `![down, up]`. A permutation that would -turn an up index into a down one is not a reindexing, since that needs a metric. The index types +the indices `0` and `1`, and the colors `![⟨vec, up⟩, ⟨vec, down⟩]` become +`![⟨vec, down⟩, ⟨vec, up⟩]`. A permutation that would turn an up index into a down one is not a +reindexing, since that needs a metric. The index types `ι₁` and `ι₂` may be different, as `Fin 1 ⊕ Fin 1` and `Fin 2` for the product `u^μ v^ν`. `S.reindex σ` moves each vector of a tensor to its new index. The name follows Mathlib's -`PiTensorProduct.reindex`. Index notation uses it to line up indices, as in `A^μ_ν + B_ν^μ`. +`PiTensorProduct.reindex`. It lines up the indices of two tensors, as in `A^μ_ν + B_ν^μ`. ## ii. Key results @@ -130,7 +131,8 @@ variable {ι₁ ι₂ ι₃ : Type} variable (S) in /-- The bijection `e` of indices applied to tensors, moving the vector of the old index `i` to the -new index `e i`, with the colors `c ∘ e.symm`. It is `Representation.Equiv.piTensorReindex`. -/ +new index `e i`, with the colors `c ∘ e.symm`. It is `Representation.Equiv.piTensorReindex`, typed +on `Tensor S (c ∘ e.symm)` so that it composes with other maps of tensors. -/ noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := Representation.Equiv.piTensorReindex (fun i => S.rep (c i)) e From 71a79ed987d44ccdfa9325ce22e403c9f61e80c2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:23:30 +0000 Subject: [PATCH 180/242] refactor(Tensors): Tensorial.toTensor as an equivalence to Species.tensorRep Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Tensorial.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 8d5ba903ea..06d4077130 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -16,7 +16,7 @@ tensors of a species with given colors, compatibly with the action. ## i. Overview A Lorentz vector `v` can be stored as its components `v^μ`, on which Mathlib's analysis acts, and -is also a tensor with one upper index, on which index notation acts. An instance +is also a tensor with one upper index, on which contraction and reindexing act. An instance `Tensorial S c M` relates the two by a Mathlib `Representation.Equiv` `toTensor : M → Tensor S c`, so that `toTensor (Λ v) = Λ • toTensor v`. @@ -73,7 +73,7 @@ class Tensorial {k : outParam Type} [RCLike k] {G : outParam Type} [Group G] {K /-- The action of the group on `M`, such as `Λ v = Λ *ᵥ v` on Lorentz vectors. -/ rep : Representation k G M /-- The elements of `M` as tensors, compatibly with the action. -/ - toTensor : rep.Equiv (Representation.ofDistribMulAction k G (Tensor S c)) + toTensor : rep.Equiv (S.tensorRep c) namespace Tensorial From ff7a8eb9ad075de043c48fdbb874b2eb76702f1a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:23:30 +0000 Subject: [PATCH 181/242] refactor(Tensors): rename reindex_swapTwo_twoIndexEquiv and inverse_eq_of_comp_eq_id, delete tmulEquiv_symm_pure and oneIndexEquiv_basis Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Inverse.lean | 8 ++++---- Physlib/Mathematics/Tensors/LinearMap.lean | 2 +- Physlib/Mathematics/Tensors/Metric.lean | 6 +++--- Physlib/Mathematics/Tensors/Product.lean | 24 +++++----------------- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index 5ee50035f4..c17eba4bc0 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -29,7 +29,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual linear map. - `Species.contr_mul_inverse` and `Species.contr_inverse_mul` prove `g_μν g^νρ = δ_μ^ρ` and `g^νμ g_μρ = δ^ν_ρ`. -- `Species.inverse_eq_left_inv` characterises the inverse as a left inverse. +- `Species.inverse_eq_of_comp_eq_id` characterises the inverse as a left inverse. - `Species.inverse_map_rep` proves `(Λg)⁻¹ = Λ(g⁻¹)`, and `Species.inverse_invariant` that the inverse of an invariant tensor is invariant. - `Species.inverse_form` proves that the inverse of the form at `c` is the form at `c.dual`. @@ -101,7 +101,7 @@ lemma contr_inverse_mul (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi (Module.Basis.comp_coordTensorHomEquiv_coordInverse h) /-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `g`. -/ -lemma inverse_eq_left_inv (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) +lemma inverse_eq_of_comp_eq_id (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLin g)) {N : S.V b.dual ⊗[k] S.V a.dual} (hN : S.toLin N ∘ₗ S.toLin g = LinearMap.id) : S.inverse g = N := Module.Basis.coordInverse_eq_of_comp_eq_id h hN @@ -133,7 +133,7 @@ lemma inverse_invariant (g : S.V a ⊗[k] S.V b) (h : Function.Bijective (S.toLi /-- The inverse of the form at `c` is the form at `c.dual`, as `η^μν` is the inverse of `η_μν`. -/ lemma inverse_form (c : Color K) [S.HasForm c.kind] : S.inverse (form c) = form c.dual := - inverse_eq_left_inv _ (toLin_form_bijective c) (form_inverse c.dual) + inverse_eq_of_comp_eq_id _ (toLin_form_bijective c) (form_inverse c.dual) /-- `g^μν g_νρ = δ^μ_ρ` at an upper color `c`, the delta at `c.dual`. The second index of the form at `c` contracted with the first index of the form at `c.dual` is the delta. -/ @@ -149,7 +149,7 @@ lemma reindex_contr_formTensor_formTensor (c : Color K) [S.HasForm c.kind] : S.reindex (ColorEquiv.swapTwo c c.dual) (Tensor.contr (ColorEquiv.matMul c c c.dual) (S.tmulEquiv _ _ (S.formTensor c ⊗ₜ S.formTensor c.dual))) = S.deltaTensor c := by - rw [contr_formTensor_formTensor, deltaTensor, reindex_twoIndexEquiv, + rw [contr_formTensor_formTensor, deltaTensor, reindex_swapTwo_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta c.dual) = S.delta c from Module.Basis.comm_coordDelta _ _] rfl diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 1f9c39d418..1f342fc868 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -299,7 +299,7 @@ lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by - rw [deltaTensor, reindex_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a + rw [deltaTensor, reindex_swapTwo_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a from Module.Basis.comm_coordDelta _ _] exact contr_deltaTensor i a ha t diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 59d92acbc1..8bce1fa1d2 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -272,13 +272,13 @@ lemma tensorBasis_repr_formTensor (c : Color K) [S.HasForm c.kind] /-- A symmetric form is symmetric under the index swap as a tensor, `g^μν = g^νμ`. -/ lemma formTensor_symm (c : Color K) [S.HasForm c.kind] [S.IsSymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = S.formTensor c := by - rw [formTensor, reindex_twoIndexEquiv, form_symm] + rw [formTensor, reindex_swapTwo_twoIndexEquiv, form_symm] /-- An antisymmetric form is antisymmetric under the index swap as a tensor, `g^μν = -g^νμ`. -/ lemma formTensor_antisymm (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : S.reindex (ColorEquiv.swapTwo c c) (S.formTensor c) = -S.formTensor c := by - rw [formTensor, reindex_twoIndexEquiv, form_antisymm, map_neg] + rw [formTensor, reindex_swapTwo_twoIndexEquiv, form_antisymm, map_neg] /-! @@ -342,7 +342,7 @@ lemma toDualAt_eq_contr {c c' : ι → Color K} (i : ι) (a : Color K) (ha : c i S.toDualAt i a ha hi h t = Tensor.contr (ColorEquiv.matMulAt i a a.dual ha hi h) (S.tmulEquiv c ![a.dual, a.dual] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a.dual a.dual) (S.formTensor a.dual))) := by - rw [formTensor, reindex_twoIndexEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] + rw [formTensor, reindex_swapTwo_twoIndexEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] rfl /-! diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index f71baa39d0..d188adbe4b 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -31,6 +31,7 @@ two indices by `twoIndexEquiv`. The components of a product are the products of - `Species.tmulEquiv_assoc` and `Species.tmulEquiv_comm` prove `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)` and `(T ⊗ U)^μρ = (U ⊗ T)^ρμ`. - `Species.tmulEquiv_smul` proves `Λ T ⊗ Λ U = Λ (T ⊗ U)`. +- `Species.oneIndexEquiv` turns a vector of `V c` into a tensor on one index. - `Species.twoIndexEquiv` turns an element of `V a ⊗ V b` into a tensor on two indices, and `Species.mapAt_one_twoIndexEquiv` proves that a linear map at its index `1` acts on the second factor. @@ -130,15 +131,6 @@ lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ S.pure fun s => Sum.rec (motive := fun s => S.V (prodColors c₁ c₂ s)) f₁ f₂ s := Representation.Equiv.piTprodSum_tmul (fun s => S.rep (prodColors c₁ c₂ s)) f₁ f₂ -/-- The inverse of the product splits a pure tensor into the pure tensor of its vectors at the -indices `inl i` and the pure tensor of its vectors at the indices `inr j`. -/ -@[simp] -lemma tmulEquiv_symm_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) - (f : Π s, S.V (prodColors c₁ c₂ s)) : - (S.tmulEquiv c₁ c₂).symm (S.pure f) = - S.pure (c := c₁) (fun i => f (.inl i)) ⊗ₜ S.pure (c := c₂) fun j => f (.inr j) := - tmulEquivDep_symm_apply _ _ f - /-! ## C. Associativity, symmetry and equivariance @@ -199,7 +191,7 @@ lemma reindex_sumCongr_tmulEquiv {c₁ : ι₁ → Color K} {c₁' : ι₁' → exact congrArg S.pure (funext fun s => by rcases s with _ | _ <;> rfl)) _ /-- Reindexing the first factor reindexes the product. With `tmulEquiv_reindex_right` it moves -reindexings out of products, the form used by index notation. -/ +reindexings out of products. -/ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Color K} {c₂ : ι₂ → Color K} (σ : ColorEquiv c₁ c₁') (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) : S.tmulEquiv c₁' c₂ (S.reindex σ t₁ ⊗ₜ t₂) = @@ -207,7 +199,7 @@ lemma tmulEquiv_reindex_left {c₁ : ι₁ → Color K} {c₁' : ι₁' → Colo rw [reindex_sumCongr_tmulEquiv, reindex_refl, Representation.Equiv.refl_apply] /-- Reindexing the second factor reindexes the product. With `tmulEquiv_reindex_left` it moves -reindexings out of products, the form used by index notation. -/ +reindexings out of products. -/ lemma tmulEquiv_reindex_right {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} {c₂' : ι₂' → Color K} (σ : ColorEquiv c₂ c₂') (t₁ : Tensor S c₁) (t₂ : Tensor S c₂) : S.tmulEquiv c₁ c₂' (t₁ ⊗ₜ S.reindex σ t₂) = @@ -239,7 +231,7 @@ lemma tmulEquiv_smul (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (g : A species gives objects with two indices, such as a metric `η_μν`, in Mathlib's binary tensor product `V a ⊗ V b`. `twoIndexEquiv` identifies this space with the tensors -`Tensor S ![a, b]` on two indices, so that such an object can be used in index notation. +`Tensor S ![a, b]` on two indices, so that such an object can be contracted with other tensors. -/ @@ -255,12 +247,6 @@ lemma oneIndexEquiv_apply (c : Color K) (v : S.V c) : S.oneIndexEquiv c v = S.pure (c := ![c]) (Fin.cons v finZeroElim) := congrArg S.pure (funext fun i => by fin_cases i; rfl) -/-- The tensor of the basis vector `S.basis c (μ 0)` is the basis tensor with label `μ`. -/ -lemma oneIndexEquiv_basis (c : Color K) (μ : Π i, basisIdx (![c] i).kind) : - S.oneIndexEquiv c (S.basis c (μ 0)) = S.tensorBasis ![c] μ := by - rw [oneIndexEquiv_apply, tensorBasis_apply] - exact congrArg S.pure (funext fun j => by fin_cases j; rfl) - variable (S) in /-- An element of `V a ⊗ V b` as a tensor with colors `![a, b]`, as the metric `η_μν e^μ ⊗ e^ν` is the tensor `η_μν`. Each factor is a one-index tensor by `oneIndexEquiv`, and the indices @@ -281,7 +267,7 @@ lemma twoIndexEquiv_tmul (a b : Color K) (x : S.V a) (y : S.V b) : exact congrArg S.pure (funext fun j => by fin_cases j <;> rfl) /-- Swapping the two indices of a tensor on two indices swaps the factors, `M^μν ↦ M^νμ`. -/ -lemma reindex_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : +lemma reindex_swapTwo_twoIndexEquiv {a b : Color K} (M : S.V a ⊗[k] S.V b) : S.reindex (ColorEquiv.swapTwo a b) (S.twoIndexEquiv a b M) = S.twoIndexEquiv b a (TensorProduct.comm k _ _ M) := by induction M using TensorProduct.inductionOn with From 67e595dbf004074e3bafc05898b17f054a9a4143 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:29:03 +0000 Subject: [PATCH 182/242] docs(Tensors): Conjugation key results and section D heading, terse docstrings Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Conjugation.lean | 18 +++++++++--------- Physlib/Mathematics/Tensors/HermitianForm.lean | 3 +-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 4c8f2293e1..aad9283dd9 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -35,8 +35,9 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde - `Color.bar` is the conjugate color, with `Color.bar_bar`. - `Species.HasConjugation` gives the conjugation of a species with its two conditions, and `Species.conjEquiv` is the conjugate of a vector, with its properties - `Species.conjEquiv_conjEquiv`, `Species.star_contr_conjEquiv` and `Species.conjEquiv_rep`. -- `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action. + `Species.conjEquiv_conjEquiv` and `Species.star_contr_conjEquiv`. +- `Species.IsEquivariantConjugation` says that the conjugation commutes with the group action, + `Species.conjEquiv_rep`. - `Species.HasConjugation.ofBasis` builds a componentwise conjugation from `ConjLabels`, and `Species.HasConjugation.isEquivariantConjugation_ofBasis` gives its equivariance from the matrices of the group. @@ -196,9 +197,8 @@ noncomputable def HasConjugation.ofBasis (e : ConjLabels basisIdx) : star_contr_conj _ u α := Module.Basis.star_coordPairing_starEquiv _ _ _ _ _ u α /-- The componentwise conjugation commutes with the group action when the matrix of each `g : G` -at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The hypothesis is needed at -the upper colors only, since it follows at the lower ones -(`Module.Basis.map_rep_of_star_coordPairing`). -/ +at `c.bar` is the conjugate of its matrix at `c`, `(Λ̄)^μ_ν = (Λ^μ_ν)*`. The hypothesis is at the +upper colors only. -/ lemma HasConjugation.isEquivariantConjugation_ofBasis (e : ConjLabels basisIdx) (rep_basis : ∀ (κ : K) (g : G) (μ ν : basisIdx κ), (S.basis ⟨star κ, .up⟩).repr @@ -234,16 +234,16 @@ lemma basis_repr_conjEquiv_of_basis [S.HasConjugation] (h : c' = c.bar) (S.basis c').repr (S.conjEquiv h v) μ = star ((S.basis c).repr v (e.symm μ)) := by rw [Module.Basis.eq_starEquiv _ _ e (S.conjEquiv h) he v, Module.Basis.repr_starEquiv] +end Species + +open Species + /-! ## D. Conjugation of tensors -/ -end Species - -open Species - variable [S.HasConjugation] {ι : Type} {c c' : ι → Color K} /-- The conjugate `T̄` of a tensor `T`, with every index conjugated and moved to the conjugate diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index a18591eaea..6fbe2764e8 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -100,8 +100,7 @@ lemma isSymm_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) -/ -/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective, since the form is -nondegenerate. -/ +/-- The linear map `y ↦ g y` of a positive-definite `g_{IJ̄}` is injective. -/ lemma toLin_injective_of_isPosDef (g : S.V a ⊗[k] S.V b) {hb : b = a.bar} (hg : (S.sesqForm g hb).IsPosDef) : Function.Injective (S.toLin g) := by rw [← LinearMap.ker_eq_bot, LinearMap.ker_eq_bot'] From 6dd38fdedcd02e6e63ff71f348713d071841b13c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:29:03 +0000 Subject: [PATCH 183/242] feat(Tensors): index notation scoped to Tensor, lowering convention in its docs Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Elab.lean | 28 ++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 9038f70a83..7c4fd97657 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -19,6 +19,8 @@ A tensor `T` of a species `S` with colors `![c₀, …, cₙ₋₁]` is written per index. The heights of the indices are not written, since the colors fix them. A real Lorentz tensor `T : Tensor S ![⟨vec, up⟩, ⟨vec, down⟩]`, with components `T^μ_ν`, is written `T | μ ν`. +The notation is scoped: it is active inside the namespace `Tensor` and under +`open scoped Tensor`, so that it does not meet the notation of `Physlib.Relativity.Tensors.Elab`. An expression `{…}ᵀ` is a tensor in `Tensor S ![…]`, or a proposition if it is an equation. The names are bound only inside the braces. Each construct is an operation of the species. @@ -30,11 +32,11 @@ names are bound only inside the braces. Each construct is an operation of the sp with a literal color list `![…]`. - `A + B` and `A = B` reindex `B` by `S.reindex` when its names come in another order. Each name must have the same color on both sides. -- `↓μ` lowers an upper index and `↑μ` raises a lower one, by `S.toDualAt`, so `{v | ↓ν}ᵀ` is - `v_ν = v^μ g_μν`. This needs an instance `S.HasForm` at the kind of the index, and a visible - variance, as in `⟨κ, .up⟩`. -- `τ(μ)` flips the variance of an index by `S.toDualAt` whichever way it points, so it needs no - visible variance. +- `τ(μ)` moves an index to the dual color by `S.toDualAt`, which contracts it with the second + index of the form, so `{v | τ(ν)}ᵀ` is `v_ν = g_νμ v^μ`. This needs an instance `S.HasForm` at + the kind of the index. +- `↓μ` and `↑μ` are `τ(μ)` with a check of the direction: `↓μ` is an error unless the index is + upper, and `↑μ` unless it is lower. They need a visible variance, as in `⟨κ, .up⟩`. - A term `m : M` with an instance `Tensorial S c M`, such as a Lorentz vector, is read as `Tensorial.toTensor m`. - Scalars, group elements and negatives are ordinary terms in front of `|`, as in `r • T | μ ν`, @@ -58,8 +60,8 @@ no indices is written `s |` with a space after the bar, since Mathlib reads `|x| value. For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written -`{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which is -`contr_formTensor_formTensor`. Examples of the notation are in +`{S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ = S.deltaTensor c.dual | μ ρ}ᵀ`, which +`tensor_regroup` proves from `contr_formTensor_formTensor`. Examples of the notation are in `Physlib.Mathematics.Tensors.ElabExamples`. ## ii. Key results @@ -117,8 +119,16 @@ syntax:65 tensorIndexExpr "+" tensorIndexExpr:66 : tensorIndexExpr /-- Parentheses. -/ syntax "(" tensorIndexExpr ")" : tensorIndexExpr -/-- Index notation. -/ -syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term +end Tensor.Notation + +namespace Tensor + +/-- Index notation, active under `open scoped Tensor`. -/ +scoped syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term + +end Tensor + +namespace Tensor.Notation /-! From 0fc4f7aec64702ae609e1f3c0b811ce5273fd1db Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:29:03 +0000 Subject: [PATCH 184/242] refactor: delete Species.toDual_apply, tensorProductToMatrix_symm_map, Lorentz.Vector.tensorComponent, realLorentz.tensorBasis_repr_deltaTensor Co-authored-by: Claude Fable 5.2 --- .../ForMathlib/TensorProductMatrix.lean | 9 ------ Physlib/Mathematics/Tensors/Metric.lean | 4 --- .../Tensors/RealTensor/Species.lean | 25 ++++++---------- .../Tensors/RealTensor/Vector/Basic.lean | 29 ------------------- 4 files changed, 9 insertions(+), 58 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean index 8fd2f232ce..e837de7d3b 100644 --- a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -31,8 +31,6 @@ and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The m - `Module.Basis.tensorProductToMatrix_symm_diagonal` : the tensor with the diagonal coefficient matrix `diagonal D` is `∑ i, D i • b i ⊗ b i`. - `Module.Basis.tensorProductToMatrix_map` : the coefficient matrix of `f ⊗ g` applied to `v`. -- `Module.Basis.tensorProductToMatrix_symm_map` : `f ⊗ g` applied to the tensor with coefficient - matrix `X`. - `Module.Basis.tensorProduct_repr_comm` : swapping the factors of a tensor transposes its coefficients. - `Module.Basis.comm_eq_smul_iff` : swapping the factors scales a tensor of `M ⊗ M` by `r` exactly @@ -107,13 +105,6 @@ lemma tensorProductToMatrix_symm_diagonal (D : ι → R) : (b₁.tensorProductToMatrix b₁).symm (diagonal D) = ∑ i, D i • (b₁ i ⊗ₜ[R] b₁ i) := by simp [tensorProductToMatrix_symm_apply, diagonal_apply, ite_smul] -/-- `f ⊗ g` sends the tensor with coefficient matrix `X` to the one with coefficient matrix -`A * X * Bᵀ`. -/ -lemma tensorProductToMatrix_symm_map (f : M →ₗ[R] M) (g : N →ₗ[R] N) (X : Matrix ι κ R) : - TensorProduct.map f g ((b₁.tensorProductToMatrix b₂).symm X) = - (b₁.tensorProductToMatrix b₂).symm (toMatrix b₁ b₁ f * X * (toMatrix b₂ b₂ g)ᵀ) := by - rw [LinearEquiv.eq_symm_apply, tensorProductToMatrix_map, LinearEquiv.apply_symm_apply] - end Action /-! diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index 8bce1fa1d2..a0d44a2bc7 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -228,10 +228,6 @@ noncomputable def toDual (c : Color K) [S.HasForm c.kind] : (S.rep c).Equiv (S.r ((S.isIntertwiningMap_toLin_iff (a := c.dual) (b := c.dual) _).2 fun g => by rw [TensorProduct.map_comm, form_invariant]).isIntertwining g -/-- `toDual` contracts `v` with the second index of the form at `c.dual`. -/ -lemma toDual_apply (c : Color K) [S.HasForm c.kind] (v : S.V c) : - S.toDual c v = S.toLin (TensorProduct.comm k _ _ (form c.dual)) v := rfl - /-- Raising undoes lowering, `(v_ν)^ν = v^ν`. -/ @[simp] lemma toDual_toDual (c : Color K) [S.HasForm c.kind] (v : S.V c) : diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 6449363874..6cf92922a8 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -34,7 +34,7 @@ delta is `δ_μ^ν = ∑ μ, e^μ ⊗ e_μ`. The one physical input is that the with the matrix `diag(1, -1, …, -1)` at both colors. So the general results apply. For example `η^μν η_νρ = δ^μ_ρ` is -`Species.contr_formTensor_formTensor` at `up`, and lowering an index, `v^μ ↦ v_ν = v^μ η_μν`, is +`Species.contr_formTensor_formTensor` at `up`, and lowering an index, `v^μ ↦ v_ν = η_νμ v^μ`, is `(realLorentz d).toDual up`. The Lorentz vectors and covectors, the spaces of the two colors, are tensors with one upper index @@ -49,7 +49,7 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ - `realLorentz.metric` and the instances `(realLorentz d).HasForm κ` and `(realLorentz d).IsSymmetricForm κ` : the Minkowski metric `η`. - `realLorentz.tensorBasis_repr_formTensor` : the components of `η^μν` and `η_μν` are the - Minkowski matrix, and `realLorentz.tensorBasis_repr_deltaTensor` : those of `δ` the identity. + Minkowski matrix. - `realLorentz.vectorTensorial` and `realLorentz.coVectorTensorial` : Lorentz vectors and covectors as tensors, with the actions `Lorentz.Vector.rep` and `Lorentz.CoVector.rep`. - `realLorentz.tensorBasis_repr_toTensor_vector` and @@ -73,7 +73,7 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ - A. The species - B. The Minkowski metric -- C. Dual bases and components of the metric and the delta +- C. Components of the metric - D. Lorentz vectors and covectors as tensors ## iv. References @@ -218,7 +218,7 @@ lemma metric_invariant (d : ℕ) (c : Color Kind) (Λ : LorentzGroup d) : Matrix.transpose_transpose, ← LorentzGroup.coe_inv, LorentzGroup.transpose_mul_minkowskiMatrix_mul_self] -/-- The metrics at a color and at its dual are inverse, `η^μν η_νρ = δ^μ_ρ`, since `η² = 1`. -/ +/-- The metrics at a color and at its dual are inverse, `η^μν η_νρ = δ^μ_ρ`. -/ lemma metric_inverse (d : ℕ) (c : Color Kind) : (realLorentz d).toLin (metric d c) ∘ₗ (realLorentz d).toLin (metric d c.dual) = LinearMap.id := @@ -243,7 +243,7 @@ instance (d : ℕ) (κ : realLorentz.Kind) : (realLorentz d).IsSymmetricForm κ /-! -## C. Dual bases and components of the metric and the delta +## C. Components of the metric -/ @@ -263,13 +263,6 @@ lemma tensorBasis_repr_formTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin minkowskiMatrix (μ 0) (μ 1) := by rw [Species.tensorBasis_repr_formTensor, formMatrix_apply] -/-- The components of the delta are those of the identity matrix, `δ^μ_ν = 1` for `μ = ν` and `0` -otherwise, at both colors. -/ -lemma tensorBasis_repr_deltaTensor (d : ℕ) (c : Color Kind) (μ : Fin 2 → Fin 1 ⊕ Fin d) : - ((realLorentz d).tensorBasis ![c.dual, c]).repr ((realLorentz d).deltaTensor c) μ = - (1 : Matrix (Fin 1 ⊕ Fin d) (Fin 1 ⊕ Fin d) ℝ) (μ 0) (μ 1) := by - rw [Species.tensorBasis_repr_deltaTensor_eq_one] - /-- Lowering or raising a vector in components, `v_ν = η_νν v^ν`: the Minkowski metric is diagonal. -/ lemma basis_repr_toDual (d : ℕ) (c : Color Kind) (v : (realLorentz d).V c) @@ -389,10 +382,10 @@ lemma tensorBasis_repr_formTensor_contr_vector (d : ℕ) (v w : Vector d) : lowered, is `∑ η_μμ η_νν F^μν G^μν` in components. -/ lemma tensorBasis_repr_toDualAt_contr_vector_tmul (d : ℕ) (F G : Vector d ⊗[ℝ] Vector d) : ((realLorentz d).tensorBasis ![]).repr {F | ↓μ ↓ν ⊗ G | μ ν}ᵀ (fun i => i.elim0) = - ∑ μ, ∑ ν, minkowskiMatrix μ μ * minkowskiMatrix ν ν * Vector.tensorComponent μ ν F * - Vector.tensorComponent μ ν G := by - tensor_components [tensorBasis_repr_toDualAt, tensorBasis_repr_toTensor_vector_tmul, - ← Vector.tensorComponent_apply] + ∑ μ, ∑ ν, minkowskiMatrix μ μ * minkowskiMatrix ν ν * + (Vector.basis.tensorProduct Vector.basis).repr F (μ, ν) * + (Vector.basis.tensorProduct Vector.basis).repr G (μ, ν) := by + tensor_components [tensorBasis_repr_toDualAt, tensorBasis_repr_toTensor_vector_tmul] exact Finset.sum_congr rfl fun μ _ => Finset.sum_congr rfl fun ν _ => by ring /-- The pairing `v^μ ω_μ` of a vector with a covector is Lorentz invariant. -/ diff --git a/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean b/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean index a8a8994af0..7855965de8 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Vector/Basic.lean @@ -7,7 +7,6 @@ module public import Mathlib.Analysis.InnerProductSpace.PiL2 public import Mathlib.Geometry.Manifold.IsManifold.Basic -public import Physlib.Mathematics.ForMathlib.TensorProductMatrix /-! # Lorentz Vectors @@ -325,34 +324,6 @@ lemma sum_inl_inr_basis_eq_zero_iff {d : ℕ} (f₀ : ℝ) (f : Fin d → ℝ) : rw [h1, sum_basis_eq_zero_iff] simp [f'] -/-- The component `F^μν` of `F` in `Vector d ⊗ Vector d`, its coordinate at `(μ, ν)` in Mathlib's -product basis `e_μ ⊗ e_ν`. -/ -def tensorComponent {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) : Vector d ⊗[ℝ] Vector d →ₗ[ℝ] ℝ := - Finsupp.lapply (μ, ν) ∘ₗ (basis.tensorProduct basis).repr.toLinearMap - -/-- The component `F^μν` is the coordinate of `F` at `(μ, ν)` in Mathlib's product basis. -/ -lemma tensorComponent_apply {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (F : Vector d ⊗[ℝ] Vector d) : - tensorComponent μ ν F = (basis.tensorProduct basis).repr F (μ, ν) := rfl - -/-- The components of `v ⊗ w` are the products of components, `(v ⊗ w)^μν = v^μ w^ν`. -/ -@[simp] -lemma tensorComponent_tmul {d : ℕ} (μ ν : Fin 1 ⊕ Fin d) (v w : Vector d) : - tensorComponent μ ν (v ⊗ₜ w) = v μ * w ν := by - simp [tensorComponent_apply, Module.Basis.tensorProduct_repr_tmul_apply, basis_repr_apply, - mul_comm] - -/-- Two elements of `Vector d ⊗ Vector d` with the same components are equal. -/ -lemma tensorComponent_ext {d : ℕ} {F G : Vector d ⊗[ℝ] Vector d} - (h : ∀ μ ν, tensorComponent μ ν F = tensorComponent μ ν G) : F = G := - (basis.tensorProduct basis).ext_elem fun μν => h μν.1 μν.2 - -/-- An element of `Vector d ⊗ Vector d` is the sum `F^μν e_μ ⊗ e_ν` of its components. -/ -lemma eq_sum_tensorComponent {d : ℕ} (F : Vector d ⊗[ℝ] Vector d) : - F = ∑ μ, ∑ ν, tensorComponent μ ν F • basis μ ⊗ₜ[ℝ] basis ν := by - conv_lhs => rw [← (basis.tensorProductToMatrix basis).symm_apply_apply F, - Module.Basis.tensorProductToMatrix_symm_apply] - rfl - /-! ## C. The Spatial part From e206d12893453ded96818837a9ee6b7a3f051bfc Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:29:03 +0000 Subject: [PATCH 185/242] refactor(Tensors): delete complexLorentz.tensorBasis_repr_deltaTensor, terse docstrings in the complex species and the Pauli tensor Co-authored-by: Claude Fable 5.2 --- .../Relativity/Bispinors/Pauli/Tensor.lean | 6 ++--- .../Tensors/ComplexTensor/Species.lean | 23 +++++-------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean index 91b0f2707b..2f452ef954 100644 --- a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -213,16 +213,14 @@ lemma bispinorToVector_vectorToBispinor (p : ℂT[up, upL, upR]) : simp only [vectorToBispinor, bispinorToVector, LinearMap.coe_mk, AddHom.coe_mk] tensor_regroup using pauliContrDown_trace_pauliCo -/-- `vectorToBispinor ∘ bispinorToVector = 2`: both spaces have dimension `16`, so the left inverse -`bispinorToVector / 2` of `vectorToBispinor` is a right inverse. -/ +/-- `vectorToBispinor ∘ bispinorToVector = 2`. -/ lemma vectorToBispinor_bispinorToVector (X : ℂT[upL, upR, upL, upR]) : vectorToBispinor (bispinorToVector X) = (2 : ℂ) • X := LinearMap.comp_apply_eq_smul_of_comp_apply_eq_smul_of_finrank_eq (by rw [Species.finrank_tensor, Species.finrank_tensor]; rfl) two_ne_zero bispinorToVector_vectorToBispinor X -/-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`: it is -`vectorToBispinor σT = vectorToBispinor (bispinorToVector (εL ⊗ εR)) / 2`. -/ +/-- The Fierz identity `σT_ν^{αβ̇} σT^{να'β̇'} = 2 ε^{αα'} ε^{β̇β̇'}`. -/ lemma pauliCo_contr_pauliContr : {pauliTensor | τ(ν) α β ⊗ pauliTensor | ν α' β' = (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean index 15aeb78d0c..daf1e2e340 100644 --- a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -56,8 +56,7 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ conjugates of the components. - `complexLorentz.formMatrix_apply` : the components `Species.formMatrix` of the form at any color are `complexLorentz.formEntries`, for lowering in components. -- `complexLorentz.tensorBasis_repr_formTensor` : the components of `ε` and `η` at any color, and - `complexLorentz.tensorBasis_repr_deltaTensor` those of `δ`. +- `complexLorentz.tensorBasis_repr_formTensor` : the components of `ε` and `η` at any color. - `complexLorentz.formTensor_upL_antisymm`, `complexLorentz.formTensor_up_symm` : `ε` is antisymmetric and `η` symmetric, and `complexLorentz.deltaTensor_swap` : `δ^μ_ν = δ_ν^μ`. - `complexLorentz.schouten_downL` and `complexLorentz.schouten_downR` : the Schouten identity of @@ -73,7 +72,7 @@ enters index notation directly, as in `{ψ | α ⊗ φ | α}ᵀ` for `ψ^α φ_ - A. The species - B. The invariant forms - C. Conjugation -- D. Components of the forms and the deltas +- D. Components of the forms - E. Symmetry of the forms - F. Weyl spinors and complex vectors as tensors - G. Examples @@ -255,8 +254,7 @@ noncomputable def form : (c : Color Kind) → complexLorentz.V c ⊗[ℂ] comple | ⟨vec, .up⟩ => ComplexVector.metricVal | ⟨vec, .down⟩ => ComplexCoVector.metricVal -/-- The forms are invariant under `SL(2, ℂ)`, each the value at `1` of an intertwining map from -the trivial representation. -/ +/-- The forms are invariant under `SL(2, ℂ)`. -/ lemma form_invariant : ∀ (c : Color Kind) (g : SL(2, ℂ)), TensorProduct.map (complexLorentz.rep c g) (complexLorentz.rep c g) (form c) = form c | ⟨left, .up⟩ => fun g => (map_neg _ _).trans <| congrArg Neg.neg <| @@ -316,7 +314,7 @@ lemma formEntries_dual_mul : ∀ c : Color Kind, (@minkowskiMatrix 3).map Complex.ofRealHom = 1 by rw [← Matrix.map_mul, minkowskiMatrix.sq, Matrix.map_one _ (map_zero _) (map_one _)] -/-- The forms at `c` and at `c.dual` are inverse to each other, since their matrices are. -/ +/-- The forms at `c` and at `c.dual` are inverse to each other. -/ lemma form_inverse (c : Color Kind) : complexLorentz.toLin (form c) ∘ₗ complexLorentz.toLin (form c.dual) = LinearMap.id := Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one _ _ _ _ <| by @@ -427,7 +425,7 @@ end complexLorentz /-! -## D. Components of the forms and the deltas +## D. Components of the forms -/ @@ -453,14 +451,6 @@ lemma tensorBasis_repr_formTensor_vec (v : Variance) μ = minkowskiMatrix (μ 0) (μ 1) := tensorBasis_repr_formTensor ⟨vec, v⟩ μ -/-- The components of the delta are those of the identity matrix, `δ^μ_ν = 1` for `μ = ν` and `0` -otherwise, at every color. -/ -lemma tensorBasis_repr_deltaTensor (c : Color Kind) - (μ : Π i, Idx (![c.dual, c] i).kind) : - (complexLorentz.tensorBasis ![c.dual, c]).repr (complexLorentz.deltaTensor c) μ = - (1 : Matrix (Idx c.kind) (Idx c.kind) ℂ) (μ 0) (μ 1) := by - rw [Species.tensorBasis_repr_deltaTensor_eq_one] - /-- `η^00 = 1` and `η^11 = -1`: the signature is mostly minus. -/ example : (complexLorentz.tensorBasis ![up, up]).repr η (Fin.cons (Sum.inl 0 : Fin 1 ⊕ Fin 3) (Fin.cons (Sum.inl 0 : Fin 1 ⊕ Fin 3) (·.elim0))) = @@ -638,8 +628,7 @@ example (c : Color Kind) : {complexLorentz.formTensor c | α β ⊗ tensor_regroup /-- The components of `ε^αβ ⊗ ε^α̇β̇` are the products of the components of `εL` and `εR`. -/ -lemma tensorBasis_repr_formTensor_upL_tmul_upR - (μ : Π i, Idx (![upL, upL, upR, upR] i).kind) : +example (μ : Π i, Idx (![upL, upL, upR, upR] i).kind) : (complexLorentz.tensorBasis _).repr {εL | α β ⊗ εR | α' β'}ᵀ μ = metricRaw (μ 0) (μ 1) * metricRaw (μ 2) (μ 3) := by rw [Species.tensorBasis_repr_reindex, Species.tensorBasis_repr_tmulEquiv, From ee9a2d8739be31d4558ce3549950bb3e1324cc59 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:31:31 +0000 Subject: [PATCH 186/242] style(Tensors): line lengths in Contraction and LinearMap Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Contraction.lean | 5 +++-- Physlib/Mathematics/Tensors/LinearMap.lean | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 969f469ced..3dee35f09b 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -24,8 +24,9 @@ The pairs to contract are chosen by a reindexing `σ : ColorEquiv c (contrColors indices. It sends each free index to an index `inl x`, of color `c' x`, and the two indices of a pair `j` to the indices `inr (inl j)` and `inr (inr j)`, of colors `a j` and `(a j).dual`. For the trace, `σ` sends `0 ↦ inl 0`, `1 ↦ inr (inl ())` and `2 ↦ inr (inr ())`. Since `σ` keeps colors, -the two indices of each pair are dual without a separate hypothesis. `Tensor.contr σ` reindexes by `σ` and then contracts -each pair by `S.contr (a j)`, with the first index of the pair as its first argument. +the two indices of each pair are dual without a separate hypothesis. `Tensor.contr σ` reindexes by +`σ` and then contracts each pair by `S.contr (a j)`, with the first index of the pair as its first +argument. Two tensors are contracted by contracting their product. In the full contraction `T^μν U_μν` the product `T ⊗ U` has four indices, which form two pairs and leave no index, `κ = Empty`. The diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index 1f342fc868..f20f1e8d3d 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -299,8 +299,9 @@ lemma contr_deltaTensor_swap [DecidableEq ι₁] {c : ι₁ → Color K} (i : ι contr (ColorEquiv.matMulAt i a a ha ha fun _ _ => rfl) (S.tmulEquiv c ![a.dual, a] (t ⊗ₜ S.reindex (ColorEquiv.swapTwo a a.dual) (S.deltaTensor a.dual))) = t := by - rw [deltaTensor, reindex_swapTwo_twoIndexEquiv, show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a - from Module.Basis.comm_coordDelta _ _] + rw [deltaTensor, reindex_swapTwo_twoIndexEquiv, + show TensorProduct.comm k _ _ (S.delta a.dual) = S.delta a from + Module.Basis.comm_coordDelta _ _] exact contr_deltaTensor i a ha t end Tensor From f41abc501317e7d8563c801c8d88bf536e6c1a49 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:49:40 +0000 Subject: [PATCH 187/242] refactor(ForMathlib): LinearMap.IsPosDef.of_comp_star for a surjective map Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/ForMathlib/SesquilinearForm.lean | 12 ++++++------ Physlib/Mathematics/Tensors/HermitianForm.lean | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index 5904ee30ca..b4229964dd 100644 --- a/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean +++ b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean @@ -22,7 +22,7 @@ positive-definite matrices (`Matrix.PosDef`), but no positive-definite sesquilin extending `LinearMap.IsSymm`. For a form `M →ₗ⋆[R] M →ₗ[R] R` symmetry is Hermiticity, `star (B x y) = B y x`. A positive-definite form is positive-semidefinite and nondegenerate, and in a basis it is positive-definite exactly when its matrix is. Symmetry and positive-definiteness -transfer along a surjective or bijective linear map that turns one form into the conjugate of the +transfer along a surjective linear map that turns one form into the conjugate of the other, as for the inverse of a Hermitian metric. ## ii. Key results @@ -31,7 +31,7 @@ other, as for the inverse of a Hermitian metric. - `LinearMap.IsPosDef.isPosSemidef` : a positive-definite form is positive-semidefinite. - `LinearMap.IsPosDef.nondegenerate` : a positive-definite form is nondegenerate. - `LinearMap.IsSymm.of_comp_star` and `LinearMap.IsPosDef.of_comp_star` : a form `B'` with - `B' (A x) (A y) = star (B y x)` for a surjective or bijective `A` is symmetric or + `B' (A x) (A y) = star (B y x)` for a surjective `A` is symmetric or positive-definite when `B` is. - `LinearMap.isPosDef_iff_posDef_toMatrix` : a form is positive-definite exactly when its matrix in a basis is. @@ -122,12 +122,12 @@ lemma IsSymm.of_comp_star (hA : Function.Surjective A) (hB : B.IsSymm) rw [h, h, starRingEnd_apply, star_star] exact (hB.eq x y).symm -/-- If `B' (A x) (A y) = star (B y x)` for a bijective `A`, then `B'` is positive-definite when `B` +/-- If `B' (A x) (A y) = star (B y x)` for a surjective `A`, then `B'` is positive-definite when `B` is. -/ -lemma IsPosDef.of_comp_star [LT R] (hA : Function.Bijective A) (hB : B.IsPosDef) +lemma IsPosDef.of_comp_star [LT R] (hA : Function.Surjective A) (hB : B.IsPosDef) (h : ∀ x y, B' (A x) (A y) = star (B y x)) : B'.IsPosDef := by - refine ⟨hB.isSymm.of_comp_star hA.2 h, fun u hu => ?_⟩ - obtain ⟨x, rfl⟩ := hA.2 u + refine ⟨hB.isSymm.of_comp_star hA h, fun u hu => ?_⟩ + obtain ⟨x, rfl⟩ := hA u rw [h, ← starRingEnd_apply, hB.isSymm.eq x x] exact hB.pos x fun hx => hu (by rw [hx, map_zero]) diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 6fbe2764e8..9e896b8c23 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -120,7 +120,7 @@ lemma isPosDef_sesqForm_inverse (g : S.V a ⊗[k] S.V b) (hb : b = a.bar) (hg : (S.sesqForm g hb).IsPosDef) : (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsPosDef := have h := toLin_bijective_of_isPosDef g hg - hg.of_comp_star h (sesqForm_inverse_toLin g h hb) + hg.of_comp_star h.2 (sesqForm_inverse_toLin g h hb) end Species From 563b923c5668abb767873152ba35bf640e38f6f4 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:49:40 +0000 Subject: [PATCH 188/242] docs(Tensors): pure tensors span, conjugation in components needs the bases, index types in the notation error Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Conjugation.lean | 8 +++++--- Physlib/Mathematics/Tensors/Elab.lean | 2 +- Physlib/Mathematics/Tensors/ElabExamples.lean | 4 ++-- Physlib/Mathematics/Tensors/Tensor.lean | 9 +++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index aad9283dd9..34ce36e1ff 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -11,12 +11,14 @@ public import Physlib.Mathematics.Tensors.Contraction # Conjugation of tensors -The conjugate of a tensor conjugates its components and moves each index to the conjugate color. +The conjugate of a tensor applies the conjugation of the species at every index, which moves the +index to the conjugate color. ## i. Overview -Conjugation conjugates the components of a tensor and moves each index to the conjugate kind, -keeping its variance. A left-handed spinor `ψ_α` becomes a right-handed spinor `ψ̄_α̇`, and a +Conjugation applies a conjugate-linear map of the species at every index of a tensor and moves the +index to the conjugate kind, keeping its variance. When these maps send the basis of each color to +the basis of its conjugate, it conjugates the components. A left-handed spinor `ψ_α` becomes a right-handed spinor `ψ̄_α̇`, and a vector index is its own conjugate. The kinds of a species carry Mathlib's `InvolutiveStar`, whose `star` gives the conjugate kind, and diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 7c4fd97657..2baf2d91d7 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -338,7 +338,7 @@ def leaf (T : Term) (idxs : Array (TSyntax `tensorIndex)) : TermElabM Node := do | throwError "index notation: internal error, the type of {T} is not a tensor type" let some indices ← Indices.ofType? ι | throwError "index notation: the index type {ι} of {T} must be built from `Fin n` with a \ - literal `n`, `Unit`, `Empty` and `⊕`" + literal `n` and `⊕`" let some colors ← colorList? c | throwError "index notation: the colors of {T} must be a literal list" unless colors.length = idxs.size do diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index b0aeda97f3..d271d04d88 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -124,8 +124,8 @@ example : {(T | μ μ) ⊗ U | μ ρ}ᵀ = {{T | μ μ}ᵀ | ⊗ U | μ ρ}ᵀ : example (f : Fin 2 → Color K) (t : Tensor S f) : Tensor S f := {t | μ ν}ᵀ -- A tensor whose index type is not literal is rejected. -/-- error: index notation: the index type Fin n of t must be built from `Fin n` with a literal `n`, -`Unit`, `Empty` and `⊕` -/ +/-- error: index notation: the index type Fin n of t must be built from `Fin n` with a literal `n` +and `⊕` -/ #guard_msgs (whitespace := lax) in example (n : ℕ) (f : Fin n → Color K) (t : Tensor S f) : Tensor S f := {t | μ}ᵀ diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 89f1ae9dbd..c9b907beeb 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -24,9 +24,9 @@ The tensors with colors `c : ι → Color K` form the space `Tensor S c`, which A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in `V (c 1)` and `β` in `V (c 2)`. Its components are products, `(u ⊗ α ⊗ β)^μ_νρ = u^μ α_ν β_ρ`. Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not -a pure tensor itself. Every tensor is such a sum, so a linear map of tensors is fixed by its -values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case -(`inductionOn_pure`). Maps of tensors, starting with the group action, are given by their value on +a pure tensor itself. Every tensor is a linear combination of pure tensors, so a linear map of +tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows +from the pure case (`inductionOn_pure`). Maps of tensors, starting with the group action, are given by their value on pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's `⨂ₜ[k] i, f i` as an element of `Tensor S c`. @@ -137,7 +137,8 @@ noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : Tens variable {S : Species k G K basisIdx} -/-- Induction on tensors through pure tensors: every tensor is a sum of pure tensors. -/ +/-- Induction on tensors through pure tensors: every tensor is a linear combination of pure +tensors. -/ lemma inductionOn_pure {P : Tensor S c → Prop} (h : ∀ f : Π i, S.V (c i), P (S.pure f)) (hsmul : ∀ (r : k) t, P t → P (r • t)) (hadd : ∀ t₁ t₂, P t₁ → P t₂ → P (t₁ + t₂)) (t : Tensor S c) : P t := From 51c0fb2493d9a3625e5313defe135f1f3ca5e13e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 11:50:21 +0000 Subject: [PATCH 189/242] style(Tensors): line lengths in the Tensor and Conjugation module docs Co-authored-by: Claude Fable 5.2 --- Physlib/Mathematics/Tensors/Conjugation.lean | 4 ++-- Physlib/Mathematics/Tensors/Tensor.lean | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 34ce36e1ff..8d1a0cf448 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -18,8 +18,8 @@ index to the conjugate color. Conjugation applies a conjugate-linear map of the species at every index of a tensor and moves the index to the conjugate kind, keeping its variance. When these maps send the basis of each color to -the basis of its conjugate, it conjugates the components. A left-handed spinor `ψ_α` becomes a right-handed spinor `ψ̄_α̇`, and a -vector index is its own conjugate. +the basis of its conjugate, it conjugates the components. A left-handed spinor `ψ_α` becomes a +right-handed spinor `ψ̄_α̇`, and a vector index is its own conjugate. The kinds of a species carry Mathlib's `InvolutiveStar`, whose `star` gives the conjugate kind, and the conjugate color is `c.bar = ⟨star c.kind, c.variance⟩`. The species gives the conjugation maps diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index c9b907beeb..71ad4b254a 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -26,9 +26,9 @@ A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not a pure tensor itself. Every tensor is a linear combination of pure tensors, so a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows -from the pure case (`inductionOn_pure`). Maps of tensors, starting with the group action, are given by their value on -pure tensors, and their properties are proved there first. `S.pure f` is Mathlib's -`⨂ₜ[k] i, f i` as an element of `Tensor S c`. +from the pure case (`inductionOn_pure`). Maps of tensors, starting with the group action, are +given by their value on pure tensors, and their properties are proved there first. `S.pure f` is +Mathlib's `⨂ₜ[k] i, f i` as an element of `Tensor S c`. A group element `Λ` acts on every index by the representation of its color, `Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation `S.tensorRep c`, From 2fc06eeb3f742b1d0f59e6515b8e1df7b0ea6d28 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:28:45 +0000 Subject: [PATCH 190/242] docs(Tensors): Tensor module doc with the motivation in the overview and the details in its sections Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 128 ++++++++++++------------ 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 71ad4b254a..130164fd6a 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -17,72 +17,33 @@ index type `ι`, and the group acts on them index by index. ## i. Overview -A tensor of a species `S` has one index `i` for each `i : ι`, and the index `i` has the color `c i`. -The tensors with colors `c : ι → Color K` form the space `Tensor S c`, which is Mathlib's -`⨂[k] i, V (c i)`. The index type `ι` can be any type, such as `Fin n` or a sum `ι₁ ⊕ ι₂`. - -A pure tensor takes one vector at each index, such as `u ⊗ α ⊗ β` with `u` in `V (c 0)`, `α` in -`V (c 1)` and `β` in `V (c 2)`. Its components are products, `(u ⊗ α ⊗ β)^μ_νρ = u^μ α_ν β_ρ`. -Most tensors are not pure. The metric `η = ∑ μ, η_μμ e^μ ⊗ e^μ` is a sum of pure tensors but not -a pure tensor itself. Every tensor is a linear combination of pure tensors, so a linear map of -tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows -from the pure case (`inductionOn_pure`). Maps of tensors, starting with the group action, are -given by their value on pure tensors, and their properties are proved there first. `S.pure f` is -Mathlib's `⨂ₜ[k] i, f i` as an element of `Tensor S c`. - -A group element `Λ` acts on every index by the representation of its color, -`Λ (u ⊗ α ⊗ β) = Λu ⊗ Λα ⊗ Λβ` (`smul_pure`). This is the representation `S.tensorRep c`, -`Representation.piTprod` on `⨂[k] i, V (c i)`, written `Λ • T` under `open scoped Tensor`. A map -that commutes with `Λ` on pure tensors commutes with it on all tensors (`map_smul_of_pure`, from -`Representation.map_piTprod_of_tprod`). - -The components `T^μ_νρ` of a tensor `T` are its coordinates in the basis `e_μ ⊗ e^ν ⊗ e^ρ` -(`tensorBasis`), which is Mathlib's `Basis.piTensorProduct`. Each basis tensor is a pure tensor of -basis vectors. - -For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, down⟩, ⟨vec, down⟩]` give the space -`Vector 3 ⊗ CoVector 3 ⊗ CoVector 3` of tensors `T = T^μ_νρ e_μ ⊗ e^ν ⊗ e^ρ`. A Lorentz -transformation `Λ` acts as -`(ΛT)^μ_νρ = Λ^μ_σ (Λ⁻¹)^τ_ν (Λ⁻¹)^λ_ρ T^σ_τλ` (`tensorBasis_repr_smul`). Lower indices -transform with `Λ⁻¹`, which keeps `u^μ α_μ` invariant. - -A linear equivalence `f` from the space of one color to that of another acts on tensors through a -single index. `S.congrAt i f` applies `f` at the index `i` and the identity at the other indices, -`u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. +The tensors of a species `S` with colors `c : ι → Color K` form the space `Tensor S c`, which is +Mathlib's `⨂[k] i, S.V (c i)`. The index type `ι` is any type, such as `Fin n`. The product of +tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so the indices of each factor keep their +names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. + +A group element acts on a tensor at every index, by the representation of its color. A linear map +at one index changes that index and keeps the others, as lowering an index with a metric does. +Both are linear maps fixed by their values on pure tensors, which take one vector at each index, so +their properties are proved on pure tensors. The components of a tensor are its coordinates in the +product basis. + +For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, +and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. A Lorentz +transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the second index with the metric, +`T^μ_ν = η_νρ T^μρ`, is a linear map at the index `1` that changes its color to `⟨vec, down⟩`. ## ii. Key results -- `Tensor` and `Species.pure` are the tensors and the pure tensors, and - `Species.inductionOn_pure` is induction through pure tensors. -- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. It is - an `ext` lemma, so `ext p q` also reduces maps out of products `Tensor S c ⊗ Tensor S c₂`, and - intertwining maps, to pure tensors. `Species.equiv_ext_pure` does the same for equivalences of - representations. -- `Species.smul_pure` proves that the group acts on a pure tensor index by index. -- `Species.map_smul_of_pure` proves that a map of tensors that commutes with the group on pure - tensors commutes with it on all tensors. -- `Species.tensorRep` is the action on tensors as a Mathlib representation, and - `Species.distribMulAction` is the scoped instance for `Λ • T`. Maps of tensors that - commute with the group are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` - of it. `Species.equivOfPure` builds an equivalence from a check on pure tensors, and - `Species.equiv_smul` and `Species.intertwiningMap_smul` give `φ (g • t) = g • φ t`. -- `Species.smul_eq_self_of_isEmpty` proves that a tensor with no indices is invariant, from - `PiTensorProduct.map_eq_id_of_isEmpty`. -- `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_pure` gives the - components of a pure tensor. `Species.finrank_tensor` gives the dimension of tensors, from - `Module.finrank_piTensorProduct`. -- `Species.tensorBasis_repr_smul` gives the components of `g • t`, from the matrix of - `PiTensorProduct.map` in `PiTensorProduct.toMatrix_map_apply`. -- `Species.mapIndices` applies a linear map at every index, with `Species.mapIndices_comp` and - `Species.mapIndices_id`, and `Species.mapIndices_smul` proves that it commutes with the group when - every index map does. -- `Species.congrIndices` applies an equivalence of representations at every index. It is - `Representation.Equiv.piTensorCongr`. -- `Species.indexMapsAt` is the family of a linear map at one index and the identity at the others, - and `Species.mapAt` applies it, with `Species.mapAt_pure`, `Species.mapAt_comp` and - `Species.mapAt_id`. -- `Species.congrAt` is `mapAt` of an equivalence of representations, as an equivalence of - representations, with `Species.congrAt_symm`. +- `Tensor` is the space of tensors, and `Species.pure` is a pure tensor. +- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. +- `Species.tensorRep` is the group action on tensors, and `Species.smul_pure` gives it on a pure + tensor. +- `Species.map_smul_of_pure` proves that a map that commutes with the group on pure tensors + commutes with it on all tensors. +- `Species.tensorBasis` is the basis of tensors, and `Species.tensorBasis_repr_smul` gives the + components of `Λ • T`. +- `Species.mapAt` and `Species.congrAt` apply a map at one index. ## iii. Table of contents @@ -93,7 +54,7 @@ single index. `S.congrAt i f` applies `f` at the index `i` and the identity at t ## iv. References -There are no known references for the material in this module. +* None. -/ @@ -112,6 +73,13 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → ## A. Tensors +`Tensor S c` is `⨂[k] i, S.V (c i)`. A pure tensor `S.pure f` takes one vector `f i` at each +index, as `u ⊗ α`. Most tensors are not pure, but every tensor is a linear combination of pure +tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a +property of all tensors follows from the pure case (`inductionOn_pure`). `ext_pure` is an `ext` +lemma, so `ext p q` also reduces maps out of products `Tensor S c ⊗ Tensor S c₂`, and +intertwining maps, to pure tensors. + -/ /-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not @@ -154,6 +122,17 @@ lemma ext_pure {E : Type*} [AddCommMonoid E] [Module k E] {f g : Tensor S c → ## B. The group action +A group element `Λ` acts on every index by the representation of its color, +`Λ (u ⊗ α) = Λu ⊗ Λα` (`smul_pure`). This is the representation `S.tensorRep c`, +`Representation.piTprod` on `⨂[k] i, S.V (c i)`. It is written `Λ • T` under +`open scoped Tensor`. + +Maps of tensors that commute with the group are Mathlib's `Representation.Equiv` and +`Representation.IntertwiningMap` for `tensorRep`, and `equiv_smul` and `intertwiningMap_smul` give +`φ (Λ • T) = Λ • φ T`. A map that commutes with `Λ` on pure tensors commutes with it on all tensors +(`map_smul_of_pure`), and `equivOfPure` builds an equivalence of representations from that check. +A tensor with no indices is invariant (`smul_eq_self_of_isEmpty`). + -/ variable (S) in @@ -246,6 +225,15 @@ lemma smul_eq_self_of_isEmpty [IsEmpty ι] (g : G) (t : Tensor S c) : g • t = ## C. The basis of tensors +The components `T^μ_ν` of a tensor are its coordinates in the basis `e_μ ⊗ e^ν` (`tensorBasis`), +Mathlib's `Basis.piTensorProduct` of the bases of the colors. Each basis tensor is a pure tensor of +basis vectors, and the components of a pure tensor are products, `(u ⊗ α)^μ_ν = u^μ α_ν` +(`tensorBasis_repr_pure`). The dimension is the product of the dimensions (`finrank_tensor`). + +The components of `Λ • T` take one matrix of `Λ` per index (`tensorBasis_repr_smul`), as in +`(ΛT)^μ_ν = Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ`. Lower indices transform with `Λ⁻¹`, which keeps `u^μ α_μ` +invariant. + -/ /-- The basis `e_μ ⊗ e^ν ⊗ …` of tensors, Mathlib's `Basis.piTensorProduct` of the bases of the @@ -290,6 +278,18 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S ## D. Linear maps at the indices +`mapIndices f` applies a linear map `f x` at every index `x`, `u ⊗ α ↦ f₀ u ⊗ f₁ α`. It is +Mathlib's `PiTensorProduct.map`, and it commutes with the group when every `f x` does +(`mapIndices_smul`). `congrIndices` is the same for equivalences of representations. + +`mapAt i f h` applies `f` at the single index `i` and keeps the others, `u ⊗ α ↦ u ⊗ f α`. The map +`f` may change the color at `i`, so the colors `c'` of the result differ from `c` there, and +`h : ∀ x, x ≠ i → c x = c' x` says they agree elsewhere. The vectors at the other indices are +moved along `h` by `S.cast`. `indexMapsAt` is this family of maps, `congrAt` is `mapAt` of an +equivalence of representations, and `tensorBasis_repr_mapAt` gives the components, +`(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…`. Lowering an index with a metric is `congrAt` of the map +`v^μ ↦ η_νμ v^μ`, in `Metric.lean`. + -/ section From f58d3065ebc31e5c8ea87e0265e5377e73f8f842 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:28:45 +0000 Subject: [PATCH 191/242] docs(Tensors): None as the references of the tensor module docs Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/CoordTensorHom.lean | 2 +- Physlib/Mathematics/ForMathlib/EquivPairs.lean | 2 +- Physlib/Mathematics/ForMathlib/FiniteDimensional.lean | 2 +- Physlib/Mathematics/ForMathlib/PiTensorProduct.lean | 2 +- Physlib/Mathematics/ForMathlib/Representation.lean | 2 +- Physlib/Mathematics/ForMathlib/Schouten.lean | 2 +- Physlib/Mathematics/ForMathlib/SemilinearMaps.lean | 2 +- Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean | 2 +- Physlib/Mathematics/Tensors/Conjugation.lean | 2 +- Physlib/Mathematics/Tensors/Contraction.lean | 2 +- Physlib/Mathematics/Tensors/Elab.lean | 2 +- Physlib/Mathematics/Tensors/ElabExamples.lean | 2 +- Physlib/Mathematics/Tensors/ElabTactics.lean | 2 +- Physlib/Mathematics/Tensors/HermitianForm.lean | 2 +- Physlib/Mathematics/Tensors/Inverse.lean | 2 +- Physlib/Mathematics/Tensors/LinearMap.lean | 2 +- Physlib/Mathematics/Tensors/Metric.lean | 2 +- Physlib/Mathematics/Tensors/Product.lean | 2 +- Physlib/Mathematics/Tensors/Reindexing.lean | 2 +- Physlib/Mathematics/Tensors/Schouten.lean | 2 +- Physlib/Mathematics/Tensors/Tensorial.lean | 2 +- Physlib/Relativity/Tensors/RealTensor/Species.lean | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean index 3bf54fe716..0e9bad6aef 100644 --- a/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -76,7 +76,7 @@ bijective the inverse is the junk value `0`, so every lemma about it assumes bij ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/EquivPairs.lean b/Physlib/Mathematics/ForMathlib/EquivPairs.lean index 2d6350099a..6308722636 100644 --- a/Physlib/Mathematics/ForMathlib/EquivPairs.lean +++ b/Physlib/Mathematics/ForMathlib/EquivPairs.lean @@ -40,7 +40,7 @@ matching of pairs is a bijection `J → J'`. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean index e08721ad99..856a1b231a 100644 --- a/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -32,7 +32,7 @@ between two spaces `V` and `W` of the same finite dimension: `g ∘ f = id` on ` ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index ed1eaf9ed7..096abb503b 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -51,7 +51,7 @@ On the index type `κ ⊕ (J ⊕ J)`, a family of pairings `p j` of the factors ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index a25b883da3..0ca2c7724e 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -66,7 +66,7 @@ intertwining. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean index 4274eb10d4..2b566fca6c 100644 --- a/Physlib/Mathematics/ForMathlib/Schouten.lean +++ b/Physlib/Mathematics/ForMathlib/Schouten.lean @@ -33,7 +33,7 @@ Read at a label on three basis vectors, it relates the entries of the matrix of ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean index 3591b2a918..988c936857 100644 --- a/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -56,7 +56,7 @@ dimension. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean index e837de7d3b..a2b8fc3538 100644 --- a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -44,7 +44,7 @@ and `b₁.tensorProductToMatrix b₂` is the linear equivalence `v ↦ X`. The m ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean index 8d1a0cf448..dd98105d89 100644 --- a/Physlib/Mathematics/Tensors/Conjugation.lean +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -59,7 +59,7 @@ its conjugate. The conjugate `Tensor.conj h T` of a tensor conjugates every inde ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean index 3dee35f09b..32cee64e55 100644 --- a/Physlib/Mathematics/Tensors/Contraction.lean +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -83,7 +83,7 @@ since the bases of a color and its dual are dual. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Elab.lean b/Physlib/Mathematics/Tensors/Elab.lean index 2baf2d91d7..783171932a 100644 --- a/Physlib/Mathematics/Tensors/Elab.lean +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -78,7 +78,7 @@ For an upper color `c`, the inverse forms `g^μν g_νρ = δ^μ_ρ` are written ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean index d271d04d88..7901ba15f5 100644 --- a/Physlib/Mathematics/Tensors/ElabExamples.lean +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -34,7 +34,7 @@ There are no declarations, only examples. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean index 7090c82902..5aca5347a5 100644 --- a/Physlib/Mathematics/Tensors/ElabTactics.lean +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -39,7 +39,7 @@ Examples are in `Physlib.Mathematics.Tensors.ElabExamples`. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean index 9e896b8c23..6038b2d8cd 100644 --- a/Physlib/Mathematics/Tensors/HermitianForm.lean +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -38,7 +38,7 @@ is again Hermitian or positive-definite, and a positive-definite `g_{IJ̄}` is i ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean index c17eba4bc0..b04c010633 100644 --- a/Physlib/Mathematics/Tensors/Inverse.lean +++ b/Physlib/Mathematics/Tensors/Inverse.lean @@ -43,7 +43,7 @@ with the group action, and the inverse of the form at `c` is the form at `c.dual ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean index f20f1e8d3d..2f33964670 100644 --- a/Physlib/Mathematics/Tensors/LinearMap.lean +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -64,7 +64,7 @@ identity. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean index a0d44a2bc7..ab4862c7b1 100644 --- a/Physlib/Mathematics/Tensors/Metric.lean +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -66,7 +66,7 @@ Martin. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index d188adbe4b..c256663165 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -47,7 +47,7 @@ two indices by `twoIndexEquiv`. The components of a product are the products of ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 232c4d27c8..44d6649d57 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -49,7 +49,7 @@ reindexing, since that needs a metric. The index types ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean index 1b187f10e8..7597b81021 100644 --- a/Physlib/Mathematics/Tensors/Schouten.lean +++ b/Physlib/Mathematics/Tensors/Schouten.lean @@ -33,7 +33,7 @@ notation for both orientations of the delta. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean index 06d4077130..bd139b7539 100644 --- a/Physlib/Mathematics/Tensors/Tensorial.lean +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -48,7 +48,7 @@ for a field strength in `Vector ⊗ Vector`. ## iv. References -There are no known references for the material in this module. +* None. -/ diff --git a/Physlib/Relativity/Tensors/RealTensor/Species.lean b/Physlib/Relativity/Tensors/RealTensor/Species.lean index 6cf92922a8..212b427bd6 100644 --- a/Physlib/Relativity/Tensors/RealTensor/Species.lean +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -78,7 +78,7 @@ and one lower index respectively (`Tensorial`). A vector transforms by `Λ *ᵥ ## iv. References -There are no known references for the material in this module. +* None. -/ From 7e78a436464cebb5ecc9aa773a8c726518b58420 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:34:09 +0000 Subject: [PATCH 192/242] docs(Tensors): name the lowered index in the Tensor overview example Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 130164fd6a..eed98d05e1 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -30,8 +30,8 @@ product basis. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. A Lorentz -transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the second index with the metric, -`T^μ_ν = η_νρ T^μρ`, is a linear map at the index `1` that changes its color to `⟨vec, down⟩`. +transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the index `ν` with the metric, +`T^μ_ν = η_νρ T^μρ`, is a linear map at that one index, which changes its color to `⟨vec, down⟩`. ## ii. Key results From 3b0ac8da8432fc525bf724d1361a7a3b76e942b5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:35:56 +0000 Subject: [PATCH 193/242] docs(Tensors): the family of vectors of a pure tensor in the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index eed98d05e1..3fb20112e5 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -73,8 +73,9 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → ## A. Tensors -`Tensor S c` is `⨂[k] i, S.V (c i)`. A pure tensor `S.pure f` takes one vector `f i` at each -index, as `u ⊗ α`. Most tensors are not pure, but every tensor is a linear combination of pure +`Tensor S c` is `⨂[k] i, S.V (c i)`. A pure tensor `S.pure f` is given by a family `f` of vectors, +one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector `u` at an up index +and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). `ext_pure` is an `ext` lemma, so `ext p q` also reduces maps out of products `Tensor S c ⊗ Tensor S c₂`, and From 42e17f03bb719c637fb609764829bdb63fe9e703 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:36:23 +0000 Subject: [PATCH 194/242] style(Tensors): line lengths in section A of the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 3fb20112e5..923b766afe 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -75,11 +75,11 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → `Tensor S c` is `⨂[k] i, S.V (c i)`. A pure tensor `S.pure f` is given by a family `f` of vectors, one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector `u` at an up index -and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear combination of pure -tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a -property of all tensors follows from the pure case (`inductionOn_pure`). `ext_pure` is an `ext` -lemma, so `ext p q` also reduces maps out of products `Tensor S c ⊗ Tensor S c₂`, and -intertwining maps, to pure tensors. +and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear +combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors +(`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). +`ext_pure` is an `ext` lemma, so `ext p q` also reduces maps out of products +`Tensor S c ⊗ Tensor S c₂`, and intertwining maps, to pure tensors. -/ From d5c08a2f6f6e43fbb6e7264e98ae1eff568091df Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:42:17 +0000 Subject: [PATCH 195/242] docs(Tensors): what ext leaves as the goal, in section A of the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 923b766afe..337e979e75 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -78,8 +78,10 @@ one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). -`ext_pure` is an `ext` lemma, so `ext p q` also reduces maps out of products -`Tensor S c ⊗ Tensor S c₂`, and intertwining maps, to pure tensors. +`ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemmas +for `⊗` and for intertwining maps. For two maps out of a product `Tensor S c ⊗ Tensor S c₂`, +`ext p q` leaves the goal on `S.pure p ⊗ₜ S.pure q`. For two intertwining maps out of tensors, +`ext p` leaves the goal on `S.pure p`. -/ From 5d68721c204f75ee3bdbd70f766ad85dcac6f6d0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:43:27 +0000 Subject: [PATCH 196/242] docs(Tensors): the families of vectors p and q in section A of the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 337e979e75..fa22c5d48b 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -79,9 +79,10 @@ and the covector `α` at a down index. Most tensors are not pure, but every tens combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). `ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemmas -for `⊗` and for intertwining maps. For two maps out of a product `Tensor S c ⊗ Tensor S c₂`, -`ext p q` leaves the goal on `S.pure p ⊗ₜ S.pure q`. For two intertwining maps out of tensors, -`ext p` leaves the goal on `S.pure p`. +for `⊗` and for intertwining maps. An equation of two maps out of a product +`Tensor S c ⊗ Tensor S c₂` becomes an equation of their values on `S.pure p ⊗ₜ S.pure q`, for any +family of vectors `p` on the indices of the first factor and `q` on those of the second. An +equation of two intertwining maps out of tensors becomes one on `S.pure p`, for any family `p`. -/ From 69841516d4c42c610c41cb83910cb94e7ab160bf Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:46:37 +0000 Subject: [PATCH 197/242] docs(Tensors): ext on products of tensors in section C of the Product module doc, not in Tensor Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Product.lean | 6 ++++++ Physlib/Mathematics/Tensors/Tensor.lean | 8 +++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean index c256663165..8ef902ea8e 100644 --- a/Physlib/Mathematics/Tensors/Product.lean +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -135,6 +135,12 @@ lemma tmulEquiv_pure (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (f₁ ## C. Associativity, symmetry and equivariance +Each law is an equality of two maps out of a tensor product of spaces of tensors, such as +`(Tensor S c₁ ⊗ Tensor S c₂) ⊗ Tensor S c₃` for associativity. The tactic `ext` reduces it to pure +tensors in every factor, through Mathlib's `ext` lemma for `⊗` and `ext_pure`. For associativity, +`ext f₁ f₂ f₃` leaves the equality on `(S.pure f₁ ⊗ₜ S.pure f₂) ⊗ₜ S.pure f₃`, with one family of +vectors `fᵢ` for each factor. + -/ /-- The product is associative, `(T ⊗ U) ⊗ W = T ⊗ (U ⊗ W)`, after the indices of the left side diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index fa22c5d48b..de8f3c0022 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -78,11 +78,9 @@ one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). -`ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemmas -for `⊗` and for intertwining maps. An equation of two maps out of a product -`Tensor S c ⊗ Tensor S c₂` becomes an equation of their values on `S.pure p ⊗ₜ S.pure q`, for any -family of vectors `p` on the indices of the first factor and `q` on those of the second. An -equation of two intertwining maps out of tensors becomes one on `S.pure p`, for any family `p`. +`ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemma +for intertwining maps. An equality of two intertwining maps out of tensors becomes an equality of +their values on `S.pure p`, for any family of vectors `p`. -/ From 0afeb330f4adad5b44225d41dac7a16f74ef9f0c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:48:23 +0000 Subject: [PATCH 198/242] docs(Tensors): linear maps out of tensors in the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index de8f3c0022..95dfd07239 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -36,7 +36,7 @@ transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the ind ## ii. Key results - `Tensor` is the space of tensors, and `Species.pure` is a pure tensor. -- `Species.ext_pure` proves that linear maps of tensors that agree on pure tensors are equal. +- `Species.ext_pure` proves that linear maps out of tensors that agree on pure tensors are equal. - `Species.tensorRep` is the group action on tensors, and `Species.smul_pure` gives it on a pure tensor. - `Species.map_smul_of_pure` proves that a map that commutes with the group on pure tensors @@ -76,8 +76,9 @@ variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → `Tensor S c` is `⨂[k] i, S.V (c i)`. A pure tensor `S.pure f` is given by a family `f` of vectors, one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector `u` at an up index and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear -combination of pure tensors. So a linear map of tensors is fixed by its values on pure tensors -(`ext_pure`), and a property of all tensors follows from the pure case (`inductionOn_pure`). +combination of pure tensors. So a linear map out of tensors, to any space, is fixed by its values +on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case +(`inductionOn_pure`). `ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemma for intertwining maps. An equality of two intertwining maps out of tensors becomes an equality of their values on `S.pure p`, for any family of vectors `p`. From d50a5bff2f2b71e67fd29d3bd58288d891df3a43 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:49:58 +0000 Subject: [PATCH 199/242] docs(Tensors): Tensor docstring, what the def shows in goals Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 95dfd07239..f575b868fe 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -85,9 +85,9 @@ their values on `S.pure p`, for any family of vectors `p`. -/ -/-- The space `⨂[k] i, S.V (c i)` of tensors with colors `c` on the indices `ι`. It is a `def`, not -an `abbrev`, so that the type `Tensor S c` keeps the species and the colors instead of unfolding to -`⨂[k] i, S.V (c i)`. Mathlib's constructions on `⨂` apply to it by definitional unfolding. -/ +/-- The tensors with colors `c : ι → Color K`, the space `⨂[k] i, S.V (c i)`. It is a `def`, not an +`abbrev`, so that goals show `Tensor S c` instead of the product. Mathlib's results on `⨂` still +apply to it, since Lean unfolds the definition. -/ def _root_.Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := ⨂[k] i, S.V (c i) From 98f9a45301e6d87ad32454c09cb83798e203ab5b Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:52:15 +0000 Subject: [PATCH 200/242] docs(Tensors): Species.pure docstring, the family f and spanning Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index f575b868fe..1cd548e02e 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -101,8 +101,9 @@ noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Colo variable {ι : Type} {c : ι → Color K} -/-- The pure tensor of a family of vectors, one per index, such as `u ⊗ α ⊗ β` with components -`u^μ α_ν β_ρ`. It is Mathlib's `⨂ₜ[k] i, f i`. -/ +/-- The pure tensor `⨂ₜ i, f i` of a family `f` of vectors, with `f i` in `S.V (c i)` at each +index `i`, such as `u ⊗ α` with the components `u^μ α_ν`. Not every tensor is pure, but every +tensor is a linear combination of pure tensors. -/ noncomputable def pure (S : Species k G K basisIdx) (f : Π i, S.V (c i)) : Tensor S c := ⨂ₜ[k] i, f i From 5e5f13548433a457177135304dabca3bb65bf0b7 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:54:46 +0000 Subject: [PATCH 201/242] docs(Tensors): maps that commute with the group in section B of the Tensor module doc, raising in the overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 1cd548e02e..84ae836848 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -23,10 +23,10 @@ tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so the in names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. A group element acts on a tensor at every index, by the representation of its color. A linear map -at one index changes that index and keeps the others, as lowering an index with a metric does. -Both are linear maps fixed by their values on pure tensors, which take one vector at each index, so -their properties are proved on pure tensors. The components of a tensor are its coordinates in the -product basis. +at one index changes that index and keeps the others, as lowering or raising an index with a metric +does. Both are linear maps fixed by their values on pure tensors, which take one vector at each +index, so their properties are proved on pure tensors. The components of a tensor are its +coordinates in the product basis. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. A Lorentz @@ -131,11 +131,14 @@ A group element `Λ` acts on every index by the representation of its color, `Representation.piTprod` on `⨂[k] i, S.V (c i)`. It is written `Λ • T` under `open scoped Tensor`. -Maps of tensors that commute with the group are Mathlib's `Representation.Equiv` and -`Representation.IntertwiningMap` for `tensorRep`, and `equiv_smul` and `intertwiningMap_smul` give -`φ (Λ • T) = Λ • φ T`. A map that commutes with `Λ` on pure tensors commutes with it on all tensors -(`map_smul_of_pure`), and `equivOfPure` builds an equivalence of representations from that check. -A tensor with no indices is invariant (`smul_eq_self_of_isEmpty`). +A map `φ` of tensors commutes with the group when `φ (Λ • T) = Λ • φ T`, as reindexing, products, +contraction, lowering and raising do. Such maps are Mathlib's `Representation.IntertwiningMap` for +`tensorRep`, or `Representation.Equiv` when they are invertible, so they compose and send invariant +tensors to invariant tensors. The commutation is checked on pure tensors only, where `Λ` acts on +each vector. `map_smul_of_pure` extends the check to all tensors, and `equivOfPure` turns a linear +equivalence with this check into an equivalence of representations. `equiv_smul` and +`intertwiningMap_smul` state the commutation with `•`. A tensor with no indices, a scalar, is +invariant (`smul_eq_self_of_isEmpty`). -/ From d4e7ea0e45c3c9724e195c0c3ba632792b860950 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:57:52 +0000 Subject: [PATCH 202/242] docs(Tensors): lowering and contraction as examples in section B of the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 84ae836848..4bdcb2dfa3 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -134,7 +134,10 @@ A group element `Λ` acts on every index by the representation of its color, A map `φ` of tensors commutes with the group when `φ (Λ • T) = Λ • φ T`, as reindexing, products, contraction, lowering and raising do. Such maps are Mathlib's `Representation.IntertwiningMap` for `tensorRep`, or `Representation.Equiv` when they are invertible, so they compose and send invariant -tensors to invariant tensors. The commutation is checked on pure tensors only, where `Λ` acts on +tensors to invariant tensors. For example, lowering the index `ν` of `T^μν` is an equivalence +`(S.tensorRep c).Equiv (S.tensorRep c')` from the colors `c = ![⟨vec, up⟩, ⟨vec, up⟩]` to +`c' = ![⟨vec, up⟩, ⟨vec, down⟩]`. The contraction `T^μ_ν ↦ T^μ_μ` is an intertwining map to the +tensors with no indices, and it has no inverse. The commutation is checked on pure tensors only, where `Λ` acts on each vector. `map_smul_of_pure` extends the check to all tensors, and `equivOfPure` turns a linear equivalence with this check into an equivalence of representations. `equiv_smul` and `intertwiningMap_smul` state the commutation with `•`. A tensor with no indices, a scalar, is From 4d257bf678311f56236457855e5a5339b75e6df6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 15:58:14 +0000 Subject: [PATCH 203/242] style(Tensors): line lengths in section B of the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 4bdcb2dfa3..25d213251b 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -137,11 +137,12 @@ contraction, lowering and raising do. Such maps are Mathlib's `Representation.In tensors to invariant tensors. For example, lowering the index `ν` of `T^μν` is an equivalence `(S.tensorRep c).Equiv (S.tensorRep c')` from the colors `c = ![⟨vec, up⟩, ⟨vec, up⟩]` to `c' = ![⟨vec, up⟩, ⟨vec, down⟩]`. The contraction `T^μ_ν ↦ T^μ_μ` is an intertwining map to the -tensors with no indices, and it has no inverse. The commutation is checked on pure tensors only, where `Λ` acts on -each vector. `map_smul_of_pure` extends the check to all tensors, and `equivOfPure` turns a linear -equivalence with this check into an equivalence of representations. `equiv_smul` and -`intertwiningMap_smul` state the commutation with `•`. A tensor with no indices, a scalar, is -invariant (`smul_eq_self_of_isEmpty`). +tensors with no indices, and it has no inverse. + +The commutation is checked on pure tensors only, where `Λ` acts on each vector. `map_smul_of_pure` +extends the check to all tensors, and `equivOfPure` turns a linear equivalence with this check into +an equivalence of representations. `equiv_smul` and `intertwiningMap_smul` state the commutation +with `•`. A tensor with no indices, a scalar, is invariant (`smul_eq_self_of_isEmpty`). -/ From 80c2ddf420323856dd4da50d51332bedccbb4611 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 16:08:23 +0000 Subject: [PATCH 204/242] docs(Tensors): the type of a tensor fixes how each index transforms, in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 25d213251b..6c46dfc4af 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -17,10 +17,12 @@ index type `ι`, and the group acts on them index by index. ## i. Overview -The tensors of a species `S` with colors `c : ι → Color K` form the space `Tensor S c`, which is -Mathlib's `⨂[k] i, S.V (c i)`. The index type `ι` is any type, such as `Fin n`. The product of -tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so the indices of each factor keep their -names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. +A tensor `T : Tensor S c` lies in Mathlib's space `⨂[k] i, S.V (c i)`, and its type also fixes how +each index transforms, by the representation `S.rep (c i)` of the color of that index. Mathlib's +`⨂` is only a vector space, with no rule for how its factors transform. The index type `ι` is any +type, such as `Fin n`. The product of tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so +the indices of each factor keep their names. The colors of the product are those of the factors, +with no renumbering into `Fin (n + m)`. A group element acts on a tensor at every index, by the representation of its color. A linear map at one index changes that index and keeps the others, as lowering or raising an index with a metric From 0fc2cb1a16f108883705578be0c514af8da8b80f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 16:09:03 +0000 Subject: [PATCH 205/242] docs(Tensors): Species.distribMulAction docstring without the Mathlib examples Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 6c46dfc4af..48ffcfe0a4 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -156,9 +156,8 @@ colors. Maps of tensors that commute with the action are Mathlib's `Representati noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor S c) := Representation.piTprod fun i => S.rep (c i) -/-- The action `Λ • T` of the group on tensors, `S.tensorRep c Λ T`. The instance is scoped to -`Tensor`, and has high priority there, since Mathlib gives some groups, such as `Circle` and `ℂˣ`, -a scalar action on every `ℂ`-module. -/ +/-- The action `Λ • T` of the group on tensors, `S.tensorRep c Λ T`, available under +`open scoped Tensor`, where it takes priority over Mathlib's scalar actions. -/ noncomputable abbrev distribMulAction : DistribMulAction G (Tensor S c) := DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ (S.tensorRep c) From 44638d1efe597a5dfa65e7d0445e83c1649c2de0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 16:10:41 +0000 Subject: [PATCH 206/242] docs(Tensors): why the scoped action on tensors has priority 1100 Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 48ffcfe0a4..68ec5c6b59 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -161,6 +161,8 @@ noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor noncomputable abbrev distribMulAction : DistribMulAction G (Tensor S c) := DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ (S.tensorRep c) +-- Priority 1100, above the default 1000, so that `Λ • T` is the action on each index and not the +-- scalar action that Mathlib gives `Circle` and `ℂˣ` on every `ℂ`-module. The same for `smul`. scoped[Tensor] attribute [instance 1100] Tensor.Species.distribMulAction /-- The scalar multiplication of `distribMulAction`, found before any scalar action of Mathlib. -/ From 42e69f15831e5996a9d05f650644c77b4c805079 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 16:14:27 +0000 Subject: [PATCH 207/242] docs(Tensors): the Circle action in the comment on the priority of the tensor action Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 68ec5c6b59..cf9f083dab 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -161,8 +161,9 @@ noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor noncomputable abbrev distribMulAction : DistribMulAction G (Tensor S c) := DistribMulAction.compHom (M := Module.End k (Tensor S c)) _ (S.tensorRep c) --- Priority 1100, above the default 1000, so that `Λ • T` is the action on each index and not the --- scalar action that Mathlib gives `Circle` and `ℂˣ` on every `ℂ`-module. The same for `smul`. +-- A `Circle` element `z` is a complex number, and Mathlib lets it act on every complex vector space +-- as multiplication by `z`. Priority 1100, above the default 1000, makes `z • T` the action of the +-- species instead. The same for `smul`. scoped[Tensor] attribute [instance 1100] Tensor.Species.distribMulAction /-- The scalar multiplication of `distribMulAction`, found before any scalar action of Mathlib. -/ From 16e983fa5ac3083cd28d063b5719cfca103b74ad Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:01:56 +0000 Subject: [PATCH 208/242] docs(Tensors): tensorBasis_repr_smul docstring with the matrix of each index and the Lorentz example Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index cf9f083dab..3086677f84 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -279,8 +279,9 @@ lemma finrank_tensor [Fintype ι] (c : ι → Color K) : Module.finrank_piTensorProduct.trans (Finset.prod_congr rfl fun i _ => Module.finrank_eq_card_basis (S.basis (c i))) -/-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, g^{φ i}_{ψ i}) t^ψ`, with one matrix entry of -`g` per index, in the color of that index. -/ +/-- The components of `g • t`, `(g t)^φ = ∑ ψ, (∏ i, M_i (φ i) (ψ i)) t^ψ`, where `M_i` is the +matrix of `g` in the color of the index `i`. For the colors `![⟨vec, up⟩, ⟨vec, down⟩]` this is +`(ΛT)^μ_ν = Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ`. -/ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S c) (φ : Π i, basisIdx (c i).kind) : (S.tensorBasis c).repr (g • t) φ = ∑ ψ, (∏ i, LinearMap.toMatrix (S.basis (c i)) From 7c70cb9dade5d8262fcba2745af441b070bae102 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:26:24 +0000 Subject: [PATCH 209/242] docs(Tensors): section D of the Tensor module doc, the uses of mapAt and congrAt first Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 30 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 3086677f84..ff92847e6c 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -294,17 +294,25 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S ## D. Linear maps at the indices -`mapIndices f` applies a linear map `f x` at every index `x`, `u ⊗ α ↦ f₀ u ⊗ f₁ α`. It is -Mathlib's `PiTensorProduct.map`, and it commutes with the group when every `f x` does -(`mapIndices_smul`). `congrIndices` is the same for equivalences of representations. - -`mapAt i f h` applies `f` at the single index `i` and keeps the others, `u ⊗ α ↦ u ⊗ f α`. The map -`f` may change the color at `i`, so the colors `c'` of the result differ from `c` there, and -`h : ∀ x, x ≠ i → c x = c' x` says they agree elsewhere. The vectors at the other indices are -moved along `h` by `S.cast`. `indexMapsAt` is this family of maps, `congrAt` is `mapAt` of an -equivalence of representations, and `tensorBasis_repr_mapAt` gives the components, -`(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…`. Lowering an index with a metric is `congrAt` of the map -`v^μ ↦ η_νμ v^μ`, in `Metric.lean`. +Many operations change one index of a tensor and keep the others, such as contracting the index +with a tensor on two indices, `T^…μ… F_μ^ν`, and lowering or raising it with a metric, +`T^μν ↦ T^μ_ν`. `mapAt i f h` applies any linear map `f` at the index `i`, `u ⊗ v ↦ u ⊗ f v`. +`congrAt i f h` applies an equivalence of representations `f` in the same way, and the result is +again an equivalence of representations, so it commutes with the group and has an inverse. +Contracting with `F` is `mapAt` of `F` read as a linear map (`contr_matMulAt`), and lowering is +`congrAt` of the map `v^μ ↦ η_νμ v^μ`. + +The map `f` may change the color at `i`, as lowering turns `⟨vec, up⟩` into `⟨vec, down⟩`. So the +result has other colors `c'`, and `h : ∀ x, x ≠ i → c x = c' x` says that they agree with `c` at +the other indices. There the vectors are moved along `h` by `S.cast`. + +In components, `mapAt` acts on the one index, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` +(`tensorBasis_repr_mapAt`). + +`mapAt` is built from `mapIndices f`, which applies a linear map `f x` at every index `x`, +`u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`, and it commutes with the group when +every `f x` does (`mapIndices_smul`). `congrIndices` is the same for equivalences of +representations, and `indexMapsAt i f h` is the family with `f` at `i` and `S.cast` elsewhere. -/ From 3b91fe6f9332836851f890b95d50835f8322d5e0 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:33:03 +0000 Subject: [PATCH 210/242] docs(Tensors): subsections B.1, B.2 and D.1 to D.4 in the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 95 +++++++++++++++++-------- 1 file changed, 67 insertions(+), 28 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index ff92847e6c..77c84ea5c4 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -51,8 +51,14 @@ transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the ind - A. Tensors - B. The group action + - B.1. The action on tensors + - B.2. Maps that commute with the group - C. The basis of tensors - D. Linear maps at the indices + - D.1. A linear map at every index + - D.2. A linear map at one index + - D.3. Components + - D.4. An equivalence of representations at one index ## iv. References @@ -128,24 +134,13 @@ lemma ext_pure {E : Type*} [AddCommMonoid E] [Module k E] {f g : Tensor S c → ## B. The group action +### B.1. The action on tensors + A group element `Λ` acts on every index by the representation of its color, `Λ (u ⊗ α) = Λu ⊗ Λα` (`smul_pure`). This is the representation `S.tensorRep c`, `Representation.piTprod` on `⨂[k] i, S.V (c i)`. It is written `Λ • T` under `open scoped Tensor`. -A map `φ` of tensors commutes with the group when `φ (Λ • T) = Λ • φ T`, as reindexing, products, -contraction, lowering and raising do. Such maps are Mathlib's `Representation.IntertwiningMap` for -`tensorRep`, or `Representation.Equiv` when they are invertible, so they compose and send invariant -tensors to invariant tensors. For example, lowering the index `ν` of `T^μν` is an equivalence -`(S.tensorRep c).Equiv (S.tensorRep c')` from the colors `c = ![⟨vec, up⟩, ⟨vec, up⟩]` to -`c' = ![⟨vec, up⟩, ⟨vec, down⟩]`. The contraction `T^μ_ν ↦ T^μ_μ` is an intertwining map to the -tensors with no indices, and it has no inverse. - -The commutation is checked on pure tensors only, where `Λ` acts on each vector. `map_smul_of_pure` -extends the check to all tensors, and `equivOfPure` turns a linear equivalence with this check into -an equivalence of representations. `equiv_smul` and `intertwiningMap_smul` state the commutation -with `•`. A tensor with no indices, a scalar, is invariant (`smul_eq_self_of_isEmpty`). - -/ variable (S) in @@ -189,6 +184,25 @@ lemma smulCommClass' : SMulCommClass k G (Tensor S c) := scoped[Tensor] attribute [instance] Tensor.Species.smulCommClass' +/-! + +### B.2. Maps that commute with the group + +A map `φ` of tensors commutes with the group when `φ (Λ • T) = Λ • φ T`, as reindexing, products, +contraction, lowering and raising do. Such maps are Mathlib's `Representation.IntertwiningMap` for +`tensorRep`, or `Representation.Equiv` when they are invertible, so they compose and send invariant +tensors to invariant tensors. For example, lowering the index `ν` of `T^μν` is an equivalence +`(S.tensorRep c).Equiv (S.tensorRep c')` from the colors `c = ![⟨vec, up⟩, ⟨vec, up⟩]` to +`c' = ![⟨vec, up⟩, ⟨vec, down⟩]`. The contraction `T^μ_ν ↦ T^μ_μ` is an intertwining map to the +tensors with no indices, and it has no inverse. + +The commutation is checked on pure tensors only, where `Λ` acts on each vector. `map_smul_of_pure` +extends the check to all tensors, and `equivOfPure` turns a linear equivalence with this check into +an equivalence of representations. `equiv_smul` and `intertwiningMap_smul` state the commutation +with `•`. A tensor with no indices, a scalar, is invariant (`smul_eq_self_of_isEmpty`). + +-/ + /-- An intertwining map from or to tensors commutes with the actions, `φ (Λ v) = Λ φ v`. -/ lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : Tensor S c) : @@ -296,23 +310,15 @@ lemma tensorBasis_repr_smul [Fintype ι] [DecidableEq ι] (g : G) (t : Tensor S Many operations change one index of a tensor and keep the others, such as contracting the index with a tensor on two indices, `T^…μ… F_μ^ν`, and lowering or raising it with a metric, -`T^μν ↦ T^μ_ν`. `mapAt i f h` applies any linear map `f` at the index `i`, `u ⊗ v ↦ u ⊗ f v`. -`congrAt i f h` applies an equivalence of representations `f` in the same way, and the result is -again an equivalence of representations, so it commutes with the group and has an inverse. -Contracting with `F` is `mapAt` of `F` read as a linear map (`contr_matMulAt`), and lowering is -`congrAt` of the map `v^μ ↦ η_νμ v^μ`. - -The map `f` may change the color at `i`, as lowering turns `⟨vec, up⟩` into `⟨vec, down⟩`. So the -result has other colors `c'`, and `h : ∀ x, x ≠ i → c x = c' x` says that they agree with `c` at -the other indices. There the vectors are moved along `h` by `S.cast`. +`T^μν ↦ T^μ_ν`. They are linear maps at one index (D.2), built from linear maps at every index +(D.1). When the map at the index is an equivalence of representations, so is the map of tensors +(D.4). -In components, `mapAt` acts on the one index, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` -(`tensorBasis_repr_mapAt`). +### D.1. A linear map at every index -`mapAt` is built from `mapIndices f`, which applies a linear map `f x` at every index `x`, -`u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is Mathlib's `PiTensorProduct.map`, and it commutes with the group when -every `f x` does (`mapIndices_smul`). `congrIndices` is the same for equivalences of -representations, and `indexMapsAt i f h` is the family with `f` at `i` and `S.cast` elsewhere. +`mapIndices f` applies a linear map `f x` at every index `x`, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is +Mathlib's `PiTensorProduct.map`, and it commutes with the group when every `f x` does +(`mapIndices_smul`). `congrIndices` is the same for equivalences of representations. -/ @@ -370,6 +376,19 @@ lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x S.congrIndices f (S.pure p) = S.pure fun x => f x (p x) := Representation.Equiv.piTensorCongr_tprod f p +/-! + +### D.2. A linear map at one index + +`mapAt i f h` applies a linear map `f` at the index `i` and keeps the others, `u ⊗ v ↦ u ⊗ f v`. +The map `f` may change the color at `i`, as lowering turns `⟨vec, up⟩` into `⟨vec, down⟩`. So the +result has other colors `c'`, and `h : ∀ x, x ≠ i → c x = c' x` says that they agree with `c` at +the other indices. There the vectors are moved along `h` by `S.cast`. `indexMapsAt i f h` is the +family with `f` at `i` and `S.cast` elsewhere, and `mapAt` is `mapIndices` of it. Contracting with +a tensor `F` on two indices is `mapAt` of `F` read as a linear map (`contr_matMulAt`). + +-/ + section variable [DecidableEq ι] @@ -444,6 +463,15 @@ lemma mapAt_id (i : ι) (h : ∀ x, x ≠ i → c x = c x) : rw [mapAt, ← eq_indexMapsAt (F := fun _ => LinearMap.id) i _ h rfl fun _ _ => LinearMap.ext fun _ => rfl, mapIndices_id] +/-! + +### D.3. Components + +In components, `mapAt` acts on the one index, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` +(`tensorBasis_repr_mapAt`). + +-/ + /-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. -/ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) @@ -471,6 +499,17 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V rw [indexMapsAt_apply_ne _ _ _ hx, Function.update_of_ne hx, hψ x hx] exact basis_repr_cast _ _ _ +/-! + +### D.4. An equivalence of representations at one index + +`congrAt i f h` applies an equivalence of representations `f` at the index `i`, as `mapAt` does. +The result is again an equivalence of representations, so it commutes with the group and has an +inverse, `congrAt` of `f.symm` (`congrAt_symm`). Lowering with a metric is `congrAt` of the map +`v^μ ↦ η_νμ v^μ`, and raising is its inverse. + +-/ + variable (S) in /-- The equivalence of representations `f` at the index `i`, and the identity, moved along `h x`, at every other index `x`. On a pure tensor, `u ⊗ v ⊗ w ↦ u ⊗ f v ⊗ w`. Its linear map is `mapAt` From 9a95db2cc806074101e06674d40543ddddcfc5bf Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:41:13 +0000 Subject: [PATCH 211/242] docs(Tensors): cross-references between B.2 and D.4 in the Tensor module doc Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 77c84ea5c4..9323cda7f0 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -193,8 +193,8 @@ contraction, lowering and raising do. Such maps are Mathlib's `Representation.In `tensorRep`, or `Representation.Equiv` when they are invertible, so they compose and send invariant tensors to invariant tensors. For example, lowering the index `ν` of `T^μν` is an equivalence `(S.tensorRep c).Equiv (S.tensorRep c')` from the colors `c = ![⟨vec, up⟩, ⟨vec, up⟩]` to -`c' = ![⟨vec, up⟩, ⟨vec, down⟩]`. The contraction `T^μ_ν ↦ T^μ_μ` is an intertwining map to the -tensors with no indices, and it has no inverse. +`c' = ![⟨vec, up⟩, ⟨vec, down⟩]`, built by `congrAt` (D.4). The contraction `T^μ_ν ↦ T^μ_μ` is an +intertwining map to the tensors with no indices, and it has no inverse. The commutation is checked on pure tensors only, where `Λ` acts on each vector. `map_smul_of_pure` extends the check to all tensors, and `equivOfPure` turns a linear equivalence with this check into @@ -505,7 +505,8 @@ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V `congrAt i f h` applies an equivalence of representations `f` at the index `i`, as `mapAt` does. The result is again an equivalence of representations, so it commutes with the group and has an -inverse, `congrAt` of `f.symm` (`congrAt_symm`). Lowering with a metric is `congrAt` of the map +inverse, `congrAt` of `f.symm` (`congrAt_symm`). It is built by `equivOfPure` (B.2), with the +check on pure tensors from `mapIndices_smul`. Lowering with a metric is `congrAt` of the map `v^μ ↦ η_νμ v^μ`, and raising is its inverse. -/ From 5f6d626992d3af1fa0706d10bc04d306b7e9d5ee Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:57:10 +0000 Subject: [PATCH 212/242] docs(Tensors): the two kinds of map and what is built from them, in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 9323cda7f0..9fca9697b1 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -24,16 +24,20 @@ type, such as `Fin n`. The product of tensors on the indices `ι₁` and `ι₂` the indices of each factor keep their names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. -A group element acts on a tensor at every index, by the representation of its color. A linear map -at one index changes that index and keeps the others, as lowering or raising an index with a metric -does. Both are linear maps fixed by their values on pure tensors, which take one vector at each -index, so their properties are proved on pure tensors. The components of a tensor are its -coordinates in the product basis. - For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, -and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. A Lorentz -transformation acts as `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`. Lowering the index `ν` with the metric, -`T^μ_ν = η_νρ T^μρ`, is a linear map at that one index, which changes its color to `⟨vec, down⟩`. +and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. + +The operations on tensors in the rest of the library are built from two kinds of linear map defined +here. A map at every index acts on each index, as a Lorentz transformation does, +`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index +changes that index and keeps the others, as lowering the index `ν` with the metric does, +`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Reindexing, raising an index and +contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from these maps, +and so is the tactic `tensor_indices`. + +Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition +and commuting with the group, are proved once on pure tensors here, and not again for each +operation. The components of a tensor are its coordinates in the product basis. ## ii. Key results From 632a478fd7fe698be1e778d59c89b916816886c7 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 17:58:34 +0000 Subject: [PATCH 213/242] docs(Tensors): section references in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 9fca9697b1..ea31df7b36 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -17,27 +17,27 @@ index type `ι`, and the group acts on them index by index. ## i. Overview -A tensor `T : Tensor S c` lies in Mathlib's space `⨂[k] i, S.V (c i)`, and its type also fixes how -each index transforms, by the representation `S.rep (c i)` of the color of that index. Mathlib's +A tensor `T : Tensor S c` lies in Mathlib's space `⨂[k] i, S.V (c i)` (A), and its type also fixes +how each index transforms, by the representation `S.rep (c i)` of the color of that index. Mathlib's `⨂` is only a vector space, with no rule for how its factors transform. The index type `ι` is any type, such as `Fin n`. The product of tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so the indices of each factor keep their names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, -and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. +and for two vectors `u` and `v` the pure tensor `u ⊗ v` (A) has the components `u^μ v^ν` (C). The operations on tensors in the rest of the library are built from two kinds of linear map defined -here. A map at every index acts on each index, as a Lorentz transformation does, -`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index -changes that index and keeps the others, as lowering the index `ν` with the metric does, -`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Reindexing, raising an index and -contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from these maps, -and so is the tactic `tensor_indices`. - -Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition -and commuting with the group, are proved once on pure tensors here, and not again for each -operation. The components of a tensor are its coordinates in the product basis. +here. A map at every index (D.1) acts on each index, as a Lorentz transformation does, +`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index (B.1). A map at one +index (D.2) changes that index and keeps the others, as lowering the index `ν` with the metric +does, `T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩` (D.4). Reindexing, raising an +index and contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from +these maps, and so is the tactic `tensor_indices`. + +Each map is fixed by its values on pure tensors such as `u ⊗ v` (A). So its laws, such as +composition and commuting with the group (B.2), are proved once on pure tensors here, and not again +for each operation. The components of a tensor are its coordinates in the product basis (C). ## ii. Key results From b545450ce2ebb686dd5e3bd3962c07ef82cf3a8e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 18:02:16 +0000 Subject: [PATCH 214/242] docs(Tensors): fewer section references and no tactic in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index ea31df7b36..eb1b005d1a 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -17,27 +17,26 @@ index type `ι`, and the group acts on them index by index. ## i. Overview -A tensor `T : Tensor S c` lies in Mathlib's space `⨂[k] i, S.V (c i)` (A), and its type also fixes -how each index transforms, by the representation `S.rep (c i)` of the color of that index. Mathlib's +A tensor `T : Tensor S c` lies in Mathlib's space `⨂[k] i, S.V (c i)`, and its type also fixes how +each index transforms, by the representation `S.rep (c i)` of the color of that index. Mathlib's `⨂` is only a vector space, with no rule for how its factors transform. The index type `ι` is any type, such as `Fin n`. The product of tensors on the indices `ι₁` and `ι₂` lives on `ι₁ ⊕ ι₂`, so the indices of each factor keep their names. The colors of the product are those of the factors, with no renumbering into `Fin (n + m)`. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, -and for two vectors `u` and `v` the pure tensor `u ⊗ v` (A) has the components `u^μ v^ν` (C). +and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. The operations on tensors in the rest of the library are built from two kinds of linear map defined here. A map at every index (D.1) acts on each index, as a Lorentz transformation does, -`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index (B.1). A map at one -index (D.2) changes that index and keeps the others, as lowering the index `ν` with the metric -does, `T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩` (D.4). Reindexing, raising an -index and contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from -these maps, and so is the tactic `tensor_indices`. - -Each map is fixed by its values on pure tensors such as `u ⊗ v` (A). So its laws, such as -composition and commuting with the group (B.2), are proved once on pure tensors here, and not again -for each operation. The components of a tensor are its coordinates in the product basis (C). +`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index +(D.2) changes that index and keeps the others, as lowering the index `ν` with the metric does, +`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Reindexing, raising an index and +contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from these maps. + +Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition +and commuting with the group (B.2), are proved once on pure tensors here, and not again for each +operation. The components of a tensor are its coordinates in the product basis (C). ## ii. Key results From 38f784516034d0a094d7c2a176af785e336e2f32 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 18:04:21 +0000 Subject: [PATCH 215/242] docs(Tensors): lowering as an equivalence (D.4) and components at one index (D.3) in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index eb1b005d1a..15de13e7c3 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -31,12 +31,15 @@ The operations on tensors in the rest of the library are built from two kinds of here. A map at every index (D.1) acts on each index, as a Lorentz transformation does, `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index (D.2) changes that index and keeps the others, as lowering the index `ν` with the metric does, -`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Reindexing, raising an index and -contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also built from these maps. +`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Since the metric is invertible and +commutes with the group, lowering is an equivalence of representations, with raising as its inverse +(D.4). Reindexing and contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also +built from these maps. Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition and commuting with the group (B.2), are proved once on pure tensors here, and not again for each -operation. The components of a tensor are its coordinates in the product basis (C). +operation. The components of a tensor are its coordinates in the product basis (C), and a map at +one index acts on the components through that index alone, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` (D.3). ## ii. Key results From 6458ef367669ba1701b5afa1d0a364d45148c7f5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 18:08:18 +0000 Subject: [PATCH 216/242] docs(Tensors): correctness and clarity review of the Tensor module doc and docstrings Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 15de13e7c3..a94d82ec10 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -12,8 +12,8 @@ public import Physlib.Mathematics.ForMathlib.Representation # Tensors of a species -The tensors of a species with colors `c : ι → Color K` form Mathlib's `⨂[k] i, V (c i)`, for any -index type `ι`, and the group acts on them index by index. +The tensors of a species `S` with colors `c : ι → Color K`, on Mathlib's `⨂[k] i, S.V (c i)` for +any index type `ι`, and the group action on them index by index. ## i. Overview @@ -32,13 +32,12 @@ here. A map at every index (D.1) acts on each index, as a Lorentz transformation `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index (D.2) changes that index and keeps the others, as lowering the index `ν` with the metric does, `T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Since the metric is invertible and -commutes with the group, lowering is an equivalence of representations, with raising as its inverse -(D.4). Reindexing and contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, are also -built from these maps. +invariant under the group, lowering is an equivalence of representations, with raising as its +inverse (D.4). Reindexing uses the map at every index, and contracting an index with a tensor on +two indices, `T^…μ… F_μ^ν`, is a map at that index. Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition -and commuting with the group (B.2), are proved once on pure tensors here, and not again for each -operation. The components of a tensor are its coordinates in the product basis (C), and a map at +and commuting with the group (B.2), are proved once here, and not again for each operation. The components of a tensor are its coordinates in the product basis (C), and a map at one index acts on the components through that index alone, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` (D.3). ## ii. Key results @@ -92,10 +91,9 @@ one vector `f i` in `S.V (c i)` at each index `i`, as `u ⊗ α` with the vector and the covector `α` at a down index. Most tensors are not pure, but every tensor is a linear combination of pure tensors. So a linear map out of tensors, to any space, is fixed by its values on pure tensors (`ext_pure`), and a property of all tensors follows from the pure case -(`inductionOn_pure`). -`ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with Mathlib's `ext` lemma -for intertwining maps. An equality of two intertwining maps out of tensors becomes an equality of -their values on `S.pure p`, for any family of vectors `p`. +(`inductionOn_pure`). `ext_pure` is an `ext` lemma, so the tactic `ext` applies it, together with +Mathlib's `ext` lemma for intertwining maps. An equality of two intertwining maps out of tensors +becomes an equality of their values on `S.pure p`, for any family of vectors `p`. -/ @@ -209,7 +207,7 @@ with `•`. A tensor with no indices, a scalar, is invariant (`smul_eq_self_of_i -/ -/-- An intertwining map from or to tensors commutes with the actions, `φ (Λ v) = Λ φ v`. -/ +/-- An intertwining map between tensors commutes with the actions, `φ (Λ T) = Λ φ T`. -/ lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := @@ -478,7 +476,7 @@ In components, `mapAt` acts on the one index, `(f T)^…ν… = ∑ μ, f^ν_μ -/ -/-- `mapAt` in components, `(f t)^…ν… = ∑ μ, f^ν_μ t^…μ…`, with `f^ν_μ` the matrix of `f`. -/ +/-- `mapAt` in components, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…`, with `f^ν_μ` the matrix of `f`. -/ lemma tensorBasis_repr_mapAt [Fintype ι] (i : ι) (f : S.V (c i) →ₗ[k] S.V (c' i)) (h : ∀ x, x ≠ i → c x = c' x) (t : Tensor S c) (φ : Π x, basisIdx (c' x).kind) (ψ : Π x, basisIdx (c x).kind) From 43e00e191a15e9f63a86dfe801accfb409d85213 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 18:08:47 +0000 Subject: [PATCH 217/242] style(Tensors): line lengths in the Tensor overview Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/Tensors/Tensor.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index a94d82ec10..bbe376175d 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -37,8 +37,9 @@ inverse (D.4). Reindexing uses the map at every index, and contracting an index two indices, `T^…μ… F_μ^ν`, is a map at that index. Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition -and commuting with the group (B.2), are proved once here, and not again for each operation. The components of a tensor are its coordinates in the product basis (C), and a map at -one index acts on the components through that index alone, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` (D.3). +and commuting with the group (B.2), are proved once here, and not again for each operation. The +components of a tensor are its coordinates in the product basis (C), and a map at one index acts on +the components through that index alone, `(f T)^…ν… = ∑ μ, f^ν_μ T^…μ…` (D.3). ## ii. Key results From eea3482203da9537462628608a4304ec8063034e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Thu, 12 Nov 2026 18:13:14 +0000 Subject: [PATCH 218/242] refactor(ForMathlib): rename Representation.Equiv.piTensorCongr, piTensorReindex and IsIntertwiningMap.piTensorMap to piTprodCongr, piTprodReindex and piTprodMap Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 22 +++++++++---------- Physlib/Mathematics/Tensors/Reindexing.lean | 6 ++--- Physlib/Mathematics/Tensors/Tensor.lean | 8 +++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 0ca2c7724e..82b8daef1f 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -44,9 +44,9 @@ intertwining. from the trivial representation picks out an invariant vector. - `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. - `Representation.piTprod` : the tensor product of a family of representations, with - `Representation.IsIntertwiningMap.piTensorMap` : `PiTensorProduct.map` of intertwining maps is - intertwining, `Representation.Equiv.piTensorCongr` : the tensor product of a family of - equivalences of representations, `Representation.Equiv.piTensorReindex` : reindexing the factors, + `Representation.IsIntertwiningMap.piTprodMap` : `PiTensorProduct.map` of intertwining maps is + intertwining, `Representation.Equiv.piTprodCongr` : the tensor product of a family of + equivalences of representations, `Representation.Equiv.piTprodReindex` : reindexing the factors, `Representation.Equiv.piTprodSum` : the product of the two halves of a family over `ι₁ ⊕ ι₂`, `Representation.Equiv.piTprodOfSubsingleton` : a family over a subsingleton index type, and `Representation.IsIntertwiningMap.contrPairs` : the contraction of invariantly paired factors is @@ -142,7 +142,7 @@ lemma Representation.piTprod_tprod (ρ : Π i, Representation k G (M i)) (g : G) /-- `PiTensorProduct.map f` intertwines `piTprod ρ` and `piTprod σ` when every `f i` intertwines `ρ i` and `σ i`. -/ -lemma Representation.IsIntertwiningMap.piTensorMap {ρ : Π i, Representation k G (M i)} +lemma Representation.IsIntertwiningMap.piTprodMap {ρ : Π i, Representation k G (M i)} {σ : Π i, Representation k G (N i)} {f : Π i, M i →ₗ[k] N i} (hf : ∀ i, (ρ i).IsIntertwiningMap (σ i) (f i)) : (Representation.piTprod ρ).IsIntertwiningMap (Representation.piTprod σ) @@ -154,32 +154,32 @@ lemma Representation.IsIntertwiningMap.piTensorMap {ρ : Π i, Representation k /-- The tensor product of a family of equivalences of representations, `⨂ i, v i ↦ ⨂ i, φ i (v i)`. It is Mathlib's `PiTensorProduct.congr` with the actions `piTprod ρ` and `piTprod σ`. -/ -noncomputable def Representation.Equiv.piTensorCongr {ρ : Π i, Representation k G (M i)} +noncomputable def Representation.Equiv.piTprodCongr {ρ : Π i, Representation k G (M i)} {σ : Π i, Representation k G (N i)} (φ : Π i, (ρ i).Equiv (σ i)) : (Representation.piTprod ρ).Equiv (Representation.piTprod σ) := .mk (PiTensorProduct.congr fun i => (φ i).toLinearEquiv) fun g => LinearMap.ext - ((Representation.IsIntertwiningMap.piTensorMap fun i => + ((Representation.IsIntertwiningMap.piTprodMap fun i => ⟨(φ i).toIntertwiningMap.isIntertwining⟩).isIntertwining g) @[simp] -lemma Representation.Equiv.piTensorCongr_tprod {ρ : Π i, Representation k G (M i)} +lemma Representation.Equiv.piTprodCongr_tprod {ρ : Π i, Representation k G (M i)} {σ : Π i, Representation k G (N i)} (φ : Π i, (ρ i).Equiv (σ i)) (m : Π i, M i) : - Representation.Equiv.piTensorCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := + Representation.Equiv.piTprodCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ /-- Reindexing the factors of a tensor product of representations along `e : ι ≃ ι'`, `⨂ i, v i ↦ ⨂ j, v (e.symm j)`. It is Mathlib's `PiTensorProduct.reindex` with the actions `piTprod ρ` and `piTprod (ρ ∘ e.symm)`. -/ -noncomputable def Representation.Equiv.piTensorReindex {ι' : Type*} +noncomputable def Representation.Equiv.piTprodReindex {ι' : Type*} (ρ : Π i, Representation k G (M i)) (e : ι ≃ ι') : (Representation.piTprod ρ).Equiv (Representation.piTprod fun j => ρ (e.symm j)) := .mk (PiTensorProduct.reindex k M e) fun g => PiTensorProduct.ext <| MultilinearMap.ext fun m => by simp [PiTensorProduct.reindex_tprod] @[simp] -lemma Representation.Equiv.piTensorReindex_tprod {ι' : Type*} (ρ : Π i, Representation k G (M i)) +lemma Representation.Equiv.piTprodReindex_tprod {ι' : Type*} (ρ : Π i, Representation k G (M i)) (e : ι ≃ ι') (m : Π i, M i) : - Representation.Equiv.piTensorReindex ρ e (⨂ₜ[k] i, m i) = ⨂ₜ[k] j, m (e.symm j) := + Representation.Equiv.piTprodReindex ρ e (⨂ₜ[k] i, m i) = ⨂ₜ[k] j, m (e.symm j) := PiTensorProduct.reindex_tprod _ _ /-- The tensor product of the two halves of a family of representations indexed by `ι₁ ⊕ ι₂`, diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean index 44d6649d57..10b0ab2bfc 100644 --- a/Physlib/Mathematics/Tensors/Reindexing.lean +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -29,7 +29,7 @@ reindexing, since that needs a metric. The index types - `ColorEquiv` is a bijection of indices that keeps their colors, with `refl`, `symm` and `trans`. - `Species.permIndices` moves the vectors of a tensor along a bijection of indices, with no colors - involved. It is `Representation.Equiv.piTensorReindex`, on Mathlib's `PiTensorProduct.reindex`. + involved. It is `Representation.Equiv.piTprodReindex`, on Mathlib's `PiTensorProduct.reindex`. - `Species.reindex` applies a reindexing to tensors, `permIndices` followed by `congrIndices` of the moves along the equalities of colors, and `Species.reindex_pure` gives it on a pure tensor. - `Species.reindex_trans`, `Species.reindex_refl` and `Species.reindex_symm` prove that reindexing @@ -131,11 +131,11 @@ variable {ι₁ ι₂ ι₃ : Type} variable (S) in /-- The bijection `e` of indices applied to tensors, moving the vector of the old index `i` to the -new index `e i`, with the colors `c ∘ e.symm`. It is `Representation.Equiv.piTensorReindex`, typed +new index `e i`, with the colors `c ∘ e.symm`. It is `Representation.Equiv.piTprodReindex`, typed on `Tensor S (c ∘ e.symm)` so that it composes with other maps of tensors. -/ noncomputable def permIndices (c : ι₁ → Color K) (e : ι₁ ≃ ι₂) : (S.tensorRep c).Equiv (S.tensorRep (c ∘ e.symm)) := - Representation.Equiv.piTensorReindex (fun i => S.rep (c i)) e + Representation.Equiv.piTprodReindex (fun i => S.rep (c i)) e /-- On a pure tensor, the new index `j` holds the vector of the old index `e.symm j`. -/ @[simp] diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index bbe376175d..bd887cb132 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -367,19 +367,19 @@ lemma mapIndices_id_apply (t : Tensor S c) : S.mapIndices (fun _ => LinearMap.id lemma mapIndices_smul {f : Π x, S.V (c x) →ₗ[k] S.V (c' x)} (hf : ∀ x g v, f x (S.rep (c x) g v) = S.rep (c' x) g (f x v)) (g : G) (t : Tensor S c) : S.mapIndices f (g • t) = g • S.mapIndices f t := - (Representation.IsIntertwiningMap.piTensorMap fun x => ⟨hf x⟩).isIntertwining g t + (Representation.IsIntertwiningMap.piTprodMap fun x => ⟨hf x⟩).isIntertwining g t variable (S) in /-- Equivalences of representations `f x` at every index `x`, as an equivalence of representations -of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is `Representation.Equiv.piTensorCongr`. -/ +of tensors, `u ⊗ v ↦ f₀ u ⊗ f₁ v`. It is `Representation.Equiv.piTprodCongr`. -/ noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : (S.tensorRep c).Equiv (S.tensorRep c') := - Representation.Equiv.piTensorCongr f + Representation.Equiv.piTprodCongr f /-- On a pure tensor, `congrIndices` applies `f x` to the vector at each index `x`. -/ lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x, S.V (c x)) : S.congrIndices f (S.pure p) = S.pure fun x => f x (p x) := - Representation.Equiv.piTensorCongr_tprod f p + Representation.Equiv.piTprodCongr_tprod f p /-! From 84ce94d5e9a2ffabe1b5ba79a5f5101af2f0d340 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 08:44:21 +0000 Subject: [PATCH 219/242] docs(ForMathlib): Representation module doc with the gap it closes, section descriptions and subsections C.1 to C.4 Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 127 ++++++++++++------ 1 file changed, 88 insertions(+), 39 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 82b8daef1f..2b02fef08c 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -12,56 +12,57 @@ public import Physlib.Mathematics.ForMathlib.PiTensorProduct # Tensor products of representations and invariant vectors -Tensor products of equivalences of representations, the tensor product of a family of -representations, invariant vectors under an intertwining map, and maps of representations given -by actions. +The representation on the tensor product of a family of representations, the maps of +representations between such products, and invariant vectors under intertwining maps. ## i. Overview -An intertwining map `φ` from a representation `ρ` to a representation `σ` satisfies -`φ (ρ g v) = σ g (φ v)`. If `v` is invariant, `ρ g v = v`, then so is `φ v`. Invariance of a tensor -built from an invariant vector by intertwining maps is therefore a corollary, as the invariance of -the Minkowski metric as a tensor follows from the invariance of the bilinear form. - -Mathlib has the tensor product of two intertwining maps, `IntertwiningMap.tensor`. Here it is -given for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on the linear equivalence -`TensorProduct.congr`. - -A family of representations `ρ i` on `M i` gives the representation `piTprod ρ` on -`⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g`, on Mathlib's `PiTensorProduct.map`. Maps `f i` that intertwine -`ρ i` and `σ i` give `PiTensorProduct.map f`, which intertwines `piTprod ρ` and `piTprod σ`. -Three of Mathlib's linear equivalences of `⨂` are equivalences of representations: -`PiTensorProduct.reindex` along `e : ι ≃ ι'`, `PiTensorProduct.tmulEquivDep`, which splits a -family over `ι₁ ⊕ ι₂` into its two halves, and `PiTensorProduct.subsingletonEquiv`, for a family -over a subsingleton index type. -Contracting pairs of factors by invariant pairings, `PiTensorProduct.contrPairs`, is also -intertwining. +A representation `ρ` of a group `G` on a space `V` says how each `g` acts on `V`. A map of +representations from `ρ` to `σ` on `W` is a linear map `φ : V → W` that commutes with the group, +`φ (ρ g v) = σ g (φ v)`. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is +invertible. It sends an invariant vector, `ρ g v = v`, to an invariant vector (A). + +Mathlib has the representation `ρ.tprod τ` on the tensor product of two representations, and the +tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the +tensor product `⨂[k] i, M i` of a family, which is where a tensor with several indices lies. So +Mathlib's linear maps of `⨂`, such as `PiTensorProduct.reindex`, are not maps of representations. +This file adds: +- the representation `piTprod ρ` on `⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g` (C.1), +- `PiTensorProduct.map`, `congr`, `reindex`, `tmulEquivDep` and `subsingletonEquiv` as maps of + representations for it (C.1, C.2), +- the check that a map commutes with `piTprod` on pure tensors only (C.3), +- the contraction of pairs of factors as a map of representations (C.4), +- the tensor product of two equivalences of representations, next to Mathlib's for intertwining + maps (B). + +For example, a representation `ρ` on `V` and its dual `ρ*` on `V*` give the representation on +`V ⊗ V* ⊗ V*` of the family `ρ`, `ρ*`, `ρ*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping +them as `V ⊗ (V* ⊗ V*)`, and contracting `V` with the first `V*` to leave `V*` all commute with the +group. So each one sends an invariant vector to an invariant vector, and a tensor built from +invariant ones by these maps is invariant with no further proof. ## ii. Key results +- `Representation.piTprod` : the representation on `⨂[k] i, M i` of a family of representations. +- `Representation.IsIntertwiningMap.piTprodMap` and `Representation.Equiv.piTprodCongr` : + `PiTensorProduct.map` and `PiTensorProduct.congr` of maps of representations. +- `Representation.Equiv.piTprodReindex`, `Representation.Equiv.piTprodSum` and + `Representation.Equiv.piTprodOfSubsingleton` : Mathlib's `reindex`, `tmulEquivDep` and + `subsingletonEquiv` as equivalences of representations. +- `Representation.map_piTprod_of_tprod` : a map that commutes with `piTprod ρ` on pure tensors + commutes with it on all tensors. - `Representation.IntertwiningMap.map_invariant` : an intertwining map sends invariant vectors to - invariant vectors, and `Representation.IntertwiningMap.map_one_invariant` : an intertwining map - from the trivial representation picks out an invariant vector. -- `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. -- `Representation.piTprod` : the tensor product of a family of representations, with - `Representation.IsIntertwiningMap.piTprodMap` : `PiTensorProduct.map` of intertwining maps is - intertwining, `Representation.Equiv.piTprodCongr` : the tensor product of a family of - equivalences of representations, `Representation.Equiv.piTprodReindex` : reindexing the factors, - `Representation.Equiv.piTprodSum` : the product of the two halves of a family over `ι₁ ⊕ ι₂`, - `Representation.Equiv.piTprodOfSubsingleton` : a family over a subsingleton index type, and - `Representation.IsIntertwiningMap.contrPairs` : the contraction of invariantly paired factors is - intertwining. -- `Representation.map_piTprod_of_tprod` : a semilinear map that commutes with `piTprod ρ` on pure - tensors commutes with it on all tensors. -- `Representation.IntertwiningMap.map_smul_ofDistribMulAction` and - `Representation.Equiv.map_smul_ofDistribMulAction` : maps of representations given by actions - commute with the actions, `φ (g • v) = g • φ v`. + invariant vectors. ## iii. Table of contents - A. Invariant vectors - B. Tensor products of equivalences - C. Tensor products of families + - C.1. The representation on a tensor product of a family + - C.2. Mathlib's equivalences of tensor products + - C.3. Commuting on pure tensors + - C.4. Contraction of pairs of factors - D. Representations given by actions ## iv. References @@ -83,6 +84,10 @@ variable {k G V W U X : Type*} [CommSemiring k] [Monoid G] [AddCommMonoid V] [Mo ## A. Invariant vectors +An intertwining map `φ` from `ρ` to `σ` satisfies `φ (ρ g v) = σ g (φ v)`, so it sends an +invariant vector, `ρ g v = v`, to an invariant vector (`map_invariant`). An intertwining map from +the trivial representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). + -/ /-- An intertwining map sends an invariant vector to an invariant vector, @@ -91,8 +96,8 @@ lemma Representation.IntertwiningMap.map_invariant (φ : ρ.IntertwiningMap σ) (hv : ρ g v = v) : σ g (φ v) = φ v := by rw [← Representation.IntertwiningMap.isIntertwining, hv] -/-- An intertwining map `φ` from the trivial representation picks out an invariant vector, its -value `φ 1`, as an invariant form picks out its tensor. -/ +/-- An intertwining map `φ` from the trivial representation on `k` picks out an invariant vector, +`φ 1`. The hypothesis `φ 1 = v` turns a formula for `φ 1` into the invariance of `v`. -/ lemma Representation.IntertwiningMap.map_one_invariant (φ : (Representation.trivial k G k).IntertwiningMap ρ) {v : V} (hv : φ 1 = v) (g : G) : ρ g v = v := @@ -102,6 +107,9 @@ lemma Representation.IntertwiningMap.map_one_invariant ## B. Tensor products of equivalences +Mathlib has the tensor product `IntertwiningMap.tensor` of two intertwining maps. `tensorCongr` is +the same for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on `TensorProduct.congr`. + -/ /-- The tensor product of two equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, Mathlib's @@ -119,6 +127,13 @@ lemma Representation.Equiv.tensorCongr_tmul (φ : ρ.Equiv σ) (ψ : τ.Equiv π ## C. Tensor products of families +### C.1. The representation on a tensor product of a family + +`piTprod ρ` is the representation `g ↦ ⨂ i, ρ i g` on `⨂[k] i, M i`, through Mathlib's +`PiTensorProduct.map`. It is the n-ary `Representation.tprod`. When every `f i` intertwines `ρ i` +and `σ i`, `PiTensorProduct.map f` intertwines `piTprod ρ` and `piTprod σ` (`piTprodMap`), and +`piTprodCongr` is the same for equivalences of representations. + -/ section @@ -167,6 +182,18 @@ lemma Representation.Equiv.piTprodCongr_tprod {ρ : Π i, Representation k G (M Representation.Equiv.piTprodCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ +/-! + +### C.2. Mathlib's equivalences of tensor products + +Three of Mathlib's linear equivalences of `⨂` commute with `piTprod`, so they are equivalences of +representations. `piTprodReindex` reindexes the factors along `e : ι ≃ ι'`, +`⨂ i, v i ↦ ⨂ j, v (e.symm j)`. `piTprodSum` joins the tensor products of the two halves of a +family over `ι₁ ⊕ ι₂`. `piTprodOfSubsingleton` identifies `ρ i₀` with the tensor product of a +family over a subsingleton index type. + +-/ + /-- Reindexing the factors of a tensor product of representations along `e : ι ≃ ι'`, `⨂ i, v i ↦ ⨂ j, v (e.symm j)`. It is Mathlib's `PiTensorProduct.reindex` with the actions `piTprod ρ` and `piTprod (ρ ∘ e.symm)`. -/ @@ -218,6 +245,15 @@ noncomputable def Representation.Equiv.piTprodOfSubsingleton [Subsingleton ι] obtain rfl := Subsingleton.elim i i₀ simp +/-! + +### C.3. Commuting on pure tensors + +A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors `⨂ₜ i, m i` +commutes with it on all tensors (`map_piTprod_of_tprod`), since pure tensors span `⨂ i, M i`. + +-/ + /-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes with it on all tensors. -/ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} @@ -228,6 +264,16 @@ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Represent | smul_tprod r m => rw [map_smul, map_smulₛₗ, h, map_smulₛₗ, map_smul] | add x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] +/-! + +### C.4. Contraction of pairs of factors + +`PiTensorProduct.contrPairs p` contracts the factors of a family over `κ ⊕ (J ⊕ J)` in pairs, by +the pairings `p j`, and keeps the factors over `κ`. When every pairing is invariant, the +contraction intertwines `piTprod ρ` and `piTprod` of the kept factors. + +-/ + /-- `PiTensorProduct.contrPairs p` intertwines `piTprod ρ` and `piTprod` of the kept factors when every pairing `p j` is invariant. -/ lemma Representation.IsIntertwiningMap.contrPairs {κ J : Type*} [Fintype J] @@ -250,6 +296,9 @@ end ## D. Representations given by actions +A representation can be given by an action, `Representation.ofDistribMulAction k G V`, with +`ρ g v = g • v`. Maps of such representations commute with the actions, `φ (g • v) = g • φ v`. + -/ section From 489f28729db3d6c6f3f82d98dc9a04c1d6cef333 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 08:45:09 +0000 Subject: [PATCH 220/242] docs(ForMathlib): the Representation summary and overview as sentences, without the list of additions Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 2b02fef08c..5734a81834 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -12,8 +12,8 @@ public import Physlib.Mathematics.ForMathlib.PiTensorProduct # Tensor products of representations and invariant vectors -The representation on the tensor product of a family of representations, the maps of -representations between such products, and invariant vectors under intertwining maps. +This file gives the representation on the tensor product of a family of representations, and makes +Mathlib's linear maps of such products into maps of representations. ## i. Overview @@ -26,14 +26,8 @@ Mathlib has the representation `ρ.tprod τ` on the tensor product of two repres tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the tensor product `⨂[k] i, M i` of a family, which is where a tensor with several indices lies. So Mathlib's linear maps of `⨂`, such as `PiTensorProduct.reindex`, are not maps of representations. -This file adds: -- the representation `piTprod ρ` on `⨂[k] i, M i`, `g ↦ ⨂ i, ρ i g` (C.1), -- `PiTensorProduct.map`, `congr`, `reindex`, `tmulEquivDep` and `subsingletonEquiv` as maps of - representations for it (C.1, C.2), -- the check that a map commutes with `piTprod` on pure tensors only (C.3), -- the contraction of pairs of factors as a map of representations (C.4), -- the tensor product of two equivalences of representations, next to Mathlib's for intertwining - maps (B). +This file defines that representation, `piTprod ρ`, and proves that Mathlib's linear maps of `⨂` +commute with it, so that they become maps of representations (C). For example, a representation `ρ` on `V` and its dual `ρ*` on `V*` give the representation on `V ⊗ V* ⊗ V*` of the family `ρ`, `ρ*`, `ρ*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping From 8829b7bd0c95f05f3c7b06c87743c7b4324fad27 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 09:03:44 +0000 Subject: [PATCH 221/242] docs(ForMathlib): intertwining maps in map form with their commutative square, in the Representation overview and section A Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 5734a81834..93486837ff 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -17,10 +17,22 @@ Mathlib's linear maps of such products into maps of representations. ## i. Overview -A representation `ρ` of a group `G` on a space `V` says how each `g` acts on `V`. A map of -representations from `ρ` to `σ` on `W` is a linear map `φ : V → W` that commutes with the group, -`φ (ρ g v) = σ g (φ v)`. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is -invertible. It sends an invariant vector, `ρ g v = v`, to an invariant vector (A). +A representation `ρ` of a group or monoid `G` on a space `V` says how each element `g` of `G` acts +on `V`, by the linear map `ρ g`. A map of representations from `ρ` to `σ` on `W` is a linear map +`φ : V → W` that commutes with the group, `φ ∘ ρ g = σ g ∘ φ` for every `g`, so that the square + +``` + ρ g + V ──────► V + │ │ + φ │ │ φ + ▼ ▼ + W ──────► W + σ g +``` + +commutes. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is invertible. It sends an +invariant vector, `ρ g v = v`, to an invariant vector `φ v` (A). Mathlib has the representation `ρ.tprod τ` on the tensor product of two representations, and the tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the @@ -78,8 +90,8 @@ variable {k G V W U X : Type*} [CommSemiring k] [Monoid G] [AddCommMonoid V] [Mo ## A. Invariant vectors -An intertwining map `φ` from `ρ` to `σ` satisfies `φ (ρ g v) = σ g (φ v)`, so it sends an -invariant vector, `ρ g v = v`, to an invariant vector (`map_invariant`). An intertwining map from +An intertwining map `φ` from `ρ` to `σ` satisfies `φ ∘ ρ g = σ g ∘ φ`. So if `ρ g v = v`, then +`σ g (φ v) = φ (ρ g v) = φ v`, and `φ v` is invariant (`map_invariant`). An intertwining map from the trivial representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). -/ From c7bdcac699c403587dddde79b0d1b096808e9dba Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:42:44 +0000 Subject: [PATCH 222/242] docs(ForMathlib): lowering with the Lorentz metric as the example of an intertwining map, in section A of Representation Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 93486837ff..1dce5c755f 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -90,8 +90,13 @@ variable {k G V W U X : Type*} [CommSemiring k] [Monoid G] [AddCommMonoid V] [Mo ## A. Invariant vectors -An intertwining map `φ` from `ρ` to `σ` satisfies `φ ∘ ρ g = σ g ∘ φ`. So if `ρ g v = v`, then -`σ g (φ v) = φ (ρ g v) = φ v`, and `φ v` is invariant (`map_invariant`). An intertwining map from +An intertwining map `φ` from `ρ` to `σ` satisfies `φ ∘ ρ g = σ g ∘ φ`. For example, a Lorentz +transformation `Λ` acts on vectors by `Λ` and on covectors by `(Λ⁻¹)ᵀ`, so that `α_μ v^μ` is +invariant. Lowering an index with the metric, `φ v = η v`, is an intertwining map between them. The +two routes in the square give `η (Λ v)` and `(Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the +defining property of a Lorentz transformation. + +So if `ρ g v = v`, then `σ g (φ v) = φ (ρ g v) = φ v`, and `φ v` is invariant (`map_invariant`). An intertwining map from the trivial representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). -/ From 25bc020befb725ea9283e4222ac83b16e01c6de2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:43:10 +0000 Subject: [PATCH 223/242] style(ForMathlib): line lengths in section A of Representation Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 1dce5c755f..e35aa8ab0c 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -96,8 +96,9 @@ invariant. Lowering an index with the metric, `φ v = η v`, is an intertwining two routes in the square give `η (Λ v)` and `(Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the defining property of a Lorentz transformation. -So if `ρ g v = v`, then `σ g (φ v) = φ (ρ g v) = φ v`, and `φ v` is invariant (`map_invariant`). An intertwining map from -the trivial representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). +An intertwining map sends invariant vectors to invariant vectors. If `ρ g v = v`, then +`σ g (φ v) = φ (ρ g v) = φ v` (`map_invariant`). An intertwining map from the trivial +representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). -/ From 06ebda95c24df57520b463d8e29876af7a532007 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:46:14 +0000 Subject: [PATCH 224/242] docs(ForMathlib): the Lorentz example in the Representation overview, and the tensor example with Lorentz tensors Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index e35aa8ab0c..278b501bd2 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -34,6 +34,12 @@ on `V`, by the linear map `ρ g`. A map of representations from `ρ` to `σ` on commutes. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is invertible. It sends an invariant vector, `ρ g v = v`, to an invariant vector `φ v` (A). +For example, a Lorentz transformation `Λ` acts on vectors by `Λ` and on covectors by `(Λ⁻¹)ᵀ`, so +that `α_μ v^μ` is invariant. Lowering an index with the metric, `φ v = η v`, is a map of +representations from vectors to covectors. The two routes in the square give `η (Λ v)` and +`(Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the defining property of a Lorentz +transformation. + Mathlib has the representation `ρ.tprod τ` on the tensor product of two representations, and the tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the tensor product `⨂[k] i, M i` of a family, which is where a tensor with several indices lies. So @@ -41,11 +47,11 @@ Mathlib's linear maps of `⨂`, such as `PiTensorProduct.reindex`, are not maps This file defines that representation, `piTprod ρ`, and proves that Mathlib's linear maps of `⨂` commute with it, so that they become maps of representations (C). -For example, a representation `ρ` on `V` and its dual `ρ*` on `V*` give the representation on -`V ⊗ V* ⊗ V*` of the family `ρ`, `ρ*`, `ρ*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping -them as `V ⊗ (V* ⊗ V*)`, and contracting `V` with the first `V*` to leave `V*` all commute with the -group. So each one sends an invariant vector to an invariant vector, and a tensor built from -invariant ones by these maps is invariant with no further proof. +In the same way, the Lorentz tensors `T^μ_νρ` lie in `V ⊗ V* ⊗ V*`, with `Λ` on the vectors `V` and +`(Λ⁻¹)ᵀ` on the covectors `V*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping them as +`V ⊗ (V* ⊗ V*)`, and contracting `V` with the first `V*`, `T^μ_μρ`, all commute with `Λ`. So each +one sends an invariant tensor to an invariant tensor, and a tensor built from invariant ones by +these maps is invariant with no further proof. ## ii. Key results @@ -90,15 +96,10 @@ variable {k G V W U X : Type*} [CommSemiring k] [Monoid G] [AddCommMonoid V] [Mo ## A. Invariant vectors -An intertwining map `φ` from `ρ` to `σ` satisfies `φ ∘ ρ g = σ g ∘ φ`. For example, a Lorentz -transformation `Λ` acts on vectors by `Λ` and on covectors by `(Λ⁻¹)ᵀ`, so that `α_μ v^μ` is -invariant. Lowering an index with the metric, `φ v = η v`, is an intertwining map between them. The -two routes in the square give `η (Λ v)` and `(Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the -defining property of a Lorentz transformation. - -An intertwining map sends invariant vectors to invariant vectors. If `ρ g v = v`, then -`σ g (φ v) = φ (ρ g v) = φ v` (`map_invariant`). An intertwining map from the trivial -representation on `k` picks out the invariant vector `φ 1` (`map_one_invariant`). +An intertwining map `φ` from `ρ` to `σ` satisfies `φ ∘ ρ g = σ g ∘ φ`, so it sends invariant +vectors to invariant vectors. If `ρ g v = v`, then `σ g (φ v) = φ (ρ g v) = φ v` +(`map_invariant`). An intertwining map from the trivial representation on `k` picks out the +invariant vector `φ 1` (`map_one_invariant`). -/ From 1786794261daa662bbade9756e8e856afa731d3e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:50:00 +0000 Subject: [PATCH 225/242] docs(ForMathlib): two subsections in section C of Representation, map_piTprod_of_tprod next to piTprod Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 77 ++++++++----------- 1 file changed, 30 insertions(+), 47 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 278b501bd2..e65275fb13 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -72,9 +72,7 @@ these maps is invariant with no further proof. - B. Tensor products of equivalences - C. Tensor products of families - C.1. The representation on a tensor product of a family - - C.2. Mathlib's equivalences of tensor products - - C.3. Commuting on pure tensors - - C.4. Contraction of pairs of factors + - C.2. Maps of representations - D. Representations given by actions ## iv. References @@ -143,9 +141,9 @@ lemma Representation.Equiv.tensorCongr_tmul (φ : ρ.Equiv σ) (ψ : τ.Equiv π ### C.1. The representation on a tensor product of a family `piTprod ρ` is the representation `g ↦ ⨂ i, ρ i g` on `⨂[k] i, M i`, through Mathlib's -`PiTensorProduct.map`. It is the n-ary `Representation.tprod`. When every `f i` intertwines `ρ i` -and `σ i`, `PiTensorProduct.map f` intertwines `piTprod ρ` and `piTprod σ` (`piTprodMap`), and -`piTprodCongr` is the same for equivalences of representations. +`PiTensorProduct.map`. It is the n-ary `Representation.tprod`. A semilinear map out of +`⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors `⨂ₜ i, m i` commutes with it on all +tensors (`map_piTprod_of_tprod`), since pure tensors span `⨂ i, M i`. -/ @@ -168,6 +166,32 @@ lemma Representation.piTprod_tprod (ρ : Π i, Representation k G (M i)) (g : G) Representation.piTprod ρ g (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, ρ i g (m i) := PiTensorProduct.map_tprod _ _ +/-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes +with it on all tensors. -/ +lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} + (f : (⨂[k] i, M i) →ₛₗ[φ] U) (g : G) + (h : ∀ m : Π i, M i, f (Representation.piTprod ρ g (⨂ₜ[k] i, m i)) = τ g (f (⨂ₜ[k] i, m i))) + (t : ⨂[k] i, M i) : f (Representation.piTprod ρ g t) = τ g (f t) := by + induction t using PiTensorProduct.induction_on with + | smul_tprod r m => rw [map_smul, map_smulₛₗ, h, map_smulₛₗ, map_smul] + | add x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] + +/-! + +### C.2. Maps of representations + +Mathlib's linear maps of `⨂` commute with `piTprod`, so they are maps of representations. When +every `f i` intertwines `ρ i` and `σ i`, `PiTensorProduct.map f` intertwines `piTprod ρ` and +`piTprod σ` (`piTprodMap`), and `piTprodCongr` is the same for equivalences of representations. +`piTprodReindex` reindexes the factors along `e : ι ≃ ι'`, `⨂ i, v i ↦ ⨂ j, v (e.symm j)`. +`piTprodSum` joins the tensor products of the two halves of a family over `ι₁ ⊕ ι₂`, and +`piTprodOfSubsingleton` identifies `ρ i₀` with the tensor product of a family over a subsingleton +index type. The contraction `PiTensorProduct.contrPairs p` of the factors of a family over +`κ ⊕ (J ⊕ J)` in pairs, by the pairings `p j`, intertwines `piTprod ρ` and `piTprod` of the kept +factors when every pairing is invariant. + +-/ + /-- `PiTensorProduct.map f` intertwines `piTprod ρ` and `piTprod σ` when every `f i` intertwines `ρ i` and `σ i`. -/ lemma Representation.IsIntertwiningMap.piTprodMap {ρ : Π i, Representation k G (M i)} @@ -195,18 +219,6 @@ lemma Representation.Equiv.piTprodCongr_tprod {ρ : Π i, Representation k G (M Representation.Equiv.piTprodCongr φ (⨂ₜ[k] i, m i) = ⨂ₜ[k] i, φ i (m i) := PiTensorProduct.congr_tprod _ _ -/-! - -### C.2. Mathlib's equivalences of tensor products - -Three of Mathlib's linear equivalences of `⨂` commute with `piTprod`, so they are equivalences of -representations. `piTprodReindex` reindexes the factors along `e : ι ≃ ι'`, -`⨂ i, v i ↦ ⨂ j, v (e.symm j)`. `piTprodSum` joins the tensor products of the two halves of a -family over `ι₁ ⊕ ι₂`. `piTprodOfSubsingleton` identifies `ρ i₀` with the tensor product of a -family over a subsingleton index type. - --/ - /-- Reindexing the factors of a tensor product of representations along `e : ι ≃ ι'`, `⨂ i, v i ↦ ⨂ j, v (e.symm j)`. It is Mathlib's `PiTensorProduct.reindex` with the actions `piTprod ρ` and `piTprod (ρ ∘ e.symm)`. -/ @@ -258,35 +270,6 @@ noncomputable def Representation.Equiv.piTprodOfSubsingleton [Subsingleton ι] obtain rfl := Subsingleton.elim i i₀ simp -/-! - -### C.3. Commuting on pure tensors - -A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors `⨂ₜ i, m i` -commutes with it on all tensors (`map_piTprod_of_tprod`), since pure tensors span `⨂ i, M i`. - --/ - -/-- A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors commutes -with it on all tensors. -/ -lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Representation k G (M i)} - (f : (⨂[k] i, M i) →ₛₗ[φ] U) (g : G) - (h : ∀ m : Π i, M i, f (Representation.piTprod ρ g (⨂ₜ[k] i, m i)) = τ g (f (⨂ₜ[k] i, m i))) - (t : ⨂[k] i, M i) : f (Representation.piTprod ρ g t) = τ g (f t) := by - induction t using PiTensorProduct.induction_on with - | smul_tprod r m => rw [map_smul, map_smulₛₗ, h, map_smulₛₗ, map_smul] - | add x y hx hy => rw [map_add, map_add, hx, hy, map_add, map_add] - -/-! - -### C.4. Contraction of pairs of factors - -`PiTensorProduct.contrPairs p` contracts the factors of a family over `κ ⊕ (J ⊕ J)` in pairs, by -the pairings `p j`, and keeps the factors over `κ`. When every pairing is invariant, the -contraction intertwines `piTprod ρ` and `piTprod` of the kept factors. - --/ - /-- `PiTensorProduct.contrPairs p` intertwines `piTprod ρ` and `piTprod` of the kept factors when every pairing `p j` is invariant. -/ lemma Representation.IsIntertwiningMap.contrPairs {κ J : Type*} [Fintype J] From 5c0455257a30de1366c7aee909ca8265033965e2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:53:50 +0000 Subject: [PATCH 226/242] docs(ForMathlib): the maps that commute with piTprod named and explained by Lorentz examples in Representation Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index e65275fb13..2c194472eb 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -44,8 +44,9 @@ Mathlib has the representation `ρ.tprod τ` on the tensor product of two repres tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the tensor product `⨂[k] i, M i` of a family, which is where a tensor with several indices lies. So Mathlib's linear maps of `⨂`, such as `PiTensorProduct.reindex`, are not maps of representations. -This file defines that representation, `piTprod ρ`, and proves that Mathlib's linear maps of `⨂` -commute with it, so that they become maps of representations (C). +This file defines that representation, `piTprod ρ`, and proves that applying maps at the factors, +reordering, grouping and contracting factors commute with it, so that they are maps of +representations (C). In the same way, the Lorentz tensors `T^μ_νρ` lie in `V ⊗ V* ⊗ V*`, with `Λ` on the vectors `V` and `(Λ⁻¹)ᵀ` on the covectors `V*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping them as @@ -180,15 +181,18 @@ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Represent ### C.2. Maps of representations -Mathlib's linear maps of `⨂` commute with `piTprod`, so they are maps of representations. When -every `f i` intertwines `ρ i` and `σ i`, `PiTensorProduct.map f` intertwines `piTprod ρ` and -`piTprod σ` (`piTprodMap`), and `piTprodCongr` is the same for equivalences of representations. -`piTprodReindex` reindexes the factors along `e : ι ≃ ι'`, `⨂ i, v i ↦ ⨂ j, v (e.symm j)`. -`piTprodSum` joins the tensor products of the two halves of a family over `ι₁ ⊕ ι₂`, and -`piTprodOfSubsingleton` identifies `ρ i₀` with the tensor product of a family over a subsingleton -index type. The contraction `PiTensorProduct.contrPairs p` of the factors of a family over -`κ ⊕ (J ⊕ J)` in pairs, by the pairings `p j`, intertwines `piTprod ρ` and `piTprod` of the kept -factors when every pairing is invariant. +The following of Mathlib's linear maps of `⨂` commute with `piTprod`, so they are maps of +representations. Each acts index by index, or pairs indices in an invariant way, as the examples on +Lorentz tensors show: +- `piTprodMap` applies a map at every index, such as lowering every index, `T^μν ↦ T_μν`. It + commutes with the group when each of the maps does, and `piTprodCongr` is the same for + equivalences of representations. +- `piTprodReindex` reorders the indices along `e : ι ≃ ι'`, as `T^μ_νρ ↦ T_ν^μ_ρ`. +- `piTprodSum` joins two tensors into one, `T^μ_ν` and `U^ρ` into `T^μ_ν U^ρ`, with the indices of + the first over `ι₁` and those of the second over `ι₂`. +- `piTprodOfSubsingleton` identifies a tensor with one index with its vector, `v^μ` with `v`. +- `IsIntertwiningMap.contrPairs` contracts indices in pairs, as in `T^μ_μρ`, when every pairing, + such as `α_μ v^μ`, is invariant. -/ From 6e2d9d2eba9ca02dbda1ff27dc4807fd44b523ca Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 10:58:45 +0000 Subject: [PATCH 227/242] =?UTF-8?q?docs(ForMathlib):=20piTprod=20by=20the?= =?UTF-8?q?=20T^=CE=BC=5F=CE=BD=20example=20in=20C.1,=20and=20the=20maps?= =?UTF-8?q?=20that=20do=20not=20commute=20in=20C.2=20of=20Representation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 2c194472eb..fa457b9577 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -141,10 +141,15 @@ lemma Representation.Equiv.tensorCongr_tmul (φ : ρ.Equiv σ) (ψ : τ.Equiv π ### C.1. The representation on a tensor product of a family -`piTprod ρ` is the representation `g ↦ ⨂ i, ρ i g` on `⨂[k] i, M i`, through Mathlib's -`PiTensorProduct.map`. It is the n-ary `Representation.tprod`. A semilinear map out of -`⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors `⨂ₜ i, m i` commutes with it on all -tensors (`map_piTprod_of_tprod`), since pure tensors span `⨂ i, M i`. +Given how the group acts at each index, by the family `ρ`, the representation `piTprod ρ` acts on +the whole tensor by transforming every index by its own rule, `g ↦ ⨂ i, ρ i g` on `⨂[k] i, M i`. +For `T^μ_ν`, with `Λ` at the up index and `(Λ⁻¹)ᵀ` at the down index, it is +`T^μ_ν ↦ Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ`, and on a pure tensor `u ⊗ α ↦ Λu ⊗ (Λ⁻¹)ᵀ α` (`piTprod_tprod`). +It is Mathlib's `Representation.tprod` on `V ⊗ W` for a family of factors, through +`PiTensorProduct.map`. + +A semilinear map out of `⨂ i, M i` that commutes with `piTprod ρ g` on pure tensors `⨂ₜ i, m i` +commutes with it on all tensors (`map_piTprod_of_tprod`), since pure tensors span `⨂ i, M i`. -/ @@ -181,9 +186,10 @@ lemma Representation.map_piTprod_of_tprod {φ : k →+* k} {ρ : Π i, Represent ### C.2. Maps of representations -The following of Mathlib's linear maps of `⨂` commute with `piTprod`, so they are maps of -representations. Each acts index by index, or pairs indices in an invariant way, as the examples on -Lorentz tensors show: +Not all linear maps out of `⨂` commute with the group action `piTprod`. Keeping only the +component `T^00` of `T^μν` does not, since a boost mixes `T^00` with the other components. The +following do, so they are maps of representations. Each acts index by index, or pairs indices in an +invariant way, as the examples on Lorentz tensors show: - `piTprodMap` applies a map at every index, such as lowering every index, `T^μν ↦ T_μν`. It commutes with the group when each of the maps does, and `piTprodCongr` is the same for equivalences of representations. From 4e119c6cadf9865396250257e7928c212ee4aec6 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 11:04:28 +0000 Subject: [PATCH 228/242] =?UTF-8?q?docs(ForMathlib):=20the=20Lorentz=20exa?= =?UTF-8?q?mple=20names=20G,=20=CF=81,=20=CF=83=20and=20=CF=86,=20and=20th?= =?UTF-8?q?e=20family=20of=20representations=20is=20explained,=20in=20the?= =?UTF-8?q?=20Representation=20overview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index fa457b9577..a267d95ef1 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -34,16 +34,19 @@ on `V`, by the linear map `ρ g`. A map of representations from `ρ` to `σ` on commutes. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is invertible. It sends an invariant vector, `ρ g v = v`, to an invariant vector `φ v` (A). -For example, a Lorentz transformation `Λ` acts on vectors by `Λ` and on covectors by `(Λ⁻¹)ᵀ`, so -that `α_μ v^μ` is invariant. Lowering an index with the metric, `φ v = η v`, is a map of -representations from vectors to covectors. The two routes in the square give `η (Λ v)` and -`(Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the defining property of a Lorentz -transformation. - -Mathlib has the representation `ρ.tprod τ` on the tensor product of two representations, and the -tensor product `IntertwiningMap.tensor` of two intertwining maps. It has no representation on the -tensor product `⨂[k] i, M i` of a family, which is where a tensor with several indices lies. So -Mathlib's linear maps of `⨂`, such as `PiTensorProduct.reindex`, are not maps of representations. +For example, let `G` be the Lorentz group. It acts on the vectors `V` by `ρ Λ = Λ`, and on the +covectors `W` by `σ Λ = (Λ⁻¹)ᵀ`, so that `α_μ v^μ` is invariant. The map `φ` is lowering an index +with the metric, `φ v = η v`, from vectors to covectors. The two routes in the square give +`φ (ρ Λ v) = η (Λ v)` and `σ Λ (φ v) = (Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the +defining property of a Lorentz transformation. + +A tensor with several indices has one representation for each index, a family `ρ i` of +representations of `G` on spaces `M i`. For `T^μ_ν` the family is `Λ` on the vectors at the up index +and `(Λ⁻¹)ᵀ` on the covectors at the down index. The tensor lies in the tensor product +`⨂[k] i, M i` of the family. Mathlib has the representation `ρ.tprod τ` on the tensor product of +two representations, and the tensor product `IntertwiningMap.tensor` of two intertwining maps, but +no representation on the tensor product of a family. So Mathlib's linear maps of `⨂`, such as +`PiTensorProduct.reindex`, are not maps of representations. This file defines that representation, `piTprod ρ`, and proves that applying maps at the factors, reordering, grouping and contracting factors commute with it, so that they are maps of representations (C). From 1de6f6a4c09caa70519ac60607803c278f67a3e2 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 11:07:44 +0000 Subject: [PATCH 229/242] =?UTF-8?q?docs(ForMathlib):=20the=20Lorentz=20exa?= =?UTF-8?q?mple=20separates=20the=20group=20element=20g=20from=20its=20mat?= =?UTF-8?q?rix=20=CE=9B,=20in=20the=20Representation=20overview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index a267d95ef1..eb8672c77b 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -34,11 +34,12 @@ on `V`, by the linear map `ρ g`. A map of representations from `ρ` to `σ` on commutes. Mathlib calls it an `IntertwiningMap`, and an `Equiv` when it is invertible. It sends an invariant vector, `ρ g v = v`, to an invariant vector `φ v` (A). -For example, let `G` be the Lorentz group. It acts on the vectors `V` by `ρ Λ = Λ`, and on the -covectors `W` by `σ Λ = (Λ⁻¹)ᵀ`, so that `α_μ v^μ` is invariant. The map `φ` is lowering an index -with the metric, `φ v = η v`, from vectors to covectors. The two routes in the square give -`φ (ρ Λ v) = η (Λ v)` and `σ Λ (φ v) = (Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the -defining property of a Lorentz transformation. +For example, let `G` be the Lorentz group, and let `Λ` be the matrix of a Lorentz transformation +`g`. Then `g` acts on the vectors `V` by `ρ g = Λ`, and on the covectors `W` by `σ g = (Λ⁻¹)ᵀ`, so +that `α_μ v^μ` is invariant. The map `φ` is lowering an index with the metric, `φ v = η v`, from +vectors to covectors. The two routes in the square give `φ (ρ g v) = η (Λ v)` and +`σ g (φ v) = (Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the defining property of a Lorentz +transformation. A tensor with several indices has one representation for each index, a family `ρ i` of representations of `G` on spaces `M i`. For `T^μ_ν` the family is `Λ` on the vectors at the up index @@ -53,7 +54,7 @@ representations (C). In the same way, the Lorentz tensors `T^μ_νρ` lie in `V ⊗ V* ⊗ V*`, with `Λ` on the vectors `V` and `(Λ⁻¹)ᵀ` on the covectors `V*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping them as -`V ⊗ (V* ⊗ V*)`, and contracting `V` with the first `V*`, `T^μ_μρ`, all commute with `Λ`. So each +`V ⊗ (V* ⊗ V*)`, and contracting `V` with the first `V*`, `T^μ_μρ`, all commute with `g`. So each one sends an invariant tensor to an invariant tensor, and a tensor built from invariant ones by these maps is invariant with no further proof. From 7c8a1418a4fd78eb7ed405c6a6d378ed8ba54519 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 12:29:42 +0000 Subject: [PATCH 230/242] =?UTF-8?q?docs(ForMathlib):=20the=20Lorentz=20exa?= =?UTF-8?q?mple=20states=20the=20square=20as=20the=20matrix=20products=20?= =?UTF-8?q?=CE=B7=20=CE=9B=20=3D=20(=CE=9B=E2=81=BB=C2=B9)=E1=B5=80=20?= =?UTF-8?q?=CE=B7,=20and=20=E2=88=98=20is=20explained,=20in=20the=20Repres?= =?UTF-8?q?entation=20overview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index eb8672c77b..2271900fae 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -19,7 +19,9 @@ Mathlib's linear maps of such products into maps of representations. A representation `ρ` of a group or monoid `G` on a space `V` says how each element `g` of `G` acts on `V`, by the linear map `ρ g`. A map of representations from `ρ` to `σ` on `W` is a linear map -`φ : V → W` that commutes with the group, `φ ∘ ρ g = σ g ∘ φ` for every `g`, so that the square +`φ : V → W` that commutes with the group, `φ ∘ ρ g = σ g ∘ φ` for every `g`, where `∘` applies the +right-hand map first. So transforming and then mapping is the same as mapping and then transforming, +and the square ``` ρ g @@ -37,9 +39,9 @@ invariant vector, `ρ g v = v`, to an invariant vector `φ v` (A). For example, let `G` be the Lorentz group, and let `Λ` be the matrix of a Lorentz transformation `g`. Then `g` acts on the vectors `V` by `ρ g = Λ`, and on the covectors `W` by `σ g = (Λ⁻¹)ᵀ`, so that `α_μ v^μ` is invariant. The map `φ` is lowering an index with the metric, `φ v = η v`, from -vectors to covectors. The two routes in the square give `φ (ρ g v) = η (Λ v)` and -`σ g (φ v) = (Λ⁻¹)ᵀ (η v)`, which agree because `Λᵀ η Λ = η`, the defining property of a Lorentz -transformation. +vectors to covectors. Composing maps is multiplying their matrices, so the two routes in the square +are `φ ∘ ρ g = η Λ` and `σ g ∘ φ = (Λ⁻¹)ᵀ η`. They agree because `Λᵀ η Λ = η`, the defining +property of a Lorentz transformation. A tensor with several indices has one representation for each index, a family `ρ i` of representations of `G` on spaces `M i`. For `T^μ_ν` the family is `Λ` on the vectors at the up index From 3393c4de8f4232fd4ab651c28dee555757e71cff Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 12:32:24 +0000 Subject: [PATCH 231/242] =?UTF-8?q?docs(ForMathlib):=20section=20B=20of=20?= =?UTF-8?q?Representation=20defines=20=CF=86=20and=20=CF=88=20and=20explai?= =?UTF-8?q?ns=20tensorCongr=20by=20lowering=20both=20indices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 2271900fae..68c0e012bb 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -125,8 +125,15 @@ lemma Representation.IntertwiningMap.map_one_invariant ## B. Tensor products of equivalences -Mathlib has the tensor product `IntertwiningMap.tensor` of two intertwining maps. `tensorCongr` is -the same for equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, on `TensorProduct.congr`. +Let `φ` be a map of representations from `ρ` on `V` to `σ` on `W`, and `ψ` one from `τ` on `U` to +`π` on `X`. Their tensor product applies each map to its own factor, `v ⊗ u ↦ φ v ⊗ ψ u`, from +`V ⊗ U` to `W ⊗ X`. It commutes with the group because each factor does. For example, if `φ` and `ψ` +both lower an index with the metric, their tensor product lowers both indices, `T^μν ↦ T_μν`. + +Mathlib has this tensor product for intertwining maps, `IntertwiningMap.tensor`, and for invertible +linear maps, `TensorProduct.congr`, but not for equivalences of representations. `tensorCongr` gives +it, so the tensor product of two invertible maps of representations is again invertible. In the +example, raising both indices undoes the lowering. -/ From d25518ca75267e339a983d2b3fc42ed088698360 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:19:49 +0000 Subject: [PATCH 232/242] =?UTF-8?q?docs(ForMathlib):=20section=20D=20of=20?= =?UTF-8?q?Representation=20explains=20the=20two=20forms=20=CF=81=20g=20v?= =?UTF-8?q?=20and=20g=20=E2=80=A2=20v=20and=20why=20map=5Fsmul=5FofDistrib?= =?UTF-8?q?MulAction=20restates=20intertwining=20with=20the=20notation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index 68c0e012bb..d19033826b 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -315,8 +315,18 @@ end ## D. Representations given by actions -A representation can be given by an action, `Representation.ofDistribMulAction k G V`, with -`ρ g v = g • v`. Maps of such representations commute with the actions, `φ (g • v) = g • φ v`. +Mathlib writes a representation in two forms. A `Representation` `ρ` gives the linear map `ρ g` of +each element `g`. The notation `g • v`, a Mathlib `DistribMulAction`, is the same linear map +attached to the type of `v`, so that Lean finds it without `ρ` being named. +`Representation.ofDistribMulAction k G V` is the representation of such an action, +`ρ g v = g • v`. For example, `ρ g v` and `g • v` both stand for `Λ v`, the vector `v` transformed +by the matrix `Λ` of a Lorentz transformation `g`. + +Tensors are written with the notation, `g • T`, defined as the representation applied to `T`. Maps +of representations are stated with `ρ`, `φ (ρ g v) = σ g (φ v)`. `map_smul_ofDistribMulAction` +restates this with the notation, `φ (g • v) = g • φ v`. For example, lowering an index gives +`lower (g • T) = g • lower T`, so boosting and then lowering is the same as lowering and then +boosting. -/ From 12af4fc6bbe4b3ba6bc746e4e518a1bbec1f164a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:29:15 +0000 Subject: [PATCH 233/242] refactor(Tensors): remove map_smul_ofDistribMulAction; intertwiningMap_smul and equiv_smul use Mathlib's IntertwiningMap.isIntertwining Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 42 ------------------- Physlib/Mathematics/Tensors/Tensor.lean | 4 +- 2 files changed, 2 insertions(+), 44 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index d19033826b..a3c24abbbf 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -80,7 +80,6 @@ these maps is invariant with no further proof. - C. Tensor products of families - C.1. The representation on a tensor product of a family - C.2. Maps of representations -- D. Representations given by actions ## iv. References @@ -310,44 +309,3 @@ lemma Representation.IsIntertwiningMap.contrPairs {κ J : Type*} [Fintype J] | add x y hx hy => simp only [map_add, hx, hy] end - -/-! - -## D. Representations given by actions - -Mathlib writes a representation in two forms. A `Representation` `ρ` gives the linear map `ρ g` of -each element `g`. The notation `g • v`, a Mathlib `DistribMulAction`, is the same linear map -attached to the type of `v`, so that Lean finds it without `ρ` being named. -`Representation.ofDistribMulAction k G V` is the representation of such an action, -`ρ g v = g • v`. For example, `ρ g v` and `g • v` both stand for `Λ v`, the vector `v` transformed -by the matrix `Λ` of a Lorentz transformation `g`. - -Tensors are written with the notation, `g • T`, defined as the representation applied to `T`. Maps -of representations are stated with `ρ`, `φ (ρ g v) = σ g (φ v)`. `map_smul_ofDistribMulAction` -restates this with the notation, `φ (g • v) = g • φ v`. For example, lowering an index gives -`lower (g • T) = g • lower T`, so boosting and then lowering is the same as lowering and then -boosting. - --/ - -section - -variable [DistribMulAction G V] [SMulCommClass G k V] [DistribMulAction G W] [SMulCommClass G k W] - -/-- An intertwining map of representations given by actions commutes with the actions, -`φ (g • v) = g • φ v`. -/ -lemma Representation.IntertwiningMap.map_smul_ofDistribMulAction - (φ : (Representation.ofDistribMulAction k G V).IntertwiningMap - (Representation.ofDistribMulAction k G W)) (g : G) (v : V) : - φ (g • v) = g • φ v := - Representation.IntertwiningMap.isIntertwining _ _ φ g v - -/-- An equivalence of representations given by actions commutes with the actions, -`φ (g • v) = g • φ v`. -/ -lemma Representation.Equiv.map_smul_ofDistribMulAction - (φ : (Representation.ofDistribMulAction k G V).Equiv - (Representation.ofDistribMulAction k G W)) (g : G) (v : V) : - φ (g • v) = g • φ v := - φ.toIntertwiningMap.map_smul_ofDistribMulAction g v - -end diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index bd887cb132..3f5cc7ed35 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -212,13 +212,13 @@ with `•`. A tensor with no indices, a scalar, is invariant (`smul_eq_self_of_i lemma intertwiningMap_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).IntertwiningMap (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := - φ.map_smul_ofDistribMulAction g t + Representation.IntertwiningMap.isIntertwining _ _ φ g t /-- An equivalence of representations of tensors commutes with the actions, `φ (Λ T) = Λ φ T`. -/ lemma equiv_smul {ι' : Type} {c' : ι' → Color K} (φ : (S.tensorRep c).Equiv (S.tensorRep c')) (g : G) (t : Tensor S c) : φ (g • t) = g • φ t := - φ.map_smul_ofDistribMulAction g t + Representation.IntertwiningMap.isIntertwining _ _ φ.toIntertwiningMap g t /-- A linear or conjugate-linear map of tensors that commutes with `Λ` on pure tensors commutes with `Λ` on all tensors. -/ From aaca278889a67d5fb13b9317bbec08033bdacf1c Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:34:39 +0000 Subject: [PATCH 234/242] docs(ForMathlib): PiTensorProduct overview motivates the n-ary results with Lorentz examples, and sections A-D are described Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 096abb503b..06125cc9a8 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -13,25 +13,22 @@ public import Mathlib.LinearAlgebra.PiTensorProduct.Dual # Matrices of `PiTensorProduct.map`, dimension and contraction of `PiTensorProduct` -The matrix of a family of linear maps in the product basis of a `PiTensorProduct`, the dimension -of a `PiTensorProduct`, `PiTensorProduct.map` on an empty index type, and the contraction of pairs -of factors. +This file extends Mathlib's results on the tensor product of two spaces to the tensor product of a +family, and adds the contraction of its factors in pairs. ## i. Overview -`PiTensorProduct.map f` applies `f i` at every index `i`. In the product bases -`Basis.piTensorProduct b`, its matrix is the n-ary Kronecker product of the matrices of the `f i`: -its entry at `(φ, ψ)` is the product of the entries at `(φ i, ψ i)`. Mathlib has the binary case, -`TensorProduct.toMatrix_map`. +A tensor with several indices lies in the tensor product `⨂[R] i, M i` of a family of spaces, one +space `M i` for each index `i`. Mathlib proves several facts about the tensor product of two +spaces, `M ⊗ N`, that it does not prove for a family. This file proves them for a family: the +matrix of a map applied at every index (A), the dimension (B), and the case with no indices (C). It +also defines contraction, which pairs indices and removes them (D). -The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`, as -`Module.finrank_tensorProduct` for two factors. - -With no indices, `⨂[R] i, M i` is `R` and `PiTensorProduct.map f` is the identity. - -On the index type `κ ⊕ (J ⊕ J)`, a family of pairings `p j` of the factors at `inr (inl j)` and -`inr (inr j)` contracts `⨂[R] i, M i` to the tensor product of the factors at `inl x`, as a trace -`T^ρμ_μ` pairs an upper and a lower index. +For example, a Lorentz transformation acts on `T^μ_ν` by `Λ` at the up index and `(Λ⁻¹)ᵀ` at the +down index. Its matrix on the `16` components is the product of the two, with entries +`Λ^μ_σ (Λ⁻¹)^τ_ν`. The space of `T^μ_ν` has dimension `4 · 4 = 16`. A tensor with no indices is a +number, which no transformation changes. The trace `T^μ_μρ` contracts the first two indices and +keeps `ρ`. ## ii. Key results @@ -65,6 +62,12 @@ variable {ι R : Type*} {M N : ι → Type*} ## A. Matrices in the product basis +`PiTensorProduct.map f` applies the map `f i` at every index `i`. In the product bases +`Basis.piTensorProduct b`, its matrix entry at `(φ, ψ)` is the product over `i` of the entries of +the matrix of `f i` at `(φ i, ψ i)`. This is the Kronecker product of the matrices, for a family. +For example, the entry of the Lorentz transformation of `T^μ_ν` at `((μ, ν), (σ, τ))` is +`Λ^μ_σ (Λ⁻¹)^τ_ν`. Mathlib has two factors, `TensorProduct.toMatrix_map`. + -/ /-- The matrix of `PiTensorProduct.map f` in the product bases is the n-ary Kronecker product of @@ -84,6 +87,10 @@ lemma PiTensorProduct.toMatrix_map_apply [CommSemiring R] [∀ i, AddCommMonoid ## B. Dimension +The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`. For example, `T^μ_νρ` +in four dimensions has `4 · 4 · 4 = 64` components. Mathlib has two factors, +`Module.finrank_tensorProduct`. + -/ /-- The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`. -/ @@ -100,6 +107,10 @@ lemma Module.finrank_piTensorProduct [CommRing R] [StrongRankCondition R] [Finty ## C. Empty index types +With no indices, `⨂[R] i, M i` is the numbers `R`, Mathlib's `PiTensorProduct.isEmptyEquiv`, and a +map applied at every index applies nothing, so it is the identity. For example, a scalar is +unchanged by every Lorentz transformation. + -/ /-- With no indices, `PiTensorProduct.map f` is the identity. -/ @@ -113,6 +124,13 @@ lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoi ## D. Contraction of pairs of factors +The indices are arranged as `κ ⊕ (J ⊕ J)`. The indices `inl x`, for `x` in `κ`, are kept. Each `j` +in `J` is a pair of indices, `inr (inl j)` and `inr (inr j)`, contracted by a pairing `p j` to a +number. On a pure tensor, `contrPairs p` multiplies the kept vectors by the pairings of the pairs. +For example, the trace `T^μ_μρ` has `κ` the index `ρ` and one pair `j`, the up index `μ` and the +down index `μ`, with the pairing `α_μ v^μ`. Any arrangement of indices is put in this form by +reindexing. + -/ section From 5db08a4374dc96589d508ea2409b0a5376370d6e Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:44:17 +0000 Subject: [PATCH 235/242] docs(ForMathlib): PiTensorProduct motivates toMatrix_map_apply as the Kronecker product of one matrix per index Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 06125cc9a8..6fe6bfc820 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -24,9 +24,11 @@ spaces, `M ⊗ N`, that it does not prove for a family. This file proves them fo matrix of a map applied at every index (A), the dimension (B), and the case with no indices (C). It also defines contraction, which pairs indices and removes them (D). -For example, a Lorentz transformation acts on `T^μ_ν` by `Λ` at the up index and `(Λ⁻¹)ᵀ` at the -down index. Its matrix on the `16` components is the product of the two, with entries -`Λ^μ_σ (Λ⁻¹)^τ_ν`. The space of `T^μ_ν` has dimension `4 · 4 = 16`. A tensor with no indices is a +For example, `T^μ_ν` has `4 · 4 = 16` components, so its space has dimension `16`. A Lorentz +transformation acts on it index by index, by `Λ` at the up index and by `(Λ⁻¹)ᵀ` at the down index. +On the `16` components it is one `16 × 16` matrix, the Kronecker product `Λ ⊗ (Λ⁻¹)ᵀ`, whose entry +at `((μ, ν), (σ, τ))` is `Λ^μ_σ (Λ⁻¹)^τ_ν`. This gives the textbook formula +`T^μ_ν ↦ Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ` from the action index by index. A tensor with no indices is a number, which no transformation changes. The trace `T^μ_μρ` contracts the first two indices and keeps `ρ`. @@ -62,11 +64,16 @@ variable {ι R : Type*} {M N : ι → Type*} ## A. Matrices in the product basis -`PiTensorProduct.map f` applies the map `f i` at every index `i`. In the product bases -`Basis.piTensorProduct b`, its matrix entry at `(φ, ψ)` is the product over `i` of the entries of -the matrix of `f i` at `(φ i, ψ i)`. This is the Kronecker product of the matrices, for a family. -For example, the entry of the Lorentz transformation of `T^μ_ν` at `((μ, ν), (σ, τ))` is -`Λ^μ_σ (Λ⁻¹)^τ_ν`. Mathlib has two factors, `TensorProduct.toMatrix_map`. +`PiTensorProduct.map f` applies the map `f i` at every index `i`. It is defined without bases, so +its definition does not give its matrix. In the product bases `Basis.piTensorProduct b`, its matrix +is the Kronecker product of the matrices of the `f i`, whose entry at `(φ, ψ)` is the product over +`i` of the entries of the matrix of `f i` at `(φ i, ψ i)`. So the large matrix on the whole tensor +product is fixed by one small matrix per index. Mathlib proves this for the tensor product of two +spaces, `TensorProduct.toMatrix_map`, and this section proves it for a family. + +For example, a Lorentz transformation of `T^μ_ν` is `map` of `Λ` and `(Λ⁻¹)ᵀ`. Its entry at +`((μ, ν), (σ, τ))` is `Λ^μ_σ (Λ⁻¹)^τ_ν`, so the components transform as +`T^μ_ν ↦ Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ`. -/ @@ -88,8 +95,8 @@ lemma PiTensorProduct.toMatrix_map_apply [CommSemiring R] [∀ i, AddCommMonoid ## B. Dimension The dimension of `⨂[R] i, M i` is the product of the dimensions of the `M i`. For example, `T^μ_νρ` -in four dimensions has `4 · 4 · 4 = 64` components. Mathlib has two factors, -`Module.finrank_tensorProduct`. +in four dimensions has `4 · 4 · 4 = 64` components. Mathlib proves this for the tensor product of +two spaces, `Module.finrank_tensorProduct`, and this section proves it for a family. -/ From d32a4642b922e789d044bef8e001f9f45ea04217 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:48:06 +0000 Subject: [PATCH 236/242] docs(ForMathlib): PiTensorProduct section C explains map_eq_id_of_isEmpty, and a tensor with no indices is a scalar Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/PiTensorProduct.lean | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 6fe6bfc820..515763471f 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -29,7 +29,7 @@ transformation acts on it index by index, by `Λ` at the up index and by `(Λ⁻ On the `16` components it is one `16 × 16` matrix, the Kronecker product `Λ ⊗ (Λ⁻¹)ᵀ`, whose entry at `((μ, ν), (σ, τ))` is `Λ^μ_σ (Λ⁻¹)^τ_ν`. This gives the textbook formula `T^μ_ν ↦ Λ^μ_σ (Λ⁻¹)^τ_ν T^σ_τ` from the action index by index. A tensor with no indices is a -number, which no transformation changes. The trace `T^μ_μρ` contracts the first two indices and +scalar, which no transformation changes. The trace `T^μ_μρ` contracts the first two indices and keeps `ρ`. ## ii. Key results @@ -114,9 +114,10 @@ lemma Module.finrank_piTensorProduct [CommRing R] [StrongRankCondition R] [Finty ## C. Empty index types -With no indices, `⨂[R] i, M i` is the numbers `R`, Mathlib's `PiTensorProduct.isEmptyEquiv`, and a -map applied at every index applies nothing, so it is the identity. For example, a scalar is -unchanged by every Lorentz transformation. +A tensor with no indices is a scalar. Mathlib identifies `⨂[R] i, M i` over an empty index type +with `R`, by `PiTensorProduct.isEmptyEquiv`. Then a map applied at every index has no index to act +on, so `PiTensorProduct.map f` is the identity (`map_eq_id_of_isEmpty`). For example, a Lorentz +transformation leaves every scalar unchanged. -/ @@ -133,7 +134,7 @@ lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoi The indices are arranged as `κ ⊕ (J ⊕ J)`. The indices `inl x`, for `x` in `κ`, are kept. Each `j` in `J` is a pair of indices, `inr (inl j)` and `inr (inr j)`, contracted by a pairing `p j` to a -number. On a pure tensor, `contrPairs p` multiplies the kept vectors by the pairings of the pairs. +scalar. On a pure tensor, `contrPairs p` multiplies the kept vectors by the pairings of the pairs. For example, the trace `T^μ_μρ` has `κ` the index `ρ` and one pair `j`, the up index `μ` and the down index `μ`, with the pairing `α_μ v^μ`. Any arrangement of indices is put in this form by reindexing. From f1c66aab04c5b083e1a8165bc85a152d159ec1e9 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 13:50:40 +0000 Subject: [PATCH 237/242] docs(ForMathlib): PiTensorProduct overview says what the scalars R are Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/PiTensorProduct.lean | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 515763471f..a16f7f9c61 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -19,10 +19,12 @@ family, and adds the contraction of its factors in pairs. ## i. Overview A tensor with several indices lies in the tensor product `⨂[R] i, M i` of a family of spaces, one -space `M i` for each index `i`. Mathlib proves several facts about the tensor product of two -spaces, `M ⊗ N`, that it does not prove for a family. This file proves them for a family: the -matrix of a map applied at every index (A), the dimension (B), and the case with no indices (C). It -also defines contraction, which pairs indices and removes them (D). +space `M i` for each index `i`. Here `R` is the scalars, `ℝ` or `ℂ` in physics, and any commutative +semiring in the results below, with a commutative ring for the dimension (B). Mathlib proves several +facts about the tensor product of two spaces, `M ⊗ N`, that it does not prove for a family. This +file proves them for a family: the matrix of a map applied at every index (A), the dimension (B), +and the case with no indices (C). It also defines contraction, which pairs indices and removes them +(D). For example, `T^μ_ν` has `4 · 4 = 16` components, so its space has dimension `16`. A Lorentz transformation acts on it index by index, by `Λ` at the up index and by `(Λ⁻¹)ᵀ` at the down index. From 7619edc4043da78abe48fa448537d399407429d5 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 14:10:58 +0000 Subject: [PATCH 238/242] =?UTF-8?q?docs(ForMathlib):=20PiTensorProduct=20s?= =?UTF-8?q?ection=20D=20explains=20the=20arrangement=20=CE=BA=20=E2=8A=95?= =?UTF-8?q?=20(J=20=E2=8A=95=20J)=20of=20contrPairs,=20with=20the=20T^?= =?UTF-8?q?=CE=BC=5F=CE=BC=CF=81=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index a16f7f9c61..8bce6be1f4 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -134,12 +134,18 @@ lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoi ## D. Contraction of pairs of factors -The indices are arranged as `κ ⊕ (J ⊕ J)`. The indices `inl x`, for `x` in `κ`, are kept. Each `j` -in `J` is a pair of indices, `inr (inl j)` and `inr (inr j)`, contracted by a pairing `p j` to a -scalar. On a pure tensor, `contrPairs p` multiplies the kept vectors by the pairings of the pairs. -For example, the trace `T^μ_μρ` has `κ` the index `ρ` and one pair `j`, the up index `μ` and the -down index `μ`, with the pairing `α_μ v^μ`. Any arrangement of indices is put in this form by -reindexing. +Contraction pairs some indices of a tensor, turns each pair into a scalar, and keeps the other +indices. To say which indices are paired, the index type is `κ ⊕ (J ⊕ J)`. An index of `κ ⊕ (J ⊕ J)` +is tagged by Mathlib's `Sum.inl` or `Sum.inr`, for the left or the right side of `⊕`. An index +`inl x`, for `x` in `κ`, is kept. Each `j` in `J` is a pair, with first index `inr (inl j)` and +second index `inr (inr j)`. A pairing `p j` turns the two vectors of the pair `j` into a scalar. On +a pure tensor, `contrPairs p` keeps the vectors at the kept indices and multiplies them by the +product of the pairings (`contrPairs_tprod`). A tensor with its indices in another order is first +reordered into this form. + +For example, `T^μ_μρ` has three indices, up `μ`, down `μ` and down `ρ`. Its kept index is `ρ`, so +`κ` has one element. Its one pair is up `μ` with down `μ`, so `J` has one element. The pairing is +`α_μ u^μ`, and on a pure tensor the contraction is `u ⊗ α ⊗ β ↦ (α_μ u^μ) β`. -/ From 53590ae8e9b7c7e6f0c95e13198a5225643c6f92 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 14:27:58 +0000 Subject: [PATCH 239/242] =?UTF-8?q?docs(ForMathlib):=20PiTensorProduct=20s?= =?UTF-8?q?ection=20D=20explains=20the=20nesting=20of=20=CE=BA=20=E2=8A=95?= =?UTF-8?q?=20(J=20=E2=8A=95=20J),=20with=20the=20Ricci=20tensor=20and=20R?= =?UTF-8?q?icci=20scalar=20examples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 8bce6be1f4..83a21a65e4 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -135,17 +135,21 @@ lemma PiTensorProduct.map_eq_id_of_isEmpty [CommSemiring R] [∀ i, AddCommMonoi ## D. Contraction of pairs of factors Contraction pairs some indices of a tensor, turns each pair into a scalar, and keeps the other -indices. To say which indices are paired, the index type is `κ ⊕ (J ⊕ J)`. An index of `κ ⊕ (J ⊕ J)` -is tagged by Mathlib's `Sum.inl` or `Sum.inr`, for the left or the right side of `⊕`. An index -`inl x`, for `x` in `κ`, is kept. Each `j` in `J` is a pair, with first index `inr (inl j)` and -second index `inr (inr j)`. A pairing `p j` turns the two vectors of the pair `j` into a scalar. On -a pure tensor, `contrPairs p` keeps the vectors at the kept indices and multiplies them by the -product of the pairings (`contrPairs_tprod`). A tensor with its indices in another order is first -reordered into this form. - -For example, `T^μ_μρ` has three indices, up `μ`, down `μ` and down `ρ`. Its kept index is `ρ`, so -`κ` has one element. Its one pair is up `μ` with down `μ`, so `J` has one element. The pairing is -`α_μ u^μ`, and on a pure tensor the contraction is `u ⊗ α ⊗ β ↦ (α_μ u^μ) β`. +indices. The index type `κ ⊕ (J ⊕ J)` says which indices are paired, with Mathlib's tags `Sum.inl` +and `Sum.inr` for the left and the right side of `⊕`. The indices `inl x`, for `x` in `κ`, are kept. +Each `j` in `J` is a pair, with first index `inr (inl j)` and second index `inr (inr j)`, contracted +by the pairing `p j`. On a pure tensor, `contrPairs p` multiplies the kept vectors by the product of +the pairings (`contrPairs_tprod`). The nesting follows the construction. Mathlib's +`PiTensorProduct.tmulEquivDep` splits a tensor product over `A ⊕ B` into the tensor product over `A` +times the tensor product over `B`. Splitting at the outer `⊕` separates the kept factors from the +paired ones. Splitting at the inner `⊕` separates the first factor of each pair from the second, so +that `p j` can contract them. A tensor whose indices are in another order is first reordered into +this form. + +For example, the Ricci tensor `R_μν = R^ρ_μρν` pairs the first and third indices and keeps the +second and fourth, so its indices are reordered first. On a pure tensor it is +`u ⊗ α ⊗ β ⊗ γ ↦ (β_ρ u^ρ) α ⊗ γ`. The Ricci scalar `R = R^μν_μν` has two pairs and no kept index, +so the result is the scalar `(α_μ u^μ) (β_ν v^ν)` on `u ⊗ v ⊗ α ⊗ β`. -/ From 9ba35ae624b48edd6fc0c8fd09a3560242f2828a Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 14:35:56 +0000 Subject: [PATCH 240/242] docs(ForMathlib): PiTensorProduct no longer overstates, contrPairs takes only the standard arrangement and the dimension needs a well-defined rank Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/PiTensorProduct.lean | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean index 83a21a65e4..ad5a2cb010 100644 --- a/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -20,11 +20,11 @@ family, and adds the contraction of its factors in pairs. A tensor with several indices lies in the tensor product `⨂[R] i, M i` of a family of spaces, one space `M i` for each index `i`. Here `R` is the scalars, `ℝ` or `ℂ` in physics, and any commutative -semiring in the results below, with a commutative ring for the dimension (B). Mathlib proves several -facts about the tensor product of two spaces, `M ⊗ N`, that it does not prove for a family. This -file proves them for a family: the matrix of a map applied at every index (A), the dimension (B), -and the case with no indices (C). It also defines contraction, which pairs indices and removes them -(D). +semiring in the results below, with a commutative ring in which dimension is well defined, such as a +field, for the dimension (B). Mathlib proves several facts about the tensor product of two spaces, +`M ⊗ N`, that it does not prove for a family. This file proves them for a family: the matrix of a +map applied at every index (A) and the dimension (B). It also treats the case with no indices (C), +and defines contraction, which pairs indices and removes them (D). For example, `T^μ_ν` has `4 · 4 = 16` components, so its space has dimension `16`. A Lorentz transformation acts on it index by index, by `Λ` at the up index and by `(Λ⁻¹)ᵀ` at the down index. @@ -143,13 +143,14 @@ the pairings (`contrPairs_tprod`). The nesting follows the construction. Mathlib `PiTensorProduct.tmulEquivDep` splits a tensor product over `A ⊕ B` into the tensor product over `A` times the tensor product over `B`. Splitting at the outer `⊕` separates the kept factors from the paired ones. Splitting at the inner `⊕` separates the first factor of each pair from the second, so -that `p j` can contract them. A tensor whose indices are in another order is first reordered into -this form. +that `p j` can contract them. `contrPairs` takes only this arrangement. A tensor whose indices are +in another order is reordered into it first, with `PiTensorProduct.reindex`. For example, the Ricci tensor `R_μν = R^ρ_μρν` pairs the first and third indices and keeps the -second and fourth, so its indices are reordered first. On a pure tensor it is -`u ⊗ α ⊗ β ⊗ γ ↦ (β_ρ u^ρ) α ⊗ γ`. The Ricci scalar `R = R^μν_μν` has two pairs and no kept index, -so the result is the scalar `(α_μ u^μ) (β_ν v^ν)` on `u ⊗ v ⊗ α ⊗ β`. +second and fourth. Reordering takes `u ⊗ α ⊗ β ⊗ γ` to `α ⊗ γ ⊗ u ⊗ β`, with `α ⊗ γ` at the kept +indices and `u`, `β` the pair. The contraction then gives `(β_ρ u^ρ) α ⊗ γ`. The Ricci scalar +`R = R^μν_μν` has two pairs and no kept index, so the result is the scalar `(α_μ u^μ) (β_ν v^ν)` on +`u ⊗ v ⊗ α ⊗ β`. -/ From e173d2158ada90c67466d03552ec15d1509557e8 Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 14:36:40 +0000 Subject: [PATCH 241/242] =?UTF-8?q?docs(ForMathlib):=20tensorCongr=20docst?= =?UTF-8?q?ring=20names=20its=20vectors=20v=20=E2=8A=97=20u,=20as=20sectio?= =?UTF-8?q?n=20B=20of=20Representation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- Physlib/Mathematics/ForMathlib/Representation.lean | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index a3c24abbbf..e1c1e9e709 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -136,7 +136,7 @@ example, raising both indices undoes the lowering. -/ -/-- The tensor product of two equivalences of representations, `v ⊗ w ↦ φ v ⊗ ψ w`, Mathlib's +/-- The tensor product of two equivalences of representations, `v ⊗ u ↦ φ v ⊗ ψ u`, Mathlib's `TensorProduct.congr` with the actions `ρ ⊗ τ` and `σ ⊗ π`. -/ noncomputable def Representation.Equiv.tensorCongr (φ : ρ.Equiv σ) (ψ : τ.Equiv π) : (ρ.tprod τ).Equiv (σ.tprod π) := From 87928e53c0c51e4d04befd52830a8f8e454ea19f Mon Sep 17 00:00:00 2001 From: Andrea Pari Date: Fri, 13 Nov 2026 14:56:41 +0000 Subject: [PATCH 242/242] =?UTF-8?q?docs(Tensors):=20piece-2=20review=20fix?= =?UTF-8?q?es,=20no=20overstatements,=20covector=20action=20(=CE=9B?= =?UTF-8?q?=E2=81=BB=C2=B9)=E1=B5=80=CE=B1,=20smul=5Fpure=20docstring,=20t?= =?UTF-8?q?ensorCongr=20in=20key=20results?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 5.5 --- .../ForMathlib/Representation.lean | 16 ++++---- Physlib/Mathematics/Tensors/Tensor.lean | 39 +++++++++---------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Physlib/Mathematics/ForMathlib/Representation.lean b/Physlib/Mathematics/ForMathlib/Representation.lean index e1c1e9e709..679e49aa27 100644 --- a/Physlib/Mathematics/ForMathlib/Representation.lean +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -45,14 +45,13 @@ property of a Lorentz transformation. A tensor with several indices has one representation for each index, a family `ρ i` of representations of `G` on spaces `M i`. For `T^μ_ν` the family is `Λ` on the vectors at the up index -and `(Λ⁻¹)ᵀ` on the covectors at the down index. The tensor lies in the tensor product -`⨂[k] i, M i` of the family. Mathlib has the representation `ρ.tprod τ` on the tensor product of -two representations, and the tensor product `IntertwiningMap.tensor` of two intertwining maps, but -no representation on the tensor product of a family. So Mathlib's linear maps of `⨂`, such as -`PiTensorProduct.reindex`, are not maps of representations. -This file defines that representation, `piTprod ρ`, and proves that applying maps at the factors, -reordering, grouping and contracting factors commute with it, so that they are maps of -representations (C). +and `(Λ⁻¹)ᵀ` on the covectors at the down index. The tensor lies in the tensor product `⨂[k] i, M i` +of the family. Mathlib has the representation `ρ.tprod τ` on the tensor product of two +representations, and the tensor product `IntertwiningMap.tensor` of two intertwining maps, but no +representation on the tensor product of a family. So Mathlib's linear maps of `⨂`, such as +`PiTensorProduct.reindex`, are not stated as maps of representations. This file defines that +representation, `piTprod ρ`, and proves that applying maps at the factors, reordering, grouping and +contracting factors commute with it, so that they are maps of representations (C). In the same way, the Lorentz tensors `T^μ_νρ` lie in `V ⊗ V* ⊗ V*`, with `Λ` on the vectors `V` and `(Λ⁻¹)ᵀ` on the covectors `V*`. Reordering the factors to `V* ⊗ V ⊗ V*`, grouping them as @@ -62,6 +61,7 @@ these maps is invariant with no further proof. ## ii. Key results +- `Representation.Equiv.tensorCongr` : the tensor product of two equivalences of representations. - `Representation.piTprod` : the representation on `⨂[k] i, M i` of a family of representations. - `Representation.IsIntertwiningMap.piTprodMap` and `Representation.Equiv.piTprodCongr` : `PiTensorProduct.map` and `PiTensorProduct.congr` of maps of representations. diff --git a/Physlib/Mathematics/Tensors/Tensor.lean b/Physlib/Mathematics/Tensors/Tensor.lean index 3f5cc7ed35..fcc3ba08ec 100644 --- a/Physlib/Mathematics/Tensors/Tensor.lean +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -27,14 +27,13 @@ with no renumbering into `Fin (n + m)`. For example, the Lorentz colors `![⟨vec, up⟩, ⟨vec, up⟩]` give the tensors `T = T^μν e_μ ⊗ e_ν`, and for two vectors `u` and `v` the pure tensor `u ⊗ v` has the components `u^μ v^ν`. -The operations on tensors in the rest of the library are built from two kinds of linear map defined -here. A map at every index (D.1) acts on each index, as a Lorentz transformation does, -`(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the representation of the color of each index. A map at one index -(D.2) changes that index and keeps the others, as lowering the index `ν` with the metric does, -`T^μ_ν = η_νρ T^μρ`, which changes its color to `⟨vec, down⟩`. Since the metric is invertible and -invariant under the group, lowering is an equivalence of representations, with raising as its -inverse (D.4). Reindexing uses the map at every index, and contracting an index with a tensor on -two indices, `T^…μ… F_μ^ν`, is a map at that index. +Many operations on tensors are built from two kinds of linear map defined here. A map at every index +(D.1) acts on each index, as a Lorentz transformation does, `(ΛT)^μν = Λ^μ_σ Λ^ν_τ T^στ`, by the +representation of the color of each index. A map at one index (D.2) changes that index and keeps the +others, as lowering the index `ν` with the metric does, `T^μ_ν = η_νρ T^μρ`, which changes its color +to `⟨vec, down⟩`. Since the metric is invertible and invariant under the group, lowering is an +equivalence of representations, with raising as its inverse (D.4). Reindexing uses the map at every +index, and contracting an index with a tensor on two indices, `T^…μ… F_μ^ν`, is a map at that index. Each map is fixed by its values on pure tensors such as `u ⊗ v`. So its laws, such as composition and commuting with the group (B.2), are proved once here, and not again for each operation. The @@ -99,8 +98,8 @@ becomes an equality of their values on `S.pure p`, for any family of vectors `p` -/ /-- The tensors with colors `c : ι → Color K`, the space `⨂[k] i, S.V (c i)`. It is a `def`, not an -`abbrev`, so that goals show `Tensor S c` instead of the product. Mathlib's results on `⨂` still -apply to it, since Lean unfolds the definition. -/ +`abbrev`, so that goals show `Tensor S c` instead of the product. Mathlib's results on `⨂` apply to +it by unfolding, and its instances are copied from `⨂`. -/ def _root_.Tensor (S : Species k G K basisIdx) {ι : Type} (c : ι → Color K) : Type := ⨂[k] i, S.V (c i) @@ -142,15 +141,14 @@ lemma ext_pure {E : Type*} [AddCommMonoid E] [Module k E] {f g : Tensor S c → ### B.1. The action on tensors A group element `Λ` acts on every index by the representation of its color, -`Λ (u ⊗ α) = Λu ⊗ Λα` (`smul_pure`). This is the representation `S.tensorRep c`, -`Representation.piTprod` on `⨂[k] i, S.V (c i)`. It is written `Λ • T` under -`open scoped Tensor`. +`Λ • (u ⊗ α) = Λu ⊗ (Λ⁻¹)ᵀα` (`smul_pure`). This is the representation `S.tensorRep c`, +`Representation.piTprod` on `⨂[k] i, S.V (c i)`. It is written `Λ • T` under `open scoped Tensor`. -/ variable (S) in /-- The action of the group on tensors, on every index by the representation of its color, -`Λ (u ⊗ α ⊗ …) = Λu ⊗ Λα ⊗ …`. It is `Representation.piTprod` of the representations of the +`Λ • (u ⊗ α ⊗ …) = Λu ⊗ (Λ⁻¹)ᵀα ⊗ …`. It is `Representation.piTprod` of the representations of the colors. Maps of tensors that commute with the action are Mathlib's `Representation.Equiv` and `Representation.IntertwiningMap` for it. -/ noncomputable abbrev tensorRep (c : ι → Color K) : Representation k G (Tensor S c) := @@ -171,6 +169,7 @@ noncomputable abbrev smul : SMul G (Tensor S c) := distribMulAction.toSMul scoped[Tensor] attribute [instance 1100] Tensor.Species.smul +/-- On a pure tensor the group acts on each vector by the representation of its color. -/ @[simp] lemma smul_pure (g : G) (f : Π i, S.V (c i)) : g • S.pure f = S.pure fun i => S.rep (c i) g (f i) := @@ -385,12 +384,12 @@ lemma congrIndices_pure (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) (p : Π x ### D.2. A linear map at one index -`mapAt i f h` applies a linear map `f` at the index `i` and keeps the others, `u ⊗ v ↦ u ⊗ f v`. -The map `f` may change the color at `i`, as lowering turns `⟨vec, up⟩` into `⟨vec, down⟩`. So the -result has other colors `c'`, and `h : ∀ x, x ≠ i → c x = c' x` says that they agree with `c` at -the other indices. There the vectors are moved along `h` by `S.cast`. `indexMapsAt i f h` is the -family with `f` at `i` and `S.cast` elsewhere, and `mapAt` is `mapIndices` of it. Contracting with -a tensor `F` on two indices is `mapAt` of `F` read as a linear map (`contr_matMulAt`). +`mapAt i f h` applies a linear map `f` at the index `i` and keeps the others, `u ⊗ v ↦ u ⊗ f v`. The +map `f` may change the color at `i`, as lowering turns `⟨vec, up⟩` into `⟨vec, down⟩`. So the result +has other colors `c'`, and `h : ∀ x, x ≠ i → c x = c' x` says that they agree with `c` at the other +indices. There the vectors are moved along `h` by `S.cast`. `indexMapsAt i f h` is the family with +`f` at `i` and `S.cast` elsewhere, and `mapAt` is `mapIndices` of it. Contracting with a tensor `F` +on two indices is `mapAt` of `F` read as a linear map. -/