File size: 15,641 Bytes
caec230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import OAI.MathematicalPhysics.NavierStokes.VelocityDetection.HistoryRouting
import OAI.MathematicalPhysics.NavierStokes.VelocityDetection.Periodization

namespace OAI

noncomputable section
namespace VelocityDetection.ChartRouting
open Set Function Filter MeasureTheory
open scoped Topology ContDiff BigOperators
open Expanding CenterPaths SpatialCalculus
variable (A : RoutingData)

def scale (N : ℕ) : ℝ := (1024 * (count A.K A.D (N+1) + 4))⁻¹

theorem scale_pos (N : ℕ) : 0 < scale A N := by
  have hd : 0 ≤ count A.K A.D (N+1) := by
    unfold count
    exact mul_nonneg A.K_nonneg (pow_nonneg (by linarith [A.D_ge_one]) _)
  unfold scale
  positivity

theorem scale_small (N : ℕ) : scale A N ≤ (1/4096 : ℝ) := by
  have hd : 0 ≤ count A.K A.D (N+1) := by
    unfold count
    exact mul_nonneg A.K_nonneg (pow_nonneg (by linarith [A.D_ge_one]) _)
  change (1024 * (count A.K A.D (N+1) + 4))⁻¹ ≤ _
  have hp : 0 < 1024 * (count A.K A.D (N+1) + 4) := by positivity
  rw [inv_eq_one_div]
  apply (div_le_iff₀ hp).mpr
  nlinarith

theorem scaled_address (N k : ℕ) (hk : (k:ℝ) ≤ count A.K A.D (N+1)) :
    scale A N * ((k:ℝ)+2) ≤ (1/1024 : ℝ) := by
  have hh : scale A N * (count A.K A.D (N+1)+4) = (1/1024:ℝ) := by
    unfold scale
    have hp : 0 < count A.K A.D (N+1)+4 := by
      have hd : 0 ≤ count A.K A.D (N+1) := by
        unfold count
        exact mul_nonneg A.K_nonneg (pow_nonneg (by linarith [A.D_ge_one]) _)
      linarith
    field_simp
  rw [← hh]
  exact mul_le_mul_of_nonneg_left (by linarith) (scale_pos A N).le

def origin : Coord 2 := ![1/4,1/2]

def targetHeight (N n : ℕ) (c : A.Instruction n) : ℝ :=
  if A.sign n c = 1 then -(7/16:ℝ) else -scale A N

def path (N n : ℕ) (c : A.Instruction n) (s : ℝ) : Coord 2 :=
  origin + center (n:ℝ) 1 (scale A N) (scale A N)
    (targetHeight A N n c / scale A N) (A.source n c) (A.target n c) s

@[fun_prop] theorem contDiff_path (N n : ℕ) (c : A.Instruction n) :
    ContDiff ℝ ∞ (path A N n c) := by unfold path; fun_prop

theorem path_source (N n : ℕ) (c : A.Instruction n) {s : ℝ} (hs : s ≤ n) :
    path A N n c s = origin + ![scale A N * A.source n c,-scale A N] := by
  unfold path
  rw [center_source_collar]
  norm_num only [div_one]
  linarith

theorem path_target (N n : ℕ) (c : A.Instruction n) {s : ℝ} (hs : n+1 ≤ s) :
    path A N n c s = origin + ![scale A N * A.target n c,targetHeight A N n c] := by
  unfold path
  rw [center_target_collar]
  · rw [div_mul_cancel₀ _ (ne_of_gt (scale_pos A N))]
  · norm_num only [div_one]
    linarith

theorem velocity_source (N n : ℕ) (c : A.Instruction n) {s : ℝ} (hs : s ≤ n) :
    deriv (path A N n c) s = 0 := by
  unfold path
  rw [deriv_const_add]
  · exact velocity_source_collar _ _ (by norm_num only [div_one]; linarith)

theorem velocity_target (N n : ℕ) (c : A.Instruction n) {s : ℝ} (hs : n+1 ≤ s) :
    deriv (path A N n c) s = 0 := by
  unfold path
  rw [deriv_const_add]
  · exact velocity_target_collar _ _ (by norm_num only [div_one]; linarith)

theorem theta_order (a T s : ℝ) : theta a T 2 s ≤ theta a T 0 s := by
  unfold theta ramp
  exact Real.smoothTransition.monotone (by norm_num only [Nat.cast_ofNat, Nat.cast_zero]; linarith)

