File size: 54,373 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Dynamics.SmoothObstruction.ManifoldMatching

namespace OAI

section

open MeasureTheory Filter Topology TopologicalSpace Set Finset
open scoped ENNReal NNReal BoundedContinuousFunction
open Finset MeasureTheory Filter Topology
open scoped BigOperators
open Finset Matrix
open scoped Matrix.Norms.L2Operator
open Matrix

namespace SmoothObstruction.FiniteBins
open Set
variable {X C : Type*} (P : X → C) (G : Set X)
/-- Only bins meeting the original good set require a reference. -/
def Active := {c : C // ∃ x∈G,P x=c}
def Fiber (c : Active P G) := {x : G // P x=c.val}

noncomputable def reference (c : Active P G) : Fiber P G c := by
  classical
  apply Classical.choice
  obtain ⟨x,hx,hP⟩ := c.property
  exact ⟨⟨⟨x,hx⟩,hP⟩⟩

instance [Finite C] : Finite (Active P G) :=
  inferInstanceAs (Finite {category : C // ∃ point∈G,P point=category})
end SmoothObstruction.FiniteBins

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι C : Type*} [Fintype ι]

/-- The original genuine finite-window estimates produce all starting-bin
thresholds simultaneously. Only finite bin reference choices survive: no
measurable choice of frame, exponent, or orbit chart code is used. -/
theorem finite_starting_references (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ rF CF rB CB D : ℝ,0<rF ∧ 1≤CF ∧ 0<rB ∧ 1≤CB ∧ 1≤D ∧
      ∀ (c : M → ι) (P : M → C) (G : Set M)
        (n N : ℕ) (B R h g : ℝ) (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint x) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        CF^N*h≤rF → CB^N*h≤rB → 0<g →
        Real.exp (-2*g*N+4*(B+Real.log D))≤1/2 →
        (let δ := ((N:ℝ)*CF*CF^(2*N)+N*CB*CB^(2*N))*h
         δ<Real.exp (-2*(B+Real.log D))/2 ∧
         Real.exp (-2*g*N+4*(B+Real.log D))+
           δ*Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))≤1/2) →
        ∃ j : Active P G → Fin (d+1),∀ Q : Active P G,
          let x₀ : G := (reference P G Q).val
          let γ := (1+8*((j Q).val:ℝ))*g
          let ε := Real.exp (-2*g*N+4*(B+Real.log D))+
            (((N:ℝ)*CF*CF^(2*N)+N*CB*CB^(2*N))*h)*
              Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))
          ∀ x:G,P x=Q.val →
            (∀ k,(A x).lam k<γ-g ∨ γ+g<(A x).lam k) ∧
            (Finset.univ.filter (fun k => γ<(A x).lam k)).card=
              (Finset.univ.filter (fun k => γ<(A x₀).lam k)).card ∧
            ‖(((A x).forward I S a c).space (Finset.univ.filter (fun k => (A x).lam k≤γ))).starProjection-
              (((A x₀).forward I S a c).space (Finset.univ.filter (fun k => (A x₀).lam k≤γ))).starProjection‖≤3*ε ∧
            ‖(((A x).forward I S a c).space (Finset.univ.filter (fun k => γ<(A x).lam k))).starProjection-
              (((A x₀).forward I S a c).space (Finset.univ.filter (fun k => γ<(A x₀).lam k))).starProjection‖≤3*ε := by
  classical
  obtain ⟨rF,CF,rB,CB,D,hrF,hCF,hrB,hCB,hD,hh⟩ :=
    uniform_genuine_bin_splitting I hclosed S a K hK hKU
  refine ⟨rF,CF,rB,CB,D,hrF,hCF,hrB,hCB,hD,?_⟩
  intro c P G n N B R h g A hc hN hco hint hdiam hsF hsB hg hdec hsmall
  have hex (Q : Active P G) := hh c (Fiber P G Q) (fun x => x.val.val)
    (reference P G Q) n N B R h g (fun x => A x.val) hc hN
    (fun x => hco _ _ (x.property.trans (reference P G Q).property.symm))
    (fun x => hint _ x.val.property)
    (fun x => hdiam _ _ (x.property.trans (reference P G Q).property.symm))
    hsF hsB hg hdec hsmall.1 hsmall.2
  choose j hj using hex
  refine ⟨j,?_⟩
  intro Q x₀ γ ε x hx
  have hgx := (hj Q).1 (⟨x,hx⟩ : Fiber P G Q)
  exact ⟨hgx.1,hgx.2,(hj Q).2.1 ⟨x,hx⟩,(hj Q).2.2 ⟨x,hx⟩⟩
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι C : Type*} [Fintype ι]

/-- The endpoint reference is indexed only by starting and endpoint coarse
bins. The common starting rank and threshold are retained unchanged. Inactive
endpoint keys are not part of the construction or the partition count. -/
theorem finite_endpoint_references (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r C₁ D : ℝ,0<r ∧ 1≤C₁ ∧ 1≤D ∧
      ∀ (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (γ : C → ℝ) (T : G → Finset (Fin d))
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint (S^[n] x)) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        C₁^N*h≤r → 0≤h →
        (∀ x y:G,P x=P y → (T x).card=(T y).card) →
        (∀ x:G,∀ j∈(T x)ᶜ,(A x).lam j≤γ (P x)-g) →
        (∀ x:G,∀ j∈T x,γ (P x)+g≤(A x).lam j) →
        (∀ q : C,let ε := Real.exp ((γ q-g)*N+2*(B+Real.log D))*
          (Real.exp ((-γ q-g)*N+2*(B+Real.log D))+N*C₁*C₁^(2*N)*h)
          ε≤1/2) →
        let EKey := fun x => (P x,P (S^[n] x))
        ∃ W : Active EKey G → Submodule ℝ (EuclideanSpace ℝ (Fin d)),
          ∀ Q : Active EKey G,∀ x:G,EKey x=Q.val →
            Module.finrank ℝ (W Q)=(T x).card ∧
            ‖((A x).forwardSpace I S a c n (T x)).starProjection-(W Q).starProjection‖≤
              3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log D))*
                (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D))+N*C₁*C₁^(2*N)*h)) := by
  classical
  obtain ⟨r,C₁,D,hr,hC,hD,hh⟩ := uniform_original_endpoint_fast_comparison I hclosed S a K hK hKU
  refine ⟨r,C₁,D,hr,hC,hD,?_⟩
  intro c P G n N B R h g γ T A hc hN hco hint hdiam ht hn hdim hslow hfast hsmall EKey
  let ref (Q : Active EKey G) : G := (reference EKey G Q).val
  let W (Q : Active EKey G) := (A (ref Q)).forwardSpace I S a c n (T (ref Q))
  refine ⟨W,?_⟩
  intro Q x hx
  have hp (y : Fiber EKey G Q) : EKey y.val=EKey (ref Q) :=
    y.property.trans (reference EKey G Q).property.symm
  have hq (y : Fiber EKey G Q) : P y.val=P (ref Q) := congrArg Prod.fst (hp y)
  have hend (y : Fiber EKey G Q) : P (S^[n] y.val.val)=P (S^[n] (ref Q).val) :=
    congrArg Prod.snd (hp y)
  have he : P x=P (ref Q) := hq ⟨x,hx⟩
  constructor
  · exact (EuclideanSplitting.finrank_groupSpace ((A (ref Q)).UF n)
      ((A (ref Q)).VF n) ((A (ref Q)).invF n).1 ((A (ref Q)).invF n).2
      (T (ref Q))).trans (hdim _ _ he.symm)
  · have hb := hh c (Fiber EKey G Q) (fun y => y.val.val) (reference EKey G Q)
      n N B R h (γ (P (ref Q))) g (fun y => T y.val) (fun y => A y.val)
      hc hN (fun y => hco _ _ (hend y)) (fun y => hint _ y.val.property)
      (fun y => hdiam _ _ (hend y)) ht hn (fun y => hdim _ _ (hq y).symm)
      (fun y j hj => by simpa only [←hq y] using hslow y.val j hj)
      (fun y j hj => by simpa only [←hq y] using hfast y.val j hj)
      (hsmall (P (ref Q))) (⟨x,hx⟩ : Fiber EKey G Q)
    simpa only [W,ref,he] using hb
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} (S : Diffeomorph I I M M ∞) (a : ι → M) (c : M → ι)
    {n : ℕ} {B R : ℝ} {x : M}

