File size: 12,418 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.IncomingCore
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.MaskLaws
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.RawMasks
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.StageCIncoming
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Tensors

namespace OAI

/-! Finite extraction histories, inherited masks and recovery bounds. -/

noncomputable section

namespace MatrixMultiplication.AllFieldHistoryIncomingMasks

open MatrixMultiplication.Foundation AllFieldHistory AllFieldParameters
open AllFieldHistoryChildLaws AllFieldHistorySupport AllFieldHistoryMasks
open AllFieldHistoryMaskLaws AllFieldHistoryRawMasks InheritedMasks
open scoped BigOperators
attribute [local instance] Classical.propDecidable Classical.decEq

variable {K tick : ℕ}

theorem empiricalLaw_congr_decidable {P A : Type*} [Fintype P]
    (d₁ d₂ : DecidableEq A) (w : P → A) (a : A) :
    @empiricalLaw P A _ d₁ w a = @empiricalLaw P A _ d₂ w a := by
  cases Subsingleton.elim d₁ d₂
  rfl

theorem typeWindow_mono_decidable {P A : Type*} [Fintype P]
    (d₁ d₂ : DecidableEq A) (ν : A → ℝ) {η δ : ℝ} (hη : η ≤ δ)
    (w : P → A) (hw : @typeWindow P A _ d₁ ν η w) :
    @typeWindow P A _ d₂ ν δ w := by
  cases Subsingleton.elim d₁ d₂
  intro a
  exact (hw a).trans hη

theorem weight_append {m n : ℕ} (l : Fin m → Fin 7) (r : Fin n → Fin 7) :
    CWStrands.weight (Fin.append l r) = CWStrands.weight l + CWStrands.weight r := by
  simp [CWStrands.weight, Fin.sum_univ_add]

theorem strand_append {F : Type*} [CommRing F] {m n : ℕ}
    (x y z : (Fin m → Fin 7) × (Fin n → Fin 7)) :
    CWStrands.strand (F := F) (Fin (m + n))
      (Fin.append x.1 x.2) (Fin.append y.1 y.2) (Fin.append z.1 z.2) =
        CWStrands.strand (Fin m) x.1 y.1 z.1 * CWStrands.strand (Fin n) x.2 y.2 z.2 := by
  simp [CWStrands.strand, Fin.prod_univ_add]

theorem shapeTensor_append {F : Type*} [CommRing F] {m n : ℕ} (g : Shape)
    (x y z : (Fin m → Fin 7) × (Fin n → Fin 7)) :
    CWStrands.shapeTensor (F := F) (Fin (m + n)) g
      (Fin.append x.1 x.2) (Fin.append y.1 y.2) (Fin.append z.1 z.2) =
        CWStrands.shapeTensor (Fin m ⊕ Fin n) g
          (Sum.elim x.1 x.2) (Sum.elim y.1 y.2) (Sum.elim z.1 z.2) := by
  simp only [CWStrands.shapeTensor, weight_append, CWStrands.weight_sum_elim,
    strand_append, CWStrands.strand_sum_elim, Tensor.product]

theorem shapeTensor_joinHalves {F : Type*} [CommRing F] (w : Work K) (g : Shape)
    (x y z : (Fin w.halfLength → Fin 7) × (Fin w.halfLength → Fin 7)) :
    CWStrands.shapeTensor (F := F) (Fin (currentLength w.source)) g
      (joinHalves w x) (joinHalves w y) (joinHalves w z) =
        CWStrands.shapeTensor (Fin w.halfLength ⊕ Fin w.halfLength) g
          (Sum.elim x.1 x.2) (Sum.elim y.1 y.2) (Sum.elim z.1 z.2) := by
  cases w <;> exact shapeTensor_append g x y z

def defaultStatistic : (w : Work K) → w.Statistic
  | .stageA _ => (0, 0)
  | .stageB _ => (0 : Fin 6)
  | .stageC _ => PUnit.unit

def choiceAt (h : Active K tick) (a : Statistic h) : SymbolChoice K tick :=
  Function.update (fun g => defaultStatistic g.val.1) h a

@[simp] theorem choiceAt_self (h : Active K tick) (a : Statistic h) :
    choiceAt h a h = a := by simp [choiceAt]

theorem rawPairCount_eq_wordPopulation (allocation : Allocation) (m : ℕ)
    (i : MaskIndex K tick) (w : ActiveRawPairs (K := K) (tick := tick) allocation m) :
    rawPairCount allocation m i w =
      (wordPopulation (fun j => (statistic i.1 (w i.1 j).1,
        statistic i.1 (w i.1 j).2)) (i.2.1 i.1, i.2.2 i.1) : ℝ) := by
  simp only [rawPairCount, wordPopulation, Fintype.card_subtype,
    Finset.natCast_card_filter, Prod.mk.injEq]

