/-Copyright (c) 2026 Carles Marín. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles Marín, Claude (AI assistant)-/module public import Formalizations.Combinatorics.Caballero.AngularGeneralA /-!# Phase widths of the primary floor patterns for general a (Caballero, Part IV) Lemma 4 of Part IV: for a >= 2 and 0 < theta <= 1/a, the phase width w_k(theta) of the primary floorpatterns k^(r)_h = floor((r+h)/(a+1)), |h| <= a, is min(theta, 1 - a theta) for r = 0 and r = a, and[min(theta, (a+2) theta - 1, 1 - a theta)]_+ for 1 <= r <= a-1; summing over r gives the three-pieceformula of U_a on (0, 1/(a+2)], [1/(a+2), 1/(a+1)], [1/(a+1), 1/a]. The phase width is the definingformula of the paper, [min(1, min_h(k_h+1-h theta)) - max(0, max_h(k_h - h theta))]_+, with the cellvector stored as Fin (2a+1) -> Z (index h + a, as in the module of cell vectors for general a); theidentification of this formula with the length of the set of u in (0,1) with floor(u + h theta) =k_h is not formalised. The hypothesis 1 <= r <= a-1 is written r + 1 <= a. The rows and the measureof fibres are not touched. ## Main results * `CaballeroWidths.phaseWidth_pattern_zero`: Lemma 4: the width of the pattern k^(0) is min(theta, 1 - a theta) for a >= 2, 0 < theta <= 1/a* `CaballeroWidths.phaseWidth_pattern_last`: Lemma 4: the width of the pattern k^(a) is min(theta, 1 - a theta) for a >= 2, 0 < theta <= 1/a* `CaballeroWidths.phaseWidth_pattern_mid`: Lemma 4: the width of k^(r), 1 <= r <= a-1, is [min(theta, (a+2) theta - 1, 1 - a theta)]_+* `CaballeroWidths.primaryPhase_first`: Lemma 4: U_a(theta) = 2 theta on (0, 1/(a+2)]* `CaballeroWidths.primaryPhase_second`: Lemma 4: U_a(theta) = a(a+1) theta - a + 1 on [1/(a+2), 1/(a+1)]* `CaballeroWidths.primaryPhase_third`: Lemma 4: U_a(theta) = a + 1 - a(a+1) theta on [1/(a+1), 1/a] ## References * C. Marín, How Many Dyck Words Do the Divisors of an Integer Make? IV. The Primary Phase of the Ambiguity Profile for General a, draft (2 December 2026), unpublished.* C. Marín, How Many Dyck Words Do the Divisors of an Integer Make? Exact Counts and Quasipolynomial Growth (Part I), Zenodo (2026), doi:10.5281/zenodo.24536372 (concept DOI).-/ @[expose] public section namespace CaballeroWidths open Finset CaballeroGeneralA /-- Lower term `k_h - hθ` of the phase width, at the index `i = h + a`. -/noncomputable def lowTerm (a : ℕ) (k : Vec a) (θ : ℝ) (i : Fin (2 * a + 1)) : ℝ := (k i : ℝ) - ((((i : ℕ) : ℤ) - a : ℤ) : ℝ) * θ /-- Upper term `k_h + 1 - hθ` of the phase width, at the index `i = h + a`. -/noncomputable def upTerm (a : ℕ) (k : Vec a) (θ : ℝ) (i : Fin (2 * a + 1)) : ℝ := (k i : ℝ) + 1 - ((((i : ℕ) : ℤ) - a : ℤ) : ℝ) * θ /-- The phase width `w_k(θ) = [min(1, min_h (k_h+1-hθ)) - max(0, max_h (k_h-hθ))]_+`, the length ofthe set of `u ∈ (0,1)` with `⌊u + hθ⌋ = k_h` for all `|h| ≤ a`. -/noncomputable def phaseWidth (a : ℕ) (k : Vec a) (θ : ℝ) : ℝ := max (min 1 (univ.inf' univ_nonempty (upTerm a k θ)) - max 0 (univ.sup' univ_nonempty (lowTerm a k θ))) 0 /-- The primary floor pattern `k^(r)_h = ⌊(r+h)/(a+1)⌋`, `-a ≤ h ≤ a`, stored at index `h + a`. -/noncomputable def primaryPattern (a r : ℕ) : Vec a := fun i => ⌊((((r : ℤ) + (((i : ℕ) : ℤ) - a) : ℤ) : ℝ)) / ((a : ℝ) + 1)⌋ /-- The primary phase function `U_a(θ) = Σ_{r=0}^{a} w_{k^(r)}(θ)`. -/noncomputable def primaryPhase (a : ℕ) (θ : ℝ) : ℝ := ∑ r ∈ range (a + 1), phaseWidth a (primaryPattern a r) θ /-! ### Auxiliary lemmas -/ /-- Floor of `(r+h)/(a+1)` as an integer function of `h`. -/noncomputable def pf (a r : ℕ) (h : ℤ) : ℤ := ⌊((((r : ℤ) + h : ℤ) : ℝ)) / ((a : ℝ) + 1)⌋ theorem primaryPattern_apply (a r : ℕ) (i : Fin (2 * a + 1)) : primaryPattern a r i = pf a r (((i : ℕ) : ℤ) - a) := rfl theorem pf_eq (a r : ℕ) (h q : ℤ) (h1 : q * ((a : ℤ) + 1) ≤ r + h) (h2 : (r : ℤ) + h < (q + 1) * ((a : ℤ) + 1)) : pf a r h = q := by unfold pf rw [Int.floor_eq_iff] have hpos : (0 : ℝ) < (a : ℝ) + 1 := by positivity constructor · rw [le_div_iff₀ hpos] exact_mod_cast h1 · rw [div_lt_iff₀ hpos] push_cast exact_mod_cast h2 theorem pf_cases (a r : ℕ) (hr : r ≤ a) (h : ℤ) (hb1 : -(a : ℤ) ≤ h) (hb2 : h ≤ a) : (h < -(r : ℤ) ∧ pf a r h = -1) ∨ (-(r : ℤ) ≤ h ∧ h ≤ (a : ℤ) - r ∧ pf a r h = 0) ∨ ((a : ℤ) + 1 - r ≤ h ∧ pf a r h = 1) := by rcases (by omega : h < -(r : ℤ) ∨ (-(r : ℤ) ≤ h ∧ h ≤ (a : ℤ) - r) ∨ (a : ℤ) + 1 - r ≤ h) with h' | ⟨h1, h2⟩ | h' · refine Or.inl ⟨h', pf_eq a r h (-1) ?_ ?_⟩ <;> push_cast <;> linarith · refine Or.inr (Or.inl ⟨h1, h2, pf_eq a r h 0 ?_ ?_⟩) <;> push_cast <;> linarith · refine Or.inr (Or.inr ⟨h', pf_eq a r h 1 ?_ ?_⟩) <;> push_cast <;> linarith theorem exists_idx (a : ℕ) (h : ℤ) (hb1 : -(a : ℤ) ≤ h) (hb2 : h ≤ a) : ∃ i : Fin (2 * a + 1), ((i : ℕ) : ℤ) - a = h := ⟨⟨(h + a).toNat, by omega⟩, by simp only; omega⟩ theorem idx_bounds (a : ℕ) (i : Fin (2 * a + 1)) : -(a : ℤ) ≤ ((i : ℕ) : ℤ) - a ∧ ((i : ℕ) : ℤ) - a ≤ a := by have := i.isLt omega theorem min_one_inf_eq {ι : Type*} [Fintype ι] (hne : (univ : Finset ι).Nonempty) (f : ι → ℝ) (v : ℝ) (hle : ∀ i, v ≤ f i) (hv : v ≤ 1) (hex : v = 1 ∨ ∃ i, f i ≤ v) : min 1 (univ.inf' hne f) = v := by apply le_antisymm · rcases hex with h | ⟨i, hi⟩ · exact le_of_le_of_eq (min_le_left _ _) h.symm · exact (min_le_right _ _).trans ((Finset.inf'_le f (mem_univ i)).trans hi) · exact le_min hv ((Finset.le_inf'_iff hne f).2 fun i _ => hle i) theorem max_zero_sup_eq {ι : Type*} [Fintype ι] (hne : (univ : Finset ι).Nonempty) (f : ι → ℝ) (v : ℝ) (hle : ∀ i, f i ≤ v) (hv : 0 ≤ v) (hex : v = 0 ∨ ∃ i, v ≤ f i) : max 0 (univ.sup' hne f) = v := by apply le_antisymm · exact max_le hv ((Finset.sup'_le_iff hne f).2 fun i _ => hle i) · rcases hex with h | ⟨i, hi⟩ · exact le_of_eq_of_le h (le_max_left _ _) · exact hi.trans ((Finset.le_sup' f (mem_univ i)).trans (le_max_right _ _)) theorem phaseWidth_eq (a : ℕ) (k : Vec a) (θ M m : ℝ) (hM : min 1 (univ.inf' univ_nonempty (upTerm a k θ)) = M) (hm : max 0 (univ.sup' univ_nonempty (lowTerm a k θ)) = m) : phaseWidth a k θ = max (M - m) 0 := by unfold phaseWidth rw [hM, hm] theorem upTerm_pattern (a r : ℕ) (θ : ℝ) (i : Fin (2 * a + 1)) : upTerm a (primaryPattern a r) θ i = (pf a r (((i : ℕ) : ℤ) - a) : ℝ) + 1 - ((((i : ℕ) : ℤ) - a : ℤ) : ℝ) * θ := rfl theorem lowTerm_pattern (a r : ℕ) (θ : ℝ) (i : Fin (2 * a + 1)) : lowTerm a (primaryPattern a r) θ i = (pf a r (((i : ℕ) : ℤ) - a) : ℝ) - ((((i : ℕ) : ℤ) - a : ℤ) : ℝ) * θ := rfl /-- Existence of an index with prescribed `h`, together with the value of the pattern. -/theorem exists_up (a r : ℕ) (θ : ℝ) (h v : ℤ) (hb1 : -(a : ℤ) ≤ h) (hb2 : h ≤ a) (hv : pf a r h = v) : ∃ i, upTerm a (primaryPattern a r) θ i = (v : ℝ) + 1 - (h : ℝ) * θ := by obtain ⟨i, hi⟩ := exists_idx a h hb1 hb2 refine ⟨i, ?_⟩ rw [upTerm_pattern, hi, hv] theorem exists_low (a r : ℕ) (θ : ℝ) (h v : ℤ) (hb1 : -(a : ℤ) ≤ h) (hb2 : h ≤ a) (hv : pf a r h = v) : ∃ i, lowTerm a (primaryPattern a r) θ i = (v : ℝ) - (h : ℝ) * θ := by obtain ⟨i, hi⟩ := exists_idx a h hb1 hb2 refine ⟨i, ?_⟩ rw [lowTerm_pattern, hi, hv] /-- Real versions of the three-way split of `h`, used in the bounds. -/theorem pf_cases_up (a r : ℕ) (hr : r ≤ a) (θ : ℝ) (i : Fin (2 * a + 1)) : ∃ h : ℤ, -(a : ℤ) ≤ h ∧ h ≤ a ∧ ((h < -(r : ℤ) ∧ upTerm a (primaryPattern a r) θ i = -(h : ℝ) * θ ∧ lowTerm a (primaryPattern a r) θ i = -1 - (h : ℝ) * θ) ∨ (-(r : ℤ) ≤ h ∧ h ≤ (a : ℤ) - r ∧ upTerm a (primaryPattern a r) θ i = 1 - (h : ℝ) * θ ∧ lowTerm a (primaryPattern a r) θ i = -(h : ℝ) * θ) ∨ ((a : ℤ) + 1 - r ≤ h ∧ upTerm a (primaryPattern a r) θ i = 2 - (h : ℝ) * θ ∧ lowTerm a (primaryPattern a r) θ i = 1 - (h : ℝ) * θ)) := by obtain ⟨hb1, hb2⟩ := idx_bounds a i refine ⟨((i : ℕ) : ℤ) - a, hb1, hb2, ?_⟩ rw [upTerm_pattern, lowTerm_pattern] rcases pf_cases a r hr _ hb1 hb2 with ⟨h1, h2⟩ | ⟨h1, h2, h3⟩ | ⟨h1, h2⟩ · refine Or.inl ⟨h1, ?_, ?_⟩ <;> rw [h2] <;> push_cast <;> ring · refine Or.inr (Or.inl ⟨h1, h2, ?_, ?_⟩) <;> rw [h3] <;> push_cast <;> ring · refine Or.inr (Or.inr ⟨h1, ?_, ?_⟩) <;> rw [h2] <;> push_cast <;> ring theorem cast_le_of (h : ℤ) (x : ℤ) (hx : h ≤ x) : (h : ℝ) ≤ (x : ℝ) := by exact_mod_cast hx /-! ### The middle patterns -/ theorem phaseWidth_pattern_mid (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 0 < θ) (h1 : θ ≤ 1 / (a : ℝ)) (r : ℕ) (hr1 : 1 ≤ r) (hr2 : r + 1 ≤ a) : phaseWidth a (primaryPattern a r) θ = max (min θ (min (((a : ℝ) + 2) * θ - 1) (1 - (a : ℝ) * θ))) 0 := by have hr : r ≤ a := by omega have haθ : (a : ℝ) * θ ≤ 1 := by have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) have := (le_div_iff₀ ha').1 h1 linarith have hr1' : (1 : ℝ) ≤ r := by exact_mod_cast hr1 have hr2' : (r : ℝ) + 1 ≤ a := by exact_mod_cast hr2 have hrθ1 : θ ≤ (r : ℝ) * θ := by nlinarith have hrθ2 : (r : ℝ) * θ + θ ≤ (a : ℝ) * θ := by nlinarith have hM : min 1 (univ.inf' univ_nonempty (upTerm a (primaryPattern a r) θ)) = min 1 (min (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ)) := by apply min_one_inf_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a r hr θ i have e1 : min 1 (min (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ)) ≤ 1 := min_le_left _ _ have e2 : min 1 (min (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ)) ≤ 1 - ((a : ℝ) - r) * θ := (min_le_right _ _).trans (min_le_left _ _) have e3 : min 1 (min (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ)) ≤ ((r : ℝ) + 1) * θ := (min_le_right _ _).trans (min_le_right _ _) have hha : (h : ℝ) ≤ a := by exact_mod_cast hb2 rcases hc with ⟨hh, hu, _⟩ | ⟨hh1, hh2, hu, _⟩ | ⟨hh, hu, _⟩ · have : (h : ℝ) ≤ -(r : ℝ) - 1 := by have : h ≤ -(r : ℤ) - 1 := by omega exact_mod_cast this rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ -(r : ℝ) - 1 - h) h0.le] · have : (h : ℝ) ≤ (a : ℝ) - r := by exact_mod_cast hh2 rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ (a : ℝ) - r - h) h0.le] · rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ (a : ℝ) - h) h0.le] · exact min_le_left _ _ · rcases min_choice 1 (min (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ)) with h | h · exact Or.inl h · right rcases min_choice (1 - ((a : ℝ) - r) * θ) (((r : ℝ) + 1) * θ) with h' | h' · obtain ⟨i, hi⟩ := exists_up a r θ ((a : ℤ) - r) 0 (by omega) (by omega) (by rcases pf_cases a r hr ((a : ℤ) - r) (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, _, h3⟩ | ⟨h1, _⟩ · omega · exact h3 · omega) refine ⟨i, ?_⟩ rw [hi, h, h'] push_cast linarith · obtain ⟨i, hi⟩ := exists_up a r θ (-(r : ℤ) - 1) (-1) (by omega) (by omega) (by rcases pf_cases a r hr (-(r : ℤ) - 1) (by omega) (by omega) with ⟨_, h2⟩ | ⟨h1, _⟩ | ⟨h1, _⟩ · exact h2 · omega · omega) refine ⟨i, ?_⟩ rw [hi, h, h'] push_cast linarith have hm : max 0 (univ.sup' univ_nonempty (lowTerm a (primaryPattern a r) θ)) = max 0 (max ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ)) := by apply max_zero_sup_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a r hr θ i have e1 : (0 : ℝ) ≤ max 0 (max ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ)) := le_max_left _ _ have e2 : (r : ℝ) * θ ≤ max 0 (max ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ)) := (le_max_left _ _).trans (le_max_right _ _) have e3 : 1 - ((a : ℝ) + 1 - r) * θ ≤ max 0 (max ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ)) := (le_max_right _ _).trans (le_max_right _ _) have hha : (-(a : ℝ)) ≤ h := by exact_mod_cast hb1 rcases hc with ⟨hh, _, hl⟩ | ⟨hh1, hh2, _, hl⟩ | ⟨hh, _, hl⟩ · rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h + a) h0.le] · have : -(r : ℝ) ≤ h := by exact_mod_cast hh1 rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h + r) h0.le] · have : (a : ℝ) + 1 - r ≤ h := by exact_mod_cast hh rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h - ((a : ℝ) + 1 - r)) h0.le] · exact le_max_left _ _ · rcases max_choice 0 (max ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ)) with h | h · exact Or.inl h · right rcases max_choice ((r : ℝ) * θ) (1 - ((a : ℝ) + 1 - r) * θ) with h' | h' · obtain ⟨i, hi⟩ := exists_low a r θ (-(r : ℤ)) 0 (by omega) (by omega) (by rcases pf_cases a r hr (-(r : ℤ)) (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, _, h3⟩ | ⟨h1, _⟩ · omega · exact h3 · omega) refine ⟨i, ?_⟩ rw [hi, h, h'] push_cast linarith · obtain ⟨i, hi⟩ := exists_low a r θ ((a : ℤ) + 1 - r) 1 (by omega) (by omega) (by rcases pf_cases a r hr ((a : ℤ) + 1 - r) (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, h2, _⟩ | ⟨_, h3⟩ · omega · omega · exact h3) refine ⟨i, ?_⟩ rw [hi, h, h'] push_cast linarith rw [phaseWidth_eq a _ θ _ _ hM hm] congr 1 simp only [min_def, max_def] split_ifs <;> linarith /-! ### The end patterns -/ theorem phaseWidth_pattern_zero (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 0 < θ) (h1 : θ ≤ 1 / (a : ℝ)) : phaseWidth a (primaryPattern a 0) θ = min θ (1 - (a : ℝ) * θ) := by have hr : 0 ≤ a := Nat.zero_le a have haθ : (a : ℝ) * θ ≤ 1 := by have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) have := (le_div_iff₀ ha').1 h1 linarith have ha0 : (0 : ℝ) ≤ a := Nat.cast_nonneg a have hM : min 1 (univ.inf' univ_nonempty (upTerm a (primaryPattern a 0) θ)) = min θ (1 - (a : ℝ) * θ) := by apply min_one_inf_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a 0 hr θ i have e2 : min θ (1 - (a : ℝ) * θ) ≤ θ := min_le_left _ _ have e3 : min θ (1 - (a : ℝ) * θ) ≤ 1 - (a : ℝ) * θ := min_le_right _ _ have hha : (h : ℝ) ≤ a := by exact_mod_cast hb2 rcases hc with ⟨hh, hu, _⟩ | ⟨hh1, hh2, hu, _⟩ | ⟨hh, hu, _⟩ · have : (h : ℝ) ≤ -1 := by have : h ≤ -1 := by omega exact_mod_cast this rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ -1 - h) h0.le] · rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ (a : ℝ) - h) h0.le] · omega · exact (min_le_right _ _).trans (by nlinarith) · right rcases min_choice θ (1 - (a : ℝ) * θ) with h' | h' · obtain ⟨i, hi⟩ := exists_up a 0 θ (-1) (-1) (by omega) (by omega) (by rcases pf_cases a 0 hr (-1) (by omega) (by omega) with ⟨_, h2⟩ | ⟨h1, _⟩ | ⟨h1, _⟩ · exact h2 · omega · omega) refine ⟨i, ?_⟩ rw [hi, h'] push_cast linarith · obtain ⟨i, hi⟩ := exists_up a 0 θ (a : ℤ) 0 (by omega) (by omega) (by rcases pf_cases a 0 hr (a : ℤ) (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, _, h3⟩ | ⟨h1, _⟩ · omega · exact h3 · omega) refine ⟨i, ?_⟩ rw [hi, h'] push_cast linarith have hm : max 0 (univ.sup' univ_nonempty (lowTerm a (primaryPattern a 0) θ)) = 0 := by apply max_zero_sup_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a 0 hr θ i have hha : (-(a : ℝ)) ≤ h := by exact_mod_cast hb1 rcases hc with ⟨hh, _, hl⟩ | ⟨hh1, hh2, _, hl⟩ | ⟨hh, _, hl⟩ · rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h + a) h0.le] · have : (0 : ℝ) ≤ h := by exact_mod_cast (by omega : (0 : ℤ) ≤ h) rw [hl] nlinarith [mul_nonneg this h0.le] · omega · exact le_refl _ · exact Or.inl rfl rw [phaseWidth_eq a _ θ _ _ hM hm] have : 0 ≤ min θ (1 - (a : ℝ) * θ) := le_min h0.le (by linarith) simp only [sub_zero] exact max_eq_left this theorem phaseWidth_pattern_last (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 0 < θ) (h1 : θ ≤ 1 / (a : ℝ)) : phaseWidth a (primaryPattern a a) θ = min θ (1 - (a : ℝ) * θ) := by have hr : a ≤ a := le_refl a have haθ : (a : ℝ) * θ ≤ 1 := by have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) have := (le_div_iff₀ ha').1 h1 linarith have ha0 : (0 : ℝ) ≤ a := Nat.cast_nonneg a have hM : min 1 (univ.inf' univ_nonempty (upTerm a (primaryPattern a a) θ)) = 1 := by apply min_one_inf_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a a hr θ i have hha : (h : ℝ) ≤ a := by exact_mod_cast hb2 rcases hc with ⟨hh, _, _⟩ | ⟨hh1, hh2, hu, _⟩ | ⟨hh, hu, _⟩ · omega · have : (h : ℝ) ≤ 0 := by exact_mod_cast (by omega : h ≤ 0) rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ -h) h0.le] · rw [hu] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ (a : ℝ) - h) h0.le] · exact le_refl _ · exact Or.inl rfl have hm : max 0 (univ.sup' univ_nonempty (lowTerm a (primaryPattern a a) θ)) = max ((a : ℝ) * θ) (1 - θ) := by apply max_zero_sup_eq · intro i obtain ⟨h, hb1, hb2, hc⟩ := pf_cases_up a a hr θ i have hha : (-(a : ℝ)) ≤ h := by exact_mod_cast hb1 have e2 : (a : ℝ) * θ ≤ max ((a : ℝ) * θ) (1 - θ) := le_max_left _ _ have e3 : 1 - θ ≤ max ((a : ℝ) * θ) (1 - θ) := le_max_right _ _ rcases hc with ⟨hh, _, _⟩ | ⟨hh1, hh2, _, hl⟩ | ⟨hh, _, hl⟩ · omega · rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h + a) h0.le] · have : (1 : ℝ) ≤ h := by exact_mod_cast (by omega : (1 : ℤ) ≤ h) rw [hl] nlinarith [mul_nonneg (by linarith : (0 : ℝ) ≤ h - 1) h0.le] · exact (by nlinarith : (0 : ℝ) ≤ (a : ℝ) * θ).trans (le_max_left _ _) · right rcases max_choice ((a : ℝ) * θ) (1 - θ) with h' | h' · obtain ⟨i, hi⟩ := exists_low a a θ (-(a : ℤ)) 0 (by omega) (by omega) (by rcases pf_cases a a hr (-(a : ℤ)) (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, _, h3⟩ | ⟨h1, _⟩ · omega · exact h3 · omega) refine ⟨i, ?_⟩ rw [hi, h'] push_cast linarith · obtain ⟨i, hi⟩ := exists_low a a θ 1 1 (by omega) (by omega) (by rcases pf_cases a a hr 1 (by omega) (by omega) with ⟨h1, _⟩ | ⟨_, h2, _⟩ | ⟨_, h3⟩ · omega · omega · exact h3) refine ⟨i, ?_⟩ rw [hi, h'] push_cast linarith rw [phaseWidth_eq a _ θ _ _ hM hm] have hθ1 : 0 ≤ 1 - (a : ℝ) * θ := by linarith simp only [min_def, max_def] split_ifs <;> linarith /-! ### The primary phase function -/ theorem primaryPhase_eq (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 0 < θ) (h1 : θ ≤ 1 / (a : ℝ)) : primaryPhase a θ = 2 * min θ (1 - (a : ℝ) * θ) + ((a : ℝ) - 1) * max (min θ (min (((a : ℝ) + 2) * θ - 1) (1 - (a : ℝ) * θ))) 0 := by obtain ⟨n, rfl⟩ : ∃ n, a = n + 2 := ⟨a - 2, by omega⟩ unfold primaryPhase rw [Finset.sum_range_succ, Finset.sum_range_succ', phaseWidth_pattern_last _ ha θ h0 h1, phaseWidth_pattern_zero _ ha θ h0 h1] have : ∑ i ∈ range (n + 1), phaseWidth (n + 2) (primaryPattern (n + 2) (i + 1)) θ = ∑ i ∈ range (n + 1), max (min θ (min ((((n + 2 : ℕ) : ℝ) + 2) * θ - 1) (1 - ((n + 2 : ℕ) : ℝ) * θ))) 0 := by refine Finset.sum_congr rfl fun i hi => ?_ have := Finset.mem_range.1 hi exact phaseWidth_pattern_mid _ ha θ h0 h1 (i + 1) (by omega) (by omega) rw [this, Finset.sum_const, Finset.card_range, nsmul_eq_mul] push_cast ring theorem primaryPhase_first (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 0 < θ) (h1 : θ ≤ 1 / ((a : ℝ) + 2)) : primaryPhase a θ = 2 * θ := by have ha0 : (0 : ℝ) ≤ a := Nat.cast_nonneg a have h1' : ((a : ℝ) + 2) * θ ≤ 1 := by have := (le_div_iff₀ (by positivity : (0 : ℝ) < (a : ℝ) + 2)).1 h1 linarith have hle : θ ≤ 1 / (a : ℝ) := by have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) rw [le_div_iff₀ ha'] nlinarith rw [primaryPhase_eq a ha θ h0 hle] have e1 : min θ (1 - (a : ℝ) * θ) = θ := min_eq_left (by nlinarith) have e2 : max (min θ (min (((a : ℝ) + 2) * θ - 1) (1 - (a : ℝ) * θ))) 0 = 0 := by apply max_eq_right exact (min_le_right _ _).trans ((min_le_left _ _).trans (by linarith)) rw [e1, e2] ring theorem primaryPhase_second (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 1 / ((a : ℝ) + 2) ≤ θ) (h1 : θ ≤ 1 / ((a : ℝ) + 1)) : primaryPhase a θ = (a : ℝ) * ((a : ℝ) + 1) * θ - a + 1 := by have ha0 : (0 : ℝ) ≤ a := Nat.cast_nonneg a have g0 : 1 ≤ θ * ((a : ℝ) + 2) := (div_le_iff₀ (by positivity : (0 : ℝ) < (a : ℝ) + 2)).1 h0 have g1 : θ * ((a : ℝ) + 1) ≤ 1 := (le_div_iff₀ (by positivity : (0 : ℝ) < (a : ℝ) + 1)).1 h1 have hpos : 0 < θ := by nlinarith have hle : θ ≤ 1 / (a : ℝ) := by have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) rw [le_div_iff₀ ha'] nlinarith rw [primaryPhase_eq a ha θ hpos hle] have e1 : min θ (1 - (a : ℝ) * θ) = θ := min_eq_left (by nlinarith) have e2 : min θ (min (((a : ℝ) + 2) * θ - 1) (1 - (a : ℝ) * θ)) = ((a : ℝ) + 2) * θ - 1 := by rw [min_eq_left (by nlinarith : ((a : ℝ) + 2) * θ - 1 ≤ 1 - (a : ℝ) * θ)] exact min_eq_right (by nlinarith) have e3 : max (((a : ℝ) + 2) * θ - 1) 0 = ((a : ℝ) + 2) * θ - 1 := max_eq_left (by nlinarith) rw [e1, e2, e3] ring theorem primaryPhase_third (a : ℕ) (ha : 2 ≤ a) (θ : ℝ) (h0 : 1 / ((a : ℝ) + 1) ≤ θ) (h1 : θ ≤ 1 / (a : ℝ)) : primaryPhase a θ = (a : ℝ) + 1 - (a : ℝ) * ((a : ℝ) + 1) * θ := by have ha0 : (0 : ℝ) ≤ a := Nat.cast_nonneg a have ha' : (0 : ℝ) < a := by exact_mod_cast (by omega : 0 < a) have g0 : 1 ≤ θ * ((a : ℝ) + 1) := (div_le_iff₀ (by positivity : (0 : ℝ) < (a : ℝ) + 1)).1 h0 have g1 : θ * (a : ℝ) ≤ 1 := (le_div_iff₀ ha').1 h1 have hpos : 0 < θ := by nlinarith rw [primaryPhase_eq a ha θ hpos h1] have e1 : min θ (1 - (a : ℝ) * θ) = 1 - (a : ℝ) * θ := min_eq_right (by nlinarith) have e2 : min θ (min (((a : ℝ) + 2) * θ - 1) (1 - (a : ℝ) * θ)) = 1 - (a : ℝ) * θ := by rw [min_eq_right (by nlinarith : 1 - (a : ℝ) * θ ≤ ((a : ℝ) + 2) * θ - 1)] exact min_eq_right (by nlinarith) have e3 : max (1 - (a : ℝ) * θ) 0 = 1 - (a : ℝ) * θ := max_eq_left (by nlinarith) rw [e1, e2, e3] ring end CaballeroWidths