import Euler.PacketSourceScaleChoice import Euler.PacketSourceScaleGuards /-! Finite-prefix control of the actual coupling recurrence. Each step may use only the bounds already proved on its preceding prefix. -/ noncomputable section namespace EulerParentRenewalPrefix open Finset EulerPacketSourceScaleChoice EulerPacketSourceScaleGuards EulerPacketSourceScaleActual EulerScale theorem relative_step_error {a b e : ℝ} (ha : 0 < a) (ha2 : a ≤ 2) (he : 0 ≤ e) (h : |b/a-1| ≤ e) : |b-a| ≤ 2*e := by have hid : (b/a-1)*a=b-a := by field_simp calc |b-a| = |b/a-1| * a := by rw [← hid,abs_mul,abs_of_pos ha] _ ≤ e*a := mul_le_mul_of_nonneg_right h ha.le _ ≤ e*2 := mul_le_mul_of_nonneg_left ha2 he _ = 2*e := by ring theorem partial_sum_le {e : ℕ → ℝ} {η : ℝ} (he : SmallSeries e η) (n : ℕ) : ∑ i ∈ range n, e i ≤ η := (he.summable.sum_le_tsum (range n) (fun i _ => he.nonneg i)).trans he.total_le theorem bounds_of_accumulated_error {e : ℕ → ℝ} {η v : ℝ} (he : SmallSeries e η) (hη : η ≤ 1/4) (n : ℕ) (hv : |v-1| ≤ 2*∑ i ∈ range n, e i) : 1/2 ≤ v ∧ v ≤ 2 := by have hs := partial_sum_le he n have ha := abs_le.mp hv constructor <;> linarith only [hs,hη,ha.1,ha.2] theorem stage_congr_at {J D : ℕ} {C c X K : ℝ} {a β b γ : ℕ → ℝ} {n : ℕ} (G : StageGuards J D C c X K a β n) (ha : a n=b n) (hβ : β n=γ n) : StageGuards J D C c X K b γ n where epsilon_pos := by simpa only [ha] using G.epsilon_pos epsilon_small := by simpa only [ha] using G.epsilon_small sigma_pos := by simpa only [hβ] using G.sigma_pos sigma_small := by simpa only [hβ] using G.sigma_small reciprocal_pos := G.reciprocal_pos reciprocal_small := G.reciprocal_small target_from_sigma := by simpa only [hβ] using G.target_from_sigma target_le_horizon := by simpa only [ha,hβ] using G.target_le_horizon horizon_le_Theta := by simpa only [ha,hβ] using G.horizon_le_Theta extra_time := by simpa only [ha,hβ] using G.extra_time next_width := G.next_width geometry_small := by simpa only [geometryError,ha] using G.geometry_small compression := by simpa only [ha,hβ] using G.compression /-- Extend just the current scalar values to a bounded artificial sequence, apply the uniform theorem, then transfer its local conclusion. No hypotheses on any future stage are required. -/ theorem stage_guards_at (J D : ℕ) (hJ : 3 ≤ J) (C c X K δ : ℝ) (hC : 4 ≤ C) (hX : 8 ≤ X) (hK : 1 ≤ K) (hδ : δ ≤ 1/2) (hδK : 1000000*K*δ ≤ 1) (hb : ActualBounds J D C c X δ) (a β : ℕ → ℝ) (n : ℕ) (ha : 1/2 ≤ a n) (ha2 : a n ≤ 2) (hβ : 1/2 ≤ β n*(scaleSequence J X n)^2) (hβ2 : β n*(scaleSequence J X n)^2 ≤ 2) : StageGuards J D C c X K a β n := by let b : ℕ → ℝ := fun _ => a n let γ : ℕ → ℝ := fun i => β n*(scaleSequence J X n)^2/(scaleSequence J X i)^2 have hx := quadratic_growth_pos J (by omega) (scaleSequence J X) (show 0 < X by linarith only [hX]) (scaleSequence_succ J X) have hγ (i : ℕ) : γ i*(scaleSequence J X i)^2=β n*(scaleSequence J X n)^2 := by dsimp only [γ] exact div_mul_cancel₀ _ (pow_ne_zero _ (hx i).ne') have H := stage_guards J D hJ C c X K δ hC hX hK hδ hδK hb b γ (fun _ => ha) (fun _ => ha2) (fun i => by rw [hγ i]; exact hβ) (fun i => by rw [hγ i]; exact hβ2) n apply stage_congr_at (b := a) (γ := β) H rfl dsimp only [γ] exact mul_div_cancel_right₀ _ (pow_ne_zero _ (hx n).ne') end EulerParentRenewalPrefix