import OAI.Geometry.IsometricImmersion.Flows.FlowParameterContinuity import Mathlib.Analysis.Calculus.Deriv.Slope namespace OAI noncomputable section open Set Metric Filter Function MeasureTheory open scoped ContDiff Topology Interval namespace SmoothLocal.Flow open SmoothLocal.Geometry SmoothLocal.ODE SmoothLocal.Weighted theorem hasDerivAt_of_exact_secant {f g : ℝ → ℝ} {s : ℝ} (hg : ContinuousAt g s) (he : ∀ᶠ u in 𝓝 s, f u - f s = (u - s) * g u) : HasDerivAt f (g s) s := by rw [hasDerivAt_iff_tendsto_slope] have hslope : slope f s =ᶠ[𝓝[≠] s] g := by filter_upwards [he.filter_mono nhdsWithin_le_nhds, self_mem_nhdsWithin] with u hu hne have hne' : u ≠ s := by simpa only [mem_compl_iff, mem_singleton_iff] using hne rw [slope_def_field, hu] exact mul_div_cancel_left₀ (g u) (sub_ne_zero.mpr hne') exact (hg.mono_left nhdsWithin_le_nhds).congr' hslope.symm variable {q : Coord → ℝ} {U : Set Coord} {Y : ℝ → ℝ → ℝ} theorem flow_difference_formula (hq : ContDiffOn ℝ ∞ q U) (hU : IsOpen U) (hSU : modelSquare ⊆ U) (hY : ContinuousOn (uncurry Y) (Icc (-2 : ℝ) 2 ×ˢ Icc (-2 : ℝ) 2)) (hrange : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, Y s t ∈ Icc (-3 : ℝ) 3) (hstart : ∀ s ∈ Icc (-2 : ℝ) 2, Y s 0 = s) (hode : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, HasDerivWithinAt (Y s) (-q (coordinatePoint t (Y s t))) (Icc (-2 : ℝ) 2) t) {s0 s1 t : ℝ} (hs0 : s0 ∈ Icc (-2 : ℝ) 2) (hs1 : s1 ∈ Icc (-2 : ℝ) 2) (ht : t ∈ Ioo (-2 : ℝ) 2) : Y s1 t - Y s0 t = (s1 - s0) * Real.exp (-(∫ u in 0..t, flowAverageCoefficient q Y s0 s1 u)) := by have hcoeff := flowAverageCoefficient_continuous hq hU hSU hY hrange have ha : ContinuousOn (flowAverageCoefficient q Y s0 s1) (Icc (-2 : ℝ) 2) := by rw [continuousOn_iff_continuous_domRestrict] exact hcoeff.comp ((continuous_const (y := (⟨s0, hs0⟩ : CapInterval))).prodMk ((continuous_const (y := (⟨s1, hs1⟩ : CapInterval))).prodMk continuous_id)) have hz (u : ℝ) (hu : u ∈ Ioo (-2 : ℝ) 2) : HasDerivAt (fun v => Y s1 v - Y s0 v) (-flowAverageCoefficient q Y s0 s1 u * (Y s1 u - Y s0 u)) u := by exact (flow_difference_hasDerivWithinAt hq hU hSU ⟨hu.1.le, hu.2.le⟩ (hrange s0 hs0 u ⟨hu.1.le, hu.2.le⟩) (hrange s1 hs1 u ⟨hu.1.le, hu.2.le⟩) (hode s0 hs0 u ⟨hu.1.le, hu.2.le⟩) (hode s1 hs1 u ⟨hu.1.le, hu.2.le⟩)).hasDerivAt (Icc_mem_nhds hu.1 hu.2) have he := scalar_linear_evolution_formula (show (0 : ℝ) < 2 by norm_num) (ha.mono Ioo_subset_Icc_self) hz ht simpa only [hstart s0 hs0, hstart s1 hs1] using he theorem cap_flow_hasDerivAt_initial (hq : ContDiffOn ℝ ∞ q U) (hU : IsOpen U) (hSU : modelSquare ⊆ U) (hY : ContinuousOn (uncurry Y) (Icc (-2 : ℝ) 2 ×ˢ Icc (-2 : ℝ) 2)) (hrange : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, Y s t ∈ Icc (-3 : ℝ) 3) (hstart : ∀ s ∈ Icc (-2 : ℝ) 2, Y s 0 = s) (hode : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, HasDerivWithinAt (Y s) (-q (coordinatePoint t (Y s t))) (Icc (-2 : ℝ) 2) t) {s t : ℝ} (hs : s ∈ Ioo (-2 : ℝ) 2) (ht : t ∈ Ioo (-2 : ℝ) 2) : HasDerivAt (fun u => Y u t) (Real.exp (-(∫ r in 0..t, coordPartial 1 q (coordinatePoint r (Y s r))))) s := by let G (u : ℝ) := Real.exp (-(∫ r in 0..t, flowAverageCoefficient q Y s u r)) have hG : ContinuousOn G (Icc (-2 : ℝ) 2) := by rw [continuousOn_iff_continuous_domRestrict] exact (((flowAverageIntegral_continuous hq hU hSU hY hrange ⟨ht.1.le, ht.2.le⟩).comp ((continuous_const (y := (⟨s, ⟨hs.1.le, hs.2.le⟩⟩ : CapInterval))).prodMk continuous_id)).neg).rexp have he : ∀ᶠ u in 𝓝 s, Y u t - Y s t = (u - s) * G u := by filter_upwards [Icc_mem_nhds hs.1 hs.2] with u hu exact flow_difference_formula hq hU hSU hY hrange hstart hode ⟨hs.1.le, hs.2.le⟩ hu ht have hd := hasDerivAt_of_exact_secant (hG.continuousAt (Icc_mem_nhds hs.1 hs.2)) he have hdiag : G s = Real.exp (-(∫ r in 0..t, coordPartial 1 q (coordinatePoint r (Y s r)))) := by simp only [G, flowAverageCoefficient, verticalAverageDerivative_diagonal] rw [hdiag] at hd exact hd theorem cap_flow_initial_deriv_pos (hq : ContDiffOn ℝ ∞ q U) (hU : IsOpen U) (hSU : modelSquare ⊆ U) (hY : ContinuousOn (uncurry Y) (Icc (-2 : ℝ) 2 ×ˢ Icc (-2 : ℝ) 2)) (hrange : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, Y s t ∈ Icc (-3 : ℝ) 3) (hstart : ∀ s ∈ Icc (-2 : ℝ) 2, Y s 0 = s) (hode : ∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, HasDerivWithinAt (Y s) (-q (coordinatePoint t (Y s t))) (Icc (-2 : ℝ) 2) t) {s t : ℝ} (hs : s ∈ Ioo (-2 : ℝ) 2) (ht : t ∈ Ioo (-2 : ℝ) 2) : 0 < deriv (fun u => Y u t) s := by rw [(cap_flow_hasDerivAt_initial hq hU hSU hY hrange hstart hode hs ht).deriv] exact Real.exp_pos _ theorem exists_cap_flow_first_variation (hq : ContDiffOn ℝ ∞ q U) (hU : IsOpen U) (hSU : modelSquare ⊆ U) (hsmall : ∀ p ∈ modelSquare, |q p| ≤ (1 : ℝ) / 100) : ∃ Y : ℝ → ℝ → ℝ, ContinuousOn (uncurry Y) (Icc (-2 : ℝ) 2 ×ˢ Icc (-2 : ℝ) 2) ∧ (∀ s ∈ Icc (-2 : ℝ) 2, ContDiffOn ℝ ∞ (Y s) (Icc (-2 : ℝ) 2)) ∧ (∀ s ∈ Icc (-2 : ℝ) 2, Y s 0 = s) ∧ (∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, HasDerivWithinAt (Y s) (-q (coordinatePoint t (Y s t))) (Icc (-2 : ℝ) 2) t) ∧ (∀ s ∈ Icc (-2 : ℝ) 2, ∀ t ∈ Icc (-2 : ℝ) 2, |Y s t - s| ≤ (1 : ℝ) / 50) ∧ (∀ s ∈ Ioo (-2 : ℝ) 2, ∀ t ∈ Ioo (-2 : ℝ) 2, HasDerivAt (fun u => Y u t) (Real.exp (-(∫ r in 0..t, coordPartial 1 q (coordinatePoint r (Y s r))))) s ∧ 0 < deriv (fun u => Y u t) s) := by obtain ⟨Y, hY, htime, hstart, hode, hdisp, hdisp', hrange, hLip⟩ := exists_cap_flow hq hU hSU hsmall have hrange' (s : ℝ) (hs : s ∈ Icc (-2 : ℝ) 2) (t : ℝ) (ht : t ∈ Icc (-2 : ℝ) 2) : Y s t ∈ Icc (-3 : ℝ) 3 := abs_le.mp ((hrange s hs t ht).1.trans (by norm_num)) refine ⟨Y, hY, htime, hstart, hode, hdisp', ?_⟩ intro s hs t ht exact ⟨cap_flow_hasDerivAt_initial hq hU hSU hY hrange' hstart hode hs ht, cap_flow_initial_deriv_pos hq hU hSU hY hrange' hstart hode hs ht⟩ end SmoothLocal.Flow end end OAI