File size: 42,008 Bytes
9eeb9c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
import OAI.Analysis.HotSpots.Green

namespace OAI

section DouglasLipschitzBase
noncomputable section
section FullBoundaryCombinedLayer
section WeightedLayer
open Set MeasureTheory
open scoped ContDiff InnerProductSpace ENNReal NNReal
open Set Filter MeasureTheory InnerProductSpace NormedSpace
open scoped Topology ContDiff
open scoped Laplacian
open Set MeasureTheory Filter InnerProductSpace Bornology
open scoped Topology Laplacian ContDiff RealInnerProductSpace
open scoped Convolution
open Set MeasureTheory Filter InnerProductSpace
open scoped Topology ContDiff Convolution Laplacian
open InnerProductSpace
open scoped Laplacian ContDiff

open Filter Set
open scoped Topology
namespace StrictHotSpots.DiskFormula

def poisson (s x : ℂ) : ℝ := (1 - ‖x‖ ^ 2) / (2 * Real.pi * ‖s-x‖ ^ 2)

def boundaryInteraction (s t : ℂ) : ℝ := 1 / (Real.pi * ‖s-t‖ ^ 2)

private def logRatio (u : ℝ) : ℝ := dslope Real.log 1 (1+u)

private lemma logRatio_zero : logRatio 0 = 1 := by
  simp [logRatio, Real.deriv_log]

private lemma logRatio_continuousAt_zero : ContinuousAt logRatio 0 := by
  have hd : ContinuousAt (dslope Real.log 1) 1 :=
    continuousAt_dslope_same.mpr (Real.differentiableAt_log one_ne_zero)
  exact hd.comp_of_eq (show ContinuousAt (fun t : ℝ => 1+t) 0 from
    continuousAt_const.add continuousAt_id) (by simp)

private lemma log_eq_mul_ratio (u : ℝ) : Real.log (1+u) = u * logRatio u := by
  have h := sub_smul_dslope Real.log 1 (1+u)
  simpa [logRatio, smul_eq_mul] using h.symm

lemma tendsto_log_mul_div {α : Type*} {l : Filter α} {a b : α → ℝ} {B : ℝ}
    (ha : Tendsto a l (𝓝 0)) (hb : Tendsto b l (𝓝 B))
    (hne : ∀ᶠ z in l, a z ≠ 0) :
    Tendsto (fun z => Real.log (1 + a z * b z) / a z) l (𝓝 B) := by
  have hu : Tendsto (fun z => a z * b z) l (𝓝 0) := by simpa using ha.mul hb
  have ht := (logRatio_continuousAt_zero.tendsto.comp hu).mul hb
  rw [logRatio_zero, one_mul] at ht
  apply ht.congr'
  filter_upwards [hne] with z hz
  rw [log_eq_mul_ratio]
  dsimp only [Function.comp_def]
  field_simp [hz]

lemma radial_one_formula (s x : ℂ) (hs : ‖s‖ = 1) (r : ℝ) :
    green (r • s) x = Real.log (1 + (1-r^2) * (1-‖x‖^2) / ‖r • s-x‖^2) /
      (4 * Real.pi) := by
  simp only [green, norm_smul, Real.norm_eq_abs, hs, mul_one, sq_abs]

lemma radial_one (s x : ℂ) (hs : ‖s‖ = 1) (hx : ‖x‖ < 1) :
    Tendsto (fun r : ℝ => green (r • s) x / (1-r)) (𝓝[<] 1) (𝓝 (poisson s x)) := by
  have hne : s-x ≠ 0 := by
    intro h
    have he : s = x := sub_eq_zero.mp h
    rw [← he, hs] at hx
    exact lt_irrefl _ hx
  have hden : ‖s-x‖^2 ≠ 0 := pow_ne_zero _ (norm_ne_zero_iff.mpr hne)
  have hd : ContinuousAt (fun r : ℝ => ‖r • s-x‖^2) 1 := by fun_prop
  have hb : Tendsto (fun r : ℝ => (1+r)*(1-‖x‖^2)/‖r • s-x‖^2)
      (𝓝[<] 1) (𝓝 (2*(1-‖x‖^2)/‖s-x‖^2)) := by
    have hh : ContinuousAt (fun r : ℝ => (1+r)*(1-‖x‖^2)/‖r • s-x‖^2) 1 :=
      ((continuousAt_const.add continuousAt_id).mul continuousAt_const).div hd
        (by simpa using hden)
    convert hh.tendsto.mono_left nhdsWithin_le_nhds using 1; norm_num
  have ha : Tendsto (fun r : ℝ => 1-r) (𝓝[<] 1) (𝓝 0) := by
    simpa using (tendsto_const_nhds.sub (tendsto_id.mono_left nhdsWithin_le_nhds) :
      Tendsto (fun r : ℝ => 1-r) (𝓝[<] 1) (𝓝 (1-1)))
  have hevent : ∀ᶠ r : ℝ in 𝓝[<] 1, (1-r) ≠ 0 := by
    filter_upwards [self_mem_nhdsWithin] with r hr
    exact (sub_pos.mpr hr).ne'
  have ht := (tendsto_log_mul_div ha hb hevent).div_const (4*Real.pi)
  have hv : 2*(1-‖x‖^2)/‖s-x‖^2/(4*Real.pi) = poisson s x := by
    simp only [poisson]
    field_simp
    ring
  rw [hv] at ht
  apply ht.congr'
  exact Filter.Eventually.of_forall fun r => by
    dsimp only
    rw [radial_one_formula s x hs]
    have he : (1-r^2)*(1-‖x‖^2)/‖r • s-x‖^2 =
        (1-r)*((1+r)*(1-‖x‖^2)/‖r • s-x‖^2) := by ring
    rw [he]
    ring

lemma radial_two_formula (s t : ℂ) (hs : ‖s‖ = 1) (ht : ‖t‖ = 1) (r : ℝ) :
    green (r • s) (r • t) =
      Real.log (1 + (1-r^2)^2 / (r^2 * ‖s-t‖^2)) / (4*Real.pi) := by
  simp only [green, ← smul_sub, norm_smul, Real.norm_eq_abs, hs, ht, mul_one,
    mul_pow, sq_abs, ← pow_two]

lemma radial_two (s t : ℂ) (hs : ‖s‖ = 1) (ht : ‖t‖ = 1) (hne : s ≠ t) :
    Tendsto (fun r : ℝ => green (r • s) (r • t) / (1-r)^2)
      (𝓝[<] 1) (𝓝 (boundaryInteraction s t)) := by
  have hden : ‖s-t‖^2 ≠ 0 := pow_ne_zero _ (norm_ne_zero_iff.mpr (sub_ne_zero.mpr hne))
  have hb : Tendsto (fun r : ℝ => (1+r)^2 / (r^2 * ‖s-t‖^2))
      (𝓝[<] 1) (𝓝 (4 / ‖s-t‖^2)) := by
    have hh : ContinuousAt (fun r : ℝ => (1+r)^2 / (r^2 * ‖s-t‖^2)) 1 := by
      apply ContinuousAt.div (by fun_prop) (by fun_prop)
      simpa using hden
    convert hh.tendsto.mono_left nhdsWithin_le_nhds using 1; norm_num
  have ha : Tendsto (fun r : ℝ => (1-r)^2) (𝓝[<] 1) (𝓝 0) := by
    have hh : ContinuousAt (fun r : ℝ => (1-r)^2) 1 := by fun_prop
    convert hh.tendsto.mono_left nhdsWithin_le_nhds using 1; norm_num
  have hevent : ∀ᶠ r : ℝ in 𝓝[<] 1, (1-r)^2 ≠ 0 := by
    filter_upwards [self_mem_nhdsWithin] with r hr
    exact pow_ne_zero _ (sub_pos.mpr hr).ne'
  have hz := (tendsto_log_mul_div ha hb hevent).div_const (4*Real.pi)
  have hv : 4/‖s-t‖^2/(4*Real.pi) = boundaryInteraction s t := by
    simp only [boundaryInteraction]
    field_simp
  rw [hv] at hz
  apply hz.congr'
  exact Filter.Eventually.of_forall fun r => by
    dsimp only
    rw [radial_two_formula s t hs ht]
    have he : (1-r^2)^2/(r^2*‖s-t‖^2) =
        (1-r)^2*((1+r)^2/(r^2*‖s-t‖^2)) := by ring
    rw [he]
    ring

