File size: 12,835 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexFiniteSeparation

namespace OAI

namespace MatrixAllFields

open scoped BigOperators Topology Polynomial

open scoped BigOperators

namespace MatrixMultiplication.Foundation
namespace Tensor

variable {K S : Type*} [CommSemiring K] [DecidableEq S]
variable {X Y Z : S → Type*}

def dependentDirectSum (T : ∀ s, Tensor K (X s) (Y s) (Z s)) :
    Tensor K (Σ s, X s) (Σ s, Y s) (Σ s, Z s) :=
  fun x y z =>
    if hxy : y.1 = x.1 then
      if hxz : z.1 = x.1 then T x.1 x.2 (hxy ▸ y.2) (hxz ▸ z.2)
      else 0
    else 0

@[simp] theorem dependentDirectSum_matching
    (T : ∀ s, Tensor K (X s) (Y s) (Z s))
    (s : S) (x : X s) (y : Y s) (z : Z s) :
    dependentDirectSum T ⟨s, x⟩ ⟨s, y⟩ ⟨s, z⟩ = T s x y z := by
  simp [dependentDirectSum]

theorem dependentDirectSum_zero_of_left_ne
    (T : ∀ s, Tensor K (X s) (Y s) (Z s))
    (x : Σ s, X s) (y : Σ s, Y s) (z : Σ s, Z s)
    (h : x.1 ≠ y.1) : dependentDirectSum T x y z = 0 := by
  simp [dependentDirectSum, Ne.symm h]

def directSumCoordinateEquiv {A : Type*} (e : ∀ s, X s ≃ A) :
    (Σ s, X s) ≃ S × A where
  toFun x := (x.1, e x.1 x.2)
  invFun x := ⟨x.1, (e x.1).symm x.2⟩
  left_inv := by
    rintro ⟨s, x⟩
    simp
  right_inv := by
    rintro ⟨s, x⟩
    simp

theorem dependentDirectSum_reindex {A B C : Type*}
    (T : ∀ s, Tensor K (X s) (Y s) (Z s))
    (ex : ∀ s, X s ≃ A) (ey : ∀ s, Y s ≃ B) (ez : ∀ s, Z s ≃ C) :
    pullback (directSumCoordinateEquiv ex).symm
      (directSumCoordinateEquiv ey).symm (directSumCoordinateEquiv ez).symm
      (dependentDirectSum T) =
      directSum (fun s => pullback (ex s).symm (ey s).symm (ez s).symm (T s)) := by
  funext x y z
  rcases x with ⟨s, x⟩
  rcases y with ⟨t, y⟩
  rcases z with ⟨v, z⟩
  by_cases ht : t = s
  · subst t
    by_cases hv : v = s
    · subst v
      simp [pullback, directSumCoordinateEquiv, dependentDirectSum, directSum]
    · simp [pullback, directSumCoordinateEquiv, dependentDirectSum, directSum,
        hv, Ne.symm hv]
  · simp [pullback, directSumCoordinateEquiv, dependentDirectSum, directSum,
      ht, Ne.symm ht]

end Tensor

variable {X Y Z S U : Type*} [DecidableEq S] [DecidableEq U]

def labelFiberTensor (lx : X → S) (ly : Y → S) (T : Tensor ℂ X Y Z) (s : S) :
    Tensor ℂ {x // lx x = s} {y // ly y = s} Z :=
  fun x y z => T x.1 y.1 z

def fiberCoordinateEquiv (lx : X → S) :
    (Σ s, {x // lx x = s} × U) ≃ X × U where
  toFun x := (x.2.1.1, x.2.2)
  invFun x := ⟨lx x.1, (⟨x.1, rfl⟩, x.2)⟩
  left_inv := by
    rintro ⟨s, ⟨⟨x, hx⟩, u⟩⟩
    cases hx
    rfl
  right_inv := by
    rintro ⟨x, u⟩
    rfl

def tagCoordinateEquiv : (Σ _ : S, Z × Unit) ≃ Z × S where
  toFun z := (z.2.1, z.1)
  invFun z := ⟨z.2, (z.1, ())⟩
  left_inv := by
    rintro ⟨s, z, u⟩
    cases u
    rfl
  right_inv := by
    rintro ⟨z, s⟩
    rfl

theorem separatedTensor_eq_fiber_directSum
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y) :
    Tensor.pullback (fiberCoordinateEquiv (U := U) lx)
      (fiberCoordinateEquiv (U := U) ly) (tagCoordinateEquiv (Z := Z) (S := S))
      (separatedTensor lx T) =
      Tensor.dependentDirectSum (fun s =>
        Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U)) := by
  classical
  funext x y z
  rcases x with ⟨s, x, u⟩
  rcases y with ⟨t, y, v⟩
  rcases z with ⟨w, z, q⟩
  by_cases ht : t = s
  · subst t
    by_cases hw : w = s
    · subst w
      rw [Tensor.dependentDirectSum_matching]
      simp [Tensor.pullback, fiberCoordinateEquiv, tagCoordinateEquiv,
        separatedTensor, Tensor.product, labelFiberTensor, Tensor.dotPairing,
        x.property, mul_ite]
    · simp [Tensor.pullback, fiberCoordinateEquiv, tagCoordinateEquiv,
        separatedTensor, Tensor.dependentDirectSum, x.property, hw, Ne.symm hw]
  · have hzero : T x.1 y.1 z = 0 := by
      by_contra hzero
      have hxy := hlabels x.1 y.1 z hzero
      exact ht (y.property.symm.trans (hxy.symm.trans x.property))
    simp [Tensor.pullback, fiberCoordinateEquiv, tagCoordinateEquiv,
      separatedTensor, Tensor.dependentDirectSum, ht, hzero]

theorem fiber_directSum_eq_separatedTensor
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y) :
    Tensor.pullback (fiberCoordinateEquiv (U := U) lx).symm
      (fiberCoordinateEquiv (U := U) ly).symm
      (tagCoordinateEquiv (Z := Z) (S := S)).symm
      (Tensor.dependentDirectSum (fun s =>
        Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U))) =
      separatedTensor lx T := by
  rw [← separatedTensor_eq_fiber_directSum T lx ly hlabels]
  funext x y z
  simp [Tensor.pullback]