/-- Time reversal keeps the original gauge and negates its rates; it is not
an independently selected certificate. -/
noncomputable def BilateralGauge.reverse (A : BilateralGauge I S a c n B R x) :
    BilateralGauge I S.symm a c n B R x where
  lam := fun j => -A.lam j
  rate_bound j := by simpa only [abs_neg] using A.rate_bound j
  UF := A.UB
  VF := A.VB
  UB := A.UF
  VB := A.VF
  invF := A.invB
  invB := A.invF
  centerU := A.centerU.symm
  centerV := A.centerV.symm
  normF := A.normB
  normB := A.normF
  stepF := A.stepB
  stepB t ht := by
    have he : S.symm.symm = S := by ext x; rfl
    simpa only [he,neg_neg] using A.stepF t ht

@[simp] lemma BilateralGauge.reverse_forwardSpace (A : BilateralGauge I S a c n B R x)
    (t : ℕ) (T : Finset (Fin d)) :
    (A.reverse I S a c).forwardSpace I S.symm a c t T=A.backwardSpace I S a c t T := rfl

/-- The two true middle subspaces agree exactly. -/
lemma BilateralGauge.backwardSpace_zero (A : BilateralGauge I S a c n B R x)
    (T : Finset (Fin d)) :
    A.backwardSpace I S a c 0 T=A.forwardSpace I S a c 0 T := by
  simp only [BilateralGauge.backwardSpace,BilateralGauge.forwardSpace,A.centerU,A.centerV]
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.FiniteBins
open Set
variable {X C Z : Type*} (P : X → C) (G : Set X)
def key (x : G) : Active P G := ⟨P x,⟨x,x.property,rfl⟩⟩
noncomputable def extend (f : Active P G → Z) (z₀ : Z) (c : C) : Z := by
  classical
  exact if h : ∃ x∈G,P x=c then f ⟨c,h⟩ else z₀
@[simp] lemma extend_active (f : Active P G → Z) (z₀ : Z) (c : Active P G) :
    extend P G f z₀ c.val=f c := by
  classical
  simp only [extend,dite_eq_left c.property]
  congr 1
@[simp] lemma extend_key (f : Active P G → Z) (z₀ : Z) (x : G) :
    extend P G f z₀ (P x)=f (key P G x) := extend_active P G f z₀ (key P G x)
end SmoothObstruction.FiniteBins

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι C : Type*} [Fintype ι]

/-- All future and past reference projectors are selected from the same
original good inputs. Their labels are exactly (starting bin, endpoint bin).
Time reversal is the original bilateral gauge, so its endpoint rank is the
complementary original rank, not a new threshold or a separately chosen frame. -/
theorem bilateral_endpoint_references (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ rF CF DF rB CB DB : ℝ,0<rF ∧ 1≤CF ∧ 1≤DF ∧ 0<rB ∧ 1≤CB ∧ 1≤DB ∧
      ∀ (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (γ : C → ℝ) (T : G → Finset (Fin d))
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint (S^[n] x) ∧ I.IsInteriorPoint (S.symm^[n] x)) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        CF^N*h≤rF → CB^N*h≤rB → 0≤h →
        (∀ x y:G,P x=P y → (T x).card=(T y).card) →
        (∀ x:G,∀ j∈(T x)ᶜ,(A x).lam j≤γ (P x)-g) →
        (∀ x:G,∀ j∈T x,γ (P x)+g≤(A x).lam j) →
        (∀ q : C,Real.exp ((γ q-g)*N+2*(B+Real.log DF))*
          (Real.exp ((-γ q-g)*N+2*(B+Real.log DF))+N*CF*CF^(2*N)*h)≤1/2) →
        (∀ q : C,Real.exp ((-γ q-g)*N+2*(B+Real.log DB))*
          (Real.exp ((γ q-g)*N+2*(B+Real.log DB))+N*CB*CB^(2*N)*h)≤1/2) →
        ∃ Wf Wb : C×C → Submodule ℝ (EuclideanSpace ℝ (Fin d)),∀ x:G,
          Module.finrank ℝ (Wf (P x,P (S^[n] x)))=(T x).card ∧
          Module.finrank ℝ (Wb (P x,P (S.symm^[n] x)))=d-(T x).card ∧
          ‖((A x).forwardSpace I S a c n (T x)).starProjection-
            (Wf (P x,P (S^[n] x))).starProjection‖≤
              3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log DF))*
                (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log DF))+N*CF*CF^(2*N)*h)) ∧
          ‖((A x).backwardSpace I S a c n (T x)ᶜ).starProjection-
            (Wb (P x,P (S.symm^[n] x))).starProjection‖≤
              3*(Real.exp ((-γ (P x)-g)*N+2*(B+Real.log DB))*
                (Real.exp ((γ (P x)-g)*N+2*(B+Real.log DB))+N*CB*CB^(2*N)*h)) := by
  classical
  obtain ⟨rF,CF,DF,hrF,hCF,hDF,hF⟩ := finite_endpoint_references (C:=C) I hclosed S a K hK hKU
  obtain ⟨rB,CB,DB,hrB,hCB,hDB,hB⟩ := finite_endpoint_references (C:=C) I hclosed S.symm a K hK hKU
  refine ⟨rF,CF,DF,rB,CB,DB,hrF,hCF,hDF,hrB,hCB,hDB,?_⟩
  intro c P G n N B R h g γ T A hc hN hco hint hdiam htF htB hh hcard hslow hfast hsmallF hsmallB
  obtain ⟨WF,hWF⟩ := hF c P G n N B R h g γ T A hc hN hco
    (fun x hx => (hint x hx).1) hdiam htF hh hcard hslow hfast hsmallF
  have hcardB (x y : G) (hxy : P x=P y) : (T x)ᶜ.card=(T y)ᶜ.card := by
    simp only [Finset.card_compl,Fintype.card_fin,hcard x y hxy]
  have hsB (x : G) (j : Fin d) (hj : j∈((T x)ᶜ)ᶜ) :
      -((A x).lam j)≤(-γ (P x))-g := by
    simp only [compl_compl] at hj
    linarith [hfast x j hj]
  have hfB (x : G) (j : Fin d) (hj : j∈(T x)ᶜ) :
      (-γ (P x))+g≤-((A x).lam j) := by linarith [hslow x j hj]
  obtain ⟨WB,hWB⟩ := hB c P G n N B R h g (fun q => -γ q) (fun x => (T x)ᶜ)
    (fun x => (A x).reverse I S a c) hc hN hco (fun x hx => (hint x hx).2)
    hdiam htB hh hcardB hsB hfB (by simpa only [neg_neg] using hsmallB)
  let PF (x : M) := (P x,P (S^[n] x))
  let PB (x : M) := (P x,P (S.symm^[n] x))
  refine ⟨extend PF G WF ⊥,extend PB G WB ⊥,?_⟩
  intro x
  have hFx := hWF (key PF G x) x rfl
  have hBx := hWB (key PB G x) x rfl
  have eF := extend_key PF G WF (⊥ : Submodule ℝ (EuclideanSpace ℝ (Fin d))) x
  have eB := extend_key PB G WB (⊥ : Submodule ℝ (EuclideanSpace ℝ (Fin d))) x
  dsimp only [PF,PB] at eF eB hFx hBx ⊢
  rw [eF,eB]
  refine ⟨hFx.1,?_,hFx.2,?_⟩
  · simpa only [Finset.card_compl,Fintype.card_fin] using hBx.1
  · simpa only [BilateralGauge.reverse_forwardSpace,neg_neg] using hBx.2
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι C : Type*} [Fintype ι]

