/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn-/module public import Mathlib.Algebra.BigOperators.Group.Finset.Basicpublic import Mathlib.Algebra.BigOperators.Ring.Finsetpublic import Mathlib.Algebra.BigOperators.Intervalspublic import Mathlib.Algebra.Order.Field.Basicpublic import Mathlib.Data.Int.GCDpublic import Mathlib.Data.Nat.ModEqpublic import Mathlib.Data.Nat.Prime.Intpublic import Mathlib.Data.Int.ModEqpublic import Mathlib.Data.ZMod.Basicpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.FieldSimppublic import Mathlib.Tactic.LinearCombinationpublic import Mathlib.Tactic.Positivitypublic import Mathlib.Tactic.Linarith /-!# Dedekind sums through the sawtooth function `DedekindSum.saw k n = ((n/k))` for `n : β€`, `0 < k` (`0` when `k β£ n`, otherwise`(n mod k)/k β 1/2`), and `DedekindSum.ded h k = s(h, k) = β_{r mod k} ((r/k)) ((hr/k))`. Mathlibhas neither (checked at the pinned commit). ## Main results * `DedekindSum.ded_congr`: `s(h, k)` depends only on `h mod k`.* `DedekindSum.ded_neg`: `s(βh, k) = βs(h, k)`.* `DedekindSum.ded_reciprocity`: `s(h, k) + s(k, h) = (hΒ² + kΒ² + 1)/(12hk) β 1/4` for coprime `h, k β₯ 1` (elementary proof: the permutation `r β¦ hr mod k` and a lattice-point count of `β βhr/kβΒ²`).* `DedekindSum.ded_inv`: `s(hβ»ΒΉ, k) = s(h, k)`.* `DedekindSum.saw_dist`: the distribution relation `β_{j<q} (((n + jm)/(qm))) = ((n/m))`.* `DedekindSum.ded_scale`: `s(qy, qm) = s(y, m)`.* `DedekindSum.petersson_knopp_prime`: for `q` prime, `β_{l<q} s(x + lm, qm) = (q + 1) s(x, m) β s(qx, m)`.* `DedekindSum.petersson_knopp_coprime_lifts`: the version for `q β€ m`, `qy β‘ x (mod m)`. ## References * S. R. Louboutin and M. Munsch, Canad. J. Math. 75 (2023), 1711β1743, arXiv:2205.01024 (its Conjecture 5.7 is the motivation).-/ @[expose] public section namespace DedekindSum open Finset /-- The sawtooth `((n/k))`. -/def saw (k : β) (n : β€) : β := if n % (k : β€) = 0 then 0 else ((n % (k : β€) : β€) : β) / k - 1 / 2 /-- The Dedekind sum `s(h,k) = Ξ£_{r=0}^{k-1} ((r/k)) ((hr/k))`. -/def ded (h : β€) (k : β) : β := β r β range k, saw k r * saw k (h * r) lemma saw_emod (k : β) (n : β€) : saw k (n % k) = saw k n := by unfold saw; rw [Int.emod_emod] lemma saw_congr {k : β} {n n' : β€} (h : n β‘ n' [ZMOD k]) : saw k n = saw k n' := by have e : n % k = n' % k := h rw [β saw_emod k n, e, saw_emod] /-- (a) `s(h,k)` depends only on `h mod k`. -/theorem ded_congr {h h' : β€} {k : β} (hh : h β‘ h' [ZMOD k]) : ded h k = ded h' k := by unfold ded refine sum_congr rfl fun r _ => ?_ rw [saw_congr (hh.mul_right (r : β€))] lemma saw_neg {k : β} (hk : 0 < k) (n : β€) : saw k (-n) = -saw k n := by unfold saw rw [Int.neg_emod] by_cases hd : (k : β€) β£ n Β· have : n % k = 0 := Int.emod_eq_zero_of_dvd hd simp [hd, this] Β· have h1 : n % (k : β€) β 0 := fun h0 => hd (Int.dvd_of_emod_eq_zero h0) have hk' : (0 : β€) < k := by exact_mod_cast hk have hlt : n % (k : β€) < k := Int.emod_lt_of_pos n hk' have h2 : ((k : β€).natAbs : β€) - n % (k : β€) β 0 := by rw [Int.natAbs_natCast]; omega simp only [hd, ite_false, h1, h2] rw [Int.natAbs_natCast] have hkq : (k : β) β 0 := by exact_mod_cast hk.ne' push_cast field_simp ring /-- (b) `s(βh,k) = βs(h,k)`. -/theorem ded_neg (h : β€) (k : β) : ded (-h) k = -ded h k := by rcases Nat.eq_zero_or_pos k with rfl | hk Β· simp [ded] unfold ded rw [β sum_neg_distrib] refine sum_congr rfl fun r _ => ?_ rw [neg_mul, saw_neg hk, mul_neg] /-! ### Two-term reciprocity (and the inverse symmetry of (F2)) -/ /-- Multiplication by a unit permutes the residues `0, β¦, k-1`. -/lemma perm_sum {h k : β} (hc : Nat.Coprime h k) (F : β β β) : β r β range k, F (h * r % k) = β r β range k, F r := by rcases Nat.eq_zero_or_pos k with rfl | hk Β· simp have hinj : Set.InjOn (fun r => h * r % k) (range k) := by intro a ha b hb hab simp only [coe_range, Set.mem_Iio] at ha hb have h1 : h * a β‘ h * b [MOD k] := hab have h2 := Nat.ModEq.cancel_left_of_coprime (Nat.Coprime.symm hc) h1 unfold Nat.ModEq at h2 rwa [Nat.mod_eq_of_lt ha, Nat.mod_eq_of_lt hb] at h2 have himg : (range k).image (fun r => h * r % k) = range k := by apply eq_of_subset_of_card_le Β· intro x hx simp only [mem_image, mem_range] at hx β’ obtain β¨r, _, rflβ© := hx exact Nat.mod_lt _ hk Β· rw [card_image_of_injOn hinj] calc β r β range k, F (h * r % k) = β x β (range k).image (fun r => h * r % k), F x := (sum_image hinj).symm _ = _ := by rw [himg] /-- **(F2)** `s(hβ»ΒΉ,k) = s(h,k)`: if `h h' β‘ 1 (mod k)` then `s(h',k) = s(h,k)`. -/theorem ded_inv {h h' k : β} (hinv : h * h' β‘ 1 [MOD k]) : ded h' k = ded h k := by have hc : Nat.Coprime h k := Nat.coprime_of_mul_modEq_one h' hinv have hmod : β n : β, saw k ((n % k : β) : β€) = saw k (n : β€) := fun n => by rw [Int.natCast_mod, saw_emod] unfold ded rw [β perm_sum hc (fun r => saw k (r : β€) * saw k ((h' : β€) * (r : β€)))] refine sum_congr rfl fun r _ => ?_ have e1 : saw k ((h * r % k : β) : β€) = saw k ((h : β€) * r) := by rw [hmod, Nat.cast_mul] have e2 : saw k ((h' : β€) * ((h * r % k : β) : β€)) = saw k (r : β€) := by apply saw_congr rw [β Nat.cast_mul, Int.natCast_modEq_iff] calc h' * (h * r % k) β‘ h' * (h * r) [MOD k] := Nat.ModEq.mul_left _ (Nat.mod_modEq _ _) _ = (h * h') * r := by ring _ β‘ 1 * r [MOD k] := Nat.ModEq.mul_right _ hinv _ = r := one_mul r rw [e1, e2, mul_comm] lemma sum_id (k : β) : β r β range k, (r : β) = k * (k - 1) / 2 := by induction k with | zero => simp | succ n ih => rw [sum_range_succ, ih]; push_cast; ring lemma sum_sq (k : β) : β r β range k, (r : β) ^ 2 = (k - 1) * k * (2 * k - 1) / 6 := by induction k with | zero => simp | succ n ih => rw [sum_range_succ, ih]; push_cast; ring lemma sum_odd (n : β) : β i β Ico 1 (n + 1), (2 * (i : β) - 1) = (n : β) ^ 2 := by induction n with | zero => simp | succ n ih => rw [sum_Ico_succ_top (by omega), ih]; push_cast; ring lemma rho_cast (h k r : β) : ((h * r % k : β) : β) = h * r - k * ((h * r / k : β) : β) := by have := congrArg (Nat.cast : β β β) (Nat.div_add_mod (h * r) k) push_cast at this linarith /-- `P h k = Ξ£ r βhr/kβ`, `Q h k = Ξ£ βhr/kβΒ²`, `M h k = Ξ£ βhr/kβ` (r over `range k`). -/def P (h k : β) : β := β r β range k, (r : β) * ((h * r / k : β) : β)def Q (h k : β) : β := β r β range k, ((h * r / k : β) : β) ^ 2def M (h k : β) : β := β r β range k, ((h * r / k : β) : β) /-- Ξ£ (hr mod k) = Ξ£ r. -/lemma eqn1 {h k : β} (hc : Nat.Coprime h k) : (h : β) * (k * (k - 1) / 2) - k * M h k = k * (k - 1) / 2 := by have := perm_sum hc (fun x => (x : β)) simp only [rho_cast] at this rw [sum_sub_distrib, β mul_sum, sum_id, β mul_sum] at this unfold M linarith /-- Ξ£ (hr mod k)Β² = Ξ£ rΒ². -/lemma eqn2 {h k : β} (hc : Nat.Coprime h k) : (h : β) ^ 2 * ((k - 1) * k * (2 * k - 1) / 6) - 2 * h * k * P h k + k ^ 2 * Q h k = (k - 1) * k * (2 * k - 1) / 6 := by have := perm_sum hc (fun x => (x : β) ^ 2) simp only [rho_cast] at this have e : β r β range k, ((h : β) * r - k * ((h * r / k : β) : β)) ^ 2 = (h : β) ^ 2 * (r : β) ^ 2 - 2 * h * k * ((r : β) * ((h * r / k : β) : β)) + (k : β) ^ 2 * ((h * r / k : β) : β) ^ 2 := fun r _ => by ring rw [sum_congr rfl e, sum_add_distrib, sum_sub_distrib, β mul_sum, β mul_sum, β mul_sum, sum_sq] at this unfold P Q linarith /-- `s(h,k)` through `P` and `M`. -/lemma ded_formula {h k : β} (hc : Nat.Coprime h k) (hk : 0 < k) : ded h k = (h : β) * ((k - 1) * k * (2 * k - 1) / 6) / k ^ 2 - P h k / k - (k * (k - 1) / 2) / (2 * k) - h * (k * (k - 1) / 2) / (2 * k) + M h k / 2 + k / 4 - 1 / 4 := by have hkq : (k : β) β 0 := by exact_mod_cast hk.ne' -- termwise have term : β r β range k, saw k (r : β€) * saw k ((h : β€) * (r : β€)) = ((r : β) / k - 1 / 2) * (((h * r % k : β) : β) / k - 1 / 2) - (if r = 0 then 1 / 4 else 0) := by intro r hr rw [mem_range] at hr have e : ((h : β€) * (r : β€)) % (k : β€) = ((h * r % k : β) : β€) := by rw [Int.natCast_mod, Nat.cast_mul] rcases Nat.eq_zero_or_pos r with rfl | hr0 Β· simp [saw]; norm_num Β· have er : (r : β€) % (k : β€) = (r : β€) := by rw [β Int.natCast_mod, Nat.mod_eq_of_lt hr] have hne : h * r % k β 0 := by intro h0 have hd : k β£ h * r := Nat.dvd_of_mod_eq_zero h0 have := Nat.eq_zero_of_dvd_of_lt ((Nat.Coprime.symm hc).dvd_of_dvd_mul_left hd) hr omega have hr0' : r β 0 := by omega unfold saw rw [e, er] simp only [Nat.cast_eq_zero, hr0', hne, ite_false, Int.cast_natCast] ring unfold ded rw [sum_congr rfl term, sum_sub_distrib, sum_ite_eq' (range k) 0, ite_eq_left (mem_range.2 hk)] simp only [rho_cast] have e2 : β r β range k, ((r : β) / k - 1 / 2) * (((h : β) * r - k * ((h * r / k : β) : β)) / k - 1 / 2) = (h : β) / k ^ 2 * (r : β) ^ 2 - 1 / k * ((r : β) * ((h * r / k : β) : β)) - (1 / (2 * k) + h / (2 * k)) * (r : β) + 1 / 2 * ((h * r / k : β) : β) + 1 / 4 := by intro r _; field_simp; ring rw [sum_congr rfl e2, sum_add_distrib, sum_add_distrib, sum_sub_distrib, sum_sub_distrib, β mul_sum, β mul_sum, β mul_sum, β mul_sum, sum_id, sum_sq, sum_const, card_range] unfold P M simp only [nsmul_eq_mul] field_simp ring /-- For `r < k`: `βhr/kβΒ² = Ξ£_{1 β€ i < h, ki β€ hr} (2i β 1)`. -/lemma sq_as_sum {h k r : β} (hh : 0 < h) (hk : 0 < k) (hr : r < k) : ((h * r / k : β) : β) ^ 2 = β i β Ico 1 h, if k * i β€ h * r then (2 * (i : β) - 1) else 0 := by rw [β sum_filter] have hΞΌ : h * r / k < h := by rw [Nat.div_lt_iff_lt_mul hk] nlinarith have hf : (Ico 1 h).filter (fun i => k * i β€ h * r) = Ico 1 (h * r / k + 1) := by ext i simp only [mem_filter, mem_Ico] have hiff : k * i β€ h * r β i β€ h * r / k := by rw [Nat.le_div_iff_mul_le hk, Nat.mul_comm i k] rw [hiff] omega rw [hf, sum_odd] /-- For `1 β€ i < h`: `#{r < k : hr β€ ki} = βki/hβ + 1`. -/lemma count_as_sum {h k i : β} (hh : 0 < h) (hk : 0 < k) (hi : i < h) : ((k * i / h : β) : β) + 1 = β r β range k, if h * r β€ k * i then (1 : β) else 0 := by rw [sum_boole] have hΞ½ : k * i / h < k := by rw [Nat.div_lt_iff_lt_mul hh] nlinarith have hf : (range k).filter (fun r => h * r β€ k * i) = range (k * i / h + 1) := by ext r simp only [mem_filter, mem_range] have hiff : h * r β€ k * i β r β€ k * i / h := by rw [Nat.le_div_iff_mul_le hh, Nat.mul_comm r h] rw [hiff] omega rw [hf, card_range] push_cast ring /-- Lattice-point double count: `Q h k + 2 P k h β M k h + (hβ1)Β² = k (hβ1)Β²`. -/lemma eqn3 {h k : β} (hc : Nat.Coprime h k) (hh : 0 < h) (hk : 0 < k) : Q h k + 2 * P k h - M k h + ((h : β) - 1) ^ 2 = k * ((h : β) - 1) ^ 2 := by -- left block have hQ : Q h k = β r β range k, β i β Ico 1 h, (if k * i β€ h * r then (2 * (i : β) - 1) else 0) := by unfold Q exact sum_congr rfl fun r hr => sq_as_sum hh hk (mem_range.1 hr) -- right block have hR : 2 * P k h - M k h + ((h : β) - 1) ^ 2 = β i β Ico 1 h, β r β range k, (if h * r β€ k * i then (2 * (i : β) - 1) else 0) := by have e1 : β i β Ico 1 h, β r β range k, (if h * r β€ k * i then (2 * (i : β) - 1) else 0) = (2 * (i : β) - 1) * (((k * i / h : β) : β) + 1) := by intro i hi rw [count_as_sum hh hk (mem_Ico.1 hi).2, mul_sum] refine sum_congr rfl fun r _ => ?_ split_ifs <;> ring rw [sum_congr rfl e1] have e2 : β i β Ico 1 h, (2 * (i : β) - 1) * (((k * i / h : β) : β) + 1) = (2 * ((i : β) * ((k * i / h : β) : β)) - ((k * i / h : β) : β)) + (2 * (i : β) - 1) := fun i _ => by ring rw [sum_congr rfl e2, sum_add_distrib] have hodd : β i β Ico 1 h, (2 * (i : β) - 1) = ((h : β) - 1) ^ 2 := by obtain β¨n, rflβ© : β n, h = n + 1 := β¨h - 1, by omegaβ© rw [sum_odd]; push_cast; ring rw [hodd] have hshift : β i β range h, (2 * ((i : β) * ((k * i / h : β) : β)) - ((k * i / h : β) : β)) = β i β Ico 1 h, (2 * ((i : β) * ((k * i / h : β) : β)) - ((k * i / h : β) : β)) := by rw [range_eq_Ico, sum_eq_sum_Ico_succ_bot hh] simp rw [β hshift, sum_sub_distrib, β mul_sum] unfold P M ring -- each pair (r,i) lies in exactly one triangle have hpair : β r β range k, β i β Ico 1 h, (if k * i β€ h * r then (2 * (i : β) - 1) else 0) + (if h * r β€ k * i then (2 * (i : β) - 1) else 0) = 2 * (i : β) - 1 := by intro r hr i hi rw [mem_range] at hr rw [mem_Ico] at hi have hne : k * i β h * r := by intro heq have hd : k β£ h * r := β¨i, heq.symmβ© have h0 := Nat.eq_zero_of_dvd_of_lt ((Nat.Coprime.symm hc).dvd_of_dvd_mul_left hd) hr subst h0 have : 0 < k * i := Nat.mul_pos hk (by omega) omega rcases lt_or_gt_of_ne hne with hlt | hgt Β· rw [ite_eq_left (by omega), ite_eq_right (by omega)]; ring Β· rw [ite_eq_right (by omega), ite_eq_left (by omega)]; ring have htot : β r β range k, β i β Ico 1 h, (2 * (i : β) - 1) = k * ((h : β) - 1) ^ 2 := by obtain β¨n, rflβ© : β n, h = n + 1 := β¨h - 1, by omegaβ© rw [sum_const, card_range, sum_odd, nsmul_eq_mul] push_cast; ring rw [add_sub_assoc, add_assoc, hR, hQ, sum_comm (s := Ico 1 h), β sum_add_distrib, β htot] refine sum_congr rfl fun r hr => ?_ rw [β sum_add_distrib] exact sum_congr rfl fun i hi => hpair r hr i hi /-- (c) **Dedekind reciprocity.** For coprime positive integers `h, k`,`s(h,k) + s(k,h) = (hΒ² + kΒ² + 1)/(12hk) β 1/4`. -/theorem ded_reciprocity {h k : β} (hc : Nat.Coprime h k) (hh : 0 < h) (hk : 0 < k) : ded h k + ded k h = ((h : β) ^ 2 + k ^ 2 + 1) / (12 * h * k) - 1 / 4 := by have hhq : (h : β) β 0 := by exact_mod_cast hh.ne' have hkq : (k : β) β 0 := by exact_mod_cast hk.ne' have E1 := eqn1 hc have E2 := eqn2 hc have E1p := eqn1 (Nat.Coprime.symm hc) have E3 := eqn3 hc hh hk rw [ded_formula hc hk, ded_formula (Nat.Coprime.symm hc) hh] linear_combination (norm := skip) (-1 / (2 * (k : β))) * E1 + (1 / (2 * (h : β) * (k : β) ^ 2)) * E2 + (-1 / (2 * (h : β))) * E3 + ((1 - (h : β)) / (2 * (h : β) ^ 2)) * E1p field_simp ring /-! ### (d) Distribution relation (F3) -/ /-- `((qn/(qm))) = ((n/m))`. -/lemma saw_scale {q m : β} (hq : 0 < q) (hm : 0 < m) (n : β€) : saw (q * m) ((q : β€) * n) = saw m n := by have hq' : (0 : β€) < q := by exact_mod_cast hq have hqq : (q : β) β 0 := by exact_mod_cast hq.ne' have hmq : (m : β) β 0 := by exact_mod_cast hm.ne' unfold saw rw [Nat.cast_mul, Int.mul_emod_mul_of_pos _ _ hq'] by_cases h0 : n % (m : β€) = 0 Β· simp [h0] Β· have : (q : β€) * (n % m) β 0 := mul_ne_zero hq'.ne' h0 rw [ite_eq_right this, ite_eq_right h0] push_cast field_simp lemma saw_of_lt {k : β} {n : β€} (h0 : 0 β€ n) (hlt : n < k) : saw k n = (n : β) / k - 1 / 2 + (if n = 0 then 1 / 2 else 0) := by unfold saw rw [Int.emod_eq_of_lt h0 hlt] by_cases h : n = 0 Β· subst h; simp Β· rw [ite_eq_right h, ite_eq_right h]; simp /-- A `q`-periodic function has the same sum over any `q` consecutive integers. -/lemma shift_sum (q : β) (H : β€ β β) (hH : β t, H (t + q) = H t) (a : β€) : β j β range q, H (a + j) = β j β range q, H j := by have step : β b : β€, β j β range q, H (b + 1 + j) = β j β range q, H (b + j) := by intro b have h1 := sum_range_succ (fun j : β => H (b + j)) q have h2 := sum_range_succ' (fun j : β => H (b + j)) q simp only [Nat.cast_add, Nat.cast_one, Nat.cast_zero, add_zero] at h1 h2 have e : β j : β, b + 1 + (j : β€) = b + ((j : β€) + 1) := fun j => by ring simp only [e] rw [hH b] at h1 linarith induction a using Int.induction_on with | zero => simp | succ i ih => rw [step, ih] | pred i ih => rw [β step (-(i : β€) - 1), sub_add_cancel, ih] /-- **(F3)** Discrete distribution relation: `Ξ£_{j<q} (((n + jm)/(qm))) = ((n/m))`. -/theorem saw_dist {q m : β} (hq : 0 < q) (hm : 0 < m) (n : β€) : β j β range q, saw (q * m) (n + j * m) = saw m n := by have hm' : (0 : β€) < m := by exact_mod_cast hm have hqq : (q : β) β 0 := by exact_mod_cast hq.ne' have hmq : (m : β) β 0 := by exact_mod_cast hm.ne' have hb0 : 0 β€ n % (m : β€) := Int.emod_nonneg _ hm'.ne' have hbm : n % (m : β€) < m := Int.emod_lt_of_pos _ hm' have hn : β j : β€, n + j * m = n % m + (n / m + j) * m := fun j => by have := Int.emod_add_mul_ediv n m linear_combination -this let H : β€ β β := fun t => saw (q * m) (n % m + t * m) have hH : β t, H (t + q) = H t := fun t => saw_congr (by rw [Int.modEq_iff_dvd]; push_cast; exact β¨-1, by ringβ©) have hterm : β t β range q, H t = (((n % m : β€) : β) / (q * m) - 1 / 2) + 1 / q * (t : β) + (if n % (m : β€) = 0 β§ t = 0 then 1 / 2 else 0) := by intro t ht rw [mem_range] at ht have htq : (t : β€) + 1 β€ q := by exact_mod_cast ht have ht0 : (0 : β€) β€ t := by positivity simp only [H] have hpos : 0 β€ n % (m : β€) + (t : β€) * m := by nlinarith have hlt : n % (m : β€) + (t : β€) * m < ((q * m : β) : β€) := by push_cast; nlinarith rw [saw_of_lt hpos hlt] by_cases hc : n % (m : β€) = 0 β§ t = 0 Β· obtain β¨h1, rflβ© := hc simp [h1] Β· have hz : n % (m : β€) + (t : β€) * m β 0 := by intro h apply hc have htm : (t : β€) * m = 0 := by nlinarith have : (t : β€) = 0 := by rcases mul_eq_zero.1 htm with h' | h' Β· exact h' Β· omega exact β¨by nlinarith, by exact_mod_cast thisβ© rw [ite_eq_right hz, ite_eq_right hc] push_cast field_simp ring calc β j β range q, saw (q * m) (n + j * m) = β j β range q, H (n / m + j) := by refine sum_congr rfl fun j _ => ?_ simp only [H] rw [hn] _ = β j β range q, H j := shift_sum q H hH _ _ = saw m n := by rw [sum_congr rfl hterm, sum_add_distrib, sum_add_distrib, β mul_sum, sum_id, sum_const, card_range, nsmul_eq_mul] by_cases hb : n % (m : β€) = 0 Β· simp only [hb, true_and] rw [sum_ite_eq' (range q) 0, ite_eq_left (mem_range.2 hq)] unfold saw rw [ite_eq_left hb] push_cast field_simp ring Β· simp only [hb, false_and, ite_false, sum_const_zero] unfold saw rw [ite_eq_right hb] field_simp ring /-! ### (d) Lemma 1: the prime case of the PeterssonβKnopp identity -/ lemma sum_range_mul (q m : β) (f : β β β) : β r β range (q * m), f r = β j β range q, β i β range m, f (j * m + i) := by induction q with | zero => simp | succ q ih => rw [add_mul, one_mul, sum_range_add, ih, sum_range_succ] /-- The inner sum over `l` in Lemma 1. -/lemma inner_value {q m : β} (hq : q.Prime) (hm : 0 < m) (x : β€) (r : β) : β l β range q, saw (q * m) ((x + l * m) * r) = saw m (x * r) + (if q β£ r then (q : β) * saw m (x * (r / q : β)) - saw m (x * r) else 0) := by have hq0 := hq.pos by_cases hd : q β£ r Β· rw [ite_eq_left hd] obtain β¨r', rflβ© := hd rw [Nat.mul_div_cancel_left r' hq0] have e : β l β range q, saw (q * m) ((x + l * m) * ((q * r' : β) : β€)) = saw m (x * r') := by intro l _ have : (x + l * m) * ((q * r' : β) : β€) = (q : β€) * (x * r' + ((l : β€) * r') * m) := by push_cast; ring rw [this, saw_scale hq0 hm] apply saw_congr rw [Int.modEq_iff_dvd] exact β¨-((l : β€) * r'), by ringβ© rw [sum_congr rfl e, sum_const, card_range, nsmul_eq_mul] ring Β· rw [ite_eq_right hd, add_zero] have hc : Nat.Coprime r q := Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hd) have e : β l β range q, saw (q * m) ((x + l * m) * r) = saw (q * m) (x * r + ((r * l % q : β) : β€) * m) := by intro l _ apply saw_congr rw [Int.modEq_iff_dvd] have h0 := Nat.mod_add_div (r * l) q have h' : ((r * l % q : β) : β€) = (r : β€) * l - q * ((r * l / q : β) : β€) := by have := congrArg (Nat.cast : β β β€) h0 simp only [Nat.cast_add, Nat.cast_mul] at this linarith refine β¨-((r * l / q : β) : β€), ?_β© rw [h', Nat.cast_mul] ring rw [sum_congr rfl e, perm_sum hc (fun j => saw (q * m) (x * r + (j : β€) * m))] exact saw_dist hq0 hm (x * r) /-- `Ξ£_{r<qm} ((r/(qm))) ((xr/m)) = s(x,m)` (split `r = jm + i`, then (F3)). -/lemma lift_sum {q m : β} (hq0 : 0 < q) (hm : 0 < m) (x : β€) : β r β range (q * m), saw (q * m) r * saw m (x * r) = β r β range m, saw m r * saw m (x * r) := by rw [sum_range_mul, sum_comm] refine sum_congr rfl fun i _ => ?_ have e : β j β range q, saw (q * m) ((j * m + i : β) : β€) * saw m (x * ((j * m + i : β) : β€)) = saw (q * m) ((i : β€) + (j : β€) * m) * saw m (x * i) := by intro j _ rw [show ((j * m + i : β) : β€) = (i : β€) + (j : β€) * m by push_cast; ring] congr 1 apply saw_congr rw [Int.modEq_iff_dvd] exact β¨-(x * j), by ringβ© rw [sum_congr rfl e, β sum_mul, saw_dist hq0 hm] /-- **(F4)**, the instance used in Corollary 1β²: `s(qy, qm) = s(y, m)`. -/theorem ded_scale {q m : β} (hq0 : 0 < q) (hm : 0 < m) (y : β€) : ded (q * y) (q * m) = ded y m := by unfold ded rw [β lift_sum hq0 hm y] refine sum_congr rfl fun r _ => ?_ rw [mul_assoc, saw_scale hq0 hm] /-- **Lemma 1** (prime case of PeterssonβKnopp): for a prime `q`, `m β₯ 1`, `x β β€`,`Ξ£_{l<q} s(x + lm, qm) = (q+1) s(x,m) β s(qx,m)`. -/theorem petersson_knopp_prime {q m : β} (hq : q.Prime) (hm : 0 < m) (x : β€) : β l β range q, ded (x + l * m) (q * m) = (q + 1) * ded x m - ded (q * x) m := by have hq0 := hq.pos have h1 := lift_sum hq0 hm x have h2 : β r β range (q * m), saw (q * m) r * (if q β£ r then (q : β) * saw m (x * (r / q : β)) - saw m (x * r) else 0) = q * β r β range m, saw m r * saw m (x * r) - β r β range m, saw m r * saw m (q * x * r) := by rw [show range (q * m) = range (m * q) by rw [Nat.mul_comm], sum_range_mul, mul_sum, β sum_sub_distrib] refine sum_congr rfl fun j _ => ?_ rw [sum_eq_single 0] Β· rw [add_zero, ite_eq_left (dvd_mul_left q j), Nat.mul_div_cancel j hq0, show ((j * q : β) : β€) = (q : β€) * j by push_cast; ring, saw_scale hq0 hm, show x * ((q : β€) * j) = q * x * j by ring] ring Β· intro i hi hi0 have hnd : Β¬ q β£ j * q + i := by intro hdv have := (Nat.dvd_add_right (dvd_mul_left q j)).1 hdv exact hi0 (Nat.eq_zero_of_dvd_of_lt this (mem_range.1 hi)) rw [ite_eq_right hnd, mul_zero] Β· intro h; exact absurd (mem_range.2 hq0) h unfold ded rw [sum_comm] simp_rw [β mul_sum] simp_rw [inner_value hq hm x, mul_add, sum_add_distrib] rw [h1, h2] ring /-- **Corollary 1β²** (coprime lifts). For a prime `q β€ m` and `y` with `q y β‘ x (mod m)`(i.e. `y = qβ»ΒΉx mod m`): the sum over the `l < q` with `q β€ x + lm` equals`(q+1) s(x,m) β s(qx,m) β s(qβ»ΒΉx,m)`. -/theorem petersson_knopp_coprime_lifts {q m : β} (hq : q.Prime) (hm : 0 < m) (hqm : Β¬ q β£ m) (x y : β€) (hy : (q : β€) * y β‘ x [ZMOD m]) : β l β range q, (if (q : β€) β£ x + l * m then 0 else ded (x + l * m) (q * m)) = (q + 1) * ded x m - ded (q * x) m - ded y m := by have hq0 := hq.pos have := Fact.mk hq have hmq : ((m : β€) : ZMod q) β 0 := by rw [Ne, ZMod.intCast_zmod_eq_zero_iff_dvd] exact fun h => hqm (Int.natCast_dvd_natCast.1 h) -- the unique excluded `l` set l0 := (-(x : ZMod q) * ((m : β€) : ZMod q)β»ΒΉ).val with hl0def have hl0 : l0 < q := ZMod.val_lt _ have hdl0 : (q : β€) β£ x + l0 * m := by rw [β ZMod.intCast_zmod_eq_zero_iff_dvd] push_cast rw [hl0def, ZMod.natCast_zmod_val] have hmq' : ((m : β) : ZMod q) β 0 := by exact_mod_cast hmq rw [mul_assoc, Int.cast_natCast, mul_comm _ ((m : β) : ZMod q), ZMod.coe_mul_inv_eq_one m (Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hqm))] ring have huniq : β l β range q, l β l0 β Β¬ (q : β€) β£ x + l * m := by intro l hl hne hdl have hd : (q : β€) β£ ((l : β€) - l0) * m := by have := dvd_sub hdl hdl0 convert this using 1 ring rcases (Nat.prime_iff_prime_int.mp hq).dvd_or_dvd hd with h | h Β· obtain β¨c, hcβ© := h have hl' : l < q := mem_range.1 hl have hcz : c = 0 := by rcases lt_trichotomy c 0 with h' | h' | h' Β· have : (q : β€) * c β€ -(q : β€) := by nlinarith omega Β· exact h' Β· have : (q : β€) * c β₯ (q : β€) := by nlinarith omega subst hcz omega Β· exact hqm (Int.natCast_dvd_natCast.1 h) have hsplit : β l β range q, ded (x + l * m) (q * m) = (if (q : β€) β£ x + l * m then 0 else ded (x + l * m) (q * m)) + (if (q : β€) β£ x + l * m then ded (x + l * m) (q * m) else 0) := by intro l _ split_ifs <;> simp have hkey : β l β range q, (if (q : β€) β£ x + l * m then ded (x + l * m) (q * m) else 0) = ded y m := by rw [sum_eq_single l0 (fun l hl hne => ite_eq_right (huniq l hl hne)) (fun h => absurd (mem_range.2 hl0) h), ite_eq_left hdl0] obtain β¨z, hzβ© := hdl0 rw [hz, ded_scale hq0 hm] apply ded_congr have h1 : (q : β€) * z β‘ (q : β€) * y [ZMOD m] := by rw [β hz] refine Int.ModEq.trans ?_ hy.symm rw [Int.modEq_iff_dvd] exact β¨-(l0 : β€), by ringβ© have hcop : Nat.Coprime m q := (Nat.Coprime.symm ((Nat.Prime.coprime_iff_not_dvd hq).2 hqm)) have h2 := Int.ModEq.cancel_left_div_gcd (by exact_mod_cast hm) h1 rwa [Int.gcd_natCast_natCast, hcop, Nat.cast_one, Int.ediv_one] at h2 have hPK := petersson_knopp_prime hq hm x rw [sum_congr rfl hsplit, sum_add_distrib, hkey] at hPK linarith end DedekindSum -- axiom audit