File size: 30,546 Bytes
ddc84c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import OAI.Analysis.DiskMaximal.Runs

namespace OAI

open MeasureTheory Set
open scoped ENNReal NNReal Topology ContDiff
open scoped RealInnerProductSpace
open MeasureTheory Set Filter
open scoped Topology ENNReal
open scoped Real RealInnerProductSpace
open MeasureTheory Set Filter
open scoped Topology RealInnerProductSpace ContDiff
open MeasureTheory Set Filter
open scoped Topology Interval NNReal
open MeasureTheory Set Filter
open scoped Topology NNReal Real
open MeasureTheory Set Metric
open scoped ENNReal NNReal Topology
open MeasureTheory Set
open scoped ENNReal NNReal
open MeasureTheory Set
open scoped ENNReal NNReal
open MeasureTheory Set
open scoped ENNReal NNReal Topology
open MeasureTheory Set Metric
open scoped ENNReal NNReal
open MeasureTheory Set
open scoped ENNReal NNReal
open Finset
open scoped RealInnerProductSpace
open MeasureTheory Set Metric
open scoped ENNReal NNReal Topology
open MeasureTheory Set
open scoped ENNReal NNReal RealInnerProductSpace
open MeasureTheory Set
open scoped ENNReal NNReal Topology
open MeasureTheory Set Metric
open scoped ENNReal NNReal RealInnerProductSpace
open scoped RealInnerProductSpace
open scoped RealInnerProductSpace
open scoped ContDiff
open Finset
open MeasureTheory Set Metric Filter
open scoped ENNReal NNReal RealInnerProductSpace ContDiff Topology
open MeasureTheory MeasureTheory.Measure Set Filter
open scoped ENNReal NNReal Topology RealInnerProductSpace ContDiff
open MeasureTheory Set Metric Filter Finset
open scoped ENNReal NNReal RealInnerProductSpace ContDiff Topology
open MeasureTheory Set
open scoped RealInnerProductSpace

noncomputable section

namespace SignedRuns
open DiskMoments ActualLabels

def Regular (f : ℂ → ℂ) : Prop := HasCompactSupport f ∧ ∃ L : ℝ≥0, LipschitzWith L f

lemma regular_zero : Regular (fun _ : ℂ => (0:ℂ)) :=
  ⟨HasCompactSupport.zero,0,LipschitzWith.const 0⟩

lemma regular_add {f g : ℂ → ℂ} (hf : Regular f) (hg : Regular g) :
    Regular (fun x => f x+g x) := by
  obtain ⟨hfC,L,hfL⟩ := hf
  obtain ⟨hgC,K,hgL⟩ := hg
  exact ⟨hfC.add hgC,L+K,hfL.add hgL⟩

lemma regular_scalar {f : ℂ → ℝ} (hc : HasCompactSupport f) {L : ℝ≥0}
    (hf : LipschitzWith L f) (n : Label) : Regular (fun x => f x • (n:ℂ)) := by
  refine ⟨hc.smul_right,L,?_⟩
  rw [lipschitzWith_iff_norm_sub_le] at hf ⊢
  intro x y
  simpa only [← sub_smul,norm_smul,label_unit,mul_one] using hf x y

lemma regular_listSum {α : Type*} (l : List α) (f : α → ℂ → ℂ)
    (hf : ∀ a ∈ l, Regular (f a)) : Regular (fun x => (l.map fun a => f a x).sum) := by
  induction l with
  | nil => exact regular_zero
  | cons a l ih =>
    exact regular_add (hf a (by simp)) (ih (fun b hb => hf b (by simp [hb])))

lemma runTerm_regular {g : ℂ → ℝ} {r : ℕ → ℝ} (hg : ContDiff ℝ 1 g)
    (hgc : HasCompactSupport g) (hr : ∀ k, 0 < r k) (hstep : ∀ k, 2*r (k+1) = r k)
    (a : Run) (ha : a.first ≤ a.last) :
    Regular (fun x => (runEnvelope g r a.first a.last x-signedAverage g x (r a.first)) • (a.dir:ℂ)) := by
  have hab : r a.last ≤ 2*r a.first := by
    have := radius_antitone r hr hstep ha
    linarith [hr a.first]
  obtain ⟨L,hL⟩ := run_envelope_lipschitz hg hgc (hr a.last) hab
  obtain ⟨K,hK⟩ := hg.lipschitzWith_of_hasCompactSupport hgc one_ne_zero
  apply regular_scalar
    ((run_envelope_hasCompactSupport hg hgc (hr a.last) hab).sub (signedAverage_hasCompactSupport hg hgc (hr a.first)))
    (hL.sub (signedAverage_lipschitz hK (hr a.first)))