noncomputable def fastSet (v : Fin d → ℝ) (γ : ℝ) : Finset (Fin d) := by
  classical
  exact Finset.univ.filter (fun j => γ<v j)
noncomputable def slowSet (v : Fin d → ℝ) (γ : ℝ) : Finset (Fin d) := by
  classical
  exact Finset.univ.filter (fun j => v j≤γ)
lemma slowSet_eq_compl (v : Fin d → ℝ) (γ : ℝ) : slowSet v γ=(fastSet v γ)ᶜ := by
  classical
  ext j
  simp only [fastSet,slowSet,Finset.mem_filter,Finset.mem_univ,true_and,Finset.mem_compl,not_lt]

/-- Simultaneous starting and two endpoint references derived from the ORIGINAL
bilateral good gauges. All choices are finite bin choices: the pointwise frames
and rate lists are never made into a measurable refinement. -/
theorem finite_references (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r₁ C₁ r₂ C₂ D r₃ C₃ D₃ r₄ C₄ D₄ : ℝ,
      0<r₁ ∧ 1≤C₁ ∧ 0<r₂ ∧ 1≤C₂ ∧ 1≤D ∧
      0<r₃ ∧ 1≤C₃ ∧ 1≤D₃ ∧ 0<r₄ ∧ 1≤C₄ ∧ 1≤D₄ ∧
      ∀ (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint x) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        C₁^N*h≤r₁ → C₂^N*h≤r₂ → C₃^N*h≤r₃ → C₄^N*h≤r₄ → 0≤h → 0<g →
        Real.exp (-2*g*N+4*(B+Real.log D))≤1/2 →
        (let δ := ((N:ℝ)*C₁*C₁^(2*N)+N*C₂*C₂^(2*N))*h
         δ<Real.exp (-2*(B+Real.log D))/2 ∧
         Real.exp (-2*g*N+4*(B+Real.log D))+
           δ*Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))≤1/2) →
        (∀ j : Fin (d+1),let γ := (1+8*(j.val:ℝ))*g
          Real.exp ((γ-g)*N+2*(B+Real.log D₃))*
            (Real.exp ((-γ-g)*N+2*(B+Real.log D₃))+N*C₃*C₃^(2*N)*h)≤1/2) →
        (∀ j : Fin (d+1),let γ := (1+8*(j.val:ℝ))*g
          Real.exp ((-γ-g)*N+2*(B+Real.log D₄))*
            (Real.exp ((γ-g)*N+2*(B+Real.log D₄))+N*C₄*C₄^(2*N)*h)≤1/2) →
        ∃ j : C → Fin (d+1),
          let γ (q : C) := (1+8*((j q).val:ℝ))*g
          let T (x : G) := fastSet (A x).lam (γ (P x))
          let ε := Real.exp (-2*g*N+4*(B+Real.log D))+
            (((N:ℝ)*C₁*C₁^(2*N)+N*C₂*C₂^(2*N))*h)*
              Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))
          ∃ Wf Wb : C×C → Submodule ℝ (EuclideanSpace ℝ (Fin d)),
            (∀ x:G,∀ i∈(T x)ᶜ,(A x).lam i≤γ (P x)-g) ∧
            (∀ x:G,∀ i∈T x,γ (P x)+g≤(A x).lam i) ∧
            (∀ x y:G,P x=P y → (T x).card=(T y).card) ∧
            (∀ x:G,let y : G := (reference P G (key P G x)).val
              ‖(((A x).forward I S a c).space (T x)ᶜ).starProjection-
                (((A y).forward I S a c).space (T y)ᶜ).starProjection‖≤3*ε ∧
              ‖(((A x).forward I S a c).space (T x)).starProjection-
                (((A y).forward I S a c).space (T y)).starProjection‖≤3*ε) ∧
            ∀ x:G,
              Module.finrank ℝ (Wf (P x,P (S^[n] x)))=(T x).card ∧
              Module.finrank ℝ (Wb (P x,P (S.symm^[n] x)))=d-(T x).card ∧
              ‖((A x).forwardSpace I S a c n (T x)).starProjection-
                (Wf (P x,P (S^[n] x))).starProjection‖≤
                  3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log D₃))*
                    (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D₃))+N*C₃*C₃^(2*N)*h)) ∧
              ‖((A x).backwardSpace I S a c n (T x)ᶜ).starProjection-
                (Wb (P x,P (S.symm^[n] x))).starProjection‖≤
                  3*(Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D₄))*
                    (Real.exp ((γ (P x)-g)*N+2*(B+Real.log D₄))+N*C₄*C₄^(2*N)*h)) := by
  classical
  obtain ⟨r₁,C₁,r₂,C₂,D,hr₁,hC₁,hr₂,hC₂,hD,hs⟩ :=
    finite_starting_references (C:=C) I hclosed S a K hK hKU
  obtain ⟨r₃,C₃,D₃,r₄,C₄,D₄,hr₃,hC₃,hD₃,hr₄,hC₄,hD₄,he⟩ :=
    bilateral_endpoint_references (C:=C) I hclosed S a K hK hKU
  refine ⟨r₁,C₁,r₂,C₂,D,r₃,C₃,D₃,r₄,C₄,D₄,
    hr₁,hC₁,hr₂,hC₂,hD,hr₃,hC₃,hD₃,hr₄,hC₄,hD₄,?_⟩
  intro c P G n N B R h g A hc hN hco hint hdiam ht₁ ht₂ ht₃ ht₄ hh hg hd hsml hfml hbml
  obtain ⟨j₀,hj₀⟩ := hs c P G n N B R h g A hc hN hco hint hdiam ht₁ ht₂ hg hd hsml
  let j := FiniteBins.extend P G j₀ (0 : Fin (d+1))
  let γ (q : C) := (1+8*((j q).val:ℝ))*g
  let T (x : G) := fastSet (A x).lam (γ (P x))
  have hj (x : G) : j (P x)=j₀ (key P G x) := extend_key P G j₀ _ x
  have hgap (x : G) (i : Fin d) : (A x).lam i<γ (P x)-g ∨ γ (P x)+g<(A x).lam i := by
    have hh := (hj₀ (key P G x) x rfl).1 i
    simpa only [γ,hj] using hh
  have hsc (x : G) (i : Fin d) (hi : i∈(T x)ᶜ) : (A x).lam i≤γ (P x)-g := by
    have hl : (A x).lam i≤γ (P x) := by simpa only [T,fastSet,Finset.mem_compl,Finset.mem_filter,
      Finset.mem_univ,true_and,not_lt] using hi
    rcases hgap x i with hi | hi
    · exact hi.le
    · linarith
  have hfc (x : G) (i : Fin d) (hi : i∈T x) : γ (P x)+g≤(A x).lam i := by
    have hl : γ (P x)<(A x).lam i := by simpa only [T,fastSet,Finset.mem_filter,
      Finset.mem_univ,true_and] using hi
    rcases hgap x i with hi | hi
    · linarith
    · exact hi.le
  have hcard (x y : G) (hxy : P x=P y) : (T x).card=(T y).card := by
    have hx := (hj₀ (key P G x) x rfl).2.1
    have hy := (hj₀ (key P G x) y hxy.symm).2.1
    dsimp only [T,fastSet,γ]
    rw [←hxy,hj x]
    exact hx.trans hy.symm
  have hi (F : Diffeomorph I I M M ∞) (x : M) (hx : I.IsInteriorPoint x) (t : ℕ) :
      I.IsInteriorPoint (F^[t] x) := by
    induction t with
    | zero => exact hx
    | succ t ih =>
      rw [Function.iterate_succ_apply']
      exact ((F.isLocalDiffeomorph _).isInteriorPoint_iff (by simp)).mp ih
  obtain ⟨Wf,Wb,hW⟩ := he c P G n N B R h g γ T A hc hN hco
    (fun x hx => ⟨hi S x (hint x hx) n,hi S.symm x (hint x hx) n⟩)
    hdiam ht₃ ht₄ hh hcard hsc hfc (fun q => hfml (j q)) (fun q => hbml (j q))
  refine ⟨j,Wf,Wb,hsc,hfc,hcard,?_,hW⟩
  intro x y
  have hy : P y=P x := (reference P G (key P G x)).property
  have hxs := (hj₀ (key P G x) x rfl).2.2
  dsimp only [T]
  rw [←slowSet_eq_compl,←slowSet_eq_compl]
  simpa only [γ,hy,hj x,slowSet,fastSet] using hxs
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]
theorem finite_starting_references_uniform (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ rF CF rB CB D : ℝ,0<rF ∧ 1≤CF ∧ 0<rB ∧ 1≤CB ∧ 1≤D ∧
      ∀ (C : Type*) (c : M → ι) (P : M → C) (G : Set M)
        (n N : ℕ) (B R h g : ℝ) (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint x) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        CF^N*h≤rF → CB^N*h≤rB → 0<g →
        Real.exp (-2*g*N+4*(B+Real.log D))≤1/2 →
        (let δ := ((N:ℝ)*CF*CF^(2*N)+N*CB*CB^(2*N))*h
         δ<Real.exp (-2*(B+Real.log D))/2 ∧
         Real.exp (-2*g*N+4*(B+Real.log D))+
           δ*Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))≤1/2) →
        ∃ j : Active P G → Fin (d+1),∀ Q : Active P G,
          let x₀ : G := (reference P G Q).val
          let γ := (1+8*((j Q).val:ℝ))*g
          let ε := Real.exp (-2*g*N+4*(B+Real.log D))+
            (((N:ℝ)*CF*CF^(2*N)+N*CB*CB^(2*N))*h)*
              Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))
          ∀ x:G,P x=Q.val →
            (∀ k,(A x).lam k<γ-g ∨ γ+g<(A x).lam k) ∧
            (Finset.univ.filter (fun k => γ<(A x).lam k)).card=
              (Finset.univ.filter (fun k => γ<(A x₀).lam k)).card ∧
            ‖(((A x).forward I S a c).space (Finset.univ.filter (fun k => (A x).lam k≤γ))).starProjection-
              (((A x₀).forward I S a c).space (Finset.univ.filter (fun k => (A x₀).lam k≤γ))).starProjection‖≤3*ε ∧
            ‖(((A x).forward I S a c).space (Finset.univ.filter (fun k => γ<(A x).lam k))).starProjection-
              (((A x₀).forward I S a c).space (Finset.univ.filter (fun k => γ<(A x₀).lam k))).starProjection‖≤3*ε := by
  classical
  obtain ⟨rF,CF,rB,CB,D,hrF,hCF,hrB,hCB,hD,hh⟩ :=
    uniform_genuine_bin_splitting I hclosed S a K hK hKU
  refine ⟨rF,CF,rB,CB,D,hrF,hCF,hrB,hCB,hD,?_⟩
  intro C c P G n N B R h g A hc hN hco hint hdiam hsF hsB hg hdec hsmall
  have hex (Q : Active P G) := hh c (Fiber P G Q) (fun x => x.val.val)
    (reference P G Q) n N B R h g (fun x => A x.val) hc hN
    (fun x => hco _ _ (x.property.trans (reference P G Q).property.symm))
    (fun x => hint _ x.val.property)
    (fun x => hdiam _ _ (x.property.trans (reference P G Q).property.symm))
    hsF hsB hg hdec hsmall.1 hsmall.2
  choose j hj using hex
  refine ⟨j,?_⟩
  intro Q x₀ γ ε x hx
  have hgx := (hj Q).1 (⟨x,hx⟩ : Fiber P G Q)
  exact ⟨hgx.1,hgx.2,(hj Q).2.1 ⟨x,hx⟩,(hj Q).2.2 ⟨x,hx⟩⟩
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]
theorem finite_endpoint_references_uniform (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r C₁ D : ℝ,0<r ∧ 1≤C₁ ∧ 1≤D ∧
      ∀ (C : Type*) (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (γ : C → ℝ) (T : G → Finset (Fin d))
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint (S^[n] x)) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        C₁^N*h≤r → 0≤h →
        (∀ x y:G,P x=P y → (T x).card=(T y).card) →
        (∀ x:G,∀ j∈(T x)ᶜ,(A x).lam j≤γ (P x)-g) →
        (∀ x:G,∀ j∈T x,γ (P x)+g≤(A x).lam j) →
        (∀ q : C,let ε := Real.exp ((γ q-g)*N+2*(B+Real.log D))*
          (Real.exp ((-γ q-g)*N+2*(B+Real.log D))+N*C₁*C₁^(2*N)*h)
          ε≤1/2) →
        let EKey := fun x => (P x,P (S^[n] x))
        ∃ W : Active EKey G → Submodule ℝ (EuclideanSpace ℝ (Fin d)),
          ∀ Q : Active EKey G,∀ x:G,EKey x=Q.val →
            Module.finrank ℝ (W Q)=(T x).card ∧
            ‖((A x).forwardSpace I S a c n (T x)).starProjection-(W Q).starProjection‖≤
              3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log D))*
                (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D))+N*C₁*C₁^(2*N)*h)) := by
  classical
  obtain ⟨r,C₁,D,hr,hC,hD,hh⟩ := uniform_original_endpoint_fast_comparison I hclosed S a K hK hKU
  refine ⟨r,C₁,D,hr,hC,hD,?_⟩
  intro C c P G n N B R h g γ T A hc hN hco hint hdiam ht hn hdim hslow hfast hsmall EKey
  let ref (Q : Active EKey G) : G := (reference EKey G Q).val
  let W (Q : Active EKey G) := (A (ref Q)).forwardSpace I S a c n (T (ref Q))
  refine ⟨W,?_⟩
  intro Q x hx
  have hp (y : Fiber EKey G Q) : EKey y.val=EKey (ref Q) :=
    y.property.trans (reference EKey G Q).property.symm
  have hq (y : Fiber EKey G Q) : P y.val=P (ref Q) := congrArg Prod.fst (hp y)
  have hend (y : Fiber EKey G Q) : P (S^[n] y.val.val)=P (S^[n] (ref Q).val) :=
    congrArg Prod.snd (hp y)
  have he : P x=P (ref Q) := hq ⟨x,hx⟩
  constructor
  · exact (EuclideanSplitting.finrank_groupSpace ((A (ref Q)).UF n)
      ((A (ref Q)).VF n) ((A (ref Q)).invF n).1 ((A (ref Q)).invF n).2
      (T (ref Q))).trans (hdim _ _ he.symm)
  · have hb := hh c (Fiber EKey G Q) (fun y => y.val.val) (reference EKey G Q)
      n N B R h (γ (P (ref Q))) g (fun y => T y.val) (fun y => A y.val)
      hc hN (fun y => hco _ _ (hend y)) (fun y => hint _ y.val.property)
      (fun y => hdiam _ _ (hend y)) ht hn (fun y => hdim _ _ (hq y).symm)
      (fun y j hj => by simpa only [←hq y] using hslow y.val j hj)
      (fun y j hj => by simpa only [←hq y] using hfast y.val j hj)
      (hsmall (P (ref Q))) (⟨x,hx⟩ : Fiber EKey G Q)
    simpa only [W,ref,he] using hb
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]
theorem bilateral_endpoint_references_uniform (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ rF CF DF rB CB DB : ℝ,0<rF ∧ 1≤CF ∧ 1≤DF ∧ 0<rB ∧ 1≤CB ∧ 1≤DB ∧
      ∀ (C : Type*) (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (γ : C → ℝ) (T : G → Finset (Fin d))
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint (S^[n] x) ∧ I.IsInteriorPoint (S.symm^[n] x)) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        CF^N*h≤rF → CB^N*h≤rB → 0≤h →
        (∀ x y:G,P x=P y → (T x).card=(T y).card) →
        (∀ x:G,∀ j∈(T x)ᶜ,(A x).lam j≤γ (P x)-g) →
        (∀ x:G,∀ j∈T x,γ (P x)+g≤(A x).lam j) →
        (∀ q : C,Real.exp ((γ q-g)*N+2*(B+Real.log DF))*
          (Real.exp ((-γ q-g)*N+2*(B+Real.log DF))+N*CF*CF^(2*N)*h)≤1/2) →
        (∀ q : C,Real.exp ((-γ q-g)*N+2*(B+Real.log DB))*
          (Real.exp ((γ q-g)*N+2*(B+Real.log DB))+N*CB*CB^(2*N)*h)≤1/2) →
        ∃ Wf Wb : C×C → Submodule ℝ (EuclideanSpace ℝ (Fin d)),∀ x:G,
          Module.finrank ℝ (Wf (P x,P (S^[n] x)))=(T x).card ∧
          Module.finrank ℝ (Wb (P x,P (S.symm^[n] x)))=d-(T x).card ∧
          ‖((A x).forwardSpace I S a c n (T x)).starProjection-
            (Wf (P x,P (S^[n] x))).starProjection‖≤
              3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log DF))*
                (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log DF))+N*CF*CF^(2*N)*h)) ∧
          ‖((A x).backwardSpace I S a c n (T x)ᶜ).starProjection-
            (Wb (P x,P (S.symm^[n] x))).starProjection‖≤
              3*(Real.exp ((-γ (P x)-g)*N+2*(B+Real.log DB))*
                (Real.exp ((γ (P x)-g)*N+2*(B+Real.log DB))+N*CB*CB^(2*N)*h)) := by
  classical
  obtain ⟨rF,CF,DF,hrF,hCF,hDF,hF⟩ := finite_endpoint_references_uniform I hclosed S a K hK hKU
  obtain ⟨rB,CB,DB,hrB,hCB,hDB,hB⟩ := finite_endpoint_references_uniform I hclosed S.symm a K hK hKU
  refine ⟨rF,CF,DF,rB,CB,DB,hrF,hCF,hDF,hrB,hCB,hDB,?_⟩
  intro C c P G n N B R h g γ T A hc hN hco hint hdiam htF htB hh hcard hslow hfast hsmallF hsmallB
  obtain ⟨WF,hWF⟩ := hF C c P G n N B R h g γ T A hc hN hco
    (fun x hx => (hint x hx).1) hdiam htF hh hcard hslow hfast hsmallF
  have hcardB (x y : G) (hxy : P x=P y) : (T x)ᶜ.card=(T y)ᶜ.card := by
    simp only [Finset.card_compl,Fintype.card_fin,hcard x y hxy]
  have hsB (x : G) (j : Fin d) (hj : j∈((T x)ᶜ)ᶜ) :
      -((A x).lam j)≤(-γ (P x))-g := by
    simp only [compl_compl] at hj
    linarith [hfast x j hj]
  have hfB (x : G) (j : Fin d) (hj : j∈(T x)ᶜ) :
      (-γ (P x))+g≤-((A x).lam j) := by linarith [hslow x j hj]
  obtain ⟨WB,hWB⟩ := hB C c P G n N B R h g (fun q => -γ q) (fun x => (T x)ᶜ)
    (fun x => (A x).reverse I S a c) hc hN hco (fun x hx => (hint x hx).2)
    hdiam htB hh hcardB hsB hfB (by simpa only [neg_neg] using hsmallB)
  let PF (x : M) := (P x,P (S^[n] x))
  let PB (x : M) := (P x,P (S.symm^[n] x))
  refine ⟨extend PF G WF ⊥,extend PB G WB ⊥,?_⟩
  intro x
  have hFx := hWF (key PF G x) x rfl
  have hBx := hWB (key PB G x) x rfl
  have eF := extend_key PF G WF (⊥ : Submodule ℝ (EuclideanSpace ℝ (Fin d))) x
  have eB := extend_key PB G WB (⊥ : Submodule ℝ (EuclideanSpace ℝ (Fin d))) x
  dsimp only [PF,PB] at eF eB hFx hBx ⊢
  rw [eF,eB]
  refine ⟨hFx.1,?_,hFx.2,?_⟩
  · simpa only [Finset.card_compl,Fintype.card_fin] using hBx.1
  · simpa only [BilateralGauge.reverse_forwardSpace,neg_neg] using hBx.2
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Filter Topology Metric MeasureTheory FiniteBins
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ProperSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]
theorem finite_references_uniform (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r₁ C₁ r₂ C₂ D r₃ C₃ D₃ r₄ C₄ D₄ : ℝ,
      0<r₁ ∧ 1≤C₁ ∧ 0<r₂ ∧ 1≤C₂ ∧ 1≤D ∧
      0<r₃ ∧ 1≤C₃ ∧ 1≤D₃ ∧ 0<r₄ ∧ 1≤C₄ ∧ 1≤D₄ ∧
      ∀ (C : Type*) (c : M → ι) (P : M → C) (G : Set M) (n N : ℕ) (B R h g : ℝ)
        (A : ∀ x:G,BilateralGauge I S a c n B R x),
        (∀ y,y∈K (c y)) → N≤n →
        (∀ x y,P x=P y → c x=c y) →
        (∀ x∈G,I.IsInteriorPoint x) →
        (∀ x y,P x=P y → ‖(extChartAt I (a (c y))) x-(extChartAt I (a (c y))) y‖≤h) →
        C₁^N*h≤r₁ → C₂^N*h≤r₂ → C₃^N*h≤r₃ → C₄^N*h≤r₄ → 0≤h → 0<g →
        Real.exp (-2*g*N+4*(B+Real.log D))≤1/2 →
        (let δ := ((N:ℝ)*C₁*C₁^(2*N)+N*C₂*C₂^(2*N))*h
         δ<Real.exp (-2*(B+Real.log D))/2 ∧
         Real.exp (-2*g*N+4*(B+Real.log D))+
           δ*Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))≤1/2) →
        (∀ j : Fin (d+1),let γ := (1+8*(j.val:ℝ))*g
          Real.exp ((γ-g)*N+2*(B+Real.log D₃))*
            (Real.exp ((-γ-g)*N+2*(B+Real.log D₃))+N*C₃*C₃^(2*N)*h)≤1/2) →
        (∀ j : Fin (d+1),let γ := (1+8*(j.val:ℝ))*g
          Real.exp ((-γ-g)*N+2*(B+Real.log D₄))*
            (Real.exp ((γ-g)*N+2*(B+Real.log D₄))+N*C₄*C₄^(2*N)*h)≤1/2) →
        ∃ j : C → Fin (d+1),
          let γ (q : C) := (1+8*((j q).val:ℝ))*g
          let T (x : G) := fastSet (A x).lam (γ (P x))
          let ε := Real.exp (-2*g*N+4*(B+Real.log D))+
            (((N:ℝ)*C₁*C₁^(2*N)+N*C₂*C₂^(2*N))*h)*
              Real.exp ((1+8*(d:ℝ))*g*N+2*(B+Real.log D))
          ∃ Wf Wb : C×C → Submodule ℝ (EuclideanSpace ℝ (Fin d)),
            (∀ x:G,∀ i∈(T x)ᶜ,(A x).lam i≤γ (P x)-g) ∧
            (∀ x:G,∀ i∈T x,γ (P x)+g≤(A x).lam i) ∧
            (∀ x y:G,P x=P y → (T x).card=(T y).card) ∧
            (∀ x:G,let y : G := (reference P G (key P G x)).val
              ‖(((A x).forward I S a c).space (T x)ᶜ).starProjection-
                (((A y).forward I S a c).space (T y)ᶜ).starProjection‖≤3*ε ∧
              ‖(((A x).forward I S a c).space (T x)).starProjection-
                (((A y).forward I S a c).space (T y)).starProjection‖≤3*ε) ∧
            ∀ x:G,
              Module.finrank ℝ (Wf (P x,P (S^[n] x)))=(T x).card ∧
              Module.finrank ℝ (Wb (P x,P (S.symm^[n] x)))=d-(T x).card ∧
              ‖((A x).forwardSpace I S a c n (T x)).starProjection-
                (Wf (P x,P (S^[n] x))).starProjection‖≤
                  3*(Real.exp ((γ (P x)-g)*N+2*(B+Real.log D₃))*
                    (Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D₃))+N*C₃*C₃^(2*N)*h)) ∧
              ‖((A x).backwardSpace I S a c n (T x)ᶜ).starProjection-
                (Wb (P x,P (S.symm^[n] x))).starProjection‖≤
                  3*(Real.exp ((-γ (P x)-g)*N+2*(B+Real.log D₄))*
                    (Real.exp ((γ (P x)-g)*N+2*(B+Real.log D₄))+N*C₄*C₄^(2*N)*h)) := by
  classical
  obtain ⟨r₁,C₁,r₂,C₂,D,hr₁,hC₁,hr₂,hC₂,hD,hs⟩ :=
    finite_starting_references_uniform I hclosed S a K hK hKU
  obtain ⟨r₃,C₃,D₃,r₄,C₄,D₄,hr₃,hC₃,hD₃,hr₄,hC₄,hD₄,he⟩ :=
    bilateral_endpoint_references_uniform I hclosed S a K hK hKU
  refine ⟨r₁,C₁,r₂,C₂,D,r₃,C₃,D₃,r₄,C₄,D₄,
    hr₁,hC₁,hr₂,hC₂,hD,hr₃,hC₃,hD₃,hr₄,hC₄,hD₄,?_⟩
  intro C c P G n N B R h g A hc hN hco hint hdiam ht₁ ht₂ ht₃ ht₄ hh hg hd hsml hfml hbml
  obtain ⟨j₀,hj₀⟩ := hs C c P G n N B R h g A hc hN hco hint hdiam ht₁ ht₂ hg hd hsml
  let j := FiniteBins.extend P G j₀ (0 : Fin (d+1))
  let γ (q : C) := (1+8*((j q).val:ℝ))*g
  let T (x : G) := fastSet (A x).lam (γ (P x))
  have hj (x : G) : j (P x)=j₀ (key P G x) := extend_key P G j₀ _ x
  have hgap (x : G) (i : Fin d) : (A x).lam i<γ (P x)-g ∨ γ (P x)+g<(A x).lam i := by
    have hh := (hj₀ (key P G x) x rfl).1 i
    simpa only [γ,hj] using hh
  have hsc (x : G) (i : Fin d) (hi : i∈(T x)ᶜ) : (A x).lam i≤γ (P x)-g := by
    have hl : (A x).lam i≤γ (P x) := by simpa only [T,fastSet,Finset.mem_compl,Finset.mem_filter,
      Finset.mem_univ,true_and,not_lt] using hi
    rcases hgap x i with hi | hi
    · exact hi.le
    · linarith
  have hfc (x : G) (i : Fin d) (hi : i∈T x) : γ (P x)+g≤(A x).lam i := by
    have hl : γ (P x)<(A x).lam i := by simpa only [T,fastSet,Finset.mem_filter,
      Finset.mem_univ,true_and] using hi
    rcases hgap x i with hi | hi
    · linarith
    · exact hi.le
  have hcard (x y : G) (hxy : P x=P y) : (T x).card=(T y).card := by
    have hx := (hj₀ (key P G x) x rfl).2.1
    have hy := (hj₀ (key P G x) y hxy.symm).2.1
    dsimp only [T,fastSet,γ]
    rw [←hxy,hj x]
    exact hx.trans hy.symm
  have hi (F : Diffeomorph I I M M ∞) (x : M) (hx : I.IsInteriorPoint x) (t : ℕ) :
      I.IsInteriorPoint (F^[t] x) := by
    induction t with
    | zero => exact hx
    | succ t ih =>
      rw [Function.iterate_succ_apply']
      exact ((F.isLocalDiffeomorph _).isInteriorPoint_iff (by simp)).mp ih
  obtain ⟨Wf,Wb,hW⟩ := he C c P G n N B R h g γ T A hc hN hco
    (fun x hx => ⟨hi S x (hint x hx) n,hi S.symm x (hint x hx) n⟩)
    hdiam ht₃ ht₄ hh hcard hsc hfc (fun q => hfml (j q)) (fun q => hbml (j q))
  refine ⟨j,Wf,Wb,hsc,hfc,hcard,?_,hW⟩
  intro x y
  have hy : P y=P x := (reference P G (key P G x)).property
  have hxs := (hj₀ (key P G x) x rfl).2.2
  dsimp only [T]
  rw [←slowSet_eq_compl,←slowSet_eq_compl]
  simpa only [γ,hy,hj x,slowSet,fastSet] using hxs
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Metric Filter Topology
open scoped Manifold ContDiff
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [TopologicalSpace M] [T2Space M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]

/-- All the one-step tube/Taylor hypotheses used by the boundary-value
solver follow from actual one-step smoothness on the finite compact atlas.
The only variable-domain input is the literal avoidance of the boundary. -/
theorem actual_tube_data (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r C : ℝ,0<r ∧ 1≤C ∧ ∀ (i j : ι) (x : M) (δ : ℝ),
      x∈K i → S x∈K j → 0≤δ → δ≤r →
      closedBall ((extChartAt I (a i)) x) δ⊆interior (range I) →
      closedBall ((extChartAt I (a i)) x) δ⊆(extChartAt I (a i)).target ∧
      closedBall ((extChartAt I (a i)) x) δ⊆domainU I S (a i) (a j) ∧
      ∀ y∈closedBall ((extChartAt I (a i)) x) δ,
        ContDiffAt ℝ ∞ (localMap I S (a i) (a j)) y ∧
        ‖fderiv ℝ (localMap I S (a i) (a j)) y‖≤C ∧
        ‖fderiv ℝ (localMap I S (a i) (a j)) y-
          fderiv ℝ (localMap I S (a i) (a j)) ((extChartAt I (a i)) x)‖≤
            C*‖y-(extChartAt I (a i)) x‖ := by
  obtain ⟨r,C,hr,hC,hh⟩ := actual_uniform_c2 I hclosed S a K hK hKU
  refine ⟨r,C,hr,hC,fun i j x δ hx hSx hδ hδr hsafe => ?_⟩
  have hball := closedBall_subset_closedBall hδr (x:=(extChartAt I (a i)) x)
  have hdat := hh i j x hx hSx
  have hdom := hball.trans hdat.1
  refine ⟨?_,hdom,fun y hy => ?_⟩
  · intro y hy
    rw [extChartAt,OpenPartialHomeomorph.extend_target]
    exact ⟨(hdom hy).1,interior_subset (hsafe hy)⟩
  · have hyd := hdat.2 y (hball hy) (hsafe hy)
    refine ⟨hyd.1,hyd.2.1,?_⟩
    apply (convex_closedBall ((extChartAt I (a i)) x) δ).norm_image_sub_le_of_norm_fderiv_le (𝕜:=ℝ)
    · intro z hz
      exact ((hdat.2 z (hball hz) (hsafe hz)).1.fderiv_right (m:=∞) (by simp)).differentiableAt (by simp)
    · intro z hz
      exact (hdat.2 z (hball hz) (hsafe hz)).2.2
    · exact mem_closedBall_self hδ
    · exact hy
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Metric
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} (S : Diffeomorph I I M M ∞) (a : ι → M) (c : M → ι)
    {n : ℕ} {B R : ℝ} {x : M}
open FiniteGreen ShortBin

lemma BilateralGauge.forward_space (A : BilateralGauge I S a c n B R x)
    (T : Finset (Fin d)) :
    (A.forward I S a c).space T=A.forwardSpace I S a c 0 T := by
  simp only [Gauge.space,BilateralGauge.forward,BilateralGauge.forwardSpace,
    Gauge.ofFinite,Nat.zero_min]

lemma BilateralGauge.backward_space (A : BilateralGauge I S a c n B R x)
    (T : Finset (Fin d)) :
    (A.backward I S a c).space T=A.forwardSpace I S a c 0 T := by
  simp only [Gauge.space,BilateralGauge.backward,BilateralGauge.forwardSpace,
    Gauge.ofFinite,Nat.zero_min,A.centerU,A.centerV]

lemma BilateralGauge.forward_fast (A : BilateralGauge I S a c n B R x)
    (T : Finset (Fin d)) :
    (((A.forward I S a c).splitting T).fast n).range=
      A.forwardSpace I S a c n Tᶜ := by
  rw [Gauge.splitting,EuclideanSplitting.splitting_fast_range]
  simp only [BilateralGauge.forward,BilateralGauge.forwardSpace,Gauge.ofFinite,Nat.min_self]

lemma BilateralGauge.backward_fast (A : BilateralGauge I S a c n B R x)
    (T : Finset (Fin d)) :
    (((A.backward I S a c).splitting T).fast n).range=
      A.backwardSpace I S a c n Tᶜ := by
  rw [Gauge.splitting,EuclideanSplitting.splitting_fast_range]
  simp only [BilateralGauge.backward,BilateralGauge.backwardSpace,Gauge.ofFinite,Nat.min_self]

/-- An old tube tests the candidate in the original input's selected charts.
The candidate is not required to belong to the good set. -/
def originalTube (δ : ℝ) (x z : M) : Prop :=
  ∀ t : Fin (n+1),S^[t.val] z∈(chartAt H (a (c (S^[t.val] x)))).source ∧
    ‖(extChartAt I (a (c (S^[t.val] x)))) (S^[t.val] z)-
      (extChartAt I (a (c (S^[t.val] x)))) (S^[t.val] x)‖≤δ

/-- Fixed endpoint projection, still based on the old input's endpoint chart. -/
def originalEndpoint (V : Submodule ℝ (EuclideanSpace ℝ (Fin d))) (x z : M) : Prop :=
  V.starProjection ((extChartAt I (a (c (S^[n] x)))) (S^[n] z))=
    V.starProjection ((extChartAt I (a (c (S^[n] x)))) (S^[n] x))
end SmoothObstruction.ManifoldCoordinates

namespace SmoothObstruction.ManifoldCoordinates
open Set Metric
open scoped Manifold ContDiff Matrix.Norms.L2Operator
variable {d : ℕ} {H : Type*} [TopologicalSpace H]
    (I : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin d)) H)
    {M : Type*} [MetricSpace M] [ChartedSpace H M] [IsManifold I ∞ M]
    {ι : Type*} [Fintype ι]
