File size: 22,927 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Geometry.PinnedDistances.FiberOverlap

namespace OAI

universe u1 u2 u3

section
noncomputable section
open NumberField
open scoped BigOperators
noncomputable section
open MeasureTheory
open scoped Topology
noncomputable section
open MeasureTheory
open scoped Topology ENNReal
noncomputable section
open MeasureTheory NumberField
open scoped BigOperators
noncomputable section
open scoped BigOperators
open MeasureTheory

namespace WeakPinned.Grid
open MeasureTheory

lemma truncated_error_integrable {z w : β„‚} (hne : z β‰  w) (t : ℝ) :
    Integrable (fun Ο‰ => max (|error Ο‰ z w|-t) 0) fullLaw :=
  (((error_integrable_full hne).abs.sub (integrable_const t)).sup (integrable_zero _ _ _))

lemma truncated_error_mean {z w : β„‚} (hne : z β‰  w) {t : ℝ} (ht : 0 ≀ t) :
    (∫ Ο‰, max (|error Ο‰ z w|-t) 0 βˆ‚fullLaw) ≀ 4*Real.exp (-t) := by
  have hm : Measurable (fun Ο‰ => max (|error Ο‰ z w|-t) 0) :=
    ((measurable_error hne).abs.sub_const t).max measurable_const
  have hn (Ο‰ : FullSample) : 0 ≀ max (|error Ο‰ z w|-t) 0 := le_max_right _ _
  have hi := truncated_error_integrable hne t
  have he : IntegrableOn (fun u : ℝ => (4*Real.exp (-t))*Real.exp (-u)) (Set.Ioi 0) volume := by
    simpa only [neg_one_mul, IntegrableOn] using
      (integrableOn_exp_mul_Ioi (by norm_num : (-1:ℝ)<0) 0).const_mul (4*Real.exp (-t))
  have hb : (∫⁻ Ο‰, ENNReal.ofReal (max (|error Ο‰ z w|-t) 0) βˆ‚fullLaw) ≀
      ENNReal.ofReal (4*Real.exp (-t)) := by
    rw [lintegral_eq_lintegral_meas_lt fullLaw
      (Filter.Eventually.of_forall hn) hm.aemeasurable]
    calc
      _ ≀ ∫⁻ u : ℝ in Set.Ioi 0, ENNReal.ofReal ((4*Real.exp (-t))*Real.exp (-u)) := by
        apply lintegral_mono_ae
        filter_upwards [ae_restrict_mem measurableSet_Ioi] with u hu
        have hu0 : 0 < u := hu
        have hs : {Ο‰ : FullSample | u < max (|error Ο‰ z w|-t) 0} =
            {Ο‰ : FullSample | u+t < |error Ο‰ z w|} := by
          ext Ο‰
          simp only [Set.mem_ofPred_eq,lt_max_iff]
          constructor
          Β· rintro (h | h) <;> linarith
          Β· intro h; exact Or.inl (by linarith)
        rw [hs, ← ENNReal.ofReal_toReal (measure_ne_top fullLaw _)]
        have hh := abs_error_tail_full hne (add_nonneg hu.le ht)
        apply ENNReal.ofReal_le_ofReal
        calc
          _ ≀ 4*Real.exp (-(u+t)) := hh
          _ = _ := by rw [neg_add,Real.exp_add]; ring
      _ = ENNReal.ofReal (4*Real.exp (-t)) := by
        rw [← ofReal_integral_eq_lintegral_ofReal he (by filter_upwards with u; positivity)]
        congr 1
        rw [integral_const_mul]
        have hz := integral_exp_mul_Ioi (by norm_num : (-1:ℝ)<0) (0:ℝ)
        simp only [neg_one_mul,mul_zero,Real.exp_zero,neg_div_neg_eq,div_one] at hz
        rw [hz,mul_one]
  rw [← ofReal_integral_eq_lintegral_ofReal hi (Filter.Eventually.of_forall hn)] at hb
  exact (ENNReal.ofReal_le_ofReal_iff (by positivity)).mp hb

lemma truncated_sum_bound (a b H : ℝ) :
    max (|a+b|-H) 0 ≀ 2*max (|a|-H/2) 0 + 2*max (|b|-H/2) 0 := by
  apply max_le
  Β· have ht := abs_add_le a b
    have h₁ := le_max_left (|a|-H/2) 0
    have hβ‚‚ := le_max_left (|b|-H/2) 0
    have h₃ := le_max_right (|a|-H/2) 0
    have hβ‚„ := le_max_right (|b|-H/2) 0
    linarith
  Β· positivity