/-- Every exact history potential is compactly supported and Lipschitz, as
needed for signed weighted integration by parts. -/
theorem potential_regular {g : ℂ → ℝ} {r : ℕ → ℝ} (hg : ContDiff ℝ 1 g)
    (hgc : HasCompactSupport g) (hr : ∀ k, 0 < r k) (hstep : ∀ k, 2*r (k+1) = r k)
    (p : List Label) : Regular (fun x => potential g r x p) := by
  cases p with
  | nil => exact regular_zero
  | cons n p =>
    simp_rw [potential_eq_runs]
    exact regular_listSum (runs (n::p)) _ (fun a ha =>
      runTerm_regular hg hgc hr hstep a (runs_bounds n p a ha).1)
end SignedRuns

namespace SignedRuns
open Complex

def divergence (V : ℂ → ℂ) (x : ℂ) : ℝ :=
  fderiv ℝ (fun y => (V y).re) x 1+fderiv ℝ (fun y => (V y).im) x I

lemma inner_coordinates (z w : ℂ) :
    inner ℝ z w = inner ℝ z (1:ℂ)*w.re+inner ℝ z I*w.im := by
  conv_lhs => rw [← Complex.re_add_im w]
  rw [inner_add_right]
  have hre : (w.re:ℂ) = w.re • (1:ℂ) := by simp
  have him : (w.im:ℂ)*I = w.im • I := by rw [Complex.real_smul]
  rw [hre,him,real_inner_smul_right,real_inner_smul_right]
  ring

lemma gradient_aestronglyMeasurable (f : ℂ → ℝ) : AEStronglyMeasurable (gradient f) :=
  ((InnerProductSpace.toDual ℝ ℂ).symm.continuous.measurable.comp
    (measurable_fderiv ℝ f)).aestronglyMeasurable

lemma gradient_norm_le {f : ℂ → ℝ} {L : ℝ≥0} (hf : LipschitzWith L f) (x : ℂ) :
    ‖gradient f x‖ ≤ (L:ℝ) := by
  simpa only [gradient,(InnerProductSpace.toDual ℝ ℂ).symm.norm_map] using
    (norm_fderiv_le_of_lipschitz ℝ hf : ‖fderiv ℝ f x‖ ≤ (L:ℝ))

lemma integrable_fderiv_apply {f : ℂ → ℝ} {L : ℝ≥0} (hf : LipschitzWith L f)
    (hc : HasCompactSupport f) (v : ℂ) : Integrable (fun x => fderiv ℝ f x v) := by
  have hi := (innerSL ℝ v).integrable_comp (_root_.OAI.LipschitzWith.integrable_gradient (μ := volume) hf hc)
  apply hi.congr
  filter_upwards with x
  change inner ℝ v (gradient f x) = _
  rw [real_inner_comm,gradient,InnerProductSpace.toDual_symm_apply]

lemma integrable_gradient_mul {f g : ℂ → ℝ} {L : ℝ≥0} (hf : LipschitzWith L f)
    (hg : Integrable g) (v : ℂ) : Integrable (fun x => inner ℝ (gradient f x) v*g x) := by
  apply hg.bdd_mul ((gradient_aestronglyMeasurable f).inner aestronglyMeasurable_const)
  filter_upwards with x
  exact (norm_inner_le_norm _ _).trans (mul_le_mul_of_nonneg_right (gradient_norm_le hf x) (norm_nonneg v))

lemma regular_integrable (V : ℂ → ℂ) (hV : Regular V) : Integrable V := by
  obtain ⟨hc,L,hL⟩ := hV
  exact hL.continuous.integrable_of_hasCompactSupport hc

lemma weighted_divergence_integrable {w : ℂ → ℝ} {K : ℝ≥0} (hw : LipschitzWith K w)
    (hb : ∀ x, ‖w x‖ ≤ 1) {V : ℂ → ℂ} (hV : Regular V) :
    Integrable (fun x => w x*divergence V x) := by
  obtain ⟨hc,L,hL⟩ := hV
  have hr := integrable_fderiv_apply (Complex.reCLM.lipschitzWith.comp hL)
    (hc.comp_left (map_zero Complex.reCLM)) (1:ℂ)
  have hi := integrable_fderiv_apply (Complex.imCLM.lipschitzWith.comp hL)
    (hc.comp_left (map_zero Complex.imCLM)) I
  exact (hr.add hi).bdd_mul hw.continuous.aestronglyMeasurable (ae_of_all _ hb)

lemma potential_flux_integrable {w : ℂ → ℝ} {K : ℝ≥0} (hw : LipschitzWith K w)
    {V : ℂ → ℂ} (hV : Regular V) :
    Integrable (fun x => inner ℝ (gradient w x) (V x)) := by
  have hi := regular_integrable V hV
  apply (hi.norm.const_mul (K:ℝ)).mono'
    ((gradient_aestronglyMeasurable w).inner hi.aestronglyMeasurable)
  filter_upwards with x
  exact (norm_inner_le_norm _ _).trans (mul_le_mul_of_nonneg_right (gradient_norm_le hw x) (norm_nonneg _))

