File size: 26,069 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
import Mathlib
import OAI.Geometry.BallPacking.Moments.RearrangementBounds

namespace OAI

noncomputable section
namespace PackingSufficiencySupport.Hamiltonian

section
open scoped ContDiff Topology
open Set Function
variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]

theorem smooth_fderiv_apply_const {D G : Type*} [NormedAddCommGroup D]
    [NormedSpace ℝ D] [NormedAddCommGroup G] [NormedSpace ℝ G] {f : D β†’ G}
    (hf : ContDiff ℝ ∞ f) (p v w : D) :
    fderiv ℝ (fun q => fderiv ℝ f q v) p w = fderiv ℝ (fderiv ℝ f) p w v := by
  have hfd : ContDiff ℝ ∞ (fderiv ℝ f) := hf.fderiv_right (by simp)
  have hh := (hfd.differentiable (by simp) p).hasFDerivAt.clm_apply
    (hasFDerivAt_const v p)
  rw [hh.fderiv]
  simp

theorem joint_spatial_fderiv {F : ℝ Γ— E β†’ E} (hF : ContDiff ℝ ∞ F) (t : ℝ) (x v : E) :
    fderiv ℝ (fun y => F (t,y)) x v = fderiv ℝ F (t,x) (0,v) := by
  have hd := (hF.differentiable (by simp) (t,x)).hasFDerivAt.comp x
    ((hasFDerivAt_const (c := t) x).prodMk (hasFDerivAt_id (π•œ := ℝ) x))
  exact congrArg (fun A : E β†’L[ℝ] E => A v) hd.fderiv

theorem joint_time_fderiv {F X : ℝ Γ— E β†’ E} (hF : ContDiff ℝ ∞ F)
    (hflow : βˆ€ t x, HasDerivAt (fun s => F (s,x)) (X (t,F (t,x))) t) (p : ℝ Γ— E) :
    fderiv ℝ F p (1,0) = X (p.1,F p) := by
  have hd := (hF.differentiable (by simp) p).hasFDerivAt.comp_hasDerivAt p.1
    ((hasDerivAt_id p.1).prodMk (hasDerivAt_const p.1 p.2))
  exact hd.unique (hflow p.1 p.2)

theorem joint_flow_variation {F X : ℝ Γ— E β†’ E} (hF : ContDiff ℝ ∞ F)
    (hX : ContDiff ℝ ∞ X)
    (hflow : βˆ€ t x, HasDerivAt (fun s => F (s,x)) (X (t,F (t,x))) t)
    (t : ℝ) (x v : E) :
    HasDerivAt (fun s => fderiv ℝ F (s,x) (0,v))
      (fderiv ℝ X (t,F (t,x)) (0,fderiv ℝ F (t,x) (0,v))) t := by
  have hFd : ContDiff ℝ ∞ (fderiv ℝ F) := hF.fderiv_right (by simp)
  have hV : ContDiff ℝ ∞ (fun p => fderiv ℝ F p (0,v)) := hFd.clm_apply contDiff_const
  have hd := (hV.differentiable (by simp) (t,x)).hasFDerivAt.comp_hasDerivAt t
    ((hasDerivAt_id t).prodMk (hasDerivAt_const t x))
  have he : (fun p => fderiv ℝ F p (1,0)) = fun p => X (p.1,F p) :=
    funext (joint_time_fderiv hF hflow)
  have hchain := (hX.differentiable (by simp) (t,F (t,x))).hasFDerivAt.comp (t,x)
    ((hasFDerivAt_fst (π•œ := ℝ) (p := (t,x))).prodMk
      (hF.differentiable (by simp) (t,x)).hasFDerivAt)
  have hc := congrArg (fun A : (ℝ Γ— E) β†’L[ℝ] E => A (0,v)) hchain.fderiv
  change fderiv ℝ (fun p => X (p.1,F p)) (t,x) (0,v) = _ at hc
  have he' := congrArg (fun g : ℝ Γ— E β†’ E => fderiv ℝ g (t,x) (0,v)) he
  rw [smooth_fderiv_apply_const hF] at he'
  rw [hc] at he'
  simp only [ContinuousLinearMap.comp_apply,ContinuousLinearMap.prod_apply] at he'
  have hs := (hF.contDiffAt (x := (t,x))).isSymmSndFDerivAt (by
    rw [minSmoothness_of_isRCLikeNormedField]
    change ((2 : β„•βˆž) : WithTop β„•βˆž) ≀ ↑(⊀ : β„•βˆž)
    exact WithTop.coe_le_coe.mpr le_top)
  apply hd.congr_deriv
  rw [smooth_fderiv_apply_const hF,hs.eq (1,0) (0,v)]
  exact he'

