module

public import SubentropyCertificates.CalibratedInverse
public import SubentropyCertificates.CurveInverse

/-! Square-root sensitivity of an increasing curve with a quadratic endpoint. -/
@[expose] public section
noncomputable section
open Set Filter Asymptotics
open scoped Topology
namespace SubentropyCertificates.EndpointCurvature

private theorem square_root_ratio (g : ℝ → ℝ) {a : ℝ} (ha : 0<a)
    (hg : (fun h : ℝ ↦ g h-a*h^2) =O[𝓝 0] (fun h ↦ h^3)) :
    Tendsto (fun h : ℝ ↦ h/Real.sqrt (g h)) (𝓝[>] 0) (𝓝 (1/Real.sqrt a)) := by
  obtain ⟨C,hC⟩ := isBigO_iff.mp hg
  have h0 : Tendsto (fun h : ℝ ↦ h) (𝓝[>] 0) (𝓝 0) := nhdsWithin_le_nhds
  have hl : Tendsto (fun h : ℝ ↦ a-C*h) (𝓝[>] 0) (𝓝 a) := by
    simpa using (tendsto_const_nhds (x:=a)).sub ((tendsto_const_nhds (x:=C)).mul h0)
  have hu : Tendsto (fun h : ℝ ↦ a+C*h) (𝓝[>] 0) (𝓝 a) := by
    simpa using (tendsto_const_nhds (x:=a)).add ((tendsto_const_nhds (x:=C)).mul h0)
  have hb : ∀ᶠ h : ℝ in 𝓝[>] 0, a-C*h ≤ g h/h^2 ∧ g h/h^2 ≤ a+C*h := by
    filter_upwards [hC.filter_mono nhdsWithin_le_nhds,self_mem_nhdsWithin] with h he (hp : 0<h)
    simp only [Real.norm_eq_abs,abs_pow,abs_of_pos hp] at he
    obtain ⟨h₁,h₂⟩ := abs_le.mp he
    exact ⟨(le_div_iff₀ (sq_pos_of_pos hp)).mpr (by nlinarith),
      (div_le_iff₀ (sq_pos_of_pos hp)).mpr (by nlinarith)⟩
  have hrat := tendsto_of_tendsto_of_tendsto_of_le_of_le' hl hu
    (hb.mono fun _ hh ↦ hh.1) (hb.mono fun _ hh ↦ hh.2)
  have hs := (tendsto_const_nhds (x:=(1:ℝ))).div
    ((Real.continuous_sqrt.tendsto a).comp hrat) (Real.sqrt_pos.mpr ha).ne'
  apply hs.congr'
  filter_upwards [self_mem_nhdsWithin,hrat.eventually_const_lt ha] with h (hp : 0<h) hg'
  have hgpos : 0 < g h := (div_pos_iff_of_pos_right (sq_pos_of_pos hp)).1 hg'
  change 1/Real.sqrt (g h/h^2) = h/Real.sqrt (g h)
  rw [Real.sqrt_div hgpos.le,Real.sqrt_sq hp.le,one_div_div]

/-- Along any approach to the upper endpoint through the range, the inverse displacement
divided by the square root of the range displacement tends to the inverse square root
of the quadratic coefficient. -/
theorem inverse_endpoint_sensitivity {α : Type*} {l : Filter α} (f : ℝ → ℝ)
    (b a : ℝ) (hb : 0<b) (ha : 0<a) (hf : StrictMonoOn f (Icc 0 b))
    (hquad : (fun h : ℝ ↦ f b-f (b-h)-a*h^2) =O[𝓝 0] (fun h ↦ h^3))
    (q e : α → ℝ) (he : ∀ i, e i ∈ Icc 0 b) (hval : ∀ i, f (e i) = q i)
    (hq : Tendsto q l (𝓝[<] (f b))) :
    Tendsto (fun i ↦ (b-e i)/Real.sqrt (f b-q i)) l (𝓝 (1/Real.sqrt a)) := by
  have hend : b ∈ Icc (0:ℝ) b := ⟨hb.le,le_rfl⟩
  obtain ⟨hq',hq_lt⟩ := tendsto_nhdsWithin_iff.mp hq
  have hgap : Tendsto (fun i ↦ b-e i) l (𝓝[>] 0) := by
    refine tendsto_nhdsWithin_iff.mpr ⟨tendsto_order.mpr ⟨?_,?_⟩,?_⟩
    · intro x hx
      exact .of_forall fun i ↦ hx.trans_le (sub_nonneg.mpr (he i).2)
    · intro r hr
      let s := max 0 (b-r/2)
      have hs : s < b := max_lt hb (by linarith)
      have hs' : s ∈ Icc 0 b := ⟨le_max_left _ _,hs.le⟩
      have hlt := hq'.eventually_const_lt (hf hs' hend hs)
      filter_upwards [hlt] with i hi
      rw [← hval i] at hi
      have hh := (hf.lt_iff_lt hs' (he i)).mp hi
      have hslo : b-r/2 ≤ s := le_max_right _ _
      change b-e i < r
      linarith
    · filter_upwards [hq_lt] with i hi
      have hi' : f (e i)<f b := by rw [hval]; exact hi
      exact sub_pos.mpr ((hf.lt_iff_lt (he i) hend).mp hi')
  have hs := (square_root_ratio (fun h ↦ f b-f (b-h)) ha hquad).comp hgap
  simpa only [Function.comp_def,sub_sub_cancel,hval] using hs

end SubentropyCertificates.EndpointCurvature