/-- Weighted vector integration by parts for the actual class of compact
Lipschitz path potentials and bounded Lipschitz spatial history weights. -/
theorem vector_weighted_ibp {w : ℂ → ℝ} {K : ℝ≥0} (hw : LipschitzWith K w)
    (hb : ∀ x, ‖w x‖ ≤ 1) {V : ℂ → ℂ} (hV : Regular V) :
    (∫ x, w x*divergence V x) = -∫ x, inner ℝ (gradient w x) (V x) := by
  obtain ⟨hc,L,hL⟩ := hV
  have hrL := Complex.reCLM.lipschitzWith.comp hL
  have hiL := Complex.imCLM.lipschitzWith.comp hL
  have hrC := hc.comp_left (map_zero Complex.reCLM)
  have hiC := hc.comp_left (map_zero Complex.imCLM)
  have hr := (integrable_fderiv_apply hrL hrC (1:ℂ)).bdd_mul
    hw.continuous.aestronglyMeasurable (ae_of_all _ hb)
  have hi := (integrable_fderiv_apply hiL hiC I).bdd_mul
    hw.continuous.aestronglyMeasurable (ae_of_all _ hb)
  have hrr := integrable_gradient_mul hw (hrL.continuous.integrable_of_hasCompactSupport hrC) (1:ℂ)
  have hir := integrable_gradient_mul hw (hiL.continuous.integrable_of_hasCompactSupport hiC) I
  change (∫ x, w x*(fderiv ℝ (Complex.reCLM ∘ V) x 1+fderiv ℝ (Complex.imCLM ∘ V) x I)) = _
  simp only [mul_add]
  rw [integral_add hr hi,_root_.OAI.LipschitzWith.integral_mul_fderiv_eq_neg (μ := volume) hw hrL hrC (1:ℂ),
    _root_.OAI.LipschitzWith.integral_mul_fderiv_eq_neg (μ := volume) hw hiL hiC I,← neg_add,← integral_add hrr hir]
  congr 1
  apply integral_congr_ae
  filter_upwards with x
  exact (inner_coordinates _ _).symm
end SignedRuns

namespace SignedRuns
open ActualLabels FiniteLabels SignedAssembly Finset DiskMoments

lemma leafSum_add (f g : List Label → ℝ) (d : ℕ) (p : List Label) :
    leafSum (fun q => f q+g q) d p = leafSum f d p+leafSum g d p := by
  induction d generalizing p with
  | zero => rfl
  | succ d ih => simp only [leafSum,ih,sum_add_distrib]

lemma leafSum_mul_const (f : List Label → ℝ) (c : ℝ) (d : ℕ) (p : List Label) :
    leafSum (fun q => f q*c) d p = leafSum f d p*c := by
  induction d generalizing p with
  | zero => rfl
  | succ d ih => simp only [leafSum,ih,sum_mul]

lemma leafSum_const_mul (f : List Label → ℝ) (c : ℝ) (d : ℕ) (p : List Label) :
    leafSum (fun q => c*f q) d p = c*leafSum f d p := by
  simpa only [mul_comm] using leafSum_mul_const f c d p

lemma leafSum_neg (f : List Label → ℝ) (d : ℕ) (p : List Label) :
    leafSum (fun q => -(f q)) d p = -leafSum f d p := by
  simpa only [neg_one_mul] using leafSum_const_mul f (-1) d p

lemma leafSum_mono (f g : List Label → ℝ) (d : ℕ) (p : List Label)
    (hle : ∀ q, q.length = p.length+d → f q ≤ g q) : leafSum f d p ≤ leafSum g d p := by
  induction d generalizing p with
  | zero => exact hle p (by simp)
  | succ d ih =>
    apply sum_le_sum
    intro n _
    apply ih
    intro q hq
    apply hle q
    simp only [List.length_cons] at hq
    omega

lemma leafSum_congr (f g : List Label → ℝ) (d : ℕ) (p : List Label)
    (he : ∀ q, q.length = p.length+d → f q = g q) : leafSum f d p = leafSum g d p :=
  le_antisymm (leafSum_mono f g d p (fun q hq => (he q hq).le))
    (leafSum_mono g f d p (fun q hq => (he q hq).ge))

lemma leafSum_weight (h : ℂ → ℂ) (r : ℕ → ℝ) (old : Label) (x : ℂ) (d : ℕ) (p : List Label) :
    leafSum (weight h r old x) d p = weight h r old x p := by
  induction d generalizing p with
  | zero => rfl
  | succ d ih =>
    simp only [leafSum,ih,weight,← mul_sum,(observation h r p.length x (p.headD old)).total,mul_one]

lemma leafSum_integrable (f : List Label → ℂ → ℝ) (hf : ∀ q, Integrable (f q))
    (d : ℕ) (p : List Label) : Integrable (fun x => leafSum (fun q => f q x) d p) := by
  induction d generalizing p with
  | zero => exact hf p
  | succ d ih => exact integrable_finsetSum _ (fun n _ => ih (n::p))

