File size: 26,069 Bytes
720fa25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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