lemma paired_error_mean {z₁ w₁ zβ‚‚ wβ‚‚ : β„‚} (h₁ : z₁ β‰  w₁) (hβ‚‚ : zβ‚‚ β‰  wβ‚‚)
    {H : ℝ} (hH : 0 ≀ H) :
    (∫ Ο‰, max (|error Ο‰ z₁ w₁ + error Ο‰ zβ‚‚ wβ‚‚|-H) 0 βˆ‚fullLaw) ≀
      16*Real.exp (-H/2) := by
  have hi : Integrable (fun Ο‰ => max (|error Ο‰ z₁ w₁ + error Ο‰ zβ‚‚ wβ‚‚|-H) 0) fullLaw := by
    exact Integrable.sup (((error_integrable_full h₁).add (error_integrable_full hβ‚‚)).abs.sub
      (integrable_const H)) (integrable_zero _ _ _)
  have hia := truncated_error_integrable h₁ (H/2)
  have hib := truncated_error_integrable hβ‚‚ (H/2)
  have hh := integral_mono hi ((hia.const_mul 2).add (hib.const_mul 2))
    (fun Ο‰ => truncated_sum_bound (error Ο‰ z₁ w₁) (error Ο‰ zβ‚‚ wβ‚‚) H)
  simp only [Pi.add_apply] at hh
  rw [integral_add (hia.const_mul 2) (hib.const_mul 2)] at hh
  simp only [integral_const_mul] at hh
  have hta := truncated_error_mean h₁ (div_nonneg hH (by norm_num : (0:ℝ)≀2))
  have htb := truncated_error_mean hβ‚‚ (div_nonneg hH (by norm_num : (0:ℝ)≀2))
  rw [neg_div'] at hta htb
  linarith

end WeakPinned.Grid

namespace WeakPinned.Global
open Hierarchy Variance SmallFiber
variable {P : Type u1} {I : Type u2} [Fintype P] [DecidableEq P] [Fintype I]
variable {K : Type u3} [Field K] [NumberField K]
variable (Z₁ Zβ‚‚ : P β†’ K) (h₁ : Function.Injective Z₁) (hβ‚‚ : Function.Injective Zβ‚‚)
include h₁ hβ‚‚

lemma arch_fiber_defect (v : InfinitePlace K) (a b : P) (hab : a β‰  b) (t : K)
    (ht : (Z₁ a-Z₁ b)*(Zβ‚‚ a-Zβ‚‚ b)=t) :
    (fun Ο‰ => (arch Z₁ v Ο‰).value a b+(arch Zβ‚‚ v Ο‰).value a b+Real.log (v t)) =ᡐ[Grid.fullLaw]
      (fun Ο‰ => Grid.error Ο‰ (v.embedding (Z₁ a)) (v.embedding (Z₁ b))+
        Grid.error Ο‰ (v.embedding (Zβ‚‚ a)) (v.embedding (Zβ‚‚ b))) := by
  have he := Arithmetic.raw_fiber_identity v.val (Z₁ a-Z₁ b) (Zβ‚‚ a-Zβ‚‚ b)
    (sub_ne_zero.mpr (h₁.ne hab)) (sub_ne_zero.mpr (hβ‚‚.ne hab))
  rw [ht] at he
  change -Real.log (v (Z₁ a-Z₁ b)) + -Real.log (v (Zβ‚‚ a-Zβ‚‚ b)) = -Real.log (v t) at he
  filter_upwards [arch_value_ae Z₁ h₁ v hab,arch_value_ae Zβ‚‚ hβ‚‚ v hab] with Ο‰ hA hB
  rw [hA,hB]
  simp only [Grid.error,← map_sub,InfinitePlace.norm_embedding_eq]
  linarith

lemma arch_defect_mean (v : InfinitePlace K) (a b : P) (hab : a β‰  b) (t : K)
    (ht : (Z₁ a-Z₁ b)*(Zβ‚‚ a-Zβ‚‚ b)=t) (H : ℝ) (hH : 0 ≀ H) :
    (∫ Ο‰, max (|(arch Z₁ v Ο‰).value a b+(arch Zβ‚‚ v Ο‰).value a b+Real.log (v t)|-H) 0 βˆ‚Grid.fullLaw) ≀
      16*Real.exp (-H/2) := by
  have he := arch_fiber_defect Z₁ Zβ‚‚ h₁ hβ‚‚ v a b hab t ht
  have he' : (fun Ο‰ => max (|(arch Z₁ v Ο‰).value a b+(arch Zβ‚‚ v Ο‰).value a b+Real.log (v t)|-H) 0) =ᡐ[Grid.fullLaw]
      (fun Ο‰ => max (|Grid.error Ο‰ (v.embedding (Z₁ a)) (v.embedding (Z₁ b))+
        Grid.error Ο‰ (v.embedding (Zβ‚‚ a)) (v.embedding (Zβ‚‚ b))|-H) 0) := by
    filter_upwards [he] with Ο‰ hh
    rw [hh]
  rw [integral_congr_ae he']
  exact Grid.paired_error_mean (embedding_ne Z₁ h₁ v hab) (embedding_ne Zβ‚‚ hβ‚‚ v hab) hH

lemma arch_weighted_defect (v : InfinitePlace K) (w : I β†’ ℝ) (p : I β†’ P β†’ ℝ)
    (x : I β†’ P) (t : I β†’ K) (hw : βˆ€ i, 0 ≀ w i) (hW : βˆ‘ i, w i=1)
    (hp : βˆ€ i y, 0 ≀ p i y) (hp1 : βˆ€ i, βˆ‘ y, p i y=1) (hx : βˆ€ i, p i (x i)=0)
    (ht : βˆ€ i y, p i y β‰  0 β†’ (Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y)=t i)
    (H : ℝ) (hH : 0 ≀ H) :
    (∫ Ο‰, βˆ‘ i, w i*βˆ‘ y, p i y*max (|(arch Z₁ v Ο‰).value (x i) y+
      (arch Zβ‚‚ v Ο‰).value (x i) y+Real.log (v (t i))|-H) 0 βˆ‚Grid.fullLaw) ≀ 16*Real.exp (-H/2) := by
  have hi (i : I) (y : P) : Integrable (fun Ο‰ => max (|(arch Z₁ v Ο‰).value (x i) y+
      (arch Zβ‚‚ v Ο‰).value (x i) y+Real.log (v (t i))|-H) 0) Grid.fullLaw :=
    ((((arch_integrable Z₁ h₁ v _ _).add (arch_integrable Zβ‚‚ hβ‚‚ v _ _)).add (integrable_const _)).abs.sub
      (integrable_const H)).sup (integrable_zero _ _ _)
  rw [integral_finsetSum _ (fun i _ => (integrable_finsetSum (s:=Finset.univ)
    (fun y _ => (hi i y).const_mul _)).const_mul _)]
  simp_rw [integral_const_mul,integral_finsetSum _ (fun y _ => (hi _ y).const_mul _),integral_const_mul]
  calc
    _ ≀ βˆ‘ i, w i * βˆ‘ y, p i y*(16*Real.exp (-H/2)) := by
      apply Finset.sum_le_sum
      intro i _
      apply mul_le_mul_of_nonneg_left _ (hw i)
      apply Finset.sum_le_sum
      intro y _
      by_cases hy : p i y=0
      Β· simp [hy]
      Β· have hxy : x i β‰  y := by intro he; rw [← he,hx i] at hy; exact hy rfl
        exact mul_le_mul_of_nonneg_left (arch_defect_mean Z₁ Zβ‚‚ h₁ hβ‚‚ v _ _ hxy _ (ht i y hy) H hH) (hp i y)
    _ = _ := by simp only [← Finset.sum_mul,hp1,one_mul,hW]

lemma arch_small_fiber (hn : 2 ≀ Fintype.card P) (v : InfinitePlace K)
    (w : I β†’ ℝ) (p : I β†’ P β†’ ℝ) (x : I β†’ P) (t : I β†’ K)
    (hw : βˆ€ i, 0 ≀ w i) (hW : βˆ‘ i, w i=1) (hp : βˆ€ i y, 0 ≀ p i y)
    (hp1 : βˆ€ i, βˆ‘ y, p i y=1) (hx : βˆ€ i, p i (x i)=0)
    (ht : βˆ€ i y, p i y β‰  0 β†’ (Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y)=t i)
    (B Ο„ lam H : ℝ) (hB : 0 ≀ B) (hlam : 0 ≀ lam) (hlam2 : lam ≀ 1/2)
    (hΟ„ : 0 < Ο„) (hΟ„1 : Ο„ ≀ 1) (hΟ„n : 2 ≀ Ο„*(Fintype.card P:ℝ)) (hH : 0 ≀ H)
    (hdom : βˆ€ a b, a β‰  b β†’ edgeLaw w p x a b ≀ B/((Fintype.card P:ℝ)*((Fintype.card P:ℝ)-1))) :
    (βˆ‘ C, (∫ Ο‰, (arch Z₁ v Ο‰).mass C βˆ‚Grid.fullLaw)*slice w p x lam C) ≀
      (2*lam/Ο„^2)*(βˆ‘ C, (∫ Ο‰, (arch Z₁ v Ο‰).mass C βˆ‚Grid.fullLaw)*pairCell Finset.univ C) +
      B*Ο„/(Fintype.card P:ℝ)*((βˆ‘ y, ∫ Ο‰, (arch Z₁ v Ο‰).potential y βˆ‚Grid.fullLaw)+
        (βˆ‘ y, ∫ Ο‰, (arch Zβ‚‚ v Ο‰).potential y βˆ‚Grid.fullLaw)) +
      2*lam*B*(βˆ‘ C, (∫ Ο‰, (arch Zβ‚‚ v Ο‰).mass C βˆ‚Grid.fullLaw)*pairCell Finset.univ C) + 2*lam*H +
      16*Real.exp (-H/2) := by
  have hiM₁ (C : Finset P) := Depth.integrable_mass_param (arch Z₁ v) (arch_measurable Z₁ h₁ v)
    (arch_integrable Z₁ h₁ v) hn C
  have hiMβ‚‚ (C : Finset P) := Depth.integrable_mass_param (arch Zβ‚‚ v) (arch_measurable Zβ‚‚ hβ‚‚ v)
    (arch_integrable Zβ‚‚ hβ‚‚ v) hn C
  have hiS₁ (y : P) := Depth.integrable_potential_param (arch Z₁ v) (arch_measurable Z₁ h₁ v)
    (arch_integrable Z₁ h₁ v) hn y
  have hiSβ‚‚ (y : P) := Depth.integrable_potential_param (arch Zβ‚‚ v) (arch_measurable Zβ‚‚ hβ‚‚ v)
    (arch_integrable Zβ‚‚ hβ‚‚ v) hn y
  have hiE (i : I) (y : P) : Integrable (fun Ο‰ => max (|(arch Z₁ v Ο‰).value (x i) y+
      (arch Zβ‚‚ v Ο‰).value (x i) y+Real.log (v (t i))|-H) 0) Grid.fullLaw := by
    have hd := ((arch_integrable Z₁ h₁ v (x i) y).add (arch_integrable Zβ‚‚ hβ‚‚ v (x i) y)).add
      (integrable_const (Real.log (v (t i))))
    exact (hd.abs.sub (integrable_const H)).sup (integrable_zero _ _ _)
  have hA := (integrable_finsetSum (s:=Finset.univ) (fun C _ => (hiM₁ C).mul_const (pairCell Finset.univ C))).const_mul (2*lam/Ο„^2)
  have hS := ((integrable_finsetSum (s:=Finset.univ) (fun y _ => hiS₁ y)).add
    (integrable_finsetSum (s:=Finset.univ) (fun y _ => hiSβ‚‚ y))).const_mul (B*Ο„/Fintype.card P)
  have hA' := (integrable_finsetSum (s:=Finset.univ) (fun C _ => (hiMβ‚‚ C).mul_const (pairCell Finset.univ C))).const_mul (2*lam*B)
  have hE := integrable_finsetSum (s:=Finset.univ) (fun i _ =>
    (integrable_finsetSum (s:=Finset.univ) (fun y _ => (hiE i y).const_mul (p i y))).const_mul (w i))
  have hb := integral_mono (integrable_finsetSum (s:=Finset.univ) (fun C _ => (hiM₁ C).mul_const (slice w p x lam C)))
    ((((hA.add hS).add hA').add (integrable_const (2*lam*H))).add hE)
    (fun Ο‰ => by
      simpa only [Pi.add_apply,sub_neg_eq_add] using
        (local_small_fiber_reduced (arch Z₁ v Ο‰) (arch Zβ‚‚ v Ο‰) w p x hw hW hp hp1 hx hn
          (fun i => -Real.log (v (t i))) B Ο„ lam H hB hlam hlam2 hΟ„ hΟ„1 hΟ„n hH hdom))
  have hI₁ := integral_add (((hA.add hS).add hA').add (integrable_const (2*lam*H))) hE
  have hIβ‚‚ := integral_add ((hA.add hS).add hA') (integrable_const (2*lam*H))
  have hI₃ := integral_add (hA.add hS) hA'
  have hIβ‚„ := integral_add hA hS
  have hIβ‚… := integral_add (integrable_finsetSum (s:=Finset.univ) (fun y _ => hiS₁ y))
    (integrable_finsetSum (s:=Finset.univ) (fun y _ => hiSβ‚‚ y))
  simp only [Pi.add_apply] at hb hI₁ hIβ‚‚ hI₃ hIβ‚„ hIβ‚…
  rw [hI₁,hIβ‚‚,hI₃,hIβ‚„] at hb
  simp only [integral_const,probReal_univ,one_smul,integral_const_mul] at hb
  rw [hIβ‚…] at hb
  rw [integral_finsetSum _ (fun C _ => (hiM₁ C).mul_const (slice w p x lam C)),
    integral_finsetSum _ (fun C _ => (hiM₁ C).mul_const (pairCell Finset.univ C)),
    integral_finsetSum _ (fun C _ => (hiMβ‚‚ C).mul_const (pairCell Finset.univ C)),
    integral_finsetSum _ (fun y _ => hiS₁ y),integral_finsetSum _ (fun y _ => hiSβ‚‚ y)] at hb
  simp only [integral_mul_const] at hb
  have he := arch_weighted_defect Z₁ Zβ‚‚ h₁ hβ‚‚ v w p x t hw hW hp hp1 hx ht H hH
  linarith

lemma finite_small_fiber (hn : 2 ≀ Fintype.card P) (v : FinitePlace K)
    (w : I β†’ ℝ) (p : I β†’ P β†’ ℝ) (x : I β†’ P) (t : I β†’ K)
    (hw : βˆ€ i, 0 ≀ w i) (hW : βˆ‘ i, w i=1) (hp : βˆ€ i y, 0 ≀ p i y)
    (hp1 : βˆ€ i, βˆ‘ y, p i y=1) (hx : βˆ€ i, p i (x i)=0)
    (ht : βˆ€ i y, p i y β‰  0 β†’ (Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y)=t i)
    (B Ο„ lam : ℝ) (hB : 0 ≀ B) (hlam : 0 ≀ lam) (hlam2 : lam ≀ 1/2)
    (hΟ„ : 0 < Ο„) (hΟ„1 : Ο„ ≀ 1) (hΟ„n : 2 ≀ Ο„*(Fintype.card P:ℝ))
    (hdom : βˆ€ a b, a β‰  b β†’ edgeLaw w p x a b ≀ B/((Fintype.card P:ℝ)*((Fintype.card P:ℝ)-1))) :
    (βˆ‘ C, (finite Z₁ h₁ v).mass C*slice w p x lam C) ≀
      (2*lam/Ο„^2)*(βˆ‘ C, (finite Z₁ h₁ v).mass C*pairCell Finset.univ C) +
      B*Ο„/(Fintype.card P:ℝ)*((βˆ‘ y, (finite Z₁ h₁ v).potential y)+(βˆ‘ y, (finite Zβ‚‚ hβ‚‚ v).potential y)) +
      2*lam*B*(βˆ‘ C, (finite Zβ‚‚ hβ‚‚ v).mass C*pairCell Finset.univ C) := by
  have hb := local_small_fiber_reduced (finite Z₁ h₁ v) (finite Zβ‚‚ hβ‚‚ v) w p x hw hW hp hp1 hx hn
    (fun i => -Real.log (v (t i))) B Ο„ lam 0 hB hlam hlam2 hΟ„ hΟ„1 hΟ„n (le_refl _) hdom
  have he (i : I) (y : P) : p i y * max (|(finite Z₁ h₁ v).value (x i) y+
      (finite Zβ‚‚ hβ‚‚ v).value (x i) y- -Real.log (v (t i))|-0) 0=0 := by
    by_cases hy : p i y=0
    Β· simp [hy]
    Β· have hxy : x i β‰  y := by intro he; rw [← he,hx i] at hy; exact hy rfl
      have hh := Arithmetic.raw_fiber_identity v.val (Z₁ (x i)-Z₁ y) (Zβ‚‚ (x i)-Zβ‚‚ y)
        (sub_ne_zero.mpr (h₁.ne hxy)) (sub_ne_zero.mpr (hβ‚‚.ne hxy))
      change -Real.log (v (Z₁ (x i)-Z₁ y)) + -Real.log (v (Zβ‚‚ (x i)-Zβ‚‚ y)) =
        -Real.log (v ((Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y))) at hh
      rw [ht i y hy] at hh
      simp only [finite,hxy,reduceIte,hh,sub_self,abs_zero,max_self,mul_zero]
  simpa only [he,Finset.sum_const_zero,mul_zero,add_zero] using hb

omit h₁ hβ‚‚ in
lemma finite_zero_outside {Z : P β†’ K} (hZ : Function.Injective Z) (v : FinitePlace K)
    (hv : v βˆ‰ badPlaces Z hZ) : βˆ€ a b, (finite Z hZ v).value a b=0 := by
  intro a b
  by_cases hab : a=b
  Β· simp [finite,hab]
  Β· have hunit : v (Z a-Z b)=1 := by
      by_contra h
      exact hv ((badPlaces_mem Z hZ v).mpr ⟨a,b,hab,h⟩)
    simp [finite,hab,hunit]

omit h₁ hβ‚‚ in
lemma finite_mass_zero {Z : P β†’ K} (hZ : Function.Injective Z) (hn : 2 ≀ Fintype.card P)
    (v : FinitePlace K) (hv : v βˆ‰ badPlaces Z hZ) (C : Finset P) : (finite Z hZ v).mass C=0 := by
  have hz := (finite Z hZ v).zero_values hn (finite_zero_outside hZ v hv)
  by_cases hC : C.card ≀ 1
  Β· exact (finite Z hZ v).mass_singleton C hC
  · obtain ⟨a,ha,b,hb,hab⟩ := Finset.one_lt_card.mp (by omega : 1<C.card)
    have hl := (finite Z hZ v).mass_le_overlap hn C a b hab ha hb
    rw [hz.2.2 a b hab] at hl
    exact le_antisymm hl ((finite Z hZ v).mass_nonneg C)

omit h₁ hβ‚‚ in
lemma sum_finite_mass {Z : P β†’ K} (hZ : Function.Injective Z) (hn : 2 ≀ Fintype.card P)
    (V : Finset (FinitePlace K)) (hV : badPlaces Z hZ βŠ† V) (C : Finset P) :
    (βˆ‘ v ∈ V, (finite Z hZ v).mass C) = βˆ‘ v ∈ badPlaces Z hZ, (finite Z hZ v).mass C := by
  symm
  exact Finset.sum_subset hV (fun v _ hv => finite_mass_zero hZ hn v hv C)

omit h₁ hβ‚‚ in
lemma sum_finite_potential {Z : P β†’ K} (hZ : Function.Injective Z) (hn : 2 ≀ Fintype.card P)
    (V : Finset (FinitePlace K)) (hV : badPlaces Z hZ βŠ† V) (a : P) :
    (βˆ‘ v ∈ V, (finite Z hZ v).potential a) = βˆ‘ v ∈ badPlaces Z hZ, (finite Z hZ v).potential a := by
  symm
  exact Finset.sum_subset hV (fun v _ hv =>
    ((finite Z hZ v).zero_values hn (finite_zero_outside hZ v hv)).2.1 a)

omit h₁ hβ‚‚ in
def placeAverage (V : Finset (FinitePlace K)) (A : InfinitePlace K β†’ ℝ) (F : FinitePlace K β†’ ℝ) : ℝ :=
  (βˆ‘ v, Arithmetic.archWeight v * A v) + (Module.finrank β„š K:ℝ)⁻¹ * βˆ‘ v ∈ V, F v

omit h₁ hβ‚‚ in
lemma placeAverage_mono (V : Finset (FinitePlace K)) {A A' : InfinitePlace K β†’ ℝ} {F F' : FinitePlace K β†’ ℝ}
    (hA : βˆ€ v, A v ≀ A' v) (hF : βˆ€ v ∈ V, F v ≀ F' v) : placeAverage V A F ≀ placeAverage V A' F' := by
  apply add_le_add
  Β· exact Finset.sum_le_sum (fun v _ => mul_le_mul_of_nonneg_left (hA v) (Arithmetic.archWeight_nonneg v))
  Β· exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum hF) (by positivity)

omit h₁ hβ‚‚ in
lemma placeAverage_add (V : Finset (FinitePlace K)) (A A' : InfinitePlace K β†’ ℝ) (F F' : FinitePlace K β†’ ℝ) :
    placeAverage V (fun v => A v+A' v) (fun v => F v+F' v) = placeAverage V A F + placeAverage V A' F' := by
  simp only [placeAverage,mul_add,Finset.sum_add_distrib]
  ring

omit h₁ hβ‚‚ in
lemma placeAverage_mul (V : Finset (FinitePlace K)) (a : ℝ) (A : InfinitePlace K β†’ ℝ) (F : FinitePlace K β†’ ℝ) :
    placeAverage V (fun v => a*A v) (fun v => a*F v) = a*placeAverage V A F := by
  simp only [placeAverage,Finset.mul_sum,mul_add]
  congr 1
  Β· exact Finset.sum_congr rfl (fun v _ => by ring)
  Β· exact Finset.sum_congr rfl (fun v _ => by ring)

omit h₁ hβ‚‚ in
lemma placeAverage_arch_const (V : Finset (FinitePlace K)) (a : ℝ) :
    placeAverage V (fun _ => a) (fun _ => 0) = a := by
  simp only [placeAverage,Finset.sum_const_zero,mul_zero,add_zero,← Finset.sum_mul,Arithmetic.sum_archWeight,one_mul]

omit h₁ hβ‚‚ in
lemma cellIntegral_placeAverage {Z : P β†’ K} (hZ : Function.Injective Z) (hn : 2 ≀ Fintype.card P)
    (V : Finset (FinitePlace K)) (hV : badPlaces Z hZ βŠ† V) (f : Finset P β†’ ℝ) :
    weightedIntegral (cellMass Z hZ) f = placeAverage V
      (fun v => βˆ‘ C, (∫ Ο‰, (arch Z v Ο‰).mass C βˆ‚Grid.fullLaw)*f C)
      (fun v => βˆ‘ C, (finite Z hZ v).mass C*f C) := by
  simp only [weightedIntegral,cellMass,placeAverage,add_mul,Finset.sum_add_distrib]
  congr 1
  Β· simp_rw [Finset.mul_sum,Finset.sum_mul]
    rw [Finset.sum_comm]
    apply Finset.sum_congr rfl
    intro v _
    exact Finset.sum_congr rfl (fun C _ => by ring)
  Β· rw [Finset.sum_comm (s:=V)]
    simp_rw [← Finset.sum_mul, sum_finite_mass hZ hn V hV,Finset.mul_sum]
    exact Finset.sum_congr rfl (fun C _ => by rw [← Finset.mul_sum]; ring)

omit h₁ hβ‚‚ in
lemma potentialIntegral_placeAverage {Z : P β†’ K} (hZ : Function.Injective Z) (hn : 2 ≀ Fintype.card P)
    (V : Finset (FinitePlace K)) (hV : badPlaces Z hZ βŠ† V) :
    (βˆ‘ y, S Z hZ y) = placeAverage V
      (fun v => βˆ‘ y, ∫ Ο‰, (arch Z v Ο‰).potential y βˆ‚Grid.fullLaw)
      (fun v => βˆ‘ y, (finite Z hZ v).potential y) := by
  simp only [S,placeAverage,Finset.sum_add_distrib]
  congr 1
  Β· simp_rw [Finset.mul_sum]
    rw [Finset.sum_comm]
  Β· rw [Finset.sum_comm (s:=V)]
    simp_rw [sum_finite_potential hZ hn V hV,Finset.mul_sum]

lemma arithmetic_small_fiber_one (hn : 2 ≀ Fintype.card P)
    (w : I β†’ ℝ) (p : I β†’ P β†’ ℝ) (x : I β†’ P) (t : I β†’ K)
    (hw : βˆ€ i, 0 ≀ w i) (hW : βˆ‘ i, w i=1) (hp : βˆ€ i y, 0 ≀ p i y)
    (hp1 : βˆ€ i, βˆ‘ y, p i y=1) (hx : βˆ€ i, p i (x i)=0)
    (ht : βˆ€ i y, p i y β‰  0 β†’ (Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y)=t i)
    (B Ο„ lam H : ℝ) (hB : 0 ≀ B) (hlam : 0 ≀ lam) (hlam2 : lam ≀ 1/2)
    (hΟ„ : 0 < Ο„) (hΟ„1 : Ο„ ≀ 1) (hΟ„n : 2 ≀ Ο„*(Fintype.card P:ℝ)) (hH : 0 ≀ H)
    (hdom : βˆ€ a b, a β‰  b β†’ edgeLaw w p x a b ≀ B/((Fintype.card P:ℝ)*((Fintype.card P:ℝ)-1))) :
    weightedIntegral (cellMass Z₁ h₁) (slice w p x lam) ≀
      (2*lam/Ο„^2)*weightedIntegral (cellMass Z₁ h₁) (pairCell Finset.univ) +
      B*Ο„/(Fintype.card P:ℝ)*((βˆ‘ y, S Z₁ h₁ y)+(βˆ‘ y, S Zβ‚‚ hβ‚‚ y)) +
      2*lam*B*weightedIntegral (cellMass Zβ‚‚ hβ‚‚) (pairCell Finset.univ) + 2*lam*H +
      16*Real.exp (-H/2) := by
  classical
  let V := badPlaces Z₁ h₁ βˆͺ badPlaces Zβ‚‚ hβ‚‚
  have hV₁ : badPlaces Z₁ h₁ βŠ† V := Finset.subset_union_left
  have hVβ‚‚ : badPlaces Zβ‚‚ hβ‚‚ βŠ† V := Finset.subset_union_right
  have hf (v : FinitePlace K) : (βˆ‘ C, (finite Z₁ h₁ v).mass C*slice w p x lam C) ≀
      (2*lam/Ο„^2)*(βˆ‘ C, (finite Z₁ h₁ v).mass C*pairCell Finset.univ C) +
      B*Ο„/(Fintype.card P:ℝ)*((βˆ‘ y, (finite Z₁ h₁ v).potential y)+(βˆ‘ y, (finite Zβ‚‚ hβ‚‚ v).potential y)) +
      2*lam*B*(βˆ‘ C, (finite Zβ‚‚ hβ‚‚ v).mass C*pairCell Finset.univ C) + 0 + 0 := by
    simpa only [add_zero] using finite_small_fiber Z₁ Zβ‚‚ h₁ hβ‚‚ hn v w p x t hw hW hp hp1 hx ht B Ο„ lam hB hlam hlam2 hΟ„ hΟ„1 hΟ„n hdom
  have hb := placeAverage_mono V
    (fun v => arch_small_fiber Z₁ Zβ‚‚ h₁ hβ‚‚ hn v w p x t hw hW hp hp1 hx ht B Ο„ lam H hB hlam hlam2 hΟ„ hΟ„1 hΟ„n hH hdom)
    (fun v _ => hf v)
  simp only [placeAverage_add,placeAverage_mul,placeAverage_arch_const] at hb
  rw [← cellIntegral_placeAverage h₁ hn V hV₁,← cellIntegral_placeAverage h₁ hn V hV₁,
    ← cellIntegral_placeAverage hβ‚‚ hn V hVβ‚‚,← potentialIntegral_placeAverage h₁ hn V hV₁,
    ← potentialIntegral_placeAverage hβ‚‚ hn V hVβ‚‚] at hb
  exact hb

/-- The global small-fiber estimate, uniformly over number fields, places and configurations. -/

lemma arithmetic_small_fiber (hn : 2 ≀ Fintype.card P)
    (w : I β†’ ℝ) (p : I β†’ P β†’ ℝ) (x : I β†’ P) (t : I β†’ K)
    (hw : βˆ€ i, 0 ≀ w i) (hW : βˆ‘ i, w i=1) (hp : βˆ€ i y, 0 ≀ p i y)
    (hp1 : βˆ€ i, βˆ‘ y, p i y=1) (hx : βˆ€ i, p i (x i)=0)
    (ht : βˆ€ i y, p i y β‰  0 β†’ (Z₁ (x i)-Z₁ y)*(Zβ‚‚ (x i)-Zβ‚‚ y)=t i)
    (B Ο„ lam H : ℝ) (hB : 0 ≀ B) (hlam : 0 ≀ lam) (hlam2 : lam ≀ 1/2)
    (hΟ„ : 0 < Ο„) (hΟ„1 : Ο„ ≀ 1) (hΟ„n : 2 ≀ Ο„*(Fintype.card P:ℝ)) (hH : 0 ≀ H)
    (hdom : βˆ€ a b, a β‰  b β†’ edgeLaw w p x a b ≀ B/((Fintype.card P:ℝ)*((Fintype.card P:ℝ)-1))) :
    weightedIntegral (totalMass Z₁ Zβ‚‚ h₁ hβ‚‚) (slice w p x lam) ≀
      (2*lam/Ο„^2+2*lam*B)*weightedIntegral (totalMass Z₁ Zβ‚‚ h₁ hβ‚‚) (pairCell Finset.univ) +
      2*B*Ο„/(Fintype.card P:ℝ)*(βˆ‘ y, totalPotential Z₁ Zβ‚‚ h₁ hβ‚‚ y) + 4*lam*H +
      32*Real.exp (-H/2) := by
  have hA := arithmetic_small_fiber_one Z₁ Zβ‚‚ h₁ hβ‚‚ hn w p x t hw hW hp hp1 hx ht B Ο„ lam H hB hlam hlam2 hΟ„ hΟ„1 hΟ„n hH hdom
  have ht' : βˆ€ i y, p i y β‰  0 β†’ (Zβ‚‚ (x i)-Zβ‚‚ y)*(Z₁ (x i)-Z₁ y)=t i := by
    intro i y hy
    rw [mul_comm]
    exact ht i y hy
  have hB' := arithmetic_small_fiber_one Zβ‚‚ Z₁ hβ‚‚ h₁ hn w p x t hw hW hp hp1 hx ht' B Ο„ lam H hB hlam hlam2 hΟ„ hΟ„1 hΟ„n hH hdom
  simp only [weightedIntegral,totalMass,totalPotential,add_mul,Finset.sum_add_distrib] at hA hB' ⊒
  linear_combination hA+hB'

end WeakPinned.Global

noncomputable section
open Filter
open scoped Topology

end
end
end
end
end
end
end

end OAI