/-
Henstock–Kurzweil programme, Part 13 (InfiniteHK13).

File 6: the normalised CutOut for the multimode class (Theorems B.2 and B.3).

Author: Yu. N. Berdinsky

The assembly is the sharpened assembly of `SharpenedAssembly.lean` applied to the
truncated determinant of the multimode class.  As in InfiniteHK12 the analytic estimates
proved in the main text (reality of the determinant, the inner Gribov bound, the
concentration bound and the non-degeneracy bound for the perturbed determinant) enter as
explicit hypotheses; everything else — the numerator and denominator estimates, the
Fresnel factor, the volume ratio and the limit — is machine-checked.

Machine-verified here:

* `InfiniteHK13.theoremB_multimode` — the normalised CutOut for `m` modes, with the
  sharpened rate `C'(2N+1)^{12}e^{−bN+2η_N}`;
* `InfiniteHK13.theoremB2_infinite_cosets` (rank Λ < 4) and
  `InfiniteHK13.theoremB3_finite_cosets` (Λ = ℤ⁴): the two regimes of the coset
  decomposition give the same CutOut statement.

No `sorry`, no new axioms.
-/
import Mathlib
import InfiniteHK12.Lean.Main
import InfiniteHK13.Lean.MultimodeBudget
import InfiniteHK13.Lean.SharpenedAssembly

open scoped BigOperators Topology

open Filter Finset Real

open InfiniteHK10 (Mode modes)

open InfiniteHK11 (Cfg boxB)

open InfiniteHK12 (Amp cutRatio)

noncomputable section

namespace InfiniteHK13

/-- The half-width of the inner box, as in InfiniteHK12. -/
def innerWidth (gam : ℝ) : ℝ := InfiniteHK12.innerWidth gam

lemma innerWidth_pos {gam : ℝ} (hgam : 0 < gam) : 0 < innerWidth gam :=
  InfiniteHK12.innerWidth_pos hgam

lemma innerWidth_le_quarter (gam : ℝ) : innerWidth gam ≤ π / 4 :=
  InfiniteHK12.innerWidth_le_quarter gam

lemma six_mul_innerWidth_sq {gam : ℝ} (hgam : 0 < gam) :
    6 * gam * (innerWidth gam) ^ 2 ≤ 1 :=
  InfiniteHK12.six_mul_innerWidth_sq hgam

/-- **Theorem B (multimode, sharpened).**  Let `K = {k_1,…,k_m}` be pairwise distinct
non-zero modes with transverse amplitudes, let `ε_N` be a common `N`-dependent amplitude
and let `Δ_N` be the (real) truncated Faddeev–Popov determinant of the multimode class.
Assume, as proved in the main text:

* the inner bound: the box of half-width `c(γ)` lies in the Gribov region of the class;
* non-negativity of `Δ_N` there;
* the concentration bound outside that box, with rate `e^{−bN+η_N}`;
* the non-degeneracy bound `Δ_N ≥ ½Δ_N(0)e^{−η_N}` on the box of the **sharpened** radius
  `ρ_N^*`;
* the error budget `η_N = o(N)` (Theorem of `MultimodeBudget.lean`: this holds as soon as
  `√N ε_N → 0`, e.g. for `ε_N = N^{−δ}` with `δ > 1/2`).

Then the normalised CutOut ratio of the multimode class tends to zero, with the sharpened
polynomial factor `(2N+1)^{12}`. -/
theorem theoremB_multimode
    {gam R b Kb : ℝ} (hgam : 0 < gam) (hR : π ≤ R) (hb : 0 < b) (hKb : 0 < Kb)
    (Δ : ℕ → Cfg → ℝ) (hcont : ∀ N, Continuous (Δ N))
    (Om : ℕ → Set Cfg)
    {D0 : ℕ → ℝ} (hD0 : ∀ N, 0 < D0 N)
    {eta : ℕ → ℝ}
    (heta : Tendsto (fun N : ℕ => eta N / (N : ℝ)) atTop (𝓝 0))
    (hgrib : ∀ N, boxB (innerWidth gam) ⊆ Om N)
    (hinner : ∀ N, ∀ A ∈ boxB (innerWidth gam), 0 ≤ Δ N A)
    (hconc : ∀ N, ∀ A ∈ boxB R \ boxB (innerWidth gam),
      |Δ N A| ≤ D0 N * Kb * Real.exp (-(b * (N : ℝ)) + eta N))
    (hnondeg : ∀ N, ∀ A ∈ boxB (rhoStar N), D0 N * Real.exp (-(eta N)) / 2 ≤ Δ N A) :
    Tendsto (fun N => ‖cutRatio Δ Om gam R N‖) atTop (𝓝 0) := by
  have hcpos : 0 < innerWidth gam := innerWidth_pos hgam
  have hcq : innerWidth gam ≤ π / 4 := innerWidth_le_quarter gam
  have hcR : innerWidth gam ≤ R := le_trans hcq (by linarith [pi_pos])
  exact cutRatio_tendsto_zero_sharp hgam hR hb hKb hcpos hcR (six_mul_innerWidth_sq hgam)
    Δ hcont Om D0 hD0 eta heta hconc hgrib hinner hnondeg

