File size: 5,245 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
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Sums
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.ZeroLeafRateFormula
import OAI.LinearAlgebra.MatrixMultiplication.FieldParameters.Contractions

namespace OAI

/-! Tensor extraction over arbitrary fields and its asymptotic rate. -/

noncomputable section

namespace MatrixMultiplication.AllFieldTerminalRates

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

def initialRate (g : Shape) : ℝ :=
  Real.log (Contractions.coefficients[shapeMax g]?.getD 0 : ℝ)

def S0 : ℝ :=
  ((sortedInitial.filter (fun g => !positive g)).map
    (fun g => (initialLaw g : ℝ) * initialRate g)).sum

def S1 : ℝ :=
  (zeroSecond.map (fun t => (secondMass t : ℝ) * AllFieldZeroLeafRates.fourRate t)).sum

def secondLeafRate (t u : Shape) : ℝ :=
  if shapeMax u = 4 then 0
  else if shapeMax u = 3 then Real.log 10
  else if !positive u then
    binaryEntropyRate (binaryParameter t u) +
      2 * (1 - (binaryParameter t u : ℝ)) * Real.log 5
  else (2 - (binaryParameter t u : ℝ)) * Real.log 5

def S2 : ℝ :=
  (positiveSecond.map (fun t =>
    ((below t).map (fun u => (littleMass t u : ℝ) * secondLeafRate t u)).sum)).sum

def S : ℝ := (S0 + S1) + S2

private theorem four_shape_max_lt_five : ∀ u ∈ shapes 4, shapeMax u < 5 := by
  decide +kernel

private theorem four_zero_max : ∀ u ∈ shapes 4, positive u = false →
    shapeMax u = 2 ∨ shapeMax u = 3 ∨ shapeMax u = 4 := by
  decide +kernel

private theorem four_positive_max : ∀ u ∈ shapes 4, positive u = true →
    shapeMax u = 2 := by
  decide +kernel

theorem second_shapeMax_lt_five (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) : shapeMax u < 5 :=
  four_shape_max_lt_five u (stageB_children_size t ht u hu)

theorem secondLeafRate_zero (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) (hz : positive u = false) :
    secondLeafRate t u = statisticRate (binaryParameter t u)
      ⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ := by
  rcases four_zero_max u (stageB_children_size t ht u hu) hz with h2 | h3 | h4
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 2 :=
      Fin.ext h2
    rw [hk, statisticRate_two]
    simp [secondLeafRate, h2, hz]
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 3 :=
      Fin.ext h3
    rw [hk, statisticRate_three]
    simp [secondLeafRate, h3]
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 4 :=
      Fin.ext h4
    rw [hk, statisticRate_four]
    simp [secondLeafRate, h4]

theorem secondLeafRate_positive (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) (hp : positive u = true) :
    secondLeafRate t u = (2 - (binaryParameter t u : ℝ)) * Real.log 5 := by
  have hm := four_positive_max u (stageB_children_size t ht u hu) hp
  simp [secondLeafRate, hm, hp]

theorem secondLeafRate_builder_expression (t u : Shape) :
    secondLeafRate t u =
      if shapeMax u = 4 then 0
      else if shapeMax u = 3 then Real.log 10
      else if !positive u then
        (MatrixMultiplication.Foundation.entropyTerm (1 - (binaryParameter t u : ℝ)) +
          MatrixMultiplication.Foundation.entropyTerm (binaryParameter t u : ℝ) +
          (binaryParameter t u : ℝ) * Real.log 2) +
          ((1 - (binaryParameter t u : ℝ)) * 2) * Real.log 5
      else (2 - (binaryParameter t u : ℝ)) * Real.log 5 := by
  unfold secondLeafRate
  split_ifs <;> try rfl
  unfold binaryEntropyRate
  ring

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]

theorem initial_zero_history_rate (K : ℕ) :
    (∑ h : {h : Initial K // initialShape h ∉ positiveInitial},
      (initialAmount h.val : ℝ) * initialRate (initialShape h.val)) = (K : ℝ) * S0 := by
  calc
    _ = ∑ h : Initial K, if initialShape h ∉ positiveInitial then
        (initialAmount h : ℝ) * initialRate (initialShape h) else 0 := by
      rw [← Finset.sum_filter]
      exact (Finset.sum_subtype
        (p := fun h : Initial K => initialShape h ∉ positiveInitial)
        (Finset.univ.filter (fun h : Initial K => initialShape h ∉ positiveInitial))
        (by simp)
        (fun h : Initial K => (initialAmount h : ℝ) * initialRate (initialShape h))).symm
    _ = ∑ h : Initial K, (initialAmount h : ℝ) *
        (if initialShape h ∉ positiveInitial then initialRate (initialShape h) else 0) := by
      apply Finset.sum_congr rfl
      intro h _
      split_ifs <;> simp
    _ = _ := by
      rw [initial_history_sum K
        (fun g => if g ∉ positiveInitial then initialRate g else 0), S0, list_filter_sum]
      apply congrArg (fun x : ℝ => (K : ℝ) * x)
      apply congrArg List.sum
      apply List.map_congr_left
      intro g hg
      cases hp : positive g <;> simp [positiveInitial, hg, hp]

end MatrixMultiplication.AllFieldTerminalRates

end

end OAI