File size: 14,863 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import OAI.LinearAlgebra.MatrixMultiplication.Arithmetic.MatrixMapDecidable
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.FinalState
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.InitialZeroFinish
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.ZeroFinish
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.StageCFinish
import OAI.LinearAlgebra.MatrixMultiplication.FieldConstruction.Terminal

namespace OAI

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

noncomputable section

namespace MatrixMultiplication.AllFieldFinishedWitness

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

private def localMapWithFintypes {F : Type*} [Field F]
    {X Y Z X' Y' Z' : Type} [newX : Fintype X] [newY : Fintype Y] [newZ : Fintype Z]
    {oldX : Fintype X} {oldY : Fintype Y} {oldZ : Fintype Z}
    {T : Tensor F X Y Z} {Q : Tensor F X' Y' Z'}
    (M : @LocalMap F _ X Y Z X' Y' Z' oldX oldY oldZ T Q) :
    @LocalMap F _ X Y Z X' Y' Z' newX newY newZ T Q := by
  cases Subsingleton.elim oldX newX
  cases Subsingleton.elim oldY newY
  cases Subsingleton.elim oldZ newZ
  exact M

private def matrixProductMap (F : Type*) [Field F]
    (A B C D E G : Type) [Fintype A] [Fintype B] [Fintype C]
    [Fintype D] [Fintype E] [Fintype G] :
    LocalMap (Tensor.product (Tensor.matrixCoefficients (K := F) A B C)
      (Tensor.matrixCoefficients D E G))
      (Tensor.matrixCoefficients (A × D) (B × E) (C × G)) := by
  apply LocalMap.ofExists
  refine ⟨_, _, _, (Tensor.pullback_eq_restrict
    (fun x : (A × D) × (B × E) => ((x.1.1, x.2.1), (x.1.2, x.2.2)))
    (fun y : (B × E) × (C × G) => ((y.1.1, y.2.1), (y.1.2, y.2.2)))
    (fun z : (C × G) × (A × D) => ((z.1.1, z.2.1), (z.1.2, z.2.2))) _).symm.trans ?_⟩
  funext x y z
  exact Tensor.matrixCoefficients_product x y z

private def combineMatrixMaps {F : Type*} [Field F]
    {X Y Z U V W A B C D E G : Type}
    [Fintype X] [Fintype Y] [Fintype Z] [Fintype U] [Fintype V] [Fintype W]
    [Fintype A] [Fintype B] [Fintype C] [Fintype D] [Fintype E] [Fintype G]
    {T : Tensor F X Y Z} {Q : Tensor F U V W}
    (first : LocalMap T (Tensor.matrixCoefficients A B C))
    (second : LocalMap Q (Tensor.matrixCoefficients D E G)) :
    LocalMap (Tensor.product T Q)
      (Tensor.matrixCoefficients (A × D) (B × E) (C × G)) :=
  (first.product second).comp (matrixProductMap F A B C D E G)

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

abbrev ZeroRows := ∀ h : TerminalZero K, AllFieldZeroFinish.Row allocation m h
abbrev ZeroMiddles := ∀ h : TerminalZero K, AllFieldZeroFinish.Middle allocation m h
abbrev ZeroColumns := ∀ h : TerminalZero K, AllFieldZeroFinish.Column allocation m h

abbrev Rows := AllFieldInitialZeroFinish.Rows (K := K) allocation (terminalDilation K m) ×
  (ZeroRows (K := K) allocation m ×
    AllFieldStageCFinish.Rows (K := K) allocation (terminalDilation K m))
abbrev Middles := AllFieldInitialZeroFinish.Middles (K := K) allocation (terminalDilation K m) ×
  (ZeroMiddles (K := K) allocation m ×
    AllFieldStageCFinish.Middles (K := K) allocation (terminalDilation K m))
abbrev Columns := AllFieldInitialZeroFinish.Columns (K := K) allocation (terminalDilation K m) ×
  (ZeroColumns (K := K) allocation m ×
    AllFieldStageCFinish.Columns (K := K) allocation (terminalDilation K m))

local instance initialRowsFintype :
    Fintype (AllFieldInitialZeroFinish.Rows (K := K) allocation (terminalDilation K m)) := by
  letI : DecidableEq (AllFieldInitialZeroFinish.Index K) := Classical.decEq _
  exact inferInstanceAs (Fintype (∀ h : AllFieldInitialZeroFinish.Index K,
    AllFieldInitialZeroFinish.RowIndex allocation (terminalDilation K m) h))

local instance initialMiddlesFintype :
    Fintype (AllFieldInitialZeroFinish.Middles (K := K) allocation (terminalDilation K m)) := by
  letI : DecidableEq (AllFieldInitialZeroFinish.Index K) := Classical.decEq _
  exact inferInstanceAs (Fintype (∀ h : AllFieldInitialZeroFinish.Index K,
    AllFieldInitialZeroFinish.MiddleIndex allocation (terminalDilation K m) h))

local instance initialColumnsFintype :
    Fintype (AllFieldInitialZeroFinish.Columns (K := K) allocation (terminalDilation K m)) := by
  letI : DecidableEq (AllFieldInitialZeroFinish.Index K) := Classical.decEq _
  exact inferInstanceAs (Fintype (∀ h : AllFieldInitialZeroFinish.Index K,
    AllFieldInitialZeroFinish.ColumnIndex allocation (terminalDilation K m) h))

local instance stageCRowsFintype :
    Fintype (AllFieldStageCFinish.Rows (K := K) allocation (terminalDilation K m)) := by
  exact inferInstanceAs (Fintype (∀ h : CWStageCProducts.PopulationHistory K,
    CWStageCProducts.RowWords (CWStageCProducts.populationSide h)
      (CWStageCProducts.populationAtomCounts allocation (terminalDilation K m) h)))

local instance stageCMiddlesFintype :
    Fintype (AllFieldStageCFinish.Middles (K := K) allocation (terminalDilation K m)) := by
  exact inferInstanceAs (Fintype (∀ h : CWStageCProducts.PopulationHistory K,
    CWStageCProducts.MiddleWords (CWStageCProducts.populationSide h)
      (CWStageCProducts.populationAtomCounts allocation (terminalDilation K m) h)))

local instance stageCColumnsFintype :
    Fintype (AllFieldStageCFinish.Columns (K := K) allocation (terminalDilation K m)) := by
  exact inferInstanceAs (Fintype (∀ h : CWStageCProducts.PopulationHistory K,
    CWStageCProducts.ColumnWords (CWStageCProducts.populationSide h)
      (CWStageCProducts.populationAtomCounts allocation (terminalDilation K m) h)))

local instance rowsFintype : Fintype (Rows (K := K) allocation m) :=
  inferInstanceAs (Fintype
    (AllFieldInitialZeroFinish.Rows (K := K) allocation (terminalDilation K m) ×
      (ZeroRows (K := K) allocation m ×
        AllFieldStageCFinish.Rows (K := K) allocation (terminalDilation K m))))

local instance middlesFintype : Fintype (Middles (K := K) allocation m) :=
  inferInstanceAs (Fintype
    (AllFieldInitialZeroFinish.Middles (K := K) allocation (terminalDilation K m) ×
      (ZeroMiddles (K := K) allocation m ×
        AllFieldStageCFinish.Middles (K := K) allocation (terminalDilation K m))))

local instance columnsFintype : Fintype (Columns (K := K) allocation m) :=
  inferInstanceAs (Fintype
    (AllFieldInitialZeroFinish.Columns (K := K) allocation (terminalDilation K m) ×
      (ZeroColumns (K := K) allocation m ×
        AllFieldStageCFinish.Columns (K := K) allocation (terminalDilation K m))))

local instance initialRawFintype :
    Fintype (AllFieldFinalState.InitialWords (K := K) allocation (terminalDilation K m)) :=
  inferInstanceAs (Fintype (∀ h : AllFieldFinalState.InitialIndex K,
    HistoryWord allocation (terminalDilation K m) (.initial h.1.val, h.2)))

local instance zeroRawFintype :
    Fintype (AllFieldFinalState.ZeroWords (K := K) allocation (terminalDilation K m)) :=
  inferInstanceAs (Fintype (∀ h : TerminalZero K,
    HistoryWord allocation (terminalDilation K m) (AllFieldTerminalStatisticLaws.history h)))

local instance stageCRawFintype :
    Fintype (AllFieldFinalState.CWords (K := K) allocation (terminalDilation K m)) :=
  inferInstanceAs (Fintype (∀ h : AllFieldFinalState.CIndex K,
    HistoryWord allocation (terminalDilation K m) (.afterC h.1, h.2)))

theorem zero_volume :
    Fintype.card (ZeroRows (K := K) allocation m) *
      Fintype.card (ZeroMiddles (K := K) allocation m) *
        Fintype.card (ZeroColumns (K := K) allocation m) = zeroVolume (K := K) allocation m := by
  simp only [ZeroRows, ZeroMiddles, ZeroColumns, Fintype.card_pi, zeroVolume]
  rw [← Finset.prod_mul_distrib, ← Finset.prod_mul_distrib]
  exact Finset.prod_congr rfl fun h _ => AllFieldZeroFinish.volume allocation m h

theorem volume_eq :
    Fintype.card (Rows (K := K) allocation m) *
      Fintype.card (Middles (K := K) allocation m) *
        Fintype.card (Columns (K := K) allocation m) = terminalVolume (K := K) allocation m := by
  simp only [Rows, Middles, Columns, Fintype.card_prod]
  calc
    _ = (Fintype.card (AllFieldInitialZeroFinish.Rows (K := K) allocation (terminalDilation K m)) *
          Fintype.card (AllFieldInitialZeroFinish.Middles (K := K) allocation (terminalDilation K m)) *
          Fintype.card (AllFieldInitialZeroFinish.Columns (K := K) allocation (terminalDilation K m))) *
        (Fintype.card (ZeroRows (K := K) allocation m) *
          Fintype.card (ZeroMiddles (K := K) allocation m) *
          Fintype.card (ZeroColumns (K := K) allocation m)) *
        (Fintype.card (AllFieldStageCFinish.Rows (K := K) allocation (terminalDilation K m)) *
          Fintype.card (AllFieldStageCFinish.Middles (K := K) allocation (terminalDilation K m)) *
          Fintype.card (AllFieldStageCFinish.Columns (K := K) allocation (terminalDilation K m))) := by ring
    _ = _ := by rw [AllFieldInitialZeroFinish.volume_eq, zero_volume,
      AllFieldStageCFinish.volume_eq_populationVolume]; rfl

theorem dimensions_pos :
    0 < Fintype.card (Rows (K := K) allocation m) ∧
      0 < Fintype.card (Middles (K := K) allocation m) ∧
        0 < Fintype.card (Columns (K := K) allocation m) := by
  have hp := terminalVolume_pos (K := K) allocation m
  rw [← volume_eq allocation m] at hp
  have hab := Nat.pos_of_mul_pos_right hp
  exact ⟨Nat.pos_of_mul_pos_right hab, Nat.pos_of_mul_pos_left hab,
    Nat.pos_of_mul_pos_left hp⟩

def zeroMap (F : Type*) [Field F] {ε : ℝ} (hm : 0 < m) (hε : 0 ≤ ε) :
    LocalMap (AllFieldFinalState.zeroTensor (K := K) allocation (terminalDilation K m) F ε)
      (Tensor.matrixCoefficients (ZeroRows (K := K) allocation m)
        (ZeroMiddles (K := K) allocation m) (ZeroColumns (K := K) allocation m)) := by
  let result := AllFieldTerminal.terminalProductMap
    (fun h : TerminalZero K => historyTensor F allocation (terminalDilation K m) ε
      (AllFieldTerminalStatisticLaws.history h))
    (fun h => AllFieldZeroFinish.Row allocation m h)
    (fun h => AllFieldZeroFinish.Middle allocation m h)
    (fun h => AllFieldZeroFinish.Column allocation m h)
    (fun h => LocalMap.redecideMatrix
      (AllFieldZeroFinish.localMap F allocation hm hε h) _ _ _)
  let aligned := localMapWithFintypes (F := F)
    (X := AllFieldFinalState.ZeroWords (K := K) allocation (terminalDilation K m))
    (Y := AllFieldFinalState.ZeroWords (K := K) allocation (terminalDilation K m))
    (Z := AllFieldFinalState.ZeroWords (K := K) allocation (terminalDilation K m))
    (newX := inferInstance) (newY := inferInstance) (newZ := inferInstance) result
  exact LocalMap.redecideMatrix aligned _ _ _

def terminalMap (F : Type*) [Field F] {ε : ℝ} (hm : 0 < m) (hε : 0 ≤ ε) :
    LocalMap (stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2))
      (Tensor.matrixCoefficients (Rows (K := K) allocation m)
        (Middles (K := K) allocation m) (Columns (K := K) allocation m)) := by
  let initialAligned := localMapWithFintypes (F := F)
    (X := AllFieldInitialZeroFinish.RawWords (K := K) allocation (terminalDilation K m))
    (Y := AllFieldInitialZeroFinish.RawWords (K := K) allocation (terminalDilation K m))
    (Z := AllFieldInitialZeroFinish.RawWords (K := K) allocation (terminalDilation K m))
    (newX := inferInstance) (newY := inferInstance) (newZ := inferInstance)
    (AllFieldInitialZeroFinish.finishMap (K := K) allocation (terminalDilation K m) F ε)
  let initial := LocalMap.redecideMatrix initialAligned
    (Classical.decEq _) (Classical.decEq _) (Classical.decEq _)
  let zeros := LocalMap.redecideMatrix (zeroMap (K := K) allocation m F hm hε)
    (Classical.decEq _) (Classical.decEq _) (Classical.decEq _)
  let interior := LocalMap.redecideMatrix
    (AllFieldStageCFinish.finishMap (K := K) allocation (terminalDilation K m) F ε)
    (Classical.decEq _) (Classical.decEq _) (Classical.decEq _)
  let second := LocalMap.redecideMatrix
    (combineMatrixMaps zeros interior)
    (Classical.decEq _) (Classical.decEq _) (Classical.decEq _)
  let third := combineMatrixMaps initial second
  exact LocalMap.redecideMatrix
    ((AllFieldFinalState.map (K := K) allocation (terminalDilation K m) F ε).comp third) _ _ _

variable {F : Type*} [Field F] {ε : ℝ} {I : Type} [Fintype I]

def execution (hm : 0 < m) (hε : 0 ≤ ε)
    (E : Execution (cwSource F K (terminalLength allocation K m))
      (Tensor.directSum (fun _ : I =>
        stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2)))) :
    Execution (cwSource F K (terminalLength allocation K m))
      (AllFieldSource.matrixTarget F (Fintype.card (Rows (K := K) allocation m))
        (Fintype.card (Middles (K := K) allocation m))
        (Fintype.card (Columns (K := K) allocation m)) (Fintype.card I)) :=
  (E.restrict _ ((terminalMap allocation m F hm hε).parallelCopies I)).restrict _
    (LocalMap.ofExists (TerminalRelabel.directSum_matrix_restrict_fin (K := F)
      I (Rows (K := K) allocation m) (Middles (K := K) allocation m)
        (Columns (K := K) allocation m)))

def witness (hm : 0 < m) (hε : 0 ≤ ε)
    (E : Execution (cwSource F K (terminalLength allocation K m))
      (Tensor.directSum (fun _ : I =>
        stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2))))
    (hI : 0 < Fintype.card I) : AllFieldWitness F :=
  (execution allocation m hm hε E).toWitness
    (dimensions_pos allocation m).1 (dimensions_pos allocation m).2.1
    (dimensions_pos allocation m).2.2 hI