theorem smooth_flow_form_transport_on {F X : ℝ Γ— E β†’ E} (hF : ContDiff ℝ ∞ F)
    (hX : ContDiff ℝ ∞ X)
    (hflow : βˆ€ t x, HasDerivAt (fun s => F (s,x)) (X (t,F (t,x))) t)
    {Ξ© : ℝ Γ— E β†’ E β†’L[ℝ] E β†’L[ℝ] ℝ} (hΞ© : ContDiff ℝ ∞ Ξ©)
    {a b : ℝ} (hab : a ≀ b) (x v w : E)
    (hPDE : βˆ€ t ∈ Ioo a b, βˆ€ u z,
      fderiv ℝ Ξ© (t,F (t,x)) (1,X (t,F (t,x))) u z +
      Ξ© (t,F (t,x)) (fderiv ℝ X (t,F (t,x)) (0,u)) z +
      Ξ© (t,F (t,x)) u (fderiv ℝ X (t,F (t,x)) (0,z)) = 0) :
    Ξ© (b,F (b,x)) (fderiv ℝ (fun y => F (b,y)) x v)
      (fderiv ℝ (fun y => F (b,y)) x w) =
    Ξ© (a,F (a,x)) (fderiv ℝ (fun y => F (a,y)) x v)
      (fderiv ℝ (fun y => F (a,y)) x w) := by
  let V (s : ℝ) := fderiv ℝ F (s,x) (0,v)
  let W (s : ℝ) := fderiv ℝ F (s,x) (0,w)
  let G (s : ℝ) := Ξ© (s,F (s,x)) (V s) (W s)
  have hpath : Continuous (fun s => (s,F (s,x))) :=
    continuous_id.prodMk (hF.continuous.comp (continuous_id.prodMk continuous_const))
  have hFd : ContDiff ℝ ∞ (fderiv ℝ F) := hF.fderiv_right (by simp)
  have hVc : Continuous V := ((hFd.continuous.comp
    (continuous_id.prodMk continuous_const)).clm_apply continuous_const)
  have hWc : Continuous W := ((hFd.continuous.comp
    (continuous_id.prodMk continuous_const)).clm_apply continuous_const)
  have hGc : Continuous G := ((hΞ©.continuous.comp hpath).clm_apply hVc).clm_apply hWc
  have hGd (s : ℝ) (hs : s ∈ Ioo a b) : HasDerivAt G 0 s := by
    have hdΞ© := (hΞ©.differentiable (by simp) (s,F (s,x))).hasFDerivAt.comp_hasDerivAt s
      ((hasDerivAt_id s).prodMk (hflow s x))
    have hd := (hdΞ©.clm_apply (joint_flow_variation hF hX hflow s x v)).clm_apply
      (joint_flow_variation hF hX hflow s x w)
    apply hd.congr_deriv
    simpa only [add_apply,Function.comp_apply,id_eq,V,W] using hPDE s hs (V s) (W s)
  have hi := intervalIntegral.integral_eq_sub_of_hasDerivAt_of_le hab
    hGc.continuousOn hGd (intervalIntegrable_const (c := (0:ℝ)))
  have he : G b = G a := sub_eq_zero.mp (by simpa using hi.symm)
  simpa only [G,V,W,joint_spatial_fderiv hF] using he


end

section
open scoped ContDiff BigOperators
open Set Function

@[simp] theorem planeRotate_comp (s t : ℝ) (z : Plane) :
    planeRotate s (planeRotate t z) = planeRotate (s+t) z := by
  apply Prod.ext <;> dsimp [planeRotate] <;> rw [Real.cos_add,Real.sin_add] <;> ring

variable {ΞΉ : Type*} [Fintype ΞΉ] [DecidableEq ΞΉ]

def toricFlow (F : (ΞΉ β†’ ℝ) β†’ ℝ) (t : ℝ) (z : PlanePhase ΞΉ) : PlanePhase ΞΉ :=
  fun i => planeRotate (t*toricAngularSpeed F z i) (z i)

omit [Fintype ΞΉ] in
@[simp] theorem toricFlow_zero (F : (ΞΉ β†’ ℝ) β†’ ℝ) (z : PlanePhase ΞΉ) : toricFlow F 0 z = z := by
  funext i
  simp [toricFlow]

omit [Fintype ΞΉ] in
@[simp] theorem toricFlow_moments (F : (ΞΉ β†’ ℝ) β†’ ℝ) (t : ℝ) (z : PlanePhase ΞΉ) :
    planeMoments (toricFlow F t z) = planeMoments z := by
  funext i
  exact radialArea_planeRotate _ _

omit [Fintype ΞΉ] in
@[simp] theorem toricAngularSpeed_flow (F : (ΞΉ β†’ ℝ) β†’ ℝ) (t : ℝ) (z : PlanePhase ΞΉ) (i : ΞΉ) :
    toricAngularSpeed F (toricFlow F t z) i = toricAngularSpeed F z i := by
  simp [toricAngularSpeed]