end StrictHotSpots.DiskFormula




open MeasureTheory Filter
open scoped Topology ENNReal


namespace StrictHotSpots.DiskFormula
open MeasureTheory

lemma log_mul_div_bounds {a b : ℝ} (ha : 0 < a) (hb : 0 ≤ b) :
    0 ≤ Real.log (1+a*b)/a ∧ Real.log (1+a*b)/a ≤ b := by
  have h1 : 1 ≤ 1+a*b := by linarith [mul_nonneg ha.le hb]
  constructor
  · exact div_nonneg (Real.log_nonneg h1) ha.le
  · apply (div_le_iff₀ ha).mpr
    have hh := Real.log_le_sub_one_of_pos (lt_of_lt_of_le zero_lt_one h1)
    nlinarith

lemma radial_one_bounded (s x : ℂ) (hs : ‖s‖ = 1)
    {R rmin r : ℝ} (hR : ‖x‖ ≤ R) (hRmin : R < rmin)
    (hrmin : rmin ≤ r) (hr : r < 1) :
    0 ≤ green (r • s) x / (1-r) ∧
      green (r • s) x / (1-r) ≤ (2/(rmin-R)^2)/(4*Real.pi) := by
  have hR0 : 0 ≤ R := (norm_nonneg x).trans hR
  have hr0 : 0 ≤ r := hR0.trans (hRmin.le.trans hrmin)
  have hx1 : ‖x‖ < 1 := hR.trans_lt (hRmin.trans_le hrmin |>.trans hr)
  have hx2 : 0 ≤ 1-‖x‖^2 := by nlinarith [norm_nonneg x]
  have hδ : 0 < rmin-R := sub_pos.mpr hRmin
  have hdist : rmin-R ≤ ‖r • s-x‖ := by
    have hh := norm_sub_norm_le (r • s) x
    rw [norm_smul, Real.norm_eq_abs, abs_of_nonneg hr0, hs, mul_one] at hh
    linarith
  have hdist0 : 0 < ‖r • s-x‖ := hδ.trans_le hdist
  have hd2 : (rmin-R)^2 ≤ ‖r • s-x‖^2 := sq_le_sq₀ hδ.le (norm_nonneg _) |>.mpr hdist
  have hnum : (1+r)*(1-‖x‖^2) ≤ 2 := by nlinarith [sq_nonneg ‖x‖]
  have hb0 : 0 ≤ (1+r)*(1-‖x‖^2)/‖r • s-x‖^2 := by positivity
  have hb : (1+r)*(1-‖x‖^2)/‖r • s-x‖^2 ≤ 2/(rmin-R)^2 := by
    calc
      _ ≤ 2/‖r • s-x‖^2 := div_le_div_of_nonneg_right hnum (sq_nonneg _)
      _ ≤ _ := div_le_div_of_nonneg_left (by norm_num) (sq_pos_of_pos hδ) hd2
  have hlog := log_mul_div_bounds (sub_pos.mpr hr) hb0
  have he : green (r • s) x/(1-r) =
      (Real.log (1 + (1-r)*((1+r)*(1-‖x‖^2)/‖r • s-x‖^2))/(1-r))/(4*Real.pi) := by
    rw [radial_one_formula s x hs]
    have hh : (1-r^2)*(1-‖x‖^2)/‖r • s-x‖^2 =
        (1-r)*((1+r)*(1-‖x‖^2)/‖r • s-x‖^2) := by ring
    rw [hh]
    ring
  rw [he]
  exact ⟨div_nonneg hlog.1 (by positivity),
    div_le_div_of_nonneg_right (hlog.2.trans hb) (by positivity)⟩

end StrictHotSpots.DiskFormula

namespace StrictHotSpots.DiskFormula
open MeasureTheory
variable {X : Type*} [MeasurableSpace X] (ν : Measure X) [IsFiniteMeasure ν]

lemma poisson_nonneg (s x : ℂ) (hx : ‖x‖ < 1) : 0 ≤ poisson s x := by
  have hh : 0 ≤ 1-‖x‖^2 := by nlinarith [norm_nonneg x]
  exact div_nonneg hh (by positivity)

lemma poisson_bounded (s x : ℂ) (hs : ‖s‖ = 1)
    {R : ℝ} (hR : ‖x‖ ≤ R) (hR1 : R < 1) :
    poisson s x ≤ 1 / (2*Real.pi*(1-R)^2) := by
  have hh := norm_sub_norm_le s x
  rw [hs] at hh
  have hd : 1-R ≤ ‖s-x‖ := by linarith
  have hd0 : 0 < 1-R := sub_pos.mpr hR1
  have hd2 : (1-R)^2 ≤ ‖s-x‖^2 := (sq_le_sq₀ hd0.le (norm_nonneg _)).mpr hd
  have hn : 1-‖x‖^2 ≤ 1 := by nlinarith [sq_nonneg ‖x‖]
  unfold poisson
  calc
    _ ≤ 1/(2*Real.pi*‖s-x‖^2) := div_le_div_of_nonneg_right hn (by positivity)
    _ ≤ _ := div_le_div_of_nonneg_left (by norm_num) (by positivity)
      (mul_le_mul_of_nonneg_left hd2 (by positivity))

lemma poisson_memLp (s : ℂ) (hs : ‖s‖ = 1) (x : X → ℂ) (hm : Measurable x)
    {R : ℝ} (hR : ∀ z, ‖x z‖ ≤ R) (hR1 : R < 1) :
    MemLp (fun z => poisson s (x z)) 2 ν := by
  refine MemLp.of_bound ?_ (1 / (2*Real.pi*(1-R)^2)) ?_
  · apply Measurable.aestronglyMeasurable
    unfold poisson
    fun_prop
  · exact Filter.Eventually.of_forall fun z => by
      rw [Real.norm_eq_abs, abs_of_nonneg (poisson_nonneg s (x z) ((hR z).trans_lt hR1))]
      exact poisson_bounded s (x z) hs (hR z) hR1

lemma radial_one_memLp (s : ℂ) (hs : ‖s‖ = 1) (x : X → ℂ) (hm : Measurable x)
    {R rmin r : ℝ} (hR : ∀ z, ‖x z‖ ≤ R) (hRmin : R < rmin)
    (hrmin : rmin ≤ r) (hr : r < 1) :
    MemLp (fun z => green (r • s) (x z)/(1-r)) 2 ν := by
  refine MemLp.of_bound ?_ ((2/(rmin-R)^2)/(4*Real.pi)) ?_
  · apply Measurable.aestronglyMeasurable
    unfold green
    fun_prop
  · exact Filter.Eventually.of_forall fun z => by
      obtain ⟨h0,hb⟩ := radial_one_bounded s (x z) hs (hR z) hRmin hrmin hr
      simpa only [Real.norm_eq_abs, abs_of_nonneg h0] using hb



