import Mathlib /-! # Globally Simple Rectangular Integer Heffter Arrays & Orthogonal Cyclic Cycle Decompositions This foundational library establishes the theory of **globally simple integer Heffter arrays** `SH(m, n; s, k)` (Costa–Morini–Pasotti–Pellegrini 2018) and their direct graph-theoretic consequence: **orthogonal cyclic `s`-cycle and `k`-cycle decompositions of the complete graph `K_{2nk+1}`** (Archdeacon 2015; Costa–Morini–Pasotti–Pellegrini 2018, Theorem 1.1). ## Main Definitions & Results 1. `IntegerHeffterArray m n s k`: partially filled `m × n` integer matrix with `s` filled entries per row, `k` per column, zero row/column sums over `ℤ`, and absolute values in bijection with `{1, …, n * k}`. 2. `IsSimpleList v L`: natural left-to-right (resp. top-to-bottom) partial sums of `L : List ℤ` are pairwise distinct in `ZMod v`. 3. `GloballySimpleHeffterArray m n s k`: an integer Heffter array in which every row and every column is simple modulo `2 * n * k + 1`. 4. **Sub-sum characterization (`isSimpleList_iff_subsum_ne_zero`)**: a zero-sum list is simple mod `v` iff no nonempty proper contiguous sub-block sums to `0 : ZMod v`. 5. **Costa–Morini–Pasotti–Pellegrini Proposition 2.1 (`isSimpleList_of_length_le_five`, `globallySimple_of_le_five`)**: every integer Heffter array with `s ≤ 5` and `k ≤ 5` is automatically globally simple. 6. **Length-6 and Length-7 Reductions (`isSimpleList_of_len_six`, `isSimpleList_of_len_seven`)**: explicit reduction of global simplicity for `s = 6` (3 length-3 sub-sums) and `s = 7` (7 length-3 sub-sums) to linear inequalities avoiding `{0, ±(2nk + 1)}`. 7. **Orthogonal Cyclic Cycle Decompositions (`toOrthogonalCyclicCycleDecomps`)**: every `GloballySimpleHeffterArray m n s k` constructs an explicit pair of orthogonal cyclic `s`-cycle and `k`-cycle decompositions of `K_{2nk+1}`. 8. **Sparse Block-Diagonal Assembly (`ofSeamAndUniformBlocksSimple`)**: assembles a seam block plus `B` uniform blocks into a `GloballySimpleHeffterArray` directly from per-block simplicity proofs. -/ namespace GloballySimpleCore open Finset /-! ## 1. Integer Heffter Arrays -/ /-- An integer Heffter array `H(m, n; s, k)`. -/ structure IntegerHeffterArray (m n s k : ℕ) where entry : Fin m → Fin n → Option ℤ row_card : ∀ i : Fin m, (Finset.univ.filter (fun j : Fin n => (entry i j).isSome)).card = s col_card : ∀ j : Fin n, (Finset.univ.filter (fun i : Fin m => (entry i j).isSome)).card = k row_sum : ∀ i : Fin m, ∑ j : Fin n, (entry i j).getD 0 = 0 col_sum : ∀ j : Fin n, ∑ i : Fin m, (entry i j).getD 0 = 0 nonzero : ∀ i j x, entry i j = some x → x ≠ 0 mag_le : ∀ i j x, entry i j = some x → x.natAbs ≤ n * k mag_inj : ∀ i₁ j₁ i₂ j₂ x₁ x₂, entry i₁ j₁ = some x₁ → entry i₂ j₂ = some x₂ → x₁.natAbs = x₂.natAbs → i₁ = i₂ ∧ j₁ = j₂ lemma IntegerHeffterArray.mul_eq {m n s k : ℕ} (H : IntegerHeffterArray m n s k) : m * s = n * k := by have h1 : (Finset.univ.filter (fun p : Fin m × Fin n => (H.entry p.1 p.2).isSome)).card = m * s := by rw [show (Finset.univ.filter (fun p : Fin m × Fin n => (H.entry p.1 p.2).isSome)) = (Finset.univ.sigma (fun i : Fin m => Finset.univ.filter (fun j : Fin n => (H.entry i j).isSome))).map ⟨fun ⟨i, j⟩ => (i, j), fun ⟨_, _⟩ ⟨_, _⟩ h => by cases h; rfl⟩ by ext ⟨i, j⟩; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_map, Finset.mem_sigma] exact ⟨fun h => ⟨⟨i, j⟩, h, rfl⟩, fun ⟨⟨_, _⟩, h, heq⟩ => by cases heq; exact h⟩] rw [Finset.card_map, Finset.card_sigma] simp [H.row_card] have h2 : (Finset.univ.filter (fun p : Fin m × Fin n => (H.entry p.1 p.2).isSome)).card = n * k := by rw [show (Finset.univ.filter (fun p : Fin m × Fin n => (H.entry p.1 p.2).isSome)) = (Finset.univ.sigma (fun j : Fin n => Finset.univ.filter (fun i : Fin m => (H.entry i j).isSome))).map ⟨fun ⟨j, i⟩ => (i, j), fun ⟨_, _⟩ ⟨_, _⟩ h => by cases h; rfl⟩ by ext ⟨i, j⟩; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_map, Finset.mem_sigma] exact ⟨fun h => ⟨⟨j, i⟩, h, rfl⟩, fun ⟨⟨_, _⟩, h, heq⟩ => by cases heq; exact h⟩] rw [Finset.card_map, Finset.card_sigma] simp [H.col_card] omega /-- Every integer in `{1, …, n * k}` appears as `|x|` for a unique filled cell of `H`. -/ lemma IntegerHeffterArray.mag_surj {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (d : ℕ) (hd1 : 1 ≤ d) (hd2 : d ≤ n * k) : ∃ i : Fin m, ∃ j : Fin n, ∃ x : ℤ, H.entry i j = some x ∧ x.natAbs = d := by let S := Finset.univ.filter (fun p : Fin m × Fin n => (H.entry p.1 p.2).isSome) have hScard : S.card = n * k := by rw [show S = (Finset.univ.sigma (fun j : Fin n => Finset.univ.filter (fun i : Fin m => (H.entry i j).isSome))).map ⟨fun ⟨j, i⟩ => (i, j), fun ⟨_, _⟩ ⟨_, _⟩ h => by cases h; rfl⟩ by ext ⟨i, j⟩; simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_map, Finset.mem_sigma] exact ⟨fun h => ⟨⟨j, i⟩, h, rfl⟩, fun ⟨⟨_, _⟩, h, heq⟩ => by cases heq; exact h⟩] rw [Finset.card_map, Finset.card_sigma] simp [H.col_card] let f : Fin m × Fin n → ℕ := fun p => ((H.entry p.1 p.2).getD 0).natAbs have hsub : S.image f ⊆ Finset.Icc 1 (n * k) := by intro y hy rcases Finset.mem_image.mp hy with ⟨⟨i, j⟩, hp, rfl⟩ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hp obtain ⟨x, hx⟩ := Option.isSome_iff_exists.mp hp simp only [f, hx, Option.getD_some, Finset.mem_Icc] have hnz := H.nonzero i j x hx exact ⟨Nat.pos_of_ne_zero (Int.natAbs_ne_zero.mpr hnz), H.mag_le i j x hx⟩ have hinj : Set.InjOn f S := by intro ⟨i₁, j₁⟩ hp₁ ⟨i₂, j₂⟩ hp₂ heq simp only [S, Finset.mem_coe, Finset.mem_filter, Finset.mem_univ, true_and] at hp₁ hp₂ obtain ⟨x₁, hx₁⟩ := Option.isSome_iff_exists.mp hp₁ obtain ⟨x₂, hx₂⟩ := Option.isSome_iff_exists.mp hp₂ simp only [f, hx₁, hx₂, Option.getD_some] at heq rcases H.mag_inj i₁ j₁ i₂ j₂ x₁ x₂ hx₁ hx₂ heq with ⟨rfl, rfl⟩ rfl have hEq : S.image f = Finset.Icc 1 (n * k) := by apply Finset.eq_of_subset_of_card_le hsub rw [Finset.card_image_of_injOn hinj, hScard, Nat.card_Icc] omega have hd_mem : d ∈ S.image f := by rw [hEq]; exact Finset.mem_Icc.mpr ⟨hd1, hd2⟩ rcases Finset.mem_image.mp hd_mem with ⟨⟨i, j⟩, hp, hfd⟩ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hp obtain ⟨x, hx⟩ := Option.isSome_iff_exists.mp hp refine ⟨i, j, x, hx, ?_⟩ simpa [f, hx] using hfd /-- Transpose of an integer Heffter array: `H(m, n; s, k)` yields `H(n, m; k, s)`. -/ def IntegerHeffterArray.transpose {m n s k : ℕ} (H : IntegerHeffterArray m n s k) : IntegerHeffterArray n m k s where entry j i := H.entry i j row_card j := H.col_card j col_card i := H.row_card i row_sum j := H.col_sum j col_sum i := H.row_sum i nonzero j i x h := H.nonzero i j x h mag_le j i x h := by rw [H.mul_eq]; exact H.mag_le i j x h mag_inj j₁ i₁ j₂ i₂ x₁ x₂ h₁ h₂ heq := by have := H.mag_inj i₁ j₁ i₂ j₂ x₁ x₂ h₁ h₂ heq exact ⟨this.2, this.1⟩ /-- Dimension cast for integer Heffter arrays. -/ def IntegerHeffterArray.cast {m₁ n₁ m₂ n₂ s k : ℕ} (hm : m₁ = m₂) (hn : n₁ = n₂) (H : IntegerHeffterArray m₁ n₁ s k) : IntegerHeffterArray m₂ n₂ s k := by subst hm; subst hn; exact H /-! ## 2. Filled Row/Column Sequences & Global Simplicity -/ /-- The natural left-to-right list of filled entries in row `i`. -/ def rowFilled {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (i : Fin m) : List ℤ := (List.finRange n).filterMap (fun j => H.entry i j) /-- The natural top-to-bottom list of filled entries in column `j`. -/ def colFilled {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (j : Fin n) : List ℤ := (List.finRange m).filterMap (fun i => H.entry i j) lemma length_filterMap_finRange {n : ℕ} {α : Type*} (f : Fin n → Option α) : ((List.finRange n).filterMap f).length = (Finset.univ.filter (fun j : Fin n => (f j).isSome)).card := by have hnodup : (List.finRange n).Nodup := List.nodup_finRange n have h1 : ((List.finRange n).filterMap f).length = ((List.finRange n).filter (fun j => (f j).isSome)).length := by clear hnodup induction (List.finRange n) with | nil => rfl | cons a l ih => simp only [List.filterMap_cons, List.filter_cons] cases hf : f a with | none => simp [ih] | some x => simp [ih] rw [h1] have h2 : ((List.finRange n).filter (fun j => (f j).isSome)).toFinset = Finset.univ.filter (fun j : Fin n => (f j).isSome) := by ext j; simp rw [← h2, List.toFinset_card_of_nodup (hnodup.filter _)] lemma sum_filterMap_finRange {n : ℕ} (f : Fin n → Option ℤ) : ((List.finRange n).filterMap f).sum = ∑ j : Fin n, (f j).getD 0 := by have hsum : ∀ l : List (Fin n), (l.filterMap f).sum = (l.map (fun j => (f j).getD 0)).sum := by intro l induction l with | nil => rfl | cons a t ih => simp only [List.filterMap_cons, List.map_cons, List.sum_cons] cases hf : f a with | none => simp [ih] | some x => simp [ih] rw [hsum, ← Fin.sum_univ_def] lemma rowFilled_length {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (i : Fin m) : (rowFilled H i).length = s := by rw [rowFilled, length_filterMap_finRange, H.row_card i] lemma colFilled_length {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (j : Fin n) : (colFilled H j).length = k := by rw [colFilled, length_filterMap_finRange, H.col_card j] lemma rowFilled_sum {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (i : Fin m) : (rowFilled H i).sum = 0 := by rw [rowFilled, sum_filterMap_finRange, H.row_sum i] lemma colFilled_sum {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (j : Fin n) : (colFilled H j).sum = 0 := by rw [colFilled, sum_filterMap_finRange, H.col_sum j] lemma rowFilled_mem_bounds {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (i : Fin m) {x : ℤ} (hx : x ∈ rowFilled H i) : 1 ≤ x.natAbs ∧ x.natAbs ≤ n * k := by simp only [rowFilled, List.mem_filterMap, List.mem_finRange, true_and] at hx rcases hx with ⟨j, hj⟩ exact ⟨Nat.pos_of_ne_zero (Int.natAbs_ne_zero.mpr (H.nonzero i j x hj)), H.mag_le i j x hj⟩ lemma colFilled_mem_bounds {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (j : Fin n) {x : ℤ} (hx : x ∈ colFilled H j) : 1 ≤ x.natAbs ∧ x.natAbs ≤ n * k := by simp only [colFilled, List.mem_filterMap, List.mem_finRange, true_and] at hx rcases hx with ⟨i, hi⟩ exact ⟨Nat.pos_of_ne_zero (Int.natAbs_ne_zero.mpr (H.nonzero i j x hi)), H.mag_le i j x hi⟩ lemma nodup_map_natAbs_filterMap {α : Type*} {l : List α} (hl : l.Nodup) (f : α → Option ℤ) (hinj : ∀ a₁ a₂ x₁ x₂, f a₁ = some x₁ → f a₂ = some x₂ → x₁.natAbs = x₂.natAbs → a₁ = a₂) : ((l.filterMap f).map Int.natAbs).Nodup := by induction l with | nil => simp | cons a t ih => rw [List.nodup_cons] at hl simp only [List.filterMap_cons] cases hf : f a with | none => exact ih hl.2 | some x => simp only [List.map_cons, List.nodup_cons] refine ⟨?_, ih hl.2⟩ intro hmem simp only [List.mem_map, List.mem_filterMap] at hmem rcases hmem with ⟨y, ⟨b, hb, hfb⟩, hy⟩ have hab : a = b := hinj a b x y hf hfb hy.symm exact hl.1 (hab ▸ hb) lemma rowFilled_mag_nodup {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (i : Fin m) : ((rowFilled H i).map Int.natAbs).Nodup := nodup_map_natAbs_filterMap (List.nodup_finRange n) (fun j => H.entry i j) (fun _ _ _ _ h₁ h₂ heq => (H.mag_inj i _ i _ _ _ h₁ h₂ heq).2) lemma colFilled_mag_nodup {m n s k : ℕ} (H : IntegerHeffterArray m n s k) (j : Fin n) : ((colFilled H j).map Int.natAbs).Nodup := nodup_map_natAbs_filterMap (List.nodup_finRange m) (fun i => H.entry i j) (fun _ _ _ _ h₁ h₂ heq => (H.mag_inj _ j _ j _ _ h₁ h₂ heq).1) /-- Nonempty prefix sums `P₁, P₂, …, P_L` of `L : List ℤ` reduced modulo `v`. -/ def partialSumsZMod (v : ℕ) (L : List ℤ) : List (ZMod v) := List.map (fun (x : ℤ) => (x : ZMod v)) (L.scanl (· + ·) (0 : ℤ)).tail /-- A list `L : List ℤ` is **simple modulo `v`** if its `L.length` nonempty prefix sums `P₁, …, P_L` are pairwise distinct in `ZMod v`. -/ def IsSimpleList (v : ℕ) (L : List ℤ) : Prop := (partialSumsZMod v L).Nodup /-- A **globally simple integer Heffter array** `SH(m, n; s, k)`: every row (left-to-right) and every column (top-to-bottom) has pairwise distinct partial sums in `ZMod (2 * (n * k) + 1)`. -/ structure GloballySimpleHeffterArray (m n s k : ℕ) extends IntegerHeffterArray m n s k where row_simple : ∀ i : Fin m, IsSimpleList (2 * (n * k) + 1) (rowFilled toIntegerHeffterArray i) col_simple : ∀ j : Fin n, IsSimpleList (2 * (n * k) + 1) (colFilled toIntegerHeffterArray j) /-- Transpose preserves global simplicity (`SH(m, n; s, k)` yields `SH(n, m; k, s)`). -/ def GloballySimpleHeffterArray.transpose {m n s k : ℕ} (H : GloballySimpleHeffterArray m n s k) : GloballySimpleHeffterArray n m k s where toIntegerHeffterArray := H.toIntegerHeffterArray.transpose row_simple j := by rw [H.mul_eq] exact H.col_simple j col_simple i := by rw [H.mul_eq] exact H.row_simple i /-- Dimension cast for globally simple integer Heffter arrays. -/ def GloballySimpleHeffterArray.cast {m₁ n₁ m₂ n₂ s k : ℕ} (hm : m₁ = m₂) (hn : n₁ = n₂) (H : GloballySimpleHeffterArray m₁ n₁ s k) : GloballySimpleHeffterArray m₂ n₂ s k := by subst hm; subst hn; exact H /-! ## 3. Prefix Sums, Sub-Sum Characterization & Automatic Simplicity for `L ≤ 5` -/ lemma partialSumsZMod_length (v : ℕ) (L : List ℤ) : (partialSumsZMod v L).length = L.length := by simp [partialSumsZMod] lemma scanl_add_getElem (L : List ℤ) (a : ℤ) (i : ℕ) (hi : i ≤ L.length) : (L.scanl (· + ·) a)[i]'(by simp; omega) = a + (L.take i).sum := by induction L generalizing a i with | nil => have hi0 : i = 0 := by simpa using hi subst hi0; simp | cons x t ih => cases i with | zero => simp | succ i' => simp only [List.scanl_cons, List.getElem_cons_succ, List.take_succ_cons, List.sum_cons] rw [ih (a + x) i' (by simpa using hi)] ring lemma partialSumsZMod_getElem (v : ℕ) (L : List ℤ) (i : ℕ) (hi : i < L.length) : (partialSumsZMod v L)[i]'(by rw [partialSumsZMod_length]; exact hi) = (((L.take (i + 1)).sum : ℤ) : ZMod v) := by have h1 : (List.map (fun (x : ℤ) => (x : ZMod v)) (L.scanl (· + ·) (0 : ℤ)).tail)[i]'(by simp; omega) = (((L.scanl (· + ·) (0 : ℤ)).tail[i]'(by simp; omega) : ℤ) : ZMod v) := List.getElem_map (f := fun (x : ℤ) => (x : ZMod v)) exact h1.trans (by rw [List.getElem_tail, scanl_add_getElem L 0 (i + 1) hi, zero_add]) lemma sum_take_sub (L : List ℤ) {a b : ℕ} (hab : a ≤ b) : (L.take b).sum - (L.take a).sum = ((L.drop a).take (b - a)).sum := by have hsplit : L.take b = L.take a ++ (L.drop a).take (b - a) := by rw [← List.take_add, Nat.add_sub_cancel' hab] rw [hsplit, List.sum_append] ring /-- **Sub-Sum Characterization**: A list `L : List ℤ` with `L.sum = 0` is simple modulo `v` if and only if every proper nonempty contiguous sub-block `(L.drop a).take (b - a)` (`0 ≤ a < b ≤ L.length`, `(a, b) ≠ (0, L.length)`) has non-zero sum in `ZMod v`. -/ theorem isSimpleList_iff_subsum_ne_zero (v : ℕ) (L : List ℤ) (hsum : L.sum = 0) : IsSimpleList v L ↔ ∀ a b : ℕ, a < b → b ≤ L.length → ¬ (a = 0 ∧ b = L.length) → ((((L.drop a).take (b - a)).sum : ℤ) : ZMod v) ≠ 0 := by rw [IsSimpleList, List.nodup_iff_injective_getElem] constructor · intro hinj a b hab hb hne hzero cases a with | zero => have hbLt : b < L.length := by omega have hlen : (partialSumsZMod v L).length = L.length := partialSumsZMod_length v L have heq : (partialSumsZMod v L)[b - 1]'(by omega) = (partialSumsZMod v L)[L.length - 1]'(by omega) := by rw [partialSumsZMod_getElem v L (b - 1) (by omega), partialSumsZMod_getElem v L (L.length - 1) (by omega)] rw [Nat.sub_add_cancel (by omega : 1 ≤ b), Nat.sub_add_cancel (by omega : 1 ≤ L.length)] rw [List.take_length, hsum, Int.cast_zero] simpa using hzero have hidx := congrArg Fin.val (hinj (a₁ := ⟨b - 1, by omega⟩) (a₂ := ⟨L.length - 1, by omega⟩) heq) simp at hidx; omega | succ a' => have hlen : (partialSumsZMod v L).length = L.length := partialSumsZMod_length v L have heq : (partialSumsZMod v L)[a']'(by omega) = (partialSumsZMod v L)[b - 1]'(by omega) := by rw [partialSumsZMod_getElem v L a' (by omega), partialSumsZMod_getElem v L (b - 1) (by omega)] rw [Nat.sub_add_cancel (by omega : 1 ≤ b)] have hsub := sum_take_sub L (a := a' + 1) (b := b) (by omega) have hcast : (((L.take b).sum : ℤ) : ZMod v) - (((L.take (a' + 1)).sum : ℤ) : ZMod v) = 0 := by rw [← Int.cast_sub, hsub]; exact hzero exact (sub_eq_zero.mp hcast).symm have hidx := congrArg Fin.val (hinj (a₁ := ⟨a', by omega⟩) (a₂ := ⟨b - 1, by omega⟩) heq) simp at hidx; omega · intro hsub ⟨i, hi⟩ ⟨j, hj⟩ heq have hlen : (partialSumsZMod v L).length = L.length := partialSumsZMod_length v L have hiL : i < L.length := by omega have hjL : j < L.length := by omega ext; dsimp only by_contra hne wlog hlt : i < j generalizing i j · exact this j hj i hi heq.symm hjL hiL (Ne.symm hne) (by omega) dsimp only at heq rw [partialSumsZMod_getElem v L i hiL, partialSumsZMod_getElem v L j hjL] at heq have hdiff : ((((L.drop (i + 1)).take ((j + 1) - (i + 1))).sum : ℤ) : ZMod v) = 0 := by rw [← sum_take_sub L (by omega : i + 1 ≤ j + 1), Int.cast_sub, heq, sub_self] exact hsub (i + 1) (j + 1) (by omega) (by omega) (by omega) hdiff lemma int_cast_zmod_ne_zero_of_abs_lt {v : ℕ} {x : ℤ} (hx0 : x ≠ 0) (hxlt : x.natAbs < v) : (x : ZMod v) ≠ 0 := by intro h have hdvd : (v : ℤ) ∣ x := (ZMod.intCast_zmod_eq_zero_iff_dvd x v).mp h have hdvdNat : v ∣ x.natAbs := Int.natCast_dvd.mp hdvd have hle : v ≤ x.natAbs := Nat.le_of_dvd (Nat.pos_of_ne_zero (Int.natAbs_ne_zero.mpr hx0)) hdvdNat omega lemma int_cast_zmod_ne_zero_of_abs_lt_two_mul {v : ℕ} {x : ℤ} (hx0 : x ≠ 0) (hxp : x ≠ (v : ℤ)) (hxm : x ≠ -(v : ℤ)) (hxlt : x.natAbs < 2 * v) : (x : ZMod v) ≠ 0 := by intro h have hdvd : (v : ℤ) ∣ x := (ZMod.intCast_zmod_eq_zero_iff_dvd x v).mp h rcases hdvd with ⟨q, hq⟩ have hqabs : x.natAbs = v * q.natAbs := by rw [hq, Int.natAbs_mul, Int.natAbs_natCast] have hqlt : q.natAbs < 2 := by nlinarith have hqnat : q.natAbs = 0 ∨ q.natAbs = 1 := by omega rcases hqnat with h0 | h1 · have : q = 0 := Int.natAbs_eq_zero.mp h0 subst this; exact hx0 (by omega) · rcases Int.natAbs_eq q with hp | hn · rw [h1, Nat.cast_one] at hp; rw [hp, mul_one] at hq; exact hxp hq · rw [h1, Nat.cast_one] at hn; rw [hn, mul_neg_one] at hq; exact hxm hq lemma sum_two_ne_zero_mod {N : ℕ} {a b : ℤ} (_ha1 : 1 ≤ a.natAbs) (ha2 : a.natAbs ≤ N) (_hb1 : 1 ≤ b.natAbs) (hb2 : b.natAbs ≤ N) (hdiff : a.natAbs ≠ b.natAbs) : ((a + b : ℤ) : ZMod (2 * N + 1)) ≠ 0 := by apply int_cast_zmod_ne_zero_of_abs_lt · intro h0; apply hdiff; have : a = -b := by omega rw [this, Int.natAbs_neg] · have : (a + b).natAbs ≤ a.natAbs + b.natAbs := Int.natAbs_add_le a b omega lemma sum_one_ne_zero_mod {N : ℕ} {a : ℤ} (ha1 : 1 ≤ a.natAbs) (ha2 : a.natAbs ≤ N) : (a : ZMod (2 * N + 1)) ≠ 0 := int_cast_zmod_ne_zero_of_abs_lt (by omega) (by omega) lemma sum_three_ne_zero_mod {N : ℕ} {a b c : ℤ} (ha2 : a.natAbs ≤ N) (hb2 : b.natAbs ≤ N) (hc2 : c.natAbs ≤ N) (h0 : a + b + c ≠ 0) (hp : a + b + c ≠ (2 * N + 1 : ℤ)) (hm : a + b + c ≠ -(2 * N + 1 : ℤ)) : ((a + b + c : ℤ) : ZMod (2 * N + 1)) ≠ 0 := by apply int_cast_zmod_ne_zero_of_abs_lt_two_mul h0 · push_cast; exact hp · push_cast; exact hm · have h1 : (a + b + c).natAbs ≤ (a + b).natAbs + c.natAbs := Int.natAbs_add_le (a + b) c have h2 : (a + b).natAbs ≤ a.natAbs + b.natAbs := Int.natAbs_add_le a b omega /-- **Costa–Morini–Pasotti–Pellegrini (2018) Proposition 2.1**: Every zero-sum list of length `≤ 5` whose entries have pairwise distinct magnitudes in `{1, …, N}` is automatically simple modulo `2 * N + 1`. -/ theorem isSimpleList_of_length_le_five {N : ℕ} (L : List ℤ) (hlen : L.length ≤ 5) (hsum : L.sum = 0) (hbnd : ∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ N) (hnodup : (L.map Int.natAbs).Nodup) : IsSimpleList (2 * N + 1) L := by rw [isSimpleList_iff_subsum_ne_zero (2 * N + 1) L hsum] intro a b hab hb hne -- Sublist S = (L.drop a).take (b - a), complement has length L.length - (b - a) -- Either (b - a) ≤ 2 or L.length - (b - a) ≤ 2! have hcomp : ((L.drop a).take (b - a)).sum = -( (L.take a).sum + (L.drop b).sum ) := by have hL : L = L.take a ++ (L.drop a).take (b - a) ++ L.drop b := by have h1 : L.take b = L.take a ++ (L.drop a).take (b - a) := by rw [← List.take_add, Nat.add_sub_cancel' (by omega : a ≤ b)] rw [← h1, List.take_append_drop] have hs := congrArg List.sum hL rw [hsum, List.sum_append, List.sum_append] at hs omega -- Helper: any 1-element or 2-element sublist with distinct positions has non-zero sum mod 2N+1 have hlen12 : ∀ (M : List ℤ), (M.length = 1 ∨ M.length = 2) → (∀ x ∈ M, 1 ≤ x.natAbs ∧ x.natAbs ≤ N) → (M.map Int.natAbs).Nodup → ((M.sum : ℤ) : ZMod (2 * N + 1)) ≠ 0 := by intro M hMl hMb hMn rcases hMl with h1 | h2 · rcases List.length_eq_one_iff.mp h1 with ⟨x, rfl⟩ simp only [List.sum_cons, List.sum_nil, add_zero] have hx := hMb x (by simp) exact sum_one_ne_zero_mod hx.1 hx.2 · rcases List.length_eq_two.mp h2 with ⟨x, y, rfl⟩ simp only [List.sum_cons, List.sum_nil, add_zero] have hx := hMb x (by simp) have hy := hMb y (by simp) have hxy : x.natAbs ≠ y.natAbs := by simp only [List.map_cons, List.map_nil, List.nodup_cons, List.mem_cons, List.not_mem_nil, or_false] at hMn exact hMn.1 exact sum_two_ne_zero_mod hx.1 hx.2 hy.1 hy.2 hxy by_cases hsmall : b - a ≤ 2 · apply hlen12 ((L.drop a).take (b - a)) · simp only [List.length_take, List.length_drop]; omega · intro x hx exact hbnd x (List.mem_of_mem_drop (List.mem_of_mem_take hx)) · have hsub : ((L.drop a).take (b - a)).Sublist L := (List.take_sublist (b - a) (L.drop a)).trans (List.drop_sublist a L) exact hnodup.sublist (hsub.map Int.natAbs) · -- Complement M = L.take a ++ L.drop b has length 1 or 2! let M := L.take a ++ L.drop b have hMsum : ((L.drop a).take (b - a)).sum = -M.sum := by rw [hcomp, List.sum_append] rw [hMsum, Int.cast_neg, neg_ne_zero] apply hlen12 M · simp only [M, List.length_append, List.length_take, List.length_drop]; omega · intro x hx simp only [M, List.mem_append] at hx rcases hx with hx1 | hx2 · exact hbnd x (List.mem_of_mem_take hx1) · exact hbnd x (List.mem_of_mem_drop hx2) · have hMsub : M.Sublist L := by have hL : L = L.take a ++ (L.drop a).take (b - a) ++ L.drop b := by have h1 : L.take b = L.take a ++ (L.drop a).take (b - a) := by rw [← List.take_add, Nat.add_sub_cancel' (by omega : a ≤ b)] rw [← h1, List.take_append_drop] conv_rhs => rw [hL] rw [List.append_assoc] exact List.Sublist.append_left (List.sublist_append_right _ _) _ exact hnodup.sublist (hMsub.map Int.natAbs) /-- Every integer Heffter array `H(m, n; s, k)` with `s ≤ 5` and `k ≤ 5` is automatically globally simple. -/ def globallySimple_of_le_five {m n s k : ℕ} (hs : s ≤ 5) (hk : k ≤ 5) (H : IntegerHeffterArray m n s k) : GloballySimpleHeffterArray m n s k where toIntegerHeffterArray := H row_simple i := isSimpleList_of_length_le_five (rowFilled H i) (by rw [rowFilled_length]; exact hs) (rowFilled_sum H i) (fun _ hx => rowFilled_mem_bounds H i hx) (rowFilled_mag_nodup H i) col_simple j := isSimpleList_of_length_le_five (colFilled H j) (by rw [colFilled_length]; exact hk) (colFilled_sum H j) (fun _ hx => colFilled_mem_bounds H j hx) (colFilled_mag_nodup H j) /-! ## 4. Exact Length-6 and Length-7 Simplicity Reductions -/ /-- **Length-6 Reduction Theorem**: A zero-sum 6-element list `[x₀, x₁, x₂, x₃, x₄, x₅]` with pairwise distinct magnitudes in `{1, …, N}` is simple modulo `2 * N + 1` if its 3 contiguous length-3 sub-sums avoid `{0, ±(2N + 1)}`. -/ theorem isSimpleList_of_len_six {N : ℕ} (x₀ x₁ x₂ x₃ x₄ x₅ : ℤ) (hsum : x₀ + x₁ + x₂ + x₃ + x₄ + x₅ = 0) (hbnd : ∀ x ∈ ([x₀, x₁, x₂, x₃, x₄, x₅] : List ℤ), 1 ≤ x.natAbs ∧ x.natAbs ≤ N) (hnodup : (([x₀, x₁, x₂, x₃, x₄, x₅] : List ℤ).map Int.natAbs).Nodup) (h012 : x₀ + x₁ + x₂ ≠ 0 ∧ x₀ + x₁ + x₂ ≠ (2 * N + 1 : ℤ) ∧ x₀ + x₁ + x₂ ≠ -(2 * N + 1 : ℤ)) (h123 : x₁ + x₂ + x₃ ≠ 0 ∧ x₁ + x₂ + x₃ ≠ (2 * N + 1 : ℤ) ∧ x₁ + x₂ + x₃ ≠ -(2 * N + 1 : ℤ)) (h234 : x₂ + x₃ + x₄ ≠ 0 ∧ x₂ + x₃ + x₄ ≠ (2 * N + 1 : ℤ) ∧ x₂ + x₃ + x₄ ≠ -(2 * N + 1 : ℤ)) : IsSimpleList (2 * N + 1) [x₀, x₁, x₂, x₃, x₄, x₅] := by have hLsum : ([x₀, x₁, x₂, x₃, x₄, x₅] : List ℤ).sum = 0 := by simp; omega rw [isSimpleList_iff_subsum_ne_zero (2 * N + 1) _ hLsum] have b0 := hbnd x₀ (by simp); have b1 := hbnd x₁ (by simp); have b2 := hbnd x₂ (by simp) have b3 := hbnd x₃ (by simp); have b4 := hbnd x₄ (by simp); have b5 := hbnd x₅ (by simp) simp only [List.map_cons, List.map_nil, List.nodup_cons, List.mem_cons, List.not_mem_nil, or_false, not_or] at hnodup rcases hnodup with ⟨⟨n01, n02, n03, n04, n05⟩, ⟨n12, n13, n14, n15⟩, ⟨n23, n24, n25⟩, ⟨n34, n35⟩, n45, _⟩ intro a b hab hb hne simp only [List.length_cons, List.length_nil] at hb hne -- Enumerate all (a, b) with 0 ≤ a < b ≤ 6 and ¬(a = 0 ∧ b = 6) have ha_lt : a < 6 := by omega have hb_le : b ≤ 6 := hb interval_cases a <;> interval_cases b all_goals simp only [List.drop_succ_cons, List.drop_zero, List.take_succ_cons, List.take_zero, List.sum_cons, List.sum_nil, add_zero] · exact sum_one_ne_zero_mod b0.1 b0.2 · exact sum_two_ne_zero_mod b0.1 b0.2 b1.1 b1.2 n01 · rw [show x₀ + (x₁ + x₂) = x₀ + x₁ + x₂ by ring] exact sum_three_ne_zero_mod b0.2 b1.2 b2.2 h012.1 h012.2.1 h012.2.2 · rw [show x₀ + (x₁ + (x₂ + x₃)) = -(x₄ + x₅) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b4.1 b4.2 b5.1 b5.2 n45 · rw [show x₀ + (x₁ + (x₂ + (x₃ + x₄))) = -x₅ by omega, Int.cast_neg, neg_ne_zero] exact sum_one_ne_zero_mod b5.1 b5.2 · exact absurd ⟨rfl, rfl⟩ hne · exact sum_one_ne_zero_mod b1.1 b1.2 · exact sum_two_ne_zero_mod b1.1 b1.2 b2.1 b2.2 n12 · rw [show x₁ + (x₂ + x₃) = x₁ + x₂ + x₃ by ring] exact sum_three_ne_zero_mod b1.2 b2.2 b3.2 h123.1 h123.2.1 h123.2.2 · rw [show x₁ + (x₂ + (x₃ + x₄)) = -(x₀ + x₅) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b0.1 b0.2 b5.1 b5.2 n05 · rw [show x₁ + (x₂ + (x₃ + (x₄ + x₅))) = -x₀ by omega, Int.cast_neg, neg_ne_zero] exact sum_one_ne_zero_mod b0.1 b0.2 · exact sum_one_ne_zero_mod b2.1 b2.2 · exact sum_two_ne_zero_mod b2.1 b2.2 b3.1 b3.2 n23 · rw [show x₂ + (x₃ + x₄) = x₂ + x₃ + x₄ by ring] exact sum_three_ne_zero_mod b2.2 b3.2 b4.2 h234.1 h234.2.1 h234.2.2 · rw [show x₂ + (x₃ + (x₄ + x₅)) = -(x₀ + x₁) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b0.1 b0.2 b1.1 b1.2 n01 · exact sum_one_ne_zero_mod b3.1 b3.2 · exact sum_two_ne_zero_mod b3.1 b3.2 b4.1 b4.2 n34 · rw [show x₃ + (x₄ + x₅) = -(x₀ + x₁ + x₂) by omega, Int.cast_neg, neg_ne_zero] exact sum_three_ne_zero_mod b0.2 b1.2 b2.2 h012.1 h012.2.1 h012.2.2 · exact sum_one_ne_zero_mod b4.1 b4.2 · exact sum_two_ne_zero_mod b4.1 b4.2 b5.1 b5.2 n45 · exact sum_one_ne_zero_mod b5.1 b5.2 /-- **Length-7 Reduction Theorem**: A zero-sum 7-element list `[x₀, …, x₆]` with pairwise distinct magnitudes in `{1, …, N}` is simple modulo `2 * N + 1` if its 7 non-trivial length-3 sub-sums (`[0,1,2]`, `[1,2,3]`, `[2,3,4]`, `[3,4,5]`, `[4,5,6]`, `[0,5,6]`, `[0,1,6]`) avoid `{0, ±(2N + 1)}`. -/ theorem isSimpleList_of_len_seven {N : ℕ} (x₀ x₁ x₂ x₃ x₄ x₅ x₆ : ℤ) (hsum : x₀ + x₁ + x₂ + x₃ + x₄ + x₅ + x₆ = 0) (hbnd : ∀ x ∈ ([x₀, x₁, x₂, x₃, x₄, x₅, x₆] : List ℤ), 1 ≤ x.natAbs ∧ x.natAbs ≤ N) (hnodup : (([x₀, x₁, x₂, x₃, x₄, x₅, x₆] : List ℤ).map Int.natAbs).Nodup) (h012 : x₀ + x₁ + x₂ ≠ 0 ∧ x₀ + x₁ + x₂ ≠ (2 * N + 1 : ℤ) ∧ x₀ + x₁ + x₂ ≠ -(2 * N + 1 : ℤ)) (h123 : x₁ + x₂ + x₃ ≠ 0 ∧ x₁ + x₂ + x₃ ≠ (2 * N + 1 : ℤ) ∧ x₁ + x₂ + x₃ ≠ -(2 * N + 1 : ℤ)) (h234 : x₂ + x₃ + x₄ ≠ 0 ∧ x₂ + x₃ + x₄ ≠ (2 * N + 1 : ℤ) ∧ x₂ + x₃ + x₄ ≠ -(2 * N + 1 : ℤ)) (h345 : x₃ + x₄ + x₅ ≠ 0 ∧ x₃ + x₄ + x₅ ≠ (2 * N + 1 : ℤ) ∧ x₃ + x₄ + x₅ ≠ -(2 * N + 1 : ℤ)) (h456 : x₄ + x₅ + x₆ ≠ 0 ∧ x₄ + x₅ + x₆ ≠ (2 * N + 1 : ℤ) ∧ x₄ + x₅ + x₆ ≠ -(2 * N + 1 : ℤ)) (h056 : x₀ + x₅ + x₆ ≠ 0 ∧ x₀ + x₅ + x₆ ≠ (2 * N + 1 : ℤ) ∧ x₀ + x₅ + x₆ ≠ -(2 * N + 1 : ℤ)) (h016 : x₀ + x₁ + x₆ ≠ 0 ∧ x₀ + x₁ + x₆ ≠ (2 * N + 1 : ℤ) ∧ x₀ + x₁ + x₆ ≠ -(2 * N + 1 : ℤ)) : IsSimpleList (2 * N + 1) [x₀, x₁, x₂, x₃, x₄, x₅, x₆] := by have hLsum : ([x₀, x₁, x₂, x₃, x₄, x₅, x₆] : List ℤ).sum = 0 := by simp; omega rw [isSimpleList_iff_subsum_ne_zero (2 * N + 1) _ hLsum] have b0 := hbnd x₀ (by simp); have b1 := hbnd x₁ (by simp); have b2 := hbnd x₂ (by simp) have b3 := hbnd x₃ (by simp); have b4 := hbnd x₄ (by simp); have b5 := hbnd x₅ (by simp) have b6 := hbnd x₆ (by simp) simp only [List.map_cons, List.map_nil, List.nodup_cons, List.mem_cons, List.not_mem_nil, or_false, not_or] at hnodup rcases hnodup with ⟨⟨n01, n02, n03, n04, n05, n06⟩, ⟨n12, n13, n14, n15, n16⟩, ⟨n23, n24, n25, n26⟩, ⟨n34, n35, n36⟩, ⟨n45, n46⟩, n56, _⟩ intro a b hab hb hne simp only [List.length_cons, List.length_nil] at hb hne have ha_lt : a < 7 := by omega have hb_le : b ≤ 7 := hb interval_cases a <;> interval_cases b all_goals simp only [List.drop_succ_cons, List.drop_zero, List.take_succ_cons, List.take_zero, List.sum_cons, List.sum_nil, add_zero] · exact sum_one_ne_zero_mod b0.1 b0.2 · exact sum_two_ne_zero_mod b0.1 b0.2 b1.1 b1.2 n01 · rw [show x₀ + (x₁ + x₂) = x₀ + x₁ + x₂ by ring] exact sum_three_ne_zero_mod b0.2 b1.2 b2.2 h012.1 h012.2.1 h012.2.2 · rw [show x₀ + (x₁ + (x₂ + x₃)) = -(x₄ + x₅ + x₆) by omega, Int.cast_neg, neg_ne_zero] exact sum_three_ne_zero_mod b4.2 b5.2 b6.2 h456.1 h456.2.1 h456.2.2 · rw [show x₀ + (x₁ + (x₂ + (x₃ + x₄))) = -(x₅ + x₆) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b5.1 b5.2 b6.1 b6.2 n56 · rw [show x₀ + (x₁ + (x₂ + (x₃ + (x₄ + x₅)))) = -x₆ by omega, Int.cast_neg, neg_ne_zero] exact sum_one_ne_zero_mod b6.1 b6.2 · exact absurd ⟨rfl, rfl⟩ hne · exact sum_one_ne_zero_mod b1.1 b1.2 · exact sum_two_ne_zero_mod b1.1 b1.2 b2.1 b2.2 n12 · rw [show x₁ + (x₂ + x₃) = x₁ + x₂ + x₃ by ring] exact sum_three_ne_zero_mod b1.2 b2.2 b3.2 h123.1 h123.2.1 h123.2.2 · rw [show x₁ + (x₂ + (x₃ + x₄)) = -(x₀ + x₅ + x₆) by omega, Int.cast_neg, neg_ne_zero] exact sum_three_ne_zero_mod b0.2 b5.2 b6.2 h056.1 h056.2.1 h056.2.2 · rw [show x₁ + (x₂ + (x₃ + (x₄ + x₅))) = -(x₀ + x₆) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b0.1 b0.2 b6.1 b6.2 n06 · rw [show x₁ + (x₂ + (x₃ + (x₄ + (x₅ + x₆)))) = -x₀ by omega, Int.cast_neg, neg_ne_zero] exact sum_one_ne_zero_mod b0.1 b0.2 · exact sum_one_ne_zero_mod b2.1 b2.2 · exact sum_two_ne_zero_mod b2.1 b2.2 b3.1 b3.2 n23 · rw [show x₂ + (x₃ + x₄) = x₂ + x₃ + x₄ by ring] exact sum_three_ne_zero_mod b2.2 b3.2 b4.2 h234.1 h234.2.1 h234.2.2 · rw [show x₂ + (x₃ + (x₄ + x₅)) = -(x₀ + x₁ + x₆) by omega, Int.cast_neg, neg_ne_zero] exact sum_three_ne_zero_mod b0.2 b1.2 b6.2 h016.1 h016.2.1 h016.2.2 · rw [show x₂ + (x₃ + (x₄ + (x₅ + x₆))) = -(x₀ + x₁) by omega, Int.cast_neg, neg_ne_zero] exact sum_two_ne_zero_mod b0.1 b0.2 b1.1 b1.2 n01 · exact sum_one_ne_zero_mod b3.1 b3.2 · exact sum_two_ne_zero_mod b3.1 b3.2 b4.1 b4.2 n34 · rw [show x₃ + (x₄ + x₅) = x₃ + x₄ + x₅ by ring] exact sum_three_ne_zero_mod b3.2 b4.2 b5.2 h345.1 h345.2.1 h345.2.2 · rw [show x₃ + (x₄ + (x₅ + x₆)) = -(x₀ + x₁ + x₂) by omega, Int.cast_neg, neg_ne_zero] exact sum_three_ne_zero_mod b0.2 b1.2 b2.2 h012.1 h012.2.1 h012.2.2 · exact sum_one_ne_zero_mod b4.1 b4.2 · exact sum_two_ne_zero_mod b4.1 b4.2 b5.1 b5.2 n45 · rw [show x₄ + (x₅ + x₆) = x₄ + x₅ + x₆ by ring] exact sum_three_ne_zero_mod b4.2 b5.2 b6.2 h456.1 h456.2.1 h456.2.2 · exact sum_one_ne_zero_mod b5.1 b5.2 · exact sum_two_ne_zero_mod b5.1 b5.2 b6.1 b6.2 n56 · exact sum_one_ne_zero_mod b6.1 b6.2 /-! ## 5. Graph-Theoretic Bridge: Orthogonal Cyclic Cycle Decompositions of `K_{2nk+1}` -/ /-- A simple `L`-cycle in the complete graph on `ZMod v`, specified by `L` pairwise distinct vertices `verts : Fin L ↪ ZMod v`. -/ structure SimpleCycle (v L : ℕ) where verts : Fin L ↪ ZMod v /-- The set of `L` edges of a simple `L`-cycle (consecutive pairs modulo `L`), shifted by `g : ZMod v`. -/ def SimpleCycle.edgesShift {v L : ℕ} [NeZero L] (C : SimpleCycle v L) (g : ZMod v) : Finset (Sym2 (ZMod v)) := Finset.univ.image (fun t : Fin L => s(C.verts t + g, C.verts ⟨(t.val + 1) % L, Nat.mod_lt _ (NeZero.pos L)⟩ + g)) /-- A **cyclic `L`-cycle decomposition** of `K_v` (`v` vertices `ZMod v`) with `B` base cycles: every non-loop edge `e = s(u, w)` (`u ≠ w`) of `K_v` belongs to a **unique** translated cycle `base i + g`. -/ structure CyclicCycleDecomp (v B L : ℕ) [NeZero L] where base : Fin B → SimpleCycle v L nonloop : ∀ (i : Fin B) (g : ZMod v) (e : Sym2 (ZMod v)), e ∈ (base i).edgesShift g → ¬ e.IsDiag partition : ∀ e : Sym2 (ZMod v), ¬ e.IsDiag → ∃! p : Fin B × ZMod v, e ∈ (base p.1).edgesShift p.2 /-- Two cyclic cycle decompositions `D₁` (of `s`-cycles) and `D₂` (of `k`-cycles) of `K_v` are **orthogonal** if every cycle of `D₁` shares **at most one edge** with every cycle of `D₂`. -/ def OrthogonalCyclicDecomps {v m n s k : ℕ} [NeZero s] [NeZero k] (D₁ : CyclicCycleDecomp v m s) (D₂ : CyclicCycleDecomp v n k) : Prop := ∀ (i : Fin m) (a : ZMod v) (j : Fin n) (b : ZMod v), (((D₁.base i).edgesShift a) ∩ ((D₂.base j).edgesShift b)).card ≤ 1 /-- Construct a `SimpleCycle` from a simple zero-sum list `L` of length `ℓ`. -/ def simpleCycleOfList {v ℓ : ℕ} (L : List ℤ) (hlen : L.length = ℓ) (hsimp : IsSimpleList v L) : SimpleCycle v ℓ where verts := ⟨fun t => (partialSumsZMod v L)[t.val]'(by rw [partialSumsZMod_length, hlen]; exact t.isLt), by intro t₁ t₂ heq have hinj := List.nodup_iff_injective_getElem.mp hsimp have h := hinj (a₁ := ⟨t₁.val, by rw [partialSumsZMod_length, hlen]; exact t₁.isLt⟩) (a₂ := ⟨t₂.val, by rw [partialSumsZMod_length, hlen]; exact t₂.isLt⟩) heq exact Fin.ext (by simpa using congrArg Fin.val h)⟩ /-- Helper: consecutive step in `simpleCycleOfList` equals the next list element in `ZMod v`. -/ lemma simpleCycleOfList_step {v ℓ : ℕ} [NeZero ℓ] (L : List ℤ) (hlen : L.length = ℓ) (hsum : L.sum = 0) (hsimp : IsSimpleList v L) (t : Fin ℓ) : (simpleCycleOfList L hlen hsimp).verts ⟨(t.val + 1) % ℓ, Nat.mod_lt _ (NeZero.pos ℓ)⟩ - (simpleCycleOfList L hlen hsimp).verts t = ((L[(t.val + 1) % ℓ]'(by rw [hlen]; exact Nat.mod_lt _ (NeZero.pos ℓ)) : ℤ) : ZMod v) := by change (partialSumsZMod v L)[(t.val + 1) % ℓ]'(by rw [partialSumsZMod_length, hlen]; exact Nat.mod_lt _ (NeZero.pos ℓ)) - (partialSumsZMod v L)[t.val]'(by rw [partialSumsZMod_length, hlen]; exact t.isLt) = _ rw [partialSumsZMod_getElem v L ((t.val + 1) % ℓ) (by rw [hlen]; exact Nat.mod_lt _ (NeZero.pos ℓ)), partialSumsZMod_getElem v L t.val (by rw [hlen]; exact t.isLt)] by_cases hlast : t.val + 1 < ℓ · have hmod : (t.val + 1) % ℓ = t.val + 1 := Nat.mod_eq_of_lt hlast simp only [hmod] have htake : L.take (t.val + 1 + 1) = L.take (t.val + 1) ++ [L[t.val + 1]'(by omega)] := by rw [List.take_add_one, List.getElem?_eq_getElem (by omega : t.val + 1 < L.length)] rfl rw [htake, List.sum_append, List.sum_singleton, Int.cast_add] ring · have htEq : t.val + 1 = ℓ := by omega simp only [htEq, Nat.mod_self, zero_add] have htakeL : L.take ℓ = L := by rw [← hlen, List.take_length] rw [htakeL, hsum, Int.cast_zero, sub_zero] have h1 : L.take 1 = [L[0]'(by omega)] := by cases L with | nil => simp at hlen; exact absurd hlen.symm (NeZero.ne ℓ) | cons x xs => simp rw [h1, List.sum_singleton] /-- Every non-zero element `u : ZMod (2N + 1)` equals either `+(d : ZMod (2N + 1))` or `-(d : ZMod (2N + 1))` for a **unique** `d ∈ {1, …, N}`. -/ lemma zmod_odd_diff_unique {N : ℕ} (u : ZMod (2 * N + 1)) (hu : u ≠ 0) : ∃! d : ℕ, (1 ≤ d ∧ d ≤ N) ∧ (u = (d : ZMod (2 * N + 1)) ∨ u = -(d : ZMod (2 * N + 1))) := by have : NeZero (2 * N + 1) := ⟨by omega⟩ have hval_lt : u.val < 2 * N + 1 := ZMod.val_lt u have hval_pos : 0 < u.val := by by_contra h0 have : u.val = 0 := by omega exact hu ((ZMod.val_eq_zero u).mp this) have hcast : ((u.val : ℕ) : ZMod (2 * N + 1)) = u := ZMod.natCast_zmod_val u by_cases hle : u.val ≤ N · refine ⟨u.val, ⟨⟨hval_pos, hle⟩, Or.inl hcast.symm⟩, ?_⟩ rintro d ⟨⟨hd1, hd2⟩, h1 | h2⟩ · have := congrArg ZMod.val h1 rw [ZMod.val_natCast_of_lt (by omega : d < 2 * N + 1)] at this omega · have hsum : u + (d : ZMod (2 * N + 1)) = 0 := by rw [h2]; ring have hcast2 : (((u.val + d : ℕ) : ZMod (2 * N + 1))) = 0 := by push_cast; rw [hcast]; exact hsum have hdvd : (2 * N + 1) ∣ (u.val + d) := (ZMod.natCast_eq_zero_iff _ _).mp hcast2 have hge := Nat.le_of_dvd (by omega) hdvd omega · let d₀ := 2 * N + 1 - u.val have hd₀1 : 1 ≤ d₀ := by omega have hd₀2 : d₀ ≤ N := by omega have hneg : u = -(d₀ : ZMod (2 * N + 1)) := by have hadd : u + (d₀ : ZMod (2 * N + 1)) = 0 := by rw [← hcast, ← Nat.cast_add, show u.val + d₀ = 2 * N + 1 by omega] exact ZMod.natCast_self (2 * N + 1) exact eq_neg_of_add_eq_zero_left hadd refine ⟨d₀, ⟨⟨hd₀1, hd₀2⟩, Or.inr hneg⟩, ?_⟩ rintro d ⟨⟨hd1, hd2⟩, h1 | h2⟩ · have := congrArg ZMod.val h1 rw [ZMod.val_natCast_of_lt (by omega : d < 2 * N + 1)] at this omega · have heq : (d : ZMod (2 * N + 1)) = (d₀ : ZMod (2 * N + 1)) := neg_inj.mp (h2.symm.trans hneg) have := congrArg ZMod.val heq rw [ZMod.val_natCast_of_lt (by omega : d < 2 * N + 1), ZMod.val_natCast_of_lt (by omega : d₀ < 2 * N + 1)] at this exact this /-- For `x, y : ℤ` with `1 ≤ |x|, |y| ≤ N`, `x ≡ ±y (mod 2N+1)` iff `|x| = |y|`. -/ lemma int_abs_eq_of_zmod_eq_or_neg {N : ℕ} {x y : ℤ} (hx1 : 1 ≤ x.natAbs) (hx2 : x.natAbs ≤ N) (hy1 : 1 ≤ y.natAbs) (hy2 : y.natAbs ≤ N) (h : (x : ZMod (2 * N + 1)) = (y : ZMod (2 * N + 1)) ∨ (x : ZMod (2 * N + 1)) = -(y : ZMod (2 * N + 1))) : x.natAbs = y.natAbs := by have hx_cast : (x : ZMod (2 * N + 1)) = (x.natAbs : ZMod (2 * N + 1)) ∨ (x : ZMod (2 * N + 1)) = -(x.natAbs : ZMod (2 * N + 1)) := by rcases Int.natAbs_eq x with hpos | hneg · left; conv_lhs => rw [hpos]; simp only [Int.cast_natCast] · right; conv_lhs => rw [hneg]; simp only [Int.cast_neg, Int.cast_natCast] have hy_cast : (y : ZMod (2 * N + 1)) = (y.natAbs : ZMod (2 * N + 1)) ∨ (y : ZMod (2 * N + 1)) = -(y.natAbs : ZMod (2 * N + 1)) := by rcases Int.natAbs_eq y with hpos | hneg · left; conv_lhs => rw [hpos]; simp only [Int.cast_natCast] · right; conv_lhs => rw [hneg]; simp only [Int.cast_neg, Int.cast_natCast] have hnz : (x : ZMod (2 * N + 1)) ≠ 0 := sum_one_ne_zero_mod hx1 hx2 obtain ⟨d, _, huniq⟩ := zmod_odd_diff_unique (x : ZMod (2 * N + 1)) hnz have hxd : x.natAbs = d := huniq x.natAbs ⟨⟨hx1, hx2⟩, hx_cast⟩ have hyd : y.natAbs = d := by apply huniq y.natAbs refine ⟨⟨hy1, hy2⟩, ?_⟩ rcases h with h1 | h2 <;> rcases hy_cast with hy1' | hy2' · left; rw [h1, hy1'] · right; rw [h1, hy2'] · right; rw [h2, hy1'] · left; rw [h2, hy2', neg_neg] rw [hxd, hyd] /-- Helper: given `Sym2` equality `s(a, b) = s(c, d)` in an additive group, the differences `b - a` and `d - c` agree up to sign. -/ lemma sym2_eq_diff_or_neg {G : Type*} [AddCommGroup G] {a b c d : G} (h : s(a, b) = s(c, d)) : b - a = d - c ∨ b - a = -(d - c) := by rcases Sym2.eq_iff.mp h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ · left; rfl · right; abel /-- Helper: given `Sym2` equality `s(a + g, b + g) = s(u, w)` where `b - a ≠ -(b - a)` (i.e. odd modulus, non-zero step), `g` is uniquely determined from `s(u, w)` and `a, b`! -/ lemma sym2_shift_unique {v : ℕ} (hv : Odd v) {a b g₁ g₂ u w : ZMod v} (hstep : b - a ≠ 0) (h1 : s(a + g₁, b + g₁) = s(u, w)) (h2 : s(a + g₂, b + g₂) = s(u, w)) : g₁ = g₂ := by have heq : s(a + g₁, b + g₁) = s(a + g₂, b + g₂) := h1.trans h2.symm rcases Sym2.eq_iff.mp heq with ⟨ha, _⟩ | ⟨hab, hba⟩ · exact add_left_cancel ha · -- a + g₁ = b + g₂ and b + g₁ = a + g₂ => 2*(b - a) = 0, impossible for odd v! have h2step : (2 : ZMod v) * (b - a) = 0 := by have : (b + g₁) - (a + g₁) = (a + g₂) - (b + g₂) := by rw [hab, hba] have hsub : b - a = -(b - a) := by calc b - a = (b + g₁) - (a + g₁) := by ring _ = (a + g₂) - (b + g₂) := this _ = -(b - a) := by ring calc (2 : ZMod v) * (b - a) = (b - a) + (b - a) := by ring _ = (b - a) + -(b - a) := by nth_rw 2 [hsub] _ = 0 := add_neg_cancel _ have hcop : Nat.Coprime 2 v := Nat.coprime_two_left.mpr hv have hunit : IsUnit (2 : ZMod v) := (ZMod.isUnit_iff_coprime 2 v).mpr hcop exact absurd (hunit.mul_right_eq_zero.mp h2step) hstep /-- Build the cyclic `s`-cycle decomposition of `K_{2nk+1}` from the rows of a globally simple integer Heffter array `SH(m, n; s, k)`. -/ def GloballySimpleHeffterArray.rowCycleDecomp {m n s k : ℕ} [NeZero s] (H : GloballySimpleHeffterArray m n s k) : CyclicCycleDecomp (2 * (n * k) + 1) m s where base i := simpleCycleOfList (rowFilled H.toIntegerHeffterArray i) (rowFilled_length H.toIntegerHeffterArray i) (H.row_simple i) nonloop i g e he := by simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] at he rcases he with ⟨t, rfl⟩ rw [Sym2.mk_isDiag_iff] intro hdiag have hstep := simpleCycleOfList_step (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i) (rowFilled_length _ i) (rowFilled_sum _ i) (H.row_simple i) t have hsub0 : (simpleCycleOfList (rowFilled H.toIntegerHeffterArray i) (rowFilled_length _ i) (H.row_simple i)).verts ⟨(t.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ - (simpleCycleOfList (rowFilled H.toIntegerHeffterArray i) (rowFilled_length _ i) (H.row_simple i)).verts t = 0 := by have h' : (simpleCycleOfList _ _ _).verts t = (simpleCycleOfList _ _ _).verts ⟨(t.val + 1) % s, _⟩ := add_right_cancel hdiag rw [h', sub_self] rw [hsub0] at hstep have hmem : (rowFilled H.toIntegerHeffterArray i)[(t.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) ∈ rowFilled H.toIntegerHeffterArray i := List.getElem_mem _ have hb := rowFilled_mem_bounds H.toIntegerHeffterArray i hmem exact sum_one_ne_zero_mod (N := n * k) hb.1 hb.2 hstep.symm partition e he := by induction e using Sym2.ind with | _ u w => have huw : w - u ≠ 0 := by intro h0; exact he (Sym2.mk_isDiag_iff.mpr (sub_eq_zero.mp h0).symm) obtain ⟨d, ⟨⟨hd1, hd2⟩, hdiff⟩, _⟩ := zmod_odd_diff_unique (N := n * k) (w - u) huw obtain ⟨i₀, j₀, x₀, hx₀, hxabs⟩ := H.mag_surj d hd1 hd2 have hx_mem : x₀ ∈ rowFilled H.toIntegerHeffterArray i₀ := by simp only [rowFilled, List.mem_filterMap, List.mem_finRange, true_and] exact ⟨j₀, hx₀⟩ obtain ⟨idx, hidx_lt, hidx_eq⟩ := List.mem_iff_getElem.mp hx_mem have hlen_i₀ : (rowFilled H.toIntegerHeffterArray i₀).length = s := rowFilled_length _ i₀ -- Find t₀ : Fin s with (t₀.val + 1) % s = idx let t₀ : Fin s := ⟨(idx + s - 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ have ht₀_next : (t₀.val + 1) % s = idx := by have hidx_s : idx < s := by omega dsimp only [t₀] cases idx with | zero => have hs1 : 1 ≤ s := NeZero.pos s rw [Nat.zero_add, Nat.mod_eq_of_lt (by omega : s - 1 < s), Nat.sub_add_cancel hs1, Nat.mod_self] | succ k' => rw [show k' + 1 + s - 1 = k' + s by omega, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega : k' < s), Nat.mod_eq_of_lt hidx_s] let C₀ := simpleCycleOfList (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i₀) (rowFilled_length _ i₀) (H.row_simple i₀) have hstep₀ : C₀.verts ⟨(t₀.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ - C₀.verts t₀ = (x₀ : ZMod (2 * (n * k) + 1)) := by rw [simpleCycleOfList_step _ _ (rowFilled_sum _ i₀)] congr 1 simp only [ht₀_next] exact hidx_eq -- Choose g₀ depending on whether w - u = +x₀ or -x₀ in ZMod (2(nk)+1) have hwu_x₀ : w - u = (x₀ : ZMod (2 * (n * k) + 1)) ∨ w - u = -(x₀ : ZMod (2 * (n * k) + 1)) := by rcases Int.natAbs_eq x₀ with hpos | hneg <;> rcases hdiff with h1 | h2 · left; rw [h1, ← hxabs]; conv_rhs => rw [hpos]; simp only [Int.cast_natCast] · right; rw [h2, ← hxabs]; conv_rhs => rw [hpos]; simp only [Int.cast_natCast] · right; rw [h1, ← hxabs]; conv_rhs => rw [hneg]; simp only [Int.cast_neg, Int.cast_natCast, neg_neg] · left; rw [h2, ← hxabs]; conv_rhs => rw [hneg]; simp only [Int.cast_neg, Int.cast_natCast] have hex_g : ∃ g₀ : ZMod (2 * (n * k) + 1), s(u, w) ∈ C₀.edgesShift g₀ := by rcases hwu_x₀ with hpos | hneg · refine ⟨u - C₀.verts t₀, ?_⟩ simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] refine ⟨t₀, ?_⟩ rw [Sym2.eq_iff]; left refine ⟨by ring, ?_⟩ have : C₀.verts ⟨(t₀.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ - C₀.verts t₀ = w - u := hstep₀.trans hpos.symm calc C₀.verts ⟨(t₀.val + 1) % s, _⟩ + (u - C₀.verts t₀) = (C₀.verts ⟨(t₀.val + 1) % s, _⟩ - C₀.verts t₀) + u := by ring _ = (w - u) + u := by rw [this] _ = w := by ring · refine ⟨w - C₀.verts t₀, ?_⟩ simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] refine ⟨t₀, ?_⟩ rw [Sym2.eq_iff]; right refine ⟨by ring, ?_⟩ have : C₀.verts ⟨(t₀.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ - C₀.verts t₀ = u - w := by rw [hstep₀]; calc (x₀ : ZMod (2 * (n * k) + 1)) = -(w - u) := by rw [hneg, neg_neg] _ = u - w := by ring calc C₀.verts ⟨(t₀.val + 1) % s, _⟩ + (w - C₀.verts t₀) = (C₀.verts ⟨(t₀.val + 1) % s, _⟩ - C₀.verts t₀) + w := by ring _ = (u - w) + w := by rw [this] _ = u := by ring rcases hex_g with ⟨g₀, hg₀⟩ refine ⟨(i₀, g₀), hg₀, ?_⟩ rintro ⟨i₁, g₁⟩ hmem₁ simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] at hmem₁ rcases hmem₁ with ⟨t₁, ht₁⟩ have hdiff₁ := sym2_eq_diff_or_neg ht₁ let C₁ := simpleCycleOfList (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i₁) (rowFilled_length _ i₁) (H.row_simple i₁) have hstep₁ := simpleCycleOfList_step (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i₁) (rowFilled_length _ i₁) (rowFilled_sum _ i₁) (H.row_simple i₁) t₁ let x₁ := (rowFilled H.toIntegerHeffterArray i₁)[(t₁.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) have hstep₁' : (C₁.verts ⟨(t₁.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ + g₁) - (C₁.verts t₁ + g₁) = (x₁ : ZMod (2 * (n * k) + 1)) := by rw [add_sub_add_right_eq_sub]; exact hstep₁ rw [hstep₁'] at hdiff₁ have hx₁_mem : x₁ ∈ rowFilled H.toIntegerHeffterArray i₁ := List.getElem_mem _ have hb₁ := rowFilled_mem_bounds H.toIntegerHeffterArray i₁ hx₁_mem have hb₀ := rowFilled_mem_bounds H.toIntegerHeffterArray i₀ hx_mem have hx₁_eq_x₀ : (x₁ : ZMod (2 * (n * k) + 1)) = (x₀ : ZMod (2 * (n * k) + 1)) ∨ (x₁ : ZMod (2 * (n * k) + 1)) = -(x₀ : ZMod (2 * (n * k) + 1)) := by rcases hdiff₁ with hd1' | hd2' <;> rcases hwu_x₀ with hp | hn · left; exact hd1'.trans hp · right; exact hd1'.trans hn · right; rw [hd2', hp] · left; rw [hd2', hn, neg_neg] have habs_eq : x₁.natAbs = x₀.natAbs := int_abs_eq_of_zmod_eq_or_neg hb₁.1 hb₁.2 hb₀.1 hb₀.2 hx₁_eq_x₀ simp only [rowFilled, List.mem_filterMap, List.mem_finRange, true_and] at hx₁_mem rcases hx₁_mem with ⟨j₁, hj₁⟩ have hij : i₀ = i₁ ∧ j₀ = j₁ := H.mag_inj i₀ j₀ i₁ j₁ x₀ x₁ hx₀ hj₁ habs_eq.symm rcases hij with ⟨rfl, rfl⟩ -- Now i₁ = i₀; deduce t₁ = t₀ because row magnitudes are nodup, then g₁ = g₀! have ht_eq : t₁ = t₀ := by have hnod := rowFilled_mag_nodup H.toIntegerHeffterArray i₀ have hinj_nod := List.nodup_iff_injective_getElem.mp hnod have hidx_eq' : ((t₁.val + 1) % s) = ((t₀.val + 1) % s) := by have hget : ((rowFilled H.toIntegerHeffterArray i₀).map Int.natAbs)[(t₁.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = ((rowFilled H.toIntegerHeffterArray i₀).map Int.natAbs)[(t₀.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) := by simp only [List.getElem_map] have hx0_at : (rowFilled H.toIntegerHeffterArray i₀)[(t₀.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = x₀ := by simp only [ht₀_next]; exact hidx_eq rw [hx0_at] exact habs_eq exact congrArg Fin.val (hinj_nod (a₁ := ⟨(t₁.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) (a₂ := ⟨(t₀.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) hget) ext have ht1l := t₁.isLt; have ht0l := t₀.isLt by_cases h1 : t₁.val + 1 < s <;> by_cases h2 : t₀.val + 1 < s · rw [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at hidx_eq'; omega · have : t₀.val + 1 = s := by omega rw [Nat.mod_eq_of_lt h1, this, Nat.mod_self] at hidx_eq'; omega · have : t₁.val + 1 = s := by omega rw [this, Nat.mod_self, Nat.mod_eq_of_lt h2] at hidx_eq'; omega · omega subst ht_eq -- Now both g₁ and g₀ send edge t₀ of C₀ to s(u, w)! simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] at hg₀ rcases hg₀ with ⟨t₀', ht₀'⟩ have ht₀'_eq : t₀' = t₀ := by have hdiff₀' := sym2_eq_diff_or_neg ht₀' have hstep₀' := simpleCycleOfList_step (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i₀) (rowFilled_length _ i₀) (rowFilled_sum _ i₀) (H.row_simple i₀) t₀' let x₀' := (rowFilled H.toIntegerHeffterArray i₀)[(t₀'.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) have hstep₀'' : (C₀.verts ⟨(t₀'.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ + g₀) - (C₀.verts t₀' + g₀) = (x₀' : ZMod (2 * (n * k) + 1)) := by rw [add_sub_add_right_eq_sub]; exact hstep₀' rw [hstep₀''] at hdiff₀' have hx₀'_mem : x₀' ∈ rowFilled H.toIntegerHeffterArray i₀ := List.getElem_mem _ have hb₀' := rowFilled_mem_bounds H.toIntegerHeffterArray i₀ hx₀'_mem have hx₀'_eq : (x₀' : ZMod (2 * (n * k) + 1)) = (x₀ : ZMod (2 * (n * k) + 1)) ∨ (x₀' : ZMod (2 * (n * k) + 1)) = -(x₀ : ZMod (2 * (n * k) + 1)) := by rcases hdiff₀' with hd1' | hd2' <;> rcases hwu_x₀ with hp | hn · left; exact hd1'.trans hp · right; exact hd1'.trans hn · right; rw [hd2', hp] · left; rw [hd2', hn, neg_neg] have habs' := int_abs_eq_of_zmod_eq_or_neg hb₀'.1 hb₀'.2 hb₀.1 hb₀.2 hx₀'_eq have hinj_nod := List.nodup_iff_injective_getElem.mp (rowFilled_mag_nodup H.toIntegerHeffterArray i₀) have hidx_eq' : ((t₀'.val + 1) % s) = ((t₀.val + 1) % s) := by have hget : ((rowFilled H.toIntegerHeffterArray i₀).map Int.natAbs)[(t₀'.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = ((rowFilled H.toIntegerHeffterArray i₀).map Int.natAbs)[(t₀.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) := by simp only [List.getElem_map] have hx0_at : (rowFilled H.toIntegerHeffterArray i₀)[(t₀.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = x₀ := by simp only [ht₀_next]; exact hidx_eq rw [hx0_at]; exact habs' exact congrArg Fin.val (hinj_nod (a₁ := ⟨(t₀'.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) (a₂ := ⟨(t₀.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) hget) ext have h1l := t₀'.isLt; have h0l := t₀.isLt by_cases h1 : t₀'.val + 1 < s <;> by_cases h2 : t₀.val + 1 < s · rw [Nat.mod_eq_of_lt h1, Nat.mod_eq_of_lt h2] at hidx_eq'; omega · have : t₀.val + 1 = s := by omega rw [Nat.mod_eq_of_lt h1, this, Nat.mod_self] at hidx_eq'; omega · have : t₀'.val + 1 = s := by omega rw [this, Nat.mod_self, Nat.mod_eq_of_lt h2] at hidx_eq'; omega · omega subst ht₀'_eq have hstep_nz : C₀.verts ⟨(t₀.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ - C₀.verts t₀ ≠ 0 := by rw [hstep₀]; exact sum_one_ne_zero_mod hb₀.1 hb₀.2 have hg_eq : g₁ = g₀ := sym2_shift_unique ⟨n * k, by ring⟩ hstep_nz ht₁ ht₀' exact Prod.ext rfl hg_eq /-- Cast a `CyclicCycleDecomp` along a vertex-count equality. -/ def CyclicCycleDecomp.cast {v₁ v₂ B L : ℕ} [NeZero L] (hv : v₁ = v₂) (D : CyclicCycleDecomp v₁ B L) : CyclicCycleDecomp v₂ B L := by subst hv; exact D /-- Build the cyclic `k`-cycle decomposition of `K_{2nk+1}` from the columns of `H` via transpose. -/ def GloballySimpleHeffterArray.colCycleDecomp {m n s k : ℕ} [NeZero k] (H : GloballySimpleHeffterArray m n s k) : CyclicCycleDecomp (2 * (n * k) + 1) n k := CyclicCycleDecomp.cast (congrArg (fun x => 2 * x + 1) H.mul_eq) H.transpose.rowCycleDecomp lemma colCycleDecomp_base_eq {m n s k : ℕ} [NeZero k] (H : GloballySimpleHeffterArray m n s k) (j : Fin n) : H.colCycleDecomp.base j = simpleCycleOfList (v := 2 * (n * k) + 1) (colFilled H.toIntegerHeffterArray j) (colFilled_length _ j) (H.col_simple j) := by -- Abstract over the vertex count equality have aux : ∀ (v : ℕ) (hv : 2 * (m * s) + 1 = v) (hsimple : ∀ j : Fin n, IsSimpleList v (colFilled H.toIntegerHeffterArray j)), (CyclicCycleDecomp.cast hv H.transpose.rowCycleDecomp).base j = simpleCycleOfList (v := v) (colFilled H.toIntegerHeffterArray j) (colFilled_length _ j) (hsimple j) := by intro v hv hsimple; subst hv simp only [CyclicCycleDecomp.cast, GloballySimpleHeffterArray.rowCycleDecomp, GloballySimpleHeffterArray.transpose, IntegerHeffterArray.transpose, rowFilled, colFilled] exact aux (2 * (n * k) + 1) (congrArg (fun x => 2 * x + 1) H.mul_eq) H.col_simple /-- **Archdeacon (2015) / Costa–Morini–Pasotti–Pellegrini (2018) Theorem 1.1**: Every globally simple integer Heffter array `SH(m, n; s, k)` yields a pair of **orthogonal** cyclic `s`-cycle and `k`-cycle decompositions of the complete graph `K_{2nk+1}`. -/ theorem GloballySimpleHeffterArray.toOrthogonalCyclicCycleDecomps {m n s k : ℕ} [NeZero s] [NeZero k] (H : GloballySimpleHeffterArray m n s k) : OrthogonalCyclicDecomps H.rowCycleDecomp H.colCycleDecomp := by intro i a j b rw [Finset.card_le_one_iff] intro e₁ e₂ he₁ he₂ rw [Finset.mem_inter] at he₁ he₂ rcases he₁ with ⟨he₁R, he₁C⟩ rcases he₂ with ⟨he₂R, he₂C⟩ simp only [rowCycleDecomp, SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] at he₁R he₂R rcases he₁R with ⟨t₁, rfl⟩ rcases he₂R with ⟨t₂, rfl⟩ rw [colCycleDecomp_base_eq] at he₁C he₂C simp only [SimpleCycle.edgesShift, Finset.mem_image, Finset.mem_univ, true_and] at he₁C he₂C rcases he₁C with ⟨c₁, hc₁⟩ rcases he₂C with ⟨c₂, hc₂⟩ -- Each shared edge's unordered difference equals |x| for a cell in BOTH row i and col j! have hcell : ∀ (t : Fin s) (c : Fin k), s((simpleCycleOfList (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i) (rowFilled_length _ i) (H.row_simple i)).verts t + a, (simpleCycleOfList (v := 2 * (n * k) + 1) (rowFilled H.toIntegerHeffterArray i) (rowFilled_length _ i) (H.row_simple i)).verts ⟨(t.val + 1) % s, Nat.mod_lt _ (NeZero.pos s)⟩ + a) = s((simpleCycleOfList (v := 2 * (n * k) + 1) (colFilled H.toIntegerHeffterArray j) (colFilled_length _ j) (H.col_simple j)).verts c + b, (simpleCycleOfList (v := 2 * (n * k) + 1) (colFilled H.toIntegerHeffterArray j) (colFilled_length _ j) (H.col_simple j)).verts ⟨(c.val + 1) % k, Nat.mod_lt _ (NeZero.pos k)⟩ + b) → H.entry i j = some ((rowFilled H.toIntegerHeffterArray i)[(t.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s))) := by intro t c heq have hdiff := sym2_eq_diff_or_neg heq rw [add_sub_add_right_eq_sub, add_sub_add_right_eq_sub] at hdiff rw [simpleCycleOfList_step _ _ (rowFilled_sum _ i), simpleCycleOfList_step _ _ (colFilled_sum _ j)] at hdiff let xr := (rowFilled H.toIntegerHeffterArray i)[(t.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) let xc := (colFilled H.toIntegerHeffterArray j)[(c.val + 1) % k]'(by rw [colFilled_length]; exact Nat.mod_lt _ (NeZero.pos k)) have hxr_mem : xr ∈ rowFilled H.toIntegerHeffterArray i := List.getElem_mem _ have hxc_mem : xc ∈ colFilled H.toIntegerHeffterArray j := List.getElem_mem _ have hbr := rowFilled_mem_bounds H.toIntegerHeffterArray i hxr_mem have hbc := colFilled_mem_bounds H.toIntegerHeffterArray j hxc_mem have habs : xr.natAbs = xc.natAbs := int_abs_eq_of_zmod_eq_or_neg hbr.1 hbr.2 hbc.1 hbc.2 hdiff simp only [rowFilled, List.mem_filterMap, List.mem_finRange, true_and] at hxr_mem simp only [colFilled, List.mem_filterMap, List.mem_finRange, true_and] at hxc_mem rcases hxr_mem with ⟨j', hj'⟩ rcases hxc_mem with ⟨i', hi'⟩ rcases H.mag_inj i j' i' j xr xc hj' hi' habs with ⟨_, rfl⟩ exact hj' have h1 := hcell t₁ c₁ hc₁.symm have h2 := hcell t₂ c₂ hc₂.symm have hval_eq : (rowFilled H.toIntegerHeffterArray i)[(t₁.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = (rowFilled H.toIntegerHeffterArray i)[(t₂.val + 1) % s]'(by rw [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) := Option.some.inj (h1.symm.trans h2) -- Since row magnitudes are nodup, (t₁ + 1) % s = (t₂ + 1) % s, hence t₁ = t₂! have hinj_nod := List.nodup_iff_injective_getElem.mp (rowFilled_mag_nodup H.toIntegerHeffterArray i) have hidx_eq : (t₁.val + 1) % s = (t₂.val + 1) % s := by have hget : ((rowFilled H.toIntegerHeffterArray i).map Int.natAbs)[(t₁.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) = ((rowFilled H.toIntegerHeffterArray i).map Int.natAbs)[(t₂.val + 1) % s]'(by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)) := by simp only [List.getElem_map] exact congrArg Int.natAbs hval_eq exact congrArg Fin.val (hinj_nod (a₁ := ⟨(t₁.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) (a₂ := ⟨(t₂.val + 1) % s, by simp [rowFilled_length]; exact Nat.mod_lt _ (NeZero.pos s)⟩) hget) have ht_eq : t₁ = t₂ := by ext have h1l := t₁.isLt; have h2l := t₂.isLt by_cases ha1 : t₁.val + 1 < s <;> by_cases ha2 : t₂.val + 1 < s · rw [Nat.mod_eq_of_lt ha1, Nat.mod_eq_of_lt ha2] at hidx_eq; omega · have : t₂.val + 1 = s := by omega rw [Nat.mod_eq_of_lt ha1, this, Nat.mod_self] at hidx_eq; omega · have : t₁.val + 1 = s := by omega rw [this, Nat.mod_self, Nat.mod_eq_of_lt ha2] at hidx_eq; omega · omega subst ht_eq rfl /-! ## 6. Sparse Block-Diagonal Seam + Uniform-Block Assembly with Global Simplicity -/ /-- Slot placement function: maps `(r, c)` to `some e` iff `rowOf c e = r`. -/ def slotOfPlacement {m n k : ℕ} (rowOf : Fin n → Fin k → Fin m) (r : Fin m) (c : Fin n) : Option (Fin k) := (List.finRange k).find? (fun e => decide (rowOf c e = r)) lemma slotOfPlacement_eq_some_iff {m n k : ℕ} (rowOf : Fin n → Fin k → Fin m) (hinj : ∀ c, Function.Injective (rowOf c)) (r : Fin m) (c : Fin n) (e : Fin k) : slotOfPlacement rowOf r c = some e ↔ rowOf c e = r := by constructor · intro h have hpred := List.find?_some (p := fun e' => decide (rowOf c e' = r)) h exact of_decide_eq_true hpred · intro h have hex : ∃ e' ∈ List.finRange k, decide (rowOf c e' = r) = true := ⟨e, List.mem_finRange e, decide_eq_true h⟩ rcases List.find?_isSome.mpr hex |> Option.isSome_iff_exists.mp with ⟨e', he'⟩ have hpred : rowOf c e' = r := of_decide_eq_true (List.find?_some (p := fun e'' => decide (rowOf c e'' = r)) he') have : e' = e := hinj c (hpred.trans h.symm) rw [← this]; exact he' lemma slotOfPlacement_map_getD {m n k : ℕ} (rowOf : Fin n → Fin k → Fin m) (hinj : ∀ c, Function.Injective (rowOf c)) (r : Fin m) (c : Fin n) (f : Fin k → ℤ) : ((slotOfPlacement rowOf r c).map f).getD 0 = ∑ e : Fin k, if rowOf c e = r then f e else 0 := by by_cases hex : ∃ e : Fin k, rowOf c e = r · rcases hex with ⟨e₀, he₀⟩ have hslot : slotOfPlacement rowOf r c = some e₀ := (slotOfPlacement_eq_some_iff rowOf hinj r c e₀).mpr he₀ rw [hslot, Option.map_some, Option.getD_some, Finset.sum_eq_single e₀] · simp [he₀] · intro e _ hne have : rowOf c e ≠ r := fun h => hne (hinj c (h.trans he₀.symm)) simp [this] · intro h; exact absurd (Finset.mem_univ e₀) h · push Not at hex have hnone : slotOfPlacement rowOf r c = none := by cases hs : slotOfPlacement rowOf r c with | none => rfl | some e => exact absurd ((slotOfPlacement_eq_some_iff rowOf hinj r c e).mp hs) (hex e) rw [hnone, Option.map_none, Option.getD_none] symm exact Finset.sum_eq_zero (fun e _ => by simp [hex e]) lemma sparse_col_card {m n k : ℕ} (slot : Fin m → Fin n → Option (Fin k)) (huniq : ∀ c e, ∃! r : Fin m, slot r c = some e) (c : Fin n) (f : Fin k → ℤ) : (Finset.univ.filter (fun r : Fin m => ((slot r c).map f).isSome)).card = k := by have hbij : (Finset.univ.filter (fun r : Fin m => ((slot r c).map f).isSome)).card = (Finset.univ : Finset (Fin k)).card := by refine Finset.card_bij (fun r hr => (Option.isSome_iff_exists.mp (by have := (Finset.mem_filter.mp hr).2 simpa using this)).choose) ?_ ?_ ?_ · intro r _; exact Finset.mem_univ _ · intro r₁ hr₁ r₂ hr₂ heq have h1 := (Option.isSome_iff_exists.mp (by simpa using (Finset.mem_filter.mp hr₁).2)).choose_spec have h2 := (Option.isSome_iff_exists.mp (by simpa using (Finset.mem_filter.mp hr₂).2)).choose_spec rw [heq] at h1 exact ((huniq c _).unique h1 h2) · intro e _ obtain ⟨r, hr, _⟩ := huniq c e refine ⟨r, Finset.mem_filter.mpr ⟨Finset.mem_univ r, by simp [hr]⟩, ?_⟩ have hspec := (Option.isSome_iff_exists.mp (by simp [hr] : (slot r c).isSome)).choose_spec exact Option.some.inj (hspec.symm.trans hr) simpa using hbij lemma sparse_col_sum {m n k : ℕ} (slot : Fin m → Fin n → Option (Fin k)) (huniq : ∀ c e, ∃! r : Fin m, slot r c = some e) (c : Fin n) (f : Fin k → ℤ) : ∑ r : Fin m, ((slot r c).map f).getD 0 = ∑ e : Fin k, f e := by have h1 : ∑ r : Fin m, ((slot r c).map f).getD 0 = ∑ r ∈ Finset.univ.filter (fun r : Fin m => (slot r c).isSome), ((slot r c).map f).getD 0 := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun r : Fin m => (slot r c).isSome)] have h0 : ∑ r ∈ Finset.univ.filter (fun r : Fin m => ¬ (slot r c).isSome), ((slot r c).map f).getD 0 = 0 := by apply Finset.sum_eq_zero; intro r hr have hnone : slot r c = none := Option.not_isSome_iff_eq_none.mp (Finset.mem_filter.mp hr).2 simp [hnone] omega rw [h1] refine Finset.sum_bij (fun r hr => (Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr).2).choose) ?_ ?_ ?_ ?_ · intro r _; exact Finset.mem_univ _ · intro r₁ hr₁ r₂ hr₂ heq have h1' := (Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr₁).2).choose_spec have h2' := (Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr₂).2).choose_spec rw [heq] at h1' exact (huniq c _).unique h1' h2' · intro e _ obtain ⟨r, hr, _⟩ := huniq c e refine ⟨r, Finset.mem_filter.mpr ⟨Finset.mem_univ r, by simp [hr]⟩, ?_⟩ have hspec := (Option.isSome_iff_exists.mp (by simp [hr] : (slot r c).isSome)).choose_spec exact Option.some.inj (hspec.symm.trans hr) · intro r hr obtain ⟨e, he⟩ := Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr).2 have hspec := (Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr).2).choose_spec have hch : (Option.isSome_iff_exists.mp (Finset.mem_filter.mp hr).2).choose = e := Option.some.inj (hspec.symm.trans he) conv_lhs => rw [he] simp only [Option.map_some, Option.getD_some] exact congrArg f hch.symm /-- Helper: `filterMap` over `finRange (a + b)` splits into `finRange a` and shifted `finRange b`. -/ lemma filterMap_finRange_add {a b : ℕ} {α : Type*} (f : Fin (a + b) → Option α) : (List.finRange (a + b)).filterMap f = (List.finRange a).filterMap (fun i => f ⟨i.val, by omega⟩) ++ (List.finRange b).filterMap (fun j => f ⟨a + j.val, by omega⟩) := by have hsplit : List.finRange (a + b) = (List.finRange a).map (fun i => ⟨i.val, by omega⟩) ++ (List.finRange b).map (fun j => ⟨a + j.val, by omega⟩) := by apply List.ext_getElem · simp · intro idx h1 h2 have hidx : idx < a + b := by simpa using h1 simp only [List.getElem_finRange] by_cases hlt : idx < a · rw [List.getElem_append_left (by simp; exact hlt)] simp · rw [List.getElem_append_right (by simp; omega)] simp only [List.length_map, List.length_finRange, List.getElem_map, List.getElem_finRange, Fin.val_cast] exact Fin.ext (by simp only [Fin.val_cast]; omega) rw [hsplit, List.filterMap_append, List.filterMap_map, List.filterMap_map] rfl lemma filterMap_finRange_none {n : ℕ} {α : Type*} (f : Fin n → Option α) (hnone : ∀ i : Fin n, f i = none) : (List.finRange n).filterMap f = [] := by rw [List.filterMap_eq_nil_iff] intro i _; exact hnone i lemma filterMap_finRange_mul_block {m₁ B : ℕ} {α : Type*} (b₀ : Fin B) (f : Fin (m₁ * B) → Option α) (hzero : ∀ j : Fin (m₁ * B), j.val / m₁ ≠ b₀.val → f j = none) (hm₁ : 0 < m₁) : (List.finRange (m₁ * B)).filterMap f = (List.finRange m₁).filterMap (fun c => f ⟨m₁ * b₀.val + c.val, by have := b₀.isLt; have := c.isLt; nlinarith⟩) := by have hdecomp : m₁ * B = (m₁ * b₀.val + m₁) + (m₁ * (B - b₀.val - 1)) := by have := b₀.isLt have : B = b₀.val + 1 + (B - b₀.val - 1) := by omega conv_lhs => rw [this] ring have h1 : ∀ (n : ℕ) (h : n = m₁ * B) (g : Fin n → Option α), (List.finRange n).filterMap g = (List.finRange (m₁ * B)).filterMap (fun j => g ⟨j.val, by omega⟩) := by intro n h g; subst h; rfl rw [← h1 ((m₁ * b₀.val + m₁) + m₁ * (B - b₀.val - 1)) hdecomp.symm (fun j => f ⟨j.val, by omega⟩)] rw [filterMap_finRange_add, filterMap_finRange_add] have hleft : (List.finRange (m₁ * b₀.val)).filterMap (fun i => f ⟨i.val, by omega⟩) = [] := by apply filterMap_finRange_none intro i; apply hzero dsimp only have hi := i.isLt have hdiv : i.val / m₁ < b₀.val := Nat.div_lt_of_lt_mul (by linarith) omega have hright : (List.finRange (m₁ * (B - b₀.val - 1))).filterMap (fun j => f ⟨m₁ * b₀.val + m₁ + j.val, by omega⟩) = [] := by apply filterMap_finRange_none intro j; apply hzero dsimp only have hdiv : b₀.val + 1 ≤ (m₁ * b₀.val + m₁ + j.val) / m₁ := (Nat.le_div_iff_mul_le hm₁).mpr (by nlinarith) omega rw [hleft, hright, List.nil_append, List.append_nil] /-- **General Sparse Seam + Uniform-Block Assembly WITH Global Simplicity**: Given an underlying `k`-row entry table `A : Fin k → ι → ℤ`, one `m₀ × n₀` seam block, and `B` uniform `m₁ × n₁` blocks whose individual rows and columns are simple modulo `2 * (n₀ + n₁ * B) * k + 1`, produces a **`GloballySimpleHeffterArray (m₀ + m₁ * B) (n₀ + n₁ * B) s k`**. -/ noncomputable def ofSeamAndUniformBlocksSimple {ι : Type*} {m₀ n₀ m₁ n₁ B s k : ℕ} (A : Fin k → ι → ℤ) (seamCol : Fin n₀ → ι) (seamSgn : Fin n₀ → ℤ) (seamSlot : Fin m₀ → Fin n₀ → Option (Fin k)) (blkCol : Fin B → Fin n₁ → ι) (blkSgn : Fin B → Fin n₁ → ℤ) (blkSlot : Fin B → Fin m₁ → Fin n₁ → Option (Fin k)) (hm₁ : 0 < m₁) (hn₁ : 0 < n₁) (hA_col : ∀ x, ∑ e : Fin k, A e x = 0) (hA_nz : ∀ e x, A e x ≠ 0) (hA_le : ∀ e x, (A e x).natAbs ≤ (n₀ + n₁ * B) * k) (hA_inj : ∀ e₁ x₁ e₂ x₂, (A e₁ x₁).natAbs = (A e₂ x₂).natAbs → e₁ = e₂ ∧ x₁ = x₂) (hsSgn : ∀ c, (seamSgn c).natAbs = 1) (hbSgn : ∀ b c, (blkSgn b c).natAbs = 1) (hseam_rc : ∀ r, (Finset.univ.filter (fun c => (seamSlot r c).isSome)).card = s) (hseam_cu : ∀ c e, ∃! r : Fin m₀, seamSlot r c = some e) (hseam_rs : ∀ r, ∑ c : Fin n₀, ((seamSlot r c).map (fun e => seamSgn c * A e (seamCol c))).getD 0 = 0) (hblk_rc : ∀ b r, (Finset.univ.filter (fun c => (blkSlot b r c).isSome)).card = s) (hblk_cu : ∀ b c e, ∃! r : Fin m₁, blkSlot b r c = some e) (hblk_rs : ∀ b r, ∑ c : Fin n₁, ((blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))).getD 0 = 0) (hseam_inj : Function.Injective seamCol) (hblk_inj : ∀ b₁ c₁ b₂ c₂, blkCol b₁ c₁ = blkCol b₂ c₂ → b₁ = b₂ ∧ c₁ = c₂) (hdisj : ∀ c₀ b c₁, seamCol c₀ ≠ blkCol b c₁) (hseam_rsimp : ∀ r : Fin m₀, let L := (List.finRange n₀).filterMap (fun c => (seamSlot r c).map (fun e => seamSgn c * A e (seamCol c))) L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) (hblk_rsimp : ∀ (b : Fin B) (r : Fin m₁), let L := (List.finRange n₁).filterMap (fun c => (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))) L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) (hseam_csimp : ∀ c : Fin n₀, let L := (List.finRange m₀).filterMap (fun r => (seamSlot r c).map (fun e => seamSgn c * A e (seamCol c))) L.length = k → L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) (hblk_csimp : ∀ (b : Fin B) (c : Fin n₁), let L := (List.finRange m₁).filterMap (fun r => (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))) L.length = k → L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) : GloballySimpleHeffterArray (m₀ + m₁ * B) (n₀ + n₁ * B) s k := by let entry : Fin (m₀ + m₁ * B) → Fin (n₀ + n₁ * B) → Option ℤ := fun i j => if hi : i.val < m₀ then if hj : j.val < n₀ then (seamSlot ⟨i.val, hi⟩ ⟨j.val, hj⟩).map (fun e => seamSgn ⟨j.val, hj⟩ * A e (seamCol ⟨j.val, hj⟩)) else none else if hj : j.val < n₀ then none else if hbm : (i.val - m₀) / m₁ = (j.val - n₀) / n₁ then have hbB : (i.val - m₀) / m₁ < B := Nat.div_lt_of_lt_mul (by have := i.isLt; omega) let b : Fin B := ⟨(i.val - m₀) / m₁, hbB⟩ let r : Fin m₁ := ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩ let c : Fin n₁ := ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩ (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c)) else none let H : IntegerHeffterArray (m₀ + m₁ * B) (n₀ + n₁ * B) s k := { entry := entry row_card := fun i => by by_cases hi : i.val < m₀ · have hbij : (Finset.univ.filter (fun j : Fin (n₀ + n₁ * B) => (entry i j).isSome)).card = (Finset.univ.filter (fun c : Fin n₀ => (seamSlot ⟨i.val, hi⟩ c).isSome)).card := by refine Finset.card_bij (fun j hj => ⟨j.val, by have h := (Finset.mem_filter.mp hj).2 by_contra hge simp only [entry, dif_pos hi, dif_neg hge] at h simp at h⟩) ?_ ?_ ?_ · intro j hj have h := (Finset.mem_filter.mp hj).2 have hjlt : j.val < n₀ := by by_contra hge; simp only [entry, dif_pos hi, dif_neg hge] at h; simp at h simp only [entry, dif_pos hi, dif_pos hjlt] at h exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simpa using h⟩ · intro j₁ _ j₂ _ heq; exact Fin.ext (by simpa using congrArg Fin.val heq) · intro c hc refine ⟨⟨c.val, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, rfl⟩ simp only [entry, dif_pos hi, dif_pos c.isLt] simpa using (Finset.mem_filter.mp hc).2 rw [hbij, hseam_rc] · have hbB : (i.val - m₀) / m₁ < B := Nat.div_lt_of_lt_mul (by have := i.isLt; omega) let b : Fin B := ⟨(i.val - m₀) / m₁, hbB⟩ let r : Fin m₁ := ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩ have hbij : (Finset.univ.filter (fun j : Fin (n₀ + n₁ * B) => (entry i j).isSome)).card = (Finset.univ.filter (fun c : Fin n₁ => (blkSlot b r c).isSome)).card := by refine Finset.card_bij (fun j _ => ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩) ?_ ?_ ?_ · intro j hj have h := (Finset.mem_filter.mp hj).2 have hjge : ¬ (j.val < n₀) := by intro hlt; simp only [entry, dif_neg hi, dif_pos hlt] at h; simp at h have hbm : (i.val - m₀) / m₁ = (j.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hi, dif_neg hjge, dif_neg hne] at h; simp at h simp only [entry, dif_neg hi, dif_neg hjge, dif_pos hbm] at h exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simpa [b, r] using h⟩ · intro j₁ hj₁ j₂ hj₂ heq have h1 := (Finset.mem_filter.mp hj₁).2 have h2 := (Finset.mem_filter.mp hj₂).2 have hj1ge : ¬ (j₁.val < n₀) := by intro hlt; simp only [entry, dif_neg hi, dif_pos hlt] at h1; simp at h1 have hj2ge : ¬ (j₂.val < n₀) := by intro hlt; simp only [entry, dif_neg hi, dif_pos hlt] at h2; simp at h2 have hbm1 : (i.val - m₀) / m₁ = (j₁.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hi, dif_neg hj1ge, dif_neg hne] at h1; simp at h1 have hbm2 : (i.val - m₀) / m₁ = (j₂.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hi, dif_neg hj2ge, dif_neg hne] at h2; simp at h2 have hmod : (j₁.val - n₀) % n₁ = (j₂.val - n₀) % n₁ := congrArg Fin.val heq have hdiv : (j₁.val - n₀) / n₁ = (j₂.val - n₀) / n₁ := by omega have hd1 := Nat.div_add_mod (j₁.val - n₀) n₁ have hd2 := Nat.div_add_mod (j₂.val - n₀) n₁ rw [hdiv, hmod] at hd1 exact Fin.ext (by omega) · intro c hc let jval := n₀ + n₁ * b.val + c.val have hjlt : jval < n₀ + n₁ * B := by have := b.isLt; have := c.isLt; dsimp only [jval]; nlinarith have hjge : ¬ (jval < n₀) := by dsimp only [jval]; omega have hdiv : (jval - n₀) / n₁ = b.val := by rw [show jval - n₀ = c.val + n₁ * b.val by dsimp only [jval]; omega] rw [Nat.add_mul_div_left _ _ hn₁, Nat.div_eq_of_lt c.isLt, zero_add] have hmod : (jval - n₀) % n₁ = c.val := by rw [show jval - n₀ = c.val + n₁ * b.val by dsimp only [jval]; omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt c.isLt] refine ⟨⟨jval, hjlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, Fin.ext hmod⟩ have hbm : (i.val - m₀) / m₁ = (jval - n₀) / n₁ := hdiv.symm simp only [entry, dif_neg hi, dif_neg hjge, dif_pos hbm] have hceq : (⟨(jval - n₀) % n₁, Nat.mod_lt _ hn₁⟩ : Fin n₁) = c := Fin.ext hmod rw [hceq] simpa [b, r] using (Finset.mem_filter.mp hc).2 rw [hbij, hblk_rc] col_card := fun j => by by_cases hj : j.val < n₀ · have hbij : (Finset.univ.filter (fun i : Fin (m₀ + m₁ * B) => (entry i j).isSome)).card = (Finset.univ.filter (fun r : Fin m₀ => ((seamSlot r ⟨j.val, hj⟩).map (fun e => seamSgn ⟨j.val, hj⟩ * A e (seamCol ⟨j.val, hj⟩))).isSome)).card := by refine Finset.card_bij (fun i hi' => ⟨i.val, by have h := (Finset.mem_filter.mp hi').2 by_contra hge; simp only [entry, dif_neg hge, dif_pos hj] at h; simp at h⟩) ?_ ?_ ?_ · intro i hi' have h := (Finset.mem_filter.mp hi').2 have hilt : i.val < m₀ := by by_contra hge; simp only [entry, dif_neg hge, dif_pos hj] at h; simp at h simp only [entry, dif_pos hilt, dif_pos hj] at h exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, h⟩ · intro i₁ _ i₂ _ heq; exact Fin.ext (by simpa using congrArg Fin.val heq) · intro r hr refine ⟨⟨r.val, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, rfl⟩ simp only [entry, dif_pos r.isLt, dif_pos hj] exact (Finset.mem_filter.mp hr).2 rw [hbij]; exact sparse_col_card seamSlot hseam_cu ⟨j.val, hj⟩ _ · have hbB : (j.val - n₀) / n₁ < B := Nat.div_lt_of_lt_mul (by have := j.isLt; omega) let b : Fin B := ⟨(j.val - n₀) / n₁, hbB⟩ let c : Fin n₁ := ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩ have hbij : (Finset.univ.filter (fun i : Fin (m₀ + m₁ * B) => (entry i j).isSome)).card = (Finset.univ.filter (fun r : Fin m₁ => ((blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))).isSome)).card := by refine Finset.card_bij (fun i _ => ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩) ?_ ?_ ?_ · intro i hi' have h := (Finset.mem_filter.mp hi').2 have hige : ¬ (i.val < m₀) := by intro hlt; simp only [entry, dif_pos hlt, dif_neg hj] at h; simp at h have hbm : (i.val - m₀) / m₁ = (j.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hige, dif_neg hj, dif_neg hne] at h; simp at h simp only [entry, dif_neg hige, dif_neg hj, dif_pos hbm] at h have hbeq : (⟨(i.val - m₀) / m₁, Nat.div_lt_of_lt_mul (by have := i.isLt; omega)⟩ : Fin B) = b := Fin.ext hbm rw [hbeq] at h exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, h⟩ · intro i₁ hi₁ i₂ hi₂ heq have h1 := (Finset.mem_filter.mp hi₁).2 have h2 := (Finset.mem_filter.mp hi₂).2 have hi1ge : ¬ (i₁.val < m₀) := by intro hlt; simp only [entry, dif_pos hlt, dif_neg hj] at h1; simp at h1 have hi2ge : ¬ (i₂.val < m₀) := by intro hlt; simp only [entry, dif_pos hlt, dif_neg hj] at h2; simp at h2 have hbm1 : (i₁.val - m₀) / m₁ = (j.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hi1ge, dif_neg hj, dif_neg hne] at h1; simp at h1 have hbm2 : (i₂.val - m₀) / m₁ = (j.val - n₀) / n₁ := by by_contra hne; simp only [entry, dif_neg hi2ge, dif_neg hj, dif_neg hne] at h2; simp at h2 have hmod : (i₁.val - m₀) % m₁ = (i₂.val - m₀) % m₁ := congrArg Fin.val heq have hdiv : (i₁.val - m₀) / m₁ = (i₂.val - m₀) / m₁ := by omega have hd1 := Nat.div_add_mod (i₁.val - m₀) m₁ have hd2 := Nat.div_add_mod (i₂.val - m₀) m₁ rw [hdiv, hmod] at hd1 exact Fin.ext (by omega) · intro r hr let ival := m₀ + m₁ * b.val + r.val have hilt : ival < m₀ + m₁ * B := by have := b.isLt; have := r.isLt; dsimp only [ival]; nlinarith have hige : ¬ (ival < m₀) := by dsimp only [ival]; omega have hdiv : (ival - m₀) / m₁ = b.val := by rw [show ival - m₀ = r.val + m₁ * b.val by dsimp only [ival]; omega] rw [Nat.add_mul_div_left _ _ hm₁, Nat.div_eq_of_lt r.isLt, zero_add] have hmod : (ival - m₀) % m₁ = r.val := by rw [show ival - m₀ = r.val + m₁ * b.val by dsimp only [ival]; omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt r.isLt] refine ⟨⟨ival, hilt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, Fin.ext hmod⟩ have hbm : (ival - m₀) / m₁ = (j.val - n₀) / n₁ := hdiv simp only [entry, dif_neg hige, dif_neg hj, dif_pos hbm] have hbeq : (⟨(ival - m₀) / m₁, Nat.div_lt_of_lt_mul (by omega)⟩ : Fin B) = b := Fin.ext hdiv have hreq : (⟨(ival - m₀) % m₁, Nat.mod_lt _ hm₁⟩ : Fin m₁) = r := Fin.ext hmod rw [hbeq, hreq] exact (Finset.mem_filter.mp hr).2 rw [hbij]; exact sparse_col_card (blkSlot b) (hblk_cu b) c _ row_sum := fun i => by by_cases hi : i.val < m₀ · refine Eq.trans ?_ (hseam_rs ⟨i.val, hi⟩) rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun j : Fin (n₀ + n₁ * B) => j.val < n₀)] have h0 : ∑ j ∈ Finset.univ.filter (fun j : Fin (n₀ + n₁ * B) => ¬ j.val < n₀), (entry i j).getD 0 = 0 := Finset.sum_eq_zero (fun j hj => by have hge := (Finset.mem_filter.mp hj).2 simp only [entry, dif_pos hi, dif_neg hge, Option.getD_none]) rw [h0, add_zero] refine Finset.sum_bij (fun j hj => ⟨j.val, (Finset.mem_filter.mp hj).2⟩) (fun _ _ => Finset.mem_univ _) (fun _ _ _ _ h => Fin.ext (by simpa using congrArg Fin.val h)) (fun c _ => ⟨⟨c.val, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, c.isLt⟩, rfl⟩) (fun j hj => by have hlt := (Finset.mem_filter.mp hj).2 simp only [entry, dif_pos hi, dif_pos hlt]) · have hbB : (i.val - m₀) / m₁ < B := Nat.div_lt_of_lt_mul (by have := i.isLt; omega) let b : Fin B := ⟨(i.val - m₀) / m₁, hbB⟩ let r : Fin m₁ := ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩ refine Eq.trans ?_ (hblk_rs b r) let P : Fin (n₀ + n₁ * B) → Prop := fun j => ¬ j.val < n₀ ∧ (i.val - m₀) / m₁ = (j.val - n₀) / n₁ rw [← Finset.sum_filter_add_sum_filter_not Finset.univ P] have h0 : ∑ j ∈ Finset.univ.filter (fun j => ¬ P j), (entry i j).getD 0 = 0 := by apply Finset.sum_eq_zero; intro j hj have hnp := (Finset.mem_filter.mp hj).2 by_cases hjlt : j.val < n₀ · simp only [entry, dif_neg hi, dif_pos hjlt, Option.getD_none] · have hne : (i.val - m₀) / m₁ ≠ (j.val - n₀) / n₁ := fun heq => hnp ⟨hjlt, heq⟩ simp only [entry, dif_neg hi, dif_neg hjlt, dif_neg hne, Option.getD_none] rw [h0, add_zero] refine Finset.sum_bij (fun j _ => ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩) (fun _ _ => Finset.mem_univ _) ?_ ?_ ?_ · intro j₁ hj₁ j₂ hj₂ heq rcases (Finset.mem_filter.mp hj₁).2 with ⟨hge1, hdiv1⟩ rcases (Finset.mem_filter.mp hj₂).2 with ⟨hge2, hdiv2⟩ have hmod : (j₁.val - n₀) % n₁ = (j₂.val - n₀) % n₁ := congrArg Fin.val heq have hdiv : (j₁.val - n₀) / n₁ = (j₂.val - n₀) / n₁ := by omega have hd1 := Nat.div_add_mod (j₁.val - n₀) n₁ have hd2 := Nat.div_add_mod (j₂.val - n₀) n₁ rw [hdiv, hmod] at hd1 exact Fin.ext (by omega) · intro c _ let jval := n₀ + n₁ * b.val + c.val have hjlt : jval < n₀ + n₁ * B := by have := b.isLt; have := c.isLt; dsimp only [jval]; nlinarith have hdiv : (jval - n₀) / n₁ = b.val := by rw [show jval - n₀ = c.val + n₁ * b.val by dsimp only [jval]; omega] rw [Nat.add_mul_div_left _ _ hn₁, Nat.div_eq_of_lt c.isLt, zero_add] have hmod : (jval - n₀) % n₁ = c.val := by rw [show jval - n₀ = c.val + n₁ * b.val by dsimp only [jval]; omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt c.isLt] refine ⟨⟨jval, hjlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by exact ⟨by dsimp only [jval]; omega, hdiv.symm⟩⟩, Fin.ext hmod⟩ · intro j hj rcases (Finset.mem_filter.mp hj).2 with ⟨hge, hdiv⟩ simp only [entry, dif_neg hi, dif_neg hge, dif_pos hdiv, b, r] col_sum := fun j => by by_cases hj : j.val < n₀ · rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun i : Fin (m₀ + m₁ * B) => i.val < m₀)] have h0 : ∑ i ∈ Finset.univ.filter (fun i : Fin (m₀ + m₁ * B) => ¬ i.val < m₀), (entry i j).getD 0 = 0 := Finset.sum_eq_zero (fun i hi => by simp only [entry, dif_neg (Finset.mem_filter.mp hi).2, dif_pos hj, Option.getD_none]) rw [h0, add_zero] have hbij : ∑ i ∈ Finset.univ.filter (fun i : Fin (m₀ + m₁ * B) => i.val < m₀), (entry i j).getD 0 = ∑ r : Fin m₀, ((seamSlot r ⟨j.val, hj⟩).map (fun e => seamSgn ⟨j.val, hj⟩ * A e (seamCol ⟨j.val, hj⟩))).getD 0 := Finset.sum_bij (fun i hi => ⟨i.val, (Finset.mem_filter.mp hi).2⟩) (fun _ _ => Finset.mem_univ _) (fun _ _ _ _ h => Fin.ext (by simpa using congrArg Fin.val h)) (fun r _ => ⟨⟨r.val, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, r.isLt⟩, rfl⟩) (fun i hi => by simp only [entry, dif_pos (Finset.mem_filter.mp hi).2, dif_pos hj]) rw [hbij, sparse_col_sum seamSlot hseam_cu ⟨j.val, hj⟩, ← Finset.mul_sum, hA_col, mul_zero] · have hbB : (j.val - n₀) / n₁ < B := Nat.div_lt_of_lt_mul (by have := j.isLt; omega) let b : Fin B := ⟨(j.val - n₀) / n₁, hbB⟩ let c : Fin n₁ := ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩ let P : Fin (m₀ + m₁ * B) → Prop := fun i => ¬ i.val < m₀ ∧ (i.val - m₀) / m₁ = (j.val - n₀) / n₁ rw [← Finset.sum_filter_add_sum_filter_not Finset.univ P] have h0 : ∑ i ∈ Finset.univ.filter (fun i => ¬ P i), (entry i j).getD 0 = 0 := by apply Finset.sum_eq_zero; intro i hi have hnp := (Finset.mem_filter.mp hi).2 by_cases hilt : i.val < m₀ · simp only [entry, dif_pos hilt, dif_neg hj, Option.getD_none] · have hne : (i.val - m₀) / m₁ ≠ (j.val - n₀) / n₁ := fun heq => hnp ⟨hilt, heq⟩ simp only [entry, dif_neg hilt, dif_neg hj, dif_neg hne, Option.getD_none] rw [h0, add_zero] have hbij : ∑ i ∈ Finset.univ.filter P, (entry i j).getD 0 = ∑ r : Fin m₁, ((blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))).getD 0 := by refine Finset.sum_bij (fun i _ => ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩) (fun _ _ => Finset.mem_univ _) ?_ ?_ ?_ · intro i₁ hi₁ i₂ hi₂ heq rcases (Finset.mem_filter.mp hi₁).2 with ⟨hge1, hdiv1⟩ rcases (Finset.mem_filter.mp hi₂).2 with ⟨hge2, hdiv2⟩ have hmod : (i₁.val - m₀) % m₁ = (i₂.val - m₀) % m₁ := congrArg Fin.val heq have hdiv : (i₁.val - m₀) / m₁ = (i₂.val - m₀) / m₁ := by omega have hd1 := Nat.div_add_mod (i₁.val - m₀) m₁ have hd2 := Nat.div_add_mod (i₂.val - m₀) m₁ rw [hdiv, hmod] at hd1 exact Fin.ext (by omega) · intro r _ let ival := m₀ + m₁ * b.val + r.val have hilt : ival < m₀ + m₁ * B := by have := b.isLt; have := r.isLt; dsimp only [ival]; nlinarith have hdiv : (ival - m₀) / m₁ = b.val := by rw [show ival - m₀ = r.val + m₁ * b.val by dsimp only [ival]; omega] rw [Nat.add_mul_div_left _ _ hm₁, Nat.div_eq_of_lt r.isLt, zero_add] have hmod : (ival - m₀) % m₁ = r.val := by rw [show ival - m₀ = r.val + m₁ * b.val by dsimp only [ival]; omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt r.isLt] refine ⟨⟨ival, hilt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by exact ⟨by dsimp only [ival]; omega, hdiv⟩⟩, Fin.ext hmod⟩ · intro i hi rcases (Finset.mem_filter.mp hi).2 with ⟨hge, hdiv⟩ simp only [entry, dif_neg hge, dif_neg hj, dif_pos hdiv] have hbeq : (⟨(i.val - m₀) / m₁, Nat.div_lt_of_lt_mul (by have := i.isLt; omega)⟩ : Fin B) = b := Fin.ext hdiv rw [hbeq] rw [hbij, sparse_col_sum (blkSlot b) (hblk_cu b) c, ← Finset.mul_sum, hA_col, mul_zero] nonzero := fun i j x hx => by dsimp only [entry] at hx by_cases hi : i.val < m₀ · by_cases hj : j.val < n₀ · simp only [dif_pos hi, dif_pos hj] at hx rcases Option.map_eq_some_iff.mp hx with ⟨e, _, rfl⟩ exact mul_ne_zero (by have := hsSgn ⟨j.val, hj⟩; omega) (hA_nz e _) · simp only [dif_pos hi, dif_neg hj] at hx; cases hx · by_cases hj : j.val < n₀ · simp only [dif_neg hi, dif_pos hj] at hx; cases hx · by_cases hbm : (i.val - m₀) / m₁ = (j.val - n₀) / n₁ · simp only [dif_neg hi, dif_neg hj, dif_pos hbm] at hx rcases Option.map_eq_some_iff.mp hx with ⟨e, _, rfl⟩ have h1 := hbSgn ⟨(i.val - m₀) / m₁, Nat.div_lt_of_lt_mul (by have := i.isLt; omega)⟩ ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩ exact mul_ne_zero (by omega) (hA_nz e _) · simp only [dif_neg hi, dif_neg hj, dif_neg hbm] at hx; cases hx mag_le := fun i j x hx => by dsimp only [entry] at hx by_cases hi : i.val < m₀ · by_cases hj : j.val < n₀ · simp only [dif_pos hi, dif_pos hj] at hx rcases Option.map_eq_some_iff.mp hx with ⟨e, _, rfl⟩ rw [Int.natAbs_mul, hsSgn, one_mul]; exact hA_le e _ · simp only [dif_pos hi, dif_neg hj] at hx; cases hx · by_cases hj : j.val < n₀ · simp only [dif_neg hi, dif_pos hj] at hx; cases hx · by_cases hbm : (i.val - m₀) / m₁ = (j.val - n₀) / n₁ · simp only [dif_neg hi, dif_neg hj, dif_pos hbm] at hx rcases Option.map_eq_some_iff.mp hx with ⟨e, _, rfl⟩ rw [Int.natAbs_mul, hbSgn, one_mul]; exact hA_le e _ · simp only [dif_neg hi, dif_neg hj, dif_neg hbm] at hx; cases hx mag_inj := fun i₁ j₁ i₂ j₂ x₁ x₂ h₁ h₂ heq => by dsimp only [entry] at h₁ h₂ by_cases hi1 : i₁.val < m₀ <;> by_cases hj1 : j₁.val < n₀ <;> by_cases hi2 : i₂.val < m₀ <;> by_cases hj2 : j₂.val < n₀ all_goals simp only [dif_pos, dif_neg, hi1, hj1, hi2, hj2, not_false_eq_true] at h₁ h₂ all_goals try cases h₁ all_goals try cases h₂ · rcases Option.map_eq_some_iff.mp h₁ with ⟨e₁, hs1, rfl⟩ rcases Option.map_eq_some_iff.mp h₂ with ⟨e₂, hs2, rfl⟩ rw [Int.natAbs_mul, Int.natAbs_mul, hsSgn, hsSgn, one_mul, one_mul] at heq rcases hA_inj e₁ _ e₂ _ heq with ⟨rfl, hx⟩ have hj_eq : (⟨j₁.val, hj1⟩ : Fin n₀) = ⟨j₂.val, hj2⟩ := hseam_inj hx have hjv : j₁ = j₂ := Fin.ext (by simpa using congrArg Fin.val hj_eq) rw [hj_eq] at hs1 have hr_eq : (⟨i₁.val, hi1⟩ : Fin m₀) = ⟨i₂.val, hi2⟩ := (hseam_cu _ e₁).unique hs1 hs2 exact ⟨Fin.ext (by simpa using congrArg Fin.val hr_eq), hjv⟩ · by_cases hbm2 : (i₂.val - m₀) / m₁ = (j₂.val - n₀) / n₁ · simp only [dif_pos hbm2] at h₂ rcases Option.map_eq_some_iff.mp h₁ with ⟨e₁, _, rfl⟩ rcases Option.map_eq_some_iff.mp h₂ with ⟨e₂, _, rfl⟩ rw [Int.natAbs_mul, Int.natAbs_mul, hsSgn, hbSgn, one_mul, one_mul] at heq exact absurd (hA_inj e₁ _ e₂ _ heq).2 (hdisj _ _ _) · simp only [dif_neg hbm2] at h₂; cases h₂ · by_cases hbm1 : (i₁.val - m₀) / m₁ = (j₁.val - n₀) / n₁ · simp only [dif_pos hbm1] at h₁ rcases Option.map_eq_some_iff.mp h₁ with ⟨e₁, _, rfl⟩ rcases Option.map_eq_some_iff.mp h₂ with ⟨e₂, _, rfl⟩ rw [Int.natAbs_mul, Int.natAbs_mul, hbSgn, hsSgn, one_mul, one_mul] at heq exact absurd (hA_inj e₁ _ e₂ _ heq).2.symm (hdisj _ _ _) · simp only [dif_neg hbm1] at h₁; cases h₁ · by_cases hbm1 : (i₁.val - m₀) / m₁ = (j₁.val - n₀) / n₁ · by_cases hbm2 : (i₂.val - m₀) / m₁ = (j₂.val - n₀) / n₁ · simp only [dif_pos hbm1] at h₁ simp only [dif_pos hbm2] at h₂ rcases Option.map_eq_some_iff.mp h₁ with ⟨e₁, hs1, rfl⟩ rcases Option.map_eq_some_iff.mp h₂ with ⟨e₂, hs2, rfl⟩ rw [Int.natAbs_mul, Int.natAbs_mul, hbSgn, hbSgn, one_mul, one_mul] at heq rcases hA_inj e₁ _ e₂ _ heq with ⟨rfl, hx⟩ rcases hblk_inj _ _ _ _ hx with ⟨hb_eq, hc_eq⟩ have hb_val : (i₁.val - m₀) / m₁ = (i₂.val - m₀) / m₁ := congrArg Fin.val hb_eq have hc_val : (j₁.val - n₀) % n₁ = (j₂.val - n₀) % n₁ := congrArg Fin.val hc_eq have hj_eq : j₁ = j₂ := by have hdiv : (j₁.val - n₀) / n₁ = (j₂.val - n₀) / n₁ := by omega have hd1 := Nat.div_add_mod (j₁.val - n₀) n₁ have hd2 := Nat.div_add_mod (j₂.val - n₀) n₁ rw [hdiv, hc_val] at hd1 exact Fin.ext (by omega) rw [hb_eq, hc_eq] at hs1 have hr_eq := (hblk_cu _ _ e₁).unique hs1 hs2 have hr_val : (i₁.val - m₀) % m₁ = (i₂.val - m₀) % m₁ := congrArg Fin.val hr_eq have hi_eq : i₁ = i₂ := by have hd1 := Nat.div_add_mod (i₁.val - m₀) m₁ have hd2 := Nat.div_add_mod (i₂.val - m₀) m₁ rw [hb_val, hr_val] at hd1 exact Fin.ext (by omega) exact ⟨hi_eq, hj_eq⟩ · simp only [dif_neg hbm2] at h₂; cases h₂ · simp only [dif_neg hbm1] at h₁; cases h₁ } exact { toIntegerHeffterArray := H row_simple := fun i => by have hsum_i := rowFilled_sum H i have hbnd_i := fun x (hx : x ∈ rowFilled H i) => rowFilled_mem_bounds H i hx have hnod_i := rowFilled_mag_nodup H i by_cases hi : i.val < m₀ · have hrow : rowFilled H i = (List.finRange n₀).filterMap (fun c => (seamSlot ⟨i.val, hi⟩ c).map (fun e => seamSgn c * A e (seamCol c))) := by rw [rowFilled, filterMap_finRange_add] have hright : (List.finRange (n₁ * B)).filterMap (fun j => H.entry i ⟨n₀ + j.val, by omega⟩) = [] := by apply filterMap_finRange_none; intro j have hge : ¬ (n₀ + j.val < n₀) := by omega simp only [H, entry, dif_pos hi, dif_neg hge] rw [hright, List.append_nil] congr 1; funext c; simp only [H, entry, dif_pos hi, dif_pos c.isLt] rw [hrow] at hsum_i hbnd_i hnod_i ⊢ exact hseam_rsimp ⟨i.val, hi⟩ hsum_i hbnd_i hnod_i · have hbB : (i.val - m₀) / m₁ < B := Nat.div_lt_of_lt_mul (by have := i.isLt; omega) let b : Fin B := ⟨(i.val - m₀) / m₁, hbB⟩ let r : Fin m₁ := ⟨(i.val - m₀) % m₁, Nat.mod_lt _ hm₁⟩ have hrow : rowFilled H i = (List.finRange n₁).filterMap (fun c => (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))) := by rw [rowFilled, filterMap_finRange_add] have hleft : (List.finRange n₀).filterMap (fun c => H.entry i ⟨c.val, by omega⟩) = [] := by apply filterMap_finRange_none; intro c simp only [H, entry, dif_neg hi, dif_pos c.isLt] rw [hleft, List.nil_append] rw [filterMap_finRange_mul_block b _ (fun j hne => by have hge : ¬ (n₀ + j.val < n₀) := by omega have hbm : (i.val - m₀) / m₁ ≠ (n₀ + j.val - n₀) / n₁ := by rw [Nat.add_sub_cancel_left]; exact hne.symm simp only [H, entry, dif_neg hi, dif_neg hge, dif_neg hbm]) hn₁] congr 1; funext c have hge : ¬ (n₀ + (n₁ * b.val + c.val) < n₀) := by omega have hdiv : (n₀ + (n₁ * b.val + c.val) - n₀) / n₁ = b.val := by rw [Nat.add_sub_cancel_left, show n₁ * b.val + c.val = c.val + n₁ * b.val by omega] rw [Nat.add_mul_div_left _ _ hn₁, Nat.div_eq_of_lt c.isLt, zero_add] have hmod : (n₀ + (n₁ * b.val + c.val) - n₀) % n₁ = c.val := by rw [Nat.add_sub_cancel_left, show n₁ * b.val + c.val = c.val + n₁ * b.val by omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt c.isLt] have hbm : (i.val - m₀) / m₁ = (n₀ + (n₁ * b.val + c.val) - n₀) / n₁ := hdiv.symm simp only [H, entry, dif_neg hi, dif_neg hge, dif_pos hbm] have hceq : (⟨(n₀ + (n₁ * b.val + c.val) - n₀) % n₁, Nat.mod_lt _ hn₁⟩ : Fin n₁) = c := Fin.ext hmod rw [hceq] rw [hrow] at hsum_i hbnd_i hnod_i ⊢ exact hblk_rsimp b r hsum_i hbnd_i hnod_i col_simple := fun j => by have hlen_j := colFilled_length H j have hsum_j := colFilled_sum H j have hbnd_j := fun x (hx : x ∈ colFilled H j) => colFilled_mem_bounds H j hx have hnod_j := colFilled_mag_nodup H j by_cases hj : j.val < n₀ · have hcol : colFilled H j = (List.finRange m₀).filterMap (fun r => (seamSlot r ⟨j.val, hj⟩).map (fun e => seamSgn ⟨j.val, hj⟩ * A e (seamCol ⟨j.val, hj⟩))) := by rw [colFilled, filterMap_finRange_add] have hright : (List.finRange (m₁ * B)).filterMap (fun i => H.entry ⟨m₀ + i.val, by omega⟩ j) = [] := by apply filterMap_finRange_none; intro i have hge : ¬ (m₀ + i.val < m₀) := by omega simp only [H, entry, dif_neg hge, dif_pos hj] rw [hright, List.append_nil] congr 1; funext r; simp only [H, entry, dif_pos r.isLt, dif_pos hj] rw [hcol] at hlen_j hsum_j hbnd_j hnod_j ⊢ exact hseam_csimp ⟨j.val, hj⟩ hlen_j hsum_j hbnd_j hnod_j · have hbB : (j.val - n₀) / n₁ < B := Nat.div_lt_of_lt_mul (by have := j.isLt; omega) let b : Fin B := ⟨(j.val - n₀) / n₁, hbB⟩ let c : Fin n₁ := ⟨(j.val - n₀) % n₁, Nat.mod_lt _ hn₁⟩ have hcol : colFilled H j = (List.finRange m₁).filterMap (fun r => (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))) := by rw [colFilled, filterMap_finRange_add] have hleft : (List.finRange m₀).filterMap (fun r => H.entry ⟨r.val, by omega⟩ j) = [] := by apply filterMap_finRange_none; intro r simp only [H, entry, dif_pos r.isLt, dif_neg hj] rw [hleft, List.nil_append] rw [filterMap_finRange_mul_block b _ (fun i hne => by have hge : ¬ (m₀ + i.val < m₀) := by omega have hbm : (m₀ + i.val - m₀) / m₁ ≠ (j.val - n₀) / n₁ := by rw [Nat.add_sub_cancel_left]; exact hne simp only [H, entry, dif_neg hge, dif_neg hj, dif_neg hbm]) hm₁] congr 1; funext r have hge : ¬ (m₀ + (m₁ * b.val + r.val) < m₀) := by omega have hdiv : (m₀ + (m₁ * b.val + r.val) - m₀) / m₁ = b.val := by rw [Nat.add_sub_cancel_left, show m₁ * b.val + r.val = r.val + m₁ * b.val by omega] rw [Nat.add_mul_div_left _ _ hm₁, Nat.div_eq_of_lt r.isLt, zero_add] have hmod : (m₀ + (m₁ * b.val + r.val) - m₀) % m₁ = r.val := by rw [Nat.add_sub_cancel_left, show m₁ * b.val + r.val = r.val + m₁ * b.val by omega] rw [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt r.isLt] have hbm : (m₀ + (m₁ * b.val + r.val) - m₀) / m₁ = (j.val - n₀) / n₁ := hdiv simp only [H, entry, dif_neg hge, dif_neg hj, dif_pos hbm] have hbeq : (⟨(m₀ + (m₁ * b.val + r.val) - m₀) / m₁, Nat.div_lt_of_lt_mul (by have := b.isLt; have := r.isLt; rw [Nat.add_sub_cancel_left]; nlinarith)⟩ : Fin B) = b := Fin.ext hdiv have hreq : (⟨(m₀ + (m₁ * b.val + r.val) - m₀) % m₁, Nat.mod_lt _ hm₁⟩ : Fin m₁) = r := Fin.ext hmod rw [hbeq, hreq] rw [hcol] at hlen_j hsum_j hbnd_j hnod_j ⊢ exact hblk_csimp b c hlen_j hsum_j hbnd_j hnod_j } /-- Convenience wrapper for `k ≤ 5`: columns are automatically simple by Proposition 2.1, and each row-simplicity obligation receives the already-proved zero-sum, magnitude-bound, and distinct-magnitude hypotheses from the assembled `IntegerHeffterArray`. -/ noncomputable def ofSeamAndUniformBlocksSimpleKLe5 {ι : Type*} {m₀ n₀ m₁ n₁ B s k : ℕ} (hk5 : k ≤ 5) (A : Fin k → ι → ℤ) (seamCol : Fin n₀ → ι) (seamSgn : Fin n₀ → ℤ) (seamSlot : Fin m₀ → Fin n₀ → Option (Fin k)) (blkCol : Fin B → Fin n₁ → ι) (blkSgn : Fin B → Fin n₁ → ℤ) (blkSlot : Fin B → Fin m₁ → Fin n₁ → Option (Fin k)) (hm₁ : 0 < m₁) (hn₁ : 0 < n₁) (hA_col : ∀ x, ∑ e : Fin k, A e x = 0) (hA_nz : ∀ e x, A e x ≠ 0) (hA_le : ∀ e x, (A e x).natAbs ≤ (n₀ + n₁ * B) * k) (hA_inj : ∀ e₁ x₁ e₂ x₂, (A e₁ x₁).natAbs = (A e₂ x₂).natAbs → e₁ = e₂ ∧ x₁ = x₂) (hsSgn : ∀ c, (seamSgn c).natAbs = 1) (hbSgn : ∀ b c, (blkSgn b c).natAbs = 1) (hseam_rc : ∀ r, (Finset.univ.filter (fun c => (seamSlot r c).isSome)).card = s) (hseam_cu : ∀ c e, ∃! r : Fin m₀, seamSlot r c = some e) (hseam_rs : ∀ r, ∑ c : Fin n₀, ((seamSlot r c).map (fun e => seamSgn c * A e (seamCol c))).getD 0 = 0) (hblk_rc : ∀ b r, (Finset.univ.filter (fun c => (blkSlot b r c).isSome)).card = s) (hblk_cu : ∀ b c e, ∃! r : Fin m₁, blkSlot b r c = some e) (hblk_rs : ∀ b r, ∑ c : Fin n₁, ((blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))).getD 0 = 0) (hseam_inj : Function.Injective seamCol) (hblk_inj : ∀ b₁ c₁ b₂ c₂, blkCol b₁ c₁ = blkCol b₂ c₂ → b₁ = b₂ ∧ c₁ = c₂) (hdisj : ∀ c₀ b c₁, seamCol c₀ ≠ blkCol b c₁) (hseam_rsimp : ∀ r : Fin m₀, let L := (List.finRange n₀).filterMap (fun c => (seamSlot r c).map (fun e => seamSgn c * A e (seamCol c))) L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) (hblk_rsimp : ∀ (b : Fin B) (r : Fin m₁), let L := (List.finRange n₁).filterMap (fun c => (blkSlot b r c).map (fun e => blkSgn b c * A e (blkCol b c))) L.sum = 0 → (∀ x ∈ L, 1 ≤ x.natAbs ∧ x.natAbs ≤ (n₀ + n₁ * B) * k) → (L.map Int.natAbs).Nodup → IsSimpleList (2 * ((n₀ + n₁ * B) * k) + 1) L) : GloballySimpleHeffterArray (m₀ + m₁ * B) (n₀ + n₁ * B) s k := ofSeamAndUniformBlocksSimple A seamCol seamSgn seamSlot blkCol blkSgn blkSlot hm₁ hn₁ hA_col hA_nz hA_le hA_inj hsSgn hbSgn hseam_rc hseam_cu hseam_rs hblk_rc hblk_cu hblk_rs hseam_inj hblk_inj hdisj hseam_rsimp hblk_rsimp (fun _ hlen hsum hbnd hnod => isSimpleList_of_length_le_five _ (by omega) hsum hbnd hnod) (fun _ _ hlen hsum hbnd hnod => isSimpleList_of_length_le_five _ (by omega) hsum hbnd hnod) #print axioms ofSeamAndUniformBlocksSimpleKLe5 #print axioms isSimpleList_iff_subsum_ne_zero #print axioms isSimpleList_of_length_le_five #print axioms globallySimple_of_le_five #print axioms isSimpleList_of_len_six #print axioms isSimpleList_of_len_seven #print axioms GloballySimpleHeffterArray.toOrthogonalCyclicCycleDecomps #print axioms ofSeamAndUniformBlocksSimple end GloballySimpleCore