File size: 5,468 Bytes
cdec422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import OAI.LinearAlgebra.MatrixMultiplication.Completion.Labels
import Mathlib.Data.Fintype.Sum
import Mathlib.Data.Fintype.BigOperators

namespace OAI

/-! Readable tensor completion and its finite arithmetic realization. -/

noncomputable section

namespace MatrixMultiplication.CompletionCardinality

open MatrixMultiplication.Foundation RecursiveCompletion CompletionLabels
attribute [local instance] Classical.propDecidable Classical.decEq

universe uCoord
variable {X Y Z : Type uCoord}

def conditionalCount [Fintype X] [Fintype Y] [Fintype Z]
    (S : FlaggedTensor X Y Z) (c : Color) : ℕ :=
  Fintype.card {a : Leaf S // leafColor S a = c}

private abbrev AllowedLeaf (S : FlaggedTensor X Y Z) (center output : Color) :=
  {a : Leaf S // leafColor S a = center ∨ leafColor S a = output}

private def centerLeafEquiv (S : FlaggedTensor X Y Z) (center : Color) (m : ℕ) :
    {a : Leaf (complete S center m) //
      leafColor (complete S center m) a = center} ≃
      (Fin m → {a : Leaf S // leafColor S a = center}) :=
  (conditionalLeafEquiv S center center m).trans
    ((Equiv.subtypeEquivRight (fun w => by simp [raisedFlag])).trans
      Equiv.subtypePiEquivPi)

private def noncenterLeafEquiv (S : FlaggedTensor X Y Z) (center output : Color)
    (m : ℕ) (h : center ≠ output) :
    {a : Leaf (complete S center m) //
      leafColor (complete S center m) a = output} ≃
      {w : Fin m → AllowedLeaf S center output //
        ¬ ∀ i, leafColor S (w i).val = center} :=
  (conditionalLeafEquiv S center output m).trans
    { toFun := fun w =>
        ⟨fun i => ⟨w.val i, w.property.2 i⟩, by
          intro hall
          have hex : ∃ i, leafColor S (w.val i) = output := by
            simpa only [raisedFlag, ite_eq_right h] using w.property.1
          obtain ⟨i, hi⟩ := hex
          exact h ((hall i).symm.trans hi)⟩
      invFun := fun w =>
        ⟨fun i => (w.val i).val, by
          constructor
          · simp only [raisedFlag, ite_eq_right h]
            by_contra hex
            apply w.property
            intro i
            rcases (w.val i).property with hc | ho
            · exact hc
            · exact False.elim (hex ⟨i, ho⟩)
          · exact fun i => (w.val i).property⟩
      left_inv := fun _ => Subtype.ext rfl
      right_inv := fun _ => Subtype.ext rfl }

private def allCenterEquiv (S : FlaggedTensor X Y Z) (center output : Color)
    (m : ℕ) :
    {w : Fin m → AllowedLeaf S center output //
      ∀ i, leafColor S (w i).val = center} ≃
      (Fin m → {a : Leaf S // leafColor S a = center}) where
  toFun w i := ⟨(w.val i).val, w.property i⟩
  invFun w := ⟨fun i => ⟨(w i).val, Or.inl (w i).property⟩,
    fun i => (w i).property⟩
  left_inv _ := Subtype.ext rfl
  right_inv _ := rfl

variable [Fintype X] [Fintype Y] [Fintype Z]

private theorem card_allowedLeaf (S : FlaggedTensor X Y Z) (center output : Color)
    (h : center ≠ output) :
    Fintype.card (AllowedLeaf S center output) =
      conditionalCount S center + conditionalCount S output := by
  apply Fintype.card_subtype_or_disjoint
  exact Pi.disjoint_iff.mpr fun a => Prop.disjoint_iff.mpr
    (fun ha => h (ha.1.symm.trans ha.2))

theorem count_complete_center (S : FlaggedTensor X Y Z) (center : Color) (m : ℕ) :
    conditionalCount (complete S center m) center = conditionalCount S center ^ m := by
  calc
    conditionalCount (complete S center m) center =
        Fintype.card (Fin m → {a : Leaf S // leafColor S a = center}) :=
      Fintype.card_congr (centerLeafEquiv S center m)
    _ = conditionalCount S center ^ m := by simp [conditionalCount]

theorem count_complete_noncenter (S : FlaggedTensor X Y Z) (center output : Color)
    (m : ℕ) (h : center ≠ output) :
    conditionalCount (complete S center m) output =
      (conditionalCount S center + conditionalCount S output) ^ m -
        conditionalCount S center ^ m := by
  have hall : Fintype.card {w : Fin m → AllowedLeaf S center output //
      ∀ i, leafColor S (w i).val = center} = conditionalCount S center ^ m := by
    calc
      _ = Fintype.card (Fin m → {a : Leaf S // leafColor S a = center}) :=
        Fintype.card_congr (allCenterEquiv S center output m)
      _ = _ := by simp [conditionalCount]
  calc
    conditionalCount (complete S center m) output =
        Fintype.card {w : Fin m → AllowedLeaf S center output //
          ¬ ∀ i, leafColor S (w i).val = center} :=
      Fintype.card_congr (noncenterLeafEquiv S center output m h)
    _ = Fintype.card (Fin m → AllowedLeaf S center output) -
        Fintype.card {w : Fin m → AllowedLeaf S center output //
          ∀ i, leafColor S (w i).val = center} := Fintype.card_subtype_compl _
    _ = (conditionalCount S center + conditionalCount S output) ^ m -
        conditionalCount S center ^ m := by
      rw [hall, Fintype.card_fun, Fintype.card_fin, card_allowedLeaf S center output h]

theorem count_complete (S : FlaggedTensor X Y Z) (center : Color) (m : ℕ) (c : Color) :
    conditionalCount (complete S center m) c =
      if c = center then conditionalCount S center ^ m
      else (conditionalCount S center + conditionalCount S c) ^ m -
        conditionalCount S center ^ m := by
  by_cases h : c = center
  · subst c
    simpa using count_complete_center S center m
  · simpa [h] using count_complete_noncenter S center c m (Ne.symm h)

end MatrixMultiplication.CompletionCardinality

end

end OAI