diff --git a/Physlib.lean b/Physlib.lean index 595b82e762..1b21a19840 100644 --- a/Physlib.lean +++ b/Physlib.lean @@ -151,10 +151,13 @@ 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.EquivPairs 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 @@ -165,8 +168,13 @@ 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 +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 @@ -191,7 +199,21 @@ 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.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.LinearMap +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 public import Physlib.Mathematics.VariationalCalculus.Basic public import Physlib.Mathematics.VariationalCalculus.HasVarAdjDeriv public import Physlib.Mathematics.VariationalCalculus.HasVarAdjoint @@ -457,6 +479,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 @@ -509,6 +532,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 @@ -551,6 +575,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/Mathematics/ForMathlib/CoordTensorHom.lean b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean new file mode 100644 index 0000000000..0e9bad6aef --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/CoordTensorHom.lean @@ -0,0 +1,394 @@ +/- +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.BilinearForm.Properties +public import Mathlib.RepresentationTheory.Invariants +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. 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 +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. + +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`. +- `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. +- `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`. +- `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. +- `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 + +* None. + +-/ + +@[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`. -/ +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] + +/-- 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') + (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 [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] + +/-- 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 + -- 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, + 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 + +-/ + +/-- 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] + +/-! + +## 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 + +/-! + +## 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/ForMathlib/EquivPairs.lean b/Physlib/Mathematics/ForMathlib/EquivPairs.lean new file mode 100644 index 0000000000..6308722636 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/EquivPairs.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.Logic.Equiv.Defs + +/-! + +# 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 + +* None. + +-/ + +@[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/ForMathlib/FiniteDimensional.lean b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean new file mode 100644 index 0000000000..856a1b231a --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/FiniteDimensional.lean @@ -0,0 +1,74 @@ +/- +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_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`. + +## iii. Table of contents + +- A. Left and right inverses + +## iv. References + +* None. + +-/ + +@[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 := + 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 + +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/Mathematics/ForMathlib/PiTensorProduct.lean b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean new file mode 100644 index 0000000000..ad5a2cb010 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/PiTensorProduct.lean @@ -0,0 +1,182 @@ +/- +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.Matrix.ToLin +public import Mathlib.LinearAlgebra.Dimension.Free +public import Mathlib.LinearAlgebra.PiTensorProduct.Dual + +/-! + +# Matrices of `PiTensorProduct.map`, dimension and contraction of `PiTensorProduct` + +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 + +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 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. +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 +scalar, which no transformation changes. The trace `T^μ_μρ` contracts the first two indices and +keeps `ρ`. + +## 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 + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +variable {ι R : Type*} {M N : ι → Type*} + +/-! + +## A. Matrices in the product basis + +`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^σ_τ`. + +-/ + +/-- 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] + +/-! + +## 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 proves this for the tensor product of +two spaces, `Module.finrank_tensorProduct`, and this section proves it for a family. + +-/ + +/-- 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] + +/-! + +## C. Empty index types + +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. + +-/ + +/-- 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] + +/-! + +## D. Contraction of pairs of factors + +Contraction pairs some indices of a tensor, turns each pair into a scalar, and keeps the other +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. `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. 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 ⊗ α ⊗ β`. + +-/ + +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 new file mode 100644 index 0000000000..679e49aa27 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/Representation.lean @@ -0,0 +1,311 @@ +/- +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 +public import Physlib.Mathematics.ForMathlib.PiTensorProduct + +/-! + +# Tensor products of representations and invariant vectors + +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 + +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`, where `∘` applies the +right-hand map first. So transforming and then mapping is the same as mapping and then transforming, +and 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). + +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. 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 +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 +`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. + +## 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. +- `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. + +## 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. Maps of representations + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct + +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} + +/-! + +## A. Invariant vectors + +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`). + +-/ + +/-- 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] + +/-- 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 := + hv ▸ φ.map_invariant rfl + +/-! + +## B. Tensor products of equivalences + +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. + +-/ + +/-- 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 π) := + .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 + +/-! + +## C. Tensor products of families + +### C.1. The representation on a tensor product of a family + +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`. + +-/ + +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 _ _ + +/-- 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 + +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. +- `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. + +-/ + +/-- `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)} + {σ : Π 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.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.piTprodMap fun i => + ⟨(φ i).toIntertwiningMap.isIntertwining⟩).isIntertwining g) + +@[simp] +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.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.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.piTprodReindex_tprod {ι' : Type*} (ρ : Π i, Representation k G (M i)) + (e : ι ≃ ι') (m : Π i, M i) : + 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 `ι₁ ⊕ ι₂`, +`(⨂ 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 + +/-- `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/ForMathlib/Schouten.lean b/Physlib/Mathematics/ForMathlib/Schouten.lean new file mode 100644 index 0000000000..2b566fca6c --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/Schouten.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.LinearAlgebra.SesquilinearForm.Basic +import Mathlib.Tactic.LinearCombination + +/-! + +# 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. +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 + +- A. Alternating forms in two dimensions + +## iv. References + +* None. + +-/ + +@[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 + +/-- 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/ForMathlib/SemilinearMaps.lean b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean new file mode 100644 index 0000000000..988c936857 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/SemilinearMaps.lean @@ -0,0 +1,252 @@ +/- +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. 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. + +## ii. Key results + +- `PiTensorProduct.mapₛₗ` applies a family of semilinear maps to a tensor product, and + `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, 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 + +- A. Semilinear maps of tensor products +- B. Conjugating the coordinates of a basis +- C. The rank under a semilinear equivalence + +## 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 + +/-- 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 + +/-! + +## 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 + +/-- 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 [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 + +/-! + +## 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/ForMathlib/SesquilinearForm.lean b/Physlib/Mathematics/ForMathlib/SesquilinearForm.lean index 5c18d593be..b4229964dd 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 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 `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 surjective `A`, then `B'` is positive-definite when `B` +is. -/ +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 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]) + +end comp + +/-! + +### A.4. The matrix of a positive-definite form -/ diff --git a/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean new file mode 100644 index 0000000000..a2b8fc3538 --- /dev/null +++ b/Physlib/Mathematics/ForMathlib/TensorProductMatrix.lean @@ -0,0 +1,135 @@ +/- +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_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.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 + +- A. The action on coefficient matrices +- B. Swapping the factors + +## iv. References + +* None. + +-/ + +@[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 + +/-- 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 [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 [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] + +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] + +/-- 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/Conjugation.lean b/Physlib/Mathematics/Tensors/Conjugation.lean new file mode 100644 index 0000000000..dd98105d89 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Conjugation.lean @@ -0,0 +1,313 @@ +/- +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.SemilinearMaps +public import Physlib.Mathematics.Tensors.Contraction +/-! + +# Conjugation of tensors + +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 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 +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 index. + +## ii. Key results + +- `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` 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. +- `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 + +- 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 + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensor + +/-! + +## A. Conjugate colors + +-/ + +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] + +/-- 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] + +/-- 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 + +-/ + +/-- 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 μ) = μ + +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 +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' + /-- 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] + +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_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_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 α + +/-- 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 + +/-! + +## C. Componentwise conjugation + +-/ + +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, with the labels read by `e`. -/ +@[instance_reducible] +noncomputable def HasConjugation.ofBasis (e : ConjLabels basisIdx) : + S.HasConjugation where + 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 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 + (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 + 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 `rep_basis` gives equivariance. + 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 + +/-- The componentwise conjugate of the basis vector `e_μ` is the basis vector `e_{e μ}`. -/ +lemma HasConjugation.ofBasis_conjEquiv_basis (e : ConjLabels basisIdx) + (h : c' = c.bar) (μ : basisIdx c.kind) : + 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, +`(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] + +end Species + +open Species + +/-! + +## D. Conjugation of tensors + +-/ + +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) : Tensor S c ≃ₗ⋆[k] Tensor S c' := + PiTensorProduct.congrₛₗ fun i => S.conjEquiv (h i) + +/-- The conjugate of a pure tensor conjugates each factor, `(u ⊗ α)‾ = ū ⊗ ᾱ`. -/ +@[simp] +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 : Tensor S c) : + conj h' (conj h t) = t := + PiTensorProduct.congrₛₗ_congrₛₗ _ _ (fun i => conjEquiv_conjEquiv (h i) (h' i)) t + +/-! + +### D.1. The group action and contraction + +-/ + +/-- 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 : 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 + +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 : 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] + | 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_conjEquiv rfl, conjEquiv_cast, cast_conjEquiv] + rfl + +/-! + +### 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 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) + (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 + 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 diff --git a/Physlib/Mathematics/Tensors/Contraction.lean b/Physlib/Mathematics/Tensors/Contraction.lean new file mode 100644 index 0000000000..32cee64e55 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Contraction.lean @@ -0,0 +1,422 @@ +/- +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.EquivPairs +public import Physlib.Mathematics.Tensors.Product +/-! + +# 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 properties. + +## 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 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 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 ())`. 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 +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 + 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)^μ_μν^ρ`. + +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 + +- `Tensor.contr` contracts the pairs of indices chosen by `σ`, as in `T^ρμ_μ`, with the + colors `contrColors` of the free indices and the pairs. +- `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. +- `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 + `Tensor.contr_tmulEquiv_eq_zero_of_antisymm_symm` prove `A^μν B_μν = 0` for `A` + antisymmetric and `B` symmetric. +- `Tensor.contr_contr` proves that two contractions in sequence are one, along + `ColorEquiv.chain`. +- `Tensor.tmulEquiv_contr_left` and `Tensor.tmulEquiv_contr_right` prove that + contracting a factor commutes with the product, along `ColorEquiv.tmulRight` and + `ColorEquiv.tmulLeft`. +- `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 + +- A. Choices of pairs of indices +- B. Contraction in the standard arrangement of indices +- C. Contraction of chosen pairs of indices + - C.1. Reindexing + - 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 + +* None. + +-/ + +@[expose] public section + +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 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 := (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 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 + +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} + +open Species + +/-! + +## B. Contraction in the standard arrangement of indices + +-/ + +/-- 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 `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) := + ⟨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`. -/ +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) := + PiTensorProduct.contrPairs_tprod _ f + +/-! + +## C. 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 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)) : + (S.tensorRep c).IntertwiningMap (S.tensorRep c') := + (contrStd c' a).comp (S.reindex σ).toIntertwiningMap + +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 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)) : + contr σ (S.pure f) = + (∏ 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 _) + +/-- A contraction with no pairs is the identity. -/ +lemma contr_noPairs (t : Tensor S c) : contr (ColorEquiv.noPairs c) t = t := + 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 + +/-! + +### C.1. Reindexing + +-/ + +/-- 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 : Tensor S c) : + contr σ (S.reindex τ t) = contr (τ.trans σ) t := by + simp only [contr, Representation.IntertwiningMap.comp_apply, + 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 : Tensor S c) : + S.reindex τ (contr σ t) = contr (σ.trans (τ.sumCongr (.refl _))) 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 + +/-! + +### C.2. Contractions of the same pairs of indices + +-/ + +/-- 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. -/ +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', σ.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 + simp only [Representation.IntertwiningMap.coe_toLinearMap] + 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 (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) _) + +/-! + +### C.3. Symmetric against antisymmetric + +-/ + +/-- 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` 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))) + {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) := + 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 + conv_lhs => enter [1]; rw [ht] + rw [contr_reindex, map_neg, hσ, neg_add_cancel] + 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 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₁) + (π₂ : ColorEquiv c₂ c₂) (e : J ≃ J) + (hπ : (π₁.sumCongr π₂).toEquiv.trans σ.toEquiv = + σ.toEquiv.trans ((Equiv.refl κ).sumCongr (e.sumCongr e))) + {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]) + +/-! + +### C.4. Two contractions are one + +-/ + +/-- 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 : Tensor S c) : + contr σ₂ (contr σ₁ t) = contr (σ₁.chain σ₂) t := + 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 + +/-! + +### C.5. Products with a tensor that is not contracted + +-/ + +/-- 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 : 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 _) + (g := (contr (σ.tmulRight d)).toLinearMap ∘ₗ (S.tmulEquiv c d).toLinearMap) + (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 + exact congrArg S.pure (funext fun y => by rcases y with x | m <;> rfl)) (t ⊗ₜ u) + +/-- 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 : 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 _) + (g := (contr (σ.tmulLeft d)).toLinearMap ∘ₗ (S.tmulEquiv d c).toLinearMap) + (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 + exact congrArg S.pure (funext fun y => by rcases y with x | m <;> rfl)) (u ⊗ₜ t) + +/-! + +## D. Components + +-/ + +/-- 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 : 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 + | 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 `μ`. + 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, + 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, 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 : 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 μ μ)) := + tensorBasis_repr_contrStd c' a _ φ + +/-- 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 : 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 + rw [tensorBasis_repr_contr] + exact Fintype.sum_equiv (Equiv.funUnique Unit (basisIdx (a ()).kind)) _ _ fun _ => rfl + +/-- 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 : 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 φ + (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 new file mode 100644 index 0000000000..783171932a --- /dev/null +++ b/Physlib/Mathematics/Tensors/Elab.lean @@ -0,0 +1,480 @@ +/- +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.Inverse +public import Physlib.Mathematics.Tensors.Tensorial +/-! + +# Index notation + +Tensor expressions with named indices, such as `{T | μ ν ⊗ U | ν ρ}ᵀ` for `T^μ_ν U^ν_ρ`. + +## i. Overview + +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 : 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. + +- `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 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` reindex `B` by `S.reindex` when its names come in another order. Each name + must have the same color on both sides. +- `τ(μ)` 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 | μ ν`, + `Λ • 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 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 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 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 +`{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 + +- `{…}ᵀ` : index notation, elaborated by `elabTensorIndex`. + +## iii. Table of contents + +- A. Syntax +- B. Elaboration + - B.1. Index types + - B.2. The steps of an expression + - B.3. The elaborator + +## iv. References + +* None. + +-/ + +public meta section + +open Lean Meta Elab Term + +namespace Tensor.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 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 +/-- A tensor with a name on each index. -/ +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 + +end Tensor.Notation + +namespace Tensor + +/-- Index notation, active under `open scoped Tensor`. -/ +scoped syntax (name := tensorIndexSyntax) "{" tensorIndexExpr "}ᵀ" : term + +end Tensor + +namespace Tensor.Notation + +/-! + +## B. Elaboration + +### B.1. Index types + +-/ + +/-- 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 + +/-- The number of indices. -/ +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`, `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` with a literal `n`, `Unit`, `Empty` and `⊕`. -/ +partial def Indices.ofType? (ι : Expr) : MetaM (Option Indices) := do + 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) + | 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 +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 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))) + +/-- 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 + 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 +`Tensor S (ι := ι) c`. -/ +def tensorTypeArgs? (ty : Expr) : Option (Expr × Expr × Expr) := + let_expr Tensor _ _ _ _ _ _ _ _ S ι c := ty | none + some (S, ι, c) + +/-- 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 + let some indices ← Indices.ofType? ι | return none + let c ← instantiateMVars c + let some cs ← colorList? c | return none + return (indices, c, cs) + +/-! + +### B.2. The steps of an expression + +-/ + +/-- A partly elaborated expression, with the name and color of each index, its index type, its +tensor and its species. -/ +structure Node where + /-- The name of each index. -/ + names : List Name + /-- The color of each index. -/ + colors : List Expr + /-- The index type. -/ + indices : Indices + /-- The tensor. -/ + tm : Term + /-- The species. -/ + species : Expr + +/-- 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) + (Tensor.ColorEquiv.mk (c₂ := $(← colorsTerm cs)) + $(← indexEquiv a.indices (.fin pre.length) pre)) + $a.tm) + +/-- 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 + return { a with indices := .fin n, tm := ← a.reindex (List.range n) a.colors } + +/-- 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 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)) + ($(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 ty ← whnfR (← instantiateMVars (← inferType e)) + if ty.isMVar then + 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 + 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 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 + 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" + 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 + | `(tensorIndex| τ($x:ident)) => pure (x.getId, true) + | _ => throwError "index notation: unsupported index {i}" + +/-- 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 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` 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, 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 + +/-- 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 + 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 ``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₂)] + | _ => 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 _ => Indices.sum J .unit) .unit + -- 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]!) + let tm ← `(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⟩ + +/-- 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 + 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 + r.reindex (l.names.map r.names.idxOf) l.colors + +/-! + +### B.3. The elaborator + +-/ + +mutual + +/-- 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 + | `(tensorIndexExpr| $a:tensorIndexExpr ⊗ $b:tensorIndexExpr) => do + let a ← uncontracted a + 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.indices b.indices, 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 + +/-- 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 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} 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 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 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 + let ty ← instantiateMVars ty + if ty.getAppFn.isMVar || ty.isSort then return (← build e).tm + if (← whnfR ty).isAppOf ``Tensor then + 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 + let some tensorTy ← try withoutErrToSorry do + 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 + `((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 + 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 (← buildFor e expectedType?) expectedType? + | _ => throwUnsupportedSyntax + +end Tensor.Notation + +end diff --git a/Physlib/Mathematics/Tensors/ElabExamples.lean b/Physlib/Mathematics/Tensors/ElabExamples.lean new file mode 100644 index 0000000000..7901ba15f5 --- /dev/null +++ b/Physlib/Mathematics/Tensors/ElabExamples.lean @@ -0,0 +1,483 @@ +/- +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.ElabTactics + +/-! + +# Examples of index notation + +Examples and tests of the index notation `{…}ᵀ` and of the tactics `tensor_components`, +`tensor_regroup` and `tensor_indices`. + +## 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. 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 + +* None. + +-/ + +namespace Tensor.Notation + +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 : Tensor S ![a, a.dual]) (T' : Tensor S ![a.dual, a]) + (κ : K) (v : Tensor S ![⟨κ, .up⟩]) (r : k) (Λ : G) + +/-! + +## A. Products, sums and contractions + +-/ + +/-- info: T : Tensor S ![a, a.dual] -/ +#guard_msgs in +#check {T | μ ν}ᵀ + +/-- The matrix product `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. -/ +example : {S.tmulEquiv _ _ (T ⊗ₜ U) | μ ν ν ρ = T | μ ν ⊗ U | ν ρ}ᵀ := rfl + +/-- 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 : 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 + +/-- 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 : 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 : Tensor S (![] : 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 : Tensor S ![…])` -/ +#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 : 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` +and `⊕` -/ +#guard_msgs (whitespace := lax) in +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 -/ +#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 + +/-! + +## 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 index is rejected. +/-- error: index notation: ↑μ needs a visibly lower index -/ +#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 : 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 : Tensor S ![⟨κ, .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 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] + congr 2 + ext x + fin_cases x <;> rfl + +/-! + +## 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 : 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 : Tensor S ![a, a.dual] := {m | μ ν ⊗ T | ν ρ}ᵀ + +/-- The contraction `m^μ_ν m^ν_ρ` inside `M ⊗ M`, through `Tensorial.prod`. -/ +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 | ν ρ}ᵀ + +open TensorProduct in +/-- 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` reindexed onto the indices `Fin 1 ⊕ Fin 1` of a product. -/ +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 : Tensor S (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 + +/-- `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 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 index 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 + +/-- 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 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 + 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. 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 | μ ρ}ᵀ) : + {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 + +/-! + +## E. Regrouping with forms + +-/ + +/-- 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, `g^ρν M^μ_ν = M^μρ`. -/ +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 : 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 : 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 : 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 : 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 : Tensor S ![⟨κ, .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 : Tensor S ![⟨κ, .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 : Tensor S ![⟨κ, .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. 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 : 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 : 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 : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : + {M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ν ρ = M | μ ν ⊗ S.formTensor ⟨κ, .down⟩ | ρ ν}ᵀ := by + tensor_regroup + +/-- `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 delta on the left side, `δ^μ_ρ = g^μν g_νρ`. -/ +example : {S.deltaTensor c.dual | μ ρ = S.formTensor c | μ ν ⊗ S.formTensor c.dual | ν ρ}ᵀ := by + tensor_regroup + +/-! + +## F. Chains of lowerings and raisings + +-/ + +/-- Lowering and then raising by `tensor_indices`, `(v_μ)^μ = v^μ`. -/ +example : {{v | ↓μ}ᵀ | ↑μ}ᵀ = v := by + tensor_indices + +/-- `tensor_indices` with the chain on the right side, `v^μ = (v_μ)^μ`. -/ +example : v = {{v | ↓μ}ᵀ | ↑μ}ᵀ := by + tensor_indices + +-- An equation with no lowering or raising is rejected. +/-- error: tensor_indices: neither side lowers or raises an index -/ +#guard_msgs in +example (w : Tensor S ![⟨κ, .up⟩]) : v = w := by + tensor_indices + +/-- Lowering both indices and raising them again, `(M_μν)^μν = M^μν`. -/ +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ↓ν}ᵀ | ↑μ ↑ν}ᵀ = M := by + tensor_indices + +/-- Lowerings at two indices commute, `(M_μ^ν)_ν = (M^μ_ν)_μ`. -/ +example (M : Tensor S ![⟨κ, .up⟩, ⟨κ, .up⟩]) : {{M | ↓μ ν}ᵀ | μ ↓ν = {M | μ ↓ν}ᵀ | ↓μ ν}ᵀ := by + tensor_indices + +end Metric + +end Tensor.Notation diff --git a/Physlib/Mathematics/Tensors/ElabTactics.lean b/Physlib/Mathematics/Tensors/ElabTactics.lean new file mode 100644 index 0000000000..5aca5347a5 --- /dev/null +++ b/Physlib/Mathematics/Tensors/ElabTactics.lean @@ -0,0 +1,904 @@ +/- +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_indices` 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_indices` : 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 + +* None. + +-/ + +public meta section + +open Lean Meta Elab Term + +namespace Tensor.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, reindexings, sums, negatives, zeros, +scalar multiples and contractions of one or two pairs. For more pairs, pass +`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,* "]" : + 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 indices by `ne_eq` and `Fin.reduceEq`. + simp only [map_add, map_neg, map_zero, Finsupp.add_apply, Finsupp.neg_apply, + 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 + -- 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, + Tensor.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 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 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 := + 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 + 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) + +/-- 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. -/ +private def firstSuccess {α β : Type} (xs : List α) (err : MessageData) (f : α → TacticM β) : + TacticM β := do + let mut err := err + for x in xs do + try return ← f x + 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 +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? ``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. -/ +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? ``Tensor.contr l + | throwError "tensor_regroup: the left side is not a contraction" + 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 (`Tensor.contr_noPairs`). The three steps run separately, since together their lemmas +loop. -/ +private def flattenContractions : TacticM Unit := do + 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.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.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 + 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 reindexing into +the contraction. -/ +private def swapFactors (q : Expr) (n m : Nat) : TacticM Unit := do + let fs := factors q + let thm ← if m = 0 then + `((Tensor.Species.tmulEquiv_comm _ _ $(← exprToSyntax fs[1]!) + $(← exprToSyntax fs[0]!)).symm) + else + `((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 [Tensor.Species.tmulEquiv_reindex_left, + 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. -/ +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 indices and contract the same pairs of indices +(`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 => 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 [] + 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 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 indices do not depend on them. + try evalTactic (← `(tactic| set_option synthInstance.maxSize 4096 in + 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" + +/-! + +## 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 => isEqFactor 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_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_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. -/ +private structure ReadContraction where + /-- The factors of `P`, from left to right. -/ + factors : List Expr + /-- The index of `P` at which each factor starts. -/ + offsets : List Nat + /-- The index of `P` of each free index, in order. -/ + free : List Nat + /-- The two indices of `P` of each pair, the first argument of `S.contr` first. -/ + pairs : List (Nat × Nat) + +/-- 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 + | .fin _, x => do + let some i ← (evalNat (← whnfD (← mkAppM ``Fin.val #[x]))).run + | 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! + else if x.isAppOfArity ``Sum.inr 3 then return a.size + (← indexNumber b x.appArg!) + 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 index of each pair. -/ +private def readContraction (e : Expr) : MetaM ReadContraction := do + -- `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.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 + let sizes ← fs.mapM fun f => do + let some (s, _, _) ← literalTensorType? (← inferType f) + | throwError "tensor_regroup: the indices or colors of{indentExpr f}\nare not literal" + return s.size + let JJ ← mkAppM ``Sum #[J, J] + let at_ (y : Expr) : MetaM Nat := 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]]) + 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 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 + 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 index `index` of the product of the left side of the goal, +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.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) + +mutual + +/-- 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 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 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 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 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 index has the color {a} of a delta" + 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 +`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 ``Tensor.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" + -- 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 + | [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 ``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 ``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 +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 reindexing. 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 _ => + -- 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")}" + -- 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 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 + 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, + 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 (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) + +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 + throwSummandError m!"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 _ => + -- 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. + 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`. -/ +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 + -- Undoes a swap whose regrouping failed in the previous round. + 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 `Tensor S 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 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. -/ +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 + throwSummandError m!"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 reindexing. + 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 ``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 +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 + 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. + 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. + throw <| if isSummandError e then e else e' + +/-! + +## F. Chains of lowerings and raisings + +-/ + +/-- `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 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])) + 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_indices" isMapIndices (← `(Tensor.Species.mapIndices_id_apply _)) + evalTactic (← `(tactic| focus + 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.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])) + +/-! + +## G. Rewriting a sub-product by an identity + +-/ + +/-- 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]! + 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 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 + 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₁ … = …}ᵀ` 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? + | 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 [Tensor.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 => 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" + 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 [Tensor.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 + 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 => 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 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 (`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^…ν… δ_ν^μ` (`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 +(`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ᵢ`. -/ +syntax (name := tensorRegroup) "tensor_regroup" (" using " term,+)? : tactic + +elab_rules : tactic + | `(tactic| tensor_regroup) => regroupWithLowerings + | `(tactic| tensor_regroup using $es,*) => do + discard goalField + regroupUsing es.getElems.toList + +end Tensor.Notation + +end diff --git a/Physlib/Mathematics/Tensors/HermitianForm.lean b/Physlib/Mathematics/Tensors/HermitianForm.lean new file mode 100644 index 0000000000..6038b2d8cd --- /dev/null +++ b/Physlib/Mathematics/Tensors/HermitianForm.lean @@ -0,0 +1,127 @@ +/- +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.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 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, 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. + +## iii. Table of contents + +- A. Hermitian forms and their inverses +- B. Positive-definite forms are invertible + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct +open scoped ComplexOrder + +namespace Tensor + +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. 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)) + (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 (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) : + (S.sesqForm (S.inverse g) (Color.dual_eq_bar_dual hb)).IsSymm := + hg.of_comp_star h.2 (sesqForm_inverse_toLin g h hb) + +/-! + +## B. Positive-definite forms are invertible + +-/ + +/-- 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'] + 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} + (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, (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)] + +/-- 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 := + have h := toLin_bijective_of_isPosDef g hg + hg.of_comp_star h.2 (sesqForm_inverse_toLin g h hb) + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/Inverse.lean b/Physlib/Mathematics/Tensors/Inverse.lean new file mode 100644 index 0000000000..b04c010633 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Inverse.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.Tensors.Metric +/-! + +# The inverse of a tensor on two indices + +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`, `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. 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`. + +## 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_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`. + +## iii. Table of contents + +- A. The inverse +- B. The contraction identities 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 Tensor + +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} + {a b : Color K} + +/-! + +## A. The inverse + +-/ + +variable (S) 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. 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 := + (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 := + Module.Basis.coordTensorHomEquiv_coordInverse h + +/-! + +## B. The contraction identities and uniqueness + +-/ + +/-- `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 + (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 + (Module.Basis.comp_coordTensorHomEquiv_coordInverse h) + +/-- A tensor `N` with `N ∘ g = id`, as linear maps, is the inverse of `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 + +/-! + +## 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) := + 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) + (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 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_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. -/ +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) + +/-- `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))) = + S.deltaTensor c := by + 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 + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/LinearMap.lean b/Physlib/Mathematics/Tensors/LinearMap.lean new file mode 100644 index 0000000000..2f33964670 --- /dev/null +++ b/Physlib/Mathematics/Tensors/LinearMap.lean @@ -0,0 +1,307 @@ +/- +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.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`. 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 `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 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 +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 +identity commutes with every group element, so the delta is invariant. `S.deltaTensor c` is the +delta as a tensor on two indices, and its components form the identity matrix. + +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 indices 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.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. +- `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 + `F` at `i`, and `Tensor.contr_deltaTensor` : for the delta this is + `T^…μ… δ_μ^ν = T^…ν…`. + +## iii. Table of contents + +- 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 + +* None. + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensor + +/-! + +## A. Choices of pairs for products with a tensor on two indices + +-/ + +namespace ColorEquiv + +variable {K ι₁ : Type} + +/-- 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`. -/ +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 + +/-- 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} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] (S : Species k G K basisIdx) + +namespace Species + +/-! + +## B. 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 abbrev toLin {a : Color K} {U : Type} [AddCommGroup U] [Module k U] : + S.V a ⊗[k] U ≃ₗ[k] (S.V a.dual →ₗ[k] U) := + (S.basis a).coordTensorHomEquiv (S.basis a.dual) + +/-- The coordinate pairing of the bases of `c` and `c.dual` is the contraction. -/ +@[simp] +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 + +/-- 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 + +/-- 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 := + Module.Basis.isIntertwiningMap_coordTensorHomEquiv_iff (S.invariant a) z + +/-! + +## C. The delta + +-/ + +/-- The Kronecker delta `∑ μ, e^μ ⊗ e_μ ∈ V c.dual ⊗ V c`, with components `δ_μ^ν`. The factor of +the dual color is first. -/ +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 := + Module.Basis.coordDelta_invariant (S.invariant c.dual) g + +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) : Tensor S ![c.dual, 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.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_twoIndexEquiv _ _ _ μ + +/-- 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) := + (tensorBasis_repr_deltaTensor c μ).trans + ((Module.Basis.tensorProduct_repr_coordDelta _ _ _ _).trans Matrix.one_apply.symm) + +end Species + +open Species PiTensorProduct + +variable {S} + +variable {ι₁ κ : Type} {c : ι₁ → Color K} {c' : κ → Color K} + +/-! + +## D. Products with a tensor on two indices + +-/ + +/-- 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) + (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.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' => + simp only [map_add, LinearMap.add_apply, tmul_add, hF, hF'] + 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, ← Species.pure] + rw [twoIndexEquiv_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 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 : 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 + refine LinearMap.congr_fun (f := (contr _).toLinearMap ∘ₗ (S.tmulEquiv c ![a.dual, b]).toLinearMap + ∘ₗ (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 + (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) + (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 + 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 : 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 + 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 diff --git a/Physlib/Mathematics/Tensors/Metric.lean b/Physlib/Mathematics/Tensors/Metric.lean new file mode 100644 index 0000000000..ab4862c7b1 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Metric.lean @@ -0,0 +1,362 @@ +/- +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.Schouten +public import Physlib.Mathematics.ForMathlib.SesquilinearForm +public import Physlib.Mathematics.Tensors.LinearMap +/-! + +# 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 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 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 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. `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. + +## ii. Key results + +- `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`, in components + `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 + raising undoes lowering. +- `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, + `T^…_ν… = g_νμ T^…μ…`. +- `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 + +- A. Invariant forms and metrics +- B. Lowering and raising vectors +- C. The form as a tensor +- D. Lowering and raising at one index +- E. Forms on a kind with two labels + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensor + +namespace Species + +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + +/-! + +## A. Invariant forms and metrics + +-/ + +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 +`ε`. -/ +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`. -/ + 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 + +variable {S : Species k G K basisIdx} + +/-- 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 + +/-- 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 + +/-- 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. -/ +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 ((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^μν`. -/ +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 + /-- 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 `ε`. -/ +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 + +/-- 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 μ ν := + (((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] : + (S.formBilin c).IsSymm := + (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] : + 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 μ ν := + (((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).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. -/ +lemma isAlt_formBilin (c : Color K) [S.HasForm c.kind] [S.IsAntisymmetricForm c.kind] : + LinearMap.IsAlt (S.formBilin c) := + (S.basis c).isAlt_coordTensorHomEquiv_of_comm_eq_neg (S.basis c.dual) (form_antisymm c) + +/-! + +## B. Lowering and raising vectors + +-/ + +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 ((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 + +/-- 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).toLinearEquiv.symm_apply_apply v + +/-- 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 ν μ := + Module.Basis.repr_coordToDual _ _ _ _ (form_inverse c) (form_inverse c.dual) v ν + +/-! + +## 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] : Tensor S ![c, 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.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_twoIndexEquiv _ _ _ μ + +/-- 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_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_swapTwo_twoIndexEquiv, form_antisymm, map_neg] + +/-! + +## D. Lowering and raising at one index + +-/ + +variable {ι : Type} [DecidableEq ι] + +variable (S) in +/-- `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) : + (S.tensorRep c).Equiv (S.tensorRep c') := + S.congrAt i (((S.cast ha).trans (S.toDual a)).trans (S.cast hi.symm)) h + +/-- 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 : 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] + exact (S.congrAt i _ h).symm_apply_apply t + +/-- 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 : 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 + +/-- 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) + (ha : c i = a) [S.HasForm a.kind] (hi : c' i = a.dual) (h : ∀ x, x ≠ i → c x = c' x) + (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 + 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 + 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 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 : 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 + rw [formTensor, reindex_swapTwo_twoIndexEquiv, Tensor.contr_matMulAt, toDualAt, congrAt_eq_mapAt] + rfl + +/-! + +## E. Forms on a kind with two labels + +-/ + +/-- 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 + simpa only [formBilin_basis] using + (isAlt_formBilin (S := S) c).schouten_basis (S.basis c.dual) h2 γ ρ α' α + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/Product.lean b/Physlib/Mathematics/Tensors/Product.lean new file mode 100644 index 0000000000..8ef902ea8e --- /dev/null +++ b/Physlib/Mathematics/Tensors/Product.lean @@ -0,0 +1,350 @@ +/- +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^ν` 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 +`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 +two indices by `twoIndexEquiv`. The components of a product are the products of the components. + +## ii. Key results + +- `prodColors` gives the colors 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` 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. +- `Species.tensorBasis_repr_tmulEquiv` proves `(T ⊗ U)^μ_ν^ρ = T^μ_ν U^ρ`. + +## iii. Table of contents + +- A. The colors and reindexings of a product +- B. The product +- C. Associativity, symmetry and equivariance +- D. Tensors on one and two indices +- E. Components + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensor + +/-! + +## A. The colors and reindexings of a product + +-/ + +/-- 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 := + fun s => Sum.rec (motive := fun _ => Color K) c₁ c₂ s + +namespace ColorEquiv + +variable {K : Type} {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} + +/-- 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` 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` 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` 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₃) := + (sumAssoc c₁ c₃ c₂).trans (((refl c₁).sumCongr (sumComm c₃ c₂)).trans + (sumAssoc c₁ c₂ c₃).symm) + +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 {ι₁ ι₂ ι₃ ι₁' ι₂' : Type} + +/-! + +## B. The product + +-/ + +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 +`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₂)) := + 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`. -/ +@[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 := + Representation.Equiv.piTprodSum_tmul (fun s => S.rep (prodColors c₁ c₂ s)) f₁ 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 +are regrouped by `ColorEquiv.sumAssoc`. -/ +lemma tmulEquiv_assoc (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) (c₃ : ι₃ → Color K) + (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₃)) := + 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) + (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₃) + +/-- 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₁ : 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 ∘ₗ + (S.tmulEquiv c₁ c₂).toLinearMap) + (g := (S.tmulEquiv c₂ c₁).toLinearMap ∘ₗ (TensorProduct.comm k _ _).toLinearMap) + (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)) _ + +/-- 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₁ : 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 ∘ₗ + (S.tmulEquiv c₁ c₂).toLinearMap) + (g := (S.tmulEquiv c₁' c₂').toLinearMap ∘ₗ + TensorProduct.map (S.reindex σ).toLinearMap (S.reindex τ).toLinearMap) + (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)) _ + +/-- Reindexing the first factor reindexes the product. With `tmulEquiv_reindex_right` it moves +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₂) = + S.reindex (σ.sumCongr (.refl c₂)) (S.tmulEquiv c₁ c₂ (t₁ ⊗ₜ t₂)) := by + 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. -/ +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₂) = + 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 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₁ : 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 + 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)^αβ`. -/ +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₂) + +/-! + +## 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 +`Tensor S ![a, b]` on two indices, so that such an object can be contracted with other tensors. + +-/ + +variable (S) in +/-- 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]) := + 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] +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) + +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 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 <| + (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 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) = + S.pure (c := ![a, b]) (Fin.cons x (Fin.cons y finZeroElim)) := by + 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 indices of a tensor on two indices swaps the factors, `M^μν ↦ M^νμ`. -/ +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 + | add x y hx hy => simp only [map_add, hx, hy] + | tmul x y => + 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 + +The components of a product are the products of the components of its factors. The components +of a tensor on two indices are the coordinates in Mathlib's basis `Basis.tensorProduct`. + +-/ + +/-- 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₁ : 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)) * + (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))) (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]) _ + +/-- The components of the tensor of a vector `v` of `V c` are its components `v^μ`. -/ +lemma tensorBasis_repr_oneIndexEquiv (c : Color K) (v : S.V c) + (μ : Π i, basisIdx (![c] i).kind) : + (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_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.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 [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 + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/Reindexing.lean b/Physlib/Mathematics/Tensors/Reindexing.lean new file mode 100644 index 0000000000..10b0ab2bfc --- /dev/null +++ b/Physlib/Mathematics/Tensors/Reindexing.lean @@ -0,0 +1,223 @@ +/- +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.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 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 `![⟨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`. It lines up the indices of two tensors, as in `A^μ_ν + B_ν^μ`. + +## ii. Key results + +- `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.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 + respects composites, the identity and inverses, and `Species.reindex_reindex` is the first on a + tensor. +- `Species.reindex` is an equivalence of representations, so reindexing commutes with the group + action. +- `Species.tensorBasis_repr_reindex` gives the components of a reindexed tensor, + `(Tᵀ)_ν^μ = T^μ_ν`. + +## iii. Table of contents + +- A. Reindexings that keep colors +- B. Reindexing tensors +- C. Composition and equivariance +- D. Components + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensor + +/-! + +## A. Reindexings that keep colors + +-/ + +/-- 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 reindexings are equal when their bijections are +(`ColorEquiv.ext`). -/ +@[ext] +structure ColorEquiv {K : Type} {ι₁ ι₂ : Type} (c₁ : ι₁ → Color K) (c₂ : ι₂ → Color K) where + /-- The bijection of indices, old index `i` to new index `toEquiv i`. -/ + toEquiv : ι₁ ≃ ι₂ + /-- 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) + +namespace ColorEquiv + +variable {K : Type} {ι₁ ι₂ ι₃ : Type} {c₁ : ι₁ → Color K} {c₂ : ι₂ → Color K} + {c₃ : ι₃ → Color K} + +/-- The identity reindexing, where every index stays in place. -/ +def refl (c₁ : ι₁ → Color K) : ColorEquiv c₁ c₁ := ⟨Equiv.refl ι₁, fun _ => rfl⟩ + +/-- 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 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 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⟩ + +@[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 + +@[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 + +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} + +/-! + +## B. Reindexing tensors + +-/ + +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.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.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] +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) := + PiTensorProduct.reindex_tprod _ _ + +variable (S) in +/-- 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₂) : + (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 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₂) + (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, permIndices_pure, congrIndices_pure] + +/-! + +## C. Composition and equivariance + +-/ + +/-- 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 + +/-- 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 σ₂) := + equiv_ext_pure fun f => by + simp only [Representation.Equiv.trans_apply, reindex_pure, cast_apply, cast_cast] + rfl + +/-- 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 : Tensor S c₁) : + S.reindex σ₂ (S.reindex σ₁ t) = S.reindex (σ₁.trans σ₂) t := by + rw [reindex_trans, Representation.Equiv.trans_apply] + +/-- 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 + rw [Representation.Equiv.apply_symm_apply, reindex_reindex, + show σ.symm.trans σ = .refl c₂ from ColorEquiv.ext (Equiv.symm_trans_self _), reindex_refl] + rfl + +/-! + +## D. Components + +-/ + +/-- 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} + {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 => + 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.symm _ _ fun j => ?_ + -- 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 + ((congr_arg_heq y (σ.toEquiv.apply_symm_apply j)).symm.trans (cast_heq _ _).symm))) + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/Schouten.lean b/Physlib/Mathematics/Tensors/Schouten.lean new file mode 100644 index 0000000000..7597b81021 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Schouten.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 Physlib.Mathematics.Tensors.ElabTactics + +/-! + +# 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 + +* None. + +-/ + +@[expose] public section + +/-! + +## A. The Schouten identity + +-/ + +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} + (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_ρα' δ_γ^α`. -/ +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] + -- `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 new file mode 100644 index 0000000000..fcc3ba08ec --- /dev/null +++ b/Physlib/Mathematics/Tensors/Tensor.lean @@ -0,0 +1,549 @@ +/- +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.Finite +public import Physlib.Mathematics.Tensors.Species +public import Physlib.Mathematics.ForMathlib.Representation +/-! + +# Tensors of a species + +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 + +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` has the components `u^μ v^ν`. + +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 +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 + +- `Tensor` is the space of tensors, and `Species.pure` is a pure tensor. +- `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 + 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 + +- 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 + +* None. + +-/ + +@[expose] public section + +open TensorProduct PiTensorProduct + +namespace Tensor + +namespace Species + +variable {k : Type} [RCLike k] {K : Type} {G : Type} [Group G] {basisIdx : K → Type} + [∀ κ, Fintype (basisIdx κ)] [∀ κ, DecidableEq (basisIdx κ)] + +/-! + +## A. Tensors + +`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 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`. + +-/ + +/-- 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 `⨂` 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) + +noncomputable instance {S : Species k G K basisIdx} {ι : Type} {c : ι → Color K} : + 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 (Tensor S c) := + inferInstanceAs (Module k (⨂[k] i, S.V (c i))) + +variable {ι : Type} {c : ι → Color K} + +/-- 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 + +variable {S : Species k G K basisIdx} + +/-- 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 := + 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*} [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) + +/-! + +## 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`. + +-/ + +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. 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`, 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) + +-- 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. -/ +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) := + Representation.piTprod_tprod _ _ _ + +/-- 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 + +scoped[Tensor] attribute [instance] Tensor.Species.smulCommClass + +/-- 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' + +/-! + +### 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⟩]`, 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 +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 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 := + 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 := + 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. -/ +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 := + 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 +representations. -/ +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 : 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 + +/-- 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} + {φ ψ : (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) + +/-- 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 := + LinearMap.congr_fun (PiTensorProduct.map_eq_id_of_isEmpty _) 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 +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) + +/-- 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) := + 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) := + Basis.piTensorProduct_repr_tprod_apply _ _ _ + +/-- 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) : + 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, 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)) + (S.basis (c i)) (S.rep (c i) g) (φ i) (ψ i)) * (S.tensorBasis c).repr t ψ := by + 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 φ + +/-! + +## D. Linear maps at the indices + +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^μ_ν`. 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). + +### D.1. A linear map at every index + +`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. + +-/ + +section + +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)) : Tensor S c →ₗ[k] Tensor S c' := + 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)) : + 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 := + (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)) + (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 + +/-- The index-wise map of the identities is the identity. -/ +lemma mapIndices_id : S.mapIndices (c := c) (fun _ => LinearMap.id) = LinearMap.id := + 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 := + 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 : Tensor S c) : + S.mapIndices f (g • t) = g • S.mapIndices f 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.piTprodCongr`. -/ +noncomputable def congrIndices (f : Π x, (S.rep (c x)).Equiv (S.rep (c' x))) : + (S.tensorRep c).Equiv (S.tensorRep c') := + 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.piTprodCongr_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. + +-/ + +section + +variable [DecidableEq ι] + +variable (S) in +/-- 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 + +@[simp] +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 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.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 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 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 + 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 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) : 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, +`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)) : + S.mapAt i f h (S.pure p) = S.pure q := by + rw [mapAt, mapIndices_pure] + refine congrArg S.pure (funext fun x => ?_) + by_cases hx : x = i + · subst 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 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) : + 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, 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_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) + (ψ : Π 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 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, + 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 [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`). 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. + +-/ + +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` +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 => + 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))) + (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`. -/ +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 + +end Species + +end Tensor diff --git a/Physlib/Mathematics/Tensors/Tensorial.lean b/Physlib/Mathematics/Tensors/Tensorial.lean new file mode 100644 index 0000000000..bd139b7539 --- /dev/null +++ b/Physlib/Mathematics/Tensors/Tensorial.lean @@ -0,0 +1,165 @@ +/- +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^μ`, on which Mathlib's analysis acts, and +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`. + +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`) 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, as the tensors with that index, + and `Tensorial.tensorBasis_repr_toTensor_ofIndexSpace` gives its components. +- `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, and `Tensorial.tensorBasis_repr_toTensor_prod_ofIndexSpace` + states it for two spaces of one index. + +## iii. Table of contents + +- A. Tensorial types +- B. Tensor products of tensorial types + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct + +namespace Tensor + +/-! + +## 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 `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} + (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 (S.tensorRep c) + +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] + [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 := + 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 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 + toTensor := S.oneIndexEquiv 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_oneIndexEquiv c v μ + +/-! + +## B. Tensor products of tensorial types + +-/ + +/-- 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] : + Tensorial S (prodColors c₁ c₂) (M ⊗[k] N) where + rep := (rep (M := M)).tprod (rep (M := N)) + 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] +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] + +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 Tensor diff --git a/Physlib/Relativity/Bispinors/Pauli/Tensor.lean b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean new file mode 100644 index 0000000000..2f452ef954 --- /dev/null +++ b/Physlib/Relativity/Bispinors/Pauli/Tensor.lean @@ -0,0 +1,290 @@ +/- +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 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. + +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 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 +`σ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 + +- `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.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 + `σ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) + +namespace Tensor.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[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) : + (complexLorentz.tensorBasis _).repr pauliTensor μ = pauliMatrix (μ 0) (μ 1) (μ 2) := by + 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 + 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 + 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 (Ne.symm hx)] + +/-- 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 | τ(ν) τ(α) τ(β)}ᵀ μ = + pauliMatrix (μ 0) (μ 2) (μ 1) := by + -- `↓` unfolds the lowered vector index by `tensorBasis_repr_pauliTensor_down` first. + tensor_components [Species.tensorBasis_repr_toDualAt, formMatrix_apply, + ↓tensorBasis_repr_pauliTensor_down] + exact (sum_metricRaw_mul_sum_metricRaw_mul _ _ _).trans + (congrFun₂ (PauliMatrix.adjugate_minkowskiMatrix_smul_pauliMatrix _) _ _) + +/-! + +## C. Contraction identities + +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 `bispinorToVector ∘ vectorToBispinor = 2`, 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] + -- 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 : + {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 + tensor_regroup + rw [h, pauliCoDown_trace_pauliCo] + tensor_regroup + +/-! + +### C.2. The Fierz identity + +-/ + +/-- `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, 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] + +/-- `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 + +/-- `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 ε^{αα'} ε^{β̇β̇'}`. -/ +lemma pauliCo_contr_pauliContr : + {pauliTensor | τ(ν) α β ⊗ pauliTensor | ν α' β' = + (2 : ℂ) • {εL | α α' ⊗ εR | β β'}ᵀ | α α' β β'}ᵀ := by + rw [map_smul, ← vectorToBispinor_bispinorToVector] + refine congrArg vectorToBispinor ?_ + simp only [bispinorToVector, LinearMap.coe_mk, AddHom.coe_mk] + -- Raising with `ε` contracts its second index, so both forms are swapped, with signs that cancel. + tensor_regroup using formTensor_upL_antisymm, formTensor_upR_antisymm + +/-! + +### 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 η^μν`. -/ +lemma pauliContr_trace_pauliContrDown : + {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 + 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' | 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. -/ +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 +`pauliContrDown_trace_pauliContr` with the lowerings written as contractions with `ε`. -/ +lemma pauliContr_pauliContr_eps_epsR_deltaR : + {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. -/ +lemma pauliContrDown_pauliContr_mul_add : + {pauliTensor | μ τ(α) τ(β) ⊗ pauliTensor | ν α β' + + pauliTensor | ν τ(α) τ(β) ⊗ pauliTensor | μ α β' = + (2 : ℂ) • {η | μ ν ⊗ δR' | β β'}ᵀ | μ ν β β'}ᵀ := by + rw [← pauliContr_pauliContr_eps_epsR_deltaR, schouten_downR] + tensor_regroup + +end Tensor.complexLorentz diff --git a/Physlib/Relativity/Fermions/Weyl/Metric.lean b/Physlib/Relativity/Fermions/Weyl/Metric.lean index 5e83c791ae..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 @@ -75,6 +88,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 `ε_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) 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] + /-! ## B. The metric for left-handed Weyl fermions diff --git a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean index 2aa6b35054..40a73db70f 100644 --- a/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean +++ b/Physlib/Relativity/PauliMatrices/SelfAdjoint.lean @@ -106,6 +106,27 @@ 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] + +/-- `σ̄^μ = η_μμ σ^μ` 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) ℂ)) : diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Species.lean b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean new file mode 100644 index 0000000000..daf1e2e340 --- /dev/null +++ b/Physlib/Relativity/Tensors/ComplexTensor/Species.lean @@ -0,0 +1,658 @@ +/- +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.Schouten +public import Physlib.Mathematics.Tensors.Conjugation +public import Physlib.Relativity.Fermions.Weyl.Metric +public import Physlib.Relativity.Tensors.ComplexTensor.Metrics.Pre +/-! + +# 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 `ψ̄_α̇`, 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 +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]`, 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. + +These spaces are tensors with one index (`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. +- `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.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 index. +- `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 +- 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 Tensor + +/-! + +## 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, ℂ)`. 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) := + Species.invariant_of_up indexRep indexBasis fun + | left => Module.Basis.coordPairing_invariant LeftHandedWeyl.contr fun μ ν => + (LeftHandedWeyl.contr_basis μ ν).trans (if_congr Fin.ext_iff.symm rfl rfl) + | 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 + +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 def 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 Tensor.complexLorentz`. -/ +scoped syntax (name := complexLorentzSyntax) "ℂT[" term,* "]" : term + +macro_rules (kind := complexLorentzSyntax) + | `(ℂT[$cs,*]) => `(Tensor Tensor.complexLorentz (open Tensor.complexLorentz in ![$cs,*])) + +/-- `ℂT[upL, downR]` is the space of tensors `T^α_β̇`. -/ +example : ℂT[upL, downR] = Tensor complexLorentz ![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. 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 + | ⟨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⟩ => 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. -/ +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⟩, μ, ν => (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`. -/ +lemma formEntries_dual_mul : ∀ c : Color Kind, + Matrix.of (formEntries c.dual) * Matrix.of (formEntries c) = 1 + | ⟨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 _)] + +/-- 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 + simpa only [basis_tensorProduct_repr_form] using formEntries_dual_mul c + +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 Tensor.complexLorentz Tensor.complexLorentz.upL + +/-- The form `ε_αβ` on two lower left-handed indices. -/ +scoped notation "εL'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.downL + +/-- The form `ε^α̇β̇` on two upper right-handed indices. -/ +scoped notation "εR" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.upR + +/-- The form `ε_α̇β̇` on two lower right-handed indices. -/ +scoped notation "εR'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.downR + +/-- The metric `η^μν` on two upper vector indices. -/ +scoped notation "η" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.up + +/-- The metric `η_μν` on two lower vector indices. -/ +scoped notation "η'" => Species.formTensor Tensor.complexLorentz Tensor.complexLorentz.down + +/-- The delta `δ^μ_ν`, on the colors `![up, down]`. -/ +scoped notation "δ" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.down + +/-- The delta `δ_μ^ν`, on the colors `![down, up]`. -/ +scoped notation "δ'" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.up + +/-- The delta `δ^α_β`, on the colors `![upL, downL]`. -/ +scoped notation "δL" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.downL + +/-- The delta `δ_α^β`, on the colors `![downL, upL]`. -/ +scoped notation "δL'" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.upL + +/-- The delta `δ^α̇_β̇`, on the colors `![upR, downR]`. -/ +scoped notation "δR" => Species.deltaTensor Tensor.complexLorentz Tensor.complexLorentz.downR + +/-- The delta `δ_α̇^β̇`, on the colors `![downR, upR]`. -/ +scoped notation "δR'" => Species.deltaTensor Tensor.complexLorentz Tensor.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 : 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)) := + Tensor.tensorBasis_repr_conj_of_basis h _ + (fun i => Species.HasConjugation.ofBasis_conjEquiv_basis _ (h i)) t μ + +end complexLorentz + +/-! + +## D. Components of the forms + +-/ + +namespace complexLorentz + +/-- 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 μ ν := + 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 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) μ = + formEntries c (μ 0) (μ 1) := + (Species.tensorBasis_repr_formTensor c μ).trans (formMatrix_apply c _ _) + +/-- 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⟩ μ + +/-- `η^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 + +/-- The Minkowski metric `η` on the vector kind is symmetric, `η^νμ = η^μν`. -/ +instance : complexLorentz.IsSymmetricForm vec where + 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 + have h : metricRaw ν μ = -metricRaw μ ν := congrFun₂ transpose_metricRaw μ ν + simp [formEntries, h] + | ⟨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 := + (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 + 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 + tensor_regroup + +/-- `ε_αβ` is antisymmetric, `ε_αβ = -ε_βα`. -/ +lemma formTensor_downL_antisymm : {εL' | α β = -εL' | β α}ᵀ := by + tensor_regroup + +/-- `ε^α̇β̇` is antisymmetric, `ε^α̇β̇ = -ε^β̇α̇`. -/ +lemma formTensor_upR_antisymm : {εR | α β = -εR | β α}ᵀ := by + tensor_regroup + +/-- `ε_α̇β̇` is antisymmetric, `ε_α̇β̇ = -ε_β̇α̇`. -/ +lemma formTensor_downR_antisymm : {εR' | α β = -εR' | β α}ᵀ := by + tensor_regroup + +/-- `η^μν` is symmetric, `η^μν = η^νμ`. -/ +lemma formTensor_up_symm : {η | μ ν = η | ν μ}ᵀ := by + tensor_regroup + +/-- `η_μν` is symmetric, `η_μν = η_νμ`. -/ +lemma formTensor_down_symm : {η' | μ ν = η' | ν μ}ᵀ := by + tensor_regroup + +/-- The Schouten identity for left-handed indices, `ε_γρ δ^α_α' = ε_α'ρ δ_γ^α - ε_α'γ δ_ρ^α`. The +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 + (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' | ρ α'}ᵀ | α γ ρ α'}ᵀ := 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) : + {complexLorentz.deltaTensor c | μ ν = complexLorentz.deltaTensor c.dual | ν μ}ᵀ := by + tensor_regroup + +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 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`. -/ +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_ofIndexSpace (S := complexLorentz) upL 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 `tensor_regroup` through `contr_formTensor_formTensor`. -/ +example : {εL | α β ⊗ εL' | β γ = δL | α γ}ᵀ := by + 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 + tensor_regroup + +/-- The components of `ε^αβ ⊗ ε^α̇β̇` are the products of the components of `εL` and `εR`. -/ +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, + 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 [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 : 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 Tensor diff --git a/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean b/Physlib/Relativity/Tensors/ComplexTensor/Vector/Basic.lean index c2f1d75159..3383d1d626 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 + rw [Module.Basis.coordPairing_tmul] + 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 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 new file mode 100644 index 0000000000..212b427bd6 --- /dev/null +++ b/Physlib/Relativity/Tensors/RealTensor/Species.lean @@ -0,0 +1,399 @@ +/- +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.ElabTactics +public import Physlib.Relativity.Tensors.RealTensor.Representation.Contraction +public import Physlib.Relativity.Tensors.RealTensor.Vector.MinkowskiProduct +import Mathlib.LinearAlgebra.Matrix.IsDiag +/-! + +# 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 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 +(`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`. + +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). +- `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. +- `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.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 `η_νν`. +- `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. Components of the metric +- D. Lorentz vectors and covectors as tensors + +## iv. References + +* None. + +-/ + +@[expose] public section + +open TensorProduct Lorentz Matrix + +namespace Tensor + +/-! + +## 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`: `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 => Vector d + | false => CoVector d + +noncomputable instance (d : ℕ) : ∀ c, AddCommGroup (realLorentz.IndexSpace d c) + | ⟨_, .up⟩ => inferInstanceAs (AddCommGroup (Vector d)) + | ⟨_, .down⟩ => inferInstanceAs (AddCommGroup (CoVector d)) + +noncomputable instance (d : ℕ) : ∀ c, Module ℝ (realLorentz.IndexSpace d c) + | ⟨_, .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 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) := + Species.invariant_of_up (basisIdx := fun _ => Fin 1 ⊕ Fin d) _ _ fun _ Λ x y => + (Vector.coordPairing_basis_tmul _ _).trans <| + (Vector.contract_rep Λ x y).trans (Vector.coordPairing_basis_tmul x y).symm + +end realLorentz + +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 def realLorentz (d : ℕ) : + Species ℝ (LorentzGroup d) Kind (fun _ => Fin 1 ⊕ Fin d) where + V := IndexSpace d + rep := IndexSpace.rep d + basis := IndexSpace.basis d + invariant := coordPairing_rep d + +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 active under +`open scoped Tensor.realLorentz`. -/ +scoped syntax (name := realLorentzSyntax) "ℝT[" term,+ "]" : term + +macro_rules (kind := realLorentzSyntax) + | `(ℝT[$d]) => `(Tensor (Tensor.realLorentz $d) ![]) + | `(ℝT[$d, $cs,*]) => `(Tensor (Tensor.realLorentz $d) (open Tensor.realLorentz in ![$cs,*])) + +end realLorentz + +/-! + +## B. The Minkowski metric + +-/ + +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 + 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. -/ +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] + +/-- 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 := + Module.Basis.coordTensorHomEquiv_comp_of_mul_eq_one _ _ _ _ <| by + simp only [basis_tensorProduct_repr_metric] + exact minkowskiMatrix.sq + +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. Components of the metric + +-/ + +namespace realLorentz + +/-- 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 +`μ : 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 + rw [Species.tensorBasis_repr_formTensor, formMatrix_apply] + +/-- 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 `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 : 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 + 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) + ![.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 [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] = Tensor (realLorentz 3) ![up, down] := rfl +end realLorentz + +/-! + +## D. Lorentz vectors and covectors as tensors + +-/ + +namespace realLorentz + +/-- 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) := + 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 : Tensor (realLorentz d) ![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) := + Tensorial.tensorBasis_repr_toTensor_ofIndexSpace (S := realLorentz d) down ω μ + +/-- 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_ofIndexSpace (S := realLorentz d) up up F μ + +/-- 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_ofIndexSpace (S := realLorentz d) down up T μ + +/-- 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) = + ∑ μ, v μ * ω μ := by + simp [Tensor.tensorBasis_repr_contr_one_pair, 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 indices 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.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. -/ +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.intertwiningMap_smul, Species.smul_eq_self_of_isEmpty] + +end realLorentz + +end Tensor