omit [Fintype ΞΉ] in
theorem toricFlow_add (F : (ΞΉ β†’ ℝ) β†’ ℝ) (s t : ℝ) (z : PlanePhase ΞΉ) :
    toricFlow F s (toricFlow F t z) = toricFlow F (s+t) z := by
  funext i
  change planeRotate (s*toricAngularSpeed F (toricFlow F t z) i)
    (planeRotate (t*toricAngularSpeed F z i) (z i)) = _
  rw [toricAngularSpeed_flow,planeRotate_comp]
  simp only [toricFlow,add_mul]

omit [DecidableEq ΞΉ] in

theorem phase_rotation_smooth {a : ℝ Γ— PlanePhase ΞΉ β†’ (ΞΉ β†’ ℝ)}
    (ha : ContDiff ℝ ∞ a) :
    ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => fun i => planeRotate (a p i) (p.2 i)) := by
  apply contDiff_pi.mpr
  intro i
  have hi : ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => a p i) :=
    (contDiff_apply ℝ ℝ i).comp ha
  have hz : ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => p.2 i) :=
    (contDiff_apply ℝ Plane i).comp contDiff_snd
  convert! planeRotate_smooth.comp (hi.prodMk hz) using 1

theorem toric_flow_angles_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => fun i => p.1*toricAngularSpeed F p.2 i) := by
  apply contDiff_pi.mpr
  intro i
  have ha : ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => toricAngularSpeed F p.2 i) :=
    (toricAngularSpeed_smooth hF i).comp contDiff_snd
  exact contDiff_fst.mul ha

@[fun_prop] theorem toricFlow_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    ContDiff ℝ ∞ (fun p : ℝ Γ— PlanePhase ΞΉ => toricFlow F p.1 p.2) := by
  exact phase_rotation_smooth (toric_flow_angles_smooth hF)

theorem toricFlow_time_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) (t : ℝ) :
    ContDiff ℝ ∞ (toricFlow F t) := by
  convert! (toricFlow_smooth hF).comp (contDiff_const.prodMk contDiff_id) using 1

def toricFlowHomeomorph {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) (t : ℝ) :
    PlanePhase ΞΉ β‰ƒβ‚œ PlanePhase ΞΉ where
  toFun := toricFlow F t
  invFun := toricFlow F (-t)
  left_inv z := by rw [toricFlow_add,neg_add_cancel,toricFlow_zero]
  right_inv z := by rw [toricFlow_add,add_neg_cancel,toricFlow_zero]
  continuous_toFun := (toricFlow_time_smooth hF t).continuous
  continuous_invFun := (toricFlow_time_smooth hF (-t)).continuous

theorem toricFlow_hasDerivAt {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F)
    (t : ℝ) (z : PlanePhase ΞΉ) :
    HasDerivAt (fun s => toricFlow F s z)
      (hamiltonianField phaseArea (toricHamiltonian F) (t,toricFlow F t z)) t := by
  apply hasDerivAt_pi.mpr
  intro i
  have hd := planeRotate_hasDerivAt ((hasDerivAt_id t).mul_const (toricAngularSpeed F z i)) (z i)
  apply hd.congr_deriv
  rw [toricHamiltonian_field hF,toricAngularSpeed_flow]
  simp only [one_mul,toricFlow,id_eq]

theorem toricFlow_symplectic {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F)
    (t : ℝ) (z v w : PlanePhase ΞΉ) :
    phaseArea (fderiv ℝ (toricFlow F t) z v) (fderiv ℝ (toricFlow F t) z w) = phaseArea v w := by
  have hHs := toricHamiltonian_smooth hF
  have hXs := hamiltonianField_smooth phaseArea_isInvertible hHs
  have he (a b : ℝ) (hab : a ≀ b) :
      phaseArea (fderiv ℝ (toricFlow F b) z v) (fderiv ℝ (toricFlow F b) z w) =
      phaseArea (fderiv ℝ (toricFlow F a) z v) (fderiv ℝ (toricFlow F a) z w) := by
    apply smooth_flow_form_transport_on (toricFlow_smooth hF) hXs (toricFlow_hasDerivAt hF)
      (Ξ© := fun _ => phaseArea) contDiff_const hab z v w
    intro s _ u q
    simpa only [fderiv_const_apply,zero_apply,zero_add] using
      hamiltonianField_transport_equation phaseArea_isInvertible phaseArea_skew hHs
        (s,toricFlow F s z) u q
  have hzero : toricFlow F 0 = id := funext (toricFlow_zero F)
  by_cases ht : 0 ≀ t
  Β· simpa only [hzero,fderiv_id,ContinuousLinearMap.id_apply] using he 0 t ht
  Β· simpa only [hzero,fderiv_id,ContinuousLinearMap.id_apply] using (he t 0 (le_of_not_ge ht)).symm