theorem separatedTensor_fiber_directSum_rankAtMost
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S) {r : ℕ}
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (h : Tensor.RankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.RankAtMost (Tensor.dependentDirectSum (fun s =>
      Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U))) r := by
  rw [← separatedTensor_eq_fiber_directSum T lx ly hlabels]
  exact h.pullback _ _ _

theorem separatedTensor_fiber_directSum_rankAtMost_iff
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S) {r : ℕ}
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y) :
    Tensor.RankAtMost (separatedTensor (U := U) lx T) r ↔
      Tensor.RankAtMost (Tensor.dependentDirectSum (fun s =>
        Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U))) r := by
  constructor
  · exact separatedTensor_fiber_directSum_rankAtMost T lx ly hlabels
  · intro h
    rw [← fiber_directSum_eq_separatedTensor T lx ly hlabels]
    exact h.pullback _ _ _

theorem separatedTensor_fiber_directSum_borderRankAtMost
    [Fintype X] [Fintype Y] [Fintype Z] [Fintype S] [Fintype U]
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S) {r : ℕ}
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (h : Tensor.BorderRankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.BorderRankAtMost (Tensor.dependentDirectSum (fun s =>
      Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U))) r := by
  classical
  rw [← separatedTensor_eq_fiber_directSum T lx ly hlabels]
  rw [Tensor.pullback_eq_restrict]
  exact h.restrict _ _ _