theorem radial_one_L2 (s : ℂ) (hs : ‖s‖ = 1) (x : X → ℂ) (hm : Measurable x)
    {R rmin : ℝ} (hR : ∀ z, ‖x z‖ ≤ R) (hRmin : R < rmin) (hrmin1 : rmin < 1)
    (r : ℕ → ℝ) (hrmin : ∀ n, rmin ≤ r n) (hr : ∀ n, r n < 1)
    (hlim : Tendsto r atTop (𝓝 1)) :
    Tendsto (fun n => (radial_one_memLp ν s hs x hm hR hRmin (hrmin n) (hr n)).toLp
      (fun z => green (r n • s) (x z) / (1-r n))) atTop
      (𝓝 ((poisson_memLp ν s hs x hm hR (hRmin.trans hrmin1)).toLp
        (fun z => poisson s (x z)))) := by
  apply L2Limits.tendsto_toLp_of_dominated
    (hb := memLp_const ((2/(rmin-R)^2)/(4*Real.pi)))
  · apply Filter.Eventually.of_forall
    intro n
    exact Filter.Eventually.of_forall fun z => by
      obtain ⟨h0,hb⟩ := radial_one_bounded s (x z) hs (hR z) hRmin (hrmin n) (hr n)
      simpa only [Real.norm_eq_abs, abs_of_nonneg h0] using hb
  · exact Filter.Eventually.of_forall fun z =>
      (radial_one s (x z) hs ((hR z).trans_lt (hRmin.trans hrmin1))).comp
        (tendsto_nhdsWithin_iff.mpr ⟨hlim, Filter.Eventually.of_forall fun n => hr n⟩)

end StrictHotSpots.DiskFormula





open MeasureTheory Filter Metric Set
open scoped Topology ENNReal
namespace StrictHotSpots.GreenLp

def logPole (z : ℂ) : ℝ := Real.log (1 + 1 / ‖z‖^2)

lemma logPole_nonneg (z : ℂ) : 0 ≤ logPole z := by
  apply Real.log_nonneg
  have : 0 ≤ 1 / ‖z‖^2 := one_div_nonneg.mpr (sq_nonneg _)
  linarith

lemma measurable_logPole : Measurable logPole := by
  unfold logPole
  fun_prop

lemma logPole_cube_bound {t : ℝ} (ht : 0 < t) (ht2 : t ≤ 2) :
    (Real.log (1 + 1/t^2))^3 ≤ 648 / t := by
  have harg : 1 + 1/t^2 ≤ (3/t)^2 := by
    rw [div_pow]
    apply (le_div_iff₀ (sq_pos_of_pos ht)).2
    field_simp
    nlinarith
  have hl : Real.log (1 + 1/t^2) ≤ 2 * Real.log (3/t) := by
    have hh := Real.log_le_log (by positivity : 0 < 1 + 1/t^2) harg
    simpa only [Real.log_pow, Nat.cast_ofNat] using hh
  have hp : 0 < 3/t := by positivity
  have hr := Real.log_le_rpow_div hp.le (show 0 < (3:ℝ)⁻¹ by norm_num)
  have he : Real.log (1 + 1/t^2) ≤ 6 * (3/t)^((3:ℝ)⁻¹) := by
    norm_num at hr
    linarith
  have hnon : 0 ≤ Real.log (1 + 1/t^2) := by
    apply Real.log_nonneg
    have := one_div_nonneg.mpr (sq_nonneg t)
    linarith
  have hh := pow_le_pow_left₀ hnon he 3
  have hh3 : ((3/t)^((3:ℝ)⁻¹))^3 = 3/t := by
    simpa only [Nat.cast_ofNat] using
      Real.rpow_inv_natCast_pow hp.le (show (3:ℕ) ≠ 0 by decide)
  rw [mul_pow, hh3] at hh
  calc
    _ ≤ 6^3 * (3/t) := hh
    _ = 648/t := by ring

lemma logPole_cube_integrable : IntegrableOn (fun z : ℂ => (logPole z)^3) (ball 0 2) := by
  apply integrableOn_ball_of_norm_le_rpow (C := 648) (α := 1)
    (by norm_num : 1 ≤ Module.finrank ℝ ℂ) (by norm_num : (1:ℝ) < Module.finrank ℝ ℂ)
  · filter_upwards [self_mem_ae_restrict measurableSet_ball] with z hz
    rw [Real.norm_of_nonneg (pow_nonneg (logPole_nonneg z) _), Real.rpow_neg_one]
    by_cases hzero : z = 0
    · simp [hzero, logPole]
    · have hp := norm_pos_iff.mpr hzero
      have hlt : ‖z‖ < 2 := by simpa using hz
      simpa only [logPole, div_eq_mul_inv] using logPole_cube_bound hp hlt.le
  · exact (measurable_logPole.pow_const 3).aestronglyMeasurable

lemma logPole_memLp_three : MemLp logPole 3 (volume.restrict (ball 0 2)) := by
  apply (integrable_norm_rpow_iff measurable_logPole.aestronglyMeasurable
    (by norm_num : (3:ℝ≥0∞) ≠ 0) (by simp : (3:ℝ≥0∞) ≠ (∞ : ℝ≥0∞))).1
  simpa only [IntegrableOn, ENNReal.toReal_ofNat, ← Real.rpow_natCast, Nat.cast_ofNat,
    Real.norm_of_nonneg (logPole_nonneg _)] using logPole_cube_integrable

lemma shifted_logPole_memLp_three (x : ℂ) (hx : ‖x‖ < 1) :
    MemLp (fun y => logPole (x-y)) 3 (volume.restrict (ball 0 1)) := by
  have hpres := (measurePreserving_sub_right (volume : Measure ℂ) x).restrict_preimage
    (show MeasurableSet (ball (0:ℂ) 2) from measurableSet_ball)
  have hh := logPole_memLp_three.comp_measurePreserving hpres
  have hsub : ball (0:ℂ) 1 ⊆ (fun y => y-x) ⁻¹' ball 0 2 := by
    intro y hy
    have hy' : ‖y‖ < 1 := by simpa using hy
    have hd := norm_sub_le y x
    change y-x ∈ ball (0:ℂ) 2
    rw [mem_ball_zero_iff]
    linarith
  have hm := MemLp.mono_measure (Measure.restrict_mono hsub le_rfl) hh
  convert hm using 1
  funext y
  dsimp only [Function.comp_def, logPole]
  rw [norm_sub_rev]

end StrictHotSpots.GreenLp


open MeasureTheory Filter Metric Set
open scoped Topology ENNReal
namespace StrictHotSpots.DiskFormula
open GreenLp

lemma measurable_green_left (x : ℂ) : Measurable (green x) := by
  unfold green
  fun_prop