theorem center_in_rectangle {S σ lo hi xmax : ℝ} {k l : ℕ}
    (hS0 : 0 ≤ S) (hk : S * k ≤ xmax) (hl : S * l ≤ xmax)
    (hrow : privateRow S k ∈ Icc lo hi) (hsource : -S ∈ Icc lo hi)
    (htarget : σ*S ∈ Icc lo hi) (a T s : ℝ) :
    (center a T S S σ k l s 0 ∈ Icc (0:ℝ) xmax) ∧
    (center a T S S σ k l s 1 ∈ Icc lo hi) := by
  have ht (j : ℕ) : theta a T j s ∈ Icc (0:ℝ) 1 :=
    ⟨ramp_nonneg _, ramp_le_one _⟩
  have hm := theta_order a T s
  constructor
  · change 0 ≤ (1-theta a T 1 s)*S*k+theta a T 1 s*S*l ∧
      (1-theta a T 1 s)*S*k+theta a T 1 s*S*l ≤ xmax
    have hp : 0 ≤ 1 - theta a T 1 s := by linarith [(ht 1).2]
    constructor
    · exact add_nonneg (mul_nonneg (mul_nonneg hp hS0) (Nat.cast_nonneg _))
        (mul_nonneg (mul_nonneg (ht 1).1 hS0) (Nat.cast_nonneg _))
    · have h₁ := mul_le_mul_of_nonneg_left hk hp
      have h₂ := mul_le_mul_of_nonneg_left hl (ht 1).1
      nlinarith
  · change lo ≤ -(1-theta a T 0 s)*S+
        (theta a T 0 s-theta a T 2 s)*privateRow S k+theta a T 2 s*σ*S ∧
      -(1-theta a T 0 s)*S+
        (theta a T 0 s-theta a T 2 s)*privateRow S k+theta a T 2 s*σ*S ≤ hi
    have hp : 0 ≤ 1-theta a T 0 s := by linarith [(ht 0).2]
    have hq : 0 ≤ theta a T 0 s-theta a T 2 s := sub_nonneg.mpr hm
    have h₁ := mul_le_mul_of_nonneg_left hsource.1 hp
    have h₂ := mul_le_mul_of_nonneg_left hrow.1 hq
    have h₃ := mul_le_mul_of_nonneg_left htarget.1 (ht 2).1
    have h₄ := mul_le_mul_of_nonneg_left hsource.2 hp
    have h₅ := mul_le_mul_of_nonneg_left hrow.2 hq
    have h₆ := mul_le_mul_of_nonneg_left htarget.2 (ht 2).1
    constructor <;> nlinarith

theorem path_bounds (N n : ℕ) (c : A.Instruction n) (hn : n < N) (s : ℝ) :
    path A N n c s 0 ∈ Icc (1/4:ℝ) (1/4+1/1024) ∧
    path A N n c s 1 ∈ Icc (1/16:ℝ) (1/2) := by
  have hk := scaled_address A N (A.source n c)
    ((A.source_count n c).trans (count_mono A.K_nonneg A.D_ge_one (by omega)))
  have hl := scaled_address A N (A.target n c)
    ((A.target_count n c).trans (count_mono A.K_nonneg A.D_ge_one (by omega)))
  have hS := scale_pos A N
  have hsmall := scale_small A N
  have hheight : targetHeight A N n c ∈ Icc (-(7/16):ℝ) 0 := by
    unfold targetHeight
    split_ifs <;> constructor <;> linarith
  have hh := center_in_rectangle hS.le
    (show scale A N * (A.source n c:ℝ) ≤ (1/1024:ℝ) by nlinarith)
    (show scale A N * (A.target n c:ℝ) ≤ (1/1024:ℝ) by nlinarith)
    (show privateRow (scale A N) (A.source n c) ∈ Icc (-(7/16):ℝ) 0 by
      dsimp [privateRow]; constructor <;> nlinarith [Nat.cast_nonneg (α := ℝ) (A.source n c)])
    (show -scale A N ∈ Icc (-(7/16):ℝ) 0 by constructor <;> linarith)
    (show targetHeight A N n c / scale A N * scale A N ∈ Icc (-(7/16):ℝ) 0 by
      rwa [div_mul_cancel₀ _ (ne_of_gt hS)]) (n:ℝ) 1 s
  change 1/4+center _ _ _ _ _ _ _ s 0 ∈ Icc _ _ ∧
    1/2+center _ _ _ _ _ _ _ s 1 ∈ Icc _ _
  exact ⟨⟨by linarith [hh.1.1],by linarith [hh.1.2]⟩,
    ⟨by linarith [hh.2.1],by linarith [hh.2.2]⟩⟩