lemma integral_leafSum (f : List Label → ℂ → ℝ) (hf : ∀ q, Integrable (f q))
    (d : ℕ) (p : List Label) :
    (∫ x, leafSum (fun q => f q x) d p) = leafSum (fun q => ∫ x, f q x) d p := by
  induction d generalizing p with
  | zero => rfl
  | succ d ih =>
    simp only [leafSum]
    rw [integral_finsetSum _ (fun n _ => leafSum_integrable f hf d (n::p))]
    exact sum_congr rfl (fun n _ => ih (n::p))

theorem history_weighted_ibp (g : ℂ → ℝ) (h : ℂ → ℂ) (r : ℕ → ℝ)
    (hg : ContDiff ℝ 1 g) (hc : HasCompactSupport g) (hr : ∀ k, 0 < r k)
    (hstep : ∀ k, 2*r (k+1) = r k) (old : Label) (m : ℕ) :
    (∫ x, leafSum (fun q => weight h r old x q*divergence (fun y => potential g r y q) x) m []) =
      -∫ x, leafSum (fun q => inner ℝ (gradient (fun y => weight h r old y q) x) (potential g r x q)) m [] := by
  have hreg (q : List Label) := potential_regular hg hc hr hstep q
  have hint (q : List Label) : Integrable (fun x => weight h r old x q*divergence (fun y => potential g r y q) x) := by
    obtain ⟨K,hK⟩ := weight_lipschitz h r hr old q
    apply weighted_divergence_integrable hK _ (hreg q)
    intro x
    rw [Real.norm_eq_abs,abs_of_nonneg (weight_nonneg h r old x q)]
    exact weight_le_one h r old x q
  have hflux (q : List Label) : Integrable (fun x => inner ℝ (gradient (fun y => weight h r old y q) x) (potential g r x q)) := by
    obtain ⟨K,hK⟩ := weight_lipschitz h r hr old q
    exact potential_flux_integrable hK (hreg q)
  rw [integral_leafSum _ hint,integral_leafSum _ hflux,← leafSum_neg]
  apply leafSum_congr
  intro q _
  obtain ⟨K,hK⟩ := weight_lipschitz h r hr old q
  apply vector_weighted_ibp hK _ (hreg q)
  intro x
  rw [Real.norm_eq_abs,abs_of_nonneg (weight_nonneg h r old x q)]
  exact weight_le_one h r old x q

/-- Averaging preserves the complete signed run-master inequality; finite
history weights sum to one at every observation point. -/
theorem expected_run_master (g : ℂ → ℝ) (h : ℂ → ℂ) (r : ℕ → ℝ)
    (hg : ContDiff ℝ 1 g) (hc : HasCompactSupport g) (hr : ∀ k, 0 < r k)
    (hstep : ∀ k, 2*r (k+1) = r k) (old : Label) (m : ℕ) (x : ℂ)
    (hd : ∀ i j, i ≤ j → DifferentiableAt ℝ (runEnvelope g r i j) x) :
    ‖gradient (runEnvelope g r 0 m) x‖ ≤
      12*leafSum (fun q => weight h r old x q*((runs q).map fun a => coneContribution a.dir
        (gradient (runEnvelope g r a.first a.last) x)).sum) (m+1) [] := by
  have hh := leafSum_mono
    (fun q => weight h r old x q*‖gradient (runEnvelope g r 0 m) x‖)
    (fun q => weight h r old x q*(12*((runs q).map fun a => coneContribution a.dir
        (gradient (runEnvelope g r a.first a.last) x)).sum)) (m+1) [] (by
      intro q hq
      cases q with
      | nil => simp at hq
      | cons n p =>
        have hp : p.length = m := by simpa using hq
        apply mul_le_mul_of_nonneg_left _ (weight_nonneg h r old x (n::p))
        simpa only [hp] using run_master hg hc hr hstep n p x hd)
  rw [leafSum_mul_const,leafSum_weight,weight,one_mul] at hh
  have he : (fun q => weight h r old x q*(12*((runs q).map fun a => coneContribution a.dir
        (gradient (runEnvelope g r a.first a.last) x)).sum)) =
      (fun q => 12*(weight h r old x q*((runs q).map fun a => coneContribution a.dir
        (gradient (runEnvelope g r a.first a.last) x)).sum)) := by funext q; ring
  rw [he,leafSum_const_mul] at hh
  exact hh
end SignedRuns

namespace SignedRuns
open DiskMoments ActualLabels Complex

lemma divergence_add {U V : ℂ → ℂ} {x : ℂ}
    (hU : DifferentiableAt ℝ U x) (hV : DifferentiableAt ℝ V x) :
    divergence (fun y => U y+V y) x = divergence U x+divergence V x := by
  have hUr : DifferentiableAt ℝ (fun y => (U y).re) x := Complex.reCLM.differentiableAt.comp x hU
  have hVr : DifferentiableAt ℝ (fun y => (V y).re) x := Complex.reCLM.differentiableAt.comp x hV
  have hUi : DifferentiableAt ℝ (fun y => (U y).im) x := Complex.imCLM.differentiableAt.comp x hU
  have hVi : DifferentiableAt ℝ (fun y => (V y).im) x := Complex.imCLM.differentiableAt.comp x hV
  simp only [divergence,Complex.add_re,Complex.add_im]
  rw [fderiv_fun_add hUr hVr,fderiv_fun_add hUi hVi]
  simp only [_root_.add_apply]
  ring

