/-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.AngularPeakspublic import Mathlib.Algebra.BigOperators.Ring.Finsetpublic import Mathlib.Order.Interval.Finset.Natpublic import Mathlib.Order.Interval.Multisetpublic import Mathlib.Data.Int.Interval /-!# Two-step cell vectors and their row multisets (Caballero, Part IV) For integers rho >= 1 and -1 <= x <= y = b - rho, the cell vector c = (x, y, b, ..., b) of Part IV(c_{-a} = x, c_{-a+1} = y, c_h = b for h >= 2 - a) has positive row multiset Z^r(c) equal to{{1..rho}} + {{1..Delta-rho}} + {{Delta-rho+2, Delta-rho+4, .., Delta+rho}} with Delta = b - x, and,when Delta >= 2 rho - 1, it is the clipped floor vector of the explicit point theta = y - x + 1/2,Lambda = x + 1/4 + a theta, which is off the lines with Lambda, theta > 0. The row multiset Z^r iscomputed from the definition (the cap does not act because x >= -1). The definitions Vec, coord,cellVec, OffLines, Realisable, R, Zr are copies, with the same bodies, of the base module of Part IV(queue entry 312). Hypotheses: a >= 2 (fixed in the paper; used for Lambda > 0); b >= 1 is notassumed. "Positive part" is Multiset.filter (0 < .). ## Main results * `CaballeroTwoStep.lem_twostep`: Lemma 16: positive part of Z^r of the two-step vector, and the explicit realising point for Delta >= 2 rho - 1* `CaballeroTwoStep.twoStep_realisable`: for Delta >= 2 rho - 1 the two-step vector is realisable* `CaballeroTwoStep.R_twoStep`: the rows of the two-step vector: only c_{-a} and c_{-a+1} contribute ## 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.-/ @[expose] public section namespace CaballeroTwoStep open Finset /-! ### Copies of the definitions of 312 (`CaballeroGeneralA`), same bodies -/ /-- A cell vector `c = (c_h)_{h=-a}^{a}`; the coordinate `c_h` is stored at index `h + a`. -/abbrev Vec (a : ℕ) := Fin (2 * a + 1) → ℤ /-- The coordinate `c_h` for `h : ℤ` (`-a ≤ h ≤ a`); it is `0` outside that range, where it isnever used. -/def coord {a : ℕ} (c : Vec a) (h : ℤ) : ℤ := if hh : -(a : ℤ) ≤ h ∧ h ≤ a then c ⟨(h + a).toNat, by omega⟩ else 0 /-- The clipped floor vector of the point `(Λ, θ)`: `c_h = clip ⌊Λ + hθ⌋`, `-a ≤ h ≤ a`, with`clip(v) = min(b, max(-b-1, v))` (`CaballeroPeaks.clip`, the clipping of Parts I-III). -/noncomputable def cellVec (a b : ℕ) (Λ θ : ℝ) : Vec a := fun k => CaballeroPeaks.clip b ⌊Λ + ((((k : ℕ) : ℤ) - a : ℤ) : ℝ) * θ⌋ /-- `(Λ, θ)` lies off the lines `Λ + hθ ∈ ℤ`, `|h| ≤ a`. -/def OffLines (a : ℕ) (Λ θ : ℝ) : Prop := ∀ h : ℤ, -(a : ℤ) ≤ h → h ≤ a → ∀ z : ℤ, Λ + (h : ℝ) * θ ≠ (z : ℝ) /-- A vector is realisable if it is the clipped floor vector of some `Λ, θ > 0` off the lines. -/def Realisable (a b : ℕ) (c : Vec a) : Prop := ∃ Λ θ : ℝ, 0 < Λ ∧ 0 < θ ∧ OffLines a Λ θ ∧ cellVec a b Λ θ = c /-- The rows `R_i(j) = Σ_{h=-i}^{a-i} min(b+1, max(0, j - c_h))`. -/def R (a b : ℕ) (c : Vec a) (i j : ℕ) : ℤ := ∑ h ∈ Icc (-(i : ℤ)) ((a : ℤ) - i), min ((b : ℤ) + 1) (max 0 ((j : ℤ) - coord c h)) /-- `Zʳ(c)`: the multiset of the `K` values `R_i(j)`, `0 ≤ i ≤ a`, `0 ≤ j ≤ b`. -/def Zr (a b : ℕ) (c : Vec a) : Multiset ℤ := (range (a + 1) ×ˢ range (b + 1)).val.map fun p => R a b c p.1 p.2 /-! ### Two-step vectors -/ /-- The two-step vector `c = (x, y, b, …, b)` with `y = b - ρ`: `c_{-a} = x`, `c_{-a+1} = y`,`c_h = b` for `h ≥ 2 - a` (index `h + a`: `0 ↦ x`, `1 ↦ y`, otherwise `b`). -/def twoStep (a b ρ : ℕ) (x : ℤ) : Vec a := fun k => if (k : ℕ) = 0 then x else if (k : ℕ) = 1 then (b : ℤ) - ρ else b theorem coord_twoStep (a b ρ : ℕ) (x : ℤ) (h : ℤ) (h1 : -(a : ℤ) ≤ h) (h2 : h ≤ a) : coord (twoStep a b ρ x) h = if h = -(a : ℤ) then x else if h = -(a : ℤ) + 1 then (b : ℤ) - ρ else b := by unfold coord twoStep have hh : -(a : ℤ) ≤ h ∧ h ≤ a := ⟨h1, h2⟩ have e : (h + a).toNat = 0 ↔ h = -(a : ℤ) := by omega have e1 : (h + a).toNat = 1 ↔ h = -(a : ℤ) + 1 := by omega simp [hh, e, e1] theorem R_twoStep (a b ρ : ℕ) (x : ℤ) (ha : 1 ≤ a) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) (i j : ℕ) (hi : i ≤ a) (hj : j ≤ b) : R a b (twoStep a b ρ x) i j = (if i = a then max 0 ((j : ℤ) - x) else 0) + (if a ≤ i + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0) := by unfold R have hg : ∀ h ∈ Icc (-(i : ℤ)) ((a : ℤ) - i), min ((b : ℤ) + 1) (max 0 ((j : ℤ) - coord (twoStep a b ρ x) h)) = (if h = -(a : ℤ) then max 0 ((j : ℤ) - x) else 0) + (if h = -(a : ℤ) + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0) := by intro h hh rw [mem_Icc] at hh rw [coord_twoStep a b ρ x h (by omega) (by omega)] split_ifs <;> omega rw [sum_congr rfl hg, sum_add_distrib, sum_ite_eq', sum_ite_eq'] simp only [mem_Icc] split_ifs <;> omega theorem map_eq_sum {ι : Type*} (S : Finset ι) (f : ι → ℤ) : S.val.map f = ∑ p ∈ S, ({f p} : Multiset ℤ) := by rw [Finset.sum_eq_multiset_sum] have := Multiset.sum_map_singleton (S.val.map f) rw [Multiset.map_map] at this exact this.symm theorem Zr_eq_sum (a b : ℕ) (c : Vec a) : Zr a b c = ∑ i ∈ range (a + 1), (range (b + 1)).val.map (fun j => R a b c i j) := by unfold Zr rw [map_eq_sum, sum_product] apply sum_congr rfl intro i _ rw [map_eq_sum] theorem row_one (b ρ : ℕ) (hy : -1 ≤ (b : ℤ) - ρ) : (((range (b + 1)).val.map fun j : ℕ => max 0 ((j : ℤ) - ((b : ℤ) - ρ))).filter (fun v => 0 < v)) = Multiset.Icc (1 : ℤ) ρ := by rw [Multiset.filter_map] have hn : Multiset.Nodup (Multiset.map (fun j : ℕ => max 0 ((j : ℤ) - ((b : ℤ) - ρ))) (Multiset.filter ((fun v : ℤ => 0 < v) ∘ fun j : ℕ => max 0 ((j : ℤ) - ((b : ℤ) - ρ))) (range (b + 1)).val)) := by apply Multiset.Nodup.map_on · intro u hu v hv e simp only [Multiset.mem_filter, Function.comp, mem_val, mem_range] at hu hv omega · exact (range (b + 1)).nodup.filter _ rw [Multiset.Nodup.ext hn (Multiset.nodup_Icc)] intro v simp only [Multiset.mem_map, Multiset.mem_filter, Function.comp, mem_val, mem_range, Multiset.mem_Icc] constructor · rintro ⟨j, ⟨hj, hp⟩, rfl⟩ omega · rintro ⟨h1, h2⟩ exact ⟨(v + ((b : ℤ) - ρ)).toNat, ⟨by omega, by omega⟩, by omega⟩ theorem row_two (b ρ : ℕ) (x : ℤ) (hρ : 1 ≤ ρ) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) : (((range (b + 1)).val.map fun j : ℕ => max 0 ((j : ℤ) - x) + max 0 ((j : ℤ) - ((b : ℤ) - ρ))).filter (fun v => 0 < v)) = Multiset.Icc (1 : ℤ) (((b : ℤ) - ρ) - x) + (Multiset.Icc (1 : ℤ) ρ).map (fun t => (((b : ℤ) - ρ) - x) + 2 * t) := by rw [Multiset.filter_map] have hn : Multiset.Nodup (Multiset.map (fun j : ℕ => max 0 ((j : ℤ) - x) + max 0 ((j : ℤ) - ((b : ℤ) - ρ))) (Multiset.filter ((fun v : ℤ => 0 < v) ∘ fun j : ℕ => max 0 ((j : ℤ) - x) + max 0 ((j : ℤ) - ((b : ℤ) - ρ))) (range (b + 1)).val)) := by apply Multiset.Nodup.map_on · intro u hu v hv e simp only [Multiset.mem_filter, Function.comp, mem_val, mem_range] at hu hv omega · exact (range (b + 1)).nodup.filter _ have hm : Multiset.Nodup (Multiset.Icc (1 : ℤ) (((b : ℤ) - ρ) - x) + (Multiset.Icc (1 : ℤ) ρ).map (fun t => (((b : ℤ) - ρ) - x) + 2 * t)) := by rw [Multiset.nodup_add] refine ⟨Multiset.nodup_Icc, ?_, ?_⟩ · apply Multiset.Nodup.map_on · intro u _ v _ e omega · exact Multiset.nodup_Icc · rw [Multiset.disjoint_left] intro v hv hv' simp only [Multiset.mem_Icc, Multiset.mem_map] at hv hv' obtain ⟨t, ⟨ht, _⟩, rfl⟩ := hv' omega rw [Multiset.Nodup.ext hn hm] intro v simp only [Multiset.mem_map, Multiset.mem_filter, Function.comp, mem_val, mem_range, Multiset.mem_add, Multiset.mem_Icc] constructor · rintro ⟨j, ⟨hj, hp⟩, rfl⟩ by_cases hjy : (j : ℤ) ≤ (b : ℤ) - ρ · left; omega · right exact ⟨(j : ℤ) - ((b : ℤ) - ρ), by omega, by omega⟩ · rintro (⟨h1, h2⟩ | ⟨t, ⟨h1, h2⟩, rfl⟩) · exact ⟨(v + x).toNat, ⟨by omega, by omega⟩, by omega⟩ · exact ⟨(t + ((b : ℤ) - ρ)).toNat, ⟨by omega, by omega⟩, by omega⟩ theorem part_one (a b ρ : ℕ) (x : ℤ) (ha : 2 ≤ a) (hρ : 1 ≤ ρ) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) : (Zr a b (twoStep a b ρ x)).filter (fun v => 0 < v) = Multiset.Icc (1 : ℤ) ρ + Multiset.Icc (1 : ℤ) (((b : ℤ) - x) - ρ) + (Multiset.Icc (1 : ℤ) ρ).map (fun t => ((b : ℤ) - x) - ρ + 2 * t) := by obtain ⟨n, rfl⟩ : ∃ n, a = n + 2 := ⟨a - 2, by omega⟩ have hrow : ∀ i ∈ range (n + 3), (range (b + 1)).val.map (fun j => R (n + 2) b (twoStep (n + 2) b ρ x) i j) = (range (b + 1)).val.map (fun j : ℕ => (if i = n + 2 then max 0 ((j : ℤ) - x) else 0) + (if n + 2 ≤ i + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0)) := by intro i hi rw [mem_range] at hi apply Multiset.map_congr rfl intro j hj rw [mem_val, mem_range] at hj exact R_twoStep (n + 2) b ρ x (by omega) hx hxy i j (by omega) (by omega) rw [Zr_eq_sum, sum_congr rfl hrow, show n + 2 + 1 = n + 1 + 1 + 1 from rfl, sum_range_succ, sum_range_succ, Multiset.filter_add, Multiset.filter_add] have h0 : (Multiset.filter (fun v : ℤ => 0 < v) (∑ i ∈ range (n + 1), (range (b + 1)).val.map (fun j : ℕ => (if i = n + 2 then max 0 ((j : ℤ) - x) else 0) + (if n + 2 ≤ i + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0)))) = 0 := by rw [Multiset.filter_eq_nil] intro v hv rw [Multiset.mem_sum] at hv obtain ⟨i, hi, hv⟩ := hv rw [mem_range] at hi rw [Multiset.mem_map] at hv obtain ⟨j, _, rfl⟩ := hv have h1 : ¬ i = n + 2 := by omega have h2 : ¬ n + 2 ≤ i + 1 := by omega simp [h1, h2] rw [h0, zero_add] have e1 : ((range (b + 1)).val.map (fun j : ℕ => (if n + 1 = n + 2 then max 0 ((j : ℤ) - x) else 0) + (if n + 2 ≤ n + 1 + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0))) = (range (b + 1)).val.map (fun j : ℕ => max 0 ((j : ℤ) - ((b : ℤ) - ρ))) := by apply Multiset.map_congr rfl intro j _ simp have e2 : ((range (b + 1)).val.map (fun j : ℕ => (if n + 2 = n + 2 then max 0 ((j : ℤ) - x) else 0) + (if n + 2 ≤ n + 2 + 1 then max 0 ((j : ℤ) - ((b : ℤ) - ρ)) else 0))) = (range (b + 1)).val.map (fun j : ℕ => max 0 ((j : ℤ) - x) + max 0 ((j : ℤ) - ((b : ℤ) - ρ))) := by apply Multiset.map_congr rfl intro j _ simp rw [e1, e2, row_one b ρ (by omega), row_two b ρ x hρ hx hxy] have e3 : ((b : ℤ) - x) - ρ = ((b : ℤ) - ρ) - x := by ring rw [e3, add_assoc] theorem part_two (a b ρ : ℕ) (x : ℤ) (ha : 2 ≤ a) (hρ : 1 ≤ ρ) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) (hΔ : 2 * (ρ : ℤ) - 1 ≤ (b : ℤ) - x) : 0 < x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) ∧ 0 < ((((b : ℤ) - ρ) - x : ℤ) : ℝ) + 1 / 2 ∧ OffLines a (x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2)) ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) ∧ cellVec a b (x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2)) ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) = twoStep a b ρ x := by obtain ⟨n, hn⟩ : ∃ n : ℤ, n = ((b : ℤ) - ρ) - x := ⟨_, rfl⟩ rw [← hn] have hn0 : 0 ≤ n := by omega have hnR : (0 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn0 have key : ∀ k : ℕ, (x : ℝ) + 1 / 4 + (a : ℝ) * ((n : ℝ) + 1 / 2) + ((((k : ℤ) - a : ℤ)) : ℝ) * ((n : ℝ) + 1 / 2) = (((x + k * n : ℤ)) : ℝ) + 1 / 4 + (k : ℝ) / 2 := by intro k push_cast ring refine ⟨?_, by linarith, ?_, ?_⟩ · have h2 : (2 : ℝ) ≤ (a : ℝ) := by exact_mod_cast ha have h3 : (x : ℝ) ≥ -1 := by exact_mod_cast hx nlinarith · intro h h1 h2 z hz obtain ⟨k, rfl⟩ : ∃ k : ℕ, h = (k : ℤ) - a := ⟨(h + a).toNat, by omega⟩ rw [key k] at hz have hr : (((4 * (x + k * n) + 1 + 2 * k : ℤ)) : ℝ) = (((4 * z : ℤ)) : ℝ) := by push_cast push_cast at hz linarith have hi := Int.cast_injective hr omega · funext k unfold cellVec twoStep rw [key (k : ℕ)] have hk : (k : ℕ) = 0 ∨ (k : ℕ) = 1 ∨ 2 ≤ (k : ℕ) := by omega rcases hk with h0 | h1 | h2 · rw [h0] have : ⌊(((x + ((0 : ℕ) : ℤ) * n : ℤ) : ℝ)) + 1 / 4 + ((0 : ℕ) : ℝ) / 2⌋ = x := by rw [Int.floor_eq_iff] push_cast constructor <;> linarith rw [this] simp only [↓reduceIte] unfold CaballeroPeaks.clip omega · rw [h1] have : ⌊(((x + ((1 : ℕ) : ℤ) * n : ℤ) : ℝ)) + 1 / 4 + ((1 : ℕ) : ℝ) / 2⌋ = (b : ℤ) - ρ := by rw [Int.floor_eq_iff] have e : ((b : ℤ) - ρ) = x + n := by omega rw [e] push_cast constructor <;> linarith rw [this] simp only [one_ne_zero, ↓reduceIte] unfold CaballeroPeaks.clip omega · have hk0 : (k : ℕ) ≠ 0 := by omega have hk1 : (k : ℕ) ≠ 1 := by omega simp only [hk0, hk1, ↓reduceIte] have hkR : (2 : ℝ) ≤ ((k : ℕ) : ℝ) := by exact_mod_cast h2 have hkn : (2 : ℝ) * n ≤ ((k : ℕ) : ℝ) * n := by nlinarith have hbn : (n : ℝ) = (b : ℝ) - ρ - x := by rw [hn]; push_cast; ring have hΔR : 2 * (ρ : ℝ) - 1 ≤ (b : ℝ) - x := by exact_mod_cast hΔ have hfl : (b : ℤ) ≤ ⌊(((x + ((k : ℕ) : ℤ) * n : ℤ) : ℝ)) + 1 / 4 + ((k : ℕ) : ℝ) / 2⌋ := by rw [Int.le_floor] push_cast nlinarith unfold CaballeroPeaks.clip omega /-- Lemma 16. For integers `ρ ≥ 1` and `-1 ≤ x ≤ y = b - ρ`, `c = (x, y, b, …, b)`,`Δ = b - x`: the positive part of `Zʳ(c)` is`{{1,…,ρ}} ⊎ {{1,…,Δ-ρ}} ⊎ {{Δ-ρ+2, Δ-ρ+4, …, Δ+ρ}}`; and if `Δ ≥ 2ρ-1`, `c` is the vector of thepoint `θ = y - x + 1/2`, `Λ = x + 1/4 + aθ` (which is off the lines, with `Λ, θ > 0`). -/theorem lem_twostep (a b ρ : ℕ) (x : ℤ) (ha : 2 ≤ a) (hρ : 1 ≤ ρ) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) : (Zr a b (twoStep a b ρ x)).filter (fun v => 0 < v) = Multiset.Icc (1 : ℤ) ρ + Multiset.Icc (1 : ℤ) (((b : ℤ) - x) - ρ) + (Multiset.Icc (1 : ℤ) ρ).map (fun t => ((b : ℤ) - x) - ρ + 2 * t) ∧ (2 * (ρ : ℤ) - 1 ≤ (b : ℤ) - x → 0 < x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) ∧ 0 < ((((b : ℤ) - ρ) - x : ℤ) : ℝ) + 1 / 2 ∧ OffLines a (x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2)) ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) ∧ cellVec a b (x + 1 / 4 + (a : ℝ) * ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2)) ((((b : ℤ) - ρ) - x : ℤ) + 1 / 2) = twoStep a b ρ x) := ⟨part_one a b ρ x ha hρ hx hxy, fun hΔ => part_two a b ρ x ha hρ hx hxy hΔ⟩ /-- Corollary: for `Δ ≥ 2ρ - 1` the two-step vector is realisable. -/theorem twoStep_realisable (a b ρ : ℕ) (x : ℤ) (ha : 2 ≤ a) (hρ : 1 ≤ ρ) (hx : -1 ≤ x) (hxy : x ≤ (b : ℤ) - ρ) (hΔ : 2 * (ρ : ℤ) - 1 ≤ (b : ℤ) - x) : Realisable a b (twoStep a b ρ x) := by obtain ⟨h1, h2, h3, h4⟩ := part_two a b ρ x ha hρ hx hxy hΔ exact ⟨_, _, h1, h2, h3, h4⟩ end CaballeroTwoStep