import Mathlib /-! # General Goethals–Seidel Hadamard Matrix Theorem & Sum-of-Four-Squares Obstruction Context: `research/tasks/HadamardCorrelation.lean` and `research/tasks/HadamardGram52.lean` verify a single `52 × 52` Hadamard matrix by brute-force `decide` (`maxHeartbeats 4000000`). Here we formalize the **general algebraic Goethals–Seidel theorem** for arbitrary `n ≥ 1`, together with the **half-lag sufficiency theorem**, the **sum-of-four-squares necessary obstruction**, and an instant (kernel-fast, default-heartbeat) proof of the `52 × 52` Hadamard matrix from `hadamard-52.json` as a direct corollary. ## Main Results 1. **Circulant Algebra & Reversal Symmetry**: - `circ_mul_comm`: any two `n × n` circulant matrices commute (`circ u * circ v = circ v * circ u`). - `circ_transpose`: `(circ w)ᵀ = circ (fun k => w (-k))`. - `revMat_mul_circ_transpose`: `R * (circ w)ᵀ = circ w * R` (and `(circ w * R)ᵀ = circ w * R`). - `circ_mul_transpose_eq_pcorr`: `(circ w * (circ w)ᵀ) i j = pcorr w (i - j)`. 2. **General Goethals–Seidel Block Identity** (`goethalsSeidel_mul_transpose`): For every `n ≥ 1`, every shift `c : ZMod n`, and any four sequences `a, b, c, d : ZMod n → ℤ`, the `4n × 4n` Goethals–Seidel matrix `GS = goethalsSeidelMat a b c d shift` satisfies `(GS * GSᵀ) (r, i) (s, j) = if r = s then pcorr a (i - j) + pcorr b (i - j) + pcorr c (i - j) + pcorr d (i - j) else 0`. 3. **General Goethals–Seidel Hadamard Theorem** (`goethalsSeidel_hadamard`): If `a, b, c, d : ZMod n → ℤ` are `±1`-valued and complementary (their periodic autocorrelations sum to `0` at every nonzero lag `k ≠ 0` — or equivalently at lags `1 ≤ k ≤ n / 2` by `complementary_of_half`), then `GS` is a Hadamard matrix of order `4 * n`: every entry lies in `{-1, 1}` and `GS * GSᵀ = (4 * n : ℤ) • 1`. 4. **Sum-of-Four-Squares Obstruction** (`complementary_sum_of_four_squares`): Every complementary quadruple of `±1`-valued length-`n` sequences satisfies `(∑ i, a i)^2 + (∑ i, b i)^2 + (∑ i, c i)^2 + (∑ i, d i)^2 = 4 * n`. 5. **Instant Order-52 Hadamard Matrix** (`hadamard_52_goethalsSeidel`): Instantiating (3) on the four length-13 sign words of `hadamard-52.json` yields a certified `52 × 52` Hadamard matrix in milliseconds under default heartbeats. -/ open scoped BigOperators Matrix open Matrix Finset namespace AutoDiscovery.GoethalsSeidelHadamard section CirculantAlgebra variable {n : ℕ} [NeZero n] /-- Periodic autocorrelation of a sequence `w : ZMod n → ℤ` at lag `k : ZMod n`. -/ def pcorr (w : ZMod n → ℤ) (k : ZMod n) : ℤ := ∑ t : ZMod n, w t * w (t + k) /-- Symmetry of periodic autocorrelation: `pcorr w (-k) = pcorr w k`. -/ lemma pcorr_neg (w : ZMod n → ℤ) (k : ZMod n) : pcorr w (-k) = pcorr w k := by unfold pcorr let e : ZMod n ≃ ZMod n := Equiv.addRight k rw [← Equiv.sum_comp e] refine Finset.sum_congr rfl fun t _ => ?_ have he : e t = t + k := rfl rw [he, mul_comm] have h1 : t + k + -k = t := by ring rw [h1] /-- For a `±1`-valued sequence, `pcorr w 0 = n`. -/ lemma pcorr_zero_of_sign {w : ZMod n → ℤ} (hw : ∀ t, w t = 1 ∨ w t = -1) : pcorr w 0 = (n : ℤ) := by unfold pcorr have h1 : ∀ t : ZMod n, w t * w t = 1 := by intro t rcases hw t with h | h <;> simp [h] simp [h1] /-- Sum of periodic autocorrelations over all lags equals the squared row sum: `∑ k : ZMod n, pcorr w k = (∑ t : ZMod n, w t) ^ 2`. -/ theorem sum_pcorr_eq_sq_sum (w : ZMod n → ℤ) : ∑ k : ZMod n, pcorr w k = (∑ t : ZMod n, w t) ^ 2 := by unfold pcorr rw [Finset.sum_comm] calc ∑ t : ZMod n, ∑ k : ZMod n, w t * w (t + k) = ∑ t : ZMod n, w t * ∑ k : ZMod n, w (t + k) := by refine Finset.sum_congr rfl fun t _ => (Finset.mul_sum _ _ _).symm _ = ∑ t : ZMod n, w t * ∑ s : ZMod n, w s := by refine Finset.sum_congr rfl fun t _ => ?_ congr 1 exact Equiv.sum_comp (Equiv.addLeft t) w _ = (∑ t : ZMod n, w t) * (∑ s : ZMod n, w s) := (Finset.sum_mul _ _ _).symm _ = (∑ t : ZMod n, w t) ^ 2 := (sq _).symm /-- **Sum-of-Four-Squares Obstruction**: For any four `±1`-valued complementary sequences of length `n`, `(∑ a)^2 + (∑ b)^2 + (∑ c)^2 + (∑ d)^2 = 4 * n`. -/ theorem complementary_sum_of_four_squares {a b c d : ZMod n → ℤ} (ha : ∀ t, a t = 1 ∨ a t = -1) (hb : ∀ t, b t = 1 ∨ b t = -1) (hc : ∀ t, c t = 1 ∨ c t = -1) (hd : ∀ t, d t = 1 ∨ d t = -1) (hcomp : ∀ k : ZMod n, k ≠ 0 → pcorr a k + pcorr b k + pcorr c k + pcorr d k = 0) : (∑ t : ZMod n, a t) ^ 2 + (∑ t : ZMod n, b t) ^ 2 + (∑ t : ZMod n, c t) ^ 2 + (∑ t : ZMod n, d t) ^ 2 = 4 * (n : ℤ) := by rw [← sum_pcorr_eq_sq_sum a, ← sum_pcorr_eq_sq_sum b, ← sum_pcorr_eq_sq_sum c, ← sum_pcorr_eq_sq_sum d] have hsum : ∑ k : ZMod n, (pcorr a k + pcorr b k + pcorr c k + pcorr d k) = (∑ k : ZMod n, pcorr a k) + (∑ k : ZMod n, pcorr b k) + (∑ k : ZMod n, pcorr c k) + (∑ k : ZMod n, pcorr d k) := by simp only [Finset.sum_add_distrib] rw [← hsum] rw [Finset.sum_eq_single (0 : ZMod n)] · rw [pcorr_zero_of_sign ha, pcorr_zero_of_sign hb, pcorr_zero_of_sign hc, pcorr_zero_of_sign hd] ring · intro k _ hk0 exact hcomp k hk0 · intro h0; exact (h0 (Finset.mem_univ 0)).elim /-- Half-lag sufficiency: checking `1 ≤ k ≤ n / 2` suffices for complementarity at all `k ≠ 0` in `ZMod n`. -/ theorem complementary_of_half {a b c d : ZMod n → ℤ} (hhalf : ∀ k : ℕ, 0 < k → k ≤ n / 2 → pcorr a (k : ZMod n) + pcorr b (k : ZMod n) + pcorr c (k : ZMod n) + pcorr d (k : ZMod n) = 0) : ∀ k : ZMod n, k ≠ 0 → pcorr a k + pcorr b k + pcorr c k + pcorr d k = 0 := by intro k hk0 have hval_pos : 0 < k.val := by by_contra h0 have hkv : k.val = 0 := by omega apply hk0 rw [← ZMod.natCast_zmod_val k, hkv, Nat.cast_zero] have hval_lt : k.val < n := ZMod.val_lt k by_cases hle : k.val ≤ n / 2 · have := hhalf k.val hval_pos hle simpa [ZMod.natCast_zmod_val] using this · -- Use symmetry `pcorr w (-k) = pcorr w k` with `(-k).val = n - k.val ≤ n / 2`! have hneg_val : (-k).val = n - k.val := by rw [ZMod.neg_val' k, Nat.mod_eq_of_lt (by omega)] have hneg_pos : 0 < n - k.val := by omega have hneg_le : n - k.val ≤ n / 2 := by omega have h1 := hhalf (n - k.val) hneg_pos hneg_le have hcast : ((n - k.val : ℕ) : ZMod n) = -k := by rw [← hneg_val, ZMod.natCast_zmod_val] rw [hcast, pcorr_neg, pcorr_neg, pcorr_neg, pcorr_neg] at h1 exact h1 /-- Circulant matrix associated to `w : ZMod n → ℤ`: entry `(i, j)` is `w (j - i)`. -/ def circ (w : ZMod n → ℤ) : Matrix (ZMod n) (ZMod n) ℤ := Matrix.of fun i j => w (j - i) /-- Reversal (anti-diagonal) matrix `R shift`: entry `(i, j)` is `1` iff `i + j = shift`. -/ def revMat (shift : ZMod n) : Matrix (ZMod n) (ZMod n) ℤ := Matrix.of fun i j => if i + j = shift then 1 else 0 omit [NeZero n] in lemma circ_transpose (w : ZMod n → ℤ) : (circ w)ᵀ = circ (fun k => w (-k)) := by ext i j simp only [Matrix.transpose_apply, circ, Matrix.of_apply] congr 1; ring lemma circ_mul_comm (u v : ZMod n → ℤ) : circ u * circ v = circ v * circ u := by ext i j simp only [Matrix.mul_apply, circ, Matrix.of_apply] let e : ZMod n ≃ ZMod n := { toFun := fun k => i + j - k invFun := fun k => i + j - k left_inv := fun k => by ring right_inv := fun k => by ring } rw [← Equiv.sum_comp e] refine Finset.sum_congr rfl fun k _ => ?_ dsimp only [e, Equiv.coe_fn_mk] rw [mul_comm] congr 1 <;> (congr 1; ring) omit [NeZero n] in lemma revMat_transpose (shift : ZMod n) : (revMat shift)ᵀ = revMat shift := by ext i j simp only [Matrix.transpose_apply, revMat, Matrix.of_apply, add_comm] lemma revMat_mul_self (shift : ZMod n) : revMat shift * revMat shift = 1 := by ext i j simp only [Matrix.mul_apply, revMat, Matrix.of_apply, Matrix.one_apply] rw [Finset.sum_eq_single (shift - i)] · have h1 : i + (shift - i) = shift := by ring have h2 : (shift - i) + j = shift ↔ i = j := by constructor <;> intro h · calc i = shift - (shift - i) := by ring _ = shift - (shift - j) := by congr 1 calc shift - i = (shift - i) + j - j := by ring _ = shift - j := by rw [h] _ = j := by ring · rw [h]; ring simp [h1, h2] · intro k _ hk have hne : i + k ≠ shift := fun h => hk (by calc k = i + k - i := by ring _ = shift - i := by rw [h]) simp [hne] · intro h; exact (h (Finset.mem_univ _)).elim lemma circ_mul_revMat_apply (w : ZMod n → ℤ) (shift i j : ZMod n) : (circ w * revMat shift) i j = w (shift - i - j) := by simp only [Matrix.mul_apply, circ, revMat, Matrix.of_apply] rw [Finset.sum_eq_single (shift - j)] · have h1 : shift - j + j = shift := by ring have h2 : shift - j - i = shift - i - j := by ring simp [h1, h2] · intro k _ hk have hne : k + j ≠ shift := fun h => hk (by calc k = k + j - j := by ring _ = shift - j := by rw [h]) simp [hne] · intro h; exact (h (Finset.mem_univ _)).elim lemma revMat_mul_circ_apply (w : ZMod n → ℤ) (shift i j : ZMod n) : (revMat shift * circ w) i j = w (j + i - shift) := by simp only [Matrix.mul_apply, circ, revMat, Matrix.of_apply] rw [Finset.sum_eq_single (shift - i)] · have h1 : i + (shift - i) = shift := by ring have h2 : j - (shift - i) = j + i - shift := by ring simp [h1, h2] · intro k _ hk have hne : i + k ≠ shift := fun h => hk (by calc k = i + k - i := by ring _ = shift - i := by rw [h]) simp [hne] · intro h; exact (h (Finset.mem_univ _)).elim /-- Every `circ w * R` is symmetric! -/ lemma circ_mul_revMat_transpose (w : ZMod n → ℤ) (shift : ZMod n) : (circ w * revMat shift)ᵀ = circ w * revMat shift := by ext i j rw [Matrix.transpose_apply, circ_mul_revMat_apply, circ_mul_revMat_apply] congr 1; ring /-- Key intertwining identity: `R * (circ w)ᵀ = circ w * R`. -/ lemma revMat_mul_circ_transpose (w : ZMod n → ℤ) (shift : ZMod n) : revMat shift * (circ w)ᵀ = circ w * revMat shift := by ext i j rw [circ_transpose, revMat_mul_circ_apply, circ_mul_revMat_apply] congr 1; ring /-- `(circ w * (circ w)ᵀ) i j = pcorr w (i - j)`. -/ lemma circ_mul_transpose_apply (w : ZMod n → ℤ) (i j : ZMod n) : (circ w * (circ w)ᵀ) i j = pcorr w (i - j) := by simp only [Matrix.mul_apply, Matrix.transpose_apply, circ, Matrix.of_apply, pcorr] let e : ZMod n ≃ ZMod n := Equiv.addRight i rw [← Equiv.sum_comp e] refine Finset.sum_congr rfl fun t _ => ?_ have he : e t = t + i := rfl rw [he] have h1 : t + i - i = t := by ring have h2 : t + i - j = t + (i - j) := by ring rw [h1, h2] /-- Helper: `(circ u * R) * (circ v * R)ᵀ = circ u * (circ v)ᵀ`. -/ lemma circ_rev_mul_transpose (u v : ZMod n → ℤ) (shift : ZMod n) : (circ u * revMat shift) * (circ v * revMat shift)ᵀ = circ u * (circ v)ᵀ := by calc (circ u * revMat shift) * (circ v * revMat shift)ᵀ = (circ u * revMat shift) * ((revMat shift)ᵀ * (circ v)ᵀ) := by rw [Matrix.transpose_mul] _ = circ u * (revMat shift * revMat shift) * (circ v)ᵀ := by rw [revMat_transpose, Matrix.mul_assoc, Matrix.mul_assoc, Matrix.mul_assoc] _ = circ u * (circ v)ᵀ := by rw [revMat_mul_self, Matrix.mul_one] /-- Helper: `(circ u)ᵀ * circ u = circ u * (circ u)ᵀ`. -/ lemma circ_transpose_mul_self (u : ZMod n → ℤ) : (circ u)ᵀ * circ u = circ u * (circ u)ᵀ := by rw [circ_transpose, circ_mul_comm] end CirculantAlgebra section GoethalsSeidel variable {n : ℕ} [NeZero n] /-- The `(r, s)` block (each `n × n`) of the Goethals–Seidel array for sequences `a, b, c, d`. -/ def gsBlock (a b c d : ZMod n → ℤ) (shift : ZMod n) : Fin 4 → Fin 4 → Matrix (ZMod n) (ZMod n) ℤ | 0, 0 => circ a | 0, 1 => circ b * revMat shift | 0, 2 => circ c * revMat shift | 0, 3 => circ d * revMat shift | 1, 0 => -(circ b * revMat shift) | 1, 1 => circ a | 1, 2 => -((circ d)ᵀ * revMat shift) | 1, 3 => (circ c)ᵀ * revMat shift | 2, 0 => -(circ c * revMat shift) | 2, 1 => (circ d)ᵀ * revMat shift | 2, 2 => circ a | 2, 3 => -((circ b)ᵀ * revMat shift) | 3, 0 => -(circ d * revMat shift) | 3, 1 => -((circ c)ᵀ * revMat shift) | 3, 2 => (circ b)ᵀ * revMat shift | 3, 3 => circ a /-- The full `4n × 4n` Goethals–Seidel matrix indexed by `Fin 4 × ZMod n`. -/ def goethalsSeidelMat (a b c d : ZMod n → ℤ) (shift : ZMod n) : Matrix (Fin 4 × ZMod n) (Fin 4 × ZMod n) ℤ := Matrix.of fun p q => gsBlock a b c d shift p.1 q.1 p.2 q.2 /-- Every entry of `goethalsSeidelMat` is `±1` when `a, b, c, d` are `±1`-valued. -/ theorem goethalsSeidelMat_entries_pm_one {a b c d : ZMod n → ℤ} (shift : ZMod n) (ha : ∀ t, a t = 1 ∨ a t = -1) (hb : ∀ t, b t = 1 ∨ b t = -1) (hc : ∀ t, c t = 1 ∨ c t = -1) (hd : ∀ t, d t = 1 ∨ d t = -1) (p q : Fin 4 × ZMod n) : goethalsSeidelMat a b c d shift p q = 1 ∨ goethalsSeidelMat a b c d shift p q = -1 := by have hneg : ∀ x : ℤ, (x = 1 ∨ x = -1) → (-x = 1 ∨ -x = -1) := by intro x hx; rcases hx with rfl | rfl <;> simp rcases p with ⟨r, i⟩ rcases q with ⟨s, j⟩ fin_cases r <;> fin_cases s <;> simp only [goethalsSeidelMat, Matrix.of_apply, gsBlock, Matrix.neg_apply] <;> (try rw [circ_transpose]) <;> (try rw [circ_mul_revMat_apply]) <;> (try simp only [circ, Matrix.of_apply]) · exact ha _ · exact hb _ · exact hc _ · exact hd _ · exact hneg _ (hb _) · exact ha _ · exact hneg _ (hd _) · exact hc _ · exact hneg _ (hc _) · exact hd _ · exact ha _ · exact hneg _ (hb _) · exact hneg _ (hd _) · exact hneg _ (hc _) · exact hb _ · exact ha _ /-- Block-level product of `GS` and `GSᵀ`: equals `A Aᵀ + B Bᵀ + C Cᵀ + D Dᵀ` on the diagonal and `0` off the diagonal! -/ lemma gsBlock_mul_transpose_sum (a b c d : ZMod n → ℤ) (shift : ZMod n) (r s : Fin 4) : ∑ k : Fin 4, gsBlock a b c d shift r k * (gsBlock a b c d shift s k)ᵀ = if r = s then circ a * (circ a)ᵀ + circ b * (circ b)ᵀ + circ c * (circ c)ᵀ + circ d * (circ d)ᵀ else 0 := by -- Precompute key identities used across the 16 blocks! have hA_eq : ∀ u v : ZMod n → ℤ, circ u * (circ v * revMat shift)ᵀ = circ v * revMat shift * (circ u)ᵀ := by intro u v rw [circ_mul_revMat_transpose, Matrix.mul_assoc, revMat_mul_circ_transpose, ← Matrix.mul_assoc, ← Matrix.mul_assoc, circ_mul_comm] have hR_trans : ∀ u v : ZMod n → ℤ, ((circ u)ᵀ * revMat shift) * ((circ v)ᵀ * revMat shift)ᵀ = (circ u)ᵀ * circ v := by intro u v rw [circ_transpose u, circ_transpose v, circ_rev_mul_transpose] rw [← circ_transpose u, ← circ_transpose v, Matrix.transpose_transpose] have hR_mix1 : ∀ u v : ZMod n → ℤ, (circ u * revMat shift) * ((circ v)ᵀ * revMat shift)ᵀ = circ u * circ v := by intro u v rw [circ_transpose v, circ_rev_mul_transpose, ← circ_transpose v, Matrix.transpose_transpose] have hR_mix2 : ∀ u v : ZMod n → ℤ, ((circ u)ᵀ * revMat shift) * (circ v * revMat shift)ᵀ = (circ u)ᵀ * (circ v)ᵀ := by intro u v rw [circ_transpose u, circ_rev_mul_transpose, ← circ_transpose u] have hAt : ∀ u v : ZMod n → ℤ, circ u * ((circ v)ᵀ * revMat shift)ᵀ = (circ v)ᵀ * revMat shift * (circ u)ᵀ := by intro u v rw [circ_transpose v] exact hA_eq u (fun k => v (-k)) fin_cases r <;> fin_cases s <;> simp only [Fin.sum_univ_four, gsBlock, Matrix.transpose_neg, Matrix.mul_neg, Matrix.neg_mul, neg_neg, ↓reduceIte] -- (0, 0) · rw [circ_rev_mul_transpose, circ_rev_mul_transpose, circ_rev_mul_transpose] -- (0, 1) · rw [hA_eq a b, hR_mix1 c d, hR_mix1 d c, circ_mul_comm d c]; abel -- (0, 2) · rw [hA_eq a c, hR_mix1 b d, hR_mix1 d b, circ_mul_comm d b]; abel -- (0, 3) · rw [hA_eq a d, hR_mix1 b c, hR_mix1 c b, circ_mul_comm c b]; abel -- (1, 0) · rw [← hA_eq a b, hR_mix2 d c, hR_mix2 c d, circ_transpose d, circ_transpose c, circ_mul_comm]; abel -- (1, 1) · rw [circ_rev_mul_transpose, hR_trans d d, hR_trans c c, circ_transpose_mul_self d, circ_transpose_mul_self c]; abel -- (1, 2) · rw [circ_rev_mul_transpose, hAt a d, hR_trans c b, circ_transpose c, circ_transpose b, circ_mul_comm]; abel -- (1, 3) · rw [circ_rev_mul_transpose, hAt a c, hR_trans d b, circ_transpose d, circ_transpose b, circ_mul_comm]; abel -- (2, 0) · rw [← hA_eq a c, hR_mix2 d b, hR_mix2 b d, circ_transpose d, circ_transpose b, circ_mul_comm]; abel -- (2, 1) · rw [circ_rev_mul_transpose, ← hAt a d, hR_trans b c, circ_transpose b, circ_transpose c, circ_mul_comm]; abel -- (2, 2) · rw [circ_rev_mul_transpose, hR_trans d d, hR_trans b b, circ_transpose_mul_self d, circ_transpose_mul_self b]; abel -- (2, 3) · rw [circ_rev_mul_transpose, hAt a b, hR_trans d c, circ_transpose d, circ_transpose c, circ_mul_comm]; abel -- (3, 0) · rw [← hA_eq a d, hR_mix2 c b, hR_mix2 b c, circ_transpose c, circ_transpose b, circ_mul_comm]; abel -- (3, 1) · rw [circ_rev_mul_transpose, ← hAt a c, hR_trans b d, circ_transpose b, circ_transpose d, circ_mul_comm]; abel -- (3, 2) · rw [circ_rev_mul_transpose, ← hAt a b, hR_trans c d, circ_transpose c, circ_transpose d, circ_mul_comm]; abel -- (3, 3) · rw [circ_rev_mul_transpose, hR_trans c c, hR_trans b b, circ_transpose_mul_self c, circ_transpose_mul_self b]; abel /-- **General Goethals–Seidel Gram Matrix Identity**: For ANY `n ≥ 1`, ANY shift `shift : ZMod n`, and ANY four integer sequences `a, b, c, d`, `(GS * GSᵀ) (r, i) (s, j) = if r = s then pcorr a (i - j) + pcorr b (i - j) + pcorr c (i - j) + pcorr d (i - j) else 0`. -/ theorem goethalsSeidel_mul_transpose (a b c d : ZMod n → ℤ) (shift : ZMod n) (r s : Fin 4) (i j : ZMod n) : (goethalsSeidelMat a b c d shift * (goethalsSeidelMat a b c d shift)ᵀ) (r, i) (s, j) = if r = s then pcorr a (i - j) + pcorr b (i - j) + pcorr c (i - j) + pcorr d (i - j) else 0 := by simp only [Matrix.mul_apply, Matrix.transpose_apply, goethalsSeidelMat, Matrix.of_apply] rw [Fintype.sum_prod_type] have hblock : ∀ k : Fin 4, ∑ t : ZMod n, gsBlock a b c d shift r k i t * gsBlock a b c d shift s k j t = (gsBlock a b c d shift r k * (gsBlock a b c d shift s k)ᵀ) i j := by intro k simp only [Matrix.mul_apply, Matrix.transpose_apply] simp_rw [hblock] have hsum_apply : (∑ k : Fin 4, (gsBlock a b c d shift r k * (gsBlock a b c d shift s k)ᵀ) i j) = (∑ k : Fin 4, gsBlock a b c d shift r k * (gsBlock a b c d shift s k)ᵀ) i j := by simp only [Matrix.sum_apply] rw [hsum_apply, gsBlock_mul_transpose_sum] split_ifs with hrs · simp only [Matrix.add_apply, circ_mul_transpose_apply] · simp /-- **General Goethals–Seidel Hadamard Matrix Theorem (1970)**: If `a, b, c, d : ZMod n → ℤ` are `±1`-valued and complementary (`pcorr a k + pcorr b k + pcorr c k + pcorr d k = 0` for every `k ≠ 0`), then `H = goethalsSeidelMat a b c d shift` is a `4n × 4n` Hadamard matrix: `H * Hᵀ = (4 * n : ℤ) • 1`. -/ theorem goethalsSeidel_hadamard {a b c d : ZMod n → ℤ} (shift : ZMod n) (ha : ∀ t, a t = 1 ∨ a t = -1) (hb : ∀ t, b t = 1 ∨ b t = -1) (hc : ∀ t, c t = 1 ∨ c t = -1) (hd : ∀ t, d t = 1 ∨ d t = -1) (hcomp : ∀ k : ZMod n, k ≠ 0 → pcorr a k + pcorr b k + pcorr c k + pcorr d k = 0) : let H := goethalsSeidelMat a b c d shift (∀ p q, H p q = 1 ∨ H p q = -1) ∧ H * Hᵀ = (4 * (n : ℤ)) • (1 : Matrix (Fin 4 × ZMod n) (Fin 4 × ZMod n) ℤ) := by intro H refine ⟨goethalsSeidelMat_entries_pm_one shift ha hb hc hd, ?_⟩ ext ⟨r, i⟩ ⟨s, j⟩ rw [goethalsSeidel_mul_transpose, Matrix.smul_apply, Matrix.one_apply, smul_eq_mul] by_cases hrs : r = s · subst hrs by_cases hij : i = j · subst hij simp only [↓reduceIte, sub_self, mul_one] rw [pcorr_zero_of_sign ha, pcorr_zero_of_sign hb, pcorr_zero_of_sign hc, pcorr_zero_of_sign hd] ring · have hne : i - j ≠ 0 := sub_ne_zero.mpr hij have hpair : (r, i) ≠ (r, j) := fun h => hij (congrArg Prod.snd h) simp only [↓reduceIte, hpair, mul_zero] exact hcomp (i - j) hne · have hpair : (r, i) ≠ (s, j) := fun h => hrs (congrArg Prod.fst h) simp [hrs, hpair] end GoethalsSeidel section Witness52 /-- The four length-13 sign sequences from `research/tasks/hadamard-52.json`, mapped to `ZMod 13 → ℤ`. -/ def w52a : ZMod 13 → ℤ := fun i => if (![true, true, false, true, false, true, false, false, false, false, false, false, false] : Fin 13 → Bool) ⟨i.val, ZMod.val_lt i⟩ then -1 else 1 def w52b : ZMod 13 → ℤ := fun i => if (![true, true, false, true, false, false, true, true, false, true, false, false, false] : Fin 13 → Bool) ⟨i.val, ZMod.val_lt i⟩ then -1 else 1 def w52c : ZMod 13 → ℤ := w52a def w52d : ZMod 13 → ℤ := fun i => if (![true, true, true, false, false, true, false, false, true, true, false, false, false] : Fin 13 → Bool) ⟨i.val, ZMod.val_lt i⟩ then -1 else 1 /-- **Instant Order-52 Hadamard Matrix Theorem** (default heartbeats, zero brute-force `52 × 52` check!): Applying the general Goethals–Seidel theorem + half-lag sufficiency to `w52a, w52b, w52c, w52d` gives a certified `52 × 52` Hadamard matrix (`H * Hᵀ = 52 • 1`). -/ theorem hadamard_52_goethalsSeidel : let H := goethalsSeidelMat w52a w52b w52c w52d (-1 : ZMod 13) (∀ p q, H p q = 1 ∨ H p q = -1) ∧ H * Hᵀ = (52 : ℤ) • (1 : Matrix (Fin 4 × ZMod 13) (Fin 4 × ZMod 13) ℤ) := by have ha : ∀ t : ZMod 13, w52a t = 1 ∨ w52a t = -1 := by decide have hb : ∀ t : ZMod 13, w52b t = 1 ∨ w52b t = -1 := by decide have hc : ∀ t : ZMod 13, w52c t = 1 ∨ w52c t = -1 := by decide have hd : ∀ t : ZMod 13, w52d t = 1 ∨ w52d t = -1 := by decide have hhalf : ∀ k : ℕ, 0 < k → k ≤ 13 / 2 → pcorr w52a (k : ZMod 13) + pcorr w52b (k : ZMod 13) + pcorr w52c (k : ZMod 13) + pcorr w52d (k : ZMod 13) = 0 := by intro k hk1 hk2 have hfin : ∀ j : Fin 6, pcorr w52a ((j.val + 1 : ℕ) : ZMod 13) + pcorr w52b ((j.val + 1 : ℕ) : ZMod 13) + pcorr w52c ((j.val + 1 : ℕ) : ZMod 13) + pcorr w52d ((j.val + 1 : ℕ) : ZMod 13) = 0 := by decide have := hfin ⟨k - 1, by omega⟩ have hsub : k - 1 + 1 = k := by omega simpa [hsub] using this have hcomp := complementary_of_half hhalf have hmain := goethalsSeidel_hadamard (-1 : ZMod 13) ha hb hc hd hcomp simpa using hmain end Witness52 #print axioms complementary_sum_of_four_squares #print axioms complementary_of_half #print axioms goethalsSeidel_mul_transpose #print axioms goethalsSeidel_hadamard #print axioms hadamard_52_goethalsSeidel end AutoDiscovery.GoethalsSeidelHadamard