math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /CoppersmithWinograd /CWShapePhysicalPermutation.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
4.87 kB
import OAI.LinearAlgebra.MatrixMultiplication.CoppersmithWinograd.CWStrands
import OAI.LinearAlgebra.MatrixMultiplication.Tensor.TensorSidePermutation
namespace OAI
/-! Coppersmith–Winograd tensors, tensor powers and local restrictions. -/
noncomputable section
namespace MatrixMultiplication.CWShapePhysicalPermutation
open MatrixMultiplication.Foundation CWStrands
open scoped BigOperators
variable {F : Type*} [CommRing F]
private theorem tensor_swapXY (q : β„•) (x y z : Fin (q + 2)) :
FieldCW.tensor F q x y z = FieldCW.tensor F q y x z := by
unfold FieldCW.tensor
simp only [Finset.sum_add_distrib, mul_comm, mul_left_comm, mul_assoc]
ring
private theorem tensor_swapYZ (q : β„•) (x y z : Fin (q + 2)) :
FieldCW.tensor F q x y z = FieldCW.tensor F q x z y := by
exact (FieldCW.tensor_cyclic F q x y z).trans
((tensor_swapXY q y z x).trans (FieldCW.tensor_cyclic F q x z y).symm)
private theorem tensor_swapXZ (q : β„•) (x y z : Fin (q + 2)) :
FieldCW.tensor F q x y z = FieldCW.tensor F q z y x := by
exact (FieldCW.tensor_cyclic F q x y z).trans (tensor_swapXY q y z x)
theorem tensor_permute (q : β„•) (Οƒ : Equiv.Perm (Fin 3))
(a : Fin 3 β†’ Fin (q + 2)) :
FieldCW.tensor F q (a (Οƒ 0)) (a (Οƒ 1)) (a (Οƒ 2)) =
FieldCW.tensor F q (a 0) (a 1) (a 2) := by
have h01 : Οƒ 0 β‰  Οƒ 1 := fun h => (by decide : (0 : Fin 3) β‰  1) (Οƒ.injective h)
have h02 : Οƒ 0 β‰  Οƒ 2 := fun h => (by decide : (0 : Fin 3) β‰  2) (Οƒ.injective h)
have h12 : Οƒ 1 β‰  Οƒ 2 := fun h => (by decide : (1 : Fin 3) β‰  2) (Οƒ.injective h)
generalize hs0 : Οƒ 0 = s0 at *
generalize hs1 : Οƒ 1 = s1 at *
generalize hs2 : Οƒ 2 = s2 at *
fin_cases s0 <;> fin_cases s1 <;> fin_cases s2 <;> try contradiction
Β· rfl
Β· exact (tensor_swapYZ q (a 0) (a 1) (a 2)).symm
Β· exact (tensor_swapXY q (a 0) (a 1) (a 2)).symm
Β· exact (FieldCW.tensor_cyclic F q (a 0) (a 1) (a 2)).symm
Β· exact FieldCW.tensor_cyclic F q (a 2) (a 0) (a 1)
Β· exact (tensor_swapXZ q (a 0) (a 1) (a 2)).symm
theorem strand_permute {P : Type*} [Fintype P]
(Οƒ : Equiv.Perm (Fin 3)) (a : Fin 3 β†’ P β†’ Fin 7) :
strand (F := F) P (a (Οƒ 0)) (a (Οƒ 1)) (a (Οƒ 2)) =
strand P (a 0) (a 1) (a 2) := by
apply Finset.prod_congr rfl
intro i _
exact tensor_permute 5 Οƒ (fun s => a s i)
theorem shapeTensor_permute {P : Type*} [Fintype P]
(Οƒ : Equiv.Perm (Fin 3)) (g : Fin 3 β†’ β„•) (a : Fin 3 β†’ P β†’ Fin 7) :
shapeTensor (F := F) P (g ∘ Οƒ) (a (Οƒ 0)) (a (Οƒ 1)) (a (Οƒ 2)) =
shapeTensor P g (a 0) (a 1) (a 2) := by
have hmask :
(weight (a (Οƒ 0)) = g (Οƒ 0) ∧ weight (a (Οƒ 1)) = g (Οƒ 1) ∧
weight (a (Οƒ 2)) = g (Οƒ 2)) ↔
(weight (a 0) = g 0 ∧ weight (a 1) = g 1 ∧ weight (a 2) = g 2) := by
constructor
· rintro ⟨h0, h1, h2⟩
have hs (s : Fin 3) : weight (a (Οƒ s)) = g (Οƒ s) := by
fin_cases s <;> assumption
exact ⟨by simpa using hs (Οƒ.symm 0), by simpa using hs (Οƒ.symm 1),
by simpa using hs (Οƒ.symm 2)⟩
· rintro ⟨h0, h1, h2⟩
have hs (s : Fin 3) : weight (a s) = g s := by
fin_cases s <;> assumption
exact ⟨hs (Οƒ 0), hs (Οƒ 1), hs (Οƒ 2)⟩
have hp := strand_permute (F := F) Οƒ a
simp only [shapeTensor, Function.comp_apply, hmask, hp]
theorem shapeTensor_prod_permute {I : Type*} [Fintype I]
(P : I β†’ Type*) [βˆ€ i, Fintype (P i)]
(Οƒ : I β†’ Equiv.Perm (Fin 3)) (g : I β†’ Fin 3 β†’ β„•)
(a : βˆ€ i, Fin 3 β†’ P i β†’ Fin 7) :
(∏ i, shapeTensor (F := F) (P i) (g i ∘ Οƒ i)
(a i (Οƒ i 0)) (a i (Οƒ i 1)) (a i (Οƒ i 2))) =
∏ i, shapeTensor (P i) (g i) (a i 0) (a i 1) (a i 2) := by
apply Finset.prod_congr rfl
intro i _
exact shapeTensor_permute (Οƒ i) (g i) (a i)
theorem shapeTensor_prod_permute_ne_zero_iff {I : Type*} [Fintype I]
(P : I β†’ Type*) [βˆ€ i, Fintype (P i)]
(Οƒ : I β†’ Equiv.Perm (Fin 3)) (g : I β†’ Fin 3 β†’ β„•)
(a : βˆ€ i, Fin 3 β†’ P i β†’ Fin 7) :
(∏ i, shapeTensor (F := F) (P i) (g i ∘ Οƒ i)
(a i (Οƒ i 0)) (a i (Οƒ i 1)) (a i (Οƒ i 2))) β‰  0 ↔
(∏ i, shapeTensor (F := F) (P i) (g i) (a i 0) (a i 1) (a i 2)) β‰  0 := by
rw [shapeTensor_prod_permute]
theorem sidePermutation_shapeTensor {P : Type*} [Fintype P]
(Οƒ : Equiv.Perm (Fin 3)) (g : Fin 3 β†’ β„•) :
Tensor.sidePermutation Οƒ (shapeTensor (F := F) P g) =
shapeTensor P (g ∘ Οƒ) := by
funext x y z
let a : Fin 3 β†’ P β†’ Fin 7 := ![x, y, z]
change Tensor.sidePermutation Οƒ (shapeTensor P g) (a 0) (a 1) (a 2) =
shapeTensor P (g ∘ Οƒ) (a 0) (a 1) (a 2)
rw [Tensor.sidePermutation_apply_coordinates]
have h := shapeTensor_permute (F := F) Οƒ g (fun s => a (Οƒ.symm s))
simpa only [Equiv.symm_apply_apply] using h.symm
end MatrixMultiplication.CWShapePhysicalPermutation
end
end OAI