File size: 10,845 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
243
244
245
246
247
248
249
250
251
252
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Populations
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.ComplementSums
import Mathlib.Algebra.BigOperators.Ring.List

namespace OAI

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

noncomputable section

namespace MatrixMultiplication.AllFieldHistory

open AllFieldParameters
open scoped BigOperators
attribute [local instance] Classical.propDecidable Classical.decEq

def secondMass (t : Shape) : ℚ :=
  2 * (positiveInitial.map (fun g => initialLaw g * stageALaw g t)).sum

def littleMass (t u : Shape) : ℚ := 2 * secondMass t * stageBLaw t u

private theorem subtype_sum_indicator {A : Type*} [Fintype A]
    (P : A → Prop) [DecidablePred P] [Fintype {a // P a}] (f : A → ℝ) :
    (∑ a : {a // P a}, f a.val) = ∑ a, if P a then f a else 0 := by
  rw [← Finset.sum_filter]
  exact (Finset.sum_subtype _ (by simp) f).symm

private theorem list_filter_sum {A : Type*} (xs : List A) (p : A → Bool)
    (f : A → ℝ) :
    ((xs.filter p).map f).sum = (xs.map (fun a => if p a then f a else 0)).sum := by
  induction xs with
  | nil => simp
  | cons a xs ih => cases hp : p a <;> simp [hp, ih]

private theorem list_sum_congr {A : Type*} (xs : List A) (f g : A → ℝ)
    (h : ∀ a ∈ xs, f a = g a) : (xs.map f).sum = (xs.map g).sum :=
  congrArg List.sum (List.map_congr_left h)

private theorem list_sum_comm {A B : Type*} (xs : List A) (ys : List B)
    (f : A → B → ℝ) :
    (xs.map (fun a => (ys.map (f a)).sum)).sum =
      (ys.map (fun b => (xs.map (fun a => f a b)).sum)).sum := by
  induction xs with
  | nil => simp
  | cons a xs ih => simp [ih, List.sum_map_add]

private theorem list_rat_cast {A : Type*} (xs : List A) (f : A → ℚ) :
    ((xs.map f).sum : ℝ) = (xs.map (fun a => (f a : ℝ))).sum := by
  induction xs with
  | nil => simp
  | cons a xs ih => simp [ih]

 theorem secondMass_cast (t : Shape) :
    (secondMass t : ℝ) =
      2 * (positiveInitial.map (fun g => (initialLaw g : ℝ) * (stageALaw g t : ℝ))).sum := by
  simp only [secondMass, Rat.cast_mul, Rat.cast_ofNat, list_rat_cast]

 theorem littleMass_cast (t u : Shape) :
    (littleMass t u : ℝ) = 2 * (secondMass t : ℝ) * (stageBLaw t u : ℝ) := by
  simp only [littleMass, Rat.cast_mul, Rat.cast_ofNat]

 theorem initial_history_sum (K : ℕ) (f : Shape → ℝ) :
    (∑ h : Initial K, (initialAmount h : ℝ) * f (initialShape h)) =
      (K : ℝ) * (sortedInitial.map (fun g => (initialLaw g : ℝ) * f g)).sum := by
  simp only [Fintype.sum_prod_type, initialAmount, initialShape, List.get_eq_getElem]
  simp_rw [Fin.sum_univ_fun_getElem sortedInitial (fun g => (initialLaw g : ℝ) * f g)]
  simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]

theorem initial_positive_sum (K : ℕ) (f : Shape → ℝ) :
    (∑ h : InitialPositive K, f (initialShape h.val)) =
      (K : ℝ) * (positiveInitial.map f).sum := by
  rw [subtype_sum_indicator (fun h : Initial K => initialShape h ∈ positiveInitial)
    (fun h => f (initialShape h))]
  rw [Fintype.sum_prod_type]
  change (∑ _ : Fin K, ∑ i : Fin sortedInitial.length,
    if sortedInitial[i.1] ∈ positiveInitial then f sortedInitial[i.1] else 0) = _
  simp_rw [Fin.sum_univ_fun_getElem sortedInitial
    (fun g => if g ∈ positiveInitial then f g else 0)]
  simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]
  apply congrArg (fun r : ℝ => (K : ℝ) * r)
  change _ = ((sortedInitial.filter positive).map f).sum
  rw [list_filter_sum]
  apply list_sum_congr
  intro g hg
  simp only [positiveInitial, List.mem_filter, hg, true_and]

private theorem stageA_parent_sum : ∀ g ∈ positiveInitial, g 0 + g 1 + g 2 = 16 := by
  decide +kernel

private theorem stageA_extend_sum (g : Shape) (hg : g ∈ positiveInitial)
    (f : Shape → ℝ) :
    ((below g).map (fun t => (stageALaw g t : ℝ) * f t)).sum =
      ((shapes 8).map (fun t => (stageALaw g t : ℝ) * f t)).sum := by
  have he : below g = (shapes 8).filter
      (fun t => t 0 ≤ g 0 && t 1 ≤ g 1 && t 2 ≤ g 2) := by
    simp only [below, stageA_parent_sum g hg]
  rw [he, list_filter_sum]
  apply list_sum_congr
  intro t ht
  by_cases hb : (t 0 ≤ g 0 && t 1 ≤ g 1 && t 2 ≤ g 2) = true
  · simp only [hb, ite_true]
  · have hnot : t ∉ below g := by rw [he]; simp [hb]
    have hz : stageALaw g t = 0 := splitLaw_outside_support _ _ g t hnot
    simp [hb, hz]

 theorem a_parent_history_sum {K : ℕ} (h : InitialPositive K) (f : Shape → ℝ) :
    (∑ p : ASplit h × Bool, (aAmount ⟨h, p⟩ : ℝ) * f (aShape ⟨h, p⟩)) =
      2 * (initialAmount h.val : ℝ) *
        ((below (initialShape h.val)).map
          (fun t => (stageALaw (initialShape h.val) t : ℝ) * f t)).sum := by
  simp only [Fintype.sum_prod_type, Fintype.sum_bool, aAmount, aShape, aSplit,
    halfShape, Bool.false_eq_true, ite_false, ite_true, Rat.cast_mul,
    List.get_eq_getElem]
  simp only [mul_assoc, Finset.sum_add_distrib, ← Finset.mul_sum]
  rw [Fin.sum_univ_fun_getElem (below (initialShape h.val))
    (fun t => (stageALaw (initialShape h.val) t : ℝ) *
      f (complement (initialShape h.val) t)),
    Fin.sum_univ_fun_getElem (below (initialShape h.val))
      (fun t => (stageALaw (initialShape h.val) t : ℝ) * f t),
    stageA_complement_sum _ h.property]
  ring

 theorem a_history_sum_parents (K : ℕ) (f : Shape → ℝ) :
    (∑ h : AfterA K, (aAmount h : ℝ) * f (aShape h)) =
      (K : ℝ) * (positiveInitial.map (fun g => 2 * (initialLaw g : ℝ) *
        ((below g).map (fun t => (stageALaw g t : ℝ) * f t)).sum)).sum := by
  rw [Fintype.sum_sigma]
  simp only [a_parent_history_sum, initialAmount]
  exact initial_positive_sum K (fun g => 2 * (initialLaw g : ℝ) *
    ((below g).map (fun t => (stageALaw g t : ℝ) * f t)).sum)

 theorem a_history_sum (K : ℕ) (f : Shape → ℝ) :
    (∑ h : AfterA K, (aAmount h : ℝ) * f (aShape h)) =
      (K : ℝ) * ((shapes 8).map (fun t => (secondMass t : ℝ) * f t)).sum := by
  rw [a_history_sum_parents]
  apply congrArg (fun r : ℝ => (K : ℝ) * r)
  calc
    _ = (positiveInitial.map (fun g => ((shapes 8).map
        (fun t => 2 * (initialLaw g : ℝ) * ((stageALaw g t : ℝ) * f t))).sum)).sum := by
      apply list_sum_congr
      intro g hg
      rw [stageA_extend_sum g hg, List.sum_map_mul_left]
    _ = ((shapes 8).map (fun t => (positiveInitial.map
        (fun g => 2 * (initialLaw g : ℝ) * ((stageALaw g t : ℝ) * f t))).sum)).sum :=
      list_sum_comm _ _ _
    _ = _ := by
      apply list_sum_congr
      intro t _
      rw [secondMass_cast]
      calc
        _ = (positiveInitial.map
            (fun g => 2 * ((initialLaw g : ℝ) * (stageALaw g t : ℝ)) * f t)).sum := by
          apply list_sum_congr
          intro g _
          ring
        _ = _ := by rw [List.sum_map_mul_right, List.sum_map_mul_left]

 theorem a_positive_history_sum (K : ℕ) (f : Shape → ℝ) :
    (∑ h : APositive K, (aAmount h.val : ℝ) * f (aShape h.val)) =
      (K : ℝ) * (positiveSecond.map (fun t => (secondMass t : ℝ) * f t)).sum := by
  rw [subtype_sum_indicator (fun h : AfterA K => aShape h ∈ positiveSecond)
    (fun h => (aAmount h : ℝ) * f (aShape h))]
  trans ∑ h : AfterA K, (aAmount h : ℝ) *
    (if aShape h ∈ positiveSecond then f (aShape h) else 0)
  · apply Finset.sum_congr rfl
    intro h _
    split_ifs <;> simp_all
  rw [a_history_sum K (fun t => if t ∈ positiveSecond then f t else 0)]
  apply congrArg (fun r : ℝ => (K : ℝ) * r)
  change _ = (((shapes 8).filter positive).map
    (fun t => (secondMass t : ℝ) * f t)).sum
  rw [list_filter_sum]
  apply list_sum_congr
  intro t ht
  simp only [positiveSecond, List.mem_filter, ht, true_and]
  split_ifs <;> simp_all

 theorem b_parent_history_sum {K : ℕ} (h : APositive K) (f : Shape → ℝ) :
    (∑ p : BSplit h × Bool, (bAmount ⟨h, p⟩ : ℝ) * f (bShape ⟨h, p⟩)) =
      2 * (aAmount h.val : ℝ) *
        ((below (aShape h.val)).map
          (fun u => (stageBLaw (aShape h.val) u : ℝ) * f u)).sum := by
  simp only [Fintype.sum_prod_type, Fintype.sum_bool, bAmount, bShape, bSplit,
    halfShape, Bool.false_eq_true, ite_false, ite_true, Rat.cast_mul,
    List.get_eq_getElem]
  simp only [mul_assoc, Finset.sum_add_distrib, ← Finset.mul_sum]
  rw [Fin.sum_univ_fun_getElem (below (aShape h.val))
    (fun u => (stageBLaw (aShape h.val) u : ℝ) *
      f (complement (aShape h.val) u)),
    Fin.sum_univ_fun_getElem (below (aShape h.val))
      (fun u => (stageBLaw (aShape h.val) u : ℝ) * f u),
    stageB_complement_sum _ h.property]
  ring

 theorem b_history_sum (K : ℕ) (f : Shape → Shape → ℝ) :
    (∑ h : AfterB K, (bAmount h : ℝ) * f (aShape h.1.val) (bShape h)) =
      (K : ℝ) * (positiveSecond.map (fun t =>
        ((below t).map (fun u => (littleMass t u : ℝ) * f t u)).sum)).sum := by
  rw [Fintype.sum_sigma]
  simp only [b_parent_history_sum]
  have he : (∑ h : APositive K, 2 * (aAmount h.val : ℝ) *
      ((below (aShape h.val)).map
        (fun u => (stageBLaw (aShape h.val) u : ℝ) * f (aShape h.val) u)).sum) =
      ∑ h : APositive K, (aAmount h.val : ℝ) *
        (2 * ((below (aShape h.val)).map
          (fun u => (stageBLaw (aShape h.val) u : ℝ) * f (aShape h.val) u)).sum) := by
    apply Finset.sum_congr rfl
    intro h _
    ring
  rw [he, a_positive_history_sum K (fun t =>
    2 * ((below t).map (fun u => (stageBLaw t u : ℝ) * f t u)).sum)]
  apply congrArg (fun r : ℝ => (K : ℝ) * r)
  apply list_sum_congr
  intro t _
  calc
    _ = 2 * (secondMass t : ℝ) *
        ((below t).map (fun u => (stageBLaw t u : ℝ) * f t u)).sum := by ring
    _ = _ := by
      rw [← List.sum_map_mul_left]
      apply list_sum_congr
      intro u _
      rw [littleMass_cast]
      ring

 theorem b_positive_history_sum (K : ℕ) (f : Shape → Shape → ℝ) :
    (∑ h : BPositive K, (bAmount h.val : ℝ) *
      f (aShape h.val.1.val) (bShape h.val)) =
      (K : ℝ) * (positiveSecond.map (fun t =>
        (((below t).filter positive).map
          (fun u => (littleMass t u : ℝ) * f t u)).sum)).sum := by
  rw [subtype_sum_indicator (fun h : AfterB K => positive (bShape h) = true)
    (fun h => (bAmount h : ℝ) * f (aShape h.1.val) (bShape h))]
  trans ∑ h : AfterB K, (bAmount h : ℝ) *
    (if positive (bShape h) then f (aShape h.1.val) (bShape h) else 0)
  · apply Finset.sum_congr rfl
    intro h _
    split_ifs <;> simp_all
  rw [b_history_sum K (fun t u => if positive u then f t u else 0)]
  apply congrArg (fun r : ℝ => (K : ℝ) * r)
  apply list_sum_congr
  intro t _
  rw [list_filter_sum]
  apply list_sum_congr
  intro u _
  split_ifs <;> simp_all

end MatrixMultiplication.AllFieldHistory

end

end OAI