File size: 8,142 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
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Construction.InitialState
import OAI.LinearAlgebra.MatrixFields.Entropy.InitialLeafRates
import OAI.LinearAlgebra.MatrixFields.Entropy.TerminalStatisticLaws

namespace OAI

namespace MatrixAllFields

open scoped BigOperators Topology Polynomial

noncomputable section

namespace MatrixMultiplication.AllFieldFinalState

open MatrixMultiplication.Foundation AllFieldHistory AllFieldFiniteFamily
open AllFieldTerminalStatisticLaws
open scoped BigOperators Classical
attribute [local instance] Classical.propDecidable Classical.decEq

abbrev InitialIndex (K : ℕ) := AllFieldInitialLeafRates.PopulationHistory K
abbrev CIndex (K : ℕ) := AfterC K × Placement
abbrev Index (K : ℕ) := InitialIndex K ⊕ (TerminalZero K ⊕ CIndex K)

def history {K : ℕ} : Index K → History K
  | .inl h => (.initial h.1.val, h.2)
  | .inr (.inl h) => AllFieldTerminalStatisticLaws.history h
  | .inr (.inr h) => (.afterC h.1, h.2)

theorem history_terminal {K : ℕ} (h : Index K) : terminal (history h).1 := by
  rcases h with h | h | h
  · exact h.1.property
  · rcases h with h | h
    · exact h.1.property
    · exact h.1.property
  · trivial

theorem terminal_resident_final {K : ℕ} (h : CanonicalHistory K) (ht : terminal h) :
    resident (K + 2) h := by
  have hlot := (initialAncestor h).1.isLt
  cases h with
  | initial h => exact fun hp => False.elim (ht hp)
  | afterA h =>
      refine ⟨?_, fun hp => False.elim (ht hp)⟩
      change h.1.val.1.val < K at hlot
      omega
  | afterB h =>
      refine ⟨?_, ht⟩
      change h.1.val.1.val.1.val < K at hlot
      omega
  | partC h => exact False.elim ht
  | afterC h =>
      change h.1.1.val.1.val.1.val.1.val < K at hlot
      change h.1.1.val.1.val.1.val.1.val + 3 ≤ K + 2
      omega

def toState {K : ℕ} (h : Index K) : State K (K + 2) :=
  ⟨history h, terminal_resident_final _ (history_terminal h)⟩

def fromState {K : ℕ} (h : State K (K + 2)) : Index K := by
  have ht := final_state_terminal h
  rcases h with ⟨⟨h, phi⟩, hr⟩
  cases h with
  | initial h => exact .inl (⟨h, ht⟩, phi)
  | afterA h => exact .inr (.inl (.inl (⟨h, ht⟩, phi)))
  | afterB h => exact .inr (.inl (.inr (⟨h, ht⟩, phi)))
  | partC h => exact False.elim ht
  | afterC h => exact .inr (.inr (h, phi))

def indexEquiv (K : ℕ) : Index K ≃ State K (K + 2) where
  toFun := toState
  invFun := fromState
  left_inv h := by
    rcases h with h | h | h
    · rfl
    · cases h <;> rfl
    · rfl
  right_inv h := by
    have ht := final_state_terminal h
    rcases h with ⟨⟨h, phi⟩, hr⟩
    cases h with
    | initial h => rfl
    | afterA h => rfl
    | afterB h => rfl
    | partC h => exact False.elim ht
    | afterC h => rfl

variable {K : ℕ} (allocation : Allocation) (dilation : ℕ)

abbrev Words := ∀ h : Index K, HistoryWord allocation dilation (history h)
abbrev InitialWords := ∀ h : InitialIndex K,
  HistoryWord allocation dilation (.initial h.1.val, h.2)
abbrev ZeroWords := ∀ h : TerminalZero K,
  HistoryWord allocation dilation (AllFieldTerminalStatisticLaws.history h)
abbrev CWords := ∀ h : CIndex K, HistoryWord allocation dilation (.afterC h.1, h.2)
abbrev Parts := InitialWords (K := K) allocation dilation ×
  (ZeroWords (K := K) allocation dilation × CWords (K := K) allocation dilation)

instance wordsFintype : Fintype (Words (K := K) allocation dilation) :=
  Fintype.ofFinite _

def grouped (F : Type*) [Field F] (ε : ℝ) :
    Tensor F (Words (K := K) allocation dilation) (Words (K := K) allocation dilation)
      (Words (K := K) allocation dilation) :=
  CommonDimensions.familyProduct (fun h : Index K =>
    historyTensor F allocation dilation ε (history h))

def stateWord (w : Words (K := K) allocation dilation) :
    ∀ h : State K (K + 2), HistoryWord allocation dilation h.val :=
  Equiv.piCongrLeft (fun h : State K (K + 2) => HistoryWord allocation dilation h.val)
    (indexEquiv K) w