lemma green_bounds {x y : ℂ} (hx : ‖x‖ ≤ 1) (hy : ‖y‖ ≤ 1) :
    0 ≤ green x y ∧ green x y ≤ logPole (x-y) / (4*Real.pi) := by
  have hwx : 0 ≤ 1-‖x‖^2 := by nlinarith [norm_nonneg x]
  have hwy : 0 ≤ 1-‖y‖^2 := by nlinarith [norm_nonneg y]
  have hn : (1-‖x‖^2)*(1-‖y‖^2) ≤ 1 := by
    nlinarith [sq_nonneg ‖x‖, sq_nonneg ‖y‖, mul_nonneg hwx (sq_nonneg ‖y‖)]
  have hdiv : 0 ≤ (1-‖x‖^2)*(1-‖y‖^2)/‖x-y‖^2 := by positivity
  have hdiv' := div_le_div_of_nonneg_right hn (sq_nonneg ‖x-y‖)
  have hlog : 0 ≤ Real.log (1+(1-‖x‖^2)*(1-‖y‖^2)/‖x-y‖^2) :=
    Real.log_nonneg (by linarith)
  refine ⟨div_nonneg hlog (by positivity), ?_⟩
  apply div_le_div_of_nonneg_right _ (by positivity : 0 ≤ 4*Real.pi)
  exact Real.log_le_log (by linarith) (by simpa only [logPole] using add_le_add_right hdiv' 1)

lemma green_pos {x y : ℂ} (hx : ‖x‖ < 1) (hy : ‖y‖ < 1) (hne : x ≠ y) :
    0 < green x y := by
  apply div_pos _ (by positivity)
  apply Real.log_pos
  have hwx : 0 < 1-‖x‖^2 := by nlinarith [norm_nonneg x]
  have hwy : 0 < 1-‖y‖^2 := by nlinarith [norm_nonneg y]
  have hd : 0 < ‖x-y‖^2 := sq_pos_of_pos (norm_pos_iff.mpr (sub_ne_zero.mpr hne))
  have := div_pos (mul_pos hwx hwy) hd
  linarith

lemma green_memLp_three (x : ℂ) (hx : ‖x‖ < 1) :
    MemLp (green x) 3 (volume.restrict (ball 0 1)) := by
  have hm : MemLp (fun y => logPole (x-y)/(4*Real.pi)) 3
      (volume.restrict (ball 0 1)) := by
    simpa only [div_eq_mul_inv] using
      (shifted_logPole_memLp_three x hx).mul_const (4*Real.pi)⁻¹
  apply hm.mono (measurable_green_left x).aestronglyMeasurable
  filter_upwards [self_mem_ae_restrict measurableSet_ball] with y hy
  have hy' : ‖y‖ ≤ 1 := (by simpa using hy : ‖y‖ < 1).le
  have hb := green_bounds hx.le hy'
  rw [Real.norm_of_nonneg hb.1, Real.norm_of_nonneg (div_nonneg (logPole_nonneg (x-y)) (by positivity :
    0 ≤ 4*Real.pi))]
  exact hb.2

lemma green_memLp_two (x : ℂ) (hx : ‖x‖ < 1) :
    MemLp (green x) 2 (volume.restrict (ball 0 1)) := by
  have : IsFiniteMeasure (volume.restrict (ball (0:ℂ) 1)) :=
    ⟨by simpa only [Measure.restrict_apply_univ] using measure_ball_lt_top (x := (0:ℂ)) (r := 1)⟩
  exact (green_memLp_three x hx).mono_exponent (by norm_num)

lemma green_memLp_three_of_le (ν : Measure ℂ) {A : ℝ≥0∞} (hA : A ≠ (∞ : ℝ≥0∞))
    (hν : ν ≤ A • volume.restrict (ball 0 1)) (x : ℂ) (hx : ‖x‖ < 1) :
    MemLp (green x) 3 ν :=
  MemLp.mono_measure hν ((green_memLp_three x hx).smul_measure hA)

lemma green_memLp_two_of_le (ν : Measure ℂ) {A : ℝ≥0∞} (hA : A ≠ (∞ : ℝ≥0∞))
    (hν : ν ≤ A • volume.restrict (ball 0 1)) (x : ℂ) (hx : ‖x‖ < 1) :
    MemLp (green x) 2 ν :=
  MemLp.mono_measure hν ((green_memLp_two x hx).smul_measure hA)

end StrictHotSpots.DiskFormula


open MeasureTheory Filter Metric Set
open scoped Topology ENNReal
namespace StrictHotSpots.GreenLp

lemma logPole_memLp_two : MemLp logPole 2 (volume.restrict (ball (0 : ℂ) 2)) := by
  let : IsFiniteMeasure (volume.restrict (ball (0 : ℂ) 2)) :=
    isFiniteMeasure_restrict.mpr Metric.isBounded_ball.measure_lt_top.ne
  exact logPole_memLp_three.mono_exponent (by norm_num)

lemma shifted_logPole_norm_bound (x : ℂ) (hx : ‖x‖ < 1) :
    eLpNorm (fun y => logPole (x-y)) 2 (volume.restrict (ball 0 1)) ≤
      eLpNorm logPole 2 (volume.restrict (ball (0 : ℂ) 2)) := by
  have hm := (measurePreserving_sub_right (volume : Measure ℂ) x).restrict_preimage
    (show MeasurableSet (ball (0 : ℂ) 2) from measurableSet_ball)
  have hs : ball (0 : ℂ) 1 ⊆ (fun y => y-x) ⁻¹' ball 0 2 := by
    intro y hy
    have hy' : ‖y‖ < 1 := by simpa using hy
    have hd := norm_sub_le y x
    change y-x ∈ ball (0 : ℂ) 2
    rw [mem_ball_zero_iff]
    linarith
  calc
    _ = eLpNorm (logPole ∘ fun y => y-x) 2 (volume.restrict (ball 0 1)) := by
      congr 1
      funext y
      simp only [Function.comp_apply, logPole, norm_sub_rev x y]
    _ ≤ eLpNorm (logPole ∘ fun y => y-x) 2
        (volume.restrict ((fun y => y-x) ⁻¹' ball 0 2)) :=
      eLpNorm_mono_measure _ (Measure.restrict_mono hs le_rfl)
    _ = _ := eLpNorm_comp_measurePreserving logPole_memLp_two.aestronglyMeasurable hm

end StrictHotSpots.GreenLp
namespace StrictHotSpots.DiskFormula
open GreenLp

lemma green_uniform_L2_bound : ∃ B : ℝ, 0 ≤ B ∧ ∀ x : ℂ, ‖x‖ < 1 →
    (eLpNorm (green x) 2 (volume.restrict (ball 0 1))).toReal ≤ B := by
  let C := ENNReal.ofReal (4*Real.pi)⁻¹ *
    eLpNorm logPole 2 (volume.restrict (ball (0 : ℂ) 2))
  have hC : C ≠ (∞ : ℝ≥0∞) := ENNReal.mul_ne_top ENNReal.ofReal_ne_top logPole_memLp_two.eLpNorm_ne_top
  refine ⟨C.toReal, ENNReal.toReal_nonneg, fun x hx => ?_⟩
  apply ENNReal.toReal_mono hC
  calc
    _ ≤ ENNReal.ofReal (4*Real.pi)⁻¹ *
        eLpNorm (fun y => logPole (x-y)) 2 (volume.restrict (ball 0 1)) := by
      apply eLpNorm_le_mul_eLpNorm_of_ae_le_mul
        (measurable_green_left x).aestronglyMeasurable
      filter_upwards [ae_restrict_mem measurableSet_ball] with y hy
      have hy' : ‖y‖ ≤ 1 := (by simpa using hy : ‖y‖ < 1).le
      have hh := green_bounds hx.le hy'
      rw [Real.norm_of_nonneg hh.1, Real.norm_of_nonneg (logPole_nonneg _)]
      simpa only [div_eq_mul_inv, mul_comm] using hh.2
    _ ≤ C := by
      dsimp [C]
      gcongr
      exact shifted_logPole_norm_bound x hx

end StrictHotSpots.DiskFormula



open MeasureTheory Filter
open scoped ENNReal Topology InnerProductSpace
namespace StrictHotSpots.MeasurableL2Kernel

lemma integral_mul_eq_inner {X : Type*} [MeasurableSpace X] {ν : Measure X}
    {f g : X → ℝ} (hf : MemLp f 2 ν) (hg : MemLp g 2 ν) :
    (∫ x, f x * g x ∂ν) = inner ℝ (hf.toLp f) (hg.toLp g) := by
  rw [L2.inner_def]
  apply integral_congr_ae
  filter_upwards [hf.coeFn_toLp, hg.coeFn_toLp] with x hx hy
  simp only [hx,hy, RCLike.inner_apply, conj_trivial]
  ring

lemma norm_integral_mul_le {X : Type*} [MeasurableSpace X] {ν : Measure X}
    {f g : X → ℝ} (hf : MemLp f 2 ν) (hg : MemLp g 2 ν) :
    ‖∫ x, f x * g x ∂ν‖ ≤ (eLpNorm f 2 ν).toReal * (eLpNorm g 2 ν).toReal := by
  rw [integral_mul_eq_inner hf hg]
  simpa only [Lp.norm_toLp] using norm_inner_le_norm (hf.toLp f) (hg.toLp g)

variable {X Y : Type*} [MeasurableSpace X] [MeasurableSpace Y]
  {μ : Measure X} {ν : Measure Y} [IsFiniteMeasure μ] [IsFiniteMeasure ν]
  (K : X × Y → ℝ) (hK : AEStronglyMeasurable K (μ.prod ν))
  (hrow : ∀ᵐ x ∂μ, MemLp (fun y => K (x,y)) 2 ν)
  {B : ℝ} (hB : 0 ≤ B)
  (hbound : ∀ᵐ x ∂μ, (eLpNorm (fun y => K (x,y)) 2 ν).toReal ≤ B)

omit [IsFiniteMeasure μ] [IsFiniteMeasure ν] in
include hrow hbound in
lemma apply_bound (f : Lp ℝ 2 ν) :
    ∀ᵐ x ∂μ, ‖∫ y, K (x,y) * f y ∂ν‖ ≤ B * ‖f‖ := by
  filter_upwards [hrow, hbound] with x hx hb
  have hh := norm_integral_mul_le hx (Lp.memLp f)
  have he : (eLpNorm (fun y => f y) 2 ν).toReal = ‖f‖ := rfl
  rw [he] at hh
  exact hh.trans (mul_le_mul_of_nonneg_right hb (norm_nonneg f))

include hK hrow hbound in
lemma apply_memLp (f : Lp ℝ 2 ν) :
    MemLp (fun x => ∫ y, K (x,y) * f y ∂ν) 2 μ := by
  have hm : AEStronglyMeasurable (fun z : X × Y => K z * f z.2) (μ.prod ν) :=
    hK.mul (Lp.aestronglyMeasurable f).comp_snd
  exact MemLp.of_bound hm.integral_prod_right' (B * ‖f‖)
    (apply_bound K hrow hbound f)



def operator : Lp ℝ 2 ν →L[ℝ] Lp ℝ 2 μ := by
  let T : Lp ℝ 2 ν →ₗ[ℝ] Lp ℝ 2 μ :=
    { toFun := fun f => (apply_memLp K hK hrow hbound f).toLp _
      map_add' := by
        intro f g
        apply Lp.ext
        filter_upwards [(apply_memLp K hK hrow hbound (f+g)).coeFn_toLp,
          (apply_memLp K hK hrow hbound f).coeFn_toLp,
          (apply_memLp K hK hrow hbound g).coeFn_toLp,
          Lp.coeFn_add ((apply_memLp K hK hrow hbound f).toLp _)
            ((apply_memLp K hK hrow hbound g).toLp _), hrow] with x hx hf hg ha hr
        rw [hx,ha, Pi.add_apply,hf,hg]
        have he : (fun y => K (x,y) * (f+g) y) =ᵐ[ν]
            (fun y => K (x,y) * f y + K (x,y) * g y) := by
          filter_upwards [Lp.coeFn_add f g] with y hy
          rw [hy, Pi.add_apply, mul_add]
        rw [integral_congr_ae he, integral_add]
        · exact hr.integrable_mul (Lp.memLp f)
        · exact hr.integrable_mul (Lp.memLp g)
      map_smul' := by
        intro c f
        apply Lp.ext
        filter_upwards [(apply_memLp K hK hrow hbound (c • f)).coeFn_toLp,
          (apply_memLp K hK hrow hbound f).coeFn_toLp,
          Lp.coeFn_smul c ((apply_memLp K hK hrow hbound f).toLp _)] with x hx hf hs
        simp only [RingHom.id_apply]
        rw [hx,hs, Pi.smul_apply,hf]
        have he : (fun y => K (x,y) * (c • f) y) =ᵐ[ν]
            (fun y => c * (K (x,y) * f y)) := by
          filter_upwards [Lp.coeFn_smul c f] with y hy
          rw [hy, Pi.smul_apply, smul_eq_mul]
          ring
        rw [integral_congr_ae he, integral_const_mul, smul_eq_mul] }
  refine T.mkContinuous ((measureUnivNNReal μ : ℝ) ^ (2 : ℝ≥0∞).toReal⁻¹ * B) ?_
  intro f
  have hh := Lp.norm_le_of_ae_bound (mul_nonneg hB (norm_nonneg f))
    (show ∀ᵐ x ∂μ, ‖T f x‖ ≤ B * ‖f‖ from by
      filter_upwards [(apply_memLp K hK hrow hbound f).coeFn_toLp,
        apply_bound K hrow hbound f] with x hx hb
      change ‖((apply_memLp K hK hrow hbound f).toLp _) x‖ ≤ _
      rw [hx]
      exact hb)
  exact hh.trans_eq (mul_assoc _ _ _).symm

lemma operator_apply_ae (f : Lp ℝ 2 ν) :
    operator K hK hrow hB hbound f =ᵐ[μ] fun x => ∫ y, K (x,y) * f y ∂ν :=
  (apply_memLp K hK hrow hbound f).coeFn_toLp

end StrictHotSpots.MeasurableL2Kernel



open MeasureTheory Filter InnerProductSpace
open scoped ENNReal Topology
namespace StrictHotSpots

lemma eq_zero_of_inner_smoothTest_eq_zero {Ω : Set Plane} (hΩ : IsOpen Ω)
    (u : Lp ℝ 2 (volume.restrict Ω))
    (h : ∀ φ : smoothTestSpace Ω, inner ℝ (H1.value (smoothTestToH1 hΩ φ)) u = 0) :
    u = 0 := by
  have hl := (Lp.memLp u).locallyIntegrable (by norm_num : (1 : ℝ≥0∞) ≤ 2)
  have hh := hΩ.ae_eq_zero_of_integral_contDiff_smul_eq_zero
    (μ := volume.restrict Ω) (hl.locallyIntegrableOn Ω)
    (fun φ hp hc hs => show (∫ x in Ω, φ x • u x) = 0 from by
      have ht := h ⟨φ,hp,hc,hs⟩
      have hv : H1.value (smoothTestToH1 hΩ ⟨φ,hp,hc,hs⟩) =
          (test_memLp (Ω := Ω) hp hc).toLp φ := rfl
      rw [hv, L2.inner_def] at ht
      convert ht using 1
      apply integral_congr_ae
      filter_upwards [(test_memLp (Ω := Ω) hp hc).coeFn_toLp] with x hx
      simp only [hx, smul_eq_mul, RCLike.inner_apply, conj_trivial]
      ring)
  apply Lp.ext
  filter_upwards [hh, ae_restrict_mem hΩ.measurableSet, Lp.coeFn_zero ℝ 2 (volume.restrict Ω)]
    with x hx hΩx hz
  rw [hz]
  exact hx hΩx

lemma smoothTest_dense_L2 {Ω : Set Plane} (hΩ : IsOpen Ω) :
    DenseRange (fun φ : smoothTestSpace Ω => H1.value (smoothTestToH1 hΩ φ)) := by
  let T : smoothTestSpace Ω →ₗ[ℝ] Lp ℝ 2 (volume.restrict Ω) :=
    H1.value.toLinearMap.comp (smoothTestToH1 hΩ)
  have hz : T.rangeᗮ = ⊥ := by
    apply eq_bot_iff.mpr
    intro u hu
    change u = 0
    apply eq_zero_of_inner_smoothTest_eq_zero hΩ u
    intro φ
    exact (Submodule.mem_orthogonal _ _).mp hu (T φ) ⟨φ,rfl⟩
  have ht : T.range.topologicalClosure = ⊤ := by
    rw [← Submodule.orthogonal_orthogonal_eq_closure, hz, Submodule.bot_orthogonal_eq_top]
  have hd : closure (Set.range T) = Set.univ := by
    change closure (T.range : Set (Lp ℝ 2 (volume.restrict Ω))) = Set.univ
    rw [← Submodule.topologicalClosure_coe, ht, Submodule.top_coe]
  exact dense_iff_closure_eq.mpr hd

end StrictHotSpots







open scoped InnerProductSpace
namespace StrictHotSpots.DirichletOperator

variable {V H : Type*}
  [NormedAddCommGroup V] [InnerProductSpace ℝ V] [CompleteSpace V]
  [NormedAddCommGroup H] [InnerProductSpace ℝ H] [CompleteSpace H]


def solution (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B) (J : V →L[ℝ] H) :
    H →L[ℝ] V :=
  hB.continuousLinearEquivOfBilin.symm.toContinuousLinearMap.comp J.adjoint

lemma solution_spec (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B) (J : V →L[ℝ] H)
    (f : H) (v : V) : B (solution B hB J f) v = ⟪f, J v⟫_ℝ := by
  rw [← hB.continuousLinearEquivOfBilin_apply]
  simp only [solution, ContinuousLinearMap.comp_apply, ContinuousLinearEquiv.coe_coe,
    ContinuousLinearEquiv.apply_symm_apply, ContinuousLinearMap.adjoint_inner_left]


def green (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B) (J : V →L[ℝ] H) :
    H →L[ℝ] H := J.comp (solution B hB J)

lemma green_apply (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B) (J : V →L[ℝ] H)
    (f : H) : green B hB J f = J (solution B hB J f) := rfl

lemma green_symmetric (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B)
    (hsym : ∀ u v, B u v = B v u) (J : V →L[ℝ] H) (f g : H) :
    ⟪green B hB J f, g⟫_ℝ = ⟪f, green B hB J g⟫_ℝ := by
  calc
    _ = ⟪g, J (solution B hB J f)⟫_ℝ := real_inner_comm _ _
    _ = B (solution B hB J g) (solution B hB J f) := (solution_spec B hB J g _).symm
    _ = B (solution B hB J f) (solution B hB J g) := hsym _ _
    _ = _ := solution_spec B hB J f _

lemma green_nonneg (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B)
    (J : V →L[ℝ] H) (f : H) : 0 ≤ ⟪f, green B hB J f⟫_ℝ := by
  rw [green_apply, ← solution_spec B hB J f (solution B hB J f)]
  obtain ⟨C, hC, h⟩ := hB
  exact (mul_nonneg (mul_nonneg hC.le (norm_nonneg _)) (norm_nonneg _)).trans (h _)



lemma green_norm_le (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B)
    (J : V →L[ℝ] H) {d : ℝ} (hd : 0 ≤ d)
    (hsub : ∀ v : V, ‖J v‖ ^ 2 ≤ d * B v v) : ‖green B hB J‖ ≤ d := by
  apply ContinuousLinearMap.opNorm_le_bound _ hd
  intro f
  let u := solution B hB J f
  have h0 := hsub u
  have hs : B u u = ⟪f, J u⟫_ℝ := solution_spec B hB J f u
  rw [hs] at h0
  have hi : ⟪f, J u⟫_ℝ ≤ ‖f‖ * ‖J u‖ := real_inner_le_norm _ _
  have ht : ‖J u‖ ^ 2 ≤ d * (‖f‖ * ‖J u‖) :=
    h0.trans (mul_le_mul_of_nonneg_left hi hd)
  change ‖J u‖ ≤ d * ‖f‖
  by_cases hz : ‖J u‖ = 0
  · rw [hz]
    positivity
  · have hp : 0 < ‖J u‖ := lt_of_le_of_ne (norm_nonneg _) (Ne.symm hz)
    nlinarith

lemma green_norm_lt_one (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B)
    (J : V →L[ℝ] H) {d : ℝ} (hd : 0 ≤ d) (hd1 : d < 1)
    (hsub : ∀ v : V, ‖J v‖ ^ 2 ≤ d * B v v) : ‖green B hB J‖ < 1 :=
  (green_norm_le B hB J hd hsub).trans_lt hd1

end StrictHotSpots.DirichletOperator


open MeasureTheory Set Filter
open scoped ENNReal Topology InnerProductSpace
namespace StrictHotSpots.PlaneGreen
open DiskH10

lemma diskBounded : Bornology.IsBounded disk := Metric.isBounded_ball

instance diskFinite : IsFiniteMeasure (volume.restrict disk) :=
  isFiniteMeasure_restrict.mpr diskBounded.measure_lt_top.ne

def kernel (x y : Plane) : ℝ := DiskFormula.green (complexIso.symm x) (complexIso.symm y)

lemma complexIso_disk_preserving : MeasurePreserving complexIso.symm
    (volume.restrict disk) (volume.restrict (Metric.ball (0 : ℂ) 1)) := by
  have h := complexIso.symm.measurePreserving.restrict_preimage
    (show MeasurableSet (Metric.ball (0 : ℂ) 1) from measurableSet_ball)
  have he : complexIso.symm ⁻¹' Metric.ball (0 : ℂ) 1 = disk := by
    ext x
    simp [disk]
  rwa [he] at h

lemma kernel_memLp (x : Plane) (hx : x ∈ disk) :
    MemLp (kernel x) 2 (volume.restrict disk) := by
  have hx' : ‖complexIso.symm x‖ < 1 := by simpa [disk] using hx
  exact (DiskFormula.green_memLp_two _ hx').comp_measurePreserving complexIso_disk_preserving

lemma measurable_kernel : Measurable (fun p : Plane × Plane => kernel p.1 p.2) := by
  unfold kernel DiskFormula.green
  fun_prop

lemma kernel_bound : ∃ B : ℝ, 0 ≤ B ∧ ∀ x ∈ disk,
    (eLpNorm (kernel x) 2 (volume.restrict disk)).toReal ≤ B := by
  obtain ⟨B,hB,h⟩ := DiskFormula.green_uniform_L2_bound
  refine ⟨B,hB,fun x hx => ?_⟩
  have hx' : ‖complexIso.symm x‖ < 1 := by simpa [disk] using hx
  have he := eLpNorm_comp_measurePreserving (p := 2)
    (DiskFormula.green_memLp_two _ hx').aestronglyMeasurable complexIso_disk_preserving
  change (eLpNorm (DiskFormula.green (complexIso.symm x) ∘ complexIso.symm) 2
    (volume.restrict disk)).toReal ≤ B
  rw [he]
  exact h _ hx'

lemma kernel_rows : ∀ᵐ x ∂volume.restrict disk, MemLp (kernel x) 2 (volume.restrict disk) := by
  filter_upwards [ae_restrict_mem diskOpen.measurableSet] with x hx
  exact kernel_memLp x hx

def integralOperator : Lp ℝ 2 (volume.restrict disk) →L[ℝ] Lp ℝ 2 (volume.restrict disk) :=
  MeasurableL2Kernel.operator (fun p => kernel p.1 p.2) measurable_kernel.aestronglyMeasurable
    kernel_rows kernel_bound.choose_spec.1
    (by filter_upwards [ae_restrict_mem diskOpen.measurableSet] with x hx
        exact kernel_bound.choose_spec.2 x hx)

lemma integralOperator_ae (f : Lp ℝ 2 (volume.restrict disk)) :
    integralOperator f =ᵐ[volume.restrict disk] fun x => ∫ y in disk, kernel x y * f y :=
  MeasurableL2Kernel.operator_apply_ae _ _ _ _ _ f

lemma potential_eq_integral {f : Plane → ℝ} (hf : ContDiff ℝ ∞ f)
    (hc : HasCompactSupport f) (hs : tsupport f ⊆ disk) {x : Plane} (hx : x ∈ disk) :
    potential f x = ∫ y in disk, kernel x y * f y := by
  obtain ⟨R,hR,hn⟩ := GreenPotential.exists_regular_neighborhood (source_compact hc)
    (source_support hs)
  have hx' : complexIso.symm x ∈ Metric.ball (0 : ℂ) R := by
    have : ‖x‖ < 1 := by simpa [disk] using hx
    simpa using this.trans hR
  have hh := GreenPotential.potential_eq_integral (source_contDiff hf) (source_compact hc)
    Metric.isOpen_ball hn hx'
  change GreenPotential.potential (source f) (complexIso.symm x) = _
  rw [hh]
  have he := complexIso.symm.measurePreserving.integral_comp
    complexIso.symm.toMeasurableEquiv.measurableEmbedding
    (fun z => DiskFormula.green (complexIso.symm x) z * source f z)
  simp only [source, complexIso.apply_symm_apply] at he
  simp only [source]
  rw [← he]
  symm
  apply setIntegral_eq_integral_of_forall_compl_eq_zero
  intro y hy
  have hfy : f y = 0 := image_eq_zero_of_notMem_tsupport (fun h => hy (hs h))
  simp [hfy, kernel]

def variationalOperator : Lp ℝ 2 (volume.restrict disk) →L[ℝ] Lp ℝ 2 (volume.restrict disk) :=
  @DirichletOperator.green (H10 diskOpen) (Lp ℝ 2 (volume.restrict disk))
    (H10.normedAddCommGroup diskOpen) (H10.innerProductSpace diskOpen)
    (H10.completeSpace diskOpen) inferInstance inferInstance inferInstance
    (H10.energy diskOpen) (H10.energy_coercive diskOpen diskBounded) (H10.value diskOpen)

lemma variationalOperator_test (f : smoothTestSpace disk) :
    variationalOperator (H1.value (smoothTestToH1 diskOpen f)) =ᵐ[volume.restrict disk]
      potential f := by
  obtain ⟨u,hu,he⟩ := exists_dirichlet_representative f.property.1 f.property.2.1 f.property.2.2
  let w := DirichletOperator.solution (H10.energy diskOpen)
    (H10.energy_coercive diskOpen diskBounded) (H10.value diskOpen)
    (H1.value (smoothTestToH1 diskOpen f))
  have hw : ∀ v, H10.energy diskOpen w v = inner ℝ
      (H1.value (smoothTestToH1 diskOpen f)) (H10.value diskOpen v) :=
    DirichletOperator.solution_spec _ _ _ _
  have he' : ∀ v, H10.energy diskOpen u v = inner ℝ
      (H1.value (smoothTestToH1 diskOpen f)) (H10.value diskOpen v) := he
  obtain ⟨_,_,hun⟩ := H10.existsUnique_energy_solution diskOpen diskBounded
    ((innerSL ℝ (H1.value (smoothTestToH1 diskOpen f))).comp (H10.value diskOpen))
  have heq : w = u := (hun w hw).trans (hun u he').symm
  change H10.value diskOpen w =ᵐ[volume.restrict disk] potential f
  rw [heq]
  exact hu



lemma integralOperator_eq_variationalOperator : integralOperator = variationalOperator := by
  apply DFunLike.coe_injective
  apply (smoothTest_dense_L2 diskOpen).equalizer integralOperator.continuous
    variationalOperator.continuous
  funext f
  simp only [Function.comp_apply]
  apply Lp.ext
  filter_upwards [integralOperator_ae (H1.value (smoothTestToH1 diskOpen f)),
    variationalOperator_test f, ae_restrict_mem diskOpen.measurableSet] with x hx hu hxd
  rw [hx,hu,potential_eq_integral f.property.1 f.property.2.1 f.property.2.2 hxd]
  apply integral_congr_ae
  have hf : H1.value (smoothTestToH1 diskOpen f) =ᵐ[volume.restrict disk] f :=
    (test_memLp (Ω := disk) f.property.1 f.property.2.1).coeFn_toLp
  filter_upwards [hf] with y hy
  rw [hy]

end StrictHotSpots.PlaneGreen


open MeasureTheory
open scoped InnerProductSpace ENNReal
namespace StrictHotSpots.H10
variable {Ω : Set Plane} (hΩ : IsOpen Ω) (hb : Bornology.IsBounded Ω)
variable {ν : Measure Plane} {C : ℝ≥0∞} (hC : C ≠ (∞ : ℝ≥0∞))
  (hν : ν ≤ C • volume.restrict Ω)


def weightedValue : H10 hΩ →L[ℝ] Lp ℝ 2 ν :=
  (Lp.LpToLpOfMeasureLeSMul hC hν).comp (value hΩ)

lemma weightedValue_ae (u : H10 hΩ) :
    weightedValue hΩ hC hν u =ᵐ[ν] value hΩ u :=
  Lp.coeFn_LpToLpOfMeasureLeSMul hC hν (value hΩ u)



def greenOperator : Lp ℝ 2 ν →L[ℝ] Lp ℝ 2 ν :=
  @DirichletOperator.green (H10 hΩ) (Lp ℝ 2 ν)
    (H10.normedAddCommGroup hΩ) (H10.innerProductSpace hΩ) (H10.completeSpace hΩ)
    inferInstance inferInstance inferInstance
    (energy hΩ) (energy_coercive hΩ hb) (weightedValue hΩ hC hν)

lemma energy_symmetric (u v : H10 hΩ) : energy hΩ u v = energy hΩ v u := by
  calc
    energy hΩ u v = inner ℝ (grad hΩ u) (grad hΩ v) := energy_apply hΩ u v
    _ = inner ℝ (grad hΩ v) (grad hΩ u) := real_inner_comm _ _
    _ = energy hΩ v u := (energy_apply hΩ v u).symm

lemma greenOperator_symmetric (f g : Lp ℝ 2 ν) :
    inner ℝ (greenOperator hΩ hb hC hν f) g =
      inner ℝ f (greenOperator hΩ hb hC hν g) :=
  DirichletOperator.green_symmetric (energy hΩ) (energy_coercive hΩ hb)
    (energy_symmetric hΩ) (weightedValue hΩ hC hν) f g

lemma greenOperator_nonneg (f : Lp ℝ 2 ν) :
    0 ≤ inner ℝ f (greenOperator hΩ hb hC hν f) :=
  DirichletOperator.green_nonneg (energy hΩ) (energy_coercive hΩ hb)
    (weightedValue hΩ hC hν) f


lemma greenOperator_norm_le {d : ℝ} (hd : 0 ≤ d)
    (hsub : ∀ u : H10 hΩ, ‖weightedValue hΩ hC hν u‖ ^ 2 ≤
      d * ‖grad hΩ u‖ ^ 2) : ‖greenOperator hΩ hb hC hν‖ ≤ d := by
  apply DirichletOperator.green_norm_le (energy hΩ) (energy_coercive hΩ hb)
    (weightedValue hΩ hC hν) hd
  intro u
  simpa only [energy_apply, real_inner_self_eq_norm_sq] using hsub u

end StrictHotSpots.H10


open MeasureTheory Filter
open scoped ENNReal InnerProductSpace
namespace StrictHotSpots.DirichletOperator

lemma green_comp {V H K : Type*}
    [NormedAddCommGroup V] [InnerProductSpace ℝ V] [CompleteSpace V]
    [NormedAddCommGroup H] [InnerProductSpace ℝ H] [CompleteSpace H]
    [NormedAddCommGroup K] [InnerProductSpace ℝ K] [CompleteSpace K]
    (B : V →L[ℝ] V →L[ℝ] ℝ) (hB : IsCoercive B) (J : V →L[ℝ] H) (S : H →L[ℝ] K) :
    green B hB (S.comp J) = (S.comp (green B hB J)).comp S.adjoint := by
  simp only [green, solution, ContinuousLinearMap.adjoint_comp, ContinuousLinearMap.comp_assoc]

end StrictHotSpots.DirichletOperator
namespace StrictHotSpots.PlaneGreen
open DiskH10
variable {ν : Measure Plane} {C : ℝ≥0∞} (hC : C ≠ (∞ : ℝ≥0∞))
  (hν : ν ≤ C • volume.restrict disk)

def inclusion : Lp ℝ 2 (volume.restrict disk) →L[ℝ] Lp ℝ 2 ν :=
  Lp.LpToLpOfMeasureLeSMul hC hν

lemma inclusion_ae (f : Lp ℝ 2 (volume.restrict disk)) : inclusion hC hν f =ᵐ[ν] f :=
  Lp.coeFn_LpToLpOfMeasureLeSMul hC hν f

include hC hν in
lemma kernel_memLp_weighted (x : Plane) (hx : x ∈ disk) : MemLp (kernel x) 2 ν :=
  ((kernel_memLp x hx).smul_measure hC).mono_measure hν

lemma inclusion_row (x : Plane) (hx : x ∈ disk) :
    inclusion hC hν ((kernel_memLp x hx).toLp (kernel x)) =
      (kernel_memLp_weighted hC hν x hx).toLp (kernel x) := by
  apply Lp.ext
  exact (inclusion_ae hC hν _).trans
    (((Measure.absolutelyContinuous_of_le_smul hν).ae_eq (kernel_memLp x hx).coeFn_toLp).trans
      (kernel_memLp_weighted hC hν x hx).coeFn_toLp.symm)

lemma integral_adjoint_row (f : Lp ℝ 2 ν) (x : Plane) (hx : x ∈ disk) :
    (∫ y in disk, kernel x y * (inclusion hC hν).adjoint f y) =
      ∫ y, kernel x y * f y ∂ν := by
  rw [MeasurableL2Kernel.integral_mul_eq_inner (kernel_memLp x hx)
    (Lp.memLp ((inclusion hC hν).adjoint f)), Lp.toLp_coeFn,
    ContinuousLinearMap.adjoint_inner_right, inclusion_row hC hν x hx]
  rw [MeasurableL2Kernel.integral_mul_eq_inner (kernel_memLp_weighted hC hν x hx)
    (Lp.memLp f), Lp.toLp_coeFn]



def weightedIntegralOperator : Lp ℝ 2 ν →L[ℝ] Lp ℝ 2 ν :=
  ((inclusion hC hν).comp integralOperator).comp (inclusion hC hν).adjoint

lemma weightedIntegralOperator_ae (f : Lp ℝ 2 ν) :
    weightedIntegralOperator hC hν f =ᵐ[ν] fun x => ∫ y, kernel x y * f y ∂ν := by
  have ha := (Measure.absolutelyContinuous_of_le_smul hν)
  filter_upwards [inclusion_ae hC hν (integralOperator ((inclusion hC hν).adjoint f)),
    ha.ae_eq (integralOperator_ae ((inclusion hC hν).adjoint f)),
    (ae_restrict_mem diskOpen.measurableSet).filter_mono ha.ae_le] with x hx hi hxd
  change inclusion hC hν (integralOperator ((inclusion hC hν).adjoint f)) x = _
  rw [hx,hi,integral_adjoint_row hC hν f x hxd]

lemma weightedIntegralOperator_eq_greenOperator : weightedIntegralOperator hC hν =
    H10.greenOperator diskOpen diskBounded hC hν := by
  unfold weightedIntegralOperator H10.greenOperator H10.weightedValue

  rw [DirichletOperator.green_comp, integralOperator_eq_variationalOperator]
  rfl



lemma weightedIntegralOperator_norm_le {d : ℝ} (hd : 0 ≤ d)
    (hsub : ∀ u : H10 diskOpen, ‖H10.weightedValue diskOpen hC hν u‖ ^ 2 ≤
      d * ‖H10.grad diskOpen u‖ ^ 2) : ‖weightedIntegralOperator hC hν‖ ≤ d := by
  rw [weightedIntegralOperator_eq_greenOperator]
  exact H10.greenOperator_norm_le diskOpen diskBounded hC hν hd hsub

lemma weightedIntegralOperator_symmetric (f g : Lp ℝ 2 ν) :
    inner ℝ (weightedIntegralOperator hC hν f) g =
      inner ℝ f (weightedIntegralOperator hC hν g) := by
  rw [weightedIntegralOperator_eq_greenOperator]
  exact H10.greenOperator_symmetric diskOpen diskBounded hC hν f g

lemma weightedIntegralOperator_nonneg (f : Lp ℝ 2 ν) :
    0 ≤ inner ℝ f (weightedIntegralOperator hC hν f) := by
  rw [weightedIntegralOperator_eq_greenOperator]
  exact H10.greenOperator_nonneg diskOpen diskBounded hC hν f

end StrictHotSpots.PlaneGreen

end WeightedLayer


end FullBoundaryCombinedLayer

end

end DouglasLipschitzBase

end OAI