@[simp] theorem fourStatistic_append (l r : Fin 2 → Fin 7) :
    CWCompleteStatistics.fourStatistic (Fin.append l r) =
      (CWCompleteStatistics.twoStatistic l, CWCompleteStatistics.twoStatistic r) := rfl

@[simp] theorem append_singletons (l r : Fin 1 → Fin 7) :
    Fin.append l r = ![l 0, r 0] := by
  funext i
  fin_cases i <;> rfl

theorem rawPass_stageB_typeWindow (allocation : Allocation) (m : ℕ) (ε : ℝ)
    (side : Fin 3) (w : ActiveRawPairs (K := K) (tick := tick) allocation m)
    (hw : rawPass allocation m ε side w) (h : APositive K) (phi : Placement)
    (ht : FiniteSchedule.lotTick (Work.stageB h).lot (Work.stageB h).stage = tick) :
    typeWindow (fun ij : PairSlot => (halfLaw (aShape h.val) (phi.symm side) ij : ℝ))
      (pairWidth ε (stageBActive h phi ht))
      (fun j => CWCompleteStatistics.fourStatistic
        (joinHalves (.stageB h) (w (stageBActive h phi ht) j))) := by
  intro ij
  have hp := hw (stageBActive h phi ht,
    choiceAt (stageBActive h phi ht) ij.1, choiceAt (stageBActive h phi ht) ij.2)
  rw [center_stageB] at hp
  simp only [choiceAt_self (stageBActive h phi ht) ij.1,
    choiceAt_self (stageBActive h phi ht) ij.2] at hp
  have hcard : Fintype.card (JointPopulation.Positions (activeCounts allocation m)
      (stageBActive h phi ht)) = population allocation m
        ((stageBActive h phi ht).val.1.source, (stageBActive h phi ht).val.2) := by
    exact (Fintype.card_congr
      (sourcePositionsEquiv allocation m (stageBActive h phi ht))).trans
        (Fintype.card_fin _)
  have hcount : (wordPopulation (fun j =>
      (statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).1,
        statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).2)) ij : ℝ) =
      rawPairCount allocation m (stageBActive h phi ht,
        choiceAt (stageBActive h phi ht) ij.1, choiceAt (stageBActive h phi ht) ij.2) w := by
    rcases ij with ⟨a, b⟩
    simp only [wordPopulation, Fintype.card_subtype,
      Finset.natCast_card_filter, rawPairCount]
    apply Finset.sum_congr rfl
    intro j _
    apply if_congr
    · simp only [choiceAt_self (stageBActive h phi ht) a,
        choiceAt_self (stageBActive h phi ht) b]
      exact Prod.mk_inj
    · rfl
    · rfl
  have hword : (fun j => CWCompleteStatistics.fourStatistic
      (joinHalves (.stageB h) (w (stageBActive h phi ht) j))) =
      (fun j => (statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).1,
        statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).2)) := by
    funext j
    rfl
  have he : empiricalLaw (fun j => CWCompleteStatistics.fourStatistic
      (joinHalves (.stageB h) (w (stageBActive h phi ht) j))) ij =
      empiricalLaw (fun j =>
        (statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).1,
          statistic (stageBActive h phi ht) (w (stageBActive h phi ht) j).2)) ij := by
    rw [hword]
    exact empiricalLaw_congr_decidable _ _ _ _
  rw [he]
  unfold empiricalLaw
  rw [hcard, hcount]
  exact hp

attribute [local irreducible] activeCounts population

theorem incoming_stageA (allocation : Allocation) (m : ℕ) (ε : ℝ)
    (side : Fin 3) (w : ActiveRawPairs (K := K) (tick := tick) allocation m)
    (h : InitialPositive K) (phi : Placement)
    (ht : FiniteSchedule.lotTick (Work.stageA h).lot (Work.stageA h).stage = tick) :
    residentMask allocation m ε ((Work.stageA h).source, phi) side
      (incomingWord allocation m ⟨(.stageA h, phi), ht⟩ w) := by
  trivial

theorem incoming_stageB (allocation : Allocation) (m : ℕ) {ε : ℝ} (hε : 0 ≤ ε)
    (side : Fin 3) (w : ActiveRawPairs (K := K) (tick := tick) allocation m)
    (hw : rawPass allocation m ε side w) (h : APositive K) (phi : Placement)
    (ht : FiniteSchedule.lotTick (Work.stageB h).lot (Work.stageB h).stage = tick) :
    residentMask allocation m ε ((Work.stageB h).source, phi) side
      (incomingWord allocation m (stageBActive h phi ht) w) := by
  intro _
  have hraw := rawPass_stageB_typeWindow allocation m ε side w hw h phi ht
  have htransport := (JointCanonicalization.typeWindow_reindex
    (fun ij : PairSlot => (halfLaw (aShape h.val) (phi.symm side) ij : ℝ))
    (pairWidth ε (stageBActive h phi ht))
    (fun j => CWCompleteStatistics.fourStatistic
      (joinHalves (.stageB h) (w (stageBActive h phi ht) j)))
    (sourcePositionsEquiv allocation m (stageBActive h phi ht)).symm).2 hraw
  have hwidth : pairWidth ε (stageBActive h phi ht) ≤ ε / 8 := by
    change ε / 16 ^ 1 ≤ ε / 8
    norm_num
    linarith
  dsimp only [Work.source, residentLaw, residentLawRat, residentWidth, residentStatistic]
  exact typeWindow_mono_decidable _ _ _ hwidth _ htransport

