/-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 Formalizations.MusicTheory.Fourierpublic import Mathlib.Analysis.Fourier.ZModpublic import Mathlib.Tactic.Ring /-!# The sixth Fourier coefficient is the parity imbalance For a pitch-class set `A β β€/12`, `Ahat A 6 = β a β A, (-1) ^ a.val`, the number of even minusthe number of odd pitch classes in `A`; so `Ahat A 6 = 0` iff `A` has as many even as odd pitchclasses. The same holds at the frequency `m` of `β€/(2m)`. ## Main results * `Ahat_six_eq_sum_neg_one_pow`, `Ahat_six_eq_card_sub`, `Ahat_six_eq_zero_iff`.* `dftN_half_eq_sum_neg_one_pow`: the general even modulus. ## References * E. Amiot, *The torii of phases*, in Mathematics and Computation in Music (MCM 2013).* J. Hoffman, *On pitch-class set cartography*, J. Music Theory 52 (2008).* C. MarΓn, *The per-voice Fourier signature: the bass is the spectrally purest voice*, Zenodo (2026), doi:10.5281/zenodo.20971089.-/ @[expose] public section namespace MusicTheory open Finset ZMod AddCharopen scoped ZModopen MusicTheory.Fourier namespace ParityA6 /-! ### Part 1 β the half-cycle coefficient is the evenβodd parity imbalance. -/ /-- **FC-K, evenSet form.** `Γ A 6 = Ξ£_{aβA} (Β±1)` with `+1` on even pcs and `β1` on odd pcs. Immediate from `Ahat_apply` and the order-2 character value `stdAddChar(β(aΒ·6)) = Β±1` (`Fourier.stdAddChar_neg_mul_six`). -/theorem Ahat_six_eq_even_sub_odd (A : Finset (ZMod 12)) : Ahat A 6 = β a β A, (if a β evenSet then (1 : β) else -1) := by rw [Ahat_apply] exact Finset.sum_congr rfl fun a _ => stdAddChar_neg_mul_six a /-- The Β±1 sign at frequency 6 IS `(β1)^a.val`: even residues `β¦ +1`, odd residues `β¦ β1`. `a β evenSet βΊ Even a.val` is decidable over `ZMod 12`; then `Even/Odd.neg_one_pow`. -/lemma neg_one_pow_val (a : ZMod 12) : ((-1 : β)) ^ a.val = if a β evenSet then (1 : β) else -1 := by have h : (a β evenSet) β Even a.val := by revert a; decide by_cases he : Even a.val Β· rw [ite_eq_left (h.mpr he), he.neg_one_pow] Β· rw [ite_eq_right (fun hc => he (h.mp hc)), (Nat.not_even_iff_odd.mp he).neg_one_pow] /-- **FC-K, the headline `(β1)^a` form.** `Γ A 6 = Ξ£_{aβA} (β1)^a` β the half-cycle coefficient equals the signed pitch-class count (`+1` even, `β1` odd). -/theorem Ahat_six_eq_sum_neg_one_pow (A : Finset (ZMod 12)) : Ahat A 6 = β a β A, ((-1 : β)) ^ a.val := by rw [Ahat_six_eq_even_sub_odd] exact Finset.sum_congr rfl fun a _ => (neg_one_pow_val a).symm /-- **FC-K, the card form.** `Γ A 6 = #(even pcs of A) β #(odd pcs of A)` as a complex number. Split the Β±1 sum over the parity partition. -/theorem Ahat_six_eq_card_sub (A : Finset (ZMod 12)) : Ahat A 6 = ((A.filter (Β· β evenSet)).card : β) - ((A.filter (fun a => a β evenSet)).card : β) := by rw [Ahat_six_eq_even_sub_odd, Finset.sum_ite] simp only [Finset.sum_const, nsmul_eq_mul, mul_one, mul_neg_one] ring /-! ### Part 2 β aβ vanishes iff the pc-set is parity-balanced. -/ /-- **FC-K characterization.** `Γ A 6 = 0 βΊ #(even pcs) = #(odd pcs)` β the half-cycle coefficient vanishes exactly when `A` carries equally many even and odd pitch classes (parity balance). -/theorem Ahat_six_eq_zero_iff (A : Finset (ZMod 12)) : Ahat A 6 = 0 β (A.filter (Β· β evenSet)).card = (A.filter (fun a => a β evenSet)).card := by rw [Ahat_six_eq_card_sub, sub_eq_zero, Nat.cast_inj] /-! ### Witnesses. -/ /-- C major triad `{0,4,7}`: even pcs `{0,4}`, odd pc `{7}`, so `aβ = 2 β 1 = 1`. -/example : Ahat {0, 4, 7} 6 = 1 := by rw [Ahat_six_eq_card_sub, show (({0, 4, 7} : Finset (ZMod 12)).filter (Β· β evenSet)).card = 2 from by decide, show (({0, 4, 7} : Finset (ZMod 12)).filter (fun a => a β evenSet)).card = 1 from by decide] norm_num /-- Whole-tone scale `{0,2,4,6,8,10}` is all-even, maximally imbalanced: `aβ = 6`. -/example : Ahat {0, 2, 4, 6, 8, 10} 6 = 6 := by rw [Ahat_six_eq_card_sub, show (({0, 2, 4, 6, 8, 10} : Finset (ZMod 12)).filter (Β· β evenSet)).card = 6 from by decide, show (({0, 2, 4, 6, 8, 10} : Finset (ZMod 12)).filter (fun a => a β evenSet)).card = 0 from by decide] norm_num /-- The diatonic scale `{0,2,4,5,7,9,11}` (3 even `{0,2,4}`, 4 odd) has `aβ = β1 β 0`. -/example : Ahat {0, 2, 4, 5, 7, 9, 11} 6 = -1 := by rw [Ahat_six_eq_card_sub, show (({0, 2, 4, 5, 7, 9, 11} : Finset (ZMod 12)).filter (Β· β evenSet)).card = 3 from by decide, show (({0, 2, 4, 5, 7, 9, 11} : Finset (ZMod 12)).filter (fun a => a β evenSet)).card = 4 from by decide] norm_num /-- A parity-balanced set, the chromatic tetrachord `{0,1,2,3}` (even `{0,2}`, odd `{1,3}`), has `aβ = 0` β the characterization in action. -/example : Ahat ({0, 1, 2, 3} : Finset (ZMod 12)) 6 = 0 := by rw [Ahat_six_eq_zero_iff]; decide /-! ### Part 3 β the general `ZMod (2*m)` case: the order-2 element `m` reads parity. Frequency `m` of `ZMod (2*m)` is the unique element of order 2 (`2Β·m = 0`); its character value`stdAddChar(β(aΒ·m)) = (β1)^a` is the parity sign, so the `m`-th DFT coefficient of a pc-set over`β€_{2m}` is again the evenβodd imbalance `Ξ£_{aβA} (β1)^a`. (The `β€ββ` case above is `m = 6`.) -/ section General /-- General 0/1 indicator on `ZMod N`. -/noncomputable def indN {N : β} (A : Finset (ZMod N)) : ZMod N β β := fun j => if j β A then 1 else 0 /-- `π (indN A) t = Ξ£_{aβA} stdAddChar(β(aΒ·t))` (the indicator collapses the sum to `A`). -/lemma dftN_apply {N : β} [NeZero N] (A : Finset (ZMod N)) (t : ZMod N) : π (indN A) t = β a β A, stdAddChar (-(a * t)) := by unfold indN rw [dft_apply] simp only [smul_eq_mul, mul_ite, mul_one, mul_zero, Finset.sum_ite_mem, Finset.univ_inter] /-- The order-2 element `m` has character value `β1`: `stdAddChar (m : ZMod (2m)) = exp(Οi) = β1`. -/lemma stdAddChar_half {m : β} [NeZero (2 * m)] : stdAddChar ((m : β) : ZMod (2 * m)) = (-1 : β) := by have h2m : 2 * m β 0 := NeZero.ne (2 * m) have hm0 : ((m : β) : β) β 0 := by have : m β 0 := by omega exact_mod_cast this have hcast : ((m : β) : ZMod (2 * m)) = ((m : β€) : ZMod (2 * m)) := by push_cast; ring rw [hcast, stdAddChar_coe, show (2 * Real.pi * Complex.I * ((m : β€) : β) / ((2 * m : β) : β)) = Real.pi * Complex.I from by push_cast; field_simp, Complex.exp_pi_mul_I] /-- The order-2 element is its own negative: `β(aΒ·m) = aΒ·m` in `ZMod (2m)`. -/lemma neg_mul_half {m : β} (a : ZMod (2 * m)) : -(a * ((m : β) : ZMod (2 * m))) = a * ((m : β) : ZMod (2 * m)) := by rw [β mul_neg] congr 1 have h2 : ((m : β) : ZMod (2 * m)) + ((m : β) : ZMod (2 * m)) = 0 := by rw [β Nat.cast_add, β two_mul, ZMod.natCast_self] exact (eq_neg_of_add_eq_zero_left h2).symm /-- **FC-K, general `ZMod (2m)`.** The `m`-th DFT coefficient (the half-cycle / order-2 frequency) of a pc-set over `β€_{2m}` is the evenβodd parity imbalance `Ξ£_{aβA} (β1)^a`. The `β€ββ` headline `Ahat_six_eq_sum_neg_one_pow` is the case `m = 6`. -/theorem dftN_half_eq_sum_neg_one_pow {m : β} [NeZero (2 * m)] (A : Finset (ZMod (2 * m))) : π (indN A) ((m : β) : ZMod (2 * m)) = β a β A, ((-1 : β)) ^ a.val := by rw [dftN_apply] refine Finset.sum_congr rfl fun a _ => ?_ rw [neg_mul_half a] have hsmul : a.val β’ ((m : β) : ZMod (2 * m)) = a * ((m : β) : ZMod (2 * m)) := by rw [nsmul_eq_mul, ZMod.natCast_rightInverse a] rw [β hsmul, map_nsmul_eq_pow, stdAddChar_half] end General end ParityA6 end MusicTheory