end

section
open scoped ContDiff
open Set Function
variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]

theorem joint_flow_fderiv {F X : ℝ Γ— E β†’ E} (hF : ContDiff ℝ ∞ F)
    (hflow : βˆ€ t x, HasDerivAt (fun s => F (s,x)) (X (t,F (t,x))) t)
    (t Ο„ : ℝ) (x v : E) :
    fderiv ℝ F (t,x) (Ο„,v) = Ο„ β€’ X (t,F (t,x)) +
      fderiv ℝ (fun y => F (t,y)) x v := by
  rw [show (Ο„,v) = Ο„ β€’ ((1,0) : ℝ Γ— E) + (0,v) by ext <;> simp,
    map_add,map_smul,joint_time_fderiv hF hflow,joint_spatial_fderiv hF]

theorem suspension_form_identity {A : E β†’L[ℝ] E β†’L[ℝ] ℝ}
    (hskew : βˆ€ v w, A v w = -A w v) {D : E β†’L[ℝ] E} {X : E} {Ξ· : E β†’L[ℝ] ℝ}
    (hsp : βˆ€ v w, A (D v) (D w) = A v w)
    (hX : βˆ€ v, A X (D v) = -Ξ· v) (v w : Plane Γ— E) :
    productForm A (v.1,v.1.2 β€’ X + D v.2) (w.1,w.1.2 β€’ X + D w.2) =
      oneCovectorForm A 1 Ξ· v w := by
  have hXX : A X X = 0 := by have hh := hskew X X; linarith
  have hright : A (D v.2) X = Ξ· v.2 := by rw [hskew,hX,neg_neg]
  simp only [productForm_apply,oneCovectorForm_apply,planarArea_apply,map_add,map_smul,
    add_apply,smul_apply,smul_eq_mul,hXX,hsp,hX,hright]
  ring

variable {ΞΉ : Type*} [Fintype ΞΉ] [DecidableEq ΞΉ]

def toricFunction (F : (ΞΉ β†’ ℝ) β†’ ℝ) (z : PlanePhase ΞΉ) : ℝ := F (planeMoments z)

omit [DecidableEq ΞΉ] in
@[fun_prop] theorem toricFunction_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    ContDiff ℝ ∞ (toricFunction F) := hF.comp planeMoments_smooth

theorem toricFlow_energy_differential {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F)
    (t : ℝ) (z v : PlanePhase ΞΉ) :
    fderiv ℝ (toricFunction F) (toricFlow F t z) (fderiv ℝ (toricFlow F t) z v) =
      fderiv ℝ (toricFunction F) z v := by
  have hs : ContDiff ℝ ∞ (toricFlow F t) := toricFlow_time_smooth hF t
  have he : toricFunction F ∘ toricFlow F t = toricFunction F := by
    funext z; simp [toricFunction]
  have hd := fderiv_comp z ((toricFunction_smooth hF).differentiable (by simp) _)
    (hs.differentiable (by simp) _)
  rw [he] at hd
  exact (congrArg (fun L : PlanePhase ΞΉ β†’L[ℝ] ℝ => L v) hd).symm

def toricSuspension (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) : Plane Γ— PlanePhase ΞΉ :=
  (p.1,toricFlow F p.1.2 p.2)

@[fun_prop] theorem toricSuspension_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    ContDiff ℝ ∞ (toricSuspension F) := by
  have ht : ContDiff ℝ ∞ (fun p : Plane Γ— PlanePhase ΞΉ => p.1.2) :=
    contDiff_snd.comp contDiff_fst
  have hz : ContDiff ℝ ∞ (fun p : Plane Γ— PlanePhase ΞΉ => toricFlow F p.1.2 p.2) := by
    convert! (toricFlow_smooth hF).comp (ht.prodMk contDiff_snd) using 1
  exact contDiff_fst.prodMk hz