theorem separatedTensor_uniform_directSum_rankAtMost {A B : Type*}
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (ex : ∀ s, {x // lx x = s} ≃ A) (ey : ∀ s, {y // ly y = s} ≃ B)
    {r : ℕ} (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (h : Tensor.RankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.RankAtMost (Tensor.directSum (fun s => Tensor.pullback
      (Equiv.prodCongr (ex s) (Equiv.refl U)).symm
      (Equiv.prodCongr (ey s) (Equiv.refl U)).symm
      (Equiv.refl (Z × Unit)).symm
      (Tensor.product (labelFiberTensor lx ly T s) (Tensor.dotPairing U)))) r := by
  have hf := separatedTensor_fiber_directSum_rankAtMost T lx ly hlabels h
  have hp := hf.pullback
    (Tensor.directSumCoordinateEquiv
      (fun s => Equiv.prodCongr (ex s) (Equiv.refl U))).symm
    (Tensor.directSumCoordinateEquiv
      (fun s => Equiv.prodCongr (ey s) (Equiv.refl U))).symm
    (Tensor.directSumCoordinateEquiv (fun _ : S => Equiv.refl (Z × Unit))).symm
  rw [Tensor.dependentDirectSum_reindex] at hp
  exact hp

theorem separatedTensor_localModels_identity {A B C : Type*}
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (fx : S → A → X) (fy : S → B → Y) (fz : S → C → Z)
    (hfx : ∀ s a, lx (fx s a) = s) (hfy : ∀ s b, ly (fy s b) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y) :
    Tensor.pullback
      (fun x : S × (A × U) => (fx x.1 x.2.1, x.2.2))
      (fun y : S × (B × U) => (fy y.1 y.2.1, y.2.2))
      (fun z : S × (C × Unit) => (fz z.1 z.2.1, z.1))
      (separatedTensor lx T) =
      Tensor.directSum (fun s => Tensor.product
        (Tensor.pullback (fx s) (fy s) (fz s) T) (Tensor.dotPairing U)) := by
  funext x y z
  rcases x with ⟨s, a, u⟩
  rcases y with ⟨t, b, v⟩
  rcases z with ⟨w, c, q⟩
  by_cases ht : t = s
  · subst t
    by_cases hw : w = s
    · subst w
      simp [Tensor.pullback, separatedTensor, Tensor.directSum,
        Tensor.product, Tensor.dotPairing, hfx, mul_ite]
    · simp [Tensor.pullback, separatedTensor, Tensor.directSum,
        hfx, Ne.symm hw]
  · have hzero : T (fx s a) (fy t b) (fz w c) = 0 := by
      by_contra hzero
      have hxy := hlabels (fx s a) (fy t b) (fz w c) hzero
      exact ht ((hfy t b).symm.trans (hxy.symm.trans (hfx s a)))
    simp [Tensor.pullback, separatedTensor, Tensor.directSum,
      Ne.symm ht, hzero]

theorem separatedTensor_localModels_rankAtMost {A B C : Type*}
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (fx : S → A → X) (fy : S → B → Y) (fz : S → C → Z)
    (hfx : ∀ s a, lx (fx s a) = s) (hfy : ∀ s b, ly (fy s b) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    {r : ℕ} (h : Tensor.RankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.RankAtMost (Tensor.directSum (fun s => Tensor.product
      (Tensor.pullback (fx s) (fy s) (fz s) T) (Tensor.dotPairing U))) r := by
  rw [← separatedTensor_localModels_identity T lx ly fx fy fz hfx hfy hlabels]
  exact h.pullback _ _ _

theorem separatedTensor_localModels_borderRankAtMost {A B C : Type*}
    [Fintype X] [Fintype Y] [Fintype Z] [Fintype S] [Fintype U]
    [Fintype A] [Fintype B] [Fintype C]
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (fx : S → A → X) (fy : S → B → Y) (fz : S → C → Z)
    (hfx : ∀ s a, lx (fx s a) = s) (hfy : ∀ s b, ly (fy s b) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    {r : ℕ} (h : Tensor.BorderRankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.BorderRankAtMost (Tensor.directSum (fun s => Tensor.product
      (Tensor.pullback (fx s) (fy s) (fz s) T) (Tensor.dotPairing U))) r := by
  classical
  rw [← separatedTensor_localModels_identity T lx ly fx fy fz hfx hfy hlabels]
  rw [Tensor.pullback_eq_restrict]
  exact h.restrict _ _ _

theorem separatedTensor_scalar_identity
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (sx : S → X) (sy : S → Y) (sz : S → Z)
    (hsx : ∀ s, lx (sx s) = s) (hsy : ∀ s, ly (sy s) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (hscalar : ∀ s, T (sx s) (sy s) (sz s) = 1) :
    Tensor.pullback
      (fun x : S × U => (sx x.1, x.2))
      (fun y : S × U => (sy y.1, y.2))
      (fun z : S × Unit => (sz z.1, z.1)) (separatedTensor lx T) =
      Tensor.directSum (fun _ : S => Tensor.dotPairing U) := by
  have hmodels := separatedTensor_localModels_identity (U := U) T lx ly
    (fun s (_ : Unit) => sx s) (fun s (_ : Unit) => sy s) (fun s (_ : Unit) => sz s)
    (fun s _ => hsx s) (fun s _ => hsy s) hlabels
  have h := congrArg (Tensor.pullback
    (fun x : S × U => (x.1, ((), x.2)))
    (fun y : S × U => (y.1, ((), y.2)))
    (fun z : S × Unit => (z.1, ((), z.2)))) hmodels
  funext x y z
  have hpoint := congrFun (congrFun (congrFun h x) y) z
  simpa only [Tensor.pullback, Tensor.directSum, Tensor.product, hscalar, one_mul]
    using hpoint

theorem separatedTensor_scalar_rankAtMost
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (sx : S → X) (sy : S → Y) (sz : S → Z)
    (hsx : ∀ s, lx (sx s) = s) (hsy : ∀ s, ly (sy s) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (hscalar : ∀ s, T (sx s) (sy s) (sz s) = 1)
    {r : ℕ} (h : Tensor.RankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.RankAtMost (Tensor.directSum (fun _ : S => Tensor.dotPairing (K := ℂ) U)) r := by
  rw [← separatedTensor_scalar_identity T lx ly sx sy sz hsx hsy hlabels hscalar]
  exact h.pullback _ _ _

theorem separatedTensor_scalar_borderRankAtMost
    [Fintype X] [Fintype Y] [Fintype Z] [Fintype S] [Fintype U]
    (T : Tensor ℂ X Y Z) (lx : X → S) (ly : Y → S)
    (sx : S → X) (sy : S → Y) (sz : S → Z)
    (hsx : ∀ s, lx (sx s) = s) (hsy : ∀ s, ly (sy s) = s)
    (hlabels : ∀ x y z, T x y z ≠ 0 → lx x = ly y)
    (hscalar : ∀ s, T (sx s) (sy s) (sz s) = 1)
    {r : ℕ} (h : Tensor.BorderRankAtMost (separatedTensor (U := U) lx T) r) :
    Tensor.BorderRankAtMost (Tensor.directSum (fun _ : S => Tensor.dotPairing U)) r := by
  classical
  rw [← separatedTensor_scalar_identity T lx ly sx sy sz hsx hsy hlabels hscalar]
  rw [Tensor.pullback_eq_restrict]
  exact h.restrict _ _ _

end MatrixMultiplication.Foundation

end MatrixAllFields

end OAI