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

File 4: band structure and coset decomposition for the multimode class.

Author: Yu. N. Berdinsky 

Machine-verified here:

* `InfiniteHK13.theorem_bandWidth_multimode` (Theorem B.1) — a non-zero block joins two
  modes at sup-distance at most `W_K = max_j |k_j|_∞`, independently of `N`;
* `InfiniteHK13.coupling_mem_lattice` — a non-zero block joins two modes differing by an
  element of the sublattice `Λ = ⟨k_1,…,k_m⟩`, so the operator preserves the cosets;
* `InfiniteHK13.det_split_coset` — the truncated determinant factorises over the coset
  decomposition (the two-block form of the factorisation, which iterates);
* `InfiniteHK13.exists_two_cosets_of_transverse_functional` — if the modes `k_j` fail to
  span `ℤ⁴` over `ℚ` (rank `Λ < 4`) the decomposition is non-trivial;
* `InfiniteHK13.single_coset_of_top` — if `Λ = ℤ⁴` there is a single coset.

No `sorry`, no new axioms.
-/
import Mathlib
import InfiniteHK13.Lean.MultimodeClass

open scoped BigOperators

open Finset Real Matrix

open InfiniteHK10 (Mode modes)

open InfiniteHK11 (Cfg)

open InfiniteHK12 (Idx modeInfty modeInfty_neg modeInfty_zero)

noncomputable section

namespace InfiniteHK13

/-! ## 1. The band-width bound -/

/-- **Theorem B.1 (banded structure).**  If the block `(n,n')` of the truncated
Faddeev–Popov matrix of the multimode class is non-zero then `|n − n'|_∞ ≤ W_K`, where
`W_K = max_j |k_j|_∞` does not depend on the truncation level `N`. -/
theorem theorem_bandWidth_multimode {m : ℕ} (d : MultiData m) (eps : Fin m → ℝ) (N : ℕ)
    (A : Cfg) (p q : Idx N) (h : fpMatrixM d eps N A p q ≠ 0) :
    modeInfty ((p.1 : Mode) - (q.1 : Mode)) ≤ bandWidth d := by
  rcases exists_coupling_of_ne_zero d eps N A p q h with h0 | ⟨j, hj⟩
  · rw [h0, sub_self, modeInfty_zero]
    exact Nat.zero_le _
  · rcases hj with hj | hj
    · have he : (p.1 : Mode) - (q.1 : Mode) = d.k j := by rw [hj]; ring
      rw [he]
      exact modeInfty_le_bandWidth d j
    · have he : (p.1 : Mode) - (q.1 : Mode) = -d.k j := by rw [hj]; ring
      rw [he, modeInfty_neg]
      exact modeInfty_le_bandWidth d j

/-- The block vanishes as soon as the two modes are farther apart than the band width. -/
theorem fpMatrixM_eq_zero_of_far {m : ℕ} (d : MultiData m) (eps : Fin m → ℝ) (N : ℕ)
    (A : Cfg) (p q : Idx N) (h : bandWidth d < modeInfty ((p.1 : Mode) - (q.1 : Mode))) :
    fpMatrixM d eps N A p q = 0 := by
  by_contra hne
  exact absurd (theorem_bandWidth_multimode d eps N A p q hne) (not_le.mpr h)

/-! ## 2. The sublattice `Λ = ⟨k_1, …, k_m⟩` and the coset decomposition -/

/-- The sublattice `Λ = ⟨k_1, …, k_m⟩ ⊆ ℤ⁴` generated by the perturbation modes. -/
def latticeK {m : ℕ} (d : MultiData m) : AddSubgroup Mode :=
  AddSubgroup.closure (Set.range d.k)

lemma mem_latticeK {m : ℕ} (d : MultiData m) (j : Fin m) : d.k j ∈ latticeK d :=
  AddSubgroup.subset_closure ⟨j, rfl⟩

/-- **The coupling preserves the cosets of `Λ`.**  A non-zero block joins two modes whose
difference lies in `Λ`. -/
theorem coupling_mem_lattice {m : ℕ} (d : MultiData m) (eps : Fin m → ℝ) (N : ℕ) (A : Cfg)
    (p q : Idx N) (h : fpMatrixM d eps N A p q ≠ 0) :
    (q.1 : Mode) - (p.1 : Mode) ∈ latticeK d := by
  rcases exists_coupling_of_ne_zero d eps N A p q h with h0 | ⟨j, hj⟩
  · rw [h0, sub_self]
    exact zero_mem _
  · rcases hj with hj | hj
    · have he : (q.1 : Mode) - (p.1 : Mode) = -d.k j := by rw [hj]; ring
      rw [he]
      exact neg_mem (mem_latticeK d j)
    · have he : (q.1 : Mode) - (p.1 : Mode) = d.k j := by rw [hj]; ring
      rw [he]
      exact mem_latticeK d j