theorem toricSuspension_fderiv {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F)
    (p v : Plane Γ— PlanePhase ΞΉ) :
    fderiv ℝ (toricSuspension F) p v =
      (v.1,v.1.2 β€’ hamiltonianField phaseArea (toricHamiltonian F)
        (p.1.2,toricFlow F p.1.2 p.2) + fderiv ℝ (toricFlow F p.1.2) p.2 v.2) := by
  have hg := (toricFlow_smooth hF).differentiable (by simp)
  have hc := (hg (p.1.2,p.2)).hasFDerivAt.comp p
    (((hasFDerivAt_snd (π•œ := ℝ) (p := p.1)).comp p
      (hasFDerivAt_fst (π•œ := ℝ) (p := p))).prodMk
        (hasFDerivAt_snd (π•œ := ℝ) (p := p)))
  have hd := (hasFDerivAt_fst (π•œ := ℝ) (p := p)).prodMk hc
  change HasFDerivAt (toricSuspension F) _ p at hd
  rw [hd.fderiv]
  change (v.1,fderiv ℝ (fun q : ℝ Γ— PlanePhase ΞΉ => toricFlow F q.1 q.2)
    (p.1.2,p.2) (v.1.2,v.2)) = _
  rw [joint_flow_fderiv (toricFlow_smooth hF) (toricFlow_hasDerivAt hF)]

theorem toricSuspension_pullback {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F)
    (p v w : Plane Γ— PlanePhase ΞΉ) :
    productForm phaseArea (fderiv ℝ (toricSuspension F) p v)
      (fderiv ℝ (toricSuspension F) p w) =
    oneCovectorForm phaseArea 1 (fderiv ℝ (toricFunction F) p.2) v w := by
  rw [toricSuspension_fderiv hF,toricSuspension_fderiv hF]
  apply suspension_form_identity phaseArea_skew (toricFlow_symplectic hF p.1.2 p.2)
  intro u
  rw [hamiltonianField_contraction phaseArea_isInvertible]
  have hh : fderiv ℝ (toricHamiltonian F) (p.1.2,toricFlow F p.1.2 p.2)
      (0,fderiv ℝ (toricFlow F p.1.2) p.2 u) =
      fderiv ℝ (toricFunction F) (toricFlow F p.1.2 p.2)
        (fderiv ℝ (toricFlow F p.1.2) p.2 u) := by
    have hd := ((toricFunction_smooth hF).differentiable (by simp) _).hasFDerivAt.comp
      (p.1.2,toricFlow F p.1.2 p.2) (hasFDerivAt_snd (π•œ := ℝ))
    exact congrArg (fun L : (ℝ Γ— PlanePhase ΞΉ) β†’L[ℝ] ℝ =>
      L (0,fderiv ℝ (toricFlow F p.1.2) p.2 u)) hd.fderiv
  rw [hh,toricFlow_energy_differential hF]


end

section
open scoped ContDiff
open Set Function
variable {ΞΉ : Type*} [Fintype ΞΉ] [DecidableEq ΞΉ]

def toricSuspensionInverse (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) : Plane Γ— PlanePhase ΞΉ :=
  (p.1,toricFlow F (-p.1.2) p.2)

@[fun_prop] theorem toricSuspensionInverse_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    ContDiff ℝ ∞ (toricSuspensionInverse F) := by
  have ht : ContDiff ℝ ∞ (fun p : Plane Γ— PlanePhase ΞΉ => -p.1.2) :=
    (contDiff_snd.comp contDiff_fst).neg
  have hz : ContDiff ℝ ∞ (fun p : Plane Γ— PlanePhase ΞΉ => toricFlow F (-p.1.2) p.2) := by
    convert! (toricFlow_smooth hF).comp (ht.prodMk contDiff_snd) using 1
  exact contDiff_fst.prodMk hz

omit [Fintype ΞΉ] in
@[simp] theorem toricSuspensionInverse_left (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) :
    toricSuspensionInverse F (toricSuspension F p) = p := by
  simp only [toricSuspensionInverse,toricSuspension,toricFlow_add,neg_add_cancel,toricFlow_zero]

omit [Fintype ΞΉ] in
@[simp] theorem toricSuspensionInverse_right (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) :
    toricSuspension F (toricSuspensionInverse F p) = p := by
  simp only [toricSuspensionInverse,toricSuspension,toricFlow_add,add_neg_cancel,toricFlow_zero]

def toricSuspensionHomeomorph {F : (ΞΉ β†’ ℝ) β†’ ℝ} (hF : ContDiff ℝ ∞ F) :
    (Plane Γ— PlanePhase ΞΉ) β‰ƒβ‚œ (Plane Γ— PlanePhase ΞΉ) where
  toFun := toricSuspension F
  invFun := toricSuspensionInverse F
  left_inv := toricSuspensionInverse_left F
  right_inv := toricSuspensionInverse_right F
  continuous_toFun := (toricSuspension_smooth hF).continuous
  continuous_invFun := (toricSuspensionInverse_smooth hF).continuous

omit [Fintype ΞΉ] in
@[simp] theorem toricSuspension_moments (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) :
    planeMoments (toricSuspension F p).2 = planeMoments p.2 := toricFlow_moments F _ _

omit [Fintype ΞΉ] in
@[simp] theorem toricSuspensionInverse_moments (F : (ΞΉ β†’ ℝ) β†’ ℝ) (p : Plane Γ— PlanePhase ΞΉ) :
    planeMoments (toricSuspensionInverse F p).2 = planeMoments p.2 := toricFlow_moments F _ _


