/-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.Convex.Functionpublic import Mathlib.Analysis.Convex.SpecificFunctions.Derivpublic import Mathlib.Algebra.BigOperators.Group.Finset.Basicpublic import Mathlib.Algebra.BigOperators.Group.Finset.Lemmaspublic import Mathlib.Data.Fin.VecNotationpublic import Mathlib.Tactic.Ringpublic import Mathlib.Tactic.FieldSimppublic import Mathlib.Tactic.Linarith /-!# The step-gap energy and balanced gap configurations A gap configuration of `k` parts of `n` is `g : Fin k β β` with `1 β€ g i` and `β g i = n`; it isbalanced when its maximum and minimum differ by at most one. For a strictly convex potential `V`on `[0, β)`, the gap energy `β V (g i)` is minimized exactly by the balanced configurations.This energy depends only on the multiset of gaps, so being balanced is necessary but notsufficient for maximal evenness; the all-pairs energy of `AllPairsEvenness` singles out themaximally even sets. ## Main results * `swap_lt`, `exists_improve`: an unbalanced configuration can be improved.* `balanced_energy_eq`: balanced configurations have equal energy.* `isMinimizer_iff_balanced`: global minimizer iff balanced.* `diatonic_balanced`: the diatonic gap word `(2, 2, 1, 2, 2, 2, 1)` is balanced. ## References * P. Bak, R. Bruinsma, *One-dimensional Ising model and the complete devil's staircase*, Phys. Rev. Lett. 49 (1982).* J. Clough, J. Douthett, *Maximally even sets*, J. Music Theory 35 (1991).* J. Douthett, R. Krantz, *Maximally even sets and configurations: common threads in mathematics, physics, and music*, J. Comb. Optim. 14 (2007).* C. MarΓn, *One transform and one duality: a machine-checked core of music theory in Lean 4*, Zenodo (2026), doi:10.5281/zenodo.20953767.-/ @[expose] public section namespace MusicTheory namespace MaximalEvenness open Finset /-! ### Definitions -/ /-- A gap configuration is `Balanced` when `max β min β€ 1`: every gap differs from every other by at most one. This is exactly the maximally-even structure (all gaps `βn/kβ` or `βn/kβ`). -/def Balanced {k : β} (g : Fin k β β) : Prop := β i j, g i β€ g j + 1 /-- The energy of a configuration for a potential `V`, casting gaps to `β`. -/noncomputable def energy {k : β} (V : β β β) (g : Fin k β β) : β := β i, V ((g i : β)) /-! ### Part 1 β the swap lemma (strict convexity) -/ /-- **Swap lemma.** For `V` strictly convex on `s` and endpoints `a, b β s` with `b + 2 β€ a`, pulling the two gaps one step together strictly lowers the pair energy: `V (a-1) + V (b+1) < V a + V b`. Proof: write `a-1` and `b+1` as strict convex combinations of the endpoints `a, b`, apply binary strict Jensen to each, and add β the endpoint coefficients each total `1`. -/theorem swap_lt {V : β β β} {s : Set β} (hV : StrictConvexOn β s V) {a b : β} (ha : a β s) (hb : b β s) (hab : b + 2 β€ a) : V (a - 1) + V (b + 1) < V a + V b := by have hd : (0 : β) < a - b := by linarith have hne_ab : a - b β 0 := ne_of_gt hd have hxy : a β b := ne_of_gt (by linarith) -- coefficients for `a - 1`: Ξ±β = (a-1-b)/(a-b) on `a`, Ξ²β = 1/(a-b) on `b` have hΞ±1 : (0 : β) < (a - 1 - b) / (a - b) := div_pos (by linarith) hd have hΞ²1 : (0 : β) < 1 / (a - b) := div_pos one_pos hd have hs1 : (a - 1 - b) / (a - b) + 1 / (a - b) = 1 := by field_simp; ring -- coefficients for `b + 1`: Ξ±β = 1/(a-b) on `a`, Ξ²β = (a-b-1)/(a-b) on `b` have hΞ±2 : (0 : β) < 1 / (a - b) := div_pos one_pos hd have hΞ²2 : (0 : β) < (a - b - 1) / (a - b) := div_pos (by linarith) hd have hs2 : 1 / (a - b) + (a - b - 1) / (a - b) = 1 := by field_simp; ring have h1 := hV.2 ha hb hxy hΞ±1 hΞ²1 hs1 have h2 := hV.2 ha hb hxy hΞ±2 hΞ²2 hs2 simp only [smul_eq_mul] at h1 h2 have e1 : (a - 1 - b) / (a - b) * a + 1 / (a - b) * b = a - 1 := by field_simp; ring have e2 : 1 / (a - b) * a + (a - b - 1) / (a - b) * b = b + 1 := by field_simp; ring rw [e1] at h1 rw [e2] at h2 have key : (a - 1 - b) / (a - b) * V a + 1 / (a - b) * V b + (1 / (a - b) * V a + (a - b - 1) / (a - b) * V b) = V a + V b := by field_simp; ring linarith [h1, h2, key] /-! ### Part 2 β minimality (the payoff) -/ /-- A two-index split of a `Finset.univ` sum: pull out the values at two distinct indices `i, j`. -/theorem sum_split_two {k : β} {M : Type*} [AddCommMonoid M] (F : Fin k β M) {i j : Fin k} (hij : i β j) : β x, F x = F i + F j + β x β (univ.erase i).erase j, F x := by rw [β Finset.add_sum_erase univ F (mem_univ i), β Finset.add_sum_erase (univ.erase i) F (mem_erase.mpr β¨Ne.symm hij, mem_univ jβ©), add_assoc] /-- **Improvement step.** If a valid configuration is not Balanced, one swap of an unbalanced pair gives another valid configuration with the same sum and strictly smaller energy. -/theorem exists_improve {k n : β} {V : β β β} (hV : StrictConvexOn β (Set.Ici (0 : β)) V) (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hnb : Β¬ Balanced g) : β g' : Fin k β β, (β i, 1 β€ g' i) β§ (β i, g' i = n) β§ energy V g' < energy V g := by -- extract an unbalanced pair `g j + 1 < g i` simp only [Balanced, not_forall, not_le] at hnb obtain β¨i, j, hltβ© := hnb have hij : i β j := by rintro rfl; omega have hge : g j + 2 β€ g i := by omega -- the swapped configuration classical set g' : Fin k β β := fun x => if x = i then g i - 1 else if x = j then g j + 1 else g x with hg' have hg'i : g' i = g i - 1 := by simp [hg'] have hg'j : g' j = g j + 1 := by simp [hg', hij.symm] have hg'rest : β x β (univ.erase i).erase j, g' x = g x := by intro x hx rw [mem_erase] at hx obtain β¨hxj, hx'β© := hx rw [mem_erase] at hx' obtain β¨hxi, -β© := hx' simp [hg', hxi, hxj] refine β¨g', ?_, ?_, ?_β© Β· -- positivity intro x by_cases hxi : x = i Β· subst hxi; rw [hg'i]; omega Β· by_cases hxj : x = j Β· subst hxj; rw [hg'j]; omega Β· simp [hg', hxi, hxj]; exact hpos x Β· -- sum preserved have h1 : β x, g' x = (g i - 1) + (g j + 1) + β x β (univ.erase i).erase j, g x := by rw [sum_split_two g' hij, hg'i, hg'j] congr 1 exact Finset.sum_congr rfl hg'rest rw [h1, β hsum, sum_split_two g hij] omega Β· -- energy strictly drops have hci : ((g' i : β)) = (g i : β) - 1 := by rw [hg'i, Nat.cast_sub (by omega), Nat.cast_one] have hcj : ((g' j : β)) = (g j : β) + 1 := by rw [hg'j]; push_cast; ring have hE_g' : energy V g' = V ((g i : β) - 1) + V ((g j : β) + 1) + β x β (univ.erase i).erase j, V ((g x : β)) := by rw [energy, sum_split_two (fun x => V ((g' x : β))) hij, hci, hcj] congr 1 exact Finset.sum_congr rfl (fun x hx => by rw [hg'rest x hx]) have hE_g : energy V g = V ((g i : β)) + V ((g j : β)) + β x β (univ.erase i).erase j, V ((g x : β)) := by rw [energy, sum_split_two (fun x => V ((g x : β))) hij] rw [hE_g', hE_g] have hmem_a : (g i : β) β Set.Ici (0 : β) := Set.mem_Ici.mpr (by positivity) have hmem_b : (g j : β) β Set.Ici (0 : β) := Set.mem_Ici.mpr (by positivity) have hsw := swap_lt hV hmem_a hmem_b (by exact_mod_cast hge) linarith [hsw] /-- **ME characterization.** Any configuration that minimizes the (strictly convex) energy over all valid configurations of the same sum is `Balanced` β i.e. maximally even. -/theorem minimizer_balanced {k n : β} {V : β β β} (hV : StrictConvexOn β (Set.Ici (0 : β)) V) (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hmin : β g' : Fin k β β, (β i, 1 β€ g' i) β (β i, g' i = n) β energy V g β€ energy V g') : Balanced g := by by_contra hnb obtain β¨g', hg'pos, hg'sum, hg'ltβ© := exists_improve hV g hpos hsum hnb exact absurd (hmin g' hg'pos hg'sum) (not_le.mpr hg'lt) /-! ### Part 3 β the converse: every Balanced config is a global energy minimizer -/ /-- Closed form of the energy of ANY balanced valid config: it depends only on `(k, n, V)`. Writing `m = βn/kβ` and `r = n % k`, exactly `r` gaps equal `m+1` and `k β r` equal `m`, so `energy V g = r Β· V(m+1) + (kβr) Β· V(m)`. (Counting identity; no convexity needed.) -/theorem balanced_energy_closed {k n : β} (V : β β β) (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hbal : Balanced g) : energy V g = ((n % k : β) : β) * V (((n / k : β) : β) + 1) + ((k - n % k : β) : β) * V ((n / k : β) : β) := by classical set m := n / k with hm set r := n % k with hr have hdm : k * m + r = n := by have h := Nat.div_add_mod n k; rw [β hm, β hr] at h; exact h -- every gap is `m` or `m+1` have hmem : β i, g i = m β¨ g i = m + 1 := by intro i have hk : 0 < k := Nat.pos_of_ne_zero (by rintro rfl; exact i.elim0) have hrk : r < k := by rw [hr]; exact Nat.mod_lt n hk have hub : g i β€ m + 1 := by by_contra hgt have hall : β j β (univ : Finset (Fin k)), m + 1 β€ g j := by intro j _; have hb := hbal i j; omega have hle := Finset.card_nsmul_le_sum univ g (m + 1) hall rw [hsum, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, Nat.cast_id] at hle have hexp : k * (m + 1) = k * m + k := by ring rw [hexp] at hle; omega have hlb : m β€ g i := by by_contra hlt have hlt' : g i < m := not_le.mp hlt have hall : β j β (univ : Finset (Fin k)), g j β€ m := by intro j _; have hb := hbal j i; omega have hlt2 := Finset.sum_lt_sum hall β¨i, mem_univ i, hlt'β© rw [hsum, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, Nat.cast_id] at hlt2 omega omega -- counts of the two gap values set c := (univ.filter (fun i => g i = m + 1)).card with hc set d := (univ.filter (fun i => Β¬ g i = m + 1)).card with hd have hcardP : c + d = k := by rw [hc, hd, Finset.card_filter_add_card_filter_not, Finset.card_univ, Fintype.card_fin] have hval2 : β i β univ.filter (fun i => Β¬ g i = m + 1), g i = m := by intro i hi; rw [Finset.mem_filter] at hi rcases hmem i with h | h Β· exact h Β· exact absurd h hi.2 -- β split of the sum to pin `c = r` have key : (β i β univ.filter (fun i => g i = m + 1), g i) + (β i β univ.filter (fun i => Β¬ g i = m + 1), g i) = β i, g i := Finset.sum_filter_add_sum_filter_not univ _ g have e1 : (β i β univ.filter (fun i => g i = m + 1), g i) = c * (m + 1) := by rw [Finset.sum_congr rfl (fun i hi => (Finset.mem_filter.mp hi).2), Finset.sum_const, nsmul_eq_mul, Nat.cast_id, β hc] have e2 : (β i β univ.filter (fun i => Β¬ g i = m + 1), g i) = d * m := by rw [Finset.sum_congr rfl hval2, Finset.sum_const, nsmul_eq_mul, Nat.cast_id, β hd] have hcount : k * m + c = n := by have h := key; rw [e1, e2, hsum] at h have hrw : c * (m + 1) + d * m = k * m + c := by have e : c * (m + 1) + d * m = (c + d) * m + c := by ring rw [e, hcardP] rw [hrw] at h; exact h have hc_eq : c = r := by omega have hd_eq : d = k - r := by omega -- β split of the energy into the two value classes have hPval : β i β univ.filter (fun i => g i = m + 1), V ((g i : β)) = V ((m : β) + 1) := by intro i hi; rw [Finset.mem_filter] at hi have hcast : (g i : β) = (m : β) + 1 := by rw [hi.2]; push_cast; ring rw [hcast] have hNval : β i β univ.filter (fun i => Β¬ g i = m + 1), V ((g i : β)) = V ((m : β)) := by intro i hi; rw [hval2 i hi] have keyE : (β i β univ.filter (fun i => g i = m + 1), V ((g i : β))) + (β i β univ.filter (fun i => Β¬ g i = m + 1), V ((g i : β))) = energy V g := by rw [energy]; exact Finset.sum_filter_add_sum_filter_not univ _ (fun i => V ((g i : β))) have hEP : (β i β univ.filter (fun i => g i = m + 1), V ((g i : β))) = (c : β) * V ((m : β) + 1) := by rw [Finset.sum_congr rfl hPval, Finset.sum_const, nsmul_eq_mul, β hc] have hEN : (β i β univ.filter (fun i => Β¬ g i = m + 1), V ((g i : β))) = (d : β) * V ((m : β)) := by rw [Finset.sum_congr rfl hNval, Finset.sum_const, nsmul_eq_mul, β hd] rw [β keyE, hEP, hEN, hc_eq, hd_eq] /-- **Balanced configs all have the same energy.** Any two balanced valid configs of the same `(k, n)` have equal energy for any `V` (they are permutations of each other). -/theorem balanced_energy_eq {k n : β} (V : β β β) (g g' : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hbal : Balanced g) (hpos' : β i, 1 β€ g' i) (hsum' : β i, g' i = n) (hbal' : Balanced g') : energy V g = energy V g' := by rw [balanced_energy_closed V g hpos hsum hbal, balanced_energy_closed V g' hpos' hsum' hbal'] /-- Termination measure for the swap descent: `Ξ¦ g = β (g i)Β²`, a strictly-decreasing β-valued well-founded measure under any balancing swap (independent of `V`). -/def phi {k : β} (g : Fin k β β) : β := β i, (g i) ^ 2 /-- **Improvement step with the `Ξ¦` measure.** Same single swap as `exists_improve`, additionally certifying that the termination measure `Ξ¦` strictly drops (`Ξ = 2(bβa)+2 β€ β2`). -/theorem exists_improve_phi {k n : β} {V : β β β} (hV : StrictConvexOn β (Set.Ici (0 : β)) V) (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hnb : Β¬ Balanced g) : β g' : Fin k β β, (β i, 1 β€ g' i) β§ (β i, g' i = n) β§ energy V g' < energy V g β§ phi g' < phi g := by simp only [Balanced, not_forall, not_le] at hnb obtain β¨i, j, hltβ© := hnb have hij : i β j := by rintro rfl; omega have hge : g j + 2 β€ g i := by omega classical set g' : Fin k β β := fun x => if x = i then g i - 1 else if x = j then g j + 1 else g x with hg' have hg'i : g' i = g i - 1 := by simp [hg'] have hg'j : g' j = g j + 1 := by simp [hg', hij.symm] have hg'rest : β x β (univ.erase i).erase j, g' x = g x := by intro x hx rw [mem_erase] at hx obtain β¨hxj, hx'β© := hx rw [mem_erase] at hx' obtain β¨hxi, -β© := hx' simp [hg', hxi, hxj] refine β¨g', ?_, ?_, ?_, ?_β© Β· intro x by_cases hxi : x = i Β· subst hxi; rw [hg'i]; omega Β· by_cases hxj : x = j Β· subst hxj; rw [hg'j]; omega Β· simp [hg', hxi, hxj]; exact hpos x Β· have h1 : β x, g' x = (g i - 1) + (g j + 1) + β x β (univ.erase i).erase j, g x := by rw [sum_split_two g' hij, hg'i, hg'j] congr 1 exact Finset.sum_congr rfl hg'rest rw [h1, β hsum, sum_split_two g hij] omega Β· have hci : ((g' i : β)) = (g i : β) - 1 := by rw [hg'i, Nat.cast_sub (by omega), Nat.cast_one] have hcj : ((g' j : β)) = (g j : β) + 1 := by rw [hg'j]; push_cast; ring have hE_g' : energy V g' = V ((g i : β) - 1) + V ((g j : β) + 1) + β x β (univ.erase i).erase j, V ((g x : β)) := by rw [energy, sum_split_two (fun x => V ((g' x : β))) hij, hci, hcj] congr 1 exact Finset.sum_congr rfl (fun x hx => by rw [hg'rest x hx]) have hE_g : energy V g = V ((g i : β)) + V ((g j : β)) + β x β (univ.erase i).erase j, V ((g x : β)) := by rw [energy, sum_split_two (fun x => V ((g x : β))) hij] rw [hE_g', hE_g] have hmem_a : (g i : β) β Set.Ici (0 : β) := Set.mem_Ici.mpr (by positivity) have hmem_b : (g j : β) β Set.Ici (0 : β) := Set.mem_Ici.mpr (by positivity) have hsw := swap_lt hV hmem_a hmem_b (by exact_mod_cast hge) linarith [hsw] Β· -- the same swap strictly decreases `Ξ¦ = β (g i)Β²` have hPhi_g' : phi g' = (g i - 1) ^ 2 + (g j + 1) ^ 2 + β x β (univ.erase i).erase j, (g x) ^ 2 := by rw [phi, sum_split_two (fun x => (g' x) ^ 2) hij, hg'i, hg'j] congr 1 exact Finset.sum_congr rfl (fun x hx => by rw [hg'rest x hx]) have hPhi_g : phi g = (g i) ^ 2 + (g j) ^ 2 + β x β (univ.erase i).erase j, (g x) ^ 2 := by rw [phi, sum_split_two (fun x => (g x) ^ 2) hij] rw [hPhi_g', hPhi_g] apply Nat.add_lt_add_right obtain β¨t, htβ© := Nat.le.dest hge have hi1 : g i - 1 = g j + 1 + t := by omega rw [hi1, β ht] have expand : (g j + 2 + t) ^ 2 + (g j) ^ 2 = (g j + 1 + t) ^ 2 + (g j + 1) ^ 2 + (2 * t + 2) := by ring rw [expand]; omega /-- **The converse (target).** For strictly convex `V`, any `Balanced` valid config is a GLOBAL energy minimizer over all valid configs of the same sum. Proof: well-founded descent on `Ξ¦ = β (g i)Β²` β a non-balanced config strictly improves (lower energy AND lower `Ξ¦`) via `exists_improve_phi`, and a balanced config matches `g`'s energy via `balanced_energy_eq`. -/theorem balanced_isMinimizer {k n : β} {V : β β β} (hV : StrictConvexOn β (Set.Ici (0 : β)) V) (g : Fin k β β) (hgpos : β i, 1 β€ g i) (hgsum : β i, g i = n) (hgbal : Balanced g) : β g' : Fin k β β, (β i, 1 β€ g' i) β (β i, g' i = n) β energy V g β€ energy V g' := by suffices H : β N : β, β g' : Fin k β β, phi g' β€ N β (β i, 1 β€ g' i) β (β i, g' i = n) β energy V g β€ energy V g' by intro g' hpos hsum; exact H (phi g') g' le_rfl hpos hsum intro N induction N with | zero => intro g' hphi hpos hsum by_cases hbal : Balanced g' Β· exact le_of_eq (balanced_energy_eq V g g' hgpos hgsum hgbal hpos hsum hbal) Β· obtain β¨g'', _, _, _, hPhiltβ© := exists_improve_phi hV g' hpos hsum hbal exfalso; omega | succ N IH => intro g' hphi hpos hsum by_cases hbal : Balanced g' Β· exact le_of_eq (balanced_energy_eq V g g' hgpos hgsum hgbal hpos hsum hbal) Β· obtain β¨g'', hpos'', hsum'', hElt, hPhiltβ© := exists_improve_phi hV g' hpos hsum hbal have hle'' : phi g'' β€ N := by omega have hrec := IH g'' hle'' hpos'' hsum'' linarith /-- **Ground-state characterization of maximal evenness (full iff).** For strictly convex `V`, a valid config `g` is a global energy minimizer over all valid configs of the same sum **iff** it is `Balanced` (maximally even). Combines `minimizer_balanced` (β) and `balanced_isMinimizer` (β). -/theorem isMinimizer_iff_balanced {k n : β} {V : β β β} (hV : StrictConvexOn β (Set.Ici (0 : β)) V) (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) : (β g' : Fin k β β, (β i, 1 β€ g' i) β (β i, g' i = n) β energy V g β€ energy V g') β Balanced g := by constructor Β· intro hmin; exact minimizer_balanced hV g hpos hsum hmin Β· intro hbal; exact balanced_isMinimizer hV g hpos hsum hbal /-! ### Part 4 β concrete music instance: the diatonic scale -/ /-- The circular gap word of the diatonic scale `{0,2,4,5,7,9,11} β β€/12`: `(2,2,1,2,2,2,1)` (`k = 7` gaps summing to `n = 12`). -/def diatonicGaps : Fin 7 β β := ![2, 2, 1, 2, 2, 2, 1] /-- Every diatonic gap is positive. -/theorem diatonic_pos : β i, 1 β€ diatonicGaps i := by decide /-- The diatonic gaps sum to the octave, `n = 12`. -/theorem diatonic_sum : β i, diatonicGaps i = 12 := by decide /-- **The diatonic scale is maximally even**: its gap word `(2,2,1,2,2,2,1)` is `Balanced` (`max β min = 2 β 1 = 1`). -/theorem diatonic_balanced : Balanced diatonicGaps := by unfold Balanced; decide /-- Strict convexity of `x β¦ xΒ²` on `[0,β)` (the canonical repulsive potential). -/theorem strictConvexOn_sq : StrictConvexOn β (Set.Ici (0 : β)) (fun x : β => x ^ 2) := strictConvexOn_pow (by norm_num) /-- **Concrete corollary.** For the quadratic energy `E g = β (g i)Β²`, any minimizer over valid configs of a fixed sum is `Balanced` (maximally even). Specializing the convex `minimizer_balanced`. -/theorem sq_minimizer_balanced {k n : β} (g : Fin k β β) (hpos : β i, 1 β€ g i) (hsum : β i, g i = n) (hmin : β g' : Fin k β β, (β i, 1 β€ g' i) β (β i, g' i = n) β energy (fun x => x ^ 2) g β€ energy (fun x => x ^ 2) g') : Balanced g := minimizer_balanced strictConvexOn_sq g hpos hsum hmin /-- **The diatonic gap word is a global minimizer of `β gapsΒ²`.** Concrete instance of the converse: the diatonic `(2,2,1,2,2,2,1)` minimizes the quadratic energy over every valid `7`-gap config summing to `12` β the full ground-state statement of its maximal evenness. -/theorem diatonic_sq_isMinimizer : β g' : Fin 7 β β, (β i, 1 β€ g' i) β (β i, g' i = 12) β energy (fun x => x ^ 2) diatonicGaps β€ energy (fun x => x ^ 2) g' := balanced_isMinimizer strictConvexOn_sq diatonicGaps diatonic_pos diatonic_sum diatonic_balanced /-- **Full iff at the diatonic.** Specializing the ground-state characterization to `V = xΒ²`, `7`-of-`12`. -/theorem diatonic_sq_iff : (β g' : Fin 7 β β, (β i, 1 β€ g' i) β (β i, g' i = 12) β energy (fun x => x ^ 2) diatonicGaps β€ energy (fun x => x ^ 2) g') β Balanced diatonicGaps := isMinimizer_iff_balanced strictConvexOn_sq diatonicGaps diatonic_pos diatonic_sum end MaximalEvenness end MusicTheory