math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /Completion /RecursiveCompletion.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
7.54 kB
import OAI.LinearAlgebra.MatrixMultiplication.Polynomial.ComplexPolynomialDegenerationComposition
import OAI.LinearAlgebra.MatrixMultiplication.Polynomial.ComplexPolynomialWitnessProduct
import Mathlib.Tactic.Ring
namespace OAI
/-! Readable tensor completion and its finite arithmetic realization. -/
noncomputable section
namespace MatrixMultiplication
open MatrixMultiplication.Foundation
open scoped BigOperators
attribute [local instance] Classical.propDecidable Classical.decEq
inductive Color where
| B | A | C
deriving DecidableEq
instance : Fintype Color where
elems := {.B, .A, .C}
complete color := by cases color <;> simp
structure FlaggedTensor (X Y Z : Type*) where
tensor : Tensor β„‚ X Y Z
flagB : X β†’ Prop
flagA : Z β†’ Prop
flagC : Y β†’ Prop
one_hot : βˆ€ x y z, tensor x y z β‰  0 β†’
(if flagB x then 1 else 0) + (if flagA z then 1 else 0) +
(if flagC y then 1 else 0) = (1 : β„•)
namespace RecursiveCompletion
def raisedFlag {I : Type*} (center color : Color) (p : I β†’ Prop) : Prop :=
if center = color then βˆ€ i, p i else βˆƒ i, p i
variable {X Y Z : Type*}
def flagB (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(x : Fin m β†’ X) : Prop := raisedFlag center .B (fun i => S.flagB (x i))
def flagA (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(z : Fin m β†’ Z) : Prop := raisedFlag center .A (fun i => S.flagA (z i))
def flagC (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(y : Fin m β†’ Y) : Prop := raisedFlag center .C (fun i => S.flagC (y i))
def weight (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(x : Fin m β†’ X) (y : Fin m β†’ Y) (z : Fin m β†’ Z) : β„• :=
(if flagB S center m x then 1 else 0) +
(if flagA S center m z then 1 else 0) +
(if flagC S center m y then 1 else 0)
theorem weight_pos (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(x : Fin m β†’ X) (y : Fin m β†’ Y) (z : Fin m β†’ Z)
(hs : Tensor.power S.tensor m x y z β‰  0) :
0 < weight S center m x y z := by
classical
have hold (i : Fin m) :
(if S.flagB (x i) then 1 else 0) +
(if S.flagA (z i) then 1 else 0) +
(if S.flagC (y i) then 1 else 0) = (1 : β„•) := by
apply S.one_hot
exact (Finset.prod_ne_zero_iff.mp hs) i (Finset.mem_univ i)
by_contra h
have hw : weight S center m x y z = 0 := Nat.eq_zero_of_not_pos h
have hb : Β¬ flagB S center m x := by
intro hb
simp only [weight, ite_eq_left hb] at hw
omega
have ha : Β¬ flagA S center m z := by
intro ha
simp only [weight, ite_eq_left ha] at hw
omega
have hc : Β¬ flagC S center m y := by
intro hc
simp only [weight, ite_eq_left hc] at hw
omega
cases center with
| B =>
change Β¬ (βˆ€ i, S.flagB (x i)) at hb
change Β¬ (βˆƒ i, S.flagA (z i)) at ha
change Β¬ (βˆƒ i, S.flagC (y i)) at hc
obtain ⟨i, hi⟩ := not_forall.mp hb
simpa [hi, not_exists.mp ha i, not_exists.mp hc i] using hold i
| A =>
change Β¬ (βˆƒ i, S.flagB (x i)) at hb
change Β¬ (βˆ€ i, S.flagA (z i)) at ha
change Β¬ (βˆƒ i, S.flagC (y i)) at hc
obtain ⟨i, hi⟩ := not_forall.mp ha
simpa [not_exists.mp hb i, hi, not_exists.mp hc i] using hold i
| C =>
change Β¬ (βˆƒ i, S.flagB (x i)) at hb
change Β¬ (βˆƒ i, S.flagA (z i)) at ha
change Β¬ (βˆ€ i, S.flagC (y i)) at hc
obtain ⟨i, hi⟩ := not_forall.mp hc
simpa [not_exists.mp hb i, not_exists.mp ha i, hi] using hold i
def complete (S : FlaggedTensor X Y Z) (center : Color) (m : β„•) :
FlaggedTensor (Fin m β†’ X) (Fin m β†’ Y) (Fin m β†’ Z) where
tensor x y z := if weight S center m x y z = 1 then
Tensor.power S.tensor m x y z else 0
flagB := flagB S center m
flagA := flagA S center m
flagC := flagC S center m
one_hot := by
intro x y z hs
by_cases hw : weight S center m x y z = 1
Β· exact hw
Β· simp [hw] at hs
@[simp] theorem complete_tensor (S : FlaggedTensor X Y Z) (center : Color)
(m : β„•) (x : Fin m β†’ X) (y : Fin m β†’ Y) (z : Fin m β†’ Z) :
(complete S center m).tensor x y z =
if weight S center m x y z = 1 then Tensor.power S.tensor m x y z else 0 := rfl
def diagonalMap {K U : Type*} [Zero K] [DecidableEq U] (f : U β†’ K) : U β†’ U β†’ K :=
fun output input => if input = output then f output else 0
theorem restrict_diagonal {K U V W : Type*} [CommSemiring K]
[Fintype U] [Fintype V] [Fintype W]
[DecidableEq U] [DecidableEq V] [DecidableEq W]
(a : U β†’ K) (b : V β†’ K) (c : W β†’ K) (T : Tensor K U V W)
(x : U) (y : V) (z : W) :
Tensor.restrict (diagonalMap a) (diagonalMap b) (diagonalMap c) T x y z =
a x * b y * c z * T x y z := by
simp [Tensor.restrict, diagonalMap, ite_mul, mul_ite]
def flagPolynomial (p : Prop) : Polynomial β„‚ := if p then Polynomial.X else 1
theorem flagPolynomial_eq (p : Prop) :
flagPolynomial p = (Polynomial.X : Polynomial β„‚) ^ (if p then 1 else 0 : β„•) := by
classical
by_cases hp : p <;> simp [flagPolynomial, hp]
theorem flagPolynomial_degree (p : Prop) : (flagPolynomial p).degree ≀ 1 := by
classical
by_cases hp : p <;> simp [flagPolynomial, hp]
variable [Fintype X] [Fintype Y] [Fintype Z]
def leftMap (S : FlaggedTensor X Y Z) (center : Color) (m : β„•) :
(Fin m β†’ X) β†’ (Fin m β†’ X) β†’ Polynomial β„‚ :=
diagonalMap (fun x => flagPolynomial (flagB S center m x))
def middleMap (S : FlaggedTensor X Y Z) (center : Color) (m : β„•) :
(Fin m β†’ Y) β†’ (Fin m β†’ Y) β†’ Polynomial β„‚ :=
diagonalMap (fun y => flagPolynomial (flagC S center m y))
def rightMap (S : FlaggedTensor X Y Z) (center : Color) (m : β„•) :
(Fin m β†’ Z) β†’ (Fin m β†’ Z) β†’ Polynomial β„‚ :=
diagonalMap (fun z => flagPolynomial (flagA S center m z))
theorem diagonalMap_flag_degree {U : Type*} [DecidableEq U] (p : U β†’ Prop)
(u v : U) : (diagonalMap (fun u => flagPolynomial (p u)) u v).degree ≀ 1 := by
classical
by_cases h : v = u
Β· simpa [diagonalMap, h] using flagPolynomial_degree (p u)
Β· simp [diagonalMap, h]
theorem scalingPolynomial (S : FlaggedTensor X Y Z) (center : Color) (m : β„•)
(x : Fin m β†’ X) (y : Fin m β†’ Y) (z : Fin m β†’ Z) :
Tensor.restrict (leftMap S center m) (middleMap S center m) (rightMap S center m)
(fun x y z => Polynomial.C (Tensor.power S.tensor m x y z)) x y z =
Polynomial.C (Tensor.power S.tensor m x y z) *
Polynomial.X ^ weight S center m x y z := by
classical
rw [leftMap, middleMap, rightMap, restrict_diagonal]
simp only [flagPolynomial_eq, weight, pow_add]
ring
def completionDegeneration (S : FlaggedTensor X Y Z) (center : Color) (m : β„•) :
Tensor.PolynomialRestrictionDegeneration (Tensor.power S.tensor m)
(complete S center m).tensor 1 1 1 1 where
leftMap := leftMap S center m
middleMap := middleMap S center m
rightMap := rightMap S center m
left_degree := diagonalMap_flag_degree _
middle_degree := diagonalMap_flag_degree _
right_degree := diagonalMap_flag_degree _
vanishes := by
intro x y z j hj
have hj0 : j = 0 := by omega
subst j
rw [scalingPolynomial, Polynomial.coeff_C_mul_X_pow]
by_cases hs : Tensor.power S.tensor m x y z = 0
Β· simp [hs]
Β· have hw := weight_pos S center m x y z hs
simp [Ne.symm (Nat.ne_of_gt hw)]
leading := by
intro x y z
rw [scalingPolynomial, Polynomial.coeff_C_mul_X_pow, complete_tensor]
simp only [eq_comm]
end RecursiveCompletion
end MatrixMultiplication
end
end OAI