theorem grouped_coefficient (F : Type*) [Field F] (ε : ℝ)
    (x y z : Words (K := K) allocation dilation) :
    stateTensor F allocation dilation ε (K + 2) (stateWord allocation dilation x)
      (stateWord allocation dilation y) (stateWord allocation dilation z) =
    grouped allocation dilation F ε x y z := by
  change (∏ h : State K (K + 2), historyTensor F allocation dilation ε h.val
    (stateWord allocation dilation x h) (stateWord allocation dilation y h)
      (stateWord allocation dilation z h)) = _
  rw [← (indexEquiv K).prod_comp]
  unfold grouped CommonDimensions.familyProduct
  apply Finset.prod_congr rfl
  intro h _
  have hstate (w : Words (K := K) allocation dilation) :
      stateWord allocation dilation w ((indexEquiv K) h) = w h :=
    Equiv.piCongrLeft_apply_apply
      (fun state : State K (K + 2) => HistoryWord allocation dilation state.val)
      (indexEquiv K) w h
  rw [hstate, hstate, hstate]
  rfl

def groupedMap (F : Type*) [Field F] (ε : ℝ) :
    LocalMap (stateTensor F (K := K) allocation dilation ε (K + 2))
      (grouped (K := K) allocation dilation F ε) := by
  apply LocalMap.ofExists
  refine ⟨_, _, _, (Tensor.pullback_eq_restrict
    (stateWord (K := K) allocation dilation) (stateWord (K := K) allocation dilation)
    (stateWord (K := K) allocation dilation) _).symm.trans ?_⟩
  funext x y z
  exact grouped_coefficient allocation dilation F ε x y z

def initialTensor (F : Type*) [Field F] (ε : ℝ) :
    Tensor F (InitialWords (K := K) allocation dilation) (InitialWords (K := K) allocation dilation)
      (InitialWords (K := K) allocation dilation) :=
  CommonDimensions.familyProduct (fun h : InitialIndex K =>
    historyTensor F allocation dilation ε (.initial h.1.val, h.2))

def zeroTensor (F : Type*) [Field F] (ε : ℝ) :
    Tensor F (ZeroWords (K := K) allocation dilation) (ZeroWords (K := K) allocation dilation)
      (ZeroWords (K := K) allocation dilation) :=
  CommonDimensions.familyProduct (fun h : TerminalZero K =>
    historyTensor F allocation dilation ε (AllFieldTerminalStatisticLaws.history h))

def cTensor (F : Type*) [Field F] (ε : ℝ) :
    Tensor F (CWords (K := K) allocation dilation) (CWords (K := K) allocation dilation)
      (CWords (K := K) allocation dilation) :=
  CommonDimensions.familyProduct (fun h : CIndex K =>
    historyTensor F allocation dilation ε (.afterC h.1, h.2))

def partsTensor (F : Type*) [Field F] (ε : ℝ) :
    Tensor F (Parts (K := K) allocation dilation) (Parts (K := K) allocation dilation)
      (Parts (K := K) allocation dilation) :=
  Tensor.product (initialTensor (K := K) allocation dilation F ε)
    (Tensor.product (zeroTensor (K := K) allocation dilation F ε)
      (cTensor (K := K) allocation dilation F ε))

def joinParts (w : Parts (K := K) allocation dilation) : Words (K := K) allocation dilation
  | .inl h => w.1 h
  | .inr (.inl h) => w.2.1 h
  | .inr (.inr h) => w.2.2 h

theorem parts_coefficient (F : Type*) [Field F] (ε : ℝ)
    (x y z : Parts (K := K) allocation dilation) :
    grouped allocation dilation F ε (joinParts allocation dilation x)
      (joinParts allocation dilation y) (joinParts allocation dilation z) =
    partsTensor allocation dilation F ε x y z := by
  unfold grouped CommonDimensions.familyProduct
  rw [Fintype.prod_sum_type, Fintype.prod_sum_type]
  rfl

def partsMap (F : Type*) [Field F] (ε : ℝ) :
    LocalMap (grouped (K := K) allocation dilation F ε)
      (partsTensor (K := K) allocation dilation F ε) := by
  apply LocalMap.ofExists
  refine ⟨_, _, _, (Tensor.pullback_eq_restrict
    (joinParts (K := K) allocation dilation) (joinParts (K := K) allocation dilation)
    (joinParts (K := K) allocation dilation) _).symm.trans ?_⟩
  funext x y z
  exact parts_coefficient allocation dilation F ε x y z

def map (F : Type*) [Field F] (ε : ℝ) :
    LocalMap (stateTensor F (K := K) allocation dilation ε (K + 2))
      (partsTensor (K := K) allocation dilation F ε) :=
  (groupedMap allocation dilation F ε).comp (partsMap allocation dilation F ε)

end MatrixMultiplication.AllFieldFinalState

end

end MatrixAllFields

end OAI