/-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.Analysis.Fourier.ZModpublic import Mathlib.Data.Finset.Powersetpublic import Mathlib.Algebra.Order.BigOperators.Group.Finsetpublic import Mathlib.Algebra.Ring.GeomSumpublic import Mathlib.Algebra.BigOperators.Group.Finset.Piecewisepublic import Mathlib.Basic.Complex.BigOperators /-!# Fourier amplitudes of subsets of a finite cyclic group For `A β ZMod N`, `Ahat A k = β a β A, stdAddChar (-(a * k))`, which equals Mathlib's `ZMod.dft`of the indicator (`Ahat_eq_dft`). This file collects the interface used by the Huddlinginequality: transposition, complementation, consecutive arcs and their geometric sums, unitfrequencies, and the threshold argument that reduces the inequality to a geometric selector. ## Main results * `Ahat_eq_dft`, `Ahat_tpose`, `Ahat_compl`, `norm_Ahat_le_card`.* `card_arc`, `Ahat_arc`, `Ahat_arc_geom`.* `sum_le_of_threshold`, `sum_lt_of_strict_threshold`.* `norm_Ahat_one_le_arc_of_geometric_selector`, `norm_Ahat_unit_le_arc_of_geometric_selector`. ## References * E. Amiot, *Discrete Fourier transform and Bach's good temperament*, Music Theory Online 15 (2009).* C. MarΓn, *Huddling, diatonic and pentatonic scales in Z12: Fourier maxima and convex-energy minima formalized in Lean 4*, Zenodo (2026), doi:10.5281/zenodo.23121103.-/ @[expose] public section namespace MusicTheory open Finset ZMod AddChar Complexopen scoped ComplexConjugate namespace CyclicHuddling /-- Every complex amplitude has a unit phase whose real projection is its norm. This turns a bound on all directional projections into a norm bound. -/theorem exists_phase_real_eq_norm (z : β) : β u : β, βuβ = 1 β§ (u * z).re = βzβ := by by_cases hz : z = 0 Β· refine β¨1, by simp, ?_β© simp [hz] Β· refine β¨conj z / (βzβ : β), ?_, ?_β© Β· simp [hz] Β· have hn : (βzβ : β) β 0 := by simp [hz] rw [div_mul_eq_mul_div, β Complex.normSq_eq_conj_mul_self, Complex.normSq_eq_norm_sq] simp [pow_two, hz] /-! The exchange step in the geometric Huddling proof is independent of roots of unity. -/ /-- A set containing all scores above a threshold maximizes their sum among sets of the same cardinality. This is the finite exchange argument needed after projecting the cyclic roots onto any chosen direction. -/theorem sum_le_of_threshold {Ξ± : Type*} [DecidableEq Ξ±] (score : Ξ± β β) (A B : Finset Ξ±) (c : β) (hcard : B.card = A.card) (hin : β x β A, c β€ score x) (hout : β x β A, score x β€ c) : (β x β B, score x) β€ β x β A, score x := by calc (β x β B, score x) = (β x β B β© A, score x) + β x β B \ A, score x := by rw [sum_inter_add_sum_sdiff] _ β€ (β x β A β© B, score x) + (B \ A).card β’ c := by rw [inter_comm] gcongr exact sum_le_card_nsmul _ _ _ (fun x hx => hout x (mem_sdiff.mp hx).2) _ = (β x β A β© B, score x) + (A \ B).card β’ c := by rw [card_sdiff_comm hcard] _ β€ (β x β A β© B, score x) + β x β A \ B, score x := by gcongr exact card_nsmul_le_sum _ _ _ (fun x hx => hin x (mem_sdiff.mp hx).1) _ = β x β A, score x := by rw [sum_inter_add_sum_sdiff] /-- Strict separation at the threshold makes the maximizer unique at its cardinality. The geometric equality analysis must account for boundary ties. -/theorem sum_lt_of_strict_threshold {Ξ± : Type*} [DecidableEq Ξ±] (score : Ξ± β β) (A B : Finset Ξ±) (c : β) (hcard : B.card = A.card) (hin : β x β A, c < score x) (hout : β x β A, score x β€ c) (hne : B β A) : (β x β B, score x) < β x β A, score x := by have hdiff : (A \ B).Nonempty := by rw [sdiff_nonempty] intro hsub exact hne (eq_of_subset_of_card_le hsub (by rw [hcard])).symm have hlow : (β x β B \ A, score x) β€ (B \ A).card β’ c := sum_le_card_nsmul _ _ _ (fun x hx => hout x (mem_sdiff.mp hx).2) have hhigh : (A \ B).card β’ c < β x β A \ B, score x := by simpa only [sum_const] using (Finset.sum_lt_sum_of_nonempty hdiff (fun x hx => hin x (mem_sdiff.mp hx).1)) calc (β x β B, score x) = (β x β B β© A, score x) + β x β B \ A, score x := by rw [sum_inter_add_sum_sdiff] _ β€ (β x β A β© B, score x) + (B \ A).card β’ c := by rw [inter_comm] simpa only [add_comm] using add_le_add_left hlow _ _ = (β x β A β© B, score x) + (A \ B).card β’ c := by rw [card_sdiff_comm hcard] _ < (β x β A β© B, score x) + β x β A \ B, score x := by simpa only [add_comm] using add_lt_add_left hhigh _ _ = β x β A, score x := by rw [sum_inter_add_sum_sdiff] variable {N : β} [NeZero N] /-- The unnormalized Fourier coefficient of a subset of `ZMod N`. -/noncomputable def Ahat (A : Finset (ZMod N)) (k : ZMod N) : β := β a β A, stdAddChar (-(a * k)) /-- The set-sum convention is Mathlib's discrete Fourier transform of its zero-one indicator. -/theorem Ahat_eq_dft (A : Finset (ZMod N)) (k : ZMod N) : Ahat A k = ZMod.dft (fun a => if a β A then (1 : β) else 0) k := by rw [Ahat, ZMod.dft_apply] simp only [smul_eq_mul, mul_ite, mul_one, mul_zero, Finset.sum_ite_mem, Finset.univ_inter] /-- Transpose a subset by one residue. -/def tpose (s : ZMod N) (A : Finset (ZMod N)) : Finset (ZMod N) := A.map β¨(Β· + s), add_left_injective sβ© /-- The frequency-zero coefficient is the number of selected residues. -/theorem Ahat_zero (A : Finset (ZMod N)) : Ahat A 0 = (A.card : β) := by simp [Ahat] /-- The elementary cardinality bound is valid at every frequency, including nonprimitive frequencies where the consecutive-arc extremal bound fails. -/theorem norm_Ahat_le_card (A : Finset (ZMod N)) (k : ZMod N) : βAhat A kβ β€ (A.card : β) := by calc βAhat A kβ β€ β a β A, β(stdAddChar (-(a * k)) : β)β := by simpa [Ahat] using norm_sum_le (s := A) (f := fun a : ZMod N => (stdAddChar (-(a * k)) : β)) _ = A.card := by simp [ZMod.stdAddChar_apply] /-- Translation changes only Fourier phase. -/theorem Ahat_tpose (s : ZMod N) (A : Finset (ZMod N)) (k : ZMod N) : Ahat (tpose s A) k = stdAddChar (-(s * k)) * Ahat A k := by rw [Ahat, Ahat, tpose, Finset.sum_map, Finset.mul_sum] refine Finset.sum_congr rfl fun a _ => ?_ simp only [Function.Embedding.coeFn_mk] rw [β map_add_eq_mul] congr 1 ring /-- Translation preserves every Fourier magnitude. -/theorem norm_tpose (s : ZMod N) (A : Finset (ZMod N)) (k : ZMod N) : βAhat (tpose s A) kβ = βAhat A kβ := by rw [Ahat_tpose, norm_mul, ZMod.stdAddChar_apply, Circle.norm_coe, one_mul] /-- Multiplication by a unit relabels a primitive-frequency coefficient as a frequency-one coefficient of the relabeled set. -/theorem Ahat_unit_frequency (A : Finset (ZMod N)) (k : ZMod N) (hk : IsUnit k) : Ahat A k = Ahat (A.image (Β· * k)) 1 := by rw [Ahat, Ahat, Finset.sum_image] Β· simp Β· intro a _ b _ hab exact hk.mul_left_injective hab omit [NeZero N] in/-- Relabeling by a unit preserves the set size. -/theorem card_image_mul_unit (A : Finset (ZMod N)) (k : ZMod N) (hk : IsUnit k) : (A.image (Β· * k)).card = A.card := by exact Finset.card_image_of_injective A hk.mul_left_injective omit [NeZero N] in/-- Translation preserves the number of selected residues. -/theorem card_tpose (s : ZMod N) (A : Finset (ZMod N)) : (tpose s A).card = A.card := by simp [tpose] /-- The complete character sum vanishes at nonzero frequency. -/theorem sum_character_eq_zero {k : ZMod N} (hk : k β 0) : (β a : ZMod N, stdAddChar (-(a * k))) = 0 := by have h := AddChar.sum_mulShift (R := ZMod N) (-k) (isPrimitive_stdAddChar N) rw [ite_eq_right (by simpa using hk), Nat.cast_zero] at h rw [β h] exact Finset.sum_congr rfl fun a _ => by rw [mul_neg, β neg_mul] /-- Away from zero frequency, complementation reverses the Fourier coefficient. -/theorem Ahat_compl {k : ZMod N} (hk : k β 0) (A : Finset (ZMod N)) : Ahat AαΆ k = -Ahat A k := by have hsplit : Ahat A k + Ahat AαΆ k = β a : ZMod N, stdAddChar (-(a * k)) := by rw [Ahat, Ahat, β Finset.sum_union (disjoint_compl_right)] rw [Finset.union_compl] rw [eq_neg_iff_add_eq_zero, add_comm, hsplit, sum_character_eq_zero hk] /-- The first `m` residues, viewed as a cyclic arc. -/def arc (m : β) : Finset (ZMod N) := (range m).image (fun j : β => (j : ZMod N)) omit [NeZero N] in/-- In the natural range, an arc contains exactly `m` residues. -/theorem card_arc (m : β) (hm : m β€ N) : (arc (N := N) m).card = m := by rw [arc, card_image_of_injOn] Β· exact card_range m Β· intro a ha b hb hab have haN : a < N := (mem_range.mp ha).trans_le hm have hbN : b < N := (mem_range.mp hb).trans_le hm have h := congrArg ZMod.val hab simpa only [ZMod.val_natCast_of_lt haN, ZMod.val_natCast_of_lt hbN] using h /-- An arc coefficient is a consecutive character sum. -/theorem Ahat_arc (m : β) (hm : m β€ N) (k : ZMod N) : Ahat (arc (N := N) m) k = β j β range m, stdAddChar (-((j : ZMod N) * k)) := by rw [Ahat, arc, sum_image] intro a ha b hb hab have haN : a < N := (mem_range.mp ha).trans_le hm have hbN : b < N := (mem_range.mp hb).trans_le hm have h := congrArg ZMod.val hab simpa only [ZMod.val_natCast_of_lt haN, ZMod.val_natCast_of_lt hbN] using h /-- The cyclic arc has a geometric-series Fourier coefficient. -/theorem Ahat_arc_geom (m : β) (hm : m β€ N) (k : ZMod N) : Ahat (arc (N := N) m) k = β j β range m, (stdAddChar (-k)) ^ j := by rw [Ahat_arc m hm] refine sum_congr rfl fun j _ => ?_ rw [β map_nsmul_eq_pow] congr 1 simp only [nsmul_eq_mul] ring /-- The geometric-series identity for an arc, without dividing by a possibly vanishing denominator at frequency zero. -/theorem Ahat_arc_mul_sub (m : β) (hm : m β€ N) (k : ZMod N) : Ahat (arc (N := N) m) k * (stdAddChar (-k) - 1) = (stdAddChar (-k)) ^ m - 1 := by rw [Ahat_arc_geom m hm] exact geom_sum_mul _ _ /-- The real projection of a Fourier coefficient is controlled by the finite top-score exchange lemma. A geometric description of the top-score sets of roots of unity is the remaining analytic step in Huddling. -/theorem re_amplitude_le_of_threshold (u : β) (k : ZMod N) (A B : Finset (ZMod N)) (c : β) (hcard : B.card = A.card) (hin : β a β A, c β€ (u * stdAddChar (-(a * k))).re) (hout : β a β A, (u * stdAddChar (-(a * k))).re β€ c) : (u * Ahat B k).re β€ (u * Ahat A k).re := by have h := sum_le_of_threshold (fun a : ZMod N => (u * stdAddChar (-(a * k))).re) A B c hcard hin hout simpa only [Ahat, Finset.mul_sum, Complex.re_sum] using h /-- Strictly separated projected scores give strict Fourier-projection inequality unless the chosen subset is the maximizing set itself. -/theorem re_amplitude_lt_of_strict_threshold (u : β) (k : ZMod N) (A B : Finset (ZMod N)) (c : β) (hcard : B.card = A.card) (hin : β a β A, c < (u * stdAddChar (-(a * k))).re) (hout : β a β A, (u * stdAddChar (-(a * k))).re β€ c) (hne : B β A) : (u * Ahat B k).re < (u * Ahat A k).re := by have h := sum_lt_of_strict_threshold (fun a : ZMod N => (u * stdAddChar (-(a * k))).re) A B c hcard hin hout hne simpa only [Ahat, Finset.mul_sum, Complex.re_sum] using h /-- At frequency one, the full norm bound follows once each directional projection has a translated arc above a separating threshold. Other primitive frequencies require pulling the arc back by their inverse. -/theorem norm_Ahat_one_le_arc_of_geometric_selector (m : β) (hm : m β€ N) (B : Finset (ZMod N)) (hcard : B.card = m) (hgeom : β u : β, βuβ = 1 β β s : ZMod N, β c : β, (β a β tpose s (arc (N := N) m), c β€ (u * stdAddChar (-(a * 1))).re) β§ (β a β tpose s (arc (N := N) m), (u * stdAddChar (-(a * 1))).re β€ c)) : βAhat B 1β β€ βAhat (arc (N := N) m) 1β := by obtain β¨u, hu, hreβ© := exists_phase_real_eq_norm (Ahat B 1) obtain β¨s, c, hin, houtβ© := hgeom u hu have hsize : (tpose s (arc (N := N) m)).card = B.card := by rw [card_tpose, card_arc m hm, hcard] have hp := re_amplitude_le_of_threshold u 1 (tpose s (arc (N := N) m)) B c hsize.symm hin hout calc βAhat B 1β = (u * Ahat B 1).re := hre.symm _ β€ (u * Ahat (tpose s (arc (N := N) m)) 1).re := hp _ β€ βu * Ahat (tpose s (arc (N := N) m)) 1β := Complex.re_le_norm _ _ = βAhat (arc (N := N) m) 1β := by rw [norm_mul, hu, one_mul, norm_tpose] /-- The same geometric selector controls every primitive frequency after relabeling the selected pitches by multiplication by that frequency. -/theorem norm_Ahat_unit_le_arc_of_geometric_selector (m : β) (hm : m β€ N) (B : Finset (ZMod N)) (hcard : B.card = m) (k : ZMod N) (hk : IsUnit k) (hgeom : β u : β, βuβ = 1 β β s : ZMod N, β c : β, (β a β tpose s (arc (N := N) m), c β€ (u * stdAddChar (-(a * 1))).re) β§ (β a β tpose s (arc (N := N) m), (u * stdAddChar (-(a * 1))).re β€ c)) : βAhat B kβ β€ βAhat (arc (N := N) m) 1β := by rw [Ahat_unit_frequency B k hk] apply norm_Ahat_one_le_arc_of_geometric_selector m hm _ ?_ hgeom rw [card_image_mul_unit B k hk, hcard] end CyclicHuddling end MusicTheory