import Mathlib /-! # L4 of `research/lemmaV-parseval.js`: `∑_{a ≠ 0 mod e} F_H(a/e) = h(e − h)` for `H = Qe + h` Job #28 (formalize). L4 is a finite identity that serves the mean-square Lemma V (PROVEN in the repo), whose route above it is CLOSED (`research/OUTCOMES.md`, 2026-08-18). A Lean proof fixes the rung of this identity only. * `pairCount e H = #{(m, n) ∈ [1, H]² : e ∣ m − n}`. `pairCount_formula`: for `H = Q e + h`, `h < e`, it equals `e Q² + 2 h Q + h`. The proof is by induction on `H`, using `D(H+1) = D(H) + 2⌊H/e⌋ + 1`. `class_form` records the brief's `h(Q+1)² + (e−h)Q²`. * `F H t = ‖∑_{m=1}^{H} e(m t)‖²` with `e(t) = exp(2πit)`. `sum_F_eq_pairCount`: `∑_{a=0}^{e−1} F_H(a/e) = e · pairCount e H`. The proof expands `|S|² = S · conj S`, swaps the sums, and uses orthogonality `∑_{a (e : ℤ) ∣ (p.1 : ℤ) - p.2)).card /-- The same count as a double sum of indicators. -/ def pairSum (e H : ℕ) : ℕ := ∑ m ∈ Icc 1 H, ∑ n ∈ Icc 1 H, ind e m n theorem pairCount_eq_pairSum (e H : ℕ) : pairCount e H = pairSum e H := by unfold pairCount pairSum ind rw [Finset.card_filter, Finset.sum_product] lemma ind_comm (e m n : ℕ) : ind e m n = ind e n m := by unfold ind exact if_congr dvd_sub_comm rfl rfl lemma ind_self (e m : ℕ) : ind e m m = 1 := by simp [ind] lemma count_dvd (e H : ℕ) : ∑ k ∈ Icc 1 H, (if e ∣ k then 1 else 0) = H / e := by induction H with | zero => simp | succ k ih => rw [Finset.sum_Icc_succ_top (by omega), ih, Nat.succ_div] /-- `#{n ∈ [1, H] : e ∣ (H + 1) − n} = ⌊H/e⌋`, by the reflection `n ↦ H + 1 − n`. -/ lemma count_shift (e H : ℕ) : ∑ n ∈ Icc 1 H, ind e (H + 1) n = H / e := by rw [← count_dvd e H] refine Finset.sum_nbij' (fun n => H + 1 - n) (fun k => H + 1 - k) ?_ ?_ ?_ ?_ ?_ · intro n hn simp only [Finset.mem_Icc] at hn ⊢ omega · intro k hk simp only [Finset.mem_Icc] at hk ⊢ omega · intro n hn simp only [Finset.mem_Icc] at hn omega · intro k hk simp only [Finset.mem_Icc] at hk omega · intro n hn simp only [Finset.mem_Icc] at hn unfold ind have h : ((H + 1 : ℕ) : ℤ) - (n : ℤ) = ((H + 1 - n : ℕ) : ℤ) := by omega exact if_congr (by rw [h, Int.natCast_dvd_natCast]) rfl rfl lemma pairSum_succ (e H : ℕ) : pairSum e (H + 1) = pairSum e H + 2 * (H / e) + 1 := by unfold pairSum rw [Finset.sum_Icc_succ_top (by omega : 1 ≤ H + 1)] have hin : ∀ m, ∑ n ∈ Icc 1 (H + 1), ind e m n = ∑ n ∈ Icc 1 H, ind e m n + ind e m (H + 1) := fun m => Finset.sum_Icc_succ_top (by omega) _ simp only [hin, Finset.sum_add_distrib, ind_self] have h1 : ∑ m ∈ Icc 1 H, ind e m (H + 1) = H / e := by rw [← count_shift e H] exact Finset.sum_congr rfl fun m _ => ind_comm e m (H + 1) rw [h1, count_shift] ring /-- The closed form `e Q² + 2 h Q + h` with `Q = ⌊H/e⌋`, `h = H mod e`. -/ def closedForm (e H : ℕ) : ℕ := e * (H / e) ^ 2 + 2 * (H % e) * (H / e) + H % e lemma closedForm_succ {e : ℕ} (he : 0 < e) (H : ℕ) : closedForm e (H + 1) = closedForm e H + 2 * (H / e) + 1 := by unfold closedForm have hdm : H % e + e * (H / e) = H := Nat.mod_add_div H e have hlt : H % e < e := Nat.mod_lt H he generalize hQ : H / e = Q at hdm ⊢ generalize hr : H % e = r at hdm hlt ⊢ rcases Nat.lt_or_ge (r + 1) e with h | h · obtain ⟨h1, h2⟩ := (Nat.div_mod_unique he).2 ⟨(by linarith : r + 1 + e * Q = H + 1), h⟩ rw [h1, h2] ring · have he' : e = r + 1 := by omega obtain ⟨h1, h2⟩ := (Nat.div_mod_unique he).2 ⟨(by linarith : 0 + e * (Q + 1) = H + 1), he⟩ rw [h1, h2, he'] ring theorem pairSum_eq_closedForm {e : ℕ} (he : 0 < e) (H : ℕ) : pairSum e H = closedForm e H := by induction H with | zero => simp [pairSum, closedForm] | succ k ih => rw [pairSum_succ, ih, closedForm_succ he] /-- The counting identity: for `H = Q e + h` with `h < e`, the number of pairs is `e Q² + 2 h Q + h`. -/ theorem pairCount_formula {e H Q h : ℕ} (he : 0 < e) (hH : H = Q * e + h) (hh : h < e) : pairCount e H = e * Q ^ 2 + 2 * h * Q + h := by obtain ⟨hQ, hr⟩ := (Nat.div_mod_unique he).2 ⟨(by rw [hH]; ring : h + e * Q = H), hh⟩ rw [pairCount_eq_pairSum, pairSum_eq_closedForm he, closedForm, hQ, hr] /-- The residue-class form: `h` classes of size `Q + 1` and `e − h` of size `Q`. -/ theorem class_form {e Q h : ℕ} (hh : h ≤ e) : h * (Q + 1) ^ 2 + (e - h) * Q ^ 2 = e * Q ^ 2 + 2 * h * Q + h := by obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hh rw [Nat.add_sub_cancel_left] ring /-! ## The complex sum -/ /-- `e(t) = exp(2πit)`. -/ noncomputable def ee (t : ℝ) : ℂ := Complex.exp (2 * π * I * t) /-- `F_H(t) = |∑_{m=1}^{H} e(m t)|²`. -/ noncomputable def F (H : ℕ) (t : ℝ) : ℝ := ‖∑ m ∈ Icc 1 H, ee (m * t)‖ ^ 2 lemma norm_ee (t : ℝ) : ‖ee t‖ = 1 := by unfold ee rw [show 2 * (π : ℂ) * I * (t : ℂ) = ((2 * π * t : ℝ) : ℂ) * I by push_cast; ring] exact Complex.norm_exp_ofReal_mul_I _ lemma F_nonneg (H : ℕ) (t : ℝ) : 0 ≤ F H t := by unfold F positivity lemma ee_mul_conj (x y : ℝ) : ee x * conj (ee y) = ee (x - y) := by unfold ee rw [← Complex.exp_conj, ← Complex.exp_add] congr 1 simp only [map_mul, map_ofNat, Complex.conj_ofReal, Complex.conj_I] push_cast ring lemma ee_int_mul (e : ℕ) (k : ℤ) (a : ℕ) : ee ((k : ℝ) * a / e) = (Complex.exp (2 * π * I / e) ^ k) ^ a := by unfold ee rw [← zpow_natCast, ← zpow_mul, ← Complex.exp_int_mul] congr 1 push_cast ring /-- Orthogonality: `∑_{a hk ((hζ.zpow_eq_one_iff_dvd k).1 h) have hpow : (Complex.exp (2 * π * I / e) ^ k) ^ e = 1 := by rw [← zpow_natCast, ← zpow_mul, mul_comm k (e : ℤ), zpow_mul, zpow_natCast, hζ.pow_eq_one, one_zpow] rw [geom_sum_eq hne, hpow, sub_self, zero_div] lemma F_eq (H : ℕ) (t : ℝ) : (F H t : ℂ) = ∑ m ∈ Icc 1 H, ∑ n ∈ Icc 1 H, ee (((m : ℝ) - n) * t) := by unfold F rw [Complex.ofReal_pow, ← Complex.mul_conj', map_sum, Finset.sum_mul_sum] refine Finset.sum_congr rfl fun m _ => Finset.sum_congr rfl fun n _ => ?_ rw [ee_mul_conj] congr 1 ring /-- `∑_{a=0}^{e−1} F_H(a/e) = e · #{(m, n) ∈ [1, H]² : e ∣ m − n}`. -/ theorem sum_F_eq_pairCount {e : ℕ} (he : 0 < e) (H : ℕ) : ∑ a ∈ range e, F H ((a : ℝ) / e) = e * pairCount e H := by apply Complex.ofReal_injective push_cast rw [pairCount_eq_pairSum] unfold pairSum push_cast simp_rw [Finset.mul_sum, F_eq] have hterm : ∀ a m n : ℕ, ee (((m : ℝ) - n) * ((a : ℝ) / e)) = (Complex.exp (2 * π * I / e) ^ ((m : ℤ) - n)) ^ a := by intro a m n rw [← ee_int_mul] congr 1 push_cast ring simp_rw [hterm] rw [Finset.sum_comm] refine Finset.sum_congr rfl fun m _ => ?_ rw [Finset.sum_comm] refine Finset.sum_congr rfl fun n _ => ?_ rw [orth he] unfold ind split_ifs <;> simp lemma F_zero (H : ℕ) : F H 0 = (H : ℝ) ^ 2 := by unfold F ee simp /-- **L4.** For `H = Q e + h` with `h < e`: `∑_{a=1}^{e−1} F_H(a/e) = h(e − h)`. -/ theorem sum_F_nonzero {e H Q h : ℕ} (he : 0 < e) (hH : H = Q * e + h) (hh : h < e) : ∑ a ∈ Ico 1 e, F H ((a : ℝ) / e) = (h : ℝ) * ((e : ℝ) - h) := by have hfull := sum_F_eq_pairCount he H have hsplit : range e = insert 0 (Ico 1 e) := by ext a simp only [Finset.mem_range, Finset.mem_insert, Finset.mem_Ico] omega rw [hsplit, Finset.sum_insert (by simp), pairCount_formula he hH hh] at hfull rw [Nat.cast_zero, zero_div, F_zero] at hfull subst hH push_cast at hfull linear_combination hfull /-! ## The consequences (V1), (V2), and the equality clause -/ /-- `Ψ(g, δ, H) = ∑_{a=1}^{g−1} F_H(a/g) e(−aδ/g)`. -/ noncomputable def Psi (g : ℕ) (δ : ℝ) (H : ℕ) : ℂ := ∑ a ∈ Ico 1 g, (F H ((a : ℝ) / g) : ℂ) * ee (-(a : ℝ) * δ / g) /-- (V2): `‖Ψ(g, δ, H)‖ ≤ h(g − h)` for `H = Q g + h`, `h < g`. -/ theorem norm_Psi_le {g H Q h : ℕ} (hg : 0 < g) (hH : H = Q * g + h) (hh : h < g) (δ : ℝ) : ‖Psi g δ H‖ ≤ (h : ℝ) * ((g : ℝ) - h) := by unfold Psi refine (norm_sum_le _ _).trans (le_of_eq ?_) rw [← sum_F_nonzero hg hH hh] refine Finset.sum_congr rfl fun a _ => ?_ rw [norm_mul, norm_ee, mul_one, Complex.norm_real, Real.norm_of_nonneg (F_nonneg _ _)] /-- (V1): `Ψ(g, δ, H) = 0` when `g ∣ H`. -/ theorem Psi_eq_zero_of_dvd {g H : ℕ} (hg : 0 < g) (hdvd : g ∣ H) (δ : ℝ) : Psi g δ H = 0 := by obtain ⟨Q, rfl⟩ := hdvd have h := norm_Psi_le hg (by ring : g * Q = Q * g + 0) hg δ simpa using h lemma F_one (t : ℝ) : F 1 t = 1 := by unfold F simp [norm_ee] /-- The script's "(V2) with equality iff delta = 0" fails: at `g = 2, H = 1` (so `h = 1`) the bound `h(g − h) = 1` is attained at `δ = 1` as well as at `δ = 0`. -/ theorem V2_equality_off_zero : ‖Psi 2 1 1‖ = ((1 : ℕ) : ℝ) * (((2 : ℕ) : ℝ) - ((1 : ℕ) : ℝ)) ∧ ‖Psi 2 1 1‖ = ‖Psi 2 0 1‖ := by have key : ∀ δ : ℝ, ‖Psi 2 δ 1‖ = 1 := by intro δ unfold Psi rw [show Finset.Ico 1 2 = {1} by decide, Finset.sum_singleton, F_one] simp [norm_ee] exact ⟨by rw [key]; norm_num, by rw [key, key]⟩ /-! ## The equality cases of (V2) away from `δ ≡ 0` -/ lemma sum_Icc_pow_eq_zero {x : ℂ} {H : ℕ} (hx : x ≠ 1) (hH : x ^ H = 1) : ∑ m ∈ Icc 1 H, x ^ m = 0 := by have hre : ∑ m ∈ Icc 1 H, x ^ m = ∑ i ∈ range H, x ^ (i + 1) := by refine Finset.sum_nbij' (fun m => m - 1) (fun i => i + 1) ?_ ?_ ?_ ?_ ?_ · intro m hm simp only [Finset.mem_Icc, Finset.mem_range] at hm ⊢ omega · intro i hi simp only [Finset.mem_Icc, Finset.mem_range] at hi ⊢ omega · intro m hm simp only [Finset.mem_Icc] at hm omega · intro i hi simp only [Finset.mem_range] at hi omega · intro m hm simp only [Finset.mem_Icc] at hm congr 1 omega rw [hre] simp_rw [pow_succ, ← Finset.sum_mul] rw [geom_sum_eq hx, hH, sub_self, zero_div, zero_mul] /-- For `H = k(2j+1)`, `F_H(2b/(2k)) = 0` when `0 < b < k`. -/ lemma F_half_even {k j b : ℕ} (hb0 : 0 < b) (hbk : b < k) : F (k * (2 * j + 1)) (((2 * b : ℕ) : ℝ) / ((2 * k : ℕ) : ℝ)) = 0 := by have hk : 0 < k := by omega have hζ := Complex.isPrimitiveRoot_exp k hk.ne' have hterm : ∀ m : ℕ, ee ((m : ℝ) * (((2 * b : ℕ) : ℝ) / ((2 * k : ℕ) : ℝ))) = (Complex.exp (2 * π * I / k) ^ b) ^ m := by intro m rw [show (m : ℝ) * (((2 * b : ℕ) : ℝ) / ((2 * k : ℕ) : ℝ)) = ((b : ℤ) : ℝ) * m / k by push_cast rw [mul_div_mul_left _ _ (by norm_num : (2 : ℝ) ≠ 0)] ring] rw [ee_int_mul, zpow_natCast] unfold F simp_rw [hterm] have hx : Complex.exp (2 * π * I / k) ^ b ≠ 1 := by rw [Ne, hζ.pow_eq_one_iff_dvd] exact Nat.not_dvd_of_pos_of_lt hb0 hbk have hH : (Complex.exp (2 * π * I / k) ^ b) ^ (k * (2 * j + 1)) = 1 := by rw [← pow_mul, show b * (k * (2 * j + 1)) = k * (b * (2 * j + 1)) by ring, pow_mul, hζ.pow_eq_one, one_pow] rw [sum_Icc_pow_eq_zero hx hH, norm_zero] norm_num /-- At odd `a`, `e(−a k/(2k)) = −1`. -/ lemma ee_half_odd {k a : ℕ} (hk : 0 < k) (ha : Odd a) : ee (-(a : ℝ) * (k : ℝ) / ((2 * k : ℕ) : ℝ)) = -1 := by have hkR : (k : ℝ) ≠ 0 := by exact_mod_cast hk.ne' rw [show -(a : ℝ) * (k : ℝ) / ((2 * k : ℕ) : ℝ) = ((-(a : ℤ) : ℤ) : ℝ) * ((1 : ℕ) : ℝ) / ((2 : ℕ) : ℝ) by push_cast field_simp] rw [ee_int_mul, pow_one, show 2 * (π : ℂ) * I / ((2 : ℕ) : ℂ) = π * I by push_cast; ring, Complex.exp_pi_mul_I] have hodd : Odd (a : ℤ) := ha.natCast rw [zpow_neg, hodd.neg_one_zpow] norm_num /-- The equality case of (V2) at `δ ≢ 0`: for `g = 2k` and `H = k(2j+1)` (so `h = k`), `‖Ψ(g, k, H)‖ = h(g − h)`. -/ theorem norm_Psi_half {k j : ℕ} (hk : 0 < k) : ‖Psi (2 * k) k (k * (2 * j + 1))‖ = (k : ℝ) * (((2 * k : ℕ) : ℝ) - k) := by have hsum := sum_F_nonzero (e := 2 * k) (H := k * (2 * j + 1)) (Q := j) (h := k) (by omega) (by ring) (by omega) have hterm : ∀ a ∈ Ico 1 (2 * k), (F (k * (2 * j + 1)) ((a : ℝ) / ((2 * k : ℕ) : ℝ)) : ℂ) * ee (-(a : ℝ) * (k : ℝ) / ((2 * k : ℕ) : ℝ)) = -(F (k * (2 * j + 1)) ((a : ℝ) / ((2 * k : ℕ) : ℝ)) : ℂ) := by intro a ha rw [Finset.mem_Ico] at ha rcases Nat.even_or_odd a with ⟨b, rfl⟩ | hodd · have h0 := F_half_even (k := k) (j := j) (b := b) (by omega) (by omega) rw [show ((b + b : ℕ) : ℝ) = ((2 * b : ℕ) : ℝ) by push_cast; ring, h0] simp · rw [ee_half_odd hk hodd] ring unfold Psi rw [Finset.sum_congr rfl hterm, Finset.sum_neg_distrib, norm_neg, ← Complex.ofReal_sum, Complex.norm_real, Real.norm_of_nonneg (Finset.sum_nonneg fun a _ => F_nonneg _ _), hsum] end ParsevalL4