theorem nonterminal_bounds (N n : ℕ) (c : A.Instruction n) (hn : n < N)
    (hc : A.sign n c = -1) (s : ℝ) :
    (1/2-1/1024:ℝ) ≤ path A N n c s 1 := by
  have hk := scaled_address A N (A.source n c)
    ((A.source_count n c).trans (count_mono A.K_nonneg A.D_ge_one (by omega)))
  have hl := scaled_address A N (A.target n c)
    ((A.target_count n c).trans (count_mono A.K_nonneg A.D_ge_one (by omega)))
  have hS := scale_pos A N
  have hsmall := scale_small A N
  have he : targetHeight A N n c = -scale A N := by norm_num [targetHeight,hc]
  have hh := center_in_rectangle hS.le
    (show scale A N * (A.source n c:ℝ) ≤ (1/1024:ℝ) by nlinarith)
    (show scale A N * (A.target n c:ℝ) ≤ (1/1024:ℝ) by nlinarith)
    (show privateRow (scale A N) (A.source n c) ∈ Icc (-(1/1024):ℝ) 0 by
      dsimp [privateRow]; constructor <;> nlinarith [Nat.cast_nonneg (α := ℝ) (A.source n c)])
    (show -scale A N ∈ Icc (-(1/1024):ℝ) 0 by constructor <;> linarith)
    (show targetHeight A N n c / scale A N * scale A N ∈ Icc (-(1/1024):ℝ) 0 by
      rw [div_mul_cancel₀ _ (ne_of_gt hS),he]; constructor <;> linarith) (n:ℝ) 1 s
  change _ ≤ 1/2+center _ _ _ _ _ _ _ s 1
  linarith [hh.2.1]

def radius (N : ℕ) : ℝ := scale A N / 16

theorem radius_pos (N : ℕ) : 0 < radius A N := div_pos (scale_pos A N) (by norm_num)

def stage (N n : ℕ) : VectorField 2 := fun s X =>
  ∑ c : A.Instruction n, TranslationGates.field (radius A N)
    (path A N n c s) (deriv (path A N n c) s) X

@[fun_prop] theorem contDiff_stage (N n : ℕ) : ContDiff ℝ ∞ (uncurry (stage A N n)) := by
  unfold stage
  apply ContDiff.sum
  intro c _
  exact TranslationGates.contDiff_movingField _ (contDiff_path A N n c)
    ((contDiff_infty_iff_deriv.mp (contDiff_path A N n c)).2)

theorem stage_zero_before (N n : ℕ) {s : ℝ} (hs : s ≤ n) (X : Coord 2) :
    stage A N n s X = 0 := by
  unfold stage
  apply Finset.sum_eq_zero
  intro c _
  rw [velocity_source A N n c hs,TranslationGates.field_zero_velocity]

theorem stage_zero_after (N n : ℕ) {s : ℝ} (hs : n+1 ≤ s) (X : Coord 2) :
    stage A N n s X = 0 := by
  unfold stage
  apply Finset.sum_eq_zero
  intro c _
  rw [velocity_target A N n c hs,TranslationGates.field_zero_velocity]

theorem stage_divergence (N n : ℕ) (s : ℝ) (X : Coord 2) :
    divergence (stage A N n) s X = 0 := by
  unfold stage
  rw [divergence_sum]
  · apply Finset.sum_eq_zero
    intro c _
    exact TranslationGates.divergence_field (radius A N) (path A N n c s)
      (deriv (path A N n c) s) X
  · intro c _ s
    exact TranslationGates.contDiff_field (radius A N) (path A N n c s)
      (deriv (path A N n c) s)