theorem incoming_stageC (allocation : Allocation) {m : ℕ} (hm : 0 < m)
    {ε : ℝ} (hε : 0 ≤ ε) (side : Fin 3)
    (e : JointPopulation.Target (activeCounts (K := K) (tick := tick) allocation m))
    (w : ActiveRawPairs (K := K) (tick := tick) allocation m)
    (h : PartC K) (phi : Placement)
    (ht : FiniteSchedule.lotTick (Work.stageC h).lot (Work.stageC h).stage = tick)
    (hweights : ∀ j,
      CWLeafStatistics.weight ((w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).1 (0 : Fin 1)) =
        (JointPopulation.shapeSide side ((e (AllFieldHistoryStageCIncoming.stageCActive h phi ht)).val j)).val ∧
      CWLeafStatistics.weight ((w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).2 (0 : Fin 1)) =
        activeParentShape (AllFieldHistoryStageCIncoming.stageCActive h phi ht) side -
          (JointPopulation.shapeSide side ((e (AllFieldHistoryStageCIncoming.stageCActive h phi ht)).val j)).val) :
    residentMask allocation m ε ((Work.stageC h).source, phi) side
      (incomingWord allocation m (AllFieldHistoryStageCIncoming.stageCActive h phi ht) w) := by
  intro _
  have hraw := AllFieldHistoryStageCIncoming.typeWindow_of_target_side_weights
    allocation hm h phi ht e w side hweights (ε / 256) (div_nonneg hε (by norm_num))
  have hword :
      (fun j => CWCompleteStatistics.twoStatistic
        (joinHalves (.stageC h)
          (w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j))) =
      (fun j => CWCompleteStatistics.twoStatistic
        ![(w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).1 (0 : Fin 1),
          (w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).2 (0 : Fin 1)]) := by
    funext j
    exact congrArg CWCompleteStatistics.twoStatistic (append_singletons _ _)
  rw [← hword] at hraw
  have htransport := (JointCanonicalization.typeWindow_reindex
    (fun a : Fin 6 => (littleLaw (cParameterParent h) (cShapeParent h) (phi.symm side) a : ℝ))
    (ε / 256)
    (fun j => CWCompleteStatistics.twoStatistic
      (joinHalves (.stageC h)
        (w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j)))
    (sourcePositionsEquiv allocation m (AllFieldHistoryStageCIncoming.stageCActive h phi ht)).symm).2 hraw
  exact typeWindow_mono_decidable _ _ _ le_rfl _ htransport

def stageCWeights (allocation : Allocation) (m : ℕ) (side : Fin 3)
    (e : JointPopulation.Target (activeCounts (K := K) (tick := tick) allocation m))
    (w : ActiveRawPairs (K := K) (tick := tick) allocation m) : Prop :=
  ∀ (h : PartC K) (phi : Placement)
    (ht : FiniteSchedule.lotTick (Work.stageC h).lot (Work.stageC h).stage = tick) j,
      CWLeafStatistics.weight ((w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).1 (0 : Fin 1)) =
        (JointPopulation.shapeSide side ((e (AllFieldHistoryStageCIncoming.stageCActive h phi ht)).val j)).val ∧
      CWLeafStatistics.weight ((w (AllFieldHistoryStageCIncoming.stageCActive h phi ht) j).2 (0 : Fin 1)) =
        activeParentShape (AllFieldHistoryStageCIncoming.stageCActive h phi ht) side -
          (JointPopulation.shapeSide side ((e (AllFieldHistoryStageCIncoming.stageCActive h phi ht)).val j)).val

theorem received_of_rawPass (allocation : Allocation) {m : ℕ} (hm : 0 < m)
    {ε : ℝ} (hε : 0 ≤ ε) (side : Fin 3)
    (e : JointPopulation.Target (activeCounts (K := K) (tick := tick) allocation m))
    (w : ActiveRawPairs (K := K) (tick := tick) allocation m)
    (hw : rawPass allocation m ε side w) (hc : stageCWeights allocation m side e w) :
    received allocation m ε side w := by
  rintro ⟨⟨work, phi⟩, ht⟩
  cases work with
  | stageA h => exact incoming_stageA allocation m ε side w h phi ht
  | stageB h => exact incoming_stageB allocation m hε side w hw h phi ht
  | stageC h => exact incoming_stageC allocation hm hε side e w h phi ht (hc h phi ht)

end MatrixMultiplication.AllFieldHistoryIncomingMasks

end

end OAI