/-- **Factorisation of the truncated determinant over an invariant family of modes.**
If a property `P` of the retained modes is preserved by the coupling, the determinant is
the product of the determinants of the two diagonal blocks. -/
theorem det_split_invariant {m : ℕ} (d : MultiData m) (eps : Fin m → ℝ) {N : ℕ} (A : Cfg)
    (P : Idx N → Prop) [DecidablePred P]
    (hP : ∀ p q, fpMatrixM d eps N A p q ≠ 0 → P q → P p) :
    (fpMatrixM d eps N A).det
      = (Matrix.toSquareBlockProp (fpMatrixM d eps N A) P).det
        * (Matrix.toSquareBlockProp (fpMatrixM d eps N A) (fun p => ¬ P p)).det := by
  refine Matrix.twoBlockTriangular_det (fpMatrixM d eps N A) P ?_
  intro i hi j hj
  by_contra hne
  exact hi (hP i j hne hj)

open scoped Classical in
/-- **Factorisation of the truncated determinant over the cosets.**  For every mode `n₀`
the determinant of the truncated Faddeev–Popov matrix of the multimode class is the
product of the determinant of the block carried by the coset `n₀ + Λ` and the determinant
of the block carried by its complement.  Iterating the identity over a system of
representatives factorises the determinant over the whole coset decomposition. -/
theorem det_split_coset {m : ℕ} (d : MultiData m) (eps : Fin m → ℝ) (N : ℕ) (A : Cfg)
    (n0 : Mode) :
    (fpMatrixM d eps N A).det
      = (Matrix.toSquareBlockProp (fpMatrixM d eps N A)
            (fun p => (p.1 : Mode) - n0 ∈ latticeK d)).det
        * (Matrix.toSquareBlockProp (fpMatrixM d eps N A)
            (fun p => ¬ ((p.1 : Mode) - n0 ∈ latticeK d))).det := by
  refine det_split_invariant d eps A _ ?_
  intro i j hne hj
  have hstep := coupling_mem_lattice d eps N A i j hne
  have he : (i.1 : Mode) - n0 = ((j.1 : Mode) - n0) - ((j.1 : Mode) - (i.1 : Mode)) := by ring
  rw [he]
  exact sub_mem hj hstep

/-! ## 3. Non-triviality of the decomposition -/

/-- The integer pairing of two modes. -/
def dotMode (v n : Mode) : ℤ := ∑ mu, v mu * n mu

lemma dotMode_add (v n n' : Mode) : dotMode v (n + n') = dotMode v n + dotMode v n' := by
  unfold dotMode
  rw [← Finset.sum_add_distrib]
  exact Finset.sum_congr rfl fun mu _ => by simp [Pi.add_apply]; ring

lemma dotMode_zero (v : Mode) : dotMode v 0 = 0 := by simp [dotMode]

lemma dotMode_neg (v n : Mode) : dotMode v (-n) = -dotMode v n := by
  unfold dotMode
  rw [← Finset.sum_neg_distrib]
  exact Finset.sum_congr rfl fun mu _ => by simp [Pi.neg_apply]

/-- A functional orthogonal to all the generators vanishes on the whole sublattice. -/
lemma dotMode_eq_zero_on_latticeK {m : ℕ} (d : MultiData m) (v : Mode)
    (hv : ∀ j, dotMode v (d.k j) = 0) {n : Mode} (hn : n ∈ latticeK d) : dotMode v n = 0 := by
  refine AddSubgroup.closure_induction (p := fun x _ => dotMode v x = 0) ?_ ?_ ?_ ?_ hn
  · rintro x ⟨j, rfl⟩
    exact hv j
  · exact dotMode_zero v
  · intro x y _ _ hx hy
    rw [dotMode_add, hx, hy, add_zero]
  · intro x _ hx
    rw [dotMode_neg, hx, neg_zero]

/-- **Non-triviality of the coset decomposition (the case `rank Λ < 4`).**  If some
non-zero integer vector `v` is orthogonal to every perturbation mode — equivalently, if
the `k_j` do not span `ℚ⁴` — then `Λ` is a proper sublattice and there exist modes lying
in different cosets; the truncated determinant factorises non-trivially. -/
theorem exists_two_cosets_of_transverse_functional {m : ℕ} (d : MultiData m) (v : Mode)
    (hv0 : v ≠ 0) (hv : ∀ j, dotMode v (d.k j) = 0) :
    ∃ n : Mode, n ∉ latticeK d := by
  obtain ⟨mu, hmu⟩ : ∃ mu, v mu ≠ 0 := by
    by_contra h
    push_neg at h
    exact hv0 (funext h)
  refine ⟨fun nu => if nu = mu then 1 else 0, fun hmem => ?_⟩
  have h := dotMode_eq_zero_on_latticeK d v hv hmem
  rw [dotMode] at h
  simp only [mul_ite, mul_one, mul_zero, Finset.sum_ite_eq' Finset.univ mu v,
    Finset.mem_univ, if_true] at h
  exact hmu h

/-- **The case `Λ = ℤ⁴`.**  If the perturbation modes generate the whole mode lattice
there is a single coset and the factorisation is trivial. -/
theorem single_coset_of_top {m : ℕ} (d : MultiData m) (h : latticeK d = ⊤) (n n' : Mode) :
    n - n' ∈ latticeK d := by
  rw [h]
  exact AddSubgroup.mem_top _

end InfiniteHK13