theorem paths_separated (N n : ℕ) (c d : A.Instruction n) (hcd : c ≠ d) (s : ℝ) :
    ∃ i : Fin 2, 16*radius A N ≤ |path A N n c s i - path A N n d s i| := by
  obtain ⟨i,hi⟩ := CenterPaths.separated (scale_pos A N).le le_rfl (n:ℝ) 1
    (targetHeight A N n c / scale A N) (targetHeight A N n d / scale A N) s
    ((A.source_injective n).ne hcd) ((A.target_injective n).ne hcd)
  refine ⟨i,?_⟩
  simpa only [path,Pi.add_apply,add_sub_add_left_eq_sub,radius,mul_div_cancel₀ _ (by norm_num : (16:ℝ) ≠ 0)] using hi

theorem stage_plateau (N n : ℕ) (c : A.Instruction n) (s : ℝ) (X : Coord 2)
    (hX : ∀ i, |X i - path A N n c s i| ≤ radius A N) :
    stage A N n s X = deriv (path A N n c) s := by
  exact TranslationGates.array_plateau (radius_pos A N)
    (fun c => path A N n c s) (fun c => deriv (path A N n c) s)
    (fun c _ d _ h => paths_separated A N n c d h s) (Finset.mem_univ c) X hX

end VelocityDetection.ChartRouting
end

noncomputable section
namespace VelocityDetection.ChartRouting
open Set Function Filter MeasureTheory
open scoped Topology ContDiff BigOperators
open Expanding CenterPaths SpatialCalculus Periodization
variable (A : RoutingData)

def chartSet : Set (Coord 2) := Icc ![1/8,1/32] ![3/8,3/4]

theorem compact_chartSet : IsCompact chartSet := isCompact_Icc

theorem chartSet_interior {X : Coord 2} (hX : X ∈ chartSet) :
    ∀ i, X i ∈ Ioo (0:ℝ) 1 := by
  intro i
  fin_cases i
  · have h₁ := hX.1 (0 : Fin 2)
    have h₂ := hX.2 (0 : Fin 2)
    norm_num only [Matrix.cons_val_zero] at h₁ h₂
    change 0 < X 0 ∧ X 0 < 1
    constructor <;> linarith
  · have h₁ := hX.1 (1 : Fin 2)
    have h₂ := hX.2 (1 : Fin 2)
    norm_num only [Matrix.cons_val_one, Matrix.cons_val_zero] at h₁ h₂
    change 0 < X 1 ∧ X 1 < 1
    constructor <;> linarith

theorem path_neighborhood (N n : ℕ) (c : A.Instruction n) (hn : n < N)
    (s : ℝ) (X : Coord 2) (hX : ∀ i, |X i-path A N n c s i| ≤ 3*radius A N) :
    X ∈ chartSet := by
  have hb := path_bounds A N n c hn s
  have hR : 3*radius A N ≤ (3/65536:ℝ) := by
    unfold radius
    linarith [scale_small A N]
  have hx := abs_le.mp (hX 0)
  have hy := abs_le.mp (hX 1)
  constructor <;> intro i <;> fin_cases i
  · change (1/8:ℝ) ≤ X 0
    linarith [hb.1.1]
  · change (1/32:ℝ) ≤ X 1
    linarith [hb.2.1]
  · change X 0 ≤ (3/8:ℝ)
    linarith [hb.1.2]
  · change X 1 ≤ (3/4:ℝ)
    linarith [hb.2.2]

theorem stage_support (N n : ℕ) (hn : n < N) (s : ℝ) (X : Coord 2)
    (hX : stage A N n s X ≠ 0) : X ∈ chartSet := by
  unfold stage at hX
  obtain ⟨c,_,hc⟩ := Finset.exists_ne_zero_of_sum_ne_zero hX
  apply path_neighborhood A N n c hn s X
  intro i
  apply le_of_lt
  by_contra hi
  exact hc (TranslationGates.field_zero_outside (radius_pos A N) _ _ _ ⟨i,not_lt.mp hi⟩)

def finiteField (N : ℕ) : VectorField 2 := fun s X => ∑ n ∈ Finset.range N, stage A N n s X

@[fun_prop] theorem contDiff_prefix (N : ℕ) : ContDiff ℝ ∞ (uncurry (finiteField A N)) := by
  unfold finiteField
  exact ContDiff.sum (fun n _ => contDiff_stage A N n)

theorem prefix_divergence (N : ℕ) (s : ℝ) (X : Coord 2) :
    divergence (finiteField A N) s X = 0 := by
  unfold finiteField
  rw [divergence_sum]
  · apply Finset.sum_eq_zero
    intro n _
    exact stage_divergence A N n s X
  · intro n _ t
    exact contDiff_slice (contDiff_stage A N n) t