open FiniteGreen ShortBin

private lemma projection_diff_iff (V : Submodule ℝ (EuclideanSpace ℝ (Fin d)))
    (u v : EuclideanSpace ℝ (Fin d)) :
    V.orthogonalProjectionOnto (u-v)=0 ↔ V.starProjection u=V.starProjection v := by
  constructor
  · intro h
    have hh := congrArg Subtype.val h
    change V.starProjection (u-v)=0 at hh
    simpa only [map_sub,sub_eq_zero] using hh
  · intro h
    apply Subtype.ext
    change V.starProjection (u-v)=0
    simpa only [map_sub,sub_eq_zero] using h

/-- An actual two-sided joining. The uniform geometry is chosen BEFORE the
window, good gauges, cells, thresholds, or endpoint ranks. Every tube and Taylor
hypothesis is derived here from the source finite atlas; the only tube-domain
condition retained is the literal avoidance of the manifold boundary. -/
theorem original_gauged_join (hclosed : IsClosed (range I))
    (S : Diffeomorph I I M M ∞) (a : ι → M) (K : ι → Set M)
    (hK : ∀ i,IsCompact (K i)) (hKU : ∀ i,K i⊆(chartAt H (a i)).source) :
    ∃ r₀ C : ℝ,0<r₀ ∧ 1≤C ∧
      ∀ (c : M → ι) (n : ℕ) (B R ε γ g α r δ : ℝ) (q x y : M)
        (A₀ : BilateralGauge I S a c n B R q)
        (Ax : BilateralGauge I S a c n B R x)
        (Ay : BilateralGauge I S a c n B R y)
        (T₀ Tx Ty : Finset (Fin d))
        (V W : Submodule ℝ (EuclideanSpace ℝ (Fin d))),
        (∀ v,v∈K (c v)) → c x=c y → I.IsInteriorPoint x → I.IsInteriorPoint y →
        Module.finrank ℝ (Ax.forwardSpace I S a c n Tx)=Module.finrank ℝ V →
        Module.finrank ℝ (Ay.backwardSpace I S a c n Tyᶜ)=Module.finrank ℝ W →
        ‖(Ax.forwardSpace I S a c n Tx).starProjection-V.starProjection‖≤1/2 →
        ‖(Ay.backwardSpace I S a c n Tyᶜ).starProjection-W.starProjection‖≤1/2 →
        0≤ε → ε≤1/2 → T₀.card=Tx.card → T₀.card=Ty.card →
        ‖(A₀.forwardSpace I S a c 0 T₀ᶜ).starProjection-
          (Ax.forwardSpace I S a c 0 Txᶜ).starProjection‖≤ε →
        ‖(A₀.forwardSpace I S a c 0 T₀).starProjection-
          (Ay.forwardSpace I S a c 0 Ty).starProjection‖≤ε →
        Real.exp (2*B)*(2*ε)≤1/2 →
        (∀ j∈Txᶜ,Ax.lam j≤γ-g) → (∀ j∈Tx,γ+g≤Ax.lam j) →
        (∀ j∈Tyᶜ,Ay.lam j≤γ-g) → (∀ j∈Ty,γ+g≤Ay.lam j) →
        (2*Real.exp (2*B))*(2*(2*Real.exp (2*B)^2*Real.exp (-2*g*n)))≤1/2 →
        |γ|+|g|≤α → 0≤r → 0<δ → δ≤r₀ →
        (∀ t : Fin (n+1),closedBall ((extChartAt I (a (c (S^[t.val] x)))) (S^[t.val] x)) δ⊆
          interior (range I)) →
        (∀ t : Fin (n+1),closedBall ((extChartAt I (a (c (S.symm^[t.val] y)))) (S.symm^[t.val] y)) δ⊆
          interior (range I)) →
        ‖(extChartAt I (a (c y))) y-(extChartAt I (a (c x))) x‖≤C*r →
        bvpInverseBound B α n*C*δ≤1/4 → bvpInverseBound B α n*C*r≤δ/4 →
        ∃ z : M,
          originalTube I S a c (n:=n) (δ/2) x z ∧
          originalTube I S.symm a c (n:=n) (δ/2) y z ∧
          originalEndpoint I S a c (n:=n) V x z ∧
          originalEndpoint I S.symm a c (n:=n) W y z ∧
          originalTube I S a c (n:=n) (2*C*bvpInverseBound B α n*r) x z ∧
          originalTube I S.symm a c (n:=n) (2*C*bvpInverseBound B α n*r) y z ∧
          ‖(extChartAt I (a (c x))) z-(extChartAt I (a (c x))) x-
            ((A₀.forward I S a c).splitting T₀ᶜ).slow 0
              ((extChartAt I (a (c y))) y-(extChartAt I (a (c x))) x)‖≤
            4*Real.exp (2*B)*(2*Real.exp (2*B))*(ε+2*Real.exp (2*B)^2*Real.exp (-2*g*n))*(C*r)+
              bvpInverseBound B α n*C*(2*C*bvpInverseBound B α n*r)^2 ∧
          ∀ z' : M,originalTube I S a c (n:=n) δ x z' →
            originalTube I S.symm a c (n:=n) δ y z' →
            originalEndpoint I S a c (n:=n) V x z' →
            originalEndpoint I S.symm a c (n:=n) W y z' → z'=z := by
  obtain ⟨rF,CF,hrF,hCF,hF⟩ := actual_tube_data I hclosed S a K hK hKU
  obtain ⟨rB,CB,hrB,hCB,hB⟩ := actual_tube_data I hclosed S.symm a K hK hKU
  let C := max CF CB
  have hFC : CF≤C := le_max_left _ _
  have hBC : CB≤C := le_max_right _ _
  have hC : 1≤C := hCF.trans hFC
  refine ⟨min rF rB,C,lt_min hrF hrB,hC,?_⟩
  intro c n B R ε γ g α r δ q x y A₀ Ax Ay T₀ Tx Ty V W hc hxy hix hiy
    hdV hdW hcV hcW hε hεhalf hdS hdT hcS hcT hsmall hsx hfx hsy hfy hgap hα hr hδ hδr
    hsafeF hsafeB hnear hcontract hdata
  let af : Fin (n+1) → M := fun t => a (c (S^[t.val] x))
  let ab : Fin (n+1) → M := fun t => a (c (S.symm^[t.val] y))
  have hgeomF (t : Fin (n+1)) := hF (c (S^[t.val] x)) (c (S (S^[t.val] x)))
    (S^[t.val] x) δ (hc _) (hc _) hδ.le (hδr.trans (min_le_left _ _)) (hsafeF t)
  have hgeomB (t : Fin (n+1)) := hB (c (S.symm^[t.val] y)) (c (S.symm (S.symm^[t.val] y)))
    (S.symm^[t.val] y) δ (hc _) (hc _) hδ.le (hδr.trans (min_le_right _ _)) (hsafeB t)
  have hstepF (t : Fin n) : (Matrix.toEuclideanCLM (n:=Fin d) (𝕜:=ℝ)).symm
      (coord I S (af t.castSucc) (af t.succ) (S^[t.val] x))*(Ax.forward I S a c).U t=
        (Ax.forward I S a c).U (t+1)*EuclideanSplitting.rate (Ax.forward I S a c).lam 1 := by
    simpa only [af,Fin.val_castSucc,Fin.val_succ,BilateralGauge.forward,Gauge.ofFinite,
      Nat.min_eq_left t.isLt.le,Nat.min_eq_left (Nat.succ_le_of_lt t.isLt)] using Ax.stepF t t.isLt
  have hstepB (t : Fin n) : (Matrix.toEuclideanCLM (n:=Fin d) (𝕜:=ℝ)).symm
      (coord I S.symm (ab t.castSucc) (ab t.succ) (S.symm^[t.val] y))*(Ay.backward I S a c).U t=
        (Ay.backward I S a c).U (t+1)*EuclideanSplitting.rate (Ay.backward I S a c).lam 1 := by
    simpa only [ab,Fin.val_castSucc,Fin.val_succ,BilateralGauge.backward,Gauge.ofFinite,
      Nat.min_eq_left t.isLt.le,Nat.min_eq_left (Nat.succ_le_of_lt t.isLt)] using Ay.stepB t t.isLt
  have hdV' : Module.finrank ℝ (((Ax.forward I S a c).splitting Txᶜ).fast n).range=
      Module.finrank ℝ V := by
    have he := Ax.forward_fast I S a c Txᶜ
    simp only [compl_compl] at he
    exact (congrArg (fun Z : Submodule ℝ (EuclideanSpace ℝ (Fin d)) => Module.finrank ℝ Z) he).trans hdV
  have hcV' : ‖(((Ax.forward I S a c).splitting Txᶜ).fast n).range.starProjection-
      V.starProjection‖≤1/2 := by simpa only [Ax.forward_fast,compl_compl] using hcV
  have hdW' : Module.finrank ℝ (((Ay.backward I S a c).splitting Ty).fast n).range=
      Module.finrank ℝ W := by
    exact (congrArg (fun Z : Submodule ℝ (EuclideanSpace ℝ (Fin d)) => Module.finrank ℝ Z)
      (Ay.backward_fast I S a c Ty)).trans hdW
  have hcW' : ‖(((Ay.backward I S a c).splitting Ty).fast n).range.starProjection-
      W.starProjection‖≤1/2 := by simpa only [Ay.backward_fast] using hcW
  have hjoin := gauged_join_ideal I S af ab x y
    (by simp only [af,ab,Fin.val_zero,Function.iterate_zero_apply,hxy])
    (fun t => hKU _ (hc _)) (fun t => hKU _ (hc _)) hix hiy
    (A₀.forward I S a c) (Ax.forward I S a c) (Ay.backward I S a c) T₀ᶜ Txᶜ Ty V W
    hdV' hdW' hcV' hcW' hε hεhalf
    (by simp only [Finset.card_compl,hdS]) (by simpa only [compl_compl] using hdT)
    (by simpa only [BilateralGauge.forward_space] using hcS)
    (by simpa only [BilateralGauge.forward_space,BilateralGauge.backward_space,compl_compl] using hcT)
    hsmall hsx (by simpa only [compl_compl,BilateralGauge.forward,Gauge.ofFinite] using hfx)
    (by intro j hj; change -Ay.lam j≤ -γ-g; linarith [hfy j hj])
    (by intro j hj; change -γ+g≤ -Ay.lam j; linarith [hsy j hj])
    hgap hα hstepF hstepB (show 0≤C by linarith) hr hδ
    (by intro t z hz; simpa only [af,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using ((hgeomF t.castSucc).2.2 z hz).1.differentiableAt (by simp))
    (by intro t z hz; simpa only [ab,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using ((hgeomB t.castSucc).2.2 z hz).1.differentiableAt (by simp))
    (by intro t z hz
        simpa only [af,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using
          (((hgeomF t.castSucc).2.2 z hz).2.2).trans (mul_le_mul_of_nonneg_right hFC (norm_nonneg _)))
    (by intro t z hz
        simpa only [ab,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using
          (((hgeomB t.castSucc).2.2 z hz).2.2).trans (mul_le_mul_of_nonneg_right hBC (norm_nonneg _)))
    (fun t => (hgeomF t).1) (fun t => (hgeomB t).1)
    (by intro t; simpa only [af,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using (hgeomF t.castSucc).2.1)
    (by intro t; simpa only [ab,Fin.val_castSucc,Fin.val_succ,Function.iterate_succ_apply'] using (hgeomB t.castSucc).2.1)
    (by simpa only [af,ab,Fin.val_zero,Function.iterate_zero_apply] using hnear) hcontract hdata
  obtain ⟨z,hzF,hzB,hzV,hzW,hsharpF,hsharpB,hideal,hunique⟩ := hjoin
  have hV (z' : M) :
      (((Ax.forward I S a c).splitting Txᶜ).terminalProjection n V hdV' hcV').P
        ((extChartAt I (af (Fin.last n))) (S^[n] z')-
          (extChartAt I (af (Fin.last n))) (S^[n] x))=0 ↔
      originalEndpoint I S a c (n:=n) V x z' := by
    exact projection_diff_iff V _ _
  have hW (z' : M) :
      (((Ay.backward I S a c).splitting Ty).terminalProjection n W hdW' hcW').P
        ((extChartAt I (ab (Fin.last n))) (S.symm^[n] z')-
          (extChartAt I (ab (Fin.last n))) (S.symm^[n] y))=0 ↔
      originalEndpoint I S.symm a c (n:=n) W y z' := by
    exact projection_diff_iff W _ _
  refine ⟨z,hzF,hzB,(hV z).mp hzV,(hW z).mp hzW,?_,?_,hideal,?_⟩
  · exact fun t => ⟨(hzF t).1,hsharpF t⟩
  · exact fun t => ⟨(hzB t).1,hsharpB t⟩
  · intro z' hzF' hzB' hzV' hzW'
    exact hunique z' hzF' hzB' ((hV z').mpr hzV') ((hW z').mpr hzW')
end SmoothObstruction.ManifoldCoordinates

end

end OAI