lemma divergence_scalar {f : ℂ → ℝ} {x : ℂ} (hf : DifferentiableAt ℝ f x) (n : ℂ) :
    divergence (fun y => f y • n) x = inner ℝ n (gradient f x) := by
  simp only [divergence,Complex.real_smul,Complex.mul_re,Complex.mul_im,
    Complex.ofReal_re,Complex.ofReal_im,zero_mul,sub_zero,add_zero]
  rw [fderiv_mul_const hf n.re,fderiv_mul_const hf n.im]
  simp only [_root_.smul_apply,smul_eq_mul]
  rw [real_inner_comm,inner_coordinates]
  simp only [gradient,InnerProductSpace.toDual_symm_apply]
  ring

lemma listSum_diff {α : Type*} (l : List α) (V : α → ℂ → ℂ) {x : ℂ}
    (hd : ∀ a ∈ l, DifferentiableAt ℝ (V a) x) :
    DifferentiableAt ℝ (fun y => (l.map fun a => V a y).sum) x := by
  induction l with
  | nil => exact differentiableAt_const 0
  | cons a l ih =>
    exact (hd a (by simp)).add (ih (fun a ha => hd a (by simp [ha])))

lemma divergence_listSum {α : Type*} (l : List α) (V : α → ℂ → ℂ) {x : ℂ}
    (hd : ∀ a ∈ l, DifferentiableAt ℝ (V a) x) :
    divergence (fun y => (l.map fun a => V a y).sum) x = (l.map fun a => divergence (V a) x).sum := by
  induction l with
  | nil => simp [divergence]
  | cons a l ih =>
    have hs := listSum_diff l V (fun b hb => hd b (by simp [hb]))
    simp only [List.map_cons,List.sum_cons]
    rw [divergence_add (hd a (by simp)) hs,ih (fun b hb => hd b (by simp [hb]))]

lemma average_diff {g : ℂ → ℝ} (hg : ContDiff ℝ 1 g) (hc : HasCompactSupport g)
    (x : ℂ) {t : ℝ} (ht : 0 < t) : DifferentiableAt ℝ (fun y => signedAverage g y t) x := by
  obtain ⟨K,hK⟩ := hg.lipschitzWith_of_hasCompactSupport hc one_ne_zero
  exact (hasFDerivAt_signedAverage hg (fun y => norm_fderiv_le_of_lipschitz ℝ hK) x ht).differentiableAt