theorem prefix_support (N : ℕ) (s : ℝ) (X : Coord 2) (hX : finiteField A N s X ≠ 0) :
    X ∈ chartSet := by
  unfold finiteField at hX
  obtain ⟨n,hn,h⟩ := Finset.exists_ne_zero_of_sum_ne_zero hX
  exact stage_support A N n (Finset.mem_range.mp hn) s X h

theorem prefix_zero_before (N : ℕ) {s : ℝ} (hs : s ≤ 0) (X : Coord 2) :
    finiteField A N s X = 0 := by
  unfold finiteField
  apply Finset.sum_eq_zero
  intro n _
  exact stage_zero_before A N n (hs.trans (Nat.cast_nonneg _)) X

theorem prefix_zero_after (N : ℕ) {s : ℝ} (hs : N ≤ s) (X : Coord 2) :
    finiteField A N s X = 0 := by
  unfold finiteField
  apply Finset.sum_eq_zero
  intro n hn
  apply stage_zero_after A N n _ X
  have h : (n:ℝ)+1 ≤ N := by exact_mod_cast Finset.mem_range.mp hn
  exact h.trans hs

theorem prefix_eq_stage (N n : ℕ) (hn : n < N) {s : ℝ} (hs : s ∈ Icc (n:ℝ) (n+1))
    (X : Coord 2) : finiteField A N s X = stage A N n s X := by
  unfold finiteField
  apply Finset.sum_eq_single n
  · intro k _ hkn
    rcases lt_or_gt_of_ne hkn with h | h
    · apply stage_zero_after A N k _ X
      have hh : (k:ℝ)+1 ≤ n := by exact_mod_cast h
      exact hh.trans hs.1
    · apply stage_zero_before A N k _ X
      have hh : (n:ℝ)+1 ≤ k := by exact_mod_cast h
      exact hs.2.trans hh
  · exact fun h => (h (Finset.mem_range.mpr hn)).elim

theorem compactSupport_prefix (N : ℕ) : HasCompactSupport (uncurry (finiteField A N)) := by
  apply HasCompactSupport.of_support_subset_isCompact (isCompact_Icc.prod compact_chartSet)
  intro q hq
  refine ⟨⟨?_,?_⟩,prefix_support A N q.1 q.2 hq⟩
  · exact (lt_of_not_ge (fun h => hq (prefix_zero_before A N h q.2))).le
  · exact (lt_of_not_ge (fun h => hq (prefix_zero_after A N h q.2))).le

def periodicPrefix (N : ℕ) : VectorField 2 := Periodization.extend (finiteField A N)

@[fun_prop] theorem contDiff_periodicPrefix (N : ℕ) :
    ContDiff ℝ ∞ (uncurry (periodicPrefix A N)) := by
  apply contDiff_extend (C := 1) (contDiff_prefix A N)
  intro s X hX i
  have hi := chartSet_interior (prefix_support A N s X hX) i
  rw [abs_of_pos hi.1]
  exact hi.2.le

theorem periodic_periodicPrefix (N : ℕ) (s : ℝ) :
    FactorsThrough (periodicPrefix A N s) PeriodicSpace.cover := periodic_extend _ _

theorem periodicPrefix_chart (N : ℕ) (s : ℝ) {X : Coord 2}
    (hX : ∀ i, X i ∈ Icc (0:ℝ) 1) : periodicPrefix A N s X = finiteField A N s X := by
  apply extend_eq_chart _ s hX
  intro t Y hY
  exact chartSet_interior (prefix_support A N t Y hY)

theorem periodicPrefix_plateau (N n : ℕ) (c : A.Instruction n) (hn : n < N)
    {s : ℝ} (hs : s ∈ Icc (n:ℝ) (n+1)) (X : Coord 2)
    (hX : ∀ i, |X i - path A N n c s i| ≤ radius A N) :
    periodicPrefix A N s X = deriv (path A N n c) s := by
  have hK := path_neighborhood A N n c hn s X
    (fun i => (hX i).trans (by linarith [radius_pos A N]))
  rw [periodicPrefix_chart A N s (fun i => ⟨(chartSet_interior hK i).1.le,
    (chartSet_interior hK i).2.le⟩),prefix_eq_stage A N n hn hs X]
  exact stage_plateau A N n c s X hX

end VelocityDetection.ChartRouting
end

end OAI