import Mathlib import OAI.Analysis.BiholderTransport.Geodesics.SprayProjection import OAI.Analysis.BiholderTransport.Coordinates.JointNormalChart import OAI.Analysis.BiholderTransport.Coordinates.NormalGradient namespace OAI noncomputable section open Set MeasureTheory Manifold Bundle open scoped ContDiff Manifold ENNReal NNReal Topology open Set Filter open scoped Topology NNReal open Set Filter open scoped Topology open Set Manifold MeasureTheory Bundle open scoped ENNReal ContDiff Topology open Set open scoped Topology open Set Filter Manifold Bundle ContinuousLinearMap open scoped Topology ContDiff Manifold Bundle open Set Filter ContinuousLinearMap InnerProductSpace open scoped Topology ContDiff open Set Filter ContinuousLinearMap open scoped Topology ContDiff open Set Filter ContinuousLinearMap open scoped Topology ContDiff open Set Filter ContinuousLinearMap open scoped Topology ContDiff open scoped NNReal open Set Filter ContinuousLinearMap open scoped Topology ContDiff open Set Filter ContinuousLinearMap open scoped Topology open MeasureTheory open scoped ContDiff ENNReal open Set Filter Manifold Bundle ContinuousLinearMap MeasureTheory open scoped Topology ContDiff Manifold Bundle ENNReal open Set Filter Manifold MeasureTheory Bundle open scoped ENNReal ContDiff Topology Manifold open Set Filter Manifold Bundle ContinuousLinearMap open scoped Topology ContDiff Manifold Bundle open Set Filter Manifold Bundle open scoped Topology ContDiff Manifold Bundle open Set Filter Manifold Bundle open scoped Topology ContDiff Manifold Bundle open Set Filter Bundle open scoped Topology Bundle open scoped Topology open Function Manifold Set open Manifold Bundle open scoped Manifold Bundle open Set namespace WeakMTWTransport variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E] [FiniteDimensional ℝ E] {M : Type*} [MetricSpace M] [CompactSpace M] [ChartedSpace E M] [IsManifold π“˜(ℝ,E) ∞ M] [RiemannianBundle (fun x : M => TangentSpace π“˜(ℝ,E) x)] [IsContMDiffRiemannianBundle π“˜(ℝ,E) ∞ E (fun x : M => TangentSpace π“˜(ℝ,E) x)] [IsRiemannianManifold π“˜(ℝ,E) M] lemma exists_local_metric_gradient (a : M) : βˆƒ Ξ΄ Ο„ : ℝ, 0 < Ξ΄ ∧ 0 < Ο„ ∧ Metric.ball a Ξ΄ βŠ† (extChartAt π“˜(ℝ,E) a).source ∧ βˆ€ b ∈ Metric.ball a Ξ΄, βˆ€ y ∈ Metric.ball a Ξ΄, βˆƒ z : TangentBundle π“˜(ℝ,E) M, z.1 = b ∧ (sprayFlow Ο„ z).1 = y ∧ dist b y = Ο„ * β€–z.2β€– ∧ HasFDerivAt (fun w : E => dist b ((extChartAt π“˜(ℝ,E) a).symm w)^2 / 2) (Ο„ β€’ riemannianCoordinateMetric a (extChartAt π“˜(ℝ,E) a y) ((extChartAt (π“˜(ℝ,E).prod π“˜(ℝ,E)) (⟨a,0⟩ : TangentBundle π“˜(ℝ,E) M) (sprayFlow Ο„ z)).2)) (extChartAt π“˜(ℝ,E) a y) := by obtain ⟨δ,r,Ο„,hΞ΄,hr,hΟ„,hΟ„r,Ξ¦,e,hΞ¦,hΞ¦0,hode,he,hesub,hed,hei,hsrc,hdiag,htarget,hdist⟩ := exists_uniform_metric_normal_coordinates (E := E) a refine ⟨δ,Ο„,hΞ΄,hΟ„,hsrc,?_⟩ intro b hb y hy let c := extChartAt π“˜(ℝ,E) a let A : TangentBundle π“˜(ℝ,E) M := ⟨a,0⟩ let Ο‡ := extChartAt (π“˜(ℝ,E).prod π“˜(ℝ,E)) A let g := riemannianCoordinateMetric (E := E) a have hp : βˆ€ z : E Γ— E, (e z).1 = z.1 := by intro z; rw [he] let f := fiberwiseHomeomorph e hp (c b) let v := f.symm (c y) have hyt : c y ∈ f.target := htarget b hb y hy have hvs : v ∈ f.source := f.map_target hyt have hcb : dist (c b) (c a) < r := (max_lt_iff.mp (show max (dist (c b) (c a)) (dist (0:E) 0) < r from hesub (hdiag b hb))).1 have hball : βˆ€ w ∈ Metric.ball (0:E) r, (c b,w) ∈ Metric.ball (c a,0) r := by intro w hw exact max_lt hcb hw have hv : v ∈ Metric.ball (0:E) r := (max_lt_iff.mp (show max (dist (c b) (c a)) (dist v 0) < r from hesub hvs)).2 let Ξ¨ : ℝ Γ— E β†’ E := fun z => (Ξ¦ (z.1,(c b,z.2))).1 let W : ℝ Γ— E β†’ E := fun z => (Ξ¦ (z.1,(c b,z.2))).2 have hΞ¦c := hΞ¦.comp (contDiffOn_fst.prodMk (contDiffOn_const.prodMk contDiffOn_snd)) (show MapsTo (fun z : ℝ Γ— E => (z.1,(c b,z.2))) (Ioo (-r) r Γ—Λ’ Metric.ball 0 r) (Ioo (-r) r Γ—Λ’ Metric.ball (c a,0) r) from fun z hz => ⟨hz.1,hball z.2 hz.2⟩) have hf : (f : E β†’ E) = fun w => Ξ¨ (Ο„,w) := by funext w change (e (c b,w)).2 = _ rw [he] have ht : Ο„ ∈ Ioo (-r) r := ⟨by linarith,hΟ„r⟩ have h0 : (0:ℝ) ∈ Ioo (-r) r := ⟨by linarith,hr⟩ have hΞ¨v : Ξ¨ (Ο„,v) = c y := (congrFun hf v).symm.trans (f.right_inv hyt) have hG : βˆ€ w : E, g (c y) (W (Ο„,v)) (fderiv ℝ f v w) = Ο„*g (c b) v w := by intro w rw [hf,←hΞ¨v] exact coordinate_endpoint_gauss (Ξ¨ := Ξ¨) (W := W) (x := c b) (g := g) (isOpen_extChartAt_target a) (contDiffOn_riemannianCoordinateMetric a) (fun _ hx => riemannianCoordinateMetric_isInvertible hx) (fun x _ u v => riemannianCoordinateMetric_symm a x u v) (show ContDiffOn ℝ ∞ Ξ¨ (Ioo (-r) r Γ—Λ’ Metric.ball 0 r) from hΞ¦c.fst) (show ContDiffOn ℝ ∞ W (Ioo (-r) r Γ—Λ’ Metric.ball 0 r) from hΞ¦c.snd) (fun t ht w hw => (hode t ht (c b,w) (hball w hw)).1) (fun t ht w hw => ⟨((hode t ht (c b,w) (hball w hw)).2).fst, ((hode t ht (c b,w) (hball w hw)).2).snd⟩) h0 (fun w hw => congrArg Prod.fst (hΞ¦0 (c b,w) (hball w hw))) (fun w hw => congrArg Prod.snd (hΞ¦0 (c b,w) (hball w hw))) ht hv w have hfd := ((fiberwiseHomeomorph_contDiffOn hp (c b) hed).contDiffAt (f.open_source.mem_nhds hvs)).differentiableAt (by simp) have hfi := ((fiberwiseHomeomorph_symm_contDiffOn hp (c b) hei).contDiffAt (f.open_target.mem_nhds hyt)).differentiableAt (by simp) have hD := normal_action_hasFDerivAt f hyt hfd hfi (g (c b)) (g (c y)) (riemannianCoordinateMetric_symm a (c b)) (W (Ο„,v)) Ο„ hG have hnear : βˆ€αΆ  w : E in 𝓝 (c y), w ∈ c.target ∧ c.symm w ∈ Metric.ball a Ξ΄ := by have hcy : c y ∈ c.target := c.map_source (hsrc hy) have hynear := ((continuousOn_extChartAt_symm a).continuousAt ((isOpen_extChartAt_target a).mem_nhds hcy)).preimage_mem_nhds (by rw [c.left_inv (hsrc hy)]; exact Metric.isOpen_ball.mem_nhds hy) filter_upwards [(isOpen_extChartAt_target a).mem_nhds hcy, hynear] with w hw hwy exact ⟨hw,hwy⟩ have hdistEq : (fun w : E => dist b (c.symm w)^2/2) =αΆ [𝓝 (c y)] (fun w => (Ο„^2/2)*g (c b) (f.symm w) (f.symm w)) := by filter_upwards [hnear] with w hw have hwt : w ∈ f.target := by change (c b,w) ∈ e.target have hh : (c b,c (c.symm w)) ∈ e.target := htarget b hb (c.symm w) hw.2 simpa only [c.right_inv hw.1] using hh have hh := hdist b hb (c.symm w) hw.2 (f.symm w) (f.map_target hwt) (show (Ξ¦ (Ο„,(c b,f.symm w))).1 = c (c.symm w) by rw [c.right_inv hw.1] exact (congrFun hf _).symm.trans (f.right_inv hwt)) have hn : 0 ≀ g (c b) (f.symm w) (f.symm w) := by by_cases hh : f.symm w = 0 Β· simp only [hh,map_zero,le_refl] Β· exact (riemannianCoordinateMetric_positive (c.map_source (hsrc hb)) hh).le rw [hh,mul_pow,Real.sq_sqrt hn] ring let z := Ο‡.symm (c b,v) have hzt : (c b,v) ∈ Ο‡.target := (tangent_chart_target_iff A _).mpr (c.map_source (hsrc hb)) have hzchart : Ο‡ z = (c b,v) := Ο‡.right_inv hzt have hzsrc : z ∈ Ο‡.source := Ο‡.map_target hzt have hzb : z.1 = b := by have hh := congrArg Prod.fst hzchart rw [tangent_chart_apply] at hh exact c.injOn ((tangent_chart_source_iff A z).mp hzsrc) (hsrc hb) hh have hflow := sprayFlow_eq_coordinate_curve A hr (fun t ht' => (hode t ht' (c b,v) (hball v hv)).1) (fun t ht' => (hode t ht' (c b,v) (hball v hv)).2) (show Ο‡.symm (Ξ¦ (0,(c b,v))) = z by rw [hΞ¦0 (c b,v) (hball v hv)]) ht have hflowchart : Ο‡ (sprayFlow Ο„ z) = Ξ¦ (Ο„,(c b,v)) := by rw [hflow,Ο‡.right_inv ((tangent_chart_target_iff A _).mpr (hode Ο„ ht (c b,v) (hball v hv)).1)] have hflowsrc : sprayFlow Ο„ z ∈ Ο‡.source := by rw [hflow] exact Ο‡.map_target ((tangent_chart_target_iff A _).mpr (hode Ο„ ht (c b,v) (hball v hv)).1) have hfy : (sprayFlow Ο„ z).1 = y := by apply c.injOn ((tangent_chart_source_iff A _).mp hflowsrc) (hsrc hy) exact (congrArg Prod.fst (tangent_chart_apply A (sprayFlow Ο„ z))).symm.trans ((congrArg Prod.fst hflowchart).trans hΞ¨v) refine ⟨z,hzb,hfy,?_,?_⟩ Β· have hh := hdist b hb y hy v hvs hΞ¨v have hn := coordinate_spray_energy A z ((tangent_chart_source_iff A z).mp hzsrc) rw [hzchart] at hn rw [hh,hn,Real.sqrt_sq (norm_nonneg _)] Β· have hh := hD.congr_of_eventuallyEq hdistEq rw [show (Ο‡ (sprayFlow Ο„ z)).2 = W (Ο„,v) from congrArg Prod.snd hflowchart] exact hh end WeakMTWTransport end end OAI