import Erdos1135.ND.Discrepancy.A5ReferenceSection3Facade

/-!
# Actual-real rates for the A5 Section 3 facade

This leaf converts the capped harmonic errors from their natural floor
argument to separate rates in the actual real threshold.  The escape row
keeps its power rate `1 / 32000`, while the ordered passage row keeps the
symbolic logarithmic exponent `d`; no common or minimum exponent is formed.
-/

namespace Erdos1135
namespace ND

open Filter
open scoped Topology

noncomputable section

private theorem half_le_natFloor_cast {x : Real} (hx : 2 <= x) :
    x / 2 <= ((Nat.floor x : Nat) : Real) := by
  have hfloor := Nat.lt_floor_add_one x
  nlinarith

private theorem half_log_le_log_natFloor_cast
    {x : Real} (hx : 2 <= x) (hlog : 2 <= Real.log x) :
    Real.log x / 2 <= Real.log ((Nat.floor x : Nat) : Real) := by
  have hxpos : 0 < x := by linarith
  have hxhalfPos : 0 < x / 2 := div_pos hxpos (by norm_num)
  have hhalf := half_le_natFloor_cast hx
  have hfloorPos : 0 < ((Nat.floor x : Nat) : Real) :=
    hxhalfPos.trans_le hhalf
  have hmono :=
    Real.strictMonoOn_log.monotoneOn hxhalfPos hfloorPos hhalf
  rw [Real.log_div hxpos.ne' (by norm_num : (2 : Real) ≠ 0)] at hmono
  have hlogTwo : Real.log (2 : Real) < 1 := by
    have h := Real.log_lt_sub_one_of_pos
      (show (0 : Real) < 2 by norm_num)
      (show (2 : Real) ≠ 1 by norm_num)
    norm_num at h
    exact h
  linarith

private theorem rpow_neg_le_two_rpow_mul_of_half_le
    {a b c e : Real} (ha : 0 < a) (haOne : 1 <= a)
    (hab : a / 2 <= b) (hc : 0 <= c) (hec : e <= c) :
    b ^ (-c) <= (2 : Real) ^ c * a ^ (-e) := by
  have hhalfPos : 0 < a / 2 := div_pos ha (by norm_num)
  have hbase : b ^ (-c) <= (a / 2) ^ (-c) :=
    Real.rpow_le_rpow_of_nonpos hhalfPos hab (neg_nonpos.mpr hc)
  have hfactor : (a / 2) ^ (-c) = (2 : Real) ^ c * a ^ (-c) := by
    rw [Real.div_rpow ha.le (by norm_num : (0 : Real) <= 2),
      Real.rpow_neg (by norm_num : (0 : Real) <= 2)]
    simp only [div_eq_mul_inv, inv_inv]
    ring
  have hexponent : a ^ (-c) <= a ^ (-e) :=
    Real.rpow_le_rpow_of_exponent_le haOne (neg_le_neg hec)
  calc
    b ^ (-c) <= (a / 2) ^ (-c) := hbase
    _ = (2 : Real) ^ c * a ^ (-c) := hfactor
    _ <= (2 : Real) ^ c * a ^ (-e) :=
      mul_le_mul_of_nonneg_left hexponent
        (Real.rpow_nonneg (by norm_num) _)

private theorem section3DeltaH_floor_le_inv_x
    {x : Real} (hx : 2 <= x)
    (hlogFloor : 1 <= Real.log ((Nat.floor x : Nat) : Real)) :
    96000 /
        (((Nat.floor x : Nat) : Real) *
          Real.log ((Nat.floor x : Nat) : Real)) <=
      192000 / x := by
  have hxpos : 0 < x := by linarith
  have hhalf := half_le_natFloor_cast hx
  have hfloorNonneg : 0 <= ((Nat.floor x : Nat) : Real) :=
    Nat.cast_nonneg _
  have hfloorLeProd : ((Nat.floor x : Nat) : Real) <=
      ((Nat.floor x : Nat) : Real) *
        Real.log ((Nat.floor x : Nat) : Real) := by
    simpa only [mul_one] using
      mul_le_mul_of_nonneg_left hlogFloor hfloorNonneg
  have hden : x / 2 <=
      ((Nat.floor x : Nat) : Real) *
        Real.log ((Nat.floor x : Nat) : Real) :=
    hhalf.trans hfloorLeProd
  calc
    96000 /
          (((Nat.floor x : Nat) : Real) *
            Real.log ((Nat.floor x : Nat) : Real)) <=
        96000 / (x / 2) :=
      div_le_div_of_nonneg_left (by norm_num)
        (div_pos hxpos (by norm_num)) hden
    _ = 192000 / x := by
      field_simp [hxpos.ne']
      norm_num

private theorem section3DeltaH_floor_le_real_power
    {x e : Real} (hx : 2 <= x)
    (hlogFloor : 1 <= Real.log ((Nat.floor x : Nat) : Real))
    (heOne : e <= 1) :
    96000 /
        (((Nat.floor x : Nat) : Real) *
          Real.log ((Nat.floor x : Nat) : Real)) <=
      192000 * x ^ (-e) := by
  have hxOne : 1 <= x := by linarith
  have hpow : x ^ (-1 : Real) <= x ^ (-e) :=
    Real.rpow_le_rpow_of_exponent_le hxOne (neg_le_neg heOne)
  calc
    96000 /
          (((Nat.floor x : Nat) : Real) *
            Real.log ((Nat.floor x : Nat) : Real)) <=
        192000 / x := section3DeltaH_floor_le_inv_x hx hlogFloor
    _ = 192000 * x ^ (-1 : Real) := by
      rw [Real.rpow_neg_one]
      ring
    _ <= 192000 * x ^ (-e) :=
      mul_le_mul_of_nonneg_left hpow (by norm_num)

private theorem four_mul_section3DeltaH_floor_le_real_log_power
    {x d : Real} (hx : 2 <= x) (hlog : 1 <= Real.log x)
    (hlogFloor : 1 <= Real.log ((Nat.floor x : Nat) : Real))
    (hdOne : d <= 1) :
    4 * (96000 /
        (((Nat.floor x : Nat) : Real) *
          Real.log ((Nat.floor x : Nat) : Real))) <=
      768000 * (Real.log x) ^ (-d) := by
  have hxpos : 0 < x := by linarith
  have hlogPos : 0 < Real.log x := zero_lt_one.trans_le hlog
  have hlogLeX : Real.log x <= x := by
    have h := Real.log_le_sub_one_of_pos hxpos
    linarith
  have hinv : 1 / x <= 1 / Real.log x :=
    one_div_le_one_div_of_le hlogPos hlogLeX
  have hlogPow : (Real.log x) ^ (-1 : Real) <=
      (Real.log x) ^ (-d) :=
    Real.rpow_le_rpow_of_exponent_le hlog (neg_le_neg hdOne)
  calc
    4 * (96000 /
          (((Nat.floor x : Nat) : Real) *
            Real.log ((Nat.floor x : Nat) : Real))) <=
        4 * (192000 / x) :=
      mul_le_mul_of_nonneg_left
        (section3DeltaH_floor_le_inv_x hx hlogFloor) (by norm_num)
    _ = 768000 * (1 / x) := by ring
    _ <= 768000 * (1 / Real.log x) :=
      mul_le_mul_of_nonneg_left hinv (by norm_num)
    _ = 768000 * (Real.log x) ^ (-1 : Real) := by
      rw [Real.rpow_neg_one]
      ring
    _ <= 768000 * (Real.log x) ^ (-d) :=
      mul_le_mul_of_nonneg_left hlogPow (by norm_num)

/-- The capped harmonic no-hit error has its genuine power rate in the
actual real threshold.  The fixed finite prefix is eventually crossed for
every cutoff `B0`.
-/
theorem eventually_ndA5ReferenceSection3NoHitError_floor_le_real_power
    (B0 : Nat) :
    ∀ᶠ x : Real in atTop,
      ndA5ReferenceSection3NoHitError B0 (Nat.floor x) <=
        192020 * x ^ (-(1 / 32000 : Real)) := by
  filter_upwards
      [tendsto_nat_floor_atTop.eventually_ge_atTop B0,
        eventually_ge_atTop (2 : Real),
        Real.tendsto_log_atTop.eventually_ge_atTop (2 : Real)]
      with x hcut hx hlog
  have hxpos : 0 < x := by linarith
  have hxOne : 1 <= x := by linarith
  have hhalf := half_le_natFloor_cast hx
  have hlogHalf := half_log_le_log_natFloor_cast hx hlog
  have hlogFloor :
      1 <= Real.log ((Nat.floor x : Nat) : Real) := by linarith
  have hpowBase :
      ((Nat.floor x : Nat) : Real) ^ (-(1 / 32000 : Real)) <=
        (2 : Real) ^ (1 / 32000 : Real) *
          x ^ (-(1 / 32000 : Real)) :=
    rpow_neg_le_two_rpow_mul_of_half_le hxpos hxOne hhalf
      (by norm_num) (le_refl _)
  have htwo : (2 : Real) ^ (1 / 32000 : Real) <= 2 := by
    calc
      (2 : Real) ^ (1 / 32000 : Real) <= (2 : Real) ^ (1 : Real) :=
        Real.rpow_le_rpow_of_exponent_le (by norm_num) (by norm_num)
      _ = 2 := by norm_num
  have hpow :
      ((Nat.floor x : Nat) : Real) ^ (-(1 / 32000 : Real)) <=
        2 * x ^ (-(1 / 32000 : Real)) :=
    hpowBase.trans
      (mul_le_mul_of_nonneg_right htwo (Real.rpow_nonneg hxpos.le _))
  have hdelta := section3DeltaH_floor_le_real_power hx hlogFloor
    (show (1 / 32000 : Real) <= 1 by norm_num)
  rw [ndA5ReferenceSection3NoHitError, if_pos hcut]
  unfold ndA5ReferenceSection3RawNoHitError
  calc
    10 * ((Nat.floor x : Nat) : Real) ^ (-(1 / 32000 : Real)) +
          96000 /
            (((Nat.floor x : Nat) : Real) *
              Real.log ((Nat.floor x : Nat) : Real)) <=
        10 * (2 * x ^ (-(1 / 32000 : Real))) +
          192000 * x ^ (-(1 / 32000 : Real)) :=
      add_le_add (mul_le_mul_of_nonneg_left hpow (by norm_num)) hdelta
    _ = 192020 * x ^ (-(1 / 32000 : Real)) := by ring

/-- The capped ordered passage error has its genuine logarithmic rate in the
actual real threshold, with the full-L1 coefficient `768064`.
-/
theorem eventually_ndA5ReferenceSection3PassTVError_floor_le_real_log_power
    (B0 : Nat) {d : Real}
    (hd : 0 < d) (hd20 : d < 1 / 20) :
    ∀ᶠ x : Real in atTop,
      ndA5ReferenceSection3PassTVError B0 d (Nat.floor x) <=
        768064 * (Real.log x) ^ (-d) := by
  have hd0 : 0 <= d := hd.le
  have hdOne : d <= 1 := by linarith [hd20]
  filter_upwards
      [tendsto_nat_floor_atTop.eventually_ge_atTop B0,
        eventually_ge_atTop (2 : Real),
        Real.tendsto_log_atTop.eventually_ge_atTop (2 : Real)]
      with x hcut hx hlog
  have hlogPos : 0 < Real.log x := by linarith
  have hlogOne : 1 <= Real.log x := by linarith
  have hlogHalf := half_log_le_log_natFloor_cast hx hlog
  have hlogFloor :
      1 <= Real.log ((Nat.floor x : Nat) : Real) := by linarith
  have hpowBase :
      Real.log ((Nat.floor x : Nat) : Real) ^ (-d) <=
        (2 : Real) ^ d * (Real.log x) ^ (-d) :=
    rpow_neg_le_two_rpow_mul_of_half_le hlogPos hlogOne hlogHalf
      hd0 (le_refl _)
  have htwo : (2 : Real) ^ d <= 2 := by
    calc
      (2 : Real) ^ d <= (2 : Real) ^ (1 : Real) :=
        Real.rpow_le_rpow_of_exponent_le (by norm_num) hdOne
      _ = 2 := by norm_num
  have hpow :
      Real.log ((Nat.floor x : Nat) : Real) ^ (-d) <=
        2 * (Real.log x) ^ (-d) :=
    hpowBase.trans
      (mul_le_mul_of_nonneg_right htwo
        (Real.rpow_nonneg hlogPos.le _))
  have hdelta :=
    four_mul_section3DeltaH_floor_le_real_log_power
      hx hlogOne hlogFloor hdOne
  rw [ndA5ReferenceSection3PassTVError, if_pos hcut]
  unfold ndA5ReferenceSection3RawPassTVError
    ndA5ReferenceInteriorScalarRate
  calc
    32 * Real.log ((Nat.floor x : Nat) : Real) ^ (-d) +
          4 * (96000 /
            (((Nat.floor x : Nat) : Real) *
              Real.log ((Nat.floor x : Nat) : Real))) <=
        32 * (2 * (Real.log x) ^ (-d)) +
          768000 * (Real.log x) ^ (-d) :=
      add_le_add (mul_le_mul_of_nonneg_left hpow (by norm_num)) hdelta
    _ = 768064 * (Real.log x) ^ (-d) := by ring

/-- One cutoff from the checked Section 3 facade simultaneously carries its
global all-threshold bounds, nonnegativity, and the two separate actual-real
rates.  The cutoff is chosen before every real threshold and later trace
parameter.
-/
theorem exists_ndA5ReferenceSection3SplitRealRatePacket
    {c kappa d : Real} (hPhase : PhaseGap c kappa)
    (hd : 0 < d) (hd20 : d < 1 / 20)
    (hdk : d < 1 / (2 * kappa)) :
    ∃ B0 : Nat, 2 <= B0 ∧
      (∀ B : Nat,
        0 <= ndA5ReferenceSection3NoHitError B0 B ∧
          0 <= ndA5ReferenceSection3PassTVError B0 d B) ∧
      (∀ᶠ x : Real in atTop,
        ndA5ReferenceSection3NoHitError B0 (Nat.floor x) <=
          192020 * x ^ (-(1 / 32000 : Real))) ∧
      (∀ᶠ x : Real in atTop,
        ndA5ReferenceSection3PassTVError B0 d (Nat.floor x) <=
          768064 * (Real.log x) ^ (-d)) ∧
      NDA5ReferenceSection3Bounds
        (ndA5ReferenceSection3NoHitError B0)
        (ndA5ReferenceSection3PassTVError B0 d) := by
  rcases exists_ndA5ReferenceSection3AllThresholdBounds
      hPhase hd hd20 hdk with
    ⟨B0, hB0, hnonneg, _hraw, hbounds⟩
  exact ⟨B0, hB0, hnonneg,
    eventually_ndA5ReferenceSection3NoHitError_floor_le_real_power B0,
    eventually_ndA5ReferenceSection3PassTVError_floor_le_real_log_power
      B0 hd hd20,
    hbounds⟩

end
end ND
end Erdos1135