/-- The divergence of the exact path potential is the complete signed sum
of run gradients, with precisely the starting-baseline gradients removed. -/
lemma potential_divergence {g : ℂ → ℝ} {r : ℕ → ℝ} (hg : ContDiff ℝ 1 g)
    (hgc : HasCompactSupport g) (hr : ∀ k, 0 < r k) (p : List Label) (x : ℂ)
    (hd : ∀ i j, i ≤ j → DifferentiableAt ℝ (runEnvelope g r i j) x) :
    divergence (fun y => potential g r y p) x =
      ((runs p).map fun a => inner ℝ (a.dir:ℂ)
        (gradient (runEnvelope g r a.first a.last) x-gradient (fun y => signedAverage g y (r a.first)) x)).sum := by
  cases p with
  | nil => simp [potential,runs,divergence]
  | cons n p =>
    simp_rw [potential_eq_runs]
    have hd' (a : Run) (ha : a ∈ runs (n::p)) :
        DifferentiableAt ℝ (fun y => runEnvelope g r a.first a.last y-signedAverage g y (r a.first)) x :=
      (hd a.first a.last (runs_bounds n p a ha).1).sub (average_diff hg hgc x (hr a.first))
    rw [divergence_listSum _ _ (fun a ha => (hd' a ha).smul_const (a.dir:ℂ))]
    apply congrArg List.sum
    apply List.map_congr_left
    intro a ha
    rw [divergence_scalar (hd' a ha)]
    congr 1
    simp only [gradient]
    rw [fderiv_fun_sub (hd a.first a.last (runs_bounds n p a ha).1) (average_diff hg hgc x (hr a.first)),map_sub]
end SignedRuns

namespace SignedRuns
open DiskMoments ActualLabels Finset ConeCoordinates

lemma radius_in_run_bin (r : ℕ → ℝ) (hstep : ∀ k, 2*r (k+1) = r k)
    {i j : ℕ} (hij : i ≤ j) {t : ℝ} (ht : t ∈ Icc (r j) (2*r i)) :
    ∃ k ∈ Set.Icc i j, t ∈ Icc (r k) (2*r k) := by
  obtain ⟨l,hl,htl⟩ := radius_in_bin (fun k => r (i+k))
    (fun k => by simpa [Nat.add_assoc] using hstep (i+k)) (j-i)
    (by simpa only [Nat.add_sub_of_le hij,Nat.add_zero] using ht)
  exact ⟨i+l,⟨by omega,by omega⟩,htl⟩

lemma attaining_endpoint {g : ℂ → ℝ} {r : ℕ → ℝ}
    (hr : ∀ k, 0 < r k) (hstep : ∀ k, 2*r (k+1) = r k)
    {i j k : ℕ} (hik : i ≤ k) (hkj : k ≤ j) {x : ℂ} {t : ℝ}
    (ht : t ∈ Icc (r k) (2*r k))
    (hmax : ∀ s ∈ Icc (r j) (2*r i), signedAverage g x s ≤ signedAverage g x t) :
    PhysicalEndpointRadius g (r k) x t := by
  refine ⟨ht,Or.inr (Or.inr ?_)⟩
  intro s hs
  apply hmax
  constructor
  · exact (radius_antitone r hr hstep hkj).trans hs.1
  · have := radius_antitone r hr hstep hik
    linarith [hs.2]

lemma attaining_interior {g : ℂ → ℝ} {r : ℕ → ℝ}
    (hr : ∀ k, 0 < r k) (hstep : ∀ k, 2*r (k+1) = r k)
    {i j k : ℕ} (hik : i < k) (hkj : k < j) {x : ℂ} {t : ℝ}
    (ht : t ∈ Icc (r k) (2*r k))
    (hmax : ∀ s ∈ Icc (r j) (2*r i), signedAverage g x s ≤ signedAverage g x t) :
    PhysicalNeighborhoodRadius g (r k) x t := by
  refine ⟨ht,?_⟩
  intro s _ hs
  apply hmax
  have hs' := abs_le.mp hs
  have hlow := radius_antitone r hr hstep (show k+1 ≤ j by omega)
  have hhigh := radius_antitone r hr hstep (show i+1 ≤ k by omega)
  constructor <;> linarith [hstep k,hstep i,hr k,ht.1,ht.2]

def unfavorable (n : Label) (z : ℂ) : ℝ :=
  max 0 (-inner ℝ (axis n 1) z)+max 0 (-inner ℝ (axis n (-1)) z)

lemma unfavorable_nonneg (n : Label) (z : ℂ) : 0 ≤ unfavorable n z :=
  add_nonneg (le_max_left _ _) (le_max_left _ _)

lemma unfavorable_le_norm (n : Label) (z : ℂ) : unfavorable n z ≤ 2*‖z‖ := by
  have h (s : ℝ) (hs : s^2 = 1) : max 0 (-inner ℝ (axis n s) z) ≤ ‖z‖ := by
    apply max_le (norm_nonneg _)
    have hh := abs_real_inner_le_norm (axis (n:ℂ) s) z
    rw [axis_norm (label_unit n) hs,one_mul] at hh
    exact (neg_le_abs _).trans hh
  have hp := h 1 (by norm_num)
  have hm := h (-1) (by norm_num)
  unfold unfavorable
  linarith

/-- The exact unfavorable run term is charged either to one interior bin or
one of its two end bins; padding by nonnegative costs permits summing runs. -/
lemma run_unfavorable_bound {g : ℂ → ℝ} {r : ℕ → ℝ}
    (hg : ContDiff ℝ 1 g) (hc : HasCompactSupport g)
    (hr : ∀ k, 0 < r k) (hstep : ∀ k, 2*r (k+1) = r k)
    (H : ℕ → Label → ℝ) (J : ℕ → ℝ) (hH : ∀ k n, 0 ≤ H k n) (hJ : ∀ k, 0 ≤ J k)
    (x : ℂ) (n : Label) {i j : ℕ} (hij : i ≤ j)
    (hd : DifferentiableAt ℝ (runEnvelope g r i j) x)
    (hinterior : ∀ k t, PhysicalNeighborhoodRadius g (r k) x t →
      unfavorable n (gradient (fun y => signedAverage g y t) x) ≤ H k n)
    (hend : ∀ k t, PhysicalEndpointRadius g (r k) x t →
      ‖gradient (fun y => signedAverage g y t) x‖ ≤ J k) :
    unfavorable n (gradient (runEnvelope g r i j) x) ≤
      (∑ k ∈ Icc i j, H k n)+2*(J i+J j) := by
  obtain ⟨t,ht,he,hm⟩ := runEnvelope_attains g r hg hr hstep hij x
  rw [run_gradient_touching hg hc hr hd ht he]
  obtain ⟨k,hk,hkt⟩ := radius_in_run_bin r hstep hij ht
  have hs : 0 ≤ ∑ k ∈ Icc i j, H k n := sum_nonneg (fun _ _ => hH _ _)
  by_cases hki : k = i
  · subst k
    have hb := (unfavorable_le_norm n _).trans (mul_le_mul_of_nonneg_left
      (hend i t (attaining_endpoint hr hstep le_rfl hij hkt hm)) (by norm_num : (0:ℝ) ≤ 2))
    linarith [hJ j]
  by_cases hkj : k = j
  · subst k
    have hb := (unfavorable_le_norm n _).trans (mul_le_mul_of_nonneg_left
      (hend j t (attaining_endpoint hr hstep hij le_rfl hkt hm)) (by norm_num : (0:ℝ) ≤ 2))
    linarith [hJ i]
  have hik := hk.1
  have hkj' := hk.2
  have hb := hinterior k t (attaining_interior hr hstep (by omega) (by omega) hkt hm)
  have hsum : H k n ≤ ∑ l ∈ Icc i j, H l n := single_le_sum (fun l _ => hH l n) (Finset.mem_Icc.mpr hk)
  linarith [hJ i,hJ j]
end SignedRuns

namespace SignedRuns
open ActualLabels FiniteLabels Finset

lemma leafSum_succ_last {E : Type*} [AddCommMonoid E] (f : List Label → E)
    (m : ℕ) (p : List Label) :
    leafSum f (m+1) p = leafSum (fun q => ∑ n, f (n::q)) m p := by
  induction m generalizing p with
  | zero => rfl
  | succ m ih =>
    change (∑ n, leafSum f (m+1) (n::p)) =
      ∑ n, leafSum (fun q => ∑ j, f (j::q)) m (n::p)
    simp_rw [ih]

lemma leafSum_sum {E : Type*} [AddCommMonoid E] {ι : Type*}
    (s : Finset ι) (f : ι → List Label → E) (m : ℕ) (p : List Label) :
    leafSum (fun q => ∑ i ∈ s, f i q) m p = ∑ i ∈ s, leafSum (f i) m p := by
  induction m generalizing p with
  | zero => rfl
  | succ m ih => simp only [leafSum,ih]; exact sum_comm

/-- The exact head law obtained from the genuine finite-history products. -/
lemma leafSum_headLaw (h : ℂ → ℂ) (r : ℕ → ℝ) (old : Label) (x : ℂ)
    (m : ℕ) (F : Label → ℝ) :
    leafSum (fun q => weight h r old x q*F (q.headD old)) m [] =
      expect (before h r old x m) F := by
  induction m generalizing F with
  | zero => simp only [leafSum,weight,List.headD_nil,one_mul,before,expect_delta]
  | succ m ih =>
    rw [leafSum_succ_last,before_move,expect_move]
    have he (q : List Label) :
        (∑ n, weight h r old x (n::q)*F ((n::q).headD old)) =
          weight h r old x q*expect (observation h r q.length x (q.headD old)) F := by
      simp only [weight,List.headD_cons,FiniteLabels.expect,mul_assoc,mul_sum]
    simp_rw [he]
    have hc := leafSum_congr
      (fun q => weight h r old x q*expect (observation h r q.length x (q.headD old)) F)
      (fun q => weight h r old x q*expect (observation h r m x (q.headD old)) F) m []
      (by
        intro q hq
        have hqm : q.length = m := by simpa using hq
        rw [hqm])
    rw [hc]
    exact ih (fun n => FiniteLabels.expect (observation h r m x n) F)

/-- Additive reward along a reversed history, with its true previous state. -/
def pathReward (old : Label) (A : ℕ → Label → Label → ℝ) : List Label → ℝ
  | [] => 0
  | n::p => pathReward old A p+A p.length (p.headD old) n

lemma leafSum_pathReward (h : ℂ → ℂ) (r : ℕ → ℝ) (old : Label) (x : ℂ)
    (A : ℕ → Label → Label → ℝ) (m : ℕ) :
    leafSum (fun q => weight h r old x q*pathReward old A q) m [] =
      ∑ k ∈ range m, expect (before h r old x k)
        (fun n => expect (observation h r k x n) (A k n)) := by
  induction m with
  | zero => simp [leafSum,pathReward]
  | succ m ih =>
    rw [leafSum_succ_last,sum_range_succ]
    have he (q : List Label) :
        (∑ n, weight h r old x (n::q)*pathReward old A (n::q)) =
          weight h r old x q*pathReward old A q+
            weight h r old x q*expect (observation h r q.length x (q.headD old)) (A q.length (q.headD old)) := by
      simp only [weight,pathReward,mul_add,sum_add_distrib,← sum_mul,← mul_sum,FiniteLabels.expect,mul_assoc]
      rw [(observation h r q.length x (q.headD old)).total,one_mul]
    simp_rw [he]
    rw [leafSum_add,ih]
    congr 1
    have hc := leafSum_congr
      (fun q => weight h r old x q*expect (observation h r q.length x (q.headD old)) (A q.length (q.headD old)))
      (fun q => weight h r old x q*expect (observation h r m x (q.headD old)) (A m (q.headD old))) m []
      (by
        intro q hq
        have hqm : q.length = m := by simpa using hq
        rw [hqm])
    rw [hc]
    exact leafSum_headLaw h r old x m (fun n => FiniteLabels.expect (observation h r m x n) (A m n))

lemma pathReward_add (old : Label) (A B : ℕ → Label → Label → ℝ) (q : List Label) :
    pathReward old (fun k n j => A k n j+B k n j) q =
      pathReward old A q+pathReward old B q := by
  induction q with
  | nil => simp [pathReward]
  | cons n p ih => simp only [pathReward,ih]; ring

lemma pathReward_nonneg (old : Label) (A : ℕ → Label → Label → ℝ)
    (hA : ∀ k n j, 0 ≤ A k n j) (q : List Label) : 0 ≤ pathReward old A q := by
  induction q with
  | nil => exact le_rfl
  | cons n p ih => exact add_nonneg ih (hA _ _ _)

end SignedRuns

namespace SignedRuns
open ActualLabels FiniteLabels Finset

lemma run_span_sum (H : ℕ → Label → ℝ) (old : Label) (p : List Label) :
    ((runs p).map fun a => ∑ k ∈ Icc a.first a.last, H k a.dir).sum =
      pathReward old (fun k _ n => H k n) p := by
  cases p with
  | nil => rfl
  | cons n p =>
    induction p generalizing n with
    | nil => simp [runs,closedRuns,activeStart,pathReward]
    | cons n' p ih =>
      by_cases hn : n = n'
      · subst n
        have hsum : (∑ k ∈ Icc (activeStart (n'::p)) (p.length+1), H k n') =
            (∑ k ∈ Icc (activeStart (n'::p)) p.length, H k n')+H (p.length+1) n' := by
          exact sum_Icc_succ_top ((activeStart_le n' p).trans (Nat.le_succ _)) _
        have hh := ih n'
        simp only [runs,List.map_cons,List.sum_cons] at hh
        simp only [runs,closedRuns,activeStart,ite_true,List.length_cons,List.map_cons,List.sum_cons,pathReward]
        rw [hsum]
        simp only [pathReward] at hh
        linarith
      · have hh := ih n'
        simp only [runs,List.map_cons,List.sum_cons] at hh
        simp only [runs,closedRuns,activeStart,hn,ite_false,List.length_cons,List.map_cons,List.sum_cons,pathReward,
          Finset.Icc_self,sum_singleton]
        simp only [pathReward] at hh
        linarith

def startReward (J : ℕ → ℝ) : ℕ → Label → Label → ℝ
  | 0,_,_ => J 0
  | k+1,n,j => if j = n then 0 else J (k+1)

def boundaryReward (J : ℕ → ℝ) : ℕ → Label → Label → ℝ
  | 0,_,_ => J 0
  | k+1,n,j => if j = n then 0 else J k+J (k+1)

lemma run_start_sum (J : ℕ → ℝ) (old : Label) (p : List Label) :
    ((runs p).map fun a => J a.first).sum = pathReward old (startReward J) p := by
  cases p with
  | nil => rfl
  | cons n p =>
    induction p generalizing n with
    | nil => simp [runs,activeStart,closedRuns,pathReward,startReward]
    | cons n' p ih =>
      have hh := ih n'
      simp only [runs,List.map_cons,List.sum_cons] at hh
      simp only [pathReward,startReward] at hh
      by_cases hn : n = n'
      · subst n
        simpa only [runs,closedRuns,activeStart,ite_true,List.length_cons,List.map_cons,List.sum_cons,
          pathReward,startReward,List.headD_cons,add_zero] using hh
      · simp only [runs,closedRuns,activeStart,hn,ite_false,List.length_cons,List.map_cons,List.sum_cons,
          pathReward,startReward,List.headD_cons]
        linarith

lemma run_boundary_sum (J : ℕ → ℝ) (old n : Label) (p : List Label) :
    ((runs (n::p)).map fun a => J a.first+J a.last).sum =
      pathReward old (boundaryReward J) (n::p)+J p.length := by
  induction p generalizing n with
  | nil => simp [runs,activeStart,closedRuns,pathReward,boundaryReward]
  | cons n' p ih =>
    have hh := ih n'
    simp only [runs,List.map_cons,List.sum_cons,pathReward,boundaryReward] at hh
    by_cases hn : n = n'
    · subst n
      simp only [runs,closedRuns,activeStart,ite_true,List.length_cons,List.map_cons,List.sum_cons,
        pathReward,boundaryReward,List.headD_cons]
      linarith
    · simp only [runs,closedRuns,activeStart,hn,ite_false,List.length_cons,List.map_cons,List.sum_cons,
        pathReward,boundaryReward,List.headD_cons]
      linarith

lemma startReward_nonneg (J : ℕ → ℝ) (hJ : ∀ k, 0 ≤ J k) (k : ℕ) (n j : Label) :
    0 ≤ startReward J k n j := by
  cases k with
  | zero => exact hJ 0
  | succ k => simp only [startReward]; split_ifs <;> first | exact le_rfl | exact hJ _

lemma boundaryReward_nonneg (J : ℕ → ℝ) (hJ : ∀ k, 0 ≤ J k) (k : ℕ) (n j : Label) :
    0 ≤ boundaryReward J k n j := by
  cases k with
  | zero => exact hJ 0
  | succ k => simp only [boundaryReward]; split_ifs <;> first | exact le_rfl | exact add_nonneg (hJ _) (hJ _)

end SignedRuns

end

end OAI