/-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.Analysis.Polynomial.RealRoots.BudanFourierpublic import Mathlib.Algebra.Polynomial.RuleOfSigns /-!# Descartes' sign variations as the Budan–Fourier count at zero The number of sign variations of the derivative tower of `p` at `0` equals the number of signvariations of the coefficients of `p`, the quantity `Polynomial.signVariations p` of Mathlib'sDescartes rule of signs. The tower evaluated at `0` has the signs of the coefficients(`p^(k)(0) = k! a_k`) listed from low to high degree, and the number of sign variations is invariantunder reversal. ## Main results * `BudanFourier.signChanges_reverse`: the number of sign variations of a list of signs is invariant under reversal.* `BudanFourier.fourierVar_zero_eq_signVariations`: `fourierVar p 0 = Polynomial.signVariations p`.-/ @[expose] public section open Polynomial namespace BudanFourier /-! ### `signChanges` is reversal-invariant -/ /-- `filter (· ≠ 0)` commutes with `reverse`. -/theorem filter_ne_zero_reverse (s : List SignType) : s.reverse.filter (· ≠ 0) = (s.filter (· ≠ 0)).reverse := by induction s with | nil => rfl | cons a t ih => rw [List.reverse_cons, List.filter_append, ih] by_cases h : a = 0 <;> simp [h] /-- The number of maximal `≠`-runs is the same forwards and backwards: `destutter (· ≠ ·)` returns amaximal `≠`-chain sublist, and reversal bijects chain sublists. -/theorem destutter_ne_length_reverse (L : List SignType) : (L.reverse.destutter (· ≠ ·)).length = (L.destutter (· ≠ ·)).length := by have aux : ∀ M : List SignType, (M.destutter (· ≠ ·)).length ≤ (M.reverse.destutter (· ≠ ·)).length := by intro M have hchain : (M.destutter (· ≠ ·)).IsChain (· ≠ ·) := List.isChain_destutter (· ≠ ·) M have hsub : List.Sublist (M.destutter (· ≠ ·)) M := List.destutter_sublist (· ≠ ·) M have hsubr : List.Sublist ((M.destutter (· ≠ ·)).reverse) M.reverse := hsub.reverse have hchainr : ((M.destutter (· ≠ ·)).reverse).IsChain (· ≠ ·) := List.isChain_reverse.mpr (hchain.imp (fun _ _ h => Ne.symm h)) have hle := List.IsChain.length_le_length_destutter_ne hsubr hchainr rwa [List.length_reverse] at hle have h1 := aux L have h2 := aux L.reverse rw [List.reverse_reverse] at h2 omega /-- `signChanges` does not depend on the orientation of the list. -/theorem signChanges_reverse (s : List SignType) : Sturm.signChanges s.reverse = Sturm.signChanges s := by unfold Sturm.signChanges rw [filter_ne_zero_reverse] congr 1 exact destutter_ne_length_reverse (s.filter (· ≠ 0)) /-! ### The tower at `0` reads the coefficient signs -/ /-- `sign (p^{(k)}(0)) = sign (a_k)`, since `p^{(k)}(0) = k!\,a_k` and `k! > 0`. -/theorem sign_iterate_derivative_eval_zero (p : Polynomial ℝ) (k : ℕ) : SignType.sign (((derivative^[k]) p).eval 0) = SignType.sign (p.coeff k) := by have he : ((derivative^[k]) p).eval 0 = (k.factorial : ℝ) * p.coeff k := by rw [← coeff_zero_eq_eval_zero, coeff_iterate_derivative] simp [Nat.descFactorial_self, nsmul_eq_mul] rw [he, sign_mul, show SignType.sign ((k.factorial : ℝ)) = 1 from sign_pos (by exact_mod_cast Nat.factorial_pos k), one_mul] /-- The tower's signs at `0`, as a list, are the coefficient signs `[sign a_0, …, sign a_n]`. -/theorem fourierSeq_eval_zero_signs (p : Polynomial ℝ) : (fourierSeq p).map (fun q => SignType.sign (q.eval 0)) = (List.range (p.natDegree + 1)).map (fun k => SignType.sign (p.coeff k)) := by rw [fourierSeq, List.map_map] exact List.map_congr_left (fun k _ => sign_iterate_derivative_eval_zero p k) /-- Mathlib's coefficient sign list is the reverse of the tower's sign list (high-to-low vslow-to-high). -/theorem coeffList_signs_eq_reverse (p : Polynomial ℝ) (hp : p ≠ 0) : (coeffList p).map SignType.sign = ((fourierSeq p).map (fun q => SignType.sign (q.eval 0))).reverse := by rw [fourierSeq_eval_zero_signs, coeffList_of_ne_zero hp] simp only [List.map_reverse, List.map_map, Function.comp_def] /-! ### Descartes' count is the engine's `V(0)` -/ /-- **Descartes through the engine.** The Budan–Fourier sign-variation count of `p` at `0` equals thecoefficient sign-variation count `Polynomial.signVariations p` of Descartes' rule of signs. -/public theorem fourierVar_zero_eq_signVariations (p : Polynomial ℝ) (hp : p ≠ 0) : fourierVar p 0 = Polynomial.signVariations p := by rw [fourierVar_eq_signVarAt, Sturm.signVarAt_eq_signChanges] have : Polynomial.signVariations p = Sturm.signChanges ((coeffList p).map SignType.sign) := rfl rw [this, coeffList_signs_eq_reverse p hp, signChanges_reverse] end BudanFourier