/-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.Algebra.Polynomial.Rootspublic import Mathlib.Topology.Algebra.Polynomialpublic import Mathlib.Topology.Order.IntermediateValuepublic import Mathlib.Tactic.Linarithpublic import Mathlib.Tactic.ComputeDegreepublic import Mathlib.Tactic.NormNumpublic import Mathlib.Tactic.FinCases /-!# Isolation count of real roots by sign changes A real polynomial of degree `d` with `d` pairwise disjoint ordered intervals, each a strict signchange or a degenerate exact root (`IsoOK`), has exactly one root in each interval, no other roots,and all roots simple. The number of roots above a threshold `q` that cuts no interval equals thenumber of intervals whose right end exceeds `q`. A variant takes the order only between neighbouringintervals. ## Main results * `IsolationCount.isolation_roots`: one root per interval, strictly monotone, no other roots, all simple* `IsolationCount.isolation_count_above`: number of distinct roots above q = number of intervals with right end above q* `IsolationCount.isolation_count_above_mult`: the same count with multiplicity* `IsolationCount.iso_order_of_neighbors`: order between neighbouring intervals implies order for all pairs* `IsolationCount.isolation_count_above_nb`: the count with only neighbour-order hypotheses ## References * Mathlib: `intermediate_value_Ioo`, `Polynomial.card_roots'`. No Sturm chain is used: only sign changes.-/ @[expose] public section namespace IsolationCount open Polynomial Finset /-- Each interval is a strict sign change, or a degenerate exact root. -/def IsoOK (p : ℝ[X]) (a b : ℝ) : Prop := (a < b ∧ p.eval a * p.eval b < 0) ∨ (a = b ∧ p.eval a = 0) /-- Exactly one root per interval, strictly monotone, and no other roots (all simple). -/theorem isolation_roots {p : ℝ[X]} {d : ℕ} (hd : 0 < d) (hdeg : p.natDegree = d) (a b : Fin d → ℝ) (hint : ∀ i, IsoOK p (a i) (b i)) (hord : ∀ i j : Fin d, i < j → b i < a j) : ∃ r : Fin d → ℝ, StrictMono r ∧ (∀ i, a i ≤ r i ∧ r i ≤ b i ∧ (a i < b i → a i < r i ∧ r i < b i)) ∧ p.roots.toFinset = univ.image r ∧ p.roots.Nodup := by classical have hp : p ≠ 0 := by intro h; rw [h, natDegree_zero] at hdeg; omega have hgap : ∀ i, ∃ z : ℝ, a i ≤ z ∧ z ≤ b i ∧ p.IsRoot z ∧ (a i < b i → a i < z ∧ z < b i) := by intro i rcases hint i with ⟨hab, hs⟩ | ⟨hab, hz⟩ · rcases mul_neg_iff.mp hs with ⟨ha, hb⟩ | ⟨ha, hb⟩ · obtain ⟨z, hz, hzero⟩ := intermediate_value_Ioo' hab.le p.continuous.continuousOn (show (0 : ℝ) ∈ Set.Ioo (p.eval (b i)) (p.eval (a i)) from ⟨hb, ha⟩) exact ⟨z, hz.1.le, hz.2.le, hzero, fun _ => hz⟩ · obtain ⟨z, hz, hzero⟩ := intermediate_value_Ioo hab.le p.continuous.continuousOn (show (0 : ℝ) ∈ Set.Ioo (p.eval (a i)) (p.eval (b i)) from ⟨ha, hb⟩) exact ⟨z, hz.1.le, hz.2.le, hzero, fun _ => hz⟩ · refine ⟨a i, le_rfl, hab.le, hz, fun h => absurd h (by rw [hab]; exact lt_irrefl _)⟩ choose r hr using hgap have hmono : StrictMono r := by intro i j hij exact lt_of_le_of_lt (hr i).2.1 (lt_of_lt_of_le (hord i j hij) (hr j).1) have hsub : univ.image r ⊆ p.roots.toFinset := by intro z hz obtain ⟨i, _, rfl⟩ := Finset.mem_image.mp hz exact Multiset.mem_toFinset.mpr ((mem_roots hp).mpr (hr i).2.2.1) have hcard : (univ.image r).card = d := by rw [Finset.card_image_of_injective _ hmono.injective]; simp have hle : p.roots.card ≤ d := hdeg ▸ card_roots' p have hge : d ≤ p.roots.toFinset.card := hcard ▸ Finset.card_le_card hsub have htoF : p.roots.toFinset.card ≤ p.roots.card := Multiset.toFinset_card_le _ have heq : univ.image r = p.roots.toFinset := Finset.eq_of_subset_of_card_le hsub (by omega) refine ⟨r, hmono, fun i => ⟨(hr i).1, (hr i).2.1, (hr i).2.2.2⟩, heq.symm, ?_⟩ exact Multiset.toFinset_card_eq_card_iff_nodup.mp (by omega) /-- Count of distinct roots strictly above `q`. -/theorem isolation_count_above {p : ℝ[X]} {d : ℕ} (hd : 0 < d) (hdeg : p.natDegree = d) (a b : Fin d → ℝ) (hint : ∀ i, IsoOK p (a i) (b i)) (hord : ∀ i j : Fin d, i < j → b i < a j) (q : ℝ) (hq : ∀ i, q ≤ a i ∨ b i ≤ q) : (p.roots.toFinset.filter (fun x => q < x)).card = (univ.filter (fun i : Fin d => q < b i)).card := by classical obtain ⟨r, hmono, hr, hset, _⟩ := isolation_roots hd hdeg a b hint hord have key : ∀ i, q < r i ↔ q < b i := by intro i obtain ⟨h1, h2, h3⟩ := hr i constructor · intro h; linarith · intro h rcases hq i with hqa | hbq · rcases lt_or_eq_of_le hqa with h' | h' · linarith · exact h' ▸ (h3 (by linarith)).1 · linarith have : p.roots.toFinset.filter (fun x => q < x) = (univ.filter (fun i : Fin d => q < b i)).image r := by rw [hset] ext x simp only [Finset.mem_filter, Finset.mem_image, Finset.mem_univ, true_and] constructor · rintro ⟨⟨i, rfl⟩, h⟩ exact ⟨i, (key i).mp h, rfl⟩ · rintro ⟨i, h, rfl⟩ exact ⟨⟨i, rfl⟩, (key i).mpr h⟩ rw [this, Finset.card_image_of_injective _ hmono.injective] /-- Same count, with multiplicity (roots are simple), in the `MultiplicityCount` form. -/theorem isolation_count_above_mult {p : ℝ[X]} {d : ℕ} (hd : 0 < d) (hdeg : p.natDegree = d) (a b : Fin d → ℝ) (hint : ∀ i, IsoOK p (a i) (b i)) (hord : ∀ i j : Fin d, i < j → b i < a j) (q : ℝ) (hq : ∀ i, q ≤ a i ∨ b i ≤ q) : (p.roots.filter (fun x => q < x)).card = (univ.filter (fun i : Fin d => q < b i)).card := by classical obtain ⟨r, hmono, hr, hset, hnd⟩ := isolation_roots hd hdeg a b hint hord rw [← isolation_count_above hd hdeg a b hint hord q hq, ← Multiset.toFinset_card_of_nodup (hnd.filter (fun x => q < x)), Multiset.toFinset_filter] /-- Order by neighbours: `a i ≤ b i` and only the `n` inequalities`b (castSucc i) < a (succ i)` give `b i < a j` for all `i < j` in `Fin (n+1)`. -/theorem iso_order_of_neighbors {n : ℕ} (a b : Fin (n + 1) → ℝ) (hab : ∀ i, a i ≤ b i) (hn : ∀ i : Fin n, b i.castSucc < a i.succ) : ∀ i j : Fin (n + 1), i < j → b i < a j := by have hmono : StrictMono a := by rw [Fin.strictMono_iff_lt_succ] intro i exact lt_of_le_of_lt (hab _) (hn i) intro i j hij obtain ⟨i', rfl⟩ := Fin.exists_castSucc_eq.mpr (Fin.ne_last_of_lt hij) exact lt_of_lt_of_le (hn i') (hmono.monotone (Fin.castSucc_lt_iff_succ_le.mp hij)) /-- `isolation_count_above` with the order given only between neighbouring intervals. -/theorem isolation_count_above_nb {p : ℝ[X]} {n : ℕ} (hdeg : p.natDegree = n + 1) (a b : Fin (n + 1) → ℝ) (hint : ∀ i, IsoOK p (a i) (b i)) (hn : ∀ i : Fin n, b i.castSucc < a i.succ) (q : ℝ) (hq : ∀ i, q ≤ a i ∨ b i ≤ q) : (p.roots.toFinset.filter (fun x => q < x)).card = (univ.filter (fun i : Fin (n + 1) => q < b i)).card := by have hab : ∀ i, a i ≤ b i := by intro i rcases hint i with ⟨h, _⟩ | ⟨h, _⟩ · exact h.le · exact h.le exact isolation_count_above (Nat.succ_pos n) hdeg a b hint (iso_order_of_neighbors a b hab hn) q hq end IsolationCount