end

section
open scoped ContDiff Topology
open Set Function
variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E]

def layerOrder : (Plane Γ— E) ≃L[ℝ] ((ℝ Γ— E) Γ— ℝ) :=
  (ContinuousLinearEquiv.prodAssoc ℝ ℝ ℝ E).trans (ContinuousLinearEquiv.prodComm ℝ ℝ (ℝ Γ— E))

omit [CompleteSpace E] in
@[simp] theorem layerOrder_apply (p : Plane Γ— E) : layerOrder p = ((p.1.2,p.2),p.1.1) := rfl
omit [CompleteSpace E] in
@[simp] theorem layerOrder_symm_apply (p : (ℝ Γ— E) Γ— ℝ) : layerOrder.symm p = ((p.2,p.1.1),p.1.2) := rfl

def stripChart (f : Plane Γ— E β†’ ℝ) (p : Plane Γ— E) : Plane Γ— E := ((f p,p.1.2),p.2)

omit [CompleteSpace E] in
@[fun_prop] theorem stripChart_smooth {f : Plane Γ— E β†’ ℝ} (hf : ContDiff ℝ ∞ f) :
    ContDiff ℝ ∞ (stripChart f) := (hf.prodMk (contDiff_snd.comp contDiff_fst)).prodMk contDiff_snd