/-- **Theorem B.2 (infinite cosets, `rank Λ < 4`).**  If some non-zero integer vector is
orthogonal to all the perturbation modes, the sublattice `Λ` is proper, the coset
decomposition of the mode lattice is non-trivial (and its cosets are infinite), and the
CutOut statement of `theoremB_multimode` holds verbatim. -/
theorem theoremB2_infinite_cosets {m : ℕ} (d : MultiData m) (v : Mode) (hv0 : v ≠ 0)
    (hv : ∀ j, dotMode v (d.k j) = 0)
    {gam R b Kb : ℝ} (hgam : 0 < gam) (hR : π ≤ R) (hb : 0 < b) (hKb : 0 < Kb)
    (Δ : ℕ → Cfg → ℝ) (hcont : ∀ N, Continuous (Δ N))
    (Om : ℕ → Set Cfg)
    {D0 : ℕ → ℝ} (hD0 : ∀ N, 0 < D0 N)
    {eta : ℕ → ℝ}
    (heta : Tendsto (fun N : ℕ => eta N / (N : ℝ)) atTop (𝓝 0))
    (hgrib : ∀ N, boxB (innerWidth gam) ⊆ Om N)
    (hinner : ∀ N, ∀ A ∈ boxB (innerWidth gam), 0 ≤ Δ N A)
    (hconc : ∀ N, ∀ A ∈ boxB R \ boxB (innerWidth gam),
      |Δ N A| ≤ D0 N * Kb * Real.exp (-(b * (N : ℝ)) + eta N))
    (hnondeg : ∀ N, ∀ A ∈ boxB (rhoStar N), D0 N * Real.exp (-(eta N)) / 2 ≤ Δ N A) :
    (∃ n : Mode, n ∉ latticeK d)
      ∧ Tendsto (fun N => ‖cutRatio Δ Om gam R N‖) atTop (𝓝 0) :=
  ⟨exists_two_cosets_of_transverse_functional d v hv0 hv,
    theoremB_multimode hgam hR hb hKb Δ hcont Om hD0 heta hgrib hinner hconc hnondeg⟩

/-- **Theorem B.3 (finite cosets, `Λ = ℤ⁴`).**  If the perturbation modes generate the
whole mode lattice there is a single coset, the truncated determinant does not factorise,
and the CutOut statement holds with the same rate. -/
theorem theoremB3_finite_cosets {m : ℕ} (d : MultiData m) (hfull : latticeK d = ⊤)
    {gam R b Kb : ℝ} (hgam : 0 < gam) (hR : π ≤ R) (hb : 0 < b) (hKb : 0 < Kb)
    (Δ : ℕ → Cfg → ℝ) (hcont : ∀ N, Continuous (Δ N))
    (Om : ℕ → Set Cfg)
    {D0 : ℕ → ℝ} (hD0 : ∀ N, 0 < D0 N)
    {eta : ℕ → ℝ}
    (heta : Tendsto (fun N : ℕ => eta N / (N : ℝ)) atTop (𝓝 0))
    (hgrib : ∀ N, boxB (innerWidth gam) ⊆ Om N)
    (hinner : ∀ N, ∀ A ∈ boxB (innerWidth gam), 0 ≤ Δ N A)
    (hconc : ∀ N, ∀ A ∈ boxB R \ boxB (innerWidth gam),
      |Δ N A| ≤ D0 N * Kb * Real.exp (-(b * (N : ℝ)) + eta N))
    (hnondeg : ∀ N, ∀ A ∈ boxB (rhoStar N), D0 N * Real.exp (-(eta N)) / 2 ≤ Δ N A) :
    (∀ n n' : Mode, n - n' ∈ latticeK d)
      ∧ Tendsto (fun N => ‖cutRatio Δ Om gam R N‖) atTop (𝓝 0) :=
  ⟨single_coset_of_top d hfull,
    theoremB_multimode hgam hR hb hKb Δ hcont Om hD0 heta hgrib hinner hconc hnondeg⟩

end InfiniteHK13
