πŸ›οΈ Atlasβœ’οΈFormalizations.MusicTheory.CycleGraphSpectrum view on GitHub β†—17 theorems
/-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.LinearAlgebra.Matrix.Circulantpublic import Mathlib.Combinatorics.SimpleGraph.Circulantpublic import Mathlib.Combinatorics.SimpleGraph.CycleGraphpublic import Mathlib.Combinatorics.SimpleGraph.AdjMatrixpublic import Mathlib.LinearAlgebra.StdBasispublic import Mathlib.LinearAlgebra.FiniteDimensional.Lemmaspublic import Mathlib.Tactic.Ring /-!# Circulant matrices are diagonalized by the discrete Fourier transform For `v : ZMod N β†’ β„‚`, Mathlib's `Matrix.circulant v` satisfies`circulant v *α΅₯ dftChar k = ZMod.dft v k β€’ dftChar k`, where `dftChar k j = stdAddChar (j * k)`.The characters form a basis (`N` times the inverse transform of the standard basis), so everycirculant is diagonal in it. For the cycle graph `C₁₂` this gives the spectrum`2 cos (2Ο€k / 12)`; for a Cayley graph of `β„€/N` the eigenvalue is a character sum. ## Main results * `circulant_mulVec_dftChar`: every circulant has the characters as eigenvectors, with eigenvalue  the transform of its first column.* `dftChar_linearIndependent`, `dftChar_basis`: the characters form a basis of `ZMod N β†’ β„‚`.* `C12_eq_adjMatrix`, `C12_eigenvalue_cos`: the adjacency matrix of `SimpleGraph.cycleGraph 12`  and its eigenvalues `2 cos (2Ο€k / 12)`.* `cayleyAdj_eigenvalue`, `cayleyAdj_eq_adjMatrix`: the spectrum of a Cayley graph of `β„€/N`. ## References * C. Godsil, G. Royle, *Algebraic Graph Theory*, Springer (2001).* L. Babai, *Spectra of Cayley graphs*, J. Combin. Theory Ser. B 27 (1979).* C. MarΓ­n, *The Tonnetz spectrum is the generating triad's Fourier balance profile*,  Zenodo (2026), doi:10.5281/zenodo.20862821.-/ @[expose] public section namespace MusicTheory open scoped Matrixopen Complex Real namespace CycleGraphSpectrum open ZMod AddChar variable {N : β„•} [NeZero N] /-! ### T1 β€” circulants are diagonalized by the DFT characters. -/ /-- The k-th DFT character as a vector on `ZMod N`: `dftChar k j = stdAddChar (j * k) = exp(2Ο€iΒ·jk/N)`.    These are the simultaneous eigenvectors of every circulant operator. -/noncomputable def dftChar (k : ZMod N) : ZMod N β†’ β„‚ := fun j => stdAddChar (j * k) @[simp] lemma dftChar_apply (k j : ZMod N) : dftChar k j = stdAddChar (j * k) := rfl /-- The DFT character is nowhere the zero vector: its value at `0` is `stdAddChar 0 = 1 β‰  0`. -/lemma dftChar_ne_zero (k : ZMod N) : dftChar k β‰  0 := by  intro h  have : dftChar k 0 = 0 := by rw [h]; rfl  rw [dftChar_apply, zero_mul, map_zero_eq_one] at this  exact one_ne_zero this /-- **THE DIAGONALIZATION (T1).** Every circulant matrix is diagonalized by the DFT characters:    `circulant v` sends the k-th character `dftChar k` to `(dft v k) β€’ dftChar k`. The eigenvalue    is exactly the k-th discrete Fourier coefficient of the generating vector `v`. -/theorem circulant_mulVec_dftChar (v : ZMod N β†’ β„‚) (k : ZMod N) :    (Matrix.circulant v) *α΅₯ (dftChar k) = (ZMod.dft v k) β€’ dftChar k := by  funext i  -- LHS i = Ξ£_j v(i-j) * stdAddChar(j*k)  simp only [Matrix.mulVec, dotProduct, Matrix.circulant_apply]  -- reindex m = i - j via Equiv.subLeft i  (j ↦ i - j),  so v(i - (i-m)) = v m  rw [← Equiv.sum_comp (Equiv.subLeft i)]  simp only [Equiv.subLeft_apply, sub_sub_cancel, dftChar_apply]  -- now: Ξ£_m v m * stdAddChar ((i - m) * k)  -- (i - m)*k = i*k - m*k = i*k + (-(m*k)); split the character  have hsplit : βˆ€ m : ZMod N,      v m * stdAddChar ((i - m) * k)        = stdAddChar (i * k) * (stdAddChar (-(m * k)) * v m) := by    intro m    rw [sub_mul, sub_eq_add_neg, map_add_eq_mul]    ring  rw [Finset.sum_congr rfl (fun m _ => hsplit m), ← Finset.mul_sum]  -- RHS = (dft v k) β€’ dftChar k at i = (dft v k) * stdAddChar (i*k)  rw [Pi.smul_apply, smul_eq_mul, dftChar_apply, ZMod.dft_apply]  simp only [smul_eq_mul]  rw [mul_comm] /-! ### T2 β€” the DFT characters are a BASIS: the simultaneous eigenbasis of all circulants. The 12 characters `dftChar k` are linearly independent, hence (card = dim = N) a basis of `ZMod N β†’ β„‚`.This is the classical "characters of a finite abelian group are independent", here in the concrete`stdAddChar` form. Combined with T1 (`circulant_mulVec_dftChar`), it makes the DFT the literaldiagonalizing eigenbasis of every circulant β€” in particular of `C12`. The load-bearing observation: each `dftChar k` is `N` times the inverse-DFT image of the standardbasis vector `Pi.single k 1`. Since `Pi.single Β· 1` is independent (`Pi.linearIndependent_single_one`),the injective linear equiv `𝓕⁻ = dft.symm` preserves independence (`LinearIndependent.map'`), andscaling by the nonzero constant `(N : β„‚)` preserves it again (`LinearIndependent.units_smul`). -/ /-- **The bridge to the standard basis.** The k-th DFT character is `N` times the inverse-DFT of the    standard basis vector `Pi.single k 1`: `dftChar k = (N : β„‚) β€’ 𝓕⁻ (Pi.single k 1)`. The inverse-DFT    of a delta is (up to the `N⁻¹` normalization) exactly a character. -/lemma dftChar_eq_smul_invDFT (k : ZMod N) :    dftChar k = (N : β„‚) β€’ (ZMod.dft (N := N)).symm (Pi.single k (1 : β„‚)) := by  funext x  rw [Pi.smul_apply, ZMod.invDFT_apply]  -- the inner sum collapses to the j = k term: stdAddChar (k * x) β€’ 1  rw [Finset.sum_eq_single k        (fun j _ hj => by rw [Pi.single_eq_of_ne hj, smul_zero])        (fun h => absurd (Finset.mem_univ k) h),      Pi.single_eq_same, dftChar_apply, mul_comm x k]  -- now: stdAddChar (k*x) = (N:β„‚) β€’ (N:β„‚)⁻¹ β€’ (stdAddChar (k*x) β€’ 1)  simp only [smul_eq_mul, mul_one]  rw [← mul_assoc, mul_inv_cancelβ‚€ (by exact_mod_cast (NeZero.ne N)), one_mul] /-- **Sealed helper.** An injective linear map out of `ZMod N β†’ β„‚` carries the standard basis    `(Pi.single k 1)_k` to a linearly independent family. Stated generically (`f` opaque) so the    elaborator/kernel never unfolds the heavy `dft` structure β€” the direct inline `map'` over    `ZMod.dft.symm` provokes a `whnf`/`isDefEq` blow-up; this generic version sidesteps it. -/theorem linearIndependent_single_one_map (M' : Type*) [AddCommGroup M'] [Module β„‚ M']    (n : β„•) (f : (ZMod n β†’ β„‚) β†’β‚—[β„‚] M') (hf : Function.Injective f) :    LinearIndependent β„‚ (fun k : ZMod n => f (Pi.single k (1 : β„‚))) :=  (Pi.linearIndependent_single_one (ZMod n) β„‚).map' f (LinearMap.ker_eq_bot.2 hf) /-- **THE EIGENBASIS β€” LINEAR INDEPENDENCE (T2, load-bearing).** The DFT characters    `(dftChar k)_{k ∈ ZMod N}` are linearly independent over β„‚. (Characters of the finite abelian    group `ZMod N`, in `stdAddChar` form.) Pushed through `𝓕⁻` from the standard basis and scaled. -/theorem dftChar_linearIndependent :    LinearIndependent β„‚ (fun k : ZMod N => dftChar k) := by  -- (Pi.single k 1)_k pushed through the injective inverse-DFT (sealed helper)  have hmap : LinearIndependent β„‚      (fun k : ZMod N => (ZMod.dft (N := N) (E := β„‚)).symm (Pi.single k (1 : β„‚))) :=    linearIndependent_single_one_map (ZMod N β†’ β„‚) N      (ZMod.dft (N := N) (E := β„‚)).symm.toLinearMap      (ZMod.dft (N := N) (E := β„‚)).symm.injective  -- scale each by the nonzero constant (N : β„‚) (as a unit) β€” preserves independence  have hN : (N : β„‚) β‰  0 := by exact_mod_cast (NeZero.ne N)  have hscale := hmap.units_smul (fun _ : ZMod N => (Units.mk0 (N : β„‚) hN))  -- the scaled family IS (dftChar k)_k, via dftChar_eq_smul_invDFT  have hfam : (fun _ : ZMod N => (Units.mk0 (N : β„‚) hN)) β€’      (fun k : ZMod N => (ZMod.dft (N := N) (E := β„‚)).symm (Pi.single k (1 : β„‚)))      = fun k : ZMod N => dftChar k := by    funext k    rw [Pi.smul_apply', dftChar_eq_smul_invDFT, Units.smul_mk0]  rwa [hfam] at hscale /-- **THE EIGENBASIS β€” AS A BASIS (T2).** The DFT characters form a basis of `ZMod N β†’ β„‚`    (linear independence + card = dim). This is the simultaneous eigenbasis diagonalizing every    circulant operator on `ZMod N`. -/noncomputable def dftChar_basis : Module.Basis (ZMod N) β„‚ (ZMod N β†’ β„‚) :=  basisOfLinearIndependentOfCardEqFinrank (b := fun k : ZMod N => dftChar k)    dftChar_linearIndependent (Module.finrank_fintype_fun_eq_card β„‚).symm @[simp] lemma dftChar_basis_apply (k : ZMod N) : dftChar_basis k = dftChar k := by  simp only [dftChar_basis, coe_basisOfLinearIndependentOfCardEqFinrank] /-! ### T3 β€” the pitch-class cycle graph C₁₂ and its spectrum. The chromatic circle: vertex `m ∈ β„€/12` joined to `m Β± 1` (its semitone neighbours). The adjacencyoperator is `C12 = circulant cycAdj` where `cycAdj` is the Β±1 semitone indicator. By T1 itseigenvalues are `dft cycAdj k`, which we compute to the textbook `2Β·cos(2Ο€k/12)`. -/ /-- The Β±1 semitone-neighbour indicator on β„€/12: `cycAdj m = 1` iff `m = 1 ∨ m = -1`, else `0`.    This is the first column of the C₁₂ adjacency matrix. -/noncomputable def cycAdj : ZMod 12 β†’ β„‚ := fun m => if m = 1 ∨ m = -1 then 1 else 0 /-- The adjacency operator of the pitch-class cycle graph C₁₂, as a circulant matrix over β„‚. -/noncomputable def C12 : Matrix (ZMod 12) (ZMod 12) β„‚ := Matrix.circulant cycAdj /-- C₁₂ is diagonalized by the DFT characters: eigenvector `dftChar k`, eigenvalue `dft cycAdj k`.    (Direct specialization of the general T1 diagonalization.) -/theorem C12_mulVec_dftChar (k : ZMod 12) :    C12 *α΅₯ (dftChar k) = (ZMod.dft cycAdj k) β€’ dftChar k :=  circulant_mulVec_dftChar cycAdj k /-- **The C₁₂ eigenvalue, as a character sum.** Only the two semitone steps `m = 1, -1` contribute,    giving `dft cycAdj k = stdAddChar k + stdAddChar (-k) = Ο‰^k + Ο‰^(-k)`, `Ο‰ = exp(2Ο€i/12)`. -/theorem C12_eigenvalue (k : ZMod 12) :    ZMod.dft cycAdj k = stdAddChar k + stdAddChar (-k) := by  rw [ZMod.dft_apply]  have h1 : (1 : ZMod 12) β‰  -1 := by decide  -- split univ into the pair {1, -1} and its complement  rw [← Finset.sum_add_sum_compl ({1, -1} : Finset (ZMod 12)), Finset.sum_pair h1]  -- the two surviving terms evaluate the character; the complement sum vanishes  have hc1 : cycAdj (1 : ZMod 12) = 1 := ite_eq_left (Or.inl rfl)  have hc2 : cycAdj (-1 : ZMod 12) = 1 := ite_eq_left (Or.inr rfl)  have e1 : stdAddChar (-((1 : ZMod 12) * k)) β€’ cycAdj 1 = stdAddChar (-k) := by    rw [hc1, one_mul, smul_eq_mul, mul_one]  have e2 : stdAddChar (-(((-1) : ZMod 12) * k)) β€’ cycAdj (-1) = stdAddChar k := by    rw [hc2, neg_one_mul, neg_neg, smul_eq_mul, mul_one]  rw [e1, e2]  have hzero : βˆ‘ m ∈ ({1, -1} : Finset (ZMod 12))ᢜ,      stdAddChar (-(m * k)) β€’ cycAdj m = 0 := by    apply Finset.sum_eq_zero    intro m hm    rw [Finset.mem_compl, Finset.mem_insert, Finset.mem_singleton] at hm    push Not at hm    have : cycAdj m = 0 := by      simp only [cycAdj, ite_eq_right (by tauto : Β¬(m = 1 ∨ m = -1))]    rw [this, smul_zero]  rw [hzero, add_zero, add_comm] /-- `stdAddChar` of a class equals the explicit root of unity `exp(2Ο€iΒ·k.val/12)`. -/lemma stdAddChar_eq_exp (k : ZMod 12) :    stdAddChar k = exp (2 * Ο€ * I * (k.val : β„‚) / 12) := by  conv_lhs => rw [← ZMod.natCast_zmod_val k]  rw [show ((k.val : ZMod 12)) = ((k.val : β„€) : ZMod 12) by push_cast; rfl,      stdAddChar_coe]  push_cast  ring_nf /-- **The C₁₂ eigenvalue in closed form (T3 payload).** The k-th eigenvalue of the chromatic    cycle graph is the textbook `2Β·cos(2Ο€k/12)` β€” the cosine spectrum of C₁₂. -/theorem C12_eigenvalue_cos (k : ZMod 12) :    ZMod.dft cycAdj k = 2 * Complex.cos (2 * Ο€ * (k.val : β„‚) / 12) := by  rw [C12_eigenvalue]  set ΞΈ : β„‚ := 2 * Ο€ * (k.val : β„‚) / 12 with hΞΈ  -- stdAddChar k = exp(ΞΈΒ·I),  stdAddChar(-k) = exp(-ΞΈΒ·I)  have hk : stdAddChar k = exp (ΞΈ * I) := by    rw [stdAddChar_eq_exp]; congr 1; rw [hΞΈ]; ring  have hnk : stdAddChar (-k) = exp (-ΞΈ * I) := by    rw [show (-k : ZMod 12) = ((-(k.val) : β„€) : ZMod 12) by push_cast; rw [ZMod.natCast_val,        ZMod.cast_id], stdAddChar_coe]    rw [hΞΈ]; push_cast; congr 1; ring  rw [hk, hnk, Complex.cos]  ring /-! ### T4 β€” the graph bridge: `C12` IS Mathlib's cycle-graph adjacency matrix. So the eigenvalues `C12_eigenvalue_cos` are LITERALLY the spectrum of `SimpleGraph.cycleGraph 12`.The Fin/ZMod subtraction-instance mismatch that defeats a direct `rw [cycleGraph_adj]` (cycleGraphlives over `Fin n`, our work over `ZMod 12`) is sidestepped by proving the adjacency characterizationwith `decide` over the 144 vertex pairs β€” the kernel evaluates both sides to a boolean, no patternunification needed. -/ /-- The adjacency relation of `cycleGraph 12` on β„€/12 is "differ by a semitone" (`i - j = Β±1`).    Checked by `decide` over the 144 pairs of `Fin 12`. -/theorem cycleGraph12_adj_iff : βˆ€ i j : ZMod 12,    (SimpleGraph.cycleGraph 12).Adj i j ↔ (i - j = 1 ∨ i - j = -1) :=  (by decide : βˆ€ i j : Fin 12,    (SimpleGraph.cycleGraph 12).Adj i j ↔ (i - j = 1 ∨ i - j = -1)) /-- **THE GRAPH BRIDGE (T4).** The circulant `C12` IS the adjacency matrix of the chromatic cycle    graph `cycleGraph 12` over β„‚. Combined with `C12_eigenvalue_cos`, this says the eigenvalues    `2Β·cos(2Ο€k/12)` are the spectrum of Mathlib's `SimpleGraph.cycleGraph 12`. -/theorem C12_eq_adjMatrix : C12 = (SimpleGraph.cycleGraph 12).adjMatrix β„‚ := by  ext i j  rw [C12, Matrix.circulant_apply]  by_cases h : (i - j = 1 ∨ i - j = -1)  Β· rw [show cycAdj (i - j) = 1 from ite_eq_left h]    exact (ite_eq_left ((cycleGraph12_adj_iff i j).mpr h)).symm  Β· rw [show cycAdj (i - j) = 0 from ite_eq_right h]    exact (ite_eq_right (fun hadj => h ((cycleGraph12_adj_iff i j).mp hadj))).symm /-- **THE PAYOFF (T2 β‹ˆ T1).** `C12` is diagonalized by the DFT basis: every basis vector    `dftChar_basis k` is an eigenvector of `C12` with eigenvalue `dft cycAdj k`. The eigenvectors    range over a genuine basis (`dftChar_basis`), so the DFT is a full diagonalizing eigenbasis of the    chromatic cycle graph's adjacency operator. -/theorem C12_diagonalized_by_dftChar_basis (k : ZMod 12) :    C12 *α΅₯ (dftChar_basis k) = (ZMod.dft cycAdj k) β€’ (dftChar_basis k) := by  rw [dftChar_basis_apply]; exact C12_mulVec_dftChar k /-! ### The spectrum of every abelian Cayley graph. C₁₂ is one instance of a general fact: an abelian Cayley graph `Cay(β„€_N, S)` β€” equivalently Mathlib's`circulantGraph S` over `ZMod N` β€” has adjacency operator the circulant of the connection-set indicator`1_S`, so by T1 it is diagonalized by the DFT characters with eigenvalue the character sum`βˆ‘_{s∈S} stdAddChar(-(sΒ·k))` (Babai's formula for abelian Cayley spectra). The bridge `cayleyAdj S =circulantGraph S` mirrors T4's `C12_eq_adjMatrix`, now for an arbitrary SYMMETRIC, `0βˆ‰S` connection set.Folklore (Babai 1979, LovΓ‘sz). -/ /-- The connection-set indicator `1_S` on `ZMod N` (first column of the Cayley adjacency matrix). -/noncomputable def cayleyAdjVec (S : Finset (ZMod N)) : ZMod N β†’ β„‚ := fun m => if m ∈ S then 1 else 0 /-- The adjacency operator of the abelian Cayley graph `Cay(β„€_N, S)`, as a circulant over β„‚. -/noncomputable def cayleyAdj (S : Finset (ZMod N)) : Matrix (ZMod N) (ZMod N) β„‚ :=  Matrix.circulant (cayleyAdjVec S) /-- **Abelian Cayley diagonalization (C-2).** Every abelian Cayley graph is diagonalized by the DFT    characters: `cayleyAdj S *α΅₯ dftChar k = (dft 1_S k) β€’ dftChar k`. Direct specialization of T1. -/theorem cayleyAdj_mulVec_dftChar (S : Finset (ZMod N)) (k : ZMod N) :    cayleyAdj S *α΅₯ (dftChar k) = (ZMod.dft (cayleyAdjVec S) k) β€’ dftChar k :=  circulant_mulVec_dftChar (cayleyAdjVec S) k /-- **The abelian Cayley eigenvalue = the character sum (C-2, Babai's formula).** The k-th eigenvalue is    `dft 1_S k = βˆ‘_{s∈S} stdAddChar(-(sΒ·k))` β€” sum of the k-th character over the connection set. -/theorem cayleyAdj_eigenvalue (S : Finset (ZMod N)) (k : ZMod N) :    ZMod.dft (cayleyAdjVec S) k = βˆ‘ s ∈ S, stdAddChar (-(s * k)) := by  have hzero : βˆ€ x ∈ (Finset.univ : Finset (ZMod N)), x βˆ‰ S β†’      stdAddChar (-(x * k)) β€’ cayleyAdjVec S x = 0 := by    intro x _ hx; unfold cayleyAdjVec; rw [ite_eq_right hx, smul_zero]  rw [ZMod.dft_apply, ← Finset.sum_subset (Finset.subset_univ S) hzero]  apply Finset.sum_congr rfl  intro s hs; unfold cayleyAdjVec; rw [ite_eq_left hs, smul_eq_mul, mul_one] omit [NeZero N] in/-- **The abelian Cayley graph bridge (C-2, mirrors T4).** For a SYMMETRIC connection set `S` not    containing `0`, the circulant `cayleyAdj S` IS the adjacency matrix of Mathlib's `circulantGraph S`.    Combined with `cayleyAdj_eigenvalue` this gives the spectrum of every such abelian Cayley graph. -/theorem cayleyAdj_eq_adjMatrix (S : Finset (ZMod N))    (hsymm : βˆ€ x : ZMod N, x ∈ S β†’ -x ∈ S) (h0 : (0 : ZMod N) βˆ‰ S) :    cayleyAdj S = (SimpleGraph.circulantGraph (S : Set (ZMod N))).adjMatrix β„‚ := by  have hadj : βˆ€ i j : ZMod N,      (SimpleGraph.circulantGraph (S : Set (ZMod N))).Adj i j ↔ (i - j ∈ S) := by    intro i j    rw [SimpleGraph.circulantGraph_adj]    simp only [Finset.mem_coe]    constructor    Β· rintro ⟨_, h | h⟩      Β· exact h      Β· have : -(j - i) ∈ S := hsymm _ h        rwa [neg_sub] at this    Β· intro h      refine ⟨fun hij => h0 ?_, Or.inl h⟩      rwa [sub_eq_zero.mpr hij] at h  ext i j  rw [cayleyAdj, Matrix.circulant_apply, SimpleGraph.adjMatrix_apply]  unfold cayleyAdjVec  by_cases h : i - j ∈ S  Β· rw [ite_eq_left h, ite_eq_left ((hadj i j).mpr h)]  Β· rw [ite_eq_right h, ite_eq_right (fun ha => h ((hadj i j).mp ha))] /-- **Instantiation: C₁₂ is the abelian Cayley graph `Cay(℀₁₂, {Β±1})`.** The semitone indicator    `cycAdj` is `1_{{1,-1}}`, so `C12 = cayleyAdj {1, -1}` β€” C₁₂ is the C-2 lemma at `S = {1, -1}`. -/example : C12 = cayleyAdj ({1, -1} : Finset (ZMod 12)) := by  rw [C12, cayleyAdj]  congr 1  funext m  unfold cycAdj cayleyAdjVec  simp only [Finset.mem_insert, Finset.mem_singleton] end CycleGraphSpectrum end MusicTheory