theorem stripChart_inverse {f : Plane Γ— E β†’ ℝ} (hf : ContDiff ℝ ∞ f)
    {V : Set E} (hV : IsOpen V) {a b : ℝ} (hab : a < b) (H : E β†’ ℝ)
    (hderiv : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v ∈ V, βˆ€ s ∈ Ioo a b,
      0 < deriv (fun r => f ((r,T),v)) s)
    (hlo : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v ∈ V, f ((a,T),v) = 0)
    (hhi : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v ∈ V, H v < f ((b,T),v)) :
    βˆƒ (W : Set (Plane Γ— E)) (g : Plane Γ— E β†’ Plane Γ— E),
      IsOpen W ∧ ContDiffOn ℝ ∞ g W ∧
      W = stripChart f '' ((Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ V) ∧
      (βˆ€ p, p.1.2 ∈ Ioo (0:ℝ) 1 β†’ p.2 ∈ V β†’ 0 < p.1.1 β†’ p.1.1 < H p.2 β†’ p ∈ W) ∧
      (βˆ€ p ∈ (Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ V, g (stripChart f p) = p) ∧
      (βˆ€ p ∈ W, g p ∈ (Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ V ∧ stripChart f (g p) = p) := by
  let e : (Plane Γ— E) ≃L[ℝ] ((ℝ Γ— E) Γ— ℝ) := layerOrder
  let f' : (ℝ Γ— E) Γ— ℝ β†’ ℝ := f ∘ e.symm
  have hf' : ContDiff ℝ ∞ f' := hf.comp e.symm.contDiff
  obtain ⟨W,g,hWo,hgs,hWe,havailable,hleft,hright⟩ := monotone_strip_inverse hf'
    (isOpen_Ioo.prod hV) hab (h := fun q : ℝ Γ— E => H q.2)
    (fun q hq s hs => hderiv q.1 hq.1 q.2 hq.2 s hs)
    (fun q hq => hlo q.1 hq.1 q.2 hq.2)
    (fun q hq => hhi q.1 hq.1 q.2 hq.2)
  let G : Plane Γ— E β†’ Plane Γ— E := fun p => ((g (e p),p.1.2),p.2)
  refine ⟨e ⁻¹' W,G,hWo.preimage e.continuous,?_,?_,?_,?_,?_⟩
  Β· have hg : ContDiffOn ℝ ∞ (fun p => g (e p)) (e ⁻¹' W) :=
      hgs.comp e.contDiff.contDiffOn (fun _ hp => hp)
    exact (hg.prodMk (contDiff_snd.comp contDiff_fst).contDiffOn).prodMk contDiff_snd.contDiffOn
  Β· ext p
    constructor
    Β· intro hp
      obtain ⟨q,hq,hqe⟩ := by simpa only [hWe] using hp
      refine ⟨e.symm q,⟨⟨hq.2,hq.1.1⟩,hq.1.2⟩,?_⟩
      apply e.injective
      change (q.1,f' q) = e p
      exact hqe
    · rintro ⟨q,hq,rfl⟩
      change e (stripChart f q) ∈ W
      rw [hWe]
      exact ⟨e q,⟨⟨hq.1.2,hq.2⟩,hq.1.1⟩,rfl⟩
  Β· intro p hpT hpV hp0 hpH
    exact havailable (p.1.2,p.2) ⟨hpT,hpV⟩ p.1.1 hp0 hpH
  Β· intro p hp
    have hh := hleft (p.1.2,p.2) ⟨hp.1.2,hp.2⟩ p.1.1 hp.1.1
    change ((g ((p.1.2,p.2),f p),p.1.2),p.2) = p
    change g ((p.1.2,p.2),f p) = p.1.1 at hh
    rw [hh]
  Β· intro p hp
    have hh := hright (e p) hp
    refine ⟨⟨⟨hh.2.1,hh.1.1⟩,hh.1.2⟩,?_⟩
    change ((f ((g (e p),p.1.2),p.2),p.1.2),p.2) = p
    have he : f ((g (e p),p.1.2),p.2) = p.1.1 := hh.2.2
    rw [he]


end

open scoped ContDiff Topology
open Set Function
variable {ΞΉ : Type*} [Fintype ΞΉ] [DecidableEq ΞΉ]

def layerChart (F : (ΞΉ β†’ ℝ) β†’ ℝ) (f : Plane Γ— PlanePhase ΞΉ β†’ ℝ) :
    Plane Γ— PlanePhase ΞΉ β†’ Plane Γ— PlanePhase ΞΉ := toricSuspension F ∘ stripChart f

@[fun_prop] theorem layerChart_smooth {F : (ΞΉ β†’ ℝ) β†’ ℝ} {f : Plane Γ— PlanePhase ΞΉ β†’ ℝ}
    (hF : ContDiff ℝ ∞ F) (hf : ContDiff ℝ ∞ f) : ContDiff ℝ ∞ (layerChart F f) :=
  (toricSuspension_smooth hF).comp (stripChart_smooth hf)

def layerForm (F : (ΞΉ β†’ ℝ) β†’ ℝ) (f : Plane Γ— PlanePhase ΞΉ β†’ ℝ)
    (p : Plane Γ— PlanePhase ΞΉ) :
    (Plane Γ— PlanePhase ΞΉ) β†’L[ℝ] (Plane Γ— PlanePhase ΞΉ) β†’L[ℝ] ℝ :=
  let dt := (ContinuousLinearMap.snd ℝ ℝ ℝ).comp (ContinuousLinearMap.fst ℝ Plane (PlanePhase ΞΉ))
  let Ξ² := fderiv ℝ f p + (fderiv ℝ (toricFunction F) p.2).comp (ContinuousLinearMap.snd ℝ Plane (PlanePhase ΞΉ))
  phaseArea.bilinearComp (ContinuousLinearMap.snd ℝ Plane (PlanePhase ΞΉ))
    (ContinuousLinearMap.snd ℝ Plane (PlanePhase ΞΉ)) + Ξ².smulRight dt - dt.smulRight Ξ²

omit [DecidableEq ΞΉ] in
theorem layerForm_apply (F : (ΞΉ β†’ ℝ) β†’ ℝ) (f : Plane Γ— PlanePhase ΞΉ β†’ ℝ)
    (p v w : Plane Γ— PlanePhase ΞΉ) :
    layerForm F f p v w = phaseArea v.2 w.2 +
      (fderiv ℝ f p v + fderiv ℝ (toricFunction F) p.2 v.2)*w.1.2 -
      v.1.2*(fderiv ℝ f p w + fderiv ℝ (toricFunction F) p.2 w.2) := by
  simp [layerForm,ContinuousLinearMap.smulRight_apply,smul_eq_mul,mul_add]

omit [DecidableEq ΞΉ] in
theorem stripChart_fderiv {f : Plane Γ— PlanePhase ΞΉ β†’ ℝ} (hf : ContDiff ℝ ∞ f)
    (p v : Plane Γ— PlanePhase ΞΉ) : fderiv ℝ (stripChart f) p v = ((fderiv ℝ f p v,v.1.2),v.2) := by
  have hd := (((hf.differentiable (by simp) p).hasFDerivAt).prodMk
    ((hasFDerivAt_snd (π•œ := ℝ) (p := p.1)).comp p (hasFDerivAt_fst (π•œ := ℝ) (p := p)))).prodMk
      (hasFDerivAt_snd (π•œ := ℝ) (p := p))
  change HasFDerivAt (stripChart f) _ p at hd
  rw [hd.fderiv]
  rfl

theorem layerChart_pullback {F : (ΞΉ β†’ ℝ) β†’ ℝ} {f : Plane Γ— PlanePhase ΞΉ β†’ ℝ}
    (hF : ContDiff ℝ ∞ F) (hf : ContDiff ℝ ∞ f) (p v w : Plane Γ— PlanePhase ΞΉ) :
    productForm phaseArea (fderiv ℝ (layerChart F f) p v)
      (fderiv ℝ (layerChart F f) p w) = layerForm F f p v w := by
  rw [layerChart,fderiv_comp p ((toricSuspension_smooth hF).differentiable (by simp) _)
    ((stripChart_smooth hf).differentiable (by simp) _)]
  simp only [ContinuousLinearMap.comp_apply]
  rw [toricSuspension_pullback hF,stripChart_fderiv hf,stripChart_fderiv hf,
    oneCovectorForm_apply,layerForm_apply]
  change phaseArea v.2 w.2 + 1*(fderiv ℝ f p v*w.1.2-v.1.2*fderiv ℝ f p w) +
    fderiv ℝ (toricFunction F) p.2 v.2*w.1.2-v.1.2*fderiv ℝ (toricFunction F) p.2 w.2 = _
  ring

theorem layerChart_inverse {F : (ΞΉ β†’ ℝ) β†’ ℝ} {f : Plane Γ— PlanePhase ΞΉ β†’ ℝ}
    (hF : ContDiff ℝ ∞ F) (hf : ContDiff ℝ ∞ f)
    {V : Set (ΞΉ β†’ ℝ)} (hV : IsOpen (planeMoments ⁻¹' V)) {a b : ℝ} (hab : a < b)
    (H : (ΞΉ β†’ ℝ) β†’ ℝ)
    (hderiv : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v, planeMoments v ∈ V β†’ βˆ€ s ∈ Ioo a b,
      0 < deriv (fun r => f ((r,T),v)) s)
    (hlo : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v, planeMoments v ∈ V β†’ f ((a,T),v) = 0)
    (hhi : βˆ€ T ∈ Ioo (0:ℝ) 1, βˆ€ v, planeMoments v ∈ V β†’ H (planeMoments v) < f ((b,T),v)) :
    βˆƒ (W : Set (Plane Γ— PlanePhase ΞΉ)) (g : Plane Γ— PlanePhase ΞΉ β†’ Plane Γ— PlanePhase ΞΉ),
      IsOpen W ∧ ContDiffOn ℝ ∞ g W ∧
      W = layerChart F f '' ((Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ (planeMoments ⁻¹' V)) ∧
      (βˆ€ p, p.1.2 ∈ Ioo (0:ℝ) 1 β†’ planeMoments p.2 ∈ V β†’
        0 < p.1.1 β†’ p.1.1 < H (planeMoments p.2) β†’ p ∈ W) ∧
      (βˆ€ p ∈ (Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ (planeMoments ⁻¹' V), g (layerChart F f p) = p) ∧
      (βˆ€ p ∈ W, g p ∈ (Ioo a b Γ—Λ’ Ioo (0:ℝ) 1) Γ—Λ’ (planeMoments ⁻¹' V) ∧ layerChart F f (g p) = p) := by
  obtain ⟨W,g,hWo,hgs,hWe,hav,hl,hr⟩ := stripChart_inverse hf hV hab (H ∘ planeMoments) hderiv hlo hhi
  let e := toricSuspensionHomeomorph hF
  let G := g ∘ toricSuspensionInverse F
  have hpre : e.symm ⁻¹' W = e '' W := by
    ext p
    exact ⟨fun hp => ⟨e.symm p,hp,e.apply_symm_apply p⟩,by rintro ⟨p,hp,rfl⟩; simpa using hp⟩
  refine ⟨e '' W,G,e.isOpen_image.mpr hWo,?_,?_,?_,?_,?_⟩
  Β· apply hgs.comp (toricSuspensionInverse_smooth hF).contDiffOn
    intro p hp
    change p ∈ e.symm ⁻¹' W
    rw [hpre]
    exact hp
  Β· rw [hWe,←image_comp]
    rfl
  Β· intro p hpT hpV hp0 hpH
    refine ⟨e.symm p,?_,e.apply_symm_apply p⟩
    apply hav (e.symm p) hpT
    · change planeMoments (toricSuspensionInverse F p).2 ∈ V
      simpa only [toricSuspensionInverse_moments] using hpV
    Β· exact hp0
    Β· change p.1.1 < H (planeMoments (toricSuspensionInverse F p).2)
      simpa using hpH
  Β· intro p hp
    change g (toricSuspensionInverse F (toricSuspension F (stripChart f p))) = p
    rw [toricSuspensionInverse_left]
    exact hl p hp
  · rintro p ⟨z,hz,rfl⟩
    have hge : G (e z) = g z := by
      change g (toricSuspensionInverse F (toricSuspension F z)) = g z
      rw [toricSuspensionInverse_left]
    rw [hge]
    refine ⟨(hr z hz).1,?_⟩
    change toricSuspension F (stripChart f (g z)) = toricSuspension F z
    rw [(hr z hz).2]



end PackingSufficiencySupport.Hamiltonian
end

end OAI