@[simp] theorem witness_volume (hm : 0 < m) (hε : 0 ≤ ε)
    (E : Execution (cwSource F K (terminalLength allocation K m))
      (Tensor.directSum (fun _ : I =>
        stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2))))
    (hI : 0 < Fintype.card I) :
    (witness allocation m hm hε E hI).volume = terminalVolume (K := K) allocation m :=
  volume_eq allocation m

@[simp] theorem witness_multiplicity (hm : 0 < m) (hε : 0 ≤ ε)
    (E : Execution (cwSource F K (terminalLength allocation K m))
      (Tensor.directSum (fun _ : I =>
        stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2))))
    (hI : 0 < Fintype.card I) :
    (witness allocation m hm hε E hI).multiplicity = Fintype.card I := rfl

@[simp] theorem witness_rankBound (hm : 0 < m) (hε : 0 ≤ ε)
    (E : Execution (cwSource F K (terminalLength allocation K m))
      (Tensor.directSum (fun _ : I =>
        stateTensor F (K := K) allocation (terminalDilation K m) ε (K + 2))))
    (hI : 0 < Fintype.card I) :
    (witness allocation m hm hε E hI).rankBound =
      AllFieldSource.rankBudget E.Copies K (terminalLength allocation K m) := rfl

end MatrixMultiplication.AllFieldFinishedWitness

end

end OAI