/-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.Algebra.BigOperators.Group.Finset.Sigmapublic import Mathlib.Order.Interval.Finset.Nat /-!# Cell vectors for general a and the row encoding of the divisor Dyck word (Caballero, Part IV) Base module of Part IV for every a: the clipped floor vector c_h = clip floor(Lambda + h theta), |h|<= a, stored as Fin (2a+1) -> Z (index h + a); realisability (Lambda, theta > 0 off the lines Lambda+ h theta in Z); the column values H_i(j) and their multiset Z(c); the rows R_i(j) and theirmultiset Z^r(c); the spread, the hypotheses of the central injectivity theorem (CentralE), thecentral cells of Section 4 (IsCentral, C_a) and ambiguity. Lemma 7 (rows_iff) is proved directly,without the Dyck word of Part I: H_i(j) counts the opening brackets before a closing bracket andR_i(j) the closing brackets before an opening bracket, and an abstract conjugacy (below_galois,conj_iff) shows that the two count multisets of two interleaved point families determine each other.Stated for all a, b (the paper fixes a >= 2, b >= 1); the positivity of Lambda, theta is part ofRealisable but is not used. ## Main results * `CaballeroGeneralA.rows_iff`: Lemma 7: for realisable c, d, Zr c = Zr d iff Z c = Z d* `CaballeroGeneralA.H_eq`: H_i(j) of a realisable vector counts the opening brackets before the closing bracket of t r^i s^j* `CaballeroGeneralA.R_eq`: R_i(j) counts the closing brackets before the opening bracket of r^i s^j* `CaballeroGeneralA.conj_iff`: for two families of points never equal across, the multiset of counts one way determines the multiset the other way ## 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 CaballeroGeneralA open Finset /-- 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 /-- `f(v) = min(b+1, max(0, v+1))`. -/def fv (b : ℕ) (v : ℤ) : ℤ := min ((b : ℤ) + 1) (max 0 (v + 1)) /-- `H_i(j) = Σ_{l=0}^{a} f(c_{i-l} + j)`. -/def H (a b : ℕ) (c : Vec a) (i j : ℕ) : ℤ := ∑ l ∈ range (a + 1), fv b (coord c ((i : ℤ) - l) + j) /-- `Z(c)`: the multiset of the `K = (a+1)(b+1)` values `H_i(j)`, `0 ≤ i ≤ a`, `0 ≤ j ≤ b`. -/def Z (a b : ℕ) (c : Vec a) : Multiset ℤ := (range (a + 1) ×ˢ range (b + 1)).val.map fun p => H a b c p.1 p.2 /-- 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 /-- The spread `max_h c_h - min_h c_h`. -/def spread {a : ℕ} (c : Vec a) : ℤ := univ.sup' ⟨⟨0, by omega⟩, mem_univ _⟩ c - univ.inf' ⟨⟨0, by omega⟩, mem_univ _⟩ c /-- The hypotheses of the central injectivity theorem (Theorem `thm:E-intro`) on the vector `c`:`-1 ≤ c_h ≤ b - E` for all `h` and spread at most `E`. -/def CentralE (a b : ℕ) (E : ℤ) (c : Vec a) : Prop := (∀ k, -1 ≤ c k ∧ c k ≤ (b : ℤ) - E) ∧ spread c ≤ E /-- The constant `C_a = 8a + 6` of Section 4. -/def Ca (a : ℕ) : ℤ := 8 * (a : ℤ) + 6 /-- Section 4: the vector of the point `(Λ, θ)` is central if `4a + 5 ≤ J ≤ b - C_a`, `J = ⌊Λ⌋`. -/def IsCentral (a b : ℕ) (Λ : ℝ) : Prop := 4 * (a : ℤ) + 5 ≤ ⌊Λ⌋ ∧ ⌊Λ⌋ ≤ (b : ℤ) - Ca a /-- `c` is ambiguous: it is realisable and another realisable vector has the same `Z`. -/def Ambiguous (a b : ℕ) (c : Vec a) : Prop := Realisable a b c ∧ ∃ d, Realisable a b d ∧ d ≠ c ∧ Z a b d = Z a b c /-! ### Points before points: an abstract conjugacy Two families of real points indexed by the same finset `I` (the opening brackets `p` and theclosing brackets `q`), never equal to each other. `before I p q v` counts the `p`-points strictlybefore the `q`-point of `v`. The multiset of these counts and the multiset of the counts theother way round determine each other. -/ section Abstract variable {ι : Type*} /-- Number of `p`-points of `I` strictly before the `q`-point of `v`. -/noncomputable def before (I : Finset ι) (p q : ι → ℝ) (v : ι) : ℕ := (I.filter fun u => p u < q v).card /-- Number of `v ∈ I` with `g v < t`. -/def below (I : Finset ι) (g : ι → ℕ) (t : ℕ) : ℕ := (I.filter fun v => g v < t).card /-- The Galois relation between the two counting functions. -/theorem below_galois (I : Finset ι) (p q : ι → ℝ) (hd : ∀ u ∈ I, ∀ v ∈ I, p u ≠ q v) (m n : ℕ) (hm : 1 ≤ m) (hmK : m ≤ I.card) (hnK : n ≤ I.card) : m ≤ below I (before I p q) n ↔ below I (before I q p) m < n := by unfold below constructor · intro hmA by_contra hBn have hBn' : n ≤ (I.filter fun u => before I q p u < m).card := not_lt.mp hBn have hAne : (I.filter fun v => before I p q v < n).Nonempty := by rw [← Finset.card_pos]; omega obtain ⟨y0, hy0A, hy0⟩ := (I.filter fun v => before I p q v < n).exists_max_image q hAne have hy0I : y0 ∈ I := (mem_filter.mp hy0A).1 have hy0n : before I p q y0 < n := (mem_filter.mp hy0A).2 by_cases hall : ∀ x ∈ I.filter (fun u => before I q p u < m), p x < q y0 · have hsub : (I.filter fun u => before I q p u < m) ⊆ I.filter fun u => p u < q y0 := by intro x hx exact mem_filter.mpr ⟨(mem_filter.mp hx).1, hall x hx⟩ have h1 := card_le_card hsub unfold before at hy0n omega · push Not at hall obtain ⟨x, hxB, hx⟩ := hall have hxI : x ∈ I := (mem_filter.mp hxB).1 have hxm : before I q p x < m := (mem_filter.mp hxB).2 have hlt : q y0 < p x := lt_of_le_of_ne hx (fun h => hd x hxI y0 hy0I h.symm) have hsub : (I.filter fun v => before I p q v < n) ⊆ I.filter fun v => q v < p x := by intro y hy exact mem_filter.mpr ⟨(mem_filter.mp hy).1, lt_of_le_of_lt (hy0 y hy) hlt⟩ have h1 := card_le_card hsub unfold before at hxm omega · intro hBn by_contra hmA have hmA' : (I.filter fun v => before I p q v < n).card < m := not_le.mp hmA have hA1 := card_filter_add_card_filter_not (s := I) (fun v => before I p q v < n) have hB1 := card_filter_add_card_filter_not (s := I) (fun u => before I q p u < m) have hAcne : (I.filter fun v => ¬ before I p q v < n).Nonempty := by rw [← Finset.card_pos]; omega have hBcne : (I.filter fun u => ¬ before I q p u < m).Nonempty := by rw [← Finset.card_pos]; omega obtain ⟨y1, hy1, hy1min⟩ := (I.filter fun v => ¬ before I p q v < n).exists_min_image q hAcne obtain ⟨x1, hx1, hx1min⟩ := (I.filter fun u => ¬ before I q p u < m).exists_min_image p hBcne have hy1I : y1 ∈ I := (mem_filter.mp hy1).1 have hx1I : x1 ∈ I := (mem_filter.mp hx1).1 have hy1n : ¬ before I p q y1 < n := (mem_filter.mp hy1).2 have hx1m : ¬ before I q p x1 < m := (mem_filter.mp hx1).2 rcases lt_or_gt_of_ne (hd x1 hx1I y1 hy1I) with h | h · have hsub : (I.filter fun v => q v < p x1) ⊆ I.filter fun v => before I p q v < n := by intro y hy obtain ⟨hyI, hyx⟩ := mem_filter.mp hy refine mem_filter.mpr ⟨hyI, ?_⟩ by_contra hyn have := hy1min y (mem_filter.mpr ⟨hyI, hyn⟩) linarith have h1 := card_le_card hsub apply hx1m unfold before omega · have hsub : (I.filter fun u => p u < q y1) ⊆ I.filter fun u => before I q p u < m := by intro x hx obtain ⟨hxI, hxy⟩ := mem_filter.mp hx refine mem_filter.mpr ⟨hxI, ?_⟩ by_contra hxn have := hx1min x (mem_filter.mpr ⟨hxI, hxn⟩) linarith have h1 := card_le_card hsub apply hy1n unfold before omega /-- The counting function of one side, read off the counting function of the other side. -/theorem below_eq_card_filter (I : Finset ι) (p q : ι → ℝ) (hd : ∀ u ∈ I, ∀ v ∈ I, p u ≠ q v) (n : ℕ) (hnK : n ≤ I.card) : below I (before I p q) n = ((Icc 1 I.card).filter fun m => below I (before I q p) m < n).card := by have hle : below I (before I p q) n ≤ I.card := card_filter_le _ _ have e : ((Icc 1 I.card).filter fun m => below I (before I q p) m < n) = Icc 1 (below I (before I p q) n) := by ext m simp only [mem_filter, mem_Icc] constructor · rintro ⟨⟨h1, h2⟩, h3⟩ exact ⟨h1, (below_galois I p q hd m n h1 h2 hnK).mpr h3⟩ · rintro ⟨h1, h2⟩ exact ⟨⟨h1, h2.trans hle⟩, (below_galois I p q hd m n h1 (h2.trans hle) hnK).mp h2⟩ rw [e, Nat.card_Icc] omega theorem below_big (I : Finset ι) (p q : ι → ℝ) (n : ℕ) (hn : I.card < n) : below I (before I p q) n = I.card := by unfold below rw [filter_true_of_mem] intro v _ exact lt_of_le_of_lt (card_filter_le _ _) hn theorem below_det (I : Finset ι) (p q p' q' : ι → ℝ) (hd : ∀ u ∈ I, ∀ v ∈ I, p u ≠ q v) (hd' : ∀ u ∈ I, ∀ v ∈ I, p' u ≠ q' v) (h : ∀ t, below I (before I q p) t = below I (before I q' p') t) : ∀ t, below I (before I p q) t = below I (before I p' q') t := by intro n by_cases hnK : n ≤ I.card · rw [below_eq_card_filter I p q hd n hnK, below_eq_card_filter I p' q' hd' n hnK] simp only [h] · rw [below_big I p q n (by omega), below_big I p' q' n (by omega)] theorem below_eq_card (I : Finset ι) (g : ι → ℕ) (t : ℕ) : below I g t = Multiset.card ((I.val.map g).filter (· < t)) := by rw [Multiset.filter_map, Multiset.card_map] rfl theorem multiset_ext_lt (s s' : Multiset ℕ) (h : ∀ t, Multiset.card (s.filter (· < t)) = Multiset.card (s'.filter (· < t))) : s = s' := by have key : ∀ (s : Multiset ℕ) (x : ℕ), Multiset.count x s + Multiset.card (s.filter (· < x)) = Multiset.card (s.filter (· < x + 1)) := by intro s x have e := Multiset.filter_add_not (fun y => y < x) (s.filter (· < x + 1)) rw [Multiset.filter_filter, Multiset.filter_filter] at e have e1 : s.filter (fun y => y < x ∧ y < x + 1) = s.filter (· < x) := Multiset.filter_congr (fun y _ => by omega) have e2 : s.filter (fun y => ¬ y < x ∧ y < x + 1) = s.filter (x = ·) := Multiset.filter_congr (fun y _ => by omega) rw [e1, e2] at e rw [← e, Multiset.card_add, Multiset.count_eq_card_filter_eq] omega ext x have h1 := key s x have h2 := key s' x have h3 := h x have h4 := h (x + 1) omega theorem map_eq_iff_below (I : Finset ι) (g g' : ι → ℕ) : I.val.map g = I.val.map g' ↔ ∀ t, below I g t = below I g' t := by constructor · intro h t rw [below_eq_card, below_eq_card, h] · intro h apply multiset_ext_lt intro t rw [← below_eq_card, ← below_eq_card] exact h t /-- The two count multisets determine each other. -/theorem conj_iff (I : Finset ι) (p q p' q' : ι → ℝ) (hd : ∀ u ∈ I, ∀ v ∈ I, p u ≠ q v) (hd' : ∀ u ∈ I, ∀ v ∈ I, p' u ≠ q' v) : I.val.map (before I q p) = I.val.map (before I q' p') ↔ I.val.map (before I p q) = I.val.map (before I p' q') := by have hs : ∀ u ∈ I, ∀ v ∈ I, q u ≠ p v := fun u hu v hv e => hd v hv u hu e.symm have hs' : ∀ u ∈ I, ∀ v ∈ I, q' u ≠ p' v := fun u hu v hv e => hd' v hv u hu e.symm rw [map_eq_iff_below, map_eq_iff_below] exact ⟨below_det I p q p' q' hd hd', below_det I q p q' p' hs hs'⟩ end Abstract /-! ### The brackets of a point `(Λ, θ)` -/ /-- Position of the opening bracket of `r^i s^j`: `iθ + j`. -/def opos (θ : ℝ) (v : ℕ × ℕ) : ℝ := (v.1 : ℝ) * θ + v.2 /-- Position of the closing bracket of `t r^i s^j`: `Λ + iθ + j`. -/def cpos (Λ θ : ℝ) (v : ℕ × ℕ) : ℝ := Λ + (v.1 : ℝ) * θ + v.2 theorem coord_cellVec (a b : ℕ) (Λ θ : ℝ) (h : ℤ) (h1 : -(a : ℤ) ≤ h) (h2 : h ≤ a) : coord (cellVec a b Λ θ) h = CaballeroPeaks.clip b ⌊Λ + (h : ℝ) * θ⌋ := by unfold coord cellVec have e : (((h + a).toNat : ℕ) : ℤ) - a = h := by omega split_ifs with hh · simp only [e] · exact absurd ⟨h1, h2⟩ hh theorem lt_iff_le_floor_of_off (x : ℝ) (hx : ∀ z : ℤ, x ≠ z) (z : ℤ) : (z : ℝ) < x ↔ z ≤ ⌊x⌋ := by rw [Int.le_floor] exact ⟨le_of_lt, fun h => lt_of_le_of_ne h (fun e => hx z e.symm)⟩ theorem card_range_filter_le (b : ℕ) (F : ℤ) : (((range (b + 1)).filter fun j : ℕ => (j : ℤ) ≤ F).card : ℤ) = min ((b : ℤ) + 1) (max 0 (F + 1)) := by have e : ((range (b + 1)).filter fun j : ℕ => (j : ℤ) ≤ F) = range (min (b + 1) (F + 1).toNat) := by ext j simp only [mem_filter, mem_range] omega rw [e, card_range] push_cast omega theorem dist_brackets (a b : ℕ) (Λ θ : ℝ) (hoff : OffLines a Λ θ) : ∀ u ∈ range (a + 1) ×ˢ range (b + 1), ∀ v ∈ range (a + 1) ×ˢ range (b + 1), opos θ u ≠ cpos Λ θ v := by intro u hu v hv e simp only [mem_product, mem_range] at hu hv apply hoff ((v.1 : ℤ) - u.1) (by omega) (by omega) ((u.2 : ℤ) - v.2) unfold opos cpos at e push_cast linarith /-- `H_i(j)` counts the opening brackets before the closing bracket of `t r^i s^j`. -/theorem H_eq (a b : ℕ) (Λ θ : ℝ) (hoff : OffLines a Λ θ) (v : ℕ × ℕ) (hi : v.1 ≤ a) (hj : v.2 ≤ b) : H a b (cellVec a b Λ θ) v.1 v.2 = (before (range (a + 1) ×ˢ range (b + 1)) (opos θ) (cpos Λ θ) v : ℤ) := by unfold before H rw [natCast_card_filter, sum_product] apply sum_congr rfl intro l hl rw [mem_range] at hl rw [coord_cellVec _ _ _ _ _ (by omega) (by omega)] have hx := hoff ((v.1 : ℤ) - l) (by omega) (by omega) simp only [sum_boole] have e : ((range (b + 1)).filter fun j' : ℕ => opos θ (l, j') < cpos Λ θ v) = (range (b + 1)).filter fun j' : ℕ => (j' : ℤ) ≤ ⌊Λ + (((v.1 : ℤ) - l : ℤ) : ℝ) * θ⌋ + v.2 := by apply filter_congr intro j' _ have key := lt_iff_le_floor_of_off _ hx ((j' : ℤ) - v.2) have ex : opos θ (l, j') < cpos Λ θ v ↔ (((j' : ℤ) - v.2 : ℤ) : ℝ) < Λ + (((v.1 : ℤ) - l : ℤ) : ℝ) * θ := by unfold opos cpos push_cast constructor <;> intro h <;> linarith rw [ex, key] omega rw [e, card_range_filter_le] unfold fv CaballeroPeaks.clip omega /-- `R_i(j)` counts the closing brackets before the opening bracket of `r^i s^j`. -/theorem R_eq (a b : ℕ) (Λ θ : ℝ) (hoff : OffLines a Λ θ) (v : ℕ × ℕ) (hi : v.1 ≤ a) (hj : v.2 ≤ b) : R a b (cellVec a b Λ θ) v.1 v.2 = (before (range (a + 1) ×ˢ range (b + 1)) (cpos Λ θ) (opos θ) v : ℤ) := by unfold before R rw [natCast_card_filter, sum_product] symm apply sum_nbij' (fun l : ℕ => (l : ℤ) - v.1) (fun h : ℤ => (h + v.1).toNat) · intro l hl simp only [mem_range, mem_Icc] at hl ⊢ omega · intro h hh simp only [mem_Icc, mem_range] at hh ⊢ omega · intro l _ simp · intro h hh simp only [mem_Icc] at hh omega · intro l hl simp only [mem_range] at hl rw [coord_cellVec _ _ _ _ _ (by omega) (by omega)] have hx := hoff ((l : ℤ) - v.1) (by omega) (by omega) simp only [sum_boole] have e : ((range (b + 1)).filter fun j' : ℕ => cpos Λ θ (l, j') < opos θ v) = (range (b + 1)).filter fun j' : ℕ => (j' : ℤ) ≤ (v.2 : ℤ) - ⌊Λ + (((l : ℤ) - v.1 : ℤ) : ℝ) * θ⌋ - 1 := by apply filter_congr intro j' _ have key := Int.floor_lt (a := Λ + (((l : ℤ) - v.1 : ℤ) : ℝ) * θ) (z := (v.2 : ℤ) - j') have ex : cpos Λ θ (l, j') < opos θ v ↔ Λ + (((l : ℤ) - v.1 : ℤ) : ℝ) * θ < (((v.2 : ℤ) - j' : ℤ) : ℝ) := by unfold opos cpos push_cast constructor <;> intro h <;> linarith rw [ex, ← key] omega rw [e, card_range_filter_le] unfold CaballeroPeaks.clip omega theorem Z_eq (a b : ℕ) (Λ θ : ℝ) (hoff : OffLines a Λ θ) : Z a b (cellVec a b Λ θ) = ((range (a + 1) ×ˢ range (b + 1)).val.map (before (range (a + 1) ×ˢ range (b + 1)) (opos θ) (cpos Λ θ))).map (Nat.cast : ℕ → ℤ) := by unfold Z rw [Multiset.map_map] apply Multiset.map_congr rfl intro v hv have hv' : v ∈ range (a + 1) ×ˢ range (b + 1) := hv simp only [mem_product, mem_range] at hv' exact H_eq a b Λ θ hoff v (by omega) (by omega) theorem Zr_eq (a b : ℕ) (Λ θ : ℝ) (hoff : OffLines a Λ θ) : Zr a b (cellVec a b Λ θ) = ((range (a + 1) ×ˢ range (b + 1)).val.map (before (range (a + 1) ×ˢ range (b + 1)) (cpos Λ θ) (opos θ))).map (Nat.cast : ℕ → ℤ) := by unfold Zr rw [Multiset.map_map] apply Multiset.map_congr rfl intro v hv have hv' : v ∈ range (a + 1) ×ˢ range (b + 1) := hv simp only [mem_product, mem_range] at hv' exact R_eq a b Λ θ hoff v (by omega) (by omega) /-- Lemma 7: for realisable `c, d`, `Zʳ(c) = Zʳ(d)` if and only if `Z(c) = Z(d)`. -/theorem rows_iff (a b : ℕ) (c d : Vec a) (hc : Realisable a b c) (hd : Realisable a b d) : Zr a b c = Zr a b d ↔ Z a b c = Z a b d := by obtain ⟨Λ, θ, -, -, hoff, rfl⟩ := hc obtain ⟨Λ', θ', -, -, hoff', rfl⟩ := hd rw [Zr_eq a b Λ θ hoff, Zr_eq a b Λ' θ' hoff', Z_eq a b Λ θ hoff, Z_eq a b Λ' θ' hoff', (Multiset.map_injective Nat.cast_injective).eq_iff, (Multiset.map_injective Nat.cast_injective).eq_iff] exact conj_iff _ (opos θ) (cpos Λ θ) (opos θ') (cpos Λ' θ') (dist_brackets a b Λ θ hoff) (dist_brackets a b Λ' θ' hoff') end CaballeroGeneralA