79 Navier–Stokes: Map, Computations and Proved Parts

79.1 Truncation-uniform equicontinuity

If \(Z(a_t)\le Z_b\) and \(A(a_t)\le A_a\) on the interval: \(\sum _r|a_{t+n,r}-a_{t,r}|^2\le n^2\cdot 2h^2A_a(135\iota ^2Z_b+\nu ^2)\). Lipschitz in time, with a constant independent of the number of modes.

ns/EquiCont.lean

/-
  ns/EquiCont.lean — T37 cycle 1081 (goal G656c): truncation-uniform equicontinuity of the implicit-viscosity
  Fourier–Galerkin scheme, over an ordered field. If on the time interval Z(a_t) ≤ Zb and A(a_t) ≤ Aa (EnstLocalTime),
  and the force satisfies Σ|N_t|² ≤ Σ|Nl(a_t,a_t)|² (e.g. Leray-projected), then for every t, n:
    equicont   Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n·n·(2h²·Aa·(135ι²·Zb + ν²)),
  i.e. |a(t + nh) − a(t)|_{L²} ≤ (nh)·sqrt(2Aa(135ι²Zb + ν²)): Lipschitz in time with a constant independent of the
  number of modes (TimeLip + ForceUniform + TimeLipN).

  Lean 4 core plus ns/TimeLipN, ns/TimeLip, ns/ForceUniform, ns/NormCompare, ns/EnstGalerkin.
-/
import ns.TimeLipN
import ns.TimeLip
import ns.EnstGalerkin
import ns.TruncRate

namespace EquiCont
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff TimeLip TimeLipN

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem sumN_const_le (f : Nat → R) (C : R) (h : ∀ j, Le K O.toCone (f j) C) : ∀ n,
    Le K O.toCone (sumN K f n) (K.mul (castN K n) C)
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero C; lib_ring⟩
  | n + 1 => by
    have ih := sumN_const_le f C h n
    have s := le_add' K O.toCone _ _ _ _ ih (h n)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (castN K n) C) C) K.zero = K.mul (K.add K.one (castN K n)) C
    lib_ring

include mul_inv h10 in
theorem equicont (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Zb Aa : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hZ : ∀ t, Le K O.toCone (Zs K l kz a t) Zb) (hA : ∀ t, Le K O.toCone (As K l kz a t) Aa) (t n : Nat) :
    Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (a (t + n) r) (a t r)) (sub3 K (a (t + n) r) (a t r))) l)
      (K.mul (castN K n) (K.mul (castN K n)
        (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul Aa (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- one step bound
  have step : ∀ j, Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (a (t + j + 1) r) (a (t + j) r)) (sub3 K (a (t + j + 1) r) (a (t + j) r))) l)
      (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul Aa (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)))) := by
    intro j
    have tl := time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (t + j + 1)) (a (t + j)) (N (t + j)) (hs (t + j))
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a (t + j)) (a (t + j))
    have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (a (t + j) p))) l
    -- Σ|N|² ≤ ι²·A·(135·Z) ≤ 135 ι² Aa Zb
    have b1 := le_trans' K O.toCone _ _ _ (hN (t + j)) fu
    have nA : O.nn (As K l kz a (t + j)) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZ : O.nn (Zs K l kz a (t + j)) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l
    have b2 : Le K O.toCone (lsum K (fun r => dot3 K (N (t + j) r) (N (t + j) r)) l)
        (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) Zb))) := by
      refine le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) ?_)
      exact TruncRate.mm K O _ _ _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZb) (le_trans' K O.toCone _ _ _ hH (hA (t + j)))
        (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) (hZ (t + j)))
    have s1 := le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq h)) b2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) (hA (t + j)))
    refine le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ s1 ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add _ _) K.zero = _
    lib_ring
  have ln := lip_n K O.toCone l (fun j => a (t + j)) n
  have sc := sumN_const_le K O _ _ step n
  have m := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) sc
  exact le_trans' K O.toCone _ _ _ ln m

end Gen

end EquiCont

#print axioms EquiCont.sumN_const_le
#print axioms EquiCont.equicont

#print axioms: 2 theorems, no axioms.

79.2 The viscous part of the step-size defect

For \(x\ge 0\) and \(c=(1+2x)(1+x)^2\): \(c\,y=x^2a_0\) implies \(y^2\le x^2a_0^2\). Bounds the \(a_0\)-term of the one-step/two-step defect by \(k^2\nu ^2w^2|a_0|^2\).

ns/DefectA0.lean

/-
  ns/DefectA0.lean — T37 cycle 1085 (goal G717, piece a): the viscous part of the one-step/two-step defect. Over an
  ordered ring without zero divisors, for x ≥ 0 and c = (1 + 2x)(1 + x)²:
    c_ge_x        x ≤ c
    defect_a0     c·y = x²·a₀  ⇒  y² ≤ x²·a₀²
  (c²y² = x⁴a₀² ≤ x²c²a₀², cancel c² ≠ 0). With x = kνw this bounds the a₀-term of StepHalf.half_defect by
  k²ν²w²|a₀|², i.e. Z-energy ≤ k²ν²·Σw³|a₀|² = k²ν²B (EnstH2 controls Σ_t k B_t).

  Lean 4 core plus ns/EnstGlobal (le_of_mul_le), ns/EnstLocal (sq_mono), lib/RingTac.
-/
import ns.EnstGlobal
import ns.EnstLocal
import ns.ForceSob

namespace DefectA0
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def cc (x : R) : R := K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))

def k2 : R := K.add K.one K.one
def k3 : R := K.add (k2 K) K.one
def k4 : R := K.add (k2 K) (k2 K)
def k5 : R := K.add (k4 K) K.one

theorem kn (h1 : O.nn K.one) : O.nn (k2 K) ∧ O.nn (k3 K) ∧ O.nn (k4 K) ∧ O.nn (k5 K) := by
  have a : O.nn (k2 K) := O.nn_add _ _ h1 h1
  have b : O.nn (k4 K) := O.nn_add _ _ a a
  exact ⟨a, O.nn_add _ _ a h1, b, O.nn_add _ _ b h1⟩

theorem c_ge_x (x : R) (hx : O.nn x) : Le K O.toCone x (cc K x) := by
  have ⟨n2, n3, _, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add K.one (K.add (K.mul (k3 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x)))),
    O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ (O.nn_mul _ _ n3 hx)
      (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx)))), ?_⟩
  unfold cc k5 k4 k3 k2; lib_ring

theorem cc_ne (x : R) (hx : O.nn x) (h10 : K.one ≠ K.zero) : cc K x ≠ K.zero := by
  have ⟨n2, _, n4, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  intro e
  have h : Le K O.toCone K.one (cc K x) := by
    refine ⟨K.add (K.mul (k4 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
      O.nn_add _ _ (O.nn_mul _ _ n4 hx) (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
    unfold cc k5 k4 k2; lib_ring
  rw [e] at h
  have h2 : Le K O.toCone K.zero K.one := ⟨K.one, RatModel.nn_one' K O.toCone, by lib_ring⟩
  exact h10 (O.antisym _ _ h h2)

theorem defect_a0 (x y a0 : R) (hx : O.nn x) (h10 : K.one ≠ K.zero) (h : K.mul (cc K x) y = K.mul (K.mul x x) a0) :
    Le K O.toCone (K.mul y y) (K.mul (K.mul x x) (K.mul a0 a0)) := by
  have hc := c_ge_x K O x hx
  have hcn : O.nn (cc K x) := by
    cases hc with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
  have sq := EnstLocal.sq_mono K O _ _ hx hcn hc
  -- c²y² = x⁴a₀² ≤ c²x²a₀²
  have m := ForceSob.le_mul_right' K O.toCone sq (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0))
  have e : K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0)) = K.mul (K.mul (cc K x) (cc K x)) (K.mul y y) := by
    calc _ = K.mul (K.mul (K.mul x x) a0) (K.mul (K.mul x x) a0) := by lib_ring
      _ = K.mul (K.mul (cc K x) y) (K.mul (cc K x) y) := by rw [h]
      _ = _ := by lib_ring
  rw [e] at m
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne m

end Gen

end DefectA0

#print axioms DefectA0.kn
#print axioms DefectA0.c_ge_x
#print axioms DefectA0.cc_ne
#print axioms DefectA0.defect_a0

#print axioms: 4 theorems, no axioms.

79.3 Fourth-power triad inequality

For integer triads: \(|k_r|^4\le 8(|k_p|^4+|k_q|^4)\), i.e. \(\delta W_r\le 8\delta (W_p+W_q)\) for \(W=|k|^4\). Hypothesis of the constant-\(c\) output-weighted force bound.

ns/Tri4.lean

/-
  ns/Tri4.lean — T37 cycle 1089 (goal G717 preparation): the fourth-power triad inequality. For integer triads
  k_p + k_q + k_r = 0 (cast into an ordered field):
    tri4          |k_r|⁴ ≤ 8(|k_p|⁴ + |k_q|⁴)      (square tri_sq and (a+b)² ≤ 2a² + 2b²)
    delta_tri4    δ·W_r ≤ δ·8(W_p + W_q)  for W = |k|⁴ — the hypothesis of ForceSobW.total_sobWc with c = 8.

  Lean 4 core plus ns/CastZAdd (tri_sq), ns/EnstLocal (sq_mono).
-/
import ns.CastZAdd
import ns.EnstLocal

namespace Tri4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin NormCompare CubeCount CastZAdd

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def k8 : R := K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (K.add K.one K.one) (K.add K.one K.one))

theorem tri4 (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K O.toCone (K.mul (dot3 K (kvec K kr) (kvec K kr)) (dot3 K (kvec K kr) (kvec K kr)))
      (K.mul (k8 K) (K.add (K.mul (dot3 K (kvec K kp) (kvec K kp)) (dot3 K (kvec K kp) (kvec K kp)))
        (K.mul (dot3 K (kvec K kq) (kvec K kq)) (dot3 K (kvec K kq) (kvec K kq))))) := by
  have t := tri_sq K O.toCone kp kq kr h
  have nr : O.nn (dot3 K (kvec K kr) (kvec K kr)) := ForceBound.dot3_nn K O.toCone _
  generalize dot3 K (kvec K kp) (kvec K kp) = a at t ⊢
  generalize dot3 K (kvec K kq) (kvec K kq) = b at t ⊢
  generalize dot3 K (kvec K kr) (kvec K kr) = c at t nr ⊢
  have hs : O.nn (K.add (K.add a a) (K.add b b)) := by
    cases t with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ nr hd.1
  have sq := EnstLocal.sq_mono K O _ _ nr hs t
  refine le_trans' K O.toCone _ _ _ sq ⟨K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b))),
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_sq _), ?_⟩
  unfold k8; lib_ring

theorem delta_tri4 {I : Type} (kz : I → Mode) (p q r : I) :
    Le K O.toCone (K.mul (TriadSparse.delta K kz p q r)
        (K.mul (dot3 K (kvec K (kz r)) (kvec K (kz r))) (dot3 K (kvec K (kz r)) (kvec K (kz r)))))
      (K.mul (TriadSparse.delta K kz p q r) (K.mul (k8 K)
        (K.add (K.mul (dot3 K (kvec K (kz p)) (kvec K (kz p))) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
          (K.mul (dot3 K (kvec K (kz q)) (kvec K (kz q))) (dot3 K (kvec K (kz q)) (kvec K (kz q))))))) := by
  show Le K O.toCone (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true => exact le_mul_nn K O.toCone K.one _ _ (RatModel.nn_one' K O.toCone) (tri4 K O (kz p) (kz q) (kz r) (of_decide_eq_true hd))

end Gen

end Tri4

#print axioms Tri4.tri4
#print axioms Tri4.delta_tri4

#print axioms: 2 theorems, no axioms.

79.4 \(H^3\)-level force bound

\(\sum _rw_r^2|N_r(a,a)|^2\le \iota ^2B_4\cdot 8(S_1Z+S_0B)\), \(B_4=\sum w^4|a|^2\), \(S_1=\sum |k|_\infty ^{-8}w^2\), \(S_0=\sum |k|_\infty ^{-8}\). Constant-\(c\) output-weighted bound with \(W=|k|^4\), \(c=8\), \(\mu =|k|_\infty ^4\).

ns/ForceH3.lean

/-
  ns/ForceH3.lean — T37 cycle 1091 (goal G717 preparation): the H³-level Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|²:
    force_h3   Σ_r w_r²|N_r(a,a)|² ≤ ι²·B₄·(8·(S₁·Z + S₀·B)),   B₄ = Σ w⁴|a|²,  B = Σ w³|a|²,
  with S₁ = Σ_p |k_p|_∞^{−8}w_p² and S₀ = Σ_p |k_p|_∞^{−8}.
  ForceSobW.total_sobWc with W = |k|⁴, c = 8 (Tri4.delta_tri4), μ = |k|_∞⁴.

  Lean 4 core plus ns/ForceH2, ns/Tri4, ns/ForceSobW (total_sobWc).
-/
import ns.ForceH2
import ns.Tri4

namespace ForceH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 Tri4 LatticeStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O mul_inv h10 in
theorem munu4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (p4 K (nn K kz p)) (p4 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold p4
  calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p))) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- n⁸ ≤ w⁴ -/
theorem mu8_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (p4 K (nn K kz p)) (p4 K (nn K kz p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s2
  have e : K.mul (p4 K (nn K kz p)) (p4 K (nn K kz p))
      = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) := by unfold p4; lib_ring
  rw [e]; exact s4

include mul_inv h10 in
theorem force_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)
        (K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))))) := by
  have T := total_sobWc K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) a a
    (fun p => p4 K (nn K kz p)) (fun p => p4 K (ivn K inv kz p)) (fun r => K.mul (wt K kz r) (wt K kz r))
    (fun r => O.nn_sq _) (k8 K) (by unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))))
    (fun p q r => delta_tri4 K O kz p q r) (delta01 K kz) (munu4 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu8_le K O kz p) (dot3_nn K O.toCone (a p))) l
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
        (K.mul (k8 K) (K.add (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz q) (wt K kz q)))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (a q) (a q)))) l) l
      = K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l))) := by
    have d1 := double_factor K l (k8 K) (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p)))
      (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q)))
    have d2 := double_factor K l (k8 K) (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
      (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q)))
    rw [show K.mul (k8 K) (K.add (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))
        = K.add (K.mul (k8 K) (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (k8 K) (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l))) by lib_ring,
      ← d1, ← d2, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
        (K.mul (k8 K) (K.add (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz q) (wt K kz q)))))
        (K.mul (wt K kz q) (dot3 K (a q) (a q))) = _
    lib_ring
  rw [e]
  have nk8 : O.nn (k8 K) := by
    unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)))
  have nS : O.nn (K.mul (k8 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l))
          (K.mul (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (K.mul (wt K kz q) (wt K kz q)) (wt K kz q)) (dot3 K (a q) (a q))) l)))) :=
    O.nn_mul _ _ nk8 (O.nn_add _ _
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH3


#print axioms ForceH3.munu4
#print axioms ForceH3.mu8_le
#print axioms ForceH3.force_h3

#print axioms: 3 theorems, no axioms.

79.5 Small-data global \(H^3\) bound

If \(c_3\iota ^2(\nu B_0+h\nu ^2B_{4,0})\le \nu ^3\) with \(c_3=8(9\cdot 135+135)\), then \(\nu B_t+h\nu ^2B_{4,t}\le \nu B_0+h\nu ^2B_{4,0}\) for all \(t\). \(B=\sum w^3|a|^2\), \(B_4=\sum w^4|a|^2\); uniform in the truncation.

ns/EnstH3.lean

/-
  ns/EnstH3.lean — T37 cycle 1093 (goal G717 preparation): the small-data global H³ bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With B = Σ w³|a|², B₄ = Σ w⁴|a|² (w = |k|²):
    S0h_le / S1h_le   Σ_p |k_p|_∞^{−8} ≤ 135,  Σ_p |k_p|_∞^{−8} w_p² ≤ 9·135
    Z_le_B            Σ w|a|² ≤ Σ w³|a|²   (w ≥ 1 on nonzero integer modes)
    h3_global         c₃ι²(νB_0 + hν²B₄_0) ≤ ν³,  c₃ = 8(9·135 + 135)  ⇒  νB_t + hν²B₄_t ≤ νB_0 + hν²B₄_0 for every t
  (EnstStepV with V = w², ForceH3, then EnstGlobal.q_le with c = c₃ι²).

  Lean 4 core plus ns/ForceH3, ns/EnstH2, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound.
-/
import ns.ForceH3
import ns.EnstH2

namespace EnstH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k9 : R := K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one)

theorem nn9 : O.nn (k9 K) := by
  have three : O.nn (K.add (K.add K.one K.one) K.one) :=
    O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
  exact O.nn_mul _ _ three three

theorem nn8 : O.nn (k8 K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_add _ _ (O.nn_add _ _ two two) (O.nn_add _ _ two two)

include mul_inv h10 in
theorem pt8_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨_, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have q := TruncRate.mm K O _ _ _ _ (O.nn_sq (ivn K inv kz p)) (RatModel.nn_one' K O.toCone) hle hle
  have m := le_mul_nn K O.toCone (p4 K (ivn K inv kz p)) _ _ (by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) q
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · unfold p4; lib_ring
  · show K.add (K.mul (p4 K (ivn K inv kz p)) (K.mul K.one K.one)) K.zero = p4 K (iv K inv (nrm (kz p)))
    show K.add (K.mul (p4 K (inv (castN K (nrm (kz p))))) (K.mul K.one K.one)) K.zero = p4 K (inv (castN K (nrm (kz p))))
    unfold p4; lib_ring

include mul_inv h10 in
theorem pt8w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p)))
      (K.mul (k9 K) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have w3 := dot_le_3nrm K O (kz p)
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have w9 := TruncRate.mm K O _ _ _ _ hw (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)) w3 w3
  have m := le_mul_nn K O.toCone (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) _ _ (O.nn_sq _) w9
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p)))
      (K.mul (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))) K.zero
    = K.mul (k9 K) (p4 K (ivn K inv kz p))
  unfold p4 k9
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one))
        (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul (K.mul n v) (K.mul n v)) (K.mul (K.mul n v) (K.mul n v)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0h_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt8_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1h_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l)
      (K.mul (k9 K) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt8w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ (nn9 K O) (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))

theorem Z_le_B (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (K.mul (wt K kz p) d) (K.add (K.add K.one K.one) d)) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (dot3_nn K O.toCone _) hd.1)
        (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) hd.1)) (dot3_nn K O.toCone _), ?_⟩
    have hw : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw]; lib_ring

def c3 : R := K.mul (k8 K) (K.add (K.mul (k9 K) (castN K 135)) (castN K 135))

theorem Bform (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by lib_ring) l

include mul_inv h10 in
/-- small-data global H³ bound, uniform in the truncation -/
theorem h3_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (c3 K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc3 : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) n135) n135)
  have hc : O.nn (K.mul (c3 K) (K.mul ι ι)) := O.nn_mul _ _ nc3 (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _)
    (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l (a s)
  rw [Bform K kz l (a s)] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = Bs
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

end Gen

end EnstH3

#print axioms EnstH3.nn9
#print axioms EnstH3.nn8
#print axioms EnstH3.pt8_le4
#print axioms EnstH3.pt8w_le
#print axioms EnstH3.S0h_le
#print axioms EnstH3.S1h_le
#print axioms EnstH3.Z_le_B
#print axioms EnstH3.Bform
#print axioms EnstH3.h3_global

#print axioms: 9 theorems, no axioms.

79.6 Splitting the step-size defect

For the one-step/two-step scheme with \(x=k\nu w\): \((a_1-b_2)^2\le 3(x^2a_0^2+k^2(N_0-N_1)^2+k^2xN_1^2)\). Viscous, force-Lipschitz and \(H^3\)-force parts of the defect.

ns/DefectSplit.lean

/-
  ns/DefectSplit.lean — T37 cycle 1095 (goal G717, piece b1): the scalar splitting of the one-step/two-step defect.
  Over an ordered ring without zero divisors, with x ≥ 0, c = (1 + 2x)(1 + x)², p = 1 + 2x + 2x²:
    p_le_c, one_le_c, x_le_csq   p ≤ c,  1 ≤ c,  x ≤ c²
    split3                        (A + B + C)² ≤ 3(A² + B² + C²)
    defect_split                  c·D = x²a₀ + k·p·M − k·x·N₁  ⇒  D² ≤ 3(x²a₀² + k²M² + k²·x·N₁²)
    half_split                    for the StepHalf scheme (x = kνw, M = N₀ − N₁):
                                  (a₁ − b₂)² ≤ 3(x²a₀² + k²(N₀ − N₁)² + k²·x·N₁²)
    defect_split4, half_split4    the same with x⁴a₀² in place of x²a₀² (1 ≤ c): the viscous part is O(k²) per step
  The three terms are O(k²)·B (viscous), k²·|N₀ − N₁|² = O(k⁴) (force Lipschitz, b₁ − a₀ = O(k)), and
  k³ν·w|N₁|² (controlled by the H³ force bound ForceH3 and EnstH3).

  Lean 4 core plus ns/DefectA0, ns/StepHalf, ns/EnstGlobal, ns/EnstLocal, ns/TruncRate.
-/
import ns.DefectA0
import ns.StepHalf
import ns.TruncRate

namespace DefectSplit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal DefectA0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def pp (x : R) : R := K.add (K.add K.one (K.add x x)) (K.add (K.mul x x) (K.mul x x))

theorem p_le_c (x : R) (hx : O.nn x) : Le K O.toCone (pp K x) (cc K x) := by
  have ⟨n2, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add (K.add x x) (K.add (K.mul (k3 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
    O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ (O.nn_mul _ _ n3 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
  unfold pp cc k3 k2; lib_ring

theorem one_le_c (x : R) (hx : O.nn x) : Le K O.toCone K.one (cc K x) := by
  have ⟨n2, _, n4, n5⟩ := kn K O (RatModel.nn_one' K O.toCone)
  refine ⟨K.add (K.mul (k4 K) x) (K.add (K.mul (k5 K) (K.mul x x)) (K.mul (k2 K) (K.mul (K.mul x x) x))),
    O.nn_add _ _ (O.nn_mul _ _ n4 hx) (O.nn_add _ _ (O.nn_mul _ _ n5 (O.nn_sq x)) (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq x) hx))), ?_⟩
  unfold cc k5 k4 k2; lib_ring

theorem x_le_csq (x : R) (hx : O.nn x) : Le K O.toCone x (K.mul (cc K x) (cc K x)) := by
  have h1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) h1
  have m := le_mul_nn K O.toCone (cc K x) _ _ hcn h1
  refine le_trans' K O.toCone _ _ _ (c_ge_x K O x hx) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m)
  show K.add (cc K x) K.zero = K.mul (cc K x) K.one
  lib_ring

theorem split3 (A B C : R) :
    Le K O.toCone (K.mul (K.add (K.add A B) C) (K.add (K.add A B) C))
      (K.mul (k3 K) (K.add (K.add (K.mul A A) (K.mul B B)) (K.mul C C))) :=
  ⟨K.add (K.add (K.mul (K.add A (K.neg B)) (K.add A (K.neg B))) (K.mul (K.add B (K.neg C)) (K.add B (K.neg C))))
      (K.mul (K.add A (K.neg C)) (K.add A (K.neg C))),
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _),
    by unfold k3 k2; lib_ring⟩

theorem defect_split (x k a0 M N1 D : R) (hx : O.nn x) (h10 : K.one ≠ K.zero)
    (h : K.mul (cc K x) D = K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) :
    Le K O.toCone (K.mul D D)
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul a0 a0)) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have hc1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hc1
  have hpn : O.nn (pp K x) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_sq x) (O.nn_sq x))
  have bA := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hx hcn (c_ge_x K O x hx))
    (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0))
  have bB := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hpn hcn (p_le_c K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq M))
  have bC := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone x _ _ hx (x_le_csq K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq N1))
  have S3 := le_mul_nn K O.toCone (k3 K) _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ bA bB) bC)
  have sp := split3 K O (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M) (K.neg (K.mul (K.mul k x) N1))
  have e1 : K.mul (K.mul (cc K x) (cc K x)) (K.mul D D)
      = K.mul (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1)))
          (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) := by
    rw [← h]; lib_ring
  have T1 : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0)))
        (K.mul (K.mul (pp K x) (pp K x)) (K.mul (K.mul k k) (K.mul M M))))
        (K.mul (K.mul x x) (K.mul (K.mul k k) (K.mul N1 N1))))) := by
    rw [e1]
    refine le_trans' K O.toCone _ _ _ sp ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have T2 := le_trans' K O.toCone _ _ _ T1 S3
  have T : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (K.mul (cc K x) (cc K x)) (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul a0 a0)) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1)))))) := by
    refine le_trans' K O.toCone _ _ _ T2 ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne T

theorem half_split (k ν w a0 a1 b1 b2 N0 N1 : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    Le K O.toCone (K.mul (K.add a1 (K.neg b2)) (K.add a1 (K.neg b2)))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul a0 a0))
          (K.mul (K.mul k k) (K.mul (K.add N0 (K.neg N1)) (K.add N0 (K.neg N1)))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.mul N1 N1))))) := by
  have hx : O.nn (K.mul (K.mul k ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hk hν) hw
  have e := StepHalf.half_defect K k ν w a0 a1 b1 b2 N0 N1 h1 h2 h3
  refine defect_split K O _ k a0 _ N1 _ hx h10 ?_
  have e2 : K.mul (K.mul (K.add k k) ν) w = K.add (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w) := by lib_ring
  rw [e2] at e
  generalize K.mul (K.mul k ν) w = x at e ⊢
  unfold cc pp
  calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2)) := by lib_ring
    _ = _ := e
    _ = _ := by lib_ring

theorem defect_split4 (x k a0 M N1 D : R) (hx : O.nn x) (h10 : K.one ≠ K.zero)
    (h : K.mul (cc K x) D = K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) :
    Le K O.toCone (K.mul D D)
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have hc1 := one_le_c K O x hx
  have hcn : O.nn (cc K x) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hc1
  have hpn : O.nn (pp K x) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_sq x) (O.nn_sq x))
  have bA := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) hcn hc1)
    (O.nn_mul _ _ (O.nn_sq x) (O.nn_mul _ _ (O.nn_sq x) (O.nn_sq a0)))
  have bB := ForceSob.le_mul_right' K O.toCone (EnstLocal.sq_mono K O _ _ hpn hcn (p_le_c K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq M))
  have bC := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone x _ _ hx (x_le_csq K O x hx))
    (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq N1))
  have S3 := le_mul_nn K O.toCone (k3 K) _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ bA bB) bC)
  have sp := split3 K O (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M) (K.neg (K.mul (K.mul k x) N1))
  have e1 : K.mul (K.mul (cc K x) (cc K x)) (K.mul D D)
      = K.mul (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1)))
          (K.add (K.add (K.mul (K.mul x x) a0) (K.mul (K.mul k (pp K x)) M)) (K.neg (K.mul (K.mul k x) N1))) := by
    rw [← h]; lib_ring
  have T1 : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul K.one K.one) (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))))
        (K.mul (K.mul (pp K x) (pp K x)) (K.mul (K.mul k k) (K.mul M M))))
        (K.mul (K.mul x x) (K.mul (K.mul k k) (K.mul N1 N1))))) := by
    rw [e1]
    refine le_trans' K O.toCone _ _ _ sp ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have T2 := le_trans' K O.toCone _ _ _ T1 S3
  have T : Le K O.toCone (K.mul (K.mul (cc K x) (cc K x)) (K.mul D D))
      (K.mul (K.mul (cc K x) (cc K x)) (K.mul (k3 K) (K.add (K.add (K.mul (K.mul x x) (K.mul (K.mul x x) (K.mul a0 a0))) (K.mul (K.mul k k) (K.mul M M)))
        (K.mul (K.mul k k) (K.mul x (K.mul N1 N1)))))) := by
    refine le_trans' K O.toCone _ _ _ T2 ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  have ccne : K.mul (cc K x) (cc K x) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl h' => exact cc_ne K O x hx h10 h'
    | inr h' => exact cc_ne K O x hx h10 h'
  exact le_of_mul_le K O _ _ _ (O.nn_sq _) ccne T

theorem half_split4 (k ν w a0 a1 b1 b2 N0 N1 : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    Le K O.toCone (K.mul (K.add a1 (K.neg b2)) (K.add a1 (K.neg b2)))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul a0 a0)))
          (K.mul (K.mul k k) (K.mul (K.add N0 (K.neg N1)) (K.add N0 (K.neg N1)))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.mul N1 N1))))) := by
  have hx : O.nn (K.mul (K.mul k ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hk hν) hw
  have e := StepHalf.half_defect K k ν w a0 a1 b1 b2 N0 N1 h1 h2 h3
  refine defect_split4 K O _ k a0 _ N1 _ hx h10 ?_
  have e2 : K.mul (K.mul (K.add k k) ν) w = K.add (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w) := by lib_ring
  rw [e2] at e
  generalize K.mul (K.mul k ν) w = x at e ⊢
  unfold cc pp
  calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2)) := by lib_ring
    _ = _ := e
    _ = _ := by lib_ring

end Gen

end DefectSplit

#print axioms DefectSplit.p_le_c
#print axioms DefectSplit.one_le_c
#print axioms DefectSplit.x_le_csq
#print axioms DefectSplit.split3
#print axioms DefectSplit.defect_split
#print axioms DefectSplit.half_split
#print axioms DefectSplit.defect_split4
#print axioms DefectSplit.half_split4

#print axioms: 8 theorems, no axioms.

79.7 Weighted step-size defect

\(\sum _rw_r|a_{1,r}-b_{2,r}|^2\le 3(k^2\nu ^2\sum w^3|a_0|^2+k^2\sum w|N_0-N_1|^2+k^3\nu \sum w^2|N_1|^2)\). One step of size \(2k\) against two steps of size \(k\), summed over modes.

ns/DefectSum.lean

/-
  ns/DefectSum.lean — T37 cycle 1097 (goal G717, piece b2): the weighted one-step/two-step defect, summed over modes.
  For the implicit-viscosity scheme with one step of size 2k (a₀ → a₁, force N₀) against two steps of size k
  (a₀ → b₁ → b₂, forces N₀, N₁), over an ordered ring without zero divisors (k, ν ≥ 0, w_r ≥ 0):
    mode_half    |a₁ − b₂|² ≤ 3(x²|a₀|² + k²|N₀ − N₁|² + k²x|N₁|²),   x = kνw
    defect_sum   Σ_r w_r|a₁,r − b₂,r|² ≤ 3(k²ν²·Σ w³|a₀|² + k²·Σ w|N₀ − N₁|² + k³ν·Σ w²|N₁|²).
    defect_sumV  the weak form with a general weight V_r (V = w² gives the A-part of the defect)
    defect_sum4  the same with k⁴ν⁴·Σ w⁵|a₀|² as the first term (sharp viscous part).
  The first sum is B (EnstH2), the last is bounded by ForceH3 with the H³ energy of EnstH3.

  Lean 4 core plus ns/DefectSplit, ns/EnstDiff (sub3), ns/ForceBound (lsum_le).
-/
import ns.DefectSplit
import ns.EnstDiff

namespace DefectSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff DefectA0 DefectSplit

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem mode_half (k ν w : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (a0 a1 b1 b2 N0 N1 : V3 R) (s1 : Step K (K.add k k) ν w a1 a0 N0) (s2 : Step K k ν w b1 a0 N0)
    (s3 : Step K k ν w b2 b1 N1) :
    Le K O.toCone (dot3 K (sub3 K a1 b2) (sub3 K a1 b2))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (dot3 K a0 a0))
          (K.mul (K.mul k k) (dot3 K (sub3 K N0 N1) (sub3 K N0 N1))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (dot3 K N1 N1))))) := by
  have cx := half_split K O k ν w a0.x a1.x b1.x b2.x N0.x N1.x hk hν hw h10 s1.1 s2.1 s3.1
  have cy := half_split K O k ν w a0.y a1.y b1.y b2.y N0.y N1.y hk hν hw h10 s1.2.1 s2.2.1 s3.2.1
  have cz := half_split K O k ν w a0.z a1.z b1.z b2.z N0.z N1.z hk hν hw h10 s1.2.2 s2.2.2 s3.2.2
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ cx cy) cz
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.mul (K.add a1.x (K.neg b2.x)) (K.add a1.x (K.neg b2.x)))
        (K.mul (K.add a1.y (K.neg b2.y)) (K.add a1.y (K.neg b2.y)))) (K.mul (K.add a1.z (K.neg b2.z)) (K.add a1.z (K.neg b2.z)))) K.zero = _
    lib_ring
  · show _ = K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))
          (K.add (K.add (K.mul a0.x a0.x) (K.mul a0.y a0.y)) (K.mul a0.z a0.z)))
        (K.mul (K.mul k k) (K.add (K.add (K.mul (K.add N0.x (K.neg N1.x)) (K.add N0.x (K.neg N1.x)))
          (K.mul (K.add N0.y (K.neg N1.y)) (K.add N0.y (K.neg N1.y)))) (K.mul (K.add N0.z (K.neg N1.z)) (K.add N0.z (K.neg N1.z))))))
      (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.add (K.add (K.mul N1.x N1.x) (K.mul N1.y N1.y)) (K.mul N1.z N1.z)))))
    generalize k3 K = c
    lib_ring

theorem defect_sum (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w : I → R) (hw : ∀ r, O.nn (w r))
    (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul k k) (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (w r)) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (w r) _ _ (hw r)
    (mode_half K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

theorem mode_half4 (k ν w : R) (hk : O.nn k) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (a0 a1 b1 b2 N0 N1 : V3 R) (s1 : Step K (K.add k k) ν w a1 a0 N0) (s2 : Step K k ν w b1 a0 N0)
    (s3 : Step K k ν w b2 b1 N1) :
    Le K O.toCone (dot3 K (sub3 K a1 b2) (sub3 K a1 b2))
      (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (dot3 K a0 a0)))
          (K.mul (K.mul k k) (dot3 K (sub3 K N0 N1) (sub3 K N0 N1))))
        (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (dot3 K N1 N1))))) := by
  have cx := half_split4 K O k ν w a0.x a1.x b1.x b2.x N0.x N1.x hk hν hw h10 s1.1 s2.1 s3.1
  have cy := half_split4 K O k ν w a0.y a1.y b1.y b2.y N0.y N1.y hk hν hw h10 s1.2.1 s2.2.1 s3.2.1
  have cz := half_split4 K O k ν w a0.z a1.z b1.z b2.z N0.z N1.z hk hν hw h10 s1.2.2 s2.2.2 s3.2.2
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ cx cy) cz
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.mul (K.add a1.x (K.neg b2.x)) (K.add a1.x (K.neg b2.x)))
        (K.mul (K.add a1.y (K.neg b2.y)) (K.add a1.y (K.neg b2.y)))) (K.mul (K.add a1.z (K.neg b2.z)) (K.add a1.z (K.neg b2.z)))) K.zero = _
    lib_ring
  · show _ = K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))
          (K.add (K.add (K.mul a0.x a0.x) (K.mul a0.y a0.y)) (K.mul a0.z a0.z))))
        (K.mul (K.mul k k) (K.add (K.add (K.mul (K.add N0.x (K.neg N1.x)) (K.add N0.x (K.neg N1.x)))
          (K.mul (K.add N0.y (K.neg N1.y)) (K.add N0.y (K.neg N1.y)))) (K.mul (K.add N0.z (K.neg N1.z)) (K.add N0.z (K.neg N1.z))))))
      (K.mul (K.mul k k) (K.mul (K.mul (K.mul k ν) w) (K.add (K.add (K.mul N1.x N1.x) (K.mul N1.y N1.y)) (K.mul N1.z N1.z)))))
    generalize k3 K = c
    lib_ring

theorem defect_sum4 (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w : I → R) (hw : ∀ r, O.nn (w r))
    (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (w r) (w r)) (K.mul (w r) (w r))) (w r)) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (w r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (w r) _ _ (hw r)
    (mode_half4 K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

theorem defect_sumV (l : List I) (k ν : R) (hk : O.nn k) (hν : O.nn ν) (w V : I → R) (hw : ∀ r, O.nn (w r))
    (hV : ∀ r, O.nn (V r)) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (w r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (w r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (w r) (b2 r) (b1 r) (N1 r)) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a1 r) (b2 r)) (sub3 K (a1 r) (b2 r)))) l)
      (K.mul (k3 K) (K.add (K.add
        (K.mul (K.mul (K.mul k k) (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (V r) (K.mul (w r) (w r))) (dot3 K (a0 r) (a0 r))) l))
        (K.mul (K.mul k k) (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)))
        (K.mul (K.mul (K.mul k k) (K.mul k ν)) (lsum K (fun r => K.mul (K.mul (V r) (w r)) (dot3 K (N1 r) (N1 r))) l)))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (V r) _ _ (hV r)
    (mode_half K O k ν (w r) hk hν (hw r) h10 (a0 r) (a1 r) (b1 r) (b2 r) (N0 r) (N1 r) (s1 r) (s2 r) (s3 r))) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_mul_left, ← lsum_add, ← lsum_add, ← lsum_mul_left]
  refine lsum_ext K _ _ (fun r => ?_) l
  generalize k3 K = c
  lib_ring

end Gen

end DefectSum

#print axioms DefectSum.mode_half
#print axioms DefectSum.defect_sum
#print axioms DefectSum.mode_half4
#print axioms DefectSum.defect_sum4
#print axioms DefectSum.defect_sumV

#print axioms: 5 theorems, no axioms.

79.8 Weighted time-Lipschitz step

\(\sum _rV_r|a'_r-a_r|^2\le 2h^2\sum V_r|N_r|^2+2h^2\nu ^2\sum V_rw_r^2|a_r|^2\) for weights \(V_r\ge 0\). With \(V=w\) this is the \(O(k^2)\) input of the force difference in the step-size defect.

ns/TimeLipW.lean

/-
  ns/TimeLipW.lean — T37 cycle 1099 (goal G717, piece b3): the weighted one-step time-Lipschitz bound. Over any
  commutative ring with a nonnegative cone (h, ν, w_r, V_r ≥ 0):
    time_lipW   Σ_r V_r|a'_r − a_r|² ≤ 2h²·Σ V_r|N_r|² + 2h²ν²·Σ V_r w_r²|a_r|².
  With V = w (H¹ weight): Σ w|b₁ − a₀|² ≤ 2k²Σ w|N₀|² + 2k²ν²B, the O(k²) input of the force difference
  N₀ − N₁ in the step-size defect (DefectSum).

  Lean 4 core plus ns/TimeLip (mode_lip), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLip

namespace TimeLipW
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep TimeLip

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem time_lipW (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w V : I → R) (hw : ∀ r, P.nn (w r))
    (hV : ∀ r, P.nn (V r)) (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (lsum K (fun r => K.mul (V r) (dot3 K (EnstDiff.sub3 K (a' r) (a r)) (EnstDiff.sub3 K (a' r) (a r)))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
          (lsum K (fun r => K.mul (K.mul (V r) (K.mul (w r) (w r))) (dot3 K (a r) (a r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (V r) _ _ (hV r)
    (mode_lip K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r))) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
  exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end TimeLipW

#print axioms TimeLipW.time_lipW

#print axioms: 1 theorem, no axioms.

79.9 Bilinear \(H^1\)-weighted force bound

\(\sum _rw_r|N_r(u,v)|^2\le \iota ^2B(u)(2S_1Z(v)+2S_0A(v))\), \(B(u)=\sum w^3|u|^2\). Two different inputs, for the force difference in the step-size defect.

ns/ForceH2Bi.lean

/-
  ns/ForceH2Bi.lean — T37 cycle 1101 (goal G717, piece b4): the H¹-weighted Galerkin force bound for two different
  inputs, uniform in the truncation, over an ordered field (integer modes, nonzero, distinct on l; w = |k|²):
    force_h2_bi   Σ_r w_r|N_r(u,v)|² ≤ ι²·B(u)·(2·S₁·Z(v) + 2·S₀·A(v)),
  B(u) = Σ w³|u|², Z(v) = Σ w|v|², A(v) = Σ w²|v|², S₁, S₀ as in ForceH2 (≤ 3·135, 135 by EnstH2).
  For the force difference N(a,a) − N(b,b) = N(a − b, a) + N(b, a − b) in the step-size defect.

  Lean 4 core plus ns/ForceH2.
-/
import ns.ForceH2

namespace ForceH2Bi
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem force_h2_bi (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l)
        (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (fun p => cube3 K (nn K kz p)) (fun p => cube3 K (ivn K inv kz p)) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu6_le K O kz p) (dot3_nn K O.toCone (u p))) l
  -- the double sum factors
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
        (K.mul (wt K kz q) (dot3 K (v q) (v q)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q)))))
    show K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l)))
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH2Bi

#print axioms ForceH2Bi.force_h2_bi

#print axioms: 1 theorem, no axioms.

79.10 \(H^4\)-level bilinear force bound

\(|k_r|^6\le 32(|k_p|^6+|k_q|^6)\) on triads, and \(\sum _rw_r^3|N_r(u,v)|^2\le \iota ^2B_5(u)\cdot 32(S_1Z(v)+S_0\sum w^4|v|^2)\). \(B_5(u)=\sum w^5|u|^2\), \(S_1=\sum |k|_\infty ^{-10}w^3\), \(S_0=\sum |k|_\infty ^{-10}\).

ns/ForceH4.lean

/-
  ns/ForceH4.lean — T37 cycle 1103 (goal G717, piece b5): the H⁴-level bilinear Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|², W = w³:
    tri6        |k_r|⁶ ≤ 32(|k_p|⁶ + |k_q|⁶) for integer triads   ((a+b)³ ≤ 4(a³ + b³) for a, b ≥ 0)
    force_h4    Σ_r w_r³|N_r(u,v)|² ≤ ι²·B₅(u)·(32·(S₁·Z(v) + S₀·Σ w⁴|v|²)),   B₅(u) = Σ w⁵|u|²,
  with S₁ = Σ_p |k_p|_∞^{−10}w_p³ and S₀ = Σ_p |k_p|_∞^{−10}.
  ForceSobW.total_sobWc with W = |k|⁶, c = 32, μ = |k|_∞⁵.

  Lean 4 core plus ns/ForceH3, ns/Tri4, ns/ForceSobW (total_sobWc).
-/
import ns.ForceH3

namespace ForceH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 Tri4 LatticeStep ForceH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k32 : R := K.mul (k8 K) (K.add (K.add K.one K.one) (K.add K.one K.one))
def cb (y : R) : R := K.mul (K.mul y y) y
def q5 (x : R) : R := K.mul (p4 K x) x
def w3 (kz : I → Mode) (r : I) : R := K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)

theorem nn32 : O.nn (k32 K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ two two) (O.nn_add _ _ two two)) (O.nn_add _ _ two two)

theorem tri6 (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K O.toCone (cb K (dot3 K (kvec K kr) (kvec K kr)))
      (K.mul (k32 K) (K.add (cb K (dot3 K (kvec K kp) (kvec K kp))) (cb K (dot3 K (kvec K kq) (kvec K kq))))) := by
  have t := tri_sq K O.toCone kp kq kr h
  have na : O.nn (dot3 K (kvec K kp) (kvec K kp)) := ForceBound.dot3_nn K O.toCone _
  have nb : O.nn (dot3 K (kvec K kq) (kvec K kq)) := ForceBound.dot3_nn K O.toCone _
  have nr : O.nn (dot3 K (kvec K kr) (kvec K kr)) := ForceBound.dot3_nn K O.toCone _
  generalize dot3 K (kvec K kp) (kvec K kp) = a at t na ⊢
  generalize dot3 K (kvec K kq) (kvec K kq) = b at t nb ⊢
  generalize dot3 K (kvec K kr) (kvec K kr) = c at t nr ⊢
  have hs : O.nn (K.add (K.add a a) (K.add b b)) := O.nn_add _ _ (O.nn_add _ _ na na) (O.nn_add _ _ nb nb)
  have sq := EnstLocal.sq_mono K O _ _ nr hs t
  have c3 := TruncRate.mm K O _ _ _ _ (O.nn_sq c) hs sq t
  have one := RatModel.nn_one' K O.toCone
  refine le_trans' K O.toCone _ _ _ c3 ⟨K.mul (k8 K) (K.mul (K.add (K.add K.one K.one) K.one)
      (K.mul (K.add a b) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b))))),
    O.nn_mul _ _ (by unfold k8; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ one one) (O.nn_add _ _ one one)) (O.nn_add _ _ (O.nn_add _ _ one one) (O.nn_add _ _ one one)))
      (O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ one one) one) (O.nn_mul _ _ (O.nn_add _ _ na nb) (O.nn_sq _))), ?_⟩
  unfold k32 cb k8; lib_ring

theorem delta_tri6 (kz : I → Mode) (p q r : I) :
    Le K O.toCone (K.mul (TriadSparse.delta K kz p q r) (w3 K kz r))
      (K.mul (TriadSparse.delta K kz p q r) (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q)))) := by
  show Le K O.toCone (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true => exact le_mul_nn K O.toCone K.one _ _ (RatModel.nn_one' K O.toCone) (tri6 K O (kz p) (kz q) (kz r) (of_decide_eq_true hd))

include O mul_inv h10 in
theorem munu5 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (q5 K (nn K kz p)) (q5 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold q5 p4
  calc _ = K.mul (K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- n¹⁰ ≤ w⁵ -/
theorem mu10_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (q5 K (nn K kz p)) (q5 K (nn K kz p)))
      (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s2
  have s5 := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hw s4 h
  have e : K.mul (q5 K (nn K kz p)) (q5 K (nn K kz p))
      = K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))) (K.mul (nn K kz p) (nn K kz p)) := by
    unfold q5 p4; lib_ring
  rw [e]; exact s5

include mul_inv h10 in
theorem force_h4 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) (dot3 K (u p) (u p))) l)
        (K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have T := total_sobWc K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (fun p => q5 K (nn K kz p)) (fun p => q5 K (ivn K inv kz p)) (w3 K kz) hw3 (k32 K) (nn32 K O)
    (fun p q r => delta_tri6 K O kz p q r) (delta01 K kz) (munu5 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu10_le K O kz p) (dot3_nn K O.toCone (u p))) l
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
        (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    have d1 := double_factor K l (k32 K) (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p))
      (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q)))
    have d2 := double_factor K l (k32 K) (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
      (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q)))
    rw [show K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))
        = K.add (K.mul (k32 K) (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (k32 K) (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) by lib_ring,
      ← d1, ← d2, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
        (K.mul (k32 K) (K.add (w3 K kz p) (w3 K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.mul (k32 K) (K.add
          (K.mul (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l))
          (K.mul (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (w3 K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) :=
    O.nn_mul _ _ (nn32 K O) (O.nn_add _ _
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (hw3 q) (hw q)) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH4

#print axioms ForceH4.nn32
#print axioms ForceH4.tri6
#print axioms ForceH4.delta_tri6
#print axioms ForceH4.munu5
#print axioms ForceH4.mu10_le
#print axioms ForceH4.force_h4

#print axioms: 6 theorems, no axioms.

79.11 Small-data global \(H^4\) bound

If \(c_4\iota ^2(\nu B_{4,0}+h\nu ^2B_{5,0})\le \nu ^3\) with \(c_4=32(27\cdot 135+135)\), then \(\nu B_{4,t}+h\nu ^2B_{5,t}\le \nu B_{4,0}+h\nu ^2B_{5,0}\) for all \(t\). \(B_4=\sum w^4|a|^2\), \(B_5=\sum w^5|a|^2\); uniform in the truncation.

ns/EnstH4.lean

/-
  ns/EnstH4.lean — T37 cycle 1105 (goal G717, piece b6): the small-data global H⁴ bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With B₄ = Σ w⁴|a|², B₅ = Σ w⁵|a|² (w = |k|²):
    S0f_le / S1f_le   Σ_p |k_p|_∞^{−10} ≤ 135,  Σ_p |k_p|_∞^{−10} w_p³ ≤ 27·135
    Z_le_B4           Σ w|a|² ≤ Σ w⁴|a|²
    h4_global         c₄ι²(νB₄_0 + hν²B₅_0) ≤ ν³,  c₄ = 32(27·135 + 135)  ⇒  νB₄_t + hν²B₅_t ≤ νB₄_0 + hν²B₅_0
  (EnstStepV with V = w³, ForceH4 with u = v = a, then EnstGlobal.q_le with c = c₄ι²).

  Lean 4 core plus ns/ForceH4, ns/EnstH3, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound.
-/
import ns.ForceH4
import ns.EnstH3

namespace EnstH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal ForceH4 EnstH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def k27 : R := K.mul (k9 K) (K.add (K.add K.one K.one) K.one)

theorem nn27 : O.nn (k27 K) :=
  O.nn_mul _ _ (nn9 K O) (O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone))

include mul_inv h10 in
theorem pt10_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨_, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have one := RatModel.nn_one' K O.toCone
  have q := TruncRate.mm K O _ _ _ _ (O.nn_sq (ivn K inv kz p)) one hle hle
  have q' := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) one q hle
  have m := le_mul_nn K O.toCone (p4 K (ivn K inv kz p)) _ _ (by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) q'
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · unfold q5 p4; lib_ring
  · show K.add (K.mul (p4 K (inv (castN K (nrm (kz p))))) (K.mul (K.mul K.one K.one) K.one)) K.zero = p4 K (inv (castN K (nrm (kz p))))
    unfold p4; lib_ring

include mul_inv h10 in
theorem pt10w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p))
      (K.mul (k27 K) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have w3' := dot_le_3nrm K O (kz p)
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have n3 : O.nn (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)
  have w9 := TruncRate.mm K O _ _ _ _ hw n3 w3' w3'
  have w27 := TruncRate.mm K O _ _ _ _ (O.nn_sq _) n3 w9 w3'
  have m := le_mul_nn K O.toCone (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) _ _ (O.nn_sq _) w27
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p)))
      (K.mul (K.mul (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))
        (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p))))) K.zero
    = K.mul (k27 K) (p4 K (ivn K inv kz p))
  unfold q5 p4 k27 k9
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) K.one)) (K.add (K.add K.one K.one) K.one))
        (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul (K.mul (K.mul n v) (K.mul n v)) (K.mul (K.mul n v) (K.mul n v))) (K.mul (K.mul n v) (K.mul n v)))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0f_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt10_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1f_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l)
      (K.mul (k27 K) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt10w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ (nn27 K O) (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))

theorem Z_le_B4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (K.mul (wt K kz p) d) (K.add (K.add (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) K.one)) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ hw hd.1)
        (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) hw) (RatModel.nn_one' K O.toCone))) (dot3_nn K O.toCone _), ?_⟩
    unfold w3
    have hw' : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw']; lib_ring

def c4 : R := K.mul (k32 K) (K.add (K.mul (k27 K) (castN K 135)) (castN K 135))

theorem B5form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by unfold w3; lib_ring) l

theorem B4form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (w3 K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by lib_ring) l

include mul_inv h10 in
/-- small-data global H⁴ bound, uniform in the truncation -/
theorem h4_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (c4 K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc4 : O.nn (c4 K) := O.nn_mul _ _ (nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn27 K O) n135) n135)
  have hc : O.nn (K.mul (c4 K) (K.mul ι ι)) := O.nn_mul _ _ nc4 (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (w3 K kz) hw hw3 (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h4 K O inv mul_inv h10 l kz hd h1 ι (a s) (a s)
  rw [B5form K kz l (a s), B4form K kz l (a s)] at fh
  have hS0 := S0f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B4 K O kz h1 l (a s)
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB4 hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB4
  have inner := le_mul_nn K O.toCone _ _ _ (nn32 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB5 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c4
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a s p) (a s p))) l = B4
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a s p) (a s p))) l = B5
  generalize castN K 135 = c
  generalize k32 K = e32
  generalize k27 K = e27
  lib_ring

end Gen

end EnstH4

#print axioms EnstH4.nn27
#print axioms EnstH4.pt10_le4
#print axioms EnstH4.pt10w_le
#print axioms EnstH4.S0f_le
#print axioms EnstH4.S1f_le
#print axioms EnstH4.Z_le_B4
#print axioms EnstH4.B5form
#print axioms EnstH4.B4form
#print axioms EnstH4.h4_global

#print axioms: 9 theorems, no axioms.

79.12 \(H^1\)-weighted force difference

With \(d=a-b\): \(\sum _rw_r|N_r(a,a)-N_r(b,b)|^2\le 2\iota ^2B(d)(2S_1Z(a)+2S_0A(a))+2\iota ^2B(b)(2S_1Z(d)+2S_0A(d))\). Uniform in the truncation; the force-Lipschitz input of the step-size defect.

ns/ForceDiffW.lean

/-
  ns/ForceDiffW.lean — T37 cycle 1107 (goal G717, piece b7): the H¹-weighted force difference, uniform in the
  truncation, over an ordered field. With d = a − b and N(a,a) − N(b,b) = N(d,a) + N(b,d) (ForceDiff.diff_eq):
    force_diffW   Σ_r w_r|N_r(a,a) − N_r(b,b)|²
                    ≤ 2·ι²B(d)(2S₁Z(a) + 2S₀A(a)) + 2·ι²B(b)(2S₁Z(d) + 2S₀A(d))
  (ForceH2Bi twice; B = Σw³|·|², Z = Σw|·|², A = Σw²|·|², S₁ ≤ 3·135, S₀ ≤ 135 by EnstH2). In the step-size defect
  d = b₁ − a₀ is O(k) in H³ (TimeLipW with V = w³, ForceH4, EnstH4).

  Lean 4 core plus ns/ForceH2Bi, ns/ForceDiff, ns/ForceTwoScale (sq2).
-/
import ns.ForceH2Bi
import ns.ForceDiff

namespace ForceDiffW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 ForceH2Bi ForceDiff EnstDiff

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Bw (kz : I → Mode) (l : List I) (u : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l
def Zw (kz : I → Mode) (l : List I) (u : I → V3 R) : R := lsum K (fun q => K.mul (wt K kz q) (dot3 K (u q) (u q))) l
def Aw (kz : I → Mode) (l : List I) (u : I → V3 R) : R :=
  lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (u q) (u q))) l
def S1w (kz : I → Mode) (l : List I) : R :=
  lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l
def S0w (kz : I → Mode) (l : List I) : R := lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l

include mul_inv h10 in
theorem force_diffW (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a b : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Bw K kz l (dF K a b))
          (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l a)))
            (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l a)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Bw K kz l b)
          (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l (dF K a b))))
            (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l (dF K a b))))))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [diff_eq K l _ ι _ a b r]) l]
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have s1 := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (wt K kz r) _ _ (dot3_nn K O.toCone _) (sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))) l
  have e : lsum K (fun r => K.mul (wt K kz r) (K.add (K.mul (K.add K.one K.one)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)))
      (K.mul (K.add K.one K.one) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))))) l
      = K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (wt K kz r)
          (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (wt K kz r)
          (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ two (force_h2_bi K O inv mul_inv h10 l kz hd h1 ι (dF K a b) a)
  · exact le_mul_nn K O.toCone _ _ _ two (force_h2_bi K O inv mul_inv h10 l kz hd h1 ι b (dF K a b))

end Gen

end ForceDiffW

#print axioms ForceDiffW.force_diffW

#print axioms: 1 theorem, no axioms.

79.13 Order of the step-size defect

For \(0\le k\le 1\): the viscous, force-difference and \(H^3\)-force parts of the weighted defect are each \(O(k^3)\). Gives \(\sum _rw_r|a_{1,r}-b_{2,r}|^2\le k^3\cdot 3(V+F+\nu E)\) under the small-data \(H^4\) bounds.

ns/DefectOrder.lean

/-
  ns/DefectOrder.lean — T37 cycle 1111 (goal G717, piece b8b): the order count of the one-step/two-step defect, over an
  ordered field (all quantities ≥ 0, 0 ≤ k ≤ 1):
    k2_le       k²X ≤ kX
    lip_k       2k²G + 2k²ν²B ≤ k(2G + 2ν²B)                     (TimeLipW inputs are O(k))
    visc_k3     kν²B₅ ≤ Q  ⇒  k⁴ν⁴B₅ ≤ k³(ν²Q)                    (EnstH4 controls hν²B₅)
    fd_k        FD ≤ 2ι²·B_d·P + 2ι²·B_b·(2S₁Z_d + 2S₀A_d), B_d ≤ kB₁, Z_d ≤ kZ₁, A_d ≤ kA₁, P ≤ P₀, B_b ≤ E,
                S₁ ≤ s₁, S₀ ≤ s₀  ⇒  FD ≤ k·(2ι²B₁P₀ + 2ι²E(2s₁Z₁ + 2s₀A₁))   (ForceDiffW)
    defect_k3   S ≤ 3(k⁴ν⁴B₅ + k²FD + k³νG), k⁴ν⁴B₅ ≤ k³V, FD ≤ kF, G ≤ E  ⇒  S ≤ k³·3(V + F + νE)
    defect_qe   νZ(D) + kν²A(D) ≤ k³·3(ν(V + F + νE) + ν²(ν²E + E + νE)) from the Z- and A-defect bounds (k ≤ 1)
    defect_qe'  the same with k·H₃ ≤ E in place of H₃ ≤ E (uniform in the step: H₃ ~ Σw³|N|² is only O(1/k))
  so the weighted defect Σw|a₁ − b₂|² of DefectSum.defect_sum4 is O(k³) per step under the small-data H⁴ bounds.

  Lean 4 core plus ns/DefectA0 (k3), ns/TruncRate (mm), ns/ForceSob (le_mul_right').
-/
import ns.DefectA0
import ns.TruncRate

namespace DefectOrder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge DefectA0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem k2_le (k X : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hX : O.nn X) :
    Le K O.toCone (K.mul (K.mul k k) X) (K.mul k X) := by
  have m := le_mul_nn K O.toCone k _ _ hk hk1
  have m2 := ForceSob.le_mul_right' K O.toCone m hX
  refine le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

theorem lip_k (k ν G B : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hG : O.nn G) (hB : O.nn B) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) G)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B)))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have X := k2_le K O k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B))) hk hk1
    (O.nn_add _ _ (O.nn_mul _ _ two hG) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) hB)))
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ X
  lib_ring

theorem visc_k3 (k ν B5 Q : R) (hk : O.nn k) (h : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul (K.mul k k) k) (K.mul (K.mul ν ν) Q)) := by
  have m := le_mul_nn K O.toCone (K.mul (K.mul (K.mul k k) k) (K.mul ν ν)) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq k) hk) (O.nn_sq ν)) h
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

theorem fd_k (k ι FD Bd P Bb Zd Ad S1 S0 B1 Z1 A1 P0 E s1 s0 : R) (hk : O.nn k)
    (nBd : O.nn Bd) (nBb : O.nn Bb) (nS1 : O.nn S1) (nS0 : O.nn S0)
    (nP0 : O.nn P0) (nZ1 : O.nn Z1) (nA1 : O.nn A1) (ns1 : O.nn s1) (ns0 : O.nn s0)
    (hFD : Le K O.toCone FD (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Bd P)))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Bb
        (K.add (K.mul (K.add K.one K.one) (K.mul S1 Zd)) (K.mul (K.add K.one K.one) (K.mul S0 Ad))))))))
    (hBd : Le K O.toCone Bd (K.mul k B1)) (hZd : Le K O.toCone Zd (K.mul k Z1)) (hAd : Le K O.toCone Ad (K.mul k A1))
    (hP : Le K O.toCone P P0) (hBb : Le K O.toCone Bb E) (hS1 : Le K O.toCone S1 s1) (hS0 : Le K O.toCone S0 s0) :
    Le K O.toCone FD (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul B1 P0)))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
        (K.add (K.mul (K.add K.one K.one) (K.mul s1 Z1)) (K.mul (K.add K.one K.one) (K.mul s0 A1)))))))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have t1 := TruncRate.mm K O _ _ _ _ nBd nP0 hBd hP
  have u1 := TruncRate.mm K O _ _ _ _ nS1 (O.nn_mul _ _ hk nZ1) hS1 hZd
  have u2 := TruncRate.mm K O _ _ _ _ nS0 (O.nn_mul _ _ hk nA1) hS0 hAd
  have u := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two u1) (le_mul_nn K O.toCone _ _ _ two u2)
  have t2 := TruncRate.mm K O _ _ _ _ nBb
    (O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ ns1 (O.nn_mul _ _ hk nZ1))) (O.nn_mul _ _ two (O.nn_mul _ _ ns0 (O.nn_mul _ _ hk nA1)))) hBb u
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) t1))
    (le_mul_nn K O.toCone _ _ _ two (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) t2))
  refine le_trans' K O.toCone _ _ _ hFD (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

theorem defect_k3 (k ν S B5 FD G V F E : R) (hk : O.nn k) (hν : O.nn ν)
    (hS : Le K O.toCone S (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) G))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hG : Le K O.toCone G E) :
    Le K O.toCone S (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.add V F) (K.mul ν E)))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have a := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ hV (le_mul_nn K O.toCone _ _ _ (O.nn_sq k) hF))
    (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_mul _ _ hk hν)) hG)
  refine le_trans' K O.toCone _ _ _ hS (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ n3 a) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize k3 K = c
  lib_ring

theorem defect_qe (k ν ZD AD B5 FD H B4 FD2 H3 V F E : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hν : O.nn ν)
    (hE : O.nn E)
    (hZ : Le K O.toCone ZD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H))))
    (hA : Le K O.toCone AD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul k k) (K.mul ν ν)) B4)
      (K.mul (K.mul k k) FD2)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hH : Le K O.toCone H E) (hB4 : Le K O.toCone B4 E)
    (hF2 : Le K O.toCone FD2 E) (hH3 : Le K O.toCone H3 E) :
    Le K O.toCone (K.add (K.mul ν ZD) (K.mul (K.mul k (K.mul ν ν)) AD))
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have z := defect_k3 K O k ν ZD B5 FD H V F E hk hν hZ hV hF hH
  -- k·k⁴ ≤ k·k³ part of the A-term: k³νH3 ≤ k²·(νE) ... use k ≤ 1 on the last term
  have a1 := le_mul_nn K O.toCone (K.mul (K.mul k k) (K.mul ν ν)) _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν)) hB4
  have a2 := le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hF2
  have hkk : Le K O.toCone (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3) (K.mul (K.mul k k) (K.mul ν E)) := by
    have m1 := le_mul_nn K O.toCone (K.mul (K.mul (K.mul k k) k) ν) _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq k) hk) hν) hH3
    have m2 := ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hk1) (O.nn_mul _ _ hν hE)
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · lib_ring
    · lib_ring
    · lib_ring
  have a := le_trans' K O.toCone _ _ _ hA (le_mul_nn K O.toCone _ _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) hkk))
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν z) (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) a)
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize k3 K = c
  lib_ring

theorem defect_qe' (k ν ZD AD B5 FD H B4 FD2 H3 V F E : R) (hk : O.nn k) (hν : O.nn ν)
    (hZ : Le K O.toCone ZD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5)
      (K.mul (K.mul k k) FD)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H))))
    (hA : Le K O.toCone AD (K.mul (k3 K) (K.add (K.add (K.mul (K.mul (K.mul k k) (K.mul ν ν)) B4)
      (K.mul (K.mul k k) FD2)) (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3))))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν))) B5) (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone FD (K.mul k F)) (hH : Le K O.toCone H E) (hB4 : Le K O.toCone B4 E)
    (hF2 : Le K O.toCone FD2 E) (hH3 : Le K O.toCone (K.mul k H3) E) :
    Le K O.toCone (K.add (K.mul ν ZD) (K.mul (K.mul k (K.mul ν ν)) AD))
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have ⟨_, n3, _, _⟩ := kn K O (RatModel.nn_one' K O.toCone)
  have z := defect_k3 K O k ν ZD B5 FD H V F E hk hν hZ hV hF hH
  -- k·k⁴ ≤ k·k³ part of the A-term: k³νH3 ≤ k²·(νE) ... use k ≤ 1 on the last term
  have a1 := le_mul_nn K O.toCone (K.mul (K.mul k k) (K.mul ν ν)) _ _ (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν)) hB4
  have a2 := le_mul_nn K O.toCone (K.mul k k) _ _ (O.nn_sq k) hF2
  have hkk : Le K O.toCone (K.mul (K.mul (K.mul k k) (K.mul k ν)) H3) (K.mul (K.mul k k) (K.mul ν E)) := by
    have m1 := le_mul_nn K O.toCone (K.mul (K.mul k k) ν) _ _ (O.nn_mul _ _ (O.nn_sq k) hν) hH3
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m1 ⟨K.zero, O.nn_zero, ?_⟩)
    · lib_ring
    · lib_ring
  have a := le_trans' K O.toCone _ _ _ hA (le_mul_nn K O.toCone _ _ _ n3 (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) hkk))
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν z) (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) a)
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize k3 K = c
  lib_ring

end Gen

end DefectOrder

#print axioms DefectOrder.k2_le
#print axioms DefectOrder.lip_k
#print axioms DefectOrder.visc_k3
#print axioms DefectOrder.fd_k
#print axioms DefectOrder.defect_k3
#print axioms DefectOrder.defect_qe
#print axioms DefectOrder.defect_qe'

#print axioms: 7 theorems, no axioms.

79.14 Young splitting for the error recursion

\(k\sum _r\mu _r|X_r+Y_r|^2\le k(1+k)\sum _r\mu _r|X_r|^2+(1+k)\sum _r\mu _r|Y_r|^2\) for weights \(\mu _r\ge 0\). Separates the Lipschitz part and the defect in the step-size error recursion.

ns/SplitK.lean

/-
  ns/SplitK.lean — T37 cycle 1113 (goal G717, piece c1): the Young splitting used to accumulate the step-size defect,
  over any commutative ring with a nonnegative cone (any k, weights μ_r ≥ 0):
    split_k      k(X + Y)² ≤ k(1 + k)X² + (1 + k)Y²        ((1+k)(kX² + Y²) − k(X+Y)² = (kX − Y)²)
    split_k_sum  k·Σ_r μ_r|X_r + Y_r|² ≤ k(1 + k)·Σ_r μ_r|X_r|² + (1 + k)·Σ_r μ_r|Y_r|²
  With X = (Lipschitz part), Y = (defect D_n): the error recursion k·E_{n+1} ≤ k(1+k)·L·E_n + (1+k)·|D_n|².

  Lean 4 core plus ns/EnstDiff (add3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.EnstDiff

namespace SplitK
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem split_k (k X Y : R) :
    Le K P (K.mul k (K.mul (K.add X Y) (K.add X Y)))
      (K.add (K.mul (K.mul k (K.add K.one k)) (K.mul X X)) (K.mul (K.add K.one k) (K.mul Y Y))) :=
  ⟨K.mul (K.add (K.mul k X) (K.neg Y)) (K.add (K.mul k X) (K.neg Y)), P.nn_sq _, by lib_ring⟩

theorem split_k3 (k : R) (X Y : V3 R) :
    Le K P (K.mul k (dot3 K (add3 K X Y) (add3 K X Y)))
      (K.add (K.mul (K.mul k (K.add K.one k)) (dot3 K X X)) (K.mul (K.add K.one k) (dot3 K Y Y))) := by
  have cx := split_k K P k X.x Y.x
  have cy := split_k K P k X.y Y.y
  have cz := split_k K P k X.z Y.z
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩)
  · show K.add (K.mul k (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y)))
      (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))) K.zero = _
    lib_ring
  · show _ = K.add (K.mul (K.mul k (K.add K.one k)) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
      (K.mul (K.add K.one k) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
    lib_ring

theorem split_k_sum (l : List I) (k : R) (μ : I → R) (hμ : ∀ r, P.nn (μ r)) (X Y : I → V3 R) :
    Le K P (K.mul k (lsum K (fun r => K.mul (μ r) (dot3 K (add3 K (X r) (Y r)) (add3 K (X r) (Y r)))) l))
      (K.add (K.mul (K.mul k (K.add K.one k)) (lsum K (fun r => K.mul (μ r) (dot3 K (X r) (X r))) l))
        (K.mul (K.add K.one k) (lsum K (fun r => K.mul (μ r) (dot3 K (Y r) (Y r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (μ r) _ _ (hμ r) (split_k3 K P k (X r) (Y r))) l
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩)
  · rw [add_zero', ← lsum_mul_left]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  · rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end SplitK

#print axioms SplitK.split_k
#print axioms SplitK.split_k3
#print axioms SplitK.split_k_sum

#print axioms: 3 theorems, no axioms.

79.15 Error recursion with a source

\(x_{n+1}\le gx_n+s\), \(1\le g\), \(s\ge 0\) imply \(x_n\le g^n(x_0+ns)\); with the Young splitting the step-size error obeys it with \(s=(1+k)k^2C\). Abstract recursion for comparing step sizes \(2k\) and \(k\).

ns/ErrRec.lean

/-
  ns/ErrRec.lean — T37 cycle 1117 (goal G717, piece c3): the abstract error recursion for the step-size comparison,
  over an ordered ring:
    one_le_pwr   1 ≤ g  ⇒  1 ≤ gⁿ
    rec_src      x_{n+1} ≤ g·x_n + s,  1 ≤ g,  s ≥ 0  ⇒  x_n ≤ gⁿ·(x_0 + n·s)
    rec_split    k·x_{n+1} ≤ k(1+k)·L_n + (1+k)·Δ_n,  L_n ≤ G·x_n,  Δ_n ≤ k·k·(k·C)  (k ≥ 0, G ≥ 0)
                 ⇒  k·x_{n+1} ≤ k·((1+k)G·x_n + (1+k)·k·k·C)
    err_bound    combined, with x₀ = 0 and g = (1+k)G ≥ 1:  x_n ≤ gⁿ·(n·(1+k)k²C)
  With x_n the Qd-error between the 2k-scheme and the k-scheme at time 2nk, L_n the two-step Lipschitz image
  (EnstLipschitz, T = 2), Δ_n the O(k³) defect (DefectOrder), and x_0 = 0: x_n ≤ gⁿ·n·(1+k)k²C = O(k) for nk ≤ T.

  Lean 4 core plus ns/EnstGronwall (pwr), ns/TruncRate (mm), lib/RingTac.
-/
import ns.EnstGronwall
import ns.TruncRate
import ns.EnstGlobal

namespace ErrRec
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_le_pwr (g : R) (hg : Le K O.toCone K.one g) : ∀ n, Le K O.toCone K.one (pwr K g n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := one_le_pwr g hg n
    have m := TruncRate.mm K O _ _ _ _ (RatModel.nn_one' K O.toCone) (TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) ih) hg ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m
    lib_ring

theorem rec_src (g s : R) (x : Nat → R) (hg : Le K O.toCone K.one g) (hs : O.nn s)
    (step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul g (x n)) s)) :
    ∀ n, Le K O.toCone (x n) (K.mul (pwr K g n) (K.add (x 0) (K.mul (castN K n) s)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (x 0) K.zero = K.mul K.one (K.add (x 0) (K.mul K.zero s)); lib_ring⟩
  | n + 1 => by
    have ih := rec_src g s x hg hs step n
    have hgn : O.nn g := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) hg
    have a := le_mul_nn K O.toCone g _ _ hgn ih
    have p1 := one_le_pwr K O g hg (n + 1)
    have b := ForceSob.le_mul_right' K O.toCone p1 hs
    have c := le_add' K O.toCone _ _ _ _ a b
    have c' := le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, (by lib_ring :
      K.add (K.add (K.mul g (x n)) s) K.zero = K.add (K.mul g (x n)) (K.mul K.one s))⟩ c
    refine le_trans' K O.toCone _ _ _ (step n) (le_trans' K O.toCone _ _ _ c' ⟨K.zero, O.nn_zero, ?_⟩)
    show K.add (K.add (K.mul g (K.mul (pwr K g n) (K.add (x 0) (K.mul (castN K n) s)))) (K.mul (K.mul g (pwr K g n)) s)) K.zero
      = K.mul (K.mul g (pwr K g n)) (K.add (x 0) (K.mul (K.add K.one (castN K n)) s))
    generalize pwr K g n = P
    generalize castN K n = c
    lib_ring

theorem rec_split (k G C x x' L Δ : R) (hk : O.nn k)
    (hsp : Le K O.toCone (K.mul k x') (K.add (K.mul (K.mul k (K.add K.one k)) L) (K.mul (K.add K.one k) Δ)))
    (hL : Le K O.toCone L (K.mul G x)) (hΔ : Le K O.toCone Δ (K.mul (K.mul k k) (K.mul k C))) :
    Le K O.toCone (K.mul k x')
      (K.mul k (K.add (K.mul (K.mul (K.add K.one k) G) x) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have a := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hk n1k) hL) (le_mul_nn K O.toCone _ _ _ n1k hΔ)
  refine le_trans' K O.toCone _ _ _ hsp (le_trans' K O.toCone _ _ _ a ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

/-- the step-size error bound: from the split recursion with x₀ = 0, x_n ≤ gⁿ·(n·((1+k)k²C)), g = (1+k)G ≥ 1 -/
theorem err_bound (k G C : R) (x L Δ : Nat → R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hC : O.nn C)
    (hg : Le K O.toCone K.one (K.mul (K.add K.one k) G)) (hx0 : x 0 = K.zero)
    (hsp : ∀ n, Le K O.toCone (K.mul k (x (n + 1))) (K.add (K.mul (K.mul k (K.add K.one k)) (L n)) (K.mul (K.add K.one k) (Δ n))))
    (hL : ∀ n, Le K O.toCone (L n) (K.mul G (x n))) (hΔ : ∀ n, Le K O.toCone (Δ n) (K.mul (K.mul k k) (K.mul k C))) (n : Nat) :
    Le K O.toCone (x n) (K.mul (pwr K (K.mul (K.add K.one k) G) n)
      (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul (K.mul (K.add K.one k) G) (x n)) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))) := by
    intro n
    have r := rec_split K O k G C (x n) (x (n + 1)) (L n) (Δ n) hk (hsp n) (hL n) (hΔ n)
    refine EnstGlobal.le_of_mul_le K O k _ _ hk hk0 (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
    lib_ring
  have b := rec_src K O _ _ x hg (O.nn_mul _ _ n1k (O.nn_mul _ _ (O.nn_sq k) hC)) step n
  rw [hx0] at b
  refine le_trans' K O.toCone _ _ _ b ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

end Gen

end ErrRec

#print axioms ErrRec.one_le_pwr
#print axioms ErrRec.rec_src
#print axioms ErrRec.rec_split
#print axioms ErrRec.err_bound

#print axioms: 4 theorems, no axioms.

79.16 Two-step Lipschitz factor

\(\nu ^2L\le (\nu +k\alpha _0)(\nu +k\alpha _1)x\) with \(\alpha _t\le \alpha \) gives \(L\le Gx\), \(G=(\nu +k\alpha )^2\nu ^{-2}\ge 1\). Feeds the step-size error recursion.

ns/LipG.lean

/-
  ns/LipG.lean — T37 cycle 1121 (goal G717, piece c4b): the two-step Lipschitz factor in the step-size recursion,
  over an ordered field (ν > 0 with inverse iν, k ≥ 0, 0 ≤ α_t ≤ α):
    lip_G     ν²·L ≤ (ν + kα₀)(ν + kα₁)·x,  x ≥ 0  ⇒  L ≤ G·x,   G = (ν + kα)²·iν²   (EnstLipschitz with T = 2)
    G_ge_one  1 ≤ G,  hence 1 ≤ (1 + k)·G   (the hypothesis of ErrRec.err_bound)

  Lean 4 core plus ns/EnstGlobal, ns/EnstLocal (sq_mono), ns/TruncRate (mm).
-/
import ns.EnstGlobal
import ns.EnstLocal
import ns.TruncRate

namespace LipG
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def GG (ν iν k α : R) : R := K.mul (K.mul (K.add ν (K.mul k α)) (K.add ν (K.mul k α))) (K.mul iν iν)

theorem lip_G (ν iν k α α0 α1 L x : R) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hk : O.nn k)
    (ha0 : O.nn α0) (ha1 : O.nn α1) (h0 : Le K O.toCone α0 α) (h1 : Le K O.toCone α1 α) (hx : O.nn x)
    (hL : Le K O.toCone (K.mul (K.mul ν ν) L) (K.mul (K.mul (K.add ν (K.mul k α0)) (K.add ν (K.mul k α1))) x)) :
    Le K O.toCone L (K.mul (GG K ν iν k α) x) := by
  have e0 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_mul_nn K O.toCone k _ _ hk h0)
  have e1 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_mul_nn K O.toCone k _ _ hk h1)
  have p := TruncRate.mm K O _ _ _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hk ha0))
    (TruncRate.nn_of_le K O _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hk ha1)) e1) e0 e1
  have q := le_trans' K O.toCone _ _ _ hL (ForceSob.le_mul_right' K O.toCone p hx)
  have r := le_mul_nn K O.toCone (K.mul iν iν) _ _ (O.nn_sq iν) q
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
  · calc K.add L K.zero = K.mul (K.mul (K.mul ν iν) (K.mul ν iν)) L := by rw [hiν]; lib_ring
      _ = _ := by lib_ring
  · unfold GG; lib_ring

theorem G_ge_one (ν iν k α : R) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hk : O.nn k) (hα : O.nn α) :
    Le K O.toCone K.one (GG K ν iν k α) ∧ Le K O.toCone K.one (K.mul (K.add K.one k) (GG K ν iν k α)) := by
  have e := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (⟨K.mul k α, O.nn_mul _ _ hk hα, by lib_ring⟩ :
    Le K O.toCone K.zero (K.mul k α))
  have s := EnstLocal.sq_mono K O _ _ hν (O.nn_add _ _ hν (O.nn_mul _ _ hk hα)) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ e)
  have m := le_mul_nn K O.toCone (K.mul iν iν) _ _ (O.nn_sq iν) s
  have g1 : Le K O.toCone K.one (GG K ν iν k α) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
    · calc K.add K.one K.zero = K.mul (K.mul ν iν) (K.mul ν iν) := by rw [hiν]; lib_ring
        _ = _ := by lib_ring
    · unfold GG; lib_ring
  refine ⟨g1, ?_⟩
  have gn : O.nn (GG K ν iν k α) := TruncRate.nn_of_le K O _ _ (RatModel.nn_one' K O.toCone) g1
  have a := TruncRate.mm K O _ _ _ _ (RatModel.nn_one' K O.toCone) gn
    (⟨k, hk, by lib_ring⟩ : Le K O.toCone K.one (K.add K.one k)) g1
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ a

end Gen

end LipG

#print axioms LipG.lip_G
#print axioms LipG.G_ge_one

#print axioms: 2 theorems, no axioms.

79.17 Step-size Cauchy estimate

If the local defect is \(\le k^3C\), then \(Q_e(a_n,b_{2n})\le g^n\,n(1+k)k^2C\) with \(g=(1+k)(\nu +k\alpha )^2\nu ^{-2}\). Step \(2k\) against step \(k\): an \(O(k)\) difference on \(nk\le T\), uniform in the truncation.

ns/StepCauchy.lean

/-
  ns/StepCauchy.lean — T37 cycle 1123 (goal G717d): the step-size Cauchy estimate for the implicit-viscosity
  Fourier–Galerkin scheme, over an ordered field. Let a be the scheme with step 2k, b the scheme with step k, and for
  each n let c n be the k-scheme started at a_n (c n 0 = a n). Measure errors in Qe(u, v) = νZ(u − v) + kν²A(u − v).
    split_Qe      k·Qe(u, v) ≤ k(1 + k)·Qe(c, v) + (1 + k)·Qe(u, c)              (SplitK)
    step_cauchy   if the local defect Qe(a_{n+1}, c n 2) ≤ k³C (DefectSum/DefectOrder), the k-scheme pairs satisfy
                  the hypotheses of EnstLipschitz with α_t ≤ α, and Qe(a_0, b_0) = 0, then for every n
                    Qe(a_n, b_{2n}) ≤ gⁿ·(n·(1 + k)k²C),   g = (1 + k)(ν + kα)²ν⁻²,
  i.e. O(k) on any interval nk ≤ T with a factor independent of the truncation (EnstLipschitz with T = 2, LipG,
  ErrRec.err_bound).

  Lean 4 core plus ns/EnstLipschitz, ns/SplitK, ns/ErrRec, ns/LipG.
-/
import ns.EnstLipschitz
import ns.SplitK
import ns.ErrRec
import ns.LipG

namespace StepCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz SplitK ErrRec LipG

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Qe (ν k : R) (l : List I) (kz : I → Mode) (u v : I → V3 R) : R :=
  K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l))
    (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l))

theorem dF_split (u c v : I → V3 R) (r : I) : dF K u v r = add3 K (dF K c v r) (dF K u c r) := by
  show sub3 K (u r) (v r) = add3 K (sub3 K (c r) (v r)) (sub3 K (u r) (c r))
  apply v3_ext
  · show K.add (u r).x (K.neg (v r).x) = K.add (K.add (c r).x (K.neg (v r).x)) (K.add (u r).x (K.neg (c r).x)); lib_ring
  · show K.add (u r).y (K.neg (v r).y) = K.add (K.add (c r).y (K.neg (v r).y)) (K.add (u r).y (K.neg (c r).y)); lib_ring
  · show K.add (u r).z (K.neg (v r).z) = K.add (K.add (c r).z (K.neg (v r).z)) (K.add (u r).z (K.neg (c r).z)); lib_ring

theorem split_Qe (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u c v : I → V3 R) :
    Le K O.toCone (K.mul k (Qe K ν k l kz u v))
      (K.add (K.mul (K.mul k (K.add K.one k)) (Qe K ν k l kz c v)) (K.mul (K.add K.one k) (Qe K ν k l kz u c))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have s1 := split_k_sum K O.toCone l k (wt K kz) hw (dF K c v) (dF K u c)
  have s2 := split_k_sum K O.toCone l k (fun r => K.mul (wt K kz r) (wt K kz r)) (fun r => O.nn_sq _) (dF K c v) (dF K u c)
  have e1 : lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l
      = lsum K (fun r => K.mul (wt K kz r) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l :=
    lsum_ext K _ _ (fun r => by rw [dF_split K u c v r]) l
  have e2 : lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l
      = lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l :=
    lsum_ext K _ _ (fun r => by rw [dF_split K u c v r]) l
  have t := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν s1)
    (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) s2)
  unfold Qe
  rw [e1, e2]
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩)
  · generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l = Z
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (add3 K (dF K c v r) (dF K u c r)) (add3 K (dF K c v r) (dF K u c r)))) l = A
    lib_ring
  · show _ = K.add (K.mul (K.mul k (K.add K.one k))
        (K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K c v r) (dF K c v r))) l))
          (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K c v r) (dF K c v r))) l))))
      (K.mul (K.add K.one k)
        (K.add (K.mul ν (lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u c r) (dF K u c r))) l))
          (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u c r) (dF K u c r))) l))))
    generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K c v r) (dF K c v r))) l = Z1
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K c v r) (dF K c v r))) l = A1
    generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u c r) (dF K u c r))) l = Z2
    generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u c r) (dF K u c r))) l = A2
    lib_ring

/-- the rate α_t of EnstLipschitz for the pair (u, v) at time t -/
def alpha (ι : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat) : R :=
  K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz u t))))
    (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz v t))))

include mul_inv h10 in
theorem alpha_nn (ι : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat) :
    O.nn (alpha K inv ι Rc l kz u v t) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
  exact O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1
      (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135)
      (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))))

include mul_inv h10 in
theorem step_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iν α C : R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hα : O.nn α) (hC : O.nn C)
    (a b Nb : Nat → I → V3 R) (c Nc : Nat → Nat → I → V3 R)
    (hc0 : ∀ n, c n 0 = a n)
    (hcs : ∀ n t r, Step K k ν (wt K kz r) (c n (t + 1) r) (c n t r) (Nc n t r))
    (hbs : ∀ t r, Step K k ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hN : ∀ n t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Nc n t r) (Nb (2 * n + t) r)) (sub3 K (Nc n t r) (Nb (2 * n + t) r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))) l))
    (hβ : ∀ n t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz (c n) t))))
      (K.mul ν ν))
    (hαle : ∀ n t, Le K O.toCone (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) t) α)
    (hΔ : ∀ n, Le K O.toCone (Qe K ν k l kz (a (n + 1)) (c n 2)) (K.mul (K.mul k k) (K.mul k C)))
    (hx0 : Qe K ν k l kz (a 0) (b 0) = K.zero) (n : Nat) :
    Le K O.toCone (Qe K ν k l kz (a n) (b (2 * n)))
      (K.mul (pwr K (K.mul (K.add K.one k) (GG K ν iν k α)) n)
        (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQn : ∀ u v, O.nn (Qe K ν k l kz u v) := fun u v =>
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
  have hL : ∀ n, Le K O.toCone (Qe K ν k l kz (c n 2) (b (2 * n + 2))) (K.mul (GG K ν iν k α) (Qe K ν k l kz (a n) (b (2 * n)))) := by
    intro n
    have lp := lipschitz K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν hk hν (c n) (fun s => b (2 * n + s)) (Nc n)
      (fun s => Nb (2 * n + s)) (hN n) (hcs n) (fun t r => hbs (2 * n + t) r) (hβ n) 2
    have lp' : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2))))
        (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0)))
          (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))) (Qe K ν k l kz (a n) (b (2 * n)))) := by
      rw [← hc0 n]
      refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ lp ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))) K.zero
          = K.mul (K.mul ν (K.mul ν K.one)) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))
        lib_ring
      · show K.add (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))
            (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0))) K.one))
            (Qe K ν k l kz (c n 0) (b (2 * n + 0)))) K.zero = _
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0 = x0
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1 = x1
        generalize Qe K ν k l kz (c n 0) (b (2 * n + 0)) = q
        lib_ring
    exact lip_G K O ν iν k α _ _ _ _ hν hiν hk (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 0)
      (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 1) (hαle n 0) (hαle n 1) (hQn _ _) lp'
  have gg := (G_ge_one K O ν iν k α hν hiν hk hα).2
  exact err_bound K O k (GG K ν iν k α) C (fun n => Qe K ν k l kz (a n) (b (2 * n)))
    (fun n => Qe K ν k l kz (c n 2) (b (2 * n + 2))) (fun n => Qe K ν k l kz (a (n + 1)) (c n 2)) hk hk0 hC gg hx0
    (fun n => split_Qe K O ν k hν hk l kz (a (n + 1)) (c n 2) (b (2 * n + 2))) hL hΔ n

end Gen

end StepCauchy

#print axioms StepCauchy.dF_split
#print axioms StepCauchy.split_Qe
#print axioms StepCauchy.alpha_nn
#print axioms StepCauchy.step_cauchy

#print axioms: 4 theorems, no axioms.

79.18 Local defect in the \(Q_e\) norm

From the scheme relations and norm bounds: \(Q_e(a_1,b_2)\le k^3\cdot 3(\nu (V+F+\nu E)+\nu ^2(\nu ^2E+E+\nu E))\). The defect hypothesis of the step-size Cauchy estimate.

ns/LocalDefect.lean

/-
  ns/LocalDefect.lean — T37 cycle 1127 (goal G717e, piece 2): the local defect in the Qe norm, from the scheme
  relations. One step of size 2k (a₀ → a₁, force N₀) against two steps of size k (a₀ → b₁ → b₂, forces N₀, N₁), over
  an ordered ring without zero divisors. If k ≤ 1, k⁴ν⁴B₅(a₀) ≤ k³V, Σw|N₀ − N₁|² ≤ kF, and Σw²|N₁|², Σw⁴|a₀|²,
  Σw²|N₀ − N₁|², Σw³|N₁|² ≤ E, then
    local_defect   Qe(a₁, b₂) = νZ(a₁ − b₂) + kν²A(a₁ − b₂) ≤ k³·3(ν(V + F + νE) + ν²(ν²E + E + νE)),
    local_defect'  the same with k·Σw³|N₁|² ≤ E (uniform in the step) and without k ≤ 1
  the hypothesis hΔ of StepCauchy.step_cauchy (DefectSum.defect_sum4, defect_sumV with V = w², DefectOrder.defect_qe).

  Lean 4 core plus ns/DefectSum, ns/DefectOrder, ns/StepCauchy (Qe).
-/
import ns.DefectSum
import ns.DefectOrder
import ns.StepCauchy

namespace LocalDefect
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff DefectA0
  DefectSum DefectOrder StepCauchy ForceDiff GalerkinForce ModeCount CubeCount ForceUniform NormCompare EnstGalerkin

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem local_defect (l : List I) (kz : I → Mode) (k ν V F E : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (hE : O.nn E) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (wt K kz r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (wt K kz r) (b2 r) (b1 r) (N1 r))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν)))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (wt K kz r)) (dot3 K (a0 r) (a0 r))) l))
      (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) (K.mul k F))
    (hH : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) E)
    (hF2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) E)
    (hH3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E) :
    Le K O.toCone (Qe K ν k l kz a1 b2)
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have dz := defect_sum4 K O l k ν hk hν (wt K kz) hw h10 a0 a1 b1 b2 N0 N1 s1 s2 s3
  have da := defect_sumV K O l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) h10
    a0 a1 b1 b2 N0 N1 s1 s2 s3
  have q := defect_qe K O k ν _ _ _ _ _ _ _ _ V F E hk hk1 hν hE dz da hV hF hH hB4 hF2 hH3
  exact q

theorem local_defect' (l : List I) (kz : I → Mode) (k ν V F E : R) (hk : O.nn k)
    (hν : O.nn ν) (h10 : K.one ≠ K.zero) (a0 a1 b1 b2 N0 N1 : I → V3 R)
    (s1 : ∀ r, Step K (K.add k k) ν (wt K kz r) (a1 r) (a0 r) (N0 r)) (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (s3 : ∀ r, Step K k ν (wt K kz r) (b2 r) (b1 r) (N1 r))
    (hV : Le K O.toCone (K.mul (K.mul (K.mul (K.mul k k) (K.mul k k)) (K.mul (K.mul ν ν) (K.mul ν ν)))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (wt K kz r)) (dot3 K (a0 r) (a0 r))) l))
      (K.mul (K.mul (K.mul k k) k) V))
    (hF : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) (K.mul k F))
    (hH : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l) E)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) E)
    (hF2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l) E)
    (hH3 : Le K O.toCone (K.mul k (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N1 r) (N1 r))) l)) E) :
    Le K O.toCone (Qe K ν k l kz a1 b2)
      (K.mul (K.mul (K.mul k k) k) (K.mul (k3 K) (K.add (K.mul ν (K.add (K.add V F) (K.mul ν E)))
        (K.mul (K.mul ν ν) (K.add (K.add (K.mul (K.mul ν ν) E) E) (K.mul ν E)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have dz := defect_sum4 K O l k ν hk hν (wt K kz) hw h10 a0 a1 b1 b2 N0 N1 s1 s2 s3
  have da := defect_sumV K O l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) h10
    a0 a1 b1 b2 N0 N1 s1 s2 s3
  have q := defect_qe' K O k ν _ _ _ _ _ _ _ _ V F E hk hν dz da hV hF hH hB4 hF2 hH3
  exact q

end Gen

end LocalDefect

#print axioms LocalDefect.local_defect
#print axioms LocalDefect.local_defect'

#print axioms: 2 theorems, no axioms.

79.19 The force difference in the defect is \(O(k)\)

With \(b_1\) the \(k\)-step from \(a_0\): \(\sum _rw_r|N_{0,r}-N_{1,r}|^2\le k\cdot F\), \(F\) explicit in the \(H^1\)–\(H^5\) norms of \(a_0\) and the force. ForceDiffW with TimeLipW for \(d=b_1-a_0\) in the weights \(w^3\), \(w\), \(w^2\).

ns/FDk.lean

/-
  ns/FDk.lean — T37 cycle 1129 (goal G717e, piece 3): the force difference inside the step-size defect is O(k).
  With b₁ the k-step from a₀ (force N₀), N₁ the force at b₁, d = b₁ − a₀, over an ordered field (0 ≤ k ≤ 1):
    lip_kQ     2k²G + 2k²ν²B₅ ≤ k(2G + 2Q)   when kν²B₅ ≤ Q
    fd_scheme  Σ w|N₀ − N₁|² ≤ k·(2ι²(2G₃ + 2Q)P₀ + 2ι²E(2s₁(2G₁ + 2ν²B₃) + 2s₀(2G₂ + 2ν²B₄)))
  given the projection bound Σw|N₀ − N₁|² ≤ Σw|N(b₁) − N(a₀)|², Σw^j|N₀|² ≤ G_j (j = 1, 2, 3), Σw³|a₀|² = B₃,
  Σw⁴|a₀|² = B₄, kν²Σw⁵|a₀|² ≤ Q, 2S₁Z(b₁) + 2S₀A(b₁) ≤ P₀, B(a₀) ≤ E, S₁ ≤ s₁, S₀ ≤ s₀
    fd_scheme'  the same with k·Σw³|N₀|² ≤ G₃ (uniform in the step)
  (ForceDiffW + TimeLipW with V = w³, w, w² + DefectOrder.fd_k).

  Lean 4 core plus ns/ForceDiffW, ns/TimeLipW, ns/DefectOrder.
-/
import ns.ForceDiffW
import ns.TimeLipW
import ns.DefectOrder

namespace FDk
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 ForceH2Bi ForceDiff EnstDiff ForceDiffW EnstStep TimeLipW DefectOrder

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lip_kQ (k ν G B5 Q : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one) (hG : O.nn G)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) G)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B5))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) Q))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have a := k2_le K O k (K.mul (K.add K.one K.one) G) hk hk1 (O.nn_mul _ _ two hG)
  have b := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hQ
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ a b) ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

theorem lip_kQ' (k ν X G B5 Q : R) (hk : O.nn k) (hX : Le K O.toCone (K.mul k X) G)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) B5) Q) :
    Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul k k)) X)
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν))) B5))
      (K.mul k (K.add (K.mul (K.add K.one K.one) G) (K.mul (K.add K.one K.one) Q))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have a := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hX
  have b := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) k) _ _ (O.nn_mul _ _ two hk) hQ
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ a b) ⟨K.zero, O.nn_zero, ?_⟩)
  · lib_ring
  · lib_ring

include mul_inv h10 in
theorem fd_scheme (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι k ν G1 G2 G3 B3 B4 Q P0 E s1 s0 : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (nG1 : O.nn G1) (nG2 : O.nn G2) (nG3 : O.nn G3) (nB3 : O.nn B3) (nB4 : O.nn B4)
    (nP0 : O.nn P0) (ns1 : O.nn s1) (ns0 : O.nn s0) (a0 b1 N0 N1 : I → V3 R)
    (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (hproj : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r)))) l))
    (hG1 : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) G1)
    (hG2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G2)
    (hG3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G3)
    (hB3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B3)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B4)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)) Q)
    (hP : Le K O.toCone (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l b1)))
      (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l b1)))) P0)
    (hBb : Le K O.toCone (Bw K kz l a0) E) (hS1 : Le K O.toCone (S1w K inv kz l) s1) (hS0 : Le K O.toCone (S0w K inv kz l) s0) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q)) P0)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
          (K.add (K.mul (K.add K.one K.one) (K.mul s1 (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))))
            (K.mul (K.add K.one K.one) (K.mul s0 (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4))))))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fdw := force_diffW K O inv mul_inv h10 l kz hd h1 ι b1 a0
  -- the three TimeLipW bounds for d = b₁ − a₀
  have tB := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) hw
    (fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)) b1 a0 N0 s2
  have tZ := time_lipW K O.toCone l k ν hk hν (wt K kz) (wt K kz) hw hw b1 a0 N0 s2
  have tA := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) b1 a0 N0 s2
  have nB5 : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nZN : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nAN : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nBN : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  -- B(d) ≤ k(2G₃ + 2Q)
  have hBd : Le K O.toCone (Bw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG3) (le_refl' K O.toCone
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul k k) (K.mul ν ν)))
        (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)))
    exact le_trans' K O.toCone _ _ _ tB (le_trans' K O.toCone _ _ _ m (lip_kQ K O k ν G3 _ Q hk hk1 nG3 hQ))
  -- Z(d) ≤ k(2G₁ + 2ν²B₃),  A(d) ≤ k(2G₂ + 2ν²B₄)
  have hZd : Le K O.toCone (Zw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG1)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB3)
    exact le_trans' K O.toCone _ _ _ tZ (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G1 B3 hk hk1 nG1 nB3))
  have hAd : Le K O.toCone (Aw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB4)
    exact le_trans' K O.toCone _ _ _ tA (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G2 B4 hk hk1 nG2 nB4))
  have nBd : O.nn (Bw K kz l (dF K b1 a0)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nBb : O.nn (Bw K kz l a0) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (S1w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (S0w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  have fk := fd_k K O k ι _ _ _ _ _ _ _ _ _ _ _ P0 E s1 s0 hk nBd nBb nS1 nS0 nP0
    (O.nn_add _ _ (O.nn_mul _ _ two nG1) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB3)))
    (O.nn_add _ _ (O.nn_mul _ _ two nG2) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB4))) ns1 ns0
    fdw hBd hZd hAd hP hBb hS1 hS0
  exact le_trans' K O.toCone _ _ _ hproj fk

include mul_inv h10 in
theorem fd_scheme' (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι k ν G1 G2 G3 B3 B4 Q P0 E s1 s0 : R) (hk : O.nn k) (hk1 : Le K O.toCone k K.one)
    (hν : O.nn ν) (nG1 : O.nn G1) (nG2 : O.nn G2) (nB3 : O.nn B3) (nB4 : O.nn B4)
    (nP0 : O.nn P0) (ns1 : O.nn s1) (ns0 : O.nn s0) (a0 b1 N0 N1 : I → V3 R)
    (s2 : ∀ r, Step K k ν (wt K kz r) (b1 r) (a0 r) (N0 r))
    (hproj : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b1 b1 r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a0 a0 r)))) l))
    (hG1 : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) G1)
    (hG2 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) G2)
    (hG3 : Le K O.toCone (K.mul k (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l)) G3)
    (hB3 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B3)
    (hB4 : Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) B4)
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l)) Q)
    (hP : Le K O.toCone (K.add (K.mul (K.add K.one K.one) (K.mul (S1w K inv kz l) (Zw K kz l b1)))
      (K.mul (K.add K.one K.one) (K.mul (S0w K inv kz l) (Aw K kz l b1)))) P0)
    (hBb : Le K O.toCone (Bw K kz l a0) E) (hS1 : Le K O.toCone (S1w K inv kz l) s1) (hS0 : Le K O.toCone (S0w K inv kz l) s0) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (sub3 K (N0 r) (N1 r)) (sub3 K (N0 r) (N1 r)))) l)
      (K.mul k (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q)) P0)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul E
          (K.add (K.mul (K.add K.one K.one) (K.mul s1 (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))))
            (K.mul (K.add K.one K.one) (K.mul s0 (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4))))))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fdw := force_diffW K O inv mul_inv h10 l kz hd h1 ι b1 a0
  -- the three TimeLipW bounds for d = b₁ − a₀
  have tB := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) hw
    (fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)) b1 a0 N0 s2
  have tZ := time_lipW K O.toCone l k ν hk hν (wt K kz) (wt K kz) hw hw b1 a0 N0 s2
  have tA := time_lipW K O.toCone l k ν hk hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _) b1 a0 N0 s2
  have nB5 : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a0 r) (a0 r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nZN : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nAN : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nBN : O.nn (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (N0 r) (N0 r))) l) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  -- B(d) ≤ k(2G₃ + 2Q)
  have hBd : Le K O.toCone (Bw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G3) (K.mul (K.add K.one K.one) Q))) :=
    le_trans' K O.toCone _ _ _ tB (lip_kQ' K O k ν _ G3 _ Q hk hG3 hQ)
  -- Z(d) ≤ k(2G₁ + 2ν²B₃),  A(d) ≤ k(2G₂ + 2ν²B₄)
  have hZd : Le K O.toCone (Zw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G1) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B3)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG1)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB3)
    exact le_trans' K O.toCone _ _ _ tZ (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G1 B3 hk hk1 nG1 nB3))
  have hAd : Le K O.toCone (Aw K kz l (dF K b1 a0)) (K.mul k (K.add (K.mul (K.add K.one K.one) G2) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) B4)))) := by
    have m := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq k)) hG2)
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq k) (O.nn_sq ν))) hB4)
    exact le_trans' K O.toCone _ _ _ tA (le_trans' K O.toCone _ _ _ m (lip_k K O k ν G2 B4 hk hk1 nG2 nB4))
  have nBd : O.nn (Bw K kz l (dF K b1 a0)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nBb : O.nn (Bw K kz l a0) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw r)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (S1w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (S0w K inv kz l) := lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  have fk := fd_k K O k ι _ _ _ _ _ _ _ _ _ _ _ P0 E s1 s0 hk nBd nBb nS1 nS0 nP0
    (O.nn_add _ _ (O.nn_mul _ _ two nG1) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB3)))
    (O.nn_add _ _ (O.nn_mul _ _ two nG2) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nB4))) ns1 ns0
    fdw hBd hZd hAd hP hBb hS1 hS0
  exact le_trans' K O.toCone _ _ _ hproj fk

end Gen

end FDk

#print axioms FDk.lip_kQ
#print axioms FDk.fd_scheme
#print axioms FDk.lip_kQ'
#print axioms FDk.fd_scheme'

#print axioms: 4 theorems, no axioms.

79.20 Weighted Cauchy–Schwarz for telescoping sums

\((\sum _jd_j)^2\le (\sum _jm_j)(\sum _jm_j^{-1}d_j^2)\), and \(\sum _r\mu _r|\sum _jd_{j,r}|^2\le (\sum _jm_j)\sum _jm_j^{-1}\sum _r\mu _r|d_{j,r}|^2\). Weighted Lagrange identity; sums dyadic Cauchy sequences without square roots.

ns/WCS.lean

/-
  ns/WCS.lean — T37 cycle 1131 (goal G737): weighted Cauchy–Schwarz for telescoping sums, over any commutative ring
  with a nonnegative cone. For weights m_j, im_j ≥ 0 with m_j·im_j = 1:
    cross      Σ_{i<n} m_i m (im_i d_i − im d)² = m·Σ im_i d_i² − 2d·Σ d_i + im d²·Σ m_i     (m·im = 1)
    wcs        (Σ_{j<n} d_j)² ≤ (Σ_{j<n} m_j)·(Σ_{j<n} im_j d_j²)                         (weighted Lagrange)
    wcs_form   Σ_r μ_r|Σ_{j<n} d_{j,r}|² ≤ (Σ_j m_j)·Σ_j im_j·Σ_r μ_r|d_{j,r}|²         (μ_r ≥ 0)
  With m_j = (3/4)^j and step-size errors ≤ C·k₀2^{−j} this sums a dyadic Cauchy sequence without square roots (G736).

  Lean 4 core plus NSChain (sumN), ns/TimeLipN (lsum_sumN, sumN_add3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLipN
import ns.AbelCount

namespace WCS
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem cross (mw imw d : Nat → R) (m im e : R) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) (hm : K.mul m im = K.one) : ∀ n,
    sumN K (fun i => K.mul (K.mul (mw i) m) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e)))
        (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e))))) n
      = K.add (K.add (K.mul m (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n))
          (K.neg (K.mul (K.add e e) (sumN K d n)))) (K.mul (K.mul im (K.mul e e)) (sumN K mw n))
  | 0 => by
    show K.zero = K.add (K.add (K.mul m K.zero) (K.neg (K.mul (K.add e e) K.zero))) (K.mul (K.mul im (K.mul e e)) K.zero)
    lib_ring
  | n + 1 => by
    show K.add (sumN K (fun i => K.mul (K.mul (mw i) m) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e)))
        (K.add (K.mul (imw i) (d i)) (K.neg (K.mul im e))))) n)
        (K.mul (K.mul (mw n) m) (K.mul (K.add (K.mul (imw n) (d n)) (K.neg (K.mul im e))) (K.add (K.mul (imw n) (d n)) (K.neg (K.mul im e)))))
      = K.add (K.add (K.mul m (K.add (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n) (K.mul (imw n) (K.mul (d n) (d n)))))
          (K.neg (K.mul (K.add e e) (K.add (sumN K d n) (d n))))) (K.mul (K.mul im (K.mul e e)) (K.add (sumN K mw n) (mw n)))
    rw [cross mw imw d m im e hmi hm n]
    have h1 := hmi n
    generalize sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n = W at *
    generalize sumN K d n = S at *
    generalize sumN K mw n = M at *
    generalize mw n = a at h1 ⊢
    generalize imw n = ia at h1 ⊢
    generalize d n = x
    calc _ = K.add (K.add (K.add (K.mul m W) (K.neg (K.mul (K.add e e) S))) (K.mul (K.mul im (K.mul e e)) M))
          (K.add (K.add (K.mul (K.mul (K.mul m ia) (K.mul x x)) (K.mul a ia))
            (K.neg (K.mul (K.mul (K.add x x) e) (K.mul (K.mul a ia) (K.mul m im)))))
            (K.mul (K.mul a (K.mul im (K.mul e e))) (K.mul m im))) := by lib_ring
      _ = K.add (K.add (K.add (K.mul m W) (K.neg (K.mul (K.add e e) S))) (K.mul (K.mul im (K.mul e e)) M))
          (K.add (K.add (K.mul (K.mul (K.mul m ia) (K.mul x x)) K.one)
            (K.neg (K.mul (K.mul (K.add x x) e) (K.mul K.one K.one))))
            (K.mul (K.mul a (K.mul im (K.mul e e))) K.one)) := by rw [h1, hm]
      _ = _ := by lib_ring

theorem sumN_nn (f : Nat → R) (h : ∀ i, P.nn (f i)) : ∀ n, P.nn (sumN K f n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (sumN_nn f h n) (h n)

theorem wcs (mw imw d : Nat → R) (hmw : ∀ i, P.nn (mw i)) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) : ∀ n,
    Le K P (K.mul (sumN K d n) (sumN K d n))
      (K.mul (sumN K mw n) (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero K.zero) K.zero = K.mul K.zero K.zero; lib_ring⟩
  | n + 1 => by
    have ih := wcs mw imw d hmw hmi n
    have cr := cross K mw imw d (mw n) (imw n) (d n) hmi (hmi n) n
    cases ih with
    | intro D hD =>
      refine ⟨K.add D (sumN K (fun i => K.mul (K.mul (mw i) (mw n)) (K.mul (K.add (K.mul (imw i) (d i)) (K.neg (K.mul (imw n) (d n))))
          (K.add (K.mul (imw i) (d i)) (K.neg (K.mul (imw n) (d n)))))) n),
        P.nn_add _ _ hD.1 (sumN_nn K P _ (fun i => P.nn_mul _ _ (P.nn_mul _ _ (hmw i) (hmw n)) (P.nn_sq _)) n), ?_⟩
      rw [cr]
      show K.add (K.mul (K.add (sumN K d n) (d n)) (K.add (sumN K d n) (d n))) _
        = K.mul (K.add (sumN K mw n) (mw n)) (K.add (sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n) (K.mul (imw n) (K.mul (d n) (d n))))
      have key := hD.2
      have h1 := hmi n
      generalize sumN K (fun i => K.mul (imw i) (K.mul (d i) (d i))) n = W at *
      generalize sumN K d n = S at *
      generalize sumN K mw n = M at *
      generalize mw n = a at h1 ⊢
      generalize imw n = ia at h1 ⊢
      generalize d n = x
      calc _ = K.add (K.add (K.mul S S) D) (K.add (K.add (K.mul K.one (K.mul x x)) (K.mul a W)) (K.mul (K.mul ia (K.mul x x)) M)) := by lib_ring
        _ = K.add (K.mul M W) (K.add (K.add (K.mul (K.mul a ia) (K.mul x x)) (K.mul a W)) (K.mul (K.mul ia (K.mul x x)) M)) := by rw [key, h1]
        _ = _ := by lib_ring

theorem wcs3 (mw imw : Nat → R) (hmw : ∀ i, P.nn (mw i)) (hmi : ∀ i, K.mul (mw i) (imw i) = K.one)
    (d : Nat → V3 R) (n : Nat) :
    Le K P (dot3 K (V3.mk (sumN K (fun j => (d j).x) n) (sumN K (fun j => (d j).y) n) (sumN K (fun j => (d j).z) n))
        (V3.mk (sumN K (fun j => (d j).x) n) (sumN K (fun j => (d j).y) n) (sumN K (fun j => (d j).z) n)))
      (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j) (d j))) n)) := by
  have cx := wcs K P mw imw (fun j => (d j).x) hmw hmi n
  have cy := wcs K P mw imw (fun j => (d j).y) hmw hmi n
  have cz := wcs K P mw imw (fun j => (d j).z) hmw hmi n
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  have e := sumN_add3 K (fun j => K.mul (imw j) (K.mul (d j).x (d j).x)) (fun j => K.mul (imw j) (K.mul (d j).y (d j).y))
    (fun j => K.mul (imw j) (K.mul (d j).z (d j).z)) n
  have e2 : sumN K (fun j => K.mul (imw j) (dot3 K (d j) (d j))) n
      = sumN K (fun j => K.add (K.add (K.mul (imw j) (K.mul (d j).x (d j).x)) (K.mul (imw j) (K.mul (d j).y (d j).y)))
          (K.mul (imw j) (K.mul (d j).z (d j).z))) n := by
    apply AbelCount.sumN_congr; intro j
    show K.mul (imw j) (K.add (K.add (K.mul (d j).x (d j).x) (K.mul (d j).y (d j).y)) (K.mul (d j).z (d j).z)) = _
    lib_ring
  rw [e2, ← e]
  show K.add (K.add (K.add (K.mul (sumN K mw n) _) (K.mul (sumN K mw n) _)) (K.mul (sumN K mw n) _)) K.zero = _
  generalize sumN K mw n = M
  lib_ring

theorem sumN_mul_l (c : R) (f : Nat → R) : ∀ n, K.mul c (sumN K f n) = sumN K (fun j => K.mul c (f j)) n
  | 0 => by show K.mul c K.zero = K.zero; lib_ring
  | n + 1 => by
    show K.mul c (K.add (sumN K f n) (f n)) = K.add (sumN K (fun j => K.mul c (f j)) n) (K.mul c (f n))
    rw [← sumN_mul_l c f n]; lib_ring

/-- the vector sum Σ_{j<n} d_j at mode r -/
def vs {I : Type} (d : Nat → I → V3 R) (n : Nat) (r : I) : V3 R :=
  V3.mk (sumN K (fun j => (d j r).x) n) (sumN K (fun j => (d j r).y) n) (sumN K (fun j => (d j r).z) n)

theorem wcs_form {I : Type} (l : List I) (μ : I → R) (hμ : ∀ r, P.nn (μ r)) (mw imw : Nat → R) (hmw : ∀ i, P.nn (mw i))
    (hmi : ∀ i, K.mul (mw i) (imw i) = K.one) (d : Nat → I → V3 R) (n : Nat) :
    Le K P (lsum K (fun r => K.mul (μ r) (dot3 K (vs K d n r) (vs K d n r))) l)
      (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (lsum K (fun r => K.mul (μ r) (dot3 K (d j r) (d j r))) l)) n)) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (μ r) _ _ (hμ r) (wcs3 K P mw imw hmw hmi (fun j => d j r) n)) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero']
  have e1 : lsum K (fun r => K.mul (μ r) (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n))) l
      = K.mul (sumN K mw n) (lsum K (fun r => sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n) l) := by
    rw [← lsum_mul_left]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (μ r) (K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n))
      = K.mul (sumN K mw n) (sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n)
    have e := sumN_mul_l K (μ r) (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n
    have e' : sumN K (fun j => K.mul (μ r) (K.mul (imw j) (dot3 K (d j r) (d j r)))) n
        = sumN K (fun j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) n :=
      AbelCount.sumN_congr K _ _ (fun j => by lib_ring) n
    rw [← e', ← e]
    generalize sumN K mw n = M
    generalize sumN K (fun j => K.mul (imw j) (dot3 K (d j r) (d j r))) n = W
    lib_ring
  rw [e1, lsum_sumN K (fun r j => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) l n]
  have e2 : sumN K (fun j => lsum K (fun r => K.mul (imw j) (K.mul (μ r) (dot3 K (d j r) (d j r)))) l) n
      = sumN K (fun j => K.mul (imw j) (lsum K (fun r => K.mul (μ r) (dot3 K (d j r) (d j r))) l)) n :=
    AbelCount.sumN_congr K _ _ (fun j => by rw [lsum_mul_left]) n
  rw [e2]

end Gen

end WCS

#print axioms WCS.cross
#print axioms WCS.sumN_nn
#print axioms WCS.wcs
#print axioms WCS.wcs3
#print axioms WCS.sumN_mul_l
#print axioms WCS.wcs_form

#print axioms: 6 theorems, no axioms.

79.21 Dyadic step-halving is Cauchy

If \(Q(x_{j+1}-x_j)\le Ck_0h^j\) then \(Q(x_J-x_0)\le c_p\,C\,k_0\,c_r\) for all \(J\); with \(h=1/2\), \(p=3/4\): \(Q(x_J-x_0)\le 12Ck_0\). Geometric sums and the weighted Cauchy–Schwarz, no square roots.

ns/Dyadic.lean

/-
  ns/Dyadic.lean — T37 cycle 1133 (goal G736): summing a dyadic sequence of step-size errors without square roots,
  over an ordered ring. For a quadratic form Q(u) = Σ_r μ_r|u_r|² (μ_r ≥ 0):
    geom_le    r, c ≥ 0, c(1 − r) = 1  ⇒  Σ_{j<J} r^j ≤ c
    tele_vs    Σ_{j<J} (x_{j+1} − x_j) = x_J − x_0   (per mode)
    dyadic     Q(x_{j+1} − x_j) ≤ C·k₀hʲ for all j, pq = 1, c_p(1 − p) = 1, c_r(1 − qh) = 1  ⇒
               Q(x_J − x_0) ≤ c_p·(C·k₀·c_r)   for every J.
  With h = 1/2, p = 3/4, q = 4/3 (qh = 2/3): Q(x_J − x_0) ≤ 12·C·k₀, so the step-halving sequence of StepCauchy is
  Cauchy in Qe with a modulus independent of the truncation (WCS.wcs_form).

  Lean 4 core plus ns/WCS, ns/EnstGronwall (pwr), ns/ForceBilin (v3_ext), ns/ForceDiff (dF).
-/
import ns.WCS
import ns.EnstGronwall
import ns.ForceDiff
import ns.TruncRate

namespace Dyadic
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall ForceBilin ForceDiff

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem sumN_le (f g : Nat → R) (h : ∀ n, Le K O.toCone (f n) (g n)) : ∀ M, Le K O.toCone (sumN K f M) (sumN K g M)
  | 0 => le_refl' K O.toCone _
  | M + 1 => le_add' K O.toCone _ _ _ _ (sumN_le f g h M) (h M)

theorem pwr_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pwr K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ hx (pwr_nn x hx n)

theorem pwr_mul (p q : R) : ∀ n, K.mul (pwr K p n) (pwr K q n) = pwr K (K.mul p q) n
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul p (pwr K p n)) (K.mul q (pwr K q n)) = K.mul (K.mul p q) (pwr K (K.mul p q) n)
    rw [← pwr_mul p q n]; lib_ring

theorem geom_id (r : R) : ∀ J, K.add (K.mul (K.add K.one (K.neg r)) (sumN K (pwr K r) J)) (pwr K r J) = K.one
  | 0 => by show K.add (K.mul (K.add K.one (K.neg r)) K.zero) K.one = K.one; lib_ring
  | J + 1 => by
    have ih := geom_id r J
    show K.add (K.mul (K.add K.one (K.neg r)) (K.add (sumN K (pwr K r) J) (pwr K r J))) (K.mul r (pwr K r J)) = K.one
    generalize sumN K (pwr K r) J = S at ih ⊢
    generalize pwr K r J = P at ih ⊢
    exact Eq.trans (by lib_ring) ih

theorem geom_le (r c : R) (hr : O.nn r) (hc : O.nn c) (h : K.mul c (K.add K.one (K.neg r)) = K.one) (J : Nat) :
    Le K O.toCone (sumN K (pwr K r) J) c := by
  have g := geom_id K r J
  refine ⟨K.mul c (pwr K r J), O.nn_mul _ _ hc (pwr_nn K O r hr J), ?_⟩
  calc K.add (sumN K (pwr K r) J) (K.mul c (pwr K r J))
      = K.add (K.mul (K.mul c (K.add K.one (K.neg r))) (sumN K (pwr K r) J)) (K.mul c (pwr K r J)) := by rw [h]; lib_ring
    _ = K.mul c (K.add (K.mul (K.add K.one (K.neg r)) (sumN K (pwr K r) J)) (pwr K r J)) := by lib_ring
    _ = c := by rw [g]; lib_ring

theorem tele_vs (x : Nat → I → V3 R) (J : Nat) (r : I) :
    vs K (fun j => dF K (x (j + 1)) (x j)) J r = sub3 K (x J r) (x 0 r) := by
  apply v3_ext
  · exact tele_fwd K (fun j => (x j r).x) J
  · exact tele_fwd K (fun j => (x j r).y) J
  · exact tele_fwd K (fun j => (x j r).z) J

theorem pwr_one : ∀ n, pwr K K.one n = K.one
  | 0 => rfl
  | n + 1 => by show K.mul K.one (pwr K K.one n) = K.one; rw [pwr_one n]; lib_ring

theorem dyadic (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ j, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (j + 1)) (x j) r) (dF K (x (j + 1)) (x j) r))) l)
      (K.mul C (K.mul k0 (pwr K h j)))) (J : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x J r) (x 0 r)) (sub3 K (x J r) (x 0 r)))) l)
      (K.mul cp (K.mul C (K.mul k0 cr))) := by
  have w := wcs_form K O.toCone l μ hμ (pwr K p) (pwr K q) (pwr_nn K O p hp)
    (fun i => by rw [pwr_mul, hpq]; exact pwr_one K i) (fun j => dF K (x (j + 1)) (x j)) J
  have e : lsum K (fun r => K.mul (μ r) (dot3 K (vs K (fun j => dF K (x (j + 1)) (x j)) J r) (vs K (fun j => dF K (x (j + 1)) (x j)) J r))) l
      = lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x J r) (x 0 r)) (sub3 K (x J r) (x 0 r)))) l :=
    lsum_ext K _ _ (fun r => by rw [tele_vs K x J r]) l
  rw [e] at w
  have s1 := sumN_le K O _ (fun j => K.mul (pwr K q j) (K.mul C (K.mul k0 (pwr K h j))))
    (fun j => le_mul_nn K O.toCone _ _ _ (pwr_nn K O q hq j) (hd j)) J
  have e2 : sumN K (fun j => K.mul (pwr K q j) (K.mul C (K.mul k0 (pwr K h j)))) J
      = K.mul (K.mul C k0) (sumN K (pwr K (K.mul q h)) J) := by
    rw [sumN_mul_l]
    apply AbelCount.sumN_congr; intro j
    rw [← pwr_mul]
    generalize pwr K q j = a; generalize pwr K h j = b
    lib_ring
  rw [e2] at s1
  have s2 := le_mul_nn K O.toCone (K.mul C k0) _ _ (O.nn_mul _ _ hC hk0)
    (geom_le K O (K.mul q h) cr (O.nn_mul _ _ hq hh) hcr hgr J)
  have s3 := geom_le K O p cp hp hcp hgp J
  have nW : O.nn (sumN K (fun j => K.mul (pwr K q j) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (j + 1)) (x j) r) (dF K (x (j + 1)) (x j) r))) l)) J) :=
    WCS.sumN_nn K O.toCone _ (fun j => O.nn_mul _ _ (pwr_nn K O q hq j) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hμ r) (dot3_nn K O.toCone _)) l)) J
  have m := TruncRate.mm K O _ _ _ _ (WCS.sumN_nn K O.toCone _ (pwr_nn K O p hp) J) (O.nn_mul _ _ (O.nn_mul _ _ hC hk0) hcr) s3
    (le_trans' K O.toCone _ _ _ s1 s2)
  refine le_trans' K O.toCone _ _ _ w (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩)
  lib_ring

end Gen

end Dyadic

#print axioms Dyadic.sumN_le
#print axioms Dyadic.pwr_nn
#print axioms Dyadic.pwr_mul
#print axioms Dyadic.geom_id
#print axioms Dyadic.geom_le
#print axioms Dyadic.tele_vs
#print axioms Dyadic.pwr_one
#print axioms Dyadic.dyadic

#print axioms: 8 theorems, no axioms.

79.22 One norm for truncation and step size

\(Q_e(u,w)\le 2Q_e(u,v)+2Q_e(v,w)\), and the truncation-comparison quantity of TruncRate is \(Q_e\) on the low modes. Splits a double refinement at the intermediate solution.

ns/QeTri.lean

/-
  ns/QeTri.lean — T37 cycle 1135 (goal G740): one norm for the truncation and step-size comparisons.
  Over any commutative ring with a nonnegative cone:
    qe_tri     Qe(u, w) ≤ 2·Qe(u, v) + 2·Qe(v, w)        (|x + y|² ≤ 2|x|² + 2|y|² per mode)
    q_eq_qe    the quantity Q(νZl, hν²Al) of TruncRate/TruncCauchy at time T is Qe(restr A_T, B_T) on the low modes
  so a solution (truncation N, step k) and the refined one (truncation N', step k/2ʲ) are compared by splitting at the
  intermediate solution (truncation N', step k): truncation part from TruncRate, step part from StepCauchy/Dyadic.

  Lean 4 core plus ns/StepCauchy (Qe, dF_split), ns/TruncCauchy (Zl, Al), ns/ForceTwoScale (sq2).
-/
import ns.StepCauchy
import ns.TruncCauchy

namespace QeTri
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qe_tri (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v w : I → V3 R) :
    Le K O.toCone (Qe K ν k l kz u w)
      (K.add (K.mul (K.add K.one K.one) (Qe K ν k l kz u v)) (K.mul (K.add K.one K.one) (Qe K ν k l kz v w))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have pt : ∀ (μ : I → R), (∀ r, O.nn (μ r)) →
      Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K u w r) (dF K u w r))) l)
        (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K u v r) (dF K u v r))) l))
          (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (μ r) (dot3 K (dF K v w r) (dF K v w r))) l))) := by
    intro μ hμ
    have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (μ r) _ _ (hμ r) (sq2 K O (dF K v w r) (dF K u v r))) l
    have e : lsum K (fun r => K.mul (μ r) (dot3 K (dF K u w r) (dF K u w r))) l
        = lsum K (fun r => K.mul (μ r) (dot3 K (add3 K (dF K v w r) (dF K u v r)) (add3 K (dF K v w r) (dF K u v r)))) l :=
      lsum_ext K _ _ (fun r => by rw [dF_split K u v w r]) l
    rw [e]
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun r => by lib_ring) l
  have a := pt (wt K kz) hw
  have b := pt (fun r => K.mul (wt K kz r) (wt K kz r)) (fun r => O.nn_sq _)
  have s := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone ν _ _ hν a)
    (le_mul_nn K O.toCone (K.mul k (K.mul ν ν)) _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) b)
  unfold Qe
  refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r))) l = Z1
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (dF K v w r) (dF K v w r))) l = Z2
  generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K u v r) (dF K u v r))) l = A1
  generalize lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (dF K v w r) (dF K v w r))) l = A2
  lib_ring

theorem q_eq_qe (ν h : R) (L : List I) (kz : I → Mode) (A B : Nat → I → V3 R) (T : Nat) :
    Q K ν h (fun t => TruncCauchy.Zl K kz L (dF K (A t) (B t))) (fun t => TruncCauchy.Al K kz L (dF K (A t) (B t))) T
      = Qe K ν h L kz (A T) (B T) := rfl

end Gen

end QeTri

#print axioms QeTri.qe_tri
#print axioms QeTri.q_eq_qe

#print axioms: 2 theorems, no axioms.

79.23 Weighted equicontinuity

\(\sum _rV_r|a_{t+n,r}-a_{t,r}|^2\le n^2(2h^2G+2h^2\nu ^2B)\) when \(\sum V|N_t|^2\le G\) and \(\sum Vw^2|a_t|^2\le B\). With \(V=w\): Lipschitz in time in the enstrophy norm, uniform in the truncation.

ns/EquiContW.lean

/-
  ns/EquiContW.lean — T37 cycle 1137 (goal G744): weighted (H¹-level) equicontinuity of the implicit-viscosity
  scheme, over an ordered ring. For weights μ_r, V_r ≥ 0:
    lip_nW      Σ_r μ_r|a_{n,r} − a_{0,r}|² ≤ n·Σ_{j<n} Σ_r μ_r|a_{j+1,r} − a_{j,r}|²
    equicontW   if Σ V|N_t|² ≤ G and Σ V w²|a_t|² ≤ B for all t, then
                Σ_r V_r|a_{t+n,r} − a_{t,r}|² ≤ n·n·(2h²·G + 2h²ν²·B)
  With V = w: Lipschitz in time in the enstrophy norm, G from ForceH2 and B from EnstH2 (truncation-uniform).

  Lean 4 core plus ns/TimeLipN (mode_lipN, lsum_sumN), ns/TimeLipW, ns/EquiCont (sumN_const_le).
-/
import ns.TimeLipW
import ns.EquiCont
import ns.WCS

namespace EquiContW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ModeCount CubeCount
  ForceUniform NormCompare EnstGalerkin EnstStep EnstDiff TimeLipN TimeLipW

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem lip_nW (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (a : Nat → I → V3 R) (n : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (a n r) (a 0 r)) (sub3 K (a n r) (a 0 r)))) l)
      (K.mul (castN K n) (sumN K (fun j => lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) l) n)) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (μ r) _ _ (hμ r) (mode_lipN K O.toCone (fun j => a j r) n)) l
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  rw [add_zero']
  have e1 : lsum K (fun r => K.mul (μ r) (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n))) l
      = K.mul (castN K n) (lsum K (fun r => sumN K (fun j => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) n) l) := by
    rw [← lsum_mul_left]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (μ r) (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n)) = _
    rw [← WCS.sumN_mul_l K (μ r)]
    generalize sumN K (fun j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) n = S
    lib_ring
  rw [e1, lsum_sumN K (fun r j => K.mul (μ r) (dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r)))) l n]

theorem equicontW (l : List I) (kz : I → Mode) (h ν G B : R) (hh : O.nn h) (hν : O.nn ν) (V : I → R) (hV : ∀ r, O.nn (V r))
    (a N : Nat → I → V3 R) (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hG : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (N t r) (N t r))) l) G)
    (hB : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (V r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a t r) (a t r))) l) B)
    (t n : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a (t + n) r) (a t r)) (sub3 K (a (t + n) r) (a t r)))) l)
      (K.mul (castN K n) (K.mul (castN K n)
        (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) G) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) B)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have step : ∀ j, Le K O.toCone
      (lsum K (fun r => K.mul (V r) (dot3 K (sub3 K (a (t + j + 1) r) (a (t + j) r)) (sub3 K (a (t + j + 1) r) (a (t + j) r)))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) G) (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) B)) := by
    intro j
    have tl := time_lipW K O.toCone l h ν hh hν (wt K kz) V hw hV (a (t + j + 1)) (a (t + j)) (N (t + j)) (hs (t + j))
    exact le_trans' K O.toCone _ _ _ tl (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_sq h)) (hG (t + j)))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) (hB (t + j))))
  have ln := lip_nW K O l V hV (fun j => a (t + j)) n
  have sc := EquiCont.sumN_const_le K O _ _ step n
  have m := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) sc
  exact le_trans' K O.toCone _ _ _ ln m

end Gen

end EquiContW

#print axioms EquiContW.lip_nW
#print axioms EquiContW.equicontW

#print axioms: 2 theorems, no axioms.

79.24 Explicit force norms

\(\sum w|N|^2\le \iota ^2B\,c_HA\), \(\sum w^2|N|^2\le \iota ^2B_4\,c_3B\), \(\sum w^3|N|^2\le \iota ^2B_5\,c_4B_4\), uniformly in the truncation. Instances of the \(H^2\)–\(H^4\) force bounds with the lattice sums.

ns/ForceNorms.lean

/-
  ns/ForceNorms.lean — T37 cycle 1141 (goal G746): explicit truncation-uniform force norms, over an ordered field
  (integer modes, nonzero, distinct, |k|_∞ ≤ M + 1; Z ≤ A ≤ B ≤ B₄ ≤ B₅ the weighted energies Σ w^j|a|²):
    nrm_h1   Σ w |N(a,a)|² ≤ ι²·B·(cH·A),     cH = 2·3·135 + 2·135             (ForceH2, EnstH2)
    nrm_h2   Σ w²|N(a,a)|² ≤ ι²·B₄·(c₃·B),    c₃ = 8(9·135 + 135)              (ForceH3, EnstH3)
    nrm_h3   Σ w³|N(a,a)|² ≤ ι²·B₅·(c₄·B₄),   c₄ = 32(27·135 + 135)            (ForceH4, EnstH4)
  Under the small-data bounds νB_t ≤ Q₃, νB₄_t ≤ Q₄ and hν²B₅_t ≤ Q₄ these give the hypotheses of the defect chain
  (FDk.fd_scheme', LocalDefect.local_defect') uniformly in the step and the truncation.

  Lean 4 core plus ns/EnstH4 (and through it EnstH2, EnstH3, ForceH2–H4).
-/
import ns.EnstH4

namespace ForceNorms
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH2 ForceH3 Tri4 ForceH4 EnstH2 EnstH3 EnstH4

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem nrm_h1 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l)
        (K.mul (cH K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι a
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l a
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l = A
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l = B
  generalize castN K 135 = c
  lib_ring

include mul_inv h10 in
theorem nrm_h2 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)
        (K.mul (c3 K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι a
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l a
  rw [Bform K kz l a] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l = B
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

include mul_inv h10 in
theorem nrm_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l)
        (K.mul (c4 K) (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have fh := force_h4 K O inv mul_inv h10 l kz hd h1 ι a a
  rw [B5form K kz l a, B4form K kz l a] at fh
  have hS0 := S0f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1f_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B4 K O kz h1 l a
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (q5 K (ivn K inv kz p)) (q5 K (ivn K inv kz p))) (w3 K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw3 p)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB4 hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB4
  have inner := le_mul_nn K O.toCone _ _ _ (nn32 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  refine le_trans' K O.toCone _ _ _ fh (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB5 inner)) ⟨K.zero, O.nn_zero, ?_⟩)
  unfold c4
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l = B4
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l = B5
  generalize castN K 135 = c
  generalize k32 K = e32
  generalize k27 K = e27
  lib_ring

end Gen

end ForceNorms

#print axioms ForceNorms.nrm_h1
#print axioms ForceNorms.nrm_h2
#print axioms ForceNorms.nrm_h3

#print axioms: 3 theorems, no axioms.

79.25 Mode-by-mode Cauchy modulus

After level \(j\) of step halving, each coefficient obeys \(\mu _r|x_{j+J,r}-x_{j,r}|^2\le c_p\,C\,k_0h^j\,c_r\) for all \(J\). Explicit modulus: the data of a constructive limit, uniform in the truncation.

ns/DyadicCoef.lean

/-
  ns/DyadicCoef.lean — T37 cycle 1175 (goal G762): mode-by-mode Cauchy with an explicit modulus for the step-halving
  solutions, over an ordered ring — the data of a constructive (Bishop-style) limit without real numbers.
    term_le     r ∈ l, all terms ≥ 0  ⇒  f r ≤ Σ_{l} f
    dyadic_from Q(x_{j+i+1} − x_{j+i}) ≤ C·(k₀hʲ)·hⁱ for all i  ⇒  Q(x_{j+J} − x_j) ≤ c_p·(C·(k₀hʲ)·c_r)
    coef_cauchy for every mode r ∈ l: μ_r·|x_{j+J,r} − x_{j,r}|² ≤ c_p·(C·(k₀hʲ)·c_r)
  With μ = νw (enstrophy weight, w_r ≥ 1) and h = 1/2: every Fourier coefficient of the step-halving sequence
  (StepCauchy) moves by at most 12·C·k₀·2^{−j}/ν after level j, uniformly in J and in the truncation.

  Lean 4 core plus ns/Dyadic.
-/
import ns.Dyadic

namespace DyadicCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall
  ForceBilin ForceDiff Dyadic

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem term_le (f : I → R) (hf : ∀ r, O.nn (f r)) : ∀ (l : List I) (r : I), r ∈ l → Le K O.toCone (f r) (lsum K f l)
  | [], _, h => nomatch h
  | x :: l, r, h => by
    cases h with
    | head => exact ⟨lsum K f l, lsum_nn K O.toCone _ hf l, rfl⟩
    | tail _ h' =>
      have ih := term_le f hf l r h'
      refine le_trans' K O.toCone _ _ _ ih ⟨f x, hf x, ?_⟩
      show K.add (lsum K f l) (f x) = K.add (f x) (lsum K f l)
      lib_ring

theorem pwr_add (h : R) (j : Nat) : ∀ i, pwr K h (j + i) = K.mul (pwr K h j) (pwr K h i)
  | 0 => by show pwr K h j = K.mul (pwr K h j) K.one; lib_ring
  | i + 1 => by
    show K.mul h (pwr K h (j + i)) = K.mul (pwr K h j) (K.mul h (pwr K h i))
    rw [pwr_add h j i]; lib_ring

theorem dyadic_from (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i)))) (j J : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r)))) l)
      (K.mul cp (K.mul C (K.mul (K.mul k0 (pwr K h j)) cr))) := by
  have d := dyadic K O l μ hμ (fun i => x (j + i)) C (K.mul k0 (pwr K h j)) h p q cp cr hC
    (O.nn_mul _ _ hk0 (Dyadic.pwr_nn K O h hh j)) hh hp hq hcp hcr hpq hgp hgr (fun i => by
      have e := hd (j + i)
      rw [pwr_add K h j i] at e
      refine le_trans' K O.toCone _ _ _ e ⟨K.zero, O.nn_zero, ?_⟩
      lib_ring) J
  exact d

theorem coef_cauchy (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i)))) (j J : Nat) (r : I) (hr : r ∈ l) :
    Le K O.toCone (K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r))))
      (K.mul cp (K.mul C (K.mul (K.mul k0 (pwr K h j)) cr))) :=
  le_trans' K O.toCone _ _ _
    (term_le K O (fun r => K.mul (μ r) (dot3 K (sub3 K (x (j + J) r) (x j r)) (sub3 K (x (j + J) r) (x j r))))
      (fun r => O.nn_mul _ _ (hμ r) (dot3_nn K O.toCone _)) l r hr)
    (dyadic_from K O l μ hμ x C k0 h p q cp cr hC hk0 hh hp hq hcp hcr hpq hgp hgr hd j J)

end Gen

end DyadicCoef

#print axioms DyadicCoef.term_le
#print axioms DyadicCoef.pwr_add
#print axioms DyadicCoef.dyadic_from
#print axioms DyadicCoef.coef_cauchy

#print axioms: 4 theorems, no axioms.

79.26 Integrated form of the scheme

\(a_n-a_0=\sum _{k<n}h\,(N_k-\nu w\,a_{k+1})\) for the implicit-viscosity scheme. Discrete Duhamel form; with the Cauchy modulus, the weak form of the limit equation.

ns/IntegralForm.lean

/-
  ns/IntegralForm.lean — T37 cycle 1177 (goal G764a): the integrated form of the implicit-viscosity scheme, over any
  commutative ring. From StepC: (1 + hνw)a' = a + hN, i.e. a' − a = h(N − νw·a'):
    step_diff    a_{k+1} − a_k = h·(N_k − νw·a_{k+1})
    integral     a_n − a_0 = Σ_{k<n} h·(N_k − νw·a_{k+1})            (discrete Duhamel / weak form)
  So along the step-halving sequence the coefficients satisfy the time-integrated Galerkin equation with Riemann sums
  of the force and the dissipation; with the Cauchy modulus of DyadicCoef this is the discrete form of
  a(t) − a(0) = ∫₀ᵗ (N(a) − νw a) ds for the limit.

  Lean 4 core plus NSChain (sumN), ns/EnstStep (StepC), ns/TimeLipN (tele_fwd), lib/RingTac.
-/
import ns.TimeLipN
import ns.AbelCount

namespace IntegralForm
open DetChain NSChain LibRing EnstStep TimeLipN

section Gen
variable {R : Type} (K : CRing R)

theorem step_diff (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    K.add a' (K.neg a) = K.mul h (K.add n (K.neg (K.mul (K.mul ν w) a'))) := by
  unfold StepC at hs
  calc K.add a' (K.neg a) = K.add (K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg a))
        (K.neg (K.mul (K.mul (K.mul h ν) w) a')) := by lib_ring
    _ = K.add (K.add (K.add a (K.mul h n)) (K.neg a)) (K.neg (K.mul (K.mul (K.mul h ν) w) a')) := by rw [hs]
    _ = _ := by lib_ring

theorem integral (h ν w : R) (a N : Nat → R) (hs : ∀ k, StepC K h ν w (a (k + 1)) (a k) (N k)) (n : Nat) :
    K.add (a n) (K.neg (a 0)) = sumN K (fun k => K.mul h (K.add (N k) (K.neg (K.mul (K.mul ν w) (a (k + 1)))))) n := by
  rw [← tele_fwd K a n]
  exact AbelCount.sumN_congr K _ _ (fun k => step_diff K h ν w (a (k + 1)) (a k) (N k) (hs k)) n

end Gen

end IntegralForm

#print axioms IntegralForm.step_diff
#print axioms IntegralForm.integral

#print axioms: 2 theorems, no axioms.

79.27 Explicit Lipschitz rate

Under \(Z(u_t)\le Z_b\), \(A(v_t)\le A_b\): \(\alpha _t\le 4\iota ^2C_1Z_b+2\iota ^2\cdot 135\,A_b\). The rate hypothesis of the step-size Cauchy estimate with an explicit constant.

ns/SmallAlpha.lean

/-
  ns/SmallAlpha.lean — T37 cycle 1187 (goal G768, piece 1): the Lipschitz rate of StepCauchy under uniform bounds.
  If Z(u_t) ≤ Zb and A(v_t) ≤ Ab for all t, then
    alpha_le   α_t = 2·2ι²C₁·Z(u_t) + 2ι²·135·A(v_t) ≤ 2·2ι²C₁·Zb + 2ι²·135·Ab
  over an ordered field (the hypothesis hαle of StepCauchy.step_cauchy with an explicit α; Zb, Ab from the small-data
  enstrophy and H² bounds EnstGalerkin, EnstH2).

  Lean 4 core plus ns/StepCauchy (alpha).
-/
import ns.StepCauchy

namespace SmallAlpha
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem alpha_le (ι Zb Ab : R) (Rc : Nat) (l : List I) (kz : I → Mode) (u v : Nat → I → V3 R) (t : Nat)
    (hZ : Le K O.toCone (Zs K l kz u t) Zb) (hA : Le K O.toCone (As K l kz v t) Ab) :
    Le K O.toCone (alpha K inv ι Rc l kz u v t)
      (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) Zb)))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) := by
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
  exact le_add' K O.toCone _ _ _ _
    (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (le_mul_nn K O.toCone _ _ _ nC1 hZ)))
    (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) hA)))

end Gen

end SmallAlpha

#print axioms SmallAlpha.alpha_le

#print axioms: 1 theorem, no axioms.

79.28 Force hypotheses from energy bounds

For a projected force: \(\sum w|N|^2\le \iota ^2E_3c_HE_2\), \(\sum w^2|N|^2\le \iota ^2E_4c_3E_3\), \(k\nu ^2\sum w^3|N|^2\le \iota ^2Qc_4E_4\). The force constants of the defect chain, uniform in the step and the truncation.

ns/ForceHyps.lean

/-
  ns/ForceHyps.lean — T37 cycle 1191 (goal G768, piece 2): the force-norm hypotheses of the defect chain from energy
  bounds, over an ordered field. For a force N with the projection bounds Σ W|N|² ≤ Σ W|N(a,a)|² (W = w, w², w³):
    g1_le   Σ w |N|² ≤ ι²·E₃·(cH·E₂)              if B(a) ≤ E₃, A(a) ≤ E₂
    g2_le   Σ w²|N|² ≤ ι²·E₄·(c₃·E₃)              if B₄(a) ≤ E₄, B(a) ≤ E₃
    g3_le   k·ν²·Σ w³|N|² ≤ ι²·Q·(c₄·E₄)          if k·ν²·B₅(a) ≤ Q, B₄(a) ≤ E₄
    diff_le Σ V|N₀ − N₁|² ≤ 2ΣV|N₀|² + 2ΣV|N₁|²
  (ForceNorms.nrm_h1–h3 and monotonicity). These are the constants G₁, G₂ and, after dividing by ν², G₃ of
  FDk.fd_scheme' and E of LocalDefect.local_defect', uniform in the step and the truncation.

  Lean 4 core plus ns/ForceNorms.
-/
import ns.ForceNorms

namespace ForceHyps
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH2 ForceH3 Tri4 ForceH4 EnstH2 EnstH3 EnstH4 ForceNorms

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem cH_nn : O.nn (cH K) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_add _ _ two (RatModel.nn_one' K O.toCone)) (RatModel.nn_cast K O.toCone 135)))
    (O.nn_mul _ _ two (RatModel.nn_cast K O.toCone 135))

include mul_inv h10 in
theorem g1_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι E3 E2 : R) (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hB : Le K O.toCone (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) E3)
    (hA : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) E2) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N r) (N r))) l) (K.mul (K.mul ι ι) (K.mul E3 (K.mul (cH K) E2))) := by
  have n := nrm_h1 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  have m := TruncRate.mm K O _ _ _ _ nB (O.nn_mul _ _ (cH_nn K O) (TruncRate.nn_of_le K O _ _ nA hA)) hB
    (le_mul_nn K O.toCone _ _ _ (cH_nn K O) hA)
  exact le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ n (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))

theorem c3_nn : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) (RatModel.nn_cast K O.toCone 135)) (RatModel.nn_cast K O.toCone 135))

theorem c4_nn : O.nn (c4 K) := O.nn_mul _ _ (nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn27 K O) (RatModel.nn_cast K O.toCone 135)) (RatModel.nn_cast K O.toCone 135))

include mul_inv h10 in
theorem g2_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι E4 E3 : R) (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hB4 : Le K O.toCone (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) E4)
    (hB : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) E3) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N r) (N r))) l) (K.mul (K.mul ι ι) (K.mul E4 (K.mul (c3 K) E3))) := by
  have n := nrm_h2 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have m := TruncRate.mm K O _ _ _ _ nB4 (O.nn_mul _ _ (c3_nn K O) (TruncRate.nn_of_le K O _ _ nB hB)) hB4
    (le_mul_nn K O.toCone _ _ _ (c3_nn K O) hB)
  exact le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ n (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))

include mul_inv h10 in
theorem g3_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (ι k ν Q E4 : R) (hk : O.nn k) (hν : O.nn ν)
    (a N : I → V3 R)
    (hP : Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N r) (N r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l))
    (hQ : Le K O.toCone (K.mul (K.mul k (K.mul ν ν))
      (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l)) Q)
    (hB4 : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) E4) :
    Le K O.toCone (K.mul (K.mul k (K.mul ν ν)) (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N r) (N r))) l))
      (K.mul (K.mul ι ι) (K.mul Q (K.mul (c4 K) E4))) := by
  have n := nrm_h3 K O inv mul_inv h10 l kz hd h1 M hM ι a
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hw3 : ∀ r, O.nn (w3 K kz r) := fun r => O.nn_mul _ _ (O.nn_sq _) (hw r)
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (hw3 p)) (dot3_nn K O.toCone _)) l
  have nB5 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw3 p)) (dot3_nn K O.toCone _)) l
  have hkv : O.nn (K.mul k (K.mul ν ν)) := O.nn_mul _ _ hk (O.nn_sq ν)
  have a1 := le_mul_nn K O.toCone _ _ _ hkv (le_trans' K O.toCone _ _ _ hP n)
  have m := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ hkv nB5) (O.nn_mul _ _ (c4_nn K O) (TruncRate.nn_of_le K O _ _ nB4 hB4)) hQ
    (le_mul_nn K O.toCone _ _ _ (c4_nn K O) hB4)
  refine le_trans' K O.toCone _ _ _ a1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) m))
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a p) (a p))) l = B5
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l = B4
  generalize c4 K = c
  lib_ring

/-- Σ V|N₀ − N₁|² ≤ 2Σ V|N₀|² + 2Σ V|N₁|² -/
theorem diff_le (l : List I) (V : I → R) (hV : ∀ r, O.nn (V r)) (N0 N1 : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (V r) (dot3 K (EnstDiff.sub3 K (N0 r) (N1 r)) (EnstDiff.sub3 K (N0 r) (N1 r)))) l)
      (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (V r) (dot3 K (N0 r) (N0 r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => K.mul (V r) (dot3 K (N1 r) (N1 r))) l))) := by
  have t := lsum_le K O.toCone _ _ (fun r => le_mul_nn K O.toCone (V r) _ _ (hV r)
    (sq2 K O (N0 r) (FourierGalerkin.neg3 K (N1 r)))) l
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩)
  · rw [add_zero']; rfl
  · rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (V r) (K.add (K.mul (K.add K.one K.one) (dot3 K (N0 r) (N0 r)))
        (K.mul (K.add K.one K.one) (K.add (K.add (K.mul (K.neg (N1 r).x) (K.neg (N1 r).x)) (K.mul (K.neg (N1 r).y) (K.neg (N1 r).y)))
          (K.mul (K.neg (N1 r).z) (K.neg (N1 r).z))))) = _
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (V r) (dot3 K (N0 r) (N0 r))))
      (K.mul (K.add K.one K.one) (K.mul (V r) (K.add (K.add (K.mul (N1 r).x (N1 r).x) (K.mul (N1 r).y (N1 r).y)) (K.mul (N1 r).z (N1 r).z))))
    lib_ring

end Gen

end ForceHyps

#print axioms ForceHyps.cH_nn
#print axioms ForceHyps.g1_le
#print axioms ForceHyps.c3_nn
#print axioms ForceHyps.c4_nn
#print axioms ForceHyps.g2_le
#print axioms ForceHyps.g3_le
#print axioms ForceHyps.diff_le

#print axioms: 7 theorems, no axioms.

79.29 Discrete energy inequality

With an energy-neutral force at the old state: \(E(a')+2h\nu Z(a')\le E(a)+3h^2\sum |N|^2+2h^2\nu ^2\sum w^2|a|^2\). Summed over steps the error is \(O(h)\): the energy inequality in the limit.

ns/EnergyStep.lean

/-
  ns/EnergyStep.lean — T37 cycle 1201 (goal G773): the discrete energy inequality of the implicit-viscosity scheme,
  over any commutative ring with a nonnegative cone (h, ν, w ≥ 0). With E = Σ|a|², Z = Σ w|a|² and a force that is
  energy-neutral at the old state, Σ_r a_r·N_r = 0 (GalerkinForce.proj_neutral):
    comp_energy   a'² + 2x·a'² ≤ a² + 2h·n·a'                        ((1+x)a' = a + hn, x = hνw)
    energy_step   E(a') + 2hν·Z(a') ≤ E(a) + 3h²·Σ|N|² + 2h²ν²·Σ w²|a|²
  (dot the step with a', ⟨a, a'⟩ ≤ (|a|² + |a'|²)/2, neutrality moves the force onto a' − a, Young and TimeLip).
  Summed over n steps of size h the error is O(h): the energy inequality E(t) + 2ν∫Z ≤ E(0) holds in the limit.

  Lean 4 core plus ns/TimeLip, ns/EnstDiff (sub3), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.TimeLip

namespace EnergyStep
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep EnstDiff TimeLip

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem comp_energy (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    Le K P (K.add (K.mul a' a') (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.mul a' a')))
      (K.add (K.mul a a) (K.mul (K.add h h) (K.mul n a'))) := by
  unfold StepC at hs
  refine ⟨K.mul (K.add a (K.neg a')) (K.add a (K.neg a')), P.nn_sq _, ?_⟩
  -- 2(1+x)a'² = 2a a' + 2h n a'
  have e : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (K.mul a' a') = K.add (K.mul a a') (K.mul h (K.mul n a')) := by
    calc _ = K.mul (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') a' := by lib_ring
      _ = K.mul (K.add a (K.mul h n)) a' := by rw [hs]
      _ = _ := by lib_ring
  generalize K.mul (K.mul h ν) w = x at e ⊢
  calc K.add (K.add (K.mul a' a') (K.mul (K.add x x) (K.mul a' a'))) (K.mul (K.add a (K.neg a')) (K.add a (K.neg a')))
      = K.add (K.add (K.mul a a) (K.neg (K.add (K.mul a a') (K.mul a a'))))
          (K.add (K.mul (K.add K.one x) (K.mul a' a')) (K.mul (K.add K.one x) (K.mul a' a'))) := by lib_ring
    _ = K.add (K.add (K.mul a a) (K.neg (K.add (K.mul a a') (K.mul a a'))))
          (K.add (K.add (K.mul a a') (K.mul h (K.mul n a'))) (K.add (K.mul a a') (K.mul h (K.mul n a')))) := by rw [e]
    _ = _ := by lib_ring

theorem mode_energy (h ν w : R) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (K.add (dot3 K a' a') (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (dot3 K a' a')))
      (K.add (dot3 K a a) (K.mul (K.add h h) (dot3 K N a'))) := by
  have cx := comp_energy K P h ν w a'.x a.x N.x hs.1
  have cy := comp_energy K P h ν w a'.y a.y N.y hs.2.1
  have cz := comp_energy K P h ν w a'.z a.z N.z hs.2.2
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩)
  · show K.add (K.add (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z))
        (K.mul (K.add (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z)))) K.zero = _
    lib_ring
  · show _ = K.add (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z))
      (K.mul (K.add h h) (K.add (K.add (K.mul N.x a'.x) (K.mul N.y a'.y)) (K.mul N.z a'.z)))
    lib_ring

/-- Young per mode: 2h⟨N, d⟩ ≤ h²|N|² + |d|² -/
theorem young3 (h : R) (N d : V3 R) :
    Le K P (K.mul (K.add h h) (dot3 K N d)) (K.add (K.mul (K.mul h h) (dot3 K N N)) (dot3 K d d)) := by
  refine ⟨K.add (K.add (K.mul (K.add (K.mul h N.x) (K.neg d.x)) (K.add (K.mul h N.x) (K.neg d.x)))
      (K.mul (K.add (K.mul h N.y) (K.neg d.y)) (K.add (K.mul h N.y) (K.neg d.y))))
      (K.mul (K.add (K.mul h N.z) (K.neg d.z)) (K.add (K.mul h N.z) (K.neg d.z))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  show K.add (K.mul (K.add h h) (K.add (K.add (K.mul N.x d.x) (K.mul N.y d.y)) (K.mul N.z d.z))) _
    = K.add (K.mul (K.mul h h) (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z)))
      (K.add (K.add (K.mul d.x d.x) (K.mul d.y d.y)) (K.mul d.z d.z))
  lib_ring

theorem energy_step (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r))
    (hneut : lsum K (fun r => dot3 K (N r) (a r)) l = K.zero) :
    Le K P (K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l)))
      (K.add (lsum K (fun r => dot3 K (a r) (a r)) l)
        (K.add (K.mul (K.mul (K.add (K.add K.one K.one) K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N r) (N r)) l))
          (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l)))) := by
  -- per-mode energy, summed
  have t := lsum_le K P _ _ (fun r => mode_energy K P h ν (w r) (a' r) (a r) (N r) (hs r)) l
  -- Σ⟨N, a'⟩ = Σ⟨N, a' − a⟩ by neutrality, then Young
  have y := lsum_le K P _ _ (fun r => young3 K P h (N r) (sub3 K (a' r) (a r))) l
  have tl := time_lip K P l h ν hh hν w hw a' a N hs
  have eN : lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (a' r))) l
      = K.add (lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (sub3 K (a' r) (a r)))) l)
          (K.mul (K.add h h) (lsum K (fun r => dot3 K (N r) (a r)) l)) := by
    rw [← lsum_mul_left, ← lsum_add]
    refine lsum_ext K _ _ (fun r => ?_) l
    show K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (a' r).x) (K.mul (N r).y (a' r).y)) (K.mul (N r).z (a' r).z))
      = K.add (K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (K.add (a' r).x (K.neg (a r).x))) (K.mul (N r).y (K.add (a' r).y (K.neg (a r).y))))
          (K.mul (N r).z (K.add (a' r).z (K.neg (a r).z)))))
        (K.mul (K.add h h) (K.add (K.add (K.mul (N r).x (a r).x) (K.mul (N r).y (a r).y)) (K.mul (N r).z (a r).z)))
    lib_ring
  rw [hneut] at eN
  have L1 : lsum K (fun r => K.add (dot3 K (a' r) (a' r)) (K.mul (K.add (K.mul (K.mul h ν) (w r)) (K.mul (K.mul h ν) (w r))) (dot3 K (a' r) (a' r)))) l
      = K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l)) := by
    rw [← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have R1 : lsum K (fun r => K.add (dot3 K (a r) (a r)) (K.mul (K.add h h) (dot3 K (N r) (a' r)))) l
      = K.add (lsum K (fun r => dot3 K (a r) (a r)) l) (lsum K (fun r => K.mul (K.add h h) (dot3 K (N r) (a' r))) l) := by
    rw [lsum_add]
  rw [L1, R1, eN] at t
  have Y1 : lsum K (fun r => K.add (K.mul (K.mul h h) (dot3 K (N r) (N r))) (dot3 K (sub3 K (a' r) (a r)) (sub3 K (a' r) (a r)))) l
      = K.add (K.mul (K.mul h h) (lsum K (fun r => dot3 K (N r) (N r)) l)) (lsum K (fun r => dot3 K (sub3 K (a' r) (a r)) (sub3 K (a' r) (a r))) l) := by
    rw [← lsum_mul_left, lsum_add]
  rw [Y1] at y
  have two : P.nn (K.add K.one K.one) := P.nn_add _ _ (RatModel.nn_one' K P) (RatModel.nn_one' K P)
  have c1 := le_trans' K P _ _ _ y (le_add' K P _ _ _ _ (le_refl' K P _) tl)
  have c2 := le_add' K P _ _ _ _ (le_refl' K P (lsum K (fun r => dot3 K (a r) (a r)) l))
    (le_add' K P _ _ _ _ c1 (le_refl' K P (K.mul (K.add h h) K.zero)))
  refine le_trans' K P _ _ _ t (le_trans' K P _ _ _ c2 ⟨K.zero, P.nn_zero, ?_⟩)
  generalize lsum K (fun r => dot3 K (a r) (a r)) l = E
  generalize lsum K (fun r => dot3 K (N r) (N r)) l = G
  generalize lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l = A
  lib_ring

end Gen

end EnergyStep
#print axioms EnergyStep.comp_energy
#print axioms EnergyStep.mode_energy
#print axioms EnergyStep.young3
#print axioms EnergyStep.energy_step

#print axioms: 4 theorems, no axioms.

79.30 Accumulated energy inequality

If \(E_{k+1}+cZ_{k+1}\le E_k+s_k\) and \(s_k\le S\), then \(E_n+c\sum _{k<n}Z_{k+1}\le E_0+nS\). With the discrete energy step: \(E(nh)+2\nu h\sum Z\le E(0)+(nh)\,h\,(3G+2\nu ^2A)\).

ns/EnergySum.lean

/-
  ns/EnergySum.lean — T37 cycle 1203 (goal G774): the accumulated discrete energy inequality, over any commutative ring
  with a nonnegative cone. If every step satisfies E_{k+1} + c·Z_{k+1} ≤ E_k + s_k with s_k ≤ S, then
    energy_sum   E_n + c·Σ_{k<n} Z_{k+1} ≤ E_0 + n·S.
  With EnergyStep (c = 2hν, s_k = 3h²Σ|N_k|² + 2h²ν²Σw²|a_k|², S = h²(3G + 2ν²A) from ForceNorms/EnstH2):
  E(nh) + 2ν·hΣZ ≤ E(0) + (nh)·h·(3G + 2ν²A) — the energy inequality with an O(h) error on [0, nh].

  Lean 4 core plus NSChain (sumN), lib/RingTac.
-/
import ns.TimeLipN

namespace EnergySum
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem energy_sum (E Z s : Nat → R) (c S : R) (hstep : ∀ k, Le K P (K.add (E (k + 1)) (K.mul c (Z (k + 1)))) (K.add (E k) (s k)))
    (hs : ∀ k, Le K P (s k) S) : ∀ n,
    Le K P (K.add (E n) (K.mul c (sumN K (fun k => Z (k + 1)) n))) (K.add (E 0) (K.mul (castN K n) S))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.add (E 0) (K.mul c K.zero)) K.zero = K.add (E 0) (K.mul K.zero S); lib_ring⟩
  | n + 1 => by
    have ih := energy_sum E Z s c S hstep hs n
    have st := hstep n
    -- E_{n+1} + c(ΣZ_n + Z_{n+1}) ≤ E_n + s_n + cΣZ_n ≤ E_0 + nS + s_n ≤ E_0 + (n+1)S
    have a := le_add' K P _ _ _ _ st (le_refl' K P (K.mul c (sumN K (fun k => Z (k + 1)) n)))
    have b := le_add' K P _ _ _ _ ih (hs n)
    refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩ (le_trans' K P _ _ _ a (le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, ?_⟩
      (le_trans' K P _ _ _ b ⟨K.zero, P.nn_zero, ?_⟩)))
    · show K.add (K.add (E (n + 1)) (K.mul c (K.add (sumN K (fun k => Z (k + 1)) n) (Z (n + 1))))) K.zero = _
      lib_ring
    · lib_ring
    · show K.add (K.add (K.add (E 0) (K.mul (castN K n) S)) S) K.zero = K.add (E 0) (K.mul (K.add K.one (castN K n)) S)
      lib_ring

end Gen

end EnergySum

#print axioms EnergySum.energy_sum

#print axioms: 1 theorem, no axioms.

79.31 Energy induction under a CFL condition

If \(E_k\le E^*\) implies \(E_{k+1}+2cZ_{k+1}\le E_k+\beta Z_k\) with \(\beta \le c\), and \(E_0+\beta Z_0\le E^*\), then \(E_n+c\sum _{k<n}Z_{k+1}+\beta Z_n\le E_0+\beta Z_0\). Global energy bound for arbitrary data, once the force error is absorbed.

ns/CFLEnergy.lean

/-
  ns/CFLEnergy.lean — T37 cycle 1215 (goal G775a): the abstract energy induction behind a global, arbitrary-data
  energy bound for the implicit-viscosity scheme under a CFL condition, over an ordered ring. Suppose that whenever
  E_k ≤ E*,
      E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k          (EnergyStep with the force error absorbed by the CFL bound)
  with 0 ≤ β ≤ c and E_0 + β·Z_0 ≤ E*. Then for every n
    cfl_energy   E_n + c·Σ_{k<n} Z_{k+1} + β·Z_n ≤ E_0 + β·Z_0,
  so E_n ≤ E* for all n and the time-integrated enstrophy is bounded — the a priori bounds of Leray's weak solutions,
  for the truncated scheme and arbitrary data (no smallness), once h is small for the truncation.

  Lean 4 core plus NSChain (sumN), lib/RingTac.
-/
import ns.TimeLipN
import ns.TruncRate

namespace CFLEnergy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem cfl_energy (E Z : Nat → R) (c β Es : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ : ∀ k, O.nn (Z k))
    (hstep : ∀ k, Le K O.toCone (E k) Es →
      Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k))))
    (h0 : Le K O.toCone (K.add (E 0) (K.mul β (Z 0))) Es) : ∀ n,
    Le K O.toCone (K.add (K.add (E n) (K.mul c (sumN K (fun k => Z (k + 1)) n))) (K.mul β (Z n)))
      (K.add (E 0) (K.mul β (Z 0)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.add (E 0) (K.mul c K.zero)) (K.mul β (Z 0))) K.zero = _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_energy E Z c β Es hβ hβc hZ hstep h0 n
    -- E_n ≤ E* from the induction hypothesis
    have hEn : Le K O.toCone (E n) Es := by
      refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul c (sumN K (fun k => Z (k + 1)) n)) (K.mul β (Z n)), ?_, ?_⟩ (le_trans' K O.toCone _ _ _ ih h0)
      · have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
        have hs : O.nn (sumN K (fun k => Z (k + 1)) n) := NSChain.sumN_nn K O.toCone (fun k => Z (k + 1)) (fun k => hZ (k + 1)) n
        exact O.nn_add _ _ (O.nn_mul _ _ hc hs) (O.nn_mul _ _ hβ (hZ n))
      · lib_ring
    have st := hstep n hEn
    -- (β − c)·Z_{n+1} ≤ 0
    cases hβc with
    | intro d hd =>
      -- c = β + d
      have ec : c = K.add β d := hd.2.symm
      refine le_trans' K O.toCone _ _ _ ⟨K.mul d (Z (n + 1)), O.nn_mul _ _ hd.1 (hZ (n + 1)), ?_⟩
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ st (le_refl' K O.toCone (K.mul c (sumN K (fun k => Z (k + 1)) n)))) ?_)
      · show K.add (K.add (K.add (E (n + 1)) (K.mul c (K.add (sumN K (fun k => Z (k + 1)) n) (Z (n + 1))))) (K.mul β (Z (n + 1)))) (K.mul d (Z (n + 1)))
          = K.add (K.add (E (n + 1)) (K.mul (K.add c c) (Z (n + 1)))) (K.mul c (sumN K (fun k => Z (k + 1)) n))
        rw [ec]; lib_ring
      · refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ ih
        lib_ring

end Gen

end CFLEnergy

#print axioms CFLEnergy.cfl_energy

#print axioms: 1 theorem, no axioms.

79.32 CFL energy step for arbitrary data

For \(|k|_\infty \le M+1\), \(c_M=3(M+1)^2\) and \(E(a)\le E^*\): \(E(a')+2h\nu Z(a')\le E(a)+h^2(3\iota ^2\cdot 135\,c_M^2E^*+2\nu ^2c_M)\,Z(a)\). With the CFL condition \(h(3\iota ^2\cdot 135\,c_M^2E^*+2\nu ^2c_M)\le \nu \) this feeds the energy induction.

ns/CFLStep.lean

/-
  ns/CFLStep.lean — T37 cycle 1217 (goal G775b): the CFL energy step of the implicit-viscosity Galerkin scheme for a
  fixed truncation |k|_∞ ≤ M + 1 and arbitrary data, over an ordered field. With c_M = 3(M+1)²:
    wt_le      w_r ≤ c_M on the modes;  A ≤ c_M·Z,  Z ≤ c_M·E
    cfl_step   if E(a) ≤ E*, the force is energy-neutral at a and Σ|N|² ≤ Σ|Nl(a,a)|², then
               E(a') + 2hν·Z(a') ≤ E(a) + β·Z(a),   β = h²·(3ι²·135·c_M²·E* + 2ν²·c_M)
  (EnergyStep, ForceUniform with NormCompare.nrm4_le). With CFLEnergy.cfl_energy (c = hν, condition β ≤ hν, i.e.
  h·(3ι²·135·c_M²E* + 2ν²c_M) ≤ ν): a global energy bound for every initial datum.

  Lean 4 core plus ns/EnergyStep, ns/ForceUniform, ns/WeightH2, ns/NormCompare, ns/EnstGalerkin, ns/TruncRate.
-/
import ns.EnergyStep
import ns.WeightH2
import ns.EnstGalerkin
import ns.TruncRate

namespace CFLStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 EnergyStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def cM (M : Nat) : R := K.add (K.add (K.mul (castN K (M + 1)) (castN K (M + 1))) (K.mul (castN K (M + 1)) (castN K (M + 1))))
  (K.mul (castN K (M + 1)) (castN K (M + 1)))

theorem cM_nn (M : Nat) : O.nn (cM K M) :=
  O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)

theorem lsum_le_mem (f g : I → R) : ∀ l : List I, (∀ r, r ∈ l → Le K O.toCone (f r) (g r)) → Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_mem f g l (fun r hr => h r (List.Mem.tail _ hr)))

theorem wt_le (kz : I → Mode) (M : Nat) (r : I) (h : nrm (kz r) ≤ M + 1) : Le K O.toCone (wt K kz r) (cM K M) := by
  have d := dot_le_3nrm K O (kz r)
  have c := RatModel.cast_mono K O.toCone _ _ h
  have s := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c
  exact le_trans' K O.toCone _ _ _ d (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s s) s)

theorem A_le_Z (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (O.nn_mul _ _ hwr (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

theorem Z_le_E (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => dot3 K (a r) (a r)) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  exact ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (dot3_nn K O.toCone (a r))

include mul_inv h10 in
theorem cfl_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (a' a N : I → V3 R)
    (hs : ∀ r, Step K h ν (wt K kz r) (a' r) (a r) (N r))
    (hneut : lsum K (fun r => dot3 K (N r) (a r)) l = K.zero)
    (hP : Le K O.toCone (lsum K (fun r => dot3 K (N r) (N r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)) l))
    (hE : Le K O.toCone (lsum K (fun r => dot3 K (a r) (a r)) l) Es) :
    Le K O.toCone (K.add (lsum K (fun r => dot3 K (a' r) (a' r)) l) (K.mul (K.add (K.mul h ν) (K.mul h ν)) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a' r) (a' r))) l)))
      (K.add (lsum K (fun r => dot3 K (a r) (a r)) l)
        (K.mul (K.mul (K.mul h h) (K.add (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul ι ι) (K.mul (castN K 135) (K.mul (K.mul (cM K M) (cM K M)) Es))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) (cM K M))))) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have es := energy_step K O.toCone l h ν hh hν (wt K kz) hw a' a N hs hneut
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) a a
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (a p))) l
  have hA := A_le_Z K O l kz M hM a
  have hZ := Z_le_E K O l kz M hM a
  have nZ : O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have ncM := cM_nn K O M
  -- Σ|N|² ≤ ι²·A·(135·Z) ≤ ι²·(c_M Z)·(135·c_M E*)
  have hZE : Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) (K.mul (cM K M) Es) :=
    le_trans' K O.toCone _ _ _ hZ (le_mul_nn K O.toCone _ _ _ ncM hE)
  have n1 : Le K O.toCone (lsum K (fun r => dot3 K (N r) (N r)) l)
      (K.mul (K.mul ι ι) (K.mul (K.mul (cM K M) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l)) (K.mul (castN K 135) (K.mul (cM K M) Es)))) := by
    refine le_trans' K O.toCone _ _ _ hP (le_trans' K O.toCone _ _ _ fu (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) ?_))
    exact TruncRate.mm K O _ _ _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ ncM (TruncRate.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l) hE)))
      (le_trans' K O.toCone _ _ _ hH hA) (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone 135) hZE)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have err := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ three (O.nn_sq h)) n1)
    (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq h) (O.nn_sq ν))) hA)
  refine le_trans' K O.toCone _ _ _ es (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) err) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize lsum K (fun r => dot3 K (a r) (a r)) l = E
  generalize lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l = Z
  generalize cM K M = c
  generalize castN K 135 = d
  lib_ring

end Gen

end CFLStep

#print axioms CFLStep.cM_nn
#print axioms CFLStep.lsum_le_mem
#print axioms CFLStep.wt_le
#print axioms CFLStep.A_le_Z
#print axioms CFLStep.Z_le_E
#print axioms CFLStep.cfl_step

#print axioms: 6 theorems, no axioms.

79.33 Global energy bound for arbitrary data

Under \(\beta \le h\nu \) and \(E_0+\beta Z_0\le E^*\): \(E_n+h\nu \sum _{t<n}Z_{t+1}+\beta Z_n\le E_0+\beta Z_0\) for every \(n\) and every initial datum. The a priori bounds of Leray’s weak solutions for the truncated implicit scheme.

ns/CFLGlobal.lean

/-
  ns/CFLGlobal.lean — T37 cycle 1219 (goal G775 — done): global discrete energy bound for arbitrary data, fixed
  truncation |k|_∞ ≤ M + 1, under a CFL condition, over an ordered field. With E_t = Σ|a_t|², Z_t = Σ w|a_t|²,
  β = h²(3ι²·135·c_M²·E* + 2ν²·c_M), c_M = 3(M+1)²: if the force is energy-neutral and Leray-projected at every step,
  β ≤ hν, and E_0 + β·Z_0 ≤ E*, then for every n
    cfl_global   E_n + hν·Σ_{t<n} Z_{t+1} + β·Z_n ≤ E_0 + β·Z_0
  — the a priori bounds of Leray's weak solutions (L^∞L² and L²H¹) for the truncated implicit scheme, every datum.

  Lean 4 core plus ns/CFLStep, ns/CFLEnergy.
-/
import ns.CFLStep
import ns.CFLEnergy

namespace CFLGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 EnergyStep CFLStep CFLEnergy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def beta (M : Nat) (ι h ν Es : R) : R :=
  K.mul (K.mul h h) (K.add (K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul ι ι) (K.mul (castN K 135) (K.mul (K.mul (cM K M) (cM K M)) Es))))
    (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) (cM K M))))

include mul_inv h10 in
theorem cfl_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone (K.add (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) n)))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l)))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  exact cfl_energy K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) Es nβ hcfl
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
    (fun t hE => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) hE)
    h0 n

end Gen

end CFLGlobal

#print axioms CFLGlobal.cfl_global

#print axioms: 1 theorem, no axioms.

79.34 Weighted norms on a fixed truncation

\(\sum _rw_r^j|a_r|^2\le c_M^j\sum _r|a_r|^2\) for every \(j\), \(c_M=3(M+1)^2\). With the global energy bound, all norms of the step-size estimate are bounded for every datum.

ns/TruncNorms.lean

/-
  ns/TruncNorms.lean — T37 cycle 1223 (goal G776, piece a): on a fixed truncation |k|_∞ ≤ M + 1 every weighted energy is
  bounded by the plain energy, over an ordered field (c_M = 3(M+1)², w_r ≤ c_M):
    wpow_le   Σ_r w_r^j·|a_r|² ≤ c_M^j·Σ_r |a_r|²   for every j
  So under the CFL energy bound E ≤ E* (CFLGlobal) all the norms Z, A, B, B₄, B₅ entering the step-size Cauchy
  estimate (StepCauchy, LocalDefect, FDk, SmallAlpha) are bounded by c_M^j·E*: for a fixed truncation the implicit
  scheme converges as h → 0 for every initial datum (finite-dimensional global existence).

  Lean 4 core plus ns/CFLStep (wt_le, lsum_le_mem), ns/EnstGronwall (pwr).
-/
import ns.CFLStep
import ns.EnstGronwall

namespace TruncNorms
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstDiff WeightH2 CFLStep EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem pwr_le (x y : R) (hx : O.nn x) (hxy : Le K O.toCone x y) : ∀ j, Le K O.toCone (pwr K x j) (pwr K y j)
  | 0 => le_refl' K O.toCone _
  | j + 1 => by
    have ih := pwr_le x y hx hxy j
    have hpx : O.nn (pwr K x j) := EnstGronwall.pwr_nn K O x hx j
    have hy : O.nn y := TruncRate.nn_of_le K O _ _ hx hxy
    exact TruncRate.mm K O _ _ _ _ hx (TruncRate.nn_of_le K O _ _ hpx ih) hxy ih

theorem wpow_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) (j : Nat) :
    Le K O.toCone (lsum K (fun r => K.mul (pwr K (wt K kz r) j) (dot3 K (a r) (a r))) l)
      (K.mul (pwr K (cM K M) j) (lsum K (fun r => dot3 K (a r) (a r)) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hw : O.nn (wt K kz r) := dot3_nn K O.toCone _
  exact ForceSob.le_mul_right' K O.toCone (pwr_le K O _ _ hw (wt_le K O kz M r (hM r hr)) j) (dot3_nn K O.toCone (a r))

end Gen

end TruncNorms

#print axioms TruncNorms.pwr_le
#print axioms TruncNorms.wpow_le

#print axioms: 2 theorems, no axioms.

79.35 The implicit step is well defined

Two solutions of \((1+h\nu w)a'=a+hn\) coincide (\(1+h\nu w\ge 1\) is invertible). A run is determined by its initial datum.

ns/StepUnique.lean

/-
  ns/StepUnique.lean — T37 cycle 1229: the implicit-viscosity step is well defined, over an ordered field.
    step_unique   StepC h ν w a' a n, StepC h ν w b' a n, h, ν, w ≥ 0  ⇒  a' = b'
  (1 + hνw ≥ 1 is invertible), so a scheme run is determined by its data and its forces; with the force a function of
  the state (Nl), the whole trajectory is determined by the initial datum.

  Lean 4 core plus ns/EnstStep (StepC), ns/BaselQ (cancel), ns/DefectSplit (one_le pattern).
-/
import ns.EnstStep
import ns.BaselQ
import ns.TruncRate

namespace StepUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem step_unique (h ν w a' b' a n : R) (hh : O.nn h) (hν : O.nn ν) (hw : O.nn w) (h10 : K.one ≠ K.zero)
    (ha : StepC K h ν w a' a n) (hb : StepC K h ν w b' a n) : a' = b' := by
  unfold StepC at ha hb
  have hx : O.nn (K.mul (K.mul h ν) w) := O.nn_mul _ _ (O.nn_mul _ _ hh hν) hw
  have hne : K.add K.one (K.mul (K.mul h ν) w) ≠ K.zero := by
    intro e
    have h1 : Le K O.toCone K.one (K.add K.one (K.mul (K.mul h ν) w)) := ⟨K.mul (K.mul h ν) w, hx, rfl⟩
    rw [e] at h1
    exact h10 (O.antisym _ _ h1 ⟨K.one, RatModel.nn_one' K O.toCone, by lib_ring⟩)
  exact BaselQ.cancel K O _ _ _ hne (ha.trans hb.symm)

end Gen

end StepUnique

#print axioms StepUnique.step_unique

#print axioms: 1 theorem, no axioms. ier–Stokes, the map of routes to global smoothness.

79.36 The statement and the routes

The fluid of Sections 93.12–93.19 obeys, in the macroscopic limit, the incompressible Navier–Stokes equations

\[ \partial _t u+(u\cdot \nabla )u=-\nabla p+\nu \Delta u,\qquad \nabla \cdot u=0 . \]
The target is global smoothness in three dimensions: smooth divergence-free data of finite energy on \(\mathbb R^3\), or smooth data on \(\mathbb T^3\), give a smooth solution for all \(t>0\). Leray’s weak solutions exist for all time and obey the energy inequality [1798]; smooth solutions exist for a time controlled by a critical norm and continue while \(\int _0^t\|\omega \|_\infty \) stays finite [1770]. The file NS_MAP.md is the dependency graph from these facts to the target: every node is marked proved (Lean), cited (paper and theorem), measured, or remaining with its exact statement. This section gives the five routes, the part of each that is proved here, and the measurements.

The scaling \(u_\lambda (x,t)=\lambda u(\lambda x,\lambda ^2t)\) maps solutions to solutions. The energy scales as \(\|u_\lambda \|_{L^2}^2=\lambda ^{-1}\|u\|_{L^2}^2\), while \(\|u\|_{L^3}\), \(\|u\|_{\dot H^{1/2}}\) are invariant. Every a priori bound known for general data controls a quantity at the level of the energy, half a derivative below the invariant level; each route below is a way to cross that half derivative.

79.37 Energy neutrality and discrete models (Lean, general)

The energy identity rests on one algebraic fact: the nonlinearity does no work, \(\langle (u\cdot \nabla )v,v\rangle =0\) for \(\nabla \cdot u=0\). In a Galerkin truncation with modes \(I\) and interaction coefficients \(C_{ijk}\), this is the antisymmetry \(C_{jik}=-C_{ijk}\), \(C_{iik}=0\), and galerkin_neutral proves

\[ \sum _{i\in I}a_i\sum _{j\in I}a_j\sum _{k\in I}C_{ijk}a_k=0 \]
in every commutative ring and for every finite mode set (the core step is double_antisym: an antisymmetric array with zero diagonal sums to zero). For the dyadic model of Desnyansky–Novikov and Katz–Pavlović [1782, 1793],
\[ \dot a_n=-\nu \lambda ^{2\alpha n}a_n+c_{n-1}a_{n-1}^2-c_na_na_{n+1}, \]
dyadic_energy_neutral proves \(\sum _n a_n(c_{n-1}a_{n-1}^2-c_na_na_{n+1})=0\) for every \(N\) and every coefficient sequence, by telescoping the flux \(g_n=c_na_n^2a_{n+1}\) (telescope).

Time stepping keeps the identity exactly. For one Crank–Nicolson step \(a'-a=h(-\nu k\odot s+N)\) with \(s=a'+a\) and an energy-neutral \(N\) (\(\sum _n s_nN_n=0\)), cn_energy proves

\[ E(a')+h\nu \sum _n k_ns_n^2=E(a),\qquad E(a)=\sum _n a_n^2 , \]
and with an order (Cone: nonnegative elements closed under \(+\), \(\cdot \), containing squares) cn_global proves that every mode stays below the initial energy for every time step and every truncation: \(a_t(m)^2\le E(a_0)\). This is global boundedness of every discrete viscous model, uniform in \(t\) and \(N\). It does not bound \(\sum _nk_na_n^2\), the quantity whose bound uniform in \(N\) is regularity of the limit.

The dyadic model shows that the energy bound alone does not decide regularity. With \(\lambda =2\), \(\nu =1\), \(a_1(0)=20\), the norm \((\sum _n4^na_n^2)^{1/2}\) peaks at \(7.3\times 10^2\) for dissipation degree \(\alpha =1/2\) and at \(1.2\)–\(1.3\times 10^4\) for \(\alpha =0.4\), with 16 and with 20 shells alike, and the energy then decays. For \(\alpha \le 0.3\) the peak grows with the number of shells (\(1.1\times 10^5\to 7.4\times 10^5\) at \(\alpha =0.3\), \(3.8\times 10^5\to 3.8\times 10^6\) at \(\alpha =0.2\)), a share of up to \(13\%\) of the energy sits in the last four shells, and the time step collapses (three of these four runs stop at the limit of \(4\times 10^5\) steps; NUMERIC_NS.txt): the cascade outruns the dissipation, as in Cheskidov’s blowup [1782]. Both regimes obey the same proved energy identity.

79.38 Route R1: critical norms

Escauriaza, Seregin and Šverák proved that a Leray–Hopf solution bounded in \(L^\infty _tL^3_x\) is smooth [1786]; so a singularity forces \(\|u(t)\|_{L^3}\to \infty \). Tao made this quantitative: an \(L^\infty _tL^3_x\) bound \(A\) controls all higher norms by \(\exp \exp \exp (A^{O(1)})\), and a blowup has \(\|u(t)\|_{L^3}\gtrsim (\log \log \log \frac 1{T^*-t})^c\) [1817]; Palasek and Barker–Prange sharpened these bounds and localized them [1775, 1808]. The missing step is the a priori bound

\[ \sup _{0\le t<T^*}\|u(t)\|_{L^3(\mathbb R^3)}\le F(\|u_0\|_{L^2},\|u_0\|_{L^3},\nu ), \]
stated as r1_critical_bound in OPEN_LEMMAS_NS.lean. In every simulated flow of this section, \(\|u(t)\|_{L^3}\) never exceeds its initial value.

79.39 Route R2: De Giorgi

Caffarelli, Kohn and Nirenberg proved that the singular set of a suitable weak solution has zero one-dimensional parabolic Hausdorff measure [1780]; Vasseur reproved it with De Giorgi’s method [1819] and obtained \(\nabla ^nu\in L^{4/(n+1),\infty }_{\rm loc}\) [1820]. The iteration at the heart of the method is proved here in the form used for the level-set energies: if \(Q\,U_{k+1}\le C^kU_k^2\), \(CU_0\le Q\) and \(Q>0\), then \(C^kU_k\le U_0\) for every \(k\) (degiorgi_iteration), so \(U_k\to 0\). For Navier–Stokes the exponent is \(5/3\) in place of \(2\), and the iteration starts only after an \(\varepsilon \)-smallness at a critical scale; the exponents \(4/(n+1)\) are exactly those allowed by the energy class. The missing step is an \(\varepsilon \)-regularity whose smallness quantity is bounded by the energy alone (r2_supercritical_eps).

79.40 Route R3: the direction of vorticity

Constantin and Fefferman proved that if the direction \(\xi =\omega /|\omega |\) is Lipschitz in space where \(|\omega |\) is large, the solution is smooth [1778]; Beirão da Veiga and Berselli lowered Lipschitz to Hölder-\(\frac 12\) [1776]. The simulations measure \(\max |\nabla \xi |\) on the set \(\{|\omega |>\|\omega \|_\infty /2\}\) and the dimensionless product \(cf^*=\max |\nabla \xi |\,(\nu /\|\omega \|_\infty )^{1/2}\), which compares the bending length of the vortex lines with the viscous length. In every run \(cf^*\) stays of order one at the vorticity maximum (values in NUMERIC_NS.txt). The missing step is the a priori bound of the Hölder-\(\frac 12\) quotient of \(\xi \) on the high-vorticity set (r3_direction_holder).

79.41 Route R4: the search for blowup

Exactly self-similar blowup is excluded: a profile in \(L^3\) is zero [1807], and so is one with local energy bounds [1818]. For the Euler equations a blowup from smooth data in a cylinder with boundary has a computer-assisted proof [1781], unstable self-similar profiles have been computed to high accuracy [1822], and \(C^{1,\alpha }\) data blow up on \(\mathbb R^3\) [1787]; for Leray solutions of forced Navier–Stokes uniqueness fails [1765]. Hou computed potentially singular Navier–Stokes behaviour with a two-scale travelling structure [1791].

A nearly self-similar singularity with \(\|\omega \|_\infty \sim (T-t)^{-1}\) and length scale \(\ell \sim (T-t)^{c_l}\) has viscous-to-inertial ratio \(\nu /(\ell ^2\|\omega \|_\infty )\sim \nu (T-t)^{1-2c_l}\). Viscosity becomes negligible at the singularity exactly when \(c_l<\frac 12\), and finite local energy requires \(c_l\ge \frac 25\). A transfer of the Euler method of [1781] to Navier–Stokes therefore needs a profile with \(c_l\in [\frac 25,\frac 12)\) in the interior (Navier–Stokes has the no-slip condition at a wall); at \(c_l=\frac 12\) the viscous term stays at leading order and the exact profile is the one excluded by [1807, 1818].

Tao constructed an averaged Navier–Stokes equation, with the same energy identity and the same harmonic-analysis bounds, whose solutions blow up [1816]. A proof of global smoothness therefore has to use structure that the averaged equation lacks: the vorticity transport, the geometry of route R3, or the pressure in route R2. The energy neutrality proved above is shared by the blowing-up dyadic model.

The search here is the spectral solver numeric_ns.py on the periodic box (pseudo-spectral, \(2/3\) dealiasing, fourth-order Runge–Kutta with the exact viscous factor, CFL step, double precision, cupy on Kamiak H100 GPUs), over Taylor–Green data [1815] with \(\nu ^{-1}\) up to \(3200\) and over a family of anti-parallel vortex tubes [1795] with perturbation amplitude \(0\)–\(0.45\) and \(\nu \) down to \(2\times 10^{-4}\). Resolution is checked by \(k_{\max }\eta \) and by the energy fraction in the outer shell.

79.42 Route R5: kinetic and discrete models

The Boltzmann equation converges to Leray solutions of Navier–Stokes in the incompressible scaling [1788]. The lattice gas of Sections 93.1–93.19 has exact conservation, the exact kinetic hierarchy and the H theorem in Lean. The discrete viscous models above are globally bounded for every truncation (cn_global). What the kinetic side does not give is a bound stronger than the energy; the missing step is convergence in a norm that controls \(\|\omega \|_\infty \) on every interval where the limit is smooth (r5_kinetic_regularity).

79.43 Claimed resolutions

In 2026 two manuscripts claim finite-time blowup: one for smooth forcing (not the unforced problem), and one, announced on 20 November 2026, for unforced smooth data on \(\mathbb R^3\) and \(\mathbb T^3\) with a computer certificate. Neither is refereed, and no statement of this section depends on them. A counterexample must have \(\|u(t)\|_{L^3}\to \infty \) at least at Tao’s rate, \(\int \|\omega \|_\infty =\infty \), a vorticity direction that loses Hölder-\(\frac 12\) continuity on the high-vorticity set, a profile that is not exactly self-similar, and a singular set of zero parabolic one-dimensional measure; NS_MAP.md lists these checks.

79.44 Two-dimensional truncations: global bound along every run

Let a two-dimensional Fourier–Galerkin truncation have modes \(n=0,\dots ,N-1\) with wave vectors \(k_n\in R^2\), a triad indicator \(\delta \) that is symmetric in its three arguments and equal to \(1\) only on triads \(k_p+k_q+k_r=0\), and coefficients \(c(p,q,r)=J(k_q,k_r)\,(|k_r|^2-|k_q|^2)\). For every Crank–Nicolson run

\[ a^{t+1}_n-a^t_n=h\bigl (-\nu |k_n|^2s^t_n+N_n(s^t)\bigr ),\qquad s^t=a^{t+1}+a^t,\qquad N_n(s)=\sum _{q,r}\delta (n,q,r)\,c(n,q,r)\,s_qs_r , \]
in every commutative ring without \(3\)-torsion,
\[ Z_T+\sum _{t<T}h\nu \sum _n|k_n|^4\,(s^t_n)^2=Z_0,\qquad Z_t=\sum _n|k_n|^2(a^t_n)^2 . \]
In an ordered ring with \(h,\nu \ge 0\) this gives \(Z_T\le Z_0\) and a total \(H^2\)-dissipation at most \(Z_0\), for every \(N\) and every \(T\).

The proof has four steps.

\begin{align*} &\textstyle \sum _{p,q,r}g(q,r,p)=\sum _{p,q,r}g(p,q,r) &&\text {(two exchanges of finite sums)}\\ &f(p,q,r)+f(q,r,p)+f(r,p,q)=\delta \,\psi _p\psi _q\psi _r\,|k_p|^2\bigl (c+c'+c''\bigr )=0 &&\text {(triad identity)}\\ &3\,\textstyle \sum _{p,q,r}f(p,q,r)=0\ \Rightarrow \ \sum _{p,q,r}f(p,q,r)=0 &&\text {(no $3$-torsion)}\\ &Z_{t+1}+h\nu D_t=Z_t\ \Rightarrow \ Z_T+\textstyle \sum _{t<T}h\nu D_t=Z_0 &&\text {(telescoping)} \end{align*}

The triad identity is Enstrophy2D.triad_enstrophy; the sum over all triads is Galerkin2D.enstrophy_zero, the step Galerkin2DCN.cn2d_enstrophy, and the run Global2DRun.run_enstrophy, run_bound.

Listing 167: ns/Galerkin2D.lean

ns/Galerkin2D.lean

/-
  ns/Galerkin2D.lean — T37 cycle 41: every 2D Fourier–Galerkin truncation conserves energy AND
  enstrophy under the nonlinearity (sum over all triads), in every commutative ring without 3-torsion.

  Lean 4 core plus ns/Enstrophy2D (J, n2, Triad, triad_energy, triad_enstrophy) and NSChain (lsum).

  Modes p ∈ l with wave vectors k_p ∈ R², stream-function coefficients ψ_p, a triad indicator δ ∈ {0,1}
  symmetric in its three arguments and 1 only on triads k_p + k_q + k_r = 0. With
  c(p,q,r) = J(k_q, k_r)(|k_r|² − |k_q|²), the energy and enstrophy rates of the nonlinearity are
      E(ψ) = Σ_{p,q,r} δ c(p,q,r) ψ_p ψ_q ψ_r ,   Z(ψ) = Σ_{p,q,r} δ |k_p|² c(p,q,r) ψ_p ψ_q ψ_r .
    triple_cycle     Σ_p Σ_q Σ_r g(q,r,p) = Σ_p Σ_q Σ_r g(p,q,r)
    energy_zero      E(ψ) = 0
    enstrophy_zero   Z(ψ) = 0
  (both after 3·(·) = 0 ⇒ (·) = 0). With Enstrophy2D.cn_weighted this gives, for every 2D truncation and
  every Crank–Nicolson step, non-increasing energy and enstrophy: the H¹ bound uniform in N and t.
-/
import ns.Enstrophy2D
import ns.FourierGalerkin

namespace Galerkin2D
open DetChain NSChain Enstrophy2D

section Ring
variable {R : Type} (K : CRing R) {I : Type}

theorem triple_cycle (g : I → I → I → R) (l : List I) :
    lsum K (fun p => lsum K (fun q => lsum K (fun r => g q r p) l) l) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => g p q r) l) l) l := by
  rw [FourierGalerkin.lsum_swap K (fun p q => lsum K (fun r => g q r p) l) l l]
  apply lsum_ext
  intro q
  exact FourierGalerkin.lsum_swap K (fun p r => g q r p) l l

def c (k : I → V2 R) (_p q r : I) : R := K.mul (J K (k q) (k r)) (sub K (n2 K (k r)) (n2 K (k q)))

def f (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (p q r : I) : R :=
  K.mul (K.mul (δ p q r) (c K k p q r)) (K.mul (ψ p) (K.mul (ψ q) (ψ r)))

def fZ (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (p q r : I) : R :=
  K.mul (K.mul (δ p q r) (K.mul (n2 K (k p)) (c K k p q r))) (K.mul (ψ p) (K.mul (ψ q) (ψ r)))

def S3 (F : I → I → I → R) (l : List I) : R :=
  lsum K (fun p => lsum K (fun q => lsum K (fun r => F p q r) l) l) l

theorem sum3_add (F G H : I → I → I → R) (l : List I) :
    K.add (K.add (S3 K F l) (S3 K G l)) (S3 K H l)
      = S3 K (fun p q r => K.add (K.add (F p q r) (G p q r)) (H p q r)) l := by
  unfold S3
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro q
  rw [← lsum_add, ← lsum_add]

theorem psi_cycle (ψ : I → R) (p q r : I) :
    K.mul (ψ q) (K.mul (ψ r) (ψ p)) = K.mul (ψ p) (K.mul (ψ q) (ψ r)) := by
  rw [K.mul_comm (ψ r) (ψ p), ← K.mul_assoc, K.mul_comm (ψ q) (ψ p), K.mul_assoc]

/-- The three cyclic copies of the energy integrand add to zero, triad by triad. -/
theorem cyc_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r))) (p q r : I) :
    K.add (K.add (f K k δ ψ p q r) (f K k δ ψ q r p)) (f K k δ ψ r p q) = K.zero := by
  unfold f
  rw [hsym1 p q r, hsym2 p q r, psi_cycle K ψ q r p, psi_cycle K ψ p q r, ← right_distrib K, ← right_distrib K,
    ← K.distrib, ← K.distrib]
  cases htri p q r with
  | inl h0 => rw [h0, zero_mul', zero_mul']
  | inr h1 =>
    unfold c
    rw [triad_energy K (k p) (k q) (k r) h1.2, mul_zero K, zero_mul']

theorem cycZ_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r))) (p q r : I) :
    K.add (K.add (fZ K k δ ψ p q r) (fZ K k δ ψ q r p)) (fZ K k δ ψ r p q) = K.zero := by
  unfold fZ
  rw [hsym1 p q r, hsym2 p q r, psi_cycle K ψ q r p, psi_cycle K ψ p q r, ← right_distrib K, ← right_distrib K,
    ← K.distrib, ← K.distrib]
  cases htri p q r with
  | inl h0 => rw [h0, zero_mul', zero_mul']
  | inr h1 =>
    unfold c
    rw [triad_enstrophy K (k p) (k q) (k r) h1.2, mul_zero K, zero_mul']

/-- **Energy**: 3E = 0 ⇒ E = 0. -/
theorem energy_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (l : List I)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero) :
    S3 K (f K k δ ψ) l = K.zero := by
  apply h3
  have e1 : S3 K (fun p q r => f K k δ ψ q r p) l = S3 K (f K k δ ψ) l := triple_cycle K _ l
  have e2 : S3 K (fun p q r => f K k δ ψ r p q) l = S3 K (f K k δ ψ) l := by
    rw [← e1]; exact triple_cycle K (fun p q r => f K k δ ψ q r p) l
  have t : K.add (K.add (S3 K (f K k δ ψ) l) (S3 K (f K k δ ψ) l)) (S3 K (f K k δ ψ) l)
      = K.add (K.add (S3 K (f K k δ ψ) l) (S3 K (fun p q r => f K k δ ψ q r p) l))
          (S3 K (fun p q r => f K k δ ψ r p q) l) := by rw [e1, e2]
  rw [t, sum3_add]
  unfold S3
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro p
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro q
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro r
  exact cyc_zero K k δ ψ hsym1 hsym2 htri p q r

/-- **Enstrophy**: 3Z = 0 ⇒ Z = 0. -/
theorem enstrophy_zero (k : I → V2 R) (δ : I → I → I → R) (ψ : I → R) (l : List I)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero) :
    S3 K (fZ K k δ ψ) l = K.zero := by
  apply h3
  have e1 : S3 K (fun p q r => fZ K k δ ψ q r p) l = S3 K (fZ K k δ ψ) l := triple_cycle K _ l
  have e2 : S3 K (fun p q r => fZ K k δ ψ r p q) l = S3 K (fZ K k δ ψ) l := by
    rw [← e1]; exact triple_cycle K (fun p q r => fZ K k δ ψ q r p) l
  have t : K.add (K.add (S3 K (fZ K k δ ψ) l) (S3 K (fZ K k δ ψ) l)) (S3 K (fZ K k δ ψ) l)
      = K.add (K.add (S3 K (fZ K k δ ψ) l) (S3 K (fun p q r => fZ K k δ ψ q r p) l))
          (S3 K (fun p q r => fZ K k δ ψ r p q) l) := by rw [e1, e2]
  rw [t, sum3_add]
  unfold S3
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro p
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro q
  apply (lsum_ext K _ (fun _ => K.zero) _ l).trans (lsum_zero K l); intro r
  exact cycZ_zero K k δ ψ hsym1 hsym2 htri p q r

end Ring

end Galerkin2D

#print axioms Galerkin2D.triple_cycle
#print axioms Galerkin2D.sum3_add
#print axioms Galerkin2D.psi_cycle
#print axioms Galerkin2D.cyc_zero
#print axioms Galerkin2D.cycZ_zero
#print axioms Galerkin2D.energy_zero
#print axioms Galerkin2D.enstrophy_zero

Listing 168: ns/Galerkin2DCN.lean

ns/Galerkin2DCN.lean

/-
  ns/Galerkin2DCN.lean — T37 cycle 53: every Crank–Nicolson step of every 2D Fourier–Galerkin
  truncation is unconditionally non-increasing in energy and in enstrophy.

  Lean 4 core plus ns/Galerkin2D (energy_zero, enstrophy_zero) and ns/Enstrophy2D (cn_weighted_le).
  Modes 0..N−1, wave vectors k_n ∈ R², CN step with the nonlinearity evaluated at s = a' + a:
      a'_n − a_n = h(−ν|k_n|² s_n + Nn_n),   Nn_n = Σ_{q,r} δ(n,q,r) c(n,q,r) s_q s_r
  (δ symmetric, supported on triads). Then for w ≡ 1 (energy) and w = |k|² (enstrophy):
      Σ w_n a'_n² ≤ Σ w_n a_n²      — no smallness, no time-step restriction (ν, h ≥ 0).
    sumN_lsum      sumN g N = lsum g [N−1, …, 0]
    cn2d_energy, cn2d_enstrophy
-/
import ns.Galerkin2D
import lib.RingTac

namespace Galerkin2DCN
open DetChain NSChain Enstrophy2D Galerkin2D

section Ring
variable {R : Type} (K : CRing R)

def lst : Nat → List Nat
  | 0 => []
  | N + 1 => N :: lst N

theorem sumN_lsum (g : Nat → R) : ∀ N, sumN K g N = lsum K g (lst N)
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K g N) (g N) = K.add (g N) (lsum K g (lst N))
    rw [sumN_lsum g N, K.add_comm]

def Nn (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) (p : Nat) : R :=
  lsum K (fun q => lsum K (fun r => K.mul (K.mul (δ p q r) (c K k p q r)) (K.mul (s q) (s r))) (lst N)) (lst N)

/-- Energy part of the nonlinearity at s, as the Galerkin triple sum. -/
theorem neutral_energy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) :
    sumN K (fun n => K.mul K.one (K.mul (s n) (Nn K k δ s N n))) N = S3 K (f K k δ s) (lst N) := by
  rw [sumN_lsum]
  unfold S3
  apply lsum_ext; intro p
  rw [K.one_mul]
  unfold Nn
  rw [← lsum_mul_left]
  apply lsum_ext; intro q
  rw [← lsum_mul_left]
  apply lsum_ext; intro r
  unfold f
  rw [← K.mul_assoc, K.mul_comm (s p), K.mul_assoc]

/-- Enstrophy part (weight |k_p|²). -/
theorem neutral_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (s : Nat → R) (N : Nat) :
    sumN K (fun n => K.mul (n2 K (k n)) (K.mul (s n) (Nn K k δ s N n))) N = S3 K (fZ K k δ s) (lst N) := by
  rw [sumN_lsum]
  unfold S3
  apply lsum_ext; intro p
  unfold Nn
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_ext; intro q
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_ext; intro r
  unfold fZ
  generalize δ p q r = d
  generalize c K k p q r = cc
  generalize n2 K (k p) = m
  generalize K.mul (s q) (s r) = t
  generalize s p = x
  lib_ring

variable (P : Cone K)

theorem n2_nn (v : V2 R) : P.nn (n2 K v) := P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)

theorem one_nn : P.nn K.one := by
  have e := P.nn_sq K.one
  rw [K.one_mul] at e
  exact e

/-- **2D CN energy**: Σ a'² ≤ Σ a² for every step. -/
theorem cn2d_energy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a a' : Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a' n) (a n))))) (Nn K k δ (fun m => K.add (a' m) (a m)) N n))) :
    Le K P (sumN K (fun n => K.mul K.one (K.mul (a' n) (a' n))) N)
      (sumN K (fun n => K.mul K.one (K.mul (a n) (a n))) N) :=
  cn_weighted_le K P (fun _ => K.one) a a' (fun n => n2 K (k n)) (Nn K k δ (fun m => K.add (a' m) (a m)) N)
    h nu N (fun _ => one_nn K P) (fun n => n2_nn K P (k n)) hh hnu step
    ((neutral_energy K k δ _ N).trans (energy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))

/-- **2D CN enstrophy**: Σ |k|² a'² ≤ Σ |k|² a² for every step. -/
theorem cn2d_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a a' : Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a' n) (a n))))) (Nn K k δ (fun m => K.add (a' m) (a m)) N n))) :
    Le K P (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a' n) (a' n))) N)
      (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a n) (a n))) N) :=
  cn_weighted_le K P (fun n => n2 K (k n)) a a' (fun n => n2 K (k n)) (Nn K k δ (fun m => K.add (a' m) (a m)) N)
    h nu N (fun n => n2_nn K P (k n)) (fun n => n2_nn K P (k n)) hh hnu step
    ((neutral_enstrophy K k δ _ N).trans (enstrophy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))

end Ring

end Galerkin2DCN

#print axioms Galerkin2DCN.sumN_lsum
#print axioms Galerkin2DCN.neutral_energy
#print axioms Galerkin2DCN.neutral_enstrophy
#print axioms Galerkin2DCN.n2_nn
#print axioms Galerkin2DCN.one_nn
#print axioms Galerkin2DCN.cn2d_energy
#print axioms Galerkin2DCN.cn2d_enstrophy

Listing 169: ns/Global2DRun.lean

ns/Global2DRun.lean

/-
  ns/Global2DRun.lean — T37 cycle 69: 2D global regularity of every Galerkin truncation, along a whole
  Crank–Nicolson run.

  Lean 4 core plus ns/Galerkin2DCN (Nn, neutral_enstrophy) , ns/Galerkin2D (enstrophy_zero),
  ns/Enstrophy2D (cn_weighted), ns/LerayDiscrete (tele_run). Modes 0..N−1 of a 2D truncation, any run
  a^0, a^1, … of CN steps with the nonlinearity at the midpoint:
      Z_T + Σ_{t<T} hν Σ_n |k_n|²·|k_n|²(a^{t+1}_n + a^t_n)² = Z_0,    Z_t = Σ_n |k_n|² (a^t_n)²
  so the enstrophy never exceeds its initial value and the total H²-dissipation is ≤ Z_0, for every N, T
  (no smallness, no step restriction): the discrete 2D global regularity theorem, in Lean.
-/
import ns.Galerkin2DCN
import ns.LerayDiscrete

namespace Global2DRun
open DetChain NSChain Enstrophy2D Galerkin2D Galerkin2DCN

section Ring
variable {R : Type} (K : CRing R)

def Zt (k : Nat → V2 R) (a : Nat → Nat → R) (N t : Nat) : R :=
  sumN K (fun n => K.mul (n2 K (k n)) (K.mul (a t n) (a t n))) N

def Dt (k : Nat → V2 R) (a : Nat → Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (n2 K (k n)) (K.mul (n2 K (k n))
    (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))) N))

/-- **2D enstrophy equality along a run.** -/
theorem run_enstrophy (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a (t + 1) n) (a t n))))) (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N n))) (T : Nat) :
    K.add (Zt K k a N T) (sumN K (Dt K k a h nu N) T) = Zt K k a N 0 :=
  LerayDiscrete.tele_run K (Zt K k a N) (Dt K k a h nu N) (fun t =>
    cn_weighted K (fun n => n2 K (k n)) (a t) (a (t + 1)) (fun n => n2 K (k n))
      (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N) h nu N (step t)
      ((neutral_enstrophy K k δ _ N).trans (enstrophy_zero K k δ _ (lst N) hsym1 hsym2 htri h3))) T

/-- **2D global bound**: Z_T ≤ Z_0 and Σ dissipation ≤ Z_0. -/
theorem run_bound (P : Cone K) (k : Nat → V2 R) (δ : Nat → Nat → Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu)
    (hsym1 : ∀ p q r, δ q r p = δ p q r) (hsym2 : ∀ p q r, δ r p q = δ p q r)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ Triad K (k p) (k q) (k r)))
    (h3 : ∀ x : R, K.add (K.add x x) x = K.zero → x = K.zero)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (n2 K (k n))
      (K.add (a (t + 1) n) (a t n))))) (Nn K k δ (fun m => K.add (a (t + 1) m) (a t m)) N n))) (T : Nat) :
    Le K P (Zt K k a N T) (Zt K k a N 0) ∧ Le K P (sumN K (Dt K k a h nu N) T) (Zt K k a N 0) := by
  have eq := run_enstrophy K k δ a h nu N hsym1 hsym2 htri h3 step T
  have nk : ∀ n, P.nn (n2 K (k n)) := fun n => n2_nn K P (k n)
  have hZ : P.nn (Zt K k a N T) := sumN_nn K P _ (fun n => P.nn_mul _ _ (nk n) (P.nn_sq _)) N
  have hD : P.nn (sumN K (Dt K k a h nu N) T) := sumN_nn K P _ (fun t =>
    P.nn_mul _ _ hh (P.nn_mul _ _ hnu (sumN_nn K P _ (fun n =>
      P.nn_mul _ _ (nk n) (P.nn_mul _ _ (nk n) (P.nn_sq _))) N))) T
  refine ⟨⟨_, hD, eq⟩, ⟨Zt K k a N T, hZ, ?_⟩⟩
  rw [K.add_comm]; exact eq

end Ring

end Global2DRun

#print axioms Global2DRun.run_enstrophy
#print axioms Global2DRun.run_bound

79.45 The discrete Leray inequality

For every Galerkin truncation in every dimension and every Crank–Nicolson run with an energy-neutral nonlinearity,

\[ E_T+\sum _{t<T}h\nu D_t=E_0,\qquad E_t=\sum _n(a^t_n)^2,\qquad D_t=\sum _nk_n\,(a^{t+1}_n+a^t_n)^2 , \]
so in an ordered ring \(\sum _{t<T}h\nu D_t\le E_0\) and \(E_T\le E_0\), uniformly in \(N\) and \(T\).
\begin{align*} &E_{t+1}+h\nu D_t=E_t &&\text {(\texttt {NSChain.cn\_energy})}\\ &E_T+\textstyle \sum _{t<T}h\nu D_t=E_0 &&\text {(\texttt {LerayDiscrete.tele\_run})}\\ &E_T\ge 0,\ D_t\ge 0 &&\text {(sums of squares)} \end{align*}

Listing 170: ns/LerayDiscrete.lean

ns/LerayDiscrete.lean

/-
  ns/LerayDiscrete.lean — T37 cycle 66: the discrete Leray energy inequality, 2D and 3D.

  Lean 4 core plus NSChain (cn_energy, Cone, Le, sumN). For a run of Crank–Nicolson steps
  a^{t+1} from a^t (every t, energy-neutral nonlinearity — true for every Galerkin truncation in any
  dimension), with E_t = Σ_n (a^t_n)² and D_t = Σ_n k_n (a^{t+1}_n + a^t_n)²:
      E_T + Σ_{t<T} hν D_t = E_0                 (energy equality, every T)       leray_eq
      Σ_{t<T} hν D_t ≤ E_0,   E_T ≤ E_0           (Leray: dissipation bounded)     leray_le
  uniformly in the truncation size N and the number of steps T. This is the a priori bound that every
  3D weak-solution construction rests on; R1 asks for the same uniformity one derivative higher.
    tele_run   E(t+1) + d t = E t  ⇒  E T + Σ_{t<T} d t = E 0
-/
import NSChain
import lib.RingTac

namespace LerayDiscrete
open DetChain NSChain LibRing

section Ring
variable {R : Type} (K : CRing R)

theorem tele_run (E d : Nat → R) (h : ∀ t, K.add (E (t + 1)) (d t) = E t) :
    ∀ T, K.add (E T) (sumN K d T) = E 0
  | 0 => by show K.add (E 0) K.zero = E 0; lib_ring
  | T + 1 => by
    show K.add (E (T + 1)) (K.add (sumN K d T) (d T)) = E 0
    rw [← tele_run E d h T, ← h T]; lib_ring

def En (a : Nat → Nat → R) (N t : Nat) : R := sumN K (fun n => K.mul (a t n) (a t n)) N

def Dn (a : Nat → Nat → R) (k : Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a (t + 1) n) (a t n))
    (K.add (a (t + 1) n) (a t n)))) N))

/-- **Energy equality** along a run. -/
theorem leray_eq (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    K.add (En K a N T) (sumN K (Dn K a k h nu N) T) = En K a N 0 :=
  tele_run K (En K a N) (Dn K a k h nu N)
    (fun t => cn_energy K (a t) (a (t + 1)) k (Nn t) h nu N (step t) (neutral t)) T

/-- **Leray**: total dissipation ≤ initial energy, and E_T ≤ E_0. -/
theorem leray_le (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    Le K P (sumN K (Dn K a k h nu N) T) (En K a N 0) ∧ Le K P (En K a N T) (En K a N 0) := by
  have eq := leray_eq K a k Nn h nu N step neutral T
  have hE : P.nn (En K a N T) := sumN_nn K P _ (fun n => P.nn_sq _) N
  have hD : P.nn (sumN K (Dn K a k h nu N) T) :=
    sumN_nn K P _ (fun t => dissipation_nonneg K P h nu k _ N hh hnu hk) T
  refine ⟨⟨En K a N T, hE, ?_⟩, ⟨_, hD, eq⟩⟩
  rw [K.add_comm]; exact eq

end Ring

end LerayDiscrete

#print axioms LerayDiscrete.tele_run
#print axioms LerayDiscrete.leray_eq
#print axioms LerayDiscrete.leray_le

79.46 Three-dimensional transfer: per-triad and summed bounds

For one triad term \(T=(u\cdot k)(v\cdot w)\) with \(u,k,v,w\in R^3\), in every ordered commutative ring,

\[ T^2\le |u|^2|k|^2|v|^2|w|^2 , \]
and for the whole transfer of a truncation, with triads \(t\in l\),
\[ \Bigl (\sum _{t\in l}T_t\Bigr )^2\le |l|\sum _{t\in l}|u_t|^2|k_t|^2|v_t|^2|w_t|^2,\qquad \Bigl (\sum _tx_ty_t\Bigr )^2\le \Bigl (\sum _t\alpha _t\Bigr )\Bigl (\sum _t\omega _t\Bigr )\ \ (x_t^2\le \alpha _t,\ y_t^2\le \omega _t). \]
\begin{align*} &(a\cdot b)^2+|a\times b|^2=|a|^2|b|^2 &&\text {(Lagrange, by the ring normaliser)}\\ &\textstyle (\sum a^2)(\sum b^2)=(\sum ab)^2+\sum _{i<j}(a_ib_j-b_ia_j)^2 &&\text {(Lagrange for sums, by induction)}\\ &0\le a\le b,\ 0\le c\le d\ \Rightarrow \ ac\le bd &&\text {(cone arithmetic)} \end{align*}

The factor \(|l|\) grows with the truncation; the weighted form replaces it by a lattice sum \(\sum _k|k|^{-2s}\). Its two ingredients are proved over \(\mathbb N\) and in every ordered ring:

\begin{align*} &(2n+1)^3-(2n-1)^3=24n^2+2\le 26n^2 &&\text {(points on the shell $|k|_\infty =n$)}\\ &v_{n+1}\le u_n-u_{n+1},\ u\ge 0\ \Rightarrow \ \textstyle \sum _{n<N}v_{n+1}\le u_0 &&\text {(telescoping comparison)} \end{align*}

and a nonnegative sequence with \(z_{k+1}\le z_k+\delta z_k^2\), \(z_0\le B\), \(N\delta (2B)^2\le B\) stays below \(2B\) for \(N\) steps (discrete Riccati comparison).

Listing 171: ns/CFKernel.lean

ns/CFKernel.lean

/-
  ns/CFKernel.lean — T37 cycle 37: the algebraic heart of Constantin–Fefferman (route R3).

  Lean 4 core plus NSChain (CRing, Cone, Le) and tools/RingNorm (the normaliser).

  The vortex-stretching integrand of the Biot–Savart law is (ê·ξ(x)) det(ê, ξ(x+y), ξ(x)); it vanishes
  when the directions are parallel and is bounded by the sine of their angle. In every ordered
  commutative ring:
    semi             (a·b)² + Σ_cyc[(a₂b₃)² + (a₃b₂)²] = (a·a)(b·b) + 2Σ_cyc (a₂b₃)(a₃b₂)   (normaliser)
    sq_sub           (x − y)² + 2xy = x² + y²
    lagrange3        (a·b)² + |a × b|² = (a·a)(b·b)                               (3D Lagrange identity)
    cauchy_schwarz3  (a·b)² ≤ (a·a)(b·b)
    triple_bound     ê·ê = 1, ξ·ξ = 1, w = η × ξ:  (ê·ξ)² (ê·w)² ≤ w·w = |η × ξ|²
                     — the squared stretching kernel is at most sin²∠(η, ξ) for unit η
  With the Beirão da Veiga–Berselli condition |sin∠(ξ(x+y), ξ(x))| ≤ ρ|y|^{1/2}, this is what turns the
  kernel |y|^{-3} into the integrable |y|^{-5/2} (R3; measured quotient: NS_MAP R3-meas2).
-/
import NSChain
import tools.RingNorm

namespace CFKernel
open DetChain NSChain RingNorm

def x1 := PE.var 0
def x2 := PE.var 1
def x3 := PE.var 2
def y1 := PE.var 3
def y2 := PE.var 4
def y3 := PE.var 5
def d (p q r s t w : PE) : PE := .add (.add (.mul p s) (.mul q t)) (.mul r w)
def sq (e : PE) : PE := .mul e e
def LHS : PE := .add (sq (d x1 x2 x3 y1 y2 y3))
  (.add (.add (.add (sq (.mul x2 y3)) (sq (.mul x3 y2))) (.add (sq (.mul x3 y1)) (sq (.mul x1 y3))))
    (.add (sq (.mul x1 y2)) (sq (.mul x2 y1))))
def RHS : PE := .add (.mul (d x1 x2 x3 x1 x2 x3) (d y1 y2 y3 y1 y2 y3))
  (.add (.add (.add (.mul (.mul x2 y3) (.mul x3 y2)) (.mul (.mul x2 y3) (.mul x3 y2)))
      (.add (.mul (.mul x3 y1) (.mul x1 y3)) (.mul (.mul x3 y1) (.mul x1 y3))))
    (.add (.mul (.mul x1 y2) (.mul x2 y1)) (.mul (.mul x1 y2) (.mul x2 y1))))
theorem chk : nf LHS = nf RHS := rfl

section Ring
variable {R : Type} (K : CRing R)

structure V (R : Type) where
  x : R
  y : R
  z : R

def rho (a b : V R) (i : Nat) : R :=
  match i with
  | 0 => a.x | 1 => a.y | 2 => a.z | 3 => b.x | 4 => b.y | _ => b.z

def dot (a b : V R) : R := K.add (K.add (K.mul a.x b.x) (K.mul a.y b.y)) (K.mul a.z b.z)
def sub (u v : R) : R := K.add u (K.neg v)
def cross (a b : V R) : V R :=
  ⟨sub K (K.mul a.y b.z) (K.mul a.z b.y), sub K (K.mul a.z b.x) (K.mul a.x b.z),
   sub K (K.mul a.x b.y) (K.mul a.y b.x)⟩

theorem semi (a b : V R) :
    K.add (K.mul (dot K a b) (dot K a b))
      (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.y b.z)) (K.mul (K.mul a.z b.y) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.z b.x)) (K.mul (K.mul a.x b.z) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.x b.y)) (K.mul (K.mul a.y b.x) (K.mul a.y b.x))))
    = K.add (K.mul (dot K a a) (dot K b b))
      (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)))) :=
  norm_sound K (rho a b) LHS RHS chk

theorem sq_sub (u v : R) :
    K.add (K.mul (sub K u v) (sub K u v)) (K.add (K.mul u v) (K.mul u v)) = K.add (K.mul u u) (K.mul v v) := by
  unfold sub
  rw [right_distrib K, K.distrib, K.distrib, mul_neg' K, neg_mul_left K, neg_mul_left K, mul_neg' K,
    neg_neg' K, K.mul_comm v u]
  generalize K.mul u u = A
  generalize K.mul u v = B
  generalize K.mul v v = C
  -- (A + −B) + (−B + C) + (B + B) = A + C
  rw [add_mid4 K, K.add_assoc, K.add_assoc, ← K.add_assoc (K.neg B) B, add_neg' K, K.zero_add,
    K.add_comm (K.neg B) C, K.add_assoc C, add_neg' K, add_zero']

def crossSq (a b : V R) : R :=
  K.add (K.add (K.mul (cross K a b).x (cross K a b).x) (K.mul (cross K a b).y (cross K a b).y))
    (K.mul (cross K a b).z (cross K a b).z)

/-- **3D Lagrange identity.** -/
theorem lagrange3 (a b : V R) :
    K.add (K.mul (dot K a b) (dot K a b)) (crossSq K a b) = K.mul (dot K a a) (dot K b b) := by
  have h := semi K a b
  -- S = crossSq + T (componentwise sq_sub)
  have hS : K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.y b.z)) (K.mul (K.mul a.z b.y) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.z b.x)) (K.mul (K.mul a.x b.z) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.x b.y)) (K.mul (K.mul a.y b.x) (K.mul a.y b.x)))
      = K.add (crossSq K a b)
        (K.add (K.add (K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)))) := by
    unfold crossSq cross
    show _ = K.add (K.add (K.add (K.mul (sub K (K.mul a.y b.z) (K.mul a.z b.y)) (sub K (K.mul a.y b.z) (K.mul a.z b.y)))
        (K.mul (sub K (K.mul a.z b.x) (K.mul a.x b.z)) (sub K (K.mul a.z b.x) (K.mul a.x b.z))))
        (K.mul (sub K (K.mul a.x b.y) (K.mul a.y b.x)) (sub K (K.mul a.x b.y) (K.mul a.y b.x)))) _
    rw [← sq_sub K (K.mul a.y b.z) (K.mul a.z b.y), ← sq_sub K (K.mul a.z b.x) (K.mul a.x b.z),
      ← sq_sub K (K.mul a.x b.y) (K.mul a.y b.x)]
    generalize K.mul (sub K (K.mul a.y b.z) (K.mul a.z b.y)) (sub K (K.mul a.y b.z) (K.mul a.z b.y)) = S1
    generalize K.mul (sub K (K.mul a.z b.x) (K.mul a.x b.z)) (sub K (K.mul a.z b.x) (K.mul a.x b.z)) = S2
    generalize K.mul (sub K (K.mul a.x b.y) (K.mul a.y b.x)) (sub K (K.mul a.x b.y) (K.mul a.y b.x)) = S3
    generalize K.add (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) (K.mul (K.mul a.y b.z) (K.mul a.z b.y)) = T1
    generalize K.add (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) (K.mul (K.mul a.z b.x) (K.mul a.x b.z)) = T2
    generalize K.add (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) (K.mul (K.mul a.x b.y) (K.mul a.y b.x)) = T3
    rw [add_mid4 K (K.add S1 S2) S3 (K.add T1 T2) T3, add_mid4 K S1 S2 T1 T2]
  rw [hS, ← K.add_assoc] at h
  exact add_cancel_left K _ _ _ (by rw [K.add_comm _ (K.add _ (crossSq K a b)), h, K.add_comm])

theorem crossSq_nn (P : Cone K) (a b : V R) : P.nn (crossSq K a b) :=
  P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

/-- **Cauchy–Schwarz in 3D.** -/
theorem cauchy_schwarz3 (P : Cone K) (a b : V R) :
    Le K P (K.mul (dot K a b) (dot K a b)) (K.mul (dot K a a) (dot K b b)) :=
  ⟨crossSq K a b, crossSq_nn K P a b, lagrange3 K a b⟩

/-- **The stretching kernel is bounded by the angle.** For unit ê, ξ and w = η × ξ:
    (ê·ξ)² (ê·w)² ≤ w·w. -/
theorem triple_bound (P : Cone K) (e xi eta : V R) (he : dot K e e = K.one) (hxi : dot K xi xi = K.one) :
    Le K P (K.mul (K.mul (dot K e xi) (dot K e xi)) (K.mul (dot K e (cross K eta xi)) (dot K e (cross K eta xi))))
      (dot K (cross K eta xi) (cross K eta xi)) := by
  let w := cross K eta xi
  have c1 := cauchy_schwarz3 K P e xi
  rw [he, hxi, K.one_mul] at c1
  have c2 := cauchy_schwarz3 K P e w
  rw [he, K.one_mul] at c2
  -- X ≤ 1, Y ≤ Z, X ≥ 0 ⇒ X·Y ≤ X·Z ≤ Z
  cases c1 with
  | intro d1 h1 =>
    cases c2 with
    | intro d2 h2 =>
      -- X Y + (X d2 + d1 Z) = Z
      refine ⟨K.add (K.mul (K.mul (dot K e xi) (dot K e xi)) d2) (K.mul d1 (dot K w w)),
        P.nn_add _ _ (P.nn_mul _ _ (P.nn_sq _) h2.1) (P.nn_mul _ _ h1.1 (by
          show P.nn (dot K w w)
          unfold dot
          exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _))), ?_⟩
      rw [← K.add_assoc, ← K.distrib, h2.2, ← right_distrib K, h1.2, K.one_mul]

end Ring

end CFKernel

#print axioms CFKernel.chk
#print axioms CFKernel.semi
#print axioms CFKernel.sq_sub
#print axioms CFKernel.lagrange3
#print axioms CFKernel.crossSq_nn
#print axioms CFKernel.cauchy_schwarz3
#print axioms CFKernel.triple_bound

Listing 172: ns/TriadBound.lean

ns/TriadBound.lean

/-
  ns/TriadBound.lean — T37 cycle 45: the size of one triad's transfer, by Cauchy–Schwarz.

  Lean 4 core plus ns/CFKernel (dot, cauchy_schwarz3, Cone, Le). For one term of the Fourier–Galerkin
  trilinear form, T = (u_p·k_q)(v_q·w_r),
      T² ≤ (|u_p|²|k_q|²)(|v_q|²|w_r|²)       (squares; no square roots needed)
  With w_r = |k_r|² u_r (enstrophy weight) this is the triad form of |dZ/dt| ≲ Σ |k_q||k_r|²|u_p||u_q||u_r|,
  the discrete local-existence estimate (Z' ≤ C Z^{3/2}-type), and with ns/Stretch3D it is attained up
  to constants by an explicit triad. What it cannot give is a bound uniform in the number of triads
  (R1-gap).
    le_mul_both    0 ≤ a ≤ b, 0 ≤ c ≤ d ⇒ ac ≤ bd (in a cone with the needed nonnegativity)
    triad_sq_bound T² ≤ (u·u)(k·k)(v·v)(w·w)
-/
import ns.CFKernel

namespace TriadBound
open DetChain NSChain CFKernel

section Ring
variable {R : Type} (K : CRing R)

theorem le_mul_both (P : Cone K) (a b c d : R) (ha : P.nn a) (hd : P.nn d)
    (h1 : Le K P a b) (h2 : Le K P c d) : Le K P (K.mul a c) (K.mul b d) := by
  cases h1 with
  | intro e1 h1 =>
    cases h2 with
    | intro e2 h2 =>
      -- b d = (a + e1)(c + e2)... use: a c + (a e2 + e1 d) = a d + e1 d = b d
      refine ⟨K.add (K.mul a e2) (K.mul e1 d), P.nn_add _ _ (P.nn_mul _ _ ha h2.1) (P.nn_mul _ _ h1.1 hd), ?_⟩
      rw [← K.add_assoc, ← K.distrib, h2.2, ← right_distrib K, h1.2]

/-- **One triad term, squared, is bounded by the product of the squared norms.** -/
theorem triad_sq_bound (P : Cone K) (u k v w : V R) :
    Le K P (K.mul (K.mul (dot K u k) (dot K u k)) (K.mul (dot K v w) (dot K v w)))
      (K.mul (K.mul (dot K u u) (dot K k k)) (K.mul (dot K v v) (dot K w w))) := by
  have nnd : ∀ a : V R, P.nn (dot K a a) := fun a => by
    unfold dot; exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)
  exact le_mul_both K P _ _ _ _ (P.nn_sq _) (P.nn_mul _ _ (nnd v) (nnd w))
    (cauchy_schwarz3 K P u k) (cauchy_schwarz3 K P v w)

end Ring

end TriadBound

#print axioms TriadBound.le_mul_both
#print axioms TriadBound.triad_sq_bound

Listing 173: ns/SumCS.lean

ns/SumCS.lean

/-
  ns/SumCS.lean — T37 cycle 51: Cauchy–Schwarz for finite sums, by the Lagrange identity.

  Lean 4 core plus NSChain (lsum, Cone, Le) and lib/RingTac. For a list l and a, b : I → R:
      (Σa²)(Σb²) = (Σab)² + D,   D = Σ_{i before j} (a_i b_j − b_i a_j)²        (lagrange_sum)
      (Σab)² ≤ (Σa²)(Σb²)                                                     (cs_sum)
  in every ordered commutative ring. This is the tool that sums the per-triad bound (TriadBound) over
  all triads of a truncation: Σ|T| ≤ (Σ…)^{1/2}(Σ…)^{1/2} in squared form.
-/
import NSChain
import lib.RingTac

namespace SumCS
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R) {I : Type}

def sq (x : R) : R := K.mul x x

def cross (a b : I → R) (x y : R) (l : List I) : R :=
  lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l

def D (a b : I → R) : List I → R
  | [] => K.zero
  | i :: l => K.add (D a b l) (cross K a b (a i) (b i) l)

theorem inner (a b : I → R) (x y : R) : ∀ l : List I,
    K.add (K.mul (sq K x) (lsum K (fun i => sq K (b i)) l)) (K.mul (sq K y) (lsum K (fun i => sq K (a i)) l))
      = K.add (K.mul (K.add (K.mul x y) (K.mul x y)) (lsum K (fun i => K.mul (a i) (b i)) l)) (cross K a b x y l)
  | [] => by show K.add (K.mul (sq K x) K.zero) (K.mul (sq K y) K.zero) = K.add (K.mul _ K.zero) K.zero; lib_ring
  | i :: l => by
    have ih := inner a b x y l
    unfold cross at ih ⊢
    show K.add (K.mul (sq K x) (K.add (sq K (b i)) (lsum K (fun i => sq K (b i)) l)))
        (K.mul (sq K y) (K.add (sq K (a i)) (lsum K (fun i => sq K (a i)) l)))
      = K.add (K.mul (K.add (K.mul x y) (K.mul x y)) (K.add (K.mul (a i) (b i)) (lsum K (fun i => K.mul (a i) (b i)) l)))
          (K.add (sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) (lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l))
    revert ih
    generalize lsum K (fun i => sq K (b i)) l = B
    generalize lsum K (fun i => sq K (a i)) l = A
    generalize lsum K (fun i => K.mul (a i) (b i)) l = C
    generalize lsum K (fun i => sq K (sub K (K.mul x (b i)) (K.mul y (a i)))) l = X
    intro ih
    have e : K.add (K.mul (sq K x) (K.add (sq K (b i)) B)) (K.mul (sq K y) (K.add (sq K (a i)) A))
        = K.add (K.add (K.mul (sq K x) B) (K.mul (sq K y) A))
            (K.add (K.mul (sq K x) (sq K (b i))) (K.mul (sq K y) (sq K (a i)))) := by unfold sq; lib_ring
    rw [e, ih]; unfold sq; lib_ring

/-- **Lagrange identity for finite sums.** -/
theorem lagrange_sum (a b : I → R) : ∀ l : List I,
    K.mul (lsum K (fun i => sq K (a i)) l) (lsum K (fun i => sq K (b i)) l)
      = K.add (sq K (lsum K (fun i => K.mul (a i) (b i)) l)) (D K a b l)
  | [] => by show K.mul K.zero K.zero = K.add (K.mul K.zero K.zero) K.zero; lib_ring
  | i :: l => by
    have ih := lagrange_sum a b l
    have hin := inner K a b (a i) (b i) l
    show K.mul (K.add (sq K (a i)) (lsum K (fun i => sq K (a i)) l)) (K.add (sq K (b i)) (lsum K (fun i => sq K (b i)) l))
      = K.add (sq K (K.add (K.mul (a i) (b i)) (lsum K (fun i => K.mul (a i) (b i)) l)))
          (K.add (D K a b l) (cross K a b (a i) (b i) l))
    revert ih hin
    generalize lsum K (fun i => sq K (b i)) l = B
    generalize lsum K (fun i => sq K (a i)) l = A
    generalize lsum K (fun i => K.mul (a i) (b i)) l = C
    generalize cross K a b (a i) (b i) l = X
    generalize D K a b l = Dl
    intro ih hin
    have e : K.mul (K.add (sq K (a i)) A) (K.add (sq K (b i)) B)
        = K.add (K.add (K.mul (sq K (a i)) (sq K (b i))) (K.add (K.mul (sq K (a i)) B) (K.mul (sq K (b i)) A)))
            (K.mul A B) := by unfold sq; lib_ring
    rw [e, hin, ih]; unfold sq; lib_ring

theorem D_nn (P : Cone K) (a b : I → R) : ∀ l : List I, P.nn (D K a b l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (D_nn P a b l) (by
      unfold cross
      generalize l = m
      induction m with
      | nil => exact P.nn_zero
      | cons j m ihm => exact P.nn_add _ _ (P.nn_sq _) ihm)

/-- **Cauchy–Schwarz for finite sums.** -/
theorem cs_sum (P : Cone K) (a b : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (a i) (b i)) l))
      (K.mul (lsum K (fun i => sq K (a i)) l) (lsum K (fun i => sq K (b i)) l)) :=
  ⟨D K a b l, D_nn K P a b l, (lagrange_sum K a b l).symm⟩

end SumCS

#print axioms SumCS.inner
#print axioms SumCS.lagrange_sum
#print axioms SumCS.D_nn
#print axioms SumCS.cs_sum

Listing 174: ns/TriadSum.lean

ns/TriadSum.lean

/-
  ns/TriadSum.lean — T37 cycle 56: the whole 3D transfer of a truncation, bounded with an explicit
  truncation-dependent constant.

  Lean 4 core plus ns/TriadBound, ns/SumCS, ns/Riccati (le_add). For triads t ∈ l with terms
  T_t = (u_t·k_t)(v_t·w_t):
      (Σ_t T_t)² ≤ |l| · Σ_t |u_t|²|k_t|²|v_t|²|w_t|²
  (|l| = number of triads, as Σ 1). The factor |l| grows with the truncation: this is the exact place
  where a uniform bound (R1) must improve on Cauchy–Schwarz (by the dyadic/Littlewood–Paley counting
  of interacting triads).
    lsum_le      termwise ≤ ⇒ sums ≤
    transfer_sq  the bound above
-/
import ns.TriadBound
import ns.SumCS
import ns.Riccati

namespace TriadSum
open DetChain NSChain CFKernel TriadBound SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_le (P : Cone K) (f g : I → R) (h : ∀ i, Le K P (f i) (g i)) :
    ∀ l : List I, Le K P (lsum K f l) (lsum K g l)
  | [] => le_refl' K P _
  | i :: l => Riccati.le_add K P _ _ _ _ (h i) (lsum_le P f g h l)

theorem lsum_nn (P : Cone K) (f : I → R) (h : ∀ i, P.nn (f i)) : ∀ l : List I, P.nn (lsum K f l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn P f h l)

theorem one_nn (P : Cone K) : P.nn K.one := by
  have e := P.nn_sq K.one
  rw [K.one_mul] at e
  exact e

/-- **Transfer bound**: (Σ T)² ≤ |l| Σ |u|²|k|²|v|²|w|². -/
theorem transfer_sq (P : Cone K) (u k v w : I → V R) (l : List I) :
    Le K P (sq K (lsum K (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) l))
      (K.mul (lsum K (fun t => K.mul (K.mul (dot K (u t) (u t)) (dot K (k t) (k t)))
          (K.mul (dot K (v t) (v t)) (dot K (w t) (w t)))) l) (lsum K (fun _ => K.one) l)) := by
  have one1 : ∀ t : I, sq K ((fun _ : I => K.one) t) = K.one := fun _ => K.one_mul _
  have e1 : lsum K (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) l
      = lsum K (fun t => K.mul (K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) ((fun _ : I => K.one) t)) l :=
    lsum_ext K _ _ (fun t => by
      show _ = K.mul _ K.one
      rw [K.mul_comm (K.mul _ _) K.one, K.one_mul]) l
  have cs := cs_sum K P (fun t => K.mul (dot K (u t) (k t)) (dot K (v t) (w t))) (fun _ => K.one) l
  rw [← e1, lsum_ext K _ _ one1 l] at cs
  refine le_trans' K P _ _ _ cs ?_
  rw [K.mul_comm _ (lsum K (fun _ => K.one) l), K.mul_comm _ (lsum K (fun _ => K.one) l)]
  apply Riccati.le_mul_left K P _ _ _ (lsum_nn K P _ (fun _ => one_nn K P) l)
  apply lsum_le K P
  intro t
  have b := triad_sq_bound K P (u t) (k t) (v t) (w t)
  have e : sq K (K.mul (dot K (u t) (k t)) (dot K (v t) (w t)))
      = K.mul (K.mul (dot K (u t) (k t)) (dot K (u t) (k t))) (K.mul (dot K (v t) (w t)) (dot K (v t) (w t))) := by
    unfold SumCS.sq
    generalize dot K (u t) (k t) = A
    generalize dot K (v t) (w t) = B
    rw [K.mul_assoc, ← K.mul_assoc B A B, K.mul_comm B A, K.mul_assoc A B B, ← K.mul_assoc]
  rw [e]; exact b

end TriadSum

#print axioms TriadSum.lsum_le
#print axioms TriadSum.lsum_nn
#print axioms TriadSum.one_nn
#print axioms TriadSum.transfer_sq

Listing 175: ns/WeightedCS.lean

ns/WeightedCS.lean

/-
  ns/WeightedCS.lean — T37 cycle 59: the weighted form of the transfer bound.

  Lean 4 core plus ns/TriadSum (lsum_le, lsum_nn) and ns/SumCS. Split each triad term as T_t = x_t·y_t
  with x_t² ≤ α_t and y_t² ≤ ω_t. Then
      (Σ_t T_t)² ≤ (Σ_t α_t)(Σ_t ω_t).
  With y_t = |k_t|^{-s}·(…) the weight sum Σω is a lattice sum Σ_k |k|^{-2s}, finite for s > 3/2 in 3D
  and independent of the truncation: this replaces the factor |l| of TriadSum (the Sobolev-type route
  to a truncation-uniform local time for H^s data, s > 3/2; with Riccati).
    le_mul_both'   0 ≤ a ≤ b, 0 ≤ c ≤ d ⇒ ac ≤ bd
    weighted_cs    the bound above
-/
import ns.TriadSum

namespace WeightedCS
open DetChain NSChain SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem le_mul_both' (P : Cone K) (a b c d : R) (ha : P.nn a) (hd : P.nn d)
    (h1 : Le K P a b) (h2 : Le K P c d) : Le K P (K.mul a c) (K.mul b d) :=
  TriadBound.le_mul_both K P a b c d ha hd h1 h2

theorem weighted_cs (P : Cone K) (x y α ω : I → R) (l : List I)
    (hx : ∀ t, Le K P (sq K (x t)) (α t)) (hy : ∀ t, Le K P (sq K (y t)) (ω t))
    (hω : ∀ t, P.nn (ω t)) :
    Le K P (sq K (lsum K (fun t => K.mul (x t) (y t)) l))
      (K.mul (lsum K α l) (lsum K ω l)) :=
  le_trans' K P _ _ _ (cs_sum K P x y l)
    (le_mul_both' K P _ _ _ _ (lsum_nn K P _ (fun t => P.nn_sq (x t)) l) (lsum_nn K P _ hω l)
      (lsum_le K P _ _ hx l) (lsum_le K P _ _ hy l))

end WeightedCS

#print axioms WeightedCS.le_mul_both'
#print axioms WeightedCS.weighted_cs

Listing 176: ns/LatticeSum.lean

ns/LatticeSum.lean

/-
  ns/LatticeSum.lean — T37 cycle 954 (goal G624c, piece 8b): the shell sum of the Abel identity is bounded by 108,
  over an ordered field (inverses of nonzero elements), uniformly in M. With iv j = 1/j, f j = (iv j)⁴, and a list
  L of naturals ≥ 1 whose counting function satisfies cnt(L, j) ≤ (2j+1)³ (ModeCount.cnt_cube):
    shell_term   cnt(L, i+1)·(f(i+1) − f(i+2)) ≤ 108·(iv(i+1) − iv(i+2))
    shell_sum    Σ_{j<M} cnt(L, j)·(f j − f(j+1)) ≤ 108.
  The term j = 0 vanishes (all entries ≥ 1); the others use PolyBound (cast), LatticeStep.step_le and telescoping.

  Lean 4 core plus ns/AbelCount, ns/PolyBound, ns/LatticeStep, ns/BaselQ (inv_nn), ns/GoodModesQ, ns/RatModel.
-/
import ns.AbelCount
import ns.PolyBound
import ns.LatticeStep
import ns.BaselQ

namespace LatticeSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep GoodModesQ

theorem cnt_zero : ∀ (L : List Nat), (∀ n, n ∈ L → 1 ≤ n) → cnt L 0 = 0
  | [], _ => rfl
  | n :: L, h => by
    have hn : 1 ≤ n := h n (List.Mem.head _)
    have hb : Nat.ble n 0 = false := AbelCount.ble_false hn
    show cond (Nat.ble n 0) (cnt L 0 + 1) (cnt L 0) = 0
    rw [hb]; exact cnt_zero L (fun x hx => h x (List.Mem.tail _ hx))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem castN_mul (a : Nat) : ∀ b, castN K (a * b) = K.mul (castN K a) (castN K b)
  | 0 => by show K.zero = K.mul (castN K a) K.zero; lib_ring
  | b + 1 => by
    show castN K (a * b + a) = K.mul (castN K a) (K.add K.one (castN K b))
    rw [RatModel.cast_add, castN_mul a b]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def iv (j : Nat) : R := inv (castN K j)
def f4 (j : Nat) : R := p4 K (iv K inv j)

include O mul_inv h10 in
theorem iv_mul (n : Nat) : K.mul (castN K (n + 1)) (iv K inv (n + 1)) = K.one :=
  mul_inv _ (cast_succ_ne K O h10 n)

include mul_inv h10 in
theorem iv_nn (n : Nat) : O.nn (iv K inv (n + 1)) :=
  BaselQ.inv_nn K O inv _ (RatModel.nn_cast K O.toCone (n + 1)) (iv_mul K O inv mul_inv h10 n)

/-- one shell, with the inverses as variables: c·(iA⁴ − iB⁴) ≤ 108·(iA − iB) for A = a, B = a + 1 -/
theorem shell_core (a : Nat) (ha : 1 ≤ a) (c : Nat) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
    (iA iB : R) (hA : K.mul (castN K a) iA = K.one) (hB : K.mul (castN K (a + 1)) iB = K.one) :
    Le K O.toCone (K.mul (castN K c) (K.add (p4 K iA) (K.neg (p4 K iB))))
      (K.mul (castN K 108) (K.add iA (K.neg iB))) := by
  have pb := PolyBound.poly_bound a ha
  have ⟨w, hw⟩ := DyadicModelZ.le_dest pb
  have hcast := congrArg (castN K) hw
  -- explicit casts
  have cB : castN K (a + 1) = K.add (castN K a) K.one := by show K.add K.one (castN K a) = _; lib_ring
  have cS : castN K (a + (a + 1)) = K.add (castN K a) (K.add (castN K a) K.one) := by
    rw [RatModel.cast_add K a (a + 1), cB]
  have c3 : ∀ x, castN K (x * x * x) = K.mul (K.mul (castN K x) (castN K x)) (castN K x) := fun x => by
    rw [castN_mul K (x * x) x, castN_mul K x x]
  have cD : castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a)
      = K.add (K.add (K.add (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K (a + 1)))
          (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K a)))
          (K.mul (K.mul (castN K (a + 1)) (castN K a)) (castN K a))) (K.mul (K.mul (castN K a) (castN K a)) (castN K a)) := by
    rw [RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a) (a * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a) ((a + 1) * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1)) ((a + 1) * (a + 1) * a),
      c3 (a + 1), c3 a, castN_mul K ((a + 1) * (a + 1)) a, castN_mul K (a + 1) (a + 1),
      castN_mul K ((a + 1) * a) a, castN_mul K (a + 1) a]
  have cL : castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1)) *
        ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a) + w)
      = K.add (K.mul (castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1))))
          (castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a))) (castN K w) := by
    rw [RatModel.cast_add, castN_mul]
  have cR : castN K (108 * (a * a * a) * ((a + 1) * (a + 1) * (a + 1)))
      = K.mul (K.mul (castN K 108) (castN K (a * a * a))) (castN K ((a + 1) * (a + 1) * (a + 1))) := by
    rw [castN_mul K (108 * (a * a * a)), castN_mul K 108]
  rw [cL, cR, cD, c3 (a + (a + 1)), c3 a, c3 (a + 1), cS, cB] at hcast
  have cC : castN K ((a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
      = K.mul (K.mul (K.add (castN K a) (K.add (castN K a) K.one)) (K.add (castN K a) (K.add (castN K a) K.one)))
          (K.add (castN K a) (K.add (castN K a) K.one)) := by
    rw [castN_mul K (a + (a + 1)), castN_mul K (a + (a + 1)), cS]; lib_ring
  rw [cB] at hB
  have An0 : O.nn (castN K a) := RatModel.nn_cast K O.toCone a
  have m0 := RatModel.cast_mono K O.toCone _ _ hc
  rw [cC] at m0
  generalize castN K a = A at hA hB hcast m0 An0
  have Bn : O.nn (K.add A K.one) := O.nn_add _ _ An0 (RatModel.nn_one' K O.toCone)
  have hD := inv4_diff K A (K.add A K.one) iA iB hA hB rfl
  have Dn : O.nn (K.add (K.add (K.add (p3 K (K.add A K.one)) (K.mul (K.mul (K.add A K.one) (K.add A K.one)) A))
      (K.mul (K.add A K.one) (K.mul A A))) (p3 K A)) := by
    unfold p3
    exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) Bn) (O.nn_mul _ _ (O.nn_sq _) An0))
      (O.nn_mul _ _ Bn (O.nn_sq _))) (O.nn_mul _ _ (O.nn_sq _) An0)
  have gn : O.nn (K.add (p4 K iA) (K.neg (p4 K iB))) := by
    rw [hD]; unfold p4
    exact O.nn_mul _ _ Dn (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)))
  have h1 := le_mul_nn K O.toCone _ _ _ gn m0
  rw [K.mul_comm _ (castN K c), K.mul_comm (K.add (p4 K iA) (K.neg (p4 K iB)))] at h1
  have h2 := step_le K O.toCone A (K.add A K.one) iA iB
    (K.mul (K.mul (K.add A (K.add A K.one)) (K.add A (K.add A K.one))) (K.add A (K.add A K.one))) (castN K w) hA hB rfl (RatModel.nn_cast K O.toCone w) (by
    unfold p3; rw [← hcast]; lib_ring)
  exact le_trans' K O.toCone _ _ _ h1 h2

include mul_inv h10 in
theorem shell_term (L : List Nat) (i : Nat)
    (hc : cnt L (i + 1) ≤ (2 * (i + 1) + 1) * ((2 * (i + 1) + 1) * (2 * (i + 1) + 1))) :
    Le K O.toCone (K.mul (castN K (cnt L (i + 1))) (gd K (f4 K inv) (i + 1)))
      (K.mul (castN K 108) (K.add (iv K inv (i + 1)) (K.neg (iv K inv (i + 1 + 1))))) := by
  have e2 : 2 * (i + 1) + 1 = (i + 1) + ((i + 1) + 1) := by lib_nat
  rw [e2] at hc
  exact shell_core K O (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) _ hc _ _
    (iv_mul K O inv mul_inv h10 i) (iv_mul K O inv mul_inv h10 (i + 1))

def u (j : Nat) : R := match j with
  | 0 => iv K inv 1
  | j + 1 => iv K inv (j + 1)

include mul_inv h10 in
theorem u_nn : ∀ j, O.nn (u K inv j)
  | 0 => iv_nn K O inv mul_inv h10 0
  | j + 1 => iv_nn K O inv mul_inv h10 j

include O mul_inv h10 in
theorem iv_one : iv K inv 1 = K.one := by
  have h := iv_mul K O inv mul_inv h10 0
  have e : castN K (0 + 1) = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [e] at h
  calc iv K inv 1 = K.mul K.one (iv K inv 1) := by lib_ring
    _ = K.one := h

include mul_inv h10 in
theorem shell_sum (L : List Nat) (h1 : ∀ n, n ∈ L → 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f4 K inv) j)) M) (castN K 108) := by
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt L j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (u K inv j) (K.neg (u K inv (j + 1))))) := by
    intro j
    cases j with
    | zero =>
      rw [cnt_zero L h1]
      refine ⟨K.zero, O.nn_zero, ?_⟩
      show K.add (K.mul K.zero (gd K (f4 K inv) 0)) K.zero = K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (iv K inv 1)))
      lib_ring
    | succ i => exact shell_term K O inv mul_inv h10 L i (hc (i + 1))
  have hs := sum_le K O.toCone _ _ term M
  rw [sumN_mul_left, telescope] at hs
  refine le_trans' K O.toCone _ _ _ hs ⟨K.mul (castN K 108) (u K inv M), O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108)
    (u_nn K O inv mul_inv h10 M), ?_⟩
  show K.add (K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (u K inv M)))) (K.mul (castN K 108) (u K inv M)) = castN K 108
  rw [iv_one K O inv mul_inv h10]; lib_ring

end Gen

end LatticeSum

#print axioms LatticeSum.cnt_zero
#print axioms LatticeSum.castN_mul
#print axioms LatticeSum.shell_term
#print axioms LatticeSum.shell_sum

Listing 177: ns/Riccati.lean

ns/Riccati.lean

/-
  ns/Riccati.lean — T37 cycle 48: the discrete Riccati comparison (local existence time).

  Lean 4 core plus NSChain (CRing, Cone, Le) and lib/RingTac. A nonnegative sequence with
      z_{k+1} ≤ z_k + δ z_k²      (the explicit-Euler form of Z' ≤ C Z², δ = hC)
  and z_0 ≤ B stays below 2B for N steps whenever N·δ·(2B)² ≤ B: the discrete local existence time
  N h ≈ 1/(4 C Z_0), which depends only on the initial size, not on the truncation. With the triad bound
  (TriadBound) summed into Z' ≤ C Z² (R1-gap: the C uniform in N), this is the local regularity step.
    le_add, le_mul_left, le_sq, rep_mono     cone arithmetic
    riccati_inv    z_k ≤ B + k·δ(2B)²  for k ≤ N
    riccati        z_k ≤ 2B           for k ≤ N
-/
import NSChain
import lib.RingTac

namespace Riccati
open DetChain NSChain

variable {R : Type} (K : CRing R)

def rep (x : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (rep x n) x

theorem le_add (P : Cone K) (x y u v : R) (h1 : Le K P x y) (h2 : Le K P u v) :
    Le K P (K.add x u) (K.add y v) := by
  cases h1 with
  | intro d1 h1 => cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add _ _ h1.1 h2.1, ?_⟩
      rw [← h1.2, ← h2.2]; lib_ring

theorem le_mul_left (P : Cone K) (c x y : R) (hc : P.nn c) (h : Le K P x y) :
    Le K P (K.mul c x) (K.mul c y) := by
  cases h with
  | intro d h =>
    refine ⟨K.mul c d, P.nn_mul _ _ hc h.1, ?_⟩
    rw [← h.2]; lib_ring

theorem le_sq (P : Cone K) (x y : R) (hx : P.nn x) (h : Le K P x y) :
    Le K P (K.mul x x) (K.mul y y) := by
  cases h with
  | intro d h =>
    refine ⟨K.add (K.mul (K.add x x) d) (K.mul d d),
      P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hx hx) h.1) (P.nn_sq d), ?_⟩
    rw [← h.2]; lib_ring

theorem rep_nn (P : Cone K) (q : R) (hq : P.nn q) : ∀ n, P.nn (rep K q n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (rep_nn P q hq n) hq

theorem rep_mono (P : Cone K) (q : R) (hq : P.nn q) (k : Nat) : ∀ j, Le K P (rep K q k) (rep K q (k + j))
  | 0 => le_refl' K P _
  | j + 1 => le_trans' K P _ _ _ (rep_mono P q hq k j) ⟨q, hq, rfl⟩

theorem le_ex {k N : Nat} (h : k ≤ N) : ∃ j, N = k + j := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => cases ih with
    | intro j hj => exact ⟨j + 1, by rw [hj]; rfl⟩

theorem riccati_inv (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (z k)))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) N) B) :
    ∀ k, k ≤ N → Le K P (z k) (K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k))
  | 0, _ => by
    show Le K P (z 0) (K.add B K.zero)
    rw [add_zero']; exact h0
  | k + 1, hk => by
    have q_nn : P.nn (K.mul δ (K.mul (K.add B B) (K.add B B))) := P.nn_mul _ _ hδ (P.nn_sq _)
    have ih := riccati_inv P z B δ N hz hδ step h0 hN k (Nat.le_of_succ_le hk)
    -- z_k ≤ 2B
    cases le_ex (Nat.le_of_succ_le hk) with
    | intro j hj =>
    have r : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k) B := by
      have m := rep_mono K P _ q_nn k j
      rw [← hj] at m
      exact le_trans' K P _ _ _ m hN
    have z2 : Le K P (z k) (K.add B B) := le_trans' K P _ _ _ ih (le_add K P _ _ _ _ (le_refl' K P B) r)
    have sq := le_mul_left K P δ _ _ hδ (le_sq K P _ _ (hz k) z2)
    refine le_trans' K P _ _ _ (step k) ?_
    have e : K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) (k + 1))
        = K.add (K.add B (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) k))
            (K.mul δ (K.mul (K.add B B) (K.add B B))) := by
      show K.add B (K.add _ _) = _; rw [K.add_assoc]
    rw [e]
    exact le_add K P _ _ _ _ ih sq

/-- **Discrete local existence**: z_k ≤ 2B for every k ≤ N. -/
theorem riccati (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (z k)))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.add B B))) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) := by
  have q_nn : P.nn (K.mul δ (K.mul (K.add B B) (K.add B B))) := P.nn_mul _ _ hδ (P.nn_sq _)
  cases le_ex hk with
  | intro j hj =>
  have m := rep_mono K P _ q_nn k j
  rw [← hj] at m
  exact le_trans' K P _ _ _ (riccati_inv K P z B δ N hz hδ step h0 hN k hk)
    (le_add K P _ _ _ _ (le_refl' K P B) (le_trans' K P _ _ _ m hN))

end Riccati

#print axioms Riccati.le_add
#print axioms Riccati.le_mul_left
#print axioms Riccati.le_sq
#print axioms Riccati.rep_nn
#print axioms Riccati.rep_mono
#print axioms Riccati.le_ex
#print axioms Riccati.riccati_inv
#print axioms Riccati.riccati

79.47 Small data: the comparison that holds for all time

A nonnegative sequence with

\[ z_{k+1}\le z_k-a\,z_k+b\,z_k^2,\qquad b\ge 0,\qquad b\,z_0\le a, \]
in an ordered ring satisfies \(z_{k+1}\le z_k\le z_0\) and \(b\,z_k\le a\) for every \(k\).
\begin{align*} &b z+d=a,\ d\ge 0\ \Rightarrow \ z-az+bz^2+dz=z &&\text {(one step)}\\ &z_{k+1}\le z_k,\ b\ge 0\ \Rightarrow \ b z_{k+1}\le b z_k\le a &&\text {(the threshold is kept)} \end{align*}

Listing 178: ns/SmallData.lean

ns/SmallData.lean

/-
  ns/SmallData.lean — T37 cycle 74: the small-data global comparison (discrete).

  Lean 4 core plus NSChain (Cone, Le) and ns/Riccati (le_add, le_mul_left), lib/RingTac.
  A nonnegative sequence with
      z_{k+1} ≤ z_k − a z_k + b z_k²      (a = hνλ₁ dissipation, b = hC the transfer constant)
  and b z_0 ≤ a is non-increasing for all k:  z_{k+1} ≤ z_k ≤ z_0.
  This is the shape of the 3D small-data global theorem (Z_0 ≤ ν²λ₁/C): once the transfer bound
  Z' ≤ −νλ₁Z + CZ² holds with C uniform in the truncation (R1), every truncation with small enstrophy
  stays small forever.
    small_step    b z ≤ a, z ≥ 0  ⇒  z − a z + b z² ≤ z
    small_global  z_k ≤ z_0 and b z_k ≤ a for every k
-/
import ns.Riccati

namespace SmallData
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R)

theorem small_step (P : Cone K) (z a b : R) (hz : P.nn z) (h : Le K P (K.mul b z) a) :
    Le K P (K.add (sub K z (K.mul a z)) (K.mul b (K.mul z z))) z := by
  cases h with
  | intro d hd =>
    refine ⟨K.mul d z, P.nn_mul _ _ hd.1 hz, ?_⟩
    rw [← hd.2]; lib_ring

theorem small_global (P : Cone K) (z : Nat → R) (a b : R) (hz : ∀ k, P.nn (z k)) (hb : P.nn b)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (sub K (z k) (K.mul a (z k))) (K.mul b (K.mul (z k) (z k)))))
    (h0 : Le K P (K.mul b (z 0)) a) :
    ∀ k, Le K P (z k) (z 0) ∧ Le K P (K.mul b (z k)) a
  | 0 => ⟨le_refl' K P _, h0⟩
  | k + 1 => by
    have ⟨ih1, ih2⟩ := small_global P z a b hz hb step h0 k
    have dec : Le K P (z (k + 1)) (z k) := le_trans' K P _ _ _ (step k) (small_step K P (z k) a b (hz k) ih2)
    exact ⟨le_trans' K P _ _ _ dec ih1,
      le_trans' K P _ _ _ (Riccati.le_mul_left K P b _ _ hb dec) ih2⟩

end SmallData

#print axioms SmallData.small_step
#print axioms SmallData.small_global

Listing 179: ns/Basel.lean

ns/Basel.lean

/-
  ns/Basel.lean — T37 cycle 71: Σ_{n≤N} 1/n² ≤ 2 in every ordered field, and the lattice weight bound.

  Lean 4 core plus ns/LatticeSum (tele_le), NSChain (Cone, Le, sumN) and lib/RingTac. Let u_n = 1/(n+1),
  given by (n+1)·u_n = 1 (no field structure needed: only these elements).
    u_nn        u_n ≥ 0                      (u_n = (n+1)u_n², and n+1 is a sum of ones)
    u_step      u_n − u_{n+1} = u_n u_{n+1}
    sq_le_diff  u_{n+1}² ≤ u_n − u_{n+1}
    basel       Σ_{n<N} u_n² ≤ 2             (Σ_{k=1}^N 1/k² ≤ 2)
    lattice     Σ_{n<N} 26(n+1)²·u_n⁴ ≤ 52   (shell n+1 has ≤ 26(n+1)² points, |k|^{-4} ≤ u_n⁴ there)
-/
import ns.LatticeSum

namespace Basel
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R)

def cN : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (cN n) K.one

theorem cN_nn (P : Cone K) (one_nn : P.nn K.one) : ∀ n, P.nn (cN K n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (cN_nn P one_nn n) one_nn

theorem one_nn (P : Cone K) : P.nn K.one := by
  have e := P.nn_sq K.one; rw [K.one_mul] at e; exact e

theorem u_nn (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    P.nn (u n) := by
  have e : u n = K.mul (cN K (n + 1)) (K.mul (u n) (u n)) := by
    rw [← K.mul_assoc, hu n, K.one_mul]
  rw [e]; exact P.nn_mul _ _ (cN_nn K P (one_nn K P) _) (P.nn_sq _)

theorem u_step (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    sub K (u n) (u (n + 1)) = K.mul (u n) (u (n + 1)) := by
  have h1 := hu n
  have h2 := hu (n + 1)
  have c2 : cN K (n + 2) = K.add (cN K (n + 1)) K.one := rfl
  rw [c2] at h2
  revert h1 h2
  generalize cN K (n + 1) = c
  generalize u n = a
  generalize u (n + 1) = b
  intro h1 h2
  have ea : a = K.mul a (K.mul (K.add c K.one) b) := by rw [h2]; lib_ring
  have eb : b = K.mul b (K.mul c a) := by rw [h1]; lib_ring
  calc sub K a b = sub K (K.mul a (K.mul (K.add c K.one) b)) (K.mul b (K.mul c a)) := by
        rw [← ea, ← eb]
    _ = K.mul a b := by lib_ring

theorem sq_le_diff (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    Le K P (K.mul (u (n + 1)) (u (n + 1))) (sub K (u n) (u (n + 1))) := by
  rw [u_step K u hu n]
  -- u_n u_{n+1} − u_{n+1}² = u_{n+1}(u_n − u_{n+1}) = u_n u_{n+1}²
  refine ⟨K.mul (u n) (K.mul (u (n + 1)) (u (n + 1))), P.nn_mul _ _ (u_nn K P u hu n) (P.nn_sq _), ?_⟩
  have s := u_step K u hu n
  have e : K.mul (u n) (u (n + 1)) = K.add (K.mul (u (n + 1)) (u (n + 1)))
      (K.mul (u (n + 1)) (sub K (u n) (u (n + 1)))) := by lib_ring
  rw [s] at e
  rw [e]; lib_ring

/-- **Σ_{k=1}^N 1/k² ≤ 2.** -/
theorem basel (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (N : Nat) :
    Le K P (sumN K (fun n => K.mul (u n) (u n)) (N + 1)) (K.add K.one K.one) := by
  have u0 : u 0 = K.one := by
    have h := hu 0
    have c1 : cN K 1 = K.one := by show K.add K.zero K.one = K.one; rw [K.zero_add]
    rw [c1, K.one_mul] at h; exact h
  -- Σ_{n<N+1} u_n² = u_0² + Σ_{n<N} u_{n+1}²
  have split : ∀ M, sumN K (fun n => K.mul (u n) (u n)) (M + 1)
      = K.add (K.mul (u 0) (u 0)) (sumN K (fun n => K.mul (u (n + 1)) (u (n + 1))) M) := by
    intro M
    induction M with
    | zero => show K.add K.zero (K.mul (u 0) (u 0)) = K.add (K.mul (u 0) (u 0)) K.zero; lib_ring
    | succ M ih =>
      show K.add (sumN K (fun n => K.mul (u n) (u n)) (M + 1)) (K.mul (u (M + 1)) (u (M + 1)))
        = K.add (K.mul (u 0) (u 0)) (K.add (sumN K (fun n => K.mul (u (n + 1)) (u (n + 1))) M)
            (K.mul (u (M + 1)) (u (M + 1))))
      rw [ih]; lib_ring
  rw [split N]
  have t := LatticeSum.tele_le K P u (fun n => K.mul (u n) (u n)) (u_nn K P u hu) (sq_le_diff K P u hu) N
  rw [u0] at t ⊢
  rw [K.one_mul]
  exact Riccati.le_add K P _ _ _ _ (le_refl' K P _) t

/-- **Lattice weight**: Σ_{n<N+1} 26·(n+1)²·u_n⁴ ≤ 26·2. -/
theorem lattice (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (N : Nat) :
    Le K P (sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) (N + 1))
      (K.mul (cN K 26) (K.add K.one K.one)) := by
  have e : ∀ n, K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) = K.mul (cN K 26) (K.mul (u n) (u n)) := by
    intro n
    have h := hu n
    revert h
    generalize cN K (n + 1) = c
    generalize u n = a
    intro h
    have r : K.mul (K.mul c c) (K.mul (K.mul a a) (K.mul a a)) = K.mul (K.mul (K.mul c a) (K.mul c a)) (K.mul a a) := by
      lib_ring
    rw [r, h]; lib_ring
  rw [sumN_ext K _ _ e, sumN_mul_left]
  exact Riccati.le_mul_left K P _ _ _ (cN_nn K P (one_nn K P) 26) (basel K P u hu N)

end Basel

#print axioms Basel.cN_nn
#print axioms Basel.one_nn
#print axioms Basel.u_nn
#print axioms Basel.u_step
#print axioms Basel.sq_le_diff
#print axioms Basel.basel
#print axioms Basel.lattice

79.48 A Beale–Kato–Majda-type closure

In an ordered field, a nonnegative sequence with

\[ z_{k+1}\le z_k-h\nu p_k+2h\,x_ky_k,\qquad x_k^2\le Kz_k,\qquad y_k^2\le p_k , \]
satisfies \(z_k\le (1+hK/\nu )^k z_0\).
\begin{align*} &2xy\le \nu y^2+x^2/\nu &&\text {(Young)}\\ &z_{k+1}\le z_k-h\nu p_k+h\nu p_k+hKz_k/\nu =(1+hK/\nu )z_k &&\\ &z_k\le (1+hK/\nu )^kz_0 &&\text {(Gr\"onwall)} \end{align*}

Listing 180: ns/BKMDiscrete.lean

ns/BKMDiscrete.lean

/-
  ns/BKMDiscrete.lean — T37 cycle 86: a Beale–Kato–Majda-type discrete closure.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (gronwall_hom, le arithmetic). In an
  ordered field, a nonnegative enstrophy sequence with
      z_{k+1} ≤ z_k − hν p_k + h·2x_k y_k,     x_k² ≤ K z_k,  y_k² ≤ p_k
  (the stretching term written as 2xy with x controlled by a velocity-gradient bound K = ‖∇u‖²_∞-type and
  y² by the palinstrophy p) satisfies
      z_{k+1} ≤ (1 + hK/ν) z_k,   z_k ≤ (1 + hK/ν)^k z_0       (no blowup while K is bounded)
    step_bound   Young 2xy ≤ νy² + x²/ν absorbs the stretching into the dissipation
    bkm          the exponential bound by the shared Grönwall
-/
import lib.OField

namespace BKMDiscrete
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K) (F : OF K P)

theorem step_bound (z z' p x y h nu Kc : R) (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero)
    (step : le K P z' (K.add (sub K z (K.mul h (K.mul nu p))) (K.mul h (K.mul (castZ K 2) (K.mul x y)))))
    (hx : le K P (K.mul x x) (K.mul Kc z)) (hy : le K P (K.mul y y) p) :
    le K P z' (K.mul (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) z) := by
  -- 2xy = 2yx ≤ ν y² + x² ν⁻¹
  have yg := young K P F nu y x hnu hnu0
  have comm : K.mul (castZ K 2) (K.mul x y) = K.mul (castZ K 2) (K.mul y x) := by lib_ring
  have inv_nn : P.nn (F.inv nu) := nn_inv K P F nu hnu hnu0
  -- ν y² ≤ ν p,  x² ν⁻¹ ≤ K z ν⁻¹
  have t1 : le K P (K.mul nu (K.mul y y)) (K.mul nu p) := le_mul_left K P hnu hy
  have t2 : le K P (K.mul (K.mul x x) (F.inv nu)) (K.mul (K.mul Kc z) (F.inv nu)) := by
    have := le_mul_left K P inv_nn hx
    rw [show K.mul (F.inv nu) (K.mul x x) = K.mul (K.mul x x) (F.inv nu) by lib_ring,
      show K.mul (F.inv nu) (K.mul Kc z) = K.mul (K.mul Kc z) (F.inv nu) by lib_ring] at this
    exact this
  have s : le K P (K.mul (castZ K 2) (K.mul x y)) (K.add (K.mul nu p) (K.mul (K.mul Kc z) (F.inv nu))) := by
    rw [comm]; exact le_trans K P yg (le_add2 K P t1 t2)
  have s2 := le_mul_left K P hh s
  have s3 := le_add K P (sub K z (K.mul h (K.mul nu p))) s2
  refine le_trans K P step ?_
  rw [show K.add (sub K z (K.mul h (K.mul nu p))) (K.mul h (K.mul (castZ K 2) (K.mul x y)))
      = K.add (K.mul h (K.mul (castZ K 2) (K.mul x y))) (sub K z (K.mul h (K.mul nu p))) by lib_ring]
  refine le_trans K P s3 ?_
  rw [show K.add (K.mul h (K.add (K.mul nu p) (K.mul (K.mul Kc z) (F.inv nu)))) (sub K z (K.mul h (K.mul nu p)))
      = K.mul (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) z by lib_ring]
  exact le_refl K P _

/-- **BKM-type bound**: z_k ≤ (1 + hK/ν)^k z_0. -/
theorem bkm (z p x y : Nat → R) (h nu Kc : R) (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hK : P.nn Kc)
    (step : ∀ k, le K P (z (k + 1)) (K.add (sub K (z k) (K.mul h (K.mul nu (p k))))
      (K.mul h (K.mul (castZ K 2) (K.mul (x k) (y k))))))
    (hx : ∀ k, le K P (K.mul (x k) (x k)) (K.mul Kc (z k))) (hy : ∀ k, le K P (K.mul (y k) (y k)) (p k)) (n : Nat) :
    le K P (z n) (K.mul (pw K (K.add K.one (K.mul h (K.mul Kc (F.inv nu)))) n) (z 0)) :=
  gronwall_hom K P z _ (P.nn_add _ _ (nn_one K P) (P.nn_mul _ _ hh (P.nn_mul _ _ hK (nn_inv K P F nu hnu hnu0))))
    (fun k => step_bound K P F (z k) (z (k + 1)) (p k) (x k) (y k) h nu Kc hh hnu hnu0 (step k) (hx k) (hy k)) n

end BKMDiscrete

#print axioms BKMDiscrete.step_bound
#print axioms BKMDiscrete.bkm

79.49 Commutator, Biot–Savart and interpolation

For a triad \(p+q+r=0\) in \(R^3\), for a divergence-free Fourier mode (\(k\cdot \hat u=0\)), and for every truncation:

\begin{align*} &|p|^2-|q|^2=(q-p)\cdot r,\qquad (|p|^2-|q|^2)^2\le |r|^2\,(2|p|^2+2|q|^2) &&\text {(commutator gain)}\\ &|k\times \hat u|^2+(k\cdot \hat u)^2=|k|^2|\hat u|^2,\qquad |\hat \omega |^2=|k|^2|\hat u|^2 &&\text {(Biot--Savart)}\\ &\Bigl (\textstyle \sum |k|^2a^2\Bigr )^2\le \Bigl (\sum a^2\Bigr )\Bigl (\sum |k|^4a^2\Bigr ) &&\text {(interpolation)} \end{align*}

Listing 181: ns/Commutator.lean

ns/Commutator.lean

/-
  ns/Commutator.lean — T37 cycle 88: the triad commutator gain.

  Lean 4 core plus NSChain (Cone, Le), ns/TriadBound (le_mul_both) and lib/RingTac. For p, q ∈ R³ and the
  third wave vector r = −(p + q) of the triad, in every ordered commutative ring,
      |p|² − |q|² = (q − p)·r,        (|p|² − |q|²)² ≤ |r|² · (2|p|² + 2|q|²).
  The enstrophy-weight difference across a triad costs one factor |r| instead of |p|²: the commutator
  structure behind the H^s local theory (the low mode r carries the extra derivative).
  Coordinates are written out (the normaliser reads ring expressions, not structure projections).
-/
import ns.TriadBound
import lib.RingTac

namespace Commutator
open DetChain NSChain LibRing

section Ring
variable {R : Type} (K : CRing R)

def d3 (a1 a2 a3 b1 b2 b3 : R) : R := K.add (K.add (K.mul a1 b1) (K.mul a2 b2)) (K.mul a3 b3)

theorem d3_nn (P : Cone K) (a1 a2 a3 : R) : P.nn (d3 K a1 a2 a3 a1 a2 a3) := by
  unfold d3; exact P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

theorem diff_eq (p1 p2 p3 q1 q2 q3 : R) :
    sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3)
      = d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3)
          (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3)) := by
  unfold d3; lib_ring

/-- Cauchy–Schwarz in coordinates (from CFKernel's Lagrange identity, restated). -/
theorem cs3 (P : Cone K) (a1 a2 a3 b1 b2 b3 : R) :
    Le K P (K.mul (d3 K a1 a2 a3 b1 b2 b3) (d3 K a1 a2 a3 b1 b2 b3))
      (K.mul (d3 K a1 a2 a3 a1 a2 a3) (d3 K b1 b2 b3 b1 b2 b3)) :=
  ⟨K.add (K.add (K.mul (sub K (K.mul a2 b3) (K.mul a3 b2)) (sub K (K.mul a2 b3) (K.mul a3 b2)))
      (K.mul (sub K (K.mul a3 b1) (K.mul a1 b3)) (sub K (K.mul a3 b1) (K.mul a1 b3))))
      (K.mul (sub K (K.mul a1 b2) (K.mul a2 b1)) (sub K (K.mul a1 b2) (K.mul a2 b1))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _),
    by unfold d3; lib_ring⟩

/-- **Commutator bound** across a triad. -/
theorem comm_bound (P : Cone K) (p1 p2 p3 q1 q2 q3 : R) :
    Le K P (K.mul (sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3))
              (sub K (d3 K p1 p2 p3 p1 p2 p3) (d3 K q1 q2 q3 q1 q2 q3)))
      (K.mul (d3 K (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3))
                   (K.neg (K.add p1 q1)) (K.neg (K.add p2 q2)) (K.neg (K.add p3 q3)))
        (K.add (K.add (d3 K p1 p2 p3 p1 p2 p3) (d3 K p1 p2 p3 p1 p2 p3))
               (K.add (d3 K q1 q2 q3 q1 q2 q3) (d3 K q1 q2 q3 q1 q2 q3)))) := by
  rw [diff_eq K]
  refine le_trans' K P _ _ _ (cs3 K P _ _ _ _ _ _) ?_
  rw [K.mul_comm (d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3) (sub K q1 p1) (sub K q2 p2) (sub K q3 p3))]
  have pl : Le K P (d3 K (sub K q1 p1) (sub K q2 p2) (sub K q3 p3) (sub K q1 p1) (sub K q2 p2) (sub K q3 p3))
      (K.add (K.add (d3 K p1 p2 p3 p1 p2 p3) (d3 K p1 p2 p3 p1 p2 p3))
             (K.add (d3 K q1 q2 q3 q1 q2 q3) (d3 K q1 q2 q3 q1 q2 q3))) :=
    ⟨d3 K (K.add p1 q1) (K.add p2 q2) (K.add p3 q3) (K.add p1 q1) (K.add p2 q2) (K.add p3 q3),
      d3_nn K P _ _ _, by unfold d3; lib_ring⟩
  exact TriadBound.le_mul_both K P _ _ _ _ (d3_nn K P _ _ _)
    (P.nn_add _ _ (P.nn_add _ _ (d3_nn K P _ _ _) (d3_nn K P _ _ _)) (P.nn_add _ _ (d3_nn K P _ _ _) (d3_nn K P _ _ _)))
    (le_refl' K P _) pl

end Ring

end Commutator

#print axioms Commutator.d3_nn
#print axioms Commutator.diff_eq
#print axioms Commutator.cs3
#print axioms Commutator.comm_bound

Listing 182: ns/BiotSavart.lean

ns/BiotSavart.lean

/-
  ns/BiotSavart.lean — T37 cycle 91: the Fourier Biot–Savart identities.

  Lean 4 core plus ns/Commutator (d3, coordinates) and lib/RingTac. For a Fourier mode û at wave vector k
  with k·û = 0 (incompressibility), ω̂ = k × û:
      |k × û|² + (k·û)² = |k|²|û|²       (Lagrange)          lagrange_c
      |ω̂|² = |k|²|û|²                     (k·û = 0)            vort_norm
  and on the integer lattice (|k|² ≥ 1, i.e. |k|² = 1 + s with s ≥ 0), |ω̂|² ≥ |û|²: enstrophy ≥ energy
  mode by mode (Poincaré on the torus)                                                    poincare_mode
-/
import ns.Commutator

namespace BiotSavart
open DetChain NSChain LibRing Commutator

section Ring
variable {R : Type} (K : CRing R)

def cx (_k1 k2 k3 _u1 u2 u3 : R) : R := sub K (K.mul k2 u3) (K.mul k3 u2)
def cy (k1 _k2 k3 u1 _u2 u3 : R) : R := sub K (K.mul k3 u1) (K.mul k1 u3)
def cz (k1 k2 _k3 u1 u2 _u3 : R) : R := sub K (K.mul k1 u2) (K.mul k2 u1)

theorem lagrange_c (k1 k2 k3 u1 u2 u3 : R) :
    K.add (d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
              (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3))
      (K.mul (d3 K k1 k2 k3 u1 u2 u3) (d3 K k1 k2 k3 u1 u2 u3))
      = K.mul (d3 K k1 k2 k3 k1 k2 k3) (d3 K u1 u2 u3 u1 u2 u3) := by
  unfold d3 cx cy cz; lib_ring

/-- **|ω̂|² = |k|²|û|²** for divergence-free û. -/
theorem vort_norm (k1 k2 k3 u1 u2 u3 : R) (div : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
        (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
      = K.mul (d3 K k1 k2 k3 k1 k2 k3) (d3 K u1 u2 u3 u1 u2 u3) := by
  have l := lagrange_c K k1 k2 k3 u1 u2 u3
  rw [div] at l
  rw [← l]; lib_ring

/-- **Poincaré, one mode**: |k|² = 1 + s, s ≥ 0 ⇒ |û|² ≤ |ω̂|². -/
theorem poincare_mode (P : Cone K) (k1 k2 k3 u1 u2 u3 s : R) (hs : P.nn s)
    (hk : d3 K k1 k2 k3 k1 k2 k3 = K.add K.one s) (div : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    Le K P (d3 K u1 u2 u3 u1 u2 u3)
      (d3 K (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)
        (cx K k1 k2 k3 u1 u2 u3) (cy K k1 k2 k3 u1 u2 u3) (cz K k1 k2 k3 u1 u2 u3)) := by
  rw [vort_norm K k1 k2 k3 u1 u2 u3 div, hk]
  exact ⟨K.mul s (d3 K u1 u2 u3 u1 u2 u3), P.nn_mul _ _ hs (d3_nn K P _ _ _), by lib_ring⟩

end Ring

end BiotSavart

#print axioms BiotSavart.lagrange_c
#print axioms BiotSavart.vort_norm
#print axioms BiotSavart.poincare_mode

Listing 183: ns/Interp.lean

ns/Interp.lean

/-
  ns/Interp.lean — T37 cycle 94: spectral interpolation Z² ≤ E·P for every truncation.

  Lean 4 core plus ns/SumCS (cs_sum) and lib/RingTac. For modes i ∈ l with amplitudes a_i and weights
  κ_i = |k_i|²:
      E = Σ a²,   Z = Σ κ a²,   P = Σ κ² a²       ⇒      Z² ≤ E · P
  (the discrete form of ‖∇u‖² ≤ ‖u‖·‖Δu‖, used to close enstrophy estimates between the energy and the
  dissipation; uniform in the truncation).
-/
import ns.SumCS

namespace Interp
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem interp (P : Cone K) (a κ : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (κ i) (K.mul (a i) (a i))) l))
      (K.mul (lsum K (fun i => K.mul (a i) (a i)) l) (lsum K (fun i => K.mul (K.mul (κ i) (κ i)) (K.mul (a i) (a i))) l)) := by
  have cs := cs_sum K P a (fun i => K.mul (κ i) (a i)) l
  have e1 : lsum K (fun i => K.mul (a i) ((fun i => K.mul (κ i) (a i)) i)) l
      = lsum K (fun i => K.mul (κ i) (K.mul (a i) (a i))) l :=
    lsum_ext K _ _ (fun i => by show K.mul (a i) (K.mul (κ i) (a i)) = _; lib_ring) l
  have e2 : lsum K (fun i => sq K (a i)) l = lsum K (fun i => K.mul (a i) (a i)) l := rfl
  have e3 : lsum K (fun i => sq K ((fun i => K.mul (κ i) (a i)) i)) l
      = lsum K (fun i => K.mul (K.mul (κ i) (κ i)) (K.mul (a i) (a i))) l :=
    lsum_ext K _ _ (fun i => by show sq K (K.mul (κ i) (a i)) = _; unfold sq; lib_ring) l
  rw [e1, e2, e3] at cs
  exact cs

end Interp

#print axioms Interp.interp

79.50 A Sobolev embedding uniform in the truncation

Group the modes of a three-dimensional truncation in cube shells \(|k|_\infty =n+1\); shell \(n\) has at most \(26(n+1)^2\) modes and \(|k|^{-4}\le (n+1)^{-4}\) there. Then

\begin{align*} &\textstyle \sum _{\text {modes}}|k|^{-4}\le \sum _n26(n+1)^2(n+1)^{-4}=26\sum _n(n+1)^{-2}\le 52 ,\\ &\textstyle \bigl (\sum _k|\hat u_k|\bigr )^2\le \bigl (\sum _k|k|^{-4}\bigr )\bigl (\sum _k|k|^4|\hat u_k|^2\bigr )\le 52\,\|\Delta u\|^2 , \end{align*}

so \(\|u\|_\infty \le \sqrt {52}\,\|\Delta u\|\) for every truncation.

Listing 184: ns/Sobolev.lean

ns/Sobolev.lean

/-
  ns/Sobolev.lean — T37 cycle 97: the discrete Sobolev embedding H² ⊂ L^∞ (Fourier side) in 3D, with a
  constant independent of the truncation.

  Lean 4 core plus ns/Basel (cN, lattice), ns/WeightedCS (weighted_cs), ns/TriadSum (lsum_le, lsum_nn).
  Modes are grouped in cube shells: shell n (|k|∞ = n + 1) is a list of at most 26(n+1)² modes, each with
  weight ω_i = |k_i|^{-4} ≤ u_n⁴ (u_n = 1/(n+1)). Then
      Σ_{n≤N} Σ_{i∈shell n} ω_i ≤ 52                                       shell_weight
      (Σ_i x_i y_i)² ≤ W · Σ_i β_i   when x_i² ≤ ω_i, y_i² ≤ β_i, Σω ≤ W            sobolev
  (with |a_i| = |k_i|^{-2}·|k_i|²|a_i|, β = |k|⁴a² and W = 52 from shell_weight)
  i.e. ‖u‖_∞ ≤ Σ|û_k| ≤ √52 ‖Δu‖ for every truncation (the shell count is LatticeSum.shell_count).
-/
import ns.Basel
import ns.WeightedCS

namespace Sobolev
open DetChain NSChain LibRing Basel TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem cN_add (a : Nat) : ∀ b, cN K (a + b) = K.add (cN K a) (cN K b)
  | 0 => by show cN K a = K.add (cN K a) K.zero; lib_ring
  | b + 1 => by
    show K.add (cN K (a + b)) K.one = K.add (cN K a) (K.add (cN K b) K.one)
    rw [cN_add a b]; lib_ring

theorem cN_mul (a : Nat) : ∀ b, cN K (a * b) = K.mul (cN K a) (cN K b)
  | 0 => by show K.zero = K.mul (cN K a) K.zero; lib_ring
  | b + 1 => by
    show cN K (a * b + a) = K.mul (cN K a) (K.add (cN K b) K.one)
    rw [cN_add, cN_mul a b]; lib_ring

theorem cN_le (P : Cone K) (a b : Nat) (h : a ≤ b) : Le K P (cN K a) (cN K b) := by
  have ⟨c, hc⟩ := Nat.le.dest h
  exact ⟨cN K c, cN_nn K P (Basel.one_nn K P) c, by rw [← hc, cN_add]⟩

/-- Σ over a list ≤ length · bound. -/
theorem lsum_le_len (P : Cone K) (f : I → R) (b : R) (hb : P.nn b) :
    ∀ l : List I, (∀ i, List.Mem i l → Le K P (f i) b) → Le K P (lsum K f l) (K.mul (cN K l.length) b)
  | [], _ => ⟨K.zero, P.nn_zero, by show K.add K.zero K.zero = K.mul K.zero b; lib_ring⟩
  | i :: l, h => by
    have ih := lsum_le_len P f b hb l (fun j hj => h j (List.Mem.tail i hj))
    have hi := h i (List.Mem.head l)
    have s := Riccati.le_add K P _ _ _ _ hi ih
    refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
    show K.add (K.add b (K.mul (cN K l.length) b)) K.zero = K.mul (K.add (cN K l.length) K.one) b
    lib_ring

/-- **Shell weight**: Σ_{n≤N} Σ_{i∈shell n} ω_i ≤ 52. -/
theorem shell_weight (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (N : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun n => lsum K ω (shell n)) (N + 1)) (K.mul (cN K 26) (K.add K.one K.one)) := by
  have u4 : ∀ n, P.nn (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))) := fun n => P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)
  have step : ∀ n, Le K P (lsum K ω (shell n))
      (K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) := by
    intro n
    refine le_trans' K P _ _ _ (lsum_le_len K P ω _ (u4 n) (shell n) (hw n)) ?_
    have c := cN_le K P _ _ (hlen n)
    rw [cN_mul, cN_mul] at c
    have := Riccati.le_mul_left K P _ _ _ (u4 n) c
    refine le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ (le_trans' K P _ _ _ this ⟨K.zero, P.nn_zero, by lib_ring⟩)
  have tot : ∀ M, Le K P (sumN K (fun n => lsum K ω (shell n)) M)
      (sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1)))
        (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))) M) := by
    intro M
    induction M with
    | zero => exact le_refl' K P _
    | succ M ih => exact Riccati.le_add K P _ _ _ _ ih (step M)
  exact le_trans' K P _ _ _ (tot (N + 1)) (lattice K P u hu N)

/-- **Sobolev, Fourier side**: (Σ x_i y_i)² ≤ W·Σβ when x_i² ≤ ω_i, y_i² ≤ β_i and Σω ≤ W. -/
theorem sobolev (P : Cone K) (x y ω β : I → R) (l : List I) (W : R)
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i))
    (hβ : ∀ i, P.nn (β i)) (hW : Le K P (lsum K ω l) W) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) l)) (K.mul W (lsum K β l)) := by
  have w := WeightedCS.weighted_cs K P x y ω β l hx hy hβ
  have m := Riccati.le_mul_left K P _ _ _ (lsum_nn K P β hβ l) hW
  rw [K.mul_comm (lsum K β l), K.mul_comm (lsum K β l)] at m
  exact le_trans' K P _ _ _ w m

end Sobolev

#print axioms Sobolev.cN_add
#print axioms Sobolev.cN_mul
#print axioms Sobolev.cN_le
#print axioms Sobolev.lsum_le_len
#print axioms Sobolev.shell_weight
#print axioms Sobolev.sobolev

79.51 Leray projector, weighted Cauchy–Schwarz, discrete Young

For a mode \(k\) with \(\kappa |k|^2=1\), \(Pu=u-\kappa (k\cdot u)k\) satisfies \(k\cdot Pu=0\), \(P(Pu)=Pu\), and \(Pu=u\) when \(k\cdot u=0\). For weights \(a\ge 0\) and every truncation,

\begin{align*} &\textstyle \sum _p\sum _qa_pa_q(X_p-X_q)^2+2\bigl (\sum aX\bigr )^2=2\bigl (\sum a\bigr )\bigl (\sum aX^2\bigr ) ,\\ &\textstyle 2\Bigl (\sum _pa_p\sum _qb_q\,c(\sigma _pq)\Bigr )^2\le 2\bigl (\sum a\bigr )^2\bigl (\sum b^2\bigr )C\qquad \bigl (\sum _qc(\sigma _pq)^2\le C\bigr ), \end{align*}

the discrete Young inequality \(|\sum _{p+q+r=0}a_pb_qc_r|\le \|a\|_1\|b\|_2\|c\|_2\).

Listing 185: ns/Leray.lean

ns/Leray.lean

/-
  ns/Leray.lean — T37 cycle 105: the Leray projector in Fourier space.

  Lean 4 core plus ns/Commutator (d3) and lib/RingTac. For a mode k ≠ 0 with κ·|k|² = 1 (κ = 1/|k|²),
      P u = u − κ (k·u) k
    div_free     k · P u = 0
    idem         P (P u) = P u                 (OneMove: reading then placing is idempotent)
    fixes        k · u = 0  ⇒  P u = u          (placing a divergence-free field then reading is the identity)
  Every Galerkin truncation applies P mode by mode, so it keeps fields divergence-free.
-/
import ns.Commutator

namespace Leray
open DetChain LibRing Commutator

section Ring
variable {R : Type} (K : CRing R)

def P1 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u1 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k1)
def P2 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u2 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k2)
def P3 (κ k1 k2 k3 u1 u2 u3 : R) : R := sub K u3 (K.mul (K.mul κ (d3 K k1 k2 k3 u1 u2 u3)) k3)

theorem div_free (κ k1 k2 k3 u1 u2 u3 : R) (hκ : K.mul κ (d3 K k1 k2 k3 k1 k2 k3) = K.one) :
    d3 K k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3) = K.zero := by
  have e : d3 K k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
      = K.mul (d3 K k1 k2 k3 u1 u2 u3) (sub K K.one (K.mul κ (d3 K k1 k2 k3 k1 k2 k3))) := by
    unfold P1 P2 P3 d3; lib_ring
  rw [e, hκ]; lib_ring

theorem fixes (κ k1 k2 k3 u1 u2 u3 : R) (h : d3 K k1 k2 k3 u1 u2 u3 = K.zero) :
    P1 K κ k1 k2 k3 u1 u2 u3 = u1 ∧ P2 K κ k1 k2 k3 u1 u2 u3 = u2 ∧ P3 K κ k1 k2 k3 u1 u2 u3 = u3 := by
  unfold P1 P2 P3; rw [h]
  exact ⟨by lib_ring, by lib_ring, by lib_ring⟩

theorem idem (κ k1 k2 k3 u1 u2 u3 : R) (hκ : K.mul κ (d3 K k1 k2 k3 k1 k2 k3) = K.one) :
    P1 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P1 K κ k1 k2 k3 u1 u2 u3 ∧
      P2 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P2 K κ k1 k2 k3 u1 u2 u3 ∧
      P3 K κ k1 k2 k3 (P1 K κ k1 k2 k3 u1 u2 u3) (P2 K κ k1 k2 k3 u1 u2 u3) (P3 K κ k1 k2 k3 u1 u2 u3)
        = P3 K κ k1 k2 k3 u1 u2 u3 :=
  fixes K κ k1 k2 k3 _ _ _ (div_free K κ k1 k2 k3 u1 u2 u3 hκ)

end Ring

end Leray

#print axioms Leray.div_free
#print axioms Leray.fixes
#print axioms Leray.idem

Listing 186: ns/WeightedJensen.lean

ns/WeightedJensen.lean

/-
  ns/WeightedJensen.lean — T37 cycle 108: weighted Cauchy–Schwarz without square roots.

  Lean 4 core plus NSChain (lsum, Cone, Le) and lib/RingTac. For weights a ≥ 0 and values X on a list l:
      Σ_p Σ_q a_p a_q (X_p − X_q)² + 2(ΣaX)² = 2(Σa)(ΣaX²)                     double_identity
      (ΣaX)² ≤ (Σa)(ΣaX²)            (in rings where 2x = 0 ⇒ x = 0 fails, stated doubled)   wcs2
  This is the step that turns a per-p bound X_p² ≤ BC into (Σ_p a_p X_p)² ≤ (Σa)² BC: the discrete
  Young inequality Σ_{p+q+r=0} a_p b_q c_r ≤ ‖a‖₁‖b‖₂‖c‖₂ for the triad sums of a truncation.
-/
import NSChain
import lib.RingTac

namespace WeightedJensen
open DetChain NSChain LibRing

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_lsum (f g : I → R) (l m : List I) :
    lsum K (fun p => lsum K (fun q => K.mul (f p) (g q)) m) l = K.mul (lsum K f l) (lsum K g m) := by
  have e : ∀ p, lsum K (fun q => K.mul (f p) (g q)) m = K.mul (f p) (lsum K g m) :=
    fun p => lsum_mul_left K (f p) g m
  rw [lsum_ext K _ _ e l]
  induction l with
  | nil => show K.zero = K.mul K.zero _; lib_ring
  | cons x l ih =>
    show K.add (K.mul (f x) (lsum K g m)) (lsum K (fun p => K.mul (f p) (lsum K g m)) l)
      = K.mul (K.add (f x) (lsum K f l)) (lsum K g m)
    rw [ih]; lib_ring

theorem double_identity (a X : I → R) (l : List I) :
    K.add (lsum K (fun p => lsum K (fun q => K.mul (K.mul (a p) (a q))
        (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))) l) l)
      (K.add (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l))
             (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l)))
      = K.add (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))
              (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l)) := by
  -- expand the inner term into three product sums
  have e : ∀ p q, K.mul (K.mul (a p) (a q)) (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))
      = K.add (K.add (K.mul (K.mul (a p) (K.mul (X p) (X p))) (a q)) (K.mul (a p) (K.mul (a q) (K.mul (X q) (X q)))))
          (K.neg (K.add (K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) (K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))))) := by
    intro p q; lib_ring
  have inner : ∀ p, lsum K (fun q => K.mul (K.mul (a p) (a q)) (K.mul (sub K (X p) (X q)) (sub K (X p) (X q)))) l
      = K.add (K.add (lsum K (fun q => K.mul (K.mul (a p) (K.mul (X p) (X p))) (a q)) l)
                     (lsum K (fun q => K.mul (a p) (K.mul (a q) (K.mul (X q) (X q)))) l))
          (K.neg (K.add (lsum K (fun q => K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) l)
                        (lsum K (fun q => K.mul (K.mul (a p) (X p)) (K.mul (a q) (X q))) l))) := by
    intro p
    rw [lsum_ext K _ _ (e p) l, lsum_add, lsum_add, lsum_neg, lsum_add]
  rw [lsum_ext K _ _ inner l, lsum_add, lsum_add, lsum_neg, lsum_add,
    lsum_lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) a l l,
    lsum_lsum K a (fun q => K.mul (a q) (K.mul (X q) (X q))) l l,
    lsum_lsum K (fun p => K.mul (a p) (X p)) (fun q => K.mul (a q) (X q)) l l]
  generalize lsum K a l = A
  generalize lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l = B
  generalize lsum K (fun p => K.mul (a p) (X p)) l = C
  lib_ring

/-- **Weighted Cauchy–Schwarz, doubled**: 2(ΣaX)² ≤ 2(Σa)(ΣaX²) for a ≥ 0. -/
theorem wcs2 (P : Cone K) (a X : I → R) (l : List I) (ha : ∀ p, P.nn (a p)) :
    Le K P (K.add (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l))
                  (K.mul (lsum K (fun p => K.mul (a p) (X p)) l) (lsum K (fun p => K.mul (a p) (X p)) l)))
      (K.add (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))
             (K.mul (lsum K a l) (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l))) := by
  have nn : ∀ m : List I, ∀ f : I → R, (∀ p, P.nn (f p)) → P.nn (lsum K f m) := by
    intro m f hf; induction m with
    | nil => exact P.nn_zero
    | cons x m ih => exact P.nn_add _ _ (hf x) ih
  refine ⟨_, nn l _ (fun p => nn l _ (fun q => P.nn_mul _ _ (P.nn_mul _ _ (ha p) (ha q)) (P.nn_sq (sub K (X p) (X q))))), ?_⟩
  rw [K.add_comm]; exact double_identity K a X l

end WeightedJensen

#print axioms WeightedJensen.lsum_lsum
#print axioms WeightedJensen.double_identity
#print axioms WeightedJensen.wcs2

Listing 187: ns/DiscreteYoung.lean

ns/DiscreteYoung.lean

/-
  ns/DiscreteYoung.lean — T37 cycle 109: the discrete Young inequality for triad sums of a truncation.

  Lean 4 core plus ns/SumCS (cs_sum), ns/WeightedJensen (wcs2), ns/TriadSum (lsum_le, lsum_nn),
  ns/Riccati (le_mul_left), lib/RingTac. For the modes l, weights a ≥ 0, and for every p a map σ_p (the
  third mode of the triad (p, q, σ_p q)) whose shifted squares are bounded, Σ_q c(σ_p q)² ≤ C:
      2 (Σ_p a_p Σ_q b_q c(σ_p q))² ≤ 2 (Σ a)² · (Σ b²) · C
  i.e. |Σ_{p+q+r=0} a_p b_q c_r| ≤ ‖a‖₁ ‖b‖₂ ‖c‖₂ (C = ‖c‖₂² when σ_p is injective on the truncation).
-/
import ns.SumCS
import ns.WeightedJensen
import ns.TriadSum

namespace DiscreteYoung
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem young (P : Cone K) (a b : I → R) (c : I → R) (σ : I → I → I) (l : List I) (C : R)
    (ha : ∀ p, P.nn (a p)) (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) :
    Le K P (K.add (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
                  (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l)))
      (K.add (K.mul (K.mul (lsum K a l) (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))
             (K.mul (K.mul (lsum K a l) (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))) := by
  let X : I → R := fun p => lsum K (fun q => K.mul (b q) (c (σ p q))) l
  -- inner: X_p² ≤ (Σb²) C
  have bn : P.nn (lsum K (fun q => sq K (b q)) l) := lsum_nn K P _ (fun q => P.nn_sq _) l
  have inner : ∀ p, Le K P (K.mul (X p) (X p)) (K.mul (lsum K (fun q => sq K (b q)) l) C) := fun p =>
    le_trans' K P _ _ _ (cs_sum K P b (fun q => c (σ p q)) l) (Riccati.le_mul_left K P _ _ _ bn (hC p))
  -- outer: 2(ΣaX)² ≤ 2(Σa)(ΣaX²) ≤ 2(Σa)(Σa)(Σb²)C
  have w := WeightedJensen.wcs2 K P a X l ha
  have s2 : Le K P (lsum K (fun p => K.mul (a p) (K.mul (X p) (X p))) l)
      (K.mul (lsum K a l) (K.mul (lsum K (fun q => sq K (b q)) l) C)) := by
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C))
      (fun p => Riccati.le_mul_left K P _ _ _ (ha p) (inner p)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [show K.add (lsum K (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C)) l) K.zero
        = lsum K (fun p => K.mul (a p) (K.mul (lsum K (fun q => sq K (b q)) l) C)) l by lib_ring]
    generalize K.mul (lsum K (fun q => sq K (b q)) l) C = BC
    rw [lsum_ext K _ (fun p => K.mul BC (a p)) (fun p => K.mul_comm _ _) l, lsum_mul_left, K.mul_comm]
  have an : P.nn (lsum K a l) := lsum_nn K P a ha l
  have m := Riccati.le_mul_left K P _ _ _ an s2
  refine le_trans' K P _ _ _ w (Riccati.le_add K P _ _ _ _ ?_ ?_) <;>
  · refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by lib_ring⟩

end DiscreteYoung

#print axioms DiscreteYoung.young

79.52 The \(H^2\) triad bound and the Riccati comparison of exponent \(3/2\)

For every three-dimensional truncation,

\[ \Bigl |\sum _{p+q+r=0}|\hat u_p|\,b_q\,c_r\Bigr |^2\le 52\,\|\Delta u\|^2\,\|b\|^2\,\|c\|^2 , \]
and in an ordered field a nonnegative sequence with \(z_{k+1}\le z_k+y_k\), \(y_k^2\le \delta ^2z_k^3\) stays below \(2B\) for \(N\) steps when \(z_0\le B\) and \(NY\le B\), \(Y^2=\delta ^2(2B)^3\), \(Y\ge 0\) (using \(0\le Y\), \(y^2\le Y^2\Rightarrow y\le Y\)).

Listing 188: ns/TriadH2.lean

ns/TriadH2.lean

/-
  ns/TriadH2.lean — T37 cycle 111: every triad sum of a truncation bounded by H² × L² × L², constant 52.

  Lean 4 core plus ns/DiscreteYoung (young), ns/Sobolev (sobolev), ns/TriadSum, ns/Riccati.
  With a_p = x_p y_p ≥ 0 (x_p² ≤ ω_p = |k_p|^{-4}, y_p² ≤ β_p = |k_p|⁴|û_p|²), Σω ≤ W (= 52 by shell_weight):
      2 (Σ_p a_p Σ_q b_q c(σ_p q))² ≤ 2 · W · (Σβ) · (Σb²) · C
  i.e. |Σ_{p+q+r=0} |û_p| b_q c_r|² ≤ 52 ‖Δu‖² ‖b‖² ‖c‖² for every truncation.
-/
import ns.DiscreteYoung
import ns.Sobolev

namespace TriadH2
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem triad_h2 (P : Cone K) (x y ω β b c : I → R) (σ : I → I → I) (l : List I) (W C : R)
    (hxy : ∀ p, P.nn (K.mul (x p) (y p)))
    (hx : ∀ i, Le K P (sq K (x i)) (ω i)) (hy : ∀ i, Le K P (sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hW : Le K P (lsum K ω l) W) (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) (hCn : P.nn C) :
    Le K P (K.add (sq K (lsum K (fun p => K.mul (K.mul (x p) (y p)) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
                  (sq K (lsum K (fun p => K.mul (K.mul (x p) (y p)) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l)))
      (K.add (K.mul (K.mul W (lsum K β l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))
             (K.mul (K.mul W (lsum K β l)) (K.mul (lsum K (fun q => sq K (b q)) l) C))) := by
  have y1 := DiscreteYoung.young K P (fun p => K.mul (x p) (y p)) b c σ l C hxy hC
  have s := Sobolev.sobolev K P x y ω β l W hx hy hβ hW
  have bn : P.nn (K.mul (lsum K (fun q => sq K (b q)) l) C) :=
    P.nn_mul _ _ (lsum_nn K P _ (fun q => P.nn_sq _) l) hCn
  have m := Riccati.le_mul_left K P _ _ _ bn s
  rw [K.mul_comm (K.mul (lsum K (fun q => sq K (b q)) l) C),
    K.mul_comm (K.mul (lsum K (fun q => sq K (b q)) l) C)] at m
  exact le_trans' K P _ _ _ y1 (Riccati.le_add K P _ _ _ _ m m)

end TriadH2

#print axioms TriadH2.triad_h2

Listing 189: ns/Riccati32.lean

ns/Riccati32.lean

/-
  ns/Riccati32.lean — T37 cycle 114: the discrete Riccati comparison with exponent 3/2 (square-root free).

  Lean 4 core plus the shared lib/OField (ordered field: totality, antisymmetry, inverses), lib/Gronwall
  (le arithmetic). [DecidableEq R] is a hypothesis on R (no classical logic).
    sq_le_mono   0 ≤ Y, y² ≤ Y²  ⇒  y ≤ Y
    riccati32    z_{k+1} ≤ z_k + y_k,  y_k² ≤ δ² z_k³,  z ≥ 0,  z_0 ≤ B,  Y ≥ 0, Y² = δ²(2B)³,  N·Y ≤ B
                 ⇒  z_k ≤ 2B for k ≤ N
  The H²-type local existence time N h ≈ B/(δ(2B)^{3/2}) for Z' ≤ C Z^{3/2} (Z = ‖Δu‖², with TriadH2).
-/
import lib.OField

namespace Riccati32
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K)

theorem sq_le_mono [DecidableEq R] (F : OF K P) (y Y : R) (hY : P.nn Y) (h : le K P (K.mul y y) (K.mul Y Y)) : le K P y Y := by
  unfold le at *
  cases F.total (sub K Y y) with
  | inl hh => exact hh
  | inr hh =>
    -- d = y − Y ≥ 0
    have hd : P.nn (sub K y Y) := by rw [show sub K y Y = K.neg (sub K Y y) by lib_ring]; exact hh
    -- d(y + Y) = y² − Y² ≥ 0 and ≤ 0  ⇒ = 0
    have hyY : P.nn (K.add y Y) := by
      rw [show K.add y Y = K.add (sub K y Y) (K.add Y Y) by lib_ring]
      exact P.nn_add _ _ hd (P.nn_add _ _ hY hY)
    have p1 : P.nn (sub K (K.mul y y) (K.mul Y Y)) := by
      rw [show sub K (K.mul y y) (K.mul Y Y) = K.mul (sub K y Y) (K.add y Y) by lib_ring]
      exact P.nn_mul _ _ hd hyY
    have z := F.antisymm _ p1 (by rw [show K.neg (sub K (K.mul y y) (K.mul Y Y)) = sub K (K.mul Y Y) (K.mul y y) by lib_ring]; exact h)
    by_cases hs : K.add y Y = K.zero
    · -- y = −Y: Y − y = Y + Y ≥ 0
      rw [show sub K Y y = K.add (K.add Y Y) (K.neg (K.add y Y)) by lib_ring, hs,
        show K.add (K.add Y Y) (K.neg K.zero) = K.add Y Y by lib_ring]
      exact P.nn_add _ _ hY hY
    · -- d = (y² − Y²)·(y + Y)⁻¹ = 0
      have hi := F.mul_inv _ hs
      have d0 : sub K y Y = K.zero := by
        rw [show sub K y Y = K.mul (K.mul (sub K y Y) (K.add y Y)) (F.inv (K.add y Y)) by
          rw [K.mul_assoc, hi]; lib_ring]
        rw [show K.mul (sub K y Y) (K.add y Y) = sub K (K.mul y y) (K.mul Y Y) by lib_ring, z]; lib_ring
      rw [show sub K Y y = K.neg (sub K y Y) by lib_ring, d0, show K.neg K.zero = K.zero by lib_ring]
      exact P.nn_zero

theorem castN_add (a : Nat) : ∀ b, castN K (a + b) = K.add (castN K a) (castN K b)
  | 0 => by show castN K a = K.add (castN K a) K.zero; lib_ring
  | b + 1 => by
    show K.add K.one (castN K (a + b)) = K.add (castN K a) (K.add K.one (castN K b))
    rw [castN_add a b]; lib_ring

theorem le_mul2 {a b c d : R} (ha : P.nn a) (hc : P.nn c) (h1 : le K P a b) (h2 : le K P c d) :
    le K P (K.mul a c) (K.mul b d) := by
  unfold le at *
  rw [show sub K (K.mul b d) (K.mul a c) = K.add (K.mul (sub K b a) (K.add (sub K d c) c)) (K.mul a (sub K d c)) by lib_ring]
  exact P.nn_add _ _ (P.nn_mul _ _ h1 (by
    rw [show K.add (sub K d c) c = d by lib_ring]
    have : P.nn (K.add (sub K d c) c) := P.nn_add _ _ h2 hc
    rw [show K.add (sub K d c) c = d by lib_ring] at this; exact this)) (P.nn_mul _ _ ha h2)

theorem castN_le (Y : R) (hY : P.nn Y) {k N : Nat} (h : k ≤ N) :
    le K P (K.mul (castN K k) Y) (K.mul (castN K N) Y) := by
  have ⟨j, hj⟩ := Nat.le.dest h
  unfold le
  rw [← hj, show sub K (K.mul (castN K (k + j)) Y) (K.mul (castN K k) Y) = K.mul (castN K j) Y by
    rw [castN_add K k j]; lib_ring]
  exact P.nn_mul _ _ (nn_castN K P j) hY

/-- **Riccati with exponent 3/2.** -/
theorem riccati32 [DecidableEq R] (F : OF K P) (z y : Nat → R) (D B Y : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hD : P.nn D) (hY : P.nn Y) (hB : P.nn B)
    (step : ∀ k, le K P (z (k + 1)) (K.add (z k) (y k)))
    (hy : ∀ k, le K P (K.mul (y k) (y k)) (K.mul D (K.mul (z k) (K.mul (z k) (z k)))))
    (hYdef : K.mul Y Y = K.mul D (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B))))
    (h0 : le K P (z 0) B) (hN : le K P (K.mul (castN K N) Y) B) :
    ∀ k, k ≤ N → le K P (z k) (K.add B (K.mul (castN K k) Y)) := by
  intro k
  induction k with
  | zero => intro _; rw [show K.add B (K.mul (castN K 0) Y) = B by show K.add B (K.mul K.zero Y) = B; lib_ring]; exact h0
  | succ k ih =>
    intro hk
    have ihk := ih (Nat.le_of_succ_le hk)
    -- z_k ≤ 2B
    have z2 : le K P (z k) (K.add B B) := le_trans K P ihk (le_trans K P (by
        unfold le; rw [show sub K (K.add B (K.mul (castN K N) Y)) (K.add B (K.mul (castN K k) Y))
          = sub K (K.mul (castN K N) Y) (K.mul (castN K k) Y) by lib_ring]
        exact castN_le K P Y hY (Nat.le_of_succ_le hk)) (by
        unfold le at hN ⊢; rw [show sub K (K.add B B) (K.add B (K.mul (castN K N) Y)) = sub K B (K.mul (castN K N) Y) by lib_ring]
        exact hN))
    have B2 : P.nn (K.add B B) := P.nn_add _ _ hB hB
    have c3 : le K P (K.mul D (K.mul (z k) (K.mul (z k) (z k))))
        (K.mul D (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B)))) :=
      le_mul_left K P hD (le_mul2 K P (hz k) (P.nn_mul _ _ (hz k) (hz k)) z2
        (le_mul2 K P (hz k) (hz k) z2 z2))
    have yY : le K P (y k) Y := sq_le_mono K P F (y k) Y hY (by rw [hYdef]; exact le_trans K P (hy k) c3)
    refine le_trans K P (step k) ?_
    have := le_add2 K P ihk yY
    refine le_trans K P this ?_
    rw [show K.add (K.add B (K.mul (castN K k) Y)) Y = K.add B (K.mul (castN K (k + 1)) Y) by
      show _ = K.add B (K.mul (K.add K.one (castN K k)) Y); lib_ring]
    exact le_refl K P _

end Riccati32

#print axioms Riccati32.sq_le_mono
#print axioms Riccati32.castN_add
#print axioms Riccati32.le_mul2
#print axioms Riccati32.castN_le
#print axioms Riccati32.riccati32

79.53 Forcing, and epochs of small dissipation

For a Crank–Nicolson step with force \(f\) on a lattice truncation (\(|k|^2=1+\kappa \ge 1\)) and an energy-neutral nonlinearity,

\[ E'+h\nu \textstyle \sum ks^2=E+h\sum sf,\qquad 2E'+h\nu \sum ks^2\le 2E+\frac h\nu \sum f^2 , \]
and along a run \(2E_T+h\nu \sum _tD_t\le 2E_0+\frac h\nu \sum _tF_t\). If \(d_t\ge 0\) and \(\sum _{t\le T}d_t\le S\), then some \(t\le T\) has \((T+1)d_t\le S\) (the minimum exists constructively from totality).

Listing 190: ns/ForcedLeray.lean

ns/ForcedLeray.lean

/-
  ns/ForcedLeray.lean — T37 cycle 120: the energy inequality with forcing, one Crank–Nicolson step.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (le), NSChain (lsum), lib/RingTac.
  Modes i ∈ l, a′_i − a_i = h(−ν k_i s_i + N_i + f_i), s = a′ + a, Σ s N = 0 (energy-neutral nonlinearity),
  k_i = 1 + κ_i with κ_i ≥ 0 (lattice: |k|² ≥ 1), h, ν ≥ 0, ν ≠ 0:
    step_identity   E′ + hν Σ k s² = E + h Σ s f
    forced_step     2E′ + hν Σ k s² ≤ 2E + (h/ν) Σ f²           (Young with ε = ν)
-/
import lib.OField
import NSChain

namespace ForcedLeray
open DetChain NSChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) {I : Type}

theorem step_identity (a a' k Nn f : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (K.add (a' i) (a i)) (Nn i)) l = K.zero) :
    K.add (lsum K (fun i => K.mul (a' i) (a' i)) l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
      = K.add (lsum K (fun i => K.mul (a i) (a i)) l)
          (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l)) := by
  -- per mode: a′² + hν k s² = a² + h s N + h s f
  have pm : ∀ i, K.add (K.mul (a' i) (a' i)) (K.mul (K.mul h nu) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
      = K.add (K.add (K.mul (a i) (a i)) (K.mul h (K.mul (K.add (a' i) (a i)) (f i))))
          (K.mul h (K.mul (K.add (a' i) (a i)) (Nn i))) := by
    intro i
    have e : K.mul (a' i) (a' i) = K.add (K.mul (a i) (a i)) (K.mul (sub K (a' i) (a i)) (K.add (a' i) (a i))) := by lib_ring
    rw [e, step i]; lib_ring
  have sum_pm := lsum_ext K _ _ pm l
  rw [lsum_add, lsum_add, lsum_add, lsum_mul_left, lsum_mul_left, lsum_mul_left, neutral] at sum_pm
  rw [show K.add (lsum K (fun i => K.mul (a i) (a i)) l) (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l))
      = K.add (K.add (lsum K (fun i => K.mul (a i) (a i)) l) (K.mul h (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l)))
          (K.mul h K.zero) by lib_ring]
  exact sum_pm

theorem lsum_le' (P : Pos K) (u v : I → R) (h : ∀ i, le K P (u i) (v i)) :
    ∀ l : List I, le K P (lsum K u l) (lsum K v l)
  | [] => le_refl K P _
  | i :: l => le_add2 K P (h i) (lsum_le' P u v h l)

/-- **Forced step**: 2E′ + hνΣks² ≤ 2E + (h/ν)Σf². -/
theorem forced_step (P : Pos K) (F : OF K P) (a a' κ Nn f : I → R) (h nu : R) (l : List I)
    (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hκ : ∀ i, P.nn (κ i))
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (K.add K.one (κ i)) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (K.add (a' i) (a i)) (Nn i)) l = K.zero) :
    le K P (K.add (K.add (lsum K (fun i => K.mul (a' i) (a' i)) l) (lsum K (fun i => K.mul (a' i) (a' i)) l))
              (K.mul (K.mul h nu) (lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)))
      (K.add (K.add (lsum K (fun i => K.mul (a i) (a i)) l) (lsum K (fun i => K.mul (a i) (a i)) l))
             (K.mul (K.mul h (F.inv nu)) (lsum K (fun i => K.mul (f i) (f i)) l))) := by
  have id := step_identity K a a' (fun i => K.add K.one (κ i)) Nn f h nu l step neutral
  -- Young per mode: 2 s f ≤ ν (1+κ) s² + f²/ν
  have y : ∀ i, le K P (K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i)))
      (K.add (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
             (K.mul (K.mul (f i) (f i)) (F.inv nu))) := by
    intro i
    refine le_trans K P (young K P F nu (K.add (a' i) (a i)) (f i) hnu hnu0) (le_add K P _ ?_)
    unfold le
    rw [show sub K (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
        (K.mul nu (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        = K.mul nu (K.mul (κ i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) by lib_ring]
    exact P.nn_mul _ _ hnu (P.nn_mul _ _ (hκ i) (P.nn_sq _))
  have ys := le_mul_left K P hh (lsum_le' K P _ _ y l)
  unfold le at ys ⊢
  revert ys id
  rw [show lsum K (fun i => K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i))) l
      = K.mul (castZ K 2) (lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l) from lsum_mul_left K _ _ l,
    show lsum K (fun i => K.add (K.mul nu (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))))
        (K.mul (K.mul (f i) (f i)) (F.inv nu))) l
      = K.add (K.mul nu (lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
          (K.mul (F.inv nu) (lsum K (fun i => K.mul (f i) (f i)) l)) by
      rw [lsum_add, lsum_mul_left]
      rw [lsum_ext K _ (fun i => K.mul (F.inv nu) (K.mul (f i) (f i))) (fun i => K.mul_comm _ _) l, lsum_mul_left]]
  generalize lsum K (fun i => K.mul (a' i) (a' i)) l = E'
  generalize lsum K (fun i => K.mul (a i) (a i)) l = E
  generalize lsum K (fun i => K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l = D
  generalize lsum K (fun i => K.mul (K.add (a' i) (a i)) (f i)) l = SF
  generalize lsum K (fun i => K.mul (f i) (f i)) l = FF
  intro id ys
  have e : sub K (K.add (K.add E E) (K.mul (K.mul h (F.inv nu)) FF)) (K.add (K.add E' E') (K.mul (K.mul h nu) D))
      = K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)))
          (K.add (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))
                 (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))) := by
    lib_ring
  rw [e, ← id]
  rw [show sub K (K.add E' (K.mul (K.mul h nu) D)) (K.add E' (K.mul (K.mul h nu) D)) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF))) (K.add K.zero K.zero)
      = sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)) by lib_ring]
  exact ys

end ForcedLeray

#print axioms ForcedLeray.step_identity
#print axioms ForcedLeray.lsum_le'
#print axioms ForcedLeray.forced_step

Listing 191: ns/ForcedRun.lean

ns/ForcedRun.lean

/-
  ns/ForcedRun.lean — T37 cycle 122: the forced Leray inequality along a whole run.

  Lean 4 core plus ns/ForcedLeray, lib/Gronwall (le arithmetic), lib/RingTac. If every step satisfies
      2E_{t+1} + hν D_t ≤ 2E_t + c F_t      (ForcedLeray.forced_step, c = h/ν)
  then for every T
      2E_T + hν Σ_{t<T} D_t ≤ 2E_0 + c Σ_{t<T} F_t.
  With F_t = ‖f‖² constant: energy grows at most linearly, and the total dissipation is bounded by the
  initial energy plus the work of the force — every truncation, every dimension.
-/
import ns.ForcedLeray

namespace ForcedRun
open DetChain NSChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def acc (d : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (acc d n) (d n)

theorem run (E D F : Nat → R) (hnu c : R)
    (step : ∀ t, le K P (K.add (K.add (E (t + 1)) (E (t + 1))) (K.mul hnu (D t)))
                        (K.add (K.add (E t) (E t)) (K.mul c (F t)))) :
    ∀ T, le K P (K.add (K.add (E T) (E T)) (K.mul hnu (acc K D T)))
                (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F T)))
  | 0 => by
    unfold le
    rw [show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F 0))) (K.add (K.add (E 0) (E 0)) (K.mul hnu (acc K D 0)))
        = K.zero by show sub K (K.add (K.add (E 0) (E 0)) (K.mul c K.zero)) (K.add (K.add (E 0) (E 0)) (K.mul hnu K.zero)) = K.zero; lib_ring]
    exact P.nn_zero
  | T + 1 => by
    have ih := run E D F hnu c step T
    have s := step T
    unfold le at ih s ⊢
    rw [show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F (T + 1))))
          (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (acc K D (T + 1))))
        = K.add (sub K (K.add (K.add (E 0) (E 0)) (K.mul c (acc K F T))) (K.add (K.add (E T) (E T)) (K.mul hnu (acc K D T))))
            (sub K (K.add (K.add (E T) (E T)) (K.mul c (F T))) (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (D T)))) by
      show sub K (K.add (K.add (E 0) (E 0)) (K.mul c (K.add (acc K F T) (F T))))
          (K.add (K.add (E (T + 1)) (E (T + 1))) (K.mul hnu (K.add (acc K D T) (D T)))) = _
      lib_ring]
    exact P.nn_add _ _ ih s

end ForcedRun

#print axioms ForcedRun.run

Listing 192: ns/Epochs.lean

ns/Epochs.lean

/-
  ns/Epochs.lean — T37 cycle 116: epochs of small dissipation (Leray's argument, discrete).

  Lean 4 core plus the shared lib/OField (totality), lib/Gronwall (le). For d_t ≥ 0, t < T:
    min_exists   ∃ i < T+1, ∀ j < T+1, d_i ≤ d_j                 (totality, no classical logic)
    epoch        Σ_{t<T+1} d_t ≤ S  ⇒  ∃ i < T+1, (T+1)·d_i ≤ S
  With LerayDiscrete (Σ_t hνD_t ≤ E_0): every run of every truncation has a step with
  hνD_t ≤ E_0/(T+1) — the discrete form of "regular times have full measure".
-/
import lib.OField
import NSChain

namespace Epochs
open DetChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) (P : Pos K)

def sumN' (d : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sumN' d n) (d n)

theorem min_exists (F : OF K P) (d : Nat → R) : ∀ T, ∃ i, i < T + 1 ∧ ∀ j, j < T + 1 → le K P (d i) (d j)
  | 0 => ⟨0, Nat.zero_lt_succ 0, fun j hj => by
      have : j = 0 := Nat.le_zero.mp (Nat.le_of_lt_succ hj)
      rw [this]; exact le_refl K P _⟩
  | T + 1 => by
    have ⟨i, hi, hmin⟩ := min_exists F d T
    cases F.total (sub K (d (T + 1)) (d i)) with
    | inl h =>
      -- d_i ≤ d_{T+1}: keep i
      refine ⟨i, Nat.lt_succ_of_lt hi, fun j hj => ?_⟩
      cases Nat.eq_or_lt_of_le (Nat.le_of_lt_succ hj) with
      | inl e => rw [e]; exact h
      | inr l => exact hmin j l
    | inr h =>
      -- d_{T+1} ≤ d_i
      have h' : le K P (d (T + 1)) (d i) := by
        unfold le; rw [show sub K (d i) (d (T + 1)) = K.neg (sub K (d (T + 1)) (d i)) by lib_ring]; exact h
      refine ⟨T + 1, Nat.lt_succ_self _, fun j hj => ?_⟩
      cases Nat.eq_or_lt_of_le (Nat.le_of_lt_succ hj) with
      | inl e => rw [e]; exact le_refl K P _
      | inr l => exact le_trans K P h' (hmin j l)

theorem sum_ge (d : Nat → R) (m : R) : ∀ n, (∀ j, j < n → le K P m (d j)) →
    le K P (K.mul (castN K n) m) (sumN' K d n)
  | 0, _ => by unfold le; rw [show sub K (sumN' K d 0) (K.mul (castN K 0) m) = K.zero by
      show sub K K.zero (K.mul K.zero m) = K.zero; lib_ring]; exact P.nn_zero
  | n + 1, h => by
    have ih := sum_ge d m n (fun j hj => h j (Nat.lt_succ_of_lt hj))
    have hn := h n (Nat.lt_succ_self n)
    have := le_add2 K P ih hn
    rw [show K.add (K.mul (castN K n) m) m = K.mul (castN K (n + 1)) m by
      show _ = K.mul (K.add K.one (castN K n)) m; lib_ring] at this
    exact this

/-- **An epoch of small dissipation.** -/
theorem epoch (F : OF K P) (d : Nat → R) (S : R) (T : Nat) (hS : le K P (sumN' K d (T + 1)) S) :
    ∃ i, i < T + 1 ∧ le K P (K.mul (castN K (T + 1)) (d i)) S := by
  have ⟨i, hi, hmin⟩ := min_exists K P F d T
  exact ⟨i, hi, le_trans K P (sum_ge K P d (d i) (T + 1) hmin) hS⟩

end Epochs

#print axioms Epochs.min_exists
#print axioms Epochs.sum_ge
#print axioms Epochs.epoch

79.54 The tail of \(\sum 1/n^2\)

In every ordered field the tail of the Basel sum beyond \(K\) is at most \(1/(K+1)\). \(\sum _{j<N}u_{K+j+1}^2\le u_K,\qquad (n+1)\,u_n=1\)

ns/BaselTail.lean

/-
  ns/BaselTail.lean — T37 cycle 134: the tail of Σ 1/n² (piece of Agmon's inequality ‖u‖_∞² ≤ C‖∇u‖‖Δu‖ in
  3D, uniform in the truncation: the high modes cost 1/K).

  Lean 4 core plus ns/Basel (u_n = 1/(n+1), sq_le_diff, u_nn), ns/LatticeSum (tele_le).
    tail     Σ_{j<N} u_{K+j+1}² ≤ u_K        (Σ_{n>K} 1/(n+1)² ≤ 1/(K+1), every N)
-/
import ns.Basel

namespace BaselTail
open DetChain NSChain LibRing Basel

variable {R : Type} (K : CRing R)

theorem tail (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (Kk N : Nat) :
    Le K P (sumN K (fun j => K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) N) (u Kk) :=
  LatticeSum.tele_le K P (fun j => u (Kk + j)) (fun j => K.mul (u (Kk + j)) (u (Kk + j)))
    (fun j => u_nn K P u hu (Kk + j)) (fun j => sq_le_diff K P u hu (Kk + j)) N

end BaselTail

#print axioms BaselTail.tail

#print axioms: 1 theorem, no axioms.

79.55 Agmon’s inequality for truncations

For every \(K\) the sup norm of a truncation is bounded by a low-shell and a high-shell weight. \((\sum |\hat u|)^2\le 52K\|\nabla u\|^2+\frac {52}{K+1}\|\Delta u\|^2\)

ns/Agmon.lean

/-
  ns/Agmon.lean — T37 cycle 137: Agmon's inequality for 3D truncations, with K free:
      (Σ_low |û| + Σ_high |û|)² ≤ 2·W_low·Σ|k|²|û|² + 2·W_high·Σ|k|⁴|û|²,
      W_low = Σ_{shells n<K} 26(n+1)²·u_n² = 26K,   W_high ≤ 26·u_K   (u_n = 1/(n+1))
  so (Σ|û|)² ≤ 52K‖∇u‖² + 52‖Δu‖²/(K+1) for every K — choosing K ≈ ‖Δu‖/‖∇u‖ gives ‖u‖_∞² ≤ C‖∇u‖‖Δu‖.

  Lean 4 core plus ns/Sobolev (sobolev, cN, lsum_le_len), ns/BaselTail, ns/Basel, lib/RingTac.
    sq_add_le    (a + b)² ≤ 2a² + 2b²
    low_weight   Σ_{n<K} 26(n+1)² u_n² = 26·K           (each shell term is 26)
    agmon        the combination, from two Sobolev-type bounds
-/
import ns.Sobolev
import ns.BaselTail

namespace Agmon
open DetChain NSChain LibRing Basel SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem sq_add_le (P : Cone K) (a b : R) :
    Le K P (sq K (K.add a b)) (K.add (K.add (sq K a) (sq K a)) (K.add (sq K b) (sq K b))) :=
  ⟨sq K (sub K a b), P.nn_sq _, by unfold sq; lib_ring⟩

theorem low_weight (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) :
    ∀ Kk, sumN K (fun n => K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (u n) (u n)))) Kk
      = K.mul (cN K 26) (cN K Kk)
  | 0 => by show K.zero = K.mul (cN K 26) K.zero; lib_ring
  | Kk + 1 => by
    show K.add (sumN K _ Kk) (K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + 1)) (cN K (Kk + 1))) (K.mul (u Kk) (u Kk))))
      = K.mul (cN K 26) (K.add (cN K Kk) K.one)
    rw [low_weight u hu Kk]
    have h := hu Kk
    revert h
    generalize cN K (Kk + 1) = c
    generalize u Kk = a
    intro h
    rw [show K.mul (K.mul c c) (K.mul a a) = K.mul (K.mul c a) (K.mul c a) by lib_ring, h]; lib_ring

/-- **Agmon, combination step**: from Sobolev-type bounds on the low and the high part. -/
theorem agmon (P : Cone K) (XL XH WL WH ZL PH : R)
    (hL : Le K P (sq K XL) (K.mul WL ZL)) (hH : Le K P (sq K XH) (K.mul WH PH)) :
    Le K P (sq K (K.add XL XH))
      (K.add (K.add (K.mul WL ZL) (K.mul WL ZL)) (K.add (K.mul WH PH) (K.mul WH PH))) :=
  le_trans' K P _ _ _ (sq_add_le K P XL XH)
    (Riccati.le_add K P _ _ _ _ (Riccati.le_add K P _ _ _ _ hL hL) (Riccati.le_add K P _ _ _ _ hH hH))

end Agmon

#print axioms Agmon.sq_add_le
#print axioms Agmon.low_weight
#print axioms Agmon.agmon

#print axioms: 3 theorems, no axioms.

79.56 Agmon’s inequality, optimised

Choosing the cut \(k\) with \(k^2Z\le P\le (k+1)^2Z\) gives the square-root free Agmon inequality. \(\|u\|_\infty ^4\le 2(a^2+b^2)\,\|\nabla u\|^2\|\Delta u\|^2\)

ns/AgmonOpt.lean

/-
  ns/AgmonOpt.lean — T37 cycle 139: the optimised Agmon inequality, square-root free.

  Lean 4 core plus ns/Agmon, ns/Riccati (le_sq, le_add, le_mul_left), lib/RingTac. If X² ≤ a·k·Z + b·P·w with
  (k + 1)w = 1, and k is chosen with k²Z ≤ P ≤ (k + 1)²Z (k ≈ √(P/Z)), then
      (X²)² ≤ 2(a² + b²) · Z · P          i.e. ‖u‖_∞⁴ ≤ C ‖∇u‖² ‖Δu‖²   (Z = ‖∇u‖², P = ‖Δu‖²)
-/
import ns.Agmon

namespace AgmonOpt
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R)

theorem opt (P : Cone K) (X a b k w Z Pp : R) (hX : P.nn (sq K X))
    (hZ : P.nn Z) (hP : P.nn Pp)
    (hkw : K.mul (K.add k K.one) w = K.one)
    (h1 : Le K P (K.mul (K.mul k k) Z) Pp) (h2 : Le K P Pp (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z))
    (hXb : Le K P (sq K X) (K.add (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w)))) :
    Le K P (K.mul (sq K X) (sq K X))
      (K.add (K.add (K.mul (K.mul a a) (K.mul Z Pp)) (K.mul (K.mul a a) (K.mul Z Pp)))
             (K.add (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul b b) (K.mul Z Pp)))) := by
  -- (X²)² ≤ (A + B)² ≤ 2A² + 2B²
  have s1 := Riccati.le_sq K P _ _ hX hXb
  have s2 := Agmon.sq_add_le K P (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w))
  -- A² = a²k²Z² ≤ a² Z P
  have hA : Le K P (sq K (K.mul a (K.mul k Z))) (K.mul (K.mul a a) (K.mul Z Pp)) := by
    have m := Riccati.le_mul_left K P (K.mul (K.mul a a) Z) _ _ (P.nn_mul _ _ (P.nn_sq a) hZ) h1
    have e : sq K (K.mul a (K.mul k Z)) = K.mul (K.mul (K.mul a a) Z) (K.mul (K.mul k k) Z) := by unfold sq; lib_ring
    rw [e]; refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by lib_ring⟩
  -- B² = b²P²w² ≤ b² Z P   since P w² ≤ (k+1)² Z w² = Z
  have hB : Le K P (sq K (K.mul b (K.mul Pp w))) (K.mul (K.mul b b) (K.mul Z Pp)) := by
    have m := Riccati.le_mul_left K P (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) _ _
      (P.nn_mul _ _ (P.nn_mul _ _ (P.nn_sq b) hP) (P.nn_sq w)) h2
    have e : sq K (K.mul b (K.mul Pp w)) = K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) Pp := by unfold sq; lib_ring
    have e2 : K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z)
        = K.mul (K.mul b b) (K.mul Z Pp) := by
      rw [show K.mul (K.mul (K.mul (K.mul b b) Pp) (K.mul w w)) (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z)
          = K.mul (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul (K.add k K.one) w) (K.mul (K.add k K.one) w)) by lib_ring,
        hkw]; lib_ring
    rw [e, ← e2]; exact m
  refine le_trans' K P _ _ _ s1 (le_trans' K P _ _ _ s2 ?_)
  exact Riccati.le_add K P _ _ _ _ (Riccati.le_add K P _ _ _ _ hA hA) (Riccati.le_add K P _ _ _ _ hB hB)

end AgmonOpt

#print axioms AgmonOpt.opt

#print axioms: 1 theorem, no axioms.

79.57 A Prodi–Serrin-type discrete criterion

Enstrophy is bounded by the product of the growth factors of the steps. \(z_{k+1}\le (1+a_k)z_k\ \Rightarrow \ z_N\le \prod _{k<N}(1+a_k)\,z_0\)

ns/SerrinDiscrete.lean

/-
  ns/SerrinDiscrete.lean — T37 cycle 145: a Prodi–Serrin-type discrete criterion.

  Lean 4 core plus the shared lib/Gronwall (comparison, le), lib/RingTac. If z_{k+1} ≤ (1 + a_k) z_k with
  a_k ≥ 0 (a_k = hC‖u_k‖²_∞/ν from the stretching estimate), then
      z_N ≤ Π_{k<N} (1 + a_k) · z_0
  so the enstrophy stays bounded on any interval where Σ_k a_k (i.e. ∫‖u‖²_∞ dt) is finite — the product
  is bounded by any bound on Π(1 + a_k), e.g. by Bernoulli-type estimates for small a_k.
-/
import lib.Gronwall

namespace SerrinDiscrete
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def prodC (a : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (K.add K.one (a n)) (prodC a n)

theorem serrin (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one)
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k))) :
    ∀ N, le K P (z N) (K.mul (prodC K a N) (z 0)) := by
  have c := comparison K P z (fun N => K.mul (prodC K a N) (z 0)) (fun k => K.add K.one (a k)) (fun _ => K.zero)
    (fun k => P.nn_add _ _ hone (ha k))
    (fun k => by rw [show K.add (K.mul (K.add K.one (a k)) (z k)) K.zero = K.mul (K.add K.one (a k)) (z k) by lib_ring]; exact step k)
    (fun k => by show K.mul (K.mul (K.add K.one (a k)) (prodC K a k)) (z 0) = K.add (K.mul (K.add K.one (a k)) (K.mul (prodC K a k) (z 0))) K.zero; lib_ring)
    (by show le K P (z 0) (K.mul K.one (z 0)); unfold le; rw [show sub K (K.mul K.one (z 0)) (z 0) = K.zero by lib_ring]; exact P.nn_zero)
  exact c

end SerrinDiscrete

#print axioms SerrinDiscrete.serrin

#print axioms: 1 theorem, no axioms.

79.58 Forced enstrophy inequality

With weights \(w\ge 0\) and an energy-neutral weighted nonlinearity (\(w=|k|^2\) in two dimensions): \(2Z'+h\nu \sum wks^2\le 2Z+\frac h\nu \sum wf^2\)

ns/ForcedEnstrophy.lean

/-
  ns/ForcedEnstrophy.lean — T37 cycle 120: the energy inequality with forcing, one Crank–Nicolson step.

  Lean 4 core plus the shared lib/OField (young), lib/Gronwall (le), NSChain (lsum), lib/RingTac.
  Modes i ∈ l, a′_i − a_i = h(−ν k_i s_i + N_i + f_i), s = a′ + a, Σ s N = 0 (energy-neutral nonlinearity),
  k_i = 1 + κ_i with κ_i ≥ 0 (lattice: |k|² ≥ 1), h, ν ≥ 0, ν ≠ 0:
    step_identity   E′ + hν Σ k s² = E + h Σ s f
    forced_step     2E′ + hν Σ k s² ≤ 2E + (h/ν) Σ f²           (Young with ε = ν)
-/
import lib.OField
import NSChain

namespace ForcedEnstrophy
open DetChain NSChain LibRing LibCS LibGron LibOF

variable {R : Type} (K : CRing R) {I : Type}

theorem step_identity (w a a' k Nn f : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l = K.zero) :
    K.add (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l))
      = K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l)
          (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l)) := by
  -- per mode: a′² + hν k s² = a² + h s N + h s f
  have pm : ∀ i, K.add (K.mul (w i) (K.mul (a' i) (a' i))) (K.mul (K.mul h nu) (K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
      = K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (K.mul h (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i)))))
          (K.mul h (K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i)))) := by
    intro i
    have e : K.mul (a' i) (a' i) = K.add (K.mul (a i) (a i)) (K.mul (sub K (a' i) (a i)) (K.add (a' i) (a i))) := by lib_ring
    rw [e, step i]; lib_ring
  have sum_pm := lsum_ext K _ _ pm l
  rw [lsum_add, lsum_add, lsum_add, lsum_mul_left, lsum_mul_left, lsum_mul_left, neutral] at sum_pm
  rw [show K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l))
      = K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l)))
          (K.mul h K.zero) by lib_ring]
  exact sum_pm

theorem lsum_le' (P : Pos K) (u v : I → R) (h : ∀ i, le K P (u i) (v i)) :
    ∀ l : List I, le K P (lsum K u l) (lsum K v l)
  | [] => le_refl K P _
  | i :: l => le_add2 K P (h i) (lsum_le' P u v h l)

/-- **Forced step**: 2E′ + hνΣks² ≤ 2E + (h/ν)Σf². -/
theorem forced_step (P : Pos K) (F : OF K P) (w a a' κ Nn f : I → R) (hw : ∀ i, P.nn (w i)) (h nu : R) (l : List I)
    (hh : P.nn h) (hnu : P.nn nu) (hnu0 : nu ≠ K.zero) (hκ : ∀ i, P.nn (κ i))
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.add (K.neg (K.mul nu (K.mul (K.add K.one (κ i)) (K.add (a' i) (a i))))) (Nn i)) (f i)))
    (neutral : lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l = K.zero) :
    le K P (K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l) (lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l))
              (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l)))
      (K.add (K.add (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l) (lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l))
             (K.mul (K.mul h (F.inv nu)) (lsum K (fun i => K.mul (w i) (K.mul (f i) (f i))) l))) := by
  have id := step_identity K w a a' (fun i => K.add K.one (κ i)) Nn f h nu l step neutral
  -- weighted Young per mode: w·2sf ≤ ν w (1+κ) s² + w f²/ν
  have y : ∀ i, le K P (K.mul (castZ K 2) (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))))
      (K.add (K.mul nu (K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
             (K.mul (F.inv nu) (K.mul (w i) (K.mul (f i) (f i))))) := by
    intro i
    have y0 := le_mul_left K P (hw i) (young K P F nu (K.add (a' i) (a i)) (f i) hnu hnu0)
    refine le_trans K P (by
        unfold le; rw [show sub K (K.mul (w i) (K.mul (castZ K 2) (K.mul (K.add (a' i) (a i)) (f i))))
            (K.mul (castZ K 2) (K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i)))) = K.zero by lib_ring]
        exact P.nn_zero) (le_trans K P y0 ?_)
    unfold le
    rw [show sub K (K.add (K.mul nu (K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))))
             (K.mul (F.inv nu) (K.mul (w i) (K.mul (f i) (f i)))))
          (K.mul (w i) (K.add (K.mul nu (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) (K.mul (K.mul (f i) (f i)) (F.inv nu))))
        = K.mul nu (K.mul (w i) (K.mul (κ i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) by lib_ring]
    exact P.nn_mul _ _ hnu (P.nn_mul _ _ (hw i) (P.nn_mul _ _ (hκ i) (P.nn_sq _)))
  have ys := le_mul_left K P hh (lsum_le' K P _ _ y l)
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_mul_left] at ys
  unfold le at ys ⊢
  revert ys id
  generalize lsum K (fun i => K.mul (w i) (K.mul (a' i) (a' i))) l = E'
  generalize lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l = E
  generalize lsum K (fun i => K.mul (w i) (K.mul (K.add K.one (κ i)) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l = D
  generalize lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (f i))) l = SF
  generalize lsum K (fun i => K.mul (w i) (K.mul (f i) (f i))) l = FF
  intro id ys
  have e : sub K (K.add (K.add E E) (K.mul (K.mul h (F.inv nu)) FF)) (K.add (K.add E' E') (K.mul (K.mul h nu) D))
      = K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)))
          (K.add (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))
                 (sub K (K.add E (K.mul h SF)) (K.add E' (K.mul (K.mul h nu) D)))) := by lib_ring
  rw [e, ← id]
  rw [show sub K (K.add E' (K.mul (K.mul h nu) D)) (K.add E' (K.mul (K.mul h nu) D)) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF))) (K.add K.zero K.zero)
      = sub K (K.mul h (K.add (K.mul nu D) (K.mul (F.inv nu) FF))) (K.mul h (K.mul (castZ K 2) SF)) by lib_ring]
  exact ys

end ForcedEnstrophy

#print axioms ForcedEnstrophy.step_identity
#print axioms ForcedEnstrophy.lsum_le'
#print axioms ForcedEnstrophy.forced_step

#print axioms: 3 theorems, no axioms.

79.59 Serrin criterion, sum form

With \(S_N=\sum _{k<N}a_k\le 1\), the step bounds give a bound linear in \(1-S_N\). \(\prod _{k<n}(1+a_k)(1-S_n)\le 1,\quad z_N(1-S_N)\le z_0\)

ns/SerrinSum.lean

/-
  ns/SerrinSum.lean — T37 cycle 152: the discrete Prodi–Serrin continuation bound with a sum condition.

  Lean 4 core plus ns/SerrinDiscrete, lib/Gronwall, lib/RingTac. For a_k ≥ 0 and S_n = Σ_{k<n} a_k,
      Π_{k<n}(1 + a_k) · (1 − S_n) ≤ 1                         (no sign condition on 1 − S_n)
  and hence, if z_{k+1} ≤ (1 + a_k) z_k, z_0 ≥ 0 and S_N ≤ 1,
      z_N · (1 − S_N) ≤ z_0 .
  With a_k = h C ‖u_k‖²_∞ / ν: the enstrophy stays below z_0 / (1 − S) while h C Σ ‖u_k‖²_∞ / ν < 1.
-/
import ns.SerrinDiscrete

namespace SerrinSum
open DetChain LibRing LibCS LibGron SerrinDiscrete

variable {R : Type} (K : CRing R) (P : Pos K)

def sumA (a : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sumA a n) (a n)

theorem sumA_nn (a : Nat → R) (ha : ∀ k, P.nn (a k)) : ∀ n, P.nn (sumA K a n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (sumA_nn a ha n) (ha n)

theorem prodC_nn (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) : ∀ n, P.nn (prodC K a n)
  | 0 => hone
  | n + 1 => P.nn_mul _ _ (P.nn_add _ _ hone (ha n)) (prodC_nn a ha hone n)

theorem bern (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) :
    ∀ n, le K P (K.mul (prodC K a n) (sub K K.one (sumA K a n))) K.one
  | 0 => by
      unfold le
      rw [show sub K K.one (K.mul (prodC K a 0) (sub K K.one (sumA K a 0))) = K.zero by
        show sub K K.one (K.mul K.one (sub K K.one K.zero)) = K.zero; lib_ring]
      exact P.nn_zero
  | n + 1 => by
      have ih := bern a ha hone n
      unfold le at ih ⊢
      have hX := prodC_nn K P a ha hone n
      have hS := sumA_nn K P a ha n
      rw [show sub K K.one (K.mul (prodC K a (n + 1)) (sub K K.one (sumA K a (n + 1))))
          = K.add (K.add (sub K K.one (K.mul (prodC K a n) (sub K K.one (sumA K a n))))
                         (K.mul (a n) (K.mul (prodC K a n) (sumA K a n))))
                  (K.mul (K.mul (a n) (a n)) (prodC K a n)) by
        show sub K K.one (K.mul (K.mul (K.add K.one (a n)) (prodC K a n)) (sub K K.one (K.add (sumA K a n) (a n))))
          = _
        lib_ring]
      exact P.nn_add _ _ (P.nn_add _ _ ih (P.nn_mul _ _ (ha n) (P.nn_mul _ _ hX hS)))
        (P.nn_mul _ _ (P.nn_sq _) hX)

theorem serrin_sum (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz0 : P.nn (z 0))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K a N))) :
    le K P (K.mul (z N) (sub K K.one (sumA K a N))) (z 0) := by
  have s := serrin K P z a ha hone step N
  have m := le_mul_left K P hS s
  have b := le_mul_left K P hz0 (bern K P a ha hone N)
  have m' : le K P (K.mul (z N) (sub K K.one (sumA K a N)))
      (K.mul (z 0) (K.mul (prodC K a N) (sub K K.one (sumA K a N)))) := by
    rw [show K.mul (z N) (sub K K.one (sumA K a N)) = K.mul (sub K K.one (sumA K a N)) (z N) by lib_ring,
        show K.mul (z 0) (K.mul (prodC K a N) (sub K K.one (sumA K a N)))
          = K.mul (sub K K.one (sumA K a N)) (K.mul (prodC K a N) (z 0)) by lib_ring]
    exact m
  have t := le_trans K P m' b
  rw [show K.mul (z 0) K.one = z 0 by lib_ring] at t
  exact t

end SerrinSum

#print axioms SerrinSum.sumA_nn
#print axioms SerrinSum.prodC_nn
#print axioms SerrinSum.bern
#print axioms SerrinSum.serrin_sum

#print axioms: 4 theorems, no axioms.

79.60 Young step

Dissipation absorbs the stretching term; the step takes the Serrin form with \(a=hc^2\|u\|_\infty ^2/(2\nu )\). \(Z'\le Z+h(cXrs-\nu s^2),\ r^2=Z\ \Rightarrow \ 2\nu Z'\le (2\nu +hc^2X^2)Z\)

ns/YoungStep.lean

/-
  ns/YoungStep.lean — T37 cycle 155: dissipation absorbs the stretching term, Serrin form of the step.

  Lean 4 core plus lib/Gronwall (le), lib/RingTac. With r² = Z = ‖∇u‖², s² = P = ‖Δu‖², X = ‖u‖_∞ and the
  stretching bound c X r s, the enstrophy step
      Z' ≤ Z + h (c X r s − ν s²)
  gives, by (ν s − c X r)² ≥ 0 (Young without division),
      2ν Z' ≤ (2ν + h c² X²) Z ,
  i.e. Z' ≤ (1 + a) Z with a = h c² X² / (2ν): the step of SerrinDiscrete / SerrinSum, a_k ∝ ‖u_k‖²_∞.
-/
import lib.Gronwall

namespace YoungStep
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem young_step (Z Z' h c X r s ν : R) (hh : P.nn h) (hν : P.nn ν) (hr : K.mul r r = Z)
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) r) s) (K.mul ν (K.mul s s)))))) :
    le K P (K.mul (K.add ν ν) Z') (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z) := by
  unfold le at step ⊢
  rw [← hr] at step ⊢
  rw [show sub K (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) (K.mul r r)) (K.mul (K.add ν ν) Z')
      = K.add (K.add (K.mul (K.add ν ν)
              (sub K (K.add (K.mul r r) (K.mul h (sub K (K.mul (K.mul (K.mul c X) r) s) (K.mul ν (K.mul s s))))) Z'))
            (K.mul h (K.mul (sub K (K.mul ν s) (K.mul (K.mul c X) r)) (sub K (K.mul ν s) (K.mul (K.mul c X) r)))))
          (K.mul h (K.mul (K.mul ν s) (K.mul ν s))) by lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hν hν) step) (P.nn_mul _ _ hh (P.nn_sq _)))
    (P.nn_mul _ _ hh (P.nn_sq _))

end YoungStep

#print axioms YoungStep.young_step

#print axioms: 1 theorem, no axioms.

79.61 Serrin criterion for a run

Young step and sum form combined: a run stays bounded while \(\frac {hc^2}{2\nu }\sum _k\|u_k\|_\infty ^2<1\). \(Z_N\bigl (1-\sum _{k<N}\tfrac {hc^2X_k^2}{2\nu }\bigr )\le Z_0\)

ns/SerrinRun.lean

/-
  ns/SerrinRun.lean — T37 cycle 158: the discrete Prodi–Serrin continuation bound for a whole run.

  Lean 4 core plus ns/YoungStep, ns/SerrinSum, lib/OField (inv, nn_inv), lib/RingTac. If every step obeys
      Z_{k+1} ≤ Z_k + h (c X_k r_k s_k − ν s_k²),   r_k² = Z_k,
  (X_k = ‖u_k‖_∞, s_k² = ‖Δu_k‖²), then with a_k = h c² X_k² / (2ν) and S_N = Σ_{k<N} a_k ≤ 1,
      Z_N (1 − S_N) ≤ Z_0 .
  The enstrophy of a run stays below Z_0/(1 − S) while (h c²/2ν) Σ_k ‖u_k‖²_∞ < 1 — the L²_t L^∞_x criterion.
-/
import ns.YoungStep
import ns.SerrinSum
import lib.OField

namespace SerrinRun
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum YoungStep

variable {R : Type} (K : CRing R) (P : Pos K)

def aRun (F : OF K P) (h c ν : R) (X : Nat → R) (k : Nat) : R :=
  K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul (K.mul c c) (K.mul (X k) (X k))))

theorem step_div (F : OF K P) (Z Z' b ν : R) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h : le K P (K.mul (K.add ν ν) Z') (K.mul (K.add (K.add ν ν) b) Z)) :
    le K P Z' (K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) b)) Z) := by
  have m := le_mul_left K P (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) h
  have hi := F.mul_inv _ hν0
  rw [show K.mul (F.inv (K.add ν ν)) (K.mul (K.add ν ν) Z') = K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) Z' by lib_ring,
      show K.mul (F.inv (K.add ν ν)) (K.mul (K.add (K.add ν ν) b) Z)
        = K.add (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) Z) (K.mul (K.mul (F.inv (K.add ν ν)) b) Z) by lib_ring,
      hi] at m
  rw [show K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) b)) Z
      = K.add (K.mul K.one Z) (K.mul (K.mul (F.inv (K.add ν ν)) b) Z) by lib_ring,
    show Z' = K.mul K.one Z' by lib_ring]
  exact m

/-- **Discrete Prodi–Serrin bound for a run.** -/
theorem serrin_run (F : OF K P) (Z r s X : Nat → R) (h c ν : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k) (hZ0 : P.nn (Z 0))
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N))) :
    le K P (K.mul (Z N) (sub K K.one (sumA K (aRun K P F h c ν X) N))) (Z 0) := by
  have hone := nn_one K P
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  exact serrin_sum K P Z (aRun K P F h c ν X) ha hone hZ0
    (fun k => step_div K P F _ _ _ ν hν hν0
      (young_step K P (Z k) (Z (k + 1)) h c (X k) (r k) (s k) ν hh hν (hr k) (step k))) N hS

end SerrinRun

#print axioms SerrinRun.step_div
#print axioms SerrinRun.serrin_run

#print axioms: 2 theorems, no axioms.

79.62 Time sum of \(\|u\|_\infty ^2\)

Agmon at every step and Cauchy–Schwarz in time bound the Serrin budget by \(\sup Z\) and \(\sum P\). \(\bigl (\sum _kX_k^2\bigr )^2\le \bigl (\sum _k1\bigr )\,C\,M\sum _kP_k\)

ns/AgmonTime.lean

/-
  ns/AgmonTime.lean — T37 cycle 161: the time sum of ‖u‖²_∞ from Agmon and Cauchy–Schwarz, square-root free.

  Lean 4 core plus ns/SumCS (cs_sum), ns/Riccati (le_add, le_mul_left), NSChain, lib/RingTac. Over the steps
  l of a run, with X_k⁴ ≤ C Z_k P_k (AgmonOpt) and Z_k ≤ M:
      (Σ_k X_k²)² ≤ (Σ_k 1) · C · M · Σ_k P_k
  i.e. ∫‖u‖²_∞ ≤ (T · C · sup Z · ∫‖Δu‖²)^{1/2}: the budget S of SerrinRun is controlled by sup Z and the
  time integral of ‖Δu‖² — the latter is exactly what the energy does not give (R1-gap, discrete form).
-/
import ns.SumCS
import ns.Riccati

namespace AgmonTime
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

theorem lsum_le (P : Cone K) (u v : I → R) (h : ∀ i, Le K P (u i) (v i)) :
    ∀ l : List I, Le K P (lsum K u l) (lsum K v l)
  | [] => le_refl' K P _
  | i :: l => Riccati.le_add K P _ _ _ _ (h i) (lsum_le P u v h l)

theorem lsum_nn (P : Cone K) (u : I → R) (h : ∀ i, P.nn (u i)) : ∀ l : List I, P.nn (lsum K u l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn P u h l)

/-- **Time sum of ‖u‖²_∞.** -/
theorem agmon_time (P : Cone K) (X Z Pp : I → R) (C M : R) (hC : P.nn C) (hP : ∀ i, P.nn (Pp i))
    (hA : ∀ i, Le K P (sq K (sq K (X i))) (K.mul C (K.mul (Z i) (Pp i))))
    (hZ : ∀ i, Le K P (Z i) M) (l : List I) :
    Le K P (sq K (lsum K (fun i => sq K (X i)) l))
      (K.mul (lsum K (fun _ => sq K K.one) l) (K.mul (K.mul C M) (lsum K Pp l))) := by
  -- (Σ X²·1)² ≤ (Σ (X²)²)(Σ 1²)
  have cs := cs_sum K P (fun i => sq K (X i)) (fun _ => K.one) l
  rw [lsum_ext K (fun i => K.mul (sq K (X i)) K.one) (fun i => sq K (X i)) (fun i => by lib_ring) l] at cs
  -- Σ (X²)² ≤ Σ C Z P ≤ Σ C M P = C M Σ P
  have s1 := lsum_le K P _ _ hA l
  have s2 := lsum_le K P (fun i => K.mul C (K.mul (Z i) (Pp i))) (fun i => K.mul (K.mul C M) (Pp i)) (fun i => by
    have m := Riccati.le_mul_left K P (K.mul C (Pp i)) _ _ (P.nn_mul _ _ hC (hP i)) (hZ i)
    rw [show K.mul (K.mul C (Pp i)) (Z i) = K.mul C (K.mul (Z i) (Pp i)) by lib_ring,
        show K.mul (K.mul C (Pp i)) M = K.mul (K.mul C M) (Pp i) by lib_ring] at m
    exact m) l
  rw [lsum_mul_left K (K.mul C M) Pp l] at s2
  have s3 := le_trans' K P _ _ _ s1 s2
  have s4 := Riccati.le_mul_left K P (lsum K (fun _ => sq K K.one) l) _ _
    (lsum_nn K P _ (fun _ => P.nn_sq K.one) l) s3
  rw [show K.mul (lsum K (fun _ => sq K K.one) l) (lsum K (fun i => sq K (sq K (X i))) l)
      = K.mul (lsum K (fun i => sq K (sq K (X i))) l) (lsum K (fun _ => sq K K.one) l) by lib_ring] at s4
  exact le_trans' K P _ _ _ cs s4

end AgmonTime

#print axioms AgmonTime.lsum_le
#print axioms AgmonTime.lsum_nn
#print axioms AgmonTime.agmon_time

#print axioms: 3 theorems, no axioms.

79.63 Windows of half budget

Each window with budget at most \(1/2\) at most doubles the enstrophy. \(2S_L\le 1\Rightarrow z_L\le 2z_0,\qquad z_{JL}\le 2^Jz_0\)

ns/SerrinWindows.lean

/-
  ns/SerrinWindows.lean — T37 cycle 164: continuation by windows of half budget.

  Lean 4 core plus ns/SerrinSum (serrin_sum, sumA), lib/Gronwall (gronwall_hom, pw), lib/RingTac.
    window    z ≥ 0, z_{k+1} ≤ (1 + a_k) z_k, 2 S_L ≤ 1  ⇒  z_L ≤ 2 z_0
    windows   if every window [jL, (j + 1)L) has budget 2 Σ a ≤ 1, then z_{JL} ≤ 2^J z_0
  With a_k = hc²‖u_k‖²_∞/(2ν): if ‖u‖_∞ is bounded on the run, windows of a fixed length L have budget ≤ 1/2 and
  the enstrophy grows at most like 2^{N/L} — Prodi–Serrin continuation in discrete form.
-/
import ns.SerrinSum

namespace SerrinWindows
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

def two : R := K.add K.one K.one

theorem window (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k)))
    (L : Nat) (hS : P.nn (sub K K.one (K.add (sumA K a L) (sumA K a L)))) :
    le K P (z L) (K.mul (two K) (z 0)) := by
  have hS1 : P.nn (sub K K.one (sumA K a L)) := by
    rw [show sub K K.one (sumA K a L) = K.add (sub K K.one (K.add (sumA K a L) (sumA K a L))) (sumA K a L) by lib_ring]
    exact P.nn_add _ _ hS (sumA_nn K P a ha L)
  have s := serrin_sum K P z a ha hone (hz 0) step L hS1
  -- z_L ≤ 2 z_L (1 − S) ≤ 2 z_0
  have m := le_mul_left K P (P.nn_add _ _ hone hone) s
  have g : le K P (z L) (K.mul (two K) (K.mul (z L) (sub K K.one (sumA K a L)))) := by
    unfold le
    rw [show sub K (K.mul (two K) (K.mul (z L) (sub K K.one (sumA K a L)))) (z L)
        = K.mul (z L) (sub K K.one (K.add (sumA K a L) (sumA K a L))) by unfold two; lib_ring]
    exact P.nn_mul _ _ (hz L) hS
  exact le_trans K P g m

/-- **Windows of half budget.** -/
theorem windows (z a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (z (k + 1)) (K.mul (K.add K.one (a k)) (z k))) (L : Nat)
    (hS : ∀ j, P.nn (sub K K.one (K.add (sumA K (fun k => a (j * L + k)) L) (sumA K (fun k => a (j * L + k)) L))))
    (J : Nat) : le K P (z (J * L)) (K.mul (pw K (two K) J) (z 0)) := by
  have hw : ∀ j, le K P (z ((j + 1) * L)) (K.mul (two K) (z (j * L))) := fun j => by
    have w := window K P (fun k => z (j * L + k)) (fun k => a (j * L + k)) (fun k => ha _) hone (fun k => hz _)
      (fun k => step (j * L + k)) L (hS j)
    rw [show (j + 1) * L = j * L + L by rw [Nat.succ_mul]]
    exact w
  have g := gronwall_hom K P (fun j => z (j * L)) (two K) (P.nn_add _ _ hone hone) hw J
  rw [show z (0 * L) = z 0 by rw [Nat.zero_mul]] at g
  exact g

end SerrinWindows

#print axioms SerrinWindows.window
#print axioms SerrinWindows.windows

#print axioms: 2 theorems, no axioms.

79.64 Two bounds for \(L^3\)

The critical norm is bounded by energy times \(L^4\) or \(L^\infty \). \(\bigl (\sum u^3\bigr )^2\le \sum u^2\sum u^4,\qquad u\le M\Rightarrow \sum u^3\le M\sum u^2\)

ns/L3Interp.lean

/-
  ns/L3Interp.lean — T37 cycle 167: two square-root free bounds for the critical L³ norm (R1 branch).

  Lean 4 core plus ns/SumCS (cs_sum), ns/AgmonTime (lsum_le, lsum_nn), ns/Riccati, lib/RingTac.
  For values u_i ≥ 0 on a grid (or Fourier weights):
    l3_l2l4     (Σ u³)² ≤ (Σ u²)(Σ u⁴)                (Hölder 3 = (2 + 4)/2, by Cauchy–Schwarz)
    l3_linf     u_i ≤ M  ⇒  Σ u³ ≤ M Σ u²            (L³³ ≤ ‖u‖_∞ ‖u‖²₂)
  With the energy bound on Σu² the critical norm is controlled by ‖u‖_∞ (or by L⁴): the R1-gap asks for a
  bound of Σu³ by the data alone, which neither inequality gives.
-/
import ns.AgmonTime

namespace L3Interp
open DetChain NSChain LibRing SumCS AgmonTime

variable {R : Type} (K : CRing R) {I : Type}

theorem l3_l2l4 (P : Cone K) (u : I → R) (l : List I) :
    Le K P (sq K (lsum K (fun i => K.mul (u i) (sq K (u i))) l))
      (K.mul (lsum K (fun i => sq K (u i)) l) (lsum K (fun i => sq K (sq K (u i))) l)) :=
  cs_sum K P u (fun i => sq K (u i)) l

theorem l3_linf (P : Cone K) (u : I → R) (M : R) (hu : ∀ i, Le K P (u i) M) (l : List I) :
    Le K P (lsum K (fun i => K.mul (u i) (sq K (u i))) l) (K.mul M (lsum K (fun i => sq K (u i)) l)) := by
  have s := lsum_le K P (fun i => K.mul (u i) (sq K (u i))) (fun i => K.mul M (sq K (u i))) (fun i => by
    have m := Riccati.le_mul_left K P (sq K (u i)) _ _ (P.nn_sq (u i)) (hu i)
    rw [show K.mul (sq K (u i)) (u i) = K.mul (u i) (sq K (u i)) by lib_ring,
        show K.mul (sq K (u i)) M = K.mul M (sq K (u i)) by lib_ring] at m
    exact m) l
  rw [lsum_mul_left K M (fun i => sq K (u i)) l] at s
  exact s

end L3Interp

#print axioms L3Interp.l3_l2l4
#print axioms L3Interp.l3_linf

#print axioms: 2 theorems, no axioms.

79.65 Exponential energy decay

With the Poincaré inequality every run loses energy at least at the viscous rate. \(E_{k+1}+h\nu Z_k\le E_k,\ E_k\le Z_k\Rightarrow E_N\le (1-h\nu )^NE_0\)

ns/EnergyDecay.lean

/-
  ns/EnergyDecay.lean — T37 cycle 170: exponential energy decay of every discrete run on T³ (mean zero).

  Lean 4 core plus lib/Gronwall (le, gronwall_hom, pw), lib/RingTac. If each step satisfies the discrete Leray
  inequality E_{k+1} + hν Z_k ≤ E_k and the Poincaré inequality E_k ≤ Z_k (all modes |k| ≥ 1; BiotSavart.
  poincare_mode per mode), and hν ≤ 1, then
      E_N ≤ (1 − hν)^N E_0 .
  The energy of every run decays at the viscous rate, whatever the nonlinearity does (it is energy-neutral):
  only the enstrophy can grow, which is where the regularity question sits.
-/
import lib.Gronwall

namespace EnergyDecay
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem decay_step (E E' Z hν : R) (hh : P.nn hν) (hstep : le K P (K.add E' (K.mul hν Z)) E)
    (hp : le K P E Z) : le K P E' (K.mul (sub K K.one hν) E) := by
  unfold le at hstep hp ⊢
  rw [show sub K (K.mul (sub K K.one hν) E) E'
      = K.add (sub K E (K.add E' (K.mul hν Z))) (K.mul hν (sub K Z E)) by lib_ring]
  exact P.nn_add _ _ hstep (P.nn_mul _ _ hh hp)

/-- **Exponential energy decay.** -/
theorem decay (E Z : Nat → R) (hν : R) (hh : P.nn hν) (h1 : P.nn (sub K K.one hν))
    (hstep : ∀ k, le K P (K.add (E (k + 1)) (K.mul hν (Z k))) (E k)) (hp : ∀ k, le K P (E k) (Z k)) (N : Nat) :
    le K P (E N) (K.mul (pw K (sub K K.one hν) N) (E 0)) :=
  gronwall_hom K P E (sub K K.one hν) h1 (fun k => decay_step K P _ _ _ hν hh (hstep k) (hp k)) N

end EnergyDecay

#print axioms EnergyDecay.decay_step
#print axioms EnergyDecay.decay

#print axioms: 2 theorems, no axioms.

79.66 Serrin criterion with a force

A force adds its time sum inside the product bound. \(z_{k+1}\le (1+a_k)z_k+b_k\Rightarrow z_N\le \prod _{k<N}(1+a_k)\bigl (z_0+\sum _{k<N}b_k\bigr )\)

ns/SerrinForced.lean

/-
  ns/SerrinForced.lean — T37 cycle 173: the discrete Prodi–Serrin bound with a force.

  Lean 4 core plus ns/SerrinSum (sumA, prodC_nn), ns/SerrinDiscrete (prodC), lib/Gronwall, lib/RingTac.
    prod_ge_one   a_k ≥ 0  ⇒  Π_{k<n}(1 + a_k) ≥ 1
    serrin_forced z_{k+1} ≤ (1 + a_k) z_k + b_k, a_k, b_k ≥ 0  ⇒  z_N ≤ Π_{k<N}(1 + a_k) · (z_0 + Σ_{k<N} b_k)
  With a_k = hc²‖u_k‖²_∞/(2ν) and b_k = (h/ν)‖f_k‖² (ForcedEnstrophy): the forced enstrophy stays bounded
  while Σ‖u_k‖²_∞ and Σ‖f_k‖² are finite.
-/
import ns.SerrinSum

namespace SerrinForced
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

theorem prod_ge_one (a : Nat → R) (ha : ∀ k, P.nn (a k)) (hone : P.nn K.one) :
    ∀ n, le K P K.one (prodC K a n)
  | 0 => le_refl K P _
  | n + 1 => by
      have ih := prod_ge_one a ha hone n
      unfold le at ih ⊢
      rw [show sub K (prodC K a (n + 1)) K.one
          = K.add (K.mul (K.add K.one (a n)) (sub K (prodC K a n) K.one)) (a n) by
        show sub K (K.mul (K.add K.one (a n)) (prodC K a n)) K.one = _; lib_ring]
      exact P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hone (ha n)) ih) (ha n)

/-- **Forced discrete Serrin bound.** -/
theorem serrin_forced (z a b : Nat → R) (ha : ∀ k, P.nn (a k)) (hb : ∀ k, P.nn (b k)) (hone : P.nn K.one)
    (step : ∀ k, le K P (z (k + 1)) (K.add (K.mul (K.add K.one (a k)) (z k)) (b k))) :
    ∀ N, le K P (z N) (K.mul (prodC K a N) (K.add (z 0) (sumA K b N)))
  | 0 => by
      show le K P (z 0) (K.mul K.one (K.add (z 0) K.zero))
      unfold le; rw [show sub K (K.mul K.one (K.add (z 0) K.zero)) (z 0) = K.zero by lib_ring]; exact P.nn_zero
  | N + 1 => by
      have ih := serrin_forced z a b ha hb hone step N
      have g := prod_ge_one K P a ha hone (N + 1)
      have s := step N
      unfold le at ih g s ⊢
      -- target − z_{N+1} = [(1+a)Πz-part − z_{N+1}] + (1+a)(Π(z0+B) − z_N) + (Π_{N+1} − 1) b_N
      rw [show sub K (K.mul (prodC K a (N + 1)) (K.add (z 0) (sumA K b (N + 1)))) (z (N + 1))
          = K.add (K.add (sub K (K.add (K.mul (K.add K.one (a N)) (z N)) (b N)) (z (N + 1)))
              (K.mul (K.add K.one (a N)) (sub K (K.mul (prodC K a N) (K.add (z 0) (sumA K b N))) (z N))))
              (K.mul (sub K (K.mul (K.add K.one (a N)) (prodC K a N)) K.one) (b N)) by
        show sub K (K.mul (K.mul (K.add K.one (a N)) (prodC K a N)) (K.add (z 0) (K.add (sumA K b N) (b N)))) (z (N + 1)) = _
        lib_ring]
      exact P.nn_add _ _ (P.nn_add _ _ s (P.nn_mul _ _ (P.nn_add _ _ hone (ha N)) ih)) (P.nn_mul _ _ g (hb N))

end SerrinForced

#print axioms SerrinForced.prod_ge_one
#print axioms SerrinForced.serrin_forced

#print axioms: 2 theorems, no axioms.

79.67 Young \(4/3\)–\(4\) and the 3D Riccati step

A sum-of-squares Young inequality turns the stretching bound \(\kappa Z^{3/4}P^{3/4}\) into a cubic Riccati step. \(3A^4+B^4-4A^3B=(A-B)^2(2A^2+(A+B)^2),\quad 4\nu ^3Z'\le 4\nu ^3Z+h\kappa ^4Z^3\)

ns/Young34.lean

/-
  ns/Young34.lean — T37 cycle 176: the 3D stretching step in Riccati form, by a sum-of-squares Young inequality.

  Lean 4 core plus lib/Gronwall (le), lib/RingTac.
    young34     3A⁴ + B⁴ − 4A³B = (A − B)²(2A² + (A + B)²) ≥ 0            (Young with exponents 4/3, 4)
    step34      Z' ≤ Z + h(κ q s³ − ν s⁴), q⁴ = Z³  ⇒  4ν³ Z' ≤ 4ν³ Z + h κ⁴ Z³
  With the measured stretching bound S ≤ κ Z^{3/4} P^{3/4} (κ ≈ 0.025, cycle 141; q = Z^{3/4}, s = P^{1/4})
  this is z' ≤ z + h (κ⁴/4ν³) z³: the Riccati step whose discrete blow-up time is the 3D local existence time.
-/
import lib.Gronwall

namespace Young34
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

def sq (x : R) : R := K.mul x x
def p4 (x : R) : R := sq K (sq K x)

theorem young34 (A B : R) :
    P.nn (sub K (K.add (K.add (p4 K A) (K.add (p4 K A) (p4 K A))) (p4 K B))
      (K.add (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B))
             (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B)))) := by
  rw [show sub K (K.add (K.add (p4 K A) (K.add (p4 K A) (p4 K A))) (p4 K B))
      (K.add (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B))
             (K.add (K.mul (K.mul A (sq K A)) B) (K.mul (K.mul A (sq K A)) B)))
      = K.mul (sq K (sub K A B)) (K.add (K.add (sq K A) (sq K A)) (sq K (K.add A B))) by unfold p4 sq; lib_ring]
  exact P.nn_mul _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _))

def four (x : R) : R := K.add (K.add x x) (K.add x x)

theorem four_nn (ν : R) (hν : P.nn ν) : P.nn (four K (K.mul ν (sq K ν))) := by
  have h3 : P.nn (K.mul ν (sq K ν)) := P.nn_mul _ _ hν (P.nn_sq ν)
  exact P.nn_add _ _ (P.nn_add _ _ h3 h3) (P.nn_add _ _ h3 h3)

/-- **3D stretching step in Riccati form.** -/
theorem step34 (Z Z' h κ q s ν : R) (hh : P.nn h) (hν : P.nn ν) (hq : sq K (sq K q) = K.mul Z (sq K Z))
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul κ q) (K.mul s (sq K s))) (K.mul ν (p4 K s)))))) :
    le K P (K.mul (four K (K.mul ν (sq K ν))) Z')
      (K.add (K.mul (four K (K.mul ν (sq K ν))) Z) (K.mul h (K.mul (p4 K κ) (K.mul Z (sq K Z))))) := by
  have y := young34 K P (K.mul ν s) (K.mul κ q)
  unfold le at step ⊢
  -- target − 4ν³Z' = 4ν³·(step defect) + h·[Young defect] + h·(νs)⁴
  rw [show K.mul (p4 K κ) (K.mul Z (sq K Z)) = p4 K (K.mul κ q) by rw [← hq]; unfold p4 sq; lib_ring]
  rw [show sub K (K.add (K.mul (four K (K.mul ν (sq K ν))) Z) (K.mul h (p4 K (K.mul κ q))))
        (K.mul (four K (K.mul ν (sq K ν))) Z')
      = K.add (K.add (K.mul (four K (K.mul ν (sq K ν)))
            (sub K (K.add Z (K.mul h (sub K (K.mul (K.mul κ q) (K.mul s (sq K s))) (K.mul ν (p4 K s))))) Z'))
          (K.mul h (sub K (K.add (K.add (p4 K (K.mul ν s)) (K.add (p4 K (K.mul ν s)) (p4 K (K.mul ν s)))) (p4 K (K.mul κ q)))
            (K.add (K.add (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)) (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)))
                   (K.add (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)) (K.mul (K.mul (K.mul ν s) (sq K (K.mul ν s))) (K.mul κ q)))))))
        (K.mul h (p4 K (K.mul ν s))) by unfold four p4 sq; lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ (four_nn K P ν hν) step) (P.nn_mul _ _ hh y)) (P.nn_mul _ _ hh (P.nn_sq _))

end Young34

#print axioms Young34.young34
#print axioms Young34.four_nn
#print axioms Young34.step34

#print axioms: 3 theorems, no axioms.

79.68 Riccati comparison, any monotone growth

Any growth bounded on \([0,2B]\) gives a local existence time; the cubic case follows the Young step. \(z_{k+1}\le z_k+\delta z_k^3,\ N\delta (2B)^3\le B\Rightarrow z_k\le 2B\ (k\le N)\)

ns/RiccatiGen.lean

/-
  ns/RiccatiGen.lean — T37 cycle 179: the discrete Riccati comparison for any monotone growth, and the cubic case.

  Lean 4 core plus ns/Riccati (rep, le_add, le_mul_left, le_sq, rep_mono, le_ex), NSChain, lib/RingTac.
    riccati_gen   z_{k+1} ≤ z_k + δ g(z_k), g(x) ≤ G on [0, 2B], z_0 ≤ B, N·δG ≤ B  ⇒  z_k ≤ 2B (k ≤ N)
    cube_mono     0 ≤ x ≤ y  ⇒  x³ ≤ y³
    riccati3      z_{k+1} ≤ z_k + δ z_k³, N·δ(2B)³ ≤ B  ⇒  z_k ≤ 2B (k ≤ N)
  With Young34 (z' ≤ z + h(κ⁴/4ν³)z³): the 3D local existence time from the measured stretching bound,
  N h ≤ ν³/(2κ⁴Z_0²) (δ = hκ⁴/(4ν³), B = Z_0), uniform in the truncation.
-/
import ns.Riccati

namespace RiccatiGen
open DetChain NSChain LibRing Riccati

variable {R : Type} (K : CRing R)

theorem riccati_gen_inv (P : Cone K) (z : Nat → R) (g : R → R) (B G δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hG : P.nn G)
    (hg : ∀ x, P.nn x → Le K P x (K.add B B) → Le K P (g x) G)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (g (z k)))))
    (h0 : Le K P (z 0) B) (hN : Le K P (rep K (K.mul δ G) N) B) :
    ∀ k, k ≤ N → Le K P (z k) (K.add B (rep K (K.mul δ G) k))
  | 0, _ => by
    show Le K P (z 0) (K.add B K.zero)
    rw [add_zero']; exact h0
  | k + 1, hk => by
    have q_nn : P.nn (K.mul δ G) := P.nn_mul _ _ hδ hG
    have ih := riccati_gen_inv P z g B G δ N hz hδ hG hg step h0 hN k (Nat.le_of_succ_le hk)
    cases le_ex (Nat.le_of_succ_le hk) with
    | intro j hj =>
    have r : Le K P (rep K (K.mul δ G) k) B := by
      have m := rep_mono K P _ q_nn k j
      rw [← hj] at m
      exact le_trans' K P _ _ _ m hN
    have z2 : Le K P (z k) (K.add B B) := le_trans' K P _ _ _ ih (le_add K P _ _ _ _ (le_refl' K P B) r)
    have gg := Riccati.le_mul_left K P δ _ _ hδ (hg (z k) (hz k) z2)
    refine le_trans' K P _ _ _ (step k) ?_
    have e : K.add B (rep K (K.mul δ G) (k + 1)) = K.add (K.add B (rep K (K.mul δ G) k)) (K.mul δ G) := by
      show K.add B (K.add _ _) = _; rw [K.add_assoc]
    rw [e]
    exact le_add K P _ _ _ _ ih gg

/-- **Discrete local existence for any monotone growth.** -/
theorem riccati_gen (P : Cone K) (z : Nat → R) (g : R → R) (B G δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hG : P.nn G)
    (hg : ∀ x, P.nn x → Le K P x (K.add B B) → Le K P (g x) G)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (g (z k)))))
    (h0 : Le K P (z 0) B) (hN : Le K P (rep K (K.mul δ G) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) := by
  cases le_ex hk with
  | intro j hj =>
  have m := rep_mono K P _ (P.nn_mul _ _ hδ hG) k j
  rw [← hj] at m
  exact le_trans' K P _ _ _ (riccati_gen_inv K P z g B G δ N hz hδ hG hg step h0 hN k hk)
    (le_add K P _ _ _ _ (le_refl' K P B) (le_trans' K P _ _ _ m hN))

theorem cube_mono (P : Cone K) (x y : R) (hx : P.nn x) (h : Le K P x y) :
    Le K P (K.mul x (K.mul x x)) (K.mul y (K.mul y y)) := by
  cases h with
  | intro d h =>
    -- y³ − x³ = d(3x² + 3xd + d²) with x, d ≥ 0
    refine ⟨K.add (K.add (K.mul d (K.add (K.add (K.mul x x) (K.mul x x)) (K.mul x x)))
        (K.mul (K.mul d d) (K.add (K.add x x) x))) (K.mul d (K.mul d d)), ?_, ?_⟩
    · have hd := h.1
      have hxx := P.nn_sq x
      exact P.nn_add _ _ (P.nn_add _ _ (P.nn_mul _ _ hd (P.nn_add _ _ (P.nn_add _ _ hxx hxx) hxx))
        (P.nn_mul _ _ (P.nn_sq d) (P.nn_add _ _ (P.nn_add _ _ hx hx) hx))) (P.nn_mul _ _ hd (P.nn_sq d))
    · rw [← h.2]; lib_ring

/-- **Cubic Riccati (3D enstrophy with the stretching bound).** -/
theorem riccati3 (P : Cone K) (z : Nat → R) (B δ : R) (N : Nat)
    (hz : ∀ k, P.nn (z k)) (hδ : P.nn δ) (hB : P.nn B)
    (step : ∀ k, Le K P (z (k + 1)) (K.add (z k) (K.mul δ (K.mul (z k) (K.mul (z k) (z k))))))
    (h0 : Le K P (z 0) B)
    (hN : Le K P (rep K (K.mul δ (K.mul (K.add B B) (K.mul (K.add B B) (K.add B B)))) N) B)
    (k : Nat) (hk : k ≤ N) : Le K P (z k) (K.add B B) :=
  riccati_gen K P z (fun x => K.mul x (K.mul x x)) B _ δ N hz hδ
    (P.nn_mul _ _ (P.nn_add _ _ hB hB) (P.nn_sq _))
    (fun x hx hxB => cube_mono K P x _ hx hxB) step h0 hN k hk

end RiccatiGen

#print axioms RiccatiGen.riccati_gen_inv
#print axioms RiccatiGen.riccati_gen
#print axioms RiccatiGen.cube_mono
#print axioms RiccatiGen.riccati3

#print axioms: 4 theorems, no axioms.

79.69 Log-convexity of the Sobolev norms

Every Sobolev norm of a truncation is bounded by its two neighbours. \(H_{j+1}^2\le H_jH_{j+2},\qquad H_j=\sum _i(\kappa _i^ja_i)^2\)

ns/LogConvex.lean

/-
  ns/LogConvex.lean — T37 cycle 182: log-convexity of the Sobolev norms of a truncation, every order.

  Lean 4 core plus ns/SumCS (cs_sum, sq), NSChain (lsum, lsum_ext), lib/Ring (pw), lib/RingTac. For modes i with
  |k_i| = κ_i and amplitudes a_i, write H_j = Σ_i (κ_i^j a_i)². Then for every j
      H_{j+1}² ≤ H_j · H_{j+2}
  (Cauchy–Schwarz with A = κ^j a, B = κ^{j+2} a). j = 0 is Interp (Z² ≤ E P); j = 1 is P² ≤ Z·‖∇Δu‖². log H_j
  is convex in j: two norms control every norm between them, none above.
-/
import ns.SumCS

namespace LogConvex
open DetChain NSChain LibRing SumCS

variable {R : Type} (K : CRing R) {I : Type}

def H (κ a : I → R) (j : Nat) (l : List I) : R := lsum K (fun i => sq K (K.mul (pw K (κ i) j) (a i))) l

theorem logconvex (P : Cone K) (κ a : I → R) (j : Nat) (l : List I) :
    Le K P (sq K (H K κ a (j + 1) l)) (K.mul (H K κ a j l) (H K κ a (j + 2) l)) := by
  have cs := cs_sum K P (fun i => K.mul (pw K (κ i) j) (a i))
    (fun i => K.mul (K.mul (K.mul (pw K (κ i) j) (κ i)) (κ i)) (a i)) l
  have e1 : lsum K (fun i => K.mul (K.mul (pw K (κ i) j) (a i)) (K.mul (K.mul (K.mul (pw K (κ i) j) (κ i)) (κ i)) (a i))) l
      = H K κ a (j + 1) l :=
    lsum_ext K _ _ (fun i => by show _ = sq K (K.mul (K.mul (pw K (κ i) j) (κ i)) (a i)); unfold sq; lib_ring) l
  rw [e1] at cs
  exact cs

end LogConvex

#print axioms LogConvex.logconvex

#print axioms: 1 theorem, no axioms.

79.70 Serrin continuation from \(\sup Z\) and \(\int \|\Delta u\|^2\)

Prefix Cauchy–Schwarz and Agmon bound the Serrin sum; the bound closes on itself. \(A^2NCM\sum _{k<N}P_k\le q^2\Rightarrow Z_N(1-q)\le Z_0\)

ns/SerrinAgmon.lean

/-
  ns/SerrinAgmon.lean — T37 cycle 185: the discrete L^∞ continuation criterion from sup Z and ∫‖Δu‖².

  Lean 4 core plus ns/SerrinRun, lib/OField, lib/RingTac. Over the prefix k < N of a run, with
      X_k⁴ ≤ C Z_k P_k   (Agmon),   A = h c² / (2ν),   S_N = A Σ_{k<N} X_k²,
  prefix Cauchy–Schwarz (by the Lagrange identity, no lists) gives (Σ X²)² ≤ N · C · M · Σ P when Z_k ≤ M, so
      A² · N · C · M · Σ_{k<N} P_k ≤ q²,  0 < q ≤ 1   ⇒   Z_N (1 − q) ≤ Z_0                     (serrin_agmon)
  and, choosing M with Z_0 ≤ M (1 − q), q < 1, the bound closes on itself:
      budget for every N  ⇒  Z_N ≤ M for every N                                            (continuation)
  Enstrophy stays bounded as long as N·Σ_{k<N}‖Δu_k‖² stays below q²/(A² C M): the R1-gap in discrete form.
-/
import ns.SerrinRun

namespace SerrinAgmon
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum YoungStep SerrinRun

variable {R : Type} (K : CRing R) (P : Pos K)

/-! ## Prefix sums -/

theorem sumA_succ (a : Nat → R) (n : Nat) : sumA K a (n + 1) = K.add (sumA K a n) (a n) := rfl

theorem sumA_ext (a b : Nat → R) (h : ∀ k, a k = b k) : ∀ n, sumA K a n = sumA K b n
  | 0 => rfl
  | n + 1 => by rw [sumA_succ, sumA_succ, sumA_ext a b h n, h n]

theorem sumA_mul (c : R) (a : Nat → R) : ∀ n, sumA K (fun k => K.mul c (a k)) n = K.mul c (sumA K a n)
  | 0 => by show K.zero = K.mul c K.zero; lib_ring
  | n + 1 => by rw [sumA_succ, sumA_succ, sumA_mul c a n]; lib_ring

theorem sumA_le (u v : Nat → R) : ∀ n, (∀ k, k < n → le K P (u k) (v k)) →
    le K P (sumA K u n) (sumA K v n)
  | 0, _ => le_refl K P _
  | n + 1, h => by
    rw [sumA_succ, sumA_succ]
    exact le_add2 K P (sumA_le u v n (fun k hk => h k (Nat.lt_succ_of_lt hk))) (h n (Nat.lt_succ_self n))

/-! ## Prefix Cauchy–Schwarz by the Lagrange identity -/

def lagIn (x : Nat → R) (y : R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (lagIn x y n) (K.mul (sub K (x n) y) (sub K (x n) y))

def lagD (x : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (lagD x n) (lagIn K x (x n) n)

theorem lagIn_eq (x : Nat → R) (y : R) : ∀ n,
    lagIn K x y n = K.add (sub K (sumA K (fun i => K.mul (x i) (x i)) n) (K.mul (K.add y y) (sumA K x n)))
      (K.mul (castN K n) (K.mul y y))
  | 0 => by
    show K.zero = K.add (sub K K.zero (K.mul (K.add y y) K.zero)) (K.mul K.zero (K.mul y y)); lib_ring
  | n + 1 => by
    have e : lagIn K x y (n + 1) = K.add (lagIn K x y n) (K.mul (sub K (x n) y) (sub K (x n) y)) := rfl
    have c : castN K (n + 1) = K.add K.one (castN K n) := rfl
    rw [e, lagIn_eq x y n, sumA_succ, sumA_succ, c]; lib_ring

theorem lagrange (x : Nat → R) : ∀ n,
    sub K (K.mul (castN K n) (sumA K (fun i => K.mul (x i) (x i)) n)) (K.mul (sumA K x n) (sumA K x n)) = lagD K x n
  | 0 => by show sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero; lib_ring
  | n + 1 => by
    have e : lagD K x (n + 1) = K.add (lagD K x n) (lagIn K x (x n) n) := rfl
    have c : castN K (n + 1) = K.add K.one (castN K n) := rfl
    rw [e, ← lagrange x n, lagIn_eq, sumA_succ, sumA_succ, c]; lib_ring

theorem lagIn_nn (x : Nat → R) (y : R) : ∀ n, P.nn (lagIn K x y n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (lagIn_nn x y n) (P.nn_sq _)

theorem lagD_nn (x : Nat → R) : ∀ n, P.nn (lagD K x n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (lagD_nn x n) (lagIn_nn K P x (x n) n)

/-- (Σ_{k<n} x_k)² ≤ n Σ_{k<n} x_k². -/
theorem cs_prefix (x : Nat → R) (n : Nat) :
    le K P (K.mul (sumA K x n) (sumA K x n)) (K.mul (castN K n) (sumA K (fun i => K.mul (x i) (x i)) n)) := by
  unfold le; rw [lagrange]; exact lagD_nn K P x n

/-- **Agmon over a prefix.** (Σ_{k<n} X_k²)² ≤ n · (C M) · Σ_{k<n} P_k. -/
theorem agmon_prefix (X Z Pp : Nat → R) (C M : R) (n : Nat) (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (hZ : ∀ k, k < n → le K P (Z k) M) :
    le K P (K.mul (sumA K (fun k => K.mul (X k) (X k)) n) (sumA K (fun k => K.mul (X k) (X k)) n))
      (K.mul (castN K n) (K.mul (K.mul C M) (sumA K Pp n))) := by
  have cs := cs_prefix K P (fun k => K.mul (X k) (X k)) n
  have s1 := sumA_le K P _ _ n (fun k _ => hA k)
  have s2 := sumA_le K P (fun k => K.mul C (K.mul (Z k) (Pp k))) (fun k => K.mul (K.mul C M) (Pp k)) n
    (fun k hk => by
      have m := le_mul_left K P (P.nn_mul _ _ hC (hP k)) (hZ k hk)
      rw [show K.mul (K.mul C (Pp k)) (Z k) = K.mul C (K.mul (Z k) (Pp k)) by lib_ring,
          show K.mul (K.mul C (Pp k)) M = K.mul (K.mul C M) (Pp k) by lib_ring] at m
      exact m)
  rw [sumA_mul K (K.mul C M) Pp n] at s2
  exact le_trans K P cs (le_mul_left K P (nn_castN K P n) (le_trans K P s1 s2))

/-! ## Square roots without roots -/

theorem le_of_sq (F : OF K P) (S q : R) (hS : P.nn S) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (h : le K P (K.mul S S) (K.mul q q)) : le K P S q := by
  have hqs : K.add q S ≠ K.zero := fun e => by
    have en : K.neg q = S := by
      rw [show K.neg q = K.add (K.add q S) (K.neg q) by rw [e]; lib_ring]; lib_ring
    exact hq0 (F.antisymm q hq (by rw [en]; exact hS))
  have hi := F.mul_inv _ hqs
  have e : K.mul (sub K (K.mul q q) (K.mul S S)) (F.inv (K.add q S))
      = K.mul (sub K q S) (K.mul (K.add q S) (F.inv (K.add q S))) := by lib_ring
  rw [hi] at e
  unfold le
  rw [show sub K q S = K.mul (sub K q S) K.one by lib_ring, ← e]
  exact P.nn_mul _ _ h (nn_inv K P F _ (P.nn_add _ _ hq hS) hqs)

/-! ## The criterion -/

def amp (F : OF K P) (h c ν : R) : R := K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul c c))

theorem sum_aRun (F : OF K P) (h c ν : R) (X : Nat → R) (n : Nat) :
    sumA K (aRun K P F h c ν X) n = K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) n) := by
  rw [← sumA_mul]
  exact sumA_ext K _ _ (fun k => by show K.mul _ _ = K.mul _ _; unfold amp; lib_ring) n

/-- **Discrete L^∞ continuation over a prefix.** -/
theorem serrin_agmon (F : OF K P) (Z r s X Pp : Nat → R) (h c ν C M q : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (N : Nat) (hZ : ∀ k, k < N → le K P (Z k) M)
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q K.one)
    (budget : le K P (K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
      (K.mul (castN K N) (K.mul (K.mul C M) (sumA K Pp N)))) (K.mul q q)) :
    le K P (K.mul (Z N) (sub K K.one q)) (Z 0) := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  -- S² = A² T² ≤ A² · N C M ΣP ≤ q²
  have ag := le_mul_left K P (P.nn_sq (amp K P F h c ν)) (agmon_prefix K P X Z Pp C M N hC hP hA hZ)
  have sq : le K P (K.mul (sumA K (aRun K P F h c ν X) N) (sumA K (aRun K P F h c ν X) N)) (K.mul q q) := by
    rw [sum_aRun]
    rw [show K.mul (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))
          (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))
        = K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
          (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N)) by lib_ring]
    exact le_trans K P ag budget
  have Sq := le_of_sq K P F _ q (sumA_nn K P _ ha N) hq hq0 sq
  -- 1 − S ≥ 1 − q ≥ 0
  have hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N)) := by
    have e : sub K K.one (sumA K (aRun K P F h c ν X) N)
        = K.add (sub K K.one q) (sub K q (sumA K (aRun K P F h c ν X) N)) := by lib_ring
    rw [e]; exact P.nn_add _ _ hq1 Sq
  have hZ0 : P.nn (Z 0) := by rw [← hr 0]; exact P.nn_sq _
  have run := serrin_run K P F Z r s X h c ν hh hν hν0 hr hZ0 step N hS
  have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
  -- Z_N (1 − q) ≤ Z_N (1 − S)
  have m := le_mul_left K P hZN (show le K P (sub K K.one q) (sub K K.one (sumA K (aRun K P F h c ν X) N)) by
    unfold le
    rw [show sub K (sub K K.one (sumA K (aRun K P F h c ν X) N)) (sub K K.one q)
        = sub K q (sumA K (aRun K P F h c ν X) N) by lib_ring]
    exact Sq)
  exact le_trans K P m run

theorem lt_succ_cases {k n : Nat} (h : k < n + 1) : k < n ∨ k = n :=
  Nat.lt_or_eq_of_le (Nat.le_of_lt_succ h)

/-- **Continuation: the bound closes on itself.** -/
theorem continuation (F : OF K P) (Z r s X Pp : Nat → R) (h c ν C M q : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hP : ∀ k, P.nn (Pp k))
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (Pp k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q K.one) (hq1' : sub K K.one q ≠ K.zero)
    (hM : le K P (Z 0) (K.mul M (sub K K.one q)))
    (budget : ∀ N, le K P (K.mul (K.mul (amp K P F h c ν) (amp K P F h c ν))
      (K.mul (castN K N) (K.mul (K.mul C M) (sumA K Pp N)))) (K.mul q q)) :
    ∀ N, le K P (Z N) M := by
  have claim : ∀ N, ∀ k, k < N → le K P (Z k) M := by
    intro N
    induction N with
    | zero => intro k hk; exact absurd hk (Nat.not_lt_zero k)
    | succ n ih =>
      intro k hk
      cases lt_succ_cases hk with
      | inl hl => exact ih k hl
      | inr he =>
        rw [he]
        have b := serrin_agmon K P F Z r s X Pp h c ν C M q hh hν hν0 hr step hC hP hA n ih hq hq0 hq1 (budget n)
        -- (M − Z_n)(1 − q) ≥ 0, then divide by 1 − q
        have t := le_trans K P b hM
        unfold le at t ⊢
        have hi := F.mul_inv _ hq1'
        have e : K.mul (sub K (K.mul M (sub K K.one q)) (K.mul (Z n) (sub K K.one q))) (F.inv (sub K K.one q))
            = K.mul (sub K M (Z n)) (K.mul (sub K K.one q) (F.inv (sub K K.one q))) := by lib_ring
        rw [hi] at e
        rw [show sub K M (Z n) = K.mul (sub K M (Z n)) K.one by lib_ring, ← e]
        exact P.nn_mul _ _ t (nn_inv K P F _ hq1 hq1')
  exact fun N => claim (N + 1) N (Nat.lt_succ_self N)

end SerrinAgmon

#print axioms SerrinAgmon.sumA_succ
#print axioms SerrinAgmon.sumA_ext
#print axioms SerrinAgmon.sumA_mul
#print axioms SerrinAgmon.sumA_le
#print axioms SerrinAgmon.lagIn_eq
#print axioms SerrinAgmon.lagrange
#print axioms SerrinAgmon.lagIn_nn
#print axioms SerrinAgmon.lagD_nn
#print axioms SerrinAgmon.cs_prefix
#print axioms SerrinAgmon.agmon_prefix
#print axioms SerrinAgmon.le_of_sq
#print axioms SerrinAgmon.sum_aRun
#print axioms SerrinAgmon.serrin_agmon
#print axioms SerrinAgmon.lt_succ_cases
#print axioms SerrinAgmon.continuation

#print axioms: 15 theorems, no axioms.

79.71 Dissipation budget

The dissipation of a run is bounded by the initial enstrophy and the Serrin integrand. \(2Z_N+h\nu \sum _{k<N}s_k^2\le 2Z_0+2\sum _{k<N}a_kZ_k\)

ns/DissBudget.lean

/-
  ns/DissBudget.lean — T37 cycle 740 (goal G453): the exact dissipation budget of a Crank–Nicolson Galerkin scheme
  over any commutative ring, and its bound over any ordered ring (ℚ included).

  Lean 4 core plus NSChain (cn_energy, dissipation_nonneg), lib/RingTac (lib_ring).
    budget       E(T) + Σ_{t<T} D_t = E(0),  D_t = h ν Σ_n k_n (a_{t+1}(n) + a_t(n))²     (exact)
    budget_le    Σ_{t<T} D_t ≤ E(0)      (only E(T) ≥ 0 is used)
  The total dissipation over all time is bounded by the initial energy: the discrete form of
  ν ∫₀^∞ ‖∇u‖² dt ≤ ½‖u₀‖².
-/
import NSChain
import lib.RingTac

namespace DissBudget
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

def En (a : Nat → Nat → R) (N t : Nat) : R := sumN K (fun n => K.mul (a t n) (a t n)) N
def D (a : Nat → Nat → R) (k : Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N))

theorem budget (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) :
    ∀ T, K.add (En K a N T) (sumN K (D K a k h nu N) T) = En K a N 0
  | 0 => by show K.add (En K a N 0) K.zero = En K a N 0; lib_ring
  | T + 1 => by
    have e := cn_energy K (a T) (a (T + 1)) k (Nn T) h nu N (step T) (neutral T)
    have ih := budget a k Nn h nu N step neutral T
    show K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T)) = En K a N 0
    have r : K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T))
        = K.add (K.add (En K a N (T + 1)) (D K a k h nu N T)) (sumN K (D K a k h nu N) T) := by lib_ring
    rw [r]
    have e' : K.add (En K a N (T + 1)) (D K a k h nu N T) = En K a N T := e
    rw [e', ih]

theorem budget_le (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    Le K P (sumN K (D K a k h nu N) T) (En K a N 0) :=
  ⟨En K a N T, sq_sum_nonneg K P (a T) N, by
    rw [K.add_comm]; exact budget K a k Nn h nu N step neutral T⟩

end Gen

end DissBudget

#print axioms DissBudget.budget
#print axioms DissBudget.budget_le

#print axioms: 2 theorems, no axioms.

79.72 The Serrin budget bounds itself

Dissipation and Agmon give a quadratic inequality for \(T=\sum \|u_k\|_\infty ^2\). \(h\nu T^2\le 2NCM(Z_0+MAT),\qquad T^2\le \alpha +\beta T\Rightarrow T\le \beta +q\)

ns/SelfBudget.lean

/-
  ns/SelfBudget.lean — T37 cycle 191: the Serrin budget bounds itself (quadratic inequality), short-time form.

  Lean 4 core plus ns/DissBudget, ns/SerrinAgmon, lib/OField, lib/RingTac. For a run with
      Z_{k+1} ≤ Z_k + h (c X_k r_k s_k − ν s_k²),  r_k² = Z_k,  X_k⁴ ≤ C Z_k s_k²  (Agmon, P_k = s_k²),
  Z_k ≤ M for k < N, and T = Σ_{k<N} X_k², A = h c²/(2ν):
      h ν T² ≤ (2 N C M) (Z_0 + M A T)                                                      (self_budget)
  (DissBudget bounds hνΣs² by 2Z_0 + 2MAT; prefix Cauchy–Schwarz and Agmon bound T² by N C M Σs².) And for any
  ordered field, the root-free reading of a quadratic inequality:
      T² ≤ α + β T,  β, q ≥ 0,  α ≤ q²,  q ≠ 0   ⇒   T ≤ β + q                                (quad_root)
  So T stays below the positive root: the budget A·T is small for N h ≲ ν³/(c⁴ C M²) — the local time of RiccatiGen.
-/
import ns.DissBudget

namespace SelfBudget
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget

variable {R : Type} (K : CRing R) (P : Pos K)

/-- **Root-free quadratic bound.** -/
theorem quad_root (F : OF K P) (T α β q : R) (hβ : P.nn β) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (hα : le K P α (K.mul q q)) (h : le K P (K.mul T T) (K.add α (K.mul β T))) :
    le K P T (K.add β q) := by
  cases le_total K P F T β with
  | inl hl =>
    unfold le at hl ⊢
    rw [show sub K (K.add β q) T = K.add (sub K β T) q by lib_ring]
    exact P.nn_add _ _ hl hq
  | inr hg =>
    -- d = T − β ≥ 0:  d² = d T − β d ≤ d T = T² − β T ≤ α ≤ q²
    have hd : P.nn (sub K T β) := hg
    have sq : le K P (K.mul (sub K T β) (sub K T β)) (K.mul q q) := by
      unfold le at h hα ⊢
      have e : sub K (K.mul q q) (K.mul (sub K T β) (sub K T β))
          = K.add (K.add (sub K (K.mul q q) α) (sub K (K.add α (K.mul β T)) (K.mul T T)))
              (K.mul β (sub K T β)) := by lib_ring
      rw [e]; exact P.nn_add _ _ (P.nn_add _ _ hα h) (P.nn_mul _ _ hβ hd)
    have r := le_of_sq K P F _ q hd hq hq0 sq
    unfold le at r ⊢
    rw [show sub K (K.add β q) T = sub K q (sub K T β) by lib_ring]
    exact r

/-- **The Serrin budget bounds itself.** -/
theorem self_budget (F : OF K P) (Z r s X : Nat → R) (h c ν C M : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hM : P.nn M)
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (K.mul (s k) (s k)))))
    (N : Nat) (hZ : ∀ k, k < N → le K P (Z k) M) :
    le K P (K.mul (K.mul h ν) (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N)))
      (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M)))
        (K.add (Z 0) (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))))) := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  -- (1) dissipation: hν Σs² ≤ 2 Z_0 + 2 Σ a_k Z_k
  have db := diss_budget K P F Z r s X h c ν hh hν hν0 hr step N
  have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
  -- Σ a_k Z_k ≤ M Σ a_k = M A T
  have az := sumA_le K P (fun k => K.mul (aRun K P F h c ν X k) (Z k)) (fun k => K.mul M (aRun K P F h c ν X k)) N
    (fun k hk => by
      have m := le_mul_left K P (ha k) (hZ k hk)
      rw [show K.mul (aRun K P F h c ν X k) M = K.mul M (aRun K P F h c ν X k) by lib_ring] at m
      exact m)
  rw [sumA_mul K M, sum_aRun] at az
  have ds : le K P (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N))
      (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
        (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N))))) := by
    have t := le_trans K P db (le_add2 K P (le_refl K P (K.add (Z 0) (Z 0))) (le_add2 K P az az))
    unfold le at t ⊢
    rw [show sub K (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
          (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))))
          (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N))
        = K.add (sub K (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))
          (K.mul M (K.mul (amp K P F h c ν) (sumA K (fun k => K.mul (X k) (X k)) N)))))
          (K.add (K.add (Z N) (Z N)) (K.mul (K.mul h ν) (sumA K (fun k => K.mul (s k) (s k)) N)))) (K.add (Z N) (Z N)) by lib_ring]
    exact P.nn_add _ _ t (P.nn_add _ _ hZN hZN)
  -- (2) Agmon prefix: T² ≤ N (C M) Σs²
  have ag := agmon_prefix K P X Z (fun k => K.mul (s k) (s k)) C M N hC (fun k => P.nn_sq _) hA hZ
  -- hν T² ≤ N C M · hν Σs² ≤ N C M (2Z_0 + 2MAT)
  have m1 := le_mul_left K P (P.nn_mul _ _ hh hν) ag
  have m2 := le_mul_left K P (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM)) ds
  generalize sumA K (fun k => K.mul (X k) (X k)) N = T at m1 m2 ⊢
  generalize sumA K (fun k => K.mul (s k) (s k)) N = S at m1 m2
  generalize amp K P F h c ν = a at m1 m2 ⊢
  generalize castN K N = n at m1 m2 ⊢
  unfold le at m1 m2 ⊢
  have e : sub K (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))))
        (K.mul (K.mul h ν) (K.mul T T))
      = K.add (sub K (K.mul (K.mul n (K.mul C M)) (K.add (K.add (Z 0) (Z 0)) (K.add (K.mul M (K.mul a T)) (K.mul M (K.mul a T)))))
          (K.mul (K.mul n (K.mul C M)) (K.mul (K.mul h ν) S)))
        (sub K (K.mul (K.mul h ν) (K.mul n (K.mul (K.mul C M) S))) (K.mul (K.mul h ν) (K.mul T T))) := by lib_ring
  rw [e]; exact P.nn_add _ _ m2 m1

end SelfBudget

#print axioms SelfBudget.quad_root
#print axioms SelfBudget.self_budget

#print axioms: 2 theorems, no axioms.

79.73 Local continuation from Agmon

No hypothesis on \(\|u\|_\infty \): Agmon alone keeps the enstrophy below \(M\) for a time. \(\alpha _N\le q^2,\ A(\beta _N+q)\le q_1\ (N\le N_{\max })\Rightarrow Z_N\le M\)

ns/LocalCont.lean

/-
  ns/LocalCont.lean — T37 cycle 194: discrete local continuation from Agmon alone (no assumption on ‖u‖_∞).

  Lean 4 core plus ns/SelfBudget, lib/OField, lib/RingTac. For a run with the stretching step, r_k² = Z_k and the
  Agmon bound X_k⁴ ≤ C Z_k s_k², put A = h c²/(2ν) and, for N steps,
      α_N = (hν)⁻¹ · 2NCM · Z_0,      β_N = (hν)⁻¹ · 2NCM · M A .
  If Z_0 ≤ M(1 − q₁) and for every N ≤ N_max:  α_N ≤ q²  and  A(β_N + q) ≤ q₁  (0 < q, 0 < q₁ < 1), then
      Z_N ≤ M  for every N ≤ N_max                                                     (local_cont)
  The proof closes the loop: Z ≤ M on [0, N) ⇒ (SelfBudget) T² ≤ α + βT ⇒ (quad_root) T ≤ β + q ⇒ the Serrin sum
  A·T ≤ q₁ ⇒ (serrin_run) Z_N(1 − q₁) ≤ Z_0 ≤ M(1 − q₁). Both conditions hold for N h ≲ ν³/(c⁴ C M²): a local
  existence time obtained from the Serrin route, the same order as the Riccati route (RiccatiGen).
-/
import ns.SelfBudget

namespace LocalCont
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget

variable {R : Type} (K : CRing R) (P : Pos K)

def alpha (F : OF K P) (h ν C M Z0 : R) (N : Nat) : R :=
  K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M))) Z0)

def beta (F : OF K P) (h c ν C M : R) (N : Nat) : R :=
  K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul (castN K N) (K.mul C M)) (K.mul (castN K N) (K.mul C M)))
    (K.mul M (amp K P F h c ν)))

/-- dividing a nonnegative difference by a positive factor -/
theorem le_of_mul_le (F : OF K P) (x y d : R) (hd : P.nn d) (hd0 : d ≠ K.zero)
    (h : le K P (K.mul d x) (K.mul d y)) : le K P x y := by
  have hi := F.mul_inv _ hd0
  unfold le at h ⊢
  have e : K.mul (F.inv d) (sub K (K.mul d y) (K.mul d x)) = K.mul (sub K y x) (K.mul d (F.inv d)) := by lib_ring
  rw [hi] at e
  rw [show sub K y x = K.mul (sub K y x) K.one by lib_ring, ← e]
  exact P.nn_mul _ _ (nn_inv K P F _ hd hd0) h

/-- **Local continuation.** -/
theorem local_cont (F : OF K P) (Z r s X : Nat → R) (h c ν C M q q1 : R) (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hC : P.nn C) (hM : P.nn M)
    (hA : ∀ k, le K P (K.mul (K.mul (X k) (X k)) (K.mul (X k) (X k))) (K.mul C (K.mul (Z k) (K.mul (s k) (s k)))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν C M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν) (K.add (beta K P F h c ν C M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M := by
  have hamp : P.nn (amp K P F h c ν) :=
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0) (P.nn_mul _ _ hh (P.nn_sq c))
  have hhν' : P.nn (K.mul h ν) := P.nn_mul _ _ hh hν
  have hinv := nn_inv K P F _ hhν' hhν
  have hi := F.mul_inv _ hhν
  have hone := nn_one K P
  have ha : ∀ k, P.nn (aRun K P F h c ν X k) := fun k =>
    P.nn_mul _ _ (nn_inv K P F _ (P.nn_add _ _ hν hν) hν0)
      (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X k))))
  have one_step : ∀ N, N ≤ Nmax → (∀ k, k < N → le K P (Z k) M) → le K P (Z N) M := by
    intro N hN hZ
    have sb := self_budget K P F Z r s X h c ν C M hh hν hν0 hr step hC hM hA N hZ
    have hβnn : P.nn (beta K P F h c ν C M N) :=
      P.nn_mul _ _ hinv (P.nn_mul _ _ (P.nn_add _ _ (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM))
        (P.nn_mul _ _ (nn_castN K P N) (P.nn_mul _ _ hC hM))) (P.nn_mul _ _ hM hamp))
    -- divide by hν:  T² ≤ α + β T
    have quad : le K P (K.mul (sumA K (fun k => K.mul (X k) (X k)) N) (sumA K (fun k => K.mul (X k) (X k)) N))
        (K.add (alpha K P F h ν C M (Z 0) N) (K.mul (beta K P F h c ν C M N) (sumA K (fun k => K.mul (X k) (X k)) N))) := by
      apply le_of_mul_le K P F _ _ (K.mul h ν) hhν' hhν
      unfold alpha beta
      generalize sumA K (fun k => K.mul (X k) (X k)) N = T at sb ⊢
      generalize amp K P F h c ν = a at sb ⊢
      generalize castN K N = n at sb ⊢
      have e : K.mul (K.mul h ν) (K.add (K.mul (F.inv (K.mul h ν))
            (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (Z 0)))
          (K.mul (K.mul (F.inv (K.mul h ν)) (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.mul M a))) T))
          = K.mul (K.mul (K.mul h ν) (F.inv (K.mul h ν)))
            (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T)))) := by lib_ring
      rw [e, hi, show K.mul K.one (K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))))
        = K.mul (K.add (K.mul n (K.mul C M)) (K.mul n (K.mul C M))) (K.add (Z 0) (K.mul M (K.mul a T))) by lib_ring]
      exact sb
    have Tb := quad_root K P F _ _ _ q hβnn hq hq0 (hα N hN) quad
    -- the Serrin sum: S = A T ≤ A (β + q) ≤ q₁
    have Sb : le K P (sumA K (aRun K P F h c ν X) N) q1 := by
      rw [sum_aRun]
      exact le_trans K P (le_mul_left K P hamp Tb) (hβ N hN)
    have hS : P.nn (sub K K.one (sumA K (aRun K P F h c ν X) N)) := by
      have e : sub K K.one (sumA K (aRun K P F h c ν X) N)
          = K.add (sub K K.one q1) (sub K q1 (sumA K (aRun K P F h c ν X) N)) := by lib_ring
      rw [e]; exact P.nn_add _ _ hq1 Sb
    have hZ0 : P.nn (Z 0) := by rw [← hr 0]; exact P.nn_sq _
    have run := serrin_run K P F Z r s X h c ν hh hν hν0 hr hZ0 step N hS
    have hZN : P.nn (Z N) := by rw [← hr N]; exact P.nn_sq _
    have m := le_mul_left K P hZN (show le K P (sub K K.one q1) (sub K K.one (sumA K (aRun K P F h c ν X) N)) by
      unfold le
      rw [show sub K (sub K K.one (sumA K (aRun K P F h c ν X) N)) (sub K K.one q1)
          = sub K q1 (sumA K (aRun K P F h c ν X) N) by lib_ring]
      exact Sb)
    have t := le_trans K P (le_trans K P m run) hM0
    -- Z_N (1 − q₁) ≤ M (1 − q₁)  ⇒  Z_N ≤ M
    apply le_of_mul_le K P F _ _ (sub K K.one q1) hq1 hq10
    rw [show K.mul (sub K K.one q1) (Z N) = K.mul (Z N) (sub K K.one q1) by lib_ring,
        show K.mul (sub K K.one q1) M = K.mul M (sub K K.one q1) by lib_ring]
    exact t
  have claim : ∀ N, N ≤ Nmax → ∀ k, k ≤ N → le K P (Z k) M := by
    intro N
    induction N with
    | zero =>
      intro _ k hk
      rw [Nat.le_zero.mp hk]
      exact one_step 0 (Nat.zero_le _) (fun k hk => absurd hk (Nat.not_lt_zero k))
    | succ n ih =>
      intro hN k hk
      have ih' := ih (Nat.le_of_succ_le hN)
      cases Nat.lt_or_eq_of_le hk with
      | inl hl => exact ih' k (Nat.le_of_lt_succ hl)
      | inr he =>
        rw [he]
        exact one_step (n + 1) hN (fun j hj => ih' j (Nat.le_of_lt_succ hj))
  exact fun N hN => claim N hN N (Nat.le_refl N)

end LocalCont

#print axioms LocalCont.le_of_mul_le
#print axioms LocalCont.local_cont

#print axioms: 2 theorems, no axioms.

79.74 The two orders agree; Agmon proved

Cone and positivity orders coincide, so AgmonOpt feeds the local continuation. \(\mathrm {Le}\Leftrightarrow \mathrm {le},\quad X^4\le 2(a^2+b^2)ZP\)

ns/LocalContAgmon.lean

/-
  ns/LocalContAgmon.lean — T37 cycle 197: the two order frameworks agree, and LocalCont with the Agmon bound proved
  (AgmonOpt) instead of assumed.

  Lean 4 core plus ns/LocalCont, ns/AgmonOpt, lib/RingTac.
    toCone, le_of_Le, Le_of_le   a positivity cone (lib, `Pos`/`le`) is a cone of NSChain (`Cone`/`Le`), and the two
                                 orders coincide — so the Cone-side results (Agmon, AgmonOpt, TriadSum, …) and the
                                 Pos-side results (Gronwall, SerrinRun, LocalCont, …) combine
    agmon_step                   AgmonOpt in the form LocalCont needs: X⁴ ≤ C Z P with C = 2(a² + b²)
    local_cont_agmon             LocalCont whose Agmon hypothesis is replaced by the hypotheses of AgmonOpt at
                                 every step (X_k² ≤ a k_k Z_k + b P_k w_k, (k_k + 1) w_k = 1, k_k² Z_k ≤ P_k ≤
                                 (k_k + 1)² Z_k: the dyadic Agmon split at the cut k_k ≈ √(P_k/Z_k))
-/
import ns.LocalCont
import ns.AgmonOpt

namespace LocalContAgmon
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget
  LocalCont

variable {R : Type} (K : CRing R)

def toCone (P : Pos K) : NSChain.Cone K :=
  { nn := P.nn, nn_zero := P.nn_zero, nn_add := P.nn_add, nn_mul := P.nn_mul, nn_sq := P.nn_sq }

theorem le_of_Le (P : Pos K) (x y : R) (h : NSChain.Le K (toCone K P) x y) : le K P x y := by
  cases h with
  | intro d hd =>
    unfold le
    rw [← hd.2, show sub K (K.add x d) x = d by lib_ring]
    exact hd.1

theorem Le_of_le (P : Pos K) (x y : R) (h : le K P x y) : NSChain.Le K (toCone K P) x y :=
  ⟨sub K y x, h, by lib_ring⟩

/-- **AgmonOpt in LocalCont form.** -/
theorem agmon_step (P : Pos K) (X a b k w Z Pp : R) (hZ : P.nn Z) (hP : P.nn Pp)
    (hkw : K.mul (K.add k K.one) w = K.one)
    (h1 : le K P (K.mul (K.mul k k) Z) Pp) (h2 : le K P Pp (K.mul (K.mul (K.add k K.one) (K.add k K.one)) Z))
    (hXb : le K P (K.mul X X) (K.add (K.mul a (K.mul k Z)) (K.mul b (K.mul Pp w)))) :
    le K P (K.mul (K.mul X X) (K.mul X X))
      (K.mul (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) (K.mul Z Pp)) := by
  have o := AgmonOpt.opt K (toCone K P) X a b k w Z Pp (P.nn_sq X) hZ hP hkw
    (Le_of_le K P _ _ h1) (Le_of_le K P _ _ h2) (Le_of_le K P _ _ hXb)
  have l := le_of_Le K P _ _ o
  unfold le at l ⊢
  have e : sub K (K.mul (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) (K.mul Z Pp))
        (K.mul (K.mul X X) (K.mul X X))
      = sub K (K.add (K.add (K.mul (K.mul a a) (K.mul Z Pp)) (K.mul (K.mul a a) (K.mul Z Pp)))
             (K.add (K.mul (K.mul b b) (K.mul Z Pp)) (K.mul (K.mul b b) (K.mul Z Pp))))
          (K.mul (SumCS.sq K X) (SumCS.sq K X)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation with the Agmon bound proved.** -/
theorem local_cont_agmon (P : Pos K) (F : OF K P) (Z r s X kc w : Nat → R) (h c ν a b M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (X k)) (r k)) (s k)) (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hXb : ∀ k, le K P (K.mul (X k) (X k))
      (K.add (K.mul a (K.mul (kc k) (Z k))) (K.mul b (K.mul (K.mul (s k) (s k)) (w k)))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b)))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν (K.add (K.add (K.mul a a) (K.mul a a)) (K.add (K.mul b b) (K.mul b b))) M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M := by
  have hZ : ∀ k, P.nn (Z k) := fun k => by rw [← hr k]; exact P.nn_sq _
  exact local_cont K P F Z r s X h c ν _ M q q1 hh hν hν0 hhν hr step
    (P.nn_add _ _ (P.nn_add _ _ (P.nn_sq a) (P.nn_sq a)) (P.nn_add _ _ (P.nn_sq b) (P.nn_sq b))) hM
    (fun k => agmon_step K P (X k) a b (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hZ k) (P.nn_sq _) (hkw k) (h1 k) (h2 k)
      (hXb k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end LocalContAgmon

#print axioms LocalContAgmon.le_of_Le
#print axioms LocalContAgmon.Le_of_le
#print axioms LocalContAgmon.agmon_step
#print axioms LocalContAgmon.local_cont_agmon

#print axioms: 4 theorems, no axioms.

79.75 Local continuation from the Sobolev halves

The \(L^\infty \) input is the Wiener norm split into low and high shells. \(X_L^2\le 26kZ,\ X_H^2\le 26wP\Rightarrow X^2\le 52kZ+52Pw\)

ns/LocalContSobolev.lean

/-
  ns/LocalContSobolev.lean — T37 cycle 200: local continuation whose L^∞ input is the Wiener norm split into a low
  and a high part, each bounded by a Sobolev-type sum (Agmon's two halves).

  Lean 4 core plus ns/LocalContAgmon, ns/Agmon, lib/RingTac. With X = X_L + X_H (Σ_low |û| + Σ_high |û| ≥ ‖u‖_∞),
      X_L² ≤ 26k · Z,   X_H² ≤ 26w · P        (Agmon: W_low = 26K, W_high ≤ 26/(K + 1) = 26w)
  Agmon.agmon gives X² ≤ 52kZ + 52Pw, which is the split of AgmonOpt with a = b = 52:
    split_from_halves   X² ≤ 52 k Z + 52 P w
    local_cont_sobolev  local_cont_agmon with the split replaced by the two Sobolev halves at every step;
                        the constant is C = 2(52² + 52²) = 10816.
-/
import ns.LocalContAgmon
import ns.Agmon

namespace LocalContSobolev
open DetChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinRun SerrinAgmon DissBudget SelfBudget
  LocalCont LocalContAgmon

variable {R : Type} (K : CRing R)

def c26 : R := castN K 26
def c52 : R := castN K 52

theorem c52_eq : c52 K = K.add (c26 K) (c26 K) := by
  unfold c52 c26
  show castN K 52 = K.add (castN K 26) (castN K 26)
  -- castN is unary; 52 = 26 + 26 by induction on the second summand
  have h : ∀ a b : Nat, castN K (a + b) = K.add (castN K a) (castN K b) := by
    intro a b
    induction b with
    | zero => show castN K a = K.add (castN K a) K.zero; lib_ring
    | succ b ih =>
      show K.add K.one (castN K (a + b)) = K.add (castN K a) (K.add K.one (castN K b))
      rw [ih]; lib_ring
  exact h 26 26

/-- **The split from Agmon's two halves.** -/
theorem split_from_halves (P : Pos K) (XL XH k w Z Pp : R)
    (hL : le K P (K.mul XL XL) (K.mul (K.mul (c26 K) k) Z))
    (hH : le K P (K.mul XH XH) (K.mul (K.mul (c26 K) w) Pp)) :
    le K P (K.mul (K.add XL XH) (K.add XL XH))
      (K.add (K.mul (c52 K) (K.mul k Z)) (K.mul (c52 K) (K.mul Pp w))) := by
  have a := Agmon.agmon K (toCone K P) XL XH (K.mul (c26 K) k) (K.mul (c26 K) w) Z Pp
    (Le_of_le K P _ _ (by show le K P (K.mul XL XL) _; exact hL))
    (Le_of_le K P _ _ (by show le K P (K.mul XH XH) _; exact hH))
  have l := le_of_Le K P _ _ a
  unfold le at l ⊢
  rw [c52_eq]
  have e : sub K (K.add (K.mul (K.add (c26 K) (c26 K)) (K.mul k Z)) (K.mul (K.add (c26 K) (c26 K)) (K.mul Pp w)))
        (K.mul (K.add XL XH) (K.add XL XH))
      = sub K (K.add (K.add (K.mul (K.mul (c26 K) k) Z) (K.mul (K.mul (c26 K) k) Z))
          (K.add (K.mul (K.mul (c26 K) w) Pp) (K.mul (K.mul (c26 K) w) Pp)))
          (SumCS.sq K (K.add XL XH)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation from the Sobolev halves.** -/
theorem local_cont_sobolev (P : Pos K) (F : OF K P) (Z r s XL XH kc w : Nat → R) (h c ν M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (K.add (XL k) (XH k))) (r k)) (s k))
        (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hL : ∀ k, le K P (K.mul (XL k) (XL k)) (K.mul (K.mul (c26 K) (kc k)) (Z k)))
    (hH : ∀ k, le K P (K.mul (XH k) (XH k)) (K.mul (K.mul (c26 K) (w k)) (K.mul (s k) (s k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν
      (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))) (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν
        (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))) (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K))))
        M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M :=
  local_cont_agmon K P F Z r s (fun k => K.add (XL k) (XH k)) kc w h c ν (c52 K) (c52 K) M q q1 hh hν hν0 hhν hr
    step hM hkw h1 h2
    (fun k => split_from_halves K P (XL k) (XH k) (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hL k) (hH k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end LocalContSobolev

#print axioms LocalContSobolev.c52_eq
#print axioms LocalContSobolev.split_from_halves
#print axioms LocalContSobolev.local_cont_sobolev

#print axioms: 3 theorems, no axioms.

79.76 The stretching bound

Every triad sum of a truncation is bounded by the Wiener norm times \(\|\nabla u\|\,\|\Delta u\|\). \((\sum _pa_p\sum _qb_qc_{\sigma (p,q)})^2\le (\sum a)^2(\sum b^2)C\)

ns/TriadLinf.lean

/-
  ns/TriadLinf.lean — T37 cycle 203: the stretching bound in L^∞ × H¹ × H² form for any truncation.

  Lean 4 core plus ns/TriadSum (lsum_le, lsum_nn), ns/SumCS (cs_sum, sq), NSChain (lsum, lsum_mul_left), lib/RingTac.
    wdev_eq      Σ_j a_j (S_j − y)² = Q − 2yM + y²A        (A = Σa, M = ΣaS, Q = ΣaS²)
    wcs          a ≥ 0 ⇒ (Σ a S)² ≤ (Σ a)(Σ a S²)          (weighted Cauchy–Schwarz without square roots)
    triad_linf   a ≥ 0, (Σ_q b_q c_{σ(p,q)})² ≤ B for each p ⇒ (Σ_p a_p Σ_q b_q c_{σ(p,q)})² ≤ (Σ a)² B
    stretch      with cs_sum for the inner sum and Σ_q c_{σ(p,q)}² ≤ C:
                 (Σ_p a_p Σ_q b_q c_{σ(p,q)})² ≤ (Σ a)² (Σ b²) C
  With a_p = |û_p| (Σa = Wiener norm ≥ ‖u‖_∞), b_q = |k_q||û_q| (Σb² = Z), c_r = |k_r|²|û_r| (C = P, σ(p,·)
  injective): |stretching| ≤ X·√Z·√P — the bound c X r s of YoungStep / LocalCont with c = 1 (times the vector
  factors of TriadBound), for every truncation, no |l| factor.
-/
import ns.TriadSum

namespace TriadLinf
open DetChain NSChain LibRing SumCS TriadSum

variable {R : Type} (K : CRing R) {I : Type}

theorem wdev_eq (a S : I → R) (y : R) : ∀ l : List I,
    lsum K (fun j => K.mul (a j) (sq K (sub K (S j) y))) l
      = K.add (sub K (lsum K (fun j => K.mul (a j) (sq K (S j))) l)
          (K.mul (K.add y y) (lsum K (fun j => K.mul (a j) (S j)) l)))
          (K.mul (K.mul y y) (lsum K a l))
  | [] => by show K.zero = K.add (sub K K.zero (K.mul (K.add y y) K.zero)) (K.mul (K.mul y y) K.zero); lib_ring
  | j :: l => by
    show K.add (K.mul (a j) (sq K (sub K (S j) y))) (lsum K (fun j => K.mul (a j) (sq K (sub K (S j) y))) l)
      = K.add (sub K (K.add (K.mul (a j) (sq K (S j))) (lsum K (fun j => K.mul (a j) (sq K (S j))) l))
          (K.mul (K.add y y) (K.add (K.mul (a j) (S j)) (lsum K (fun j => K.mul (a j) (S j)) l))))
          (K.mul (K.mul y y) (K.add (a j) (lsum K a l)))
    rw [wdev_eq a S y l]; unfold sq; lib_ring

/-- **Weighted Cauchy–Schwarz.** -/
theorem wcs (P : Cone K) (a S : I → R) (ha : ∀ i, P.nn (a i)) : ∀ l : List I,
    Le K P (sq K (lsum K (fun i => K.mul (a i) (S i)) l))
      (K.mul (lsum K a l) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
  | [] => ⟨K.zero, P.nn_zero, by show K.add (sq K K.zero) K.zero = K.mul K.zero K.zero; unfold sq; lib_ring⟩
  | j :: l => by
    cases wcs P a S ha l with
    | intro d hd =>
      -- new gap = old gap + a_j · Σ_i a_i (S_i − S_j)²
      have dev := wdev_eq K a S (S j) l
      refine ⟨K.add d (K.mul (a j) (lsum K (fun i => K.mul (a i) (sq K (sub K (S i) (S j)))) l)),
        P.nn_add _ _ hd.1 (P.nn_mul _ _ (ha j) (lsum_nn K P _ (fun i => P.nn_mul _ _ (ha i) (P.nn_sq _)) l)), ?_⟩
      rw [dev]
      show K.add (sq K (K.add (K.mul (a j) (S j)) (lsum K (fun i => K.mul (a i) (S i)) l))) _
        = K.mul (K.add (a j) (lsum K a l)) (K.add (K.mul (a j) (sq K (S j))) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
      have ed : d = sub K (K.mul (lsum K a l) (lsum K (fun i => K.mul (a i) (sq K (S i))) l))
          (sq K (lsum K (fun i => K.mul (a i) (S i)) l)) := by
        rw [← hd.2]; lib_ring
      rw [ed]
      unfold sq; lib_ring

/-- **Triad sum with an L^∞ factor.** -/
theorem triad_linf (P : Cone K) (a S : I → R) (B : R) (l : List I) (ha : ∀ i, P.nn (a i))
    (hS : ∀ i, Le K P (sq K (S i)) B) :
    Le K P (sq K (lsum K (fun i => K.mul (a i) (S i)) l)) (K.mul (sq K (lsum K a l)) B) := by
  have w := wcs K P a S ha l
  have s := lsum_le K P (fun i => K.mul (a i) (sq K (S i))) (fun i => K.mul B (a i))
    (fun i => by
      have m := Riccati.le_mul_left K P (a i) _ _ (ha i) (hS i)
      rw [show K.mul (a i) B = K.mul B (a i) by lib_ring] at m
      exact m) l
  rw [lsum_mul_left K B a l] at s
  have m := Riccati.le_mul_left K P (lsum K a l) _ _ (lsum_nn K P a ha l) s
  refine le_trans' K P _ _ _ w (le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, ?_⟩)
  unfold sq; lib_ring

/-- **The stretching bound.** -/
theorem stretch (P : Cone K) (a b c : I → R) (σ : I → I → I) (C : R) (l : List I) (ha : ∀ i, P.nn (a i))
    (hC : ∀ p, Le K P (lsum K (fun q => sq K (c (σ p q))) l) C) :
    Le K P (sq K (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σ p q))) l)) l))
      (K.mul (sq K (lsum K a l)) (K.mul (lsum K (fun q => sq K (b q)) l) C)) := by
  apply triad_linf K P a _ _ l ha
  intro p
  exact le_trans' K P _ _ _ (cs_sum K P b (fun q => c (σ p q)) l)
    (Riccati.le_mul_left K P _ _ _ (lsum_nn K P _ (fun q => P.nn_sq (b q)) l) (hC p))

end TriadLinf

#print axioms TriadLinf.wdev_eq
#print axioms TriadLinf.wcs
#print axioms TriadLinf.triad_linf
#print axioms TriadLinf.stretch

#print axioms: 4 theorems, no axioms.

79.77 Crank–Nicolson enstrophy step

The midpoint step of a truncation in the form of the local continuation. \(Z'+h\nu \sum wks^2=Z+h\sum wsN\)

ns/EnstrophyStep.lean

/-
  ns/EnstrophyStep.lean — T37 cycle 206 (goal G1, step 1): the Crank–Nicolson enstrophy step of a 3D truncation in
  the step form of LocalCont, with midpoint quantities.

  Lean 4 core plus ns/ForcedEnstrophy (step_identity), lib/Gronwall, NSChain, lib/RingTac. Modes i ∈ l,
  a′_i − a_i = h(−ν k_i s_i + N_i), s = a′ + a, weights w_i ≥ 0 (w = |k|² gives the enstrophy). No neutrality:
    enstrophy_identity   Z′ + hν Σ w k s² = Z + h Σ w s N               (the transfer T = Σ w s N stays)
    enstrophy_step       if h T ≤ h c X ρ σ with ρ² = Σ w s², σ² = Σ w k s² (TriadLinf.stretch), then
                         Z′ ≤ Z + h (c X ρ σ − ν σ²)
  The step of LocalCont with (r, s) evaluated at the midpoint a′ + a; ρ² ≤ 2(Z + Z′) links ρ to r (next step of G1).
-/
import ns.ForcedEnstrophy

namespace EnstrophyStep
open DetChain NSChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) {I : Type}

def Zw (w a : I → R) (l : List I) : R := lsum K (fun i => K.mul (w i) (K.mul (a i) (a i))) l

theorem enstrophy_identity (w a a' k Nn : I → R) (h nu : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i))) :
    K.add (Zw K w a' l)
        (K.mul (K.mul h nu) (lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l))
      = K.add (Zw K w a l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)) := by
  -- step_identity with the transfer as forcing and zero nonlinearity
  have id := ForcedEnstrophy.step_identity K w a a' k (fun _ => K.zero) Nn h nu l
    (fun i => by rw [step i]; lib_ring)
    (by
      have e := lsum_ext K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) K.zero)) (fun _ => K.zero)
        (fun i => by lib_ring) l
      rw [e]
      have z : ∀ l : List I, lsum K (fun _ => K.zero) l = K.zero := by
        intro l; induction l with
        | nil => rfl
        | cons x l ih => show K.add K.zero (lsum K (fun _ => K.zero) l) = K.zero; rw [ih]; lib_ring
      exact z l)
  exact id

/-- **Enstrophy step, LocalCont form.** -/
theorem enstrophy_step (P : Pos K) (w a a' k Nn : I → R) (h nu c X ρ σ : R) (l : List I)
    (step : ∀ i, sub K (a' i) (a i) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k i) (K.add (a' i) (a i))))) (Nn i)))
    (hσ : K.mul σ σ = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))) l)
    (hT : le K P (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l))
      (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))) :
    le K P (Zw K w a' l) (K.add (Zw K w a l) (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul nu (K.mul σ σ))))) := by
  have id := enstrophy_identity K w a a' k Nn h nu l step
  rw [← hσ] at id
  unfold le at hT ⊢
  have e : sub K (K.add (Zw K w a l) (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul nu (K.mul σ σ)))))
        (Zw K w a' l)
      = K.add (sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
          (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)))
        (sub K (K.add (Zw K w a l) (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)))
          (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ)))) := by lib_ring
  rw [e, ← id, show sub K (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ)))
      (K.add (Zw K w a' l) (K.mul (K.mul h nu) (K.mul σ σ))) = K.zero by lib_ring]
  rw [show K.add (sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
      (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l))) K.zero
      = sub K (K.mul h (K.mul (K.mul (K.mul c X) ρ) σ))
      (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (Nn i))) l)) by lib_ring]
  exact hT

end EnstrophyStep

#print axioms EnstrophyStep.enstrophy_identity
#print axioms EnstrophyStep.enstrophy_step

#print axioms: 2 theorems, no axioms.

79.78 Midpoint enstrophy

The midpoint is controlled by the endpoints. \(\sum w(a'+a)^2\le 2Z'+2Z\)

ns/Midpoint.lean

/-
  ns/Midpoint.lean — T37 cycle 209 (goal G1, step 2): the midpoint enstrophy of a Crank–Nicolson step is controlled
  by the endpoint enstrophies.

  Lean 4 core plus ns/EnstrophyStep (Zw), lib/Gronwall, NSChain, lib/RingTac. For weights w ≥ 0:
      ρ² = Σ w (a′ + a)² ≤ 2 Z′ + 2 Z ,   Z = Σ w a²,  Z′ = Σ w a′²          (midpoint)
  ((x + y)² + (x − y)² = 2x² + 2y²). With EnstrophyStep: Z′ ≤ Z + h(cXρσ − νσ²) and ρ² ≤ 2(Z + Z′).
-/
import ns.EnstrophyStep

namespace Midpoint
open DetChain NSChain LibRing LibCS LibGron EnstrophyStep

variable {R : Type} (K : CRing R) {I : Type}

theorem midpoint (P : Pos K) (w a a' : I → R) (hw : ∀ i, P.nn (w i)) : ∀ l : List I,
    le K P (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)
      (K.add (K.add (Zw K w a' l) (Zw K w a' l)) (K.add (Zw K w a l) (Zw K w a l)))
  | [] => by
    unfold le; show P.nn (sub K (K.add (K.add K.zero K.zero) (K.add K.zero K.zero)) K.zero)
    rw [show sub K (K.add (K.add K.zero K.zero) (K.add K.zero K.zero)) K.zero = K.zero by lib_ring]; exact P.nn_zero
  | i :: l => by
    have ih := midpoint P w a a' hw l
    unfold le at ih ⊢
    show P.nn (sub K (K.add (K.add (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l))
        (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l)))
        (K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l)) (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l))))
      (K.add (K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)))
    have e : sub K (K.add (K.add (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l))
        (K.add (K.mul (w i) (K.mul (a' i) (a' i))) (Zw K w a' l)))
        (K.add (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l)) (K.add (K.mul (w i) (K.mul (a i) (a i))) (Zw K w a l))))
      (K.add (K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i))))
        (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l))
      = K.add (K.mul (w i) (K.mul (sub K (a' i) (a i)) (sub K (a' i) (a i))))
          (sub K (K.add (K.add (Zw K w a' l) (Zw K w a' l)) (K.add (Zw K w a l) (Zw K w a l)))
            (lsum K (fun i => K.mul (w i) (K.mul (K.add (a' i) (a i)) (K.add (a' i) (a i)))) l)) := by lib_ring
    rw [e]; exact P.nn_add _ _ (P.nn_mul _ _ (hw i) (P.nn_sq _)) ih

end Midpoint

#print axioms Midpoint.midpoint

#print axioms: 1 theorem, no axioms.

79.79 Young step, midpoint form

Dissipation absorbs the stretching at the midpoint. \(2(2\nu -hc^2X^2)Z'\le 2(2\nu +hc^2X^2)Z\)

ns/MidpointStep.lean

/-
  ns/MidpointStep.lean — T37 cycle 212 (goal G1, step 3): Young's step for the Crank–Nicolson (midpoint) form.

  Lean 4 core plus lib/Gronwall (le, le_trans, le_mul_left), lib/RingTac. If
      Z′ ≤ Z + h(cXρσ − νσ²)   and   ρ² ≤ 2(Z + Z′)          (EnstrophyStep, Midpoint)
  then, with (2νσ − cXρ)² ≥ 0,
      2(2ν − hc²X²) Z′ ≤ 2(2ν + hc²X²) Z                                            (midpoint_step)
  i.e. Z′ ≤ Z (1 + a)/(1 − a), a = hc²X²/(2ν) < 1: the CN analogue of YoungStep (Z′ ≤ (1 + a) Z); the product
  over a run gives Π (1 + a_k)/(1 − a_k), bounded while Σ a_k stays small (Serrin, midpoint form).
-/
import lib.Gronwall

namespace MidpointStep
open DetChain LibRing LibCS LibGron

variable {R : Type} (K : CRing R) (P : Pos K)

theorem midpoint_step (Z Z' h c X ρ σ ν : R) (hh : P.nn h) (hν : P.nn ν)
    (step : le K P Z' (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul ν (K.mul σ σ))))))
    (hρ : le K P (K.mul ρ ρ) (K.add (K.add Z Z) (K.add Z' Z'))) :
    le K P (K.add (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z')
                  (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z'))
      (K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)
             (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)) := by
  have s1 := le_mul_left K P (P.nn_add _ _ (P.nn_add _ _ hν hν) (P.nn_add _ _ hν hν)) step
  have s2 := le_mul_left K P (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq X))) hρ
  unfold le at s1 s2 ⊢
  have g3 : P.nn (K.mul h (K.mul (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ))
      (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ)))) := P.nn_mul _ _ hh (P.nn_sq _)
  have e : sub K (K.add (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z)
             (K.mul (K.add (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z))
        (K.add (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z')
               (K.mul (sub K (K.add ν ν) (K.mul h (K.mul (K.mul c c) (K.mul X X)))) Z'))
      = K.add (K.add
          (sub K (K.mul (K.add (K.add ν ν) (K.add ν ν))
            (K.add Z (K.mul h (sub K (K.mul (K.mul (K.mul c X) ρ) σ) (K.mul ν (K.mul σ σ))))))
            (K.mul (K.add (K.add ν ν) (K.add ν ν)) Z'))
          (sub K (K.mul (K.mul h (K.mul (K.mul c c) (K.mul X X))) (K.add (K.add Z Z) (K.add Z' Z')))
            (K.mul (K.mul h (K.mul (K.mul c c) (K.mul X X))) (K.mul ρ ρ))))
          (K.mul h (K.mul (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ))
            (sub K (K.mul (K.add ν ν) σ) (K.mul (K.mul c X) ρ)))) := by lib_ring
  rw [e]; exact P.nn_add _ _ (P.nn_add _ _ s1 s2) g3

end MidpointStep

#print axioms MidpointStep.midpoint_step

#print axioms: 1 theorem, no axioms.

79.80 Prodi–Serrin, Crank–Nicolson form

The midpoint step costs a square in the Serrin factor. \(z_N(1-S_N)^2\le z_0\)

ns/SerrinMid.lean

/-
  ns/SerrinMid.lean — T37 cycle 215 (goal G1, step 4): the discrete Prodi–Serrin bound for Crank–Nicolson runs.

  Lean 4 core plus ns/SerrinSum (sumA, prodC, bern), lib/Gronwall, lib/RingTac. With 0 ≤ b_k ≤ 1 and
      z_{k+1} (1 − b_k) ≤ (1 + b_k) z_k        (MidpointStep with b = hc²X²/(2ν), after dividing by 2·2ν)
    prodM          Π_{k<n} (1 − b_k)
    weier          Π (1 − b_k) ≥ 1 − Σ b_k                    (Weierstrass product inequality)
    mid_prod       z_N Π (1 − b_k) ≤ z_0 Π (1 + b_k)
    serrin_mid     z_N (1 − S_N)² ≤ z_0       (S_N = Σ_{k<N} b_k ≤ 1)
  The CN enstrophy stays below z_0/(1 − S)² while (hc²/2ν) Σ ‖u_k‖²_∞ < 1 — the square is the price of the midpoint.
-/
import ns.SerrinSum

namespace SerrinMid
open DetChain LibRing LibCS LibGron SerrinDiscrete SerrinSum

variable {R : Type} (K : CRing R) (P : Pos K)

def prodM (b : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (sub K K.one (b n)) (prodM b n)

theorem prodM_nn (b : Nat → R) (hb1 : ∀ k, P.nn (sub K K.one (b k))) (hone : P.nn K.one) :
    ∀ n, P.nn (prodM K b n)
  | 0 => hone
  | n + 1 => P.nn_mul _ _ (hb1 n) (prodM_nn b hb1 hone n)

/-- **Weierstrass.** -/
theorem weier (b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k))) :
    ∀ n, le K P (sub K K.one (sumA K b n)) (prodM K b n)
  | 0 => by
    unfold le; show P.nn (sub K K.one (sub K K.one K.zero))
    rw [show sub K K.one (sub K K.one K.zero) = K.zero by lib_ring]; exact P.nn_zero
  | n + 1 => by
    have ih := weier b hb hb1 n
    unfold le at ih ⊢
    show P.nn (sub K (K.mul (sub K K.one (b n)) (prodM K b n)) (sub K K.one (K.add (sumA K b n) (b n))))
    rw [show sub K (K.mul (sub K K.one (b n)) (prodM K b n)) (sub K K.one (K.add (sumA K b n) (b n)))
        = K.add (K.mul (sub K K.one (b n)) (sub K (prodM K b n) (sub K K.one (sumA K b n))))
            (K.mul (b n) (sumA K b n)) by lib_ring]
    exact P.nn_add _ _ (P.nn_mul _ _ (hb1 n) ih) (P.nn_mul _ _ (hb n) (sumA_nn K P b hb n))

theorem mid_prod (z b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k)))
    (hone : P.nn K.one)
    (step : ∀ k, le K P (K.mul (z (k + 1)) (sub K K.one (b k))) (K.mul (K.add K.one (b k)) (z k))) :
    ∀ N, le K P (K.mul (z N) (prodM K b N)) (K.mul (z 0) (prodC K b N))
  | 0 => by
    unfold le; show P.nn (sub K (K.mul (z 0) K.one) (K.mul (z 0) K.one))
    rw [show sub K (K.mul (z 0) K.one) (K.mul (z 0) K.one) = K.zero by lib_ring]; exact P.nn_zero
  | N + 1 => by
    have ih := mid_prod z b hb hb1 hone step N
    have s := le_mul_left K P (prodM_nn K P b hb1 hone N) (step N)
    have t := le_mul_left K P (P.nn_add _ _ hone (hb N)) ih
    unfold le at s t ⊢
    show P.nn (sub K (K.mul (z 0) (K.mul (K.add K.one (b N)) (prodC K b N)))
      (K.mul (z (N + 1)) (K.mul (sub K K.one (b N)) (prodM K b N))))
    rw [show sub K (K.mul (z 0) (K.mul (K.add K.one (b N)) (prodC K b N)))
          (K.mul (z (N + 1)) (K.mul (sub K K.one (b N)) (prodM K b N)))
        = K.add (sub K (K.mul (K.add K.one (b N)) (K.mul (z 0) (prodC K b N)))
              (K.mul (K.add K.one (b N)) (K.mul (z N) (prodM K b N))))
            (sub K (K.mul (prodM K b N) (K.mul (K.add K.one (b N)) (z N)))
              (K.mul (prodM K b N) (K.mul (z (N + 1)) (sub K K.one (b N))))) by lib_ring]
    exact P.nn_add _ _ t s

/-- **Discrete Prodi–Serrin, Crank–Nicolson form.** -/
theorem serrin_mid (z b : Nat → R) (hb : ∀ k, P.nn (b k)) (hb1 : ∀ k, P.nn (sub K K.one (b k)))
    (hone : P.nn K.one) (hz : ∀ k, P.nn (z k))
    (step : ∀ k, le K P (K.mul (z (k + 1)) (sub K K.one (b k))) (K.mul (K.add K.one (b k)) (z k)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K b N))) :
    le K P (K.mul (z N) (K.mul (sub K K.one (sumA K b N)) (sub K K.one (sumA K b N)))) (z 0) := by
  -- z_N (1−S)(1−S) ≤ z_N Π(1−b) (1−S) ≤ z_0 Π(1+b) (1−S) ≤ z_0
  have w := le_mul_left K P (P.nn_mul _ _ (hz N) hS) (weier K P b hb hb1 N)
  have m := le_mul_left K P hS (mid_prod K P z b hb hb1 hone step N)
  have br := le_mul_left K P (hz 0) (bern K P b hb hone N)
  unfold le at w m br ⊢
  rw [show sub K (z 0) (K.mul (z N) (K.mul (sub K K.one (sumA K b N)) (sub K K.one (sumA K b N))))
      = K.add (K.add (sub K (K.mul (K.mul (z N) (sub K K.one (sumA K b N))) (prodM K b N))
              (K.mul (K.mul (z N) (sub K K.one (sumA K b N))) (sub K K.one (sumA K b N))))
            (sub K (K.mul (sub K K.one (sumA K b N)) (K.mul (z 0) (prodC K b N)))
              (K.mul (sub K K.one (sumA K b N)) (K.mul (z N) (prodM K b N)))))
          (sub K (K.mul (z 0) K.one) (K.mul (z 0) (K.mul (prodC K b N) (sub K K.one (sumA K b N))))) by lib_ring]
  exact P.nn_add _ _ (P.nn_add _ _ w m) br

end SerrinMid

#print axioms SerrinMid.prodM_nn
#print axioms SerrinMid.weier
#print axioms SerrinMid.mid_prod
#print axioms SerrinMid.serrin_mid

#print axioms: 4 theorems, no axioms.

79.81 Prodi–Serrin for a Galerkin run

From the step equations and the transfer bound of a truncation. \(Z_N(1-S_N)^2\le Z_0,\quad b_t=hc^2X_t^2/(2\nu )\)

ns/CNSerrin.lean

/-
  ns/CNSerrin.lean — T37 cycle 218 (goal G1, step 5): the discrete Prodi–Serrin bound for a Crank–Nicolson run of a
  3D Galerkin truncation, from the step equations and the transfer bound.

  Lean 4 core plus ns/SerrinMid, ns/MidpointStep, ns/Midpoint, ns/LocalCont (le_of_mul_le), lib/OField. For each time
  t: modes i ∈ l, a_{t+1,i} − a_{t,i} = h(−ν k_i s_i + N_{t,i}), s = a_{t+1} + a_t, weights w ≥ 0,
  σ_t² = Σ w k s², ρ_t² = Σ w s², and the transfer bound h Σ w s N_t ≤ h c X_t ρ_t σ_t (TriadLinf.stretch). Then
  with b_t = (2ν)⁻¹ h c² X_t², 0 ≤ b_t ≤ 1 and S_N = Σ_{t<N} b_t ≤ 1:
      Z_N (1 − S_N)² ≤ Z_0,   Z_t = Σ_i w_i a_{t,i}²                                   (cn_serrin)
-/
import ns.SerrinMid
import ns.MidpointStep
import ns.Midpoint
import ns.LocalCont

namespace CNSerrin
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinDiscrete SerrinSum SerrinMid MidpointStep Midpoint
  EnstrophyStep LocalCont

variable {R : Type} (K : CRing R) {I : Type}

def bk (P : Pos K) (F : OF K P) (h c ν : R) (X : Nat → R) (t : Nat) : R :=
  K.mul (F.inv (K.add ν ν)) (K.mul h (K.mul (K.mul c c) (K.mul (X t) (X t))))

theorem cn_serrin (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h c ν : R)
    (l : List I) (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hT : ∀ t, le K P (K.mul h (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l))
      (K.mul h (K.mul (K.mul (K.mul c (X t)) (ρ t)) (σ t))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h c ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h c ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h c ν X) N))
      (sub K K.one (sumA K (bk K P F h c ν X) N)))) (Zw K w (a 0) l) := by
  have hone := nn_one K P
  have hinv := nn_inv K P F _ (P.nn_add _ _ hν hν) hν0
  have hi := F.mul_inv _ hν0
  have hb : ∀ t, P.nn (bk K P F h c ν X t) := fun t =>
    P.nn_mul _ _ hinv (P.nn_mul _ _ hh (P.nn_mul _ _ (P.nn_sq c) (P.nn_sq (X t))))
  have hZ : ∀ t, P.nn (Zw K w (a t) l) := fun t => by
    unfold Zw
    have g : ∀ l : List I, P.nn (lsum K (fun i => K.mul (w i) (K.mul (a t i) (a t i))) l) := by
      intro l; induction l with
      | nil => exact P.nn_zero
      | cons x l ih => exact P.nn_add _ _ (P.nn_mul _ _ (hw x) (P.nn_sq _)) ih
    exact g l
  apply serrin_mid K P (fun t => Zw K w (a t) l) (bk K P F h c ν X) hb hb1 hone hZ _ N hS
  intro t
  have es := enstrophy_step K P w (a t) (a (t + 1)) k (Nn t) h ν c (X t) (ρ t) (σ t) l (step t) (hσ t) (hT t)
  have mp := midpoint K P w (a t) (a (t + 1)) hw l
  rw [← hρ t] at mp
  have ms := midpoint_step K P (Zw K w (a t) l) (Zw K w (a (t + 1)) l) h c (X t) (ρ t) (σ t) ν hh hν es
    (by
      unfold le at mp ⊢
      rw [show sub K (K.add (K.add (Zw K w (a t) l) (Zw K w (a t) l)) (K.add (Zw K w (a (t + 1)) l) (Zw K w (a (t + 1)) l)))
          (K.mul (ρ t) (ρ t))
        = sub K (K.add (K.add (Zw K w (a (t + 1)) l) (Zw K w (a (t + 1)) l)) (K.add (Zw K w (a t) l) (Zw K w (a t) l)))
          (K.mul (ρ t) (ρ t)) by lib_ring]
      exact mp)
  -- divide 2·(2ν)·[(1 − b) Z'] ≤ 2·(2ν)·[(1 + b) Z] by 2·2ν
  apply le_of_mul_le K P F _ _ (K.add (K.add ν ν) (K.add ν ν)) (P.nn_add _ _ (P.nn_add _ _ hν hν) (P.nn_add _ _ hν hν)) h4
  unfold bk
  generalize Zw K w (a (t + 1)) l = Z' at ms ⊢
  generalize Zw K w (a t) l = Z at ms ⊢
  generalize K.mul h (K.mul (K.mul c c) (K.mul (X t) (X t))) = A at ms ⊢
  unfold le at ms ⊢
  have e1 : K.mul (K.add (K.add ν ν) (K.add ν ν)) (K.mul (K.add K.one (K.mul (F.inv (K.add ν ν)) A)) Z)
      = K.add (K.mul (K.add (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z)
              (K.mul (K.add (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z) := by lib_ring
  have e2 : K.mul (K.add (K.add ν ν) (K.add ν ν)) (K.mul Z' (sub K K.one (K.mul (F.inv (K.add ν ν)) A)))
      = K.add (K.mul (sub K (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z')
              (K.mul (sub K (K.add ν ν) (K.mul (K.mul (K.add ν ν) (F.inv (K.add ν ν))) A)) Z') := by lib_ring
  rw [e1, e2, hi, show K.mul K.one A = A by lib_ring]
  exact ms

end CNSerrin

#print axioms CNSerrin.cn_serrin

#print axioms: 1 theorem, no axioms.

79.82 Agmon halves: shell weights

Below the cut the weights sum to \(26K\), above it to \(26/(K+1)\). \(\sum _{n<K}\sum |k|^{-2}\le 26K,\quad \sum _{n>K}\sum |k|^{-4}\le 26u_K\)

ns/AgmonHalves.lean

/-
  ns/AgmonHalves.lean — T37 cycle 221 (goal G2, step 1): the shell weights of Agmon's two halves.

  Lean 4 core plus ns/Sobolev (cN_mul, cN_le, lsum_le_len), ns/BaselTail (tail), ns/Basel, lib/RingTac. Shell n
  (|k|_∞ = n + 1) has at most 26(n + 1)² modes; u_n = 1/(n + 1).
    low_weights    ω_i ≤ u_n² on shells n < K   ⇒  Σ_{n<K} Σ_{shell n} ω ≤ 26·K        (ω = |k|^{-2})
    high_weights   ω_i ≤ u_n⁴ on shells n > K   ⇒  Σ_{j<M} Σ_{shell K+1+j} ω ≤ 26·u_K   (ω = |k|^{-4})
  With Sobolev.sobolev: (Σ_low |û|)² ≤ 26K·Z and (Σ_high |û|)² ≤ 26 u_K·P — the hypotheses hL, hH of
  LocalContSobolev at cut K (low shells n < K, high shells n > K; shell K goes to either side with weight 26).
-/
import ns.Sobolev
import ns.BaselTail

namespace AgmonHalves
open DetChain NSChain LibRing Basel Sobolev

variable {R : Type} (K : CRing R) {I : Type}

theorem shell_bound (P : Cone K)
    (shell : Nat → List I) (ω : I → R) (n : Nat) (e : R) (he : P.nn e)
    (hlen : (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ i, List.Mem i (shell n) → Le K P (ω i) e) :
    Le K P (lsum K ω (shell n)) (K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) e)) := by
  refine le_trans' K P _ _ _ (lsum_le_len K P ω _ he (shell n) hw) ?_
  have c := cN_le K P _ _ hlen
  rw [cN_mul, cN_mul] at c
  have := Riccati.le_mul_left K P _ _ _ he c
  exact le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ (le_trans' K P _ _ _ this ⟨K.zero, P.nn_zero, by lib_ring⟩)

theorem sumN_le (P : Cone K) (f g : Nat → R) (h : ∀ n, Le K P (f n) (g n)) : ∀ M, Le K P (sumN K f M) (sumN K g M)
  | 0 => le_refl' K P _
  | M + 1 => Riccati.le_add K P _ _ _ _ (sumN_le P f g h M) (h M)

/-- **Low shells.** -/
theorem low_weights (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (u n) (u n))) :
    Le K P (sumN K (fun n => lsum K ω (shell n)) Kk) (K.mul (cN K 26) (cN K Kk)) := by
  have s := sumN_le K P _ (fun _ => cN K 26)
    (fun n => by
      have b := shell_bound K P shell ω n _ (P.nn_sq _) (hlen n) (hw n)
      rw [show K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (cN K (n + 1))) (K.mul (u n) (u n)))
          = K.mul (cN K 26) (K.mul (K.mul (cN K (n + 1)) (u n)) (K.mul (cN K (n + 1)) (u n))) by lib_ring,
        hu n, show K.mul (cN K 26) (K.mul K.one K.one) = cN K 26 by lib_ring] at b
      exact b) Kk
  have c : ∀ M, sumN K (fun _ => cN K 26) M = K.mul (cN K 26) (cN K M) := by
    intro M; induction M with
    | zero => show K.zero = K.mul (cN K 26) K.zero; lib_ring
    | succ M ih => show K.add (sumN K (fun _ => cN K 26) M) (cN K 26) = K.mul (cN K 26) (K.add (cN K M) K.one); rw [ih]; lib_ring
  rw [c] at s; exact s

/-- **High shells.** -/
theorem high_weights (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) (K.mul (cN K 26) (u Kk)) := by
  have s := sumN_le K P _ (fun j => K.mul (cN K 26) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))))
    (fun j => by
      have b := shell_bound K P shell ω (Kk + j + 1) _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)) (hlen _) (hw _)
      have e : K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + j + 1 + 1)) (cN K (Kk + j + 1 + 1)))
            (K.mul (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1)))))
          = K.mul (cN K 26) (K.mul (K.mul (K.mul (cN K (Kk + j + 1 + 1)) (u (Kk + j + 1)))
              (K.mul (cN K (Kk + j + 1 + 1)) (u (Kk + j + 1)))) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1)))) := by lib_ring
      rw [e, hu (Kk + j + 1), show K.mul (cN K 26) (K.mul (K.mul K.one K.one) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))))
          = K.mul (cN K 26) (K.mul (u (Kk + j + 1)) (u (Kk + j + 1))) by lib_ring] at b
      exact b) M
  rw [sumN_mul_left] at s
  have t := Riccati.le_mul_left K P _ _ _ (cN_nn K P (Basel.one_nn K P) 26) (BaselTail.tail K P u hu Kk M)
  exact le_trans' K P _ _ _ s t

end AgmonHalves

#print axioms AgmonHalves.shell_bound
#print axioms AgmonHalves.sumN_le
#print axioms AgmonHalves.low_weights
#print axioms AgmonHalves.high_weights

#print axioms: 4 theorems, no axioms.

79.83 Agmon halves on mode lists

The low and high Wiener sums. \(X_L^2\le 26K\,Z,\quad X_H^2\le 26u_K\,P\)

ns/AgmonHalves2.lean

/-
  ns/AgmonHalves2.lean — T37 cycle 224 (goal G2, step 2): Agmon's two halves for the low and high mode lists.

  Lean 4 core plus ns/AgmonHalves, ns/Sobolev (sobolev), NSChain, lib/RingTac. lowL K = shells n < K, highL K M =
  shells K + 1 … K + M (concatenated). For x_i² ≤ ω_i, y_i² ≤ β_i:
    low_half    ω ≤ u_n² on shell n   ⇒  (Σ_{lowL} x y)² ≤ 26K · Σ_{lowL} β        (x = |k|^{-1}, y = |k||û|: Z)
    high_half   ω ≤ u_n⁴ on shell n   ⇒  (Σ_{highL} x y)² ≤ 26u_K · Σ_{highL} β     (x = |k|^{-2}, y = |k|²|û|: P)
  i.e. X_L² ≤ 26K·Z and X_H² ≤ 26u_K·P — the hypotheses hL, hH of LocalContSobolev (in the Cone order).
-/
import ns.AgmonHalves

namespace AgmonHalves2
open DetChain NSChain LibRing Basel Sobolev AgmonHalves

variable {R : Type} (K : CRing R) {I : Type}

def lowL (shell : Nat → List I) : Nat → List I
  | 0 => []
  | n + 1 => lowL shell n ++ shell n

def highL (shell : Nat → List I) (Kk : Nat) : Nat → List I
  | 0 => []
  | j + 1 => highL shell Kk j ++ shell (Kk + j + 1)

theorem lsum_append (f : I → R) : ∀ l1 l2 : List I, lsum K f (l1 ++ l2) = K.add (lsum K f l1) (lsum K f l2)
  | [], l2 => by show lsum K f l2 = K.add K.zero (lsum K f l2); lib_ring
  | x :: l1, l2 => by
    show K.add (f x) (lsum K f (l1 ++ l2)) = K.add (K.add (f x) (lsum K f l1)) (lsum K f l2)
    rw [lsum_append f l1 l2]; lib_ring

theorem lsum_lowL (f : I → R) (shell : Nat → List I) : ∀ n,
    lsum K f (lowL shell n) = sumN K (fun m => lsum K f (shell m)) n
  | 0 => rfl
  | n + 1 => by
    show lsum K f (lowL shell n ++ shell n) = K.add (sumN K (fun m => lsum K f (shell m)) n) (lsum K f (shell n))
    rw [lsum_append, lsum_lowL f shell n]

theorem lsum_highL (f : I → R) (shell : Nat → List I) (Kk : Nat) : ∀ j,
    lsum K f (highL shell Kk j) = sumN K (fun m => lsum K f (shell (Kk + m + 1))) j
  | 0 => rfl
  | j + 1 => by
    show lsum K f (highL shell Kk j ++ shell (Kk + j + 1))
      = K.add (sumN K (fun m => lsum K f (shell (Kk + m + 1))) j) (lsum K f (shell (Kk + j + 1)))
    rw [lsum_append, lsum_highL f shell Kk j]

/-- **Low half.** -/
theorem low_half (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (u n) (u n)))
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i)) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)))
      (K.mul (K.mul (cN K 26) (cN K Kk)) (lsum K β (lowL shell Kk))) := by
  apply sobolev K P x y ω β _ _ hx hy hβ
  rw [lsum_lowL]
  exact low_weights K P u hu shell ω Kk hlen hw

/-- **High half.** -/
theorem high_half (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))
    (hx : ∀ i, Le K P (SumCS.sq K (x i)) (ω i)) (hy : ∀ i, Le K P (SumCS.sq K (y i)) (β i)) (hβ : ∀ i, P.nn (β i)) :
    Le K P (SumCS.sq K (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)))
      (K.mul (K.mul (cN K 26) (u Kk)) (lsum K β (highL shell Kk M))) := by
  apply sobolev K P x y ω β _ _ hx hy hβ
  rw [lsum_highL]
  exact high_weights K P u hu shell ω Kk M hlen hw

end AgmonHalves2

#print axioms AgmonHalves2.lsum_append
#print axioms AgmonHalves2.lsum_lowL
#print axioms AgmonHalves2.lsum_highL
#print axioms AgmonHalves2.low_half
#print axioms AgmonHalves2.high_half

#print axioms: 5 theorems, no axioms.

79.84 Agmon halves in the positivity order

The inputs of the local continuation from the lattice. \(X_L^2\le 26kZ,\ X_H^2\le 26wP,\ (k+1)w=1\)

ns/AgmonHalves3.lean

/-
  ns/AgmonHalves3.lean — T37 cycle 227 (goal G2, step 3): the hypotheses hL, hH of LocalContSobolev from the lattice.

  Lean 4 core plus ns/AgmonHalves2, ns/LocalContSobolev (c26), ns/LocalContAgmon (toCone, le_of_Le, Le_of_le). For a
  positivity cone P on R (so that both orders apply), cut K with k = cN K, w = u_K ((k + 1)w = 1), and
  Σ_low β_Z ≤ Z, Σ_high β_P ≤ P (the low/high parts of the enstrophy and of ‖Δu‖²):
    hL_of_lattice   (Σ_low x y)² ≤ 26 k Z          (x = |k|^{-1}, y = |k||û|)
    hH_of_lattice   (Σ_high x y)² ≤ 26 w P         (x = |k|^{-2}, y = |k|²|û|)
  exactly the per-step inputs hL, hH of local_cont_sobolev (with X_L, X_H the two partial Wiener sums).
-/
import ns.AgmonHalves2
import ns.LocalContSobolev

namespace AgmonHalves3
open DetChain NSChain LibRing LibCS LibGron Basel AgmonHalves2 LocalContAgmon LocalContSobolev

variable {R : Type} (K : CRing R) {I : Type}

theorem c26_eq : c26 K = cN K 26 := by
  unfold c26
  have h : ∀ n, castN K n = cN K n := by
    intro n; induction n with
    | zero => rfl
    | succ n ih => show K.add K.one (castN K n) = K.add (cN K n) K.one; rw [ih]; lib_ring
  exact h 26

theorem hL_of_lattice (P : Pos K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk : Nat) (Z : R)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K (toCone K P) (ω i) (K.mul (u n) (u n)))
    (hx : ∀ i, le K P (K.mul (x i) (x i)) (ω i)) (hy : ∀ i, le K P (K.mul (y i) (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hZ : le K P (lsum K β (lowL shell Kk)) Z) :
    le K P (K.mul (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)) (lsum K (fun i => K.mul (x i) (y i)) (lowL shell Kk)))
      (K.mul (K.mul (c26 K) (cN K Kk)) Z) := by
  have l := le_of_Le K P _ _ (low_half K (toCone K P) u hu shell x y ω β Kk hlen hw
    (fun i => Le_of_le K P _ _ (hx i)) (fun i => Le_of_le K P _ _ (hy i)) hβ)
  have m := le_mul_left K P (P.nn_mul _ _ (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) 26)
    (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) Kk)) hZ
  rw [c26_eq]
  exact le_trans K P l m

theorem hH_of_lattice (P : Pos K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (x y ω β : I → R) (Kk M : Nat) (Pp : R)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K (toCone K P) (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n))))
    (hx : ∀ i, le K P (K.mul (x i) (x i)) (ω i)) (hy : ∀ i, le K P (K.mul (y i) (y i)) (β i)) (hβ : ∀ i, P.nn (β i))
    (hP : le K P (lsum K β (highL shell Kk M)) Pp) :
    le K P (K.mul (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)) (lsum K (fun i => K.mul (x i) (y i)) (highL shell Kk M)))
      (K.mul (K.mul (c26 K) (u Kk)) Pp) := by
  have l := le_of_Le K P _ _ (high_half K (toCone K P) u hu shell x y ω β Kk M hlen hw
    (fun i => Le_of_le K P _ _ (hx i)) (fun i => Le_of_le K P _ _ (hy i)) hβ)
  have m := le_mul_left K P (P.nn_mul _ _ (Basel.cN_nn K (toCone K P) (Basel.one_nn K (toCone K P)) 26)
    (Basel.u_nn K (toCone K P) u hu Kk)) hP
  rw [c26_eq]
  exact le_trans K P l m

end AgmonHalves3

#print axioms AgmonHalves3.c26_eq
#print axioms AgmonHalves3.hL_of_lattice
#print axioms AgmonHalves3.hH_of_lattice

#print axioms: 3 theorems, no axioms.

79.85 Local continuation on the lattice

Every mode is in one half; the boundary shell goes high. \(X^2\le 52kZ+104Pw\)

ns/AgmonHalves4.lean

/-
  ns/AgmonHalves4.lean — T37 cycle 230 (goal G2b): the boundary shell K in the high half, and local continuation
  with every mode accounted for.

  Lean 4 core plus ns/AgmonHalves3, ns/LocalContSobolev, ns/LocalContAgmon, lib/RingTac.
    u_sq_le         u_K² ≤ u_K                               (u_K = 1/(K + 1) ≤ 1)
    high_weights0   ω ≤ u_n⁴ on shells n ≥ K  ⇒  Σ_{j<M} Σ_{shell K+j} ω ≤ 26(u_K + u_K)
    split_52_104    X_L² ≤ 26kZ, X_H² ≤ 26(w + w)P  ⇒  X² ≤ 52kZ + 104Pw
    local_cont_lattice   local_cont_agmon with a = 52, b = 104 and the two halves as hypotheses: low shells
                    n < K, high shells n ≥ K — the split K covers every mode.
-/
import ns.AgmonHalves3

namespace AgmonHalves4
open DetChain NSChain LibRing LibCS LibGron LibOF Basel AgmonHalves AgmonHalves2 LocalContAgmon LocalContSobolev
  SerrinDiscrete SerrinSum SerrinRun SerrinAgmon LocalCont

variable {R : Type} (K : CRing R) {I : Type}

theorem u_sq_le (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one) (n : Nat) :
    Le K P (K.mul (u n) (u n)) (u n) := by
  -- u − u² = u·(1 − u) = u·(cN n · u) since (cN n + 1) u = 1
  refine ⟨K.mul (cN K n) (K.mul (u n) (u n)), P.nn_mul _ _ (cN_nn K P (one_nn K P) n) (P.nn_sq _), ?_⟩
  have h := hu n
  have c : cN K (n + 1) = K.add (cN K n) K.one := rfl
  rw [c] at h
  calc K.add (K.mul (u n) (u n)) (K.mul (cN K n) (K.mul (u n) (u n)))
      = K.mul (u n) (K.mul (K.add (cN K n) K.one) (u n)) := by lib_ring
    _ = u n := by rw [h]; lib_ring

/-- **High shells from K.** -/
theorem high_weights0 (P : Cone K) (u : Nat → R) (hu : ∀ n, K.mul (cN K (n + 1)) (u n) = K.one)
    (shell : Nat → List I) (ω : I → R) (Kk M : Nat)
    (hlen : ∀ n, (shell n).length ≤ 26 * ((n + 1) * (n + 1)))
    (hw : ∀ n i, List.Mem i (shell n) → Le K P (ω i) (K.mul (K.mul (u n) (u n)) (K.mul (u n) (u n)))) :
    Le K P (sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)) (K.mul (cN K 26) (K.add (u Kk) (u Kk))) := by
  -- shell Kk alone ≤ 26 u_K², the rest is high_weights
  have b0 := shell_bound K P shell ω Kk _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_sq _)) (hlen Kk) (hw Kk)
  rw [show K.mul (cN K 26) (K.mul (K.mul (cN K (Kk + 1)) (cN K (Kk + 1)))
        (K.mul (K.mul (u Kk) (u Kk)) (K.mul (u Kk) (u Kk))))
      = K.mul (cN K 26) (K.mul (K.mul (K.mul (cN K (Kk + 1)) (u Kk)) (K.mul (cN K (Kk + 1)) (u Kk))) (K.mul (u Kk) (u Kk)))
      by lib_ring, hu Kk] at b0
  have rest := high_weights K P u hu shell ω Kk M hlen hw
  -- Σ_{j<M+1} f(Kk + j) = f(Kk) + Σ_{j<M} f(Kk + j + 1)
  have shift : ∀ M, sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)
      = K.add (lsum K ω (shell Kk)) (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) := by
    intro M; induction M with
    | zero => show K.add K.zero (lsum K ω (shell (Kk + 0))) = K.add (lsum K ω (shell Kk)) K.zero; rw [Nat.add_zero]; lib_ring
    | succ M ih =>
      show K.add (sumN K (fun j => lsum K ω (shell (Kk + j))) (M + 1)) (lsum K ω (shell (Kk + (M + 1))))
        = K.add (lsum K ω (shell Kk)) (K.add (sumN K (fun j => lsum K ω (shell (Kk + j + 1))) M) (lsum K ω (shell (Kk + M + 1))))
      rw [ih, show Kk + (M + 1) = Kk + M + 1 from rfl]; lib_ring
  rw [shift]
  have s := Riccati.le_add K P _ _ _ _ b0 rest
  have uu := Riccati.le_mul_left K P (cN K 26) _ _ (cN_nn K P (one_nn K P) 26) (u_sq_le K P u hu Kk)
  refine le_trans' K P _ _ _ s ?_
  refine le_trans' K P _ _ _ (Riccati.le_add K P _ _ _ _ (le_trans' K P _ _ _ ⟨K.zero, P.nn_zero, by lib_ring⟩ uu)
    (le_refl' K P (K.mul (cN K 26) (u Kk)))) ⟨K.zero, P.nn_zero, by lib_ring⟩

/-- **The split with every mode.** -/
theorem split_52_104 (P : Pos K) (XL XH k w Z Pp : R)
    (hL : le K P (K.mul XL XL) (K.mul (K.mul (c26 K) k) Z))
    (hH : le K P (K.mul XH XH) (K.mul (K.mul (c26 K) (K.add w w)) Pp)) :
    le K P (K.mul (K.add XL XH) (K.add XL XH))
      (K.add (K.mul (c52 K) (K.mul k Z)) (K.mul (K.add (c52 K) (c52 K)) (K.mul Pp w))) := by
  have a := Agmon.agmon K (toCone K P) XL XH (K.mul (c26 K) k) (K.mul (c26 K) (K.add w w)) Z Pp
    (Le_of_le K P _ _ (by show le K P (K.mul XL XL) _; exact hL))
    (Le_of_le K P _ _ (by show le K P (K.mul XH XH) _; exact hH))
  have l := le_of_Le K P _ _ a
  unfold le at l ⊢
  rw [c52_eq]
  have e : sub K (K.add (K.mul (K.add (c26 K) (c26 K)) (K.mul k Z))
        (K.mul (K.add (K.add (c26 K) (c26 K)) (K.add (c26 K) (c26 K))) (K.mul Pp w)))
        (K.mul (K.add XL XH) (K.add XL XH))
      = sub K (K.add (K.add (K.mul (K.mul (c26 K) k) Z) (K.mul (K.mul (c26 K) k) Z))
          (K.add (K.mul (K.mul (c26 K) (K.add w w)) Pp) (K.mul (K.mul (c26 K) (K.add w w)) Pp)))
          (SumCS.sq K (K.add XL XH)) := by unfold SumCS.sq; lib_ring
  rw [e]; exact l

/-- **Local continuation, every mode in one of the halves.** -/
theorem local_cont_lattice (P : Pos K) (F : OF K P) (Z r s XL XH kc w : Nat → R) (h c ν M q q1 : R)
    (hh : P.nn h) (hν : P.nn ν)
    (hν0 : K.add ν ν ≠ K.zero) (hhν : K.mul h ν ≠ K.zero) (hr : ∀ k, K.mul (r k) (r k) = Z k)
    (step : ∀ k, le K P (Z (k + 1))
      (K.add (Z k) (K.mul h (sub K (K.mul (K.mul (K.mul c (K.add (XL k) (XH k))) (r k)) (s k))
        (K.mul ν (K.mul (s k) (s k)))))))
    (hM : P.nn M)
    (hkw : ∀ k, K.mul (K.add (kc k) K.one) (w k) = K.one)
    (h1 : ∀ k, le K P (K.mul (K.mul (kc k) (kc k)) (Z k)) (K.mul (s k) (s k)))
    (h2 : ∀ k, le K P (K.mul (s k) (s k)) (K.mul (K.mul (K.add (kc k) K.one) (K.add (kc k) K.one)) (Z k)))
    (hL : ∀ k, le K P (K.mul (XL k) (XL k)) (K.mul (K.mul (c26 K) (kc k)) (Z k)))
    (hH : ∀ k, le K P (K.mul (XH k) (XH k)) (K.mul (K.mul (c26 K) (K.add (w k) (w k))) (K.mul (s k) (s k))))
    (hq : P.nn q) (hq0 : q ≠ K.zero) (hq1 : le K P q1 K.one) (hq10 : sub K K.one q1 ≠ K.zero)
    (hM0 : le K P (Z 0) (K.mul M (sub K K.one q1))) (Nmax : Nat)
    (hα : ∀ N, N ≤ Nmax → le K P (alpha K P F h ν
      (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K)))
        (K.add (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K))) (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K)))))
      M (Z 0) N) (K.mul q q))
    (hβ : ∀ N, N ≤ Nmax → le K P (K.mul (amp K P F h c ν)
      (K.add (beta K P F h c ν
        (K.add (K.add (K.mul (c52 K) (c52 K)) (K.mul (c52 K) (c52 K)))
          (K.add (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K))) (K.mul (K.add (c52 K) (c52 K)) (K.add (c52 K) (c52 K)))))
        M N) q)) q1) :
    ∀ N, N ≤ Nmax → le K P (Z N) M :=
  local_cont_agmon K P F Z r s (fun k => K.add (XL k) (XH k)) kc w h c ν (c52 K) (K.add (c52 K) (c52 K)) M q q1
    hh hν hν0 hhν hr step hM hkw h1 h2
    (fun k => split_52_104 K P (XL k) (XH k) (kc k) (w k) (Z k) (K.mul (s k) (s k)) (hL k) (hH k))
    hq hq0 hq1 hq10 hM0 Nmax hα hβ

end AgmonHalves4

#print axioms AgmonHalves4.u_sq_le
#print axioms AgmonHalves4.high_weights0
#print axioms AgmonHalves4.split_52_104
#print axioms AgmonHalves4.local_cont_lattice

#print axioms: 4 theorems, no axioms.

79.86 Transfer bound from triads

The transfer of a step is bounded by the Wiener norm and the two Sobolev norms. \(T^2\le (\sum _pa_pS_p)^2\Rightarrow hT\le hX\rho \sigma \)

ns/TransferBound.lean

/-
  ns/TransferBound.lean — T37 cycle 248 (goal G6): the transfer hypothesis hT of CNSerrin from the triad sum.

  Lean 4 core plus ns/TriadLinf (stretch), ns/LocalContAgmon (toCone, le_of_Le), ns/SerrinAgmon (le_of_sq),
  lib/OField. If the transfer T of a step is dominated by the triad sum of magnitudes,
      T² ≤ (Σ_p a_p Σ_q b_q c_{σ(p,q)})²,   a ≥ 0,   Σ_q c_{σ(p,q)}² ≤ σ² for every p,   ρ² = Σ b²,
  then with X = Σ a (Wiener norm) and X, ρ, σ ≥ 0, Xρσ ≠ 0:
      T ≤ X ρ σ,   h T ≤ h (1·X) ρ σ                                             (transfer_bound)
  — the hypothesis hT of CNSerrin.cn_serrin with c = 1. The domination T² ≤ (Σ a S)² is the triangle inequality
  for the Fourier triad sum (|û_p·k_q||û_q·w_r| ≤ |û_p||k_q||û_q||w_r|, TriadBound), left as the per-truncation input.
-/
import ns.TriadLinf
import ns.LocalContAgmon

namespace TransferBound
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinAgmon LocalContAgmon TriadLinf

variable {R : Type} (K : CRing R) {I : Type}

theorem le_of_sq_signed (P : Pos K) (F : OF K P) (T q : R) (hq : P.nn q) (hq0 : q ≠ K.zero)
    (h : le K P (K.mul T T) (K.mul q q)) : le K P T q := by
  cases F.total T with
  | inl hT => exact le_of_sq K P F T q hT hq hq0 h
  | inr hT =>
    unfold le
    rw [show sub K q T = K.add q (K.neg T) by rfl]
    exact P.nn_add _ _ hq hT

theorem transfer_bound (P : Pos K) (F : OF K P) (a b c : I → R) (σf : I → I → I) (l : List I)
    (T h X ρ σ : R) (hh : P.nn h) (ha : ∀ i, P.nn (a i))
    (hX : X = lsum K a l) (hρ : K.mul ρ ρ = lsum K (fun q => K.mul (b q) (b q)) l)
    (hC : ∀ p, le K P (lsum K (fun q => K.mul (c (σf p q)) (c (σf p q))) l) (K.mul σ σ))
    (hXn : P.nn X) (hρn : P.nn ρ) (hσn : P.nn σ) (h0 : K.mul (K.mul X ρ) σ ≠ K.zero)
    (hdom : le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σf p q))) l)) l)
             (lsum K (fun p => K.mul (a p) (lsum K (fun q => K.mul (b q) (c (σf p q))) l)) l))) :
    le K P (K.mul h T) (K.mul h (K.mul (K.mul (K.mul K.one X) ρ) σ)) := by
  have st := le_of_Le K P _ _
    (stretch K (toCone K P) a b c σf (K.mul σ σ) l ha (fun p => Le_of_le K P _ _ (hC p)))
  have t := le_trans K P hdom st
  have e : K.mul (SumCS.sq K (lsum K a l)) (K.mul (lsum K (fun q => SumCS.sq K (b q)) l) (K.mul σ σ))
      = K.mul (K.mul (K.mul X ρ) σ) (K.mul (K.mul X ρ) σ) := by
    rw [hX] at *
    have : lsum K (fun q => SumCS.sq K (b q)) l = K.mul ρ ρ := by rw [hρ]; rfl
    rw [this]; unfold SumCS.sq; lib_ring
  rw [e] at t
  have r := le_of_sq_signed K P F T _ (P.nn_mul _ _ (P.nn_mul _ _ hXn hρn) hσn) h0 t
  have m := le_mul_left K P hh r
  rw [show K.mul (K.mul (K.mul K.one X) ρ) σ = K.mul (K.mul X ρ) σ by lib_ring]
  exact m

end TransferBound

#print axioms TransferBound.le_of_sq_signed
#print axioms TransferBound.transfer_bound

#print axioms: 2 theorems, no axioms.

79.87 Squared triangle inequality

A signed sum is bounded by the sum of magnitudes, without absolute values. \(t_i^2\le m_i^2,\ m_i>0\Rightarrow (\sum t)^2\le (\sum m)^2\)

ns/SignedSum.lean

/-
  ns/SignedSum.lean — T37 cycle 251 (goal G9): the triangle inequality for sums, in squared form, every ordered field.

  Lean 4 core plus ns/TransferBound (le_of_sq_signed), lib/OField, lib/RingTac.
    mul_le_of_sq   t² ≤ m², T² ≤ M², m, M ≥ 0, mM ≠ 0  ⇒  tT ≤ mM
    signed_sum     t_i² ≤ m_i², m_i ≥ 0, m_i ≠ 0  ⇒  (Σ t)² ≤ (Σ m)²   (prod_ne, sum_ne: no zero products)
  With t = a signed triad term and m its magnitude (TriadBound: t² ≤ |u|²|k|²|v|²|w|²), this is the domination
  T² ≤ (Σ a S)² that TransferBound takes as input.
-/
import ns.TransferBound

namespace SignedSum
open DetChain NSChain LibRing LibCS LibGron LibOF TransferBound

variable {R : Type} (K : CRing R) {I : Type}

theorem mul_le_of_sq (P : Pos K) (F : OF K P) (t T m M : R) (hm : P.nn m) (hM : P.nn M)
    (h0 : K.mul m M ≠ K.zero)
    (ht : le K P (K.mul t t) (K.mul m m)) (hT : le K P (K.mul T T) (K.mul M M)) :
    le K P (K.mul t T) (K.mul m M) := by
  apply le_of_sq_signed K P F _ _ (P.nn_mul _ _ hm hM) h0
  -- (tT)² = t²T² ≤ m²T² ≤ m²M²
  have a := le_mul_left K P (P.nn_sq T) ht
  have b := le_mul_left K P (P.nn_sq m) hT
  unfold le at a b ⊢
  rw [show sub K (K.mul (K.mul m M) (K.mul m M)) (K.mul (K.mul t T) (K.mul t T))
      = K.add (sub K (K.mul (K.mul m m) (K.mul M M)) (K.mul (K.mul m m) (K.mul T T)))
          (sub K (K.mul (K.mul T T) (K.mul m m)) (K.mul (K.mul T T) (K.mul t t))) by lib_ring]
  exact P.nn_add _ _ b a

theorem prod_ne (P : Pos K) (F : OF K P) (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := by
  intro h
  apply hy
  have hi := F.mul_inv x hx
  calc y = K.mul (K.mul x (F.inv x)) y := by rw [hi]; lib_ring
    _ = K.mul (F.inv x) (K.mul x y) := by lib_ring
    _ = K.zero := by rw [h]; lib_ring

theorem sum_ne (P : Pos K) (F : OF K P) (m : I → R) (hm : ∀ i, P.nn (m i)) (h0 : ∀ i, m i ≠ K.zero) :
    ∀ (j : I) (l : List I), lsum K m (j :: l) ≠ K.zero := by
  intro j l h
  have hr : P.nn (lsum K m l) := by
    have g : ∀ l : List I, P.nn (lsum K m l) := by
      intro l; induction l with
      | nil => exact P.nn_zero
      | cons x l ihl => exact P.nn_add _ _ (hm x) ihl
    exact g l
  apply h0 j
  apply F.antisymm _ (hm j)
  have e : K.neg (m j) = lsum K m l := by
    have h' : K.add (m j) (lsum K m l) = K.zero := h
    calc K.neg (m j) = K.add (K.neg (m j)) (K.add (m j) (lsum K m l)) := by rw [h']; lib_ring
      _ = lsum K m l := by lib_ring
  rw [e]; exact hr

theorem signed_sum (P : Pos K) (F : OF K P) (t m : I → R) (hm : ∀ i, P.nn (m i))
    (h0 : ∀ i, m i ≠ K.zero) (ht : ∀ i, le K P (K.mul (t i) (t i)) (K.mul (m i) (m i))) :
    ∀ l : List I, le K P (K.mul (lsum K t l) (lsum K t l)) (K.mul (lsum K m l) (lsum K m l))
  | [] => by
    unfold le; show P.nn (sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero))
    rw [show sub K (K.mul K.zero K.zero) (K.mul K.zero K.zero) = K.zero by lib_ring]; exact P.nn_zero
  | i :: l => by
    have ih := signed_sum P F t m hm h0 ht l
    have hM : P.nn (lsum K m l) := by
      have g : ∀ l : List I, P.nn (lsum K m l) := by
        intro l; induction l with
        | nil => exact P.nn_zero
        | cons x l ihl => exact P.nn_add _ _ (hm x) ihl
      exact g l
    have cross : le K P (K.mul (t i) (lsum K t l)) (K.mul (m i) (lsum K m l)) := by
      cases l with
      | nil =>
        unfold le; show P.nn (sub K (K.mul (m i) K.zero) (K.mul (t i) K.zero))
        rw [show sub K (K.mul (m i) K.zero) (K.mul (t i) K.zero) = K.zero by lib_ring]; exact P.nn_zero
      | cons j l' =>
        exact mul_le_of_sq K P F _ _ _ _ (hm i) hM (prod_ne K P F _ _ (h0 i) (sum_ne K P F m hm h0 j l')) (ht i) ih
    unfold le at ih cross ⊢
    show P.nn (sub K (K.mul (K.add (m i) (lsum K m l)) (K.add (m i) (lsum K m l)))
      (K.mul (K.add (t i) (lsum K t l)) (K.add (t i) (lsum K t l))))
    rw [show sub K (K.mul (K.add (m i) (lsum K m l)) (K.add (m i) (lsum K m l)))
        (K.mul (K.add (t i) (lsum K t l)) (K.add (t i) (lsum K t l)))
      = K.add (K.add (sub K (K.mul (m i) (m i)) (K.mul (t i) (t i)))
          (sub K (K.mul (lsum K m l) (lsum K m l)) (K.mul (lsum K t l) (lsum K t l))))
          (K.add (sub K (K.mul (m i) (lsum K m l)) (K.mul (t i) (lsum K t l)))
                 (sub K (K.mul (m i) (lsum K m l)) (K.mul (t i) (lsum K t l)))) by lib_ring]
    exact P.nn_add _ _ (P.nn_add _ _ (ht i) ih) (P.nn_add _ _ cross cross)

end SignedSum

#print axioms SignedSum.mul_le_of_sq
#print axioms SignedSum.prod_ne
#print axioms SignedSum.sum_ne
#print axioms SignedSum.signed_sum

#print axioms: 4 theorems, no axioms.

79.88 Prodi–Serrin from triad data

The Crank–Nicolson Galerkin bound with the transfer read from triad magnitudes. \(Z_N(1-S_N)^2\le Z_0,\quad S_N=\sum _{t<N}hX_t^2/(2\nu )\)

ns/CNSerrinTriad.lean

/-
  ns/CNSerrinTriad.lean — T37 cycle 257 (goal G11): Prodi–Serrin for a Crank–Nicolson Galerkin run with the
  transfer bound derived from the triad sum.

  Lean 4 core plus ns/CNSerrin, ns/TransferBound, ns/SignedSum. As CNSerrin.cn_serrin with c = 1, except that the
  hypothesis hT is replaced, at every time t, by the triad data: magnitudes a_t ≥ 0 (X_t = Σ a_t, the Wiener
  norm), b_t with Σ b_t² = ρ_t², c_t with Σ_q c_t(σ(p,q))² ≤ σ_t² for every p, X_tρ_tσ_t ≠ 0, and the domination
  T_t² ≤ (Σ_p a_p Σ_q b_q c_{σ(p,q)})² of the transfer T_t = Σ_i w_i s_i N_{t,i} (SignedSum.signed_sum when T_t is a
  sum of terms dominated termwise). Then
      Z_N (1 − S_N)² ≤ Z_0,   S_N = Σ_{t<N} h X_t²/(2ν)                                   (cn_serrin_triad)
-/
import ns.CNSerrin
import ns.SignedSum

namespace CNSerrinTriad
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound

variable {R : Type} (K : CRing R) {I : Type}

theorem cn_serrin_triad (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    -- triad data at every time
    (hA : ∀ t i, P.nn (A t i)) (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    (hdom : ∀ t, le K P
      (K.mul (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l)
             (lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l))
      (K.mul (lsum K (fun p => K.mul (A t p) (lsum K (fun q => K.mul (B t q) (C t (σf t p q))) (lt t))) (lt t))
             (lsum K (fun p => K.mul (A t p) (lsum K (fun q => K.mul (B t q) (C t (σf t p q))) (lt t))) (lt t))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  cn_serrin K P F w k a Nn X ρ σ h K.one ν l hw hh hν hν0 h4 step hσ hρ
    (fun t => transfer_bound K P F (A t) (B t) (C t) (σf t) (lt t) _ h (X t) (ρ t) (σ t) hh (hA t) (hX t) (hB t)
      (hC t) (hXn t) (hρn t) (hσn t) (h0 t) (hdom t))
    hb1 N hS

end CNSerrinTriad

#print axioms CNSerrinTriad.cn_serrin_triad

#print axioms: 1 theorem, no axioms.

79.89 Domination from termwise triad bounds

The domination hypothesis of the CN Prodi–Serrin chain follows from termwise bounds on the triad expansion of the transfer. \(t_{pq}^2\le (A_pB_qC_{\sigma (p,q)})^2\Rightarrow \big (\sum _{p,q}t_{pq}\big )^2\le \big (\sum _pA_p\sum _qB_qC_{\sigma (p,q)}\big )^2\)

ns/TriadDom.lean

/-
  ns/TriadDom.lean — T37 cycle 272 (goal G14): the domination hypothesis hdom of CNSerrinTriad from termwise triad
  bounds, every ordered field.

  Lean 4 core plus ns/SignedSum (signed_sum, prod_ne, sum_ne), ns/CNSerrinTriad, NSChain (lsum_mul_left).
    triad_dom        T = Σ_p Σ_q t(p,q),  t(p,q)² ≤ (A_p · B_q C_{σ(p,q)})²,  A, B, C ≥ 0, A_p ≠ 0, B_q C_{σ(p,q)} ≠ 0
                     ⇒  T² ≤ (Σ_p A_p Σ_q B_q C_{σ(p,q)})²        (inner and outer signed_sum)
    cn_serrin_triad_terms   CNSerrinTriad.cn_serrin_triad with hdom replaced by the triad expansion of the transfer
                     and the termwise bounds (TriadBound.triad_sq_bound gives them for Fourier triads).
-/
import ns.CNSerrinTriad

namespace TriadDom
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound SignedSum

variable {R : Type} (K : CRing R) {I : Type}

theorem le_refl_sq (P : Pos K) (x : R) : le K P (K.mul x x) (K.mul x x) := by
  unfold le; rw [show sub K (K.mul x x) (K.mul x x) = K.zero by lib_ring]; exact P.nn_zero

/-- **Domination from termwise triad bounds.** -/
theorem triad_dom (P : Pos K) (F : OF K P) (A B C : I → R) (σf : I → I → I) (t : I → I → R) (l : List I) (T : R)
    (hT : T = lsum K (fun p => lsum K (t p) l) l)
    (hA : ∀ p, P.nn (A p)) (hB : ∀ q, P.nn (B q)) (hC : ∀ r, P.nn (C r))
    (hA0 : ∀ p, A p ≠ K.zero) (hBC0 : ∀ p q, K.mul (B q) (C (σf p q)) ≠ K.zero)
    (ht : ∀ p q, le K P (K.mul (t p q) (t p q))
      (K.mul (K.mul (A p) (K.mul (B q) (C (σf p q)))) (K.mul (A p) (K.mul (B q) (C (σf p q)))))) :
    le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) l)) l)
             (lsum K (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) l)) l)) := by
  rw [hT]
  cases l with
  | nil => exact le_refl_sq K P _
  | cons j l' =>
    -- inner: (Σ_q t(p,q))² ≤ (A_p Σ_q B_q C)²
    have inner : ∀ p, le K P (K.mul (lsum K (t p) (j :: l')) (lsum K (t p) (j :: l')))
        (K.mul (K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))
               (K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))) := by
      intro p
      have s := signed_sum K P F (t p) (fun q => K.mul (A p) (K.mul (B q) (C (σf p q))))
        (fun q => P.nn_mul _ _ (hA p) (P.nn_mul _ _ (hB q) (hC _)))
        (fun q => prod_ne K P F _ _ (hA0 p) (hBC0 p q)) (ht p) (j :: l')
      rw [lsum_mul_left K (A p) (fun q => K.mul (B q) (C (σf p q))) (j :: l')] at s
      exact s
    have hS : ∀ p, P.nn (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')) := by
      intro p
      have g : ∀ l : List I, P.nn (lsum K (fun q => K.mul (B q) (C (σf p q))) l) := by
        intro l; induction l with
        | nil => exact P.nn_zero
        | cons x l ihl => exact P.nn_add _ _ (P.nn_mul _ _ (hB x) (hC _)) ihl
      exact g _
    have hS0 : ∀ p, lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l') ≠ K.zero := fun p =>
      sum_ne K P F (fun q => K.mul (B q) (C (σf p q))) (fun q => P.nn_mul _ _ (hB q) (hC _)) (hBC0 p) j l'
    exact signed_sum K P F (fun p => lsum K (t p) (j :: l'))
      (fun p => K.mul (A p) (lsum K (fun q => K.mul (B q) (C (σf p q))) (j :: l')))
      (fun p => P.nn_mul _ _ (hA p) (hS p)) (fun p => prod_ne K P F _ _ (hA0 p) (hS0 p)) inner (j :: l')

/-- **Prodi–Serrin for a CN Galerkin run from termwise triad bounds.** -/
theorem cn_serrin_triad_terms (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I) (tt : Nat → I → I → R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- the transfer as a triad sum, and the termwise bounds
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (tt t p) (lt t)) (lt t))
    (hA0 : ∀ t p, A t p ≠ K.zero) (hBC0 : ∀ t p q, K.mul (B t q) (C t (σf t p q)) ≠ K.zero)
    (htt : ∀ t p q, le K P (K.mul (tt t p q) (tt t p q))
      (K.mul (K.mul (A t p) (K.mul (B t q) (C t (σf t p q)))) (K.mul (A t p) (K.mul (B t q) (C t (σf t p q))))))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  CNSerrinTriad.cn_serrin_triad K P F w k a Nn X ρ σ h ν l A B C σf lt hw hh hν hν0 h4 step hσ hρ hA hX hB hC
    hXn hρn hσn h0
    (fun t => triad_dom K P F (A t) (B t) (C t) (σf t) (tt t) (lt t) _ (hexp t) (hA t) (hBn t) (hCn t) (hA0 t)
      (hBC0 t) (htt t))
    hb1 N hS

end TriadDom

#print axioms TriadDom.le_refl_sq
#print axioms TriadDom.triad_dom
#print axioms TriadDom.cn_serrin_triad_terms

#print axioms: 3 theorems, no axioms.

79.90 Fourier triads in the CN Prodi–Serrin chain

A Fourier triad term is bounded by its magnitudes, so the chain runs on the vectors of the Galerkin run. \(((u_p\cdot k_q)(v_q\cdot w_r))^2\le (A_pB_qC_r)^2,\quad A^2=|u|^2,\ B^2=|k|^2|v|^2,\ C^2=|w|^2\)

ns/FourierTriad.lean

/-
  ns/FourierTriad.lean — T37 cycle 273 (goal G15): the termwise triad bounds of TriadDom for Fourier–Galerkin triads.

  Lean 4 core plus ns/TriadDom, ns/TriadBound (triad_sq_bound), ns/LocalContAgmon (toCone, le_of_Le). One triad term
  t = (u_p·k_q)(v_q·w_r) with magnitudes A² = u·u, B² = (k·k)(v·v), C² = w·w satisfies t² ≤ (A·(B·C))²
  (triad_term), so cn_serrin_triad_terms applies with the vectors of the run (cn_serrin_fourier):
      Z_N (1 − S_N)² ≤ Z_0,  S_N = Σ_{t<N} h X_t²/(2ν),  X_t = Σ_p A_{t,p}
  given the triad expansion of the transfer, the magnitude identities, and the non-vanishing hypotheses.
-/
import ns.TriadDom
import ns.TriadBound
import ns.TriadDomEps

namespace FourierTriad
open DetChain NSChain LibRing LibCS LibGron LibOF SerrinSum EnstrophyStep CNSerrin TransferBound SignedSum
  LocalContAgmon
open CFKernel (V dot)

variable {R : Type} (K : CRing R) {I : Type}

/-- **One Fourier triad term against its magnitudes.** -/
theorem triad_term (P : Pos K) (u k v w : V R) (A B C : R) (hA : K.mul A A = dot K u u)
    (hB : K.mul B B = K.mul (dot K k k) (dot K v v)) (hC : K.mul C C = dot K w w) :
    le K P (K.mul (K.mul (dot K u k) (dot K v w)) (K.mul (dot K u k) (dot K v w)))
      (K.mul (K.mul A (K.mul B C)) (K.mul A (K.mul B C))) := by
  have s := le_of_Le K P _ _ (TriadBound.triad_sq_bound K (toCone K P) u k v w)
  rw [show K.mul (K.mul (dot K u k) (dot K v w)) (K.mul (dot K u k) (dot K v w))
      = K.mul (K.mul (dot K u k) (dot K u k)) (K.mul (dot K v w) (dot K v w)) by lib_ring,
    show K.mul (K.mul A (K.mul B C)) (K.mul A (K.mul B C)) = K.mul (K.mul A A) (K.mul (K.mul B B) (K.mul C C)) by lib_ring,
    hA, hB, hC,
    show K.mul (dot K u u) (K.mul (K.mul (dot K k k) (dot K v v)) (dot K w w))
      = K.mul (K.mul (dot K u u) (dot K k k)) (K.mul (dot K v v) (dot K w w)) by lib_ring]
  exact s

/-- **Prodi–Serrin for a CN Fourier–Galerkin run, triads written out.** -/
theorem cn_serrin_fourier (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (U Kv Vv W : Nat → I → V R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (hX : ∀ t, X t = lsum K (A t) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (B t q) (B t q)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (C t (σf t p q)) (C t (σf t p q))) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- Fourier triads: transfer = Σ_p Σ_q (U_p·Kv_q)(Vv_q·W_{σ(p,q)}), magnitudes
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (fun q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t)) (lt t))
    (hAA : ∀ t p, K.mul (A t p) (A t p) = dot K (U t p) (U t p))
    (hBB : ∀ t q, K.mul (B t q) (B t q) = K.mul (dot K (Kv t q) (Kv t q)) (dot K (Vv t q) (Vv t q)))
    (hCC : ∀ t r, K.mul (C t r) (C t r) = dot K (W t r) (W t r))
    (hA0 : ∀ t p, A t p ≠ K.zero) (hBC0 : ∀ t p q, K.mul (B t q) (C t (σf t p q)) ≠ K.zero)
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  TriadDom.cn_serrin_triad_terms K P F w k a Nn X ρ σ h ν l A B C σf lt
    (fun t p q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q))))
    hw hh hν hν0 h4 step hσ hρ hA hBn hCn hX hB hC hXn hρn hσn h0 hexp hA0 hBC0
    (fun t p q => triad_term K P (U t p) (Kv t q) (Vv t q) (W t (σf t p q)) (A t p) (B t q) (C t (σf t p q))
      (hAA t p) (hBB t q) (hCC t _))
    hb1 N hS

/-- **The same with ε-shifted magnitudes: no non-vanishing hypothesis on A, B, C.** -/
theorem cn_serrin_fourier_eps (P : Pos K) (F : OF K P) (w k : I → R) (a Nn : Nat → I → R) (X ρ σ : Nat → R) (h ν : R)
    (l : List I) (A B C : Nat → I → R) (σf : Nat → I → I → I) (lt : Nat → List I)
    (U Kv Vv W : Nat → I → V R)
    (hw : ∀ i, P.nn (w i)) (hh : P.nn h) (hν : P.nn ν) (hν0 : K.add ν ν ≠ K.zero)
    (h4 : K.add (K.add ν ν) (K.add ν ν) ≠ K.zero)
    (step : ∀ t i, sub K (a (t + 1) i) (a t i)
      = K.mul h (K.add (K.neg (K.mul ν (K.mul (k i) (K.add (a (t + 1) i) (a t i))))) (Nn t i)))
    (hσ : ∀ t, K.mul (σ t) (σ t)
      = lsum K (fun i => K.mul (w i) (K.mul (k i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i))))) l)
    (hρ : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (K.add (a (t + 1) i) (a t i)))) l)
    (hA : ∀ t i, P.nn (A t i)) (hBn : ∀ t i, P.nn (B t i)) (hCn : ∀ t i, P.nn (C t i))
    (e : R) (he : P.nn e) (he0 : e ≠ K.zero)
    (hX : ∀ t, X t = lsum K (fun p => K.add (A t p) e) (lt t))
    (hB : ∀ t, K.mul (ρ t) (ρ t) = lsum K (fun q => K.mul (K.add (B t q) e) (K.add (B t q) e)) (lt t))
    (hC : ∀ t p, le K P (lsum K (fun q => K.mul (K.add (C t (σf t p q)) e) (K.add (C t (σf t p q)) e)) (lt t)) (K.mul (σ t) (σ t)))
    (hXn : ∀ t, P.nn (X t)) (hρn : ∀ t, P.nn (ρ t)) (hσn : ∀ t, P.nn (σ t))
    (h0 : ∀ t, K.mul (K.mul (X t) (ρ t)) (σ t) ≠ K.zero)
    -- Fourier triads: transfer = Σ_p Σ_q (U_p·Kv_q)(Vv_q·W_{σ(p,q)}), magnitudes
    (hexp : ∀ t, lsum K (fun i => K.mul (w i) (K.mul (K.add (a (t + 1) i) (a t i)) (Nn t i))) l
      = lsum K (fun p => lsum K (fun q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t)) (lt t))
    (hAA : ∀ t p, K.mul (A t p) (A t p) = dot K (U t p) (U t p))
    (hBB : ∀ t q, K.mul (B t q) (B t q) = K.mul (dot K (Kv t q) (Kv t q)) (dot K (Vv t q) (Vv t q)))
    (hCC : ∀ t r, K.mul (C t r) (C t r) = dot K (W t r) (W t r))
    (hb1 : ∀ t, P.nn (sub K K.one (bk K P F h K.one ν X t)))
    (N : Nat) (hS : P.nn (sub K K.one (sumA K (bk K P F h K.one ν X) N))) :
    le K P (K.mul (Zw K w (a N) l) (K.mul (sub K K.one (sumA K (bk K P F h K.one ν X) N))
      (sub K K.one (sumA K (bk K P F h K.one ν X) N)))) (Zw K w (a 0) l) :=
  CNSerrinTriad.cn_serrin_triad K P F w k a Nn X ρ σ h ν l (fun t p => K.add (A t p) e) (fun t q => K.add (B t q) e)
    (fun t r => K.add (C t r) e) σf lt hw hh hν hν0 h4 step hσ hρ (fun t p => P.nn_add _ _ (hA t p) he) hX hB hC
    hXn hρn hσn h0
    (fun t => by
      rw [hexp t]
      exact TriadDomEps.triad_dom_eps K P F (A t) (B t) (C t) (σf t)
        (fun p q => K.mul (dot K (U t p) (Kv t q)) (dot K (Vv t q) (W t (σf t p q)))) (lt t) _ e rfl
        (hA t) (hBn t) (hCn t) he he0
        (fun p q => triad_term K P (U t p) (Kv t q) (Vv t q) (W t (σf t p q)) (A t p) (B t q) (C t (σf t p q))
          (hAA t p) (hBB t q) (hCC t _)))
    hb1 N hS

end FourierTriad

#print axioms FourierTriad.triad_term
#print axioms FourierTriad.cn_serrin_fourier
#print axioms FourierTriad.cn_serrin_fourier_eps

#print axioms: 3 theorems, no axioms.

79.91 Domination without non-vanishing hypotheses

For every \(\varepsilon >0\) the shifted magnitudes are non-zero and dominate; vanishing Fourier modes are allowed. \(T^2\le \big (\sum _p(A_p+\varepsilon )\sum _q(B_q+\varepsilon )(C_{\sigma (p,q)}+\varepsilon )\big )^2\)

ns/TriadDomEps.lean

/-
  ns/TriadDomEps.lean — T37 cycle 293 (goal G36): TriadDom without the non-vanishing hypotheses, by an ε-shift.

  Lean 4 core plus ns/TriadDom (triad_dom), ns/SignedSum (prod_ne), lib/OField. For every ε ≥ 0, ε ≠ 0 the shifted
  magnitudes A + ε, B + ε, C + ε are non-zero (a + ε = 0 with a, ε ≥ 0 forces ε = 0) and dominate the old ones, so
      t(p,q)² ≤ (A_p B_q C_{σ(p,q)})²   ⇒   T² ≤ (Σ_p (A_p + ε) Σ_q (B_q + ε)(C_{σ(p,q)} + ε))²
  with no condition on zero magnitudes (Fourier modes may vanish). Letting ε → 0 is not available constructively;
  the bound holds for every ε.
    shift_ne, sq_mono, mul_mono, triad_dom_eps
-/
import ns.TriadDom

namespace TriadDomEps
open DetChain NSChain LibRing LibCS LibGron LibOF SignedSum TriadDom

variable {R : Type} (K : CRing R) {I : Type}

theorem shift_ne (P : Pos K) (F : OF K P) (a e : R) (ha : P.nn a) (he : P.nn e) (he0 : e ≠ K.zero) :
    K.add a e ≠ K.zero := by
  intro h
  apply he0
  apply F.antisymm _ he
  rw [show K.neg e = a by
    calc K.neg e = K.add (K.neg e) (K.add a e) := by rw [h]; lib_ring
      _ = a := by lib_ring]
  exact ha

theorem sq_mono (P : Pos K) (x y : R) (hx : P.nn x) (h : le K P x y) : le K P (K.mul x x) (K.mul y y) := by
  unfold le at *
  rw [show sub K (K.mul y y) (K.mul x x) = K.add (K.mul (sub K y x) (sub K y x)) (K.add (K.mul x (sub K y x)) (K.mul x (sub K y x))) by lib_ring]
  exact P.nn_add _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_mul _ _ hx h) (P.nn_mul _ _ hx h))

theorem mul_mono (P : Pos K) (a b a' b' : R) (ha : P.nn a) (hb' : P.nn b') (h1 : le K P a a') (h2 : le K P b b') :
    le K P (K.mul a b) (K.mul a' b') := by
  unfold le at *
  rw [show sub K (K.mul a' b') (K.mul a b) = K.add (K.mul (sub K a' a) b') (K.mul a (sub K b' b)) by lib_ring]
  exact P.nn_add _ _ (P.nn_mul _ _ h1 hb') (P.nn_mul _ _ ha h2)

theorem le_shift (P : Pos K) (a e : R) (he : P.nn e) : le K P a (K.add a e) := by
  unfold le; rw [show sub K (K.add a e) a = e by lib_ring]; exact he

/-- **Domination for every ε-shift, no non-vanishing hypotheses.** -/
theorem triad_dom_eps (P : Pos K) (F : OF K P) (A B C : I → R) (σf : I → I → I) (t : I → I → R) (l : List I) (T e : R)
    (hT : T = lsum K (fun p => lsum K (t p) l) l)
    (hA : ∀ p, P.nn (A p)) (hB : ∀ q, P.nn (B q)) (hC : ∀ r, P.nn (C r)) (he : P.nn e) (he0 : e ≠ K.zero)
    (ht : ∀ p q, le K P (K.mul (t p q) (t p q))
      (K.mul (K.mul (A p) (K.mul (B q) (C (σf p q)))) (K.mul (A p) (K.mul (B q) (C (σf p q)))))) :
    le K P (K.mul T T)
      (K.mul (lsum K (fun p => K.mul (K.add (A p) e) (lsum K (fun q => K.mul (K.add (B q) e) (K.add (C (σf p q)) e)) l)) l)
             (lsum K (fun p => K.mul (K.add (A p) e) (lsum K (fun q => K.mul (K.add (B q) e) (K.add (C (σf p q)) e)) l)) l)) := by
  apply triad_dom K P F (fun p => K.add (A p) e) (fun q => K.add (B q) e) (fun r => K.add (C r) e) σf t l T hT
    (fun p => P.nn_add _ _ (hA p) he) (fun q => P.nn_add _ _ (hB q) he) (fun r => P.nn_add _ _ (hC r) he)
    (fun p => shift_ne K P F _ _ (hA p) he he0)
    (fun p q => prod_ne K P F _ _ (shift_ne K P F _ _ (hB q) he he0) (shift_ne K P F _ _ (hC _) he he0))
  intro p q
  apply le_trans K P (ht p q)
  apply sq_mono K P _ _ (P.nn_mul _ _ (hA p) (P.nn_mul _ _ (hB q) (hC _)))
  apply mul_mono K P _ _ _ _ (hA p) (P.nn_mul _ _ (P.nn_add _ _ (hB q) he) (P.nn_add _ _ (hC _) he)) (le_shift K P _ _ he)
  exact mul_mono K P _ _ _ _ (hB q) (P.nn_add _ _ (hC _) he) (le_shift K P _ _ he) (le_shift K P _ _ he)

end TriadDomEps

#print axioms TriadDomEps.shift_ne
#print axioms TriadDomEps.sq_mono
#print axioms TriadDomEps.mul_mono
#print axioms TriadDomEps.triad_dom_eps

#print axioms: 4 theorems, no axioms.

79.92 Large data, supercritical dissipation: Crank–Nicolson steps

Dyadic model, every truncation \(N\): with \(s_t=a_{t+1}+a_t\) under the hypotheses of the rate bound, \(E_w(a_t)\le E_w(a_0)+t\,hB\), \(B=\sum _{n<n_0}q_nEA\) independent of \(N\ge n_0\).

ns/DyadicLargeCN.lean

/-
  ns/DyadicLargeCN.lean — T37 cycle 686 (goal G399): the large-data bound of ns/DyadicLarge for actual
  Crank–Nicolson time steps.

  Lean 4 core plus ns/DyadicCN, ns/DyadicLarge. With s_t = a_{t+1} + a_t and, for every step, the hypotheses
  of DyadicLarge.rate_bound on s_t (shells ≤ A, s² ≤ E, tail shells n ≥ n₀ below the critical threshold M_n),
    cn_large_step    E_w(a_{t+1}) ≤ E_w(a_t) + h·B,        B = Σ_{n<N} [n < n₀] q_n E A
    cn_large_global  E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B    (linear growth in t)
  and B does not depend on N ≥ n₀ (DyadicLarge.head_stable): no truncation of the supercritical model
  blows up, uniformly in N.
-/
import ns.DyadicCN
import ns.DyadicLarge

namespace DyadicLargeCN
open DetChain NSChain DyadicHs DyadicCN DyadicLarge

section Ring
variable {R : Type} (K : CRing R)

def headB (w c : Nat → R) (E A : R) (n0 N : Nat) : R :=
  sumN K (fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero) N

theorem cn_large_step (P : Cone K) (w c k a a' M : Nat → R) (h nu A E : R) (n0 N : Nat) (hh : P.nn h)
    (h0 : a 0 = K.zero) (h0' : a' 0 = K.zero) (hN : a (N + 1) = K.zero) (hN' : a' (N + 1) = K.zero)
    (step : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a' (n + 1)) (a (n + 1))))))
          (dyN K c (fun i => K.add (a' i) (a i)) n)))
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ n, n0 ≤ n → Le K P (K.add (a' (n + 1)) (a (n + 1))) (M n))
    (hA : ∀ n, Le K P (K.add (a' (n + 1)) (a (n + 1))) A) (hAnn : P.nn A)
    (hE : ∀ n, Le K P (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))) E)
    (hD : ∀ n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))))) :
    Le K P (Ew K w a' N) (K.add (Ew K w a N) (K.mul h (headB K w c E A n0 N))) := by
  have s0 : K.add (a' 0) (a 0) = K.zero := by rw [h0, h0', K.zero_add]
  have sN : K.add (a' (N + 1)) (a (N + 1)) = K.zero := by rw [hN, hN', K.zero_add]
  have hr := rate_bound K P w c k (fun i => K.add (a' i) (a i)) M nu A E n0 N s0 sN hq hcrit htail hA hAnn hE hD
  rw [cn_hs_identity K w c k a a' (fun i => K.add (a' i) (a i)) h nu N (fun _ => rfl) step]
  exact le_add' K P _ _ _ _ (le_refl' K P _) (le_mul_nn K P h _ _ hh hr)

theorem cn_large_global (P : Cone K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A E : R) (n0 N : Nat)
    (hh : P.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t, a t (N + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ t n, n0 ≤ n → Le K P (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (M n))
    (hA : ∀ t n, Le K P (K.add (a (t + 1) (n + 1)) (a t (n + 1))) A) (hAnn : P.nn A)
    (hE : ∀ t n, Le K P (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))) E)
    (hD : ∀ t n, P.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))))) :
    ∀ t, Le K P (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c E A n0 N)) t))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add _ _ = K.add _ K.zero; rw [add_zero']⟩
  | t + 1 => by
      have s1 := cn_large_step K P w c k (a t) (a (t + 1)) M h nu A E n0 N hh (h0 t) (h0 (t + 1)) (hN t)
        (hN (t + 1)) (step t) hq hcrit (htail t) (hA t) hAnn (hE t) (hD t)
      have ih := cn_large_global P w c k M a h nu A E n0 N hh h0 hN step hq hcrit htail hA hAnn hE hD t
      have s2 := le_add' K P _ _ _ _ ih (le_refl' K P (K.mul h (headB K w c E A n0 N)))
      have s3 := le_trans' K P _ _ _ s1 s2
      show Le K P _ (K.add (Ew K w (a 0) N)
        (K.add (sumN K (fun _ => K.mul h (headB K w c E A n0 N)) t) (K.mul h (headB K w c E A n0 N))))
      rw [← K.add_assoc]
      exact s3

end Ring

end DyadicLargeCN

#print axioms DyadicLargeCN.cn_large_step
#print axioms DyadicLargeCN.cn_large_global

#print axioms: 2 theorems, no axioms.

79.93 Large data from one energy bound

If every mode satisfies \(a_t(n)^2\le E\), \(4E\le A^2\) and \(M_n\ge A\) for \(n\ge n_0\), then \(E_w(a_t)\le E_w(a_0)+t\,hB\) with \(B=\sum _{n<n_0}q_n(4E)A\) (ordered ring, every truncation).

ns/DyadicLargeEnergy.lean

/-
  ns/DyadicLargeEnergy.lean — T37 cycle 687 (goal G400): the per-step hypotheses of ns/DyadicLargeCN from one
  energy bound.

  Lean 4 core plus ns/DyadicLargeCN, ns/DyadicSmallGlobal (ordered ring OrdR), lib/RingTac.
    sum_sq_le        x² ≤ E, y² ≤ E ⇒ (x + y)² ≤ 4E          ((x+y)² + (x−y)² = 2x² + 2y²)
    shell_le         x² ≤ E, y² ≤ E, 4E ≤ A², A ≥ 0 ⇒ x + y ≤ A
    cn_large_energy  if every mode of every a_t has a_t(n)² ≤ E (e.g. E = E(a_0), NSChain.mode_le_energy with the
                     energy decay of the CN scheme), 4E ≤ A², and the critical threshold M_n ≥ A for n ≥ n₀, then
                     E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B with B = Σ_{n<N} [n < n₀] q_n (4E) A.
-/
import ns.DyadicLargeCN
import ns.DyadicSmallGlobal
import lib.RingTac

namespace DyadicLargeEnergy
open DetChain NSChain DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicSmallGlobal

section Ring
variable {R : Type} (K : CRing R)

def four (E : R) : R := K.add (K.add E E) (K.add E E)

theorem sum_sq_le (P : Cone K) (x y E : R) (hx : Le K P (K.mul x x) E) (hy : Le K P (K.mul y y) E) :
    Le K P (K.mul (K.add x y) (K.add x y)) (four K E) := by
  have e : K.add (K.mul (K.add x y) (K.add x y)) (K.mul (K.add x (K.neg y)) (K.add x (K.neg y)))
      = K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y)) := by lib_ring
  have s1 : Le K P (K.mul (K.add x y) (K.add x y))
      (K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y))) :=
    ⟨_, P.nn_sq _, e⟩
  have s2 : Le K P (K.add (K.add (K.mul x x) (K.mul y y)) (K.add (K.mul x x) (K.mul y y))) (four K E) :=
    le_add' K P _ _ _ _ (le_add' K P _ _ _ _ hx hy) (le_add' K P _ _ _ _ hx hy)
  exact le_trans' K P _ _ _ s1 s2

theorem shell_le (O : OrdR K) (x y E A : R) (hx : Le K O.toCone (K.mul x x) E)
    (hy : Le K O.toCone (K.mul y y) E) (hA : Le K O.toCone (four K E) (K.mul A A)) (hAnn : O.nn A) :
    Le K O.toCone (K.add x y) A :=
  le_of_sq_le K O _ _ hAnn (le_trans' K O.toCone _ _ _ (sum_sq_le K O.toCone x y E hx hy) hA)

theorem cn_large_energy (O : OrdR K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A E : R) (n0 N : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t, a t (N + 1) = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, O.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (hmode : ∀ t n, Le K O.toCone (K.mul (a t n) (a t n)) E)
    (hA2 : Le K O.toCone (four K E) (K.mul A A)) (hAnn : O.nn A)
    (hM : ∀ n, n0 ≤ n → Le K O.toCone A (M n))
    (hD : ∀ t n, O.nn (K.mul nu (K.mul (w n) (K.mul (k n)
      (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n))))))) :
    ∀ t, Le K O.toCone (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c (four K E) A n0 N)) t)) :=
  have hA : ∀ t n, Le K O.toCone (K.add (a (t + 1) (n + 1)) (a t (n + 1))) A := fun t n =>
    shell_le K O _ _ E A (hmode (t + 1) (n + 1)) (hmode t (n + 1)) hA2 hAnn
  cn_large_global K O.toCone w c k M a h nu A (four K E) n0 N hh h0 hN step hq hcrit
    (fun t n hn => le_trans' K O.toCone _ _ _ (hA t n) (hM n hn)) hA hAnn
    (fun t n => sum_sq_le K O.toCone _ _ E (hmode (t + 1) n) (hmode t n)) hD

end Ring

end DyadicLargeEnergy

#print axioms DyadicLargeEnergy.sum_sq_le
#print axioms DyadicLargeEnergy.shell_le
#print axioms DyadicLargeEnergy.cn_large_energy

#print axioms: 3 theorems, no axioms.

79.94 Large data, supercritical dissipation: bound by the data

Crank–Nicolson dyadic model, every truncation: \(a_t(n)^2\le E_0=\sum a_0(n)^2\), and with \(4E_0\le A^2\), \(M_n\ge A\) for \(n\ge n_0\): \(E_w(a_t)\le E_w(a_0)+t\,h\sum _{n<n_0}q_n(4E_0)A\).

ns/DyadicLargeData.lean

/-
  ns/DyadicLargeData.lean — T37 cycle 688 (goal G401): the supercritical large-data bound of the dyadic model
  in terms of the data alone.

  Lean 4 core plus ns/DyadicLargeEnergy, NSChain.cn_global. For Crank–Nicolson steps of the viscous dyadic model on
  shells 1..N (h, ν, k_n, w_n ≥ 0), the CN energy decay gives a_t(n)² ≤ E₀ = Σ_{n<N} a_0(n+1)² for every t, n
  (NSChain.cn_global with the energy-neutral nonlinearity); so with 4E₀ ≤ A² and the critical threshold M_n ≥ A
  for n ≥ n₀,
    cn_large_data   E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B,   B = Σ_{n<N} [n < n₀] q_n (4E₀) A,
  every truncation N, every time t; B depends only on the data (E₀, A) and n₀, not on N ≥ n₀.
-/
import ns.DyadicLargeEnergy

namespace DyadicLargeData
open DetChain NSChain DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicSmallGlobal DyadicLargeEnergy

section Ring
variable {R : Type} (K : CRing R)

def E0 (a : Nat → Nat → R) (N : Nat) : R := sumN K (fun n => K.mul (a 0 (n + 1)) (a 0 (n + 1))) N

/-- Every mode of every time step is bounded by the initial energy. -/
theorem mode_bound (P : Cone K) (c k : Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n))) :
    ∀ t n, Le K P (K.mul (a t n) (a t n)) (E0 K a N) := by
  have g := cn_global K P (fun t n => a t (n + 1)) (fun n => k (n + 1))
    (fun t n => dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n) h nu N hh hnu (fun n => hk (n + 1)) step
    (fun t => dyadic_energy_neutral K c (fun i => K.add (a (t + 1) i) (a t i)) N
      (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
      (by show K.add (a (t + 1) (N + 1)) (a t (N + 1)) = K.zero
          rw [hz (t + 1) (N + 1) (Nat.le_refl _), hz t (N + 1) (Nat.le_refl _), K.zero_add]))
  have hE0 : P.nn (E0 K a N) := sq_sum_nonneg K P (fun n => a 0 (n + 1)) N
  have zle : ∀ t n, a t n = K.zero → Le K P (K.mul (a t n) (a t n)) (E0 K a N) := by
    intro t n hn
    rw [hn, mul_zero K]
    exact ⟨E0 K a N, hE0, K.zero_add _⟩
  intro t n
  cases n with
  | zero => exact zle t 0 (h0 t)
  | succ m =>
    cases Nat.decLt m N with
    | isTrue hm => exact g.2 t m hm
    | isFalse hm => exact zle t (m + 1) (hz t (m + 1) (Nat.succ_le_succ (Nat.le_of_not_lt hm)))

theorem cn_large_data (O : OrdR K) (w c k M : Nat → R) (a : Nat → Nat → R) (h nu A : R) (n0 N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hw : ∀ n, O.nn (w n))
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, O.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (hA2 : Le K O.toCone (four K (E0 K a N)) (K.mul A A)) (hAnn : O.nn A)
    (hM : ∀ n, n0 ≤ n → Le K O.toCone A (M n)) :
    ∀ t, Le K O.toCone (Ew K w (a t) N)
      (K.add (Ew K w (a 0) N) (sumN K (fun _ => K.mul h (headB K w c (four K (E0 K a N)) A n0 N)) t)) :=
  cn_large_energy K O w c k M a h nu A (E0 K a N) n0 N hh h0 (fun t => hz t (N + 1) (Nat.le_refl _)) step hq
    hcrit (mode_bound K O.toCone c k a h nu N hh hnu hk h0 hz step) hA2 hAnn hM
    (fun _ n => O.nn_mul _ _ hnu (O.nn_mul _ _ (hw n) (O.nn_mul _ _ (hk n) (O.nn_sq _))))

end Ring

end DyadicLargeData

#print axioms DyadicLargeData.mode_bound
#print axioms DyadicLargeData.cn_large_data

#print axioms: 2 theorems, no axioms.

79.95 The integers as an ordered ring

\(\mathbb Z\) with \(x\ge 0\iff \exists n,\ x=n\) is an ordered ring without zero divisors (no axioms), so the large-data dyadic bound holds for integer data.

ns/IntOrd.lean

/-
  ns/IntOrd.lean — T37 cycle 689 (goal G402): the integers as an ordered ring (DyadicSmallGlobal.OrdR) without
  axioms, so that the ordered-ring theorems of the dyadic model (small_data_global, DyadicLargeData.cn_large_data)
  apply to integer data.

  Lean 4 core plus lib/IntRing (LibInt.intR, int_noZD, lib_int). nn x := ∃ n : Nat, x = ofNat n.
    nn_or_neg   every integer z has nn z or nn (−z)
    intOrd      OrdR LibInt.intR (cone, total, antisymmetric, no zero divisors)
    int_large_data   DyadicLargeData.cn_large_data for integer data (CN dyadic model over ℤ)
-/
import ns.DyadicLargeData
import lib.IntRing

namespace IntOrd
open DetChain NSChain DyadicSmallGlobal LibInt

def NN (x : Int) : Prop := ∃ n : Nat, x = Int.ofNat n

theorem nn_or_neg : ∀ z : Int, NN z ∨ NN (-z)
  | Int.ofNat m => Or.inl ⟨m, rfl⟩
  | Int.negSucc m => Or.inr ⟨m + 1, rfl⟩

theorem nn_sq' : ∀ x : Int, NN (x * x)
  | Int.ofNat m => ⟨m * m, rfl⟩
  | Int.negSucc m => ⟨(m + 1) * (m + 1), rfl⟩

def intCone : Cone intR where
  nn := NN
  nn_zero := ⟨0, rfl⟩
  nn_add := fun x y hx hy => by
    cases hx with
    | intro m hm => cases hy with
      | intro n hn => exact ⟨m + n, by rw [hm, hn]; rfl⟩
  nn_mul := fun x y hx hy => by
    cases hx with
    | intro m hm => cases hy with
      | intro n hn => exact ⟨m * n, by rw [hm, hn]; rfl⟩
  nn_sq := nn_sq'

theorem nat_add_eq_zero : ∀ m n : Nat, m + n = 0 → m = 0
  | 0, _, _ => rfl
  | _ + 1, 0, h => Nat.noConfusion h
  | _ + 1, _ + 1, h => Nat.noConfusion h

theorem int_total (x y : Int) : Le intR intCone x y ∨ Le intR intCone y x := by
  cases nn_or_neg (y + -x) with
  | inl h => exact Or.inl ⟨y + -x, h, show x + (y + -x) = y by lib_int⟩
  | inr h => exact Or.inr ⟨-(y + -x), h, show y + -(y + -x) = x by lib_int⟩

theorem int_antisym (x y : Int) (h1 : Le intR intCone x y) (h2 : Le intR intCone y x) : x = y := by
  cases h1 with
  | intro d hd => cases h2 with
    | intro e he =>
      have e1 : x + d = y := hd.2
      have e2 : y + e = x := he.2
      have s : d + e = 0 := by
        have t : x + (d + e) = x := by
          rw [show x + (d + e) = (x + d) + e by lib_int, e1, e2]
        rw [show d + e = (x + (d + e)) + -x by lib_int, t]
        lib_int
      cases hd.1 with
      | intro m hm => cases he.1 with
        | intro n hn =>
          rw [hm, hn] at s
          have m0 : m = 0 := nat_add_eq_zero m n (Int.ofNat.inj s)
          rw [← e1, hm, m0]
          show x = x + 0
          lib_int

def intOrd : OrdR intR where
  toCone := intCone
  total := int_total
  antisym := int_antisym
  noZD := int_noZD

open DyadicHs DyadicCN DyadicLarge DyadicLargeCN DyadicLargeEnergy DyadicLargeData in
theorem int_large_data (w c k M : Nat → Int) (a : Nat → Nat → Int) (h nu A : Int) (n0 N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n)) (hw : ∀ n, NN (w n))
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hq : ∀ n, NN (qq intR w c n))
    (hcrit : ∀ n, n0 ≤ n → intR.mul (qq intR w c n) (M n) = intR.mul nu (intR.mul (w n) (k n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul A A)) (hAnn : NN A)
    (hM : ∀ n, n0 ≤ n → Le intR intCone A (M n)) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (sumN intR (fun _ => intR.mul h (headB intR w c (four intR (E0 intR a N)) A n0 N)) t)) :=
  cn_large_data intR intOrd w c k M a h nu A n0 N hh hnu hk hw h0 hz step hq hcrit hA2 hAnn hM

end IntOrd

#print axioms IntOrd.nn_or_neg
#print axioms IntOrd.nn_sq'
#print axioms IntOrd.nat_add_eq_zero
#print axioms IntOrd.int_total
#print axioms IntOrd.int_antisym
#print axioms IntOrd.intOrd
#print axioms IntOrd.int_large_data

#print axioms: 7 theorems, no axioms.

79.96 A concrete supercritical dyadic model over \(\mathbb Z\)

\(w_n=k_n=4^n\), \(c_n=2^n\), \(\nu =3\): \(M_n=2^n\), so \(n_0=A\); integer data with \(4E_0\le A^2\) satisfy \(E_w(a_t)\le E_w(a_0)+t\,h\sum _{n<A}q_n(4E_0)A\) for every truncation.

ns/DyadicModelZ.lean

/-
  ns/DyadicModelZ.lean — T37 cycle 690 (goal G403): a concrete supercritical dyadic model over ℤ with explicit n₀.

  Lean 4 core plus ns/IntOrd. Weights w_n = 4ⁿ (H¹), coupling c_n = 2ⁿ (λ = 2), dissipation k_n = 4ⁿ (α = 1),
  ν = 3. Then q_n = (w_{n+1} − w_n) c_n = 3·4ⁿ·2ⁿ, the critical threshold is M_n = 2ⁿ (q_n M_n = ν w_n k_n), and
  M_n ≥ A for every n ≥ n₀ = A. So for integer data with 4E₀ ≤ A²:
    model_bound   E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B,  B = Σ_{n<N} [n < A] q_n (4E₀) A,  every N, every t.
  (Powers are LibRing.pw intR, since core Int.pow lemmas need propext.)
-/
import ns.IntOrd

namespace DyadicModelZ
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData

def P2 (n : Nat) : Int := pw intR 2 n
def P4 (n : Nat) : Int := pw intR 4 n

theorem p2_nat : ∀ n, P2 n = Int.ofNat (2 ^ n)
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P2 n) 2 = Int.ofNat (2 ^ n * 2)
    rw [p2_nat n]; rfl

theorem p4_sq : ∀ n, P4 n = P2 n * P2 n
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P4 n) 4 = intR.mul (P2 n) 2 * intR.mul (P2 n) 2
    rw [p4_sq n]
    show P2 n * P2 n * 4 = P2 n * 2 * (P2 n * 2)
    lib_int

theorem nn_p2 (n : Nat) : NN (P2 n) := ⟨2 ^ n, p2_nat n⟩
theorem nn_p4 (n : Nat) : NN (P4 n) := by rw [p4_sq]; exact intCone.nn_mul _ _ (nn_p2 n) (nn_p2 n)

def w : Nat → Int := P4
def c : Nat → Int := P2
def k : Nat → Int := P4
def M : Nat → Int := P2

theorem qq_eq (n : Nat) : qq intR w c n = 3 * (P4 n * P2 n) := by
  show (intR.mul (P4 n) 4 + -P4 n) * P2 n = 3 * (P4 n * P2 n)
  show (P4 n * 4 + -P4 n) * P2 n = 3 * (P4 n * P2 n)
  lib_int

theorem crit (n : Nat) : intR.mul (qq intR w c n) (M n) = intR.mul 3 (intR.mul (w n) (k n)) := by
  rw [qq_eq]
  show 3 * (P4 n * P2 n) * P2 n = 3 * (P4 n * P4 n)
  rw [p4_sq n]
  lib_int

theorem nn_qq (n : Nat) : NN (qq intR w c n) := by
  rw [qq_eq]
  exact intCone.nn_mul _ _ ⟨3, rfl⟩ (intCone.nn_mul _ _ (nn_p4 n) (nn_p2 n))

theorem one_le_two_pow : ∀ n : Nat, 1 ≤ 2 ^ n
  | 0 => Nat.le_refl 1
  | n + 1 => by
    show 1 ≤ 2 ^ n * 2
    have e : 2 ^ n * 2 = 2 ^ n + 2 ^ n := by lib_nat
    rw [e]; exact Nat.le_trans (one_le_two_pow n) (Nat.le_add_right _ _)

theorem le_two_pow : ∀ n : Nat, n ≤ 2 ^ n
  | 0 => Nat.zero_le _
  | n + 1 => by
    show n + 1 ≤ 2 ^ n * 2
    have h1 : n + 1 ≤ 2 ^ n + 2 ^ n := Nat.add_le_add (le_two_pow n) (one_le_two_pow n)
    have e : 2 ^ n * 2 = 2 ^ n + 2 ^ n := by lib_nat
    rw [e]; exact h1

theorem le_dest {m p : Nat} (h : m ≤ p) : ∃ d, m + d = p := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    cases ih with
    | intro d hd => exact ⟨d + 1, by rw [← hd]; rfl⟩

theorem ofNat_le {m p : Nat} (h : m ≤ p) : Le intR intCone (Int.ofNat m) (Int.ofNat p) := by
  cases le_dest h with
  | intro d hd => exact ⟨Int.ofNat d, ⟨d, rfl⟩, by rw [← hd]; rfl⟩

/-- The critical threshold M_n = 2ⁿ is ≥ A = m for every n ≥ m. -/
theorem thresh (m n : Nat) (hn : m ≤ n) : Le intR intCone (Int.ofNat m) (M n) := by
  show Le intR intCone _ (P2 n)
  rw [p2_nat]
  exact ofNat_le (Nat.le_trans hn (le_two_pow n))

theorem model_bound (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N)
        (sumN intR (fun _ => intR.mul h (headB intR w c (four intR (E0 intR a N)) (Int.ofNat m) m N)) t)) :=
  int_large_data w c k M a h 3 (Int.ofNat m) m N hh ⟨3, rfl⟩ nn_p4 nn_p4 h0 hz step nn_qq
    (fun n _ => crit n) hA2 ⟨m, rfl⟩ (fun n hn => thresh m n hn)

end DyadicModelZ

#print axioms DyadicModelZ.p2_nat
#print axioms DyadicModelZ.p4_sq
#print axioms DyadicModelZ.nn_p2
#print axioms DyadicModelZ.nn_p4
#print axioms DyadicModelZ.qq_eq
#print axioms DyadicModelZ.crit
#print axioms DyadicModelZ.nn_qq
#print axioms DyadicModelZ.one_le_two_pow
#print axioms DyadicModelZ.le_two_pow
#print axioms DyadicModelZ.le_dest
#print axioms DyadicModelZ.ofNat_le
#print axioms DyadicModelZ.thresh
#print axioms DyadicModelZ.model_bound

#print axioms: 13 theorems, no axioms.

79.97 The explicit constant

For the model over \(\mathbb Z\) (\(q_n=3\cdot 8^n\), \(7\sum _{n<m}8^n+1=8^m\)): \(E_w(a_t)\le E_w(a_0)+t\,h\,8^A\cdot 4E_0\cdot A\) for every truncation and every time.

ns/DyadicModelB.lean

/-
  ns/DyadicModelB.lean — T37 cycle 699 (goal G412): the constant of DyadicModelZ.model_bound made explicit.

  Lean 4 core plus ns/DyadicModelZ. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ, q_n = 3·8ⁿ and
    geom           7·Σ_{n<m} 8ⁿ + 1 = 8ᵐ
    head_le        B = Σ_{n<N} [n < m] q_n X ≤ 8ᵐ·X  for every N (X = 4E₀·A ≥ 0, m = A)
    model_explicit E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·(8^A·(4E₀·A))   for integer CN data with 4E₀ ≤ A²,
  every truncation N and every time t.
-/
import ns.DyadicModelZ

namespace DyadicModelB
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ

def P8 (n : Nat) : Int := pw intR 8 n

theorem p8_eq : ∀ n, P4 n * P2 n = P8 n
  | 0 => rfl
  | n + 1 => by
    show intR.mul (P4 n) 4 * intR.mul (P2 n) 2 = intR.mul (P8 n) 8
    rw [← p8_eq n]
    show P4 n * 4 * (P2 n * 2) = P4 n * P2 n * 8
    lib_int

theorem nn_p8 (n : Nat) : NN (P8 n) := by
  rw [← p8_eq]; exact intCone.nn_mul _ _ (nn_p4 n) (nn_p2 n)

def S (m : Nat) : Int := sumN intR P8 m

theorem geom : ∀ m, 7 * S m + 1 = P8 m
  | 0 => rfl
  | m + 1 => by
    show 7 * (S m + P8 m) + 1 = P8 m * 8
    rw [← geom m]
    lib_int

theorem nn_S (m : Nat) : NN (S m) := sumN_nn intR intCone P8 nn_p8 m

theorem three_le (m : Nat) : Le intR intCone (3 * S m) (P8 m) :=
  ⟨4 * S m + 1, intCone.nn_add _ _ (intCone.nn_mul _ _ ⟨4, rfl⟩ (nn_S m)) ⟨1, rfl⟩,
    by show 3 * S m + (4 * S m + 1) = P8 m; rw [← geom m]; lib_int⟩

theorem sumN_ext_lt (f g : Nat → Int) : ∀ N, (∀ n, n < N → f n = g n) → sumN intR f N = sumN intR g N
  | 0, _ => rfl
  | N + 1, h => by
    show intR.add (sumN intR f N) (f N) = intR.add (sumN intR g N) (g N)
    rw [sumN_ext_lt f g N (fun n hn => h n (Nat.lt_succ_of_lt hn)), h N (Nat.lt_succ_self N)]

theorem mono (f : Nat → Int) (hf : ∀ n, NN (f n)) (N : Nat) :
    ∀ j, Le intR intCone (sumN intR f N) (sumN intR f (N + j))
  | 0 => le_refl' intR intCone _
  | j + 1 => le_trans' intR intCone _ _ _ (mono f hf N j) (le_of_nn_add intR intCone _ _ (hf (N + j)))

theorem head_le (E A : Int) (hE : NN E) (hA : NN A) (m N : Nat) :
    Le intR intCone (headB intR w c E A m N) ((E * A) * P8 m) := by
  let f : Nat → Int := fun n => if n < m then intR.mul (qq intR w c n) (intR.mul E A) else intR.zero
  have hX : NN (E * A) := intCone.nn_mul _ _ hE hA
  have hf : ∀ n, NN (f n) := by
    intro n
    cases Nat.decLt n m with
    | isTrue hl =>
      show NN (if n < m then _ else _); rw [ite_eq_left hl]
      exact intCone.nn_mul _ _ (nn_qq n) hX
    | isFalse hg =>
      show NN (if n < m then _ else _); rw [ite_eq_right hg]
      exact ⟨0, rfl⟩
  -- Σ_{n<N} f ≤ Σ_{n<m} f
  have s1 : Le intR intCone (sumN intR f N) (sumN intR f m) := by
    cases Nat.decLe N m with
    | isTrue hl =>
      cases le_dest hl with
      | intro j hj => rw [← hj]; exact mono f hf N j
    | isFalse hg =>
      cases le_dest (Nat.le_of_lt (Nat.lt_of_not_le hg)) with
      | intro j hj =>
        rw [← hj, head_stable intR (fun n => intR.mul (qq intR w c n) (intR.mul E A)) m j]
        exact le_refl' intR intCone _
  -- Σ_{n<m} f = (3·(E·A))·S m
  have s2 : sumN intR f m = intR.mul (3 * (E * A)) (S m) := by
    rw [sumN_ext_lt f (fun n => intR.mul (3 * (E * A)) (P8 n)) m (fun n hn => by
      show (if n < m then _ else _) = _
      rw [ite_eq_left hn, qq_eq, ← p8_eq]
      show 3 * (P4 n * P2 n) * (E * A) = 3 * (E * A) * (P4 n * P2 n)
      lib_int)]
    exact sumN_mul_left intR (3 * (E * A)) P8 m
  have s3 : Le intR intCone (intR.mul (3 * (E * A)) (S m)) ((E * A) * P8 m) := by
    have := le_mul_nn intR intCone (E * A) _ _ hX (three_le m)
    rw [show intR.mul (3 * (E * A)) (S m) = intR.mul (E * A) (3 * S m) by
      show 3 * (E * A) * S m = E * A * (3 * S m); lib_int]
    exact this
  rw [s2] at s1
  exact le_trans' intR intCone _ _ _ s1 s3

theorem model_explicit (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N)
        (sumN intR (fun _ => intR.mul h ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)) t)) := by
  intro t
  have hE : NN (four intR (E0 intR a N)) := by
    have e0 : NN (E0 intR a N) := sq_sum_nonneg intR intCone (fun n => a 0 (n + 1)) N
    exact intCone.nn_add _ _ (intCone.nn_add _ _ e0 e0) (intCone.nn_add _ _ e0 e0)
  have hb := model_bound a h m N hh h0 hz step hA2 t
  have hs := sum_le intR intCone _ _ (fun _ => le_mul_nn intR intCone h _ _ hh
    (head_le (four intR (E0 intR a N)) (Int.ofNat m) hE ⟨m, rfl⟩ m N)) t
  exact le_trans' intR intCone _ _ _ hb (le_add' intR intCone _ _ _ _ (le_refl' intR intCone _) hs)

end DyadicModelB

#print axioms DyadicModelB.p8_eq
#print axioms DyadicModelB.nn_p8
#print axioms DyadicModelB.geom
#print axioms DyadicModelB.nn_S
#print axioms DyadicModelB.three_le
#print axioms DyadicModelB.sumN_ext_lt
#print axioms DyadicModelB.mono
#print axioms DyadicModelB.head_le
#print axioms DyadicModelB.model_explicit

#print axioms: 9 theorems, no axioms.

79.98 Model bound on a time horizon

The bound of the explicit model in closed form, one bound for all \(t\le T\). \(E_w(a_t)\le E_w(a_0)+T\cdot h\cdot 8^A\cdot 4E_0\cdot A\) for \(t\le T\) and every truncation.

ns/DyadicModelT.lean

/-
  ns/DyadicModelT.lean — T37 cycle 703 (goal G416): the explicit bound of DyadicModelB as a closed form
  and on a time horizon.

  Lean 4 core plus ns/DyadicModelB. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ with X = 4E₀·A·8^A,
    sumN_const     Σ_{j<t} Y = t·Y
    model_linear   E_w(a_t) ≤ E_w(a_0) + t·(h·X)
    model_horizon  t ≤ T  →  E_w(a_t) ≤ E_w(a_0) + T·(h·X)   (one bound for every t ≤ T and every truncation N).
-/
import ns.DyadicModelB

namespace DyadicModelT
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB

theorem sumN_const (Y : Int) : ∀ t, sumN intR (fun _ => Y) t = Int.ofNat t * Y
  | 0 => by show (0 : Int) = 0 * Y; lib_int
  | t + 1 => by
    show intR.add (sumN intR (fun _ => Y) t) Y = (Int.ofNat t + 1) * Y
    rw [sumN_const Y t]; show Int.ofNat t * Y + Y = (Int.ofNat t + 1) * Y; lib_int

theorem nat_mul_le (Y : Int) (hY : NN Y) (t T : Nat) (h : t ≤ T) :
    Le intR intCone (Int.ofNat t * Y) (Int.ofNat T * Y) := by
  cases le_dest h with
  | intro d hd =>
    refine ⟨Int.ofNat d * Y, intCone.nn_mul _ _ ⟨d, rfl⟩ hY, ?_⟩
    rw [← hd]
    show Int.ofNat t * Y + Int.ofNat d * Y = (Int.ofNat t + Int.ofNat d) * Y
    lib_int

theorem nn_X (a : Nat → Nat → Int) (m N : Nat) :
    NN ((four intR (E0 intR a N) * Int.ofNat m) * P8 m) := by
  have e0 : NN (E0 intR a N) := sq_sum_nonneg intR intCone (fun n => a 0 (n + 1)) N
  have hE : NN (four intR (E0 intR a N)) :=
    intCone.nn_add _ _ (intCone.nn_add _ _ e0 e0) (intCone.nn_add _ _ e0 e0)
  exact intCone.nn_mul _ _ (intCone.nn_mul _ _ hE ⟨m, rfl⟩) (nn_p8 m)

theorem model_linear (a : Nat → Nat → Int) (h : Int) (m N : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (Int.ofNat t * (h * ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)))) := by
  intro t
  have hb := model_explicit a h m N hh h0 hz step hA2 t
  rw [sumN_const] at hb
  exact hb

theorem model_horizon (a : Nat → Nat → Int) (h : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le intR intCone (four intR (E0 intR a N)) (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, t ≤ T → Le intR intCone (Ew intR w (a t) N)
      (intR.add (Ew intR w (a 0) N) (Int.ofNat T * (h * ((four intR (E0 intR a N) * Int.ofNat m) * P8 m)))) := by
  intro t ht
  exact le_trans' intR intCone _ _ _ (model_linear a h m N hh h0 hz step hA2 t)
    (le_add' intR intCone _ _ _ _ (le_refl' intR intCone _)
      (nat_mul_le _ (intCone.nn_mul _ _ hh (nn_X a m N)) t T ht))

end DyadicModelT

#print axioms DyadicModelT.sumN_const
#print axioms DyadicModelT.nat_mul_le
#print axioms DyadicModelT.nn_X
#print axioms DyadicModelT.model_linear
#print axioms DyadicModelT.model_horizon

#print axioms: 5 theorems, no axioms.

79.99 Model bound uniform in the truncation

The horizon bound with constants independent of the truncation \(N\). \(E_w(a_0)\le W,\ 4E_0\le F\le A^2\Rightarrow E_w(a_t)\le W+T\,h\,F\,A\,8^A\) for \(t\le T\).

ns/DyadicModelU.lean

/-
  ns/DyadicModelU.lean — T37 cycle 706 (goal G419): the horizon bound of DyadicModelT uniform in the truncation.

  Lean 4 core plus ns/DyadicModelT. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    le_mul_r       x ≤ y, c ≥ 0  ⇒  x·c ≤ y·c
    model_uniform  E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A²  ⇒  E_w(a_t) ≤ W + T·h·F·A·8^A   for every t ≤ T,
  with W, F, A, T independent of the truncation N: the a priori bound needed to pass N → ∞.
-/
import ns.DyadicModelT

namespace DyadicModelU
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT

theorem le_mul_r (c x y : Int) (hc : NN c) (h : Le intR intCone x y) :
    Le intR intCone (x * c) (y * c) := by
  cases h with
  | intro d h =>
    refine ⟨d * c, intCone.nn_mul _ _ h.1 hc, ?_⟩
    rw [← h.2]
    show x * c + d * c = (x + d) * c
    lib_int

theorem model_uniform (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m))) :
    ∀ t, t ≤ T → Le intR intCone (Ew intR w (a t) N)
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) := by
  intro t ht
  have hA2 := le_trans' intR intCone _ _ _ hF hFA
  have hb := model_horizon a h m N T hh h0 hz step hA2 t ht
  have hT : NN (Int.ofNat T) := ⟨T, rfl⟩
  have hX : Le intR intCone ((four intR (E0 intR a N) * Int.ofNat m) * P8 m) ((F * Int.ofNat m) * P8 m) :=
    le_mul_r _ _ _ (nn_p8 m) (le_mul_r _ _ _ ⟨m, rfl⟩ hF)
  have hY := le_mul_nn intR intCone (Int.ofNat T) _ _ hT (le_mul_nn intR intCone h _ _ hh hX)
  exact le_trans' intR intCone _ _ _ hb (le_add' intR intCone _ _ _ _ hW hY)

end DyadicModelU

#print axioms DyadicModelU.le_mul_r
#print axioms DyadicModelU.model_uniform

#print axioms: 2 theorems, no axioms.

79.100 Mode decay in the model

One constant bounds every shell, every time up to \(T\) and every truncation. \(4^{n+1}a_t(n+1)^2\le W+T\,h\,F\,A\,8^A\) for \(n<N\), \(t\le T\).

ns/DyadicModelMode.lean

/-
  ns/DyadicModelMode.lean — T37 cycle 709 (goal G422): mode-by-mode decay from the uniform bound of DyadicModelU.

  Lean 4 core plus ns/DyadicModelU. For any ordered ring with cone P:
    term_le     f ≥ 0 termwise, n < N  ⇒  f n ≤ Σ_{j<N} f j
  and for the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    mode_decay  E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A², n < N, t ≤ T  ⇒  4^{n+1}·a_t(n+1)² ≤ W + T·h·F·A·8^A,
  one constant for every mode, every time up to T and every truncation: the shell amplitudes decay like 2^{-n}.
-/
import ns.DyadicModelU

namespace DyadicModelMode
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU

section Ring
variable {R : Type} (K : CRing R)

theorem term_le (P : Cone K) (f : Nat → R) (hf : ∀ n, P.nn (f n)) (n : Nat) :
    ∀ N, n < N → Le K P (f n) (sumN K f N)
  | 0, h => absurd h (Nat.not_lt_zero n)
  | N + 1, h => by
    cases Nat.lt_or_ge n N with
    | inl l =>
      exact le_trans' K P _ _ _ (term_le P f hf n N l) (le_of_nn_add K P _ _ (hf N))
    | inr g =>
      have e : n = N := Nat.le_antisymm (Nat.le_of_lt_succ h) g
      rw [e]
      exact le_zero_add K P _ _ (sumN_nn K P f hf N)

end Ring

theorem mode_decay (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T) :
    Le intR intCone (intR.mul (w (n + 1)) (intR.mul (a t (n + 1)) (a t (n + 1))))
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) :=
  le_trans' intR intCone _ _ _
    (term_le intR intCone (fun j => intR.mul (w (j + 1)) (intR.mul (a t (j + 1)) (a t (j + 1))))
      (fun j => intCone.nn_mul _ _ (nn_p4 (j + 1)) (nn_sq' _)) n N hn)
    (model_uniform a h W F m N T hh h0 hz step hW hF hFA t ht)

end DyadicModelMode

#print axioms DyadicModelMode.term_le
#print axioms DyadicModelMode.mode_decay

#print axioms: 2 theorems, no axioms.

79.101 Tightness in the model

The \(L^2\) energy beyond shell \(M\) is at most \(C/4^M\), uniformly in the truncation and in \(t\le T\). \(4^M\sum _{M\le n\le N}a_t(n)^2\le W+T\,h\,F\,A\,8^A\).

ns/DyadicModelTail.lean

/-
  ns/DyadicModelTail.lean — T37 cycle 712 (goal G425): tightness — the L² energy beyond shell M is ≤ C/4^M,
  uniformly in the truncation and in t ≤ T.

  Lean 4 core plus ns/DyadicModelMode. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    pow_mono    M ≤ n ⇒ 2^M ≤ 2^n
    p4_mono     M ≤ n ⇒ 4^M ≤ 4^n                                    (in the cone order of ℤ)
    tail_le     4^M · Σ_{j<N, M ≤ j+1} a(j+1)² ≤ E_w(a)
    tail_decay  4^M · Σ_{j<N, M ≤ j+1} a_t(j+1)² ≤ W + T·h·F·A·8^A   (t ≤ T, every N)
  The uniform tail bound is the compactness (tightness) input for N → ∞.
-/
import ns.DyadicModelMode

namespace DyadicModelTail
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU DyadicModelMode

theorem pow_mono (M : Nat) : ∀ d, 2 ^ M ≤ 2 ^ (M + d)
  | 0 => Nat.le_refl _
  | d + 1 => by
    show 2 ^ M ≤ 2 ^ (M + d) * 2
    have e : 2 ^ (M + d) * 2 = 2 ^ (M + d) + 2 ^ (M + d) := by lib_nat
    rw [e]
    exact Nat.le_trans (pow_mono M d) (Nat.le_add_right _ _)

theorem p2_mono (M n : Nat) (h : M ≤ n) : Le intR intCone (P2 M) (P2 n) := by
  cases le_dest h with
  | intro d hd =>
    rw [p2_nat, p2_nat, ← hd]
    exact ofNat_le (pow_mono M d)

theorem p4_mono (M n : Nat) (h : M ≤ n) : Le intR intCone (P4 M) (P4 n) := by
  rw [p4_sq, p4_sq]
  exact le_trans' intR intCone _ _ _ (le_mul_nn intR intCone (P2 M) _ _ (nn_p2 M) (p2_mono M n h))
    (le_mul_r _ _ _ (nn_p2 n) (p2_mono M n h))

def tl (M : Nat) (b : Nat → Int) (j : Nat) : Int :=
  match Nat.ble M (j + 1) with
  | true => intR.mul (b (j + 1)) (b (j + 1))
  | false => 0

theorem term (M : Nat) (b : Nat → Int) (j : Nat) :
    Le intR intCone (intR.mul (P4 M) (tl M b j)) (intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))) := by
  unfold tl
  cases e : Nat.ble M (j + 1) with
  | true =>
    have hm := Nat.le_of_ble_eq_true e
    show Le intR intCone (P4 M * intR.mul (b (j + 1)) (b (j + 1))) (P4 (j + 1) * intR.mul (b (j + 1)) (b (j + 1)))
    exact le_mul_r _ _ _ (nn_sq' _) (p4_mono M (j + 1) hm)
  | false =>
    have hx : NN (intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))) :=
      intCone.nn_mul _ _ (nn_p4 (j + 1)) (nn_sq' _)
    refine ⟨_, hx, ?_⟩
    show P4 M * 0 + intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))
      = intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1)))
    generalize intR.mul (w (j + 1)) (intR.mul (b (j + 1)) (b (j + 1))) = z
    lib_int

theorem tail_le (M N : Nat) (b : Nat → Int) :
    Le intR intCone (intR.mul (P4 M) (sumN intR (tl M b) N)) (Ew intR w b N) := by
  rw [← sumN_mul_left]
  exact sum_le intR intCone _ _ (term M b) N

theorem tail_decay (a : Nat → Nat → Int) (h W F : Int) (m N T M : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (t : Nat) (ht : t ≤ T) :
    Le intR intCone (intR.mul (P4 M) (sumN intR (tl M (a t)) N))
      (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m)))) :=
  le_trans' intR intCone _ _ _ (tail_le M N (a t)) (model_uniform a h W F m N T hh h0 hz step hW hF hFA t ht)

end DyadicModelTail

#print axioms DyadicModelTail.pow_mono
#print axioms DyadicModelTail.p2_mono
#print axioms DyadicModelTail.p4_mono
#print axioms DyadicModelTail.term
#print axioms DyadicModelTail.tail_le
#print axioms DyadicModelTail.tail_decay

#print axioms: 6 theorems, no axioms.

79.102 Finite support of the integer model

Integrality turns the uniform weighted bound into a cutoff: shells with \(4^n>C\) stay empty. \(C=W+T\,h\,F\,A\,8^A<4^{n+1},\ n<N,\ t\le T\Rightarrow a_t(n+1)=0\).

ns/DyadicModelSupp.lean

/-
  ns/DyadicModelSupp.lean — T37 cycle 715 (goal G428): in the integer model the solution lives on finitely many
  shells, the same for every time t ≤ T and every truncation N.

  Lean 4 core plus ns/DyadicModelMode. For the model w_n = k_n = 4ⁿ, c_n = 2ⁿ, ν = 3 over ℤ:
    one_le_sq   a ≠ 0  ⇒  1 ≤ a²
    no_le_pred  ¬ (C + 1 ≤ C)
    contra, zero_of   4ⁿ·a² ≤ C < 4ⁿ  ⇒  a = 0
    support     C = W + T·h·F·A·8^A, C < 4^{n+1}, n < N, t ≤ T  ⇒  a_t(n+1) = 0
  Integrality turns the uniform weighted bound into a uniform cutoff: no energy reaches shells with 4ⁿ > C.
-/
import ns.DyadicModelMode

namespace DyadicModelSupp
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData DyadicModelZ DyadicModelB DyadicModelT DyadicModelU DyadicModelMode

theorem one_le_sq : ∀ a : Int, a ≠ 0 → Le intR intCone 1 (a * a)
  | Int.ofNat 0, h => absurd rfl h
  | Int.ofNat (k + 1), _ => by
    refine ⟨Int.ofNat (k * (k + 1) + k), ⟨_, rfl⟩, ?_⟩
    show Int.ofNat (1 + (k * (k + 1) + k)) = Int.ofNat ((k + 1) * (k + 1))
    have e : 1 + (k * (k + 1) + k) = (k + 1) * (k + 1) := by lib_nat
    rw [e]
  | Int.negSucc k, _ => by
    refine ⟨Int.ofNat (k * (k + 1) + k), ⟨_, rfl⟩, ?_⟩
    show Int.ofNat (1 + (k * (k + 1) + k)) = Int.ofNat ((k + 1) * (k + 1))
    have e : 1 + (k * (k + 1) + k) = (k + 1) * (k + 1) := by lib_nat
    rw [e]

theorem no_le_pred (C : Int) : ¬ Le intR intCone (C + 1) C := by
  intro h
  cases h with
  | intro d hd =>
    cases hd.1 with
    | intro n hn =>
      have e : d = -1 := by
        have h2 : C + 1 + d = C := hd.2
        have h3 : d = (C + 1 + d) + -(C + 1) := by lib_int
        rw [h3, h2]; lib_int
      rw [e] at hn
      exact Int.noConfusion hn

theorem contra (C x a : Int) (hx : NN x) (ha : a ≠ 0) (h1 : Le intR intCone (intR.mul x (intR.mul a a)) C)
    (h2 : Le intR intCone (C + 1) x) : False := by
  have l1 : Le intR intCone (intR.mul x 1) (intR.mul x (intR.mul a a)) :=
    le_mul_nn intR intCone x _ _ hx (one_le_sq a ha)
  have ex : intR.mul x 1 = x := by show x * 1 = x; lib_int
  rw [ex] at l1
  exact no_le_pred C (le_trans' intR intCone _ _ _ h2 (le_trans' intR intCone _ _ _ l1 h1))

theorem zero_of (C x : Int) (hx : NN x) : ∀ a : Int, Le intR intCone (intR.mul x (intR.mul a a)) C →
    Le intR intCone (C + 1) x → a = 0
  | Int.ofNat 0, _, _ => rfl
  | Int.ofNat (k + 1), h1, h2 => (contra C x _ hx (fun e => Nat.noConfusion (Int.ofNat.inj (e : Int.ofNat (k + 1) = Int.ofNat 0))) h1 h2).elim
  | Int.negSucc _, h1, h2 => (contra C x _ hx (fun e => Int.noConfusion e) h1 h2).elim

theorem support (a : Nat → Nat → Int) (h W F : Int) (m N T : Nat) (hh : NN h)
    (h0 : ∀ t, a t 0 = 0) (hz : ∀ t n, N + 1 ≤ n → a t n = 0)
    (step : ∀ t n, intR.add (a (t + 1) (n + 1)) (intR.neg (a t (n + 1)))
      = intR.mul h (intR.add (intR.neg (intR.mul 3 (intR.mul (k (n + 1)) (intR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN intR c (fun i => intR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le intR intCone (Ew intR w (a 0) N) W)
    (hF : Le intR intCone (four intR (E0 intR a N)) F)
    (hFA : Le intR intCone F (intR.mul (Int.ofNat m) (Int.ofNat m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T)
    (hc : Le intR intCone (intR.add W (Int.ofNat T * (h * ((F * Int.ofNat m) * P8 m))) + 1) (P4 (n + 1))) :
    a t (n + 1) = 0 :=
  zero_of _ _ (nn_p4 (n + 1)) _ (mode_decay a h W F m N T hh h0 hz step hW hF hFA n t hn ht) hc

end DyadicModelSupp

#print axioms DyadicModelSupp.one_le_sq
#print axioms DyadicModelSupp.no_le_pred
#print axioms DyadicModelSupp.contra
#print axioms DyadicModelSupp.zero_of
#print axioms DyadicModelSupp.support

#print axioms: 5 theorems, no axioms.

79.103 Integer schemes drop energy finitely often

An integer Crank–Nicolson Galerkin scheme loses energy at no more than \(E_0\) steps, on any horizon. \(\#\{t<T:E(t+1)\ne E(t)\}\le E(0)\).

ns/IntDrops.lean

/-
  ns/IntDrops.lean — T37 cycle 719 (goal G432): in an integer Crank–Nicolson Galerkin scheme the energy drops
  strictly at most E₀ times.

  Lean 4 core plus ns/IntOrd (intR, intCone), NSChain (cn_global), lib/IntRing (lib_int).
    succ_le_of_ne  x ≤ y, x ≠ y  ⇒  x + 1 ≤ y                  (in ℤ)
    drops_le       e(t+1) ≤ e(t) for all t  ⇒  #{t < T : e(t+1) ≠ e(t)} + e(T) ≤ e(0)
    step_mono      CN step with neutral nonlinearity  ⇒  E(t+1) ≤ E(t)   (cn_global on the shifted solution)
    int_drops      #{t < T : E(t+1) ≠ E(t)} ≤ E(0)            for every T
  For integer data the scheme is energy-conserving except at no more than E₀ steps, whatever the time horizon.
-/
import ns.IntOrd

namespace IntDrops
open DetChain NSChain DyadicSmallGlobal LibInt IntOrd

theorem succ_le_of_ne (x y : Int) (h : Le intR intCone x y) (hne : x ≠ y) : Le intR intCone (x + 1) y := by
  cases h with
  | intro d hd =>
    cases hd.1 with
    | intro n hn =>
      match n, hn with
      | 0, hn =>
        have e : x = y := by
          have h2 : x + d = y := hd.2
          rw [← h2, hn]; show x = x + 0; lib_int
        exact absurd e hne
      | n + 1, hn =>
        refine ⟨Int.ofNat n, ⟨n, rfl⟩, ?_⟩
        have h2 : x + d = y := hd.2
        rw [← h2, hn]
        show x + 1 + Int.ofNat n = x + (Int.ofNat n + 1)
        lib_int

def dr (e : Nat → Int) (t : Nat) : Int :=
  match Int.decEq (e (t + 1)) (e t) with
  | isTrue _ => 0
  | isFalse _ => 1

theorem dr_step (e : Nat → Int) (t : Nat) (h : Le intR intCone (e (t + 1)) (e t)) :
    Le intR intCone (dr e t + e (t + 1)) (e t) := by
  unfold dr
  cases Int.decEq (e (t + 1)) (e t) with
  | isTrue q =>
    show Le intR intCone (0 + e (t + 1)) (e t)
    have z : (0 : Int) + e (t + 1) = e (t + 1) := by lib_int
    rw [z]; exact h
  | isFalse q =>
    show Le intR intCone (1 + e (t + 1)) (e t)
    have z : (1 : Int) + e (t + 1) = e (t + 1) + 1 := by lib_int
    rw [z]; exact succ_le_of_ne _ _ h q

theorem drops_le (e : Nat → Int) (mono : ∀ t, Le intR intCone (e (t + 1)) (e t)) :
    ∀ T, Le intR intCone (sumN intR (dr e) T + e T) (e 0)
  | 0 => by
    show Le intR intCone (0 + e 0) (e 0)
    have z : (0 : Int) + e 0 = e 0 := by lib_int
    rw [z]; exact le_refl' intR intCone _
  | T + 1 => by
    have r : sumN intR (dr e) (T + 1) + e (T + 1) = sumN intR (dr e) T + (dr e T + e (T + 1)) := by
      show sumN intR (dr e) T + dr e T + e (T + 1) = sumN intR (dr e) T + (dr e T + e (T + 1)); lib_int
    rw [r]
    exact le_trans' intR intCone _ _ _
      (le_add2 (sumN intR (dr e) T) _ _ (dr_step e T (mono T))) (drops_le e mono T)
where
  le_add2 (s x y : Int) (h : Le intR intCone x y) : Le intR intCone (s + x) (s + y) := by
    cases h with
    | intro d hd => exact ⟨d, hd.1, by rw [← hd.2]; show s + x + d = s + (x + d); lib_int⟩

def En (a : Nat → Nat → Int) (N t : Nat) : Int := sumN intR (fun n => intR.mul (a t n) (a t n)) N

theorem step_mono (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (t : Nat) : Le intR intCone (En a N (t + 1)) (En a N t) :=
  (cn_global intR intCone (fun u => a (t + u)) k (fun u => Nn (t + u)) h nu N hh hnu hk
    (fun u n => step (t + u) n) (fun u => neutral (t + u))).1 1

theorem int_drops (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : NN h) (hnu : NN nu) (hk : ∀ n, NN (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (T : Nat) : Le intR intCone (sumN intR (dr (En a N)) T) (En a N 0) := by
  have d := drops_le (En a N) (step_mono a k Nn h nu N hh hnu hk step neutral) T
  have hE : NN (En a N T) := sq_sum_nonneg intR intCone (a T) N
  exact le_trans' intR intCone _ _ _ ⟨_, hE, rfl⟩ d

end IntDrops

#print axioms IntDrops.succ_le_of_ne
#print axioms IntDrops.dr_step
#print axioms IntDrops.drops_le
#print axioms IntDrops.step_mono
#print axioms IntDrops.int_drops

#print axioms: 5 theorems, no axioms.

79.104 Energy-conserving integer steps are sign flips

At a step of an integer Crank–Nicolson scheme with \(h,\nu ,k_n\ge 1\) and \(E(t+1)=E(t)\) every mode flips sign. \(E(t+1)=E(t)\Rightarrow a_{t+1}(n)=-a_t(n)\) for \(n<N\).

ns/IntFlip.lean

/-
  ns/IntFlip.lean — T37 cycle 722 (goal G435): at an energy-conserving step of an integer Crank–Nicolson Galerkin
  scheme (h, ν, k_n ≥ 1) every mode flips sign: a_{t+1}(n) = −a_t(n).

  Lean 4 core plus ns/DyadicModelSupp (one_le_sq, no_le_pred), ns/DyadicModelMode (term_le), ns/IntDrops,
  NSChain (cn_energy: E' + hν Σ k (a'+a)² = E, exact).
    nn_le_zero    x ≥ 0, x ≤ 0 ⇒ x = 0
    le_mul_ge1    1 ≤ h, X ≥ 0 ⇒ X ≤ h·X
    zero_of_mul   1 ≤ h, X ≥ 0, h·X = 0 ⇒ X = 0
    sq_zero       a·a = 0 ⇒ a = 0
    flip          E(t+1) = E(t) ⇒ a_{t+1}(n) = −a_t(n) for n < N
  With IntDrops: all but at most E₀ steps of an integer scheme are pure sign flips.
-/
import ns.DyadicModelSupp
import ns.IntDrops

namespace IntFlip
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp

theorem nn_le_zero (x : Int) (hx : NN x) (h : Le intR intCone x 0) : x = 0 := by
  cases hx with
  | intro p hp =>
    cases h with
    | intro d hd =>
      cases hd.1 with
      | intro q hq =>
        have e : Int.ofNat p + Int.ofNat q = 0 := by rw [← hp, ← hq]; exact hd.2
        have e2 : Int.ofNat (p + q) = Int.ofNat 0 := e
        have e3 : p + q = 0 := Int.ofNat.inj e2
        rw [hp, Nat.eq_zero_of_add_eq_zero_right e3]; rfl

theorem le_mul_ge1 (h X : Int) (h1 : Le intR intCone 1 h) (hX : NN X) : Le intR intCone X (h * X) := by
  cases h1 with
  | intro d hd =>
    refine ⟨d * X, intCone.nn_mul _ _ hd.1 hX, ?_⟩
    have e : (1 : Int) + d = h := hd.2
    rw [← e]; show X + d * X = (1 + d) * X; lib_int

theorem zero_of_mul (h X : Int) (h1 : Le intR intCone 1 h) (hX : NN X) (e : h * X = 0) : X = 0 :=
  nn_le_zero X hX (by have l := le_mul_ge1 h X h1 hX; rw [e] at l; exact l)

theorem sq_zero : ∀ a : Int, a * a = 0 → a = 0
  | Int.ofNat 0, _ => rfl
  | Int.ofNat (k + 1), e => by
    have l := one_le_sq (Int.ofNat (k + 1)) (fun q => Nat.noConfusion (Int.ofNat.inj (q : Int.ofNat (k + 1) = Int.ofNat 0)))
    rw [e] at l; exact absurd l (no_le_pred 0)
  | Int.negSucc k, e => by
    have l := one_le_sq (Int.negSucc k) (fun q => Int.noConfusion q)
    rw [e] at l; exact absurd l (no_le_pred 0)

theorem nn_of_ge1 (h : Int) (h1 : Le intR intCone 1 h) : NN h := by
  cases h1 with
  | intro d hd => rw [← hd.2]; exact intCone.nn_add _ _ ⟨1, rfl⟩ hd.1

theorem flip (a a' k Nn : Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ n, intR.add (a' n) (intR.neg (a n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN intR (fun n => intR.mul (intR.add (a' n) (a n)) (Nn n)) N = intR.zero)
    (heq : sumN intR (fun n => intR.mul (a' n) (a' n)) N = sumN intR (fun n => intR.mul (a n) (a n)) N) :
    ∀ n, n < N → a' n = -a n := by
  intro n hn
  have ce := cn_energy intR a a' k Nn h nu N step neutral
  rw [heq] at ce
  generalize sumN intR (fun n => intR.mul (a n) (a n)) N = E at ce
  let f : Nat → Int := fun n => intR.mul (k n) (intR.mul (intR.add (a' n) (a n)) (intR.add (a' n) (a n)))
  have hf : ∀ j, NN (f j) := fun j => intCone.nn_mul _ _ (nn_of_ge1 _ (hk j)) (nn_sq' _)
  have hS : NN (sumN intR f N) := sumN_nn intR intCone f hf N
  have hD : intR.mul h (intR.mul nu (sumN intR f N)) = 0 := by
    have c2 : E + intR.mul h (intR.mul nu (sumN intR f N)) = E := ce
    generalize intR.mul h (intR.mul nu (sumN intR f N)) = D at c2
    have : D = (E + D) + -E := by lib_int
    rw [this, c2]; lib_int
  have hS0 : sumN intR f N = 0 :=
    zero_of_mul nu _ hnu hS (zero_of_mul h _ hh (intCone.nn_mul _ _ (nn_of_ge1 _ hnu) hS) hD)
  have hfn : f n = 0 := by
    have l := term_le intR intCone f hf n N hn
    rw [hS0] at l
    exact nn_le_zero _ (hf n) l
  have hs2 : intR.mul (intR.add (a' n) (a n)) (intR.add (a' n) (a n)) = 0 :=
    zero_of_mul (k n) _ (hk n) (nn_sq' _) hfn
  have hs : a' n + a n = 0 := sq_zero _ hs2
  have : a' n = (a' n + a n) + -a n := by lib_int
  rw [this, hs]; lib_int

end IntFlip

#print axioms IntFlip.nn_le_zero
#print axioms IntFlip.le_mul_ge1
#print axioms IntFlip.zero_of_mul
#print axioms IntFlip.sq_zero
#print axioms IntFlip.nn_of_ge1
#print axioms IntFlip.flip

#print axioms: 6 theorems, no axioms.

79.105 Long sign-flip stretches of integer schemes

For every \(L\), one of the first \(E_0+1\) blocks of length \(L\) consists of pure sign-flip steps. \(E(0)=e\Rightarrow \exists j\le e\ \forall i<L,\ n<N:\ a_{Lj+i+1}(n)=-a_{Lj+i}(n)\).

ns/IntStretch.lean

/-
  ns/IntStretch.lean — T37 cycle 725 (goal G438): an integer Crank–Nicolson Galerkin scheme (h, ν, k_n ≥ 1) has,
  for every L, a stretch of L consecutive pure sign-flip steps that starts before time L·E₀ (exactly: in one of
  the first E₀ + 1 blocks of length L).

  Lean 4 core plus ns/IntFlip (flip, nn_le_zero, nn_of_ge1), ns/IntDrops (dr, int_drops, succ_le_of_ne).
    sumN_shift   Σ_{i<n+L} f = Σ_{i<n} f + Σ_{i<L} f(n+i)
    blocks       Σ_{i<L·q} f = Σ_{j<q} Σ_{i<L} f(L·j+i)
    search       B ≥ 0  ⇒  (∃ j < q, B j = 0) ∨ q ≤ Σ_{j<q} B j
    stretch      E(0) = e  ⇒  ∃ j ≤ e, ∀ i < L, ∀ n < N, a_{Lj+i+1}(n) = −a_{Lj+i}(n)
  So the integer dynamics is 2-periodic (a_{t+2} = a_t) on arbitrarily long windows: pigeonhole on the ≤ E₀ drops.
-/
import ns.IntFlip

namespace IntStretch
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp
  IntDrops IntFlip

theorem sumN_shift (f : Nat → Int) (n : Nat) : ∀ L,
    sumN intR f (n + L) = sumN intR f n + sumN intR (fun i => f (n + i)) L
  | 0 => by show sumN intR f n = sumN intR f n + 0; lib_int
  | L + 1 => by
    show sumN intR f (n + L) + f (n + L) = sumN intR f n + (sumN intR (fun i => f (n + i)) L + f (n + L))
    rw [sumN_shift f n L]; lib_int

theorem blocks (f : Nat → Int) (L : Nat) : ∀ q,
    sumN intR f (L * q) = sumN intR (fun j => sumN intR (fun i => f (L * j + i)) L) q
  | 0 => rfl
  | q + 1 => by
    show sumN intR f (L * q + L) = sumN intR (fun j => sumN intR (fun i => f (L * j + i)) L) q
      + sumN intR (fun i => f (L * q + i)) L
    rw [sumN_shift, blocks f L q]

theorem search (B : Nat → Int) (hB : ∀ j, NN (B j)) : ∀ q,
    (∃ j, j < q ∧ B j = 0) ∨ Le intR intCone (Int.ofNat q) (sumN intR B q)
  | 0 => Or.inr (le_refl' intR intCone _)
  | q + 1 => by
    cases search B hB q with
    | inl l =>
      cases l with
      | intro j hj => exact Or.inl ⟨j, Nat.lt_succ_of_lt hj.1, hj.2⟩
    | inr r =>
      cases Int.decEq (B q) 0 with
      | isTrue z => exact Or.inl ⟨q, Nat.lt_succ_self q, z⟩
      | isFalse z =>
        have h0 : Le intR intCone 0 (B q) := ⟨B q, hB q, by show 0 + B q = B q; lib_int⟩
        have h1 := succ_le_of_ne 0 (B q) h0 (fun e => z e.symm)
        have h1' : Le intR intCone 1 (B q) := by
          have e : (0 : Int) + 1 = 1 := rfl
          rw [e] at h1; exact h1
        exact Or.inr (le_add' intR intCone _ _ _ _ r h1')

theorem dr_nn (e : Nat → Int) (t : Nat) : NN (dr e t) := by
  unfold dr
  cases Int.decEq (e (t + 1)) (e t) with
  | isTrue _ => exact ⟨0, rfl⟩
  | isFalse _ => exact ⟨1, rfl⟩

theorem eq_of_dr (e : Nat → Int) (t : Nat) (h : dr e t = 0) : e (t + 1) = e t := by
  unfold dr at h
  cases q : Int.decEq (e (t + 1)) (e t) with
  | isTrue p => exact p
  | isFalse _ =>
    rw [q] at h
    exact Int.noConfusion h (fun e' => Nat.noConfusion e')

theorem stretch (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (e : Nat) (hE : IntDrops.En a N 0 = Int.ofNat e) (L : Nat) :
    ∃ j, j < e + 1 ∧ ∀ i, i < L → ∀ n, n < N → a (L * j + i + 1) n = -a (L * j + i) n := by
  have d := int_drops a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n))
    step neutral (L * (e + 1))
  rw [blocks, hE] at d
  let B : Nat → Int := fun j => sumN intR (fun i => dr (IntDrops.En a N) (L * j + i)) L
  have hB : ∀ j, NN (B j) := fun j => sumN_nn intR intCone _ (fun i => dr_nn _ _) L
  cases search B hB (e + 1) with
  | inr r => exact absurd (le_trans' intR intCone _ _ _ r d) (no_le_pred (Int.ofNat e))
  | inl l =>
    cases l with
    | intro j hj =>
      refine ⟨j, hj.1, ?_⟩
      intro i hi n hn
      have hz : dr (IntDrops.En a N) (L * j + i) = 0 := by
        have l1 := term_le intR intCone (fun i => dr (IntDrops.En a N) (L * j + i)) (fun i => dr_nn _ _) i L hi
        have hB0 : B j = 0 := hj.2
        have l2 : Le intR intCone (dr (IntDrops.En a N) (L * j + i)) 0 := by
          have : sumN intR (fun i => dr (IntDrops.En a N) (L * j + i)) L = 0 := hB0
          rw [this] at l1; exact l1
        exact nn_le_zero _ (dr_nn _ _) l2
      have heq := eq_of_dr _ _ hz
      exact flip (a (L * j + i)) (a (L * j + i + 1)) k (Nn (L * j + i)) h nu N hh hnu hk
        (step (L * j + i)) (neutral (L * j + i)) heq n hn

end IntStretch

#print axioms IntStretch.sumN_shift
#print axioms IntStretch.blocks
#print axioms IntStretch.search
#print axioms IntStretch.dr_nn
#print axioms IntStretch.eq_of_dr
#print axioms IntStretch.stretch

#print axioms: 6 theorems, no axioms.

79.106 Extinction of integer schemes

An integer Crank–Nicolson scheme with \(h,\nu ,k_n\ge 1\) and a nonlinearity vanishing with the midpoint sum is zero after \(E_0\) steps. \(E(0)=e\Rightarrow E(e)=0\).

ns/IntVanish.lean

/-
  ns/IntVanish.lean — T37 cycle 728 (goal G441): an integer Crank–Nicolson Galerkin scheme (h, ν, k_n ≥ 1) whose
  nonlinearity vanishes with the midpoint sum (Nn = N(a_{t+1} + a_t), N(0) = 0, as for the dyadic model) is
  identically zero after at most E₀ steps.

  Lean 4 core plus ns/IntStretch, ns/DyadicModelT (sumN_const), ns/DyadicModelB (sumN_ext_lt).
    dbl_zero     x + x = 0 ⇒ x = 0                         (ℤ, by constructors)
    vanish       E(t+1) = E(t) ⇒ a_t(n) = 0 for n < N  and E(t) = 0
    e_mono       E(t + d) ≤ E(t)
    count        (∃ t < T, E t = 0) ∨ T ≤ #drops before T
    extinct      E(0) = e ⇒ E(e) = 0, so a_e(n) = 0 for all n < N
-/
import ns.IntStretch
import ns.DyadicModelT

namespace IntVanish
open DetChain NSChain DyadicSmallGlobal LibInt LibRing IntOrd DyadicLarge DyadicModelMode DyadicModelSupp
  IntDrops IntFlip IntStretch DyadicModelB DyadicModelT

theorem dbl_zero : ∀ x : Int, x + x = 0 → x = 0
  | Int.ofNat p, h => by
    have h' : Int.ofNat (p + p) = Int.ofNat 0 := h
    have e := Int.ofNat.inj h'
    rw [Nat.eq_zero_of_add_eq_zero_left e]; rfl
  | Int.negSucc p, h => by
    have h' : Int.negSucc (Nat.succ (p + p)) = 0 := h
    exact Int.noConfusion h'

theorem sum_zero (f : Nat → Int) (N : Nat) (h : ∀ n, n < N → f n = 0) : sumN intR f N = 0 := by
  rw [sumN_ext_lt f (fun _ => 0) N h, sumN_const]; show Int.ofNat N * 0 = 0; lib_int

theorem vanish (a a' k Nn : Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ n, intR.add (a' n) (intR.neg (a n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN intR (fun n => intR.mul (intR.add (a' n) (a n)) (Nn n)) N = intR.zero)
    (hN : (∀ n, n < N → a' n + a n = 0) → ∀ n, n < N → Nn n = 0)
    (heq : sumN intR (fun n => intR.mul (a' n) (a' n)) N = sumN intR (fun n => intR.mul (a n) (a n)) N) :
    (∀ n, n < N → a n = 0) ∧ sumN intR (fun n => intR.mul (a n) (a n)) N = 0 := by
  have fl := flip a a' k Nn h nu N hh hnu hk step neutral heq
  have hs : ∀ n, n < N → a' n + a n = 0 := fun n hn => by rw [fl n hn]; lib_int
  have hz := hN hs
  have ha : ∀ n, n < N → a n = 0 := by
    intro n hn
    have st := step n
    have s0 : a' n + a n = 0 := hs n hn
    have z0 : Nn n = 0 := hz n hn
    have st' : a' n + -a n = h * (-(nu * (k n * (a' n + a n))) + Nn n) := st
    rw [s0, z0] at st'
    have st2 : a' n + -a n = 0 := by rw [st']; lib_int
    have f0 : a' n = -a n := fl n hn
    rw [f0] at st2
    have : a n + a n = -(-a n + -a n) := by lib_int
    exact dbl_zero _ (by rw [this, st2]; rfl)
  exact ⟨ha, sum_zero _ N (fun n hn => by show a n * a n = 0; rw [ha n hn]; rfl)⟩

theorem e_mono (E : Nat → Int) (mono : ∀ t, Le intR intCone (E (t + 1)) (E t)) (t : Nat) :
    ∀ d, Le intR intCone (E (t + d)) (E t)
  | 0 => le_refl' intR intCone _
  | d + 1 => le_trans' intR intCone _ _ _ (mono (t + d)) (e_mono E mono t d)

theorem dr_one (E : Nat → Int) (t : Nat) (h : E (t + 1) ≠ E t) : dr E t = 1 := by
  unfold dr
  cases Int.decEq (E (t + 1)) (E t) with
  | isTrue p => exact absurd p h
  | isFalse _ => rfl

theorem count (E : Nat → Int) (hv : ∀ t, E (t + 1) = E t → E t = 0) : ∀ T,
    (∃ t, t < T ∧ E t = 0) ∨ Le intR intCone (Int.ofNat T) (sumN intR (dr E) T)
  | 0 => Or.inr (le_refl' intR intCone _)
  | T + 1 => by
    cases count E hv T with
    | inl l => cases l with
      | intro t ht => exact Or.inl ⟨t, Nat.lt_succ_of_lt ht.1, ht.2⟩
    | inr r =>
      cases Int.decEq (E T) 0 with
      | isTrue z => exact Or.inl ⟨T, Nat.lt_succ_self T, z⟩
      | isFalse z =>
        have d1 : dr E T = 1 := dr_one E T (fun q => z (hv T q))
        refine Or.inr ?_
        show Le intR intCone (intR.add (Int.ofNat T) 1) (intR.add (sumN intR (dr E) T) (dr E T))
        rw [d1]
        exact le_add' intR intCone _ _ _ _ r (le_refl' intR intCone 1)

theorem extinct (a : Nat → Nat → Int) (k : Nat → Int) (Nn : Nat → Nat → Int) (h nu : Int) (N : Nat)
    (hh : Le intR intCone 1 h) (hnu : Le intR intCone 1 nu) (hk : ∀ n, Le intR intCone 1 (k n))
    (step : ∀ t n, intR.add (a (t + 1) n) (intR.neg (a t n))
      = intR.mul h (intR.add (intR.neg (intR.mul nu (intR.mul (k n) (intR.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN intR (fun n => intR.mul (intR.add (a (t + 1) n) (a t n)) (Nn t n)) N = intR.zero)
    (hN : ∀ t, (∀ n, n < N → a (t + 1) n + a t n = 0) → ∀ n, n < N → Nn t n = 0)
    (e : Nat) (hE : IntDrops.En a N 0 = Int.ofNat e) :
    IntDrops.En a N e = 0 := by
  have hv : ∀ t, IntDrops.En a N (t + 1) = IntDrops.En a N t → IntDrops.En a N t = 0 := fun t q =>
    (vanish (a t) (a (t + 1)) k (Nn t) h nu N hh hnu hk (step t) (neutral t) (hN t) q).2
  have mono := step_mono a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n)) step neutral
  have dd := int_drops a k Nn h nu N (nn_of_ge1 _ hh) (nn_of_ge1 _ hnu) (fun n => nn_of_ge1 _ (hk n))
    step neutral (e + 1)
  rw [hE] at dd
  cases count (IntDrops.En a N) hv (e + 1) with
  | inr r => exact absurd (le_trans' intR intCone _ _ _ r dd) (no_le_pred (Int.ofNat e))
  | inl l =>
    cases l with
    | intro t ht =>
      have ⟨d, hd⟩ := DyadicModelZ.le_dest (Nat.le_of_lt_succ ht.1)
      have l2 := e_mono _ mono t d
      rw [hd, ht.2] at l2
      exact nn_le_zero _ (sq_sum_nonneg intR intCone (a e) N) l2

end IntVanish

#print axioms IntVanish.dbl_zero
#print axioms IntVanish.sum_zero
#print axioms IntVanish.vanish
#print axioms IntVanish.e_mono
#print axioms IntVanish.dr_one
#print axioms IntVanish.count
#print axioms IntVanish.extinct

#print axioms: 7 theorems, no axioms.

79.107 The concrete model over any ordered ring and over \(\mathbb Q\)

The large-data bound for \(w=k=4^n\), \(c=2^n\), \(\nu =3\) over any ordered ring, in particular \(\mathbb Q\). \(E_w(a_t)\le E_w(a_0)+\sum _{j<t}h\,B\) for rational data and time step with \(4E_0\le A^2\).

ns/RatModel.lean

/-
  ns/RatModel.lean — T37 cycle 731 (goal G444): the concrete supercritical dyadic model over any ordered ring, in
  particular over ℚ (lib/RatField from lib_shared), so the large-data bound applies to non-integer data and time
  steps (the integer schemes are degenerate: ns/IntVanish).

  Lean 4 core plus ns/DyadicLargeData (cn_large_data), lib/RatField (ratR, ratPos, rat_total, rat_antisymm,
  rat_noZD), lib/RingTac (lib_ring).
    ratOrd        ℚ as an OrdR (cone of nonnegative numerators, total, antisymmetric, no zero divisors)
    crit_g        q_n M_n = ν w_n k_n for w = k = 4ⁿ, c = M = 2ⁿ, ν = 3 (any ring)
    cast_le_p2    n ≤ 2ⁿ as ring elements; cast_mono  m ≤ n ⇒ m ≤ n
    model_gen     E_w(a_t) ≤ E_w(a_0) + Σ_{j<t} h·B for data with 4E₀ ≤ A², A = m·1 (any OrdR)
    model_rat     the same over ℚ
-/
import ns.DyadicLargeData
import lib.RatField
import ns.DyadicModelZ

namespace RatModel
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData

section Gen
variable {R : Type} (K : CRing R)

def P2 (n : Nat) : R := pw K (castN K 2) n
def P4 (n : Nat) : R := pw K (castN K 4) n

theorem p4_sq : ∀ n, P4 K n = K.mul (P2 K n) (P2 K n)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | n + 1 => by
    show K.mul (P4 K n) (castN K 4) = K.mul (K.mul (P2 K n) (castN K 2)) (K.mul (P2 K n) (castN K 2))
    rw [p4_sq n]
    show K.mul (K.mul (P2 K n) (P2 K n)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))
      = K.mul (K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))) (K.mul (P2 K n) (K.add K.one (K.add K.one K.zero)))
    lib_ring

theorem crit_g (n : Nat) :
    K.mul (qq K (P4 K) (P2 K) n) (P2 K n) = K.mul (castN K 3) (K.mul (P4 K n) (P4 K n)) := by
  unfold qq
  show K.mul (K.mul (K.add (K.mul (P4 K n) (castN K 4)) (K.neg (P4 K n))) (P2 K n)) (P2 K n)
    = K.mul (castN K 3) (K.mul (P4 K n) (P4 K n))
  rw [p4_sq K n]
  show K.mul (K.mul (K.add (K.mul (K.mul (P2 K n) (P2 K n)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))))
      (K.neg (K.mul (P2 K n) (P2 K n)))) (P2 K n)) (P2 K n)
    = K.mul (K.add K.one (K.add K.one (K.add K.one K.zero))) (K.mul (K.mul (P2 K n) (P2 K n)) (K.mul (P2 K n) (P2 K n)))
  lib_ring

variable (P : Cone K)

theorem nn_one' : P.nn K.one := by
  have := P.nn_sq K.one
  rw [show K.mul K.one K.one = K.one by lib_ring] at this; exact this

theorem nn_cast : ∀ n, P.nn (castN K n)
  | 0 => P.nn_zero
  | n + 1 => P.nn_add _ _ (nn_one' K P) (nn_cast n)

theorem nn_pw (x : R) (hx : P.nn x) : ∀ n, P.nn (pw K x n)
  | 0 => nn_one' K P
  | n + 1 => P.nn_mul _ _ (nn_pw x hx n) hx

theorem cast_add (m : Nat) : ∀ d, castN K (m + d) = K.add (castN K m) (castN K d)
  | 0 => by show castN K m = K.add (castN K m) K.zero; lib_ring
  | d + 1 => by
    show K.add K.one (castN K (m + d)) = K.add (castN K m) (K.add K.one (castN K d))
    rw [cast_add m d]; lib_ring

theorem cast_mono (m n : Nat) (h : m ≤ n) : Le K P (castN K m) (castN K n) := by
  have ⟨d, hd⟩ := DyadicModelZ.le_dest h
  exact ⟨castN K d, nn_cast K P d, by rw [← hd, cast_add]⟩

theorem one_le_p2 : ∀ n, Le K P K.one (P2 K n)
  | 0 => le_refl' K P _
  | n + 1 => by
    have ⟨d, hd⟩ := one_le_p2 n
    refine ⟨K.add (K.add d d) K.one, P.nn_add _ _ (P.nn_add _ _ hd.1 hd.1) (nn_one' K P), ?_⟩
    show K.add K.one (K.add (K.add d d) K.one) = K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))
    rw [← hd.2]; lib_ring

theorem cast_le_p2 : ∀ n, Le K P (castN K n) (P2 K n)
  | 0 => ⟨K.one, nn_one' K P, by show K.add K.zero K.one = K.one; lib_ring⟩
  | n + 1 => by
    have ⟨d, hd⟩ := cast_le_p2 n
    have ⟨e, he⟩ := one_le_p2 K P n
    refine ⟨K.add d e, P.nn_add _ _ hd.1 he.1, ?_⟩
    show K.add (K.add K.one (castN K n)) (K.add d e) = K.mul (P2 K n) (K.add K.one (K.add K.one K.zero))
    have r : K.mul (P2 K n) (K.add K.one (K.add K.one K.zero)) = K.add (P2 K n) (P2 K n) := by lib_ring
    have r2 : K.add (P2 K n) (P2 K n) = K.add (K.add (castN K n) d) (K.add K.one e) := by rw [hd.2, he.2]
    rw [r, r2]; lib_ring

theorem nn_qq_g (n : Nat) : P.nn (qq K (P4 K) (P2 K) n) := by
  unfold qq
  have e : K.mul (K.add (P4 K (n + 1)) (K.neg (P4 K n))) (P2 K n)
      = K.mul (castN K 3) (K.mul (P4 K n) (P2 K n)) := by
    show K.mul (K.add (K.mul (P4 K n) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))) (K.neg (P4 K n))) (P2 K n)
      = K.mul (K.add K.one (K.add K.one (K.add K.one K.zero))) (K.mul (P4 K n) (P2 K n))
    lib_ring
  rw [e]
  exact P.nn_mul _ _ (nn_cast K P 3) (P.nn_mul _ _ (nn_pw K P _ (nn_cast K P 4) n) (nn_pw K P _ (nn_cast K P 2) n))

theorem model_gen (O : OrdR K) (a : Nat → Nat → R) (h : R) (m N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le K O.toCone (four K (E0 K a N)) (K.mul (castN K m) (castN K m))) :
    ∀ t, Le K O.toCone (Ew K (P4 K) (a t) N)
      (K.add (Ew K (P4 K) (a 0) N)
        (sumN K (fun _ => K.mul h (headB K (P4 K) (P2 K) (four K (E0 K a N)) (castN K m) m N)) t)) :=
  cn_large_data K O (P4 K) (P2 K) (P4 K) (P2 K) a h (castN K 3) (castN K m) m N hh (nn_cast K O.toCone 3)
    (fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) n) (fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) n)
    h0 hz step (nn_qq_g K O.toCone) (fun n _ => crit_g K n) hA2 (nn_cast K O.toCone m)
    (fun n hn => le_trans' K O.toCone _ _ _ (cast_mono K O.toCone m n hn) (cast_le_p2 K O.toCone n))

end Gen

open LibRat in
def ratCone : Cone ratR where
  nn := ratPos.nn
  nn_zero := ratPos.nn_zero
  nn_add := ratPos.nn_add
  nn_mul := ratPos.nn_mul
  nn_sq := ratPos.nn_sq

open LibRat in
def ratOrd : OrdR ratR where
  toCone := ratCone
  total := fun x y => by
    cases rat_total (ratR.add y (ratR.neg x)) with
    | inl p => exact Or.inl ⟨_, p, by lib_ring⟩
    | inr p => exact Or.inr ⟨_, p, by lib_ring⟩
  antisym := fun x y h1 h2 => by
    cases h1 with
    | intro d hd => cases h2 with
      | intro e he =>
        have hde : e = ratR.neg d := by
          have s : ratR.add d e = ratR.zero := by
            have : ratR.add d e = ratR.add (ratR.add (ratR.add x d) e) (ratR.neg x) := by lib_ring
            rw [this, hd.2, he.2]; lib_ring
          have : e = ratR.add (ratR.add d e) (ratR.neg d) := by lib_ring
          rw [this, s]; lib_ring
        have d0 : d = ratR.zero := rat_antisymm d hd.1 (by rw [← hde]; exact he.1)
        rw [← hd.2, d0]; lib_ring
  noZD := rat_noZD

theorem model_rat (a : Nat → Nat → LibRat.Q) (h : LibRat.Q) (m N : Nat) (hh : ratOrd.nn h)
    (h0 : ∀ t, a t 0 = LibRat.ratR.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = LibRat.ratR.zero)
    (step : ∀ t n, LibRat.ratR.add (a (t + 1) (n + 1)) (LibRat.ratR.neg (a t (n + 1)))
      = LibRat.ratR.mul h (LibRat.ratR.add (LibRat.ratR.neg (LibRat.ratR.mul (castN LibRat.ratR 3)
          (LibRat.ratR.mul (P4 LibRat.ratR (n + 1)) (LibRat.ratR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN LibRat.ratR (P2 LibRat.ratR) (fun i => LibRat.ratR.add (a (t + 1) i) (a t i)) n)))
    (hA2 : Le LibRat.ratR ratOrd.toCone (four LibRat.ratR (E0 LibRat.ratR a N))
      (LibRat.ratR.mul (castN LibRat.ratR m) (castN LibRat.ratR m))) :
    ∀ t, Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a t) N)
      (LibRat.ratR.add (Ew LibRat.ratR (P4 LibRat.ratR) (a 0) N)
        (sumN LibRat.ratR (fun _ => LibRat.ratR.mul h
          (headB LibRat.ratR (P4 LibRat.ratR) (P2 LibRat.ratR) (four LibRat.ratR (E0 LibRat.ratR a N))
            (castN LibRat.ratR m) m N)) t)) :=
  model_gen LibRat.ratR ratOrd a h m N hh h0 hz step hA2

end RatModel

#print axioms RatModel.p4_sq
#print axioms RatModel.crit_g
#print axioms RatModel.nn_one'
#print axioms RatModel.nn_cast
#print axioms RatModel.nn_pw
#print axioms RatModel.cast_add
#print axioms RatModel.one_le_p2
#print axioms RatModel.cast_mono
#print axioms RatModel.cast_le_p2
#print axioms RatModel.nn_qq_g
#print axioms RatModel.model_gen
#print axioms RatModel.ratOrd
#print axioms RatModel.model_rat

#print axioms: 13 theorems, no axioms.

79.108 Uniform and mode bounds over \(\mathbb Q\)

The horizon, truncation-uniform and mode bounds for the concrete model over any ordered ring and over \(\mathbb Q\). \(E_w(a_t)\le W+T\,h\,B(F)\) and \(4^{n+1}a_t(n+1)^2\le W+T\,h\,B(F)\) for \(t\le T\), \(n<N\).

ns/RatModelU.lean

/-
  ns/RatModelU.lean — T37 cycle 734 (goal G447): the horizon, truncation-uniform and mode bounds (G416–G422)
  for the concrete model over any ordered ring, in particular ℚ.

  Lean 4 core plus ns/RatModel (model_gen, ratOrd), ns/DyadicModelMode (term_le), lib/RingTac (lib_ring).
    sumN_const_g   Σ_{j<t} X = t·X
    le_mul_rg      x ≤ y, c ≥ 0 ⇒ x·c ≤ y·c
    headB_mono     E ≤ F ⇒ B(E) ≤ B(F)
    uniform_g      E_w(a_0) ≤ W, 4E₀ ≤ F ≤ A², t ≤ T ⇒ E_w(a_t) ≤ W + T·h·B(F)
    mode_g         … ⇒ 4^{n+1}·a_t(n+1)² ≤ W + T·h·B(F)   (n < N)
    uniform_rat    uniform_g over ℚ
-/
import ns.RatModel
import ns.DyadicModelMode

namespace RatModelU
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData RatModel DyadicModelMode

section Gen
variable {R : Type} (K : CRing R)

theorem sumN_const_g (X : R) : ∀ t, sumN K (fun _ => X) t = K.mul (castN K t) X
  | 0 => by show K.zero = K.mul K.zero X; lib_ring
  | t + 1 => by
    show K.add (sumN K (fun _ => X) t) X = K.mul (K.add K.one (castN K t)) X
    rw [sumN_const_g X t]; lib_ring

variable (P : Cone K)

theorem le_mul_rg (c x y : R) (hc : P.nn c) (h : Le K P x y) : Le K P (K.mul x c) (K.mul y c) := by
  cases h with
  | intro d hd =>
    exact ⟨K.mul d c, P.nn_mul _ _ hd.1 hc, by rw [← hd.2]; lib_ring⟩

theorem ite_le (c : Prop) (inst : Decidable c) (x y : R) (h : c → Le K P x y) :
    Le K P (@ite _ c inst x K.zero) (@ite _ c inst y K.zero) := by
  cases inst with
  | isTrue p => exact h p
  | isFalse _ => exact le_refl' K P _

theorem ite_nn (c : Prop) (inst : Decidable c) (x : R) (h : c → P.nn x) :
    P.nn (@ite _ c inst x K.zero) := by
  cases inst with
  | isTrue p => exact h p
  | isFalse _ => exact P.nn_zero

theorem headB_mono (w c : Nat → R) (E F A : R) (n0 N : Nat) (hq : ∀ n, P.nn (qq K w c n)) (hA : P.nn A)
    (hEF : Le K P E F) : Le K P (headB K w c E A n0 N) (headB K w c F A n0 N) := by
  unfold headB
  apply sum_le K P
  intro n
  exact ite_le K P _ _ _ _ (fun _ => le_mul_nn K P _ _ _ (hq n) (le_mul_rg K P A E F hA hEF))

theorem nn_headB (w c : Nat → R) (F A : R) (n0 N : Nat) (hq : ∀ n, P.nn (qq K w c n)) (hA : P.nn A)
    (hF : P.nn F) : P.nn (headB K w c F A n0 N) := by
  unfold headB
  apply sumN_nn K P
  intro n
  exact ite_nn K P _ _ _ (fun _ => P.nn_mul _ _ (hq n) (P.nn_mul _ _ hF hA))

theorem nn_four (E : R) (hE : P.nn E) : P.nn (four K E) :=
  P.nn_add _ _ (P.nn_add _ _ hE hE) (P.nn_add _ _ hE hE)

end Gen

theorem uniform_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (t : Nat) (ht : t ≤ T) :
    Le K O.toCone (Ew K (P4 K) (a t) N)
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) := by
  have hA2 := le_trans' K O.toCone _ _ _ hF hFA
  have hb := model_gen K O a h m N hh h0 hz step hA2 t
  rw [sumN_const_g] at hb
  have e0 : O.nn (four K (E0 K a N)) := nn_four K O.toCone _ (sq_sum_nonneg K O.toCone (fun n => a 0 (n + 1)) N)
  have hFn : O.nn F := by
    cases hF with
    | intro d hd => rw [← hd.2]; exact O.nn_add _ _ e0 hd.1
  have hq := nn_qq_g K O.toCone
  have hAm := nn_cast K O.toCone m
  have hB := headB_mono K O.toCone (P4 K) (P2 K) _ _ (castN K m) m N hq hAm hF
  have hX := le_mul_nn K O.toCone h _ _ hh hB
  have hY := le_mul_nn K O.toCone (castN K t) _ _ (nn_cast K O.toCone t) hX
  have hT := le_mul_rg K O.toCone (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)) _ _
    (O.nn_mul _ _ hh (nn_headB K O.toCone _ _ F _ m N hq hAm hFn)) (cast_mono K O.toCone t T ht)
  exact le_trans' K O.toCone _ _ _ hb
    (le_add' K O.toCone _ _ _ _ hW (le_trans' K O.toCone _ _ _ hY hT))

theorem mode_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (n t : Nat) (hn : n < N) (ht : t ≤ T) :
    Le K O.toCone (K.mul (P4 K (n + 1)) (K.mul (a t (n + 1)) (a t (n + 1))))
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) :=
  le_trans' K O.toCone _ _ _
    (term_le K O.toCone (fun j => K.mul (P4 K (j + 1)) (K.mul (a t (j + 1)) (a t (j + 1))))
      (fun j => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (j + 1)) (O.nn_sq _)) n N hn)
    (uniform_g K O a h W F m N T hh h0 hz step hW hF hFA t ht)

theorem uniform_rat (a : Nat → Nat → LibRat.Q) (h W F : LibRat.Q) (m N T : Nat) (hh : ratOrd.nn h)
    (h0 : ∀ t, a t 0 = LibRat.ratR.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = LibRat.ratR.zero)
    (step : ∀ t n, LibRat.ratR.add (a (t + 1) (n + 1)) (LibRat.ratR.neg (a t (n + 1)))
      = LibRat.ratR.mul h (LibRat.ratR.add (LibRat.ratR.neg (LibRat.ratR.mul (castN LibRat.ratR 3)
          (LibRat.ratR.mul (P4 LibRat.ratR (n + 1)) (LibRat.ratR.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN LibRat.ratR (P2 LibRat.ratR) (fun i => LibRat.ratR.add (a (t + 1) i) (a t i)) n)))
    (hW : Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a 0) N) W)
    (hF : Le LibRat.ratR ratOrd.toCone (four LibRat.ratR (E0 LibRat.ratR a N)) F)
    (hFA : Le LibRat.ratR ratOrd.toCone F (LibRat.ratR.mul (castN LibRat.ratR m) (castN LibRat.ratR m)))
    (t : Nat) (ht : t ≤ T) :
    Le LibRat.ratR ratOrd.toCone (Ew LibRat.ratR (P4 LibRat.ratR) (a t) N)
      (LibRat.ratR.add W (LibRat.ratR.mul (castN LibRat.ratR T)
        (LibRat.ratR.mul h (headB LibRat.ratR (P4 LibRat.ratR) (P2 LibRat.ratR) F (castN LibRat.ratR m) m N)))) :=
  uniform_g LibRat.ratR ratOrd a h W F m N T hh h0 hz step hW hF hFA t ht

end RatModelU

#print axioms RatModelU.sumN_const_g
#print axioms RatModelU.le_mul_rg
#print axioms RatModelU.ite_le
#print axioms RatModelU.ite_nn
#print axioms RatModelU.headB_mono
#print axioms RatModelU.nn_headB
#print axioms RatModelU.nn_four
#print axioms RatModelU.uniform_g
#print axioms RatModelU.mode_g
#print axioms RatModelU.uniform_rat

#print axioms: 10 theorems, no axioms.

79.109 Tightness over \(\mathbb Q\)

The tail bound of the concrete model over any ordered ring, uniform in \(N\) and \(t\le T\). \(4^M\sum _{M\le n\le N}a_t(n)^2\le W+T\,h\,B(F)\).

ns/RatModelTail.lean

/-
  ns/RatModelTail.lean — T37 cycle 737 (goal G450): tightness for the concrete model over any ordered ring / ℚ —
  the L² energy beyond shell M is ≤ C/4^M, uniformly in the truncation and in t ≤ T (G425 over ℚ).

  Lean 4 core plus ns/RatModelU (uniform_g), ns/RatModel (P4, cast lemmas), lib/RingTac (lib_ring).
    pw_step     x ≥ 1, y ≥ 0 ⇒ y ≤ y·x
    p4_mono_g   M ≤ n ⇒ 4^M ≤ 4^n
    tail_le_g   4^M · Σ_{j<N, M ≤ j+1} a(j+1)² ≤ E_w(a)
    tail_g      4^M · Σ_{j<N, M ≤ j+1} a_t(j+1)² ≤ W + T·h·B(F)
-/
import ns.RatModelU

namespace RatModelTail
open DetChain NSChain DyadicSmallGlobal LibRing DyadicHs DyadicCN DyadicLarge DyadicLargeCN
  DyadicLargeEnergy DyadicLargeData RatModel RatModelU

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem pw_step (x y : R) (hx : Le K P K.one x) (hy : P.nn y) : Le K P y (K.mul y x) := by
  cases hx with
  | intro d hd => exact ⟨K.mul y d, P.nn_mul _ _ hy hd.1, by rw [← hd.2]; lib_ring⟩

theorem one_le_four : Le K P K.one (castN K 4) :=
  ⟨castN K 3, nn_cast K P 3, by show K.add K.one (castN K 3) = K.add K.one (castN K 3); rfl⟩

theorem p4_mono_g (M : Nat) : ∀ d, Le K P (P4 K M) (P4 K (M + d))
  | 0 => le_refl' K P _
  | d + 1 => le_trans' K P _ _ _ (p4_mono_g M d)
      (pw_step K P _ _ (one_le_four K P) (nn_pw K P _ (nn_cast K P 4) (M + d)))

def tl (M : Nat) (b : Nat → R) (j : Nat) : R :=
  match Nat.ble M (j + 1) with
  | true => K.mul (b (j + 1)) (b (j + 1))
  | false => K.zero

theorem term_g (M : Nat) (b : Nat → R) (j : Nat) :
    Le K P (K.mul (P4 K M) (tl K M b j)) (K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1)))) := by
  unfold tl
  cases e : Nat.ble M (j + 1) with
  | true =>
    have ⟨d, hd⟩ := DyadicModelZ.le_dest (Nat.le_of_ble_eq_true e)
    have l := le_mul_rg K P (K.mul (b (j + 1)) (b (j + 1))) _ _ (P.nn_sq _) (p4_mono_g K P M d)
    rw [hd] at l; exact l
  | false =>
    refine ⟨K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1))),
      P.nn_mul _ _ (nn_pw K P _ (nn_cast K P 4) (j + 1)) (P.nn_sq _), ?_⟩
    show K.add (K.mul (P4 K M) K.zero) (K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1))))
      = K.mul (P4 K (j + 1)) (K.mul (b (j + 1)) (b (j + 1)))
    lib_ring

theorem tail_le_g (M N : Nat) (b : Nat → R) :
    Le K P (K.mul (P4 K M) (sumN K (tl K M b) N)) (Ew K (P4 K) b N) := by
  rw [← sumN_mul_left]
  exact sum_le K P _ _ (term_g K P M b) N

end Gen

theorem tail_g {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h W F : R) (m N T M : Nat)
    (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n)))
    (hW : Le K O.toCone (Ew K (P4 K) (a 0) N) W)
    (hF : Le K O.toCone (four K (E0 K a N)) F)
    (hFA : Le K O.toCone F (K.mul (castN K m) (castN K m)))
    (t : Nat) (ht : t ≤ T) :
    Le K O.toCone (K.mul (P4 K M) (sumN K (tl K M (a t)) N))
      (K.add W (K.mul (castN K T) (K.mul h (headB K (P4 K) (P2 K) F (castN K m) m N)))) :=
  le_trans' K O.toCone _ _ _ (tail_le_g K O.toCone M N (a t)) (uniform_g K O a h W F m N T hh h0 hz step hW hF hFA t ht)

end RatModelTail

#print axioms RatModelTail.pw_step
#print axioms RatModelTail.one_le_four
#print axioms RatModelTail.p4_mono_g
#print axioms RatModelTail.term_g
#print axioms RatModelTail.tail_le_g
#print axioms RatModelTail.tail_g

#print axioms: 6 theorems, no axioms.

79.110 Dissipation budget

The exact energy budget of a Crank–Nicolson Galerkin scheme over any commutative ring, bounded over any ordered ring. \(E(T)+\sum _{t<T}h\nu \sum _nk_n(a_{t+1}(n)+a_t(n))^2=E(0)\).

ns/DissBudget.lean

/-
  ns/DissBudget.lean — T37 cycle 740 (goal G453): the exact dissipation budget of a Crank–Nicolson Galerkin scheme
  over any commutative ring, and its bound over any ordered ring (ℚ included).

  Lean 4 core plus NSChain (cn_energy, dissipation_nonneg), lib/RingTac (lib_ring).
    budget       E(T) + Σ_{t<T} D_t = E(0),  D_t = h ν Σ_n k_n (a_{t+1}(n) + a_t(n))²     (exact)
    budget_le    Σ_{t<T} D_t ≤ E(0)      (only E(T) ≥ 0 is used)
  The total dissipation over all time is bounded by the initial energy: the discrete form of
  ν ∫₀^∞ ‖∇u‖² dt ≤ ½‖u₀‖².
-/
import NSChain
import lib.RingTac

namespace DissBudget
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

def En (a : Nat → Nat → R) (N t : Nat) : R := sumN K (fun n => K.mul (a t n) (a t n)) N
def D (a : Nat → Nat → R) (k : Nat → R) (h nu : R) (N t : Nat) : R :=
  K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N))

theorem budget (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) :
    ∀ T, K.add (En K a N T) (sumN K (D K a k h nu N) T) = En K a N 0
  | 0 => by show K.add (En K a N 0) K.zero = En K a N 0; lib_ring
  | T + 1 => by
    have e := cn_energy K (a T) (a (T + 1)) k (Nn T) h nu N (step T) (neutral T)
    have ih := budget a k Nn h nu N step neutral T
    show K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T)) = En K a N 0
    have r : K.add (En K a N (T + 1)) (K.add (sumN K (D K a k h nu N) T) (D K a k h nu N T))
        = K.add (K.add (En K a N (T + 1)) (D K a k h nu N T)) (sumN K (D K a k h nu N) T) := by lib_ring
    rw [r]
    have e' : K.add (En K a N (T + 1)) (D K a k h nu N T) = En K a N T := e
    rw [e', ih]

theorem budget_le (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R) (h nu : R) (N : Nat)
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) (T : Nat) :
    Le K P (sumN K (D K a k h nu N) T) (En K a N 0) :=
  ⟨En K a N T, sq_sum_nonneg K P (a T) N, by
    rw [K.add_comm]; exact budget K a k Nn h nu N step neutral T⟩

end Gen

end DissBudget

#print axioms DissBudget.budget
#print axioms DissBudget.budget_le

#print axioms: 2 theorems, no axioms.

79.111 Dissipation budget of the dyadic model

The exact budget for the dyadic model over any ring, and the \(L^2_tH^1_x\) bound of the midpoints for the concrete model. \(\sum _{t<T}h\cdot 3\sum _{n<N}4^{n+1}(a_{t+1}(n+1)+a_t(n+1))^2\le E_0\).

ns/DyadicBudget.lean

/-
  ns/DyadicBudget.lean — T37 cycle 743 (goal G456): the dissipation budget for the dyadic model over any ring, and
  for the concrete supercritical model over any ordered ring / ℚ: the discrete L²_t H¹_x bound of the midpoints.

  Lean 4 core plus ns/DissBudget (budget, budget_le), ns/RatModel (P4, P2), ns/DyadicLargeData (E0),
  NSChain (dyadic_energy_neutral).
    dy_budget     E(T) + Σ_{t<T} h ν Σ_{n<N} k_{n+1} (a_{t+1}(n+1) + a_t(n+1))² = E(0)       (exact, any ring)
    model_budget  Σ_{t<T} h·3·Σ_{n<N} 4^{n+1} (a_{t+1}(n+1) + a_t(n+1))² ≤ E₀           (any ordered ring, ℚ)
-/
import ns.DissBudget
import ns.RatModel

namespace DyadicBudget
open DetChain NSChain LibRing DyadicLargeData DissBudget RatModel

section Gen
variable {R : Type} (K : CRing R)

theorem dy_budget (c k : Nat → R) (a : Nat → Nat → R) (h nu : R) (N : Nat)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    K.add (En K (fun t n => a t (n + 1)) N T) (sumN K (D K (fun t n => a t (n + 1)) (fun n => k (n + 1)) h nu N) T)
      = E0 K a N :=
  budget K (fun t n => a t (n + 1)) (fun n => k (n + 1))
    (fun t n => dyN K c (fun i => K.add (a (t + 1) i) (a t i)) n) h nu N step
    (fun t => dyadic_energy_neutral K c (fun i => K.add (a (t + 1) i) (a t i)) N
      (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
      (by show K.add (a (t + 1) (N + 1)) (a t (N + 1)) = K.zero
          rw [hz (t + 1) (N + 1) (Nat.le_refl _), hz t (N + 1) (Nat.le_refl _), K.zero_add])) T

theorem model_budget (P : Cone K) (a : Nat → Nat → R) (h : R) (N : Nat)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    Le K P (sumN K (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N) T) (E0 K a N) :=
  ⟨_, sq_sum_nonneg K P (fun n => a T (n + 1)) N, by
    rw [K.add_comm]; exact dy_budget K (P2 K) (P4 K) a h (castN K 3) N h0 hz step T⟩

end Gen

end DyadicBudget

#print axioms DyadicBudget.dy_budget
#print axioms DyadicBudget.model_budget

#print axioms: 2 theorems, no axioms.

79.112 Good times

Among the first \(T+1\) steps of the concrete model there is one with dissipation at most \(E_0/(T+1)\). \(\exists \,t\le T:\ (T+1)\,D_t\le E_0\).

ns/GoodTime.lean

/-
  ns/GoodTime.lean — T37 cycle 746 (goal G459): good times. Among the first T+1 steps of the concrete model over
  any ordered ring (ℚ included) there is one whose dissipation is at most E₀/(T+1) — the discrete form of Leray's
  "u(t) ∈ H¹ for almost every t" (the set of bad times has small measure).

  Lean 4 core plus ns/DyadicBudget (model_budget), ns/RatModel (cast lemmas), lib/RingTac (lib_ring).
    avg_le      ∃ t ≤ T, (T+1)·d_t ≤ Σ_{j≤T} d_j      (total order, constructive by induction)
    good_time   ∃ t ≤ T, (T+1)·D_t ≤ E₀  for the concrete model
-/
import ns.DyadicBudget

namespace GoodTime
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem avg_le (d : Nat → R) (hd : ∀ t, O.nn (d t)) : ∀ T, ∃ t, t ≤ T ∧
    Le K O.toCone (K.mul (castN K (T + 1)) (d t)) (sumN K d (T + 1))
  | 0 => ⟨0, Nat.le_refl 0, ⟨K.zero, O.nn_zero, by
      show K.add (K.mul (K.add K.one K.zero) (d 0)) K.zero = K.add K.zero (d 0); lib_ring⟩⟩
  | T + 1 => by
    have ⟨t, ht, hle⟩ := avg_le d hd T
    have hc : O.nn (castN K (T + 1)) := nn_cast K O.toCone (T + 1)
    cases O.total (d t) (d (T + 1)) with
    | inl l =>
      refine ⟨t, Nat.le_succ_of_le ht, ?_⟩
      have := le_add' K O.toCone _ _ _ _ hle l
      have e : K.mul (castN K (T + 1 + 1)) (d t) = K.add (K.mul (castN K (T + 1)) (d t)) (d t) := by
        show K.mul (K.add K.one (castN K (T + 1))) (d t) = _; lib_ring
      rw [e]; exact this
    | inr l =>
      refine ⟨T + 1, Nat.le_refl _, ?_⟩
      have l2 := le_mul_nn K O.toCone _ _ _ hc l
      have := le_add' K O.toCone _ _ _ _ (le_trans' K O.toCone _ _ _ l2 hle) (le_refl' K O.toCone (d (T + 1)))
      have e : K.mul (castN K (T + 1 + 1)) (d (T + 1))
          = K.add (K.mul (castN K (T + 1)) (d (T + 1))) (d (T + 1)) := by
        show K.mul (K.add K.one (castN K (T + 1))) (d (T + 1)) = _; lib_ring
      rw [e]; exact this

theorem good_time (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ Le K O.toCone
      (K.mul (castN K (T + 1)) (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N t)) (E0 K a N) := by
  have hd : ∀ t, O.nn (D K (fun t n => a t (n + 1)) (fun n => P4 K (n + 1)) h (castN K 3) N t) := fun t =>
    O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)) (O.nn_sq _)) N))
  have ⟨t, ht, hle⟩ := avg_le K O _ hd T
  exact ⟨t, ht, le_trans' K O.toCone _ _ _ hle (model_budget K O.toCone a h N h0 hz step (T + 1))⟩

end Gen

end GoodTime

#print axioms GoodTime.avg_le
#print axioms GoodTime.good_time

#print axioms: 2 theorems, no axioms.

79.113 Mode decay at good times

At some \(t\le T\) every midpoint mode decays like \(2^{-n}\), with no large-data hypothesis. \((T+1)\,h\cdot 3\cdot 4^{n+1}(a_{t+1}(n+1)+a_t(n+1))^2\le E_0\) for all \(n<N\).

ns/GoodModes.lean

/-
  ns/GoodModes.lean — T37 cycle 750 (goal G463): at a good time every midpoint mode decays like 2^{-n}, with no
  large-data hypothesis: for the concrete model over any ordered ring (ℚ included) there is t ≤ T with
    (T+1)·h·3·4^{n+1}·(a_{t+1}(n+1) + a_t(n+1))² ≤ E₀   for every n < N.

  Lean 4 core plus ns/GoodTime (good_time), ns/DyadicModelMode (term_le), ns/RatModel.
    good_modes   the statement above
-/
import ns.GoodTime
import ns.DyadicModelMode

namespace GoodModes
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge
  GoodTime DyadicModelMode

theorem good_modes {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1))
        (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))))
      (E0 K a N) := by
  have ⟨t, ht, hle⟩ := good_time K O a h N hh h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  have tl := term_le K O.toCone (fun j => K.mul (P4 K (j + 1))
      (K.mul (K.add (a (t + 1) (j + 1)) (a t (j + 1))) (K.add (a (t + 1) (j + 1)) (a t (j + 1)))))
    (fun j => O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (j + 1)) (O.nn_sq _)) n N hn
  have l1 := le_mul_nn K O.toCone (castN K 3) _ _ (nn_cast K O.toCone 3) tl
  have l2 := le_mul_nn K O.toCone h _ _ hh l1
  have l3 := le_mul_nn K O.toCone (castN K (T + 1)) _ _ (nn_cast K O.toCone (T + 1)) l2
  exact le_trans' K O.toCone _ _ _ l3 hle

end GoodModes

#print axioms GoodModes.good_modes

#print axioms: 1 theorem, no axioms.

79.114 Recovering the new state from the midpoint

Each mode of \(a_{t+1}\) is controlled by the midpoint sum \(s\) and the step, over any ring with a positive cone. \(4a_{t+1}(n)^2\le 2s_n^2+4h^2(\nu ^2k_n^2s_n^2+N_n^2)\).

ns/MidRecover.lean

/-
  ns/MidRecover.lean — T37 cycle 753 (goal G466): recovering a_{t+1} from the midpoint sum s = a_{t+1} + a_t and
  the step, mode by mode, over any ring with a positive cone (ℚ included):
    4·a_{t+1}(n)² ≤ 2 s_n² + 4 h² (ν² k_n² s_n² + N_n²)
  where the CN step reads a_{t+1}(n) − a_t(n) = h(−ν k_n s_n + N_n) (N_n = dyN for the dyadic model).

  Lean 4 core plus ns/DyadicLarge (le_add'), NSChain (Cone, Le), lib/RingTac (lib_ring).
    sq_add_le   (x + y)² ≤ 2x² + 2y²
    le_two_mul  x ≤ y, c ≥ 0 ⇒ c·x ≤ c·y
    recover     the inequality above
-/
import ns.DyadicLarge
import lib.RingTac

namespace MidRecover
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def two (x : R) : R := K.add x x

theorem sq_add_le (x y : R) :
    Le K P (K.mul (K.add x y) (K.add x y)) (K.add (two K (K.mul x x)) (two K (K.mul y y))) :=
  ⟨K.mul (K.add x (K.neg y)) (K.add x (K.neg y)), P.nn_sq _, by unfold two; lib_ring⟩

theorem le_two_mul (c x y : R) (hc : P.nn c) (h : Le K P x y) : Le K P (K.mul c x) (K.mul c y) := by
  cases h with
  | intro d hd => exact ⟨K.mul c d, P.nn_mul _ _ hc hd.1, by rw [← hd.2]; lib_ring⟩

theorem recover (a a' k Nn : R) (h nu : R)
    (step : K.add a' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)) :
    Le K P (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a' a'))
      (K.add (two K (K.mul (K.add a' a) (K.add a' a)))
        (two K (two K (K.mul (K.mul h h)
          (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))))) := by
  -- 2a' = s + Δ with Δ = a' − a = h(−ν k s + N)
  have e1 : K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul a' a')
      = K.mul (K.add (K.add a' a) (K.add a' (K.neg a))) (K.add (K.add a' a) (K.add a' (K.neg a))) := by lib_ring
  rw [e1]
  have l1 := sq_add_le K P (K.add a' a) (K.add a' (K.neg a))
  apply le_trans' K P _ _ _ l1
  apply le_add' K P _ _ _ _ (le_refl' K P _)
  unfold two
  rw [step]
  -- Δ² = h² (−ν k s + N)² ≤ h² · 2 (ν²k²s² + N²)
  have l2 := sq_add_le K P (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn
  have l3 := le_two_mul K P (K.mul h h) _ _ (P.nn_sq h) l2
  have l4 := le_add' K P _ _ _ _ l3 l3
  have e2 : K.mul (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn))
      (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn))
      = K.mul (K.mul h h) (K.mul (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)
          (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Nn)) := by lib_ring
  rw [e2]
  have e3 : K.add (K.mul (K.mul h h) (K.add (two K (K.mul (K.neg (K.mul nu (K.mul k (K.add a' a))))
        (K.neg (K.mul nu (K.mul k (K.add a' a)))))) (two K (K.mul Nn Nn))))
      (K.mul (K.mul h h) (K.add (two K (K.mul (K.neg (K.mul nu (K.mul k (K.add a' a))))
        (K.neg (K.mul nu (K.mul k (K.add a' a)))))) (two K (K.mul Nn Nn))))
      = K.add (K.add (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))
          (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn))))
        (K.add (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))
          (K.mul (K.mul h h) (K.add (K.mul (K.mul nu nu) (K.mul (K.mul k k) (K.mul (K.add a' a) (K.add a' a)))) (K.mul Nn Nn)))) := by
    unfold two; lib_ring
  unfold two at l4
  unfold two at e3
  rw [e3] at l4
  exact l4

end Gen

end MidRecover

#print axioms MidRecover.sq_add_le
#print axioms MidRecover.le_two_mul
#print axioms MidRecover.recover

#print axioms: 3 theorems, no axioms.

79.115 The dyadic nonlinearity by mode bounds

If \(s_i^2\le B_i\) (\(i=n,n+1,n+2\)) then \(\mathrm {dyN}(c,s)_n^2\le 2c_n^2B_n^2+2c_{n+1}^2B_{n+1}B_{n+2}\), any ring with a cone. Combined with the recovery bound this controls \(a_{t+1}\) at a good time.

ns/DyNBound.lean

/-
  ns/DyNBound.lean — T37 cycle 756 (goal G469): the dyadic nonlinearity is controlled by mode bounds, over any ring
  with a positive cone (ℚ included): if s_i² ≤ B_i for i = n, n+1, n+2 then
    dyN(c, s)_n² ≤ 2 c_n² B_n² + 2 c_{n+1}² B_{n+1} B_{n+2}.
  With MidRecover and GoodModes this bounds a_{t+1} at a good time by the midpoint mode bounds.

  Lean 4 core plus ns/MidRecover (sq_add_le, le_two_mul), NSChain (dyN), lib/RingTac (lib_ring).
    mul_le_mul'  0 ≤ x ≤ X, 0 ≤ y ≤ Y ⇒ x y ≤ X Y
    dyN_sq       the bound above
-/
import ns.MidRecover

namespace DyNBound
open DetChain NSChain LibRing DyadicLarge MidRecover

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem mul_le_mul' (x X y Y : R) (hx : P.nn x) (hY : P.nn Y) (h1 : Le K P x X) (h2 : Le K P y Y) :
    Le K P (K.mul x y) (K.mul X Y) := by
  have a1 : Le K P (K.mul x y) (K.mul x Y) := le_two_mul K P x y Y hx h2
  have a2 : Le K P (K.mul x Y) (K.mul X Y) := by
    cases h1 with
    | intro d hd => exact ⟨K.mul d Y, P.nn_mul _ _ hd.1 hY, by rw [← hd.2]; lib_ring⟩
  exact le_trans' K P _ _ _ a1 a2

theorem dyN_sq (c s : Nat → R) (n : Nat) (B0 B1 B2 : R) (hB2 : P.nn B2)
    (h0 : Le K P (K.mul (s n) (s n)) B0) (h1 : Le K P (K.mul (s (n + 1)) (s (n + 1))) B1)
    (h2 : Le K P (K.mul (s (n + 2)) (s (n + 2))) B2) (hB0 : P.nn B0) :
    Le K P (K.mul (dyN K c s n) (dyN K c s n))
      (K.add (two K (K.mul (K.mul (c n) (c n)) (K.mul B0 B0)))
        (two K (K.mul (K.mul (c (n + 1)) (c (n + 1))) (K.mul B1 B2)))) := by
  unfold dyN
  have l := sq_add_le K P (K.mul (c n) (K.mul (s n) (s n))) (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
  apply le_trans' K P _ _ _ l
  unfold two
  -- (c s²)² = c² (s²)(s²) ≤ c² B0 B0 ;  (−c' s1 s2)² = c'² (s1²)(s2²) ≤ c'² B1 B2
  have p0 := mul_le_mul' K P _ _ _ _ (P.nn_sq (s n)) hB0 h0 h0
  have p1 := mul_le_mul' K P _ _ _ _ (P.nn_sq (s (n + 1))) hB2 h1 h2
  have q0 := le_two_mul K P (K.mul (c n) (c n)) _ _ (P.nn_sq _) p0
  have q1 := le_two_mul K P (K.mul (c (n + 1)) (c (n + 1))) _ _ (P.nn_sq _) p1
  have e0 : K.mul (K.mul (c n) (K.mul (s n) (s n))) (K.mul (c n) (K.mul (s n) (s n)))
      = K.mul (K.mul (c n) (c n)) (K.mul (K.mul (s n) (s n)) (K.mul (s n) (s n))) := by lib_ring
  have e1 : K.mul (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
      (K.neg (K.mul (c (n + 1)) (K.mul (s (n + 1)) (s (n + 2)))))
      = K.mul (K.mul (c (n + 1)) (c (n + 1))) (K.mul (K.mul (s (n + 1)) (s (n + 1))) (K.mul (s (n + 2)) (s (n + 2)))) := by
    lib_ring
  rw [e0, e1]
  exact le_add' K P _ _ _ _ (le_add' K P _ _ _ _ q0 q0) (le_add' K P _ _ _ _ q1 q1)

end Gen

end DyNBound

#print axioms DyNBound.mul_le_mul'
#print axioms DyNBound.dyN_sq

#print axioms: 2 theorems, no axioms.

79.116 Good-time mode bounds in divided form

Over an ordered field (\(\mathbb Q\) included) the good-time bound gives each midpoint mode directly. \((a_{t+1}(n+1)+a_t(n+1))^2\le E_0\,\big ((T+1)\,h\cdot 3\cdot 4^{n+1}\big )^{-1}\) for all \(n<N\).

ns/GoodModesQ.lean

/-
  ns/GoodModesQ.lean — T37 cycle 760 (goal G472): GoodModes in divided form over an ordered field (ℚ included):
  there is t ≤ T with, for every n < N,
    (a_{t+1}(n+1) + a_t(n+1))² ≤ E₀ · X_n⁻¹,   X_n = (T+1)·h·3·4^{n+1}    (h ≠ 0).

  Lean 4 core plus ns/GoodModes, ns/RatModelU (le_mul_rg), lib/RingTac (lib_ring).
    ne_of_one_add   1 ≠ 0, x ≥ 0 ⇒ 1 + x ≠ 0
    cast_succ_ne    castN (n+1) ≠ 0;  pw_ne  x ≠ 0 ⇒ xⁿ ≠ 0;  mul_ne  (no zero divisors)
    nn_inv'         x ≥ 0, x·x⁻¹ = 1 ⇒ x⁻¹ ≥ 0
    div_le          X ≥ 0, X·X⁻¹ = 1, X·y ≤ E ⇒ y ≤ E·X⁻¹
    good_modes_q    the statement
-/
import ns.GoodModes
import ns.RatModelU

namespace GoodModesQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData DissBudget RatModel DyadicBudget DyadicLarge
  GoodTime DyadicModelMode GoodModes RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem one_eq_zero_of (h10 : K.one ≠ K.zero) (x : R) (hx : O.nn x) (e : K.add K.one x = K.zero) : False :=
  h10 (O.antisym _ _ ⟨x, hx, e⟩ ⟨K.one, nn_one' K O.toCone, K.zero_add _⟩)

theorem ne_of_one_add (h10 : K.one ≠ K.zero) (x : R) (hx : O.nn x) : K.add K.one x ≠ K.zero :=
  fun e => one_eq_zero_of K O h10 x hx e

theorem cast_succ_ne (h10 : K.one ≠ K.zero) (n : Nat) : castN K (n + 1) ≠ K.zero :=
  ne_of_one_add K O h10 _ (nn_cast K O.toCone n)

theorem mul_ne (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := fun e => by
  cases O.noZD x y e with
  | inl h => exact hx h
  | inr h => exact hy h

theorem pw_ne (h10 : K.one ≠ K.zero) (x : R) (hx : x ≠ K.zero) : ∀ n, pw K x n ≠ K.zero
  | 0 => h10
  | n + 1 => mul_ne K O _ _ (pw_ne h10 x hx n) hx

theorem nn_inv' (h10 : K.one ≠ K.zero) (x xi : R) (hx : O.nn x) (hm : K.mul x xi = K.one) : O.nn xi := by
  cases O.total K.zero xi with
  | inl l => cases l with
    | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
  | inr l => cases l with
    | intro d hd =>
      exfalso
      have e : K.add K.one (K.mul x d) = K.zero := by
        have : d = K.neg xi := by
          have : d = K.add (K.add xi d) (K.neg xi) := by lib_ring
          rw [this, hd.2]; lib_ring
        rw [this, ← hm]; lib_ring
      exact one_eq_zero_of K O h10 _ (O.nn_mul _ _ hx hd.1) e

theorem div_le (X Xi y E : R) (h10 : K.one ≠ K.zero) (hX : O.nn X) (hm : K.mul X Xi = K.one)
    (h : Le K O.toCone (K.mul X y) E) : Le K O.toCone y (K.mul E Xi) := by
  have l := le_mul_rg K O.toCone Xi _ _ (nn_inv' K O h10 X Xi hX hm) h
  have e : K.mul (K.mul X y) Xi = y := by
    have : K.mul (K.mul X y) Xi = K.mul (K.mul X Xi) y := by lib_ring
    rw [this, hm]; lib_ring
  rw [e] at l; exact l

end Gen

theorem good_modes_q {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h) (hh0 : h ≠ K.zero)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))
      (K.mul (E0 K a N) (inv (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))))) := by
  have ⟨t, ht, hb⟩ := good_modes K O a h N hh h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  have hX0 : K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) ≠ K.zero :=
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (n + 1))))
  have hXn : O.nn (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))) :=
    O.nn_mul _ _ (nn_cast K O.toCone _) (O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1))))
  apply div_le K O _ _ _ _ h10 hXn (mul_inv _ hX0)
  have e : K.mul (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))
      (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))
      = K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1))
        (K.mul (K.add (a (t + 1) (n + 1)) (a t (n + 1))) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))) := by lib_ring
  rw [e]; exact hb n hn

end GoodModesQ

#print axioms GoodModesQ.one_eq_zero_of
#print axioms GoodModesQ.ne_of_one_add
#print axioms GoodModesQ.cast_succ_ne
#print axioms GoodModesQ.mul_ne
#print axioms GoodModesQ.pw_ne
#print axioms GoodModesQ.nn_inv'
#print axioms GoodModesQ.div_le
#print axioms GoodModesQ.good_modes_q

#print axioms: 8 theorems, no axioms.

79.117 The state at a good time

At a good time each mode of \(a_{t+1}\) is bounded by the midpoint bounds \(B(i)=E_0X_{i-1}^{-1}\), with no large-data hypothesis. \(4a_{t+1}(n+1)^2\le 2B(n+1)+4h^2\big (9\cdot 16^{n+1}B(n+1)+2\cdot 4^nB(n)^2+2\cdot 4^{n+1}B(n+1)B(n+2)\big )\).

ns/GoodState.lean

/-
  ns/GoodState.lean — T37 cycle 763 (goal G475): at a good time the new state itself is bounded mode by mode, over
  an ordered field (ℚ included), with no large-data hypothesis. With B(i) = E₀·X_{i−1}⁻¹ (i ≥ 1), B(0) = E₀,
  X_n = (T+1)·h·3·4^{n+1}: there is t ≤ T with, for every n < N,
    4·a_{t+1}(n+1)² ≤ 2B(n+1) + 4h²(9·16^{n+1}·B(n+1) + 2·4ⁿ·B(n)² + 2·4^{n+1}·B(n+1)B(n+2)).

  Lean 4 core plus ns/GoodModesQ, ns/DyNBound (dyN_sq), ns/MidRecover (recover, le_two_mul).
    Bf, s_le_B     every midpoint mode is ≤ B (interior: GoodModesQ; boundary and beyond the truncation: 0)
    good_state     the statement
-/
import ns.GoodModesQ
import ns.DyNBound

namespace GoodState
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLargeData RatModel DyadicLarge GoodModesQ MidRecover
  DyNBound RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)

def X (h : R) (T n : Nat) : R := K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))

def Bf (E h : R) (T : Nat) : Nat → R
  | 0 => E
  | i + 1 => K.mul E (inv (X K h T i))

end Gen

theorem good_state {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h : R) (N : Nat) (hh : O.nn h) (hh0 : h ≠ K.zero)
    (h0 : ∀ t, a t 0 = K.zero) (hz : ∀ t n, N + 1 ≤ n → a t n = K.zero)
    (step : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.mul h (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (K.add (a (t + 1) (n + 1)) (a t (n + 1))))))
          (dyN K (P2 K) (fun i => K.add (a (t + 1) i) (a t i)) n))) (T : Nat) :
    ∃ t, t ≤ T ∧ ∀ n, n < N → Le K O.toCone
      (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul (a (t + 1) (n + 1)) (a (t + 1) (n + 1))))
      (K.add (two K (Bf K inv (E0 K a N) h T (n + 1)))
        (two K (two K (K.mul (K.mul h h)
          (K.add (K.mul (K.mul (castN K 3) (castN K 3)) (K.mul (K.mul (P4 K (n + 1)) (P4 K (n + 1))) (Bf K inv (E0 K a N) h T (n + 1))))
            (K.add (two K (K.mul (K.mul (P2 K n) (P2 K n)) (K.mul (Bf K inv (E0 K a N) h T n) (Bf K inv (E0 K a N) h T n))))
              (two K (K.mul (K.mul (P2 K (n + 1)) (P2 K (n + 1)))
                (K.mul (Bf K inv (E0 K a N) h T (n + 1)) (Bf K inv (E0 K a N) h T (n + 2))))))))))) := by
  have ⟨t, ht, hb⟩ := good_modes_q K O inv mul_inv h10 a h N hh hh0 h0 hz step T
  refine ⟨t, ht, fun n hn => ?_⟩
  let s : Nat → R := fun i => K.add (a (t + 1) i) (a t i)
  have hE : O.nn (E0 K a N) := sq_sum_nonneg K O.toCone (fun n => a 0 (n + 1)) N
  have hXnn : ∀ i, O.nn (X K h T i) := fun i =>
    O.nn_mul _ _ (nn_cast K O.toCone _) (O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3)
      (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (i + 1))))
  have hX0 : ∀ i, X K h T i ≠ K.zero := fun i =>
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (i + 1))))
  have hBnn : ∀ i, O.nn (Bf K inv (E0 K a N) h T i) := fun i => by
    cases i with
    | zero => exact hE
    | succ i => exact O.nn_mul _ _ hE (nn_inv' K O h10 _ _ (hXnn i) (mul_inv _ (hX0 i)))
  have zero_le : ∀ i, s i = K.zero → Le K O.toCone (K.mul (s i) (s i)) (Bf K inv (E0 K a N) h T i) := by
    intro i hi
    rw [hi, mul_zero K]; exact ⟨_, hBnn i, K.zero_add _⟩
  have sB : ∀ i, Le K O.toCone (K.mul (s i) (s i)) (Bf K inv (E0 K a N) h T i) := by
    intro i
    cases i with
    | zero => exact zero_le 0 (by show K.add (a (t + 1) 0) (a t 0) = K.zero; rw [h0, h0, K.zero_add])
    | succ i =>
      cases Nat.lt_or_ge i N with
      | inl l => exact hb i l
      | inr g => exact zero_le (i + 1) (by
          show K.add (a (t + 1) (i + 1)) (a t (i + 1)) = K.zero
          rw [hz (t + 1) (i + 1) (Nat.succ_le_succ g), hz t (i + 1) (Nat.succ_le_succ g), K.zero_add])
  have rc := recover K O.toCone (a t (n + 1)) (a (t + 1) (n + 1)) (P4 K (n + 1))
    (dyN K (P2 K) s n) h (castN K 3) (step t n)
  apply le_trans' K O.toCone _ _ _ rc
  have dn := dyN_sq K O.toCone (P2 K) s n _ _ _ (hBnn (n + 2)) (sB n) (sB (n + 1)) (sB (n + 2)) (hBnn n)
  have kk := le_two_mul K O.toCone (K.mul (castN K 3) (castN K 3)) _ _ (O.nn_sq _)
    (le_two_mul K O.toCone (K.mul (P4 K (n + 1)) (P4 K (n + 1))) _ _ (O.nn_sq _) (sB (n + 1)))
  have inner := le_add' K O.toCone _ _ _ _ kk dn
  have hh2 := le_two_mul K O.toCone (K.mul h h) _ _ (O.nn_sq h) inner
  have tw : ∀ x y, Le K O.toCone x y → Le K O.toCone (two K x) (two K y) := fun x y l => le_add' K O.toCone _ _ _ _ l l
  exact le_add' K O.toCone _ _ _ _ (tw _ _ (sB (n + 1))) (tw _ _ (tw _ _ hh2))

end GoodState

#print axioms GoodState.good_state

#print axioms: 1 theorem, no axioms.

79.118 Exact decay of the good-time bounds

The midpoint bounds \(B(i)=E_0X_{i-1}^{-1}\) drop by exactly \(4\) per shell. \(4B(n+2)=B(n+1)\), \(4^nB(n+1)=B(1)\).

ns/BDecay.lean

/-
  ns/BDecay.lean — T37 cycle 767 (goal G478): the good-time midpoint bounds of GoodState decay exactly by 4 per
  shell: B(n+1) = 4·B(n+2), hence 4ⁿ·B(n+1) = B(1).

  Lean 4 core plus ns/GoodState (X, Bf), ns/GoodModesQ (mul_ne, cast_succ_ne, pw_ne), lib/RingTac.
    inv_unique   x·y = 1, x·z = 1 ⇒ y = z
    x_succ       X(n+1) = X(n)·4
    b_step       4·B(n+2) = B(n+1)
    b_pow        4ⁿ·B(n+1) = B(1)
-/
import ns.GoodState

namespace BDecay
open DetChain NSChain DyadicSmallGlobal LibRing RatModel GoodModesQ GoodState

section Gen
variable {R : Type} (K : CRing R)

theorem inv_unique (x y z : R) (hy : K.mul x y = K.one) (hz : K.mul x z = K.one) : y = z := by
  have : y = K.mul (K.mul x z) y := by rw [hz]; lib_ring
  rw [this, show K.mul (K.mul x z) y = K.mul (K.mul x y) z by lib_ring, hy]; lib_ring

theorem x_succ (h : R) (T n : Nat) : X K h T (n + 1) = K.mul (X K h T n) (castN K 4) := by
  unfold X
  show K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (castN K 4))))
    = K.mul (K.mul (castN K (T + 1)) (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))) (castN K 4)
  lib_ring

theorem b_step (O : OrdR K) (inv : R → R) (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one)
    (h10 : K.one ≠ K.zero) (E h : R) (hh0 : h ≠ K.zero) (T n : Nat) :
    K.mul (castN K 4) (Bf K inv E h T (n + 2)) = Bf K inv E h T (n + 1) := by
  have hX0 : ∀ i, X K h T i ≠ K.zero := fun i =>
    mul_ne K O _ _ (cast_succ_ne K O h10 T) (mul_ne K O _ _ hh0 (mul_ne K O _ _ (cast_succ_ne K O h10 2)
      (pw_ne K O h10 _ (cast_succ_ne K O h10 3) (i + 1))))
  show K.mul (castN K 4) (K.mul E (inv (X K h T (n + 1)))) = K.mul E (inv (X K h T n))
  have h1 := mul_inv _ (hX0 (n + 1))
  rw [x_succ] at h1
  have h2 : K.mul (X K h T n) (K.mul (castN K 4) (inv (K.mul (X K h T n) (castN K 4)))) = K.one := by
    rw [← h1]; lib_ring
  have e := inv_unique K _ _ _ h2 (mul_inv _ (hX0 n))
  rw [x_succ, ← e]; lib_ring

theorem b_pow (O : OrdR K) (inv : R → R) (mul_inv : ∀ x, x ≠ K.zero → K.mul x (inv x) = K.one)
    (h10 : K.one ≠ K.zero) (E h : R) (hh0 : h ≠ K.zero) (T : Nat) :
    ∀ n, K.mul (P4 K n) (Bf K inv E h T (n + 1)) = Bf K inv E h T 1
  | 0 => by show K.mul K.one (Bf K inv E h T 1) = Bf K inv E h T 1; lib_ring
  | n + 1 => by
    have ih := b_pow O inv mul_inv h10 E h hh0 T n
    show K.mul (K.mul (P4 K n) (castN K 4)) (Bf K inv E h T (n + 2)) = Bf K inv E h T 1
    rw [show K.mul (K.mul (P4 K n) (castN K 4)) (Bf K inv E h T (n + 2))
        = K.mul (P4 K n) (K.mul (castN K 4) (Bf K inv E h T (n + 2))) by lib_ring,
      b_step K O inv mul_inv h10 E h hh0 T n, ih]

end Gen

end BDecay

#print axioms BDecay.inv_unique
#print axioms BDecay.x_succ
#print axioms BDecay.b_step
#print axioms BDecay.b_pow

#print axioms: 4 theorems, no axioms.

79.119 One mode of the linear Crank–Nicolson step

With \(x=h\nu k\ge 0\): \((1+x)a'=(1-x)a\), the solution is unique and damped, over an ordered ring without zero divisors. \(a'-a=-x(a'+a)\Rightarrow a'^2\le a^2\).

ns/StokesCN.lean

/-
  ns/StokesCN.lean — T37 cycle 770 (goal G481): one mode of the linear (Stokes) Crank–Nicolson step, over any
  ordered ring with no zero divisors (ℚ included). With x = hνk ≥ 0 and a' − a = −x(a' + a):
    lin_form     (1 + x)·a' = (1 − x)·a
    lin_unique   two solutions a', b' from the same a coincide (1 + x ≠ 0)
    lin_damp     a'² ≤ a²     ((1 + x)² a'² = (1 − x)² a² and (1 − x)² ≤ (1 + x)²)
  The damping factor (1 − x)/(1 + x) ∈ [−1, 1]; G478b (the nonlinear step) is the perturbation of this.

  Lean 4 core plus ns/GoodModesQ (ne_of_one_add, mul_ne, one_eq_zero_of), ns/RatModelU (le_mul_rg),
  lib/RingTac (lib_ring).
-/
import ns.GoodModesQ

namespace StokesCN
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModelU

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

omit O in
theorem lin_form (a a' x : R) (step : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a))) :
    K.mul (K.add K.one x) a' = K.mul (K.add K.one (K.neg x)) a := by
  have : K.mul (K.add K.one x) a' = K.add (K.add (K.add a' (K.neg a)) (K.mul x (K.add a' a)))
      (K.mul (K.add K.one (K.neg x)) a) := by lib_ring
  rw [this, step]; lib_ring

theorem lin_unique (h10 : K.one ≠ K.zero) (a a' b' x : R) (hx : O.nn x)
    (s1 : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a)))
    (s2 : K.add b' (K.neg a) = K.neg (K.mul x (K.add b' a))) : a' = b' := by
  have e1 := lin_form K a a' x s1
  have e2 := lin_form K a b' x s2
  have d : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.zero := by
    have : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.add (K.mul (K.add K.one x) a')
        (K.neg (K.mul (K.add K.one x) b')) := by lib_ring
    rw [this, e1, e2]; lib_ring
  cases O.noZD _ _ d with
  | inl h => exact absurd h (ne_of_one_add K O h10 x hx)
  | inr h =>
    have : a' = K.add (K.add a' (K.neg b')) b' := by lib_ring
    rw [this, h]; lib_ring

theorem lin_damp (h10 : K.one ≠ K.zero) (a a' x : R) (hx : O.nn x)
    (step : K.add a' (K.neg a) = K.neg (K.mul x (K.add a' a))) :
    Le K O.toCone (K.mul a' a') (K.mul a a) := by
  have e := lin_form K a a' x step
  -- (1+x)² (a² − a'²) = ((1+x)² − (1−x)²) a² = 4x a² ≥ 0
  have key : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add (K.mul a a) (K.neg (K.mul a' a')))
      = K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a) := by
    have : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
        = K.mul (K.mul (K.add K.one (K.neg x)) (K.add K.one (K.neg x))) (K.mul a a) := by
      rw [show K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
          = K.mul (K.mul (K.add K.one x) a') (K.mul (K.add K.one x) a') by lib_ring, e]; lib_ring
    rw [show K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add (K.mul a a) (K.neg (K.mul a' a')))
        = K.add (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a a))
          (K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a'))) by lib_ring, this]
    lib_ring
  -- so D = a² − a'² satisfies (1+x)²·D ≥ 0 with (1+x)² > 0, hence D ≥ 0
  have hR : O.nn (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)) :=
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)) (O.nn_sq a)
  generalize hD : K.add (K.mul a a) (K.neg (K.mul a' a')) = D at key
  have hD' : O.nn D := by
    cases O.total K.zero D with
    | inl l => cases l with
      | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
    | inr l => cases l with
      | intro d hd =>
        -- D + d = 0, d ≥ 0, and (1+x)²·D ≥ 0 ⇒ (1+x)²·d ≤ 0 ⇒ (1+x)²·d = 0 ⇒ d = 0 ⇒ D = 0
        have dD : D = K.neg d := by
          have : D = K.add (K.add D d) (K.neg d) := by lib_ring
          rw [this, hd.2]; lib_ring
        have q0 : O.nn (K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d)) := by
          rw [show K.neg (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d)
              = K.mul (K.mul (K.add K.one x) (K.add K.one x)) D by rw [dD]; lib_ring, key]; exact hR
        have q1 : O.nn (K.mul (K.mul (K.add K.one x) (K.add K.one x)) d) := O.nn_mul _ _ (O.nn_sq _) hd.1
        have z : K.mul (K.mul (K.add K.one x) (K.add K.one x)) d = K.zero :=
          O.antisym _ _ ⟨_, q0, by lib_ring⟩ ⟨_, q1, K.zero_add _⟩
        have d0 : d = K.zero := by
          cases O.noZD _ _ z with
          | inl h => exact absurd h (mul_ne K O _ _ (ne_of_one_add K O h10 x hx) (ne_of_one_add K O h10 x hx))
          | inr h => exact h
        rw [dD, d0]; show O.nn (K.neg K.zero)
        rw [show K.neg K.zero = K.zero by lib_ring]; exact O.nn_zero
  exact ⟨D, hD', by rw [← hD]; lib_ring⟩

end Gen

end StokesCN

#print axioms StokesCN.lin_form
#print axioms StokesCN.lin_unique
#print axioms StokesCN.lin_damp

#print axioms: 3 theorems, no axioms.

79.120 Uniqueness of the Crank–Nicolson step for small \(h\)

Two solutions of one CN step coincide when the nonlinearity is one-sided Lipschitz on the pair and \(hL\le 1-c\), \(c>0\). \(\sum d_n(N^a_n-N^b_n)\le L\sum d_n^2\Rightarrow d=a'-b'=0\).

ns/CNUnique.lean

/-
  ns/CNUnique.lean — T37 cycle 773 (goal G484): uniqueness of a Crank–Nicolson Galerkin step for small h, over any
  ordered ring without zero divisors (ℚ included), when the nonlinearity is one-sided Lipschitz on the pair:
  if a', b' both solve the step from a (with nonlinear terms Na, Nb), d = a' − b', and
    Σ_{n<N} d_n (Na_n − Nb_n) ≤ L·Σ d_n²,   h·L ≤ 1 − c  (c ≥ 0, c ≠ 0),   h, ν, k_n ≥ 0,
  then a'_n = b'_n for every n < N.

  Lean 4 core plus ns/GoodModesQ (mul_ne), ns/DyadicModelMode (term_le), ns/RatModelU (le_mul_rg),
  ns/DyadicLarge (le_add', le_mul_nn), lib/RingTac (lib_ring).
    diff_step    d_n = h(−ν k_n d_n + (Na_n − Nb_n))
    sq_zero'     x·x = 0 ⇒ x = 0
    cn_unique    the statement
-/
import ns.GoodModesQ
import ns.DyadicModelMode

namespace CNUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModelU DyadicModelMode

section Gen
variable {R : Type} (K : CRing R)

theorem diff_step (a a' b' k Na Nb h nu : R)
    (s1 : K.add a' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))
    (s2 : K.add b' (K.neg a) = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb)) :
    K.add a' (K.neg b') = K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' (K.neg b'))))) (K.add Na (K.neg Nb))) := by
  have z1 : K.add (K.add a' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))) = K.zero := by
    rw [s1]; lib_ring
  have z2 : K.add (K.add b' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb))) = K.zero := by
    rw [s2]; lib_ring
  have t : K.add a' (K.neg b') = K.add (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' (K.neg b'))))) (K.add Na (K.neg Nb))))
      (K.add (K.add (K.add a' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add a' a)))) Na))))
        (K.neg (K.add (K.add b' (K.neg a)) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul k (K.add b' a)))) Nb)))))) := by
    lib_ring
  exact t.trans (by rw [z1, z2]; lib_ring)

variable (O : OrdR K)
include O

theorem sq_zero' (x : R) (h : K.mul x x = K.zero) : x = K.zero := by
  cases O.noZD x x h with
  | inl e => exact e
  | inr e => exact e

theorem sum_sq_zero (f : Nat → R) (N : Nat) (h : sumN K (fun n => K.mul (f n) (f n)) N = K.zero) :
    ∀ n, n < N → f n = K.zero := by
  intro n hn
  have l := term_le K O.toCone (fun n => K.mul (f n) (f n)) (fun n => O.nn_sq _) n N hn
  rw [h] at l
  apply sq_zero' K O
  exact O.antisym _ _ l ⟨_, O.nn_sq (f n), K.zero_add _⟩

theorem cn_unique (a a' b' k Na Nb : Nat → R) (h nu L c : R) (N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hc : O.nn c) (hc0 : c ≠ K.zero)
    (s1 : ∀ n, K.add (a' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Na n)))
    (s2 : ∀ n, K.add (b' n) (K.neg (a n)) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (b' n) (a n))))) (Nb n)))
    (lip : Le K O.toCone (sumN K (fun n => K.mul (K.add (a' n) (K.neg (b' n))) (K.add (Na n) (K.neg (Nb n)))) N)
      (K.mul L (sumN K (fun n => K.mul (K.add (a' n) (K.neg (b' n))) (K.add (a' n) (K.neg (b' n)))) N)))
    (small : Le K O.toCone (K.mul h L) (K.add K.one (K.neg c))) :
    ∀ n, n < N → a' n = b' n := by
  let d : Nat → R := fun n => K.add (a' n) (K.neg (b' n))
  let D : Nat → R := fun n => K.add (Na n) (K.neg (Nb n))
  have ds : ∀ n, d n = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (d n)))) (D n)) := fun n =>
    diff_step K (a n) (a' n) (b' n) (k n) (Na n) (Nb n) h nu (s1 n) (s2 n)
  -- Σ d² + hν Σ k d² = h Σ d D
  have ident : ∀ M, K.add (sumN K (fun n => K.mul (d n) (d n)) M)
      (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M)))
      = K.mul h (sumN K (fun n => K.mul (d n) (D n)) M) := by
    intro M
    induction M with
    | zero => show K.add K.zero (K.mul h (K.mul nu K.zero)) = K.mul h K.zero; lib_ring
    | succ M ih =>
      show K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M) (K.mul (d M) (d M)))
          (K.mul h (K.mul nu (K.add (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M) (K.mul (k M) (K.mul (d M) (d M))))))
        = K.mul h (K.add (sumN K (fun n => K.mul (d n) (D n)) M) (K.mul (d M) (D M)))
      have z : K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M)))) = K.zero := by
        have := ds M
        exact (show K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M))))
          = K.add (d M) (K.neg (d M)) from by rw [← this]).trans (by lib_ring)
      have e : K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))
          = K.mul h (K.mul (d M) (D M)) := by
        have t : K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))
            = K.add (K.mul h (K.mul (d M) (D M)))
              (K.mul (d M) (K.add (d M) (K.neg (K.mul h (K.add (K.neg (K.mul nu (K.mul (k M) (d M)))) (D M)))))) := by
          lib_ring
        exact t.trans (by rw [z]; lib_ring)
      have r : K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M) (K.mul (d M) (d M)))
          (K.mul h (K.mul nu (K.add (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M) (K.mul (k M) (K.mul (d M) (d M))))))
          = K.add (K.add (sumN K (fun n => K.mul (d n) (d n)) M)
              (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) M))))
            (K.add (K.mul (d M) (d M)) (K.mul h (K.mul nu (K.mul (k M) (K.mul (d M) (d M)))))) := by lib_ring
      rw [r, ih, e]
      lib_ring
  generalize hS : sumN K (fun n => K.mul (d n) (d n)) N = S
  have hSnn : O.nn S := by rw [← hS]; exact sumN_nn K O.toCone _ (fun n => O.nn_sq _) N
  have hP : O.nn (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (d n) (d n))) N))) :=
    O.nn_mul _ _ hh (O.nn_mul _ _ hnu (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (hk n) (O.nn_sq _)) N))
  have id2 := ident N
  rw [hS] at id2 lip
  -- S ≤ S + P = h Σ dD ≤ h L S ≤ (1 − c) S
  have l1 : Le K O.toCone S (K.mul h (sumN K (fun n => K.mul (d n) (D n)) N)) := ⟨_, hP, id2⟩
  have l2 := le_mul_nn K O.toCone h _ _ hh lip
  have l3 : Le K O.toCone (K.mul h (K.mul L S)) (K.mul (K.add K.one (K.neg c)) S) := by
    rw [show K.mul h (K.mul L S) = K.mul (K.mul h L) S by lib_ring]
    exact le_mul_rg K O.toCone S _ _ hSnn small
  have l4 := le_trans' K O.toCone _ _ _ l1 (le_trans' K O.toCone _ _ _ l2 l3)
  -- c·S ≤ 0 and c·S ≥ 0 ⇒ c·S = 0 ⇒ S = 0
  have cS : K.mul c S = K.zero := by
    cases l4 with
    | intro e he =>
      apply O.antisym
      · exact ⟨e, he.1, by
          have : K.add (K.mul c S) e = K.add (K.add S e) (K.neg (K.mul (K.add K.one (K.neg c)) S)) := by lib_ring
          rw [this, he.2]; lib_ring⟩
      · exact ⟨_, O.nn_mul _ _ hc hSnn, K.zero_add _⟩
  have S0 : S = K.zero := by
    cases O.noZD _ _ cS with
    | inl e => exact absurd e hc0
    | inr e => exact e
  intro n hn
  have := sum_sq_zero K O d N (by rw [hS, S0]) n hn
  have : a' n = K.add (d n) (b' n) := by show a' n = K.add (K.add (a' n) (K.neg (b' n))) (b' n); lib_ring
  rw [this, show d n = K.zero from sum_sq_zero K O d N (by rw [hS, S0]) n hn]; lib_ring

end Gen

end CNUnique

#print axioms CNUnique.diff_step
#print axioms CNUnique.sq_zero'
#print axioms CNUnique.sum_sq_zero
#print axioms CNUnique.cn_unique

#print axioms: 4 theorems, no axioms.

79.121 Young’s inequality with a bounded coefficient

No absolute values: over an ordered ring without zero divisors, a coefficient bounded in square is enough. \(M\ge 0,\ M\ne 0,\ X^2\le M^2\Rightarrow 2xyX\le M(x^2+y^2)\).

ns/YoungM.lean

/-
  ns/YoungM.lean — T37 cycle 776 (goal G487): Young's inequality with a bounded coefficient and no absolute values,
  over any ordered ring without zero divisors (ℚ included):
    M ≥ 0, M ≠ 0, X² ≤ M²  ⇒  2·x·y·X ≤ M·(x² + y²).
  First piece of G484b (the dyadic nonlinearity is one-sided Lipschitz under mode bounds).

  Lean 4 core plus ns/StokesCN (pattern), ns/GoodModesQ, lib/RingTac.
    pos_cancel   M ≥ 0, M ≠ 0, M·Z ≥ 0 ⇒ Z ≥ 0
    young_m      the statement
-/
import ns.StokesCN

namespace YoungM
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem pos_cancel (M Z : R) (hM : O.nn M) (hM0 : M ≠ K.zero) (h : O.nn (K.mul M Z)) : O.nn Z := by
  cases O.total K.zero Z with
  | inl l => cases l with
    | intro d hd => rw [← hd.2, K.zero_add]; exact hd.1
  | inr l => cases l with
    | intro d hd =>
      have dZ : Z = K.neg d := by
        have : Z = K.add (K.add Z d) (K.neg d) := by lib_ring
        rw [this, hd.2]; lib_ring
      have q0 : O.nn (K.neg (K.mul M d)) := by
        rw [show K.neg (K.mul M d) = K.mul M Z by rw [dZ]; lib_ring]; exact h
      have q1 : O.nn (K.mul M d) := O.nn_mul _ _ hM hd.1
      have z : K.mul M d = K.zero := O.antisym _ _ ⟨_, q0, by lib_ring⟩ ⟨_, q1, K.zero_add _⟩
      have d0 : d = K.zero := by
        cases O.noZD _ _ z with
        | inl e => exact absurd e hM0
        | inr e => exact e
      rw [dZ, d0, show K.neg K.zero = K.zero by lib_ring]; exact O.nn_zero

theorem young_m (M X x y : R) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (hX : Le K O.toCone (K.mul X X) (K.mul M M)) :
    Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X)) (K.mul M (K.add (K.mul x x) (K.mul y y))) := by
  cases hX with
  | intro e he =>
    -- M·(M(x²+y²) − 2xyX) = (Mx − Xy)² + (M² − X²) y² = (Mx − Xy)² + e·y²
    let Z := K.add (K.mul M (K.add (K.mul x x) (K.mul y y))) (K.neg (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X)))
    have key : K.mul M Z = K.add (K.mul (K.add (K.mul M x) (K.neg (K.mul X y))) (K.add (K.mul M x) (K.neg (K.mul X y))))
        (K.mul e (K.mul y y)) := by
      have : e = K.add (K.mul M M) (K.neg (K.mul X X)) := by
        have : e = K.add (K.add (K.mul X X) e) (K.neg (K.mul X X)) := by lib_ring
        rw [this, he.2]
      show K.mul M (K.add (K.mul M (K.add (K.mul x x) (K.mul y y))) (K.neg (K.mul (K.add K.one K.one) (K.mul (K.mul x y) X))))
        = _
      rw [this]; lib_ring
    have hZ : O.nn Z := pos_cancel K O M Z hM hM0 (by rw [key]; exact O.nn_add _ _ (O.nn_sq _) (O.nn_mul _ _ he.1 (O.nn_sq _)))
    exact ⟨Z, hZ, by show K.add _ (K.add _ _) = _; lib_ring⟩

end Gen

end YoungM

#print axioms YoungM.pos_cancel
#print axioms YoungM.young_m

#print axioms: 2 theorems, no axioms.

79.122 The dyadic nonlinearity is one-sided Lipschitz (pointwise)

With \(d=s-r\) and the three coefficients bounded in square by \(M^2\), each mode’s contribution is controlled. \(2d_{n+1}(\mathrm {dyN}(s)_n-\mathrm {dyN}(r)_n)\le M(d_n^2+d_{n+1}^2)+2Md_{n+1}^2+M(d_{n+1}^2+d_{n+2}^2)\).

ns/DyLip.lean

/-
  ns/DyLip.lean — T37 cycle 779 (goal G490 = G484b, pointwise part): the dyadic nonlinearity is one-sided Lipschitz
  mode by mode under coefficient bounds, over any ordered ring without zero divisors (ℚ included). With d = s − r,
  X1 = c_n(s_n + r_n), X2 = c_{n+1}s_{n+2}, X3 = c_{n+1}r_{n+1} and X_i² ≤ M²:
    2·d_{n+1}·(dyN(c,s)_n − dyN(c,r)_n) ≤ M(d_n² + d_{n+1}²) + 2M·d_{n+1}² + M(d_{n+1}² + d_{n+2}²).

  Lean 4 core plus ns/YoungM (young_m), ns/DyadicLarge (le_add'), NSChain (dyN), lib/RingTac.
    dy_diff      dyN(c,s)_n − dyN(c,r)_n = X1·d_n − X2·d_{n+1} − X3·d_{n+2}       (exact, d = s − r)
    dy_lip_pt    the inequality above
-/
import ns.YoungM

namespace DyLip
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM

section Gen
variable {R : Type} (K : CRing R)

theorem dy_diff (c s r : Nat → R) (n : Nat) :
    K.add (dyN K c s n) (K.neg (dyN K c r n))
      = K.add (K.add (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.add (s n) (K.neg (r n))))
          (K.neg (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))))
        (K.neg (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.add (s (n + 2)) (K.neg (r (n + 2)))))) := by
  unfold dyN; lib_ring

variable (O : OrdR K)
include O

theorem dy_lip_pt (c s r : Nat → R) (n : Nat) (M : R) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (h1 : Le K O.toCone (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.mul (c n) (K.add (s n) (r n)))) (K.mul M M))
    (h2 : Le K O.toCone (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.mul (c (n + 1)) (s (n + 2)))) (K.mul M M))
    (h3 : Le K O.toCone (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.mul (c (n + 1)) (r (n + 1)))) (K.mul M M)) :
    Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1))))
        (K.add (dyN K c s n) (K.neg (dyN K c r n)))))
      (K.add (K.add (K.mul M (K.add (K.mul (K.add (s n) (K.neg (r n))) (K.add (s n) (K.neg (r n))))
                                    (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))))
                     (K.mul M (K.add (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))
                                     (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1))))))))
        (K.mul M (K.add (K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1)))))
                        (K.mul (K.add (s (n + 2)) (K.neg (r (n + 2)))) (K.add (s (n + 2)) (K.neg (r (n + 2)))))))) := by
  rw [dy_diff]
  generalize K.add (s n) (K.neg (r n)) = d0
  generalize K.add (s (n + 1)) (K.neg (r (n + 1))) = d1
  generalize K.add (s (n + 2)) (K.neg (r (n + 2))) = d2
  generalize hX1 : K.mul (c n) (K.add (s n) (r n)) = X1 at h1
  generalize hX2 : K.mul (c (n + 1)) (s (n + 2)) = X2 at h2
  generalize hX3 : K.mul (c (n + 1)) (r (n + 1)) = X3 at h3
  have n2 : Le K O.toCone (K.mul (K.neg X2) (K.neg X2)) (K.mul M M) := by
    rw [show K.mul (K.neg X2) (K.neg X2) = K.mul X2 X2 by lib_ring]; exact h2
  have n3 : Le K O.toCone (K.mul (K.neg X3) (K.neg X3)) (K.mul M M) := by
    rw [show K.mul (K.neg X3) (K.neg X3) = K.mul X3 X3 by lib_ring]; exact h3
  have y1 := young_m K O M X1 d0 d1 hM hM0 h1
  have y2 := young_m K O M (K.neg X2) d1 d1 hM hM0 n2
  have y3 := young_m K O M (K.neg X3) d1 d2 hM hM0 n3
  have s := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ y1 y2) y3
  have e : K.mul (K.add K.one K.one) (K.mul d1 (K.add (K.add (K.mul X1 d0) (K.neg (K.mul X2 d1))) (K.neg (K.mul X3 d2))))
      = K.add (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul d0 d1) X1))
          (K.mul (K.add K.one K.one) (K.mul (K.mul d1 d1) (K.neg X2))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul d1 d2) (K.neg X3))) := by lib_ring
  rw [e]; exact s

end Gen

end DyLip

#print axioms DyLip.dy_diff
#print axioms DyLip.dy_lip_pt

#print axioms: 2 theorems, no axioms.

79.123 Uniqueness of the dyadic Crank–Nicolson step

Two solutions of one dyadic CN step from the same state coincide when the midpoint coefficients are bounded by \(M\) and \(3hM\le 1-c\). \(\sum d_{n+1}(\mathrm {dyN}(s)_n-\mathrm {dyN}(r)_n)\le 3M\sum d_{n+1}^2\) (boundary shifts), then the energy argument.

ns/DyUnique.lean

/-
  ns/DyUnique.lean — T37 cycle 783 (goal G494 = G490b): uniqueness of one Crank–Nicolson step of the dyadic model
  for small h, over any ordered ring without zero divisors (ℚ included). If a', b' both solve the step from a
  (modes 1..N, a(0) = 0, zero beyond N), the midpoints s = a' + a, r = b' + a have coefficient bounds
  (c_n(s_n + r_n))², (c_{n+1}s_{n+2})², (c_{n+1}r_{n+1})² ≤ M² (M > 0) and h·3M ≤ 1 − c (c > 0), then a' = b'.

  Lean 4 core plus ns/DyLip (dy_lip_pt), ns/CNUnique (cn_unique), ns/YoungM (pos_cancel), lib/RingTac.
    tele_r       Σ_{n<N} f n + f N = f 0 + Σ_{n<N} f (n+1)
    shift_le     d_0 = 0 ⇒ Σ_{n<N} d_n² ≤ Σ_{n<N} d_{n+1}²;   d_{N+1} = 0 ⇒ Σ_{n<N} d_{n+2}² ≤ Σ_{n<N} d_{n+1}²
    dy_lip       Σ_{n<N} d_{n+1}(dyN(s)_n − dyN(r)_n) ≤ 3M·Σ_{n<N} d_{n+1}²
    dy_unique    the statement
-/
import ns.DyLip
import ns.CNUnique

namespace DyUnique
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM DyLip CNUnique GoodModesQ RatModel

section Gen
variable {R : Type} (K : CRing R)

theorem tele_r (f : Nat → R) : ∀ N, K.add (sumN K f N) (f N) = K.add (f 0) (sumN K (fun n => f (n + 1)) N)
  | 0 => by show K.add K.zero (f 0) = K.add (f 0) K.zero; lib_ring
  | N + 1 => by
    show K.add (K.add (sumN K f N) (f N)) (f (N + 1)) = K.add (f 0) (K.add (sumN K (fun n => f (n + 1)) N) (f (N + 1)))
    rw [tele_r f N]; lib_ring

variable (O : OrdR K)
include O

theorem shift_lo (d : Nat → R) (h0 : d 0 = K.zero) (N : Nat) :
    Le K O.toCone (sumN K (fun n => K.mul (d n) (d n)) N) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) := by
  have t : K.add (sumN K (fun n => K.mul (d n) (d n)) N) (K.mul (d N) (d N))
      = K.add (K.mul (d 0) (d 0)) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) :=
    tele_r K (fun n => K.mul (d n) (d n)) N
  rw [h0] at t
  exact ⟨_, O.nn_sq (d N), t.trans (by lib_ring)⟩

theorem shift_hi (d : Nat → R) (N : Nat) (hN : d (N + 1) = K.zero) :
    Le K O.toCone (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) := by
  have t : K.add (sumN K (fun n => K.mul (d (n + 1)) (d (n + 1))) N) (K.mul (d (N + 1)) (d (N + 1)))
      = K.add (K.mul (d 1) (d 1)) (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) :=
    tele_r K (fun n => K.mul (d (n + 1)) (d (n + 1))) N
  rw [hN] at t
  exact ⟨_, O.nn_sq (d 1), (show K.add (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) (K.mul (d 1) (d 1))
      = K.add (K.mul (d 1) (d 1)) (sumN K (fun n => K.mul (d (n + 2)) (d (n + 2))) N) by lib_ring).trans
    (t.symm.trans (by lib_ring))⟩

theorem dy_lip (c s r : Nat → R) (N : Nat) (M : R) (hM : O.nn M) (hM0 : M ≠ K.zero) (h10 : K.one ≠ K.zero)
    (hd0 : K.add (s 0) (K.neg (r 0)) = K.zero) (hdN : K.add (s (N + 1)) (K.neg (r (N + 1))) = K.zero)
    (h1 : ∀ n, Le K O.toCone (K.mul (K.mul (c n) (K.add (s n) (r n))) (K.mul (c n) (K.add (s n) (r n)))) (K.mul M M))
    (h2 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (s (n + 2))) (K.mul (c (n + 1)) (s (n + 2)))) (K.mul M M))
    (h3 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (r (n + 1))) (K.mul (c (n + 1)) (r (n + 1)))) (K.mul M M)) :
    Le K O.toCone
      (sumN K (fun n => K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)
      (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M
        (sumN K (fun n => K.mul (K.add (s (n + 1)) (K.neg (r (n + 1)))) (K.add (s (n + 1)) (K.neg (r (n + 1))))) N))) := by
  let d : Nat → R := fun n => K.add (s n) (K.neg (r n))
  let q : Nat → R := fun n => K.mul (d n) (d n)
  show Le K O.toCone (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)
    (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))
  have pt := fun n => dy_lip_pt K O c s r n M hM hM0 (h1 n) (h2 n) (h3 n)
  have sm := sum_le K O.toCone _ _ pt N
  -- Σ 2·d(n+1)ΔN ≤ M(A + 4B + C) ≤ 6M·B
  have hA := shift_lo K O d hd0 N
  have hC := shift_hi K O d N hdN
  -- rewrite both sums as linear combinations
  have eL : sumN K (fun n => K.mul (K.add K.one K.one) (K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n))))) N
      = K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N) :=
    sumN_mul_left K _ _ N
  have eR : ∀ N', sumN K (fun n => K.add (K.add (K.mul M (K.add (q n) (q (n + 1)))) (K.mul M (K.add (q (n + 1)) (q (n + 1)))))
        (K.mul M (K.add (q (n + 1)) (q (n + 2))))) N'
      = K.mul M (K.add (K.add (sumN K q N') (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N')))
          (sumN K (fun n => q (n + 2)) N')) := by
    intro N'
    induction N' with
    | zero => show K.zero = K.mul M (K.add (K.add K.zero (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) K.zero)) K.zero); lib_ring
    | succ N' ih =>
      show K.add (sumN K (fun n => K.add (K.add (K.mul M (K.add (q n) (q (n + 1)))) (K.mul M (K.add (q (n + 1)) (q (n + 1)))))
          (K.mul M (K.add (q (n + 1)) (q (n + 2))))) N')
        (K.add (K.add (K.mul M (K.add (q N') (q (N' + 1)))) (K.mul M (K.add (q (N' + 1)) (q (N' + 1)))))
          (K.mul M (K.add (q (N' + 1)) (q (N' + 2))))) = K.mul M (K.add (K.add (K.add (sumN K q N') (q N'))
        (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (sumN K (fun n => q (n + 1)) N') (q (N' + 1)))))
        (K.add (sumN K (fun n => q (n + 2)) N') (q (N' + 2))))
      rw [ih]; lib_ring
  have sm' := sm
  rw [eL, eR] at sm'
  -- bound A + 4B + C ≤ 6B
  have hAB : Le K O.toCone (K.add (K.add (sumN K q N) (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))
      (sumN K (fun n => q (n + 2)) N))
      (K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N)) := by
    have := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ hA (le_refl' K O.toCone
      (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))) hC
    have e6 : K.add (K.add (sumN K (fun n => q (n + 1)) N) (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (sumN K (fun n => q (n + 1)) N)))
        (sumN K (fun n => q (n + 1)) N)
        = K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N) := by lib_ring
    rw [e6] at this; exact this
  have l2 := le_trans' K O.toCone _ _ _ sm' (le_mul_nn K O.toCone M _ _ hM hAB)
  -- 2·S ≤ 2·(3·(M·B))  ⇒  S ≤ 3·(M·B)
  have h2ne : K.add K.one K.one ≠ K.zero := ne_of_one_add K O h10 K.one (nn_one' K O.toCone)
  have l3 : Le K O.toCone (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))
      (K.mul (K.add K.one K.one) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))) := by
    have e : K.mul M (K.mul (K.add (K.add K.one (K.add K.one K.one)) (K.add K.one (K.add K.one K.one))) (sumN K (fun n => q (n + 1)) N))
        = K.mul (K.add K.one K.one) (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N))) := by lib_ring
    rw [← e]; exact l2
  cases l3 with
  | intro e he =>
    have he2 : e = K.mul (K.add K.one K.one) (K.add (K.mul (K.add K.one (K.add K.one K.one)) (K.mul M (sumN K (fun n => q (n + 1)) N)))
        (K.neg (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))) := by
      have : e = K.add (K.add (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N)) e)
          (K.neg (K.mul (K.add K.one K.one) (sumN K (fun n => K.mul (d (n + 1)) (K.add (dyN K c s n) (K.neg (dyN K c r n)))) N))) := by lib_ring
      rw [this, he.2]; lib_ring
    have hnn := pos_cancel K O (K.add K.one K.one) _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) h2ne
      (by rw [← he2]; exact he.1)
    exact ⟨_, hnn, by lib_ring⟩

omit O in
theorem sumN_ext_g (f g : Nat → R) : ∀ N, (∀ n, n < N → f n = g n) → sumN K f N = sumN K g N
  | 0, _ => rfl
  | N + 1, h => by
    show K.add (sumN K f N) (f N) = K.add (sumN K g N) (g N)
    rw [sumN_ext_g f g N (fun n hn => h n (Nat.lt_succ_of_lt hn)), h N (Nat.lt_succ_self N)]

theorem dy_unique (c k : Nat → R) (a a' b' : Nat → R) (h nu M cc : R) (N : Nat)
    (hh : O.nn h) (hnu : O.nn nu) (hk : ∀ n, O.nn (k n)) (hM : O.nn M) (hM0 : M ≠ K.zero)
    (hcc : O.nn cc) (hcc0 : cc ≠ K.zero) (h10 : K.one ≠ K.zero)
    (a0' : a' 0 = K.zero) (b0' : b' 0 = K.zero) (aN' : a' (N + 1) = K.zero) (bN' : b' (N + 1) = K.zero)
    (s1 : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1))) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1))
      (K.add (a' (n + 1)) (a (n + 1)))))) (dyN K c (fun i => K.add (a' i) (a i)) n)))
    (s2 : ∀ n, K.add (b' (n + 1)) (K.neg (a (n + 1))) = K.mul h (K.add (K.neg (K.mul nu (K.mul (k (n + 1))
      (K.add (b' (n + 1)) (a (n + 1)))))) (dyN K c (fun i => K.add (b' i) (a i)) n)))
    (h1 : ∀ n, Le K O.toCone (K.mul (K.mul (c n) (K.add (K.add (a' n) (a n)) (K.add (b' n) (a n))))
      (K.mul (c n) (K.add (K.add (a' n) (a n)) (K.add (b' n) (a n))))) (K.mul M M))
    (h2 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (K.add (a' (n + 2)) (a (n + 2))))
      (K.mul (c (n + 1)) (K.add (a' (n + 2)) (a (n + 2))))) (K.mul M M))
    (h3 : ∀ n, Le K O.toCone (K.mul (K.mul (c (n + 1)) (K.add (b' (n + 1)) (a (n + 1))))
      (K.mul (c (n + 1)) (K.add (b' (n + 1)) (a (n + 1))))) (K.mul M M))
    (small : Le K O.toCone (K.mul h (K.mul (K.add K.one (K.add K.one K.one)) M)) (K.add K.one (K.neg cc))) :
    ∀ n, n < N → a' (n + 1) = b' (n + 1) := by
  let sA : Nat → R := fun i => K.add (a' i) (a i)
  let sB : Nat → R := fun i => K.add (b' i) (a i)
  have lp := dy_lip K O c sA sB N M hM hM0 h10
    (by show K.add (K.add (a' 0) (a 0)) (K.neg (K.add (b' 0) (a 0))) = K.zero; rw [a0', b0']; lib_ring)
    (by show K.add (K.add (a' (N + 1)) (a (N + 1))) (K.neg (K.add (b' (N + 1)) (a (N + 1)))) = K.zero
        rw [aN', bN']; lib_ring) h1 h2 h3
  have e1 := sumN_ext_g K (fun n => K.mul (K.add (sA (n + 1)) (K.neg (sB (n + 1)))) (K.add (dyN K c sA n) (K.neg (dyN K c sB n))))
    (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (dyN K c sA n) (K.neg (dyN K c sB n)))) N
    (fun n _ => by show K.mul (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1))))) _ = _; lib_ring)
  have e2 := sumN_ext_g K (fun n => K.mul (K.add (sA (n + 1)) (K.neg (sB (n + 1)))) (K.add (sA (n + 1)) (K.neg (sB (n + 1)))))
    (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N
    (fun n _ => by
      show K.mul (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1)))))
        (K.add (K.add (a' (n + 1)) (a (n + 1))) (K.neg (K.add (b' (n + 1)) (a (n + 1))))) = _; lib_ring)
  rw [e1, e2] at lp
  have lp' : Le K O.toCone (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1))))
      (K.add (dyN K c sA n) (K.neg (dyN K c sB n)))) N)
      (K.mul (K.mul (K.add K.one (K.add K.one K.one)) M)
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)) := by
    rw [show K.mul (K.mul (K.add K.one (K.add K.one K.one)) M)
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)
      = K.mul (K.add K.one (K.add K.one K.one)) (K.mul M
        (sumN K (fun n => K.mul (K.add (a' (n + 1)) (K.neg (b' (n + 1)))) (K.add (a' (n + 1)) (K.neg (b' (n + 1))))) N)) by lib_ring]
    exact lp
  exact cn_unique K O (fun n => a (n + 1)) (fun n => a' (n + 1)) (fun n => b' (n + 1)) (fun n => k (n + 1))
    (fun n => dyN K c sA n) (fun n => dyN K c sB n) h nu (K.mul (K.add K.one (K.add K.one K.one)) M) cc N
    hh hnu (fun n => hk (n + 1)) hcc hcc0 s1 s2 lp' small

end Gen

end DyUnique

#print axioms DyUnique.tele_r
#print axioms DyUnique.shift_lo
#print axioms DyUnique.shift_hi
#print axioms DyUnique.dy_lip
#print axioms DyUnique.sumN_ext_g
#print axioms DyUnique.dy_unique

#print axioms: 6 theorems, no axioms.

79.124 The semi-implicit step: existence and uniqueness over \(\mathbb Q\)

Dissipation implicit, forcing explicit: the next mode is given by a formula over any ordered field. \(a'-a=-x(a'+a)+hf\iff a'=((1-x)a+hf)(1+x)^{-1}\).

ns/ForcedCN.lean

/-
  ns/ForcedCN.lean — T37 cycle 786 (goal G497): one mode of the linear Crank–Nicolson step with a given forcing f
  (the semi-implicit scheme: dissipation implicit, nonlinearity explicit), over an ordered field (ℚ included):
    a' − a = −x(a' + a) + h f,   x = hνk ≥ 0
  has the solution a' = ((1 − x)a + h f)·(1 + x)⁻¹ and no other. So the semi-implicit dyadic scheme always has a
  unique next state over ℚ (the implicit CN step needs roots of a quadratic system).

  Lean 4 core plus ns/StokesCN, ns/GoodModesQ (ne_of_one_add), lib/RingTac.
    forced_exist   the formula solves the step
    forced_unique  any two solutions coincide
-/
import ns.StokesCN

namespace ForcedCN
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem forced_exist (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (a x hf : R) (hx : O.nn x) :
    let a' := K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x))
    K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf := by
  intro a'
  have hi := mul_inv _ (ne_of_one_add K O h10 x hx)
  -- (1+x)a' = (1−x)a + hf, then rearrange
  have e : K.mul (K.add K.one x) a' = K.add (K.mul (K.add K.one (K.neg x)) a) hf := by
    show K.mul (K.add K.one x) (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x))) = _
    have : K.mul (K.add K.one x) (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (inv (K.add K.one x)))
        = K.mul (K.mul (K.add K.one x) (inv (K.add K.one x))) (K.add (K.mul (K.add K.one (K.neg x)) a) hf) := by lib_ring
    rw [this, hi]; lib_ring
  have t : K.add a' (K.neg a) = K.add (K.add (K.neg (K.mul x (K.add a' a))) hf)
      (K.add (K.mul (K.add K.one x) a') (K.neg (K.add (K.mul (K.add K.one (K.neg x)) a) hf))) := by lib_ring
  exact t.trans (by rw [e]; lib_ring)

theorem forced_unique (h10 : K.one ≠ K.zero) (a a' b' x hf : R) (hx : O.nn x)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf)
    (s2 : K.add b' (K.neg a) = K.add (K.neg (K.mul x (K.add b' a))) hf) : a' = b' := by
  have d : K.mul (K.add K.one x) (K.add a' (K.neg b')) = K.zero := by
    have t : K.mul (K.add K.one x) (K.add a' (K.neg b'))
        = K.add (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hf)))
          (K.neg (K.add (K.add b' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add b' a))) hf)))) := by lib_ring
    exact t.trans (by rw [s1, s2]; lib_ring)
  cases O.noZD _ _ d with
  | inl e => exact absurd e (ne_of_one_add K O h10 x hx)
  | inr e =>
    have : a' = K.add (K.add a' (K.neg b')) b' := by lib_ring
    rw [this, e]; lib_ring

end Gen

end ForcedCN

#print axioms ForcedCN.forced_exist
#print axioms ForcedCN.forced_unique

#print axioms: 2 theorems, no axioms.

79.125 Energy identity of the semi-implicit scheme

Exact, over any commutative ring, with neutrality of the nonlinearity at the old state. \(E'+\sum x_n(a'_n+a_n)^2=E+h\sum (a'_n-a_n)N_n\).

ns/SemiEnergy.lean

/-
  ns/SemiEnergy.lean — T37 cycle 789 (goal G500): the exact energy identity of the semi-implicit scheme
  (dissipation implicit, nonlinearity explicit) over any commutative ring:
    a'_n − a_n = −x_n (a'_n + a_n) + h N_n,   Σ_{n<N} a_n N_n = 0  (neutrality at the old state)
  ⇒ E' + Σ x_n (a'_n + a_n)² = E + h Σ (a'_n − a_n) N_n.
  The defect h Σ (a' − a) N is O(h²) (a' − a = O(h)); with it bounded, the large-data argument carries over.

  Lean 4 core plus NSChain (sumN), lib/RingTac (lib_ring).
    step_energy   one mode:  a'² − a² + x(a'+a)² = h (a' − a) N + 2 h a N
    semi_energy   the summed identity
-/
import NSChain
import lib.RingTac

namespace SemiEnergy
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

theorem step_energy (a a' x hN : R) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hN) :
    K.add (K.mul a' a') (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      = K.add (K.add (K.mul a a) (K.mul (K.add a' (K.neg a)) hN)) (K.add (K.mul a hN) (K.mul a hN)) := by
  -- (a' − a)(a' + a) = −x s² + hN·s, and hN·s = hN(a' − a) + 2 a hN
  have t : K.add (K.mul a' a') (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      = K.add (K.add (K.mul a a) (K.mul (K.add a' (K.neg a)) (K.add a' a)))
        (K.mul x (K.mul (K.add a' a) (K.add a' a))) := by lib_ring
  have z : K.mul (K.add a' (K.neg a)) (K.add a' a)
      = K.add (K.neg (K.mul x (K.mul (K.add a' a) (K.add a' a)))) (K.mul hN (K.add a' a)) := by
    have u : K.mul (K.add a' (K.neg a)) (K.add a' a)
        = K.add (K.mul (K.add (K.neg (K.mul x (K.add a' a))) hN) (K.add a' a))
          (K.mul (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hN))) (K.add a' a)) := by lib_ring
    exact u.trans (by rw [st]; lib_ring)
  rw [t, z]
  have w : K.mul hN (K.add a' a) = K.add (K.mul (K.add a' (K.neg a)) hN) (K.add (K.mul a hN) (K.mul a hN)) := by lib_ring
  rw [w]; lib_ring

theorem semi_energy (a a' x Nn : Nat → R) (h : R)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n)))
    (N : Nat) (neutral : sumN K (fun n => K.mul (a n) (Nn n)) N = K.zero) :
    K.add (sumN K (fun n => K.mul (a' n) (a' n)) N) (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) N)
      = K.add (sumN K (fun n => K.mul (a n) (a n)) N)
          (K.mul h (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) N)) := by
  have gen : ∀ M, K.add (sumN K (fun n => K.mul (a' n) (a' n)) M)
      (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
      = K.add (K.add (sumN K (fun n => K.mul (a n) (a n)) M)
          (K.mul h (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) M)))
        (K.mul (K.add h h) (sumN K (fun n => K.mul (a n) (Nn n)) M)) := by
    intro M
    induction M with
    | zero => show K.add K.zero K.zero = K.add (K.add K.zero (K.mul h K.zero)) (K.mul (K.add h h) K.zero); lib_ring
    | succ M ih =>
      have e := step_energy K (a M) (a' M) (x M) (K.mul h (Nn M)) (st M)
      show K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M) (K.mul (a' M) (a' M)))
          (K.add (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
            (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M)))))
        = K.add (K.add (K.add (sumN K (fun n => K.mul (a n) (a n)) M) (K.mul (a M) (a M)))
            (K.mul h (K.add (sumN K (fun n => K.mul (K.add (a' n) (K.neg (a n))) (Nn n)) M)
              (K.mul (K.add (a' M) (K.neg (a M))) (Nn M)))))
          (K.mul (K.add h h) (K.add (sumN K (fun n => K.mul (a n) (Nn n)) M) (K.mul (a M) (Nn M))))
      have r : K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M) (K.mul (a' M) (a' M)))
          (K.add (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M)
            (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M)))))
          = K.add (K.add (sumN K (fun n => K.mul (a' n) (a' n)) M)
              (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) M))
            (K.add (K.mul (a' M) (a' M)) (K.mul (x M) (K.mul (K.add (a' M) (a M)) (K.add (a' M) (a M))))) := by lib_ring
      rw [r, ih, e]; lib_ring
  rw [gen, neutral]; lib_ring

end Gen

end SemiEnergy

#print axioms SemiEnergy.step_energy
#print axioms SemiEnergy.semi_energy

#print axioms: 2 theorems, no axioms.

79.126 Energy inequality of the semi-implicit scheme

Completing the square \(x(s+hN)^2\ge 0\) per mode; the growth per step is second order. \(2E'+\sum x_ns_n^2\le 2E+h^2\sum (x_n+2)N_n^2\).

ns/SemiBound.lean

/-
  ns/SemiBound.lean — T37 cycle 792 (goal G503): the energy inequality of the semi-implicit scheme over any ring
  with a positive cone (ℚ included): with a'_n − a_n = −x_n s_n + h N_n (s = a' + a), x_n ≥ 0 and Σ a N = 0,
    2E' + Σ x_n s_n² ≤ 2E + h² Σ (x_n + 2) N_n².
  The growth per step is O(h²)·Σ(x+2)N²: second order, as for CN, but with an explicit next state (ForcedCN).

  Lean 4 core plus ns/SemiEnergy (semi_energy), ns/DyadicLarge (le_add', sum_le), lib/RingTac.
    mode_bound   2(a'² − a²) + x s² ≤ h²(x + 2)N² + 4h·a·N      (completing the square x(s + hN)² ≥ 0)
    semi_bound   the summed inequality
-/
import ns.SemiEnergy
import ns.DyadicLarge

namespace SemiBound
open DetChain NSChain LibRing DyadicLarge SemiEnergy

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def two (y : R) : R := K.add y y

theorem mode_bound (a a' x hN0 h : R) (hx : P.nn x)
    (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0)) :
    Le K P (K.add (two K (K.mul a' a')) (K.mul x (K.mul (K.add a' a) (K.add a' a))))
      (K.add (two K (K.mul a a)) (K.add (K.mul (K.mul h h) (K.mul (K.add x (K.add K.one K.one)) (K.mul hN0 hN0)))
        (two K (two K (K.mul h (K.mul a hN0)))))) := by
  -- witness: x (s + hN)²
  refine ⟨K.mul x (K.mul (K.add (K.add a' a) (K.mul h hN0)) (K.add (K.add a' a) (K.mul h hN0))),
    P.nn_mul _ _ hx (P.nn_sq _), ?_⟩
  -- use a' = a − x s + hN to eliminate a'² via the step
  have z : K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0))) = K.zero := by
    rw [st]; lib_ring
  have t : K.add (K.add (two K (K.mul a' a')) (K.mul x (K.mul (K.add a' a) (K.add a' a))))
        (K.mul x (K.mul (K.add (K.add a' a) (K.mul h hN0)) (K.add (K.add a' a) (K.mul h hN0))))
      = K.add (K.add (two K (K.mul a a)) (K.add (K.mul (K.mul h h) (K.mul (K.add x (K.add K.one K.one)) (K.mul hN0 hN0)))
          (two K (two K (K.mul h (K.mul a hN0))))))
        (K.mul (K.add (two K (K.add a' a)) (two K (K.mul h hN0)))
          (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h hN0))))) := by
    unfold two; lib_ring
  exact t.trans (by rw [z]; lib_ring)

theorem two_sum (f : Nat → R) : ∀ M, sumN K (fun n => two K (f n)) M = two K (sumN K f M)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | M + 1 => by
    show K.add (sumN K (fun n => two K (f n)) M) (two K (f M)) = two K (K.add (sumN K f M) (f M))
    rw [two_sum f M]; unfold two; lib_ring

theorem semi_bound (a a' x Nn : Nat → R) (h : R) (N : Nat) (hx : ∀ n, P.nn (x n))
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n)))
    (neutral : sumN K (fun n => K.mul (a n) (Nn n)) N = K.zero) :
    Le K P (K.add (two K (sumN K (fun n => K.mul (a' n) (a' n)) N))
        (sumN K (fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) N))
      (K.add (two K (sumN K (fun n => K.mul (a n) (a n)) N))
        (K.mul (K.mul h h) (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn n) (Nn n))) N))) := by
  have sm := sum_le K P _ _ (fun n => mode_bound K P (a n) (a' n) (x n) (Nn n) h (hx n) (st n)) N
  let A' : Nat → R := fun n => K.mul (a' n) (a' n)
  let A : Nat → R := fun n => K.mul (a n) (a n)
  let D : Nat → R := fun n => K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))
  let G : Nat → R := fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn n) (Nn n))
  let C : Nat → R := fun n => K.mul (a n) (Nn n)
  have L : sumN K (fun n => K.add (two K (A' n)) (D n)) N = K.add (two K (sumN K A' N)) (sumN K D N) := by
    rw [sumN_add, two_sum]
  have Rr : sumN K (fun n => K.add (two K (A n)) (K.add (K.mul (K.mul h h) (G n)) (two K (two K (K.mul h (C n)))))) N
      = K.add (K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N))) (two K (two K (K.mul h (sumN K C N)))) := by
    rw [sumN_add, sumN_add, two_sum, sumN_mul_left, two_sum, two_sum, sumN_mul_left]; lib_ring
  have sm' : Le K P (sumN K (fun n => K.add (two K (A' n)) (D n)) N)
      (sumN K (fun n => K.add (two K (A n)) (K.add (K.mul (K.mul h h) (G n)) (two K (two K (K.mul h (C n)))))) N) := sm
  rw [L, Rr] at sm'
  have hC : sumN K C N = K.zero := neutral
  rw [hC] at sm'
  have e : K.add (K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N))) (two K (two K (K.mul h K.zero)))
      = K.add (two K (sumN K A N)) (K.mul (K.mul h h) (sumN K G N)) := by unfold two; lib_ring
  rw [e] at sm'; exact sm'

end Gen

end SemiBound

#print axioms SemiBound.mode_bound
#print axioms SemiBound.two_sum
#print axioms SemiBound.semi_bound

#print axioms: 3 theorems, no axioms.

79.127 A discrete Riccati horizon

\(e_{t+1}\le e_t+Ke_t^2\), \(4TKe_0\le 1\Rightarrow e_t\le e_0+4tKe_0^2\le 2e_0\) for \(t\le T\), over any ordered ring. The closing step for the semi-implicit energy inequality once \(N^2\) is bounded by the energy.

ns/RiccatiH.lean

/-
  ns/RiccatiH.lean — T37 cycle 795 (goal G506): a discrete Riccati horizon over any ordered ring (ℚ included):
    0 ≤ e_t,  e_{t+1} ≤ e_t + K·e_t²,  K ≥ 0,  4·T·K·e_0 ≤ 1   ⇒   e_t ≤ e_0 + 4tK e_0² ≤ 2 e_0   for t ≤ T.
  For the semi-implicit scheme (SemiBound: E' ≤ E + h²C E² after bounding N² by the energy) this gives
  E_t ≤ 2E_0 on horizons t·h ≤ 1/(4hC E_0) — growing like 1/h as h → 0.

  Lean 4 core plus ns/DyNBound (mul_le_mul'), ns/RatModel (castN lemmas), ns/DyadicLarge, lib/RingTac.
    sq_le         0 ≤ x ≤ y ⇒ x² ≤ y²
    ric_step      the invariant e_t ≤ e_0 + 4tK e_0²
    ric_horizon   e_t ≤ 2e_0 for t ≤ T
-/
import ns.DyNBound
import ns.RatModel

namespace RiccatiH
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge DyNBound RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem sq_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : Le K O.toCone (K.mul x x) (K.mul y y) := by
  have hy : O.nn y := by cases h with | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
  exact mul_le_mul' K O.toCone x y x y hx hy h h

theorem ric_step (e : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (rec : ∀ t, Le K O.toCone (e (t + 1)) (K.add (e t) (K.mul Kc (K.mul (e t) (e t)))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (e t) (K.add (e 0) (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))))) := by
  intro t
  induction t with
  | zero => intro _; exact ⟨_, O.nn_mul _ _ (nn_cast K O.toCone 0) (O.nn_mul _ _ hK (O.nn_sq _)), rfl⟩
  | succ t ih =>
    intro ht
    have ih' := ih (Nat.le_of_succ_le ht)
    -- e_t ≤ 2 e_0
    have q : Le K O.toCone (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0)))) (e 0) := by
      have c1 : Le K O.toCone (castN K (4 * t)) (castN K (4 * T)) := cast_mono K O.toCone _ _ (Nat.mul_le_mul_left 4 (Nat.le_of_succ_le ht))
      have c2 := le_mul_rg' (castN K (4 * t)) (castN K (4 * T)) (K.mul Kc (e 0)) (O.nn_mul _ _ hK (he 0)) c1
      have c3 := le_trans' K O.toCone _ _ _ c2 small
      have c4 := le_mul_rg' _ _ (e 0) (he 0) c3
      rw [show K.mul (K.mul (castN K (4 * t)) (K.mul Kc (e 0))) (e 0)
          = K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))) by lib_ring,
        show K.mul K.one (e 0) = e 0 by lib_ring] at c4
      exact c4
    have two : Le K O.toCone (e t) (K.add (e 0) (e 0)) :=
      le_trans' K O.toCone _ _ _ ih' (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) q)
    have sq := sq_le K O _ _ (he t) two
    have r := rec t
    have s1 := le_add' K O.toCone _ _ _ _ ih' (le_mul_nn K O.toCone Kc _ _ hK sq)
    apply le_trans' K O.toCone _ _ _ r
    apply le_trans' K O.toCone _ _ _ s1
    refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (e 0) (K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0)))))
        (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0))))) K.zero
      = K.add (e 0) (K.mul (castN K (4 * t + 4)) (K.mul Kc (K.mul (e 0) (e 0))))
    rw [cast_add K (4 * t) 4]
    show _ = K.add (e 0) (K.mul (K.add (castN K (4 * t)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))))
      (K.mul Kc (K.mul (e 0) (e 0))))
    lib_ring
where
  le_mul_rg' (x y c : R) (hc : O.nn c) (h : Le K O.toCone x y) : Le K O.toCone (K.mul x c) (K.mul y c) := by
    cases h with
    | intro d hd => exact ⟨K.mul d c, O.nn_mul _ _ hd.1 hc, by rw [← hd.2]; lib_ring⟩

theorem ric_horizon (e : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (rec : ∀ t, Le K O.toCone (e (t + 1)) (K.add (e t) (K.mul Kc (K.mul (e t) (e t)))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (e t) (K.add (e 0) (e 0)) := by
  intro t ht
  have st := ric_step K O e Kc T hK he rec small t ht
  have c1 : Le K O.toCone (castN K (4 * t)) (castN K (4 * T)) := cast_mono K O.toCone _ _ (Nat.mul_le_mul_left 4 ht)
  have c2 := ric_step.le_mul_rg' K O (castN K (4 * t)) (castN K (4 * T)) (K.mul Kc (e 0)) (O.nn_mul _ _ hK (he 0)) c1
  have c3 := le_trans' K O.toCone _ _ _ c2 small
  have c4 := ric_step.le_mul_rg' K O _ _ (e 0) (he 0) c3
  rw [show K.mul (K.mul (castN K (4 * t)) (K.mul Kc (e 0))) (e 0)
      = K.mul (castN K (4 * t)) (K.mul Kc (K.mul (e 0) (e 0))) by lib_ring,
    show K.mul K.one (e 0) = e 0 by lib_ring] at c4
  exact le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) c4)

end Gen

end RiccatiH

#print axioms RiccatiH.sq_le
#print axioms RiccatiH.ric_step
#print axioms RiccatiH.ric_horizon

#print axioms: 3 theorems, no axioms.

79.128 Global bound for the semi-implicit scheme

If the nonlinearity is controlled by the energy, \(\sum (x_n+2)N_n^2\le CE^2\), then \(E_t\le 2E_0\) for \(t\le T\) with \(4Th^2CE_0\le 1\). The time horizon \(hT\) grows like \(1/(hCE_0)\) as \(h\to 0\).

ns/SemiGlobal.lean

/-
  ns/SemiGlobal.lean — T37 cycle 797 (goal G509): a global bound for the semi-implicit scheme over any ordered ring
  without zero divisors (ℚ included). If every step satisfies
    a_{t+1}(n) − a_t(n) = −x_n (a_{t+1}(n) + a_t(n)) + h N_t(n),  x_n ≥ 0,  Σ a_t N_t = 0,
    Σ_n (x_n + 2) N_t(n)² ≤ C·E_t²                     (nonlinearity controlled by the energy),
  and 4T·h²C·E_0 ≤ 1, then E_t ≤ 2E_0 for every t ≤ T. (For fixed truncation N the dyadic N satisfies the
  nonlinearity hypothesis with C = C_N by DyNBound and mode ≤ energy.)

  Lean 4 core plus ns/SemiBound (semi_bound), ns/RiccatiH (ric_horizon), ns/YoungM (pos_cancel), lib/RingTac.
    halve        2x ≤ 2y + w, w ≥ 0 ⇒ x ≤ y + w
    semi_global  the statement
-/
import ns.SemiBound
import ns.RiccatiH
import ns.YoungM

namespace SemiGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiBound RiccatiH YoungM GoodModesQ RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem halve (h10 : K.one ≠ K.zero) (x y w : R) (hw : O.nn w)
    (h : Le K O.toCone (K.add x x) (K.add (K.add y y) w)) : Le K O.toCone x (K.add y w) := by
  cases h with
  | intro d hd =>
    -- 2(y + w − x) = d + w ≥ 0
    have e : K.mul (K.add K.one K.one) (K.add (K.add y w) (K.neg x)) = K.add d w := by
      have t : K.mul (K.add K.one K.one) (K.add (K.add y w) (K.neg x))
          = K.add (K.add d w) (K.add (K.add (K.add y y) w) (K.neg (K.add (K.add x x) d))) := by lib_ring
      exact t.trans (by rw [hd.2]; lib_ring)
    have hnn := pos_cancel K O (K.add K.one K.one) _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone))
      (ne_of_one_add K O h10 K.one (nn_one' K O.toCone)) (by rw [e]; exact O.nn_add _ _ hd.1 hw)
    exact ⟨_, hnn, by lib_ring⟩

theorem semi_global (h10 : K.one ≠ K.zero) (a : Nat → Nat → R) (x : Nat → R) (Nn : Nat → Nat → R)
    (h C : R) (N T : Nat) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (st : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.add (K.neg (K.mul (x n) (K.add (a (t + 1) n) (a t n))))
      (K.mul h (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (a t n) (Nn t n)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)))
      K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (a t n) (a t n)) N)
      (K.add (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) := by
  let E : Nat → R := fun t => sumN K (fun n => K.mul (a t n) (a t n)) N
  have hE : ∀ t, O.nn (E t) := fun t => sq_sum_nonneg K O.toCone (a t) N
  have hrec : ∀ t, Le K O.toCone (E (t + 1)) (K.add (E t) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t)))) := by
    intro t
    have sb := semi_bound K O.toCone (a t) (a (t + 1)) x (Nn t) h N hx (st t) (neutral t)
    -- drop the dissipation and bound the nonlinearity
    have d0 : Le K O.toCone (K.add (E (t + 1)) (E (t + 1)))
        (K.add (two K (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N))
          (sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N)) :=
      ⟨_, sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (hx n) (O.nn_sq _)) N, rfl⟩
    have d1 := le_trans' K O.toCone _ _ _ d0 sb
    have d2 := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (nb t)
    have d3 := le_trans' K O.toCone _ _ _ d1 (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) d2)
    have w0 : O.nn (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t))) :=
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq h) hC) (O.nn_sq _)
    apply halve K O h10 _ _ _ w0
    have e : K.add (two K (E t)) (K.mul (K.mul h h) (K.mul C (K.mul (E t) (E t))))
        = K.add (K.add (E t) (E t)) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t))) := by unfold two; lib_ring
    have d4 : Le K O.toCone (K.add (E (t + 1)) (E (t + 1)))
        (K.add (K.add (E t) (E t)) (K.mul (K.mul (K.mul h h) C) (K.mul (E t) (E t)))) := by
      rw [← e]; exact d3
    exact d4
  exact ric_horizon K O E (K.mul (K.mul h h) C) T (O.nn_mul _ _ (O.nn_sq h) hC) hE hrec small

end Gen

end SemiGlobal

#print axioms SemiGlobal.halve
#print axioms SemiGlobal.semi_global

#print axioms: 2 theorems, no axioms.

79.129 The dyadic nonlinearity at fixed truncation

\(\sum _{n<N}(x_n+2)\,\mathrm {dyN}(c,b)_n^2\le C_N E^2\), \(C_N=\sum _{n<N}(x_n+2)(2c_n^2+2c_{n+1}^2)\): the hypothesis of the semi-implicit global bound. Every mode is \(\le E\) (boundary zeros outside \(1..N\)), then DyNBound.

ns/DyCN.lean

/-
  ns/DyCN.lean — T37 cycle 800 (goal G512 = G509b): for a fixed truncation N the dyadic nonlinearity is controlled
  by the energy, over any ring with a positive cone (ℚ included): with b_0 = 0, b_{N+1} = 0, E = Σ_{n<N} b_{n+1}²,
    Σ_{n<N} (x_n + 2)·dyN(c, b)_n² ≤ C_N·E²,   C_N = Σ_{n<N} (x_n + 2)·(2c_n² + 2c_{n+1}²).
  This is the hypothesis of SemiGlobal, so the semi-implicit dyadic scheme is energy-stable on horizons
  4T·h²·C_N·E_0 ≤ 1.

  Lean 4 core plus ns/DyNBound (dyN_sq), ns/SemiBound (two), ns/DyUnique (sumN_ext_g), ns/RatModel (nn_one'),
  NSChain (mode_le_energy, sumN_mul_left).
    mode_E      b_i² ≤ E for every i (inside by mode_le_energy, outside 0)
    dy_cn       the statement
-/
import ns.DyNBound
import ns.SemiBound
import ns.DyUnique

namespace DyCN
open DetChain NSChain LibRing DyadicLarge DyNBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem mode_E (b : Nat → R) (N : Nat) (h0 : b 0 = K.zero) (hN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    ∀ i, Le K P (K.mul (b i) (b i)) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := by
  have hE : P.nn (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := sq_sum_nonneg K P (fun n => b (n + 1)) N
  have z : ∀ i, b i = K.zero → Le K P (K.mul (b i) (b i)) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) := by
    intro i hi; rw [hi, mul_zero K]; exact ⟨_, hE, K.zero_add _⟩
  intro i
  cases i with
  | zero => exact z 0 h0
  | succ j =>
    cases Nat.lt_or_ge j N with
    | inl l => exact mode_le_energy K P (fun n => b (n + 1)) N j l
    | inr g => exact z (j + 1) (hN (j + 1) (Nat.succ_le_succ g))

theorem dy_cn (b c x : Nat → R) (N : Nat) (hx : ∀ n, P.nn (x n)) (h0 : b 0 = K.zero)
    (hN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    Le K P (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (dyN K c b n) (dyN K c b n))) N)
      (K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N)
        (K.mul (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N))) := by
  generalize hEd : sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N = E
  have mE := mode_E K P b N h0 hN
  rw [hEd] at mE
  have hEn : P.nn E := by rw [← hEd]; exact sq_sum_nonneg K P (fun n => b (n + 1)) N
  have pt : ∀ n, Le K P (K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (dyN K c b n) (dyN K c b n)))
      (K.mul (K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) (K.mul E E)) := by
    intro n
    have d := dyN_sq K P c b n E E E hEn (mE n) (mE (n + 1)) (mE (n + 2)) hEn
    have d' := le_mul_nn K P (K.add (x n) (K.add K.one K.one)) _ _
      (P.nn_add _ _ (hx n) (P.nn_add _ _ (RatModel.nn_one' K P) (RatModel.nn_one' K P))) d
    apply le_trans' K P _ _ _ d'
    refine ⟨K.zero, P.nn_zero, ?_⟩
    unfold MidRecover.two SemiBound.two; lib_ring
  have sm := sum_le K P _ _ pt N
  have e : sumN K (fun n => K.mul (K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) (K.mul E E)) N
      = K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N) (K.mul E E) := by
    rw [K.mul_comm (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
        (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N) (K.mul E E),
      ← sumN_mul_left]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  rw [e] at sm; exact sm

end Gen

end DyCN

#print axioms DyCN.mode_E
#print axioms DyCN.dy_cn

#print axioms: 2 theorems, no axioms.

79.130 The semi-implicit dyadic scheme over \(\mathbb Q\): solution and energy bound

An explicit solution of the semi-implicit dyadic scheme over any ordered field, and \(E_t\le 2E_0\) for \(t\le T\) when \(4Th^2C_NE_0\le 1\). \(a_{t+1}(j+1)=((1-x_j)a_t(j+1)+h\,\mathrm {dyN}(c,a_t)_j)(1+x_j)^{-1}\).

ns/SemiDyadic.lean

/-
  ns/SemiDyadic.lean — T37 cycle 803 (goal G515 = G512b): the semi-implicit dyadic scheme over an ordered field
  (ℚ included) — an explicit solution and its energy bound. For data b (b_0 = 0, b_i = 0 for i > N), x_j ≥ 0
  (x_j = hνk_{j+1}), the sequence
    a_0 = b,   a_{t+1}(j+1) = ((1 − x_j)·a_t(j+1) + h·dyN(c, a_t)_j)·(1 + x_j)⁻¹   (j < N),   0 elsewhere
  solves a_{t+1} − a_t = −x(a_{t+1} + a_t) + h·dyN(c, a_t) on the modes 1..N (ForcedCN), and if
  4T·h²·C_N·E_0 ≤ 1 (C_N of DyCN) then E_t ≤ 2E_0 for every t ≤ T (SemiGlobal).

  Lean 4 core plus ns/SemiGlobal, ns/DyCN, ns/ForcedCN, ns/DyUnique (sumN_ext_g), NSChain (dyadic_energy_neutral).
    sq           the sequence;  sq_zero0, sq_zeroN  boundary zeros;  sq_step  the scheme on modes 1..N
    semi_dyadic  the energy bound
-/
import ns.SemiGlobal
import ns.DyCN
import ns.ForcedCN

namespace SemiDyadic
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModel ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def upd (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) : Nat → R
  | 0 => K.zero
  | j + 1 => match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero

def sq (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) : Nat → Nat → R
  | 0 => b
  | t + 1 => upd K inv c x h N (sq inv c x h N b t)

theorem upd_in (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (j : Nat) (hj : j < N) :
    upd K inv c x h N a (j + 1)
      = K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j))) := by
  show (match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero) = _
  rw [Nat.ble_eq_true_of_le hj]

theorem upd_out (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (j : Nat) (hj : N ≤ j) :
    upd K inv c x h N a (j + 1) = K.zero := by
  show (match Nat.ble (j + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x j))) (a (j + 1))) (K.mul h (dyN K c a j))) (inv (K.add K.one (x j)))
    | false => K.zero) = _
  cases e : Nat.ble (j + 1) N with
  | false => rfl
  | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hj))

theorem sq_zero0 (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) (hb0 : b 0 = K.zero) :
    ∀ t, sq K inv c x h N b t 0 = K.zero
  | 0 => hb0
  | _ + 1 => rfl

theorem sq_zeroN (inv : R → R) (c x : Nat → R) (h : R) (N : Nat) (b : Nat → R) (hbN : ∀ i, N + 1 ≤ i → b i = K.zero) :
    ∀ t i, N + 1 ≤ i → sq K inv c x h N b t i = K.zero
  | 0, i, hi => hbN i hi
  | t + 1, i, hi => by
    cases i with
    | zero => rfl
    | succ j => exact upd_out K inv c x h N _ j (Nat.le_of_succ_le_succ hi)

end Gen

theorem semi_dyadic {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (c x : Nat → R) (h : R) (N T : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n))
    (hb0 : b 0 = K.zero) (hbN : ∀ i, N + 1 ≤ i → b i = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
          (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N))
        (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (sq K inv c x h N b t (n + 1)) (sq K inv c x h N b t (n + 1))) N)
      (K.add (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N) (sumN K (fun n => K.mul (b (n + 1)) (b (n + 1))) N)) := by
  let A : Nat → Nat → R := fun t n => sq K inv c x h N b t (n + 1)
  let Nn : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => dyN K c (sq K inv c x h N b t) n
    | false => K.zero
  let C := sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one))
    (K.add (SemiBound.two K (K.mul (c n) (c n))) (SemiBound.two K (K.mul (c (n + 1)) (c (n + 1)))))) N
  have hC : O.nn C := sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (O.nn_add _ _ (hx n)
    (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)))
    (O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)))) N
  have st : ∀ t n, K.add (A (t + 1) n) (K.neg (A t n)) = K.add (K.neg (K.mul (x n) (K.add (A (t + 1) n) (A t n))))
      (K.mul h (Nn t n)) := by
    intro t n
    show K.add (upd K inv c x h N (sq K inv c x h N b t) (n + 1)) (K.neg (sq K inv c x h N b t (n + 1)))
      = K.add (K.neg (K.mul (x n) (K.add (upd K inv c x h N (sq K inv c x h N b t) (n + 1)) (sq K inv c x h N b t (n + 1)))))
        (K.mul h (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero))
    cases Nat.lt_or_ge n N with
    | inl hn =>
      have m1 : (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero) = dyN K c (sq K inv c x h N b t) n := by rw [Nat.ble_eq_true_of_le hn]
      rw [m1, upd_in K inv c x h N _ n hn]
      exact forced_exist K O inv mul_inv h10 (sq K inv c x h N b t (n + 1)) (x n)
        (K.mul h (dyN K c (sq K inv c x h N b t) n)) (hx n)
    | inr hn =>
      have m0 : (match Nat.ble (n + 1) N with
          | true => dyN K c (sq K inv c x h N b t) n
          | false => K.zero) = K.zero := by
        cases e : Nat.ble (n + 1) N with
        | false => rfl
        | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
      have z : sq K inv c x h N b t (n + 1) = K.zero :=
        sq_zeroN K inv c x h N b hbN t (n + 1) (Nat.succ_le_succ hn)
      rw [m0, upd_out K inv c x h N _ n hn, z]; lib_ring
  have inner : ∀ t n, n < N → Nn t n = dyN K c (sq K inv c x h N b t) n := by
    intro t n hn
    show (match Nat.ble (n + 1) N with
      | true => dyN K c (sq K inv c x h N b t) n
      | false => K.zero) = _
    rw [Nat.ble_eq_true_of_le hn]
  have neutral : ∀ t, sumN K (fun n => K.mul (A t n) (Nn t n)) N = K.zero := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (sq K inv c x h N b t (n + 1)) (dyN K c (sq K inv c x h N b t) n)) N
      (fun n hn => by show K.mul (A t n) (Nn t n) = _; rw [inner t n hn])]
    exact dyadic_energy_neutral K c (sq K inv c x h N b t) N (sq_zero0 K inv c x h N b hb0 t)
      (sq_zeroN K inv c x h N b hbN t (N + 1) (Nat.le_refl _))
  have nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (A t n) (A t n)) N) (sumN K (fun n => K.mul (A t n) (A t n)) N))) := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (K.add (x n) (K.add K.one K.one))
      (K.mul (dyN K c (sq K inv c x h N b t) n) (dyN K c (sq K inv c x h N b t) n))) N
      (fun n hn => by show K.mul _ (K.mul (Nn t n) (Nn t n)) = _; rw [inner t n hn])]
    exact DyCN.dy_cn K O.toCone (sq K inv c x h N b t) c x N hx (sq_zero0 K inv c x h N b hb0 t)
      (sq_zeroN K inv c x h N b hbN t)
  exact SemiGlobal.semi_global K O h10 A x Nn h C N T hx hC st neutral nb small

end SemiDyadic

#print axioms SemiDyadic.upd_in
#print axioms SemiDyadic.upd_out
#print axioms SemiDyadic.sq_zero0
#print axioms SemiDyadic.sq_zeroN
#print axioms SemiDyadic.semi_dyadic

#print axioms: 5 theorems, no axioms.

79.131 Dissipation budget with a quadratic defect

\(2e_{t+1}+D_t\le 2e_t+Ke_t^2\), \(e_t\le 2e_0\), \(4TKe_0\le 1\Rightarrow \sum _{t<T}D_t\le 3e_0\), over any ordered ring. For the semi-implicit dyadic scheme: the discrete \(\nu \int \|\nabla u\|^2\) bound with an explicit next state.

ns/SemiBudget.lean

/-
  ns/SemiBudget.lean — T37 cycle 806 (goal G518): the dissipation budget of a scheme with a quadratic defect,
  over any ordered ring (ℚ included): if
    2e_{t+1} + D_t ≤ 2e_t + K e_t²,   e_t ≥ 0,   e_t ≤ 2e_0 (t < T),   4T·K·e_0 ≤ 1,
  then Σ_{t<T} D_t ≤ 3e_0. For the semi-implicit dyadic scheme (SemiBound + DyCN + SemiDyadic) this is
  Σ_t Σ_n hνk (a_{t+1} + a_t)² ≤ 3E_0 — the discrete ν∫‖∇u‖² bound with an explicit next state.

  Lean 4 core plus ns/RiccatiH (sq_le, castN lemmas), ns/DyadicLarge, lib/RingTac.
    tele_budget   2e_T + Σ_{t<T} D_t ≤ 2e_0 + Σ_{t<T} K e_t²
    budget3       Σ_{t<T} D_t ≤ 3e_0
-/
import ns.RiccatiH

namespace SemiBudget
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel RiccatiH

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem tele_budget (e D : Nat → R) (Kc : R)
    (st : ∀ t, Le K O.toCone (K.add (K.add (e (t + 1)) (e (t + 1))) (D t)) (K.add (K.add (e t) (e t)) (K.mul Kc (K.mul (e t) (e t))))) :
    ∀ T, Le K O.toCone (K.add (K.add (e T) (e T)) (sumN K D T))
      (K.add (K.add (e 0) (e 0)) (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) T))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.add (e 0) (e 0)) K.zero) K.zero = K.add (K.add (e 0) (e 0)) K.zero; lib_ring⟩
  | T + 1 => by
    have ih := tele_budget e D Kc st T
    have s := le_add' K O.toCone _ _ _ _ (st T) (le_refl' K O.toCone (sumN K D T))
    have s2 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul Kc (K.mul (e T) (e T))))
    -- combine: 2e_{T+1} + Σ_{T+1} D = (2e_{T+1} + D_T) + Σ_T D ≤ (2e_T + K e_T²) + Σ_T D ≤ 2e_0 + Σ_{T+1} K e²
    have e1 : K.add (K.add (e (T + 1)) (e (T + 1))) (K.add (sumN K D T) (D T))
        = K.add (K.add (K.add (e (T + 1)) (e (T + 1))) (D T)) (sumN K D T) := by lib_ring
    have e2 : K.add (K.add (K.add (e T) (e T)) (K.mul Kc (K.mul (e T) (e T)))) (sumN K D T)
        = K.add (K.add (K.add (e T) (e T)) (sumN K D T)) (K.mul Kc (K.mul (e T) (e T))) := by lib_ring
    show Le K O.toCone (K.add (K.add (e (T + 1)) (e (T + 1))) (K.add (sumN K D T) (D T)))
      (K.add (K.add (e 0) (e 0)) (K.add (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) T) (K.mul Kc (K.mul (e T) (e T)))))
    rw [e1]
    rw [e2] at s
    apply le_trans' K O.toCone _ _ _ s
    apply le_trans' K O.toCone _ _ _ s2
    exact ⟨K.zero, O.nn_zero, by lib_ring⟩

theorem budget3 (e D : Nat → R) (Kc : R) (T : Nat) (hK : O.nn Kc) (he : ∀ t, O.nn (e t))
    (st : ∀ t, Le K O.toCone (K.add (K.add (e (t + 1)) (e (t + 1))) (D t)) (K.add (K.add (e t) (e t)) (K.mul Kc (K.mul (e t) (e t)))))
    (b2 : ∀ t, t < T → Le K O.toCone (e t) (K.add (e 0) (e 0)))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) K.one) :
    Le K O.toCone (sumN K D T) (K.add (K.add (e 0) (e 0)) (e 0)) := by
  have tb := tele_budget K O e D Kc st T
  -- Σ_{t<T} K e_t² ≤ T·K·(2e_0)² = 4T K e_0² ≤ e_0
  have sq4 : ∀ t, t < T → Le K O.toCone (K.mul Kc (K.mul (e t) (e t))) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0)))) :=
    fun t ht => le_mul_nn K O.toCone Kc _ _ hK (sq_le K O _ _ (he t) (b2 t ht))
  have sumB : ∀ M, M ≤ T → Le K O.toCone (sumN K (fun t => K.mul Kc (K.mul (e t) (e t))) M)
      (K.mul (castN K M) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0))))) := by
    intro M
    induction M with
    | zero => intro _; exact ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero _; lib_ring⟩
    | succ M ih =>
      intro hM
      have a := le_add' K O.toCone _ _ _ _ (ih (Nat.le_of_succ_le hM)) (sq4 M (Nat.lt_of_succ_le hM))
      apply le_trans' K O.toCone _ _ _ a
      exact ⟨K.zero, O.nn_zero, by show _ = K.mul (K.add K.one (castN K M)) _; lib_ring⟩
  have sB := sumB T (Nat.le_refl T)
  -- T·K·4e_0² = (4T)·(K e_0)·e_0 ≤ 1·e_0
  have c1 : Le K O.toCone (K.mul (castN K T) (K.mul Kc (K.mul (K.add (e 0) (e 0)) (K.add (e 0) (e 0)))))
      (K.mul (K.mul (castN K (4 * T)) (K.mul Kc (e 0))) (e 0)) := by
    refine ⟨K.zero, O.nn_zero, ?_⟩
    have : castN K (4 * T) = K.mul (castN K 4) (castN K T) := by
      clear sB sumB sq4 tb small b2 st he
      induction T with
      | zero => show K.zero = K.mul _ K.zero; lib_ring
      | succ T ih =>
        rw [show 4 * (T + 1) = 4 * T + 4 by rfl, cast_add K (4 * T) 4, ih]
        show _ = K.mul (castN K 4) (K.add K.one (castN K T))
        show K.add (K.mul (castN K 4) (castN K T)) (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))))
          = K.mul (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) (K.add K.one (castN K T))
        lib_ring
    rw [this]
    show _ = K.mul (K.mul (K.mul (K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero)))) (castN K T)) (K.mul Kc (e 0))) (e 0)
    lib_ring
  have c2 := le_mul_rg_e (e 0) (he 0) small
  have tot := le_trans' K O.toCone _ _ _ sB (le_trans' K O.toCone _ _ _ c1 c2)
  have tb2 := le_trans' K O.toCone _ _ _ tb (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tot)
  -- drop 2e_T ≥ 0
  have hT : O.nn (K.add (e T) (e T)) := O.nn_add _ _ (he T) (he T)
  cases tb2 with
  | intro d hd =>
    refine ⟨K.add d (K.add (e T) (e T)), O.nn_add _ _ hd.1 hT, ?_⟩
    have : K.add (sumN K D T) (K.add d (K.add (e T) (e T))) = K.add (K.add (K.add (e T) (e T)) (sumN K D T)) d := by lib_ring
    rw [this, hd.2]; lib_ring
where
  le_mul_rg_e (y : R) (hy : O.nn y) {a b : R} (h : Le K O.toCone a b) : Le K O.toCone (K.mul a y) (K.mul b y) := by
    cases h with
    | intro d hd => exact ⟨K.mul d y, O.nn_mul _ _ hd.1 hy, by rw [← hd.2]; lib_ring⟩

end Gen

end SemiBudget

#print axioms SemiBudget.tele_budget
#print axioms SemiBudget.budget3

#print axioms: 2 theorems, no axioms.

79.132 Damping of a forced semi-implicit mode

\((1+x)^2a'^2\le 2(1+x)^2a^2+2h^2f^2\) for \(a'-a=-x(a'+a)+hf\), \(x\ge 0\): high modes are slaved to the forcing. Over any ring with a positive cone.

ns/ForcedDamp.lean

/-
  ns/ForcedDamp.lean — T37 cycle 809 (goal G521): mode damping of the semi-implicit step with forcing, over any
  ring with a positive cone (ℚ included): with x ≥ 0 and a' − a = −x(a' + a) + h f,
    (1 + x)²·a'² ≤ 2(1 + x)²·a² + 2h²f².
  High modes (large x = hνk) are slaved to the forcing: a'² ≤ 2a² + 2h²f²/(1 + x)².

  Lean 4 core plus ns/ForcedCN (lin form), ns/MidRecover (sq_add_le, two), lib/RingTac.
    forced_form   (1 + x)a' = (1 − x)a + h f
    forced_damp   the inequality
-/
import ns.ForcedCN
import ns.MidRecover

namespace ForcedDamp
open DetChain NSChain LibRing DyadicLarge MidRecover

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem forced_form (a a' x hf : R) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf) :
    K.mul (K.add K.one x) a' = K.add (K.mul (K.add K.one (K.neg x)) a) hf := by
  have t : K.mul (K.add K.one x) a' = K.add (K.add (K.mul (K.add K.one (K.neg x)) a) hf)
      (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) hf))) := by lib_ring
  exact t.trans (by rw [st]; lib_ring)

theorem forced_damp (a a' x hf : R) (hx : P.nn x) (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hf) :
    Le K P (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a'))
      (K.add (two K (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a a))) (two K (K.mul hf hf))) := by
  have e := forced_form K a a' x hf st
  have l := sq_add_le K P (K.mul (K.add K.one (K.neg x)) a) hf
  have e2 : K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul a' a')
      = K.mul (K.add (K.mul (K.add K.one (K.neg x)) a) hf) (K.add (K.mul (K.add K.one (K.neg x)) a) hf) := by
    rw [← e]; lib_ring
  rw [e2]
  apply le_trans' K P _ _ _ l
  apply le_add' K P _ _ _ _ _ (le_refl' K P _)
  -- (1 − x)² a² ≤ (1 + x)² a²: difference 4x a² ≥ 0
  unfold two
  refine ⟨K.add (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)) (K.mul (K.add (K.add x x) (K.add x x)) (K.mul a a)),
    P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)) (P.nn_sq a))
      (P.nn_mul _ _ (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)) (P.nn_sq a)), ?_⟩
  lib_ring

end Gen

end ForcedDamp

#print axioms ForcedDamp.forced_form
#print axioms ForcedDamp.forced_damp

#print axioms: 2 theorems, no axioms.

79.133 Weighted energy identity of the semi-implicit scheme

\(\sum w(a'^2-a^2)+\sum wx(a'+a)^2=h\sum w(a'-a)N+2h\sum waN\), exact over any commutative ring. The last sum is the weighted flux, controlled in the large-data argument.

ns/SemiWeighted.lean

/-
  ns/SemiWeighted.lean — T37 cycle 814 (goal G525): the weighted energy identity of the semi-implicit scheme over any
  commutative ring: with weights w_n and a'_n − a_n = −x_n(a'_n + a_n) + hN_n,
    Σ w(a'² − a²) + Σ w x (a' + a)² = h Σ w (a' − a) N + 2h Σ w a N            (exact).
  For w ≡ 1 and a neutral nonlinearity the last sum vanishes (SemiEnergy); for the dyadic model with w_n = 4ⁿ it is
  the flux sum Σ (w_{n+1} − w_n)·flux_n, the term the large-data argument controls.

  Lean 4 core plus ns/SemiEnergy (step_energy), lib/RingTac.
    semi_weighted   the identity
-/
import ns.SemiEnergy

namespace SemiWeighted
open DetChain NSChain LibRing SemiEnergy

section Gen
variable {R : Type} (K : CRing R)

theorem semi_weighted (w a a' x Nn : Nat → R) (h : R)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n))) :
    ∀ N, K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
        (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
      = K.add (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))
          (K.mul (K.add h h) (sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N))
  | 0 => by show K.add K.zero K.zero = K.add (K.mul h K.zero) (K.mul (K.add h h) K.zero); lib_ring
  | N + 1 => by
    have ih := semi_weighted w a a' x Nn h st N
    have e := step_energy K (a N) (a' N) (x N) (K.mul h (Nn N)) (st N)
    show K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
          (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N))))))
        (K.add (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N))))))
      = K.add (K.mul h (K.add (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)
            (K.mul (w N) (K.mul (K.add (a' N) (K.neg (a N))) (Nn N)))))
          (K.mul (K.add h h) (K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N) (K.mul (w N) (K.mul (a N) (Nn N)))))
    -- per mode: w(a'² − a²) + w x s² = w·(h(a'−a)N + 2haN), from step_energy
    have m1 : K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
        (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
        = K.add (K.mul h (K.mul (w N) (K.mul (K.add (a' N) (K.neg (a N))) (Nn N))))
          (K.mul (K.add h h) (K.mul (w N) (K.mul (a N) (Nn N)))) := by
      have t : K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
          = K.add (K.mul (w N) (K.add (K.add (K.mul (a' N) (a' N)) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))
              (K.neg (K.mul (a N) (a N))))) K.zero := by lib_ring
      rw [t, e]; lib_ring
    have r : K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
          (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N))))))
        (K.add (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N)
          (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N))))))
        = K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
            (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N))
          (K.add (K.mul (w N) (K.add (K.mul (a' N) (a' N)) (K.neg (K.mul (a N) (a N)))))
            (K.mul (w N) (K.mul (x N) (K.mul (K.add (a' N) (a N)) (K.add (a' N) (a N)))))) := by lib_ring
    rw [r, ih, m1]; lib_ring

end Gen

end SemiWeighted

#print axioms SemiWeighted.semi_weighted

#print axioms: 1 theorem, no axioms.

79.134 One weighted step of the semi-implicit scheme

With the state rate bound \(\sum wa(-\nu ka+N)\le B\): \(\sum w(a'^2-a^2)\le 2hB+\sum wx\Delta ^2+h\sum w\Delta N\), \(\Delta =a'-a\). The midpoint dissipation cancels the state dissipation up to \(\sum wx\Delta ^2\).

ns/SemiWStep.lean

/-
  ns/SemiWStep.lean — T37 cycle 817 (goal G528): one weighted step of the semi-implicit scheme over any ring with a
  positive cone (ℚ included). With x_n = hνk_n, w_n, k_n, h, ν ≥ 0, Δ = a' − a, and the state rate bound
    Σ w_n a_n (−ν k_n a_n + N_n) ≤ B                  (DyadicLarge.rate_bound gives this for the dyadic N),
  one has
    Σ w (a'² − a²) ≤ 2hB + Σ w x Δ² + h Σ w Δ N.
  The dissipation at the midpoint cancels the one at the state up to Σ w x Δ² = O(h²)-type terms.

  Lean 4 core plus ns/SemiWeighted (semi_weighted), ns/DyadicLarge, ns/DyUnique (sumN_ext_g), lib/RingTac.
  The midpoint/state exchange uses the parallelogram identity s² + Δ² = 2a'² + 2a² termwise.
    semi_wstep  the inequality
-/
import ns.SemiWeighted
import ns.DyadicLarge
import ns.DyUnique

namespace SemiWStep
open DetChain NSChain LibRing DyadicLarge SemiWeighted

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem semi_wstep (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hw : ∀ n, P.nn (w n)) (hk : ∀ n, P.nn (k n)) (hh : P.nn h) (hnu : P.nn nu)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K P (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K P (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
      (K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
            (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
        (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))) := by
  have id := semi_weighted K w a a' (fun n => K.mul h (K.mul nu (k n))) Nn h st N
  -- abbreviations as sums
  generalize hS1 : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = S1 at id ⊢
  generalize hDs : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N = Ds at id
  generalize hDd : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
      (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N = Dd
  generalize hX : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N = X at id ⊢
  generalize hY : sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N = Y at id
  -- Y = Σ w a(−νka + N) + ν Σ w k a² ≤ B + ν Σ w k a²
  let Da : R := sumN K (fun n => K.mul (w n) (K.mul nu (K.mul (k n) (K.mul (a n) (a n))))) N
  have eY : Y = K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) Da := by
    rw [← hY, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- 2hνΣ w k a² ≤ Ds + Dd  (parallelogram, termwise)
  have par : Le K P (K.mul (K.add h h) Da) (K.add Ds Dd) := by
    rw [← hDs, ← hDd, ← sumN_add, ← sumN_mul_left]
    apply sum_le K P
    intro n
    refine ⟨K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n)))),
      P.nn_mul _ _ (hw n) (P.nn_mul _ _ (P.nn_mul _ _ hh (P.nn_mul _ _ hnu (hk n))) (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _))), ?_⟩
    lib_ring
  -- assemble: S1 = hX + 2hY − Ds ≤ hX + 2h(B + Da) − Ds ≤ hX + 2hB + Dd
  have r2 := le_mul_nn K P (K.add h h) _ _ (P.nn_add _ _ hh hh) rate
  cases r2 with
  | intro e1 he1 =>
    cases par with
    | intro e2 he2 =>
      refine ⟨K.add e1 e2, P.nn_add _ _ he1.1 he2.1, ?_⟩
      have hS : S1 = K.add (K.add (K.mul h X) (K.mul (K.add h h) Y)) (K.neg Ds) := by
        have : S1 = K.add (K.add S1 Ds) (K.neg Ds) := by lib_ring
        rw [this, id]
      rw [hS, eY]
      have u1 := he1.2
      have u2 := he2.2
      generalize sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N = Z at u1 ⊢
      generalize Da = D at u2 ⊢
      -- u1: 2h·Z + e1 = 2h·B ;  u2: 2h·D + e2 = Ds + Dd
      have : K.add (K.add (K.add (K.mul h X) (K.mul (K.add h h) (K.add Z D))) (K.neg Ds)) (K.add e1 e2)
          = K.add (K.add (K.add (K.mul (K.add h h) Z) e1) (K.add (K.mul (K.add h h) D) e2)) (K.add (K.mul h X) (K.neg Ds)) := by
        lib_ring
      rw [this, u1, u2]; lib_ring

end Gen

end SemiWStep

#print axioms SemiWStep.semi_wstep

#print axioms: 1 theorem, no axioms.

79.135 One-step stability of the semi-implicit scheme

The difference of two solutions solves the same forced step: \((1+x)^2d'^2\le 2(1+x)^2d^2+2h^2(N_a-N_b)^2\). Over any ring with a positive cone.

ns/SemiStab.lean

/-
  ns/SemiStab.lean — T37 cycle 820 (goal G531): one-step stability of the semi-implicit scheme in the data, per
  mode, over any ring with a positive cone (ℚ included): if a' and b' are the next states from a and b,
    a' − a = −x(a' + a) + h·Na,   b' − b = −x(b' + b) + h·Nb,   x ≥ 0,
  then d = a − b, d' = a' − b' satisfy
    (1 + x)²·d'² ≤ 2(1 + x)²·d² + 2h²(Na − Nb)².
  (The difference solves the same forced step with forcing Na − Nb; ForcedDamp.)

  Lean 4 core plus ns/ForcedDamp (forced_damp), lib/RingTac.
    diff_forced   d' − d = −x(d' + d) + h(Na − Nb)
    semi_stab     the inequality
-/
import ns.ForcedDamp

namespace SemiStab
open DetChain NSChain LibRing DyadicLarge MidRecover ForcedDamp

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem diff_forced (a b a' b' x h Na Nb : R)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))
    (s2 : K.add b' (K.neg b) = K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)) :
    K.add (K.add a' (K.neg b')) (K.neg (K.add a (K.neg b)))
      = K.add (K.neg (K.mul x (K.add (K.add a' (K.neg b')) (K.add a (K.neg b))))) (K.mul h (K.add Na (K.neg Nb))) := by
  have t : K.add (K.add a' (K.neg b')) (K.neg (K.add a (K.neg b)))
      = K.add (K.add (K.neg (K.mul x (K.add (K.add a' (K.neg b')) (K.add a (K.neg b))))) (K.mul h (K.add Na (K.neg Nb))))
        (K.add (K.add (K.add a' (K.neg a)) (K.neg (K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))))
          (K.neg (K.add (K.add b' (K.neg b)) (K.neg (K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)))))) := by lib_ring
  exact t.trans (by rw [s1, s2]; lib_ring)

theorem semi_stab (a b a' b' x h Na Nb : R) (hx : P.nn x)
    (s1 : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) (K.mul h Na))
    (s2 : K.add b' (K.neg b) = K.add (K.neg (K.mul x (K.add b' b))) (K.mul h Nb)) :
    Le K P (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul (K.add a' (K.neg b')) (K.add a' (K.neg b'))))
      (K.add (two K (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul (K.add a (K.neg b)) (K.add a (K.neg b)))))
        (two K (K.mul (K.mul h (K.add Na (K.neg Nb))) (K.mul h (K.add Na (K.neg Nb)))))) :=
  forced_damp K P (K.add a (K.neg b)) (K.add a' (K.neg b')) x (K.mul h (K.add Na (K.neg Nb))) hx
    (diff_forced K a b a' b' x h Na Nb s1 s2)

end Gen

end SemiStab

#print axioms SemiStab.diff_forced
#print axioms SemiStab.semi_stab

#print axioms: 2 theorems, no axioms.

79.136 Weighted semi-implicit step for the dyadic model

The rate hypothesis of the weighted step supplied by the large-data head bound at the state. \(\sum w(a'^2-a^2)\le 2hB_{\rm head}+\sum wx\Delta ^2+h\sum w\Delta \,\mathrm {dyN}(c,a)\).

ns/SemiWDy.lean

/-
  ns/SemiWDy.lean — T37 cycle 823 (goal G534): the weighted semi-implicit step for the dyadic model, over any ring
  with a positive cone (ℚ included): SemiWStep with its rate hypothesis supplied by DyadicLarge.rate_bound
  (large-data head bound at the state a). With Δ_n = a'(n+1) − a(n+1), x_n = hνk_{n+1}:
    Σ w_{n+1}(a'(n+1)² − a(n+1)²) ≤ 2h·B_head + Σ w_{n+1} x_n Δ_n² + h Σ w_{n+1} Δ_n dyN(c, a)_n,
  B_head = Σ_{n<N} [n < n0] q_n E A  (the same head constant as in the CN large-data bound).

  Lean 4 core plus ns/SemiWStep (semi_wstep), ns/DyadicLarge (rate_bound).
-/
import ns.SemiWStep

namespace SemiWDy
open DetChain NSChain LibRing DyadicLarge SemiWStep

theorem semi_wdy {R : Type} (K : CRing R) (P : Cone K) (w c k a a' M : Nat → R) (h nu A E : R) (n0 N : Nat)
    (hw : ∀ n, P.nn (w n)) (hk : ∀ n, P.nn (k n)) (hh : P.nn h) (hnu : P.nn nu)
    (st : ∀ n, K.add (a' (n + 1)) (K.neg (a (n + 1))) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k (n + 1))))
      (K.add (a' (n + 1)) (a (n + 1))))) (K.mul h (dyN K c a n)))
    (h0 : a 0 = K.zero) (hN : a (N + 1) = K.zero)
    (hq : ∀ n, P.nn (qq K w c n))
    (hcrit : ∀ n, n0 ≤ n → K.mul (qq K w c n) (M n) = K.mul nu (K.mul (w n) (k n)))
    (htail : ∀ n, n0 ≤ n → Le K P (a (n + 1)) (M n))
    (hA : ∀ n, Le K P (a (n + 1)) A) (hAnn : P.nn A)
    (hE : ∀ n, Le K P (K.mul (a n) (a n)) E) :
    Le K P (sumN K (fun n => K.mul (w (n + 1)) (K.add (K.mul (a' (n + 1)) (a' (n + 1))) (K.neg (K.mul (a (n + 1)) (a (n + 1)))))) N)
      (K.add (K.add (K.mul (K.add h h) (sumN K (fun n => if n < n0 then K.mul (qq K w c n) (K.mul E A) else K.zero) N))
          (sumN K (fun n => K.mul (w (n + 1)) (K.mul (K.mul h (K.mul nu (k (n + 1))))
            (K.mul (K.add (a' (n + 1)) (K.neg (a (n + 1)))) (K.add (a' (n + 1)) (K.neg (a (n + 1))))))) N))
        (K.mul h (sumN K (fun n => K.mul (w (n + 1)) (K.mul (K.add (a' (n + 1)) (K.neg (a (n + 1)))) (dyN K c a n))) N))) :=
  semi_wstep K P (fun n => w (n + 1)) (fun n => k (n + 1)) (fun n => a (n + 1)) (fun n => a' (n + 1)) (fun n => dyN K c a n)
    h nu _ N (fun n => hw (n + 1)) (fun n => hk (n + 1)) hh hnu st
    (rate_bound K P w c k a M nu A E n0 N h0 hN hq hcrit htail hA hAnn hE
      (fun n => P.nn_mul _ _ hnu (P.nn_mul _ _ (hw n) (P.nn_mul _ _ (hk n) (P.nn_sq _)))))

end SemiWDy

#print axioms SemiWDy.semi_wdy

#print axioms: 1 theorem, no axioms.

79.137 Low modes of the semi-implicit \(\Delta \)-terms

For \(0\le x\), \(4x\le 1\), \(\Delta =-xs+hN\): \(8x\Delta ^2\le xs^2+16xh^2N^2\) — an eighth of the midpoint dissipation plus \(O(h^2)\). Over any ordered ring.

ns/LowMode.lean

/-
  ns/LowMode.lean — T37 cycle 826 (goal G537): the low-mode part of the semi-implicit Δ-terms (G528b), over any
  ordered ring (ℚ included): for a mode with 0 ≤ x and 4x ≤ 1 and Δ = −x s + hN,
    8x·Δ² ≤ x·s² + 16x·h²N².
  So on low modes Σ w x Δ² ≤ (1/8)Σ w x s² + 2h²Σ w x N²: an eighth of the midpoint dissipation plus O(h²).
  (High modes, 4x > 1, are handled by ForcedDamp.)

  Lean 4 core plus ns/RiccatiH (sq_le), ns/MidRecover (sq_add_le, two), lib/RingTac.
-/
import ns.RiccatiH
import ns.MidRecover

namespace LowMode
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RiccatiH MidRecover

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem low_mode (x s hN : R) (hx : O.nn x)
    (h4 : Le K O.toCone (K.add (K.add x x) (K.add x x)) K.one) :
    Le K O.toCone (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.mul (K.add (K.neg (K.mul x s)) hN) (K.add (K.neg (K.mul x s)) hN)))
      (K.add (K.mul x (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
          (K.add K.one K.one)) (K.mul hN hN))) := by
  -- Δ² ≤ 2x²s² + 2(hN)²
  have q := sq_add_le K O.toCone (K.neg (K.mul x s)) hN
  have hx8 : O.nn (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)) (O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx))
  have q8 := le_mul_nn K O.toCone _ _ _ hx8 q
  apply le_trans' K O.toCone _ _ _ q8
  -- 8x·2x²s² = x·(4x)²·s² ≤ x·s²
  have f4 : O.nn (K.add (K.add x x) (K.add x x)) := O.nn_add _ _ (O.nn_add _ _ hx hx) (O.nn_add _ _ hx hx)
  have sq1 := sq_le K O _ _ f4 h4
  have t1 := le_mul_nn K O.toCone x _ _ hx sq1
  have t2 : Le K O.toCone (K.mul (K.mul x (K.mul (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))) (K.mul s s))
      (K.mul (K.mul x (K.mul K.one K.one)) (K.mul s s)) := by
    cases t1 with
    | intro d hd => exact ⟨K.mul d (K.mul s s), O.nn_mul _ _ hd.1 (O.nn_sq s), by rw [← hd.2]; lib_ring⟩
  have tot := le_add' K O.toCone _ _ _ _ t2 (le_refl' K O.toCone
    (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)))
  have e1 : K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.add (two K (K.mul (K.neg (K.mul x s)) (K.neg (K.mul x s)))) (two K (K.mul hN hN)))
      = K.add (K.mul (K.mul x (K.mul (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))) (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)) := by
    unfold two; lib_ring
  have e2 : K.add (K.mul (K.mul x (K.mul K.one K.one)) (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN))
      = K.add (K.mul x (K.mul s s))
        (K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)) := by
    lib_ring
  rw [e1]; rw [e2] at tot; exact tot

end Gen

end LowMode

#print axioms LowMode.low_mode

#print axioms: 1 theorem, no axioms.

79.138 The weighted semi-implicit step, keeping the new-state dissipation

With the parallelogram identity used exactly: \(\sum w(a'^2-a^2)+2\sum wxa'^2\le 2hB+\sum wx\Delta ^2+h\sum w\Delta N\). No sign conditions on \(w,k,\nu \); summed over time the term \(2\sum wxa'^2\) absorbs the low-mode \(\Delta \)-terms.

ns/SemiWStep2.lean

/-
  ns/SemiWStep2.lean — T37 cycle 830 (goal G540): one weighted step of the semi-implicit scheme over any ring with a
  positive cone (ℚ included). With x_n = hνk_n, h ≥ 0 (no sign needed on w, k, ν), Δ = a' − a, and the rate bound
    Σ w_n a_n (−ν k_n a_n + N_n) ≤ B                  (DyadicLarge.rate_bound gives this for the dyadic N),
  one has the sharper
    Σ w (a'² − a²) + 2 Σ w x a'² ≤ 2hB + Σ w x Δ² + h Σ w Δ N
  (the parallelogram identity x s² + x Δ² = 2x a² + 2x a'² is used exactly, keeping the new-state dissipation).

  Lean 4 core plus ns/SemiWeighted (semi_weighted), ns/DyadicLarge, ns/DyUnique (sumN_ext_g), lib/RingTac.
  The midpoint/state exchange uses the parallelogram identity s² + Δ² = 2a'² + 2a² termwise.
    semi_wstep  the inequality
-/
import ns.SemiWeighted
import ns.DyadicLarge
import ns.DyUnique

namespace SemiWStep2
open DetChain NSChain LibRing DyadicLarge SemiWeighted

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem semi_wstep2 (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hh : P.nn h)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K P (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K P (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N)
        (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N))
      (K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
            (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
        (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))) := by
  have id := semi_weighted K w a a' (fun n => K.mul h (K.mul nu (k n))) Nn h st N
  -- abbreviations as sums
  generalize hS1 : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = S1 at id ⊢
  generalize hDs : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n))))) N = Ds at id
  generalize hDd : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n)))
      (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N = Dd
  generalize hX : sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N = X at id ⊢
  generalize hY : sumN K (fun n => K.mul (w n) (K.mul (a n) (Nn n))) N = Y at id
  -- Y = Σ w a(−νka + N) + ν Σ w k a² ≤ B + ν Σ w k a²
  let Da : R := sumN K (fun n => K.mul (w n) (K.mul nu (K.mul (k n) (K.mul (a n) (a n))))) N
  have eY : Y = K.add (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) Da := by
    rw [← hY, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- Ds + Dd = 2hν Σ w k a² + Σ w x (2a'²)  (parallelogram, exact)
  generalize hW : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N = W
  have par : K.add Ds Dd = K.add (K.mul (K.add h h) Da) W := by
    rw [← hDs, ← hDd, ← hW, ← sumN_add, ← sumN_mul_left, ← sumN_add]
    apply DyUnique.sumN_ext_g
    intro n _; lib_ring
  -- assemble: S1 + W = hX + 2hY − Ds + W = hX + 2h(Z + Da) − Ds + W = hX + 2hZ + Dd ≤ hX + 2hB + Dd
  have r2 := le_mul_nn K P (K.add h h) _ _ (P.nn_add _ _ hh hh) rate
  cases r2 with
  | intro e1 he1 =>
    refine ⟨e1, he1.1, ?_⟩
    have hS : S1 = K.add (K.add (K.mul h X) (K.mul (K.add h h) Y)) (K.neg Ds) := by
      have : S1 = K.add (K.add S1 Ds) (K.neg Ds) := by lib_ring
      rw [this, id]
    rw [hS, eY]
    have u1 := he1.2
    generalize sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N = Z at u1 ⊢
    generalize Da = D at par ⊢
    have : K.add (K.add (K.add (K.add (K.mul h X) (K.mul (K.add h h) (K.add Z D))) (K.neg Ds)) W) e1
        = K.add (K.add (K.add (K.mul (K.add h h) Z) e1) (K.add (K.mul (K.add h h) D) W)) (K.add (K.mul h X) (K.neg Ds)) := by
      lib_ring
    rw [this, u1, ← par]; lib_ring

end Gen

end SemiWStep2

#print axioms SemiWStep2.semi_wstep2

#print axioms: 1 theorem, no axioms.

79.139 Time-summed absorption of the low-mode terms

\(4E_{t+1}+7G_{t+1}\le 4E_t+G_t+4R_t\Rightarrow (4E_T+G_T)+6\sum _{t<T}G_{t+1}\le (4E_0+G_0)+4\sum _{t<T}R_t\). The potential \(4E+G\) telescopes; over any ordered ring.

ns/TwoLevel.lean

/-
  ns/TwoLevel.lean — T37 cycle 833 (goal G543): the time-summed absorption lemma for the semi-implicit low modes,
  over any ordered ring (ℚ included): if for every t
    4E_{t+1} + 7G_{t+1} ≤ 4E_t + G_t + 4R_t
  (from SemiWStep2 + LowMode: the new-state dissipation 2G_{t+1} minus a quarter of G_{t+1} + G_t), then with
  Φ_t = 4E_t + G_t,
    Φ_T + 6·Σ_{t<T} G_{t+1} ≤ Φ_0 + 4·Σ_{t<T} R_t.

  Lean 4 core plus NSChain (sumN), ns/DyadicLarge (le_add'), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace TwoLevel
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def c4 (y : R) : R := K.add (K.add y y) (K.add y y)
def c6 (y : R) : R := K.add (K.add (K.add y y) (K.add y y)) (K.add y y)
def c7 (y : R) : R := K.add (c6 K y) y

theorem two_level (E G Rr : Nat → R)
    (st : ∀ t, Le K P (K.add (c4 K (E (t + 1))) (c7 K (G (t + 1)))) (K.add (K.add (c4 K (E t)) (G t)) (c4 K (Rr t)))) :
    ∀ T, Le K P (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T)))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (sumN K Rr T)))
  | 0 => by
    refine ⟨K.zero, P.nn_zero, ?_⟩
    show K.add (K.add (K.add (c4 K (E 0)) (G 0)) (c6 K K.zero)) K.zero = K.add (K.add (c4 K (E 0)) (G 0)) (c4 K K.zero)
    unfold c4 c6; lib_ring
  | T + 1 => by
    have ih := two_level E G Rr st T
    have s := le_add' K P _ _ _ _ (st T) (le_refl' K P (c6 K (sumN K (fun t => G (t + 1)) T)))
    have s2 := le_add' K P _ _ _ _ ih (le_refl' K P (c4 K (Rr T)))
    have l : K.add (K.add (c4 K (E (T + 1))) (G (T + 1))) (c6 K (K.add (sumN K (fun t => G (t + 1)) T) (G (T + 1))))
        = K.add (K.add (c4 K (E (T + 1))) (c7 K (G (T + 1)))) (c6 K (sumN K (fun t => G (t + 1)) T)) := by
      unfold c7; unfold c6; unfold c4; lib_ring
    have r1 : K.add (K.add (K.add (c4 K (E T)) (G T)) (c4 K (Rr T))) (c6 K (sumN K (fun t => G (t + 1)) T))
        = K.add (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T))) (c4 K (Rr T)) := by
      unfold c4 c6; lib_ring
    have r2 : K.add (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (sumN K Rr T))) (c4 K (Rr T))
        = K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.add (sumN K Rr T) (Rr T))) := by
      unfold c4; lib_ring
    show Le K P (K.add (K.add (c4 K (E (T + 1))) (G (T + 1))) (c6 K (K.add (sumN K (fun t => G (t + 1)) T) (G (T + 1)))))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.add (sumN K Rr T) (Rr T))))
    rw [l, ← r2]; rw [r1] at s
    exact le_trans' K P _ _ _ s s2

end Gen

end TwoLevel

#print axioms TwoLevel.two_level

#print axioms: 1 theorem, no axioms.

79.140 Per-mode absorption for low modes

\(0\le x\), \(4x\le 1\): \(8x\Delta ^2\le 2xa'^2+2xa^2+16xh^2N^2\) — the input of the time-summed absorption. Over any ordered ring.

ns/LowStep.lean

/-
  ns/LowStep.lean — T37 cycle 836 (goal G546): when every mode is low (4x_n ≤ 1, a CFL-type condition for fixed
  truncation), the Δ-term of SemiWStep2 is absorbed: per mode, over any ordered ring (ℚ included),
    8x·Δ² ≤ 2x·a'² + 2x·a² + 16x·h²N²          (Δ = a' − a = −x(a' + a) + hN, 0 ≤ x, 4x ≤ 1),
  (summed with weights w ≥ 0 and combined with SemiWStep2 it yields the hypothesis 4E' + 7G' ≤ 4E + G + 4R of
  TwoLevel).

  Lean 4 core plus ns/LowMode (low_mode), ns/MidRecover (sq_add_le), ns/DyadicLarge, lib/RingTac.
    low_pt     the per-mode inequality
-/
import ns.LowMode

namespace LowStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LowMode MidRecover

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

def e8 (y : R) : R := K.add (K.add (K.add y y) (K.add y y)) (K.add (K.add y y) (K.add y y))

theorem low_pt (a a' x hN : R) (hx : O.nn x) (h4 : Le K O.toCone (K.add (K.add x x) (K.add x x)) K.one)
    (st : K.add a' (K.neg a) = K.add (K.neg (K.mul x (K.add a' a))) hN) :
    Le K O.toCone (e8 K (K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a)))))
      (K.add (K.add (K.add (K.mul x (K.mul a' a')) (K.mul x (K.mul a' a'))) (K.add (K.mul x (K.mul a a)) (K.mul x (K.mul a a))))
        (K.mul (K.mul (e8 K x) (K.add K.one K.one)) (K.mul hN hN))) := by
  have lm := low_mode K O x (K.add a' a) hN hx h4
  rw [← st] at lm
  have e1 : e8 K (K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))))
      = K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x)))
        (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))) := by unfold e8; lib_ring
  rw [e1]
  apply le_trans' K O.toCone _ _ _ lm
  -- x s² ≤ 2x a'² + 2x a²   (s² + Δ² = 2a'² + 2a², x ≥ 0)
  have q : Le K O.toCone (K.mul x (K.mul (K.add a' a) (K.add a' a)))
      (K.add (K.add (K.mul x (K.mul a' a')) (K.mul x (K.mul a' a'))) (K.add (K.mul x (K.mul a a)) (K.mul x (K.mul a a)))) :=
    ⟨K.mul x (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a))), O.nn_mul _ _ hx (O.nn_sq _), by lib_ring⟩
  have e2 : K.mul (K.mul (K.add (K.add (K.add x x) (K.add x x)) (K.add (K.add x x) (K.add x x))) (K.add K.one K.one)) (K.mul hN hN)
      = K.mul (K.mul (e8 K x) (K.add K.one K.one)) (K.mul hN hN) := by unfold e8; rfl
  rw [e2]
  exact le_add' K O.toCone _ _ _ _ q (le_refl' K O.toCone _)

end Gen

end LowStep

#print axioms LowStep.low_pt

#print axioms: 1 theorem, no axioms.

79.141 Scalar assembly of the low-mode step

\((E'-E)+2G'\le 2hB+D+hX\) and \(8D\le 2G'+2G+16q\) give \(4E'+7G'\le 4E+G+4(2hB+hX+2q)\) — the TwoLevel hypothesis. Over any ordered ring without zero divisors.

ns/LowAssemble.lean

/-
  ns/LowAssemble.lean — T37 cycle 839 (goal G549): the scalar assembly for the CFL version of the weighted
  semi-implicit bound, over any ordered ring without zero divisors (ℚ included): from
    (i)  (E' − E) + 2G' ≤ 2hB + D + hX          (SemiWStep2)
    (ii) 8D ≤ 2G' + 2G + 16q                     (LowStep summed)
  follows  4E' + 7G' ≤ 4E + G + 4(2hB + hX + 2q)  — the hypothesis of TwoLevel with R = 2hB + hX + 2q.

  Lean 4 core plus ns/SemiGlobal (halve), ns/RatModel (nn_one'), ns/DyadicLarge, lib/RingTac.
-/
import ns.SemiGlobal

namespace LowAssemble
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiGlobal RatModel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

def m2 (y : R) : R := K.add y y
def m4 (y : R) : R := m2 K (m2 K y)
def m8 (y : R) : R := m2 K (m4 K y)

theorem low_assemble (h10 : K.one ≠ K.zero) (E E' G G' B D X q h : R)
    (i1 : Le K O.toCone (K.add (K.add E' (K.neg E)) (m2 K G')) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X)))
    (i2 : Le K O.toCone (m8 K D) (K.add (K.add (m2 K G') (m2 K G)) (m8 K (m2 K q)))) :
    Le K O.toCone (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
      (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) := by
  -- 8·(i): 8E' − 8E + 16G' ≤ 16hB + 8D + 8hX ≤ 16hB + 2G' + 2G + 16q + 8hX
  have s8 := le_mul_nn K O.toCone (m8 K K.one) _ _ (by unfold m8 m4 m2; exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone))) (O.nn_add _ _ (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)) (O.nn_add _ _ (nn_one' K O.toCone) (nn_one' K O.toCone)))) i1
  have s := le_trans' K O.toCone _ _ _ s8
    (show Le K O.toCone (K.mul (m8 K K.one) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X)))
      (K.add (K.add (K.add (m2 K G') (m2 K G)) (m8 K (m2 K q))) (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X)))) from by
      have := le_add' K O.toCone _ _ _ _ i2 (le_refl' K O.toCone (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X))))
      have e : K.mul (m8 K K.one) (K.add (K.add (K.mul (m2 K h) B) D) (K.mul h X))
          = K.add (m8 K D) (K.mul (m8 K K.one) (K.add (K.mul (m2 K h) B) (K.mul h X))) := by unfold m8 m4 m2; lib_ring
      rw [e]; exact this)
  -- rearrange into 2·(target) ≤ 2·(rhs) + 0, then halve
  have H : Le K O.toCone (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
      (K.add (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) K.zero) := by
    apply halve K O h10 _ _ K.zero O.nn_zero
    cases s with
    | intro d hd =>
      refine ⟨d, hd.1, ?_⟩
      have e1 : K.add (K.add (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))
          (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G')))) d
          = K.add (K.add (K.add (K.mul (m8 K K.one) (K.add (K.add E' (K.neg E)) (m2 K G'))) d) (m8 K E)) (K.neg (m2 K G')) := by
        unfold m8 m4 m2; lib_ring
      rw [e1, hd.2]; unfold m8 m4 m2; lib_ring
  rw [show K.add (K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q)))) K.zero
    = K.add (K.add (m4 K E) G) (m4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h X)) (m2 K q))) by lib_ring] at H
  exact H

end Gen

end LowAssemble

#print axioms LowAssemble.low_assemble

#print axioms: 1 theorem, no axioms.

79.142 Weighted sum of the low-mode absorption

\(8\sum wx\Delta ^2\le 2\sum wxa'^2+2\sum wxa^2+16\sum wx(hN)^2\) when \(0\le x_n\), \(4x_n\le 1\) — the second input of the scalar assembly. Over any ordered ring.

ns/LowSum.lean

/-
  ns/LowSum.lean — T37 cycle 842 (goal G552): the weighted sum of LowStep, in the form of LowAssemble's (ii), over
  any ordered ring (ℚ included): if 0 ≤ x_n, 4x_n ≤ 1, w_n ≥ 0 and a'_n − a_n = −x_n(a'_n + a_n) + h N_n for n < N,
    8·D ≤ 2·G' + 2·G + 16·q,
  D = Σ w x (a' − a)²,  G' = Σ w x a'²,  G = Σ w x a²,  q = Σ w x (hN)².

  Lean 4 core plus ns/LowStep (low_pt), ns/LowAssemble (m2, m8), ns/DyadicLarge (sum_le), lib/RingTac.
-/
import ns.LowStep
import ns.LowAssemble

namespace LowSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LowStep LowAssemble

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

omit O in
theorem m2_sum (f : Nat → R) : ∀ M, sumN K (fun n => m2 K (f n)) M = m2 K (sumN K f M)
  | 0 => by show K.zero = K.add K.zero K.zero; lib_ring
  | M + 1 => by
    show K.add (sumN K (fun n => m2 K (f n)) M) (m2 K (f M)) = m2 K (K.add (sumN K f M) (f M))
    rw [m2_sum f M]; unfold m2; lib_ring

omit O in
theorem m8_sum (f : Nat → R) (M : Nat) : sumN K (fun n => m8 K (f n)) M = m8 K (sumN K f M) := by
  unfold m8 m4
  rw [m2_sum K (fun n => m2 K (m2 K (f n))), m2_sum K (fun n => m2 K (f n)), m2_sum]

theorem low_sum (w x a a' Nn : Nat → R) (h : R) (N : Nat) (hw : ∀ n, O.nn (w n)) (hx : ∀ n, O.nn (x n))
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (x n) (x n)) (K.add (x n) (x n))) K.one)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (x n) (K.add (a' n) (a n)))) (K.mul h (Nn n))) :
    Le K O.toCone (m8 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
      (K.add (K.add (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))) N))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))) N)))
        (m8 K (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N)))) := by
  have pt : ∀ n, Le K O.toCone (m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))))
      (K.add (K.add (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n))))) (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a n) (a n))))))
        (m8 K (m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))))) := by
    intro n
    have l := le_mul_nn K O.toCone (w n) _ _ (hw n) (low_pt K O (a n) (a' n) (x n) (K.mul h (Nn n)) (hx n) (h4 n) (st n))
    cases l with
    | intro d hd =>
      refine ⟨d, hd.1, ?_⟩
      have e1 : K.add (m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) d
          = K.add (K.mul (w n) (e8 K (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) d := by
        unfold m8 m4 m2 e8; lib_ring
      rw [e1, hd.2]; unfold m8 m4 m2 e8; lib_ring
  have sm := sum_le K O.toCone _ _ pt N
  have L : sumN K (fun n => m8 K (K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n))))))) N
      = m8 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N) := by
    exact m8_sum K _ N
  have Rr : sumN K (fun n => K.add (K.add (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n))))) (m2 K (K.mul (w n) (K.mul (x n) (K.mul (a n) (a n))))))
        (m8 K (m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))))) N
      = K.add (K.add (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))) N))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))) N)))
        (m8 K (m2 K (sumN K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N))) := by
    rw [sumN_add, sumN_add, m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a' n) (a' n)))),
      m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (a n) (a n)))),
      m8_sum K (fun n => m2 K (K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))),
      m2_sum K (fun n => K.mul (w n) (K.mul (x n) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n)))))]
  rw [L, Rr] at sm; exact sm

end Gen

end LowSum

#print axioms LowSum.m2_sum
#print axioms LowSum.m8_sum
#print axioms LowSum.low_sum

#print axioms: 3 theorems, no axioms.

79.143 The weighted semi-implicit step under a CFL condition

With \(4x_n\le 1\) for all \(n<N\): \(4E'+7G'\le 4E+G+4R\), \(R=2hB+h\sum w\Delta N+2\sum wx(hN)^2\) — summed by TwoLevel. Assembly of SemiWStep2, LowSum and LowAssemble.

ns/LowCFL.lean

/-
  ns/LowCFL.lean — T37 cycle 846 (goal G556 = G546b): the weighted semi-implicit bound under the CFL-type condition
  4x_n ≤ 1 (x_n = hνk_n, all n < N), over any ordered ring without zero divisors (ℚ included). One step:
    4E' + 7G' ≤ 4E + G + 4R,   E = Σ w a², G = Σ w x a², R = 2hB + h Σ w (a' − a) N + 2 Σ w x (hN)²,
  given the rate bound Σ w a(−νka + N) ≤ B at the state (SemiWStep2 + LowSum + LowAssemble). Summed over time by
  TwoLevel: (4E_T + G_T) + 6 Σ G ≤ (4E_0 + G_0) + 4 Σ R.

  Lean 4 core plus ns/SemiWStep2, ns/LowSum, ns/LowAssemble, ns/TwoLevel, lib/RingTac.
    step_low    the one-step inequality in TwoLevel's form
-/
import ns.SemiWStep2
import ns.LowSum
import ns.TwoLevel

namespace LowCFL
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiWStep2 LowSum LowAssemble TwoLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem step_low (h10 : K.one ≠ K.zero) (w k a a' Nn : Nat → R) (h nu B : R) (N : Nat)
    (hh : O.nn h) (hw : ∀ n, O.nn (w n)) (hx : ∀ n, O.nn (K.mul h (K.mul nu (k n))))
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (K.mul h (K.mul nu (k n))) (K.mul h (K.mul nu (k n))))
      (K.add (K.mul h (K.mul nu (k n))) (K.mul h (K.mul nu (k n))))) K.one)
    (st : ∀ n, K.add (a' n) (K.neg (a n)) = K.add (K.neg (K.mul (K.mul h (K.mul nu (k n))) (K.add (a' n) (a n))))
      (K.mul h (Nn n)))
    (rate : Le K O.toCone (sumN K (fun n => K.mul (w n) (K.mul (a n) (K.add (K.neg (K.mul nu (K.mul (k n) (a n)))) (Nn n)))) N) B) :
    Le K O.toCone
      (K.add (c4 K (sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N))
        (c7 K (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a' n) (a' n)))) N)))
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N))
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a n) (a n)))) N))
        (c4 K (K.add (K.add (K.mul (m2 K h) B) (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)))
          (m2 K (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N))))) := by
  have s1 := semi_wstep2 K O.toCone w k a a' Nn h nu B N hh st rate
  have s2 := low_sum K O w (fun n => K.mul h (K.mul nu (k n))) a a' Nn h N hw hx h4 st
  generalize hE' : sumN K (fun n => K.mul (w n) (K.mul (a' n) (a' n))) N = E' 
  generalize hE : sumN K (fun n => K.mul (w n) (K.mul (a n) (a n))) N = E
  generalize hG' : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a' n) (a' n)))) N = G' at s2 ⊢
  generalize hG : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (a n) (a n)))) N = G at s2 ⊢
  -- convert s1 to LowAssemble's (i1)
  have eS : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N = K.add E' (K.neg E) := by
    have t : K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N) E = E' := by
      rw [← hE, ← hE', ← sumN_add]; apply DyUnique.sumN_ext_g; intro n _; lib_ring
    have : sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N
        = K.add (K.add (sumN K (fun n => K.mul (w n) (K.add (K.mul (a' n) (a' n)) (K.neg (K.mul (a n) (a n))))) N) E) (K.neg E) := by
      lib_ring
    rw [this, t]
  have eW : sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.add (K.mul (a' n) (a' n)) (K.mul (a' n) (a' n))))) N
      = m2 K G' := by
    rw [← hG', ← m2_sum]; apply DyUnique.sumN_ext_g; intro n _; unfold m2; lib_ring
  rw [eS, eW] at s1
  have asm := low_assemble K O h10 E E' G G' B
    (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N)
    (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)
    (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.mul h (Nn n)) (K.mul h (Nn n))))) N) h
    (by
      have e : K.add (K.add (K.mul (m2 K h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
          (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N))
          = K.add (K.add (K.mul (K.add h h) B)
          (sumN K (fun n => K.mul (w n) (K.mul (K.mul h (K.mul nu (k n))) (K.mul (K.add (a' n) (K.neg (a n))) (K.add (a' n) (K.neg (a n)))))) N))
          (K.mul h (sumN K (fun n => K.mul (w n) (K.mul (K.add (a' n) (K.neg (a n))) (Nn n))) N)) := rfl
      rw [e]; exact s1)
    s2
  cases asm with
  | intro d hd =>
    refine ⟨d, hd.1, ?_⟩
    have : K.add (K.add (c4 K E') (c7 K G')) d = K.add (K.add (m4 K E') (K.add (m4 K G') (K.add (m2 K G') G'))) d := by
      unfold c7 c6 c4 m4 m2; lib_ring
    rw [this, hd.2]; unfold c4 m4 m2; lib_ring

end Gen

end LowCFL

#print axioms LowCFL.step_low

#print axioms: 1 theorem, no axioms.

79.144 The time-summed bound with a uniform remainder

\(R_t\le \rho \) for all \(t\) gives \((4E_T+G_T)+6\sum G\le (4E_0+G_0)+4T\rho \).

ns/TwoLevelConst.lean

/-
  ns/TwoLevelConst.lean — T37 cycle 849 (goal G559): TwoLevel with a uniform remainder bound, over any ring with a
  positive cone (ℚ included): if 4E_{t+1} + 7G_{t+1} ≤ 4E_t + G_t + 4R_t and R_t ≤ ρ for every t, then
    (4E_T + G_T) + 6·Σ_{t<T} G_{t+1} ≤ (4E_0 + G_0) + 4·T·ρ      (T·ρ = castN T · ρ).
  With LowCFL this is the CFL-version weighted bound once ρ bounds 2hB_t + hX_t + 2q_t.

  Lean 4 core plus ns/TwoLevel (two_level), ns/RatModelU (sumN_const_g), ns/DyadicLarge (sum_le), lib/RingTac.
-/
import ns.TwoLevel
import ns.RatModelU

namespace TwoLevelConst
open DetChain NSChain LibRing DyadicLarge TwoLevel RatModelU

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem two_level_const (E G Rr : Nat → R) (rho : R)
    (st : ∀ t, Le K P (K.add (c4 K (E (t + 1))) (c7 K (G (t + 1)))) (K.add (K.add (c4 K (E t)) (G t)) (c4 K (Rr t))))
    (hR : ∀ t, Le K P (Rr t) rho) (T : Nat) :
    Le K P (K.add (K.add (c4 K (E T)) (G T)) (c6 K (sumN K (fun t => G (t + 1)) T)))
      (K.add (K.add (c4 K (E 0)) (G 0)) (c4 K (K.mul (castN K T) rho))) := by
  have tl := two_level K P E G Rr st T
  have sR := sum_le K P _ _ hR T
  rw [sumN_const_g] at sR
  have c := le_add' K P _ _ _ _ (le_refl' K P (K.add (c4 K (E 0)) (G 0)))
    (show Le K P (c4 K (sumN K Rr T)) (c4 K (K.mul (castN K T) rho)) from by
      unfold c4; exact le_add' K P _ _ _ _ (le_add' K P _ _ _ _ sR sR) (le_add' K P _ _ _ _ sR sR))
  exact le_trans' K P _ _ _ tl c

end Gen

end TwoLevelConst

#print axioms TwoLevelConst.two_level_const

#print axioms: 1 theorem, no axioms.

79.145 Square bounds give value bounds

\(A\ge 0\), \(a^2\le A^2\Rightarrow a\le A\) over any ordered ring without zero divisors — the mode bound for the rate estimate.

ns/SqRoot.lean

/-
  ns/SqRoot.lean — T37 cycle 853 (goal G562): square bounds give value bounds, over any ordered ring without zero
  divisors (ℚ included):  A ≥ 0, a² ≤ A²  ⇒  a ≤ A.
  Used to turn the energy bound a_n² ≤ 2E_0 ≤ A² into the mode bound a_{n+1} ≤ A required by rate_bound (uniform
  remainder ρ in the CFL-version weighted bound).

  Lean 4 core plus ns/YoungM (pos_cancel), ns/GoodModesQ, lib/RingTac.
    le_of_sq_le   the statement
-/
import ns.YoungM

namespace SqRoot
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge YoungM

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem le_of_sq_le (a A : R) (hA : O.nn A) (h : Le K O.toCone (K.mul a a) (K.mul A A)) : Le K O.toCone a A := by
  cases O.total a A with
  | inl l => exact l
  | inr l =>
    -- A ≤ a: write a = A + d, d ≥ 0; then a² = A² + d(2A + d) ≤ A² forces d(2A + d) ≤ 0, so d(2A + d) = 0
    cases l with
    | intro d hd =>
      have ha : a = K.add A d := hd.2.symm
      cases h with
      | intro e he =>
        -- (A + d)² + e = A²  ⇒  d(2A + d) + e = 0
        have z : K.add (K.mul d (K.add (K.add A A) d)) e = K.zero := by
          have t : K.add (K.mul d (K.add (K.add A A) d)) e
              = K.add (K.add (K.mul a a) e) (K.neg (K.mul A A)) := by rw [ha]; lib_ring
          rw [t, he.2]; lib_ring
        have nn1 : O.nn (K.mul d (K.add (K.add A A) d)) := O.nn_mul _ _ hd.1 (O.nn_add _ _ (O.nn_add _ _ hA hA) hd.1)
        have z1 : K.mul d (K.add (K.add A A) d) = K.zero := nn_sum_zero K O _ _ nn1 he.1 z
        -- d = 0 or 2A + d = 0; in the second case d = 0 too (both ≥ 0)
        have d0 : d = K.zero := by
          cases O.noZD _ _ z1 with
          | inl q => exact q
          | inr q =>
            have : K.add (K.add A A) d = K.zero := q
            exact (nn_sum_zero K O _ _ hd.1 (O.nn_add _ _ hA hA) (by rw [K.add_comm]; exact this))
        rw [ha, d0]; exact ⟨K.zero, O.nn_zero, by lib_ring⟩

end Gen

end SqRoot

#print axioms SqRoot.le_of_sq_le

#print axioms: 1 theorem, no axioms.

79.146 The rate bound at every state of bounded energy

For the concrete model: \(\sum a_{n+1}^2\le A^2\Rightarrow \sum 4^{n+1}a_{n+1}(-3\cdot 4^{n+1}a_{n+1}+\mathrm {dyN}_n)\le B_{\rm head}(A^2,A,m)\), \(A=m\). The rate hypothesis of the weighted semi-implicit step, uniformly along a scheme with energy \(\le A^2\).

ns/StateRate.lean

/-
  ns/StateRate.lean — T37 cycle 856 (goal G565): the rate bound at any state of energy ≤ A², for the concrete
  supercritical model over any ordered ring without zero divisors (ℚ included). With w = k = 4ⁿ, c = M = 2ⁿ, ν = 3,
  A = m·1, a_0 = 0, a_{N+1} = 0 and Σ_{n<N} a_{n+1}² ≤ A²:
    Σ_{n<N} 4^{n+1} a_{n+1}(−3·4^{n+1} a_{n+1} + dyN(c, a)_n) ≤ B_head(A², A, n₀ = m).
  This is SemiWDy's rate hypothesis at every step of a scheme whose energy stays ≤ A² (e.g. A² ≥ 2E₀ with
  SemiDyadic), giving a uniform remainder for the CFL-version weighted bound.

  Lean 4 core plus ns/RatModel (P2, P4, crit_g, nn_qq_g, cast lemmas), ns/SqRoot (le_of_sq_le),
  ns/DyCN (mode_E), ns/DyadicLarge (rate_bound).
-/
import ns.SqRoot
import ns.DyCN
import ns.RatModel

namespace StateRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel SqRoot DyCN

theorem state_rate {R : Type} (K : CRing R) (O : OrdR K) (a : Nat → R) (m N : Nat)
    (h0 : a 0 = K.zero) (hN : ∀ i, N + 1 ≤ i → a i = K.zero)
    (hE : Le K O.toCone (sumN K (fun n => K.mul (a (n + 1)) (a (n + 1))) N) (K.mul (castN K m) (castN K m))) :
    Le K O.toCone (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a (n + 1))
        (K.add (K.neg (K.mul (castN K 3) (K.mul (P4 K (n + 1)) (a (n + 1))))) (dyN K (P2 K) a n)))) N)
      (sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N) := by
  have hA : O.nn (castN K m) := nn_cast K O.toCone m
  have mE := mode_E K O.toCone a N h0 hN
  have mEA : ∀ i, Le K O.toCone (K.mul (a i) (a i)) (K.mul (castN K m) (castN K m)) :=
    fun i => le_trans' K O.toCone _ _ _ (mE i) hE
  have aA : ∀ i, Le K O.toCone (a i) (castN K m) := fun i => SqRoot.le_of_sq_le K O (a i) (castN K m) hA (mEA i)
  exact rate_bound K O.toCone (P4 K) (P2 K) (P4 K) a (P2 K) (castN K 3) (castN K m) (K.mul (castN K m) (castN K m)) m N
    h0 (hN (N + 1) (Nat.le_refl _)) (nn_qq_g K O.toCone) (fun n _ => crit_g K n)
    (fun n hn => le_trans' K O.toCone _ _ _ (aA (n + 1))
      (le_trans' K O.toCone _ _ _ (cast_mono K O.toCone m n hn) (cast_le_p2 K O.toCone n)))
    (fun n => aA (n + 1)) hA mEA
    (fun n => O.nn_mul _ _ (nn_cast K O.toCone 3) (O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) n)
      (O.nn_mul _ _ (nn_pw K O.toCone _ (nn_cast K O.toCone 4) n) (O.nn_sq _))))

end StateRate

#print axioms StateRate.state_rate

#print axioms: 1 theorem, no axioms.

79.147 Weighted bound for the semi-implicit concrete model under CFL

With \(4x_n\le 1\), energies \(\le A^2\) and the step remainders \(\le \rho '\): \((4E_T+G_T)+6\sum G\le (4E_0+G_0)+4T(2hB_{\rm head}+\rho ')\) for the \(4^n\)-weighted energy. Assembly of LowCFL, StateRate and TwoLevelConst.

ns/CFLBound.lean

/-
  ns/CFLBound.lean — T37 cycle 859 (goal G568): the weighted (H¹-type, w = 4ⁿ) bound for the semi-implicit
  concrete model under a CFL condition, over any ordered ring without zero divisors (ℚ included). Hypotheses:
  the semi-implicit steps a_{t+1}(n+1) − a_t(n+1) = −x_n(a_{t+1}(n+1) + a_t(n+1)) + h·dyN(2ⁿ, a_t)_n with
  x_n = h·3·4^{n+1}, 4x_n ≤ 1 (n < N), boundary zeros, energies Σ a_t(n+1)² ≤ A² (A = m; e.g. from SemiDyadic), and
  the remaining step terms h Σ w Δ N + 2 Σ w x (hN)² ≤ ρ'. Then with E_t = Σ 4^{n+1} a_t(n+1)², G_t = Σ 4^{n+1} x_n a_t(n+1)²:
    (4E_T + G_T) + 6 Σ_{t<T} G_{t+1} ≤ (4E_0 + G_0) + 4·T·(2h·B_head + ρ').

  Lean 4 core plus ns/LowCFL (step_low), ns/StateRate (state_rate), ns/TwoLevelConst, ns/LowAssemble (m2).
-/
import ns.LowCFL
import ns.StateRate
import ns.TwoLevelConst

namespace CFLBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RatModel LowAssemble TwoLevel LowCFL StateRate TwoLevelConst

theorem cfl_bound {R : Type} (K : CRing R) (O : OrdR K) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (h rho : R) (m N T : Nat) (hh : O.nn h)
    (h4 : ∀ n, Le K O.toCone (K.add (K.add (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))
      (K.add (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))))) K.one)
    (st : ∀ t n, K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))
      = K.add (K.neg (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.add (a (t + 1) (n + 1)) (a t (n + 1)))))
        (K.mul h (dyN K (P2 K) (a t) n)))
    (h0 : ∀ t, a t 0 = K.zero) (hN : ∀ t i, N + 1 ≤ i → a t i = K.zero)
    (hE : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (a t (n + 1)) (a t (n + 1))) N) (K.mul (castN K m) (castN K m)))
    (hrest : ∀ t, Le K O.toCone
      (K.add (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1))))
          (dyN K (P2 K) (a t) n))) N))
        (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))) rho) :
    Le K O.toCone
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a T (n + 1)) (a T (n + 1)))) N))
          (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a T (n + 1)) (a T (n + 1))))) N))
        (c6 K (sumN K (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (a (t + 1) (n + 1)) (a (t + 1) (n + 1))))) N) T)))
      (K.add (K.add (c4 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a 0 (n + 1)) (a 0 (n + 1)))) N))
          (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a 0 (n + 1)) (a 0 (n + 1))))) N))
        (c4 K (K.mul (castN K T) (K.add (K.mul (m2 K h)
          (sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N))
          rho)))) := by
  let B := sumN K (fun n => if n < m then K.mul (qq K (P4 K) (P2 K) n) (K.mul (K.mul (castN K m) (castN K m)) (castN K m)) else K.zero) N
  have hx : ∀ n, O.nn (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) := fun n =>
    O.nn_mul _ _ hh (O.nn_mul _ _ (nn_cast K O.toCone 3) (nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)))
  have hw : ∀ n, O.nn (P4 K (n + 1)) := fun n => nn_pw K O.toCone _ (nn_cast K O.toCone 4) (n + 1)
  refine two_level_const K O.toCone
    (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (a t (n + 1)) (a t (n + 1)))) N)
    (fun t => sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1)))) (K.mul (a t (n + 1)) (a t (n + 1))))) N)
    (fun t => K.add (K.add (K.mul (m2 K h) B)
      (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N)))
      (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
        (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N)))
    (K.add (K.mul (m2 K h) B) rho) ?_ ?_ T
  · intro t
    exact step_low K O h10 (fun n => P4 K (n + 1)) (fun n => P4 K (n + 1)) (fun n => a t (n + 1)) (fun n => a (t + 1) (n + 1))
      (fun n => dyN K (P2 K) (a t) n) h (castN K 3) B N hh hw hx h4 (st t)
      (state_rate K O (a t) m N (h0 t) (hN t) (hE t))
  · intro t
    have e : K.add (K.add (K.mul (m2 K h) B)
        (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N)))
        (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
          (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))
        = K.add (K.mul (m2 K h) B) (K.add
          (K.mul h (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.add (a (t + 1) (n + 1)) (K.neg (a t (n + 1)))) (dyN K (P2 K) (a t) n))) N))
          (m2 K (sumN K (fun n => K.mul (P4 K (n + 1)) (K.mul (K.mul h (K.mul (castN K 3) (P4 K (n + 1))))
            (K.mul (K.mul h (dyN K (P2 K) (a t) n)) (K.mul h (dyN K (P2 K) (a t) n))))) N))) := by lib_ring
    rw [e]
    exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (hrest t)

end CFLBound

#print axioms CFLBound.cfl_bound

#print axioms: 1 theorem, no axioms.

79.148 The semi-implicit scheme for any neutral nonlinearity

For any nonlinearity \(N_f\) with \(\sum a\,N_f(a)=0\) and \(\sum (x+2)N_f(a)^2\le C\,E(a)^2\) (every Galerkin truncation, 3D included): the explicit sequence over an ordered field solves the scheme and \(E_t\le 2E_0\) for \(4Th^2CE_0\le 1\).

ns/SemiGeneral.lean

/-
  ns/SemiGeneral.lean — T37 cycle 862 (goal G571): the semi-implicit scheme for ANY energy-neutral nonlinearity over
  an ordered field (ℚ included) — covering every Galerkin truncation, in particular 3D Fourier–Galerkin (neutral by
  the triad antisymmetry, FourierGalerkin.neutral), once flattened to scalar modes n < N. Given
    Nf : (Nat → R) → Nat → R   with   Σ_{n<N} a_n Nf(a)_n = 0   and   Σ_{n<N} (x_n + 2) Nf(a)_n² ≤ C·E(a)²
  for every state a (E(a) = Σ_{n<N} a_n²), the sequence
    a_{t+1}(n) = ((1 − x_n) a_t(n) + h Nf(a_t)_n)·(1 + x_n)⁻¹   (n < N),   0 otherwise
  solves the semi-implicit step on n < N, and E_t ≤ 2E_0 for t ≤ T whenever 4T·h²·C·E_0 ≤ 1.

  Lean 4 core plus ns/SemiGlobal (semi_global), ns/ForcedCN (forced_exist), ns/DyUnique (sumN_ext_g).
-/
import ns.SemiGlobal
import ns.ForcedCN
import ns.DyUnique

namespace SemiGeneral
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge GoodModesQ RatModel ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def upd (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (n : Nat) : R :=
  match Nat.ble (n + 1) N with
  | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n)))
  | false => K.zero

def sq (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (b : Nat → R) : Nat → Nat → R
  | 0 => b
  | t + 1 => upd K inv Nf x h N (sq inv Nf x h N b t)

theorem upd_in (inv : R → R) (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h : R) (N : Nat) (a : Nat → R) (n : Nat)
    (hn : n < N) :
    upd K inv Nf x h N a n = K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n))) := by
  show (match Nat.ble (n + 1) N with
    | true => K.mul (K.add (K.mul (K.add K.one (K.neg (x n))) (a n)) (K.mul h (Nf a n))) (inv (K.add K.one (x n)))
    | false => K.zero) = _
  rw [Nat.ble_eq_true_of_le hn]

end Gen

theorem semi_general {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (Nf : (Nat → R) → Nat → R) (x : Nat → R) (h C : R) (N T : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (neutral : ∀ a : Nat → R, sumN K (fun n => K.mul (a n) (Nf a n)) N = K.zero)
    (nb : ∀ a : Nat → R, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nf a n) (Nf a n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a n) (a n)) N) (sumN K (fun n => K.mul (a n) (a n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (b n) (b n)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun n => K.mul (sq K inv Nf x h N b t n) (sq K inv Nf x h N b t n)) N)
      (K.add (sumN K (fun n => K.mul (b n) (b n)) N) (sumN K (fun n => K.mul (b n) (b n)) N)) := by
  let A : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => sq K inv Nf x h N b t n
    | false => K.zero
  let Nn : Nat → Nat → R := fun t n => match Nat.ble (n + 1) N with
    | true => Nf (sq K inv Nf x h N b t) n
    | false => K.zero
  have inA : ∀ t n, n < N → A t n = sq K inv Nf x h N b t n := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _; rw [Nat.ble_eq_true_of_le hn]
  have inN : ∀ t n, n < N → Nn t n = Nf (sq K inv Nf x h N b t) n := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _; rw [Nat.ble_eq_true_of_le hn]
  have outA : ∀ t n, N ≤ n → A t n = K.zero := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _
    cases e : Nat.ble (n + 1) N with
    | false => rfl
    | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
  have outN : ∀ t n, N ≤ n → Nn t n = K.zero := by
    intro t n hn; show (match Nat.ble (n + 1) N with | true => _ | false => _) = _
    cases e : Nat.ble (n + 1) N with
    | false => rfl
    | true => exact absurd (Nat.le_of_ble_eq_true e) (Nat.not_le_of_lt (Nat.lt_succ_of_le hn))
  have sumA : ∀ t, sumN K (fun n => K.mul (A t n) (A t n)) N = sumN K (fun n => K.mul (sq K inv Nf x h N b t n) (sq K inv Nf x h N b t n)) N :=
    fun t => DyUnique.sumN_ext_g K _ _ N (fun n hn => by rw [inA t n hn])
  have st : ∀ t n, K.add (A (t + 1) n) (K.neg (A t n)) = K.add (K.neg (K.mul (x n) (K.add (A (t + 1) n) (A t n)))) (K.mul h (Nn t n)) := by
    intro t n
    cases Nat.lt_or_ge n N with
    | inl hn =>
      rw [inA (t + 1) n hn, inA t n hn, inN t n hn]
      show K.add (upd K inv Nf x h N (sq K inv Nf x h N b t) n) (K.neg (sq K inv Nf x h N b t n))
        = K.add (K.neg (K.mul (x n) (K.add (upd K inv Nf x h N (sq K inv Nf x h N b t) n) (sq K inv Nf x h N b t n))))
          (K.mul h (Nf (sq K inv Nf x h N b t) n))
      rw [upd_in K inv Nf x h N _ n hn]
      exact forced_exist K O inv mul_inv h10 (sq K inv Nf x h N b t n) (x n) (K.mul h (Nf (sq K inv Nf x h N b t) n)) (hx n)
    | inr hn =>
      rw [outA (t + 1) n hn, outA t n hn, outN t n hn]; lib_ring
  have neu : ∀ t, sumN K (fun n => K.mul (A t n) (Nn t n)) N = K.zero := by
    intro t
    rw [DyUnique.sumN_ext_g K _ (fun n => K.mul (sq K inv Nf x h N b t n) (Nf (sq K inv Nf x h N b t) n)) N
      (fun n hn => by rw [inA t n hn, inN t n hn])]
    exact neutral _
  have nbt : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (A t n) (A t n)) N) (sumN K (fun n => K.mul (A t n) (A t n)) N))) := by
    intro t
    rw [sumA t, DyUnique.sumN_ext_g K _ (fun n => K.mul (K.add (x n) (K.add K.one K.one))
      (K.mul (Nf (sq K inv Nf x h N b t) n) (Nf (sq K inv Nf x h N b t) n))) N (fun n hn => by rw [inN t n hn])]
    exact nb _
  have small' : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (A 0 n) (A 0 n)) N))) K.one := by
    rw [sumA 0]; exact small
  intro t ht
  have g := SemiGlobal.semi_global K O h10 A x Nn h C N T hx hC st neu nbt small' t ht
  rw [sumA t, sumA 0] at g
  exact g

end SemiGeneral

#print axioms SemiGeneral.upd_in
#print axioms SemiGeneral.semi_general

#print axioms: 2 theorems, no axioms.

79.149 Quadratic nonlinearities and the semi-implicit Galerkin scheme

For \(N_n(a)=\sum _{p,q}T_{npq}a_pa_q\): \(N_n^2\le (\sum T_{npq}^2)E^2\) (Cauchy–Schwarz), so with neutrality every Galerkin truncation (3D included) has an explicit semi-implicit solution over \(\mathbb Q\) with \(E_t\le 2E_0\) for \(4Th^2CE_0\le 1\).

ns/QuadBound.lean

/-
  ns/QuadBound.lean — T37 cycle 865 (goal G574): any quadratic nonlinearity N_n(a) = Σ_{p,q<N} T_{npq} a_p a_q (every
  Galerkin truncation, 3D Fourier–Galerkin included, after flattening modes and components to n < N) is controlled by
  the energy, over any ordered ring (ℚ included), by Cauchy–Schwarz (lib/CauchySchwarz):
    N_n(a)² ≤ (Σ_{p,q} T_{npq}²) · E(a)²,     Σ_n (x_n + 2) N_n(a)² ≤ C · E(a)²,   C = Σ_n (x_n + 2) Σ_{p,q} T_{npq}².
  With energy neutrality this is exactly SemiGeneral's hypothesis: the semi-implicit Galerkin scheme over ℚ has an
  explicit solution with E_t ≤ 2E_0 for 4T·h²·C·E_0 ≤ 1.

  Lean 4 core plus lib/CauchySchwarz (sA, sB, sC, cauchy_schwarz), ns/SemiGeneral, ns/DyadicLarge, lib/RingTac.
    row, grid     the list of pairs (T_{npq}, a_p a_q)
    sB_grid       Σ (a_p a_q)² = E²
    sA_indep      Σ T² does not depend on the state
    quad_sq       N_n² ≤ (ΣT²)·E²
    quad_nb       the weighted bound
    quad_semi     the semi-implicit Galerkin scheme: E_t ≤ 2E_0
-/
import lib.CauchySchwarz
import ns.SemiGeneral

namespace QuadBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LibCS

section Gen
variable {R : Type} (K : CRing R)

def row (T : Nat → Nat → Nat → R) (a : Nat → R) (n p : Nat) : Nat → List (R × R)
  | 0 => []
  | q + 1 => (T n p q, K.mul (a p) (a q)) :: row T a n p q

def grid (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) : Nat → List (R × R)
  | 0 => []
  | p + 1 => row K T a n p N ++ grid T a n N p

def Nq (T : Nat → Nat → Nat → R) (N : Nat) (a : Nat → R) (n : Nat) : R := sC K (grid K T a n N N)

theorem sB_app : ∀ l1 l2 : List (R × R), sB K (l1 ++ l2) = K.add (sB K l1) (sB K l2)
  | [], l2 => by show sB K l2 = K.add K.zero (sB K l2); lib_ring
  | p :: l1, l2 => by
    show K.add (K.mul p.2 p.2) (sB K (l1 ++ l2)) = K.add (K.add (K.mul p.2 p.2) (sB K l1)) (sB K l2)
    rw [sB_app l1 l2]; lib_ring

theorem sA_app : ∀ l1 l2 : List (R × R), sA K (l1 ++ l2) = K.add (sA K l1) (sA K l2)
  | [], l2 => by show sA K l2 = K.add K.zero (sA K l2); lib_ring
  | p :: l1, l2 => by
    show K.add (K.mul p.1 p.1) (sA K (l1 ++ l2)) = K.add (K.add (K.mul p.1 p.1) (sA K l1)) (sA K l2)
    rw [sA_app l1 l2]; lib_ring

theorem sB_row (T : Nat → Nat → Nat → R) (a : Nat → R) (n p : Nat) :
    ∀ M, sB K (row K T a n p M) = K.mul (K.mul (a p) (a p)) (sumN K (fun q => K.mul (a q) (a q)) M)
  | 0 => by show K.zero = K.mul _ K.zero; lib_ring
  | M + 1 => by
    show K.add (K.mul (K.mul (a p) (a M)) (K.mul (a p) (a M))) (sB K (row K T a n p M))
      = K.mul (K.mul (a p) (a p)) (K.add (sumN K (fun q => K.mul (a q) (a q)) M) (K.mul (a M) (a M)))
    rw [sB_row T a n p M]; lib_ring

theorem sB_grid (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) :
    ∀ P, sB K (grid K T a n N P) = K.mul (sumN K (fun p => K.mul (a p) (a p)) P) (sumN K (fun q => K.mul (a q) (a q)) N)
  | 0 => by show K.zero = K.mul K.zero _; lib_ring
  | P + 1 => by
    show sB K (row K T a n P N ++ grid K T a n N P) = _
    rw [sB_app, sB_row, sB_grid T a n N P]
    show _ = K.mul (K.add (sumN K (fun p => K.mul (a p) (a p)) P) (K.mul (a P) (a P))) _
    lib_ring

theorem sA_row (T : Nat → Nat → Nat → R) (a b : Nat → R) (n p : Nat) : ∀ M, sA K (row K T a n p M) = sA K (row K T b n p M)
  | 0 => rfl
  | M + 1 => by
    show K.add (K.mul (T n p M) (T n p M)) (sA K (row K T a n p M)) = K.add (K.mul (T n p M) (T n p M)) (sA K (row K T b n p M))
    rw [sA_row T a b n p M]

theorem sA_indep (T : Nat → Nat → Nat → R) (a b : Nat → R) (n N : Nat) : ∀ P, sA K (grid K T a n N P) = sA K (grid K T b n N P)
  | 0 => rfl
  | P + 1 => by
    show sA K (row K T a n P N ++ grid K T a n N P) = sA K (row K T b n P N ++ grid K T b n N P)
    rw [sA_app, sA_app, sA_row K T a b, sA_indep T a b n N P]

variable (O : OrdR K)
include O

def posO : Pos K := ⟨O.nn, O.nn_sq, O.nn_add, O.nn_mul, O.nn_zero⟩

theorem quad_sq (T : Nat → Nat → Nat → R) (a : Nat → R) (n N : Nat) :
    Le K O.toCone (K.mul (Nq K T N a n) (Nq K T N a n))
      (K.mul (sA K (grid K T a n N N)) (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
  have cs := cauchy_schwarz K (posO K O) (grid K T a n N N)
  rw [sB_grid] at cs
  exact ⟨_, cs, by unfold Nq sub; lib_ring⟩

theorem quad_nb (T : Nat → Nat → Nat → R) (x : Nat → R) (hx : ∀ n, O.nn (x n)) (N : Nat) (a : Nat → R) :
    Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nq K T N a n) (Nq K T N a n))) N)
      (K.mul (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N)
        (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
  have pt : ∀ n, Le K O.toCone (K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nq K T N a n) (Nq K T N a n)))
      (K.mul (K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N)))
        (K.mul (sumN K (fun p => K.mul (a p) (a p)) N) (sumN K (fun p => K.mul (a p) (a p)) N))) := by
    intro n
    have q := quad_sq K O T a n N
    rw [sA_indep K T a (fun _ => K.zero)] at q
    have l := le_mul_nn K O.toCone (K.add (x n) (K.add K.one K.one)) _ _
      (O.nn_add _ _ (hx n) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))) q
    cases l with
    | intro d hd => exact ⟨d, hd.1, hd.2.trans (by lib_ring)⟩
  generalize hE : sumN K (fun p => K.mul (a p) (a p)) N = E at pt ⊢
  have sm := sum_le K O.toCone _ _ pt N
  cases sm with
  | intro d hd =>
    refine ⟨d, hd.1, hd.2.trans ?_⟩
    rw [K.mul_comm (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N) (K.mul E E),
      ← sumN_mul_left]
    apply DyUnique.sumN_ext_g; intro n _; lib_ring

end Gen

theorem quad_semi {R : Type} (K : CRing R) (O : OrdR K) (inv : R → R)
    (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (T : Nat → Nat → Nat → R) (x : Nat → R) (h : R) (N S : Nat) (b : Nat → R) (hx : ∀ n, O.nn (x n))
    (neutral : ∀ a : Nat → R, sumN K (fun n => K.mul (a n) (Nq K T N a n)) N = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * S)) (K.mul (K.mul (K.mul h h)
      (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (sA K (grid K T (fun _ => K.zero) n N N))) N))
      (sumN K (fun n => K.mul (b n) (b n)) N))) K.one) :
    ∀ t, t ≤ S → Le K O.toCone (sumN K (fun n => K.mul (SemiGeneral.sq K inv (Nq K T N) x h N b t n) (SemiGeneral.sq K inv (Nq K T N) x h N b t n)) N)
      (K.add (sumN K (fun n => K.mul (b n) (b n)) N) (sumN K (fun n => K.mul (b n) (b n)) N)) :=
  SemiGeneral.semi_general K O inv mul_inv h10 (Nq K T N) x h _ N S b hx
    (sumN_nn K O.toCone _ (fun n => O.nn_mul _ _ (O.nn_add _ _ (hx n) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)))
      (by
        clear neutral small
        induction N with
        | zero => exact O.nn_zero
        | succ N _ => exact (show O.nn (sA K (grid K T (fun _ => K.zero) n (N + 1) (N + 1))) from nn_sA (grid K T (fun _ => K.zero) n (N + 1) (N + 1)))) ) N)
    neutral (quad_nb K O T x hx N) small
where
  nn_sA : ∀ l : List (R × R), O.nn (sA K l)
    | [] => O.nn_zero
    | p :: l => O.nn_add _ _ (O.nn_sq _) (nn_sA l)

end QuadBound

#print axioms QuadBound.sB_app
#print axioms QuadBound.sA_app
#print axioms QuadBound.sB_row
#print axioms QuadBound.sB_grid
#print axioms QuadBound.sA_row
#print axioms QuadBound.sA_indep
#print axioms QuadBound.quad_sq
#print axioms QuadBound.quad_nb
#print axioms QuadBound.quad_semi

#print axioms: 9 theorems, no axioms.

79.150 Dissipation budget of the semi-implicit scheme (any neutral nonlinearity)

\(\sum _{t<T}\sum _nx_n(a_{t+1}(n)+a_t(n))^2\le 3E_0\) when \(4Th^2CE_0\le 1\), for every energy-neutral nonlinearity bounded by \(CE^2\).

ns/SemiBudgetArr.lean

/-
  ns/SemiBudgetArr.lean — T37 cycle 868 (goal G577): the dissipation budget of the semi-implicit scheme for any
  energy-neutral nonlinearity bounded by C·E² (arrays a_t(n), n < N; every Galerkin truncation), over any ordered
  ring without zero divisors (ℚ included): if 4T·h²·C·E_0 ≤ 1 then
    Σ_{t<T} Σ_{n<N} x_n (a_{t+1}(n) + a_t(n))² ≤ 3E_0
  — the discrete ν∫‖∇u‖² dt ≤ (3/2)‖u_0‖² for the scheme with an explicit next state (SemiGeneral).

  Lean 4 core plus ns/SemiGlobal (semi_global, halve), ns/SemiBound (semi_bound), ns/SemiBudget (budget3).
-/
import ns.SemiGlobal
import ns.SemiBudget

namespace SemiBudgetArr
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge SemiBound SemiGlobal SemiBudget

theorem semi_budget {R : Type} (K : CRing R) (O : OrdR K) (h10 : K.one ≠ K.zero)
    (a : Nat → Nat → R) (x : Nat → R) (Nn : Nat → Nat → R) (h C : R) (N T : Nat) (hx : ∀ n, O.nn (x n)) (hC : O.nn C)
    (st : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n)) = K.add (K.neg (K.mul (x n) (K.add (a (t + 1) n) (a t n))))
      (K.mul h (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (a t n) (Nn t n)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun n => K.mul (K.add (x n) (K.add K.one K.one)) (K.mul (Nn t n) (Nn t n))) N)
      (K.mul C (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)))
      K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N) T)
      (K.add (K.add (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N))
        (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) := by
  have hE : ∀ t, O.nn (sumN K (fun n => K.mul (a t n) (a t n)) N) := fun t => sq_sum_nonneg K O.toCone (a t) N
  have step : ∀ t, Le K O.toCone (K.add (K.add (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N)
        (sumN K (fun n => K.mul (a (t + 1) n) (a (t + 1) n)) N))
      (sumN K (fun n => K.mul (x n) (K.mul (K.add (a (t + 1) n) (a t n)) (K.add (a (t + 1) n) (a t n)))) N))
      (K.add (K.add (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N))
        (K.mul (K.mul (K.mul h h) C) (K.mul (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a t n) (a t n)) N)))) := by
    intro t
    have sb := semi_bound K O.toCone (a t) (a (t + 1)) x (Nn t) h N hx (st t) (neutral t)
    have d2 := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (nb t)
    have d3 := le_trans' K O.toCone _ _ _ sb (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) d2)
    cases d3 with
    | intro d hd => exact ⟨d, hd.1, by unfold SemiBound.two at hd; exact hd.2.trans (by lib_ring)⟩
  exact budget3 K O (fun t => sumN K (fun n => K.mul (a t n) (a t n)) N) _ (K.mul (K.mul h h) C) T (O.nn_mul _ _ (O.nn_sq h) hC) hE step
    (fun t ht => semi_global K O h10 a x Nn h C N T hx hC st neutral nb small t (Nat.le_of_lt ht)) small

end SemiBudgetArr

#print axioms SemiBudgetArr.semi_budget

#print axioms: 1 theorem, no axioms.

79.151 The discrete Leray projection on one mode

\(Pv=v-(k\cdot v)\kappa k\), \(\kappa =(k\cdot k)^{-1}\): \(k\cdot Pv=0\), \(P(Pv)=Pv\), \(|Pv|^2+(k\cdot v)^2\kappa =|v|^2\), over any commutative ring with \(\kappa \) given.

ns/LerayProj.lean

/-
  ns/LerayProj.lean — T37 cycle 871 (goal G580): the discrete Leray projection on one Fourier mode, over an ordered
  field (ℚ included): for a wave vector k with k·k ≠ 0 and κ = (k·k)⁻¹,
    P v = v − (k·v)κ k
  satisfies  k·(P v) = 0 (divergence-free),  P(P v) = P v,  and  |P v|² + (k·v)²κ = |v|²  (so |P v|² ≤ |v|²).
  This is the projection the semi-implicit 3D Galerkin scheme needs to keep the state divergence-free, so that
  FourierGalerkin.neutral applies at every step (G574b).

  Lean 4 core plus ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.FourierGalerkin
import lib.RingTac

namespace LerayProj
open DetChain NSChain LibRing FourierGalerkin

section Gen
variable {R : Type} (K : CRing R)

def smul3 (c : R) (v : V3 R) : V3 R := ⟨K.mul c v.x, K.mul c v.y, K.mul c v.z⟩

def proj (κ : R) (k v : V3 R) : V3 R := add3 K v (neg3 K (smul3 K (K.mul (dot3 K k v) κ) k))

theorem div_free (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) : dot3 K k (proj K κ k v) = K.zero := by
  unfold proj add3 neg3 smul3 dot3
  unfold dot3 at hκ
  generalize hd : K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z) = d
  have t : K.add (K.add (K.mul k.x (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))))
      (K.mul k.y (K.add v.y (K.neg (K.mul (K.mul d κ) k.y))))) (K.mul k.z (K.add v.z (K.neg (K.mul (K.mul d κ) k.z))))
      = K.add (K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z))
        (K.neg (K.mul d (K.mul (K.add (K.add (K.mul k.x k.x) (K.mul k.y k.y)) (K.mul k.z k.z)) κ))) := by lib_ring
  rw [t, hκ, hd]; lib_ring

theorem energy (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) :
    K.add (dot3 K (proj K κ k v) (proj K κ k v)) (K.mul (K.mul (dot3 K k v) (dot3 K k v)) κ) = dot3 K v v := by
  unfold proj add3 neg3 smul3 dot3
  unfold dot3 at hκ
  generalize hd : K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z) = d
  generalize hk : K.add (K.add (K.mul k.x k.x) (K.mul k.y k.y)) (K.mul k.z k.z) = kk at hκ
  -- expand: |v|² − 2dκ(k·v) + d²κ²|k|² + d²κ = |v|² − 2d²κ + d²κ(κ kk) + d²κ = |v|²
  have t : K.add (K.add (K.add (K.mul (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))) (K.add v.x (K.neg (K.mul (K.mul d κ) k.x))))
      (K.mul (K.add v.y (K.neg (K.mul (K.mul d κ) k.y))) (K.add v.y (K.neg (K.mul (K.mul d κ) k.y)))))
      (K.mul (K.add v.z (K.neg (K.mul (K.mul d κ) k.z))) (K.add v.z (K.neg (K.mul (K.mul d κ) k.z)))))
      (K.mul (K.mul d d) κ)
      = K.add (K.add (K.add (K.mul v.x v.x) (K.mul v.y v.y)) (K.mul v.z v.z))
        (K.add (K.mul (K.mul (K.mul d d) κ) (K.add (K.mul kk κ) (K.neg K.one)))
          (K.mul (K.add (K.mul d K.one) (K.neg (K.add (K.add (K.mul k.x v.x) (K.mul k.y v.y)) (K.mul k.z v.z))))
            (K.mul (K.add d d) κ))) := by
    rw [← hk]; lib_ring
  rw [t, hκ, hd]; lib_ring

theorem idem (κ : R) (k v : V3 R) (hκ : K.mul (dot3 K k k) κ = K.one) : proj K κ k (proj K κ k v) = proj K κ k v := by
  have h0 := div_free K κ k v hκ
  show add3 K (proj K κ k v) (neg3 K (smul3 K (K.mul (dot3 K k (proj K κ k v)) κ) k)) = proj K κ k v
  rw [h0]
  generalize proj K κ k v = w
  cases w with
  | mk wx wy wz =>
    show (⟨K.add wx (K.neg (K.mul (K.mul K.zero κ) k.x)), K.add wy (K.neg (K.mul (K.mul K.zero κ) k.y)),
      K.add wz (K.neg (K.mul (K.mul K.zero κ) k.z))⟩ : V3 R) = ⟨wx, wy, wz⟩
    have ex : K.add wx (K.neg (K.mul (K.mul K.zero κ) k.x)) = wx := by lib_ring
    have ey : K.add wy (K.neg (K.mul (K.mul K.zero κ) k.y)) = wy := by lib_ring
    have ez : K.add wz (K.neg (K.mul (K.mul K.zero κ) k.z)) = wz := by lib_ring
    rw [ex, ey, ez]

end Gen

end LerayProj

#print axioms LerayProj.div_free
#print axioms LerayProj.energy
#print axioms LerayProj.idem

#print axioms: 3 theorems, no axioms.

79.152 Self-adjointness of the Leray projection

\((Pv)\cdot w=v\cdot (Pw)\), \(Pa=a\) for \(k\cdot a=0\), hence \(a\cdot (PF)=a\cdot F\): projecting the forcing keeps energy neutrality.

ns/LerayProj2.lean

/-
  ns/LerayProj2.lean — T37 cycle 874 (goal G583): more on the one-mode Leray projection P v = v − (k·v)κk, over any
  commutative ring (κ given with κ·(k·k) = 1 only where needed):
    selfadj      (P v)·w = v·(P w)
    fix          k·a = 0  ⇒  P a = a
    proj_test    k·a = 0  ⇒  a·(P F) = a·F
  So projecting the explicit forcing of the semi-implicit 3D Galerkin step keeps the energy neutrality
  Σ_p a_p·(P F)_p = Σ_p a_p·F_p = B(a, a, a) = 0 (FourierGalerkin.neutral) for divergence-free states.

  Lean 4 core plus ns/LerayProj (proj, smul3), lib/RingTac.
-/
import ns.LerayProj

namespace LerayProj2
open DetChain NSChain LibRing FourierGalerkin LerayProj

section Gen
variable {R : Type} (K : CRing R)

theorem selfadj (κ : R) (k v w : V3 R) : dot3 K (proj K κ k v) w = dot3 K v (proj K κ k w) := by
  cases k with
  | mk kx ky kz => cases v with
    | mk vx vy vz => cases w with
      | mk wx wy wz =>
        show K.add (K.add (K.mul (K.add vx (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) kx))) wx)
            (K.mul (K.add vy (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) ky))) wy))
            (K.mul (K.add vz (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx vx) (K.mul ky vy)) (K.mul kz vz)) κ) kz))) wz)
          = K.add (K.add (K.mul vx (K.add wx (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) kx))))
            (K.mul vy (K.add wy (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) ky)))))
            (K.mul vz (K.add wz (K.neg (K.mul (K.mul (K.add (K.add (K.mul kx wx) (K.mul ky wy)) (K.mul kz wz)) κ) kz))))
        lib_ring

theorem fix (κ : R) (k a : V3 R) (ha : dot3 K k a = K.zero) : proj K κ k a = a := by
  unfold proj
  rw [ha]
  cases a with
  | mk ax ay az =>
    show (⟨K.add ax (K.neg (K.mul (K.mul K.zero κ) k.x)), K.add ay (K.neg (K.mul (K.mul K.zero κ) k.y)),
      K.add az (K.neg (K.mul (K.mul K.zero κ) k.z))⟩ : V3 R) = ⟨ax, ay, az⟩
    have ex : K.add ax (K.neg (K.mul (K.mul K.zero κ) k.x)) = ax := by lib_ring
    have ey : K.add ay (K.neg (K.mul (K.mul K.zero κ) k.y)) = ay := by lib_ring
    have ez : K.add az (K.neg (K.mul (K.mul K.zero κ) k.z)) = az := by lib_ring
    rw [ex, ey, ez]

theorem proj_test (κ : R) (k a F : V3 R) (ha : dot3 K k a = K.zero) : dot3 K a (proj K κ k F) = dot3 K a F := by
  rw [← selfadj K κ k a F, fix K κ k a ha]

end Gen

end LerayProj2

#print axioms LerayProj2.selfadj
#print axioms LerayProj2.fix
#print axioms LerayProj2.proj_test

#print axioms: 3 theorems, no axioms.

79.153 The projected semi-implicit step on one 3D mode

\(a'=((1-x)a+h\,PF)(1+x)^{-1}\) is divergence-free when \(a\) is, and componentwise \(a'-a=-x(a'+a)+h\,PF\).

ns/ProjStep.lean

/-
  ns/ProjStep.lean — T37 cycle 878 (goal G592): the projected semi-implicit step on one 3D Fourier mode, over an
  ordered field (ℚ included). For a divergence-free a (k·a = 0), x ≥ 0, a forcing F and ι = (1 + x)⁻¹:
    a' = ((1 − x)·a + h·P F)·ι          (P the Leray projection; P a = a)
  satisfies k·a' = 0 and, componentwise, a'_c − a_c = −x(a'_c + a_c) + h(P F)_c (ForcedCN). So the 3D semi-implicit
  Galerkin scheme with projected forcing keeps every mode divergence-free and is of SemiGeneral's form with the
  neutral forcing P F (LerayProj2.proj_test).

  Lean 4 core plus ns/LerayProj2, ns/ForcedCN (forced_exist), lib/RingTac.
-/
import ns.LerayProj2
import ns.ForcedCN

namespace ProjStep
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin LerayProj LerayProj2 ForcedCN

section Gen
variable {R : Type} (K : CRing R)

def comb (ι x h : R) (a G : V3 R) : V3 R :=
  ⟨K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.x) (K.mul h G.x)) ι,
   K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.y) (K.mul h G.y)) ι,
   K.mul (K.add (K.mul (K.add K.one (K.neg x)) a.z) (K.mul h G.z)) ι⟩

theorem comb_div (ι κ x h : R) (k a F : V3 R) (ha : dot3 K k a = K.zero) (hκ : K.mul (dot3 K k k) κ = K.one) :
    dot3 K k (comb K ι x h a (proj K κ k F)) = K.zero := by
  have hp := div_free K κ k F hκ
  have t : dot3 K k (comb K ι x h a (proj K κ k F))
      = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (dot3 K k a)) (K.mul h (dot3 K k (proj K κ k F)))) ι := by
    generalize proj K κ k F = G
    cases k with
    | mk kx ky kz => cases a with
      | mk ax ay az => cases G with
        | mk gx gy gz =>
          show K.add (K.add (K.mul kx (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ax) (K.mul h gx)) ι))
              (K.mul ky (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ay) (K.mul h gy)) ι)))
              (K.mul kz (K.mul (K.add (K.mul (K.add K.one (K.neg x)) az) (K.mul h gz)) ι))
            = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add (K.add (K.mul kx ax) (K.mul ky ay)) (K.mul kz az)))
              (K.mul h (K.add (K.add (K.mul kx gx) (K.mul ky gy)) (K.mul kz gz)))) ι
          lib_ring
  rw [t, ha, hp]; lib_ring

variable (O : OrdR K)
include O

theorem comb_step (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (x h ac Gc : R) (hx : O.nn x) :
    K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ac) (K.mul h Gc)) (inv (K.add K.one x))) (K.neg ac)
      = K.add (K.neg (K.mul x (K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) ac) (K.mul h Gc)) (inv (K.add K.one x))) ac)))
        (K.mul h Gc) :=
  forced_exist K O inv mul_inv h10 ac x (K.mul h Gc) hx

end Gen

end ProjStep

#print axioms ProjStep.comb_div
#print axioms ProjStep.comb_step

#print axioms: 2 theorems, no axioms.

79.154 Flattening 3D mode arrays

\(\sum _{n<3N}\mathrm {flat}(a)_n\,\mathrm {flat}(b)_n=\sum _{p<N}a_p\cdot b_p\): the scalar semi-implicit theorems apply to 3D Galerkin arrays.

ns/Flatten3.lean

/-
  ns/Flatten3.lean — T37 cycle 881 (goal G594): flattening 3D mode arrays to scalar arrays, over any commutative
  ring. For a : Nat → V3 R let flat a (3p + c) be the c-th component of a p. Then
    Σ_{n<3N} flat a n · flat b n = Σ_{p<N} a_p · b_p,
  so the scalar semi-implicit theorems (SemiGeneral, SemiBudgetArr, QuadBound) apply to 3D Galerkin arrays with
  E = Σ_p |a_p|² and the neutrality Σ_p a_p·F_p = 0.

  Lean 4 core plus ns/FourierGalerkin (V3, dot3), NSChain (sumN), lib/RingTac.
    sum3, sum_flat     block sums of three and the identity Σ_blocks = Σ_p a_p·b_p
    sumN_three         Σ_{n<3N} f n = Σ_{p<N} (f(3p) + f(3p+1) + f(3p+2))
    sum_flat_scalar    Σ_{n<3N} fa n · fb n = Σ_{p<N} a_p·b_p for arrays holding the components at 3p + c
-/
import ns.FourierGalerkin
import lib.RingTac
import lib.Semi

namespace Flatten3
open DetChain NSChain LibRing FourierGalerkin

section Gen
variable {R : Type} (K : CRing R)

/-- flatC a p c: component c ∈ {0, 1, 2} of a p (others give zero). -/
def comp (v : V3 R) : Nat → R
  | 0 => v.x
  | 1 => v.y
  | 2 => v.z
  | _ => K.zero

/-- Σ_{n < 3N} of a function of the flattened index, block by block. -/
def sum3 (f : Nat → Nat → R) : Nat → R
  | 0 => K.zero
  | N + 1 => K.add (sum3 f N) (K.add (K.add (f N 0) (f N 1)) (f N 2))

theorem sum_flat (a b : Nat → V3 R) : ∀ N,
    sum3 K (fun p c => K.mul (comp K (a p) c) (comp K (b p) c)) N = sumN K (fun p => dot3 K (a p) (b p)) N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K (fun p c => K.mul (comp K (a p) c) (comp K (b p) c)) N)
        (K.add (K.add (K.mul (a N).x (b N).x) (K.mul (a N).y (b N).y)) (K.mul (a N).z (b N).z))
      = K.add (sumN K (fun p => dot3 K (a p) (b p)) N) (dot3 K (a N) (b N))
    rw [sum_flat a b N]; rfl

theorem sum3_ext (f g : Nat → Nat → R) (h : ∀ p c, c < 3 → f p c = g p c) : ∀ N, sum3 K f N = sum3 K g N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K f N) (K.add (K.add (f N 0) (f N 1)) (f N 2)) = K.add (sum3 K g N) (K.add (K.add (g N 0) (g N 1)) (g N 2))
    rw [sum3_ext f g h N, h N 0 (Nat.succ_pos 2), h N 1 (Nat.succ_lt_succ (Nat.succ_pos 1)),
      h N 2 (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_pos 0)))]

/-- A scalar sum over 3N indices is the block sum. -/
theorem sumN_three (f : Nat → R) : ∀ N, sumN K f (3 * N) = sum3 K (fun p c => f (3 * p + c)) N
  | 0 => rfl
  | N + 1 => by
    have e : 3 * (N + 1) = 3 * N + 2 + 1 := by lib_nat
    rw [e]
    show K.add (K.add (K.add (sumN K f (3 * N)) (f (3 * N))) (f (3 * N + 1))) (f (3 * N + 2))
      = K.add (sum3 K (fun p c => f (3 * p + c)) N) (K.add (K.add (f (3 * N + 0)) (f (3 * N + 1))) (f (3 * N + 2)))
    rw [sumN_three f N]
    generalize sum3 K (fun p c => f (3 * p + c)) N = S
    show K.add (K.add (K.add S (f (3 * N))) (f (3 * N + 1))) (f (3 * N + 2))
      = K.add S (K.add (K.add (f (3 * N)) (f (3 * N + 1))) (f (3 * N + 2)))
    generalize f (3 * N) = u; generalize f (3 * N + 1) = v; generalize f (3 * N + 2) = w
    lib_ring

/-- The energy of the flattened array: Σ_{n<3N} (flat a n)(flat b n) = Σ_{p<N} a_p · b_p, for any scalar
    arrays fa, fb that hold component c of block p at index 3p + c. -/
theorem sum_flat_scalar (a b : Nat → V3 R) (fa fb : Nat → R)
    (hfa : ∀ p c, c < 3 → fa (3 * p + c) = comp K (a p) c) (hfb : ∀ p c, c < 3 → fb (3 * p + c) = comp K (b p) c) (N : Nat) :
    sumN K (fun n => K.mul (fa n) (fb n)) (3 * N) = sumN K (fun p => dot3 K (a p) (b p)) N := by
  rw [sumN_three K _ N, ← sum_flat K a b N]
  apply sum3_ext K
  intro p c hc
  show K.mul (fa (3 * p + c)) (fb (3 * p + c)) = K.mul (comp K (a p) c) (comp K (b p) c)
  rw [hfa p c hc, hfb p c hc]

end Gen

end Flatten3

#print axioms Flatten3.sum_flat
#print axioms Flatten3.sum3_ext
#print axioms Flatten3.sumN_three
#print axioms Flatten3.sum_flat_scalar

#print axioms: 4 theorems, no axioms.

79.155 The Galerkin force and its projected neutrality

\(B(u,v,w)=\sum _r w_r\cdot N_r(u,v)\) with \(N_r=\sum _{p,q}\delta _{pqr}\,\iota (u_p\cdot k_q)v_q\); hence \(\sum _r u_r\cdot P_rN_r(u,u)=0\) for divergence-free \(u\).

ns/GalerkinForce.lean

/-
  ns/GalerkinForce.lean — T37 cycle 887 (goal G574b): the truncated nonlinearity as a vector field on the modes.
  Over any commutative ring, with c(p,q,r) = δ(p,q,r)·ι(u_p·k_q) and
    Nl_r = Σ_{p∈l} Σ_{q∈l} c(p,q,r) v_q        (a vector in R³ for every mode r),
  the trilinear form of FourierGalerkin is B(u, v, w) = Σ_{r∈l} w_r · Nl_r. Hence (neutral_force) for a
  divergence-free u and symmetric triads, Σ_r u_r · Nl_r(u, u) = 0 in characteristic ≠ 2: the forcing −Nl of the
  semi-implicit Galerkin step is energy-neutral, and stays so after the Leray projection (LerayProj2.proj_test).

  Lean 4 core plus ns/FourierGalerkin (B, antisym/neutral, lsum_swap), NSChain (lsum lemmas), lib/RingTac.
-/
import ns.FourierGalerkin
import ns.LerayProj2
import lib.RingTac

namespace GalerkinForce
open DetChain NSChain LibRing FourierGalerkin LerayProj LerayProj2

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def coef (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (p q r : I) : R :=
  K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))

/-- The nonlinear force at mode r. -/
def Nl (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) : V3 R :=
  ⟨lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l,
   lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l,
   lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l⟩

theorem dot_force (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (w : V3 R) (r : I) :
    dot3 K w (Nl K l δ ι k u v r)
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (dot3 K (v q) w)) l) l := by
  show K.add (K.add (K.mul w.x (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l))
      (K.mul w.y (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l)))
      (K.mul w.z (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l)) = _
  rw [← lsum_mul_left K w.x, ← lsum_mul_left K w.y, ← lsum_mul_left K w.z, ← lsum_add, ← lsum_add]
  apply lsum_ext
  intro p
  rw [← lsum_mul_left K w.x, ← lsum_mul_left K w.y, ← lsum_mul_left K w.z, ← lsum_add, ← lsum_add]
  apply lsum_ext
  intro q
  show K.add (K.add (K.mul w.x (K.mul (coef K δ ι k u p q r) (v q).x)) (K.mul w.y (K.mul (coef K δ ι k u p q r) (v q).y)))
      (K.mul w.z (K.mul (coef K δ ι k u p q r) (v q).z))
    = K.mul (coef K δ ι k u p q r) (K.add (K.add (K.mul (v q).x w.x) (K.mul (v q).y w.y)) (K.mul (v q).z w.z))
  generalize coef K δ ι k u p q r = c
  lib_ring

/-- B(u, v, w) = Σ_r w_r · Nl_r. -/
theorem B_eq (l : List I) (δ : I → I → I → R) (ι : R) (k u v w : I → V3 R) :
    B K l δ ι k u v w = lsum K (fun r => dot3 K (w r) (Nl K l δ ι k u v r)) l := by
  rw [lsum_ext K _ _ (fun r => dot_force K l δ ι k u v (w r) r) l]
  unfold B
  -- Σ_p Σ_q Σ_r X  →  Σ_p Σ_r Σ_q X  →  Σ_r Σ_p Σ_q X
  rw [lsum_ext K _ (fun p => lsum K (fun r => lsum K (fun q =>
      K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l) l)
    (fun p => lsum_swap K (fun q r => K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l l) l]
  rw [lsum_swap K (fun p r => lsum K (fun q =>
      K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (dot3 K (v q) (w r))) l) l l]
  rfl

/-- **Neutral force**: Σ_r u_r · Nl_r(u, u) = 0 for divergence-free u, triads, char ≠ 2. -/
theorem neutral_force (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ x : R, K.add x x = K.zero → x = K.zero) :
    lsum K (fun r => dot3 K (u r) (Nl K l δ ι k u u r)) l = K.zero := by
  rw [← B_eq]; exact neutral K l δ ι k u u hdiv htri hsym h2

/-- **Projected neutral force**: with the Leray projection on every mode, Σ_r u_r · P_r(Nl_r(u, u)) = 0. -/
theorem proj_neutral (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (κ : I → R)
    (hdiv : ∀ p, dot3 K (u p) (k p) = K.zero)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ x : R, K.add x x = K.zero → x = K.zero) :
    lsum K (fun r => dot3 K (u r) (proj K (κ r) (k r) (Nl K l δ ι k u u r))) l = K.zero := by
  rw [lsum_ext K _ (fun r => dot3 K (u r) (Nl K l δ ι k u u r)) (fun r => by
    rw [proj_test K (κ r) (k r) (u r) _ (by rw [dot3_comm K]; exact hdiv r)]) l]
  exact neutral_force K l δ ι k u hdiv htri hsym h2

end Gen

end GalerkinForce

#print axioms GalerkinForce.dot_force
#print axioms GalerkinForce.B_eq
#print axioms GalerkinForce.neutral_force
#print axioms GalerkinForce.proj_neutral

#print axioms: 4 theorems, no axioms.

79.156 The Leray projection does not increase energy

\(|P_pv|^2\le |v|^2\) for \(\kappa _p\ge 0\), and summed over modes: a bound \(\sum |N_p|^2\le CE^2\) passes to the projected force.

ns/LerayProj3.lean

/-
  ns/LerayProj3.lean — T37 cycle 892 (G592b piece): the Leray projection does not increase energy, mode by mode and
  summed, over any commutative ring with a nonnegative cone (ℚ included): for κ_p ≥ 0 with κ_p (k_p·k_p) = 1,
    |P_p v|² ≤ |v|²     and     Σ_{p<N} |P_p v_p|² ≤ Σ_{p<N} |v_p|².
  So a bound Σ_p |N_p|² ≤ C·E² for the Galerkin force passes to the projected force P N, as SemiGlobal needs.

  Lean 4 core plus ns/LerayProj (proj, energy), ns/DyadicLarge (sum_le).
-/
import ns.LerayProj
import ns.DyadicLarge

namespace LerayProj3
open DetChain NSChain FourierGalerkin LerayProj DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem proj_le (κ : R) (k v : V3 R) (hnn : P.nn κ) (hκ : K.mul (dot3 K k k) κ = K.one) :
    Le K P (dot3 K (proj K κ k v) (proj K κ k v)) (dot3 K v v) :=
  ⟨K.mul (K.mul (dot3 K k v) (dot3 K k v)) κ, P.nn_mul _ _ (P.nn_sq _) hnn, energy K κ k v hκ⟩

theorem proj_sum_le (κ : Nat → R) (k v : Nat → V3 R) (hnn : ∀ p, P.nn (κ p))
    (hκ : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one) (N : Nat) :
    Le K P (sumN K (fun p => dot3 K (proj K (κ p) (k p) (v p)) (proj K (κ p) (k p) (v p))) N)
      (sumN K (fun p => dot3 K (v p) (v p)) N) :=
  sum_le K P _ _ (fun p => proj_le K P (κ p) (k p) (v p) (hnn p) (hκ p)) N

end Gen

end LerayProj3

#print axioms LerayProj3.proj_le
#print axioms LerayProj3.proj_sum_le

#print axioms: 2 theorems, no axioms.

79.157 Global bound for the semi-implicit scheme on 3D mode arrays

For neutral forcing with \(\sum _p(x_p+2)|F_p|^2\le CE^2\) and \(4Th^2CE_0\le 1\): \(E_t\le 2E_0\) for \(t\le T\) (flattening without division).

ns/Galerkin3D.lean

/-
  ns/Galerkin3D.lean — T37 cycle 893 (goal G592b): global energy bound for the semi-implicit scheme on 3D mode
  arrays, over any ordered ring without zero divisors (ℚ included). States a_t : Nat → V3 R (modes p < N), damping
  x_p ≥ 0, forcing F_t : Nat → V3 R with, for every t, mode p and component c < 3,
    a_{t+1,p,c} − a_{t,p,c} = −x_p (a_{t+1,p,c} + a_{t,p,c}) + h F_{t,p,c},
  energy neutrality Σ_p a_{t,p}·F_{t,p} = 0 and Σ_p (x_p + 2)|F_{t,p}|² ≤ C·E_t² (E_t = Σ_p |a_{t,p}|²).
  If 4T·h²·C·E_0 ≤ 1 then E_t ≤ 2E_0 for all t ≤ T.
  With F = P N (projected Galerkin force) the hypotheses are supplied by ProjStep, GalerkinForce.proj_neutral and
  LerayProj3.proj_sum_le.

  Lean 4 core plus ns/SemiGlobal (semi_global), ns/Flatten3 (comp, sum3, sumN_three), lib/RingTac, lib/Semi.
    go, go_at     flattening by recursion (no division): go a (3p + c) q = comp (a (q + p)) c
    go_cover      every index is 3p + c for some p, c < 3 (in the form needed)
    sum3_block    Σ over 3N of a flattened expression = Σ_p of the block sum
    global3       the statement
-/
import ns.SemiGlobal
import ns.Flatten3

namespace Galerkin3D
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin Flatten3 SemiGlobal

section Gen
variable {R : Type} (K : CRing R)

def go (a : Nat → V3 R) : Nat → Nat → R
  | 0, q => (a q).x
  | 1, q => (a q).y
  | 2, q => (a q).z
  | n + 3, q => go a n (q + 1)

theorem go_at (a : Nat → V3 R) (c : Nat) (hc : c < 3) : ∀ p q, go a (3 * p + c) q = comp K (a (q + p)) c
  | 0, q => by
    show go a (3 * 0 + c) q = comp K (a q) c
    rw [show 3 * 0 + c = c by lib_nat]
    cases c with
    | zero => rfl
    | succ c => cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
  | p + 1, q => by
    rw [show 3 * (p + 1) + c = (3 * p + c) + 3 by lib_nat]
    show go a (3 * p + c) (q + 1) = _
    rw [go_at a c hc p (q + 1), show q + 1 + p = q + (p + 1) by lib_nat]

theorem go_cover : ∀ n q, ∃ p c, c < 3 ∧ ∀ a : Nat → V3 R, go a n q = comp K (a p) c
  | 0, q => ⟨q, 0, Nat.succ_pos 2, fun _ => rfl⟩
  | 1, q => ⟨q, 1, Nat.succ_lt_succ (Nat.succ_pos 1), fun _ => rfl⟩
  | 2, q => ⟨q, 2, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_pos 0)), fun _ => rfl⟩
  | n + 3, q => by
    cases go_cover n (q + 1) with
    | intro p hp => cases hp with
      | intro c hc => exact ⟨p, c, hc.1, fun a => hc.2 a⟩

def fl (a : Nat → V3 R) (n : Nat) : R := go a n 0

theorem fl_at (a : Nat → V3 R) (p c : Nat) (hc : c < 3) : fl a (3 * p + c) = comp K (a p) c := by
  unfold fl; rw [go_at K a c hc p 0, Nat.zero_add]

theorem sum3_eq (f : Nat → Nat → R) : ∀ N, sum3 K f N = sumN K (fun p => K.add (K.add (f p 0) (f p 1)) (f p 2)) N
  | 0 => rfl
  | N + 1 => by
    show K.add (sum3 K f N) _ = K.add (sumN K _ N) _
    rw [sum3_eq f N]

/-- Σ over 3N of a flattened expression. -/
theorem sum_block (φ : Nat → R) (ψ : Nat → Nat → R) (h : ∀ p c, c < 3 → φ (3 * p + c) = ψ p c) (N : Nat) :
    sumN K φ (3 * N) = sumN K (fun p => K.add (K.add (ψ p 0) (ψ p 1)) (ψ p 2)) N := by
  rw [sumN_three K φ N, sum3_ext K _ ψ (fun p c hc => h p c hc) N, sum3_eq K ψ N]

def vx (x : Nat → R) (p : Nat) : V3 R := ⟨x p, x p, x p⟩

theorem comp_vx (x : Nat → R) (p c : Nat) (hc : c < 3) : comp K (vx x p) c = x p := by
  cases c with
  | zero => rfl
  | succ c => cases c with
    | zero => rfl
    | succ c => cases c with
      | zero => rfl
      | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))

theorem energy_flat (a : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl a n) (fl a n)) (3 * N) = sumN K (fun p => dot3 K (a p) (a p)) N :=
  (sum_block K _ (fun p c => K.mul (comp K (a p) c) (comp K (a p) c))
    (fun p c hc => by show K.mul (fl a (3 * p + c)) (fl a (3 * p + c)) = _; rw [fl_at K a p c hc]) N).trans
    (sumN_ext K _ _ (fun p => rfl) N)

theorem dot_flat (a b : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl a n) (fl b n)) (3 * N) = sumN K (fun p => dot3 K (a p) (b p)) N :=
  (sum_block K _ (fun p c => K.mul (comp K (a p) c) (comp K (b p) c))
    (fun p c hc => by show K.mul (fl a (3 * p + c)) (fl b (3 * p + c)) = _; rw [fl_at K a p c hc, fl_at K b p c hc]) N).trans
    (sumN_ext K _ _ (fun p => rfl) N)

theorem weight_flat (x : Nat → R) (F : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (K.add (fl (vx x) n) (K.add K.one K.one)) (K.mul (fl F n) (fl F n))) (3 * N)
      = sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F p) (F p))) N := by
  rw [sum_block K _ (fun p c => K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (comp K (F p) c) (comp K (F p) c)))
    (fun p c hc => by
      show K.mul (K.add (fl (vx x) (3 * p + c)) (K.add K.one K.one)) (K.mul (fl F (3 * p + c)) (fl F (3 * p + c))) = _
      rw [fl_at K (vx x) p c hc, fl_at K F p c hc, comp_vx K x p c hc]) N]
  apply sumN_ext
  intro p
  show K.add (K.add (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).x (F p).x))
      (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).y (F p).y)))
      (K.mul (K.add (x p) (K.add K.one K.one)) (K.mul (F p).z (F p).z))
    = K.mul (K.add (x p) (K.add K.one K.one)) (K.add (K.add (K.mul (F p).x (F p).x) (K.mul (F p).y (F p).y)) (K.mul (F p).z (F p).z))
  generalize (F p).x = u; generalize (F p).y = v; generalize (F p).z = w; generalize x p = y
  lib_ring

variable (O : OrdR K)
include O

/-- **Global bound for the 3D semi-implicit Galerkin scheme.** -/
theorem global3 (h10 : K.one ≠ K.zero) (a : Nat → Nat → V3 R) (x : Nat → R) (F : Nat → Nat → V3 R)
    (h C : R) (N T : Nat) (hx : ∀ p, O.nn (x p)) (hC : O.nn C)
    (st : ∀ t p c, c < 3 → K.add (comp K (a (t + 1) p) c) (K.neg (comp K (a t p) c))
      = K.add (K.neg (K.mul (x p) (K.add (comp K (a (t + 1) p) c) (comp K (a t p) c)))) (K.mul h (comp K (F t p) c)))
    (neutral : ∀ t, sumN K (fun p => dot3 K (a t p) (F t p)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F t p) (F t p))) N)
      (K.mul C (K.mul (sumN K (fun p => dot3 K (a t p) (a t p)) N) (sumN K (fun p => dot3 K (a t p) (a t p)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)))
      K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun p => dot3 K (a t p) (a t p)) N)
      (K.add (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)) := by
  intro t ht
  have g := semi_global K O h10 (fun t n => fl (a t) n) (fun n => fl (vx x) n) (fun t n => fl (F t) n) h C (3 * N) T
    (fun n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show O.nn (go (vx x) n 0)
          rw [hc.2 (vx x), comp_vx K x p c hc.1]; exact hx p)
    hC
    (fun t n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show K.add (go (a (t + 1)) n 0) (K.neg (go (a t) n 0))
            = K.add (K.neg (K.mul (go (vx x) n 0) (K.add (go (a (t + 1)) n 0) (go (a t) n 0)))) (K.mul h (go (F t) n 0))
          rw [hc.2 (a (t + 1)), hc.2 (a t), hc.2 (vx x), hc.2 (F t), comp_vx K x p c hc.1]
          exact st t p c hc.1)
    (fun t => by rw [dot_flat K (a t) (F t) N]; exact neutral t)
    (fun t => by rw [weight_flat K x (F t) N, energy_flat K (a t) N]; exact nb t)
    (by rw [energy_flat K (a 0) N]; exact small) t ht
  rw [energy_flat K (a t) N, energy_flat K (a 0) N] at g
  exact g

end Gen

end Galerkin3D

#print axioms Galerkin3D.go_at
#print axioms Galerkin3D.go_cover
#print axioms Galerkin3D.sum_block
#print axioms Galerkin3D.energy_flat
#print axioms Galerkin3D.dot_flat
#print axioms Galerkin3D.weight_flat
#print axioms Galerkin3D.global3

#print axioms: 7 theorems, no axioms.

79.158 Cauchy–Schwarz for 3-vectors, double mode sums and the Galerkin force

\((a\cdot b)^2\le |a|^2|b|^2\), \((\sum _p\sum _q fg)^2\le (\sum \sum f^2)(\sum \sum g^2)\), hence \(|N_r|^2\le \iota ^2(\sum _p|u_p|^2)(\sum _q|k_q|^2)\sum _{p,q}|v_q|^2\) for triad indicators \(\delta \in \{0,1\}\).

ns/ForceBound.lean

/-
  ns/ForceBound.lean — T37 cycle 901 (goal G592c, first piece): Cauchy–Schwarz in the two forms the Galerkin force
  bound needs, over any commutative ring with a nonnegative cone (ℚ included):
    dot3_cs     (a·b)² ≤ |a|² |b|²                                      (3-vectors)
    double_cs   (Σ_{p∈l} Σ_{q∈l} f g)² ≤ (Σ_p Σ_q f²)(Σ_p Σ_q g²)        (double sums over a mode list)
  via lib/CauchySchwarz on explicit lists of pairs.

  Lean 4 core plus lib/CauchySchwarz (sA, sB, sC, cauchy_schwarz), ns/QuadBound (sA_app, sB_app), NSChain (lsum, Le).
-/
import ns.QuadBound
import ns.FourierGalerkin
import ns.GalerkinForce

namespace ForceBound
open DetChain NSChain LibRing LibCS FourierGalerkin QuadBound GalerkinForce DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def pos : Pos K := ⟨P.nn, P.nn_sq, P.nn_add, P.nn_mul, P.nn_zero⟩

theorem le_of_cs (A B C : R) (h : P.nn (sub K (K.mul A B) (K.mul C C))) : Le K P (K.mul C C) (K.mul A B) :=
  ⟨_, h, by unfold sub; lib_ring⟩

theorem dot3_cs (a b : V3 R) : Le K P (K.mul (dot3 K a b) (dot3 K a b)) (K.mul (dot3 K a a) (dot3 K b b)) := by
  have cs := cauchy_schwarz K (pos K P) [(a.x, b.x), (a.y, b.y), (a.z, b.z)]
  have eA : sA K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K a a := by
    show K.add (K.mul a.x a.x) (K.add (K.mul a.y a.y) (K.add (K.mul a.z a.z) K.zero)) = dot3 K a a
    unfold dot3; lib_ring
  have eB : sB K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K b b := by
    show K.add (K.mul b.x b.x) (K.add (K.mul b.y b.y) (K.add (K.mul b.z b.z) K.zero)) = dot3 K b b
    unfold dot3; lib_ring
  have eC : sC K [(a.x, b.x), (a.y, b.y), (a.z, b.z)] = dot3 K a b := by
    show K.add (K.mul a.x b.x) (K.add (K.mul a.y b.y) (K.add (K.mul a.z b.z) K.zero)) = dot3 K a b
    unfold dot3; lib_ring
  rw [eA, eB, eC] at cs
  exact le_of_cs K P _ _ _ cs

def prow (f g : I → I → R) (p : I) : List I → List (R × R)
  | [] => []
  | q :: m => (f p q, g p q) :: prow f g p m

def pgrid (f g : I → I → R) (l : List I) : List I → List (R × R)
  | [] => []
  | p :: m => prow f g p l ++ pgrid f g l m

theorem sA_prow (f g : I → I → R) (p : I) : ∀ m, sA K (prow f g p m) = lsum K (fun q => K.mul (f p q) (f p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (f p q) (f p q)) (sA K (prow f g p m)) = _
    rw [sA_prow f g p m]; rfl

theorem sB_prow (f g : I → I → R) (p : I) : ∀ m, sB K (prow f g p m) = lsum K (fun q => K.mul (g p q) (g p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (g p q) (g p q)) (sB K (prow f g p m)) = _
    rw [sB_prow f g p m]; rfl

theorem sC_prow (f g : I → I → R) (p : I) : ∀ m, sC K (prow f g p m) = lsum K (fun q => K.mul (f p q) (g p q)) m
  | [] => rfl
  | q :: m => by
    show K.add (K.mul (f p q) (g p q)) (sC K (prow f g p m)) = _
    rw [sC_prow f g p m]; rfl

theorem sC_app : ∀ l1 l2 : List (R × R), sC K (l1 ++ l2) = K.add (sC K l1) (sC K l2)
  | [], l2 => (K.zero_add _).symm
  | p :: l1, l2 => by
    show K.add (K.mul p.1 p.2) (sC K (l1 ++ l2)) = K.add (K.add (K.mul p.1 p.2) (sC K l1)) (sC K l2)
    rw [sC_app l1 l2, K.add_assoc]

theorem sA_grid (f g : I → I → R) (l : List I) : ∀ m,
    sA K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (f p q) (f p q)) l) m
  | [] => rfl
  | p :: m => by
    show sA K (prow f g p l ++ pgrid f g l m) = _
    rw [sA_app, sA_prow, sA_grid f g l m]; rfl

theorem sB_grid (f g : I → I → R) (l : List I) : ∀ m,
    sB K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (g p q) (g p q)) l) m
  | [] => rfl
  | p :: m => by
    show sB K (prow f g p l ++ pgrid f g l m) = _
    rw [sB_app, sB_prow, sB_grid f g l m]; rfl

theorem sC_grid (f g : I → I → R) (l : List I) : ∀ m,
    sC K (pgrid f g l m) = lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) m
  | [] => rfl
  | p :: m => by
    show sC K (prow f g p l ++ pgrid f g l m) = _
    rw [sC_app, sC_prow, sC_grid f g l m]; rfl

theorem double_cs (f g : I → I → R) (l : List I) :
    Le K P (K.mul (lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (f p q) (g p q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (f p q) (f p q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (g p q) (g p q)) l) l)) := by
  have cs := cauchy_schwarz K (pos K P) (pgrid f g l l)
  rw [sA_grid, sB_grid, sC_grid] at cs
  exact le_of_cs K P _ _ _ cs

theorem sum3_split (l : List I) (Acoef : R) (vx vy vz : I → R) :
    K.add (K.add (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vx q) (vx q)) l) l))
      (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vy q) (vy q)) l) l)))
      (K.mul Acoef (lsum K (fun _ => lsum K (fun q => K.mul (vz q) (vz q)) l) l))
    = K.mul Acoef (lsum K (fun _ => lsum K (fun q =>
        K.add (K.add (K.mul (vx q) (vx q)) (K.mul (vy q) (vy q))) (K.mul (vz q) (vz q))) l) l) := by
  rw [← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
  congr 1
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]

/-- |N_r|² ≤ (Σ_p Σ_q c(p,q,r)²) · (Σ_p Σ_q |v_q|²). -/
theorem force_sq (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r)) l) l)
        (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)) := by
  have cx := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).x) l
  have cy := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).y) l
  have cz := double_cs K P (fun p q => coef K δ ι k u p q r) (fun _ q => (v q).z) l
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  rw [sum3_split K l _ (fun q => (v q).x) (fun q => (v q).y) (fun q => (v q).z)] at s
  exact s

theorem lsum_le (f g : I → R) (h : ∀ i, Le K P (f i) (g i)) : ∀ l : List I, Le K P (lsum K f l) (lsum K g l)
  | [] => le_refl' K P _
  | i :: l => le_add' K P _ _ _ _ (h i) (lsum_le f g h l)

theorem lsum_nn (f : I → R) (h : ∀ i, P.nn (f i)) : ∀ l : List I, P.nn (lsum K f l)
  | [] => P.nn_zero
  | i :: l => P.nn_add _ _ (h i) (lsum_nn f h l)

theorem dot3_nn (a : V3 R) : P.nn (dot3 K a a) :=
  P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _)

/-- c(p,q,r)² ≤ ι² |u_p|² |k_q|² for a triad indicator δ ∈ {0, 1}. -/
theorem coef_sq (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (p q r : I)
    (hδ : δ p q r = K.zero ∨ δ p q r = K.one) :
    Le K P (K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r))
      (K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q)))) := by
  unfold coef
  cases hδ with
  | inl h0 =>
    rw [h0]
    refine ⟨K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q))),
      P.nn_mul _ _ (P.nn_sq ι) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _)), ?_⟩
    generalize dot3 K (u p) (k q) = w
    generalize K.mul (K.mul ι ι) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q))) = z
    lib_ring
  | inr h1 =>
    rw [h1]
    have cs := dot3_cs K P (u p) (k q)
    have l := le_mul_nn K P (K.mul ι ι) _ _ (P.nn_sq ι) cs
    cases l with
    | intro d hd =>
      refine ⟨d, hd.1, Eq.trans ?_ hd.2⟩
      generalize dot3 K (u p) (k q) = w
      lib_ring

theorem double_factor (l : List I) (c : R) (a b : I → R) :
    lsum K (fun p => lsum K (fun q => K.mul c (K.mul (a p) (b q))) l) l
      = K.mul c (K.mul (lsum K a l) (lsum K b l)) := by
  have e1 : ∀ p, lsum K (fun q => K.mul c (K.mul (a p) (b q))) l = K.mul (K.mul c (a p)) (lsum K b l) := by
    intro p
    rw [← lsum_mul_left]
    apply lsum_ext; intro q; lib_ring
  rw [lsum_ext K _ _ e1 l]
  have e2 : lsum K (fun p => K.mul (K.mul c (a p)) (lsum K b l)) l
      = lsum K (fun p => K.mul (K.mul c (lsum K b l)) (a p)) l := lsum_ext K _ _ (fun p => by lib_ring) l
  rw [e2, lsum_mul_left]; lib_ring

/-- |N_r|² ≤ ι² (Σ_p |u_p|²)(Σ_q |k_q|²) · Σ_p Σ_q |v_q|². -/
theorem force_mode (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (u p) (u p)) l) (lsum K (fun q => dot3 K (k q) (k q)) l)))
        (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)) := by
  have hc := lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => coef_sq K P δ ι k u p q r (hδ p q r)) l) l
  rw [double_factor K l (K.mul ι ι) (fun p => dot3 K (u p) (u p)) (fun q => dot3 K (k q) (k q))] at hc
  have hv : P.nn (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l) :=
    lsum_nn K P _ (fun _ => lsum_nn K P _ (fun q => dot3_nn K P (v q)) l) l
  have m := le_mul_nn K P _ _ _ hv hc
  rw [K.mul_comm _ (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (coef K δ ι k u p q r)) l) l),
    K.mul_comm (lsum K (fun _ => lsum K (fun q => dot3 K (v q) (v q)) l) l)] at m
  exact le_trans' K P _ _ _ (force_sq K P l δ ι k u v r) m

end Gen

end ForceBound

#print axioms ForceBound.dot3_cs
#print axioms ForceBound.sA_grid
#print axioms ForceBound.sB_grid
#print axioms ForceBound.sC_grid
#print axioms ForceBound.double_cs
#print axioms ForceBound.sum3_split
#print axioms ForceBound.force_sq
#print axioms ForceBound.lsum_le
#print axioms ForceBound.coef_sq
#print axioms ForceBound.double_factor
#print axioms ForceBound.force_mode

#print axioms: 11 theorems, no axioms.

79.159 Weighted bound for the projected Galerkin force

\(\sum _{r<N}w_r|P_rN_r(a,a)|^2\le (\sum _rw_r)\,\iota ^2K_k\,N\,E^2\) with \(K_k=\sum _q|k_q|^2\): the constant \(C\) of the 3D semi-implicit global bound.

ns/ForceSum.lean

/-
  ns/ForceSum.lean — T37 cycle 905 (goal G592d): the weighted force bound Galerkin3D needs, for the actual projected
  Galerkin force on the modes p < N, over any commutative ring with a nonnegative cone (ℚ included). With
  E = Σ_{p<N} |a_p|², K_k = Σ_{q<N} |k_q|², weights w_r ≥ 0 (w_r = x_r + 2) and κ_r ≥ 0, κ_r (k_r·k_r) = 1:
    Σ_{r<N} w_r |P_r N_r(a, a)|² ≤ (Σ_r w_r) · ι² · K_k · N · E².
  So Galerkin3D.global3 applies to the projected 3D Fourier–Galerkin scheme with C = (Σ_r w_r) ι² K_k N.

  Lean 4 core plus ns/ForceBound (force_mode), ns/LerayProj3 (proj_le), NSChain (sumN, lsum), lib/Ring (castN).
    rl, lsum_rl       the mode list [N−1, …, 0] and lsum over it = sumN
    lsum_const        Σ_{p<N} c = N·c
    force_weighted    the statement
-/
import ns.ForceBound
import ns.LerayProj3

namespace ForceSum
open DetChain NSChain LibRing FourierGalerkin GalerkinForce ForceBound LerayProj LerayProj3 DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def rl : Nat → List Nat
  | 0 => []
  | N + 1 => N :: rl N

theorem lsum_rl (f : Nat → R) : ∀ N, lsum K f (rl N) = sumN K f N
  | 0 => rfl
  | N + 1 => by
    show K.add (f N) (lsum K f (rl N)) = K.add (sumN K f N) (f N)
    rw [lsum_rl f N, K.add_comm]

theorem lsum_const (c : R) : ∀ N, lsum K (fun _ => c) (rl N) = K.mul (castN K N) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | N + 1 => by
    show K.add c (lsum K (fun _ => c) (rl N)) = K.mul (K.add K.one (castN K N)) c
    rw [lsum_const c N]; lib_ring

theorem force_weighted (δ : Nat → Nat → Nat → R) (ι : R) (k a : Nat → V3 R) (κ w : Nat → R) (N : Nat)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hκ : ∀ r, P.nn (κ r))
    (hκk : ∀ r, K.mul (dot3 K (k r) (k r)) (κ r) = K.one) (hw : ∀ r, P.nn (w r)) :
    Le K P (sumN K (fun r => K.mul (w r) (dot3 K (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))
        (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r)))) N)
      (K.mul (sumN K w N) (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N))
        (K.mul (castN K N) (K.mul (sumN K (fun p => dot3 K (a p) (a p)) N) (sumN K (fun p => dot3 K (a p) (a p)) N))))) := by
  generalize hE : sumN K (fun p => dot3 K (a p) (a p)) N = E
  generalize hKk : sumN K (fun q => dot3 K (k q) (k q)) N = Kk
  have pt : ∀ r, Le K P (K.mul (w r) (dot3 K (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))
        (proj K (κ r) (k r) (Nl K (rl N) δ ι k a a r))))
      (K.mul (w r) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)))) := by
    intro r
    apply le_mul_nn K P _ _ _ (hw r)
    have fm := force_mode K P (rl N) δ ι k a a r hδ
    rw [lsum_rl, lsum_rl, hE, hKk, lsum_const] at fm
    have e : K.mul (K.mul (K.mul ι ι) (K.mul E Kk)) (K.mul (castN K N) E)
        = K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)) := by lib_ring
    rw [e] at fm
    exact le_trans' K P _ _ _ (proj_le K P (κ r) (k r) _ (hκ r) (hκk r)) fm
  have s := sum_le K P _ _ pt N
  have e : sumN K (fun r => K.mul (w r) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E)))) N
      = K.mul (sumN K w N) (K.mul (K.mul (K.mul ι ι) Kk) (K.mul (castN K N) (K.mul E E))) := by
    rw [K.mul_comm (sumN K w N), ← sumN_mul_left]
    exact sumN_ext K _ _ (fun r => K.mul_comm _ _) N
  rw [e] at s
  exact s

end Gen

end ForceSum

#print axioms ForceSum.lsum_rl
#print axioms ForceSum.lsum_const
#print axioms ForceSum.force_weighted

#print axioms: 3 theorems, no axioms.

79.160 The projected semi-implicit 3D Galerkin scheme is globally bounded

Explicit next state \(a_{t+1}=((1-x)a_t+hPN(a_t,a_t))/(1+x)\): divergence-free, and \(E_t\le 2E_0\) for \(t\le T\) when \(4Th^2CE_0\le 1\), \(C=(\sum _r(x_r+2))\,\iota ^2K_k\,N\).

ns/NS3DScheme.lean

/-
  ns/NS3DScheme.lean — T37 cycle 906 (goal G592e): the projected semi-implicit scheme for the Fourier–Galerkin
  truncation of 3D Navier–Stokes on the modes p < N, with an explicit next state, over any ordered field (ℚ included):
    a_{t+1,p} = ((1 − x_p) a_{t,p} + h · P_p N_p(a_t, a_t)) / (1 + x_p)
  (x_p = νh|k_p|²/2 ≥ 0, P_p the Leray projection, N_p the truncated nonlinearity with triad indicator δ and
  coefficient ι). If a_0 is divergence-free and 4T·h²·C·E_0 ≤ 1 with the explicit truncation constant
    C = (Σ_{r<N} (x_r + 2)) · ι² · (Σ_{q<N} |k_q|²) · N,
  then every a_t is divergence-free and E_t ≤ 2E_0 for all t ≤ T.

  Lean 4 core plus ns/Galerkin3D (global3), ns/ProjStep (comb, comb_div, comb_step), ns/GalerkinForce
  (proj_neutral), ns/ForceSum (force_weighted, rl, lsum_rl), ns/RatModel (nn_one').
-/
import ns.Galerkin3D
import ns.ProjStep
import ns.ForceSum

namespace NS3DScheme
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin LerayProj GalerkinForce ProjStep ForceSum Galerkin3D Flatten3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- The scheme. -/
def sch (inv : R → R) (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat)
    (a0 : Nat → V3 R) : Nat → Nat → V3 R
  | 0 => a0
  | t + 1 => fun p => comb K (inv (K.add K.one (x p))) (x p) h (sch inv δ ι h k x κ N a0 t p)
      (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch inv δ ι h k x κ N a0 t) (sch inv δ ι h k x κ N a0 t) p))

theorem sch_div (inv : R → R) (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat)
    (a0 : Nat → V3 R) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero) : ∀ t p, dot3 K (k p) (sch K inv δ ι h k x κ N a0 t p) = K.zero
  | 0, p => h0 p
  | t + 1, p => comb_div K _ (κ p) (x p) h (k p) _ _ (sch_div inv δ ι h k x κ N a0 hκk h0 t p) (hκk p)

theorem castN_nn : ∀ n, O.nn (castN K n)
  | 0 => O.nn_zero
  | n + 1 => O.nn_add _ _ (RatModel.nn_one' K O.toCone) (castN_nn n)

include O

/-- **Global bound for the projected 3D Galerkin scheme.** -/
theorem global (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
      (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have hdiv := sch_div K inv δ ι h k x κ N a0 hκk h0
  have hw : ∀ r, O.nn (K.add (x r) (K.add K.one K.one)) := fun r =>
    O.nn_add _ _ (hx r) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
  let F : Nat → Nat → V3 R := fun t p =>
    proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)
  have hC : O.nn (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
      (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))) :=
    O.nn_mul _ _ (sumN_nn K O.toCone _ hw N) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ι)
      (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)) (castN_nn K O N))
  refine global3 K O h10 (sch K inv δ ι h k x κ N a0) x F h _ N T hx hC ?_ ?_ ?_ small
  · intro t p c hc
    have e := comb_step K O inv mul_inv h10 (x p) h (comp K (sch K inv δ ι h k x κ N a0 t p) c) (comp K (F t p) c) (hx p)
    have ec : comp K (sch K inv δ ι h k x κ N a0 (t + 1) p) c
        = K.mul (K.add (K.mul (K.add K.one (K.neg (x p))) (comp K (sch K inv δ ι h k x κ N a0 t p) c))
          (K.mul h (comp K (F t p) c))) (inv (K.add K.one (x p))) := by
      cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => cases c with
          | zero => rfl
          | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
    rw [ec]; exact e
  · intro t
    rw [← lsum_rl K _ N]
    exact proj_neutral K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) κ
      (fun p => by rw [dot3_comm K]; exact hdiv t p) htri hsym h2
  · intro t
    have fw := force_weighted K O.toCone δ ι k (sch K inv δ ι h k x κ N a0 t) κ (fun r => K.add (x r) (K.add K.one K.one)) N
      hδ hκ hκk hw
    generalize sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N = E at fw ⊢
    cases fw with
    | intro d hd =>
      refine ⟨d, hd.1, hd.2.trans ?_⟩
      generalize sumN K (fun r => K.add (x r) (K.add K.one K.one)) N = W
      generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
      generalize castN K N = n
      lib_ring

end Gen

end NS3DScheme

#print axioms NS3DScheme.sch_div
#print axioms NS3DScheme.castN_nn
#print axioms NS3DScheme.global

#print axioms: 3 theorems, no axioms.

79.161 Dissipation budget of the projected 3D Galerkin scheme

\(\sum _{t<T}\sum _{p<N}x_p|a_{t+1,p}+a_{t,p}|^2\le 3E_0\) under the hypotheses of the global bound: the discrete \(\nu \int \|\nabla u\|^2\le \frac 32\|u_0\|^2\).

ns/Galerkin3DB.lean

/-
  ns/Galerkin3DB.lean — T37 cycle 910 (goal G608): the dissipation budget for the semi-implicit scheme on 3D mode
  arrays (the 3D form of SemiBudgetArr), over any ordered ring without zero divisors (ℚ included): under the
  hypotheses of Galerkin3D.global3,
    Σ_{t<T} Σ_{p<N} x_p |a_{t+1,p} + a_{t,p}|² ≤ 3E_0
  — the discrete ν∫‖∇u‖² dt ≤ (3/2)‖u_0‖² (x_p = νh|k_p|²/2), and (budget) its corollary for NS3DScheme.

  Lean 4 core plus ns/Galerkin3D (flattening), ns/SemiBudgetArr (semi_budget), ns/NS3DScheme.
-/
import ns.Galerkin3D
import ns.SemiBudgetArr
import ns.NS3DScheme

namespace Galerkin3DB
open DetChain NSChain DyadicSmallGlobal LibRing FourierGalerkin Flatten3 Galerkin3D SemiBudgetArr

section Gen
variable {R : Type} (K : CRing R)

theorem diss_flat (x : Nat → R) (a b : Nat → V3 R) (N : Nat) :
    sumN K (fun n => K.mul (fl (vx x) n) (K.mul (K.add (fl b n) (fl a n)) (K.add (fl b n) (fl a n)))) (3 * N)
      = sumN K (fun p => K.mul (x p) (dot3 K (add3 K (b p) (a p)) (add3 K (b p) (a p)))) N := by
  rw [sum_block K _ (fun p c => K.mul (x p) (K.mul (K.add (comp K (b p) c) (comp K (a p) c)) (K.add (comp K (b p) c) (comp K (a p) c))))
    (fun p c hc => by
      show K.mul (fl (vx x) (3 * p + c)) (K.mul (K.add (fl b (3 * p + c)) (fl a (3 * p + c))) (K.add (fl b (3 * p + c)) (fl a (3 * p + c)))) = _
      rw [fl_at K (vx x) p c hc, fl_at K b p c hc, fl_at K a p c hc, comp_vx K x p c hc]) N]
  apply sumN_ext
  intro p
  show K.add (K.add (K.mul (x p) (K.mul (K.add (b p).x (a p).x) (K.add (b p).x (a p).x)))
      (K.mul (x p) (K.mul (K.add (b p).y (a p).y) (K.add (b p).y (a p).y))))
      (K.mul (x p) (K.mul (K.add (b p).z (a p).z) (K.add (b p).z (a p).z)))
    = K.mul (x p) (K.add (K.add (K.mul (K.add (b p).x (a p).x) (K.add (b p).x (a p).x))
      (K.mul (K.add (b p).y (a p).y) (K.add (b p).y (a p).y))) (K.mul (K.add (b p).z (a p).z) (K.add (b p).z (a p).z)))
  generalize (b p).x = b1; generalize (b p).y = b2; generalize (b p).z = b3
  generalize (a p).x = a1; generalize (a p).y = a2; generalize (a p).z = a3; generalize x p = y
  lib_ring

variable (O : OrdR K)
include O

theorem budget3d (h10 : K.one ≠ K.zero) (a : Nat → Nat → V3 R) (x : Nat → R) (F : Nat → Nat → V3 R)
    (h C : R) (N T : Nat) (hx : ∀ p, O.nn (x p)) (hC : O.nn C)
    (st : ∀ t p c, c < 3 → K.add (comp K (a (t + 1) p) c) (K.neg (comp K (a t p) c))
      = K.add (K.neg (K.mul (x p) (K.add (comp K (a (t + 1) p) c) (comp K (a t p) c)))) (K.mul h (comp K (F t p) c)))
    (neutral : ∀ t, sumN K (fun p => dot3 K (a t p) (F t p)) N = K.zero)
    (nb : ∀ t, Le K O.toCone (sumN K (fun p => K.mul (K.add (x p) (K.add K.one K.one)) (dot3 K (F t p) (F t p))) N)
      (K.mul C (K.mul (sumN K (fun p => dot3 K (a t p) (a t p)) N) (sumN K (fun p => dot3 K (a t p) (a t p)) N))))
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h) C) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)))
      K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun p => K.mul (x p) (dot3 K (add3 K (a (t + 1) p) (a t p)) (add3 K (a (t + 1) p) (a t p)))) N) T)
      (K.add (K.add (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N) (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N))
        (sumN K (fun p => dot3 K (a 0 p) (a 0 p)) N)) := by
  have g := semi_budget K O h10 (fun t n => fl (a t) n) (fun n => fl (vx x) n) (fun t n => fl (F t) n) h C (3 * N) T
    (fun n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show O.nn (go (vx x) n 0)
          rw [hc.2 (vx x), comp_vx K x p c hc.1]; exact hx p)
    hC
    (fun t n => by
      cases go_cover K n 0 with
      | intro p hp => cases hp with
        | intro c hc =>
          show K.add (go (a (t + 1)) n 0) (K.neg (go (a t) n 0))
            = K.add (K.neg (K.mul (go (vx x) n 0) (K.add (go (a (t + 1)) n 0) (go (a t) n 0)))) (K.mul h (go (F t) n 0))
          rw [hc.2 (a (t + 1)), hc.2 (a t), hc.2 (vx x), hc.2 (F t), comp_vx K x p c hc.1]
          exact st t p c hc.1)
    (fun t => by rw [dot_flat K (a t) (F t) N]; exact neutral t)
    (fun t => by rw [weight_flat K x (F t) N, energy_flat K (a t) N]; exact nb t)
    (by rw [energy_flat K (a 0) N]; exact small)
  rw [energy_flat K (a 0) N, sumN_ext K _ _ (fun t => diss_flat K x (a t) (a (t + 1)) N) T] at g
  exact g

end Gen

section Scheme
open LerayProj GalerkinForce ProjStep ForceSum NS3DScheme
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

/-- **Dissipation budget for the projected 3D Galerkin scheme.** -/
theorem scheme_budget (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (h0 : ∀ p, dot3 K (k p) (a0 p) = K.zero)
    (small : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one) :
    Le K O.toCone (sumN K (fun t => sumN K (fun p => K.mul (x p) (dot3 K (add3 K (sch K inv δ ι h k x κ N a0 (t + 1) p)
        (sch K inv δ ι h k x κ N a0 t p)) (add3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N a0 t p)))) N) T)
      (K.add (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have hdiv := sch_div K inv δ ι h k x κ N a0 hκk h0
  have hw : ∀ r, O.nn (K.add (x r) (K.add K.one K.one)) := fun r =>
    O.nn_add _ _ (hx r) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
  let F : Nat → Nat → V3 R := fun t p =>
    proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)
  have hC : O.nn (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
      (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))) :=
    O.nn_mul _ _ (sumN_nn K O.toCone _ hw N) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ι)
      (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)) (castN_nn K O N))
  refine budget3d K O h10 (sch K inv δ ι h k x κ N a0) x F h _ N T hx hC ?_ ?_ ?_ small
  · intro t p c hc
    have e := comb_step K O inv mul_inv h10 (x p) h (comp K (sch K inv δ ι h k x κ N a0 t p) c) (comp K (F t p) c) (hx p)
    have ec : comp K (sch K inv δ ι h k x κ N a0 (t + 1) p) c
        = K.mul (K.add (K.mul (K.add K.one (K.neg (x p))) (comp K (sch K inv δ ι h k x κ N a0 t p) c))
          (K.mul h (comp K (F t p) c))) (inv (K.add K.one (x p))) := by
      cases c with
      | zero => rfl
      | succ c => cases c with
        | zero => rfl
        | succ c => cases c with
          | zero => rfl
          | succ c => exact absurd hc (Nat.not_lt_of_le (Nat.le_add_left 3 c))
    rw [ec]; exact e
  · intro t
    rw [← lsum_rl K _ N]
    exact proj_neutral K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) κ
      (fun p => by rw [dot3_comm K]; exact hdiv t p) htri hsym h2
  · intro t
    have fw := force_weighted K O.toCone δ ι k (sch K inv δ ι h k x κ N a0 t) κ (fun r => K.add (x r) (K.add K.one K.one)) N
      hδ hκ hκk hw
    generalize sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N = E at fw ⊢
    cases fw with
    | intro d hd =>
      refine ⟨d, hd.1, hd.2.trans ?_⟩
      generalize sumN K (fun r => K.add (x r) (K.add K.one K.one)) N = W
      generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
      generalize castN K N = n
      lib_ring

end Scheme

end Galerkin3DB

#print axioms Galerkin3DB.diss_flat
#print axioms Galerkin3DB.budget3d
#print axioms Galerkin3DB.scheme_budget

#print axioms: 3 theorems, no axioms.

79.162 Bilinearity of the Galerkin force

\(N(u_1+u_2,v)=N(u_1,v)+N(u_2,v)\), \(N(u,v_1+v_2)=N(u,v_1)+N(u,v_2)\), hence \(N(b+d,b+d)=N(b,b)+N(d,b+d)+N(b,d)\).

ns/ForceBilin.lean

/-
  ns/ForceBilin.lean — T37 cycle 918 (goal G614): the Galerkin force N(u, v) of GalerkinForce is bilinear, over any
  commutative ring:
    Nl_add_left    N(u₁ + u₂, v) = N(u₁, v) + N(u₂, v)
    Nl_add_right   N(u, v₁ + v₂) = N(u, v₁) + N(u, v₂)
    Nl_diff        N(a, a) = N(b, b) + N(a − b, a) + N(b, a − b)   (with a = b + d)
  so the difference of two forces is controlled by ForceBound.force_mode applied to N(d, a) and N(b, d): the first
  step of continuous dependence (discrete uniqueness/stability) for the projected 3D scheme.

  Lean 4 core plus ns/GalerkinForce (Nl, coef), NSChain (lsum lemmas), lib/RingTac.
-/
import ns.GalerkinForce

namespace ForceBilin
open DetChain NSChain LibRing FourierGalerkin GalerkinForce

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def addF (u w : I → V3 R) : I → V3 R := fun p => add3 K (u p) (w p)

theorem lsum2_add (l : List I) (f g : I → I → R) :
    lsum K (fun p => lsum K (fun q => K.add (f p q) (g p q)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => f p q) l) l) (lsum K (fun p => lsum K (fun q => g p q) l) l) := by
  rw [← lsum_add]
  apply lsum_ext; intro p
  exact lsum_add K _ _ l

theorem v3_ext (a b : V3 R) (hx : a.x = b.x) (hy : a.y = b.y) (hz : a.z = b.z) : a = b := by
  cases a; cases b
  cases hx; cases hy; cases hz; rfl

theorem Nl_add_left (l : List I) (δ : I → I → I → R) (ι : R) (k u₁ u₂ v : I → V3 R) (r : I) :
    Nl K l δ ι k (addF K u₁ u₂) v r = add3 K (Nl K l δ ι k u₁ v r) (Nl K l δ ι k u₂ v r) := by
  have hc : ∀ p q, coef K δ ι k (addF K u₁ u₂) p q r = K.add (coef K δ ι k u₁ p q r) (coef K δ ι k u₂ p q r) := by
    intro p q
    show K.mul (δ p q r) (K.mul ι (dot3 K (add3 K (u₁ p) (u₂ p)) (k q)))
      = K.add (K.mul (δ p q r) (K.mul ι (dot3 K (u₁ p) (k q)))) (K.mul (δ p q r) (K.mul ι (dot3 K (u₂ p) (k q))))
    rw [dot3_comm K (add3 K (u₁ p) (u₂ p)), dot3_add, dot3_comm K (k q), dot3_comm K (k q)]
    generalize dot3 K (u₁ p) (k q) = e1; generalize dot3 K (u₂ p) (k q) = e2
    lib_ring
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).x) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).x) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).x) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).y) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).y) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).y) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (addF K u₁ u₂) p q r) (v q).z) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₁ p q r) (v q).z) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u₂ p q r) (v q).z) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc]; lib_ring

theorem Nl_add_right (l : List I) (δ : I → I → I → R) (ι : R) (k u v₁ v₂ : I → V3 R) (r : I) :
    Nl K l δ ι k u (addF K v₁ v₂) r = add3 K (Nl K l δ ι k u v₁ r) (Nl K l δ ι k u v₂ r) := by
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).x (v₂ q).x)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).x) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).x) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).y (v₂ q).y)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).y) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).y) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (K.add (v₁ q).z (v₂ q).z)) l) l
      = K.add (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₁ q).z) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v₂ q).z) l) l)
    rw [← lsum2_add]; apply lsum_ext; intro p; apply lsum_ext; intro q; exact K.distrib _ _ _

/-- With a = b + d: N(a, a) = N(b, b) + N(d, a) + N(b, d). -/
theorem Nl_diff (l : List I) (δ : I → I → I → R) (ι : R) (k b d : I → V3 R) (r : I) :
    Nl K l δ ι k (addF K b d) (addF K b d) r
      = add3 K (add3 K (Nl K l δ ι k b b r) (Nl K l δ ι k d (addF K b d) r)) (Nl K l δ ι k b d r) := by
  rw [Nl_add_left, Nl_add_right K l δ ι k b b d]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k b d r = B; generalize Nl K l δ ι k d (addF K b d) r = C
  apply v3_ext
  · show K.add (K.add A.x B.x) C.x = K.add (K.add A.x C.x) B.x
    lib_ring
  · show K.add (K.add A.y B.y) C.y = K.add (K.add A.y C.y) B.y
    lib_ring
  · show K.add (K.add A.z B.z) C.z = K.add (K.add A.z C.z) B.z
    lib_ring

end Gen

end ForceBilin

#print axioms ForceBilin.lsum2_add
#print axioms ForceBilin.v3_ext
#print axioms ForceBilin.Nl_add_left
#print axioms ForceBilin.Nl_add_right
#print axioms ForceBilin.Nl_diff

#print axioms: 5 theorems, no axioms.

79.163 One-step stability of the semi-implicit step

If \((1+x)\iota =1\), \(x,h\ge 0\) and \(d'=((1-x)d+hG)\iota \), then \(d'^2\le (1+h)d^2+(h+h^2)G^2\).

ns/StepStab.lean

/-
  ns/StepStab.lean — T37 cycle 920 (goal G614b, piece 1): one-step stability of the semi-implicit step for a
  difference, over any commutative ring with a nonnegative cone (ℚ included). If (1 + x)·ι = 1, x ≥ 0, h ≥ 0 and
    d' = ((1 − x)·d + h·G)·ι,
  then  d'² ≤ (1 + h)·d² + (h + h²)·G².
  Summed over modes and components this is the step of the continuous-dependence estimate for two solutions of the
  projected 3D scheme (G = difference of the projected forces, bounded by ForceBound via ForceBilin.Nl_diff).

  Lean 4 core plus NSChain (Cone, Le), ns/DyadicLarge (le_add', le_mul_nn), lib/RingTac.
    sq_le_one_of    ι²(1 + x)² = 1, x ≥ 0  ⇒  ι² ≤ 1 and ((1 − x)ι)² ≤ 1
    two_mul_le      2uv ≤ u² + v²
    step_diff       the statement
-/
import ns.DyadicLarge
import lib.RingTac

namespace StepStab
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem one_nn : P.nn K.one := by
  have h := P.nn_sq K.one
  rwa [K.one_mul] at h

theorem two_mul_le (u v : R) : Le K P (K.mul (K.add K.one K.one) (K.mul u v)) (K.add (K.mul u u) (K.mul v v)) :=
  ⟨K.mul (K.add u (K.neg v)) (K.add u (K.neg v)), P.nn_sq _, by lib_ring⟩

theorem iota_sq_le (x ι : R) (hx : P.nn x) (hι : K.mul (K.add K.one x) ι = K.one) : Le K P (K.mul ι ι) K.one := by
  -- ι² (1 + x)² = 1 and (1 + x)² = 1 + (2x + x²)
  refine ⟨K.mul (K.mul ι ι) (K.add (K.add x x) (K.mul x x)),
    P.nn_mul _ _ (P.nn_sq ι) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_sq x)), ?_⟩
  have e : K.add (K.mul ι ι) (K.mul (K.mul ι ι) (K.add (K.add x x) (K.mul x x)))
      = K.mul (K.mul (K.add K.one x) ι) (K.mul (K.add K.one x) ι) := by lib_ring
  rw [e, hι, K.one_mul]

theorem c_sq_le (x ι : R) (hx : P.nn x) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (K.mul (K.mul (K.add K.one (K.neg x)) ι) (K.mul (K.add K.one (K.neg x)) ι)) K.one := by
  -- (1 − x)² ι² + 4x ι² = (1 + x)² ι² = 1
  refine ⟨K.mul (K.mul ι ι) (K.add (K.add x x) (K.add x x)),
    P.nn_mul _ _ (P.nn_sq ι) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_add _ _ hx hx)), ?_⟩
  have e : K.add (K.mul (K.mul (K.add K.one (K.neg x)) ι) (K.mul (K.add K.one (K.neg x)) ι))
      (K.mul (K.mul ι ι) (K.add (K.add x x) (K.add x x)))
      = K.mul (K.mul (K.add K.one x) ι) (K.mul (K.add K.one x) ι) := by lib_ring
  rw [e, hι, K.one_mul]

theorem step_diff (x ι h d G : R) (hx : P.nn x) (hh : P.nn h) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι))
      (K.add (K.mul (K.add K.one h) (K.mul d d)) (K.mul (K.add h (K.mul h h)) (K.mul G G))) := by
  have hc2 := c_sq_le K P x ι hx hι
  have he2 := iota_sq_le K P x ι hx hι
  have e1 : K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d) (K.mul h G)) ι)
      = K.add (K.add (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) ι) d) (K.mul (K.mul (K.add K.one (K.neg x)) ι) d))
          (K.mul h (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.add K.one (K.neg x)) ι) d) (K.mul ι G)))))
        (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))) := by lib_ring
  rw [e1]
  generalize K.mul (K.add K.one (K.neg x)) ι = c at hc2 ⊢
  have y := le_mul_nn K P h _ _ hh (two_mul_le K P (K.mul c d) (K.mul ι G))
  have S1 : P.nn (K.mul (K.add K.one h) (K.mul d d)) := P.nn_mul _ _ (P.nn_add _ _ (one_nn K P) hh) (P.nn_sq d)
  have S2 : P.nn (K.mul (K.add h (K.mul h h)) (K.mul G G)) := P.nn_mul _ _ (P.nn_add _ _ hh (P.nn_sq h)) (P.nn_sq G)
  have z1 := le_mul_nn K P _ _ _ S1 hc2
  have z2 := le_mul_nn K P _ _ _ S2 he2
  cases y with
  | intro w hw => cases z1 with
    | intro w1 hw1 => cases z2 with
      | intro w2 hw2 =>
        refine ⟨K.add w (K.add w1 w2), P.nn_add _ _ hw.1 (P.nn_add _ _ hw1.1 hw2.1), ?_⟩
        have t1 := hw.2; have t2 := hw1.2; have t3 := hw2.2
        generalize K.mul h (K.mul (K.add K.one K.one) (K.mul (K.mul c d) (K.mul ι G))) = B at t1 ⊢
        calc _ = K.add (K.add (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))))
                (K.add B w)) (K.add w1 w2) := by lib_ring
          _ = K.add (K.add (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul h h) (K.mul (K.mul ι G) (K.mul ι G))))
                (K.mul h (K.add (K.mul (K.mul c d) (K.mul c d)) (K.mul (K.mul ι G) (K.mul ι G))))) (K.add w1 w2) := by rw [t1]
          _ = K.add (K.add (K.mul (K.mul (K.add K.one h) (K.mul d d)) (K.mul c c)) w1)
                (K.add (K.mul (K.mul (K.add h (K.mul h h)) (K.mul G G)) (K.mul ι ι)) w2) := by lib_ring
          _ = _ := by rw [t2, t3, mul_one' K, mul_one' K]

end Gen

end StepStab

#print axioms StepStab.two_mul_le
#print axioms StepStab.iota_sq_le
#print axioms StepStab.c_sq_le
#print axioms StepStab.step_diff

#print axioms: 4 theorems, no axioms.

79.164 Stability of the projected 3D step for differences

The step is linear, and \(\sum _p|\mathrm {comb}_p(d_p,G_p)|^2\le (1+h)\sum _p|d_p|^2+(h+h^2)\sum _p|G_p|^2\).

ns/StepStab3.lean

/-
  ns/StepStab3.lean — T37 cycle 922 (goal G614b, piece 2): the 3D form of StepStab, over any commutative ring with
  a nonnegative cone (ℚ included). For the projected step comb (ProjStep) with (1 + x_p)ι_p = 1:
    comb_sub      comb(a, G) − comb(b, H) = comb(a − b, G − H)          (the step is linear)
    mode_stab     |comb(d, G)|² ≤ (1 + h)|d|² + (h + h²)|G|²
    sum_stab      Σ_{p<N} |comb_p(d_p, G_p)|² ≤ (1 + h) Σ_p |d_p|² + (h + h²) Σ_p |G_p|²
  So two solutions a, b of the projected 3D scheme satisfy
    D_{t+1} ≤ (1 + h) D_t + (h + h²) Σ_p |P N_p(a_t) − P N_p(b_t)|²,   D_t = Σ_p |a_{t,p} − b_{t,p}|².

  Lean 4 core plus ns/StepStab (step_diff), ns/ProjStep (comb), ns/DyadicLarge (sum_le, le_add').
-/
import ns.StepStab
import ns.ProjStep

namespace StepStab3
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ProjStep StepStab

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def sub3 (a b : V3 R) : V3 R := ⟨K.add a.x (K.neg b.x), K.add a.y (K.neg b.y), K.add a.z (K.neg b.z)⟩

theorem comb_sub (ι x h : R) (a b G H : V3 R) :
    sub3 K (comb K ι x h a G) (comb K ι x h b H) = comb K ι x h (sub3 K a b) (sub3 K G H) := by
  cases a with
  | mk a1 a2 a3 => cases b with
    | mk b1 b2 b3 => cases G with
      | mk g1 g2 g3 => cases H with
        | mk h1 h2 h3 =>
          show (⟨K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a1) (K.mul h g1)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b1) (K.mul h h1)) ι)),
                K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a2) (K.mul h g2)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b2) (K.mul h h2)) ι)),
                K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a3) (K.mul h g3)) ι)
                  (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b3) (K.mul h h3)) ι))⟩ : V3 R)
            = ⟨K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a1 (K.neg b1))) (K.mul h (K.add g1 (K.neg h1)))) ι,
               K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a2 (K.neg b2))) (K.mul h (K.add g2 (K.neg h2)))) ι,
               K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a3 (K.neg b3))) (K.mul h (K.add g3 (K.neg h3)))) ι⟩
          have e1 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a1) (K.mul h g1)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b1) (K.mul h h1)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a1 (K.neg b1))) (K.mul h (K.add g1 (K.neg h1)))) ι := by lib_ring
          have e2 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a2) (K.mul h g2)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b2) (K.mul h h2)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a2 (K.neg b2))) (K.mul h (K.add g2 (K.neg h2)))) ι := by lib_ring
          have e3 : K.add (K.mul (K.add (K.mul (K.add K.one (K.neg x)) a3) (K.mul h g3)) ι)
              (K.neg (K.mul (K.add (K.mul (K.add K.one (K.neg x)) b3) (K.mul h h3)) ι))
              = K.mul (K.add (K.mul (K.add K.one (K.neg x)) (K.add a3 (K.neg b3))) (K.mul h (K.add g3 (K.neg h3)))) ι := by lib_ring
          rw [e1, e2, e3]

theorem mode_stab (ι x h : R) (d G : V3 R) (hx : P.nn x) (hh : P.nn h) (hι : K.mul (K.add K.one x) ι = K.one) :
    Le K P (dot3 K (comb K ι x h d G) (comb K ι x h d G))
      (K.add (K.mul (K.add K.one h) (dot3 K d d)) (K.mul (K.add h (K.mul h h)) (dot3 K G G))) := by
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (step_diff K P x ι h d.x G.x hx hh hι) (step_diff K P x ι h d.y G.y hx hh hι))
    (step_diff K P x ι h d.z G.z hx hh hι)
  cases s with
  | intro w hw =>
    refine ⟨w, hw.1, ?_⟩
    have t := hw.2
    show K.add (K.add (K.add (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.x) (K.mul h G.x)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.x) (K.mul h G.x)) ι))
        (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.y) (K.mul h G.y)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.y) (K.mul h G.y)) ι)))
        (K.mul (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.z) (K.mul h G.z)) ι)
        (K.mul (K.add (K.mul (K.add K.one (K.neg x)) d.z) (K.mul h G.z)) ι))) w
      = K.add (K.mul (K.add K.one h) (K.add (K.add (K.mul d.x d.x) (K.mul d.y d.y)) (K.mul d.z d.z)))
        (K.mul (K.add h (K.mul h h)) (K.add (K.add (K.mul G.x G.x) (K.mul G.y G.y)) (K.mul G.z G.z)))
    rw [t]; lib_ring

theorem sum_stab (ι x : Nat → R) (h : R) (d G : Nat → V3 R) (hx : ∀ p, P.nn (x p)) (hh : P.nn h)
    (hι : ∀ p, K.mul (K.add K.one (x p)) (ι p) = K.one) (N : Nat) :
    Le K P (sumN K (fun p => dot3 K (comb K (ι p) (x p) h (d p) (G p)) (comb K (ι p) (x p) h (d p) (G p))) N)
      (K.add (K.mul (K.add K.one h) (sumN K (fun p => dot3 K (d p) (d p)) N))
        (K.mul (K.add h (K.mul h h)) (sumN K (fun p => dot3 K (G p) (G p)) N))) := by
  have s := sum_le K P _ _ (fun p => mode_stab K P (ι p) (x p) h (d p) (G p) (hx p) hh (hι p)) N
  rw [sumN_add K, sumN_mul_left, sumN_mul_left] at s
  exact s

end Gen

end StepStab3

#print axioms StepStab3.comb_sub
#print axioms StepStab3.mode_stab
#print axioms StepStab3.sum_stab

#print axioms: 3 theorems, no axioms.

79.165 The projected Galerkin force is locally Lipschitz

\(N(a,a)-N(b,b)=N(a-b,a)+N(b,a-b)\), and \(|P_rN_r(a)-P_rN_r(b)|^2\le 2\iota ^2K_k\,[\,D\,V_a+E_b\,V_d\,]\) with \(D=\sum |a-b|^2\).

ns/ForceLip.lean

/-
  ns/ForceLip.lean — T37 cycle 923 (goal G614b, piece 3): the projected Galerkin force is locally Lipschitz, over
  any commutative ring with a nonnegative cone (ℚ included). For two states a, b with d = a − b, on a mode list l:
    Nl_sub     N(a, a) − N(b, b) = N(d, a) + N(b, d)
    lip_mode   |P_r N_r(a, a) − P_r N_r(b, b)|²
                 ≤ 2ι² (Σ_q|k_q|²) [ (Σ_p|d_p|²)(Σ_pΣ_q|a_q|²) + (Σ_p|b_p|²)(Σ_pΣ_q|d_q|²) ]
  (Leray projection linear and contracting, ForceBilin.Nl_diff, ForceBound.force_mode, |X + Y|² ≤ 2|X|² + 2|Y|²).

  Lean 4 core plus ns/ForceBilin, ns/ForceBound, ns/LerayProj3 (proj_le), ns/StepStab3 (sub3).
-/
import ns.ForceBilin
import ns.ForceBound
import ns.LerayProj3
import ns.StepStab3

namespace ForceLip
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBilin ForceBound LerayProj LerayProj3 StepStab3

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem Nl_congr (l : List I) (δ : I → I → I → R) (ι : R) (k u u' v v' : I → V3 R) (r : I)
    (hu : ∀ p, u p = u' p) (hv : ∀ q, v q = v' q) : Nl K l δ ι k u v r = Nl K l δ ι k u' v' r := by
  have hc : ∀ p q, coef K δ ι k u p q r = coef K δ ι k u' p q r := by
    intro p q; unfold coef; rw [hu p]
  apply v3_ext
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).x) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).x) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).y) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).y) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]
  · show lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (v q).z) l) l
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u' p q r) (v' q).z) l) l
    apply lsum_ext; intro p; apply lsum_ext; intro q; rw [hc, hv]

theorem add_sub3 (b a : V3 R) : add3 K b (sub3 K a b) = a := by
  apply v3_ext
  · show K.add b.x (K.add a.x (K.neg b.x)) = a.x
    lib_ring
  · show K.add b.y (K.add a.y (K.neg b.y)) = a.y
    lib_ring
  · show K.add b.z (K.add a.z (K.neg b.z)) = a.z
    lib_ring

theorem Nl_sub (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K l δ ι k b b r)
      = add3 K (Nl K l δ ι k (fun p => sub3 K (a p) (b p)) a r) (Nl K l δ ι k b (fun p => sub3 K (a p) (b p)) r) := by
  have ha : ∀ p, addF K b (fun p => sub3 K (a p) (b p)) p = a p := fun p => add_sub3 K (b p) (a p)
  have e := Nl_diff K l δ ι k b (fun p => sub3 K (a p) (b p)) r
  rw [Nl_congr K l δ ι k _ a _ a r ha ha,
    Nl_congr K l δ ι k (fun p => sub3 K (a p) (b p)) (fun p => sub3 K (a p) (b p)) _ a r (fun _ => rfl) ha] at e
  rw [e]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k (fun p => sub3 K (a p) (b p)) a r = B
  generalize Nl K l δ ι k b (fun p => sub3 K (a p) (b p)) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x
    lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y
    lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z
    lib_ring

theorem proj_sub (κ : R) (k v w : V3 R) : sub3 K (proj K κ k v) (proj K κ k w) = proj K κ k (sub3 K v w) := by
  cases k with
  | mk k1 k2 k3 => cases v with
    | mk v1 v2 v3 => cases w with
      | mk w1 w2 w3 =>
        apply v3_ext
        · show K.add (K.add v1 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k1)))
              (K.neg (K.add w1 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k1))))
            = K.add (K.add v1 (K.neg w1)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k1))
          lib_ring
        · show K.add (K.add v2 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k2)))
              (K.neg (K.add w2 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k2))))
            = K.add (K.add v2 (K.neg w2)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k2))
          lib_ring
        · show K.add (K.add v3 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 v1) (K.mul k2 v2)) (K.mul k3 v3)) κ) k3)))
              (K.neg (K.add w3 (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 w1) (K.mul k2 w2)) (K.mul k3 w3)) κ) k3))))
            = K.add (K.add v3 (K.neg w3)) (K.neg (K.mul (K.mul (K.add (K.add (K.mul k1 (K.add v1 (K.neg w1)))
                (K.mul k2 (K.add v2 (K.neg w2)))) (K.mul k3 (K.add v3 (K.neg w3)))) κ) k3))
          lib_ring

theorem sq_add_le (X Y : V3 R) :
    Le K P (dot3 K (add3 K X Y) (add3 K X Y)) (K.add (K.mul (K.add K.one K.one) (dot3 K X X)) (K.mul (K.add K.one K.one) (dot3 K Y Y))) := by
  refine ⟨dot3 K (sub3 K X Y) (sub3 K X Y), dot3_nn K P _, ?_⟩
  show K.add (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y))) (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))
      (K.add (K.add (K.mul (K.add X.x (K.neg Y.x)) (K.add X.x (K.neg Y.x))) (K.mul (K.add X.y (K.neg Y.y)) (K.add X.y (K.neg Y.y))))
        (K.mul (K.add X.z (K.neg Y.z)) (K.add X.z (K.neg Y.z))))
    = K.add (K.mul (K.add K.one K.one) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
      (K.mul (K.add K.one K.one) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
  lib_ring

theorem lip_mode (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (κ : R) (r : I)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hκ : P.nn κ) (hκk : K.mul (dot3 K (k r) (k r)) κ = K.one) :
    Le K P (dot3 K (sub3 K (proj K κ (k r) (Nl K l δ ι k a a r)) (proj K κ (k r) (Nl K l δ ι k b b r)))
        (sub3 K (proj K κ (k r) (Nl K l δ ι k a a r)) (proj K κ (k r) (Nl K l δ ι k b b r))))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (sub3 K (a p) (b p)) (sub3 K (a p) (b p))) l)
          (lsum K (fun q => dot3 K (k q) (k q)) l))) (lsum K (fun _ => lsum K (fun q => dot3 K (a q) (a q)) l) l)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => dot3 K (b p) (b p)) l)
          (lsum K (fun q => dot3 K (k q) (k q)) l))) (lsum K (fun _ => lsum K (fun q => dot3 K (sub3 K (a q) (b q)) (sub3 K (a q) (b q))) l) l)))) := by
  rw [proj_sub, Nl_sub]
  have two_nn : P.nn (K.add K.one K.one) := P.nn_add _ _ (StepStab.one_nn K P) (StepStab.one_nn K P)
  have f1 := le_mul_nn K P _ _ _ two_nn (force_mode K P l δ ι k (fun p => sub3 K (a p) (b p)) a r hδ)
  have f2 := le_mul_nn K P _ _ _ two_nn (force_mode K P l δ ι k b (fun p => sub3 K (a p) (b p)) r hδ)
  exact le_trans' K P _ _ _ (proj_le K P κ (k r) _ hκ hκk)
    (le_trans' K P _ _ _ (sq_add_le K P _ _) (le_add' K P _ _ _ _ f1 f2))

end Gen

end ForceLip

#print axioms ForceLip.Nl_congr
#print axioms ForceLip.add_sub3
#print axioms ForceLip.Nl_sub
#print axioms ForceLip.proj_sub
#print axioms ForceLip.sq_add_le
#print axioms ForceLip.lip_mode

#print axioms: 6 theorems, no axioms.

79.166 Lipschitz dependence on the data for the projected 3D scheme

\(D_{t+1}\le (1+h+(h+h^2)\,2\iota ^2K_kN^2(E^a_t+E^b_t))D_t\), hence \(D_t\le \rho ^tD_0\) on the horizon of the global bound.

ns/SchemeLip.lean

/-
  ns/SchemeLip.lean — T37 cycle 925 (goal G614b): continuous dependence for the projected semi-implicit 3D Galerkin
  scheme NS3DScheme.sch, over any ordered field (ℚ included). For two solutions a, b (same parameters, initial data
  a0, b0), with D_t = Σ_{p<N} |a_{t,p} − b_{t,p}|², E^a_t, E^b_t their energies and K_k = Σ_{q<N} |k_q|²:
    D_{t+1} ≤ (1 + h + (h + h²) · 2ι² K_k N² (E^a_t + E^b_t)) · D_t.
  With NS3DScheme.global (E ≤ 2E_0) this gives D_t ≤ ρ^t D_0 on the bounded horizon: discrete uniqueness and
  Lipschitz dependence on the data for every truncation.

  Lean 4 core plus ns/NS3DScheme (sch), ns/StepStab3 (comb_sub, sum_stab), ns/ForceLip (lip_mode),
  ns/ForceSum (rl, lsum_rl, lsum_const), ns/ForcedCN (ne_of_one_add via GoodModesQ).
-/
import ns.NS3DScheme
import ns.StepStab3
import ns.ForceLip

namespace SchemeLip
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin LerayProj GalerkinForce ProjStep ForceSum
  NS3DScheme StepStab3 ForceLip GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sumN_const (c : R) : ∀ N, sumN K (fun _ => c) N = K.mul (castN K N) c
  | 0 => by show K.zero = K.mul K.zero c; lib_ring
  | N + 1 => by
    show K.add (sumN K (fun _ => c) N) c = K.mul (K.add K.one (castN K N)) c
    rw [sumN_const c N]; lib_ring

include O

theorem diff_step (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N : Nat) (a0 b0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hh : O.nn h) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (t : Nat) :
    Le K O.toCone (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p))
        (sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p))) N)
      (K.mul (K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul (K.mul (K.add K.one K.one)
          (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N) (K.mul (castN K N) (castN K N)))))
          (K.add (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
            (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N b0 t p) (sch K inv δ ι h k x κ N b0 t p)) N)))))
        (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))
          (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))) N)) := by
  generalize ha : sch K inv δ ι h k x κ N a0 t = a
  generalize hb : sch K inv δ ι h k x κ N b0 t = b
  have hι : ∀ p, K.mul (K.add K.one (x p)) (inv (K.add K.one (x p))) = K.one :=
    fun p => mul_inv _ (ne_of_one_add K O h10 (x p) (hx p))
  let G : Nat → V3 R := fun p => sub3 K (proj K (κ p) (k p) (Nl K (rl N) δ ι k a a p)) (proj K (κ p) (k p) (Nl K (rl N) δ ι k b b p))
  have step : ∀ p, sub3 K (sch K inv δ ι h k x κ N a0 (t + 1) p) (sch K inv δ ι h k x κ N b0 (t + 1) p)
      = comb K (inv (K.add K.one (x p))) (x p) h (sub3 K (a p) (b p)) (G p) := by
    intro p
    show sub3 K (comb K (inv (K.add K.one (x p))) (x p) h (sch K inv δ ι h k x κ N a0 t p)
        (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N a0 t) (sch K inv δ ι h k x κ N a0 t) p)))
      (comb K (inv (K.add K.one (x p))) (x p) h (sch K inv δ ι h k x κ N b0 t p)
        (proj K (κ p) (k p) (Nl K (rl N) δ ι k (sch K inv δ ι h k x κ N b0 t) (sch K inv δ ι h k x κ N b0 t) p))) = _
    rw [ha, hb, comb_sub]
  rw [sumN_ext K _ _ (fun p => by rw [step p]) N]
  have s1 := sum_stab K O.toCone (fun p => inv (K.add K.one (x p))) x h (fun p => sub3 K (a p) (b p)) G hx hh hι N
  -- Σ_r |G_r|² ≤ N · (2ι²(D K_k)(N E_a) + 2ι²(E_b K_k)(N D))
  have s2 := sum_le K O.toCone _ _ (fun r => lip_mode K O.toCone (rl N) δ ι k a b (κ r) r hδ (hκ r) (hκk r)) N
  rw [lsum_const, lsum_const] at s2
  repeat rw [lsum_rl] at s2
  rw [sumN_const] at s2
  have s3 := le_mul_nn K O.toCone (K.add h (K.mul h h)) _ _ (O.nn_add _ _ hh (O.nn_sq h)) s2
  have s4 := le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) s3)
  refine le_trans' K O.toCone _ _ _ s4 ⟨K.zero, O.nn_zero, ?_⟩
  generalize sumN K (fun p => dot3 K (sub3 K (a p) (b p)) (sub3 K (a p) (b p))) N = D
  generalize sumN K (fun q => dot3 K (k q) (k q)) N = Kk
  generalize sumN K (fun p => dot3 K (a p) (a p)) N = Ea
  generalize sumN K (fun p => dot3 K (b p) (b p)) N = Eb
  generalize castN K N = n
  lib_ring

/-- Discrete Gronwall: D_{t+1} ≤ c_t D_t with 0 ≤ c_t ≤ ρ, D_t ≥ 0 ⇒ D_t ≤ ρ^t D_0 for t ≤ T. -/
theorem gronwall (D c : Nat → R) (ρ : R) (T : Nat) (hD : ∀ t, O.nn (D t)) (hcn : ∀ t, O.nn (c t))
    (hc : ∀ t, t < T → Le K O.toCone (c t) ρ) (st : ∀ t, t < T → Le K O.toCone (D (t + 1)) (K.mul (c t) (D t))) :
    ∀ t, t ≤ T → Le K O.toCone (D t) (K.mul (pw K ρ t) (D 0))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (D 0) K.zero = K.mul K.one (D 0); lib_ring⟩
  | t + 1, ht => by
    have ih := gronwall D c ρ T hD hcn hc st t (Nat.le_of_lt ht)
    have ρnn : O.nn ρ := by
      cases hc t ht with
      | intro w hw => rw [← hw.2]; exact O.nn_add _ _ (hcn t) hw.1
    cases st t ht with
    | intro w1 hw1 => cases hc t ht with
      | intro w2 hw2 => cases ih with
        | intro w3 hw3 =>
          refine ⟨K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3)),
            O.nn_add _ _ hw1.1 (O.nn_add _ _ (O.nn_mul _ _ hw2.1 (hD t)) (O.nn_mul _ _ ρnn hw3.1)), ?_⟩
          show K.add (D (t + 1)) (K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3))) = K.mul (K.mul (pw K ρ t) ρ) (D 0)
          have e1 := hw1.2; have e2 := hw2.2; have e3 := hw3.2
          calc K.add (D (t + 1)) (K.add w1 (K.add (K.mul w2 (D t)) (K.mul ρ w3)))
              = K.add (K.add (D (t + 1)) w1) (K.add (K.mul w2 (D t)) (K.mul ρ w3)) := by lib_ring
            _ = K.add (K.mul (c t) (D t)) (K.add (K.mul w2 (D t)) (K.mul ρ w3)) := by rw [e1]
            _ = K.add (K.mul (K.add (c t) w2) (D t)) (K.mul ρ w3) := by lib_ring
            _ = K.add (K.mul ρ (D t)) (K.mul ρ w3) := by rw [e2]
            _ = K.mul ρ (K.add (D t) w3) := by lib_ring
            _ = K.mul ρ (K.mul (pw K ρ t) (D 0)) := by rw [e3]
            _ = _ := by lib_ring

/-- **Lipschitz dependence on the data** for the projected 3D scheme, on the horizon of NS3DScheme.global. -/
theorem lipschitz (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)
    (δ : Nat → Nat → Nat → R) (ι h : R) (k : Nat → V3 R) (x κ : Nat → R) (N T : Nat) (a0 b0 : Nat → V3 R)
    (hx : ∀ p, O.nn (x p)) (hh : O.nn h) (hκ : ∀ p, O.nn (κ p)) (hκk : ∀ p, K.mul (dot3 K (k p) (k p)) (κ p) = K.one)
    (htri : ∀ p q r, δ p q r = K.zero ∨ (δ p q r = K.one ∧ add3 K (k q) (k r) = neg3 K (k p)))
    (hsym : ∀ p q r, δ p q r = δ p r q) (h2 : ∀ y : R, K.add y y = K.zero → y = K.zero)
    (ha0 : ∀ p, dot3 K (k p) (a0 p) = K.zero) (hb0 : ∀ p, dot3 K (k p) (b0 p) = K.zero)
    (smalla : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))) K.one)
    (smallb : Le K O.toCone (K.mul (castN K (4 * T)) (K.mul (K.mul (K.mul h h)
        (K.mul (sumN K (fun r => K.add (x r) (K.add K.one K.one)) N)
          (K.mul (K.mul (K.mul ι ι) (sumN K (fun q => dot3 K (k q) (k q)) N)) (castN K N))))
        (sumN K (fun p => dot3 K (b0 p) (b0 p)) N))) K.one) :
    ∀ t, t ≤ T → Le K O.toCone
      (sumN K (fun p => dot3 K (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))
        (sub3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N b0 t p))) N)
      (K.mul (pw K (K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul (K.mul (K.add K.one K.one)
          (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N) (K.mul (castN K N) (castN K N)))))
          (K.add (K.add (sumN K (fun p => dot3 K (a0 p) (a0 p)) N) (sumN K (fun p => dot3 K (a0 p) (a0 p)) N))
            (K.add (sumN K (fun p => dot3 K (b0 p) (b0 p)) N) (sumN K (fun p => dot3 K (b0 p) (b0 p)) N)))))) t)
        (sumN K (fun p => dot3 K (sub3 K (a0 p) (b0 p)) (sub3 K (a0 p) (b0 p))) N)) := by
  have hδ : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one := fun p q r =>
    (htri p q r).elim Or.inl (fun h => Or.inr h.1)
  have ga := NS3DScheme.global K O inv mul_inv h10 δ ι h k x κ N T a0 hx hκ hκk htri hsym h2 ha0 smalla
  have gb := NS3DScheme.global K O inv mul_inv h10 δ ι h k x κ N T b0 hx hκ hκk htri hsym h2 hb0 smallb
  have Qnn : O.nn (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N)
      (K.mul (castN K N) (castN K N))))) :=
    O.nn_mul _ _ (O.nn_add _ _ (StepStab.one_nn K O.toCone) (StepStab.one_nn K O.toCone))
      (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (sumN_nn K O.toCone _ (fun q => ForceBound.dot3_nn K O.toCone (k q)) N)
        (O.nn_mul _ _ (castN_nn K O N) (castN_nn K O N))))
  have hhh : O.nn (K.add h (K.mul h h)) := O.nn_add _ _ hh (O.nn_sq h)
  have h1h : O.nn (K.add K.one h) := O.nn_add _ _ (StepStab.one_nn K O.toCone) hh
  generalize hQ : K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (sumN K (fun q => dot3 K (k q) (k q)) N)
      (K.mul (castN K N) (castN K N)))) = Q at Qnn ⊢
  refine gronwall K O _ (fun t => K.add (K.add K.one h) (K.mul (K.add h (K.mul h h)) (K.mul Q
      (K.add (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N a0 t p) (sch K inv δ ι h k x κ N a0 t p)) N)
        (sumN K (fun p => dot3 K (sch K inv δ ι h k x κ N b0 t p) (sch K inv δ ι h k x κ N b0 t p)) N))))) _ T
    (fun t => sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N)
    (fun t => O.nn_add _ _ h1h (O.nn_mul _ _ hhh (O.nn_mul _ _ Qnn (O.nn_add _ _
      (sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N) (sumN_nn K O.toCone _ (fun p => ForceBound.dot3_nn K O.toCone _) N)))))
    (fun t ht => le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_mul_nn K O.toCone _ _ _ hhh
      (le_mul_nn K O.toCone _ _ _ Qnn (le_add' K O.toCone _ _ _ _ (ga t (Nat.le_of_lt ht)) (gb t (Nat.le_of_lt ht))))))
    (fun t _ => ?_)
  have d := diff_step K O inv mul_inv h10 δ ι h k x κ N a0 b0 hx hh hκ hκk hδ t
  rw [hQ] at d
  exact d

end Gen

end SchemeLip

#print axioms SchemeLip.sumN_const
#print axioms SchemeLip.diff_step
#print axioms SchemeLip.gronwall
#print axioms SchemeLip.lipschitz

#print axioms: 4 theorems, no axioms.

79.167 Enstrophy form of the Galerkin force bound

\(|N_r(u,v)|^2\le \iota ^2\,T_r\,\sum _{p,q}|u_p|^2|k_q|^2|v_q|^2\) with the triad count \(T_r=\sum _{p,q}\delta _{pqr}^2\).

ns/ForceEnst.lean

/-
  ns/ForceEnst.lean — T37 cycle 936 (goal G624): the enstrophy form of the Galerkin force bound, over any commutative
  ring with a nonnegative cone (ℚ included). Grouping Cauchy–Schwarz differently from ForceBound.force_mode:
    |N_r(u, v)|² ≤ ι² · (Σ_p Σ_q δ(p,q,r)²) · Σ_p Σ_q |u_p|² |k_q|² |v_q|²
                 = ι² · T_r · E(u) · Z(v)      (T_r = Σ_p Σ_q δ², Z(v) = Σ_q |k_q|²|v_q|²)
  — the discrete form of |(u·∇)v| ≲ |u| |∇v|: the truncation enters only through the triad count T_r, which is at
  most the number of modes p (for each p, r at most one q closes the triad k_q = −k_p − k_r).

  Lean 4 core plus ns/ForceBound (double_cs, dot3_cs, lsum_le, dot3_nn, sum3_split), ns/GalerkinForce (Nl, coef).
-/
import ns.ForceBound

namespace ForceEnst
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem force_enst (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  -- component c: N_{r,c} = Σ Σ (δ ι) · ((u_p·k_q) v_{q,c})
  have comp : ∀ (vc : I → R), Le K P
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul ι (δ p q r)) (K.mul ι (δ p q r))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (vc q)) (K.mul (dot3 K (u p) (k q)) (vc q))) l) l)) := by
    intro vc
    have cs := double_cs K P (fun p q => K.mul ι (δ p q r)) (fun p q => K.mul (dot3 K (u p) (k q)) (vc q)) l
    have e : ∀ p q, K.mul (coef K δ ι k u p q r) (vc q) = K.mul (K.mul ι (δ p q r)) (K.mul (dot3 K (u p) (k q)) (vc q)) := by
      intro p q; unfold coef; lib_ring
    rw [lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => e p q) l) l]
    exact cs
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (comp (fun q => (v q).x)) (comp (fun q => (v q).y))) (comp (fun q => (v q).z))
  generalize hD : lsum K (fun p => lsum K (fun q => K.mul (K.mul ι (δ p q r)) (K.mul ι (δ p q r))) l) l = Dl at s
  have hDn : P.nn Dl := by rw [← hD]; exact lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_sq _) l) l
  -- regroup: Σ_c Dl·ΣΣ (w v_c)² = Dl · ΣΣ w² |v_q|²   (w = u_p·k_q)
  have e1 : ∀ (A : R), K.add (K.add (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).x) (K.mul (dot3 K (u p) (k q)) (v q).x)) l) l))
        (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).y) (K.mul (dot3 K (u p) (k q)) (v q).y)) l) l)))
        (K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (v q).z) (K.mul (dot3 K (u p) (k q)) (v q).z)) l) l))
      = K.mul A (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l) := by
    intro A
    rw [← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
    congr 1
    apply lsum_ext; intro p
    rw [← lsum_add, ← lsum_add]
    apply lsum_ext; intro q
    show _ = K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (K.add (K.add (K.mul (v q).x (v q).x) (K.mul (v q).y (v q).y)) (K.mul (v q).z (v q).z))
    generalize dot3 K (u p) (k q) = w
    lib_ring
  rw [e1] at s
  have b := lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q =>
      le_mul_nn K P (dot3 K (v q) (v q)) _ _ (dot3_nn K P (v q)) (dot3_cs K P (u p) (k q))) l) l
  have b2 := le_mul_nn K P _ _ _ hDn b
  have hDl : Dl = K.mul (K.mul ι ι) (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l) := by
    rw [← hD, ← lsum_mul_left]
    apply lsum_ext; intro p
    rw [← lsum_mul_left]
    apply lsum_ext; intro q; lib_ring
  have b3 : Le K P (K.mul Dl (lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l))
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => lsum K (fun q => K.mul (δ p q r) (δ p q r)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
    have e2 : lsum K (fun p => lsum K (fun q => K.mul (dot3 K (v q) (v q)) (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q)))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (K.mul (dot3 K (u p) (k q)) (dot3 K (u p) (k q))) (dot3 K (v q) (v q))) l) l :=
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => K.mul_comm _ _) l) l
    have e3 : lsum K (fun p => lsum K (fun q => K.mul (dot3 K (v q) (v q)) (K.mul (dot3 K (u p) (u p)) (dot3 K (k q) (k q)))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (dot3 K (u p) (u p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l :=
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => by lib_ring) l) l
    rw [e2, e3, hDl] at b2
    cases b2 with
    | intro w hw => exact ⟨w, hw.1, by rw [hDl]; exact hw.2.trans (by lib_ring)⟩
  exact le_trans' K P _ _ _ s b3

end Gen

end ForceEnst

#print axioms ForceEnst.force_enst

#print axioms: 1 theorem, no axioms.

79.168 Cauchy–Schwarz for double sums of dot products

\((\sum _p\sum _qX_{pq}\cdot Y_{pq})^2\le (\sum \sum |X_{pq}|^2)(\sum \sum |Y_{pq}|^2)\) for 3-vectors.

ns/VecCS.lean

/-
  ns/VecCS.lean — T37 cycle 938 (goal G624b, piece 1): Cauchy–Schwarz for double sums of dot products of 3-vectors,
  over any commutative ring with a nonnegative cone (ℚ included):
    (Σ_{p∈l} Σ_{q∈l} X_pq · Y_pq)² ≤ (Σ_p Σ_q |X_pq|²) (Σ_p Σ_q |Y_pq|²)
  via lib/CauchySchwarz on the list of all component pairs.

  Lean 4 core plus ns/ForceBound (pos, le_of_cs, sC_app), ns/QuadBound (sA_app, sB_app), lib/CauchySchwarz.
-/
import ns.ForceBound

namespace VecCS
open DetChain NSChain LibRing LibCS FourierGalerkin QuadBound ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def trip (X Y : V3 R) : List (R × R) := [(X.x, Y.x), (X.y, Y.y), (X.z, Y.z)]

def vrow (X Y : I → I → V3 R) (p : I) : List I → List (R × R)
  | [] => []
  | q :: m => trip (X p q) (Y p q) ++ vrow X Y p m

def vgrid (X Y : I → I → V3 R) (l : List I) : List I → List (R × R)
  | [] => []
  | p :: m => vrow X Y p l ++ vgrid X Y l m

theorem sA_trip (X Y : V3 R) : sA K (trip X Y) = dot3 K X X := by
  show K.add (K.mul X.x X.x) (K.add (K.mul X.y X.y) (K.add (K.mul X.z X.z) K.zero)) = dot3 K X X
  unfold dot3; lib_ring
theorem sB_trip (X Y : V3 R) : sB K (trip X Y) = dot3 K Y Y := by
  show K.add (K.mul Y.x Y.x) (K.add (K.mul Y.y Y.y) (K.add (K.mul Y.z Y.z) K.zero)) = dot3 K Y Y
  unfold dot3; lib_ring
theorem sC_trip (X Y : V3 R) : sC K (trip X Y) = dot3 K X Y := by
  show K.add (K.mul X.x Y.x) (K.add (K.mul X.y Y.y) (K.add (K.mul X.z Y.z) K.zero)) = dot3 K X Y
  unfold dot3; lib_ring

theorem sA_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sA K (vrow X Y p m) = lsum K (fun q => dot3 K (X p q) (X p q)) m
  | [] => rfl
  | q :: m => by
    show sA K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sA_app, sA_trip, sA_vrow X Y p m]; rfl
theorem sB_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sB K (vrow X Y p m) = lsum K (fun q => dot3 K (Y p q) (Y p q)) m
  | [] => rfl
  | q :: m => by
    show sB K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sB_app, sB_trip, sB_vrow X Y p m]; rfl
theorem sC_vrow (X Y : I → I → V3 R) (p : I) : ∀ m, sC K (vrow X Y p m) = lsum K (fun q => dot3 K (X p q) (Y p q)) m
  | [] => rfl
  | q :: m => by
    show sC K (trip (X p q) (Y p q) ++ vrow X Y p m) = _
    rw [sC_app, sC_trip, sC_vrow X Y p m]; rfl

theorem sA_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sA K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (X p q) (X p q)) l) m
  | [] => rfl
  | p :: m => by
    show sA K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sA_app, sA_vrow, sA_vgrid X Y l m]; rfl
theorem sB_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sB K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (Y p q) (Y p q)) l) m
  | [] => rfl
  | p :: m => by
    show sB K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sB_app, sB_vrow, sB_vgrid X Y l m]; rfl
theorem sC_vgrid (X Y : I → I → V3 R) (l : List I) : ∀ m,
    sC K (vgrid X Y l m) = lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) m
  | [] => rfl
  | p :: m => by
    show sC K (vrow X Y p l ++ vgrid X Y l m) = _
    rw [sC_app, sC_vrow, sC_vgrid X Y l m]; rfl

theorem vec_double_cs (X Y : I → I → V3 R) (l : List I) :
    Le K P (K.mul (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) l)
        (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (Y p q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => dot3 K (X p q) (X p q)) l) l)
        (lsum K (fun p => lsum K (fun q => dot3 K (Y p q) (Y p q)) l) l)) := by
  have cs := cauchy_schwarz K (pos K P) (vgrid X Y l l)
  rw [sA_vgrid, sB_vgrid, sC_vgrid] at cs
  exact le_of_cs K P _ _ _ cs

end Gen

end VecCS

#print axioms VecCS.sA_vgrid
#print axioms VecCS.sB_vgrid
#print axioms VecCS.sC_vgrid
#print axioms VecCS.vec_double_cs

#print axioms: 4 theorems, no axioms.

79.169 A truncation-uniform bound of the Galerkin force

With weights \(\mu _p\nu _p=1\) and triad sparsity (\(\sum _q\delta _{pqr}\le 1\), \(\sum _r\delta _{pqr}\le 1\)): \(\sum _r|N_r(u,v)|^2\le \iota ^2\,(\sum _p\mu _p^2|u_p|^2)(\sum _p\nu _p^2)(\sum _q|k_q|^2|v_q|^2)\).

ns/ForceSob.lean

/-
  ns/ForceSob.lean — T37 cycle 939 (goal G624b): a truncation-uniform form of the Galerkin force bound, over any
  commutative ring with a nonnegative cone (ℚ included). Weights μ_p ν_p = 1, a triad indicator δ ∈ {0, 1} with the
  triad sparsity of Fourier modes (for fixed p and r at most one q, for fixed p and q at most one r):
    spA : Σ_{q∈l} δ(p,q,r) ≤ 1,     spB : Σ_{r∈l} δ(p,q,r) ≤ 1.
  Then, with H_μ(u) = Σ_p μ_p²|u_p|², Z(v) = Σ_q |k_q|²|v_q|²,
    mode_sob    |N_r(u,v)|² ≤ ι² · Σ_pΣ_q δ μ_p²|u_p|² · Σ_pΣ_q δ ν_p²|k_q|²|v_q|²
    total_sob   Σ_r |N_r(u,v)|² ≤ ι² · H_μ(u) · (Σ_p ν_p²) · Z(v)
  — the discrete ‖(u·∇)v‖² ≤ ‖u‖²_{L∞}‖∇v‖² with ‖u‖²_{L∞} ≤ H_μ(u)·Σ_p ν_p²; with μ_p² = |k_p|^{2s} the factor
  Σ_p ν_p² = Σ_p |k_p|^{−2s} is bounded uniformly in the truncation for s > 3/2.

  Lean 4 core plus ns/VecCS (vec_double_cs), ns/ForceBound (lsum_le, lsum_nn, dot3_nn), ns/GalerkinForce (Nl, coef).
-/
import ns.VecCS

namespace ForceSob
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound VecCS LerayProj

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem le_mul_right' {x y z : R} (h : Le K P x y) (hz : P.nn z) : Le K P (K.mul x z) (K.mul y z) := by
  cases h with
  | intro d hd => exact ⟨K.mul d z, P.nn_mul _ _ hd.1 hz, by rw [← hd.2]; lib_ring⟩

def sm (c : R) (v : V3 R) : V3 R := ⟨K.mul c v.x, K.mul c v.y, K.mul c v.z⟩

theorem dot_sm (c d : R) (a b : V3 R) : dot3 K (sm K c a) (sm K d b) = K.mul (K.mul c d) (dot3 K a b) := by
  show K.add (K.add (K.mul (K.mul c a.x) (K.mul d b.x)) (K.mul (K.mul c a.y) (K.mul d b.y))) (K.mul (K.mul c a.z) (K.mul d b.z))
    = K.mul (K.mul c d) (K.add (K.add (K.mul a.x b.x) (K.mul a.y b.y)) (K.mul a.z b.z))
  lib_ring

/-- One component of the force as a double sum of dot products. -/
theorem comp_eq (l : List I) (δ : I → I → I → R) (ι : R) (k u : I → V3 R) (μ ν : I → R) (vc : I → R) (r : I)
    (hδ : ∀ p q, K.mul (δ p q r) (δ p q r) = δ p q r) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one) :
    lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l
      = lsum K (fun p => lsum K (fun q => dot3 K (sm K (K.mul (K.mul (δ p q r) ι) (μ p)) (u p))
          (sm K (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (k q))) l) l := by
  apply lsum_ext; intro p; apply lsum_ext; intro q
  rw [dot_sm]
  unfold coef
  have e : K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) (ν p)) (vc q))
      = K.mul (K.mul (K.mul (δ p q r) (δ p q r)) ι) (K.mul (K.mul (μ p) (ν p)) (vc q)) := by lib_ring
  rw [e, hδ, hμν]
  generalize dot3 K (u p) (k q) = w
  lib_ring

theorem mode_sob (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν : I → R) (r : I)
    (hδ : ∀ p q, K.mul (δ p q r) (δ p q r) = δ p q r) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one) :
    Le K P (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
          (dot3 K (u p) (u p))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) := by
  have cs : ∀ vc : I → R, Le K P
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (vc q)) l) l))
      (K.mul (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
          (dot3 K (u p) (u p))) l) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (K.mul (K.mul (δ p q r) (ν p)) (vc q)))
          (dot3 K (k q) (k q))) l) l)) := by
    intro vc
    rw [comp_eq K l δ ι k u μ ν vc r hδ hμν]
    have c := vec_double_cs K P (fun p q => sm K (K.mul (K.mul (δ p q r) ι) (μ p)) (u p))
      (fun p q => sm K (K.mul (K.mul (δ p q r) (ν p)) (vc q)) (k q)) l
    rw [lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => dot_sm K _ _ (u p) (u p)) l) l,
      lsum_ext K _ _ (fun p => lsum_ext K _ _ (fun q => dot_sm K _ _ (k q) (k q)) l) l] at c
    exact c
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ (cs (fun q => (v q).x)) (cs (fun q => (v q).y))) (cs (fun q => (v q).z))
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← K.distrib, ← K.distrib, ← lsum_add, ← lsum_add]
  congr 1
  apply lsum_ext; intro p
  rw [← lsum_add, ← lsum_add]
  apply lsum_ext; intro q
  show _ = K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
    (K.mul (dot3 K (k q) (k q)) (K.add (K.add (K.mul (v q).x (v q).x) (K.mul (v q).y (v q).y)) (K.mul (v q).z (v q).z)))
  generalize dot3 K (k q) (k q) = kk
  generalize K.mul (δ p q r) (ν p) = dn
  lib_ring

theorem total_sob (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν : I → R)
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r)) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (K.mul (lsum K (fun p => K.mul (ν p) (ν p)) l) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  -- B_r ≥ 0 and Σ_r B_r ≤ (Σν²) Z
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν)
    (le_mul_right' K P (hA r) (hBn r))) l
  refine le_trans' K P _ _ _ s1 ?_
  rw [lsum_mul_left]
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have key : Le K P (lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) l)
      (K.mul (lsum K (fun p => K.mul (ν p) (ν p)) l) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)) := by
    -- Σ_r Σ_p Σ_q (δν)² W_q = Σ_p Σ_q ν_p² W_q Σ_r δ ≤ Σ_p Σ_q ν_p² W_q
    have sw : lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
            (lsum K (fun r => δ p q r) l)) l) l := by
      rw [lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l l]
      apply lsum_ext; intro p
      rw [lsum_swap K (fun r q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l l]
      apply lsum_ext; intro q
      rw [← lsum_mul_left]
      apply lsum_ext; intro r
      have t : K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [sw]
    refine le_trans' K P _ _ _ (lsum_le K P _ _ (fun p => lsum_le K P _
      (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) K.one)
      (fun q => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) (spB p q)) l) l)
      ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']
    have e2 := double_factor K l K.one (fun p => K.mul (ν p) (ν p)) (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))
    rw [K.one_mul] at e2
    rw [← e2]
    apply lsum_ext; intro p; apply lsum_ext; intro q; lib_ring

  have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
  refine le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero']
  generalize lsum K (fun p => K.mul (ν p) (ν p)) l = Nu
  generalize lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l = Z
  lib_ring
end Gen

end ForceSob

#print axioms ForceSob.dot_sm
#print axioms ForceSob.comp_eq
#print axioms ForceSob.mode_sob
#print axioms ForceSob.total_sob

#print axioms: 4 theorems, no axioms.

79.170 Telescoping and the Basel partial sums

\(a_n+b_{n+1}\le b_n\Rightarrow \sum _{n<N}a_n+b_N\le b_0\); hence \(\sum _{n<N}z_n\le 2\) when \(z_{n+1}+w_{n+1}\le w_n\), \(z_0\le w_0=1\) (e.g. \(z_n=(n+1)^{-2}\)).

ns/TeleSum.lean

/-
  ns/TeleSum.lean — T37 cycle 941 (goal G624c, piece 1): telescoping bounds for the lattice sums of ForceSob, over any
  commutative ring with a nonnegative cone (ℚ included).
    tele_le     a_n + b_{n+1} ≤ b_n for all n  ⇒  Σ_{n<N} a_n + b_N ≤ b_0
    basel_le    z_0 ≤ w_0 = 1, z_{n+1} + w_{n+1} ≤ w_n, w ≥ 0  ⇒  Σ_{n<N} z_n ≤ 2
                (with z_n = 1/(n+1)², w_n = 1/(n+1): the Basel partial sums are bounded uniformly in N, since
                1/(n+2)² ≤ 1/((n+1)(n+2)) = 1/(n+1) − 1/(n+2)).
  The shell count of the lattice cube (24n² + 2 points with |k|_∞ = n) then gives Σ_{0<|k|_∞≤N} |k|^{−4} ≤ C
  uniformly in N.

  Lean 4 core plus NSChain (Cone, Le, sumN), ns/DyadicLarge, lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace TeleSum
open DetChain NSChain LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem tele_le (a b : Nat → R) (h : ∀ n, Le K P (K.add (a n) (b (n + 1))) (b n)) :
    ∀ N, Le K P (K.add (sumN K a N) (b N)) (b 0)
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.add K.zero (b 0)) K.zero = b 0; lib_ring⟩
  | N + 1 => by
    have ih := tele_le a b h N
    cases h N with
    | intro d hd => cases ih with
      | intro d' hd' =>
        refine ⟨K.add d d', P.nn_add _ _ hd.1 hd'.1, ?_⟩
        show K.add (K.add (K.add (sumN K a N) (a N)) (b (N + 1))) (K.add d d') = b 0
        rw [← hd'.2, ← hd.2]; lib_ring

theorem basel_le (z w : Nat → R) (hw : ∀ n, P.nn (w n)) (hw0 : w 0 = K.one) (hz0 : Le K P (z 0) K.one)
    (h : ∀ n, Le K P (K.add (z (n + 1)) (w (n + 1))) (w n)) :
    ∀ N, Le K P (sumN K z N) (K.add K.one K.one)
  | 0 => ⟨K.add K.one K.one, P.nn_add _ _ (by have := P.nn_sq K.one; rwa [K.one_mul] at this)
      (by have := P.nn_sq K.one; rwa [K.one_mul] at this), by show K.add K.zero _ = _; lib_ring⟩
  | N + 1 => by
    -- Σ_{n<N+1} z_n = z_0 + Σ_{n<N} z_{n+1} ≤ 1 + (w_0 − w_N) ≤ 1 + 1
    have t := tele_le K P (fun n => z (n + 1)) w h N
    have shift : ∀ M, sumN K z (M + 1) = K.add (z 0) (sumN K (fun n => z (n + 1)) M) := by
      intro M
      induction M with
      | zero => show K.add K.zero (z 0) = K.add (z 0) K.zero; lib_ring
      | succ M ih =>
        show K.add (sumN K z (M + 1)) (z (M + 1)) = K.add (z 0) (K.add (sumN K (fun n => z (n + 1)) M) (z (M + 1)))
        rw [ih]; lib_ring
    rw [shift]
    rw [hw0] at t
    cases t with
    | intro d hd => cases hz0 with
      | intro d0 hd0 =>
        refine ⟨K.add d0 (K.add d (w N)), P.nn_add _ _ hd0.1 (P.nn_add _ _ hd.1 (hw N)), ?_⟩
        rw [← hd.2]
        calc _ = K.add (K.add (z 0) d0) (K.add (K.add (sumN K (fun n => z (n + 1)) N) (w N)) d) := by lib_ring
          _ = K.add K.one (K.add (K.add (sumN K (fun n => z (n + 1)) N) (w N)) d) := by rw [hd0.2]
          _ = _ := by rw [hd.2]

end Gen

end TeleSum

#print axioms TeleSum.tele_le
#print axioms TeleSum.basel_le

#print axioms: 2 theorems, no axioms.

79.171 Basel partial sums over an ordered field

Over any ordered field with inverses (in particular \(\mathbb Q\)): \(\sum _{n<N}(n+1)^{-2}\le 2\) for every \(N\). Via the telescoping bound with \(w_n=(n+1)^{-1}\), the step identity checked after clearing denominators.

ns/BaselQ.lean

/-
  ns/BaselQ.lean — T37 cycle 942 (goal G624c, piece 2): the Basel partial sums over an ordered field (ℚ included):
    Σ_{n<N} 1/(n+1)² ≤ 2   for every N,
  from TeleSum.basel_le with w_n = 1/(n+1): the step 1/(n+2)² + 1/(n+2) + 1/((n+1)(n+2)²) = 1/(n+1) is checked
  after multiplying by (n+1)(n+2)² ≠ 0 (no zero divisors), and inverses of positive elements are nonnegative.

  Lean 4 core plus ns/TeleSum (basel_le), ns/GoodModesQ (cast_succ_ne, mul_ne, nn_cast), lib/RingTac.
-/
import ns.TeleSum
import ns.GoodModesQ

namespace BaselQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge TeleSum GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
include O

theorem inv_nn (inv : R → R) (y : R) (hy : O.nn y) (hyi : K.mul y (inv y) = K.one) : O.nn (inv y) := by
  have e : inv y = K.mul y (K.mul (inv y) (inv y)) := by
    calc inv y = K.mul (K.mul y (inv y)) (inv y) := by rw [hyi, K.one_mul]
      _ = _ := by lib_ring
  rw [e]; exact O.nn_mul _ _ hy (O.nn_sq _)

theorem cancel (c x y : R) (hc : c ≠ K.zero) (h : K.mul c x = K.mul c y) : x = y := by
  have e : K.mul c (K.add x (K.neg y)) = K.zero := by
    have : K.mul c (K.add x (K.neg y)) = K.add (K.mul c x) (K.neg (K.mul c y)) := by lib_ring
    rw [this, h]; lib_ring
  cases O.noZD _ _ e with
  | inl h0 => exact absurd h0 hc
  | inr h0 =>
    calc x = K.add (K.add x (K.neg y)) y := by lib_ring
      _ = y := by rw [h0]; lib_ring

theorem basel (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero) (N : Nat) :
    Le K O.toCone (sumN K (fun n => inv (K.mul (castN K (n + 1)) (castN K (n + 1)))) N) (K.add K.one K.one) := by
  have hA : ∀ n, castN K (n + 1) ≠ K.zero := fun n => cast_succ_ne K O h10 n
  have hAn : ∀ n, O.nn (castN K (n + 1)) := fun n => RatModel.nn_cast K O.toCone (n + 1)
  refine basel_le K O.toCone (fun n => inv (K.mul (castN K (n + 1)) (castN K (n + 1)))) (fun n => inv (castN K (n + 1)))
    (fun n => inv_nn K O inv _ (hAn n) (mul_inv _ (hA n))) ?_ ?_ ?_ N
  · -- w_0 = inv 1 = 1
    show inv (K.add K.one K.zero) = K.one
    have h1 : K.add K.one K.zero = K.one := by lib_ring
    rw [h1]
    have := mul_inv K.one h10
    rw [K.one_mul] at this; exact this
  · refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (inv (K.mul (K.add K.one K.zero) (K.add K.one K.zero))) K.zero = K.one
    have h1 : K.mul (K.add K.one K.zero) (K.add K.one K.zero) = K.one := by lib_ring
    rw [h1]
    have := mul_inv K.one h10
    rw [K.one_mul] at this; rw [this]; lib_ring
  · intro n
    generalize hA' : castN K (n + 1) = A
    have hB : castN K (n + 1 + 1) = K.add K.one A := by rw [← hA']; rfl
    rw [hB]
    have A0 : A ≠ K.zero := by rw [← hA']; exact hA n
    have B0 : K.add K.one A ≠ K.zero := by rw [← hB]; exact hA (n + 1)
    have An : O.nn A := by rw [← hA']; exact hAn n
    generalize hBdef : K.add K.one A = B at B0 ⊢
    have Bn : O.nn B := by rw [← hBdef]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) An
    have BB0 : K.mul B B ≠ K.zero := mul_ne K O _ _ B0 B0
    have c0 : K.mul A (K.mul B B) ≠ K.zero := mul_ne K O _ _ A0 BB0
    have iA := mul_inv A A0
    have iB := mul_inv B B0
    have iBB := mul_inv _ BB0
    have ic := mul_inv _ c0
    refine ⟨inv (K.mul A (K.mul B B)), inv_nn K O inv _ (O.nn_mul _ _ An (O.nn_mul _ _ Bn Bn)) ic, ?_⟩
    apply cancel K O (K.mul A (K.mul B B)) _ _ c0
    generalize inv A = wA at iA ⊢
    generalize inv B = wB at iB ⊢
    generalize inv (K.mul B B) = zB at iBB ⊢
    generalize inv (K.mul A (K.mul B B)) = d at ic ⊢
    -- c(zB + wB + d) = A + A B + 1 = B² = c wA   (B = 1 + A)
    calc K.mul (K.mul A (K.mul B B)) (K.add (K.add zB wB) d)
        = K.add (K.add (K.mul A (K.mul (K.mul B B) zB)) (K.mul (K.mul A B) (K.mul B wB))) (K.mul (K.mul A (K.mul B B)) d) := by lib_ring
      _ = K.add (K.add A (K.mul A B)) K.one := by rw [iBB, iB, ic]; lib_ring
      _ = K.mul (K.mul B B) (K.mul A wA) := by rw [iA, ← hBdef]; lib_ring
      _ = _ := by lib_ring

end Gen

end BaselQ

#print axioms BaselQ.inv_nn
#print axioms BaselQ.cancel
#print axioms BaselQ.basel

#print axioms: 3 theorems, no axioms.

79.172 Pigeonhole bound for duplicate-free lists

A duplicate-free list contained in a list \(L\) has length at most \(|L|\). Used to count distinct Fourier modes in a lattice cube.

ns/NodupLen.lean

/-
  ns/NodupLen.lean — T37 cycle 944 (goal G624c, piece 3): the pigeonhole bound for lists.
  A duplicate-free list whose members all lie in a list L is no longer than L:
    Pairwise (· ≠ ·) l,  (∀ x ∈ l, x ∈ L)  ⟹  l.length ≤ L.length.
  Used to count Fourier modes in a cube of the lattice (|k|_∞ ≤ n gives at most (2n+1)³ distinct modes),
  which turns the weighted sum Σ_p |k_p|^{-4} over any set of distinct modes into a bound independent of N.

  Lean 4 core only (own removal function, hand proofs).
-/

namespace NodupLen

variable {α : Type} [DecidableEq α]

/-- remove the first occurrence of x -/
def rem (x : α) : List α → List α
  | [] => []
  | y :: ys => if y = x then ys else y :: rem x ys

theorem rem_len (x : α) : ∀ (L : List α), x ∈ L → (rem x L).length + 1 = L.length
  | [], h => nomatch h
  | y :: ys, h => by
    show (if y = x then ys else y :: rem x ys).length + 1 = ys.length + 1
    by_cases e : y = x
    · rw [ite_eq_left e]
    · rw [ite_eq_right e]
      have hx : x ∈ ys := by
        cases h with
        | head => exact absurd rfl e
        | tail _ h' => exact h'
      show (rem x ys).length + 1 + 1 = ys.length + 1
      rw [rem_len x ys hx]

theorem mem_rem (x : α) : ∀ (L : List α) (y : α), y ∈ L → y ≠ x → y ∈ rem x L
  | [], _, h, _ => nomatch h
  | z :: zs, y, h, hyx => by
    show y ∈ (if z = x then zs else z :: rem x zs)
    by_cases e : z = x
    · rw [ite_eq_left e]
      cases h with
      | head => exact absurd e hyx
      | tail _ h' => exact h'
    · rw [ite_eq_right e]
      cases h with
      | head => exact List.Mem.head _
      | tail _ h' => exact List.Mem.tail _ (mem_rem x zs y h' hyx)

theorem len_le : ∀ (l L : List α), List.Pairwise (fun a b => a ≠ b) l → (∀ x, x ∈ l → x ∈ L) →
    l.length ≤ L.length
  | [], L, _, _ => Nat.zero_le _
  | x :: xs, L, hp, hs => by
    cases hp with
    | cons hx hxs =>
      have hxL : x ∈ L := hs x (List.Mem.head _)
      have sub : ∀ y, y ∈ xs → y ∈ rem x L := fun y hy =>
        mem_rem x L y (hs y (List.Mem.tail _ hy)) (fun e => hx y hy e.symm)
      have ih := len_le xs (rem x L) hxs sub
      show xs.length + 1 ≤ L.length
      rw [← rem_len x L hxL]
      exact Nat.succ_le_succ ih

end NodupLen

#print axioms NodupLen.rem_len
#print axioms NodupLen.mem_rem
#print axioms NodupLen.len_le

#print axioms: 3 theorems, no axioms.

79.173 Counting lattice modes in a cube

At most \((2n+1)^3\) distinct modes \(k\in \mathbb Z^3\) satisfy \(|k|_\infty \le n\). Explicit enumeration of \([-n,n]^3\) and the pigeonhole bound.

ns/CubeCount.lean

/-
  ns/CubeCount.lean — T37 cycle 945 (goal G624c, piece 4): counting lattice modes in a cube.
  The integers with |x| ≤ n form an explicit list of length 2n + 1, the cube [−n, n]³ ⊂ ℤ³ an explicit list of
  length (2n + 1)·((2n + 1)·(2n + 1)), and so (NodupLen.len_le) any duplicate-free list of modes k with
  |k|_∞ ≤ n has at most (2n + 1)³ members.

  Lean 4 core plus ns/NodupLen; own list lemmas (core List.length_map / length_append use propext).
-/
import ns.NodupLen

namespace CubeCount

abbrev Mode := Int × Int × Int

/-- the integers of absolute value at most n -/
def ilist : Nat → List Int
  | 0 => [0]
  | n + 1 => Int.ofNat (n + 1) :: Int.negSucc n :: ilist n

theorem ilist_len : ∀ n, (ilist n).length = 2 * n + 1
  | 0 => rfl
  | n + 1 => by
    show (ilist n).length + 1 + 1 = 2 * (n + 1) + 1
    rw [ilist_len n]; rfl

theorem le_succ_cases {a n : Nat} (h : a ≤ n + 1) : a = n + 1 ∨ a ≤ n := by
  cases Nat.eq_or_lt_of_le h with
  | inl e => exact Or.inl e
  | inr l => exact Or.inr (Nat.le_of_lt_succ l)

theorem mem_ilist : ∀ (n : Nat) (x : Int), x.natAbs ≤ n → x ∈ ilist n
  | 0, x, h => by
    have h0 : x.natAbs = 0 := Nat.eq_zero_of_le_zero h
    have e : x = 0 := by
      cases x with
      | ofNat k =>
        have : k = 0 := h0
        rw [this]; rfl
      | negSucc k => exact nomatch h0
    rw [e]; exact List.Mem.head _
  | n + 1, x, h => by
    cases le_succ_cases h with
    | inl e =>
      cases x with
      | ofNat k =>
        have : k = n + 1 := e
        rw [this]; exact List.Mem.head _
      | negSucc k =>
        have : k + 1 = n + 1 := e
        have hk : k = n := Nat.succ.inj this
        rw [hk]; exact List.Mem.tail _ (List.Mem.head _)
    | inr l => exact List.Mem.tail _ (List.Mem.tail _ (mem_ilist n x l))

theorem len_app {β : Type} : ∀ (l m : List β), (l ++ m).length = l.length + m.length
  | [], m => (Nat.zero_add _).symm
  | _ :: l, m => by
    show (l ++ m).length + 1 = l.length + 1 + m.length
    rw [len_app l m, Nat.add_right_comm]

def row (a b : Int) : List Int → List Mode
  | [] => []
  | c :: cs => (a, b, c) :: row a b cs

def plane (a : Int) (M : List Int) : List Int → List Mode
  | [] => []
  | b :: bs => row a b M ++ plane a M bs

def cube (M : List Int) : List Int → List Mode
  | [] => []
  | a :: as => plane a M M ++ cube M as

theorem row_len (a b : Int) : ∀ (M : List Int), (row a b M).length = M.length
  | [] => rfl
  | _ :: cs => by
    show (row a b cs).length + 1 = cs.length + 1
    rw [row_len a b cs]

theorem plane_len (a : Int) (M : List Int) : ∀ (B : List Int), (plane a M B).length = B.length * M.length
  | [] => (Nat.zero_mul _).symm
  | _ :: bs => by
    show (row a _ M ++ plane a M bs).length = (bs.length + 1) * M.length
    rw [len_app, row_len, plane_len a M bs, Nat.succ_mul, Nat.add_comm]

theorem cube_len (M : List Int) : ∀ (A : List Int), (cube M A).length = A.length * (M.length * M.length)
  | [] => (Nat.zero_mul _).symm
  | _ :: as => by
    show (plane _ M M ++ cube M as).length = (as.length + 1) * (M.length * M.length)
    rw [len_app, plane_len, cube_len M as, Nat.succ_mul, Nat.add_comm]

theorem mem_row (a b c : Int) : ∀ (M : List Int), c ∈ M → (a, b, c) ∈ row a b M
  | [], h => nomatch h
  | _ :: cs, h => by
    cases h with
    | head => exact List.Mem.head _
    | tail _ h' => exact List.Mem.tail _ (mem_row a b c cs h')

theorem mem_plane (a b c : Int) (M : List Int) (hc : c ∈ M) : ∀ (B : List Int), b ∈ B → (a, b, c) ∈ plane a M B
  | [], h => nomatch h
  | _ :: bs, h => by
    cases h with
    | head => exact List.mem_append_left _ (mem_row a _ c M hc)
    | tail _ h' => exact List.mem_append_right _ (mem_plane a b c M hc bs h')

theorem mem_cube (a b c : Int) (M : List Int) (hb : b ∈ M) (hc : c ∈ M) :
    ∀ (A : List Int), a ∈ A → (a, b, c) ∈ cube M A
  | [], h => nomatch h
  | _ :: as, h => by
    cases h with
    | head => exact List.mem_append_left _ (mem_plane _ b c M hc M hb)
    | tail _ h' => exact List.mem_append_right _ (mem_cube a b c M hb hc as h')

/-- the sup norm bound |k|_∞ ≤ n -/
def InCube (n : Nat) (k : Mode) : Prop := k.1.natAbs ≤ n ∧ k.2.1.natAbs ≤ n ∧ k.2.2.natAbs ≤ n

/-- at most (2n + 1)³ distinct modes have |k|_∞ ≤ n -/
theorem count_cube (n : Nat) (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (hc : ∀ k, k ∈ l → InCube n k) :
    l.length ≤ (2 * n + 1) * ((2 * n + 1) * (2 * n + 1)) := by
  have h := NodupLen.len_le l (cube (ilist n) (ilist n)) hd (fun k hk => by
    have q := hc k hk
    exact mem_cube k.1 k.2.1 k.2.2 (ilist n) (mem_ilist n _ q.2.1) (mem_ilist n _ q.2.2) (ilist n)
      (mem_ilist n _ q.1))
  rw [cube_len, ilist_len] at h
  exact h

end CubeCount

#print axioms CubeCount.ilist_len
#print axioms CubeCount.mem_ilist
#print axioms CubeCount.cube_len
#print axioms CubeCount.mem_cube
#print axioms CubeCount.count_cube

#print axioms: 5 theorems, no axioms.

79.174 Abel summation against counting functions

For \(n_p\le M\): \(\sum _p f(n_p)=\sum _{j<M}\#\{p:n_p\le j\}\,(f_j-f_{j+1})+|l|\,f_M\) over any commutative ring. With the cube count this bounds \(\sum _p|k_p|_\infty ^{-4}\) uniformly in the truncation.

ns/AbelCount.lean

/-
  ns/AbelCount.lean — T37 cycle 947 (goal G624c, piece 5): Abel summation against counting functions, over any
  commutative ring. For a list of naturals n_p ≤ M and any f : ℕ → R, with g_j = f_j − f_{j+1} and
  cnt(l, j) = #{p : n_p ≤ j}:
    abel_list   Σ_p f(n_p) = Σ_{j<M} cnt(l, j)·g_j + |l|·f(M).
  With n_p = |k_p|_∞, f(n) = n^{−4}, the cube count cnt(l, j) ≤ (2j+1)³ (CubeCount) and
  (2j+1)³((j+1)⁴ − j⁴) ≤ 51 j³(j+1)³ this bounds Σ_p |k_p|_∞^{−4} uniformly in the truncation.

  Lean 4 core plus NSChain (sumN, sumN_add, sumN_zero), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace AbelCount
open DetChain NSChain LibRing

/-- the number of entries ≤ j -/
def cnt : List Nat → Nat → Nat
  | [], _ => 0
  | n :: l, j => cond (Nat.ble n j) (cnt l j + 1) (cnt l j)

section Gen
variable {R : Type} (K : CRing R)

/-- indicator of n ≤ j -/
def ind (n j : Nat) : R := cond (Nat.ble n j) K.one K.zero

def gd (f : Nat → R) (j : Nat) : R := K.add (f j) (K.neg (f (j + 1)))

def lsumN (h : Nat → R) : List Nat → R
  | [] => K.zero
  | n :: l => K.add (h n) (lsumN h l)

theorem ble_false {n j : Nat} (h : j < n) : Nat.ble n j = false := by
  cases hb : Nat.ble n j with
  | false => rfl
  | true => exact absurd (Nat.lt_of_lt_of_le h (Nat.le_of_ble_eq_true hb)) (Nat.lt_irrefl j)

theorem zero_below (f : Nat → R) (n : Nat) :
    ∀ J, J ≤ n → sumN K (fun j => K.mul (ind K n j) (gd K f j)) J = K.zero
  | 0, _ => rfl
  | J + 1, h => by
    show K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) J) (K.mul (ind K n J) (gd K f J)) = K.zero
    rw [zero_below f n J (Nat.le_of_lt h)]
    have hi : ind K n J = K.zero := by
      show cond (Nat.ble n J) K.one K.zero = K.zero
      rw [ble_false h]; rfl
    rw [hi]; lib_ring

theorem tele_one (f : Nat → R) (n : Nat) :
    ∀ M, n ≤ M → K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (f M) = f n
  | 0, h => by
    have e : n = 0 := Nat.eq_zero_of_le_zero h
    rw [e]; show K.add K.zero (f 0) = f 0; lib_ring
  | M + 1, h => by
    cases Nat.eq_or_lt_of_le h with
    | inl e =>
      rw [zero_below K f n (M + 1) (Nat.le_of_eq e.symm), e]; lib_ring
    | inr l =>
      have hM : n ≤ M := Nat.le_of_lt_succ l
      have ih := tele_one f n M hM
      have hi : ind K n M = K.one := by
        show cond (Nat.ble n M) K.one K.zero = K.one
        rw [Nat.ble_eq_true_of_le hM]; rfl
      show K.add (K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (K.mul (ind K n M) (gd K f M)))
        (f (M + 1)) = f n
      rw [hi, ← ih]
      show _ = K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (f M)
      generalize sumN K (fun j => K.mul (ind K n j) (gd K f j)) M = S
      show K.add (K.add S (K.mul K.one (K.add (f M) (K.neg (f (M + 1)))))) (f (M + 1)) = K.add S (f M)
      lib_ring

theorem sumN_congr (f g : Nat → R) (h : ∀ j, f j = g j) : ∀ N, sumN K f N = sumN K g N
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K f N) (f N) = K.add (sumN K g N) (g N)
    rw [sumN_congr f g h N, h N]

theorem cnt_step (n : Nat) (l : List Nat) (g : Nat → R) (j : Nat) :
    K.mul (castN K (cnt (n :: l) j)) (g j) = K.add (K.mul (ind K n j) (g j)) (K.mul (castN K (cnt l j)) (g j)) := by
  show K.mul (castN K (cond (Nat.ble n j) (cnt l j + 1) (cnt l j))) (g j)
    = K.add (K.mul (cond (Nat.ble n j) K.one K.zero) (g j)) (K.mul (castN K (cnt l j)) (g j))
  generalize Nat.ble n j = b
  cases b with
  | true =>
    show K.mul (K.add K.one (castN K (cnt l j))) (g j) = K.add (K.mul K.one (g j)) (K.mul (castN K (cnt l j)) (g j))
    lib_ring
  | false =>
    show K.mul (castN K (cnt l j)) (g j) = K.add (K.mul K.zero (g j)) (K.mul (castN K (cnt l j)) (g j))
    lib_ring

theorem abel_list (f : Nat → R) (M : Nat) :
    ∀ (l : List Nat), (∀ n, n ∈ l → n ≤ M) →
      lsumN K f l = K.add (sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M) (K.mul (castN K l.length) (f M))
  | [], _ => by
    show K.zero = K.add (sumN K (fun j => K.mul K.zero (gd K f j)) M) (K.mul K.zero (f M))
    have hz : sumN K (fun j => K.mul K.zero (gd K f j)) M = sumN K (fun _ => K.zero) M := by
      exact sumN_congr K _ _ (fun j => by show K.mul K.zero (gd K f j) = K.zero; lib_ring) M
    rw [hz, sumN_zero]; lib_ring
  | n :: l, h => by
    have ih := abel_list f M l (fun x hx => h x (List.Mem.tail _ hx))
    have t := tele_one K f n M (h n (List.Mem.head _))
    have hs : sumN K (fun j => K.mul (castN K (cnt (n :: l) j)) (gd K f j)) M
        = K.add (sumN K (fun j => K.mul (ind K n j) (gd K f j)) M) (sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M) := by
      rw [← sumN_add]; exact sumN_congr K _ _ (fun j => cnt_step K n l (gd K f) j) M
    show K.add (f n) (lsumN K f l) = K.add (sumN K (fun j => K.mul (castN K (cnt (n :: l) j)) (gd K f j)) M)
      (K.mul (K.add K.one (castN K l.length)) (f M))
    rw [hs, ih, ← t]
    generalize sumN K (fun j => K.mul (ind K n j) (gd K f j)) M = A
    generalize sumN K (fun j => K.mul (castN K (cnt l j)) (gd K f j)) M = B
    lib_ring

end Gen

end AbelCount

#print axioms AbelCount.zero_below
#print axioms AbelCount.tele_one
#print axioms AbelCount.abel_list

#print axioms: 3 theorems, no axioms.

79.175 Counting function of distinct modes

For duplicate-free modes, \(\#\{p:|k_p|_\infty \le j\}\le (2j+1)^3\) for every \(j\). The modes with norm at most \(j\) form a duplicate-free list inside the cube.

ns/ModeCount.lean

/-
  ns/ModeCount.lean — T37 cycle 948 (goal G624c, piece 6): the counting function of a set of distinct modes.
  For a duplicate-free list l of modes k ∈ ℤ³ with sup norms nrm k = max(|k₁|, |k₂|, |k₃|):
    cnt_cube   cnt(norms l, j) ≤ (2j + 1)·((2j + 1)·(2j + 1))   for every j,
  i.e. the input of AbelCount.abel_list is bounded by the cube count (CubeCount.count_cube) — the entries of l
  with nrm ≤ j form a duplicate-free sublist inside the cube [−j, j]³.

  Lean 4 core plus ns/CubeCount, ns/AbelCount (cnt); own max and filter (core Nat.max lemmas use propext).
-/
import ns.CubeCount
import ns.AbelCount

namespace ModeCount
open CubeCount AbelCount

def mx (a b : Nat) : Nat := cond (Nat.ble a b) b a

theorem le_mx_left (a b : Nat) : a ≤ mx a b := by
  show a ≤ cond (Nat.ble a b) b a
  cases hb : Nat.ble a b with
  | true => exact Nat.le_of_ble_eq_true hb
  | false => exact Nat.le_refl a

theorem le_mx_right (a b : Nat) : b ≤ mx a b := by
  show b ≤ cond (Nat.ble a b) b a
  cases hb : Nat.ble a b with
  | true => exact Nat.le_refl b
  | false =>
    cases Nat.lt_or_ge a b with
    | inl h => exact absurd (Nat.ble_eq_true_of_le (Nat.le_of_lt h)) (by rw [hb]; exact Bool.noConfusion)
    | inr h => exact h

/-- sup norm |k|_∞ -/
def nrm (k : Mode) : Nat := mx k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs)

def norms : List Mode → List Nat
  | [] => []
  | k :: l => nrm k :: norms l

/-- the modes with nrm ≤ j -/
def flt (j : Nat) : List Mode → List Mode
  | [] => []
  | k :: l => cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l)

theorem flt_len (j : Nat) : ∀ (l : List Mode), (flt j l).length = cnt (norms l) j
  | [] => rfl
  | k :: l => by
    show (cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l)).length = cond (Nat.ble (nrm k) j) (cnt (norms l) j + 1) (cnt (norms l) j)
    cases Nat.ble (nrm k) j with
    | true => show (flt j l).length + 1 = cnt (norms l) j + 1; rw [flt_len j l]
    | false => exact flt_len j l

theorem mem_flt (j : Nat) (x : Mode) : ∀ (l : List Mode), x ∈ flt j l → x ∈ l ∧ nrm x ≤ j
  | [], h => nomatch h
  | k :: l, h => by
    have h' : x ∈ cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l) := h
    cases hb : Nat.ble (nrm k) j with
    | true =>
      rw [hb] at h'
      cases h' with
      | head => exact ⟨List.Mem.head _, Nat.le_of_ble_eq_true hb⟩
      | tail _ h'' =>
        have r := mem_flt j x l h''
        exact ⟨List.Mem.tail _ r.1, r.2⟩
    | false =>
      rw [hb] at h'
      have r := mem_flt j x l h'
      exact ⟨List.Mem.tail _ r.1, r.2⟩

theorem flt_pw (j : Nat) : ∀ (l : List Mode), List.Pairwise (fun a b => a ≠ b) l →
    List.Pairwise (fun a b => a ≠ b) (flt j l)
  | [], _ => List.Pairwise.nil
  | k :: l, hp => by
    cases hp with
    | cons hk hl =>
      show List.Pairwise _ (cond (Nat.ble (nrm k) j) (k :: flt j l) (flt j l))
      cases Nat.ble (nrm k) j with
      | true => exact List.Pairwise.cons (fun y hy => hk y (mem_flt j y l hy).1) (flt_pw j l hl)
      | false => exact flt_pw j l hl

theorem incube (j : Nat) (k : Mode) (h : nrm k ≤ j) : InCube j k :=
  ⟨Nat.le_trans (le_mx_left _ _) h,
   Nat.le_trans (Nat.le_trans (le_mx_left _ _) (le_mx_right _ _)) h,
   Nat.le_trans (Nat.le_trans (le_mx_right _ _) (le_mx_right _ _)) h⟩

theorem cnt_cube (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l) (j : Nat) :
    cnt (norms l) j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1)) := by
  rw [← flt_len j l]
  exact count_cube j (flt j l) (flt_pw j l hd) (fun k hk => incube j k (mem_flt j k l hk).2)

end ModeCount

#print axioms ModeCount.le_mx_left
#print axioms ModeCount.le_mx_right
#print axioms ModeCount.flt_len
#print axioms ModeCount.cnt_cube

#print axioms: 4 theorems, no axioms.

79.176 The polynomial inequality for the lattice weights

For \(a\ge 1\), \(b=a+1\): \(b^4=a^4+D\) and \((a+b)^3D\le 108\,a^3b^3\), \(D=b^3+b^2a+ba^2+a^3\). Hence \((2a+1)^3(a^{-4}-b^{-4})\le 108(a^{-1}-b^{-1})\).

ns/PolyBound.lean

/-
  ns/PolyBound.lean — T37 cycle 949 (goal G624c, piece 7): the polynomial inequality behind the uniform bound
  of Σ_p |k_p|_∞^{−4}. With a ≥ 1, b = a + 1 and D = b³ + b²a + ba² + a³ (so b⁴ = a⁴ + D):
    four_diff   b⁴ = a⁴ + D
    poly_bound  (a + b)³ · D ≤ 108 · a³ · b³
  from a + b ≤ 3a and D ≤ 4b³; i.e. (2a+1)³(a^{−4} − (a+1)^{−4}) ≤ 108 (a^{−1} − (a+1)^{−1}).
  (The sharp constant is 51 — computed — but its witness identity is too heavy for the kernel; 108 suffices.)

  Lean 4 core plus lib/Semi (lib_nat).
-/
import lib.Semi

namespace PolyBound

theorem four_diff (a : Nat) :
    (a + 1) * (a + 1) * (a + 1) * (a + 1) = a * a * a * a +
      ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a) := by
  lib_nat

theorem mul3 {x y : Nat} (h : x ≤ y) : x * x * x ≤ y * y * y :=
  Nat.mul_le_mul (Nat.mul_le_mul h h) h

theorem poly_bound (a : Nat) (ha : 1 ≤ a) :
    (a + (a + 1)) * (a + (a + 1)) * (a + (a + 1)) *
      ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a)
      ≤ 108 * (a * a * a) * ((a + 1) * (a + 1) * (a + 1)) := by
  have hb : a + 1 ≤ a + a := Nat.add_le_add_left ha a
  have h3 : a + (a + 1) ≤ 3 * a := by
    have : a + (a + 1) ≤ a + (a + a) := Nat.add_le_add_left hb a
    have e : a + (a + a) = 3 * a := by lib_nat
    rw [← e]; exact this
  have hab : a ≤ a + 1 := Nat.le_succ a
  let b := a + 1
  have t1 : (a + 1) * (a + 1) * a ≤ b * b * b := Nat.mul_le_mul_left _ hab
  have t2 : (a + 1) * a * a ≤ b * b * b := Nat.mul_le_mul (Nat.mul_le_mul_left _ hab) hab
  have t3 : a * a * a ≤ b * b * b := mul3 hab
  have hD : (a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a ≤ 4 * (b * b * b) := by
    have s := Nat.add_le_add (Nat.add_le_add (Nat.add_le_add (Nat.le_refl (b * b * b)) t1) t2) t3
    have e : b * b * b + b * b * b + b * b * b + b * b * b = 4 * (b * b * b) := by lib_nat
    rw [← e]; exact s
  have hC := mul3 h3
  have e2 : 3 * a * (3 * a) * (3 * a) * (4 * (b * b * b)) = 108 * (a * a * a) * (b * b * b) := by lib_nat
  rw [← e2]
  exact Nat.mul_le_mul hC hD

end PolyBound

#print axioms PolyBound.four_diff
#print axioms PolyBound.poly_bound

#print axioms: 2 theorems, no axioms.

79.177 The per-shell inequality

If \(A\,i_A=B\,i_B=1\), \(B=A+1\) and \(C D+W=108A^3B^3\) with \(W\ge 0\), then \(C(i_A^4-i_B^4)\le 108(i_A-i_B)\). Over any commutative ring with a nonnegative cone.

ns/LatticeStep.lean

/-
  ns/LatticeStep.lean — T37 cycle 951 (goal G624c, piece 8a): the per-shell inequality over a commutative ring
  with a nonnegative cone. With A·iA = 1, B·iB = 1, B = A + 1, iA, iB ≥ 0, D = B³ + B²A + BA² + A³ and a
  nonnegative W with C·D + W = 108·A³B³ (PolyBound.poly_bound, cast):
    step_le   C·(iA⁴ − iB⁴) ≤ 108·(iA − iB),
  i.e. (2j+1)³ (j^{−4} − (j+1)^{−4}) ≤ 108 (j^{−1} − (j+1)^{−1}); summed over j this telescopes.
  Proof: iA⁴ − iB⁴ = D·iA⁴iB⁴ and iA − iB = iA·iB = A³B³·iA⁴iB⁴, so the difference is W·iA⁴iB⁴ ≥ 0.

  Lean 4 core plus NSChain (Cone, Le), lib/RingTac.
-/
import ns.DyadicLarge
import lib.RingTac

namespace LatticeStep
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def p4 (x : R) : R := K.mul (K.mul x x) (K.mul x x)
def p3 (x : R) : R := K.mul (K.mul x x) x

theorem inv4_diff (A B iA iB : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one) :
    K.add (p4 K iA) (K.neg (p4 K iB)) =
      K.mul (K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A))
        (K.mul (p4 K iA) (p4 K iB)) := by
  unfold p4 p3
  -- D·iA⁴iB⁴ = (B⁴ − A⁴)·iA⁴iB⁴ = (B iB)⁴ iA⁴ − (A iA)⁴ iB⁴
  have e1 : K.mul (K.add (K.add (K.add (K.mul (K.mul B B) B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (K.mul (K.mul A A) A))
        (K.mul (K.mul (K.mul iA iA) (K.mul iA iA)) (K.mul (K.mul iB iB) (K.mul iB iB)))
      = K.add (K.mul (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul (K.mul B iB) (K.mul B iB))) (K.mul (K.mul iA iA) (K.mul iA iA)))
          (K.neg (K.mul (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul (K.mul A iA) (K.mul A iA))) (K.mul (K.mul iB iB) (K.mul iB iB)))) := by
    rw [hBA]; lib_ring
  rw [e1, hA, hB]; lib_ring

theorem step_le (A B iA iB C W : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one)
    (hW : P.nn W)
    (hCW : K.add (K.mul C (K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A))) W
      = K.mul (K.mul (castN K 108) (p3 K A)) (p3 K B)) :
    Le K P (K.mul C (K.add (p4 K iA) (K.neg (p4 K iB)))) (K.mul (castN K 108) (K.add iA (K.neg iB))) := by
  have hq : P.nn (K.mul (p4 K iA) (p4 K iB)) := by
    unfold p4
    exact P.nn_mul _ _ (P.nn_mul _ _ (P.nn_sq iA) (P.nn_sq iA)) (P.nn_mul _ _ (P.nn_sq iB) (P.nn_sq iB))
  refine ⟨K.mul W (K.mul (p4 K iA) (p4 K iB)), P.nn_mul _ _ hW hq, ?_⟩
  rw [inv4_diff K A B iA iB hA hB hBA]
  generalize hD : K.add (K.add (K.add (p3 K B) (K.mul (K.mul B B) A)) (K.mul B (K.mul A A))) (p3 K A) = D at hCW
  -- C·D·q + W·q = (C·D + W)·q = 108 A³B³ q
  have e2 : K.add (K.mul C (K.mul D (K.mul (p4 K iA) (p4 K iB)))) (K.mul W (K.mul (p4 K iA) (p4 K iB)))
      = K.mul (K.add (K.mul C D) W) (K.mul (p4 K iA) (p4 K iB)) := by lib_ring
  rw [e2, hCW]
  -- 108 A³B³ iA⁴iB⁴ = 108 (A iA)³ (B iB)³ iA iB = 108 iA iB = 108 (B − A) iA iB = 108 (iA (B iB) − iB (A iA))
  unfold p3 p4
  have e3 : K.mul (K.mul (K.mul (castN K 108) (K.mul (K.mul A A) A)) (K.mul (K.mul B B) B))
        (K.mul (K.mul (K.mul iA iA) (K.mul iA iA)) (K.mul (K.mul iB iB) (K.mul iB iB)))
      = K.mul (K.mul (castN K 108) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA)))
          (K.mul (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul B iB)) (K.mul iA iB)) := by lib_ring
  have e4 : K.add iA (K.neg iB) = K.mul (K.add B (K.neg A)) (K.mul iA iB) := by
    have : K.mul (K.add B (K.neg A)) (K.mul iA iB) = K.add (K.mul iA (K.mul B iB)) (K.neg (K.mul iB (K.mul A iA))) := by
      lib_ring
    rw [this, hA, hB]; lib_ring
  rw [e3, e4, hA, hB, hBA]; lib_ring

end Gen

end LatticeStep

#print axioms LatticeStep.inv4_diff
#print axioms LatticeStep.step_le

#print axioms: 2 theorems, no axioms.

79.178 The shell sum is bounded by 108

For entries \(\ge 1\) with \(\mathrm {cnt}(L,j)\le (2j+1)^3\): \(\sum _{j<M}\mathrm {cnt}(L,j)\,(j^{-4}-(j+1)^{-4})\le 108\) for every \(M\), over an ordered field. Shells \(j\ge 1\) by the per-shell inequality, then telescoping; the shell \(j=0\) is empty.

ns/LatticeSum.lean

/-
  ns/LatticeSum.lean — T37 cycle 954 (goal G624c, piece 8b): the shell sum of the Abel identity is bounded by 108,
  over an ordered field (inverses of nonzero elements), uniformly in M. With iv j = 1/j, f j = (iv j)⁴, and a list
  L of naturals ≥ 1 whose counting function satisfies cnt(L, j) ≤ (2j+1)³ (ModeCount.cnt_cube):
    shell_term   cnt(L, i+1)·(f(i+1) − f(i+2)) ≤ 108·(iv(i+1) − iv(i+2))
    shell_sum    Σ_{j<M} cnt(L, j)·(f j − f(j+1)) ≤ 108.
  The term j = 0 vanishes (all entries ≥ 1); the others use PolyBound (cast), LatticeStep.step_le and telescoping.

  Lean 4 core plus ns/AbelCount, ns/PolyBound, ns/LatticeStep, ns/BaselQ (inv_nn), ns/GoodModesQ, ns/RatModel.
-/
import ns.AbelCount
import ns.PolyBound
import ns.LatticeStep
import ns.BaselQ

namespace LatticeSum
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep GoodModesQ

theorem cnt_zero : ∀ (L : List Nat), (∀ n, n ∈ L → 1 ≤ n) → cnt L 0 = 0
  | [], _ => rfl
  | n :: L, h => by
    have hn : 1 ≤ n := h n (List.Mem.head _)
    have hb : Nat.ble n 0 = false := AbelCount.ble_false hn
    show cond (Nat.ble n 0) (cnt L 0 + 1) (cnt L 0) = 0
    rw [hb]; exact cnt_zero L (fun x hx => h x (List.Mem.tail _ hx))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem castN_mul (a : Nat) : ∀ b, castN K (a * b) = K.mul (castN K a) (castN K b)
  | 0 => by show K.zero = K.mul (castN K a) K.zero; lib_ring
  | b + 1 => by
    show castN K (a * b + a) = K.mul (castN K a) (K.add K.one (castN K b))
    rw [RatModel.cast_add, castN_mul a b]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def iv (j : Nat) : R := inv (castN K j)
def f4 (j : Nat) : R := p4 K (iv K inv j)

include O mul_inv h10 in
theorem iv_mul (n : Nat) : K.mul (castN K (n + 1)) (iv K inv (n + 1)) = K.one :=
  mul_inv _ (cast_succ_ne K O h10 n)

include mul_inv h10 in
theorem iv_nn (n : Nat) : O.nn (iv K inv (n + 1)) :=
  BaselQ.inv_nn K O inv _ (RatModel.nn_cast K O.toCone (n + 1)) (iv_mul K O inv mul_inv h10 n)

/-- one shell, with the inverses as variables: c·(iA⁴ − iB⁴) ≤ 108·(iA − iB) for A = a, B = a + 1 -/
theorem shell_core (a : Nat) (ha : 1 ≤ a) (c : Nat) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
    (iA iB : R) (hA : K.mul (castN K a) iA = K.one) (hB : K.mul (castN K (a + 1)) iB = K.one) :
    Le K O.toCone (K.mul (castN K c) (K.add (p4 K iA) (K.neg (p4 K iB))))
      (K.mul (castN K 108) (K.add iA (K.neg iB))) := by
  have pb := PolyBound.poly_bound a ha
  have ⟨w, hw⟩ := DyadicModelZ.le_dest pb
  have hcast := congrArg (castN K) hw
  -- explicit casts
  have cB : castN K (a + 1) = K.add (castN K a) K.one := by show K.add K.one (castN K a) = _; lib_ring
  have cS : castN K (a + (a + 1)) = K.add (castN K a) (K.add (castN K a) K.one) := by
    rw [RatModel.cast_add K a (a + 1), cB]
  have c3 : ∀ x, castN K (x * x * x) = K.mul (K.mul (castN K x) (castN K x)) (castN K x) := fun x => by
    rw [castN_mul K (x * x) x, castN_mul K x x]
  have cD : castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a)
      = K.add (K.add (K.add (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K (a + 1)))
          (K.mul (K.mul (castN K (a + 1)) (castN K (a + 1))) (castN K a)))
          (K.mul (K.mul (castN K (a + 1)) (castN K a)) (castN K a))) (K.mul (K.mul (castN K a) (castN K a)) (castN K a)) := by
    rw [RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a) (a * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a) ((a + 1) * a * a),
      RatModel.cast_add K ((a + 1) * (a + 1) * (a + 1)) ((a + 1) * (a + 1) * a),
      c3 (a + 1), c3 a, castN_mul K ((a + 1) * (a + 1)) a, castN_mul K (a + 1) (a + 1),
      castN_mul K ((a + 1) * a) a, castN_mul K (a + 1) a]
  have cL : castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1)) *
        ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a) + w)
      = K.add (K.mul (castN K ((a + (a + 1)) * (a + (a + 1)) * (a + (a + 1))))
          (castN K ((a + 1) * (a + 1) * (a + 1) + (a + 1) * (a + 1) * a + (a + 1) * a * a + a * a * a))) (castN K w) := by
    rw [RatModel.cast_add, castN_mul]
  have cR : castN K (108 * (a * a * a) * ((a + 1) * (a + 1) * (a + 1)))
      = K.mul (K.mul (castN K 108) (castN K (a * a * a))) (castN K ((a + 1) * (a + 1) * (a + 1))) := by
    rw [castN_mul K (108 * (a * a * a)), castN_mul K 108]
  rw [cL, cR, cD, c3 (a + (a + 1)), c3 a, c3 (a + 1), cS, cB] at hcast
  have cC : castN K ((a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
      = K.mul (K.mul (K.add (castN K a) (K.add (castN K a) K.one)) (K.add (castN K a) (K.add (castN K a) K.one)))
          (K.add (castN K a) (K.add (castN K a) K.one)) := by
    rw [castN_mul K (a + (a + 1)), castN_mul K (a + (a + 1)), cS]; lib_ring
  rw [cB] at hB
  have An0 : O.nn (castN K a) := RatModel.nn_cast K O.toCone a
  have m0 := RatModel.cast_mono K O.toCone _ _ hc
  rw [cC] at m0
  generalize castN K a = A at hA hB hcast m0 An0
  have Bn : O.nn (K.add A K.one) := O.nn_add _ _ An0 (RatModel.nn_one' K O.toCone)
  have hD := inv4_diff K A (K.add A K.one) iA iB hA hB rfl
  have Dn : O.nn (K.add (K.add (K.add (p3 K (K.add A K.one)) (K.mul (K.mul (K.add A K.one) (K.add A K.one)) A))
      (K.mul (K.add A K.one) (K.mul A A))) (p3 K A)) := by
    unfold p3
    exact O.nn_add _ _ (O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) Bn) (O.nn_mul _ _ (O.nn_sq _) An0))
      (O.nn_mul _ _ Bn (O.nn_sq _))) (O.nn_mul _ _ (O.nn_sq _) An0)
  have gn : O.nn (K.add (p4 K iA) (K.neg (p4 K iB))) := by
    rw [hD]; unfold p4
    exact O.nn_mul _ _ Dn (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)))
  have h1 := le_mul_nn K O.toCone _ _ _ gn m0
  rw [K.mul_comm _ (castN K c), K.mul_comm (K.add (p4 K iA) (K.neg (p4 K iB)))] at h1
  have h2 := step_le K O.toCone A (K.add A K.one) iA iB
    (K.mul (K.mul (K.add A (K.add A K.one)) (K.add A (K.add A K.one))) (K.add A (K.add A K.one))) (castN K w) hA hB rfl (RatModel.nn_cast K O.toCone w) (by
    unfold p3; rw [← hcast]; lib_ring)
  exact le_trans' K O.toCone _ _ _ h1 h2

include mul_inv h10 in
theorem shell_term (L : List Nat) (i : Nat)
    (hc : cnt L (i + 1) ≤ (2 * (i + 1) + 1) * ((2 * (i + 1) + 1) * (2 * (i + 1) + 1))) :
    Le K O.toCone (K.mul (castN K (cnt L (i + 1))) (gd K (f4 K inv) (i + 1)))
      (K.mul (castN K 108) (K.add (iv K inv (i + 1)) (K.neg (iv K inv (i + 1 + 1))))) := by
  have e2 : 2 * (i + 1) + 1 = (i + 1) + ((i + 1) + 1) := by lib_nat
  rw [e2] at hc
  exact shell_core K O (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) _ hc _ _
    (iv_mul K O inv mul_inv h10 i) (iv_mul K O inv mul_inv h10 (i + 1))

def u (j : Nat) : R := match j with
  | 0 => iv K inv 1
  | j + 1 => iv K inv (j + 1)

include mul_inv h10 in
theorem u_nn : ∀ j, O.nn (u K inv j)
  | 0 => iv_nn K O inv mul_inv h10 0
  | j + 1 => iv_nn K O inv mul_inv h10 j

include O mul_inv h10 in
theorem iv_one : iv K inv 1 = K.one := by
  have h := iv_mul K O inv mul_inv h10 0
  have e : castN K (0 + 1) = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [e] at h
  calc iv K inv 1 = K.mul K.one (iv K inv 1) := by lib_ring
    _ = K.one := h

include mul_inv h10 in
theorem shell_sum (L : List Nat) (h1 : ∀ n, n ∈ L → 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f4 K inv) j)) M) (castN K 108) := by
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt L j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (u K inv j) (K.neg (u K inv (j + 1))))) := by
    intro j
    cases j with
    | zero =>
      rw [cnt_zero L h1]
      refine ⟨K.zero, O.nn_zero, ?_⟩
      show K.add (K.mul K.zero (gd K (f4 K inv) 0)) K.zero = K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (iv K inv 1)))
      lib_ring
    | succ i => exact shell_term K O inv mul_inv h10 L i (hc (i + 1))
  have hs := sum_le K O.toCone _ _ term M
  rw [sumN_mul_left, telescope] at hs
  refine le_trans' K O.toCone _ _ _ hs ⟨K.mul (castN K 108) (u K inv M), O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108)
    (u_nn K O inv mul_inv h10 M), ?_⟩
  show K.add (K.mul (castN K 108) (K.add (iv K inv 1) (K.neg (u K inv M)))) (K.mul (castN K 108) (u K inv M)) = castN K 108
  rw [iv_one K O inv mul_inv h10]; lib_ring

end Gen

end LatticeSum

#print axioms LatticeSum.cnt_zero
#print axioms LatticeSum.castN_mul
#print axioms LatticeSum.shell_term
#print axioms LatticeSum.shell_sum

#print axioms: 4 theorems, no axioms.

79.179 Uniform lattice bound \(\sum |k|_\infty ^{-4}\le 135\)

For every duplicate-free list of nonzero modes \(k\in \mathbb Z^3\), over an ordered field: \(\sum _k|k|_\infty ^{-4}\le 135\), independent of the truncation. Abel identity, shell sum \(\le 108\), last term \(\le 27\) by the cube count.

ns/LatticeBound.lean

/-
  ns/LatticeBound.lean — T37 cycle 958 (goal G624c, piece 8c): the uniform lattice bound. Over an ordered field,
  for every duplicate-free list l of nonzero modes k ∈ ℤ³ (any truncation, any M ≥ |k|_∞):
    lattice_bound   Σ_{k∈l} |k|_∞^{−4} ≤ 135.
  Abel identity (AbelCount.abel_list) = shell sum (≤ 108, LatticeSum.shell_sum) + |l|·(M+1)^{−4} (≤ 27, since
  |l| ≤ (2M+3)³ ≤ 27(M+1)³ by the cube count). This is the factor Σ_p ν_p² of ForceSob for s = 2, bounded
  independently of the Galerkin truncation.

  Lean 4 core plus ns/LatticeSum, ns/ModeCount, ns/PolyBound (mul3).
-/
import ns.LatticeSum
import ns.ModeCount

namespace LatticeBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount

theorem mem_norms (n : Nat) : ∀ (l : List Mode), n ∈ norms l → ∃ k, k ∈ l ∧ n = nrm k
  | [], h => nomatch h
  | k :: l, h => by
    cases h with
    | head => exact ⟨k, List.Mem.head _, rfl⟩
    | tail _ h' =>
      have ⟨k', hk', e⟩ := mem_norms n l h'
      exact ⟨k', List.Mem.tail _ hk', e⟩

theorem cnt_all (j : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → n ≤ j) → cnt L j = L.length
  | [], _ => rfl
  | n :: L, h => by
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = L.length + 1
    rw [Nat.ble_eq_true_of_le (h n (List.Mem.head _)), cnt_all j L (fun x hx => h x (List.Mem.tail _ hx))]
    rfl

theorem cube27 (a : Nat) (ha : 1 ≤ a) : (2 * a + 1) * ((2 * a + 1) * (2 * a + 1)) ≤ 27 * (a * a * a) := by
  have h3 : 2 * a + 1 ≤ 3 * a := by
    have : 2 * a + 1 ≤ 2 * a + a := Nat.add_le_add_left ha _
    have e : 2 * a + a = 3 * a := by lib_nat
    rw [← e]; exact this
  have m := PolyBound.mul3 h3
  have e1 : (2 * a + 1) * ((2 * a + 1) * (2 * a + 1)) = (2 * a + 1) * (2 * a + 1) * (2 * a + 1) := by lib_nat
  have e2 : 3 * a * (3 * a) * (3 * a) = 27 * (a * a * a) := by lib_nat
  rw [e1, ← e2]; exact m

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- the last Abel term: c·(M+1)^{−4} ≤ 27 when c ≤ (2(M+1)+1)³ -/
theorem last_term (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (castN K 27) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have hiA := iv_nn K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have hMn : O.nn (castN K M) := RatModel.nn_cast K O.toCone M
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA hiA ⊢
  have cA : castN K (M + 1) = K.add K.one (castN K M) := rfl
  rw [cA] at hA m0
  generalize castN K M = X at hA m0 hMn
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  -- 27 A³ iA⁴ = 27 (A iA)³ iA = 27 iA
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul (K.add K.one X) (K.add K.one X)) (K.add K.one X))) (p4 K iA)
      = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul (K.add K.one X) (K.add K.one X)) (K.add K.one X))) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul (K.add K.one X) iA) (K.mul (K.add K.one X) iA))
            (K.mul (K.add K.one X) iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  -- iA ≤ 1 since 1 = iA + X·iA
  have s2 : Le K O.toCone iA K.one := ⟨K.mul X iA, O.nn_mul _ _ hMn hiA, by rw [← hA]; lib_ring⟩
  have s3 := le_mul_nn K O.toCone (castN K 27) _ _ (RatModel.nn_cast K O.toCone 27) s2
  have e3 : K.mul (castN K 27) K.one = castN K 27 := by lib_ring
  rw [e3] at s3
  exact le_trans' K O.toCone _ _ _ s1 s3

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−4} ≤ 135 for every duplicate-free list of nonzero modes -/
theorem lattice_bound (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (castN K 135) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have S := shell_sum K O inv mul_inv h10 (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ S T
  have e : K.add (castN K 108) (castN K 27) = castN K 135 := (RatModel.cast_add K 108 27).symm
  rw [e] at U; exact U

end Gen

end LatticeBound

#print axioms LatticeBound.mem_norms
#print axioms LatticeBound.cnt_all
#print axioms LatticeBound.cube27
#print axioms LatticeBound.last_term
#print axioms LatticeBound.lattice_bound

#print axioms: 5 theorems, no axioms.

79.180 Triad sparsity of Fourier modes

For distinct modes and \(\delta (p,q,r)=[k_p+k_q+k_r=0]\): \(\sum _q\delta (p,q,r)\le 1\) and \(\sum _r\delta (p,q,r)\le 1\). These are the hypotheses spA, spB of the truncation-uniform force bound.

ns/TriadSparse.lean

/-
  ns/TriadSparse.lean — T37 cycle 961 (goal G624c, last piece): triad sparsity of Fourier modes, the hypotheses
  spA, spB of ForceSob. For modes k : I → ℤ³ that are pairwise distinct on the list l and the triad indicator
  δ(p, q, r) = [k_p + k_q + k_r = 0] ∈ {0, 1}:
    spA   Σ_{q∈l} δ(p, q, r) ≤ 1     (for fixed p, r),
    spB   Σ_{r∈l} δ(p, q, r) ≤ 1     (for fixed p, q),
  over any commutative ring with a nonnegative cone. Generic form at_most_one: an indicator whose true set has a
  single k-value sums to at most 1 over a list with distinct k-values. The cancellation k_p + x + k_r = 0 =
  k_p + y + k_r ⇒ x = y is checked componentwise by lib_int (core Int.add_comm, add_left_cancel use propext).

  Lean 4 core plus ns/ForceBound (lsum_le), ns/RatModel (nn_one'), lib/IntRing (lib_int).
-/
import ns.ForceBound
import ns.RatModel
import lib.IntRing

namespace TriadSparse
open DetChain NSChain LibRing DyadicLarge

abbrev Mode := Int × Int × Int

def tadd (a b : Mode) : Mode := (a.1 + b.1, a.2.1 + b.2.1, a.2.2 + b.2.2)

def zero3 : Mode := (0, 0, 0)

theorem cancel1 (a c x y : Int) (hx : a + x + c = 0) (hy : a + y + c = 0) : x = y := by
  have ex : x = (a + x + c) + (-a) + (-c) := by lib_int
  have ey : y = (a + y + c) + (-a) + (-c) := by lib_int
  rw [ex, ey, hx, hy]

theorem triad_uniq (a c x y : Mode) (hx : tadd (tadd a x) c = zero3) (hy : tadd (tadd a y) c = zero3) : x = y := by
  have h1x : a.1 + x.1 + c.1 = 0 := congrArg Prod.fst hx
  have h1y : a.1 + y.1 + c.1 = 0 := congrArg Prod.fst hy
  have h2x : a.2.1 + x.2.1 + c.2.1 = 0 := congrArg (fun t => t.2.1) hx
  have h2y : a.2.1 + y.2.1 + c.2.1 = 0 := congrArg (fun t => t.2.1) hy
  have h3x : a.2.2 + x.2.2 + c.2.2 = 0 := congrArg (fun t => t.2.2) hx
  have h3y : a.2.2 + y.2.2 + c.2.2 = 0 := congrArg (fun t => t.2.2) hy
  have e1 := cancel1 _ _ _ _ h1x h1y
  have e2 := cancel1 _ _ _ _ h2x h2y
  have e3 := cancel1 _ _ _ _ h3x h3y
  cases x with
  | mk x1 x23 => cases x23 with
    | mk x2 x3 => cases y with
      | mk y1 y23 => cases y23 with
        | mk y2 y3 =>
          have e1' : x1 = y1 := e1
          have e2' : x2 = y2 := e2
          have e3' : x3 = y3 := e3
          rw [e1', e2', e3']

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

def ind (b : Bool) : R := cond b K.one K.zero

theorem sum_zero (t : I → Bool) : ∀ (l : List I), (∀ q, q ∈ l → t q = false) → lsum K (fun q => ind K (t q)) l = K.zero
  | [], _ => rfl
  | x :: l, h => by
    show K.add (cond (t x) K.one K.zero) (lsum K (fun q => ind K (t q)) l) = K.zero
    rw [h x (List.Mem.head _), sum_zero t l (fun q hq => h q (List.Mem.tail _ hq))]
    show K.add K.zero K.zero = K.zero; lib_ring

/-- an indicator whose true set has a single k-value sums to at most 1 over distinct k-values -/
theorem at_most_one {β : Type} (k : I → β) (t : I → Bool) (uniq : ∀ q q', t q = true → t q' = true → k q = k q') :
    ∀ (l : List I), List.Pairwise (fun a b => k a ≠ k b) l → Le K P (lsum K (fun q => ind K (t q)) l) K.one
  | [], _ => ⟨K.one, RatModel.nn_one' K P, by show K.add K.zero K.one = K.one; lib_ring⟩
  | x :: l, hp => by
    cases hp with
    | cons hx hl =>
      show Le K P (K.add (cond (t x) K.one K.zero) (lsum K (fun q => ind K (t q)) l)) K.one
      cases htx : t x with
      | true =>
        have hz : lsum K (fun q => ind K (t q)) l = K.zero := sum_zero K t l (fun q hq => by
          cases htq : t q with
          | false => rfl
          | true => exact absurd (uniq x q htx htq) (hx q hq))
        rw [hz]
        exact ⟨K.zero, P.nn_zero, by show K.add (K.add K.one K.zero) K.zero = K.one; lib_ring⟩
      | false =>
        have ih := at_most_one k t uniq l hl
        cases ih with
        | intro d hd => exact ⟨d, hd.1, by rw [← hd.2]; show K.add (K.add K.zero _) d = _; lib_ring⟩

/-- the triad indicator [k_p + k_q + k_r = 0] -/
def delta [DecidableEq Mode] (k : I → Mode) (p q r : I) : R :=
  ind K (decide (tadd (tadd (k p) (k q)) (k r) = zero3))

theorem spA (k : I → Mode) (l : List I) (hd : List.Pairwise (fun a b => k a ≠ k b) l) (p r : I) :
    Le K P (lsum K (fun q => delta K k p q r) l) K.one :=
  at_most_one K P k (fun q => decide (tadd (tadd (k p) (k q)) (k r) = zero3))
    (fun _ _ hq hq' => triad_uniq _ _ _ _ (of_decide_eq_true hq) (of_decide_eq_true hq')) l hd

theorem spB (k : I → Mode) (l : List I) (hd : List.Pairwise (fun a b => k a ≠ k b) l) (p q : I) :
    Le K P (lsum K (fun r => delta K k p q r) l) K.one := by
  refine at_most_one K P k (fun r => decide (tadd (tadd (k p) (k q)) (k r) = zero3)) ?_ l hd
  intro r r' hr hr'
  have h1 := of_decide_eq_true hr
  have h2 := of_decide_eq_true hr'
  -- k_p + k_q + x = 0 = k_p + k_q + y, i.e. (k_p + k_q) + x + 0 = 0
  have z : ∀ a x : Mode, tadd a x = zero3 → tadd (tadd a x) zero3 = zero3 := fun a x h => by
    rw [h]; rfl
  exact triad_uniq (tadd (k p) (k q)) zero3 (k r) (k r') (z _ _ h1) (z _ _ h2)

end Gen

end TriadSparse

#print axioms TriadSparse.triad_uniq
#print axioms TriadSparse.at_most_one
#print axioms TriadSparse.spA
#print axioms TriadSparse.spB

#print axioms: 4 theorems, no axioms.

79.181 Galerkin force bound uniform in the truncation

For distinct nonzero integer modes: \(\sum _r|N_r(u,v)|^2\le \iota ^2\,H_\mu (u)\cdot 135\,Z(v)\), \(\mu _p=|k_p|_\infty ^2\), for every finite mode set. Assembled from the weighted Cauchy–Schwarz bound, triad sparsity and the lattice bound.

ns/ForceUniform.lean

/-
  ns/ForceUniform.lean — T37 cycle 969 (goal G637): the Galerkin force bound with a constant independent of the
  truncation, over an ordered field. For integer modes kz : I → ℤ³, nonzero (every index) and pairwise distinct on the mode list
  l (any finite truncation), the triad indicator δ = [kz_p + kz_q + kz_r = 0] (TriadSparse.delta), any ring vectors
  k, u, v and weights μ_p = |kz_p|_∞², ν_p = |kz_p|_∞^{−2}:
    force_uniform   Σ_r |N_r(u,v)|² ≤ ι² · H_μ(u) · (135 · Z(v)),
  H_μ(u) = Σ_p |kz_p|_∞⁴ |u_p|², Z(v) = Σ_q |k_q|² |v_q|² — the discrete ‖(u·∇)v‖² ≲ ‖u‖²_{H²}‖∇v‖² with a constant
  (135) that does not depend on the number of modes. ForceSob.total_sob + TriadSparse (spA, spB) +
  LatticeBound.lattice_bound (Σ_p ν_p² = Σ_p |kz_p|_∞^{−4} ≤ 135).

  Lean 4 core plus ns/ForceSob, ns/TriadSparse, ns/LatticeBound.
-/
import ns.ForceSob
import ns.TriadSparse
import ns.LatticeBound

namespace ForceUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  AbelCount LatticeStep LatticeSum ModeCount CubeCount LatticeBound GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_norms (g : Nat → R) (kz : I → Mode) :
    ∀ (l : List I), lsum K (fun p => g (nrm (kz p))) l = lsumN K g (norms (l.map kz))
  | [] => rfl
  | p :: l => by
    show K.add (g (nrm (kz p))) (lsum K (fun p => g (nrm (kz p))) l) = K.add (g (nrm (kz p))) (lsumN K g (norms (l.map kz)))
    rw [lsum_norms g kz l]

theorem mem_map_inv (kz : I → Mode) (y : Mode) : ∀ (l : List I), y ∈ l.map kz → ∃ p, p ∈ l ∧ kz p = y
  | [], h => nomatch h
  | p :: l, h => by
    cases h with
    | head => exact ⟨p, List.Mem.head _, rfl⟩
    | tail _ h' =>
      have ⟨q, hq, e⟩ := mem_map_inv kz y l h'
      exact ⟨q, List.Mem.tail _ hq, e⟩

theorem pw_map (kz : I → Mode) : ∀ (l : List I), List.Pairwise (fun a b => kz a ≠ kz b) l →
    List.Pairwise (fun a b => a ≠ b) (l.map kz)
  | [], _ => List.Pairwise.nil
  | p :: l, hp => by
    cases hp with
    | cons hx hl =>
      exact List.Pairwise.cons (fun y hy => by
        have ⟨q, hq, e⟩ := mem_map_inv kz y l hy
        rw [← e]; exact hx q hq) (pw_map kz l hl)

theorem delta01 (kz : I → Mode) (p q r : I) :
    TriadSparse.delta K kz p q r = K.zero ∨ TriadSparse.delta K kz p q r = K.one := by
  show cond _ K.one K.zero = K.zero ∨ cond _ K.one K.zero = K.one
  generalize decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) = b
  cases b with
  | false => exact Or.inl rfl
  | true => exact Or.inr rfl

include O mul_inv h10 in
theorem inv_cast (n : Nat) (hn : 1 ≤ n) : K.mul (castN K n) (inv (castN K n)) = K.one := by
  cases n with
  | zero => exact absurd hn (Nat.not_succ_le_zero 0)
  | succ n' => exact mul_inv _ (cast_succ_ne K O h10 n')

def mu (kz : I → Mode) (p : I) : R := K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p)))
def nu (kz : I → Mode) (p : I) : R := K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p))))

include mul_inv h10 in
theorem force_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι : R) (k u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι k u v r) (Nl K l (TriadSparse.delta K kz) ι k u v r)) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (u p) (u p))) l)
        (K.mul (castN K 135) (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l)))) := by
  have hμν : ∀ p, K.mul (mu K kz p) (nu K inv kz p) = K.one := by
    intro p
    have hc := inv_cast K O inv mul_inv h10 (nrm (kz p)) (h1 p)
    show K.mul (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))
      (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p))))) = K.one
    generalize castN K (nrm (kz p)) = c at hc
    calc K.mul (K.mul c c) (K.mul (inv c) (inv c)) = K.mul (K.mul c (inv c)) (K.mul c (inv c)) := by lib_ring
      _ = K.one := by rw [hc]; lib_ring
  have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι k u v (mu K kz) (nu K inv kz) (delta01 K kz) hμν
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  have eS : lsum K (fun p => K.mul (nu K inv kz p) (nu K inv kz p)) l = lsumN K (f4 K inv) (norms (l.map kz)) :=
    lsum_norms K (f4 K inv) kz l
  have hS := lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)
  rw [← eS] at hS
  generalize lsum K (fun p => K.mul (nu K inv kz p) (nu K inv kz p)) l = S at T hS
  have Zn : O.nn (lsum K (fun q => K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have Hn : O.nn (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have s1 := le_mul_right' K O.toCone hS Zn
  have s2 := le_mul_nn K O.toCone _ _ _ Hn s1
  have s3 := le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) s2
  exact le_trans' K O.toCone _ _ _ T s3

end Gen

end ForceUniform

#print axioms ForceUniform.lsum_norms
#print axioms ForceUniform.pw_map
#print axioms ForceUniform.force_uniform

#print axioms: 3 theorems, no axioms.

79.182 Pairing inequality for the enstrophy step

\(2\varepsilon \sum _r w_r\,(a_r\cdot N_r)\le \varepsilon ^2\sum _r w_r^2|a_r|^2+\sum _r|N_r|^2\), from \(\sum _r|\varepsilon w_ra_r-N_r|^2\ge 0\). Over any commutative ring with a nonnegative cone.

ns/EnstPair.lean

/-
  ns/EnstPair.lean — T37 cycle 973 (goal G638, piece a): the pairing inequality for the enstrophy step, over any
  commutative ring with a nonnegative cone. For weights w_r (w_r = |k_r|² in the enstrophy step), vectors a_r, N_r:
    pair_bound   2ε Σ_r w_r (a_r·N_r) ≤ ε² Σ_r w_r²|a_r|² + Σ_r |N_r|²
  from Σ_r |ε w_r a_r − N_r|² ≥ 0. With ForceUniform (Σ|N_r|² ≤ 135 ι² H Z, H ≤ Σ w_r²|a_r|²) the nonlinear term
  of d/dt Σ|k|²|a|² is at most (ε² + 135 ι² Z)/(2ε) · Σ|k|⁴|a|², which the dissipation 2ν Σ|k|⁴|a|² absorbs when
  Z = Σ|k|²|a|² is small — the N-uniform small-data enstrophy bound.

  Lean 4 core plus ns/ForceBound (lsum_le), ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.ForceBound

namespace EnstPair
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem pair_one (ε w : R) (a N : V3 R) :
    Le K P (K.mul (K.mul (K.add K.one K.one) ε) (K.mul w (dot3 K a N)))
      (K.add (K.mul (K.mul ε ε) (K.mul (K.mul w w) (dot3 K a a))) (dot3 K N N)) := by
  refine ⟨K.add (K.add (K.mul (K.add (K.mul (K.mul ε w) a.x) (K.neg N.x)) (K.add (K.mul (K.mul ε w) a.x) (K.neg N.x)))
      (K.mul (K.add (K.mul (K.mul ε w) a.y) (K.neg N.y)) (K.add (K.mul (K.mul ε w) a.y) (K.neg N.y))))
      (K.mul (K.add (K.mul (K.mul ε w) a.z) (K.neg N.z)) (K.add (K.mul (K.mul ε w) a.z) (K.neg N.z))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  unfold dot3
  lib_ring

theorem pair_bound (l : List I) (ε : R) (w : I → R) (a N : I → V3 R) :
    Le K P (lsum K (fun r => K.mul (K.mul (K.add K.one K.one) ε) (K.mul (w r) (dot3 K (a r) (N r)))) l)
      (K.add (K.mul (K.mul ε ε) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l))
        (lsum K (fun r => dot3 K (N r) (N r)) l)) := by
  have h := lsum_le K P _ _ (fun r => pair_one K P ε (w r) (a r) (N r)) l
  rw [lsum_add, lsum_mul_left K (K.mul ε ε)] at h
  exact h

end Gen

end EnstPair

#print axioms EnstPair.pair_one
#print axioms EnstPair.pair_bound

#print axioms: 2 theorems, no axioms.

79.183 Sup norm against Euclidean norm for integer modes

For \(k\in \mathbb Z^3\) cast into the ring: \(|k|_\infty ^2\le |k|^2\) and \(|k|_\infty ^4\le |k|^4\). Hence the weight \(H_\mu \) of the uniform force bound is at most \(\sum _p|k_p|^4|u_p|^2\).

ns/NormCompare.lean

/-
  ns/NormCompare.lean — T37 cycle 975 (goal G638, piece b): sup norm against Euclidean norm for integer modes cast
  into a commutative ring with a nonnegative cone. For kz ∈ ℤ³ with ring vector kv = (castZ kz₁, castZ kz₂, castZ kz₃):
    sq_castZ      castZ x · castZ x = castN |x| · castN |x|
    nrm_le_dot    |kz|_∞² ≤ |kv|²                (as ring elements)
    nrm4_le       |kz|_∞⁴ ≤ |kv|⁴
  so H_μ(u) = Σ |kz_p|_∞⁴|u_p|² ≤ Σ |k_p|⁴|u_p|² = ‖Δu‖² in ForceUniform.

  Lean 4 core plus ns/ModeCount (nrm, mx), ns/FourierGalerkin (V3, dot3), ns/ForceSob (le_mul_right').
-/
import ns.ModeCount
import ns.ForceSob

namespace NormCompare
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ModeCount CubeCount

theorem mx_cases (a b : Nat) : mx a b = a ∨ mx a b = b := by
  show cond (Nat.ble a b) b a = a ∨ cond (Nat.ble a b) b a = b
  cases Nat.ble a b with
  | true => exact Or.inr rfl
  | false => exact Or.inl rfl

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def kvec (k : Mode) : V3 R := ⟨castZ K k.1, castZ K k.2.1, castZ K k.2.2⟩

theorem sq_castZ (x : Int) : K.mul (castZ K x) (castZ K x) = K.mul (castN K x.natAbs) (castN K x.natAbs) := by
  cases x with
  | ofNat n => rfl
  | negSucc n =>
    show K.mul (K.neg (castN K (n + 1))) (K.neg (castN K (n + 1))) = K.mul (castN K (n + 1)) (castN K (n + 1))
    lib_ring

theorem nrm_le_dot (k : Mode) :
    Le K P (K.mul (castN K (nrm k)) (castN K (nrm k))) (dot3 K (kvec K k) (kvec K k)) := by
  show Le K P _ (K.add (K.add (K.mul (castZ K k.1) (castZ K k.1)) (K.mul (castZ K k.2.1) (castZ K k.2.1)))
    (K.mul (castZ K k.2.2) (castZ K k.2.2)))
  rw [sq_castZ, sq_castZ, sq_castZ]
  generalize hx : k.1.natAbs = x
  generalize hy : k.2.1.natAbs = y
  generalize hz : k.2.2.natAbs = z
  have hn : nrm k = mx x (mx y z) := by rw [← hx, ← hy, ← hz]; rfl
  rw [hn]
  have sx := P.nn_sq (castN K x)
  have sy := P.nn_sq (castN K y)
  have sz := P.nn_sq (castN K z)
  cases mx_cases x (mx y z) with
  | inl e =>
    rw [e]
    exact ⟨K.add (K.mul (castN K y) (castN K y)) (K.mul (castN K z) (castN K z)), P.nn_add _ _ sy sz, by lib_ring⟩
  | inr e =>
    rw [e]
    cases mx_cases y z with
    | inl e2 =>
      rw [e2]
      exact ⟨K.add (K.mul (castN K x) (castN K x)) (K.mul (castN K z) (castN K z)), P.nn_add _ _ sx sz, by lib_ring⟩
    | inr e2 =>
      rw [e2]
      exact ⟨K.add (K.mul (castN K x) (castN K x)) (K.mul (castN K y) (castN K y)), P.nn_add _ _ sx sy, by lib_ring⟩

theorem nrm4_le (k : Mode) :
    Le K P (K.mul (K.mul (castN K (nrm k)) (castN K (nrm k))) (K.mul (castN K (nrm k)) (castN K (nrm k))))
      (K.mul (dot3 K (kvec K k) (kvec K k)) (dot3 K (kvec K k) (kvec K k))) := by
  have h := nrm_le_dot K P k
  have hs := P.nn_sq (castN K (nrm k))
  generalize K.mul (castN K (nrm k)) (castN K (nrm k)) = s at h hs
  generalize dot3 K (kvec K k) (kvec K k) = d at h
  cases h with
  | intro e he =>
    -- (s + e)² = s² + (2se + e²), s = n² ≥ 0
    exact ⟨K.add (K.mul (K.add s s) e) (K.mul e e), P.nn_add _ _ (P.nn_mul _ _ (P.nn_add _ _ hs hs) he.1) (P.nn_sq e),
      by rw [← he.2]; lib_ring⟩

end Gen

end NormCompare

#print axioms NormCompare.sq_castZ
#print axioms NormCompare.nrm_le_dot
#print axioms NormCompare.nrm4_le

#print axioms: 3 theorems, no axioms.

79.184 One enstrophy step with implicit viscosity

For \((1+h\nu w_r)a_r'=a_r+hN_r\): \(\nu Z'+h\nu ^2A'\le \nu Z+h\sum _r|N_r|^2\), \(Z=\sum w|a|^2\), \(A=\sum w^2|a|^2\). Slack \(\nu w|a'-a|^2+h|\nu wa'-N|^2\); no inverses needed.

ns/EnstStep.lean

/-
  ns/EnstStep.lean — T37 cycle 976 (goal G638, piece c): one step of the enstrophy inequality for the implicit-viscosity
  (backward Euler) Galerkin step (1 + hν w_r) a'_r = a_r + h N_r, over any commutative ring with a nonnegative cone
  (h, ν, w_r ≥ 0; no inverses needed):
    mode_step   ν w|a'|² + h ν² w²|a'|² ≤ ν w|a|² + h|N|²
    enst_step   ν Z' + h ν² A' ≤ ν Z + h Σ_r |N_r|²,   Z = Σ w|a|²,  A = Σ w²|a|².
  Slack: ν w|a' − a|² + h|ν w a' − N|² (from 2a'·a ≤ |a'|² + |a|² and 2νw a'·N ≤ ν²w²|a'|² + |N|²).
  With w_r = |k_r|² and ForceUniform (Σ|N|² ≤ 135ι²·A·Z) the quantity νZ + hν²A is nonincreasing while
  135ι²Z ≤ ν² — next piece.

  Lean 4 core plus ns/ForceBound (lsum_le), ns/FourierGalerkin (V3, dot3), lib/RingTac.
-/
import ns.ForceBound

namespace EnstStep
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

/-- the step relation on one component: (1 + hνw) a' = a + h N -/
def StepC (h ν w a' a n : R) : Prop := K.mul (K.add K.one (K.mul (K.mul h ν) w)) a' = K.add a (K.mul h n)

def Step (h ν w : R) (a' a N : V3 R) : Prop :=
  StepC K h ν w a'.x a.x N.x ∧ StepC K h ν w a'.y a.y N.y ∧ StepC K h ν w a'.z a.z N.z

theorem comp_eq (h ν w a' a n : R) (hs : StepC K h ν w a' a n) :
    a = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg (K.mul h n)) := by
  rw [hs]; lib_ring

theorem mode_step (h ν w : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (K.add (K.mul (K.mul ν w) (dot3 K a' a')) (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (dot3 K a' a')))
      (K.add (K.mul (K.mul ν w) (dot3 K a a)) (K.mul h (dot3 K N N))) := by
  have hνw : P.nn (K.mul ν w) := P.nn_mul _ _ hν hw
  have sq3 : ∀ v : V3 R, P.nn (dot3 K v v) := fun v => dot3_nn K P v
  let dv : V3 R := ⟨K.add a'.x (K.neg a.x), K.add a'.y (K.neg a.y), K.add a'.z (K.neg a.z)⟩
  let ev : V3 R := ⟨K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x), K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y),
    K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z)⟩
  refine ⟨K.add (K.mul (K.mul ν w) (dot3 K dv dv)) (K.mul h (dot3 K ev ev)),
    P.nn_add _ _ (P.nn_mul _ _ hνw (sq3 dv)) (P.nn_mul _ _ hh (sq3 ev)), ?_⟩
  have ex := comp_eq K h ν w a'.x a.x N.x hs.1
  have ey := comp_eq K h ν w a'.y a.y N.y hs.2.1
  have ez := comp_eq K h ν w a'.z a.z N.z hs.2.2
  show K.add (K.add (K.mul (K.mul ν w) (dot3 K a' a')) (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (dot3 K a' a')))
      (K.add (K.mul (K.mul ν w) (dot3 K dv dv)) (K.mul h (dot3 K ev ev)))
    = K.add (K.mul (K.mul ν w) (dot3 K a a)) (K.mul h (dot3 K N N))
  unfold dot3
  show K.add (K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z)))
      (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul w w))) (K.add (K.add (K.mul a'.x a'.x) (K.mul a'.y a'.y)) (K.mul a'.z a'.z))))
      (K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul (K.add a'.x (K.neg a.x)) (K.add a'.x (K.neg a.x)))
          (K.mul (K.add a'.y (K.neg a.y)) (K.add a'.y (K.neg a.y)))) (K.mul (K.add a'.z (K.neg a.z)) (K.add a'.z (K.neg a.z)))))
        (K.mul h (K.add (K.add (K.mul (K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x)) (K.add (K.mul (K.mul ν w) a'.x) (K.neg N.x)))
          (K.mul (K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y)) (K.add (K.mul (K.mul ν w) a'.y) (K.neg N.y))))
          (K.mul (K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z)) (K.add (K.mul (K.mul ν w) a'.z) (K.neg N.z))))))
    = K.add (K.mul (K.mul ν w) (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z)))
      (K.mul h (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z)))
  rw [ex, ey, ez]
  lib_ring

theorem enst_step (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l))) := by
  have t := lsum_le K P _ _ (fun r => mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r)) l
  rw [lsum_add, lsum_add] at t
  have e1 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e2 : lsum K (fun r => K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))) l
      = K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e3 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e4 : lsum K (fun r => K.mul h (dot3 K (N r) (N r))) l = K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l) :=
    lsum_mul_left K h _ l
  rw [e1, e2, e3, e4] at t
  exact t

end Gen

end EnstStep

#print axioms EnstStep.mode_step
#print axioms EnstStep.enst_step

#print axioms: 2 theorems, no axioms.

79.185 Small-data invariant of the enstrophy step

If \(\nu Z_{t+1}+h\nu ^2A_{t+1}\le \nu Z_t+hcA_tZ_t\) and \(cQ_0\le \nu ^3\) (\(Q_t=\nu Z_t+h\nu ^2A_t\)), then \(Q_t\le Q_0\) and \(cZ_t\le \nu ^2\) for all \(t\). Over an ordered ring without zero divisors.

ns/EnstGlobal.lean

/-
  ns/EnstGlobal.lean — T37 cycle 977 (goal G638, piece d): the small-data invariant of the enstrophy step, over an
  ordered ring without zero divisors (ℚ included). Sequences Z_t, A_t ≥ 0, constants ν > 0, h ≥ 0, c ≥ 0 with
    νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h·c·A_t·Z_t           (EnstStep.enst_step with Σ|N|² ≤ c·A·Z, c = 135ι²)
  and Q_t = νZ_t + hν²A_t. If c·Q_0 ≤ ν³ then for every t
    q_le     Q_t ≤ Q_0,      z_small   c·Z_t ≤ ν²,
  i.e. the enstrophy of the Galerkin scheme stays below Q_0/ν for all time, with constants independent of the
  truncation (global small-data bound in 3D).

  Lean 4 core plus ns/DyadicSmallGlobal (OrdR), ns/DyadicLarge (le_mul_nn), lib/RingTac.
-/
import ns.DyadicSmallGlobal
import ns.DyadicLarge
import lib.RingTac

namespace EnstGlobal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- cancel a positive factor: ν x ≤ ν y, ν ≥ 0, ν ≠ 0 ⇒ x ≤ y -/
theorem le_of_mul_le (ν x y : R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (h : Le K O.toCone (K.mul ν x) (K.mul ν y)) :
    Le K O.toCone x y := by
  cases O.total x y with
  | inl hxy => exact hxy
  | inr hyx =>
    have h2 := le_mul_nn K O.toCone ν y x hν hyx
    have e := O.antisym _ _ h h2
    have z : K.mul ν (K.add x (K.neg y)) = K.zero := by
      have : K.mul ν (K.add x (K.neg y)) = K.add (K.mul ν x) (K.neg (K.mul ν y)) := by lib_ring
      rw [this, e]; lib_ring
    cases O.noZD _ _ z with
    | inl h0 => exact absurd h0 hν0
    | inr h0 =>
      have : x = y := by
        calc x = K.add (K.add x (K.neg y)) y := by lib_ring
          _ = y := by rw [h0]; lib_ring
      rw [this]; exact le_refl' K O.toCone y

def Q (ν h : R) (Z A : Nat → R) (t : Nat) : R := K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))

theorem q_le (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ t, Le K O.toCone (Q K ν h Z A t) (Q K ν h Z A 0)
  | 0 => le_refl' K O.toCone _
  | t + 1 => by
    have ih := q_le ν h c Z A hν hν0 hh hc hZ hA step small t
    -- νZ_t ≤ Q_t ≤ Q_0, so ν(cZ_t) ≤ cQ_0 ≤ ν·ν², hence cZ_t ≤ ν²
    have hq : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have h1 := le_trans' K O.toCone _ _ _ hq ih
    have h2 := le_mul_nn K O.toCone c _ _ hc h1
    have h3 := le_trans' K O.toCone _ _ _ h2 small
    have e3 : K.mul c (K.mul ν (Z t)) = K.mul ν (K.mul c (Z t)) := by lib_ring
    rw [e3] at h3
    have hcz := le_of_mul_le K O ν _ _ hν hν0 h3
    -- h c A Z ≤ h ν² A
    have h4 := le_mul_nn K O.toCone (K.mul h (A t)) _ _ (O.nn_mul _ _ hh (hA t)) hcz
    have e4 : K.mul (K.mul h (A t)) (K.mul c (Z t)) = K.mul (K.mul h c) (K.mul (A t) (Z t)) := by lib_ring
    have e5 : K.mul (K.mul h (A t)) (K.mul ν ν) = K.mul (K.mul h (K.mul ν ν)) (A t) := by lib_ring
    rw [e4, e5] at h4
    have h5 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) h4
    exact le_trans' K O.toCone _ _ _ (step t) (le_trans' K O.toCone _ _ _ h5 ih)

theorem z_small (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.mul c (Z t)) (K.mul ν ν) := by
  have ih := q_le K O ν h c Z A hν hν0 hh hc hZ hA step small t
  have hq : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
  have h3 := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c _ _ hc (le_trans' K O.toCone _ _ _ hq ih)) small
  have e3 : K.mul c (K.mul ν (Z t)) = K.mul ν (K.mul c (Z t)) := by lib_ring
  rw [e3] at h3
  exact le_of_mul_le K O ν _ _ hν hν0 h3

end Gen

end EnstGlobal

#print axioms EnstGlobal.le_of_mul_le
#print axioms EnstGlobal.q_le
#print axioms EnstGlobal.z_small

#print axioms: 3 theorems, no axioms.

79.186 Global small-data enstrophy bound, uniform in the truncation

For the Galerkin scheme \((1+h\nu |k_r|^2)a^{t+1}_r=a^t_r+hN_r(a^t)\) with distinct nonzero integer modes: if \(135\iota ^2Q_0\le \nu ^3\) then \(Q_t\le Q_0\) for all \(t\), \(Q_t=\nu Z_t+h\nu ^2A_t\). The constant and the smallness condition do not depend on the number of modes.

ns/EnstGalerkin.lean

/-
  ns/EnstGalerkin.lean — T37 cycle 979 (goal G638, piece e): global small-data enstrophy bound for the Fourier–Galerkin
  scheme of 3D Navier–Stokes with implicit viscosity, uniform in the truncation, over an ordered field.
  Modes: integer kz_p (nonzero, distinct on the mode list l), ring wave vectors k_p = kvec(kz_p), w_p = |k_p|².
  Scheme: (1 + hν w_r) a^{t+1}_r = a^t_r + h N_r(a^t),  N = Galerkin force Nl with the triad indicator
  [kz_p + kz_q + kz_r = 0] and coupling ι. With Z_t = Σ w|a^t|², A_t = Σ w²|a^t|², Q_t = νZ_t + hν²A_t, c = 135ι²:
    enst_global   c·Q_0 ≤ ν³  ⇒  Q_t ≤ Q_0 for every t
  (so Z_t ≤ Q_0/ν for all time; the constant 135 and the smallness condition do not depend on the number of modes).

  Lean 4 core plus ns/ForceUniform, ns/NormCompare, ns/EnstStep, ns/EnstGlobal.
-/
import ns.ForceUniform
import ns.NormCompare
import ns.EnstStep
import ns.EnstGlobal

namespace EnstGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def kv (kz : I → Mode) (p : I) : V3 R := kvec K (kz p)
def wt (kz : I → Mode) (p : I) : R := dot3 K (kv K kz p) (kv K kz p)
def Zs (l : List I) (kz : I → Mode) (a : Nat → I → V3 R) (t : Nat) : R :=
  lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l
def As (l : List I) (kz : I → Mode) (a : Nat → I → V3 R) (t : Nat) : R :=
  lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a t r) (a t r))) l

include mul_inv h10 in
theorem enst_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  refine q_le K O ν h _ (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA ?_ small t
  intro s
  -- one step: νZ' + hν²A' ≤ νZ + hB
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  -- B ≤ ι² H (135 Z),  H ≤ A
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hZn := hZ s
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2

end Gen

end EnstGalerkin

#print axioms EnstGalerkin.enst_global

#print axioms: 1 theorem, no axioms.

79.187 The small-data bound for the Leray-projected scheme

The global enstrophy bound holds for any force with \(\sum |N_t|^2\le \sum |N(a^t)|^2\), in particular for the Leray-projected Galerkin force. Same smallness \(135\iota ^2Q_0\le \nu ^3\), uniform in the truncation.

ns/EnstProj.lean

/-
  ns/EnstProj.lean — T37 cycle 981 (goal G643): the global small-data enstrophy bound for any force N_t whose energy is
  at most that of the Galerkin force (enst_global_gen), in particular for the Leray-projected force
  P_r N_r = N_r − (k_r·N_r) κ_r k_r with κ_r |k_r|² = 1 (enst_proj, via LerayProj3.proj_le). Scheme:
  (1 + hν|k_r|²) a^{t+1}_r = a^t_r + h P_r N_r(a^t); if 135ι²(νZ_0 + hν²A_0) ≤ ν³ then νZ_t + hν²A_t stays below
  its initial value for every t, uniformly in the truncation.

  Lean 4 core plus ns/EnstGalerkin, ns/LerayProj3.
-/
import ns.EnstGalerkin
import ns.LerayProj3

namespace EnstProj
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin LerayProj LerayProj3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem enst_global_gen (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R) (N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  refine q_le K O ν h _ (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA ?_ small t
  intro s
  -- one step: νZ' + hν²A' ≤ νZ + hB
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  -- B ≤ ι² H (135 Z),  H ≤ A
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hZn := hZ s
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb := le_trans' K O.toCone _ _ _ (hN s) hb
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2


include mul_inv h10 in
theorem enst_proj (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (κ : I → R) (hκn : ∀ r, O.nn (κ r)) (hκ : ∀ r, K.mul (dot3 K (kv K kz r) (kv K kz r)) (κ r) = K.one)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r)
      (proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
    (t : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
  enst_global_gen K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 a
    (fun t r => proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))
    (fun _ => lsum_le K O.toCone _ _ (fun r => proj_le K O.toCone (κ r) (kv K kz r) _ (hκn r) (hκ r)) l)
    hs small t

end Gen

end EnstProj

#print axioms EnstProj.enst_global_gen
#print axioms EnstProj.enst_proj

#print axioms: 2 theorems, no axioms.

79.188 Closed-form enstrophy bound for every step size

For the projected scheme: \(135\iota ^2(Z_0+h\nu A_0)\le \nu ^2\) implies \(Z_t\le Z_0+h\nu A_0\) for every \(t\). Uniform in the truncation; as \(h\to 0\) the condition becomes \(135\iota ^2Z_0<\nu ^2\).

ns/EnstBound.lean

/-
  ns/EnstBound.lean — T37 cycle 985 (goal G643, last part): the enstrophy bound in closed form, for every step size.
  For the Leray-projected implicit-viscosity Galerkin scheme (EnstProj.enst_proj), with c = 135ι²:
    c·(Z_0 + hν A_0) ≤ ν²   ⇒   Z_t ≤ Z_0 + hν A_0   for every t,
  uniformly in the truncation; as h → 0 (H² data, A_0 fixed) the condition becomes 135ι² Z_0 < ν² and the bound
  Z_t ≤ Z_0 + o(1) — the discrete form of the classical small-enstrophy global bound in 3D.

  Lean 4 core plus ns/EnstProj, ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstProj

namespace EnstBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin LerayProj EnstProj

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem enst_bound (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a : Nat → I → V3 R)
    (κ : I → R) (hκn : ∀ r, O.nn (κ r)) (hκ : ∀ r, K.mul (dot3 K (kv K kz r) (kv K kz r)) (κ r) = K.one)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r)
      (proj K (κ r) (kv K kz r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)))
    (small : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι))
      (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0)))) (K.mul ν ν))
    (t : Nat) :
    Le K O.toCone (Zs K l kz a t) (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))) := by
  have hQ0 : Q K ν h (Zs K l kz a) (As K l kz a) 0 = K.mul ν (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))) := by
    show K.add (K.mul ν (Zs K l kz a 0)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a 0)) = _
    lib_ring
  have sm : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
      (K.mul ν (K.mul ν ν)) := by
    rw [hQ0]
    have m := le_mul_nn K O.toCone ν _ _ hν small
    have e : K.mul ν (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0))))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul ν (K.add (Zs K l kz a 0) (K.mul (K.mul h ν) (As K l kz a 0)))) := by
      lib_ring
    rw [e] at m; exact m
  have q := enst_proj K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 a κ hκn hκ hs sm t
  rw [hQ0] at q
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q)

end Gen

end EnstBound

#print axioms EnstBound.enst_bound

#print axioms: 1 theorem, no axioms.

79.189 Tail of the lattice sum

For distinct modes with \(R+1\le |k|_\infty \): \(\sum _k|k|_\infty ^{-4}\le 135/(R+1)\), uniformly in the truncation. Empty shells below \(R\), telescoping above, last Abel term \(\le 27/(R+1)\).

ns/LatticeTail.lean

/-
  ns/LatticeTail.lean — T37 cycle 987 (goal G648, piece a): the tail of the lattice sum. Over an ordered field, for a
  duplicate-free list l of modes k ∈ ℤ³ with R + 1 ≤ |k|_∞ ≤ M + 1:
    lattice_tail   Σ_{k∈l} |k|_∞^{−4} ≤ 135 / (R + 1),
  uniformly in M. Same Abel route as LatticeBound: the shells j ≤ R are empty, the shells j > R telescope to
  108·(R+1)^{−1}, and the last Abel term is ≤ 27·(M+1)^{−1} ≤ 27·(R+1)^{−1}. This is the high-frequency half of the
  cutoff argument for the large-data enstrophy step (‖u‖_∞ split at |k|_∞ = R).

  Lean 4 core plus ns/LatticeBound, ns/LatticeSum, ns/AbelCount, ns/EnstGlobal (le_of_mul_le).
-/
import ns.LatticeBound
import ns.EnstGlobal

namespace LatticeTail
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound GoodModesQ

theorem cnt_zero_below (R : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → R + 1 ≤ n) → ∀ j, j ≤ R → cnt L j = 0
  | [], _, _, _ => rfl
  | n :: L, h, j, hj => by
    have hn : j < n := Nat.lt_of_le_of_lt hj (h n (List.Mem.head _))
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = 0
    rw [AbelCount.ble_false hn]
    exact cnt_zero_below R L (fun x hx => h x (List.Mem.tail _ hx)) j hj

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- 1/(n+1) is antitone -/
theorem iv_anti (a b : Nat) (h : a ≤ b) : Le K O.toCone (iv K inv (b + 1)) (iv K inv (a + 1)) := by
  have hA := iv_mul K O inv mul_inv h10 a
  have hB := iv_mul K O inv mul_inv h10 b
  have nA := iv_nn K O inv mul_inv h10 a
  have nB := iv_nn K O inv mul_inv h10 b
  have m := RatModel.cast_mono K O.toCone _ _ (Nat.succ_le_succ h)
  -- (a+1)·iB ≤ (b+1)·iB = 1 = (a+1)·iA
  have m1 := le_mul_nn K O.toCone (iv K inv (b + 1)) _ _ nB m
  rw [K.mul_comm _ (castN K (b + 1)), hB, ← hA, K.mul_comm (iv K inv (b + 1))] at m1
  exact EnstGlobal.le_of_mul_le K O _ _ _ (RatModel.nn_cast K O.toCone (a + 1)) (cast_succ_ne K O h10 a) m1

/-- u_R j = 1/(R+1) for j ≤ R + 1, 1/j beyond -/
def uR (Rc : Nat) (j : Nat) : R := cond (Nat.ble j (Rc + 1)) (iv K inv (Rc + 1)) (iv K inv j)

include mul_inv h10 in
theorem tail_shell (Rc : Nat) (L : List Nat) (h1 : ∀ n, n ∈ L → Rc + 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f4 K inv) j)) M)
      (K.mul (castN K 108) (iv K inv (Rc + 1))) := by
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt L j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (uR K inv Rc j) (K.neg (uR K inv Rc (j + 1))))) := by
    intro j
    cases Nat.lt_or_ge j (Rc + 1) with
    | inl hj =>
      have hj' : j ≤ Rc := Nat.le_of_lt_succ hj
      rw [cnt_zero_below Rc L h1 j hj']
      have e1 : uR K inv Rc j = iv K inv (Rc + 1) := by
        show cond (Nat.ble j (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le (Nat.le_of_lt hj)]; rfl
      have e2 : uR K inv Rc (j + 1) = iv K inv (Rc + 1) := by
        show cond (Nat.ble (j + 1) (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le hj]; rfl
      rw [e1, e2]
      exact ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero (gd K (f4 K inv) j)) K.zero = _; lib_ring⟩
    | inr hj =>
      -- j = i + 1 with i ≥ Rc
      cases j with
      | zero => exact absurd hj (Nat.not_succ_le_zero _)
      | succ i =>
        have e1 : uR K inv Rc (i + 1) = iv K inv (i + 1) := by
          show cond (Nat.ble (i + 1) (Rc + 1)) _ _ = _
          cases Nat.eq_or_lt_of_le hj with
          | inl e => rw [← e, Nat.ble_eq_true_of_le (Nat.le_refl _)]; rfl
          | inr l => rw [AbelCount.ble_false l]; rfl
        have e2 : uR K inv Rc (i + 1 + 1) = iv K inv (i + 1 + 1) := by
          show cond (Nat.ble (i + 1 + 1) (Rc + 1)) _ _ = _
          rw [AbelCount.ble_false (Nat.lt_succ_of_le hj)]; rfl
        rw [e1, e2]
        exact shell_term K O inv mul_inv h10 L i (hc (i + 1))
  have hs := sum_le K O.toCone _ _ term M
  rw [sumN_mul_left, telescope] at hs
  have u0 : uR K inv Rc 0 = iv K inv (Rc + 1) := by
    show cond (Nat.ble 0 (Rc + 1)) _ _ = _; rw [Nat.ble_eq_true_of_le (Nat.zero_le _)]; rfl
  have uMn : O.nn (uR K inv Rc M) := by
    cases hb : Nat.ble M (Rc + 1) with
    | true => show O.nn (cond (Nat.ble M (Rc + 1)) _ _); rw [hb]; exact iv_nn K O inv mul_inv h10 Rc
    | false =>
      cases M with
      | zero => exact absurd (Nat.ble_eq_true_of_le (Nat.zero_le (Rc + 1))) (by rw [hb]; exact Bool.noConfusion)
      | succ M' => show O.nn (cond (Nat.ble (M' + 1) (Rc + 1)) _ _); rw [hb]; exact iv_nn K O inv mul_inv h10 M'
  rw [u0] at hs
  refine le_trans' K O.toCone _ _ _ hs ⟨K.mul (castN K 108) (uR K inv Rc M), O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) uMn, ?_⟩
  lib_ring

include mul_inv h10 in
theorem last_term_iv (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (K.mul (castN K 27) (iv K inv (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA) = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−4} ≤ 135/(R+1) when every mode has R + 1 ≤ |k|_∞ -/
theorem lattice_tail (Rc : Nat) (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → Rc + 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) (hRM : Rc ≤ M) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (K.mul (castN K 135) (iv K inv (Rc + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → Rc + 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have S := tail_shell K O inv mul_inv h10 Rc (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term_iv K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have T2 := le_trans' K O.toCone _ _ _ T
    (le_mul_nn K O.toCone (castN K 27) _ _ (RatModel.nn_cast K O.toCone 27) (iv_anti K O inv mul_inv h10 Rc M hRM))
  have U := le_add' K O.toCone _ _ _ _ S T2
  have e : K.add (K.mul (castN K 108) (iv K inv (Rc + 1))) (K.mul (castN K 27) (iv K inv (Rc + 1)))
      = K.mul (castN K 135) (iv K inv (Rc + 1)) := by
    rw [show castN K 135 = K.add (castN K 108) (castN K 27) from RatModel.cast_add K 108 27]; lib_ring
  rw [e] at U; exact U

end Gen

end LatticeTail

#print axioms LatticeTail.cnt_zero_below
#print axioms LatticeTail.iv_anti
#print axioms LatticeTail.tail_shell
#print axioms LatticeTail.last_term_iv
#print axioms LatticeTail.lattice_tail

#print axioms: 5 theorems, no axioms.

79.190 Weight sums split at a cutoff

For distinct nonzero modes and \(c\ge 0\): \(\sum _p(\text {if }|k_p|_\infty \le R\text { then }c\text { else }|k_p|_\infty ^{-4})\le (2R+1)^3c+135/(R+1)\). Low modes counted by the cube, high modes by the lattice tail.

ns/WeightSplit.lean

/-
  ns/WeightSplit.lean — T37 cycle 989 (goal G648, piece b1): weight sums for the two-scale force bound, over an ordered
  field. Modes kz_p ∈ ℤ³, nonzero, distinct on the list l, |kz_p|_∞ ≤ M + 1; cutoff R ≤ M; n_p = |kz_p|_∞.
    low_count   Σ_p [n_p ≤ R] ≤ (2R+1)³
    high_sum    Σ_p [n_p > R] n_p^{−4} ≤ 135/(R+1)
    s_split     Σ_p (if n_p ≤ R then c else n_p^{−4}) ≤ (2R+1)³·c + 135/(R+1)   for any c ≥ 0
  (S1 uses c ≥ n_p^{−2} on low modes, i.e. c = 1; S2 uses c = T^{−2}.)

  Lean 4 core plus ns/LatticeTail, ns/ForceUniform (lsum_norms, mem_map_inv, pw_map).
-/
import ns.LatticeTail
import ns.ForceUniform

namespace WeightSplit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound LatticeTail ForceUniform GoodModesQ ForceSob

/-- the modes with |k|_∞ > R -/
def fltH (Rc : Nat) : List Mode → List Mode
  | [] => []
  | k :: l => cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l)

theorem mem_fltH (Rc : Nat) (x : Mode) : ∀ (l : List Mode), x ∈ fltH Rc l → x ∈ l ∧ Rc + 1 ≤ nrm x
  | [], h => nomatch h
  | k :: l, h => by
    have h' : x ∈ cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l) := h
    cases hb : Nat.ble (nrm k) Rc with
    | true =>
      rw [hb] at h'
      have r := mem_fltH Rc x l h'
      exact ⟨List.Mem.tail _ r.1, r.2⟩
    | false =>
      rw [hb] at h'
      have hk : Rc + 1 ≤ nrm k := by
        cases Nat.lt_or_ge Rc (nrm k) with
        | inl lt => exact lt
        | inr ge => exact absurd (Nat.ble_eq_true_of_le ge) (by rw [hb]; exact Bool.noConfusion)
      cases h' with
      | head => exact ⟨List.Mem.head _, hk⟩
      | tail _ h'' =>
        have r := mem_fltH Rc x l h''
        exact ⟨List.Mem.tail _ r.1, r.2⟩

theorem fltH_pw (Rc : Nat) : ∀ (l : List Mode), List.Pairwise (fun a b => a ≠ b) l →
    List.Pairwise (fun a b => a ≠ b) (fltH Rc l)
  | [], _ => List.Pairwise.nil
  | k :: l, hp => by
    cases hp with
    | cons hk hl =>
      show List.Pairwise _ (cond (Nat.ble (nrm k) Rc) (fltH Rc l) (k :: fltH Rc l))
      cases Nat.ble (nrm k) Rc with
      | true => exact fltH_pw Rc l hl
      | false => exact List.Pairwise.cons (fun y hy => hk y (mem_fltH Rc y l hy).1) (fltH_pw Rc l hl)

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def lowB (Rc : Nat) (kz : I → Mode) (p : I) : Bool := Nat.ble (nrm (kz p)) Rc

theorem count_low (Rc : Nat) (kz : I → Mode) :
    ∀ (l : List I), lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l = castN K (cnt (norms (l.map kz)) Rc)
  | [] => rfl
  | p :: l => by
    show K.add (cond (Nat.ble (nrm (kz p)) Rc) K.one K.zero) (lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l)
      = castN K (cond (Nat.ble (nrm (kz p)) Rc) (cnt (norms (l.map kz)) Rc + 1) (cnt (norms (l.map kz)) Rc))
    rw [count_low Rc kz l]
    cases Nat.ble (nrm (kz p)) Rc with
    | true => rfl
    | false =>
      show K.add K.zero (castN K (cnt (norms (l.map kz)) Rc)) = castN K (cnt (norms (l.map kz)) Rc)
      lib_ring

theorem high_eq (Rc : Nat) (kz : I → Mode) (g : Nat → R) :
    ∀ (l : List I), lsum K (fun p => cond (lowB Rc kz p) K.zero (g (nrm (kz p)))) l = lsumN K g (norms (fltH Rc (l.map kz)))
  | [] => rfl
  | p :: l => by
    show K.add (cond (Nat.ble (nrm (kz p)) Rc) K.zero (g (nrm (kz p)))) (lsum K (fun p => cond (lowB Rc kz p) K.zero (g (nrm (kz p)))) l)
      = lsumN K g (norms (cond (Nat.ble (nrm (kz p)) Rc) (fltH Rc (l.map kz)) (kz p :: fltH Rc (l.map kz))))
    rw [high_eq Rc kz g l]
    cases Nat.ble (nrm (kz p)) Rc with
    | true =>
      show K.add K.zero (lsumN K g (norms (fltH Rc (l.map kz)))) = lsumN K g (norms (fltH Rc (l.map kz)))
      lib_ring
    | false => rfl

include mul_inv h10 in
/-- Σ_p (if n_p ≤ R then c else n_p^{−4}) ≤ (2R+1)³ c + 135/(R+1) -/
theorem s_split (Rc : Nat) (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (hRM : Rc ≤ M) (c : R) (hc : O.nn c) :
    Le K O.toCone (lsum K (fun p => cond (lowB Rc kz p) c (f4 K inv (nrm (kz p)))) l)
      (K.add (K.mul (castN K ((2 * Rc + 1) * ((2 * Rc + 1) * (2 * Rc + 1)))) c)
        (K.mul (castN K 135) (iv K inv (Rc + 1)))) := by
  have split : lsum K (fun p => cond (lowB Rc kz p) c (f4 K inv (nrm (kz p)))) l
      = K.add (K.mul (lsum K (fun p => cond (lowB Rc kz p) K.one K.zero) l) c)
          (lsum K (fun p => cond (lowB Rc kz p) K.zero (f4 K inv (nrm (kz p)))) l) := by
    rw [K.mul_comm _ c, ← lsum_mul_left, ← lsum_add]
    exact lsum_ext K _ _ (fun p => by
      show cond (lowB Rc kz p) c _ = K.add (K.mul c (cond (lowB Rc kz p) K.one K.zero)) (cond (lowB Rc kz p) K.zero _)
      cases lowB Rc kz p with
      | true => show c = K.add (K.mul c K.one) K.zero; lib_ring
      | false =>
        show f4 K inv (nrm (kz p)) = K.add (K.mul c K.zero) (f4 K inv (nrm (kz p)))
        lib_ring) l
  rw [split, count_low K Rc kz l, high_eq K Rc kz _ l]
  have hlow := RatModel.cast_mono K O.toCone _ _ (cnt_cube (l.map kz) (pw_map kz l hd) Rc)
  have t1 := le_mul_right' K O.toCone hlow hc
  have t2 := lattice_tail K O inv mul_inv h10 Rc (fltH Rc (l.map kz)) (fltH_pw Rc _ (pw_map kz l hd))
    (fun k hk => (mem_fltH Rc k _ hk).2) M
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k l (mem_fltH Rc k _ hk).1
      rw [← e]; exact hM q hq) hRM
  exact le_add' K O.toCone _ _ _ _ t1 t2

end Gen

end WeightSplit

#print axioms WeightSplit.fltH_pw
#print axioms WeightSplit.count_low
#print axioms WeightSplit.high_eq
#print axioms WeightSplit.s_split

#print axioms: 4 theorems, no axioms.

79.191 Two-scale Galerkin force bound

\(\sum _r|N_r(u,v)|^2\le 2\iota ^2[Z(u)C_1Z(v)+A(u)C_2Z(v)]\), \(C_1=(2R+1)^3+135/(R+1)\), \(C_2=(2R+1)^3/T^2+135/(R+1)\), uniformly in the truncation. Split \(u\) at \(|k|_\infty =R\) and apply the weighted bound twice with cast weights (no square roots).

ns/ForceTwoScale.lean

/-
  ns/ForceTwoScale.lean — T37 cycle 993 (goal G648, piece b2): the two-scale Galerkin force bound, over an ordered field.
  Split u at the cutoff |kz|_∞ = R into u_low + u_high (Nl is linear in u, |X+Y|² ≤ 2|X|² + 2|Y|²) and apply
  ForceSob.total_sob twice with cast weights (no square roots):
    low part:  μ = |k|_∞ on low modes, |k|_∞² on high modes   ⇒  H ≤ Z(u),  Σν² ≤ (2R+1)³ + 135/(R+1)
    high part: μ = T on low modes,      |k|_∞² on high modes   ⇒  H ≤ A(u),  Σν² ≤ (2R+1)³/T² + 135/(R+1)
  Result (two_scale):
    Σ_r |N_r(u,v)|² ≤ 2ι²·[Z(u)·C₁·Z(v) + A(u)·C₂·Z(v)],
    C₁ = (2R+1)³ + 135/(R+1),  C₂ = (2R+1)³/T² + 135/(R+1),
  with Z(u) = Σ|k|²|u|², A(u) = Σ|k|⁴|u|², uniformly in the truncation. With T large and R ~ Z/ν² the A-term is
  absorbed by the dissipation (next piece).

  Lean 4 core plus ns/WeightSplit, ns/ForceBilin (Nl_add_left), ns/ForceLip (Nl_congr), ns/NormCompare, ns/EnstGalerkin.
-/
import ns.WeightSplit
import ns.ForceBilin
import ns.ForceLip
import ns.NormCompare
import ns.EnstGalerkin

namespace ForceTwoScale
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  AbelCount LatticeStep LatticeSum ModeCount CubeCount LatticeBound ForceUniform NormCompare WeightSplit
  EnstGalerkin ForceBilin GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def z3 : V3 R := ⟨K.zero, K.zero, K.zero⟩

def uLo (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) : V3 R := cond (lowB Rc kz p) (u p) (z3 K)
def uHi (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) : V3 R := cond (lowB Rc kz p) (z3 K) (u p)

theorem split_u (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    u p = addF K (uLo K Rc kz u) (uHi K Rc kz u) p := by
  show u p = add3 K (cond (lowB Rc kz p) (u p) (z3 K)) (cond (lowB Rc kz p) (z3 K) (u p))
  cases lowB Rc kz p with
  | true =>
    apply ForceBilin.v3_ext
    · show (u p).x = K.add (u p).x K.zero; lib_ring
    · show (u p).y = K.add (u p).y K.zero; lib_ring
    · show (u p).z = K.add (u p).z K.zero; lib_ring
  | false =>
    apply ForceBilin.v3_ext
    · show (u p).x = K.add K.zero (u p).x; lib_ring
    · show (u p).y = K.add K.zero (u p).y; lib_ring
    · show (u p).z = K.add K.zero (u p).z; lib_ring

theorem sq2 (X Y : V3 R) :
    Le K O.toCone (dot3 K (add3 K X Y) (add3 K X Y))
      (K.add (K.mul (K.add K.one K.one) (dot3 K X X)) (K.mul (K.add K.one K.one) (dot3 K Y Y))) :=
  ⟨dot3 K (add3 K X (neg3 K Y)) (add3 K X (neg3 K Y)), dot3_nn K O.toCone _, by
    show K.add (K.add (K.add (K.mul (K.add X.x Y.x) (K.add X.x Y.x)) (K.mul (K.add X.y Y.y) (K.add X.y Y.y)))
        (K.mul (K.add X.z Y.z) (K.add X.z Y.z)))
      (K.add (K.add (K.mul (K.add X.x (K.neg Y.x)) (K.add X.x (K.neg Y.x))) (K.mul (K.add X.y (K.neg Y.y)) (K.add X.y (K.neg Y.y))))
        (K.mul (K.add X.z (K.neg Y.z)) (K.add X.z (K.neg Y.z))))
      = K.add (K.mul (K.add K.one K.one) (K.add (K.add (K.mul X.x X.x) (K.mul X.y X.y)) (K.mul X.z X.z)))
        (K.mul (K.add K.one K.one) (K.add (K.add (K.mul Y.x Y.x) (K.mul Y.y Y.y)) (K.mul Y.z Y.z)))
    lib_ring⟩

def nn (kz : I → Mode) (p : I) : R := castN K (nrm (kz p))
def ivn (kz : I → Mode) (p : I) : R := inv (castN K (nrm (kz p)))

def mu1 (Rc : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (nn K kz p) (K.mul (nn K kz p) (nn K kz p))
def nu1 (Rc : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (ivn K inv kz p) (K.mul (ivn K inv kz p) (ivn K inv kz p))
def mu2 (Rc t : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (castN K (t + 1)) (K.mul (nn K kz p) (nn K kz p))
def nu2 (Rc t : Nat) (kz : I → Mode) (p : I) : R :=
  cond (lowB Rc kz p) (inv (castN K (t + 1))) (K.mul (ivn K inv kz p) (ivn K inv kz p))

include O mul_inv h10 in
theorem nn_ivn (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) : K.mul (nn K kz p) (ivn K inv kz p) = K.one :=
  inv_cast K O inv mul_inv h10 _ (h1 p)

include O mul_inv h10 in
theorem munu1 (Rc : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (mu1 K Rc kz p) (nu1 K inv Rc kz p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  show K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _) = K.one
  cases lowB Rc kz p with
  | true => exact e
  | false =>
    show K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)) = K.one
    calc _ = K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
      _ = K.one := by rw [e]; lib_ring

include O mul_inv h10 in
theorem munu2 (Rc t : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (mu2 K Rc t kz p) (nu2 K inv Rc t kz p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  show K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _) = K.one
  cases lowB Rc kz p with
  | true => exact mul_inv _ (cast_succ_ne K O h10 t)
  | false =>
    show K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)) = K.one
    calc _ = K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
      _ = K.one := by rw [e]; lib_ring

include O mul_inv h10 in
theorem ivn_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    O.nn (ivn K inv kz p) ∧ Le K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have hn : O.nn (nn K kz p) := RatModel.nn_cast K O.toCone _
  have hi : O.nn (ivn K inv kz p) := BaselQ.inv_nn K O inv _ hn e
  refine ⟨hi, ?_⟩
  -- ivn ≤ 1: 1 = n·ivn = ivn + (n − 1)·ivn
  have hle : Le K O.toCone (ivn K inv kz p) K.one := by
    cases hm : nrm (kz p) with
    | zero =>
      have h1p := h1 p
      rw [hm] at h1p
      exact absurd h1p (Nat.not_succ_le_zero 0)
    | succ m =>
      have e' : K.mul (castN K (m + 1)) (ivn K inv kz p) = K.one := by
        have : nn K kz p = castN K (m + 1) := by show castN K (nrm (kz p)) = _; rw [hm]
        rw [← this]; exact e
      refine ⟨K.mul (castN K m) (ivn K inv kz p), O.nn_mul _ _ (RatModel.nn_cast K O.toCone m) hi, ?_⟩
      rw [← e']; show _ = K.mul (K.add K.one (castN K m)) _; lib_ring
  have h2 := le_mul_nn K O.toCone (ivn K inv kz p) _ _ hi hle
  have e1 : K.mul (ivn K inv kz p) K.one = ivn K inv kz p := by lib_ring
  rw [e1] at h2
  exact le_trans' K O.toCone _ _ _ h2 hle

theorem H1p (Rc : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu1 K Rc kz p) (mu1 K Rc kz p)) (dot3 K (uLo K Rc kz u p) (uLo K Rc kz u p)))
      (K.mul (wt K kz p) (dot3 K (u p) (u p))) := by
  have hw : O.nn (K.mul (wt K kz p) (dot3 K (u p) (u p))) := O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)
  show Le K O.toCone (K.mul (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _))
    (dot3 K (cond (lowB Rc kz p) (u p) (z3 K)) (cond (lowB Rc kz p) (u p) (z3 K)))) _
  cases lowB Rc kz p with
  | true => exact le_mul_right' K O.toCone (nrm_le_dot K O.toCone (kz p)) (dot3_nn K O.toCone _)
  | false =>
    refine ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), hw, ?_⟩
    show K.add (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
      (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem H2p (Rc t : Nat) (kz : I → Mode) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu2 K Rc t kz p) (mu2 K Rc t kz p)) (dot3 K (uHi K Rc kz u p) (uHi K Rc kz u p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) := by
  have hw : O.nn (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) :=
    O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)
  show Le K O.toCone (K.mul (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _))
    (dot3 K (cond (lowB Rc kz p) (z3 K) (u p)) (cond (lowB Rc kz p) (z3 K) (u p)))) _
  cases lowB Rc kz p with
  | true =>
    refine ⟨K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p)), hw, ?_⟩
    show K.add (K.mul (K.mul (castN K (t + 1)) (castN K (t + 1)))
      (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring
  | false => exact le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)

include O mul_inv h10 in
theorem S1p (Rc : Nat) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (nu1 K inv Rc kz p) (nu1 K inv Rc kz p)) (cond (lowB Rc kz p) K.one (f4 K inv (nrm (kz p)))) := by
  have hb := (ivn_le K O inv mul_inv h10 kz h1 p).2
  show Le K O.toCone (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _)) (cond (lowB Rc kz p) _ _)
  cases lowB Rc kz p with
  | true => exact hb
  | false => exact le_refl' K O.toCone _

theorem S2p (Rc t : Nat) (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (nu2 K inv Rc t kz p) (nu2 K inv Rc t kz p))
      (cond (lowB Rc kz p) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))) (f4 K inv (nrm (kz p)))) := by
  show Le K O.toCone (K.mul (cond (lowB Rc kz p) _ _) (cond (lowB Rc kz p) _ _)) (cond (lowB Rc kz p) _ _)
  cases lowB Rc kz p with
  | true => exact le_refl' K O.toCone _
  | false => exact le_refl' K O.toCone _

def cube (Rc : Nat) : Nat := (2 * Rc + 1) * ((2 * Rc + 1) * (2 * Rc + 1))

/-- monotone product step: x ≤ x', y ≤ y', x ≥ 0, c ≥ 0 ⇒ c·(x·(y·z)) ≤ c·(x'·(y'·z)) for z ≥ 0 -/
theorem mono3 (c x x' y y' z : R) (hc : O.nn c) (hx : O.nn x) (hz : O.nn z) (hy'z : O.nn (K.mul y' z))
    (h1 : Le K O.toCone x x') (h2 : Le K O.toCone y y') :
    Le K O.toCone (K.mul c (K.mul x (K.mul y z))) (K.mul c (K.mul x' (K.mul y' z))) := by
  have a := le_mul_right' K O.toCone h2 hz
  have b := le_mul_nn K O.toCone x _ _ hx a
  have d := le_mul_right' K O.toCone h1 hy'z
  exact le_mul_nn K O.toCone c _ _ hc (le_trans' K O.toCone _ _ _ b d)

include mul_inv h10 in
theorem two_scale (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t : Nat) (hRM : Rc ≤ M)
    (ι : R) (u v : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
          (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l)
          (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))))
              (K.mul (castN K 135) (iv K inv (Rc + 1))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))))) := by
  have hsplit : ∀ r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r
      = add3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v r) := by
    intro r
    rw [ForceLip.Nl_congr K l _ ι _ u (addF K (uLo K Rc kz u) (uHi K Rc kz u)) v v r (split_u K Rc kz u) (fun _ => rfl),
      Nl_add_left]
  rw [lsum_ext K _ _ (fun r => by rw [hsplit r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) (uLo K Rc kz u) v (mu1 K Rc kz) (nu1 K inv Rc kz)
      (delta01 K kz) (munu1 K O inv mul_inv h10 Rc kz h1)
      (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
    have hH := lsum_le K O.toCone _ _ (fun p => H1p K O Rc kz u p) l
    have hS := le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => S1p K O inv mul_inv h10 Rc kz h1 p) l)
      (s_split K O inv mul_inv h10 Rc l kz hd M hM hRM K.one (RatModel.nn_one' K O.toCone))
    have nH : O.nn (lsum K (fun p => K.mul (K.mul (mu1 K Rc kz p) (mu1 K Rc kz p)) (dot3 K (uLo K Rc kz u p) (uLo K Rc kz u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZu : O.nn (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nC : O.nn (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
      O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
    have m := mono3 K O (K.mul ι ι) _ _ _ _ _ (O.nn_sq ι) nH nZv (O.nn_mul _ _ nC nZv) hH hS
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ T m)
  · have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) (uHi K Rc kz u) v (mu2 K Rc t kz) (nu2 K inv Rc t kz)
      (delta01 K kz) (munu2 K O inv mul_inv h10 Rc t kz h1)
      (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
    have hH := lsum_le K O.toCone _ _ (fun p => H2p K O Rc t kz u p) l
    have hS := le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => S2p K O inv Rc t kz p) l)
      (s_split K O inv mul_inv h10 Rc l kz hd M hM hRM _ (O.nn_sq _))
    have nH : O.nn (lsum K (fun p => K.mul (K.mul (mu2 K Rc t kz p) (mu2 K Rc t kz p)) (dot3 K (uHi K Rc kz u p) (uHi K Rc kz u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nAu : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l) :=
      lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
    have nZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have nC : O.nn (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t + 1))) (inv (castN K (t + 1)))))
        (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
      O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (iv_nn K O inv mul_inv h10 Rc))
    have m := mono3 K O (K.mul ι ι) _ _ _ _ _ (O.nn_sq ι) nH nZv (O.nn_mul _ _ nC nZv) hH hS
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ T m)

end Gen

end ForceTwoScale

#print axioms ForceTwoScale.split_u
#print axioms ForceTwoScale.sq2
#print axioms ForceTwoScale.munu1
#print axioms ForceTwoScale.munu2
#print axioms ForceTwoScale.ivn_le
#print axioms ForceTwoScale.H1p
#print axioms ForceTwoScale.H2p
#print axioms ForceTwoScale.S1p
#print axioms ForceTwoScale.S2p
#print axioms ForceTwoScale.two_scale

#print axioms: 11 theorems, no axioms.

79.192 Large-data local enstrophy bound, abstract form

If \(\nu Z_{t+1}+h\nu ^2A_{t+1}\le \nu Z_t+h(c_1Z_t^2+c_2A_tZ_t)\), \(c_2Z_b\le \nu ^2\) and \(Q_0+n\,hc_1Z_b^2\le \nu Z_b\), then \(Q_t\le Q_0+t\,hc_1Z_b^2\) and \(Z_t\le Z_b\) for \(t\le n\). No smallness of the data; the time \(nh\) depends only on \(Q_0\) and the constants.

ns/EnstLocal.lean

/-
  ns/EnstLocal.lean — T37 cycle 995 (goal G648, piece c): the large-data local-in-time enstrophy bound, abstract form,
  over an ordered ring without zero divisors. Sequences Z_t, A_t ≥ 0, constants ν > 0, h, c₁, c₂ ≥ 0, a level Zb ≥ 0:
    step     νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(c₁ Z_t Z_t + c₂ A_t Z_t)     (EnstStep + ForceTwoScale)
    choice   c₂ Zb ≤ ν²                                                   (cutoff R, T chosen from Zb)
    budget   Q_0 + n·h·c₁·Zb² ≤ ν Zb,          Q_t = νZ_t + hν²A_t
  ⇒ for every t ≤ n:  Q_t ≤ Q_0 + t·h·c₁·Zb²  and  Z_t ≤ Zb.
  No smallness of the data: with Zb = 2Q_0/ν the number of steps n·h ~ ν²/(c₁ Q_0) is a time depending only on Q_0
  (and on R through c₁), not on the truncation.

  Lean 4 core plus ns/EnstGlobal (Q, le_of_mul_le), ns/RatModel (cast_mono), ns/ForceSob (le_mul_right').
-/
import ns.EnstGlobal
import ns.RatModel
import ns.ForceSob

namespace EnstLocal
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_mono (x y : R) (hx : O.nn x) (hy : O.nn y) (h : Le K O.toCone x y) :
    Le K O.toCone (K.mul x x) (K.mul y y) := by
  have a := le_mul_nn K O.toCone x _ _ hx h
  have b := le_mul_nn K O.toCone y _ _ hy h
  rw [K.mul_comm y x] at b
  exact le_trans' K O.toCone _ _ _ a b

theorem local_bound (ν h c1 c2 Zb : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hc1 : O.nn c1) (hc2 : O.nn c2) (hZb : O.nn Zb) (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
    (choice : Le K O.toCone (K.mul c2 Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb))))) (K.mul ν Zb)) :
    ∀ t, t ≤ n → Le K O.toCone (Q K ν h Z A t) (K.add (Q K ν h Z A 0) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (Q K ν h Z A 0) K.zero = K.add (Q K ν h Z A 0) (K.mul K.zero _); lib_ring⟩
  | t + 1, ht => by
    have ih := local_bound ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice n budget t (Nat.le_of_succ_le ht)
    have hD : O.nn (K.mul h (K.mul c1 (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
    -- Q_t ≤ Q_0 + nD ≤ νZb, so Z_t ≤ Zb
    have tn : Le K O.toCone (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))
        (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb)))) :=
      ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n (Nat.le_of_succ_le ht)) hD
    have q1 := le_trans' K O.toCone _ _ _ ih (le_trans' K O.toCone _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have zt : Le K O.toCone (Z t) Zb := le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
    -- c₁Z_t² ≤ c₁Zb²,  c₂A_tZ_t ≤ ν²A_t
    have s1 := le_mul_nn K O.toCone c1 _ _ hc1 (sq_mono K O _ _ (hZ t) hZb zt)
    have c2z := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c2 _ _ hc2 zt) choice
    have s2 := le_mul_nn K O.toCone (A t) _ _ (hA t) c2z
    have e2 : K.mul (A t) (K.mul c2 (Z t)) = K.mul c2 (K.mul (A t) (Z t)) := by lib_ring
    rw [e2] at s2
    have s3 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ s1 s2)
    have s4 := le_trans' K O.toCone _ _ _ (step t) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) s3)
    -- νZ_t + h(c₁Zb² + A_tν²) = Q_t + D ≤ Q_0 + tD + D
    have e4 : K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul Zb Zb)) (K.mul (A t) (K.mul ν ν))))
        = K.add (Q K ν h Z A t) (K.mul h (K.mul c1 (K.mul Zb Zb))) := by
      show _ = K.add (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))) _
      lib_ring
    rw [e4] at s4
    have s5 := le_trans' K O.toCone _ _ _ s4 (le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h (K.mul c1 (K.mul Zb Zb)))))
    refine le_trans' K O.toCone _ _ _ s5 ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (Q K ν h Z A 0) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      (K.mul h (K.mul c1 (K.mul Zb Zb)))) K.zero
      = K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K t)) (K.mul h (K.mul c1 (K.mul Zb Zb))))
    lib_ring

end Gen

end EnstLocal

#print axioms EnstLocal.sq_mono
#print axioms EnstLocal.local_bound

#print axioms: 2 theorems, no axioms.

79.193 Large-data local enstrophy bound for the Galerkin scheme

For the implicit-viscosity Galerkin scheme with any force of energy at most the Galerkin force: if \(c_2Z_b\le \nu ^2\) and \(Q_0+n\,hc_1Z_b^2\le \nu Z_b\) then \(Q_t\le Q_0+t\,hc_1Z_b^2\) for \(t\le n\). \(c_1,c_2\) depend on the cutoffs \(R,T\) only, not on the number of modes.

ns/EnstLocalGalerkin.lean

/-
  ns/EnstLocalGalerkin.lean — T37 cycle 997 (goal G648, piece d): N-uniform local-in-time enstrophy bound for the
  implicit-viscosity Fourier–Galerkin scheme of 3D Navier–Stokes with arbitrary (large) data, over an ordered field.
  Scheme (1 + hν|k_r|²) a^{t+1}_r = a^t_r + h N^t_r with Σ_r|N^t_r|² ≤ Σ_r|Nl_r(a^t, a^t)|² (e.g. the Leray-projected
  Galerkin force). For a cutoff R ≤ M, T = t₀ + 1, a level Zb ≥ 0 with c₂Zb ≤ ν² and Q_0 + n·h·c₁Zb² ≤ νZb, where
    c₁ = 2ι²((2R+1)³ + 135/(R+1)),   c₂ = 2ι²((2R+1)³/T² + 135/(R+1)),
  every t ≤ n has Q_t ≤ Q_0 + t·h·c₁Zb² and Z_t ≤ Zb (local_galerkin). The constants do not depend on the number of
  modes: given Q_0, pick Zb = 2Q_0/ν, then R and T so that c₂Zb ≤ ν², and the time n·h ≤ Q_0/(c₁Zb²).

  Lean 4 core plus ns/ForceTwoScale, ns/EnstLocal, ns/EnstStep, ns/EnstGalerkin.
-/
import ns.ForceTwoScale
import ns.EnstLocal

namespace EnstLocalGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def C1 (Rc : Nat) : R := K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))
def C2 (Rc t0 : Nat) : R := K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (Rc + 1)))
def cc (ι C : R) : R := K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) C

include mul_inv h10 in
theorem local_galerkin (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (K.mul (castN K t) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  refine local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA ?_ choice n budget t ht
  intro s
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
  have ts := two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (a s)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) ts)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Zs K l kz a s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C1 K inv Rc))
        (K.mul (Zs K l kz a s) (Zs K l kz a s)))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))
  lib_ring

end Gen

end EnstLocalGalerkin

#print axioms EnstLocalGalerkin.local_galerkin

#print axioms: 1 theorem, no axioms.

79.194 The cutoffs of the local bound exist

Over an Archimedean ordered field: for every level \(Z_b\ge 0\) there are \(R,t_0\) with \(c_2(R,t_0)Z_b\le \nu ^2\). So the large-data local bound applies with constants fixed by \(Z_b\) alone.

ns/ChooseCutoff.lean

/-
  ns/ChooseCutoff.lean — T37 cycle 1005 (goal G655): the cutoffs of the large-data local bound exist. Over an ordered
  field with inverses that is Archimedean (every element is below some castN n; ℚ is), for every level Zb ≥ 0,
  coupling ι and viscosity ν ≠ 0 there are R, t₀ ∈ ℕ with
    c₂(R, t₀) · Zb ≤ ν²,   c₂ = 2ι²((2R+1)³/(t₀+1)² + 135/(R+1))
  (choose_cutoff), so EnstLocalGalerkin.local_galerkin applies to arbitrary data with constants fixed by Zb alone.
  Step lemma (small_inv): if X ≤ castN(n+1) then X·(n+1)^{−1} ≤ 1.

  Lean 4 core plus ns/EnstLocalGalerkin (C2, cc), ns/LatticeSum (iv), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocalGalerkin

namespace ChooseCutoff
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal LatticeSum EnstLocalGalerkin ForceTwoScale
  GoodModesQ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem small_inv (X : R) (n : Nat) (h : Le K O.toCone X (castN K (n + 1))) :
    Le K O.toCone (K.mul X (iv K inv (n + 1))) K.one := by
  have m := ForceSob.le_mul_right' K O.toCone h (iv_nn K O inv mul_inv h10 n)
  rw [iv_mul K O inv mul_inv h10 n] at m
  exact m

include O in
theorem two_ne (h1 : K.one ≠ K.zero) : K.add K.one K.one ≠ K.zero := by
  intro e
  have n1 : O.nn K.one := RatModel.nn_one' K O.toCone
  have l1 : Le K O.toCone K.one K.zero := ⟨K.one, n1, e⟩
  have l2 : Le K O.toCone K.zero K.one := ⟨K.one, n1, by lib_ring⟩
  exact h1 (O.antisym _ _ l1 l2)

/-- if c·(Y·ν⁻²)·x ≤ 1-type bound: from X·iv ≤ 1 with X = W·iν·iν, get W·iv ≤ ν² -/
theorem scale_back (W ν iν v : R) (hν : K.mul ν iν = K.one) (hW : Le K O.toCone (K.mul (K.mul W (K.mul iν iν)) v) K.one) :
    Le K O.toCone (K.mul W v) (K.mul ν ν) := by
  have m := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) hW
  have e : K.mul (K.mul ν ν) (K.mul (K.mul W (K.mul iν iν)) v) = K.mul (K.mul W v) (K.mul (K.mul ν iν) (K.mul ν iν)) := by
    lib_ring
  rw [e, hν] at m
  have e2 : K.mul (K.mul W v) (K.mul K.one K.one) = K.mul W v := by lib_ring
  have e3 : K.mul (K.mul ν ν) K.one = K.mul ν ν := by lib_ring
  rw [e2, e3] at m
  exact m

include mul_inv h10 in
theorem choose_cutoff (arch : ∀ x : R, ∃ n : Nat, Le K O.toCone x (castN K n))
    (ι ν Zb : R) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb) :
    ∃ Rc t0 : Nat, Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) := by
  have hν := mul_inv ν hν0
  generalize inv ν = iν at hν
  -- 4ι²·135·Zb·ν⁻² ≤ R + 1
  have ⟨n1, hn1⟩ := arch (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν))
  -- n1 ≤ n1 + 1 as casts
  have hR : Le K O.toCone (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (castN K (n1 + 1)) :=
    le_trans' K O.toCone _ _ _ hn1 (RatModel.cast_mono K O.toCone n1 (n1 + 1) (Nat.le_succ n1))
  have bR := scale_back K O _ ν iν _ hν (by
    have := small_inv K O inv mul_inv h10 _ n1 hR
    have e : K.mul (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (iv K inv (n1 + 1))
        = K.mul (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (K.mul iν iν)) (iv K inv (n1 + 1)) := rfl
    exact this)
  -- 4ι²·cube(R)·Zb·ν⁻² ≤ t₀ + 1
  have ⟨n2, hn2⟩ := arch (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) (K.mul iν iν))
  have hT : Le K O.toCone (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) (K.mul iν iν)) (castN K (n2 + 1)) :=
    le_trans' K O.toCone _ _ _ hn2 (RatModel.cast_mono K O.toCone n2 (n2 + 1) (Nat.le_succ n2))
  have bT := scale_back K O _ ν iν _ hν (small_inv K O inv mul_inv h10 _ n2 hT)
  refine ⟨n1, n2, ?_⟩
  -- iT² ≤ iT since iT ≤ 1
  have iTn := iv_nn K O inv mul_inv h10 n2
  have iT1 : Le K O.toCone (iv K inv (n2 + 1)) K.one := by
    have e := iv_mul K O inv mul_inv h10 n2
    refine ⟨K.mul (castN K n2) (iv K inv (n2 + 1)), O.nn_mul _ _ (RatModel.nn_cast K O.toCone n2) iTn, ?_⟩
    rw [← e]; show _ = K.mul (K.add K.one (castN K n2)) _; lib_ring
  have iTT := le_mul_nn K O.toCone (iv K inv (n2 + 1)) _ _ iTn iT1
  have e1 : K.mul (iv K inv (n2 + 1)) K.one = iv K inv (n2 + 1) := by lib_ring
  rw [e1] at iTT
  -- the T-term: 2ι²·cube·iT²·Zb ≤ 2ι²·cube·iT·Zb ≤ ν²/2
  have nW : O.nn (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 4) (O.nn_sq ι)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hZb)
  have tT := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ nW iTT) bT
  -- sum: 2·(c₂ Zb) ≤ 2 ν²  ⇒  c₂ Zb ≤ ν²
  have s := le_add' K O.toCone _ _ _ _ tT bR
  apply le_of_mul_le K O (K.add K.one K.one) _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
    (two_ne K O h10)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι))
        (K.add (K.mul (castN K (cube n1)) (K.mul (inv (castN K (n2 + 1))) (inv (castN K (n2 + 1)))))
          (K.mul (castN K 135) (iv K inv (n1 + 1))))) Zb)) K.zero
      = K.add (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K (cube n1)) Zb))
          (K.mul (iv K inv (n2 + 1)) (iv K inv (n2 + 1))))
        (K.mul (K.mul (K.mul (castN K 4) (K.mul ι ι)) (K.mul (castN K 135) Zb)) (iv K inv (n1 + 1)))
    have c4 : castN K 4 = K.add (K.add K.one K.one) (K.add K.one K.one) := by
      show K.add K.one (K.add K.one (K.add K.one (K.add K.one K.zero))) = _; lib_ring
    rw [c4]; show _ = K.add (K.mul (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul ι ι))
        (K.mul (castN K (cube n1)) Zb)) (K.mul (inv (castN K (n2 + 1))) (inv (castN K (n2 + 1))))) _
    lib_ring
  · lib_ring

end Gen

end ChooseCutoff

#print axioms ChooseCutoff.small_inv
#print axioms ChooseCutoff.two_ne
#print axioms ChooseCutoff.scale_back
#print axioms ChooseCutoff.choose_cutoff

#print axioms: 4 theorems, no axioms.

79.195 Local time in closed form

With \(\nu Z_b=2Q_0\), \(c_2Z_b\le \nu ^2\) and \(n\,hc_1Z_b^2\le Q_0\): \(Q_t\le 2Q_0\) and \(Z_t\le Z_b\) for all \(t\le n\). The time \(nh\le \nu ^2/(4c_1Q_0)\) depends on \(Q_0\) and the cutoffs only.

ns/EnstLocalTime.lean

/-
  ns/EnstLocalTime.lean — T37 cycle 1027 (goal G675): the large-data local bound in closed form. With Zb chosen so
  that ν·Zb = 2Q_0 and cutoffs with c₂Zb ≤ ν² (ChooseCutoff), every step count n with n·h·c₁·Zb² ≤ Q_0 gives, for
  all t ≤ n,
    Q_t ≤ 2 Q_0     and     Z_t ≤ Zb = 2Q_0/ν,
  i.e. the enstrophy at most doubles (relative to Q_0/ν) on the time interval n·h ≤ Q_0/(c₁ Zb²) = ν²/(4 c₁ Q_0),
  which depends only on Q_0, ν and the cutoffs — not on the truncation (local_time).

  Lean 4 core plus ns/EnstLocalGalerkin, ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocalGalerkin

namespace EnstLocalTime
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem local_time (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) t)
        (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
      ∧ Le K O.toCone (Zs K l kz a t) Zb := by
  have budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb) := by
    rw [hZ0]; exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hn
  have q := local_galerkin K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN hs choice n
    budget t ht
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := by
    have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_mul _ _ hh (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
      (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))) (O.nn_sq Zb))
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n ht) hD
  have q2 := le_trans' K O.toCone _ _ _ q (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) (le_trans' K O.toCone _ _ _ tn hn))
  refine ⟨q2, ?_⟩
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  rw [← hZ0] at q2
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q2)

end Gen

end EnstLocalTime

#print axioms EnstLocalTime.local_time

#print axioms: 1 theorem, no axioms.

79.196 The step for differences

The implicit-viscosity step is linear: the difference of two solutions satisfies it with the force difference. Prepares the continuous-dependence estimate in the enstrophy norm.

ns/EnstDiff.lean

/-
  ns/EnstDiff.lean — T37 cycle 1029 (goal G671, piece a): the implicit-viscosity step is linear, so the difference of
  two solutions obeys the same step with the force difference:
    step_sub   Step(a', a, N_a), Step(b', b, N_b)  ⇒  Step(a' − b', a − b, N_a − N_b)
  (componentwise (1 + hνw)(a' − b') = (a − b) + h(N_a − N_b)). Then EnstStep.enst_step bounds the enstrophy of the
  difference by the force difference, which ForceBilin.Nl_diff splits as N(δ, a) + N(b, δ).

  Lean 4 core plus ns/EnstStep, ns/FourierGalerkin (add3, neg3).
-/
import ns.EnstStep

namespace EnstDiff
open DetChain NSChain LibRing FourierGalerkin EnstStep

section Gen
variable {R : Type} (K : CRing R)

def sub3 (X Y : V3 R) : V3 R := add3 K X (neg3 K Y)

theorem stepc_sub (h ν w a' a n b' b m : R) (ha : StepC K h ν w a' a n) (hb : StepC K h ν w b' b m) :
    StepC K h ν w (K.add a' (K.neg b')) (K.add a (K.neg b)) (K.add n (K.neg m)) := by
  unfold StepC at *
  have e : K.mul (K.add K.one (K.mul (K.mul h ν) w)) (K.add a' (K.neg b'))
      = K.add (K.mul (K.add K.one (K.mul (K.mul h ν) w)) a') (K.neg (K.mul (K.add K.one (K.mul (K.mul h ν) w)) b')) := by
    lib_ring
  rw [e, ha, hb]; lib_ring

theorem step_sub (h ν w : R) (a' a Na b' b Nb : V3 R) (ha : Step K h ν w a' a Na) (hb : Step K h ν w b' b Nb) :
    Step K h ν w (sub3 K a' b') (sub3 K a b) (sub3 K Na Nb) :=
  ⟨stepc_sub K h ν w _ _ _ _ _ _ ha.1 hb.1, stepc_sub K h ν w _ _ _ _ _ _ ha.2.1 hb.2.1,
   stepc_sub K h ν w _ _ _ _ _ _ ha.2.2 hb.2.2⟩

end Gen

end EnstDiff

#print axioms EnstDiff.stepc_sub
#print axioms EnstDiff.step_sub

#print axioms: 2 theorems, no axioms.

79.197 The force difference

With \(d=a-b\): \(\sum _r|N_r(a,a)-N_r(b,b)|^2\le 2\cdot 2\iota ^2[Z(d)C_1Z(a)+A(d)C_2Z(a)]+2\iota ^2A(b)\cdot 135\,Z(d)\), uniformly in the truncation. \(N(a,a)-N(b,b)=N(d,a)+N(b,d)\); two-scale bound for the first, uniform bound for the second.

ns/ForceDiff.lean

/-
  ns/ForceDiff.lean — T37 cycle 1031 (goal G671, piece b): the force difference in the enstrophy setting, over an
  ordered field, uniformly in the truncation. With d = a − b (componentwise), N(a,a) − N(b,b) = N(d, a) + N(b, d), and
    force_diff   Σ_r |N_r(a,a) − N_r(b,b)|²
                   ≤ 2·(2ι²[Z(d)C₁Z(a) + A(d)C₂Z(a)]) + 2·(ι²·A(b)·(135·Z(d)))
  (ForceTwoScale.two_scale for N(d, a), ForceUniform.force_uniform with NormCompare.nrm4_le for N(b, d)).

  Lean 4 core plus ns/ForceTwoScale, ns/EnstDiff.
-/
import ns.ForceTwoScale
import ns.EnstDiff

namespace ForceDiff
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def dF (a b : I → V3 R) (p : I) : V3 R := sub3 K (a p) (b p)

theorem a_split (a b : I → V3 R) (p : I) : a p = addF K b (dF K a b) p := by
  show a p = add3 K (b p) (add3 K (a p) (neg3 K (b p)))
  apply v3_ext
  · show (a p).x = K.add (b p).x (K.add (a p).x (K.neg (b p).x)); lib_ring
  · show (a p).y = K.add (b p).y (K.add (a p).y (K.neg (b p).y)); lib_ring
  · show (a p).z = K.add (b p).z (K.add (a p).z (K.neg (b p).z)); lib_ring

theorem diff_eq (l : List I) (δ : I → I → I → R) (ι : R) (k a b : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K l δ ι k b b r)
      = add3 K (Nl K l δ ι k (dF K a b) a r) (Nl K l δ ι k b (dF K a b) r) := by
  rw [ForceLip.Nl_congr K l δ ι k a (addF K b (dF K a b)) a (addF K b (dF K a b)) r (a_split K a b) (a_split K a b),
    Nl_diff]
  rw [ForceLip.Nl_congr K l δ ι k (dF K a b) (dF K a b) (addF K b (dF K a b)) a r (fun _ => rfl)
    (fun p => (a_split K a b p).symm)]
  generalize Nl K l δ ι k b b r = A; generalize Nl K l δ ι k (dF K a b) a r = B
  generalize Nl K l δ ι k b (dF K a b) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x; lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y; lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z; lib_ring

include mul_inv h10 in
theorem force_diff (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι : R) (a b : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K a b p) (dF K a b p))) l)
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (b p) (b p))) l)
          (K.mul (castN K 135) (lsum K (fun q => K.mul (wt K kz q) (dot3 K (dF K a b q) (dF K a b q))) l)))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [diff_eq K l _ ι _ a b r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (dF K a b) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b (dF K a b) r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (dF K a b) a)
  · have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) b (dF K a b)
    have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (b p))) l
    have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (dF K a b q) (dF K a b q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) nZ)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ fu m2)

end Gen

end ForceDiff

#print axioms ForceDiff.a_split
#print axioms ForceDiff.diff_eq
#print axioms ForceDiff.force_diff

#print axioms: 3 theorems, no axioms.

79.198 Product-form discrete Gronwall

If \(\nu Z_{t+1}+h\nu ^2A_{t+1}\le \nu Z_t+h(\alpha _tZ_t+\beta _tA_t)\) with \(\beta _t\le \nu ^2\), then \(\nu ^TQ_T\le \prod _{t<T}(\nu +h\alpha _t)\,Q_0\). Applied to the difference of two Galerkin solutions.

ns/EnstGronwall.lean

/-
  ns/EnstGronwall.lean — T37 cycle 1033 (goal G671, piece c): product-form discrete Gronwall for the difference
  enstrophy, over an ordered ring. With Q_t = νZ_t + hν²A_t, A_t, α_t, h, ν ≥ 0 and β_t ≤ ν²:
    step:  νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(α_t Z_t + β_t A_t)
    ⇒      ν^T · Q_T ≤ Π_{t<T} (ν + hα_t) · Q_0          (diff_gronwall)
  (one step: νQ_{t+1} ≤ νQ_t + hα_t·νZ_t ≤ (ν + hα_t)Q_t). For two solutions of the Galerkin scheme, ForceDiff gives
  α_t ≈ ι²(C₁Z(a_t) + A(b_t)) and β_t ≈ ι²C₂Z(a_t), so the difference is controlled by its initial value with a
  factor independent of the truncation.

  Lean 4 core plus ns/EnstGlobal (Q), ns/ForceSob (le_mul_right'), ns/RatModel (nn_one'), lib/RingTac.
-/
import ns.EnstGlobal
import ns.ForceSob
import ns.RatModel

namespace EnstGronwall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def prodN (f : Nat → R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul (f n) (prodN f n)

def pwr (x : R) : Nat → R
  | 0 => K.one
  | n + 1 => K.mul x (pwr x n)

theorem prod_nn (f : Nat → R) (hf : ∀ n, O.nn (f n)) : ∀ n, O.nn (prodN K f n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ (hf n) (prod_nn f hf n)

theorem pwr_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pwr K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ hx (pwr_nn x hx n)

theorem one_step (ν h : R) (Z A α β : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hA : ∀ t, O.nn (A t))
    (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t)))))) (t : Nat) :
    Le K O.toCone (K.mul ν (Q K ν h Z A (t + 1))) (K.mul (K.add ν (K.mul h (α t))) (Q K ν h Z A t)) := by
  -- β A ≤ ν² A
  have b1 := ForceSob.le_mul_right' K O.toCone (hβ t) (hA t)
  have b2 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (α t) (Z t))) b1)
  have s1 := le_trans' K O.toCone _ _ _ (step t) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) b2)
  -- times ν
  have s2 := le_mul_nn K O.toCone ν _ _ hν s1
  refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)),
    O.nn_mul _ _ (O.nn_mul _ _ hh (hα t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t)), ?_⟩
  show K.add (K.mul ν (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (K.mul ν ν) (A t))))))
      (K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    = K.mul (K.add ν (K.mul h (α t))) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
  lib_ring

theorem diff_gronwall (ν h : R) (Z A α β : Nat → R) (hν : O.nn ν) (hh : O.nn h)
    (hA : ∀ t, O.nn (A t)) (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t)))))) :
    ∀ T, Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) T) (Q K ν h Z A 0))
  | 0 => by
    show Le K O.toCone (K.mul K.one _) (K.mul K.one _)
    exact le_refl' K O.toCone _
  | T + 1 => by
    have ih := diff_gronwall ν h Z A α β hν hh hA hα hβ step T
    have os := one_step K O ν h Z A α β hν hh hA hα hβ step T
    -- ν^{T+1} Q_{T+1} = ν^T (ν Q_{T+1}) ≤ ν^T (ν + hα_T) Q_T = (ν + hα_T)(ν^T Q_T) ≤ (ν + hα_T) P_T Q_0
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have m2 := le_mul_nn K O.toCone (K.add ν (K.mul h (α T))) _ _ (O.nn_add _ _ hν (O.nn_mul _ _ hh (hα T))) ih
    have e1 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    have e2 : K.mul (pwr K ν T) (K.mul (K.add ν (K.mul h (α T))) (Q K ν h Z A T))
        = K.mul (K.add ν (K.mul h (α T))) (K.mul (pwr K ν T) (Q K ν h Z A T)) := by lib_ring
    have e3 : K.mul (K.add ν (K.mul h (α T))) (K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) T) (Q K ν h Z A 0))
        = K.mul (prodN K (fun t => K.add ν (K.mul h (α t))) (T + 1)) (Q K ν h Z A 0) := by
      show _ = K.mul (K.mul (K.add ν (K.mul h (α T))) (prodN K (fun t => K.add ν (K.mul h (α t))) T)) _; lib_ring
    rw [e1]; rw [e2] at m1; rw [e3] at m2
    exact le_trans' K O.toCone _ _ _ m1 m2

end Gen

end EnstGronwall

#print axioms EnstGronwall.one_step
#print axioms EnstGronwall.diff_gronwall

#print axioms: 2 theorems, no axioms.

79.199 Continuous dependence in the enstrophy norm, uniform in the truncation

For two solutions of the implicit-viscosity Galerkin scheme with \(d=a-b\): \(\nu ^TQ_d(T)\le \prod _{t<T}(\nu +h\alpha _t)\,Q_d(0)\), \(\alpha _t\sim \iota ^2(C_1Z(a_t)+135A(b_t))\), if \(\beta _t\sim \iota ^2C_2Z(a_t)\le \nu ^2\). The factors do not depend on the number of modes.

ns/EnstLipschitz.lean

/-
  ns/EnstLipschitz.lean — T37 cycle 1035 (goal G671, piece d — done): continuous dependence of the implicit-viscosity
  Fourier–Galerkin scheme in the enstrophy norm, with factors independent of the truncation, over an ordered field.
  Two solutions a, b (forces N^a, N^b with Σ|N^a_t − N^b_t|² ≤ Σ|Nl(a_t,a_t) − Nl(b_t,b_t)|², e.g. Leray-projected),
  d_t = a_t − b_t, Qd_t = νZ(d_t) + hν²A(d_t). With
    α_t = 2·2ι²C₁·Z(a_t) + 2ι²·135·A(b_t),   β_t = 2·2ι²C₂·Z(a_t)  and  β_t ≤ ν² (cutoffs, Z(a_t) ≤ Zb),
  for every T:
    lipschitz   ν^T · Qd_T ≤ Π_{t<T}(ν + hα_t) · Qd_0.
  (EnstDiff.step_sub + EnstStep.enst_step + ForceDiff.force_diff + EnstGronwall.diff_gronwall.)

  Lean 4 core plus ns/ForceDiff, ns/EnstGronwall, ns/EnstGalerkin.
-/
import ns.ForceDiff
import ns.EnstGronwall

namespace EnstLipschitz
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def C1 (Rc : Nat) : R := K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))
def C2 (Rc t0 : Nat) : R := K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (Rc + 1)))
def two : R := K.add K.one K.one

def dS (a b : Nat → I → V3 R) (t : Nat) (p : I) : V3 R := dF K (a t) (b t) p

include mul_inv h10 in
theorem lipschitz (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a b Na Nb : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Na t r) (Nb t r)) (sub3 K (Na t r) (Nb t r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l))
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Na t r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hβ : ∀ t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a t))))
      (K.mul ν ν))
    (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T) (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) T))
      (K.mul (prodN K (fun t => K.add ν (K.mul h
          (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a t))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b t))))))) T)
        (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) 0)) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (u : Nat → I → V3 R) t, O.nn (Zs K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hAn : ∀ (u : Nat → I → V3 R) t, O.nn (As K l kz u t) := fun u t =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nC1 : O.nn (C1 K inv Rc) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  refine diff_gronwall K O ν h _ _ _ _ hν hh (hAn (dS K a b)) (fun t => O.nn_add _ _
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_sq ι)) (O.nn_mul _ _ nC1 (hZn a t))))
    (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn b t))))) hβ ?_ T
  intro s
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (dS K a b (s + 1)) (dS K a b s)
    (fun r => sub3 K (Na s r) (Nb s r)) (fun r => step_sub K h ν (wt K kz r) _ _ _ _ _ _ (ha s r) (hb s r))
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (b s)
  have hb2 := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) fd)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add
      (K.mul (two K) (K.add
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (Zs K l kz (dS K a b) s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz (dS K a b) s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))
      (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz b s) (K.mul (castN K 135) (Zs K l kz (dS K a b) s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add
      (K.mul (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
          (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s))))) (Zs K l kz (dS K a b) s))
      (K.mul (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s))))
        (As K l kz (dS K a b) s))))
  lib_ring

end Gen

end EnstLipschitz

#print axioms EnstLipschitz.lipschitz

#print axioms: 1 theorem, no axioms.

79.200 The truncation error of the Galerkin force

For a sub-truncation \(l_N=l_M\cap N\): \(N_{l_M}(a,a)-N_{l_N}(a,a)=N_{l_M}(a_H,a)+N_{l_M}(a_N,a_H)\), \(a_N,a_H\) the parts on and off \(N\). The restriction of an \(l_M\)-solution solves the \(l_N\)-scheme up to this error.

ns/TruncError.lean

/-
  ns/TruncError.lean — T37 cycle 1039 (goal G683, piece a): the truncation error of the Galerkin force. For a mode list
  lM, a sub-truncation given by a Boolean predicate inN (lN = lM.filter inN) and fields u, v:
    restr_eq   Nl over lN of (u, v) = Nl over lM of (u|_N, v|_N)
    trunc_err  Nl_lM(a, a) − Nl_lN(a, a) = Nl_lM(a|_H, a) + Nl_lM(a|_N, a|_H)      (H = complement of N)
  So the restriction of a solution of the lM-scheme to the modes of lN solves the lN-scheme up to the error
  E = N(a_H, a) + N(a_N, a_H), whose energy ForceTwoScale / ForceUniform bound by the high-mode part a_H.

  Lean 4 core plus ns/ForceDiff (sub3, a_split), ns/ForceBilin (Nl_add_left/right, v3_ext), ns/ForceLip (Nl_congr).
-/
import ns.ForceDiff

namespace TruncError
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBilin ForceTwoScale EnstDiff ForceDiff

section Gen
variable {R : Type} (K : CRing R) {I : Type}

def restr (inN : I → Bool) (u : I → V3 R) (p : I) : V3 R := cond (inN p) (u p) (z3 K)
def hiF (inN : I → Bool) (u : I → V3 R) (p : I) : V3 R := cond (inN p) (z3 K) (u p)

theorem u_split (inN : I → Bool) (u : I → V3 R) (p : I) : u p = addF K (restr K inN u) (hiF K inN u) p := by
  show u p = add3 K (cond (inN p) (u p) (z3 K)) (cond (inN p) (z3 K) (u p))
  cases inN p with
  | true =>
    apply v3_ext
    · show (u p).x = K.add (u p).x K.zero; lib_ring
    · show (u p).y = K.add (u p).y K.zero; lib_ring
    · show (u p).z = K.add (u p).z K.zero; lib_ring
  | false =>
    apply v3_ext
    · show (u p).x = K.add K.zero (u p).x; lib_ring
    · show (u p).y = K.add K.zero (u p).y; lib_ring
    · show (u p).z = K.add K.zero (u p).z; lib_ring

/-- a sum over l of terms vanishing off the predicate equals the sum over the filtered list -/
theorem lsum_filter (inN : I → Bool) (f : I → R) (hf : ∀ p, inN p = false → f p = K.zero) :
    ∀ (l : List I), lsum K f l = lsum K f (l.filter inN)
  | [] => rfl
  | p :: l => by
    have ih := lsum_filter inN f hf l
    show K.add (f p) (lsum K f l) = lsum K f (List.filter inN (p :: l))
    rw [show List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl]
    cases hp : inN p with
    | true => show K.add (f p) (lsum K f l) = K.add (f p) (lsum K f (List.filter inN l)); rw [ih]
    | false => show K.add (f p) (lsum K f l) = lsum K f (List.filter inN l); rw [hf p hp, ih]; lib_ring

theorem lsum_ext_on {f g : I → R} : ∀ (l : List I), (∀ i, i ∈ l → f i = g i) → lsum K f l = lsum K g l
  | [], _ => rfl
  | p :: l, h => by
    show K.add (f p) (lsum K f l) = K.add (g p) (lsum K g l)
    rw [h p (List.Mem.head _), lsum_ext_on l (fun i hi => h i (List.Mem.tail _ hi))]

theorem mem_filter_true (inN : I → Bool) (x : I) : ∀ (l : List I), x ∈ l.filter inN → inN x = true
  | [], h => nomatch h
  | p :: l, h => by
    have h' : x ∈ cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
        show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
        cases inN p <;> rfl
      rw [← e]; exact h
    cases hp : inN p with
    | true =>
      rw [hp] at h'
      cases h' with
      | head => exact hp
      | tail _ h'' => exact mem_filter_true inN x l h''
    | false => rw [hp] at h'; exact mem_filter_true inN x l h'

/-- one component: Σ_p Σ_q coef(u) v_q over the filtered list = the same over lM for the restricted fields -/
theorem comp_restr (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I)
    (sel : V3 R → R) (hsel : sel (z3 K) = K.zero) :
    lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k u p q r) (sel (v q))) (l.filter inN)) (l.filter inN)
      = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (restr K inN v q))) l) l := by
  -- inner sums
  have inner : ∀ p, lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (restr K inN v q))) l
      = lsum K (fun q => K.mul (coef K δ ι k (restr K inN u) p q r) (sel (v q))) (l.filter inN) := by
    intro p
    rw [lsum_filter K inN _ (fun q hq => by
      show K.mul _ (sel (cond (inN q) (v q) (z3 K))) = K.zero
      rw [hq]; show K.mul _ (sel (z3 K)) = K.zero; rw [hsel]; lib_ring) l]
    exact lsum_ext_on K _ (fun q hq => by
      show K.mul _ (sel (cond (inN q) (v q) (z3 K))) = _
      rw [mem_filter_true inN q l hq]; rfl)
  rw [lsum_ext K _ _ inner l]
  rw [lsum_filter K inN _ (fun p hp => by
    have hc : ∀ q, coef K δ ι k (restr K inN u) p q r = K.zero := by
      intro q
      show K.mul (δ p q r) (K.mul ι (dot3 K (cond (inN p) (u p) (z3 K)) (k q))) = K.zero
      rw [hp]; show K.mul (δ p q r) (K.mul ι (K.add (K.add (K.mul K.zero (k q).x) (K.mul K.zero (k q).y)) (K.mul K.zero (k q).z))) = K.zero
      lib_ring
    rw [lsum_ext K _ (fun _ => K.zero) (fun q => by rw [hc q]; lib_ring)]
    exact lsum_zero K _) l]
  symm
  exact lsum_ext_on K _ (fun p hp => lsum_ext_on K _ (fun q _ => by
    have hpN := mem_filter_true inN p l hp
    show K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (cond (inN p) (u p) (z3 K)) (k q)))) (sel (v q))
      = K.mul (K.mul (δ p q r) (K.mul ι (dot3 K (u p) (k q)))) (sel (v q))
    rw [hpN]; rfl))

theorem restr_eq (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (r : I) :
    Nl K (l.filter inN) δ ι k u v r = Nl K l δ ι k (restr K inN u) (restr K inN v) r := by
  apply v3_ext
  · exact comp_restr K l inN δ ι k u v r (fun w => w.x) rfl
  · exact comp_restr K l inN δ ι k u v r (fun w => w.y) rfl
  · exact comp_restr K l inN δ ι k u v r (fun w => w.z) rfl

theorem trunc_err (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k a : I → V3 R) (r : I) :
    sub3 K (Nl K l δ ι k a a r) (Nl K (l.filter inN) δ ι k a a r)
      = add3 K (Nl K l δ ι k (hiF K inN a) a r) (Nl K l δ ι k (restr K inN a) (hiF K inN a) r) := by
  rw [restr_eq K l inN δ ι k a a r]
  rw [ForceLip.Nl_congr K l δ ι k a (addF K (restr K inN a) (hiF K inN a)) a (addF K (restr K inN a) (hiF K inN a)) r
    (u_split K inN a) (u_split K inN a), Nl_diff]
  rw [ForceLip.Nl_congr K l δ ι k (hiF K inN a) (hiF K inN a) (addF K (restr K inN a) (hiF K inN a)) a r (fun _ => rfl)
    (fun p => (u_split K inN a p).symm)]
  generalize Nl K l δ ι k (restr K inN a) (restr K inN a) r = A
  generalize Nl K l δ ι k (hiF K inN a) a r = B
  generalize Nl K l δ ι k (restr K inN a) (hiF K inN a) r = C
  apply v3_ext
  · show K.add (K.add (K.add A.x B.x) C.x) (K.neg A.x) = K.add B.x C.x; lib_ring
  · show K.add (K.add (K.add A.y B.y) C.y) (K.neg A.y) = K.add B.y C.y; lib_ring
  · show K.add (K.add (K.add A.z B.z) C.z) (K.neg A.z) = K.add B.z C.z; lib_ring

end Gen

end TruncError

#print axioms TruncError.u_split
#print axioms TruncError.lsum_filter
#print axioms TruncError.comp_restr
#print axioms TruncError.restr_eq
#print axioms TruncError.trunc_err

#print axioms: 7 theorems, no axioms.

79.201 Energy of the truncation error

For the sub-truncation \(|k|_\infty \le R\): the error energy is at most \(2\,\mathrm {TS}(a_H,a)+2\iota ^2A(a)\cdot 135\,Z(a_H)\), and \((R+1)^2Z(a_H)\le A(a_H)\). Small for large \(R\) once the internal cutoffs make \(C_2\) small.

ns/TruncBound.lean

/-
  ns/TruncBound.lean — T37 cycle 1041 (goal G683, piece b): the energy of the truncation error, uniformly in both
  truncations, over an ordered field. Sub-truncation N = {|kz|_∞ ≤ R} of a mode list l (lowB R), a_N, a_H the parts:
    err_bound   Σ_r |N_l(a,a)_r − N_{l∩N}(a,a)_r|² ≤ 2·TS(a_H, a) + 2·ι²A(a_N)·135·Z(a_H)
  (TS = the two-scale bound of ForceTwoScale with free internal cutoffs R', t₀), and the high-mode estimates
    high_Z     (R+1)²·Z(a_H) ≤ A(a_H),      high_A  A(a_H) ≤ A(a),      low_A  A(a_N) ≤ A(a).
  So the error is ≲ ι²A(a)Z(a)·(C₂ + C₁/(R+1)²): small for large R once C₂ is made small by R', t₀.

  Lean 4 core plus ns/TruncError, ns/EnstLocal (sq_mono), ns/ForceTwoScale, ns/WeightSplit (lowB), ns/NormCompare.
-/
import ns.TruncError
import ns.EnstLocal

namespace TruncBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff ForceDiff TruncError
  WeightSplit LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem part_le (kz : I → Mode) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul (wt K kz p) (dot3 K (cond sel (a p) (z3 K)) (cond sel (a p) (z3 K))))
      (K.mul (wt K kz p) (dot3 K (a p) (a p))) := by
  cases sel with
  | true => exact le_refl' K O.toCone _
  | false =>
    refine ⟨K.mul (wt K kz p) (dot3 K (a p) (a p)), O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul (wt K kz p) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem high_Z (l : List I) (kz : I → Mode) (Rc : Nat) (a : I → V3 R) :
    Le K O.toCone (K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1)))
        (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l))
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l) := by
  rw [← lsum_mul_left]
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  show Le K O.toCone (K.mul _ (K.mul (wt K kz p) (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p)))))
    (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (cond (lowB Rc kz p) (z3 K) (a p)) (cond (lowB Rc kz p) (z3 K) (a p))))
  cases hb : lowB Rc kz p with
  | true =>
    refine ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.mul _ (K.mul (wt K kz p) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero)))) K.zero
      = K.mul (K.mul (wt K kz p) (wt K kz p)) (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))
    lib_ring
  | false =>
    -- R + 1 ≤ nrm, so (R+1)² ≤ nrm² ≤ w
    have hge : Rc + 1 ≤ nrm (kz p) := by
      cases Nat.lt_or_ge Rc (nrm (kz p)) with
      | inl lt => exact lt
      | inr ge => exact absurd (Nat.ble_eq_true_of_le ge) (by
          show ¬ Nat.ble (nrm (kz p)) Rc = true; rw [show Nat.ble (nrm (kz p)) Rc = lowB Rc kz p from rfl, hb]; exact Bool.noConfusion)
    have m := RatModel.cast_mono K O.toCone _ _ hge
    have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) m
    have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
    have m2 := ForceSob.le_mul_right' K O.toCone w1 (O.nn_mul _ _ (dot3_nn K O.toCone (kv K kz p)) (dot3_nn K O.toCone (a p)))
    have e2 : K.mul (dot3 K (kvec K (kz p)) (kvec K (kz p))) (K.mul (dot3 K (kv K kz p) (kv K kz p)) (dot3 K (a p) (a p)))
        = K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p)) := by
      show K.mul (wt K kz p) (K.mul (wt K kz p) (dot3 K (a p) (a p))) = _; lib_ring
    rw [e2] at m2
    exact m2

theorem part_leW (W : R) (hW : O.nn W) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul W (dot3 K (cond sel (a p) (z3 K)) (cond sel (a p) (z3 K)))) (K.mul W (dot3 K (a p) (a p))) := by
  cases sel with
  | true => exact le_refl' K O.toCone _
  | false =>
    refine ⟨K.mul W (dot3 K (a p) (a p)), O.nn_mul _ _ hW (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul W (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

theorem partH_leW (W : R) (hW : O.nn W) (a : I → V3 R) (p : I) (sel : Bool) :
    Le K O.toCone (K.mul W (dot3 K (cond sel (z3 K) (a p)) (cond sel (z3 K) (a p)))) (K.mul W (dot3 K (a p) (a p))) := by
  cases sel with
  | false => exact le_refl' K O.toCone _
  | true =>
    refine ⟨K.mul W (dot3 K (a p) (a p)), O.nn_mul _ _ hW (dot3_nn K O.toCone _), ?_⟩
    show K.add (K.mul W (K.add (K.add (K.mul K.zero K.zero) (K.mul K.zero K.zero)) (K.mul K.zero K.zero))) _ = _
    lib_ring

include mul_inv h10 in
theorem err_bound (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι : R) (a : I → V3 R) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
          (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) a a r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
          (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.add
        (K.mul (K.add K.one K.one) (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l)
            (K.mul (K.add (K.mul (castN K (cube R')) K.one) (K.mul (castN K 135) (iv K inv (R' + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) a p) (hiF K (lowB Rc kz) a p))) l)
            (K.mul (K.add (K.mul (castN K (cube R')) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (R' + 1))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l)
          (K.mul (castN K 135) (lsum K (fun q => K.mul (wt K kz q) (dot3 K (hiF K (lowB Rc kz) a q) (hiF K (lowB Rc kz) a q))) l)))))) := by
  rw [lsum_ext K _ _ (fun r => by rw [trunc_err K l (lowB Rc kz) _ ι _ a r]) l]
  have s1 := lsum_le K O.toCone _ _ (fun r => sq2 K O
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (hiF K (lowB Rc kz) a) a r)
    (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) a) (hiF K (lowB Rc kz) a) r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at s1
  refine le_trans' K O.toCone _ _ _ s1 (le_add' K O.toCone _ _ _ _ ?_ ?_)
  · exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (two_scale K O inv mul_inv h10 l kz hd h1 M hM R' t0 hRM ι (hiF K (lowB Rc kz) a) a)
  · have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (restr K (lowB Rc kz) a) (hiF K (lowB Rc kz) a)
    have hH := lsum_le K O.toCone _ _ (fun p => le_trans' K O.toCone _ _ _
      (le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone (restr K (lowB Rc kz) a p)))
      (part_leW K O _ (O.nn_sq _) a p (lowB Rc kz p))) l
    have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (hiF K (lowB Rc kz) a q) (hiF K (lowB Rc kz) a q))) l) :=
      lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) nZ)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    exact le_mul_nn K O.toCone _ _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
      (le_trans' K O.toCone _ _ _ fu m2)

end Gen

end TruncBound

#print axioms TruncBound.part_le
#print axioms TruncBound.high_Z
#print axioms TruncBound.part_leW
#print axioms TruncBound.partH_leW
#print axioms TruncBound.err_bound

#print axioms: 5 theorems, no axioms.

79.202 Discrete Gronwall with a source

If \(\nu Z_{t+1}+h\nu ^2A_{t+1}\le \nu Z_t+h(\alpha _tZ_t+\beta _tA_t)+hS_t\), \(\beta _t\le \nu ^2\), \(\alpha _t\le \alpha \), \(S_t\le S\), then \(\nu ^TQ_T\le (\nu +h\alpha )^T(Q_0+ThS)\). For the truncation comparison the source is the truncation-error energy and \(Q_0=0\).

ns/GronwallSource.lean

/-
  ns/GronwallSource.lean — T37 cycle 1043 (goal G684, piece a): discrete Gronwall with a source, constant
  coefficients, over an ordered ring. With Q_t = νZ_t + hν²A_t, A_t ≥ 0, β_t ≤ ν², 0 ≤ α_t ≤ α, 0 ≤ S_t ≤ S:
    step:  νZ_{t+1} + hν²A_{t+1} ≤ νZ_t + h(α_t Z_t + β_t A_t) + h S_t
    ⇒      ν^T · Q_T ≤ (ν + hα)^T · (Q_0 + T·h·S)                          (gronwall_src)
  Used for the restriction of a large-truncation solution against the small-truncation solution, where S is the
  truncation error energy (TruncBound) and Q_0 = 0.

  Lean 4 core plus ns/EnstGronwall (pwr, one-step pattern), ns/EnstGlobal (Q).
-/
import ns.EnstGronwall

namespace GronwallSource
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem pwr_mono (x y : R) (hx : O.nn x) (hxy : Le K O.toCone x y) : ∀ n, Le K O.toCone (pwr K x n) (pwr K y n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => by
    have ih := pwr_mono x y hx hxy n
    have hy : O.nn y := by
      cases hxy with
      | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1
    have m1 := ForceSob.le_mul_right' K O.toCone hxy (pwr_nn K O x hx n)
    have m2 := le_mul_nn K O.toCone y _ _ hy ih
    exact le_trans' K O.toCone _ _ _ m1 m2

theorem gronwall_src (ν h α S : R) (Z A αs β Ss : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hS : O.nn S)
    (hA : ∀ t, O.nn (A t)) (hαs : ∀ t, O.nn (αs t)) (hαle : ∀ t, Le K O.toCone (αs t) α)
    (hSs : ∀ t, O.nn (Ss t)) (hSle : ∀ t, Le K O.toCone (Ss t) S) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (hZ : ∀ t, O.nn (Z t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (αs t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (Ss t)))) :
    ∀ T, Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T) (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one (K.add _ (K.mul K.zero _)); lib_ring⟩
  | T + 1 => by
    have ih := gronwall_src ν h α S Z A αs β Ss hν hh hα hS hA hαs hαle hSs hSle hβ hZ step T
    have hQ : O.nn (Q K ν h Z A T) := O.nn_add _ _ (O.nn_mul _ _ hν (hZ T)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T))
    -- one step: νQ_{T+1} ≤ (ν + hα)Q_T + hνS
    have b1 := ForceSob.le_mul_right' K O.toCone (hβ T) (hA T)
    have a1 := ForceSob.le_mul_right' K O.toCone (hαle T) (hZ T)
    have c1 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ a1 b1)
    have s1 := le_trans' K O.toCone _ _ _ (step T) (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z T))) c1)
      (le_mul_nn K O.toCone h _ _ hh (hSle T)))
    have s2 := le_mul_nn K O.toCone ν _ _ hν s1
    have os : Le K O.toCone (K.mul ν (Q K ν h Z A (T + 1)))
        (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S))) := by
      refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)),
        O.nn_mul _ _ (O.nn_mul _ _ hh hα) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T)), ?_⟩
      show K.add (K.mul ν (K.add (K.add (K.mul ν (Z T)) (K.mul h (K.add (K.mul α (Z T)) (K.mul (K.mul ν ν) (A T))))) (K.mul h S)))
          (K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.add (K.mul ν (Z T)) (K.mul (K.mul h (K.mul ν ν)) (A T)))) (K.mul ν (K.mul h S))
      lib_ring
    -- ν^{T+1}Q_{T+1} ≤ (ν+hα)·ν^T Q_T + ν^{T+1} hS ≤ (ν+hα)^{T+1}(Q_0 + T hνS) + (ν+hα)^{T+1} hνS
    have hνα : O.nn (K.add ν (K.mul h α)) := O.nn_add _ _ hν (O.nn_mul _ _ hh hα)
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have pm := pwr_mono K O ν (K.add ν (K.mul h α)) hν ⟨K.mul h α, O.nn_mul _ _ hh hα, rfl⟩ (T + 1)
    have hhS : O.nn (K.mul h S) := O.nn_mul _ _ hh hS
    have src := ForceSob.le_mul_right' K O.toCone pm hhS
    have main := le_mul_nn K O.toCone (K.add ν (K.mul h α)) _ _ hνα ih
    have e0 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    rw [e0]
    refine le_trans' K O.toCone _ _ _ m1 ?_
    have e1 : K.mul (pwr K ν T) (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K ν T) (Q K ν h Z A T))) (K.mul (pwr K ν (T + 1)) (K.mul h S)) := by
      show _ = K.add _ (K.mul (K.mul ν (pwr K ν T)) _); lib_ring
    rw [e1]
    refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ main src) ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S)))))
        (K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T)) (K.mul h S))) K.zero
      = K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T))
        (K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K T)) (K.mul h S)))
    lib_ring

end Gen

end GronwallSource

#print axioms GronwallSource.pwr_mono
#print axioms GronwallSource.gronwall_src

#print axioms: 2 theorems, no axioms.

79.203 The restricted solution solves the small scheme up to the error

On the modes of \(l_N\): \((1+h\nu w_r)a'_r=a_r+h(N_{l_N}(a,a)_r+E_r)\) for \(a=A|_N\), \(E=N_l(A,A)-N_{l_N}(A,A)\). Uses \(N_{l_N}(A|_N,A|_N)=N_{l_N}(A,A)\).

ns/RestrStep.lean

/-
  ns/RestrStep.lean — T37 cycle 1045 (goal G684, piece b1): the restriction of a large-truncation solution solves the
  small-truncation scheme up to the truncation error. For a solution A of the implicit-viscosity scheme on the mode list
  l with force N_l(A, A), and the sub-truncation l_N = l.filter inN, the restricted field a = A|_N satisfies, at every
  mode r of l_N,
    restr_step   (1 + hνw_r) a'_r = a_r + h (N_{l_N}(a, a)_r + E_r),    E = N_l(A, A) − N_{l_N}(A, A),
  using N_{l_N}(a, a) = N_{l_N}(A, A) (TruncError.restr_eq twice, restriction is idempotent).

  Lean 4 core plus ns/TruncError, ns/EnstStep (Step).
-/
import ns.TruncError

namespace RestrStep
open DetChain NSChain LibRing FourierGalerkin GalerkinForce ForceBilin ForceTwoScale EnstDiff EnstStep TruncError

section Gen
variable {R : Type} (K : CRing R) {I : Type}

theorem restr_idem (inN : I → Bool) (u : I → V3 R) (p : I) : restr K inN (restr K inN u) p = restr K inN u p := by
  show cond (inN p) (cond (inN p) (u p) (z3 K)) (z3 K) = cond (inN p) (u p) (z3 K)
  cases inN p <;> rfl

theorem force_restr (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι : R) (k A : I → V3 R) (r : I) :
    Nl K (l.filter inN) δ ι k (restr K inN A) (restr K inN A) r = Nl K (l.filter inN) δ ι k A A r := by
  rw [restr_eq K l inN δ ι k (restr K inN A) (restr K inN A) r, restr_eq K l inN δ ι k A A r]
  exact ForceLip.Nl_congr K l δ ι k _ _ _ _ r (restr_idem K inN A) (restr_idem K inN A)

theorem restr_step (l : List I) (inN : I → Bool) (δ : I → I → I → R) (ι h ν : R) (w : I → R) (k : I → V3 R)
    (A : Nat → I → V3 R) (hA : ∀ t r, Step K h ν (w r) (A (t + 1) r) (A t r) (Nl K l δ ι k (A t) (A t) r))
    (t : Nat) (r : I) (hr : inN r = true) :
    Step K h ν (w r) (restr K inN (A (t + 1)) r) (restr K inN (A t) r)
      (add3 K (Nl K (l.filter inN) δ ι k (restr K inN (A t)) (restr K inN (A t)) r)
        (sub3 K (Nl K l δ ι k (A t) (A t) r) (Nl K (l.filter inN) δ ι k (A t) (A t) r))) := by
  rw [force_restr K l inN δ ι k (A t) r]
  have e1 : restr K inN (A (t + 1)) r = A (t + 1) r := by show cond (inN r) _ _ = _; rw [hr]; rfl
  have e0 : restr K inN (A t) r = A t r := by show cond (inN r) _ _ = _; rw [hr]; rfl
  rw [e1, e0]
  have hs := hA t r
  generalize Nl K (l.filter inN) δ ι k (A t) (A t) r = Q at hs ⊢
  generalize Nl K l δ ι k (A t) (A t) r = P at hs ⊢
  have ex : K.add Q.x (K.add P.x (K.neg Q.x)) = P.x := by lib_ring
  have ey : K.add Q.y (K.add P.y (K.neg Q.y)) = P.y := by lib_ring
  have ez : K.add Q.z (K.add P.z (K.neg Q.z)) = P.z := by lib_ring
  refine ⟨?_, ?_, ?_⟩
  · show StepC K h ν (w r) _ _ (K.add Q.x (K.add P.x (K.neg Q.x))); rw [ex]; exact hs.1
  · show StepC K h ν (w r) _ _ (K.add Q.y (K.add P.y (K.neg Q.y))); rw [ey]; exact hs.2.1
  · show StepC K h ν (w r) _ _ (K.add Q.z (K.add P.z (K.neg Q.z))); rw [ez]; exact hs.2.2

end Gen

end RestrStep

#print axioms RestrStep.restr_idem
#print axioms RestrStep.force_restr
#print axioms RestrStep.restr_step

#print axioms: 3 theorems, no axioms.

79.204 Cauchy estimate in the truncation

For a solution \(A\) on \(l\) and \(B\) on \(l_N=l\cap \{|k|_\infty \le R\}\), \(d=A|_N-B\): \(\nu ^TQ_d(T)\le (\nu +h\alpha )^T(Q_d(0)+ThS)\), \(S\) the truncation-error energy. Given the coefficient bounds \(\alpha _t\le \alpha \), \(\beta _t\le \nu ^2\), \(2E_t\le S\); all constants independent of the truncations.

ns/TruncCauchy.lean

/-
  ns/TruncCauchy.lean — T37 cycle 1047 (goal G684, piece b2): comparison of two Galerkin truncations. A solves the
  implicit-viscosity scheme on the mode list l (force N_l), B on the sub-truncation l_N = l.filter (|k|_∞ ≤ R).
  With d = A|_N − B on l_N, the enstrophy step of d has the forcing (N_{l_N}(a,a) − N_{l_N}(B,B)) + E,
  E = N_l(A,A) − N_{l_N}(A,A), so (ForceDiff, TruncBound, GronwallSource), given the coefficient bounds α_t ≤ α,
  β_t ≤ ν² and the error bound S_t ≤ S,
    trunc_cauchy   ν^T · Q_d(T) ≤ (ν + hα)^T · (Q_d(0) + T·h·S),
  with Q_d = νZ(d) + hν²A(d) on l_N. S is the truncation-error energy, small for large R (TruncBound); Q_d(0) = 0
  when B starts from A|_N. This is the Cauchy estimate in the truncation for the discrete-time Galerkin scheme.

  Lean 4 core plus ns/RestrStep, ns/TruncBound, ns/ForceDiff, ns/GronwallSource.
-/
import ns.RestrStep
import ns.TruncBound
import ns.GronwallSource

namespace TruncCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem lsum_le_on {f g : I → R} : ∀ (l : List I), (∀ i, i ∈ l → Le K P (f i) (g i)) → Le K P (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K P _
  | i :: l, h => le_add' K P _ _ _ _ (h i (List.Mem.head _)) (lsum_le_on l (fun j hj => h j (List.Mem.tail _ hj)))

/-- enst_step with the step relation required only on the list -/
theorem enst_step_on (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, r ∈ l → Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l))) := by
  have t := lsum_le_on K P _ (fun r hr => mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r hr))
  rw [lsum_add, lsum_add] at t
  have e1 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e2 : lsum K (fun r => K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))) l
      = K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a' r) (a' r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e3 : lsum K (fun r => K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) l
      = K.mul ν (lsum K (fun r => K.mul (w r) (dot3 K (a r) (a r))) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have e4 : lsum K (fun r => K.mul h (dot3 K (N r) (N r))) l = K.mul h (lsum K (fun r => dot3 K (N r) (N r)) l) :=
    lsum_mul_left K h _ l
  rw [e1, e2, e3, e4] at t
  exact t

theorem mem_of_mem_filter (inN : I → Bool) (x : I) : ∀ (l : List I), x ∈ l.filter inN → x ∈ l
  | [], h => nomatch h
  | p :: l, h => by
    have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl
    rw [e] at h
    cases hp : inN p with
    | true =>
      rw [hp] at h
      cases h with
      | head => exact List.Mem.head _
      | tail _ h' => exact List.Mem.tail _ (mem_of_mem_filter inN x l h')
    | false => rw [hp] at h; exact List.Mem.tail _ (mem_of_mem_filter inN x l h)

theorem pw_filter {β : Type} (kz : I → β) (inN : I → Bool) : ∀ (l : List I),
    List.Pairwise (fun a b => kz a ≠ kz b) l → List.Pairwise (fun a b => kz a ≠ kz b) (l.filter inN)
  | [], _ => List.Pairwise.nil
  | p :: l, hp => by
    cases hp with
    | cons hx hl =>
      have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
        show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
        cases inN p <;> rfl
      rw [e]
      cases inN p with
      | true => exact List.Pairwise.cons (fun y hy => hx y (mem_of_mem_filter inN y l hy)) (pw_filter kz inN l hl)
      | false => exact pw_filter kz inN l hl

/-- the sum over the filtered list of nonnegative terms is at most the sum over the list -/
theorem lsum_filter_le (inN : I → Bool) (f : I → R) (hf : ∀ i, P.nn (f i)) :
    ∀ (l : List I), Le K P (lsum K f (l.filter inN)) (lsum K f l)
  | [] => le_refl' K P _
  | p :: l => by
    have ih := lsum_filter_le inN f hf l
    have e : List.filter inN (p :: l) = cond (inN p) (p :: List.filter inN l) (List.filter inN l) := by
      show (match inN p with | true => p :: List.filter inN l | false => List.filter inN l) = _
      cases inN p <;> rfl
    rw [e]
    cases inN p with
    | true => exact le_add' K P _ _ _ _ (le_refl' K P (f p)) ih
    | false =>
      show Le K P (lsum K f (List.filter inN l)) (K.add (f p) (lsum K f l))
      cases ih with
      | intro d hd => exact ⟨K.add d (f p), P.nn_add _ _ hd.1 (hf p), by rw [← hd.2]; lib_ring⟩

end Gen

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def two' : R := K.add K.one K.one
def Zl (kz : I → Mode) (L : List I) (u : I → V3 R) : R := lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) L
def Al (kz : I → Mode) (L : List I) (u : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) L
def C1' (R' : Nat) : R := K.add (K.mul (castN K (cube R')) K.one) (K.mul (castN K 135) (iv K inv (R' + 1)))
def C2' (R' t0 : Nat) : R := K.add (K.mul (castN K (cube R')) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
  (K.mul (castN K 135) (iv K inv (R' + 1)))

/-- error energy bound (TruncBound.err_bound right-hand side) -/
def EB (kz : I → Mode) (l : List I) (Rc R' t0 : Nat) (ι : R) (A : I → V3 R) : R :=
  K.add
    (K.mul (two' K) (K.add
      (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Zl K kz l (hiF K (lowB Rc kz) A)) (K.mul (C1' K inv R') (Zl K kz l A)))))
      (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Al K kz l (hiF K (lowB Rc kz) A)) (K.mul (C2' K inv R' t0) (Zl K kz l A)))))))
    (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (Al K kz l A) (K.mul (castN K 135) (Zl K kz l (hiF K (lowB Rc kz) A))))))

def alph (kz : I → Mode) (L : List I) (R' : Nat) (ι : R) (a B : I → V3 R) : R :=
  K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') (Zl K kz L a))))
    (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) (Al K kz L B))))
def bet (kz : I → Mode) (L : List I) (R' t0 : Nat) (ι : R) (a : I → V3 R) : R :=
  K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) (Zl K kz L a)))

theorem sq2' (G X E : V3 R) (hx : G.x = K.add X.x E.x) (hy : G.y = K.add X.y E.y) (hz : G.z = K.add X.z E.z) :
    Le K O.toCone (dot3 K G G) (K.add (K.mul (two' K) (dot3 K X X)) (K.mul (two' K) (dot3 K E E))) := by
  have e : G = add3 K X E := v3_ext _ _ hx hy hz
  rw [e]; exact sq2 K O X E

set_option maxRecDepth 200000 in
include mul_inv h10 in
theorem trunc_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν α S : R) (hh : O.nn h) (hν : O.nn ν) (hα0 : O.nn α) (hS0 : O.nn S) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hα : ∀ t, Le K O.toCone (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t)) α)
    (hβ : ∀ t, Le K O.toCone (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t))) (K.mul ν ν))
    (hS : ∀ t, Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) S) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h S)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hdN := pw_filter kz (lowB Rc kz) l hd
  have hMN : ∀ p, p ∈ l.filter (lowB Rc kz) → nrm (kz p) ≤ M + 1 := fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)
  have hSn : ∀ t, O.nn (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) := fun t =>
    O.nn_mul _ _ n2 (O.nn_add _ _
      (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hZn _ _) (O.nn_mul _ _ nC1 (hZn _ _)))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ nC2 (hZn _ _)))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZn _ _))))))
  refine gronwall_src K O ν h α S _ _ (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) hν hh hα0 hS0 (fun t => hAn _ _)
    (fun t => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn _ _)))))
    hα hSn
    hS hβ (fun t => hZn _ _) ?_ T
  intro s
  have hsd : ∀ r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r)
      (dF K (restr K (lowB Rc kz) (A (s + 1))) (B (s + 1)) r) (dF K (restr K (lowB Rc kz) (A s)) (B s) r)
      (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r)) := fun r hr =>
    step_sub K h ν (wt K kz r) _ _ _ _ _ _
      (restr_step K l (lowB Rc kz) (TriadSparse.delta K kz) ι h ν (wt K kz) (kv K kz) A hA s r
        (mem_filter_true (lowB Rc kz) r l hr)) (hB s r hr)
  have es := enst_step_on K O.toCone (l.filter (lowB Rc kz)) h ν hh hν (wt K kz) hw _ _ _ hsd
  -- |G|² ≤ 2|X|² + 2|E|²
  have hG := lsum_le_on K O.toCone (l.filter (lowB Rc kz)) (fun r _ => sq2' K O
    (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring))
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at hG
  have fd := force_diff K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdN h1 M hMN R' t0 hRM ι (restr K (lowB Rc kz) (A s)) (B s)
  have eb0 := lsum_filter_le K O.toCone (lowB Rc kz)
    (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))) (fun _ => dot3_nn K O.toCone _) l
  have eb := le_trans' K O.toCone _ _ _ eb0 (err_bound K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι (A s))
  have sum := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 fd) (le_mul_nn K O.toCone (two' K) _ _ n2 eb)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ hG sum)
  have st := le_trans' K O.toCone _ _ _ es (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st ⟨K.zero, O.nn_zero, ?_⟩
  unfold alph bet EB two' Zl Al C1' C2'
  generalize castN K 135 = c135
  generalize castN K (cube R') = cb
  generalize iv K inv (R' + 1) = ivR
  generalize inv (castN K (t0 + 1)) = it
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Zd
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Ad
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (restr K (lowB Rc kz) (A s) q) (restr K (lowB Rc kz) (A s) q)))
    (List.filter (lowB Rc kz) l) = Za
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B s p) (B s p))) (List.filter (lowB Rc kz) l) = AB
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Zh
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Ah
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (A s q) (A s q))) l = ZA
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A s p) (A s p))) l = AA
  lib_ring

end Main

end TruncCauchy

#print axioms TruncCauchy.lsum_le_on
#print axioms TruncCauchy.enst_step_on
#print axioms TruncCauchy.pw_filter
#print axioms TruncCauchy.lsum_filter_le
#print axioms TruncCauchy.trunc_cauchy

#print axioms: 6 theorems, no axioms.

79.205 Explicit rate in the truncation

With \(Z(A_t)\le Z_b\), \(A(A_t)\le A_a\), \(A(B_t)\le A_b\) and \(8\iota ^2C_2Z_b\le \nu ^2\): the source is \(S\lesssim \iota ^2(C_1Z_bA_a/(R+1)^2+C_2A_aZ_b+135A_a^2/(R+1)^2)\). So \(S\to 0\) as \(R\to \infty \) and \(C_2\to 0\): the discrete-time Galerkin family is Cauchy.

ns/TruncRate.lean

/-
  ns/TruncRate.lean — T37 cycle 1053 (goal G695): the truncation Cauchy estimate with explicit constants. If on the time
  interval the large-truncation solution A has Z(A_t) ≤ Zb and A(A_t) ≤ Aa (EnstLocalTime gives both), and the small
  one has A(B_t) ≤ Ab, and the cutoff constant satisfies 8ι²C₂·Zb ≤ ν², then (TruncCauchy.trunc_cauchy)
    ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S),
    α = 8ι²C₁Zb + 4ι²·135·Ab,
    S = 2[2(2ι²·Zh·C₁·Zb + 2ι²·Aa·C₂·Zb) + 2ι²·Aa·135·Zh],   Zh = Aa/(R+1)²  (high-mode enstrophy bound).
  As R → ∞ (and C₂ → 0 by the internal cutoffs) the source S → 0: the discrete-time Galerkin family is Cauchy.

  Lean 4 core plus ns/TruncCauchy.
-/
import ns.TruncCauchy

namespace TruncRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

/-- monotone in the restricted field: Z over the filtered list of A|_N ≤ Z over l of A -/
theorem Z_restr_le (l : List I) (kz : I → Mode) (inN : I → Bool) (A : I → V3 R) :
    Le K O.toCone (Zl K kz (l.filter inN) (restr K inN A)) (Zl K kz l A) := by
  have h1 := lsum_filter_le K O.toCone inN (fun p => K.mul (wt K kz p) (dot3 K (restr K inN A p) (restr K inN A p)))
    (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have h2 := lsum_le K O.toCone _ _ (fun p => part_leW K O (wt K kz p) (dot3_nn K O.toCone _) A p (inN p)) l
  exact le_trans' K O.toCone _ _ _ h1 h2

theorem Zh_le (l : List I) (kz : I → Mode) (Rc : Nat) (A : I → V3 R) (Aa : R)
    (hA : Le K O.toCone (Al K kz l A) Aa) (iR : R)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one) (hiRn : O.nn iR) :
    Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) A)) (K.mul Aa iR) := by
  have hz := high_Z K O l kz Rc A
  have ha := lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) A p (lowB Rc kz p)) l
  have h3 := le_trans' K O.toCone _ _ _ hz (le_trans' K O.toCone _ _ _ ha hA)
  have h4 := le_mul_nn K O.toCone iR _ _ hiRn h3
  show Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l) _
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) A p) (hiF K (lowB Rc kz) A p))) l = Zh at h4 ⊢
  generalize castN K (Rc + 1) = c at h4 hiR
  have e : K.mul iR (K.mul (K.mul c c) Zh) = K.mul (K.mul (K.mul c c) iR) Zh := by lib_ring
  rw [e, hiR, K.mul_comm iR] at h4
  rw [show K.mul K.one Zh = Zh by lib_ring] at h4
  exact h4

theorem mm (x x' y y' : R) (hx : O.nn x) (hy' : O.nn y') (h1 : Le K O.toCone x x') (h2 : Le K O.toCone y y') :
    Le K O.toCone (K.mul x y) (K.mul x' y') :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone x _ _ hx h2) (ForceSob.le_mul_right' K O.toCone h1 hy')

theorem nn_of_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : O.nn y := by
  cases h with
  | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1

include mul_inv h10 in
theorem trunc_rate (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hZA : ∀ t, Le K O.toCone (Zl K kz l (A t)) Zb) (hAA : ∀ t, Le K O.toCone (Al K kz l (A t)) Aa)
    (hAB : ∀ t, Le K O.toCone (Al K kz (l.filter (lowB Rc kz)) (B t)) Ab)
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have refl := fun (x : R) => le_refl' K O.toCone x
  have hZa : ∀ t, Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Zb := fun t =>
    le_trans' K O.toCone _ _ _ (Z_restr_le K O l kz (lowB Rc kz) (A t)) (hZA t)
  have hZh : ∀ t, Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t =>
    Zh_le K O l kz Rc (A t) Aa (hAA t) iR hiR hiRn
  have hAh : ∀ t, Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) Aa := fun t =>
    le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) (A t) p (lowB Rc kz p)) l) (hAA t)
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  refine trunc_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ _ hh hν
    (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hZb)))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 hAb))))
    (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nZh (O.nn_mul _ _ nC1 hZb))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ nC2 hZb))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ n135 nZh))))))
    A B hA hB ?_ ?_ ?_ T
  · intro t
    exact le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ nC1 (hZa t))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ n135 (hAB t))))
  · intro t
    exact le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
      (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t)))) hβc
  · intro t
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hZb) (hZh t) (le_mul_nn K O.toCone _ _ _ nC1 (hZA t)))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hZb) (hAh t) (le_mul_nn K O.toCone _ _ _ nC2 (hZA t)))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (hAA t) (le_mul_nn K O.toCone _ _ _ n135 (hZh t))))))

end Gen

end TruncRate

#print axioms TruncRate.Z_restr_le
#print axioms TruncRate.Zh_le
#print axioms TruncRate.mm
#print axioms TruncRate.nn_of_le
#print axioms TruncRate.trunc_rate

#print axioms: 5 theorems, no axioms.

79.206 One step of size \(2k\) against two of size \(k\)

With \(x=k\nu w\): \((1+2x)(1+x)^2(a_1-b_2)=x^2a_0+k(N_0-N_1)+kx(2N_0-3N_1)+2kx^2(N_0-N_1)\), exactly. The consistency identity for the limit \(h\to 0\).

ns/StepHalf.lean

/-
  ns/StepHalf.lean — T37 cycle 1057 (goal G656a): one implicit-viscosity step of size 2k against two steps of size k,
  exact identity, over any commutative ring (componentwise). With x = kνw and
    (1 + 2x) a₁ = a₀ + 2k N₀,     (1 + x) b₁ = a₀ + k N₀,     (1 + x) b₂ = b₁ + k N₁,
  the defect satisfies
    half_defect   (1 + 2x)(1 + x)²(a₁ − b₂) = x² a₀ + k(N₀ − N₁) + kx(2N₀ − 3N₁) + 2k x²(N₀ − N₁).
  So the one-step/two-step difference is of order k² once N₀ − N₁ = O(k) (Lipschitz force, a₀ − b₁ = O(k)) —
  the consistency step for h → 0 (G656b, c).

  Lean 4 core plus ns/EnstStep (StepC), lib/RingTac.
-/
import ns.EnstStep

namespace StepHalf
open DetChain NSChain LibRing EnstStep

section Gen
variable {R : Type} (K : CRing R)

theorem half_defect (k ν w a0 a1 b1 b2 N0 N1 : R)
    (h1 : K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w)) a1 = K.add a0 (K.mul (K.add k k) N0))
    (h2 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b1 = K.add a0 (K.mul k N0))
    (h3 : K.mul (K.add K.one (K.mul (K.mul k ν) w)) b2 = K.add b1 (K.mul k N1)) :
    K.mul (K.mul (K.add K.one (K.mul (K.mul (K.add k k) ν) w))
        (K.mul (K.add K.one (K.mul (K.mul k ν) w)) (K.add K.one (K.mul (K.mul k ν) w)))) (K.add a1 (K.neg b2))
      = K.add (K.add (K.add (K.mul (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w)) a0)
          (K.mul k (K.add N0 (K.neg N1))))
          (K.mul (K.mul k (K.mul (K.mul k ν) w)) (K.add (K.add N0 N0) (K.neg (K.add (K.add N1 N1) N1)))))
        (K.mul (K.mul (K.add k k) (K.mul (K.mul (K.mul k ν) w) (K.mul (K.mul k ν) w))) (K.add N0 (K.neg N1))) := by
  generalize hx : K.mul (K.mul k ν) w = x at h2 h3
  have hx2 : K.mul (K.mul (K.add k k) ν) w = K.add x x := by rw [← hx]; lib_ring
  rw [hx2] at h1
  rw [hx2]
  -- (1+2x)(1+x)² a₁ = (1+x)²(a₀ + 2kN₀)
  have ea : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) a1
      = K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.add a0 (K.mul (K.add k k) N0)) := by
    rw [← h1]; lib_ring
  -- (1+2x)(1+x)² b₂ = (1+2x)(a₀ + kN₀ + k(1+x)N₁)
  have eb : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2
      = K.mul (K.add K.one (K.add x x)) (K.add (K.add a0 (K.mul k N0)) (K.mul (K.mul k (K.add K.one x)) N1)) := by
    have : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2
        = K.mul (K.add K.one (K.add x x)) (K.add (K.mul (K.add K.one x) b1) (K.mul (K.mul k (K.add K.one x)) N1)) := by
      calc _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.add K.one x)) (K.mul (K.add K.one x) b2) := by lib_ring
        _ = K.mul (K.mul (K.add K.one (K.add x x)) (K.add K.one x)) (K.add b1 (K.mul k N1)) := by rw [h3]
        _ = _ := by lib_ring
    rw [this, h2]
  have e : K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) (K.add a1 (K.neg b2))
      = K.add (K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) a1)
          (K.neg (K.mul (K.mul (K.add K.one (K.add x x)) (K.mul (K.add K.one x) (K.add K.one x))) b2)) := by lib_ring
  rw [e, ea, eb]; lib_ring

end Gen

end StepHalf

#print axioms StepHalf.half_defect

#print axioms: 1 theorem, no axioms.

79.207 The weighted energy step

For any weight \(V\ge 0\): \(\nu \sum wV|a'|^2+h\nu ^2\sum w^2V|a'|^2\le \nu \sum wV|a|^2+h\sum V|N|^2\). With \(V=|k|^2\) this is the \(H^2\)-energy step.

ns/EnstStepV.lean

/-
  ns/EnstStepV.lean — T37 cycle 1059 (goal G656b, piece i): the enstrophy step with an extra weight V ≥ 0, over a
  commutative ring with a nonnegative cone. For (1 + hνw_r) a'_r = a_r + h N_r:
    enst_stepV   ν Σ wV|a'|² + hν² Σ w²V|a'|² ≤ ν Σ wV|a|² + h Σ V|N|²
  (mode_step times V). With V = w = |k|² this is the H²-energy step νA' + hν²Σw³|a'|² ≤ νA + hΣw|N|².

  Lean 4 core plus ns/EnstStep.
-/
import ns.EnstStep

namespace EnstStepV
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem enst_stepV (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w V : I → R) (hw : ∀ r, P.nn (w r))
    (hV : ∀ r, P.nn (V r)) (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (V r)) (dot3 K (a' r) (a' r))) l)))
      (K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l))) := by
  have t := lsum_le K P _ _ (fun r => le_mul_nn K P (V r) _ _ (hV r)
    (mode_step K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r))) l
  have eL : lsum K (fun r => K.mul (V r) (K.add (K.mul (K.mul ν (w r)) (dot3 K (a' r) (a' r)))
      (K.mul (K.mul h (K.mul (K.mul ν ν) (K.mul (w r) (w r)))) (dot3 K (a' r) (a' r))))) l
      = K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a' r) (a' r))) l))
        (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun r => K.mul (K.mul (K.mul (w r) (w r)) (V r)) (dot3 K (a' r) (a' r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  have eR : lsum K (fun r => K.mul (V r) (K.add (K.mul (K.mul ν (w r)) (dot3 K (a r) (a r))) (K.mul h (dot3 K (N r) (N r))))) l
      = K.add (K.mul ν (lsum K (fun r => K.mul (K.mul (w r) (V r)) (dot3 K (a r) (a r))) l))
        (K.mul h (lsum K (fun r => K.mul (V r) (dot3 K (N r) (N r))) l)) := by
    rw [← lsum_mul_left, ← lsum_mul_left, ← lsum_add]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [eL, eR] at t
  exact t

end Gen

end EnstStepV

#print axioms EnstStepV.enst_stepV

#print axioms: 1 theorem, no axioms.

79.208 Force bound with an output weight

If \(\delta (p,q,r)W_r\le \delta (p,q,r)(2W_p+2W_q)\), then \(\sum _rW_r|N_r|^2\le \iota ^2H_\mu (u)\sum _{p,q}\nu _p^2(2W_p+2W_q)|k_q|^2|v_q|^2\). The derivative of the force is distributed onto the inputs.

ns/ForceSobW.lean

/-
  ns/ForceSobW.lean — T37 cycle 1061 (goal G656b, piece ii): the Galerkin force bound with an output weight W_r, over
  a commutative ring with a nonnegative cone. If the weight satisfies the triad triangle inequality
  δ(p,q,r)·W_r ≤ δ(p,q,r)·(2W_p + 2W_q) (for W = |k|² and k_r = −(k_p + k_q)), then
    total_sobW   Σ_r W_r|N_r(u,v)|² ≤ ι²·H_μ(u)·Σ_pΣ_q ν_p²(2W_p + 2W_q)|k_q|²|v_q|²,
  the derivative of the force distributed onto the inputs — the H²-level counterpart of ForceSob.total_sob.
    total_sobWc  the same with δ·W_r ≤ δ·c(W_p + W_q) for a constant c ≥ 0 (for W = |k|⁴, c = 8).

  Lean 4 core plus ns/ForceSob (mode_sob, le_mul_right'), ns/FourierGalerkin (lsum_swap).
-/
import ns.ForceSob

namespace ForceSobW
open DetChain NSChain LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound VecCS LerayProj ForceSob

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem total_sobW (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν W : I → R) (hW : ∀ r, P.nn (W r))
    (htri : ∀ p q r, Le K P (K.mul (δ p q r) (W r)) (K.mul (δ p q r) (K.add (K.add (W p) (W p)) (K.add (W q) (W q)))))
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => K.mul (W r) (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_mul_nn K P (W r) _ _ (hW r)
    (le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν) (le_mul_right' K P (hA r) (hBn r)))) l
  refine le_trans' K P _ _ _ s1 ?_
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have e0 : lsum K (fun r => K.mul (W r) (K.mul (K.mul (K.mul ι ι) H)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) l
      = K.mul (K.mul (K.mul ι ι) H) (lsum K (fun r => K.mul (W r)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e0]
  -- Σ_r W_r B_r = Σ_p Σ_q ν_p² X_q Σ_r δ W_r ≤ Σ_p Σ_q ν_p² X_q (2W_p + 2W_q) Σ_r δ ≤ ...
  have sw : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
    have e1 : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
        = lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
      apply lsum_ext; intro r
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (W r) (K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          = K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (K.mul (δ p q r) (δ p q r)) (W r)) := by lib_ring
      rw [t, hδ]
    rw [e1, lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l l]
    apply lsum_ext; intro p
    exact lsum_swap K (fun r q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l l
  rw [sw]
  suffices key : Le K P (lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (δ p q r) (W r))) l) l) l)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) by
    have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
    exact le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by rw [add_zero']; lib_ring⟩
  refine lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => ?_) l) l
  -- Σ_r X (δ W_r) ≤ Σ_r X δ (2W_p + 2W_q) = X (2W_p+2W_q) Σ_r δ ≤ X (2W_p + 2W_q)
  have hX : P.nn (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) :=
    P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))
  have hWpq : P.nn (K.add (K.add (W p) (W p)) (K.add (W q) (W q))) :=
    P.nn_add _ _ (P.nn_add _ _ (hW p) (hW p)) (P.nn_add _ _ (hW q) (hW q))
  have a1 := lsum_le K P _ _ (fun r => le_mul_nn K P _ _ _ hX (htri p q r)) l
  have e2 : lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
      (K.mul (δ p q r) (K.add (K.add (W p) (W p)) (K.add (W q) (W q))))) l
      = K.mul (K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.add (K.add (W p) (W p)) (K.add (W q) (W q)))) (lsum K (fun r => δ p q r) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e2] at a1
  have a2 := le_mul_nn K P _ _ _ (P.nn_mul _ _ hX hWpq) (spB p q)
  refine le_trans' K P _ _ _ a1 (le_trans' K P _ _ _ a2 ⟨K.zero, P.nn_zero, ?_⟩)
  rw [add_zero']; lib_ring

theorem total_sobWc (l : List I) (δ : I → I → I → R) (ι : R) (k u v : I → V3 R) (μ ν W : I → R) (hW : ∀ r, P.nn (W r))
    (c : R) (hc : P.nn c) (htri : ∀ p q r, Le K P (K.mul (δ p q r) (W r)) (K.mul (δ p q r) (K.mul c (K.add (W p) (W q)))))
    (h01 : ∀ p q r, δ p q r = K.zero ∨ δ p q r = K.one) (hμν : ∀ p, K.mul (μ p) (ν p) = K.one)
    (spA : ∀ p r, Le K P (lsum K (fun q => δ p q r) l) K.one) (spB : ∀ p q, Le K P (lsum K (fun r => δ p q r) l) K.one) :
    Le K P (lsum K (fun r => K.mul (W r) (dot3 K (Nl K l δ ι k u v r) (Nl K l δ ι k u v r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l)
        (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul c (K.add (W p) (W q))))
          (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) := by
  have hδ : ∀ p q r, K.mul (δ p q r) (δ p q r) = δ p q r := by
    intro p q r
    cases h01 p q r with
    | inl h => rw [h]; exact zero_mul' K _
    | inr h => rw [h]; exact K.one_mul _
  have hδn : ∀ p q r, P.nn (δ p q r) := by
    intro p q r; rw [← hδ]; exact P.nn_sq _
  generalize hH : lsum K (fun p => K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) l = H
  -- A_r ≤ ι² H
  have hA : ∀ r, Le K P (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
      (dot3 K (u p) (u p))) l) l) (K.mul (K.mul ι ι) H) := by
    intro r
    have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p)))
        (dot3 K (u p) (u p))) l) l
        = K.mul (K.mul ι ι) (lsum K (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) (lsum K (fun q => δ p q r) l)) l) := by
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left, ← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (K.mul (K.mul (K.mul (δ p q r) ι) (μ p)) (K.mul (K.mul (δ p q r) ι) (μ p))) (dot3 K (u p) (u p))
          = K.mul (K.mul (δ p q r) (δ p q r)) (K.mul (K.mul ι ι) (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p)))) := by lib_ring
      rw [t, hδ]; lib_ring
    rw [e, ← hH]
    apply le_mul_nn K P _ _ _ (P.nn_sq ι)
    refine le_trans' K P _ _ _ (lsum_le K P _ (fun p => K.mul (K.mul (K.mul (μ p) (μ p)) (dot3 K (u p) (u p))) K.one)
      (fun p => le_mul_nn K P _ _ _ (P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) (spA p r)) l) ⟨K.zero, P.nn_zero, ?_⟩
    rw [add_zero']; apply lsum_ext; intro p; exact mul_one' K _
  have hBn : ∀ r, P.nn (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) := fun r =>
    lsum_nn K P _ (fun p => lsum_nn K P _ (fun q => P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))) l) l
  have s1 := lsum_le K P _ _ (fun r => le_mul_nn K P (W r) _ _ (hW r)
    (le_trans' K P _ _ _ (mode_sob K P l δ ι k u v μ ν r (fun p q => hδ p q r) hμν) (le_mul_right' K P (hA r) (hBn r)))) l
  refine le_trans' K P _ _ _ s1 ?_
  have Hn : P.nn H := by rw [← hH]; exact lsum_nn K P _ (fun p => P.nn_mul _ _ (P.nn_sq _) (dot3_nn K P _)) l
  have e0 : lsum K (fun r => K.mul (W r) (K.mul (K.mul (K.mul ι ι) H)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l))) l
      = K.mul (K.mul (K.mul ι ι) H) (lsum K (fun r => K.mul (W r)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e0]
  -- Σ_r W_r B_r = Σ_p Σ_q ν_p² X_q Σ_r δ W_r ≤ Σ_p Σ_q ν_p² X_q (2W_p + 2W_q) Σ_r δ ≤ ...
  have sw : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
      = lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
    have e1 : lsum K (fun r => K.mul (W r) (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p)))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l)) l
        = lsum K (fun r => lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l) l := by
      apply lsum_ext; intro r
      rw [← lsum_mul_left]
      apply lsum_ext; intro p
      rw [← lsum_mul_left]
      apply lsum_ext; intro q
      have t : K.mul (W r) (K.mul (K.mul (K.mul (δ p q r) (ν p)) (K.mul (δ p q r) (ν p))) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          = K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (K.mul (δ p q r) (δ p q r)) (W r)) := by lib_ring
      rw [t, hδ]
    rw [e1, lsum_swap K (fun r p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l) l l]
    apply lsum_ext; intro p
    exact lsum_swap K (fun r q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul (δ p q r) (W r))) l l
  rw [sw]
  suffices key : Le K P (lsum K (fun p => lsum K (fun q => lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p))
      (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) (K.mul (δ p q r) (W r))) l) l) l)
      (lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul c (K.add (W p) (W q))))
        (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) l) l) by
    have m := le_mul_nn K P (K.mul (K.mul ι ι) H) _ _ (P.nn_mul _ _ (P.nn_sq ι) Hn) key
    exact le_trans' K P _ _ _ m ⟨K.zero, P.nn_zero, by rw [add_zero']; lib_ring⟩
  refine lsum_le K P _ _ (fun p => lsum_le K P _ _ (fun q => ?_) l) l
  -- Σ_r X (δ W_r) ≤ Σ_r X δ (2W_p + 2W_q) = X (2W_p+2W_q) Σ_r δ ≤ X (2W_p + 2W_q)
  have hX : P.nn (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q)))) :=
    P.nn_mul _ _ (P.nn_sq _) (P.nn_mul _ _ (dot3_nn K P _) (dot3_nn K P _))
  have hWpq : P.nn (K.mul c (K.add (W p) (W q))) :=
    P.nn_mul _ _ hc (P.nn_add _ _ (hW p) (hW q))
  have a1 := lsum_le K P _ _ (fun r => le_mul_nn K P _ _ _ hX (htri p q r)) l
  have e2 : lsum K (fun r => K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
      (K.mul (δ p q r) (K.mul c (K.add (W p) (W q))))) l
      = K.mul (K.mul (K.mul (K.mul (ν p) (ν p)) (K.mul (dot3 K (k q) (k q)) (dot3 K (v q) (v q))))
          (K.mul c (K.add (W p) (W q)))) (lsum K (fun r => δ p q r) l) := by
    rw [← lsum_mul_left]; exact lsum_ext K _ _ (fun r => by lib_ring) l
  rw [e2] at a1
  have a2 := le_mul_nn K P _ _ _ (P.nn_mul _ _ hX hWpq) (spB p q)
  refine le_trans' K P _ _ _ a1 (le_trans' K P _ _ _ a2 ⟨K.zero, P.nn_zero, ?_⟩)
  rw [add_zero']; lib_ring

end Gen

end ForceSobW

#print axioms ForceSobW.total_sobW
#print axioms ForceSobW.total_sobWc

#print axioms: 2 theorems, no axioms.

79.209 Integer casts and the triad triangle inequality

\(\mathrm {castZ}\) is additive; for integer triads \(k_p+k_q+k_r=0\): \(|k_r|^2\le 2|k_p|^2+2|k_q|^2\), hence \(\delta W_r\le \delta (2W_p+2W_q)\) for \(W=|k|^2\). Supplies the hypothesis of the output-weighted force bound.

ns/CastZAdd.lean

/-
  ns/CastZAdd.lean — T37 cycle 1063 (goal G656b, piece iii): the integer cast into a commutative ring is additive, and
  integer triads satisfy the triangle inequality for |k|².
    castZ_sNN     castZ (m ⊖ n) = castN m − castN n        (subNatNat)
    castZ_add     castZ (a + b) = castZ a + castZ b
    castZ_neg     castZ (−a) = −castZ a
    tri_sq        k_r = −(k_p + k_q) (integer triad, componentwise) ⇒ |k_r|² ≤ 2|k_p|² + 2|k_q|² (ring vectors)
  (witness |k_p − k_q|²). This supplies the hypothesis of ForceSobW.total_sobW for W = |k|².

  Lean 4 core plus lib/IntRing (subNatNat lemmas), ns/RatModel (cast_add), ns/NormCompare (kvec), lib/RingTac.
-/
import lib.IntRing
import ns.NormCompare
import ns.TriadSparse

namespace CastZAdd
open DetChain NSChain LibRing DyadicLarge FourierGalerkin LibInt NormCompare CubeCount

section Gen
variable {R : Type} (K : CRing R)

theorem castZ_sNN (m n : Nat) : castZ K (Int.subNatNat m n) = K.add (castN K m) (K.neg (castN K n)) := by
  cases Nat.lt_or_ge m n with
  | inr hge =>
    have ⟨d, hd⟩ := LibGcd.le_dest hge
    rw [hd, show Int.subNatNat (n + d) n = Int.subNatNat (d + n) (0 + n) by rw [Nat.add_comm n d, Nat.zero_add],
      sNN_add_k, sNN_zero]
    show castN K d = K.add (castN K (n + d)) (K.neg (castN K n))
    rw [RatModel.cast_add K n d]; lib_ring
  | inl hlt =>
    have ⟨d, hd⟩ := LibGcd.le_dest (Nat.succ_le_of_lt hlt)
    rw [hd, show Int.subNatNat m (m + 1 + d) = Int.subNatNat (0 + m) ((d + 1) + m) by
      rw [Nat.zero_add, show m + 1 + d = (d + 1) + m by lib_nat], sNN_add_k, sNN_zero_left]
    show K.neg (castN K (d + 1)) = K.add (castN K m) (K.neg (castN K (m + 1 + d)))
    rw [show m + 1 + d = m + (d + 1) by lib_nat, RatModel.cast_add K m (d + 1)]; lib_ring

theorem castZ_add (a b : Int) : castZ K (a + b) = K.add (castZ K a) (castZ K b) := by
  cases a with
  | ofNat m => cases b with
    | ofNat n => show castN K (m + n) = K.add (castN K m) (castN K n); exact RatModel.cast_add K m n
    | negSucc n =>
      show castZ K (Int.subNatNat m (n + 1)) = K.add (castN K m) (K.neg (castN K (n + 1)))
      exact castZ_sNN K m (n + 1)
  | negSucc m => cases b with
    | ofNat n =>
      show castZ K (Int.subNatNat n (m + 1)) = K.add (K.neg (castN K (m + 1))) (castN K n)
      rw [castZ_sNN]; lib_ring
    | negSucc n =>
      show K.neg (castN K (m + n + 1 + 1)) = K.add (K.neg (castN K (m + 1))) (K.neg (castN K (n + 1)))
      rw [show m + n + 1 + 1 = (m + 1) + (n + 1) by lib_nat, RatModel.cast_add K (m + 1) (n + 1)]; lib_ring

theorem castZ_neg (a : Int) : castZ K (-a) = K.neg (castZ K a) := by
  have h := castZ_add K a (-a)
  rw [show a + -a = 0 by lib_int] at h
  have h0 : castZ K 0 = K.zero := rfl
  rw [h0] at h
  calc castZ K (-a) = K.add (K.add (castZ K a) (castZ K (-a))) (K.neg (castZ K a)) := by lib_ring
    _ = K.neg (castZ K a) := by rw [← h]; lib_ring

theorem comp_neg (x y z : Int) (h : x + y + z = 0) : castZ K z = K.neg (K.add (castZ K x) (castZ K y)) := by
  have e : z = -(x + y) := by
    have : z = (x + y + z) + (-(x + y)) := by lib_int
    rw [this, h]; lib_int
  rw [e, castZ_neg, castZ_add]

variable (P : Cone K)

/-- integer triad k_p + k_q + k_r = 0 ⇒ |k_r|² ≤ 2|k_p|² + 2|k_q|² -/
theorem tri_sq (kp kq kr : Mode) (h : TriadSparse.tadd (TriadSparse.tadd kp kq) kr = TriadSparse.zero3) :
    Le K P (dot3 K (kvec K kr) (kvec K kr))
      (K.add (K.add (dot3 K (kvec K kp) (kvec K kp)) (dot3 K (kvec K kp) (kvec K kp)))
        (K.add (dot3 K (kvec K kq) (kvec K kq)) (dot3 K (kvec K kq) (kvec K kq)))) := by
  have h1 : kp.1 + kq.1 + kr.1 = 0 := congrArg Prod.fst h
  have h2 : kp.2.1 + kq.2.1 + kr.2.1 = 0 := congrArg (fun t => t.2.1) h
  have h3 : kp.2.2 + kq.2.2 + kr.2.2 = 0 := congrArg (fun t => t.2.2) h
  have e1 := comp_neg K _ _ _ h1
  have e2 := comp_neg K _ _ _ h2
  have e3 := comp_neg K _ _ _ h3
  refine ⟨K.add (K.add (K.mul (K.add (castZ K kp.1) (K.neg (castZ K kq.1))) (K.add (castZ K kp.1) (K.neg (castZ K kq.1))))
      (K.mul (K.add (castZ K kp.2.1) (K.neg (castZ K kq.2.1))) (K.add (castZ K kp.2.1) (K.neg (castZ K kq.2.1)))))
      (K.mul (K.add (castZ K kp.2.2) (K.neg (castZ K kq.2.2))) (K.add (castZ K kp.2.2) (K.neg (castZ K kq.2.2)))),
    P.nn_add _ _ (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)) (P.nn_sq _), ?_⟩
  show K.add (K.add (K.add (K.mul (castZ K kr.1) (castZ K kr.1)) (K.mul (castZ K kr.2.1) (castZ K kr.2.1)))
      (K.mul (castZ K kr.2.2) (castZ K kr.2.2))) _ = _
  rw [e1, e2, e3]
  show _ = K.add (K.add (K.add (K.add (K.mul (castZ K kp.1) (castZ K kp.1)) (K.mul (castZ K kp.2.1) (castZ K kp.2.1)))
      (K.mul (castZ K kp.2.2) (castZ K kp.2.2))) (K.add (K.add (K.mul (castZ K kp.1) (castZ K kp.1)) (K.mul (castZ K kp.2.1) (castZ K kp.2.1)))
      (K.mul (castZ K kp.2.2) (castZ K kp.2.2))))
    (K.add (K.add (K.add (K.mul (castZ K kq.1) (castZ K kq.1)) (K.mul (castZ K kq.2.1) (castZ K kq.2.1)))
      (K.mul (castZ K kq.2.2) (castZ K kq.2.2))) (K.add (K.add (K.mul (castZ K kq.1) (castZ K kq.1)) (K.mul (castZ K kq.2.1) (castZ K kq.2.1)))
      (K.mul (castZ K kq.2.2) (castZ K kq.2.2))))
  lib_ring

/-- the hypothesis of ForceSobW.total_sobW for W = |k|² and the integer triad indicator -/
theorem delta_tri {I : Type} (kz : I → Mode) (p q r : I) :
    Le K P (K.mul (TriadSparse.delta K kz p q r) (dot3 K (kvec K (kz r)) (kvec K (kz r))))
      (K.mul (TriadSparse.delta K kz p q r) (K.add (K.add (dot3 K (kvec K (kz p)) (kvec K (kz p))) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
        (K.add (dot3 K (kvec K (kz q)) (kvec K (kz q))) (dot3 K (kvec K (kz q)) (kvec K (kz q)))))) := by
  show Le K P (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
    (K.mul (cond (decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3)) K.one K.zero) _)
  cases hd : decide (TriadSparse.tadd (TriadSparse.tadd (kz p) (kz q)) (kz r) = TriadSparse.zero3) with
  | false => exact ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero _) K.zero = K.mul K.zero _; lib_ring⟩
  | true =>
    have t := tri_sq K P (kz p) (kz q) (kz r) (of_decide_eq_true hd)
    have m := le_mul_nn K P K.one _ _ (RatModel.nn_one' K P) t
    exact m

end Gen

end CastZAdd

#print axioms CastZAdd.castZ_sNN
#print axioms CastZAdd.castZ_add
#print axioms CastZAdd.castZ_neg
#print axioms CastZAdd.comp_neg
#print axioms CastZAdd.tri_sq
#print axioms CastZAdd.delta_tri

#print axioms: 6 theorems, no axioms.

79.210 Euclidean against sup norm, the other direction

\(|k|^2\le 3|k|_\infty ^2\) for integer modes cast into an ordered field.

ns/WeightH2.lean

/-
  ns/WeightH2.lean — T37 cycle 1065 (goal G656b, piece iv-a): weights for the H² force bound, over an ordered field.
    dot_le_3nrm   |k|² ≤ 3|k|_∞²   (integer modes cast into the ring)
  With NormCompare.nrm_le_dot (|k|_∞² ≤ |k|²) this sandwiches the Euclidean weight; next: Σ_p |k_p|_∞^{−6}|k_p|² ≤ 3·135.

  Lean 4 core plus ns/NormCompare (kvec, sq_castZ, mx_cases), ns/LatticeBound, ns/EnstLocal (sq_mono).
-/
import ns.NormCompare
import ns.LatticeBound
import ns.EnstLocal

namespace WeightH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin NormCompare ModeCount CubeCount
  LatticeSum LatticeBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_le_mx (x y : Nat) : Le K O.toCone (K.mul (castN K x) (castN K x)) (K.mul (castN K (mx x y)) (castN K (mx x y))) :=
  EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (le_mx_left x y))

theorem sq_le_mx' (x y : Nat) : Le K O.toCone (K.mul (castN K y) (castN K y)) (K.mul (castN K (mx x y)) (castN K (mx x y))) :=
  EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (le_mx_right x y))

/-- |k|² ≤ 3|k|_∞² -/
theorem dot_le_3nrm (k : Mode) :
    Le K O.toCone (dot3 K (kvec K k) (kvec K k))
      (K.add (K.add (K.mul (castN K (nrm k)) (castN K (nrm k))) (K.mul (castN K (nrm k)) (castN K (nrm k))))
        (K.mul (castN K (nrm k)) (castN K (nrm k)))) := by
  show Le K O.toCone (K.add (K.add (K.mul (castZ K k.1) (castZ K k.1)) (K.mul (castZ K k.2.1) (castZ K k.2.1)))
    (K.mul (castZ K k.2.2) (castZ K k.2.2))) _
  rw [sq_castZ, sq_castZ, sq_castZ]
  have hn : nrm k = mx k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs) := rfl
  rw [hn]
  have a1 := sq_le_mx K O k.1.natAbs (mx k.2.1.natAbs k.2.2.natAbs)
  have a2 := le_trans' K O.toCone _ _ _ (sq_le_mx K O k.2.1.natAbs k.2.2.natAbs) (sq_le_mx' K O k.1.natAbs _)
  have a3 := le_trans' K O.toCone _ _ _ (sq_le_mx' K O k.2.1.natAbs k.2.2.natAbs) (sq_le_mx' K O k.1.natAbs _)
  exact le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a1 a2) a3

end Gen

end WeightH2

#print axioms WeightH2.sq_le_mx
#print axioms WeightH2.sq_le_mx'
#print axioms WeightH2.dot_le_3nrm

#print axioms: 3 theorems, no axioms.

79.211 \(H^2\)-level force bound

\(\sum _rw_r|N_r(a,a)|^2\le \iota ^2B(2S_1Z+2S_0A)\), \(B=\sum w^3|a|^2\), \(S_1=\sum _p|k_p|_\infty ^{-6}w_p\), \(S_0=\sum _p|k_p|_\infty ^{-6}\). Output-weighted bound with \(\mu =|k|_\infty ^3\) and the triad triangle inequality.

ns/ForceH2.lean

/-
  ns/ForceH2.lean — T37 cycle 1067 (goal G656b, piece iv-b): the H²-level Galerkin force bound, uniform in the
  truncation, over an ordered field. For integer modes (nonzero, distinct on l), w_r = |k_r|²:
    force_h2   Σ_r w_r|N_r(a,a)|² ≤ ι²·B·(2·S₁·Z + 2·S₀·A),   B = Σ w³|a|²,  Z = Σ w|a|²,  A = Σ w²|a|²,
  with S₁ = Σ_p |k_p|_∞^{−6}w_p and S₀ = Σ_p |k_p|_∞^{−6} (each bounded by a multiple of 135 via LatticeBound; next).
  ForceSobW.total_sobW with μ = |k|_∞³, the triad triangle inequality (CastZAdd.delta_tri), |k|_∞² ≤ |k|² ≤ 3|k|_∞².

  Lean 4 core plus ns/ForceSobW, ns/CastZAdd, ns/WeightH2, ns/ForceTwoScale, ns/ForceUniform.
-/
import ns.ForceSobW
import ns.CastZAdd
import ns.WeightH2
import ns.ForceTwoScale
import ns.TruncRate

namespace ForceH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def cube3 (x : R) : R := K.mul (K.mul x x) x

include O mul_inv h10 in
theorem munu3 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    K.mul (cube3 K (nn K kz p)) (cube3 K (ivn K inv kz p)) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold cube3
  calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
        (K.mul (nn K kz p) (ivn K inv kz p)) := by lib_ring
    _ = K.one := by rw [e]; lib_ring

/-- the u-side weight: n⁶|a|² ≤ w³|a|² -/
theorem mu6_le (kz : I → Mode) (p : I) :
    Le K O.toCone (K.mul (cube3 K (nn K kz p)) (cube3 K (nn K kz p)))
      (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) := by
  have h := nrm_le_dot K O.toCone (kz p)
  have hn : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have hw : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have s2 := EnstLocal.sq_mono K O _ _ hn hw h
  have s3 := TruncRate.mm K O _ _ _ _ (O.nn_mul _ _ hn hn) hw s2 h
  have e : K.mul (cube3 K (nn K kz p)) (cube3 K (nn K kz p))
      = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)) := by
    unfold cube3; lib_ring
  rw [e]; exact s3

include mul_inv h10 in
theorem force_h2 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι) (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l)
        (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) a a
    (fun p => cube3 K (nn K kz p)) (fun p => cube3 K (ivn K inv kz p)) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3 K O inv mul_inv h10 kz h1)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (mu6_le K O kz p) (dot3_nn K O.toCone (a p))) l
  -- the double sum factors
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (a q) (a q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show _ = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
        (K.mul (wt K kz q) (dot3 K (a q) (a q)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q)))))
    show K.mul (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (a q) (a q))) = _
    lib_ring
  rw [e]
  have nS : O.nn (K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l)))) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l)))
      (O.nn_mul _ _ two (O.nn_mul _ _ (lsum_nn K O.toCone _ (fun p => O.nn_sq _) l)
        (lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)))
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (ForceSob.le_mul_right' K O.toCone hH nS)

end Gen

end ForceH2

#print axioms ForceH2.munu3
#print axioms ForceH2.mu6_le
#print axioms ForceH2.force_h2

#print axioms: 3 theorems, no axioms.

79.212 Small-data global \(H^2\) bound, uniform in the truncation

If \(1080\,\iota ^2(\nu A_0+h\nu ^2B_0)\le \nu ^3\) (\(A=\sum w^2|a|^2\), \(B=\sum w^3|a|^2\)) then \(\nu A_t+h\nu ^2B_t\le \nu A_0+h\nu ^2B_0\) for all \(t\). Weighted step with \(V=|k|^2\), the \(H^2\) force bound, \(\sum |k|_\infty ^{-6}\le 135\), \(Z\le A\).

ns/EnstH2.lean

/-
  ns/EnstH2.lean — T37 cycle 1069 (goal G656b, piece iv-c): the small-data global H² bound for the implicit-viscosity
  Galerkin scheme, uniform in the truncation, over an ordered field. With A = Σ w²|a|², B = Σ w³|a|² (w = |k|²):
    S0_le / S1_le   Σ_p |k_p|_∞^{−6} ≤ 135,  Σ_p |k_p|_∞^{−6} w_p ≤ 3·135
    Z_le_A          Σ w|a|² ≤ Σ w²|a|²   (w ≥ 1 on nonzero integer modes)
    h2_global       cH·ι²(νA_0 + hν²B_0) ≤ ν³,  cH = 2·3·135 + 2·135 = 1080  ⇒  νA_t + hν²B_t ≤ νA_0 + hν²B_0 for every t
  (EnstStepV with V = w, ForceH2, then EnstGlobal.q_le with c = 1080ι²).

  Lean 4 core plus ns/ForceH2, ns/EnstStepV, ns/EnstGlobal, ns/LatticeBound, ns/ForceUniform (lsum_norms).
-/
import ns.ForceH2
import ns.EnstStepV

namespace EnstH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 EnstStep EnstStepV EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem pt6_le4 (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (f4 K inv (nrm (kz p))) := by
  have ⟨hi, hle⟩ := ivn_le K O inv mul_inv h10 kz h1 p
  have h4 : O.nn (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have m := le_mul_nn K O.toCone _ _ _ h4 hle
  have e1 : K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))
      = K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
          (K.mul (ivn K inv kz p) (ivn K inv kz p)) := by unfold cube3; lib_ring
  rw [e1]
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul _ K.one) K.zero = K.mul (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
    (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
  show K.add (K.mul (K.mul (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))
    (K.mul (inv (castN K (nrm (kz p)))) (inv (castN K (nrm (kz p)))))) K.one) K.zero = _
  lib_ring

include mul_inv h10 in
theorem pt6w_le (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (p : I) :
    Le K O.toCone (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p))
      (K.mul (K.add (K.add K.one K.one) K.one) (f4 K inv (nrm (kz p)))) := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  have hi := (ivn_le K O inv mul_inv h10 kz h1 p).1
  have w3 := dot_le_3nrm K O (kz p)
  have m := le_mul_nn K O.toCone (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) _ _ (O.nn_sq _) w3
  refine le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p)))
      (K.add (K.add (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))) K.zero
    = K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
  unfold cube3
  generalize nn K kz p = n at e
  generalize ivn K inv kz p = v at e
  calc _ = K.mul (K.add (K.add K.one K.one) K.one) (K.mul (K.mul (K.mul v v) (K.mul v v)) (K.mul (K.mul n v) (K.mul n v))) := by lib_ring
    _ = _ := by rw [e]; lib_ring

include mul_inv h10 in
theorem S0_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) (castN K 135) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt6_le4 K O inv mul_inv h10 kz h1 p) l
  rw [lsum_norms K (f4 K inv) kz l] at a
  exact le_trans' K O.toCone _ _ _ a (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq))

include mul_inv h10 in
theorem S1_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l)
      (K.mul (K.add (K.add K.one K.one) K.one) (castN K 135)) := by
  have a := lsum_le K O.toCone _ _ (fun p => pt6w_le K O inv mul_inv h10 kz h1 p) l
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz l] at a
  refine le_trans' K O.toCone _ _ _ a (le_mul_nn K O.toCone _ _ _ ?_ (lattice_bound K O inv mul_inv h10 (l.map kz) (pw_map kz l hd)
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact h1 q) M
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k l hk; rw [← e]; exact hM q hq)))
  exact O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)

def cH : R := K.add (K.mul (K.add K.one K.one) (K.mul (K.add (K.add K.one K.one) K.one) (castN K 135)))
  (K.mul (K.add K.one K.one) (castN K 135))

theorem Z_le_A (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (l : List I) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l)
      (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l) := by
  refine lsum_le K O.toCone _ _ (fun p => ?_) l
  have c1 := RatModel.cast_mono K O.toCone 1 _ (h1 p)
  have one1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  rw [one1] at c1
  have sq := EnstLocal.sq_mono K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone _) c1
  have w1 := le_trans' K O.toCone _ _ _ sq (nrm_le_dot K O.toCone (kz p))
  rw [show K.mul K.one K.one = K.one by lib_ring] at w1
  cases w1 with
  | intro d hd =>
    refine ⟨K.mul (K.mul (wt K kz p) d) (dot3 K (a p) (a p)),
      O.nn_mul _ _ (O.nn_mul _ _ (dot3_nn K O.toCone _) hd.1) (dot3_nn K O.toCone _), ?_⟩
    have hw : wt K kz p = K.add K.one d := hd.2.symm
    rw [hw]; lib_ring

include mul_inv h10 in
/-- small-data global H² bound, uniform in the truncation -/
theorem h2_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.mul (cH K) (K.mul ι ι))
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) 0))
      (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t)
      (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) 0) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hc : O.nn (K.mul (cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  refine q_le K O ν h _ _ _ hν hν0 hh hc
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l) ?_ small t
  intro s
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l (a s)
  have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a s q) (a s q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  -- 2 S1 Z + 2 S0 A ≤ cH · A
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l = As'
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l = Bs
  generalize castN K 135 = c
  lib_ring

end Gen

end EnstH2

#print axioms EnstH2.pt6_le4
#print axioms EnstH2.pt6w_le
#print axioms EnstH2.S0_le
#print axioms EnstH2.S1_le
#print axioms EnstH2.Z_le_A
#print axioms EnstH2.h2_global

#print axioms: 6 theorems, no axioms.

79.213 Time-Lipschitz estimate of one step

\(\sum _r|a'_r-a_r|^2\le 2h^2\sum _r|N_r|^2+2h^2\nu ^2\sum _rw_r^2|a_r|^2\) for the implicit-viscosity step. With the uniform force bound this is a truncation-uniform equicontinuity estimate.

ns/TimeLip.lean

/-
  ns/TimeLip.lean — T37 cycle 1075 (goal G656, equicontinuity): one implicit-viscosity step moves the solution by at most
    time_lip   Σ_r |a'_r − a_r|² ≤ 2h²·(Σ_r |N_r|² + ν²·Σ_r w_r²|a_r|²)
  over any commutative ring with a nonnegative cone (h, ν, w ≥ 0): from (1 + x)(a' − a) = hN − x a with x = hνw ≥ 0,
  |a' − a| ≤ |hN − x a| ((1+x)² ≥ 1) and |hN − xa|² ≤ 2h²|N|² + 2x²|a|². With ForceUniform (Σ|N|² ≤ 135ι²·A·Z) and the
  local bounds this is a truncation-uniform time-Lipschitz (equicontinuity) estimate in L², an ingredient for the h → 0
  compactness.

  Lean 4 core plus ns/EnstDiff (sub3; EnstStep: StepC, Step), ns/ForceBound (lsum_le), lib/RingTac.
-/
import ns.EnstDiff

namespace TimeLip
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

theorem comp_lip (h ν w a' a n : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (hs : StepC K h ν w a' a n) :
    Le K P (K.mul (K.add a' (K.neg a)) (K.add a' (K.neg a)))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul n n)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (K.mul a a)))) := by
  have hx : P.nn (K.mul (K.mul h ν) w) := P.nn_mul _ _ (P.nn_mul _ _ hh hν) hw
  unfold StepC at hs
  generalize hxd : K.mul (K.mul h ν) w = x at hs hx
  -- y = a' − a satisfies (1 + x) y = hN − x a
  generalize hy : K.add a' (K.neg a) = y
  have hy2 : K.mul (K.add K.one x) y = K.add (K.mul h n) (K.neg (K.mul x a)) := by
    rw [← hy]
    calc _ = K.add (K.mul (K.add K.one x) a') (K.neg (K.mul (K.add K.one x) a)) := by lib_ring
      _ = _ := by rw [hs]; lib_ring
  generalize hz : K.add (K.mul h n) (K.neg (K.mul x a)) = z at hy2
  -- y² ≤ z² = y²(1+x)²;  z² ≤ 2h²n² + 2x²a²
  refine ⟨K.add (K.mul (K.mul y y) (K.add (K.add x x) (K.mul x x)))
      (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))),
    P.nn_add _ _ (P.nn_mul _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_add _ _ hx hx) (P.nn_sq _))) (P.nn_sq _), ?_⟩
  have hzz : K.mul z z = K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul y y) := by
    rw [← hy2]; lib_ring
  have zdef : z = K.add (K.mul h n) (K.neg (K.mul x a)) := hz.symm
  calc K.add (K.mul y y) (K.add (K.mul (K.mul y y) (K.add (K.add x x) (K.mul x x)))
        (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))))
      = K.add (K.mul (K.mul (K.add K.one x) (K.add K.one x)) (K.mul y y))
          (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))) := by lib_ring
    _ = K.add (K.mul z z) (K.mul (K.add (K.mul h n) (K.mul x a)) (K.add (K.mul h n) (K.mul x a))) := by rw [hzz]
    _ = _ := by rw [zdef]; lib_ring

theorem mode_lip (h ν w : R) (hh : P.nn h) (hν : P.nn ν) (hw : P.nn w) (a' a N : V3 R) (hs : Step K h ν w a' a N) :
    Le K P (dot3 K (EnstDiff.sub3 K a' a) (EnstDiff.sub3 K a' a))
      (K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (dot3 K N N)))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w)) (dot3 K a a)))) := by
  have cx := comp_lip K P h ν w a'.x a.x N.x hh hν hw hs.1
  have cy := comp_lip K P h ν w a'.y a.y N.y hh hν hw hs.2.1
  have cz := comp_lip K P h ν w a'.z a.z N.z hh hν hw hs.2.2
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  show K.add (K.add (K.add _ _) _) K.zero
    = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.add (K.add (K.mul N.x N.x) (K.mul N.y N.y)) (K.mul N.z N.z))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (K.mul h ν) w) (K.mul (K.mul h ν) w))
        (K.add (K.add (K.mul a.x a.x) (K.mul a.y a.y)) (K.mul a.z a.z))))
  lib_ring

/-- Σ_r |a'_r − a_r|² ≤ 2h²Σ|N_r|² + 2h²ν²Σ w_r²|a_r|² -/
theorem time_lip (l : List I) (h ν : R) (hh : P.nn h) (hν : P.nn ν) (w : I → R) (hw : ∀ r, P.nn (w r))
    (a' a N : I → V3 R) (hs : ∀ r, Step K h ν (w r) (a' r) (a r) (N r)) :
    Le K P (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a' r) (a r)) (EnstDiff.sub3 K (a' r) (a r))) l)
      (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N r) (N r)) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
          (lsum K (fun r => K.mul (K.mul (w r) (w r)) (dot3 K (a r) (a r))) l))) := by
  have t := lsum_le K P _ _ (fun r => mode_lip K P h ν (w r) hh hν (hw r) (a' r) (a r) (N r) (hs r)) l
  refine le_trans' K P _ _ _ t ⟨K.zero, P.nn_zero, ?_⟩
  rw [add_zero', ← lsum_mul_left, ← lsum_mul_left, ← lsum_add]
  exact lsum_ext K _ _ (fun r => by lib_ring) l

end Gen

end TimeLip

#print axioms TimeLip.comp_lip
#print axioms TimeLip.mode_lip
#print axioms TimeLip.time_lip

#print axioms: 3 theorems, no axioms.

79.214 Equicontinuity over many steps

\((\sum _{j<n}x_j)^2\le n\sum _{j<n}x_j^2\) and \(\sum _r|a_{n,r}-a_{0,r}|^2\le n\sum _{j<n}\sum _r|a_{j+1,r}-a_{j,r}|^2\). With the one-step bound: the solution moves by at most \(2C(nh)^2\) in \(n\) steps, uniformly in the truncation.

ns/TimeLipN.lean

/-
  ns/TimeLipN.lean — T37 cycle 1077 (goal G656c, equicontinuity over many steps): over any commutative ring with a
  nonnegative cone,
    cs_sum     (Σ_{j<n} x_j)² ≤ n · Σ_{j<n} x_j²            (Lagrange: n Q − S² = Σ_{i<j}(x_i − x_j)², by induction)
    lip_n      Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n · Σ_{j<n} Σ_r |a_{t+j+1,r} − a_{t+j,r}|²
  so with TimeLip (each step ≤ 2h²C) the solution moves by at most n²·2h²C = 2C·(nh)² in n steps: Lipschitz in time
  with a constant independent of the truncation.

  Lean 4 core plus NSChain (sumN), ns/EnstDiff (sub3), ns/ForceBound, lib/RingTac.
-/
import ns.EnstDiff

namespace TimeLipN
open DetChain NSChain LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- Σ_{i<n} (x_i − y)² = Q_n − 2yS_n + n y² -/
theorem sq_dev (x : Nat → R) (y : R) : ∀ n,
    sumN K (fun i => K.mul (K.add (x i) (K.neg y)) (K.add (x i) (K.neg y))) n
      = K.add (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.neg (K.mul (K.add y y) (sumN K x n))))
          (K.mul (castN K n) (K.mul y y))
  | 0 => by show K.zero = K.add (K.add K.zero (K.neg (K.mul (K.add y y) K.zero))) (K.mul K.zero (K.mul y y)); lib_ring
  | n + 1 => by
    show K.add (sumN K (fun i => K.mul (K.add (x i) (K.neg y)) (K.add (x i) (K.neg y))) n) (K.mul (K.add (x n) (K.neg y)) (K.add (x n) (K.neg y)))
      = K.add (K.add (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.mul (x n) (x n)))
          (K.neg (K.mul (K.add y y) (K.add (sumN K x n) (x n))))) (K.mul (K.add K.one (castN K n)) (K.mul y y))
    rw [sq_dev x y n]; lib_ring

theorem sumN_sq_nn (f : Nat → R) : ∀ m, P.nn (sumN K (fun i => K.mul (f i) (f i)) m)
  | 0 => P.nn_zero
  | m + 1 => P.nn_add _ _ (sumN_sq_nn f m) (P.nn_sq _)

theorem cs_sum (x : Nat → R) : ∀ n, Le K P (K.mul (sumN K x n) (sumN K x n))
    (K.mul (castN K n) (sumN K (fun i => K.mul (x i) (x i)) n))
  | 0 => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero K.zero) K.zero = K.mul K.zero K.zero; lib_ring⟩
  | n + 1 => by
    have ih := cs_sum x n
    cases ih with
    | intro D hD =>
      refine ⟨K.add D (sumN K (fun i => K.mul (K.add (x i) (K.neg (x n))) (K.add (x i) (K.neg (x n)))) n),
        P.nn_add _ _ hD.1 (sumN_sq_nn K P (fun i => K.add (x i) (K.neg (x n))) n), ?_⟩
      rw [sq_dev K x (x n) n]
      show K.add (K.mul (K.add (sumN K x n) (x n)) (K.add (sumN K x n) (x n))) _
        = K.mul (K.add K.one (castN K n)) (K.add (sumN K (fun i => K.mul (x i) (x i)) n) (K.mul (x n) (x n)))
      have key := hD.2
      generalize sumN K x n = S at key ⊢
      generalize sumN K (fun i => K.mul (x i) (x i)) n = Q at key ⊢
      generalize castN K n = c at key ⊢
      calc _ = K.add (K.add (K.mul S S) D) (K.add (K.add Q (K.mul (x n) (x n))) (K.mul c (K.mul (x n) (x n)))) := by lib_ring
        _ = K.add (K.mul c Q) (K.add (K.add Q (K.mul (x n) (x n))) (K.mul c (K.mul (x n) (x n)))) := by rw [key]
        _ = _ := by lib_ring

theorem tele_fwd (g : Nat → R) : ∀ n, sumN K (fun j => K.add (g (j + 1)) (K.neg (g j))) n = K.add (g n) (K.neg (g 0))
  | 0 => by show K.zero = K.add (g 0) (K.neg (g 0)); lib_ring
  | n + 1 => by
    show K.add (sumN K (fun j => K.add (g (j + 1)) (K.neg (g j))) n) (K.add (g (n + 1)) (K.neg (g n)))
      = K.add (g (n + 1)) (K.neg (g 0))
    rw [tele_fwd g n]; lib_ring

theorem sumN_add3 (f g h : Nat → R) : ∀ n,
    K.add (K.add (sumN K f n) (sumN K g n)) (sumN K h n) = sumN K (fun j => K.add (K.add (f j) (g j)) (h j)) n
  | 0 => by show K.add (K.add K.zero K.zero) K.zero = K.zero; lib_ring
  | n + 1 => by
    show K.add (K.add (K.add (sumN K f n) (f n)) (K.add (sumN K g n) (g n))) (K.add (sumN K h n) (h n))
      = K.add (sumN K (fun j => K.add (K.add (f j) (g j)) (h j)) n) (K.add (K.add (f n) (g n)) (h n))
    rw [← sumN_add3 f g h n]; lib_ring

/-- one mode: |a_n − a_0|² ≤ n Σ_{j<n} |a_{j+1} − a_j|² -/
theorem mode_lipN (a : Nat → V3 R) (n : Nat) :
    Le K P (dot3 K (sub3 K (a n) (a 0)) (sub3 K (a n) (a 0)))
      (K.mul (castN K n) (sumN K (fun j => dot3 K (sub3 K (a (j + 1)) (a j)) (sub3 K (a (j + 1)) (a j))) n)) := by
  have cx := cs_sum K P (fun j => K.add (a (j + 1)).x (K.neg (a j).x)) n
  have cy := cs_sum K P (fun j => K.add (a (j + 1)).y (K.neg (a j).y)) n
  have cz := cs_sum K P (fun j => K.add (a (j + 1)).z (K.neg (a j).z)) n
  rw [tele_fwd K (fun j => (a j).x)] at cx
  rw [tele_fwd K (fun j => (a j).y)] at cy
  rw [tele_fwd K (fun j => (a j).z)] at cz
  have s := le_add' K P _ _ _ _ (le_add' K P _ _ _ _ cx cy) cz
  refine le_trans' K P _ _ _ s ⟨K.zero, P.nn_zero, ?_⟩
  have e := sumN_add3 K (fun j => K.mul (K.add (a (j + 1)).x (K.neg (a j).x)) (K.add (a (j + 1)).x (K.neg (a j).x)))
    (fun j => K.mul (K.add (a (j + 1)).y (K.neg (a j).y)) (K.add (a (j + 1)).y (K.neg (a j).y)))
    (fun j => K.mul (K.add (a (j + 1)).z (K.neg (a j).z)) (K.add (a (j + 1)).z (K.neg (a j).z))) n
  show K.add (K.add (K.add (K.mul (castN K n) _) (K.mul (castN K n) _)) (K.mul (castN K n) _)) K.zero = K.mul (castN K n) _
  rw [add_zero', ← K.distrib, ← K.distrib, e]
  rfl

theorem lsum_sumN {I : Type} (f : I → Nat → R) (l : List I) : ∀ n,
    lsum K (fun r => sumN K (f r) n) l = sumN K (fun j => lsum K (fun r => f r j) l) n
  | 0 => lsum_zero K l
  | n + 1 => by
    show lsum K (fun r => K.add (sumN K (f r) n) (f r n)) l = K.add (sumN K (fun j => lsum K (fun r => f r j) l) n) (lsum K (fun r => f r n) l)
    rw [lsum_add, lsum_sumN f l n]

/-- Σ_r |a_{n,r} − a_{0,r}|² ≤ n · Σ_{j<n} Σ_r |a_{j+1,r} − a_{j,r}|² -/
theorem lip_n {I : Type} (l : List I) (a : Nat → I → V3 R) (n : Nat) :
    Le K P (lsum K (fun r => dot3 K (sub3 K (a n r) (a 0 r)) (sub3 K (a n r) (a 0 r))) l)
      (K.mul (castN K n) (sumN K (fun j => lsum K (fun r => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) l) n)) := by
  have t := lsum_le K P _ _ (fun r => mode_lipN K P (fun j => a j r) n) l
  rw [lsum_mul_left, lsum_sumN K (fun r j => dot3 K (sub3 K (a (j + 1) r) (a j r)) (sub3 K (a (j + 1) r) (a j r))) l n] at t
  exact t

end Gen

end TimeLipN

#print axioms TimeLipN.sq_dev
#print axioms TimeLipN.cs_sum
#print axioms TimeLipN.tele_fwd
#print axioms TimeLipN.mode_lipN
#print axioms TimeLipN.sumN_sq_nn
#print axioms TimeLipN.sumN_add3
#print axioms TimeLipN.lsum_sumN
#print axioms TimeLipN.lip_n

#print axioms: 9 theorems, no axioms.

79.215 Exponential decay of the CFL Lyapunov functional

With \(V_k=E_k+\beta Z_k\), a step \(E_{k+1}+2cZ_{k+1}\le E_k+\beta Z_k\), Poincaré \(E\le Z\) and \(0\le \beta \le c\) give \((1+\beta +c)V_{k+1}\le (1+\beta )V_k\), hence \((1+\beta +c)^nV_n\le (1+\beta )^nV_0\).

ns/CFLDecay.lean

/-
  ns/CFLDecay.lean — T37 cycle 1311 (goal G787a): exponential decay of the CFL Lyapunov functional, over an ordered
  ring. With V_k = E_k + β·Z_k, suppose for every k
      E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k      (CFLStep, once E_k ≤ E* is known from CFLGlobal)
      E_k ≤ Z_k                                (Poincaré: every mode has |k| ≥ 1)
  with 0 ≤ β ≤ c. Then
    decay_step   (1 + β + c)·V_{k+1} ≤ (1 + β)·V_k
    cfl_decay    (1 + β + c)ⁿ·V_n ≤ (1 + β)ⁿ·V_0,
  so for arbitrary data and fixed truncation the discrete energy decays geometrically, at the rate
  (1 + β)/(1 + β + hν) per step (c = hν).

  Lean 4 core plus ns/CFLEnergy (Le API, TruncRate.nn_of_le), lib/RingTac.
-/
import ns.CFLEnergy

namespace CFLDecay
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem decay_step (E E' Z Z' c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ' : O.nn Z')
    (hP : Le K O.toCone E' Z')
    (hs : Le K O.toCone (K.add E' (K.mul (K.add c c) Z')) (K.add E (K.mul β Z))) :
    Le K O.toCone (K.mul (K.add (K.add K.one β) c) (K.add E' (K.mul β Z')))
      (K.mul (K.add K.one β) (K.add E (K.mul β Z))) := by
  have one : O.nn (K.add K.one β) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ
  have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
  cases hs with
  | intro s hs =>
    cases hP with
    | intro p hp =>
      cases hβc with
      | intro d hd =>
        refine ⟨K.add (K.add (K.mul (K.add K.one β) s) (K.mul c p)) (K.mul (K.mul (K.add K.one β) d) Z'),
          O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ one hs.1) (O.nn_mul _ _ hc hp.1))
            (O.nn_mul _ _ (O.nn_mul _ _ one hd.1) hZ'), ?_⟩
        rw [← hs.2]
        have ez : Z' = K.add E' p := hp.2.symm
        have ec : c = K.add β d := hd.2.symm
        rw [ez, ec]
        lib_ring

theorem pw_nn (x : R) (hx : O.nn x) : ∀ n, O.nn (pw K x n)
  | 0 => RatModel.nn_one' K O.toCone
  | n + 1 => O.nn_mul _ _ (pw_nn x hx n) hx

theorem cfl_decay (E Z : Nat → R) (c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hZ : ∀ k, O.nn (Z k))
    (hP : ∀ k, Le K O.toCone (E k) (Z k))
    (hstep : ∀ k, Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k)))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (E n) (K.mul β (Z n))))
      (K.mul (pw K (K.add K.one β) n) (K.add (E 0) (K.mul β (Z 0))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_decay E Z c β hβ hβc hZ hP hstep n
    have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
    have nA : O.nn (K.add (K.add K.one β) c) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ) hc
    have nB : O.nn (K.add K.one β) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hβ
    have st := decay_step K O (E n) (E (n + 1)) (Z n) (Z (n + 1)) c β hβ hβc (hZ (n + 1)) (hP (n + 1)) (hstep n)
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one β) c) n) _ _ (pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add K.one β) _ _ nB ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (K.add K.one β) c)) _) K.zero = _
      lib_ring
    · show K.add (K.mul (pw K (K.add (K.add K.one β) c) n) (K.mul (K.add K.one β) (K.add (E n) (K.mul β (Z n))))) K.zero
        = K.mul (K.add K.one β) (K.mul (pw K (K.add (K.add K.one β) c) n) (K.add (E n) (K.mul β (Z n))))
      lib_ring
    · show K.add (K.mul (K.add K.one β) (K.mul (pw K (K.add K.one β) n) (K.add (E 0) (K.mul β (Z 0))))) K.zero
        = K.mul (K.mul (pw K (K.add K.one β) n) (K.add K.one β)) (K.add (E 0) (K.mul β (Z 0)))
      lib_ring

end Gen

end CFLDecay

#print axioms CFLDecay.decay_step
#print axioms CFLDecay.pw_nn
#print axioms CFLDecay.cfl_decay

#print axioms: 3 theorems, no axioms.

79.216 Geometric energy decay for every datum (fixed truncation)

Under the hypotheses of cfl_global: discrete Poincaré \(\sum |a|^2\le \sum |k|^2|a|^2\) and \((1+\beta +h\nu )^nV_n\le (1+\beta )^nV_0\) for the implicit Fourier–Galerkin scheme, no smallness of the data.

ns/CFLDecayG.lean

/-
  ns/CFLDecayG.lean — T37 cycle 1313 (goal G787 — done): exponential energy decay for arbitrary data and fixed
  truncation |k|_∞ ≤ M + 1, over an ordered field. Under the hypotheses of CFLGlobal.cfl_global (Leray-projected,
  energy-neutral force, CFL condition β ≤ hν, E_0 + β·Z_0 ≤ E*), with V_n = E_n + β·Z_n:
    wt_ge_one        1 ≤ |k|²  for every integer mode with |k|_∞ ≥ 1
    poincare         Σ|a_r|² ≤ Σ w_r|a_r|²            (discrete Poincaré on mean-zero modes)
    cfl_decay_global (1 + β + hν)ⁿ·V_n ≤ (1 + β)ⁿ·V_0,
  so every discrete run decays geometrically at the rate (1 + β)/(1 + β + hν) per step — no smallness of the data.

  Lean 4 core plus ns/CFLGlobal, ns/CFLDecay, ns/NormCompare, ns/RatModel.
-/
import ns.CFLGlobal
import ns.CFLDecay

namespace CFLDecayG
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal CFLDecay

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem wt_ge_one (kz : I → Mode) (r : I) (h1 : 1 ≤ nrm (kz r)) : Le K O.toCone K.one (wt K kz r) := by
  have c1 : castN K 1 = K.one := by show K.add K.one K.zero = K.one; lib_ring
  have hm := RatModel.cast_mono K O.toCone 1 (nrm (kz r)) h1
  have hd := nrm_le_dot K O.toCone (kz r)
  refine le_trans' K O.toCone _ _ _ ?_ hd
  cases hm with
  | intro e he =>
    refine ⟨K.add (K.add e e) (K.mul e e), O.nn_add _ _ (O.nn_add _ _ he.1 he.1) (O.nn_sq e), ?_⟩
    rw [← he.2, c1]
    lib_ring

theorem poincare (l : List I) (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (a r) (a r)) l) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a r) (a r))) l) := by
  refine lsum_le K O.toCone _ _ (fun r => ?_) l
  have m := le_mul_right' K O.toCone (wt_ge_one K O kz r (h1 r)) (dot3_nn K O.toCone (a r))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ m

include mul_inv h10 in
theorem cfl_decay_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (K.mul (pw K (K.add (K.add K.one (beta K M ι h ν Es)) (K.mul h ν)) n)
        (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l))))
      (K.mul (pw K (K.add K.one (beta K M ι h ν Es)) n)
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have hc : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  -- E_t ≤ E* for every t (CFLGlobal)
  have hE : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (a t r) (a t r)) l) Es := by
    intro t
    have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 t
    have hS : O.nn (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t) :=
      NSChain.sumN_nn K O.toCone _ (fun k => nZ (k + 1)) t
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l)),
      O.nn_add _ _ (O.nn_mul _ _ hc hS) (O.nn_mul _ _ nβ (nZ t)), ?_⟩ (le_trans' K O.toCone _ _ _ g h0)
    lib_ring
  exact cfl_decay K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) nβ hcfl nZ
    (fun t => poincare K O l kz h1 (a t))
    (fun t => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) (hE t))
    n

end Gen

end CFLDecayG

#print axioms CFLDecayG.wt_ge_one
#print axioms CFLDecayG.poincare
#print axioms CFLDecayG.cfl_decay_global

#print axioms: 3 theorems, no axioms.

79.217 A decay rate independent of the CFL constant

If moreover \(\beta \le 1\): \((2+c)V_{k+1}\le 2V_k\); for the scheme with \(h\nu \le 1\), \((2+h\nu )^nV_n\le 2^nV_0\), i.e. decay at rate at least \(\nu /2\) per unit time for every datum.

ns/CFLRate.lean

/-
  ns/CFLRate.lean — T37 cycle 1315 (goal G788 — done): a decay rate independent of β, over an ordered field.
  With V_k = E_k + β·Z_k, the CFL step E_{k+1} + 2c·Z_{k+1} ≤ E_k + β·Z_k, Poincaré E ≤ Z and 0 ≤ β ≤ c, β ≤ 1:
    rate_step         (2 + c)·V_{k+1} ≤ 2·V_k          (witness 2s + c·p + Z'(β(1−β) + (c−β)(3−β)))
    cfl_rate          (2 + c)ⁿ·V_n ≤ 2ⁿ·V_0
    cfl_rate_global   for the implicit Fourier–Galerkin scheme under the cfl_global hypotheses and hν ≤ 1:
                      (2 + hν)ⁿ·V_n ≤ 2ⁿ·V_0,
  i.e. V(t) ≤ (1 + hν/2)^{−t/h}·V(0) → e^{−νt/2}·V(0): every datum, fixed truncation, rate ν/2 per unit time.

  Lean 4 core plus ns/CFLDecayG (poincare, cfl_global, cfl_step), ns/CFLDecay (pw_nn), lib/RingTac.
-/
import ns.CFLDecayG

namespace CFLRate
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal CFLDecay CFLDecayG

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem rate_step (E E' Z Z' c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hβ1 : Le K O.toCone β K.one)
    (hZ' : O.nn Z') (hP : Le K O.toCone E' Z')
    (hs : Le K O.toCone (K.add E' (K.mul (K.add c c) Z')) (K.add E (K.mul β Z))) :
    Le K O.toCone (K.mul (K.add (K.add K.one K.one) c) (K.add E' (K.mul β Z')))
      (K.mul (K.add K.one K.one) (K.add E (K.mul β Z))) := by
  have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  cases hs with
  | intro s hs =>
    cases hP with
    | intro p hp =>
      cases hβc with
      | intro d hd =>
        cases hβ1 with
        | intro b hb =>
          refine ⟨K.add (K.add (K.mul (K.add K.one K.one) s) (K.mul c p))
              (K.mul Z' (K.add (K.mul β b) (K.mul d (K.add (K.add K.one K.one) b)))),
            O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ two hs.1) (O.nn_mul _ _ hc hp.1))
              (O.nn_mul _ _ hZ' (O.nn_add _ _ (O.nn_mul _ _ hβ hb.1) (O.nn_mul _ _ hd.1 (O.nn_add _ _ two hb.1)))), ?_⟩
          rw [← hs.2]
          have ez : Z' = K.add E' p := hp.2.symm
          have ec : c = K.add β d := hd.2.symm
          have eb : K.add β b = K.one := hb.2
          rw [ez, ec]
          -- the defect equals Z'·c·(β + b − 1), which vanishes since β + b = 1
          calc _ = K.add (K.mul (K.add (K.add K.one K.one) (K.add β d)) (K.add E' (K.mul β (K.add E' p))))
                (K.add (K.add (K.mul (K.add K.one K.one) s) (K.mul (K.add β d) p))
                  (K.mul (K.add E' p) (K.add (K.mul β b) (K.mul d (K.add (K.add K.one K.one) b))))) := rfl
            _ = K.add (K.mul (K.add K.one K.one) (K.add (K.add E' (K.mul (K.add (K.add β d) (K.add β d)) (K.add E' p))) s))
                (K.mul (K.mul (K.add E' p) (K.add β d)) (K.add (K.add β b) (K.neg K.one))) := by lib_ring
            _ = _ := by rw [eb]; lib_ring

theorem cfl_rate (E Z : Nat → R) (c β : R) (hβ : O.nn β) (hβc : Le K O.toCone β c) (hβ1 : Le K O.toCone β K.one)
    (hZ : ∀ k, O.nn (Z k)) (hP : ∀ k, Le K O.toCone (E k) (Z k))
    (hstep : ∀ k, Le K O.toCone (K.add (E (k + 1)) (K.mul (K.add c c) (Z (k + 1)))) (K.add (E k) (K.mul β (Z k)))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (E n) (K.mul β (Z n))))
      (K.mul (pw K (K.add K.one K.one) n) (K.add (E 0) (K.mul β (Z 0))))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := cfl_rate E Z c β hβ hβc hβ1 hZ hP hstep n
    have hc : O.nn c := TruncRate.nn_of_le K O _ _ hβ hβc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nA : O.nn (K.add (K.add K.one K.one) c) := O.nn_add _ _ two hc
    have st := rate_step K O (E n) (E (n + 1)) (Z n) (Z (n + 1)) c β hβ hβc hβ1 (hZ (n + 1)) (hP (n + 1)) (hstep n)
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one K.one) c) n) _ _ (pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (K.add K.one K.one) c)) _) K.zero = _
      lib_ring
    · show K.add (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.mul (K.add K.one K.one) (K.add (E n) (K.mul β (Z n))))) K.zero
        = K.mul (K.add K.one K.one) (K.mul (pw K (K.add (K.add K.one K.one) c) n) (K.add (E n) (K.mul β (Z n))))
      lib_ring
    · show K.add (K.mul (K.add K.one K.one) (K.mul (pw K (K.add K.one K.one) n) (K.add (E 0) (K.mul β (Z 0))))) K.zero
        = K.mul (K.mul (pw K (K.add K.one K.one) n) (K.add K.one K.one)) (K.add (E 0) (K.mul β (Z 0)))
      lib_ring

include mul_inv h10 in
theorem cfl_rate_global (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (hhν : Le K O.toCone (K.mul h ν) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n)
        (K.add (lsum K (fun r => dot3 K (a n r) (a n r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a n r) (a n r))) l))))
      (K.mul (pw K (K.add K.one K.one) n)
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have hc : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  have hE : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (a t r) (a t r)) l) Es := by
    intro t
    have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 t
    have hS : O.nn (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t) :=
      NSChain.sumN_nn K O.toCone _ (fun k => nZ (k + 1)) t
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) t))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l)),
      O.nn_add _ _ (O.nn_mul _ _ hc hS) (O.nn_mul _ _ nβ (nZ t)), ?_⟩ (le_trans' K O.toCone _ _ _ g h0)
    lib_ring
  exact cfl_rate K O (fun t => lsum K (fun r => dot3 K (a t r) (a t r)) l)
    (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) (K.mul h ν) (beta K M ι h ν Es) nβ hcfl
    (le_trans' K O.toCone _ _ _ hcfl hhν) nZ
    (fun t => poincare K O l kz h1 (a t))
    (fun t => cfl_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν (a (t + 1)) (a t) (N t) (hs t) (hneut t) (hP t) (hE t))
    n

end Gen

end CFLRate

#print axioms CFLRate.rate_step
#print axioms CFLRate.cfl_rate
#print axioms CFLRate.cfl_rate_global

#print axioms: 3 theorems, no axioms.

79.218 A good time with small enstrophy, every datum

Pigeonhole on the integrated enstrophy of cfl_global: for every \(n\) some \(t\le n\) has \((n+1)h\nu Z_{t+1}\le E_0+h\nu Z_0\), a bound with no dependence on the truncation.

ns/EventSmall.lean

/-
  ns/EventSmall.lean — T37 cycle 1317 (goal G789 — done): a good time with small enstrophy, for arbitrary data, over
  an ordered field (pigeonhole on the integrated enstrophy of CFLGlobal).
    argmin          every finite family Y_0 … Y_n has a smallest member (order totality)
    sum_ge          c ≤ Y_s for all s < n  ⇒  n·c ≤ Σ_{s<n} Y_s
    pigeon          Σ_{s<n} Y_s ≤ S  ⇒  ∃ t < n + 1 … with (n + 1)·Y_t ≤ S   (n + 1 terms)
    event_small     under the cfl_global hypotheses, for every n there is t ≤ n with
                    (n + 1)·hν·Z_{t+1} ≤ E_0 + hν·Z_0,
  a bound on the right with no dependence on the truncation (β ≤ hν): among the first n + 1 steps one has enstrophy
  at most (E_0 + hνZ_0)/((n + 1)hν) — the discrete form of Leray's "eventually small" enstrophy.

  Lean 4 core plus ns/CFLGlobal, lib/RingTac.
-/
import ns.CFLGlobal

namespace EventSmall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lt_succ_cases {s n : Nat} (h : s < n + 1) : s < n ∨ s = n :=
  Nat.lt_or_eq_of_le (Nat.le_of_lt_succ h)

theorem argmin (Y : Nat → R) : ∀ n, ∃ t, t < n + 1 ∧ ∀ s, s < n + 1 → Le K O.toCone (Y t) (Y s)
  | 0 => ⟨0, Nat.lt_succ_self 0, fun s hs => by
      cases lt_succ_cases hs with
      | inl h => exact absurd h (Nat.not_lt_zero s)
      | inr h => rw [h]; exact le_refl' K O.toCone _⟩
  | n + 1 => by
    have ⟨t, ht, hm⟩ := argmin Y n
    cases O.total (Y t) (Y (n + 1)) with
    | inl hle =>
      refine ⟨t, Nat.lt_succ_of_lt ht, fun s hs => ?_⟩
      cases lt_succ_cases hs with
      | inl h => exact hm s h
      | inr h => rw [h]; exact hle
    | inr hge =>
      refine ⟨n + 1, Nat.lt_succ_self (n + 1), fun s hs => ?_⟩
      cases lt_succ_cases hs with
      | inl h => exact le_trans' K O.toCone _ _ _ hge (hm s h)
      | inr h => rw [h]; exact le_refl' K O.toCone _

theorem sum_ge (Y : Nat → R) (c : R) : ∀ n, (∀ s, s < n → Le K O.toCone c (Y s)) →
    Le K O.toCone (K.mul (castN K n) c) (sumN K Y n)
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.zero c) K.zero = K.zero; lib_ring⟩
  | n + 1, h => by
    have ih := sum_ge Y c n (fun s hs => h s (Nat.lt_succ_of_lt hs))
    have s := le_add' K O.toCone _ _ _ _ ih (h n (Nat.lt_succ_self n))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ s
    show K.add (K.mul (K.add K.one (castN K n)) c) K.zero = K.add (K.mul (castN K n) c) c
    lib_ring

theorem pigeon (Y : Nat → R) (S : R) (n : Nat) (h : Le K O.toCone (sumN K Y (n + 1)) S) :
    ∃ t, t < n + 1 ∧ Le K O.toCone (K.mul (castN K (n + 1)) (Y t)) S := by
  have ⟨t, ht, hm⟩ := argmin K O Y n
  exact ⟨t, ht, le_trans' K O.toCone _ _ _ (sum_ge K O Y (Y t) (n + 1) hm) h⟩

include mul_inv h10 in
theorem event_small (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    ∃ t, t < n + 1 ∧ Le K O.toCone
      (K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nE : ∀ t, O.nn (lsum K (fun r => dot3 K (a t r) (a t r)) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 (n + 1)
  -- hν·Σ_{t ≤ n} Z_{t+1} ≤ V_0 ≤ E_0 + hν·Z_0
  have e1 : Le K O.toCone (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)))
      (K.add (K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1))))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l))) :=
    ⟨K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l) (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l)),
      O.nn_add _ _ (nE (n + 1)) (O.nn_mul _ _ nβ (nZ (n + 1))), by lib_ring⟩
  have e2 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l))
    (le_mul_right' K O.toCone hcfl (nZ 0))
  have tot := le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g e2)
  have sm : sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1)
      = K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)) :=
    NSChain.sumN_mul_left K _ _ (n + 1)
  rw [← sm] at tot
  exact pigeon K O _ _ n tot

end Gen

end EventSmall

#print axioms EventSmall.lt_succ_cases
#print axioms EventSmall.argmin
#print axioms EventSmall.sum_ge
#print axioms EventSmall.pigeon
#print axioms EventSmall.event_small

#print axioms: 5 theorems, no axioms.

79.219 Discrete eventual regularity for every datum

If \(c(\nu +h\nu ^2c_M)(E_0+h\nu Z_0)\le \nu ^3T\) with \(T=(n+1)h\nu \), some \(t\le n\) has \(cQ_{t+1}\le \nu ^3\) and \(Q_{t+1+s}\le Q_{t+1}\) for all \(s\); with \(h\nu c_M\le 1\) the waiting time and the bound do not depend on the truncation.

ns/EventReg.lean

/-
  ns/EventReg.lean — T37 cycle 1319 (goal G790 — done): discrete eventual regularity for arbitrary data, over an
  ordered field. Implicit Fourier–Galerkin scheme with Leray-projected, energy-neutral force, truncation
  |k|_∞ ≤ M + 1, CFL condition β ≤ hν and E_0 + β·Z_0 ≤ E* (the cfl_global hypotheses), h, ν ≠ 0, c = 135ι²,
  W = E_0 + hν·Z_0. If the time T = (n + 1)hν satisfies
      c·(ν + hν²·c_M)·W ≤ ν³·T,
  then there is t ≤ n such that, with Q = νZ + hν²A,
    event_reg    c·Q_{t+1} ≤ ν³   and   Q_{t+1+s} ≤ Q_{t+1} for every s ≥ 0.
  So after the good time the enstrophy stays below ν²/c forever. When hν·c_M ≤ 1 (a CFL-type step bound) the
  waiting time T ≥ 2c·W/ν³ and the final bound ν²/c do not depend on the truncation — Leray's eventual regularity
  for the discrete scheme.

  Lean 4 core plus ns/EventSmall, ns/EnstProj (enst_global_gen), ns/CFLStep (A_le_Z), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EventSmall
import ns.EnstProj

namespace EventReg
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem cast_succ_ne (n : Nat) : castN K (n + 1) ≠ K.zero := by
  intro e
  have e' : K.add K.one (castN K n) = K.zero := e
  exact h10 (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone n) e')

include mul_inv h10 in
theorem event_reg (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν (K.mul ν ν)) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) := by
  have ⟨t, ht, hz⟩ := event_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have nm : O.nn (K.mul (castN K (n + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have nf : O.nn (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) :=
    O.nn_add _ _ hν (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (cM_nn K O M))
  have m0 : K.mul (castN K (n + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  -- Q_{t+1} ≤ (ν + hν²c_M)·Z_{t+1}
  have hA := A_le_Z K O l kz M hM (a (t + 1))
  have hQ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))
      (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (Zs K l kz a (t + 1))) := by
    have s := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Zs K l kz a (t + 1))))
      (le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul ν (Zs K l kz a (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.mul (cM K M) (Zs K l kz a (t + 1))))) K.zero = _
    lib_ring
  -- (n+1)hν·(c·Q_{t+1}) ≤ (n+1)hν·ν³
  have k1 := le_mul_nn K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n + 1)) (K.mul h ν))) _ _
    (O.nn_mul _ _ hc nm) hQ
  have k2 := le_mul_nn K O.toCone (K.mul (castN K 135) (K.mul ι ι)) _ _ hc
    (le_mul_nn K O.toCone (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) _ _ nf hz)
  have key : Le K O.toCone (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))
      (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ k1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ k2 (le_trans' K O.toCone _ _ _ htime ⟨K.zero, O.nn_zero, ?_⟩))))
    · lib_ring
    · show K.add (K.mul (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n + 1)) (K.mul h ν)))
          (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (Zs K l kz a (t + 1)))) K.zero
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M)))
          (K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l))))
      show _ = K.mul _ (K.mul _ (K.mul _ (K.mul _ (Zs K l kz a (t + 1)))))
      lib_ring
    · lib_ring
  have small := le_of_mul_le K O _ _ _ nm m0 key
  refine ⟨t, ht, small, fun s => ?_⟩
  exact EnstProj.enst_global_gen K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + s)) (fun s => N (t + 1 + s)) (fun s => hP (t + 1 + s)) (fun s r => hs (t + 1 + s) r) small s

end Gen

end EventReg

#print axioms EventReg.cast_succ_ne
#print axioms EventReg.event_reg

#print axioms: 2 theorems, no axioms.

79.220 Eventual regularity with truncation-free constants

Under \(h\nu c_M\le 1\) and \(2c(E_0+h\nu Z_0)\le \nu ^2T\): some \(t\le n\) has \(cQ_{t+1}\le \nu ^3\) and \(Q_{t+1+s}\le Q_{t+1}\) for all \(s\); waiting time and bound are independent of the truncation.

ns/EventRegU.lean

/-
  ns/EventRegU.lean — T37 cycle 1321 (goal G791 — done): the truncation-free form of EventReg, over an ordered field.
  Under the cfl_global hypotheses, h, ν ≠ 0, the step bound hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and a time
  T = (n + 1)hν with
      2c·W ≤ ν²·T,
  there is t ≤ n with
    event_reg_uniform   c·Q_{t+1} ≤ ν³  and  Q_{t+1+s} ≤ Q_{t+1} for all s,
  where neither the waiting time 2cW/ν² nor the final bound (Z ≤ ν²/c after t + 1) depends on the truncation.

  Lean 4 core plus ns/EventReg.
-/
import ns.EventReg

namespace EventRegU
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_reg_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have nW : O.nn (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) :=
    O.nn_add _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l)
      (O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
  -- ν + hν²c_M ≤ ν + ν
  have f1 : Le K O.toCone (K.add ν (K.mul (K.mul h (K.mul ν ν)) (cM K M))) (K.add ν ν) := by
    have m := le_mul_nn K O.toCone ν _ _ hν hstepM
    refine le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone ν) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, ?_⟩))
    · show K.add (K.mul (K.mul h (K.mul ν ν)) (cM K M)) K.zero = K.mul ν (K.mul (K.mul h ν) (cM K M))
      lib_ring
    · show K.add (K.mul ν K.one) K.zero = ν
      lib_ring
  have g1 := le_mul_nn K O.toCone (K.mul (castN K 135) (K.mul ι ι)) _ _ hc (ForceSob.le_mul_right' K O.toCone f1 nW)
  have g2 := le_mul_nn K O.toCone ν _ _ hν htime
  refine event_reg K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs a N hs hneut hP hcfl h0 n
    (le_trans' K O.toCone _ _ _ g1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ g2 ⟨K.zero, O.nn_zero, ?_⟩)))
  · lib_ring
  · lib_ring

end Gen

end EventRegU

#print axioms EventRegU.event_reg_uniform

#print axioms: 1 theorem, no axioms.

79.221 Dissipated \(H^2\) mass in the small-data regime

If \(2cQ_0\le \nu ^3\) then \(2Q_{t+1}+h\nu ^2A_t\le 2Q_t\) and \(2Q_n+h\nu ^2\sum _{t<n}A_t\le 2Q_0\): the time-integrated \(H^2\) norm is bounded independently of the truncation.

ns/EnstDiss.lean

/-
  ns/EnstDiss.lean — T37 cycle 1323 (goal G792a): the dissipated H² mass in the small-data regime, over an ordered
  field. Sequences Z_t, A_t ≥ 0, ν > 0, h, c ≥ 0, Q_t = νZ_t + hν²A_t with the enstrophy step
      Q_{t+1} ≤ νZ_t + h·c·A_t·Z_t
  (EnstStep with Σ|N|² ≤ c·A·Z). If 2c·Q_0 ≤ ν³ then
    diss_step   2Q_{t+1} + hν²A_t ≤ 2Q_t
    enst_diss   2Q_n + hν²·Σ_{t<n} A_t ≤ 2Q_0,
  so Σ_t h·A_t ≤ 2Q_0/ν²: the time-integrated H² norm is bounded (by a constant independent of the truncation once
  c = 135ι²), and a pigeonhole (EventSmall.pigeon) yields a time where A is small.

  Lean 4 core plus ns/EnstGlobal (Q, q_le, le_of_mul_le), ns/RatModel, lib/RingTac.
-/
import ns.EnstGlobal
import ns.RatModel

namespace EnstDiss
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem diss_step (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A t)) := by
  have nQ0 : O.nn (Q K ν h Z A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ 0)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA 0))
  -- c·Q_0 ≤ 2c·Q_0 ≤ ν³
  have small1 : Le K O.toCone (K.mul c (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul c (Q K ν h Z A 0), O.nn_mul _ _ hc nQ0, by lib_ring⟩ small
  have hq := q_le K O ν h c Z A hν hν0 hh hc hZ hA step small1 t
  -- ν·(2c·Z_t) ≤ ν·ν²
  have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
  have h2 := le_trans' K O.toCone _ _ _
    (le_mul_nn K O.toCone (K.add c c) _ _ (O.nn_add _ _ hc hc) (le_trans' K O.toCone _ _ _ hz hq)) small
  have e : K.mul (K.add c c) (K.mul ν (Z t)) = K.mul ν (K.mul (K.add c c) (Z t)) := by lib_ring
  rw [e] at h2
  have hcz := le_of_mul_le K O ν _ _ hν hν0 h2
  -- h·A_t·(2cZ_t) ≤ h·A_t·ν²
  have h4 := le_mul_nn K O.toCone (K.mul h (A t)) _ _ (O.nn_mul _ _ hh (hA t)) hcz
  have st := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (step t))
    (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (A t)))
  refine le_trans' K O.toCone _ _ _ st ?_
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))) h4)
      ⟨K.zero, O.nn_zero, ?_⟩)
  · show K.add (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
        (K.mul (K.mul h (K.mul ν ν)) (A t))) K.zero
      = K.add (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
          (K.mul (K.mul h (A t)) (K.mul (K.add c c) (Z t)))
    lib_ring
  · show K.add (K.add (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        (K.mul (K.mul h (A t)) (K.mul ν ν))) K.zero
      = K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    lib_ring

theorem enst_diss (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ n, Le K O.toCone (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A n)) (K.mul (K.mul h (K.mul ν ν)) (sumN K A n)))
      (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.mul h (K.mul ν ν)) K.zero)) K.zero = _; lib_ring⟩
  | n + 1 => by
    have ih := enst_diss ν h c Z A hν hν0 hh hc hZ hA step small n
    have ds := diss_step K O ν h c Z A hν hν0 hh hc hZ hA step small n
    have s := le_add' K O.toCone _ _ _ _ ds (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K A n)))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s ih)
    show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.add (sumN K A n) (A n)))) K.zero
      = K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (A n)))
          (K.mul (K.mul h (K.mul ν ν)) (sumN K A n))
    lib_ring

end Gen

end EnstDiss

#print axioms EnstDiss.diss_step
#print axioms EnstDiss.enst_diss

#print axioms: 2 theorems, no axioms.

79.222 Discrete eventual \(H^2\) regularity for every datum

If \(4c(E_0+h\nu Z_0)\le \nu ^2T_1\) and \(2\cdot 1080\iota ^2\le cT_2\) (\(T_2\ge 16\)), some time \(t_2\) has \(1080\iota ^2(\nu A+h\nu ^2B)(t_2)\le \nu ^3\) and this quantity never increases afterwards; constants independent of the truncation.

ns/EventH2.lean

/-
  ns/EventH2.lean — T37 cycle 1325 (goal G792b — done): discrete eventual H² regularity for arbitrary data, over an
  ordered field. Implicit Fourier–Galerkin scheme, Leray-projected energy-neutral force (Σ|N|² ≤ Σ|Nl|² and
  Σw|N|² ≤ Σw|Nl|²), cfl_global hypotheses, step bound hν·c_M ≤ 1, h, ν, ι ≠ 0, c = 135ι², W = E_0 + hν·Z_0.
  If T_1 = (n + 1)hν and T_2 = (n_2 + 1)hν satisfy
      2·(2c·W) ≤ ν²·T_1        and        2·1080ι² ≤ c·T_2       (i.e. T_2 ≥ 16, a pure number)
  then there is a time t_2 (≤ n + 1 + n_2) with, writing Q₂ = νA + hν²B (A = Σw²|a|², B = Σw³|a|²),
    h2_eventual   1080ι²·Q₂(t_2) ≤ ν³   and   Q₂(t_2 + s) ≤ Q₂(t_2) for every s,
  so from t_2 on the H² norm stays below ν²/(1080ι²). Waiting times and bounds do not depend on the truncation.
  Pieces: alg_q (Q ≤ 2νZ from A ≤ c_M Z, hνc_M ≤ 1), alg_cancel1/2 (cancel the time factor), B_le_A (B ≤ c_M A),
  proj_step (the enstrophy step for the projected force), good_time (κ·Q_{t+1} ≤ ν³ for a general κ).

  Lean 4 core plus ns/EventRegU, ns/EnstDiss, ns/EnstH2 (h2_global, cH), ns/EnstProj.
-/
import ns.EventRegU
import ns.EnstDiss
import ns.EnstH2

namespace EventH2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

/-- νZ + hν²A ≤ 2νZ when A ≤ c_M·Z and hν·c_M ≤ 1 -/
theorem alg_q (ν h cm Z A : R) (hν : O.nn ν) (hh : O.nn h) (hZ : O.nn Z)
    (hA : Le K O.toCone A (K.mul cm Z)) (hs : Le K O.toCone (K.mul (K.mul h ν) cm) K.one) :
    Le K O.toCone (K.add (K.mul ν Z) (K.mul (K.mul h (K.mul ν ν)) A)) (K.mul (K.add K.one K.one) (K.mul ν Z)) := by
  have m1 := le_mul_nn K O.toCone (K.mul h (K.mul ν ν)) _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA
  have m2 := le_mul_right' K O.toCone hs (O.nn_mul _ _ hν hZ)
  have s := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν Z))
    (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m2))
  · refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul ν Z) (K.mul K.one (K.mul ν Z))) K.zero = _
    lib_ring
  · lib_ring

/-- m·Z ≤ W, Q ≤ 2νZ, 2κW ≤ ν²m, m ≠ 0  ⇒  κQ ≤ ν³ -/
theorem alg_cancel1 (κ ν m Z W Qv : R) (hκ : O.nn κ) (hν : O.nn ν) (hm : O.nn m) (hm0 : m ≠ K.zero)
    (hz : Le K O.toCone (K.mul m Z) W) (hQ : Le K O.toCone Qv (K.mul (K.add K.one K.one) (K.mul ν Z)))
    (ht : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul κ W)) (K.mul (K.mul ν ν) m)) :
    Le K O.toCone (K.mul κ Qv) (K.mul ν (K.mul ν ν)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have k1 := le_mul_nn K O.toCone (K.mul m κ) _ _ (O.nn_mul _ _ hm hκ) hQ
  have k2 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) (K.mul κ ν)) _ _ (O.nn_mul _ _ two (O.nn_mul _ _ hκ hν)) hz
  have k3 := le_mul_nn K O.toCone ν _ _ hν ht
  have key : Le K O.toCone (K.mul m (K.mul κ Qv)) (K.mul m (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k1
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k2
        (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k3 ⟨K.zero, O.nn_zero, ?_⟩)))))
    all_goals lib_ring
  exact le_of_mul_le K O _ _ _ hm hm0 key

/-- m·(νA) ≤ 2Q₁, 2cQ₁ ≤ ν³, 2d ≤ c·m, Q₂ ≤ 2νA, c·m ≠ 0  ⇒  d·Q₂ ≤ ν³ -/
theorem alg_cancel2 (c d m ν A Q1 Q2 : R) (hc : O.nn c) (hd : O.nn d) (hm : O.nn m) (hν : O.nn ν)
    (hcm0 : K.mul c m ≠ K.zero)
    (hP : Le K O.toCone (K.mul m (K.mul ν A)) (K.add Q1 Q1))
    (hS : Le K O.toCone (K.mul (K.add c c) Q1) (K.mul ν (K.mul ν ν)))
    (hT : Le K O.toCone (K.mul (K.add K.one K.one) d) (K.mul c m))
    (hQ : Le K O.toCone Q2 (K.mul (K.add K.one K.one) (K.mul ν A))) :
    Le K O.toCone (K.mul d Q2) (K.mul ν (K.mul ν ν)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have k1 : Le K O.toCone (K.mul c (K.mul m (K.mul ν A))) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone c _ _ hc hP)
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ hS)
  have k2 := le_mul_nn K O.toCone (K.mul (K.mul c m) d) _ _ (O.nn_mul _ _ (O.nn_mul _ _ hc hm) hd) hQ
  have k3 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) d) _ _ (O.nn_mul _ _ two hd) k1
  have k4 := le_mul_right' K O.toCone hT (O.nn_mul _ _ hν (O.nn_sq ν))
  have key : Le K O.toCone (K.mul (K.mul c m) (K.mul d Q2)) (K.mul (K.mul c m) (K.mul ν (K.mul ν ν))) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k2
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ k3
        (le_trans' K O.toCone _ _ _ k4 ⟨K.zero, O.nn_zero, ?_⟩))))
    all_goals lib_ring
  exact le_of_mul_le K O _ _ _ (O.nn_mul _ _ hc hm) hcm0 key

theorem B_le_A (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (wt K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr)) (O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

include mul_inv h10 in
/-- the enstrophy step for any force whose energy is at most that of the Galerkin force -/
theorem proj_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : O.nn (Zs K l kz a s) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) _ (hs s)
  have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
  have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l)
      (As K l kz a s) :=
    lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
  have hb : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r)) l)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) hZn)
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have t2 := le_trans' K O.toCone _ _ _ fu m2
    have e : K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (castN K 135) (Zs K l kz a s)))
        = K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
    rw [e] at t2
    exact t2
  have hb := le_trans' K O.toCone _ _ _ (hN s) hb
  have hb2 := le_mul_nn K O.toCone h _ _ hh hb
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  have e2 : K.mul h (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) (Zs K l kz a s)))
      = K.mul (K.mul h (K.mul (castN K 135) (K.mul ι ι))) (K.mul (As K l kz a s) (Zs K l kz a s)) := by lib_ring
  rw [e2] at st2
  exact st2

include mul_inv h10 in
/-- a good time for a general constant κ: κ·Q_{t+1} ≤ ν³ -/
theorem good_time (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es κ : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hκ : O.nn κ)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul κ
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧ Le K O.toCone (K.mul κ (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) := by
  have ⟨t, ht, hz⟩ := event_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n
  have nm : O.nn (K.mul (castN K (n + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m0 : K.mul (castN K (n + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have hZ : O.nn (Zs K l kz a (t + 1)) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hQ := alg_q K O ν h (cM K M) (Zs K l kz a (t + 1)) (As K l kz a (t + 1)) hν hh hZ (A_le_Z K O l kz M hM (a (t + 1))) hstepM
  refine ⟨t, ht, alg_cancel1 K O κ ν _ (Zs K l kz a (t + 1)) _ _ hκ hν nm m0 ?_ hQ htime⟩
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ hz
  show K.add (K.mul (K.mul (castN K (n + 1)) (K.mul h ν)) (Zs K l kz a (t + 1))) K.zero
    = K.mul (castN K (n + 1)) (K.mul (K.mul h ν) (Zs K l kz a (t + 1)))
  lib_ring

include mul_inv h10 in
theorem h2_eventual (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    ∃ t2, Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t2 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have c0 : K.mul (castN K 135) (K.mul ι ι) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 134 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hι0 e3
      | inr e4 => exact hι0 e4
  -- stage 1: a time t1 with 2c·Q(t1) ≤ ν³
  have ⟨t, _, hq1⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime1
  -- stage 2: dissipated H² mass after t1, then a time with small A
  have dis := EnstDiss.enst_diss K O ν h (K.mul (castN K 135) (K.mul ι ι)) (fun j => Zs K l kz a (t + 1 + j))
    (fun j => As K l kz a (t + 1 + j)) hν hν0 hh hc (fun j => hZ _) (fun j => hA _)
    (fun j => by
      have st := proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hP hs (t + 1 + j)
      exact st)
    hq1 (n2 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) j) := fun j =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))) (n2 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n2 + 1)]
    refine le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) (n2 + 1)),
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n2 hsum
  -- stage 3: the H² smallness at t2 = t + 1 + j
  have nm2 : O.nn (K.mul (castN K (n2 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m20 : K.mul (castN K (n2 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n2 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact c0 e1
    | inr e2 => exact m20 e2
  have hAt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a (t + 1 + j) r))) l
  have hQ2 := alg_q K O ν h (cM K M) (As K l kz a (t + 1 + j))
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t + 1 + j) p) (a (t + 1 + j) p))) l)
    hν hh hAt (B_le_A K O l kz M hM (a (t + 1 + j))) hstepM
  have hdd : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
    have c135 := RatModel.nn_cast K O.toCone 135
    have : O.nn (EnstH2.cH K) :=
      O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three c135)) (O.nn_mul _ _ two c135)
    exact O.nn_mul _ _ this (O.nn_sq ι)
  have hj' : Le K O.toCone (K.mul (castN K (n2 + 1)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := hj
  have hq1' : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) (K.mul ν (K.mul ν ν)) := hq1
  generalize Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0 = Q1 at hj' hq1'
  have small2 := alg_cancel2 K O (K.mul (castN K 135) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))
    (K.mul (castN K (n2 + 1)) (K.mul h ν)) ν (As K l kz a (t + 1 + j)) Q1 _
    hc hdd nm2 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj' ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hq1' htime2 hQ2
  refine ⟨t + 1 + j, small2, fun s => ?_⟩
  exact EnstH2.h2_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + j + s)) (fun s => N (t + 1 + j + s)) (fun s => hPw (t + 1 + j + s))
    (fun s r => hs (t + 1 + j + s) r) small2 s

end Gen

end EventH2

#print axioms EventH2.alg_q
#print axioms EventH2.alg_cancel1
#print axioms EventH2.alg_cancel2
#print axioms EventH2.B_le_A
#print axioms EventH2.proj_step
#print axioms EventH2.good_time
#print axioms EventH2.h2_eventual

#print axioms: 7 theorems, no axioms.

79.223 The \(H^2\) enstrophy step as a lemma

For any force with \(\sum w|N|^2\le \sum w|N_l|^2\): \(Q_2(s+1)\le \nu A_s+h\cdot 1080\iota ^2B_sA_s\), \(Q_2=\nu A+h\nu ^2B\).

ns/H2Step.lean

/-
  ns/H2Step.lean — T37 cycle 1327 (goal G793a): the H² enstrophy step of the implicit-viscosity Galerkin scheme as a
  standalone lemma, over an ordered field. With A = Σw²|a|², B = Σw³|a|², Q₂ = νA + hν²B, cH = 1080 and any force with
  Σw|N|² ≤ Σw|Nl|² (Leray projection):
    h2_step   Q₂(s + 1) ≤ νA_s + h·(cH ι²)·B_s·A_s
  (the step inside EnstH2.h2_global, exposed so that EnstDiss can bound the dissipated H³ mass).

  Lean 4 core plus ns/EnstH2.
-/
import ns.EnstH2

namespace H2Step
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound ForceH2 EnstStep EnstStepV EnstGlobal EnstH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h2_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (s + 1))
      (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l))
        (K.mul (K.mul h (K.mul (cH K) (K.mul ι ι)))
          (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l)
            (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hc : O.nn (K.mul (cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h2 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZA := Z_le_A K O kz h1 l (a s)
  have nZ : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a s q) (a s q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (hw q) (dot3_nn K O.toCone _)) l
  have nA : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) (wt K kz p)) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (hw p)) l
  have nS0 : O.nn (lsum K (fun p => K.mul (cube3 K (ivn K inv kz p)) (cube3 K (ivn K inv kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_sq _) l
  -- 2 S1 Z + 2 S0 A ≤ cH · A
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nA hS1 hZA
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nA
  have inner := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two i1) (le_mul_nn K O.toCone _ _ _ two i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold cH
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a s p) (a s p))) l = As'
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a s p) (a s p))) l = Bs
  generalize castN K 135 = c
  lib_ring

end Gen

end H2Step

#print axioms H2Step.h2_step

#print axioms: 1 theorem, no axioms.

79.224 Eventual \(H^2\) regularity with a general smallness constant

For any \(\kappa _2\ge 1080\iota ^2\) with \(2\kappa _2\le cT_2\): some \(t_2\) has \(\kappa _2Q_2(t_2)\le \nu ^3\) and \(Q_2\) never increases afterwards.

ns/EventH2K.lean

/-
  ns/EventH2K.lean — T37 cycle 1327 (goal G793a): EventH2.h2_eventual with a general smallness constant κ₂ ≥ 1080ι²:
  if 2κ₂ ≤ c·T_2 (and the T_1 condition of h2_eventual), some time t_2 has κ₂·(νA + hν²B)(t_2) ≤ ν³ and νA + hν²B
  never increases afterwards. With κ₂ = 2·1080ι² this is the input of EnstDiss for the H³ stage (G793).

  Lean 4 core plus ns/EventH2.
-/
import ns.EventH2

namespace EventH2K
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h2_eventual_k (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es) (κ2 : R) (hκ2 : O.nn κ2) (hκle : Le K O.toCone (K.mul (EnstH2.cH K) (K.mul ι ι)) κ2)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) κ2)
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    ∃ t2, Le K O.toCone (K.mul κ2
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t2 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) t2) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have c0 : K.mul (castN K 135) (K.mul ι ι) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 134 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hι0 e3
      | inr e4 => exact hι0 e4
  -- stage 1: a time t1 with 2c·Q(t1) ≤ ν³
  have ⟨t, _, hq1⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime1
  -- stage 2: dissipated H² mass after t1, then a time with small A
  have dis := EnstDiss.enst_diss K O ν h (K.mul (castN K 135) (K.mul ι ι)) (fun j => Zs K l kz a (t + 1 + j))
    (fun j => As K l kz a (t + 1 + j)) hν hν0 hh hc (fun j => hZ _) (fun j => hA _)
    (fun j => by
      have st := proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hP hs (t + 1 + j)
      exact st)
    hq1 (n2 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) j) := fun j =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))) (n2 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n2 + 1)]
    refine le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) (n2 + 1)),
      O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n2 hsum
  -- stage 3: the H² smallness at t2 = t + 1 + j
  have nm2 : O.nn (K.mul (castN K (n2 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m20 : K.mul (castN K (n2 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n2 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact c0 e1
    | inr e2 => exact m20 e2
  have hAt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq (wt K kz r)) (dot3_nn K O.toCone (a (t + 1 + j) r))) l
  have hQ2 := alg_q K O ν h (cM K M) (As K l kz a (t + 1 + j))
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t + 1 + j) p) (a (t + 1 + j) p))) l)
    hν hh hAt (B_le_A K O l kz M hM (a (t + 1 + j))) hstepM
  have hdd : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := by
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
    have c135 := RatModel.nn_cast K O.toCone 135
    have : O.nn (EnstH2.cH K) :=
      O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three c135)) (O.nn_mul _ _ two c135)
    exact O.nn_mul _ _ this (O.nn_sq ι)
  have hj' : Le K O.toCone (K.mul (castN K (n2 + 1)) (K.mul (K.mul h (K.mul ν ν)) (As K l kz a (t + 1 + j))))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) := hj
  have hq1' : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0)) (K.mul ν (K.mul ν ν)) := hq1
  generalize Q K ν h (fun j => Zs K l kz a (t + 1 + j)) (fun j => As K l kz a (t + 1 + j)) 0 = Q1 at hj' hq1'
  have small2 := alg_cancel2 K O (K.mul (castN K 135) (K.mul ι ι)) κ2
    (K.mul (castN K (n2 + 1)) (K.mul h ν)) ν (As K l kz a (t + 1 + j)) Q1 _
    hc hκ2 nm2 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj' ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hq1' htime2 hQ2
  have nQ2 : O.nn (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l)
      (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) (t + 1 + j)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν hAt) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
      (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have smallH := le_trans' K O.toCone _ _ _ (le_mul_right' K O.toCone hκle nQ2) small2
  refine ⟨t + 1 + j, small2, fun s => ?_⟩
  exact EnstH2.h2_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + j + s)) (fun s => N (t + 1 + j + s)) (fun s => hPw (t + 1 + j + s))
    (fun s r => hs (t + 1 + j + s) r) smallH s

end Gen

end EventH2K

#print axioms EventH2K.h2_eventual_k

#print axioms: 1 theorem, no axioms.

79.225 Discrete eventual \(H^3\) regularity for every datum

After the \(H^2\) stage (with \(\kappa _2=2\cdot 1080\iota ^2\)) and a pure-number waiting time \(2c_3\iota ^2\le 1080\iota ^2T_3\): some \(t_3\) has \(c_3\iota ^2(\nu \tilde B+h\nu ^2B_4)(t_3)\le \nu ^3\), never increasing afterwards; constants independent of the truncation.

ns/EventH3.lean

/-
  ns/EventH3.lean — T37 cycle 1329 (goal G793b — done): discrete eventual H³ regularity for arbitrary data, over an
  ordered field. Hypotheses of EventH2.h2_eventual (with κ₂ = 2·1080ι²) plus Σw²|N|² ≤ Σw²|Nl|² (Leray projection).
  With d₂ = 1080ι², c₃ = 8(9·135 + 135), B̃ = Σw(w·w)|a|², B₄ = Σ(w·w)(w·w)|a|², Q₃ = νB̃ + hν²B₄: if moreover
      2·c₃ι² ≤ d₂·T_3,    T_3 = (n_3 + 1)hν          (a pure-number waiting time, T_3 ≥ 2c₃/1080)
  then some time t_3 has
    event_h3   c₃ι²·Q₃(t_3) ≤ ν³   and   Q₃(t_3 + s) ≤ Q₃(t_3) for every s,
  all constants independent of the truncation. Route: EventH2K (κ₂ = 2d₂) → EnstDiss on (A, B) with H2Step.h2_step
  → pigeonhole → B₄ ≤ c_M B̃ and alg_cancel2 → EnstH3.h3_global on the shifted run.

  Lean 4 core plus ns/EventH2K, ns/H2Step, ns/EnstH3, ns/EnstDiss.
-/
import ns.EventH2K
import ns.H2Step
import ns.EnstH3

namespace EventH3
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem cH_ne : EnstH2.cH K ≠ K.zero := by
  have e : EnstH2.cH K = K.mul (K.add K.one (K.add (K.add (K.add K.one K.one) K.one) (K.add (K.add K.one K.one) (K.add K.one K.one))))
      (castN K 135) := by
    unfold EnstH2.cH; lib_ring
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  intro h0
  rw [e] at h0
  cases O.noZD _ _ h0 with
  | inl e1 =>
    exact h10 (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone)
      (O.nn_add _ _ (O.nn_add _ _ two (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ two two)) e1)
  | inr e2 => exact cast_succ_ne K O h10 134 e2

theorem B4_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (K.mul (wt K kz r) (wt K kz r))) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr))
    (O.nn_mul _ _ (O.nn_mul _ _ hwr (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)

include mul_inv h10 in
theorem event_h3 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH3.c3 K) (K.mul ι ι)))
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)))) :
    ∃ t3, Le K O.toCone (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t3 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hd2 : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  -- stage H²: a time t2 with 2d₂·Q₂(t2) ≤ ν³
  have ⟨t2, hk, _⟩ := EventH2K.h2_eventual_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (O.nn_add _ _ hd2 hd2)
    ⟨K.mul (EnstH2.cH K) (K.mul ι ι), hd2, rfl⟩ hstepM a N hs hneut hP hPw hcfl h0 n n2 htime1 htime2
  -- dissipated H³ mass after t2
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH2.cH K) (K.mul ι ι))
    (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hν0 hh hd2 (fun j => hA _) (fun j => hB _)
    (fun j => H2Step.h2_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw hs (t2 + j)) hk (n3 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
      (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)) (n3 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n3 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n3 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n3 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n3 + 1)) (K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := hj
  rw [EnstH3.Bform K kz l (a (t2 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0))
      (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H³ at t3 = t2 + j
  have nm3 : O.nn (K.mul (castN K (n3 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m30 : K.mul (castN K (n3 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n3 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact cH_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m30 e2
  have hBt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have hQ3 := alg_q K O ν h (cM K M)
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hh hBt (B4_le K O l kz M hM (a (t2 + j))) hstepM
  have small3 := alg_cancel2 K O (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))
    (K.mul (castN K (n3 + 1)) (K.mul h ν)) ν
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) Q1 _
    hd2 hc3 nm3 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime3 hQ3
  refine ⟨t2 + j, small3, fun s => ?_⟩
  exact EnstH3.h3_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t2 + j + s)) (fun s => N (t2 + j + s)) (fun s => hPw2 (t2 + j + s))
    (fun s r => hs (t2 + j + s) r) small3 s

end Gen

end EventH3

#print axioms EventH3.cH_ne
#print axioms EventH3.B4_le
#print axioms EventH3.event_h3

#print axioms: 3 theorems, no axioms.

79.226 The \(H^3\) enstrophy step as a lemma

For any force with \(\sum w^2|N|^2\le \sum w^2|N_l|^2\): \(Q_3(s+1)\le \nu \tilde B_s+h\,c_3\iota ^2B_{4,s}\tilde B_s\).

ns/H3Step.lean

/-
  ns/H3Step.lean — T37 cycle 1331 (goal G794a): the H³ enstrophy step of the implicit-viscosity Galerkin scheme as a
  standalone lemma, over an ordered field. With B̃ = Σw(w·w)|a|², B₄ = Σ(w·w)(w·w)|a|², Q₃ = νB̃ + hν²B₄,
  c₃ = 8(9·135 + 135), and any force with Σw²|N|² ≤ Σw²|Nl|²:
    h3_step   Q₃(s + 1) ≤ νB̃_s + h·(c₃ι²)·B₄_s·B̃_s
  (the step inside EnstH3.h3_global, exposed for EnstDiss at the H⁴ stage).

  Lean 4 core plus ns/EnstH3.
-/
import ns.EnstH3

namespace H3Step
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep ForceH3 Tri4 EnstStep EnstStepV EnstGlobal EnstH2 EnstH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem h3_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
        (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (s + 1))
      (K.add (K.mul ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l))
        (K.mul (K.mul h (K.mul (c3 K) (K.mul ι ι)))
          (K.mul (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l)
            (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nc3 : O.nn (c3 K) := O.nn_mul _ _ (nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (nn9 K O) n135) n135)
  have hc : O.nn (K.mul (c3 K) (K.mul ι ι)) := O.nn_mul _ _ nc3 (O.nn_sq ι)
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (fun r => K.mul (wt K kz r) (wt K kz r)) hw (fun r => O.nn_sq _)
    (a (s + 1)) (a s) (N s) (hs s)
  have fh := force_h3 K O inv mul_inv h10 l kz hd h1 ι (a s)
  have hS0 := S0h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hS1 := S1h_le K O inv mul_inv h10 l kz hd h1 M hM
  have hZB := Z_le_B K O kz h1 l (a s)
  rw [Bform K kz l (a s)] at hZB fh
  have nB : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nB4 : O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have nS1 : O.nn (lsum K (fun p => K.mul (K.mul (p4 K (ivn K inv kz p)) (p4 K (ivn K inv kz p))) (K.mul (wt K kz p) (wt K kz p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) l
  have i1 := TruncRate.mm K O _ _ _ _ nS1 nB hS1 hZB
  have i2 := ForceSob.le_mul_right' K O.toCone hS0 nB
  have inner := le_mul_nn K O.toCone _ _ _ (nn8 K O) (le_add' K O.toCone _ _ _ _ i1 i2)
  have b1 := le_trans' K O.toCone _ _ _ (hN s) fh
  have b2 := le_trans' K O.toCone _ _ _ b1 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) (le_mul_nn K O.toCone _ _ _ nB4 inner))
  have hb := le_mul_nn K O.toCone h _ _ hh b2
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  unfold c3
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = Bs
  generalize lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a s p) (a s p))) l = B4
  generalize castN K 135 = c
  generalize k8 K = e8
  generalize k9 K = e9
  lib_ring

end Gen

end H3Step

#print axioms H3Step.h3_step

#print axioms: 1 theorem, no axioms.

79.227 Eventual \(H^3\) regularity with a general smallness constant

For any \(\kappa _3\ge c_3\iota ^2\) with \(2\kappa _3\le 1080\iota ^2T_3\): some \(t_3\) has \(\kappa _3Q_3(t_3)\le \nu ^3\), never increasing afterwards.

ns/EventH3K.lean

/-
  ns/EventH3K.lean — T37 cycle 1331 (goal G794a): EventH3.event_h3 with a general smallness constant κ₃ ≥ c₃ι²:
  if 2κ₃ ≤ 1080ι²·T_3 (and the T_1, T_2 conditions of event_h3), some time t_3 has κ₃·(νB̃ + hν²B₄)(t_3) ≤ ν³ and
  νB̃ + hν²B₄ never increases afterwards. With κ₃ = 2c₃ι² this feeds EnstDiss at the H⁴ stage.

  Lean 4 core plus ns/EventH3.
-/
import ns.EventH3

namespace EventH3K
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2 EventH3

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_h3_k (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es) (κ3 : R) (hκ3 : O.nn κ3) (hκle : Le K O.toCone (K.mul (EnstH3.c3 K) (K.mul ι ι)) κ3)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) κ3)
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)))) :
    ∃ t3, Le K O.toCone (K.mul κ3
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t3 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) t3) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have hd2 : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l
  -- stage H²: a time t2 with 2d₂·Q₂(t2) ≤ ν³
  have ⟨t2, hk, _⟩ := EventH2K.h2_eventual_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (O.nn_add _ _ hd2 hd2)
    ⟨K.mul (EnstH2.cH K) (K.mul ι ι), hd2, rfl⟩ hstepM a N hs hneut hP hPw hcfl h0 n n2 htime1 htime2
  -- dissipated H³ mass after t2
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH2.cH K) (K.mul ι ι))
    (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hν0 hh hd2 (fun j => hA _) (fun j => hB _)
    (fun j => H2Step.h2_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw hs (t2 + j)) hk (n3 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
      (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)) (n3 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n3 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n3 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n3 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n3 + 1)) (K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0)) := hj
  rw [EnstH3.Bform K kz l (a (t2 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
        (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0))
      (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H³ at t3 = t2 + j
  have nm3 : O.nn (K.mul (castN K (n3 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m30 : K.mul (castN K (n3 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n3 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact cH_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m30 e2
  have hBt := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have hQ3 := alg_q K O ν h (cM K M)
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l)
    hν hh hBt (B4_le K O l kz M hM (a (t2 + j))) hstepM
  have small3 := alg_cancel2 K O (K.mul (EnstH2.cH K) (K.mul ι ι)) κ3
    (K.mul (castN K (n3 + 1)) (K.mul h ν)) ν
    (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t2 + j) p) (a (t2 + j) p))) l) Q1 _
    hd2 hκ3 nm3 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime3 hQ3
  have nB4 := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq (wt K kz r)) (O.nn_sq (wt K kz r))) (dot3_nn K O.toCone (a (t2 + j) r))) l
  have nQ3 : O.nn (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l)
      (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) (t2 + j)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν hBt) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) nB4)
  have smallH := le_trans' K O.toCone _ _ _ (le_mul_right' K O.toCone hκle nQ3) small3
  refine ⟨t2 + j, small3, fun s => ?_⟩
  exact EnstH3.h3_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t2 + j + s)) (fun s => N (t2 + j + s)) (fun s => hPw2 (t2 + j + s))
    (fun s r => hs (t2 + j + s) r) smallH s

end Gen

end EventH3K

#print axioms EventH3K.event_h3_k

#print axioms: 1 theorem, no axioms.

79.228 Discrete eventual \(H^4\) regularity for every datum

After the \(H^3\) stage (with \(\kappa _3=2c_3\iota ^2\)) and a pure-number waiting time \(2c_4\iota ^2\le c_3\iota ^2T_4\): some \(t_4\) has \(c_4\iota ^2(\nu B_4'+h\nu ^2B_5)(t_4)\le \nu ^3\), never increasing afterwards; constants independent of the truncation.

ns/EventH4.lean

/-
  ns/EventH4.lean — T37 cycle 1333 (goal G794 — done): discrete eventual H⁴ regularity for arbitrary data, over an
  ordered field. Hypotheses of EventH3.event_h3 (with κ₃ = 2c₃ι²) plus Σw3|N|² ≤ Σw3|Nl|² (w3 = w³, Leray
  projection). With c₄ = 32(27·135 + 135), B₄' = Σw·w3|a|², B₅ = Σ(w·w)w3|a|², Q₄ = νB₄' + hν²B₅: if moreover
      2·c₄ι² ≤ c₃ι²·T_4,    T_4 = (n_4 + 1)hν          (a pure-number waiting time)
  then some time t_4 has
    event_h4   c₄ι²·Q₄(t_4) ≤ ν³   and   Q₄(t_4 + s) ≤ Q₄(t_4) for every s,
  all constants independent of the truncation. Pieces: c3_ne (c₃ ≠ 0), B4form ((w·w)(w·w) = w·w3), B5_le (B₅ ≤ c_M B₄').

  Lean 4 core plus ns/EventH3K, ns/H3Step, ns/EnstH4.
-/
import ns.EventH3K
import ns.H3Step
import ns.EnstH4

namespace EventH4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2 ForceH4

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include O h10 in
theorem c3_ne : EnstH3.c3 K ≠ K.zero := by
  have e : EnstH3.c3 K = K.mul (K.add (Tri4.k8 K) (K.mul (Tri4.k8 K) (EnstH3.k9 K))) (castN K 135) := by
    unfold EnstH3.c3; lib_ring
  have one := RatModel.nn_one' K O.toCone
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ one one
  have four : O.nn (K.add (K.add K.one K.one) (K.add K.one K.one)) := O.nn_add _ _ two two
  intro h0
  rw [e] at h0
  cases O.noZD _ _ h0 with
  | inl e1 =>
    have k0 := nn_sum_zero K O _ _ (EnstH3.nn8 K O) (O.nn_mul _ _ (EnstH3.nn8 K O) (EnstH3.nn9 K O)) e1
    have f0 : K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add (K.add K.one K.one) (K.add K.one K.one)) = K.zero := k0
    have g0 := nn_sum_zero K O _ _ four four f0
    have t0 := nn_sum_zero K O _ _ two two g0
    exact h10 (nn_sum_zero K O _ _ one one t0)
  | inr e2 => exact cast_succ_ne K O h10 134 e2

theorem B4form (kz : I → Mode) (l : List I) (a : I → V3 R) :
    lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a p) (a p))) l
      = lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a p) (a p))) l :=
  lsum_ext K _ _ (fun p => by unfold w3; lib_ring) l

theorem B5_le (l : List I) (kz : I → Mode) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (a : I → V3 R) :
    Le K O.toCone (lsum K (fun r => K.mul (K.mul (K.mul (wt K kz r) (wt K kz r)) (w3 K kz r)) (dot3 K (a r) (a r))) l)
      (K.mul (cM K M) (lsum K (fun r => K.mul (K.mul (wt K kz r) (w3 K kz r)) (dot3 K (a r) (a r))) l)) := by
  rw [← lsum_mul_left]
  apply lsum_le_mem K O
  intro r hr
  have hwr : O.nn (wt K kz r) := dot3_nn K O.toCone _
  have m := ForceSob.le_mul_right' K O.toCone (wt_le K O kz M r (hM r hr))
    (O.nn_mul _ _ (O.nn_mul _ _ hwr (O.nn_mul _ _ (O.nn_sq (wt K kz r)) hwr)) (dot3_nn K O.toCone (a r)))
  exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by unfold w3; lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by unfold w3; lib_ring⟩)

include mul_inv h10 in
theorem event_h4 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hι0 : ι ≠ K.zero)
    (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hPw : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw2 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hPw3 : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (w3 K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n n2 n3 n4 : Nat)
    (htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))))
      (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K (n2 + 1)) (K.mul h ν))))
    (htime3 : Le K O.toCone (K.mul (K.add K.one K.one) (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))))
      (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (castN K (n3 + 1)) (K.mul h ν))))
    (htime4 : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (EnstH4.c4 K) (K.mul ι ι)))
      (K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (castN K (n4 + 1)) (K.mul h ν)))) :
    ∃ t4, Le K O.toCone (K.mul (K.mul (EnstH4.c4 K) (K.mul ι ι))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t4))
        (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) (t4 + s))
        (Q K ν h (fun t => lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a t p) (a t p))) l)
          (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a t p) (a t p))) l) t4) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hc3 : O.nn (K.mul (EnstH3.c3 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (EnstH3.nn8 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH3.nn9 K O) n135) n135)) (O.nn_sq ι)
  have hc4 : O.nn (K.mul (EnstH4.c4 K) (K.mul ι ι)) :=
    O.nn_mul _ _ (O.nn_mul _ _ (ForceH4.nn32 K O) (O.nn_add _ _ (O.nn_mul _ _ (EnstH4.nn27 K O) n135) n135)) (O.nn_sq ι)
  have hA : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (hw p) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  have hB : ∀ t, O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a t p) (a t p))) l) := fun t =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (dot3_nn K O.toCone _)) l
  -- stage H³: a time t3 with 2c₃ι²·Q₃(t3) ≤ ν³
  have ⟨t3, hk, _⟩ := EventH3K.event_h3_k K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hι0 hEs
    (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι))) (O.nn_add _ _ hc3 hc3)
    ⟨K.mul (EnstH3.c3 K) (K.mul ι ι), hc3, rfl⟩ hstepM a N hs hneut hP hPw hPw2 hcfl h0 n n2 n3 htime1 htime2 htime3
  -- dissipated H⁴ mass after t3
  have dis := EnstDiss.enst_diss K O ν h (K.mul (EnstH3.c3 K) (K.mul ι ι)) (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)
    hν hν0 hh hc3 (fun j => hA _) (fun j => hB _)
    (fun j => H3Step.h3_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hPw2 hs (t3 + j)) hk (n4 + 1)
  have nQ : ∀ j, O.nn (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) j) :=
    fun j => O.nn_add _ _ (O.nn_mul _ _ hν (hA _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hB _))
  have hsum : Le K O.toCone (sumN K (fun j => K.mul (K.mul h (K.mul ν ν))
        (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)) (n4 + 1))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) := by
    rw [NSChain.sumN_mul_left K _ _ (n4 + 1)]
    exact le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h _ _ (n4 + 1)), O.nn_mul _ _ two (nQ _), by lib_ring⟩ dis
  have ⟨j, _, hj⟩ := pigeon K O _ _ n4 hsum
  have hj0 : Le K O.toCone (K.mul (castN K (n4 + 1)) (K.mul (K.mul h (K.mul ν ν)) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l)))
      (K.mul (K.add K.one K.one) (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) := hj
  rw [B4form K kz l (a (t3 + j))] at hj0
  have hk' : Le K O.toCone (K.mul (K.add (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH3.c3 K) (K.mul ι ι)))
      (Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0)) (K.mul ν (K.mul ν ν)) := hk
  generalize Q K ν h (fun j => lsum K (fun p => K.mul (K.mul (wt K kz p) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (fun j => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (K.mul (wt K kz p) (wt K kz p))) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) 0 = Q1 at hj0 hk'
  -- stage H⁴ at t4 = t3 + j
  have nm4 : O.nn (K.mul (castN K (n4 + 1)) (K.mul h ν)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_mul _ _ hh hν)
  have m40 : K.mul (castN K (n4 + 1)) (K.mul h ν) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 => exact cast_succ_ne K O h10 n4 e1
    | inr e2 =>
      cases O.noZD _ _ e2 with
      | inl e3 => exact hh0 e3
      | inr e4 => exact hν0 e4
  have cm0 : K.mul (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (castN K (n4 + 1)) (K.mul h ν)) ≠ K.zero := by
    intro e
    cases O.noZD _ _ e with
    | inl e1 =>
      cases O.noZD _ _ e1 with
      | inl e2 => exact c3_ne K O h10 e2
      | inr e3 =>
        cases O.noZD _ _ e3 with
        | inl e4 => exact hι0 e4
        | inr e5 => exact hι0 e5
    | inr e2 => exact m40 e2
  have hBt : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_mul _ _ (hw r) (O.nn_mul _ _ (O.nn_sq _) (hw r))) (dot3_nn K O.toCone _)) l
  have hQ4 := alg_q K O ν h (cM K M) (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) hν hh hBt (B5_le K O l kz M hM (a (t3 + j))) hstepM
  have small4 := alg_cancel2 K O (K.mul (EnstH3.c3 K) (K.mul ι ι)) (K.mul (EnstH4.c4 K) (K.mul ι ι))
    (K.mul (castN K (n4 + 1)) (K.mul h ν)) ν (lsum K (fun p => K.mul (K.mul (wt K kz p) (w3 K kz p)) (dot3 K (a (t3 + j) p) (a (t3 + j) p))) l) Q1 _
    hc3 hc4 nm4 hν cm0
    (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hj0 ⟨K.zero, O.nn_zero, by lib_ring⟩))
    hk' htime4 hQ4
  refine ⟨t3 + j, small4, fun s => ?_⟩
  exact EnstH4.h4_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t3 + j + s)) (fun s => N (t3 + j + s)) (fun s => hPw3 (t3 + j + s))
    (fun s r => hs (t3 + j + s) r) small4 s

end Gen

end EventH4

#print axioms EventH4.c3_ne
#print axioms EventH4.B4form
#print axioms EventH4.B5_le
#print axioms EventH4.event_h4

#print axioms: 4 theorems, no axioms.

79.229 Stability form of the step-size comparison

Without \(Q_e(a_0,b_0)=0\): \(Q_e(a_n,b_{2n})\le g^n(Q_e(a_0,b_0)+n(1+k)k^2C)\), \(g=(1+k)G\ge 1\); nearby starting states stay nearby on bounded time intervals, constants independent of the truncation.

ns/StepStabE.lean

/-
  ns/StepStabE.lean — T37 cycle 1335 (goal G795a): stability form of the step-size comparison, over an ordered field.
    err_bound0   the split recursion without x₀ = 0:  x_n ≤ gⁿ·(x_0 + n·(1+k)k²C),  g = (1+k)G ≥ 1
    step_stab    StepCauchy.step_cauchy without Qe(a_0, b_0) = 0:
                   Qe(a_n, b_{2n}) ≤ gⁿ·(Qe(a_0, b_0) + n·(1+k)k²C)
  so two runs of steps 2k and k that start from nearby states stay nearby on bounded time intervals, with constants
  independent of the truncation. With EventH4 (all H¹…H⁴ small after a truncation-free time) this is the form of the
  convergence chain usable after the eventual-regularity time.

  Lean 4 core plus ns/StepCauchy, ns/ErrRec.
-/
import ns.StepCauchy

namespace StepStabE
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz SplitK ErrRec LipG StepCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem err_bound0 (k G C : R) (x L Δ : Nat → R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hC : O.nn C)
    (hg : Le K O.toCone K.one (K.mul (K.add K.one k) G))
    (hsp : ∀ n, Le K O.toCone (K.mul k (x (n + 1))) (K.add (K.mul (K.mul k (K.add K.one k)) (L n)) (K.mul (K.add K.one k) (Δ n))))
    (hL : ∀ n, Le K O.toCone (L n) (K.mul G (x n))) (hΔ : ∀ n, Le K O.toCone (Δ n) (K.mul (K.mul k k) (K.mul k C))) (n : Nat) :
    Le K O.toCone (x n) (K.mul (pwr K (K.mul (K.add K.one k) G) n)
      (K.add (x 0) (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))))) := by
  have n1k : O.nn (K.add K.one k) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) hk
  have step : ∀ n, Le K O.toCone (x (n + 1)) (K.add (K.mul (K.mul (K.add K.one k) G) (x n)) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))) := by
    intro n
    have r := rec_split K O k G C (x n) (x (n + 1)) (L n) (Δ n) hk (hsp n) (hL n) (hΔ n)
    refine EnstGlobal.le_of_mul_le K O k _ _ hk hk0 (le_trans' K O.toCone _ _ _ r ⟨K.zero, O.nn_zero, ?_⟩)
    lib_ring
  exact rec_src K O _ _ x hg (O.nn_mul _ _ n1k (O.nn_mul _ _ (O.nn_sq k) hC)) step n

include mul_inv h10 in
theorem step_stab (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι k ν iν α C : R) (hk : O.nn k) (hk0 : k ≠ K.zero) (hν : O.nn ν) (hiν : K.mul ν iν = K.one) (hα : O.nn α) (hC : O.nn C)
    (a b Nb : Nat → I → V3 R) (c Nc : Nat → Nat → I → V3 R)
    (hc0 : ∀ n, c n 0 = a n)
    (hcs : ∀ n t r, Step K k ν (wt K kz r) (c n (t + 1) r) (c n t r) (Nc n t r))
    (hbs : ∀ t r, Step K k ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r))
    (hN : ∀ n t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Nc n t r) (Nb (2 * n + t) r)) (sub3 K (Nc n t r) (Nb (2 * n + t) r))) l)
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (c n t) (c n t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b (2 * n + t)) (b (2 * n + t)) r))) l))
    (hβ : ∀ n t, Le K O.toCone (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz (c n) t))))
      (K.mul ν ν))
    (hαle : ∀ n t, Le K O.toCone (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) t) α)
    (hΔ : ∀ n, Le K O.toCone (Qe K ν k l kz (a (n + 1)) (c n 2)) (K.mul (K.mul k k) (K.mul k C)))
    (n : Nat) :
    Le K O.toCone (Qe K ν k l kz (a n) (b (2 * n)))
      (K.mul (pwr K (K.mul (K.add K.one k) (GG K ν iν k α)) n)
        (K.add (Qe K ν k l kz (a 0) (b 0)) (K.mul (castN K n) (K.mul (K.add K.one k) (K.mul (K.mul k k) C))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQn : ∀ u v, O.nn (Qe K ν k l kz u v) := fun u v =>
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν)) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
  have hL : ∀ n, Le K O.toCone (Qe K ν k l kz (c n 2) (b (2 * n + 2))) (K.mul (GG K ν iν k α) (Qe K ν k l kz (a n) (b (2 * n)))) := by
    intro n
    have lp := lipschitz K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι k ν hk hν (c n) (fun s => b (2 * n + s)) (Nc n)
      (fun s => Nb (2 * n + s)) (hN n) (hcs n) (fun t r => hbs (2 * n + t) r) (hβ n) 2
    have lp' : Le K O.toCone (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2))))
        (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0)))
          (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))) (Qe K ν k l kz (a n) (b (2 * n)))) := by
      rw [← hc0 n]
      refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ lp ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν ν) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))) K.zero
          = K.mul (K.mul ν (K.mul ν K.one)) (Qe K ν k l kz (c n 2) (b (2 * n + 2)))
        lib_ring
      · show K.add (K.mul (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1)))
            (K.mul (K.add ν (K.mul k (alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0))) K.one))
            (Qe K ν k l kz (c n 0) (b (2 * n + 0)))) K.zero = _
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 0 = x0
        generalize alpha K inv ι Rc l kz (c n) (fun s => b (2 * n + s)) 1 = x1
        generalize Qe K ν k l kz (c n 0) (b (2 * n + 0)) = q
        lib_ring
    exact lip_G K O ν iν k α _ _ _ _ hν hiν hk (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 0)
      (alpha_nn K O inv mul_inv h10 ι Rc l kz _ _ 1) (hαle n 0) (hαle n 1) (hQn _ _) lp'
  have gg := (G_ge_one K O ν iν k α hν hiν hk hα).2
  exact err_bound0 K O k (GG K ν iν k α) C (fun n => Qe K ν k l kz (a n) (b (2 * n)))
    (fun n => Qe K ν k l kz (c n 2) (b (2 * n + 2))) (fun n => Qe K ν k l kz (a (n + 1)) (c n 2)) hk hk0 hC gg
    (fun n => split_Qe K O ν k hν hk l kz (a (n + 1)) (c n 2) (b (2 * n + 2))) hL hΔ n

end Gen

end StepStabE

#print axioms StepStabE.err_bound0
#print axioms StepStabE.step_stab

#print axioms: 2 theorems, no axioms.

79.230 Exponential enstrophy decay in the small-data regime

If \(2cQ_0\le \nu ^3\) and \(Z\le A\): \((2+2h\nu )Q_{t+1}\le (2+h\nu )Q_t\), hence \((2+2h\nu )^nQ_n\le (2+h\nu )^nQ_0\), for the projected Galerkin scheme with constants independent of the truncation.

ns/EnstExp.lean

/-
  ns/EnstExp.lean — T37 cycle 1337 (goal G796 — done): exponential decay of the enstrophy functional in the
  small-data regime, over an ordered field. With Q_t = νZ_t + hν²A_t, the step Q_{t+1} ≤ νZ_t + h·c·A_t·Z_t,
  2c·Q_0 ≤ ν³ and Z_t ≤ A_t (|k| ≥ 1):
    exp_step    (2 + 2hν)·Q_{t+1} ≤ (2 + hν)·Q_t        (witness hν²(A − Z) + (1 + hν)·s from EnstDiss.diss_step)
    enst_exp    (2 + 2hν)ⁿ·Q_n ≤ (2 + hν)ⁿ·Q_0
    proj_exp    the same for the implicit Fourier–Galerkin scheme with Leray-projected force (EventH2.proj_step,
                EnstH2.Z_le_A), c = 135ι², constants independent of the truncation.
  So after the EventRegU time (where 2cQ ≤ ν³ can be reached by doubling the waiting time) the enstrophy decays at a
  rate ≈ ν/2 per unit time.

  Lean 4 core plus ns/EnstDiss, ns/EventH2 (proj_step), ns/EnstH2 (Z_le_A), ns/CFLDecay (pw_nn).
-/
import ns.EnstDiss
import ns.EventH2
import ns.CFLDecay

namespace EnstExp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem exp_step (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) (t : Nat) :
    Le K O.toCone (K.mul (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) (Q K ν h Z A (t + 1)))
      (K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (Q K ν h Z A t)) := by
  have ds := EnstDiss.diss_step K O ν h c Z A hν hν0 hh hc hZ hA step small t
  have hhν : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
  cases ds with
  | intro s hs =>
    cases hZA t with
    | intro p hp =>
      refine ⟨K.add (K.mul (K.mul h (K.mul ν ν)) p) (K.mul (K.add K.one (K.mul h ν)) s),
        O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hp.1)
          (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) hhν) hs.1), ?_⟩
      have eA : A t = K.add (Z t) p := hp.2.symm
      -- (1 + hν)·(2Q' + hν²A + s) = (1 + hν)·2Q
      have e2 := congrArg (K.mul (K.add K.one (K.mul h ν))) hs.2
      generalize Q K ν h Z A (t + 1) = Q' at e2 ⊢
      show _ = K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
      have e3 : K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (Q K ν h Z A t))
          = K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))) := rfl
      rw [e3, eA] at e2
      rw [eA]
      calc _ = K.add (K.mul (K.add K.one (K.mul h ν)) (K.add (K.add (K.mul (K.add K.one K.one) Q') (K.mul (K.mul h (K.mul ν ν)) (K.add (Z t) p))) s)) (K.neg (K.add (K.mul (K.mul h (K.mul ν ν)) (Z t)) (K.mul (K.mul (K.mul h h) (K.mul ν (K.mul ν ν))) (K.add (Z t) p)))) := by lib_ring
        _ = K.add (K.mul (K.add K.one (K.mul h ν)) (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (K.add (Z t) p))))) (K.neg (K.add (K.mul (K.mul h (K.mul ν ν)) (Z t)) (K.mul (K.mul (K.mul h h) (K.mul ν (K.mul ν ν))) (K.add (Z t) p)))) := by rw [e2]
        _ = _ := by lib_ring

theorem enst_exp (ν h c : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h) (hc : O.nn c)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hZA : ∀ t, Le K O.toCone (Z t) (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1)) (K.add (K.mul ν (Z t)) (K.mul (K.mul h c) (K.mul (A t) (Z t)))))
    (small : Le K O.toCone (K.mul (K.add c c) (Q K ν h Z A 0)) (K.mul ν (K.mul ν ν))) :
    ∀ n, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) (Q K ν h Z A n))
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h Z A 0))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one _; lib_ring⟩
  | n + 1 => by
    have ih := enst_exp ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have hhν : O.nn (K.mul h ν) := O.nn_mul _ _ hh hν
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nA : O.nn (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) := O.nn_add _ _ two (O.nn_add _ _ hhν hhν)
    have nB : O.nn (K.add (K.add K.one K.one) (K.mul h ν)) := O.nn_add _ _ two hhν
    have st := exp_step K O ν h c Z A hν hν0 hh hc hZ hA hZA step small n
    have m1 := le_mul_nn K O.toCone (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) _ _
      (CFLDecay.pw_nn K O _ nA n) st
    have m2 := le_mul_nn K O.toCone (K.add (K.add K.one K.one) (K.mul h ν)) _ _ nB ih
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ m1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.mul (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n)
          (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν)))) (Q K ν h Z A (n + 1))) K.zero = _
      generalize Q K ν h Z A (n + 1) = q
      generalize pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n = P
      lib_ring
    · generalize Q K ν h Z A n = q
      generalize pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n = P
      lib_ring
    · show K.add (K.mul (K.add (K.add K.one K.one) (K.mul h ν)) (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h Z A 0))) K.zero
        = K.mul (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (K.add (K.add K.one K.one) (K.mul h ν))) (Q K ν h Z A 0)
      generalize Q K ν h Z A 0 = q
      generalize pw K (K.add (K.add K.one K.one) (K.mul h ν)) n = P
      lib_ring

include mul_inv h10 in
theorem proj_exp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν))) (n : Nat) :
    Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) n) (Q K ν h (Zs K l kz a) (As K l kz a) n))
      (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) n) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  exact enst_exp K O ν h (K.mul (castN K 135) (K.mul ι ι)) (Zs K l kz a) (As K l kz a) hν hν0 hh
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι))
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
    (fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l)
    (fun t => EnstH2.Z_le_A K O kz h1 l (a t))
    (fun t => EventH2.proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hN hs t) small n

end Gen

end EnstExp

#print axioms EnstExp.exp_step
#print axioms EnstExp.enst_exp
#print axioms EnstExp.proj_exp

#print axioms: 3 theorems, no axioms.

79.231 Eventual exponential enstrophy decay for every datum

If \(4c(E_0+h\nu Z_0)\le \nu ^2T\): some \(t\le n\) has \(2cQ_{t+1}\le \nu ^3\) and \((2+2h\nu )^sQ_{t+1+s}\le (2+h\nu )^sQ_{t+1}\) for all \(s\); the waiting time does not depend on the truncation.

ns/EventDecay.lean

/-
  ns/EventDecay.lean — T37 cycle 1341 (goal G799 — done): eventual exponential enstrophy decay for arbitrary data,
  over an ordered field. Under the cfl_global hypotheses, h, ν ≠ 0, hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and
  2·(2c·W) ≤ ν²·T, T = (n + 1)hν, there is t ≤ n with
    event_decay   2c·Q_{t+1} ≤ ν³   and   (2 + 2hν)ˢ·Q_{t+1+s} ≤ (2 + hν)ˢ·Q_{t+1} for every s,
  Q = νZ + hν²A: after a waiting time independent of the truncation the enstrophy decays geometrically, rate ≈ ν/2.

  Lean 4 core plus ns/EventH2 (good_time), ns/EnstExp (proj_exp).
-/
import ns.EventH2
import ns.EnstExp

namespace EventDecay
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_decay (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) s)
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)))
        (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) s) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have ⟨t, ht, hq⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  refine ⟨t, ht, hq, fun s => ?_⟩
  exact EnstExp.proj_exp K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun s => a (t + 1 + s)) (fun s => N (t + 1 + s)) (fun s => hP (t + 1 + s)) (fun s r => hs (t + 1 + s) r) hq s

end Gen

end EventDecay

#print axioms EventDecay.event_decay

#print axioms: 1 theorem, no axioms.

79.232 Global enstrophy bound under an energy–enstrophy condition

Local bound \(Q_t\le 2Q_0\) on \(t\le n+1\) (local_time) plus a good time inside that window (event_reg_uniform) give \(Q_T\le 2Q_0\) for every \(T\); the windows match when \(8c\,c_1WQ_0\le \nu ^5\) (energy \(\times \) enstrophy small), uniformly in the truncation.

ns/GlobalEZ.lean

/-
  ns/GlobalEZ.lean — T37 cycle 1343 (goal G800 — done): a global, truncation-uniform enstrophy bound for large
  enstrophy and small energy·enstrophy, over an ordered field. Combine
    EnstLocalTime.local_time   Q_t ≤ 2Q_0 for t ≤ n_L, where n_L·h·c₁Zb² ≤ Q_0, νZb = 2Q_0
    EventRegU.event_reg_uniform  a good time t + 1 ≤ n + 1 with Q_{t+1+s} ≤ Q_{t+1} for all s, once 2cW ≤ ν²(n+1)hν
  with the windows matched (n_L = n + 1):
    global_ez   Q_T ≤ 2Q_0 for every T.
  The two window conditions are compatible when 2cW/ν³ ≤ Q_0/(c₁Zb²) = ν²/(4c₁Q_0), i.e. 8c·c₁·W·Q_0 ≤ ν⁵ — a
  smallness condition on energy × enstrophy (Leray's product form) rather than on the enstrophy alone; the bound
  2Q_0 is independent of the truncation.

  Lean 4 core plus ns/EventRegU, ns/EnstLocalTime, ns/DyadicModelZ (le_dest).
-/
import ns.EventRegU
import ns.EnstLocalTime

namespace GlobalEZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  have ⟨t, ht, _, hmono⟩ := EventRegU.event_reg_uniform K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs
    hstepM a N hs hneut hP hcfl h0 n htime
  have loc := fun T (hT : T ≤ n + 1) => EnstLocalTime.local_time K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb
    hh hν hν0 hZb a N hP hs choice hZ0 (n + 1) hn T hT
  have ht1 : t + 1 ≤ n + 1 := Nat.succ_le_of_lt ht
  cases Nat.lt_or_ge T (t + 1) with
  | inl hlt => exact (loc T (Nat.le_trans (Nat.le_of_lt_succ hlt) (Nat.le_succ_of_le (Nat.le_of_lt_succ ht)))).1
  | inr hge =>
    have ⟨s, hsT⟩ := DyadicModelZ.le_dest hge
    rw [← hsT]
    exact le_trans' K O.toCone _ _ _ (hmono s) (loc (t + 1) ht1).1

end Gen

end GlobalEZ

#print axioms GlobalEZ.global_ez

#print axioms: 1 theorem, no axioms.

79.233 Global enstrophy bound under a single product condition

With \(X=c_1Z_b^2\) and \(\tau =(n+1)h\), \(\tau X=Q_0\): if \(2cWX\le \nu ^3Q_0\) (i.e. \(8c\,c_1WQ_0\le \nu ^5\)) then \(Q_T\le 2Q_0\) for every \(T\), uniformly in the truncation.

ns/GlobalEZP.lean

/-
  ns/GlobalEZP.lean — T37 cycle 1345 (goal G801 — done): GlobalEZ under a single product condition, over an ordered
  field. With X = c₁·Zb² (c₁ = cc(ι, C1), νZb = 2Q_0), c = 135ι², W = E_0 + hν·Z_0, choose the time
  τ = (n + 1)h with τ·X = Q_0 (τ = Q_0/X). If
      2c·W·X ≤ ν³·Q_0          (energy × enstrophy small: with Zb = 2Q_0/ν this is 8c·c₁·W·Q_0 ≤ ν⁵)
  then both window conditions of GlobalEZ hold and
    global_ez_prod   Q_T ≤ 2Q_0 for every T,
  uniformly in the truncation.

  Lean 4 core plus ns/GlobalEZ.
-/
import ns.GlobalEZ

namespace GlobalEZP
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_prod (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb τ : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (hX : O.nn (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))
    (hX0 : K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb) ≠ K.zero)
    (n : Nat) (hτ : K.mul (castN K (n + 1)) h = τ)
    (hτX : K.mul τ (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)) = Q K ν h (Zs K l kz a) (As K l kz a) 0)
    (hprod : Le K O.toCone
      (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
        (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))
      (K.mul (K.mul ν (K.mul ν ν)) (Q K ν h (Zs K l kz a) (As K l kz a) 0))) (T : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) := by
  generalize hXd : K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb) = X at hX hX0 hτX hprod
  have hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h X)) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
    ⟨K.zero, O.nn_zero, by rw [← hτX, ← hτ]; lib_ring⟩
  have htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))) := by
    refine EnstGlobal.le_of_mul_le K O X _ _ hX hX0 ?_
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ hprod ⟨K.zero, O.nn_zero, ?_⟩)
    rw [← hτX, ← hτ]; lib_ring
  rw [← hXd] at hn
  exact GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
    hs hneut hP hcfl h0 choice hZ0 n hn htime T

end Gen

end GlobalEZP

#print axioms GlobalEZP.global_ez_prod

#print axioms: 1 theorem, no axioms.

79.234 Bounded and eventually decaying for small energy \(\times \) enstrophy

Under the GlobalEZ hypotheses and \(4cW\le \nu ^2T_2\): \(Q_T\le 2Q_0\) for all \(T\), and after some \(t\le n_2\) the enstrophy decays geometrically; constants independent of the truncation.

ns/GlobalEZD.lean

/-
  ns/GlobalEZD.lean — T37 cycle 1351 (goal G802 — done): bounded for all time and eventually exponentially decaying,
  for data with small energy × enstrophy, over an ordered field. Under the hypotheses of GlobalEZ.global_ez and the
  waiting-time condition of EventDecay (4c·W ≤ ν²·T₂, T₂ = (n₂ + 1)hν):
    global_ez_decay   Q_T ≤ 2Q_0 for every T, and some t ≤ n₂ has 2c·Q_{t+1} ≤ ν³ and
                      (2 + 2hν)ˢ·Q_{t+1+s} ≤ (2 + hν)ˢ·Q_{t+1} for every s,
  all constants independent of the truncation.

  Lean 4 core plus ns/GlobalEZ, ns/EventDecay.
-/
import ns.GlobalEZ
import ns.EventDecay

namespace GlobalEZD
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_decay (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n n2 : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))))
    (htime2 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n2 + 1)) (K.mul h ν)))) :
    (∀ T, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) T)
      (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))) ∧
    ∃ t, t < n2 + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s, Le K O.toCone (K.mul (pw K (K.add (K.add K.one K.one) (K.add (K.mul h ν) (K.mul h ν))) s)
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1 + s)))
        (K.mul (pw K (K.add (K.add K.one K.one) (K.mul h ν)) s) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) :=
  ⟨fun T => GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
      hs hneut hP hcfl h0 choice hZ0 n hn htime T,
    EventDecay.event_decay K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hh0 hν0 hEs hstepM a N hs hneut hP hcfl h0
      n2 htime2⟩

end Gen

end GlobalEZD

#print axioms GlobalEZD.global_ez_decay

#print axioms: 1 theorem, no axioms.

79.235 Hölder-\(1/2\) equicontinuity for small data without \(H^2\) bounds

If \(2cQ_0\le \nu ^3\): \(\nu ^3\sum |a_{t+n}-a_t|^2\le n\cdot 4h(cQ_0+\nu ^3)Q_0\) for all \(t,n\), using only the dissipated \(H^2\) mass; constants independent of the truncation.

ns/HolderSmall.lean

/-
  ns/HolderSmall.lean — T37 cycle 1353 (goal G803 — done): Hölder-1/2 equicontinuity in time for small data with no
  H² hypothesis, over an ordered field. Implicit Fourier–Galerkin scheme, Leray-projected force (Σ|N|² ≤ Σ|Nl|²),
  c = 135ι², Q = νZ + hν²A. If 2c·Q_0 ≤ ν³ then for every t and n
    holder_small   ν³·Σ_r |a_{t+n,r} − a_{t,r}|² ≤ n·(4h·(c·Q_0 + ν³)·Q_0),
  i.e. |a(t + τ) − a(t)|²_{L²} ≤ 4τ(cQ_0 + ν³)Q_0/ν³ for τ = nh: a modulus √τ independent of the truncation, using
  only the time-integrated H² norm (EnstDiss) instead of a pointwise H² bound (compare EquiCont).
  Pieces: step_nu (ν·|a_{s+1} − a_s|² ≤ 2h²(cQ_0 + ν³)A_s), then TimeLipN.lip_n and EnstDiss.enst_diss from time t.

  Lean 4 core plus ns/TimeLip, ns/TimeLipN, ns/EnstDiss, ns/EventH2 (proj_step), ns/Dyadic (sumN_le).
-/
import ns.TimeLip
import ns.TimeLipN
import ns.EnstDiss
import ns.EventH2
import ns.Dyadic

namespace HolderSmall
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem holder_small (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (small : Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν))) (t n : Nat) :
    Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + n) r) (a t r)) (EnstDiff.sub3 K (a (t + n) r) (a t r))) l))
      (K.mul (castN K n) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)))
          (Q K ν h (Zs K l kz a) (As K l kz a) 0)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  generalize hcd : K.mul (castN K 135) (K.mul ι ι) = c at hc small ⊢
  have nQ : ∀ s, O.nn (Q K ν h (Zs K l kz a) (As K l kz a) s) := fun s =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ s)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s))
  -- the projected step in the form of EnstDiss
  have pstep : ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul (K.mul h c) (K.mul (As K l kz a s) (Zs K l kz a s)))) := by
    intro s; rw [← hcd]
    exact EventH2.proj_step K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν a N hN hs s
  -- Σ|N_s|² ≤ c·A_s·Z_s
  have hb : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (N s r) (N s r)) l) (K.mul c (K.mul (As K l kz a s) (Zs K l kz a s))) := by
    intro s
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
    have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l) (As K l kz a s) :=
      lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZ s))
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    refine le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ fu (le_trans' K O.toCone _ _ _ m2 ⟨K.zero, O.nn_zero, ?_⟩))
    rw [← hcd]; lib_ring
  -- Q_s ≤ Q_0, so ν·Z_s ≤ Q_0
  have small1 : Le K O.toCone (K.mul c (Q K ν h (Zs K l kz a) (As K l kz a) 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul c (Q K ν h (Zs K l kz a) (As K l kz a) 0), O.nn_mul _ _ hc (nQ 0), by lib_ring⟩ small
  have hq : ∀ s, Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) s) (Q K ν h (Zs K l kz a) (As K l kz a) 0) :=
    q_le K O ν h c (Zs K l kz a) (As K l kz a) hν hν0 hh hc hZ hA pstep small1
  have hz : ∀ s, Le K O.toCone (K.mul ν (Zs K l kz a s)) (Q K ν h (Zs K l kz a) (As K l kz a) 0) := fun s =>
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a s), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s), rfl⟩ (hq s)
  generalize Q K ν h (Zs K l kz a) (As K l kz a) 0 = Q0 at hz hq small ⊢
  -- one step: ν·D_s ≤ 2h²(cQ_0 + ν³)·A_s
  have step_nu : ∀ s, Le K O.toCone
      (K.mul ν (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (s + 1) r) (a s r)) (EnstDiff.sub3 K (a (s + 1) r) (a s r))) l))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν)))) (As K l kz a s)) := by
    intro s
    have tl := TimeLip.time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
    have m := le_mul_nn K O.toCone ν _ _ hν tl
    have k1 := le_mul_nn K O.toCone (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) _ _
      (O.nn_mul _ _ hν (O.nn_mul _ _ two (O.nn_sq h))) (hb s)
    have k2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul c (As K l kz a s))) _ _
      (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq h)) (O.nn_mul _ _ hc (hA s))) (hz s)
    refine le_trans' K O.toCone _ _ _ m (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
      (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ k1 (le_refl' K O.toCone
        (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s)))) ?_))
    · show K.add (K.mul ν (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (lsum K (fun r => dot3 K (N s r) (N s r)) l))
          (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))
            (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a s r) (a s r))) l)))) K.zero
        = K.add (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) (lsum K (fun r => dot3 K (N s r) (N s r)) l))
          (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s))
      show _ = K.add _ (K.mul _ (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (a s r) (a s r))) l))
      lib_ring
    · refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_refl' K O.toCone
        (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul (K.mul h h) (K.mul ν ν)))) (As K l kz a s))))
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ k2 (le_refl' K O.toCone _)) ⟨K.zero, O.nn_zero, ?_⟩)
      · show K.add (K.mul (K.mul ν (K.mul (K.add K.one K.one) (K.mul h h))) (K.mul c (K.mul (As K l kz a s) (Zs K l kz a s)))) K.zero
          = K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul c (As K l kz a s))) (K.mul ν (Zs K l kz a s))
        lib_ring
      · lib_ring
  -- dissipation from time t: hν²Σ_{j<n} A_{t+j} ≤ 2Q_t ≤ 2Q_0
  have dis := EnstDiss.enst_diss K O ν h c (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) hν hν0 hh hc
    (fun j => hZ _) (fun j => hA _) (fun j => pstep (t + j))
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (K.add c c) _ _ (O.nn_add _ _ hc hc) (hq t)) small) n
  have nQt : O.nn (Q K ν h (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) n) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have dis2 : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K (fun j => As K l kz a (t + j)) n)) (K.mul (K.add K.one K.one) Q0) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (Q K ν h (fun j => Zs K l kz a (t + j)) (fun j => As K l kz a (t + j)) n),
      O.nn_mul _ _ two nQt, by lib_ring⟩ (le_trans' K O.toCone _ _ _ dis (le_mul_nn K O.toCone _ _ _ two (hq t)))
  -- summed steps
  have ss := Dyadic.sumN_le K O _ _ (fun j => step_nu (t + j)) n
  rw [NSChain.sumN_mul_left K _ _ n, NSChain.sumN_mul_left K _ _ n] at ss
  have ln := TimeLipN.lip_n K O.toCone l (fun j => a (t + j)) n
  have hDn : O.nn (sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) n) :=
    NSChain.sumN_nn K O.toCone _ (fun j => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l) n
  generalize sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) n = D at ss ln hDn
  generalize sumN K (fun j => As K l kz a (t + j)) n = SA at ss dis2
  -- ν³·D ≤ 4h(cQ_0 + ν³)Q_0
  have f1 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ss
  have f2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν)))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ two hh) (O.nn_add _ _ (O.nn_mul _ _ hc (TruncRate.nn_of_le K O _ _ (O.nn_mul _ _ hν (hZ 0)) (hz 0)))
      (O.nn_mul _ _ hν (O.nn_sq ν)))) dis2
  have fD : Le K O.toCone (K.mul (K.mul ν (K.mul ν ν)) D)
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h) (K.mul (K.add (K.mul c Q0) (K.mul ν (K.mul ν ν))) Q0)) := by
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f1
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f2 ⟨K.zero, O.nn_zero, ?_⟩)))
    all_goals lib_ring
  have g := le_mul_nn K O.toCone (castN K n) _ _ (RatModel.nn_cast K O.toCone n) fD
  have l3 := le_mul_nn K O.toCone (K.mul ν (K.mul ν ν)) _ _ (O.nn_mul _ _ hν (O.nn_sq ν)) ln
  refine le_trans' K O.toCone _ _ _ l3 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ g)
  show K.add (K.mul (K.mul ν (K.mul ν ν)) (K.mul (castN K n) D)) K.zero = K.mul (castN K n) (K.mul (K.mul ν (K.mul ν ν)) D)
  lib_ring

end Gen

end HolderSmall

#print axioms HolderSmall.holder_small

#print axioms: 1 theorem, no axioms.

79.236 Eventual Hölder equicontinuity for every datum

If \(4cW\le \nu ^2T\): some \(t\le n\) has \(2cQ^*\le \nu ^3\) (\(Q^*=Q_{t+1}\)) and \(\nu ^3\sum |a_{t+1+s+k}-a_{t+1+s}|^2\le k\cdot 4h(cQ^*+\nu ^3)Q^*\) for all \(s,k\).

ns/EventHolder.lean

/-
  ns/EventHolder.lean — T37 cycle 1355 (goal G804 — done): eventual Hölder-1/2 equicontinuity for arbitrary data,
  over an ordered field. Under the cfl_global hypotheses, h, ν ≠ 0, hν·c_M ≤ 1, c = 135ι², W = E_0 + hν·Z_0 and
  4c·W ≤ ν²·T (T = (n + 1)hν), there is t ≤ n such that, with Q* = Q_{t+1},
    event_holder   2c·Q* ≤ ν³  and  ν³·Σ_r |a_{t+1+s+k,r} − a_{t+1+s,r}|² ≤ k·(4h(cQ* + ν³)Q*)  for all s, k,
  a time modulus independent of the truncation after a truncation-free waiting time.

  Lean 4 core plus ns/EventH2 (good_time), ns/HolderSmall.
-/
import ns.EventH2
import ns.HolderSmall

namespace EventHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg
  EventH2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem event_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    ∃ t, t < n + 1 ∧
      Le K O.toCone (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) ∧
      ∀ s k, Le K O.toCone
        (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))
          (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))) l))
        (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
          (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
            (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))) := by
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have ⟨t, ht, hq⟩ := good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  refine ⟨t, ht, hq, fun s k => ?_⟩
  exact HolderSmall.holder_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
    (fun j => a (t + 1 + j)) (fun j => N (t + 1 + j)) (fun j => hP (t + 1 + j)) (fun j r => hs (t + 1 + j) r) hq s k

end Gen

end EventHolder

#print axioms EventHolder.event_holder

#print axioms: 1 theorem, no axioms.

79.237 Dissipated \(H^2\) mass on the large-data local window

Under the local-bound hypotheses with \(2c_2Z_b\le \nu ^2\) and \(D=hc_1Z_b^2\): \(2Q_t+h\nu ^2\sum _{j<t}A_j\le 2Q_0+2tD\) for every \(t\le n\), no smallness of the data.

ns/LocalDiss.lean

/-
  ns/LocalDiss.lean — T37 cycle 1357 (goal G805a): the dissipated H² mass on the large-data local window, abstract
  form, over an ordered field. Hypotheses of EnstLocal.local_bound with the cutoff choice tightened to
  2c₂Zb ≤ ν²; D = h·c₁·Zb². Then for every t ≤ n
    local_diss   2Q_t + hν²·Σ_{j<t} A_j ≤ 2Q_0 + 2t·D,
  so on the window n·h ≤ (νZb − Q_0)/(c₁Zb²) the time-integrated H² norm is bounded by constants independent of the
  truncation (no smallness of the data).

  Lean 4 core plus ns/EnstLocal (local_bound, sq_mono), ns/EnstGlobal (le_of_mul_le).
-/
import ns.EnstLocal

namespace LocalDiss
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstLocal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem local_diss (ν h c1 c2 Zb : R) (Z A : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hc1 : O.nn c1) (hc2 : O.nn c2) (hZb : O.nn Zb) (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
    (choice2 : Le K O.toCone (K.mul (K.add c2 c2) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h (K.mul c1 (K.mul Zb Zb))))) (K.mul ν Zb)) :
    ∀ t, t ≤ n → Le K O.toCone
      (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A t)) (K.mul (K.mul h (K.mul ν ν)) (sumN K A t)))
      (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
        (K.mul (K.add K.one K.one) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))))
  | 0, _ => ⟨K.zero, O.nn_zero, by
      show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.mul h (K.mul ν ν)) K.zero)) K.zero
        = K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0)) (K.mul (K.add K.one K.one) (K.mul K.zero (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      lib_ring⟩
  | t + 1, ht => by
    have ih := local_diss ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice2 n budget t (Nat.le_of_succ_le ht)
    have choice : Le K O.toCone (K.mul c2 Zb) (K.mul ν ν) :=
      le_trans' K O.toCone _ _ _ ⟨K.mul c2 Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
    have lb := local_bound K O ν h c1 c2 Zb Z A hν hν0 hh hc1 hc2 hZb hZ hA step choice n budget t (Nat.le_of_succ_le ht)
    have hD : O.nn (K.mul h (K.mul c1 (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n (Nat.le_of_succ_le ht)) hD
    have q1 := le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Z t)) (Q K ν h Z A t) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (A t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t), rfl⟩
    have zt : Le K O.toCone (Z t) Zb := le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
    -- 2c₁Z² ≤ 2c₁Zb²,  2c₂AZ ≤ ν²A
    have s1 := le_mul_nn K O.toCone c1 _ _ hc1 (sq_mono K O _ _ (hZ t) hZb zt)
    have c2z := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (K.add c2 c2) _ _ (O.nn_add _ _ hc2 hc2) zt) choice2
    have s2 := le_mul_nn K O.toCone (A t) _ _ (hA t) c2z
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have st2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (step t)
    -- 2Q_{t+1} + hν²A_t ≤ 2Q_t + 2D
    have one : Le K O.toCone
        (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A t)) (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))) := by
      have a1 := le_add' K O.toCone _ _ _ _ st2 (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (A t)))
      have a2 := le_mul_nn K O.toCone (K.add h h) _ _ (O.nn_add _ _ hh hh) s1
      have a3 := le_mul_nn K O.toCone h _ _ hh s2
      refine le_trans' K O.toCone _ _ _ a1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ a2 a3)
          (le_refl' K O.toCone (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t))))) ⟨K.zero, O.nn_zero, ?_⟩))
      · show K.add (K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t))
            (K.mul h (K.add (K.mul c1 (K.mul (Z t) (Z t))) (K.mul c2 (K.mul (A t) (Z t)))))))
            (K.mul (K.mul h (K.mul ν ν)) (A t))) K.zero
          = K.add (K.add (K.mul (K.add h h) (K.mul c1 (K.mul (Z t) (Z t)))) (K.mul h (K.mul (A t) (K.mul (K.add c2 c2) (Z t)))))
              (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))
        lib_ring
      · show K.add (K.add (K.add (K.mul (K.add h h) (K.mul c1 (K.mul Zb Zb))) (K.mul h (K.mul (A t) (K.mul ν ν))))
            (K.add (K.add (K.mul ν (Z t)) (K.mul ν (Z t))) (K.mul (K.mul h (K.mul ν ν)) (A t)))) K.zero
          = K.add (K.mul (K.add K.one K.one) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))))
              (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))
        lib_ring
    have s := le_add' K O.toCone _ _ _ _ one (le_refl' K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K A t)))
    have ih' := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ s
      (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ ih' ⟨K.zero, O.nn_zero, ?_⟩)))
    · show K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A (t + 1))) (K.mul (K.mul h (K.mul ν ν)) (K.add (sumN K A t) (A t)))) K.zero = _
      generalize Q K ν h Z A (t + 1) = q1'
      generalize Q K ν h Z A t = q0'
      lib_ring
    · generalize Q K ν h Z A t = q0'
      lib_ring
    · show K.add (K.add (K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
          (K.mul (K.add K.one K.one) (K.mul (castN K t) (K.mul h (K.mul c1 (K.mul Zb Zb))))))
          (K.mul (K.add K.one K.one) (K.mul h (K.mul c1 (K.mul Zb Zb))))) K.zero
        = K.add (K.mul (K.add K.one K.one) (Q K ν h Z A 0))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one (castN K t)) (K.mul h (K.mul c1 (K.mul Zb Zb)))))
      generalize Q K ν h Z A 0 = q00
      generalize castN K t = ct
      lib_ring

end Gen

end LocalDiss

#print axioms LocalDiss.local_diss

#print axioms: 1 theorem, no axioms.

79.238 Hölder equicontinuity on the large-data local window

With \(2c_2Z_b\le \nu ^2\) and the local budget: \(\nu ^2\sum |a_{t+k}-a_t|^2\le k\cdot 4h\nu (cZ_b+\nu ^2)Z_b\) for \(t+k\le n\), no smallness of the data, constants independent of the truncation.

ns/LocalHolder.lean

/-
  ns/LocalHolder.lean — T37 cycle 1359 (goal G805b): Hölder-1/2 equicontinuity on the large-data local window, over
  an ordered field. Hypotheses of EnstLocalGalerkin.local_galerkin with the cutoff choice tightened to
  2c₂Zb ≤ ν² (c₁ = cc(ι, C1), c₂ = cc(ι, C2)) and the budget Q_0 + n·h·c₁Zb² ≤ νZb; c = 135ι². For t + k ≤ n:
    local_step     the local enstrophy step νZ' + hν²A' ≤ νZ + h(c₁Z² + c₂AZ) as a lemma
    sum_le_upto    termwise bounds below k give the bound on the partial sum
    sumN_split     Σ_{j<t+k} A_j = Σ_{j<t} A_j + Σ_{j<k} A_{t+j}
    local_holder   ν²·Σ_r |a_{t+k,r} − a_{t,r}|² ≤ k·(4hν(cZb + ν²)Zb)
  no smallness of the data, constants independent of the truncation.

  Lean 4 core plus ns/LocalDiss, ns/EnstLocalGalerkin, ns/TimeLip, ns/TimeLipN.
-/
import ns.LocalDiss
import ns.EnstLocalGalerkin
import ns.TimeLip
import ns.TimeLipN

namespace LocalHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem local_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (s + 1))
      (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (cc K ι (C1 K inv Rc)) (K.mul (Zs K l kz a s) (Zs K l kz a s)))
        (K.mul (cc K ι (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
  have ts := two_scale K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (a s)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) ts)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (Zs K l kz a s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
      (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul (As K l kz a s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))) K.zero
    = K.add (K.mul ν (Zs K l kz a s)) (K.mul h (K.add (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C1 K inv Rc))
        (K.mul (Zs K l kz a s) (Zs K l kz a s)))
      (K.mul (K.mul (K.mul (K.add K.one K.one) (K.mul ι ι)) (C2 K inv Rc t0)) (K.mul (As K l kz a s) (Zs K l kz a s)))))
  lib_ring

theorem sum_le_upto (f g : Nat → R) : ∀ k, (∀ j, j < k → Le K O.toCone (f j) (g j)) →
    Le K O.toCone (sumN K f k) (sumN K g k)
  | 0, _ => le_refl' K O.toCone _
  | k + 1, h => le_add' K O.toCone _ _ _ _ (sum_le_upto f g k (fun j hj => h j (Nat.lt_succ_of_lt hj))) (h k (Nat.lt_succ_self k))

theorem sumN_split (A : Nat → R) (t : Nat) : ∀ k, sumN K A (t + k) = K.add (sumN K A t) (sumN K (fun j => A (t + j)) k)
  | 0 => by show sumN K A t = K.add (sumN K A t) K.zero; lib_ring
  | k + 1 => by
    show K.add (sumN K A (t + k)) (A (t + k)) = K.add (sumN K A t) (K.add (sumN K (fun j => A (t + j)) k) (A (t + k)))
    rw [sumN_split A t k]; lib_ring

include mul_inv h10 in
theorem local_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t k : Nat) (htk : t + k ≤ n) :
    Le K O.toCone
      (K.mul (K.mul ν ν) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + k) r) (a t r)) (EnstDiff.sub3 K (a (t + k) r) (a t r))) l))
      (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  have stp := local_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a N hN hs
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
  -- Z_s ≤ Zb for s ≤ n
  have zs : ∀ s, s ≤ n → Le K O.toCone (Zs K l kz a s) Zb := by
    intro s hsn
    have lb := local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice n budget s hsn
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone s n hsn) hD
    have q1 := le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
    have hz : Le K O.toCone (K.mul ν (Zs K l kz a s)) (Q K ν h (Zs K l kz a) (As K l kz a) s) :=
      ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a s), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA s), rfl⟩
    exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q1)
  -- one step: D_s ≤ 2h²(cZb + ν²)A_s
  have stepD : ∀ s, s ≤ n → Le K O.toCone
      (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (s + 1) r) (a s r)) (EnstDiff.sub3 K (a (s + 1) r) (a s r))) l)
      (K.mul (K.mul (K.add K.one K.one) (K.mul h h)) (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) (As K l kz a s))) := by
    intro s hsn
    have tl := TimeLip.time_lip K O.toCone l h ν hh hν (wt K kz) hw (a (s + 1)) (a s) (N s) (hs s)
    have fu := force_uniform K O inv mul_inv h10 l kz hd h1 M hM ι (kv K kz) (a s) (a s)
    have hH : Le K O.toCone (lsum K (fun p => K.mul (K.mul (mu K kz p) (mu K kz p)) (dot3 K (a s p) (a s p))) l) (As K l kz a s) :=
      lsum_le K O.toCone _ _ (fun p => le_mul_right' K O.toCone (nrm4_le K O.toCone (kz p)) (dot3_nn K O.toCone _)) l
    have m1 := le_mul_right' K O.toCone hH (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZ s))
    have m2 := le_mul_nn K O.toCone (K.mul ι ι) _ _ (O.nn_sq ι) m1
    have hb : Le K O.toCone (lsum K (fun r => dot3 K (N s r) (N s r)) l)
        (K.mul (K.mul (castN K 135) (K.mul ι ι)) (K.mul (As K l kz a s) Zb)) := by
      have z := le_mul_nn K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (As K l kz a s)) _ _ (O.nn_mul _ _ hc (hA s)) (zs s hsn)
      refine le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ fu (le_trans' K O.toCone _ _ _ m2
        (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ z ⟨K.zero, O.nn_zero, ?_⟩))))
      all_goals lib_ring
    have b2 := le_mul_nn K O.toCone (K.mul (K.add K.one K.one) (K.mul h h)) _ _ (O.nn_mul _ _ two (O.nn_sq h)) hb
    refine le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ b2 (le_refl' K O.toCone _)) ⟨K.zero, O.nn_zero, ?_⟩)
    unfold As
    lib_ring
  -- sum over the k steps from t
  have ss := sum_le_upto K O _ _ k (fun j hj => stepD (t + j) (Nat.le_trans (Nat.le_of_lt (Nat.add_lt_add_left hj t)) htk))
  rw [NSChain.sumN_mul_left K _ _ k, NSChain.sumN_mul_left K _ _ k] at ss
  -- dissipation up to t + k
  have ld := LocalDiss.local_diss K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice2 n budget (t + k) htk
  rw [sumN_split K (As K l kz a) t k] at ld
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone (t + k) n htk) hD
  have nQ := O.nn_add _ _ (O.nn_mul _ _ hν (hZ (t + k))) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA (t + k)))
  have nS0 := NSChain.sumN_nn K O.toCone (As K l kz a) hA t
  have ln := TimeLipN.lip_n K O.toCone l (fun j => a (t + j)) k
  generalize sumN K (fun j => lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))
      (EnstDiff.sub3 K (a (t + j + 1) r) (a (t + j) r))) l) k = D at ss ln
  generalize sumN K (fun j => As K l kz a (t + j)) k = SA at ss ld
  -- hν²·SA ≤ 2νZb
  have sa : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) SA) (K.mul (K.add K.one K.one) (K.mul ν Zb)) := by
    have b2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two
      (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) 0)) tn) budget)
    refine le_trans' K O.toCone _ _ _ ⟨K.add (K.mul (K.add K.one K.one) (Q K ν h (Zs K l kz a) (As K l kz a) (t + k)))
        (K.mul (K.mul h (K.mul ν ν)) (sumN K (As K l kz a) t)),
      O.nn_add _ _ (O.nn_mul _ _ two nQ) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) nS0), by lib_ring⟩
      (le_trans' K O.toCone _ _ _ ld (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ b2))
  have hcz : O.nn (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) := O.nn_add _ _ (O.nn_mul _ _ hc hZb) (O.nn_sq ν)
  have f1 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ss
  have f2 := le_mul_nn K O.toCone (K.mul (K.mul (K.add K.one K.one) h) (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ two hh) hcz) sa
  have fD : Le K O.toCone (K.mul (K.mul ν ν) D)
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)) := by
    refine le_trans' K O.toCone _ _ _ f1 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ f2 ⟨K.zero, O.nn_zero, ?_⟩))
    all_goals lib_ring
  have g := le_mul_nn K O.toCone (castN K k) _ _ (RatModel.nn_cast K O.toCone k) fD
  have l3 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) ln
  refine le_trans' K O.toCone _ _ _ l3 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ g)
  show K.add (K.mul (K.mul ν ν) (K.mul (castN K k) D)) K.zero = K.mul (castN K k) (K.mul (K.mul ν ν) D)
  lib_ring

end Gen

end LocalHolder

#print axioms LocalHolder.local_step
#print axioms LocalHolder.sum_le_upto
#print axioms LocalHolder.sumN_split
#print axioms LocalHolder.local_holder

#print axioms: 4 theorems, no axioms.

79.239 Hölder equicontinuity for all time under the energy–enstrophy condition

Local window (local_holder) before the good time, small regime (holder_small) after it, triangle inequality across: \(\nu ^3\sum |a_{T+k}-a_T|^2\le k\cdot 2G\) for all \(T,k\), with \(G\) independent of the truncation and of \(T\).

ns/GlobalHolder.lean

/-
  ns/GlobalHolder.lean — T37 cycle 1361 (goal G805c — done): Hölder-1/2 equicontinuity for all time for data with
  small energy × enstrophy, over an ordered field. Hypotheses: cfl_global's (projected, energy-neutral force, CFL),
  h, ν ≠ 0, hν·c_M ≤ 1, the local window w = n + 1 (2c₂Zb ≤ ν², Q_0 + w·h·c₁Zb² ≤ νZb) and the good-time condition
  4c·W ≤ ν²·w·hν (c = 135ι², W = E_0 + hνZ_0). With L = 4hν(cZb + ν²)Zb, S₀ = 4h(ν³ + ν³)(νZb), G = νL + S₀:
    tri_sq        |x − z|² ≤ 2|x − y|² + 2|y − z|²  (summed over modes)
    cancel_add    T + a = T + b ⇒ a = b              (hand proof; Nat.add_left_cancel carries propext)
    global_holder ν³·Σ_r |a_{T+k,r} − a_{T,r}|² ≤ k·(2G) for all T, k,
  i.e. |a(T + τ) − a(T)|² ≤ 2Gτ/(hν³) with a constant independent of the truncation and of T: LocalHolder before the
  end of the window, HolderSmall after the good time t + 1 ≤ w, the triangle inequality across it.

  Lean 4 core plus ns/LocalHolder, ns/HolderSmall, ns/EventH2 (good_time).
-/
import ns.LocalHolder
import ns.HolderSmall
import ns.EventH2

namespace GlobalHolder
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem tri3 (x y z : V3 R) : Le K O.toCone (dot3 K (EnstDiff.sub3 K x z) (EnstDiff.sub3 K x z))
    (K.add (K.mul (K.add K.one K.one) (dot3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K x y)))
      (K.mul (K.add K.one K.one) (dot3 K (EnstDiff.sub3 K y z) (EnstDiff.sub3 K y z)))) := by
  refine ⟨dot3 K (EnstDiff.sub3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K y z)) (EnstDiff.sub3 K (EnstDiff.sub3 K x y) (EnstDiff.sub3 K y z)),
    dot3_nn K O.toCone _, ?_⟩
  show K.add (K.add (K.add (K.mul (K.add x.x (K.neg z.x)) (K.add x.x (K.neg z.x))) (K.mul (K.add x.y (K.neg z.y)) (K.add x.y (K.neg z.y)))) (K.mul (K.add x.z (K.neg z.z)) (K.add x.z (K.neg z.z)))) (K.add (K.add (K.mul (K.add (K.add x.x (K.neg y.x)) (K.neg (K.add y.x (K.neg z.x)))) (K.add (K.add x.x (K.neg y.x)) (K.neg (K.add y.x (K.neg z.x))))) (K.mul (K.add (K.add x.y (K.neg y.y)) (K.neg (K.add y  -- (+652 chars; full line in the Zenodo source)
  lib_ring

theorem tri_sq (l : List I) (x y z : I → V3 R) :
    Le K O.toCone (lsum K (fun r => dot3 K (EnstDiff.sub3 K (x r) (z r)) (EnstDiff.sub3 K (x r) (z r))) l)
      (K.add (K.mul (K.add K.one K.one) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (x r) (y r)) (EnstDiff.sub3 K (x r) (y r))) l))
        (K.mul (K.add K.one K.one) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (y r) (z r)) (EnstDiff.sub3 K (y r) (z r))) l))) := by
  have t := lsum_le K O.toCone _ _ (fun r => tri3 K O (x r) (y r) (z r)) l
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at t
  exact t

theorem cancel_add : ∀ T a b : Nat, T + a = T + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | T + 1, a, b, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact cancel_add T a b (Nat.succ.inj h)

include mul_inv h10 in
theorem global_holder (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν)) (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T k : Nat) :
    Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) (K.mul (K.add K.one K.one)
        (K.add (K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
            (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)))
          (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
            (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb)))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZ : ∀ t, O.nn (Zs K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have hA : ∀ t, O.nn (As K l kz a t) := fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have four : O.nn (K.add (K.add K.one K.one) (K.add K.one K.one)) := O.nn_add _ _ two two
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have hc1 : O.nn (cc K ι (C1 K inv Rc)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := O.nn_mul _ _ hh (O.nn_mul _ _ hc1 (O.nn_sq Zb))
  have nν3 : O.nn (K.mul ν (K.mul ν ν)) := O.nn_mul _ _ hν (O.nn_sq ν)
  -- the good time τ = t + 1 ≤ n + 1 with 2c·Q_τ ≤ ν³
  have ⟨t, ht, hq⟩ := EventH2.good_time K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es
    (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
    hh hν hh0 hν0 hEs (O.nn_add _ _ hc hc) hstepM a N hs hneut hP hcfl h0 n htime
  have hτw : t + 1 ≤ n + 1 := Nat.succ_le_of_lt ht
  -- Q_τ ≤ νZb (local bound on the window)
  have stp := LocalHolder.local_step K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν hh hν a N hP hs
  have qτ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) (K.mul ν Zb) := by
    have lb := local_bound K O ν h _ _ Zb (Zs K l kz a) (As K l kz a) hν hν0 hh hc1 hc2 hZb hZ hA stp choice (n + 1) budget (t + 1) hτw
    have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone (t + 1) (n + 1) hτw) hD
    exact le_trans' K O.toCone _ _ _ lb (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn) budget)
  have nQτ : O.nn (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA _))
  have cqτ : Le K O.toCone (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1)),
      O.nn_mul _ _ hc nQτ, by lib_ring⟩ hq
  -- the small-regime constant S ≤ S₀
  have hSS : Le K O.toCone
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
          (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))
      (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
        (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb))) :=
    le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ four hh)
      (TruncRate.mm K O _ _ _ _ (O.nn_add _ _ (O.nn_mul _ _ hc nQτ) nν3) (O.nn_mul _ _ hν hZb)
        (le_add' K O.toCone _ _ _ _ cqτ (le_refl' K O.toCone _)) qτ)
  generalize hLd : K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
      (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)) = Lν
  generalize hS0d : K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
      (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb)) = S0 at hSS
  have nL : O.nn Lν := by
    rw [← hLd]
    exact O.nn_mul _ _ hν (O.nn_mul _ _ (O.nn_mul _ _ four (O.nn_mul _ _ hh hν)) (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ hc hZb) (O.nn_sq ν)) hZb))
  have nS0 : O.nn S0 := by
    rw [← hS0d]
    exact O.nn_mul _ _ (O.nn_mul _ _ four hh) (O.nn_mul _ _ (O.nn_add _ _ nν3 nν3) (O.nn_mul _ _ hν hZb))
  -- window bound and small-regime bound
  have hL : ∀ T k, T + k ≤ n + 1 → Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) Lν) := by
    intro T k htk
    have lh := LocalHolder.local_holder K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hP hs choice2 (n + 1) budget T k htk
    have m := le_mul_nn K O.toCone ν _ _ hν lh
    rw [← hLd]
    exact le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, by lib_ring⟩ (le_trans' K O.toCone _ _ _ m ⟨K.zero, O.nn_zero, by lib_ring⟩)
  have hS : ∀ s k, Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + s + k) r) (a (t + 1 + s) r))
        (EnstDiff.sub3 K (a (t + 1 + s + k) r) (a (t + 1 + s) r))) l))
      (K.mul (castN K k) S0) := by
    intro s k
    have hb := HolderSmall.holder_small K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0
      (fun j => a (t + 1 + j)) (fun j => N (t + 1 + j)) (fun j => hP (t + 1 + j)) (fun j r => hs (t + 1 + j) r) hq s k
    have hb' : Le K O.toCone
        (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))
          (EnstDiff.sub3 K (a (t + 1 + (s + k)) r) (a (t + 1 + s) r))) l))
        (K.mul (castN K k) (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
          (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))) (K.mul ν (K.mul ν ν)))
            (Q K ν h (Zs K l kz a) (As K l kz a) (t + 1))))) := hb
    rw [← Nat.add_assoc] at hb'
    exact le_trans' K O.toCone _ _ _ hb' (le_mul_nn K O.toCone _ _ _ (RatModel.nn_cast K O.toCone k) hSS)
  have kG : ∀ X, Le K O.toCone X Lν → Le K O.toCone X (K.mul (K.add K.one K.one) (K.add Lν S0)) := fun X hX =>
    le_trans' K O.toCone _ _ _ hX ⟨K.add Lν (K.add S0 S0), O.nn_add _ _ nL (O.nn_add _ _ nS0 nS0), by lib_ring⟩
  have kG' : ∀ X, Le K O.toCone X S0 → Le K O.toCone X (K.mul (K.add K.one K.one) (K.add Lν S0)) := fun X hX =>
    le_trans' K O.toCone _ _ _ hX ⟨K.add (K.add Lν Lν) S0, O.nn_add _ _ (O.nn_add _ _ nL nL) nS0, by lib_ring⟩
  have nk := RatModel.nn_cast K O.toCone k
  cases Nat.lt_or_ge (n + 1) (T + k) with
  | inr hwin =>
    exact le_trans' K O.toCone _ _ _ (hL T k hwin) (le_mul_nn K O.toCone _ _ _ nk (kG Lν (le_refl' K O.toCone _)))
  | inl hout =>
    cases Nat.lt_or_ge T (t + 1) with
    | inr hge =>
      have ⟨s, hs'⟩ := DyadicModelZ.le_dest hge
      rw [← hs']
      exact le_trans' K O.toCone _ _ _ (hS s k) (le_mul_nn K O.toCone _ _ _ nk (kG' S0 (le_refl' K O.toCone _)))
    | inl hlt =>
      have ⟨k1, hk1⟩ := DyadicModelZ.le_dest (Nat.le_of_lt hlt)
      have ⟨k2, hk2⟩ := DyadicModelZ.le_dest (Nat.le_of_lt (Nat.lt_of_le_of_lt hτw hout))
      have e : k1 + k2 = k := by
        apply cancel_add T
        rw [← Nat.add_assoc, hk1, hk2]
      have tr := tri_sq K O l (a (T + k)) (a (t + 1)) (a T)
      have b1 := hS 0 k2
      have b1' : Le K O.toCone
          (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a (t + 1) r))
            (EnstDiff.sub3 K (a (T + k) r) (a (t + 1) r))) l)) (K.mul (castN K k2) S0) := by
        rw [← hk2]; exact b1
      have b2 := hL T k1 (by rw [hk1]; exact hτw)
      rw [hk1] at b2
      have nk1 := RatModel.nn_cast K O.toCone k1
      have nk2 := RatModel.nn_cast K O.toCone k2
      have m := le_mul_nn K O.toCone (K.mul ν (K.mul ν ν)) _ _ nν3 tr
      have comb := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (K.add K.one K.one) _ _ two b1')
        (le_mul_nn K O.toCone (K.add K.one K.one) _ _ two b2)
      have ce : castN K k = K.add (castN K k1) (castN K k2) := by rw [← e, RatModel.cast_add]
      rw [ce]
      refine le_trans' K O.toCone _ _ _ m (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
        (le_trans' K O.toCone _ _ _ comb ⟨K.add (K.mul (K.add K.one K.one) (K.mul (castN K k1) S0))
          (K.mul (K.add K.one K.one) (K.mul (castN K k2) Lν)),
          O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ nk1 nS0)) (O.nn_mul _ _ two (O.nn_mul _ _ nk2 nL)), ?_⟩))
      · lib_ring
      · lib_ring

end Gen

end GlobalHolder

#print axioms GlobalHolder.tri3
#print axioms GlobalHolder.tri_sq
#print axioms GlobalHolder.cancel_add
#print axioms GlobalHolder.global_holder

#print axioms: 4 theorems, no axioms.

79.240 Pointwise enstrophy bound under the energy–enstrophy condition

Under the GlobalEZ hypotheses (\(\nu Z_b=2Q_0\)): \(Z_T\le Z_b\) for every \(T\), uniformly in the truncation.

ns/GlobalEZZ.lean

/-
  ns/GlobalEZZ.lean — T37 cycle 1377 (goal G812 — done): the pointwise enstrophy bound for data with small
  energy × enstrophy, over an ordered field. Under the hypotheses of GlobalEZ.global_ez (νZb = 2Q_0):
    global_ez_Z   Z_T ≤ Zb = 2Q_0/ν for every T,
  since νZ_T ≤ Q_T ≤ 2Q_0 = νZb; the bound is independent of the truncation.

  Lean 4 core plus ns/GlobalEZ.
-/
import ns.GlobalEZ

namespace GlobalEZZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin EnstGlobal CFLStep CFLGlobal EventSmall EventReg

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_Z (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) (T : Nat) :
    Le K O.toCone (Zs K l kz a T) Zb := by
  have g := GlobalEZ.global_ez K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb hstepM a N
    hs hneut hP hcfl h0 choice hZ0 n hn htime T
  have hA : O.nn (As K l kz a T) := lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a T)) (Q K ν h (Zs K l kz a) (As K l kz a) T) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a T), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  rw [← hZ0] at g
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz g)

end Gen

end GlobalEZZ

#print axioms GlobalEZZ.global_ez_Z

#print axioms: 1 theorem, no axioms.

79.241 Uniform bound and uniform time modulus under the energy–enstrophy condition

Under the GlobalHolder hypotheses and \(\nu Z_b=2Q_0\): \(Z_T\le Z_b\) for all \(T\) and \(\nu ^3\sum |a_{T+k}-a_T|^2\le k\cdot 2G\) for all \(T,k\), both independent of the truncation.

ns/GlobalEZAll.lean

/-
  ns/GlobalEZAll.lean — T37 cycle 1379 (goal G813 — done): one statement for data with small energy × enstrophy,
  over an ordered field. Under the hypotheses of GlobalHolder.global_holder and νZb = 2Q_0:
    global_ez_all   Z_T ≤ Zb for every T, and ν³·Σ_r|a_{T+k,r} − a_{T,r}|² ≤ k·(2G) for all T, k,
  a truncation-uniform bound and a truncation-uniform time modulus (the Arzelà–Ascoli input). The GlobalEZ hypotheses
  are derived: c₂Zb ≤ ν² from 2c₂Zb ≤ ν², the window (n+1)hc₁Zb² ≤ Q_0 from the budget Q_0 + (n+1)hc₁Zb² ≤ νZb = 2Q_0,
  and the κ = c waiting condition from the κ = 2c one.

  Lean 4 core plus ns/GlobalEZZ, ns/GlobalHolder.
-/
import ns.GlobalEZZ
import ns.GlobalHolder

namespace GlobalEZAll
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum CFLStep CFLGlobal

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem global_ez_all (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Es Zb : R) (hh : O.nn h) (hν : O.nn ν) (hh0 : h ≠ K.zero) (hν0 : ν ≠ K.zero) (hEs : O.nn Es) (hZb : O.nn Zb)
    (hstepM : Le K O.toCone (K.mul (K.mul h ν) (cM K M)) K.one)
    (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es)
    (choice2 : Le K O.toCone (K.mul (K.add (cc K ι (C2 K inv Rc t0)) (cc K ι (C2 K inv Rc t0))) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0) (Q K ν h (Zs K l kz a) (As K l kz a) 0))
    (n : Nat)
    (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (htime : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (castN K 135) (K.mul ι ι)) (K.mul (castN K 135) (K.mul ι ι)))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν)))) :
    (∀ T, Le K O.toCone (Zs K l kz a T) Zb) ∧
    (∀ T k, Le K O.toCone
      (K.mul (K.mul ν (K.mul ν ν)) (lsum K (fun r => dot3 K (EnstDiff.sub3 K (a (T + k) r) (a T r)) (EnstDiff.sub3 K (a (T + k) r) (a T r))) l))
      (K.mul (castN K k) (K.mul (K.add K.one K.one)
        (K.add (K.mul ν (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.mul h ν))
            (K.mul (K.add (K.mul (K.mul (castN K 135) (K.mul ι ι)) Zb) (K.mul ν ν)) Zb)))
          (K.mul (K.mul (K.add (K.add K.one K.one) (K.add K.one K.one)) h)
            (K.mul (K.add (K.mul ν (K.mul ν ν)) (K.mul ν (K.mul ν ν))) (K.mul ν Zb))))))) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have hc2 : O.nn (cc K ι (C2 K inv Rc t0)) := O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
    (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))
  have choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (cc K ι (C2 K inv Rc t0)) Zb, O.nn_mul _ _ hc2 hZb, by lib_ring⟩ choice2
  -- (n+1)·D ≤ Q_0 from Q_0 + (n+1)D ≤ νZb = Q_0 + Q_0
  have hn : Le K O.toCone (K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))
      (Q K ν h (Zs K l kz a) (As K l kz a) 0) := by
    rw [hZ0] at budget
    cases budget with
    | intro d hdd =>
      refine ⟨d, hdd.1, ?_⟩
      have e := hdd.2
      generalize Q K ν h (Zs K l kz a) (As K l kz a) 0 = Q0 at e ⊢
      generalize K.mul (castN K (n + 1)) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) = X at e ⊢
      calc K.add X d = K.add (K.neg Q0) (K.add (K.add Q0 X) d) := by lib_ring
        _ = K.add (K.neg Q0) (K.add Q0 Q0) := by rw [e]
        _ = Q0 := by lib_ring
  have hc : O.nn (K.mul (castN K 135) (K.mul ι ι)) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_sq ι)
  have hW : O.nn (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) :=
    O.nn_add _ _ (lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l)
      (O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l))
  have htime1 : Le K O.toCone
      (K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))))
      (K.mul (K.mul ν ν) (K.mul (castN K (n + 1)) (K.mul h ν))) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.add K.one K.one) (K.mul (K.mul (castN K 135) (K.mul ι ι))
        (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
          (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))),
      O.nn_mul _ _ two (O.nn_mul _ _ hc hW), by lib_ring⟩ htime
  exact ⟨fun T => GlobalEZZ.global_ez_Z K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb
      hstepM a N hs hneut hP hcfl h0 choice hZ0 n hn htime1 T,
    fun T k => GlobalHolder.global_holder K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Es Zb hh hν hh0 hν0 hEs hZb
      hstepM a N hs hneut hP hcfl h0 choice2 n budget htime T k⟩

end Gen

end GlobalEZAll

#print axioms GlobalEZAll.global_ez_all

#print axioms: 1 theorem, no axioms.

79.242 Status

Statement

Status

Where

Galerkin and dyadic nonlinearities are energy neutral, every ring, every \(N\)

proved (Lean)

galerkin_neutral, dyadic_energy_neutral

exact discrete energy identity of a Crank–Nicolson step

proved (Lean)

cn_energy

every truncation globally bounded by the initial energy

proved (Lean)

cn_global

every Crank–Nicolson run of every 2D truncation: \(Z_T+\sum _t h\nu D^{(2)}_t=Z_0\)

proved (Lean)

run_enstrophy

discrete Leray inequality \(E_T+\sum _t h\nu D_t=E_0\), every dimension

proved (Lean)

leray_eq

per-triad and summed 3D transfer bounds, weighted form, lattice-sum ingredients, Riccati comparison

proved (Lean)

triad_sq_bound, transfer_sq, weighted_cs, riccati

small-data comparison \(bz_0\le a\Rightarrow z_k\le z_0\) for all \(k\); \(\sum _{k\le N}k^{-2}\le 2\) in every ordered field

proved (Lean)

small_global, basel

BKM-type closure \(z_k\le (1+hK/\nu )^kz_0\)

proved (Lean)

bkm

commutator gain, Biot–Savart, \(Z^2\le EP\)

proved (Lean)

comm_bound, vort_norm, interp

Sobolev \(H^2\subset L^\infty \), constant \(\sqrt {52}\), every truncation

proved (Lean)

shell_weight, sobolev

Leray projector; weighted Cauchy–Schwarz; discrete Young

proved (Lean)

idem, wcs2, young

\(H^2\) triad bound (constant \(52\)); Riccati of exponent \(3/2\)

proved (Lean)

triad_h2, riccati32

forced Leray step and run; epochs of small dissipation

proved (Lean)

forced_step, run, epoch

tail of \(\sum 1/n^2\) \(\le 1/(K+1)\)

proved (Lean)

tail

Agmon for truncations (pieces + combination)

proved (Lean)

agmon

optimised Agmon \(\|u\|_\infty ^4\le C\|\nabla u\|^2\|\Delta u\|^2\)

proved (Lean)

opt

Prodi–Serrin-type discrete criterion

proved (Lean)

serrin

sum form \(z_N(1-S_N)\le z_0\) for \(S_N\le 1\)

proved (Lean)

serrin_sum

Young step \(2\nu Z'\le (2\nu +hc^2X^2)Z\)

proved (Lean)

young_step

discrete \(L^2_tL^\infty _x\) criterion for a run

proved (Lean)

serrin_run

time sum \((\sum X_k^2)^2\le NCM\sum P_k\)

proved (Lean)

agmon_time

windows of half budget: \(z_{JL}\le 2^Jz_0\)

proved (Lean)

windows

\(L^3\) by \(L^2\cdot L^4\) and \(L^2\cdot L^\infty \)

proved (Lean)

l3_l2l4, l3_linf

exponential energy decay \(E_N\le (1-h\nu )^NE_0\)

proved (Lean)

decay

forced Serrin bound

proved (Lean)

serrin_forced

Young \(4/3\)–\(4\) (SOS), 3D Riccati step

proved (Lean)

young34, step34

Riccati comparison for monotone growth; cubic case

proved (Lean)

riccati_gen, riccati3

log-convexity \(H_{j+1}^2\le H_jH_{j+2}\), every \(j\)

proved (Lean)

logconvex

forced enstrophy inequality (2D \(H^1\) with force)

proved (Lean)

forced_step

De Giorgi iteration \(QU_{k+1}\le C^kU_k^2\Rightarrow C^kU_k\le U_0\)

proved (Lean)

degiorgi_iteration

implicit-viscosity Galerkin: small-data \(H^1\)–\(H^4\) bounds, uniform in the truncation

proved (Lean)

enst_global, h2_global, h3_global, h4_global

step-size Cauchy (\(2k\) vs \(k\)), dyadic convergence with explicit modulus per mode

proved (Lean)

step_cauchy, dyadic, coef_cauchy

discrete energy inequality with \(O(h)\) error, accumulated

proved (Lean)

energy_step, energy_sum

global energy bound for arbitrary data under a CFL condition (fixed truncation)

proved (Lean)

cfl_global

Leray solutions; BKM; ESS; CKN; Vasseur; CF; BdVB; NRS; Tsai; Tao 2016, 2019

cited

NS_MAP.md

Euler blowup with boundary (computer-assisted); \(C^{1,\alpha }\) Euler blowup; forced non-uniqueness

cited

[1765, 1781, 1787]

dyadic model: bounded for \(\alpha \ge 0.4\), cascade to the cutoff for \(\alpha \le 0.3\)

measured

NUMERIC_NS.txt

\(\|u(t)\|_{L^3}\) never above its initial value; \(cf^*=O(1)\); no blowup signature in the runs

measured

NUMERIC_NS.txt

a priori bound of \(\|u\|_{L^3}\) (R1)

remaining

r1_critical_bound

\(\varepsilon \)-regularity at the energy level (R2)

remaining

r2_supercritical_eps

Hölder-\(\frac 12\) bound of the vorticity direction (R3)

remaining

r3_direction_holder

profile with \(c_l\in [\frac 25,\frac 12)\), or its exclusion (R4)

remaining

r4_no_profile

kinetic convergence controlling \(\|\omega \|_\infty \) (R5)

remaining

r5_kinetic_regularity

global smoothness of 3D Navier–Stokes

remaining

—

79.243 Machine check

The Lean file of this section is NSChain.lean (Listing 193). In AXIOMS.txt it has \(28\) theorems, and all \(28\) #print axioms lines read “does not depend on any axioms”. It imports DetChain.lean for the commutative ring CRing. The numbers come from numeric_ns.py (Listing 194); its output is NUMERIC_NS.txt and the figure fig_ns_runs.pdf.

Listing 193: NSChain.lean: energy neutrality, discrete energy identity, global bounds, De Giorgi iteration

NSChain.lean

/-
  NSChain.lean
  The algebra of the Navier–Stokes energy identity, in discrete models (T37, R2/R4/R5).

  Lean 4 core plus DetChain (`CRing`: a commutative ring given by its operations and
  laws, and the ring lemmas proved there). Proofs are hand-written rewriting and
  induction; no omega / simp / ring / decide / norm_num / linarith.

  Every statement holds in every commutative ring (ℤ, ℚ, ℝ, …), and for every number
  of modes N: the models are finite truncations, the theorems are uniform in N.

  Proved here:
    sumN_add, sumN_zero, sumN_neg, sumN_mul_left    sums over 0..N-1
    telescope                    Σ_{n<N} (g n − g (n+1)) = g 0 − g N
    dyadic_energy_neutral        the dyadic (Desnyansky–Novikov / Katz–Pavlović) nonlinearity
                                 Σ a_{n+1}(c_n a_n² − c_{n+1} a_{n+1} a_{n+2}) = 0 for every N, c
    lsum_*, double_antisym       Σ_i Σ_j T i j = 0 when T j i = −T i j and T i i = 0
    galerkin_neutral             every Galerkin truncation with b(u,v,w) = −b(u,w,v):
                                 Σ_i a_i Σ_j a_j Σ_k C i j k a_k = 0
    sq_diff                      a'² = a² + (a' − a)(a' + a)
    cn_energy                    Crank–Nicolson step of a viscous energy-neutral system:
                                 E' + h ν Σ k_n s_n² = E   (s = a' + a), exactly
    le_trans', dissipation_nonneg, cn_decreasing    with an order (nonnegative cone):
                                 E' ≤ E at every step
    sq_sum_nonneg, mode_le_energy                   a_m² ≤ E for every mode m < N
    cn_global                    all steps t, all modes m: a_t(m)² ≤ E(a_0)
                                 (global boundedness of every discrete viscous model,
                                 uniform in t and N: the "regularity" of the truncation)
    degiorgi_iteration           Nat: Q U_{k+1} ≤ C^k U_k², C U_0 ≤ Q, Q > 0
                                 ⇒ C^k U_k ≤ U_0 for all k (De Giorgi's iteration lemma, β = 2)
    dyadic_example               a 3-shell integer check of dyadic_energy_neutral (rfl)
-/
import DetChain

namespace NSChain
open DetChain

section Ring
variable {R : Type} (K : CRing R)

/-! ## Sums over 0..N-1 -/

def sumN (f : Nat → R) : Nat → R
  | 0 => K.zero
  | n + 1 => K.add (sumN f n) (f n)

theorem sumN_add (f g : Nat → R) :
    ∀ N, sumN K (fun n => K.add (f n) (g n)) N = K.add (sumN K f N) (sumN K g N)
  | 0 => (K.zero_add K.zero).symm
  | N + 1 => by
    show K.add (sumN K (fun n => K.add (f n) (g n)) N) (K.add (f N) (g N))
      = K.add (K.add (sumN K f N) (f N)) (K.add (sumN K g N) (g N))
    rw [sumN_add f g N, add_mid4 K]

theorem sumN_zero : ∀ N, sumN K (fun _ => K.zero) N = K.zero
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K (fun _ => K.zero) N) K.zero = K.zero
    rw [sumN_zero N, K.zero_add]

theorem sumN_ext (f g : Nat → R) (h : ∀ n, f n = g n) : ∀ N, sumN K f N = sumN K g N
  | 0 => rfl
  | N + 1 => by
    show K.add (sumN K f N) (f N) = K.add (sumN K g N) (g N)
    rw [sumN_ext f g h N, h N]

theorem sumN_neg (f : Nat → R) (N : Nat) :
    sumN K (fun n => K.neg (f n)) N = K.neg (sumN K f N) := by
  apply neg_unique K
  rw [K.add_comm, ← sumN_add]
  rw [sumN_ext K _ (fun _ => K.zero) (fun n => K.add_neg (f n)) N]
  exact sumN_zero K N

theorem sumN_mul_left (c : R) (f : Nat → R) :
    ∀ N, sumN K (fun n => K.mul c (f n)) N = K.mul c (sumN K f N)
  | 0 => (mul_zero K c).symm
  | N + 1 => by
    show K.add (sumN K (fun n => K.mul c (f n)) N) (K.mul c (f N))
      = K.mul c (K.add (sumN K f N) (f N))
    rw [sumN_mul_left c f N, K.distrib]

/-- Telescoping. -/
theorem telescope (g : Nat → R) :
    ∀ N, sumN K (fun n => K.add (g n) (K.neg (g (n + 1)))) N = K.add (g 0) (K.neg (g N))
  | 0 => (K.add_neg (g 0)).symm
  | N + 1 => by
    show K.add (sumN K (fun n => K.add (g n) (K.neg (g (n + 1)))) N) (K.add (g N) (K.neg (g (N + 1))))
      = K.add (g 0) (K.neg (g (N + 1)))
    rw [telescope g N, K.add_assoc, ← K.add_assoc (K.neg (g N)), add_neg' K, K.zero_add]

/-! ## The dyadic model -/

/-- The dyadic nonlinearity of shell n+1 (shell 0 is the boundary a 0 = 0):
    c_n a_n² − c_{n+1} a_{n+1} a_{n+2}. With c_n = λ^n this is the Desnyansky–Novikov /
    Katz–Pavlović model. -/
def dyN (c a : Nat → R) (n : Nat) : R :=
  K.add (K.mul (c n) (K.mul (a n) (a n))) (K.neg (K.mul (c (n + 1)) (K.mul (a (n + 1)) (a (n + 2)))))

theorem mul_neg' (a b : R) : K.mul a (K.neg b) = K.neg (K.mul a b) := by
  rw [K.mul_comm, neg_mul_left K, K.mul_comm]

/-- Energy flux through shell n: g n = c_n a_n² a_{n+1}. -/
def flux (c a : Nat → R) (n : Nat) : R := K.mul (K.mul (c n) (K.mul (a n) (a n))) (a (n + 1))

theorem dyadic_term (c a : Nat → R) (n : Nat) :
    K.mul (a (n + 1)) (dyN K c a n) = K.add (flux K c a n) (K.neg (flux K c a (n + 1))) := by
  unfold dyN flux
  rw [K.distrib, mul_neg', K.mul_comm (a (n + 1)) (K.mul (c n) (K.mul (a n) (a n)))]
  -- a_{n+1} (c_{n+1} (a_{n+1} a_{n+2})) = c_{n+1} (a_{n+1} a_{n+1}) a_{n+2}
  have e : K.mul (a (n + 1)) (K.mul (c (n + 1)) (K.mul (a (n + 1)) (a (n + 2))))
      = K.mul (K.mul (c (n + 1)) (K.mul (a (n + 1)) (a (n + 1)))) (a (n + 1 + 1)) := by
    rw [← K.mul_assoc, K.mul_comm (a (n + 1)) (c (n + 1)), K.mul_assoc, K.mul_assoc,
      ← K.mul_assoc (a (n + 1)) (a (n + 1))]
  rw [e]

/-- The dyadic nonlinearity is energy neutral, for every N and every coefficient sequence. -/
theorem dyadic_energy_neutral (c a : Nat → R) (N : Nat) (h0 : a 0 = K.zero)
    (hN : a (N + 1) = K.zero) :
    sumN K (fun n => K.mul (a (n + 1)) (dyN K c a n)) N = K.zero := by
  rw [sumN_ext K _ _ (dyadic_term K c a) N, telescope]
  have g0 : flux K c a 0 = K.zero := by
    unfold flux
    rw [h0, mul_zero K, mul_zero K, zero_mul']
  have gN : flux K c a N = K.zero := by
    unfold flux
    rw [hN, mul_zero K]
  rw [g0, gN, K.zero_add]
  exact (neg_unique K K.zero K.zero (K.zero_add K.zero)).symm

/-! ## Galerkin truncations: an antisymmetric trilinear form -/

def lsum {I : Type} (f : I → R) : List I → R
  | [] => K.zero
  | x :: l => K.add (f x) (lsum f l)

theorem lsum_add {I : Type} (f g : I → R) :
    ∀ l : List I, lsum K (fun i => K.add (f i) (g i)) l = K.add (lsum K f l) (lsum K g l)
  | [] => (K.zero_add K.zero).symm
  | x :: l => by
    show K.add (K.add (f x) (g x)) (lsum K (fun i => K.add (f i) (g i)) l)
      = K.add (K.add (f x) (lsum K f l)) (K.add (g x) (lsum K g l))
    rw [lsum_add f g l, add_mid4 K]

theorem lsum_ext {I : Type} (f g : I → R) (h : ∀ i, f i = g i) :
    ∀ l : List I, lsum K f l = lsum K g l
  | [] => rfl
  | x :: l => by
    show K.add (f x) (lsum K f l) = K.add (g x) (lsum K g l)
    rw [h x, lsum_ext f g h l]

theorem lsum_zero {I : Type} : ∀ l : List I, lsum K (fun _ => K.zero) l = K.zero
  | [] => rfl
  | _ :: l => by
    show K.add K.zero (lsum K (fun _ => K.zero) l) = K.zero
    rw [lsum_zero l, K.zero_add]

theorem lsum_neg {I : Type} (f : I → R) (l : List I) :
    lsum K (fun i => K.neg (f i)) l = K.neg (lsum K f l) := by
  apply neg_unique K
  rw [K.add_comm, ← lsum_add]
  rw [lsum_ext K _ (fun _ => K.zero) (fun i => K.add_neg (f i)) l]
  exact lsum_zero K l

theorem lsum_mul_left {I : Type} (c : R) (f : I → R) :
    ∀ l : List I, lsum K (fun i => K.mul c (f i)) l = K.mul c (lsum K f l)
  | [] => (mul_zero K c).symm
  | x :: l => by
    show K.add (K.mul c (f x)) (lsum K (fun i => K.mul c (f i)) l)
      = K.mul c (K.add (f x) (lsum K f l))
    rw [lsum_mul_left c f l, K.distrib]

/-- Σ_{i∈l} Σ_{j∈l} T i j = 0 when T is antisymmetric with zero diagonal. -/
theorem double_antisym {I : Type} (T : I → I → R) (hanti : ∀ i j, T j i = K.neg (T i j))
    (hdiag : ∀ i, T i i = K.zero) :
    ∀ l : List I, lsum K (fun i => lsum K (fun j => T i j) l) l = K.zero := by
  -- the general statement with the row list growing in front
  have row : ∀ (x : I) (l : List I),
      lsum K (fun i => lsum K (fun j => T i j) (x :: l)) l
        = K.add (K.neg (lsum K (fun j => T x j) l)) (lsum K (fun i => lsum K (fun j => T i j) l) l) := by
    intro x l
    have e : ∀ i, lsum K (fun j => T i j) (x :: l) = K.add (T i x) (lsum K (fun j => T i j) l) :=
      fun _ => rfl
    rw [lsum_ext K _ _ e l, lsum_add]
    rw [lsum_ext K (fun i => T i x) (fun i => K.neg (T x i)) (fun i => hanti x i) l, lsum_neg]
  intro l
  induction l with
  | nil => rfl
  | cons x l ih =>
    show K.add (lsum K (fun j => T x j) (x :: l)) (lsum K (fun i => lsum K (fun j => T i j) (x :: l)) l)
      = K.zero
    rw [row x l, ih]
    show K.add (K.add (T x x) (lsum K (fun j => T x j) l))
      (K.add (K.neg (lsum K (fun j => T x j) l)) K.zero) = K.zero
    rw [hdiag x, K.zero_add, add_zero', K.add_neg]

/-- Galerkin energy neutrality: if the interaction coefficients satisfy
    C j i k = −C i j k (the identity b(u,v,w) = −b(u,w,v) of div-free fields,
    written in a basis) and C i i k = 0, then Σ_i a_i N_i(a) = 0 with
    N_i(a) = Σ_j a_j Σ_k C i j k a_k. Every finite mode set, every ring. -/
theorem galerkin_neutral {I : Type} (C : I → I → I → R) (a : I → R)
    (hanti : ∀ i j k, C j i k = K.neg (C i j k)) (hdiag : ∀ i k, C i i k = K.zero) (l : List I) :
    lsum K (fun i => K.mul (a i) (lsum K (fun j => K.mul (a j)
      (lsum K (fun k => K.mul (C i j k) (a k)) l)) l)) l = K.zero := by
  let D : I → I → R := fun i j => lsum K (fun k => K.mul (C i j k) (a k)) l
  let T : I → I → R := fun i j => K.mul (a i) (K.mul (a j) (D i j))
  have hD : ∀ i j, D j i = K.neg (D i j) := by
    intro i j
    show lsum K (fun k => K.mul (C j i k) (a k)) l = K.neg (lsum K (fun k => K.mul (C i j k) (a k)) l)
    rw [← lsum_neg]
    apply lsum_ext
    intro k
    rw [hanti i j k, neg_mul_left K]
  have hT : ∀ i j, T j i = K.neg (T i j) := by
    intro i j
    show K.mul (a j) (K.mul (a i) (D j i)) = K.neg (K.mul (a i) (K.mul (a j) (D i j)))
    rw [hD i j, mul_neg', mul_neg', ← K.mul_assoc, K.mul_comm (a j) (a i), K.mul_assoc]
  have hT0 : ∀ i, T i i = K.zero := by
    intro i
    show K.mul (a i) (K.mul (a i) (lsum K (fun k => K.mul (C i i k) (a k)) l)) = K.zero
    rw [lsum_ext K _ (fun _ => K.zero) (fun k => by rw [hdiag i k, zero_mul']) l, lsum_zero,
      mul_zero K, mul_zero K]
  have e : ∀ i, K.mul (a i) (lsum K (fun j => K.mul (a j) (D i j)) l)
      = lsum K (fun j => T i j) l := fun i => (lsum_mul_left K (a i) _ l).symm
  show lsum K (fun i => K.mul (a i) (lsum K (fun j => K.mul (a j) (D i j)) l)) l = K.zero
  rw [lsum_ext K _ _ e l]
  exact double_antisym K T hT hT0 l

/-! ## A Crank–Nicolson step: the exact discrete energy identity -/

theorem sq_diff (a' a : R) :
    K.mul a' a' = K.add (K.mul a a) (K.mul (K.add a' (K.neg a)) (K.add a' a)) := by
  -- (a' − a)(a' + a) = a'a' + a'a − a a' − a a
  rw [right_distrib K, K.distrib, K.distrib, neg_mul_left K, neg_mul_left K,
    K.mul_comm a' a]
  -- a a + ((a'a' + a a') + (−(a a') + −(a a)))
  rw [K.add_assoc (K.mul a' a') (K.mul a a'), ← K.add_assoc (K.mul a a') (K.neg (K.mul a a')),
    K.add_neg, K.zero_add, K.add_comm (K.mul a' a') (K.neg (K.mul a a)), ← K.add_assoc,
    K.add_neg, K.zero_add]

/-- One Crank–Nicolson step of the viscous system  a' − a = h (−ν k ⊙ s + N),
    s = a' + a, where N is energy neutral at s (Σ s_n N_n = 0; e.g. dyadic_energy_neutral
    or galerkin_neutral evaluated at s). Then E(a') + h ν Σ k_n s_n² = E(a), exactly. -/
theorem cn_energy (a a' k Nn : Nat → R) (h nu : R) (N : Nat)
    (step : ∀ n, K.add (a' n) (K.neg (a n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN K (fun n => K.mul (K.add (a' n) (a n)) (Nn n)) N = K.zero) :
    K.add (sumN K (fun n => K.mul (a' n) (a' n)) N)
      (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (K.add (a' n) (a n)) (K.add (a' n) (a n)))) N)))
      = sumN K (fun n => K.mul (a n) (a n)) N := by
  let s : Nat → R := fun n => K.add (a' n) (a n)
  let D : Nat → R := fun n => K.mul (k n) (K.mul (s n) (s n))
  have e1 : ∀ n, K.mul (a' n) (a' n)
      = K.add (K.mul (a n) (a n))
          (K.add (K.neg (K.mul h (K.mul nu (D n)))) (K.mul h (K.mul (s n) (Nn n)))) := by
    intro n
    rw [sq_diff K (a' n) (a n), step n]
    show K.add (K.mul (a n) (a n))
        (K.mul (K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (s n)))) (Nn n))) (s n)) = _
    rw [K.mul_assoc, right_distrib K, neg_mul_left K, K.distrib, mul_neg', K.mul_comm (Nn n) (s n)]
    have e : K.mul (K.mul nu (K.mul (k n) (s n))) (s n) = K.mul nu (D n) := by
      show K.mul (K.mul nu (K.mul (k n) (s n))) (s n) = K.mul nu (K.mul (k n) (K.mul (s n) (s n)))
      rw [K.mul_assoc, K.mul_assoc]
    rw [e]
  rw [sumN_ext K _ _ e1 N, sumN_add, sumN_add, sumN_neg, sumN_mul_left, sumN_mul_left,
    sumN_mul_left]
  show K.add (K.add (sumN K (fun n => K.mul (a n) (a n)) N)
      (K.add (K.neg (K.mul h (K.mul nu (sumN K D N))))
        (K.mul h (sumN K (fun n => K.mul (s n) (Nn n)) N))))
    (K.mul h (K.mul nu (sumN K D N))) = _
  have neutral' : sumN K (fun n => K.mul (s n) (Nn n)) N = K.zero := neutral
  rw [neutral', mul_zero K, add_zero', K.add_assoc, add_neg' K, add_zero']

/-! ## Order: a nonnegative cone -/

/-- The nonnegative elements of an ordered commutative ring (ℤ, ℚ, ℝ): closed under
    + and ·, containing 0 and every square. -/
structure Cone (K : CRing R) where
  nn : R → Prop
  nn_zero : nn K.zero
  nn_add : ∀ x y, nn x → nn y → nn (K.add x y)
  nn_mul : ∀ x y, nn x → nn y → nn (K.mul x y)
  nn_sq : ∀ x, nn (K.mul x x)

/-- x ≤ y : y = x + (something nonnegative). -/
def Le (P : Cone K) (x y : R) : Prop := ∃ d, P.nn d ∧ K.add x d = y

theorem le_refl' (P : Cone K) (x : R) : Le K P x x := ⟨K.zero, P.nn_zero, add_zero' K x⟩

theorem le_trans' (P : Cone K) (x y z : R) (h1 : Le K P x y) (h2 : Le K P y z) : Le K P x z := by
  cases h1 with
  | intro d1 h1 =>
    cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add d1 d2 h1.1 h2.1, ?_⟩
      rw [← K.add_assoc, h1.2, h2.2]

theorem sumN_nn (P : Cone K) (f : Nat → R) (hf : ∀ n, P.nn (f n)) : ∀ N, P.nn (sumN K f N)
  | 0 => P.nn_zero
  | N + 1 => P.nn_add _ _ (sumN_nn P f hf N) (hf N)

theorem sq_sum_nonneg (P : Cone K) (a : Nat → R) (N : Nat) :
    P.nn (sumN K (fun n => K.mul (a n) (a n)) N) :=
  sumN_nn K P _ (fun n => P.nn_sq (a n)) N

theorem dissipation_nonneg (P : Cone K) (h nu : R) (k s : Nat → R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n)) :
    P.nn (K.mul h (K.mul nu (sumN K (fun n => K.mul (k n) (K.mul (s n) (s n))) N))) :=
  P.nn_mul _ _ hh (P.nn_mul _ _ hnu (sumN_nn K P _ (fun n => P.nn_mul _ _ (hk n) (P.nn_sq (s n))) N))

/-- The energy does not increase in one Crank–Nicolson step (h, ν, k_n ≥ 0). -/
theorem cn_decreasing (P : Cone K) (a a' k Nn : Nat → R) (h nu : R) (N : Nat)
    (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (step : ∀ n, K.add (a' n) (K.neg (a n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a' n) (a n))))) (Nn n)))
    (neutral : sumN K (fun n => K.mul (K.add (a' n) (a n)) (Nn n)) N = K.zero) :
    Le K P (sumN K (fun n => K.mul (a' n) (a' n)) N) (sumN K (fun n => K.mul (a n) (a n)) N) :=
  ⟨_, dissipation_nonneg K P h nu k (fun n => K.add (a' n) (a n)) N hh hnu hk,
    cn_energy K a a' k Nn h nu N step neutral⟩

/-- Each mode is bounded by the energy: a_m² ≤ Σ_{n<N} a_n² for m < N. -/
theorem mode_le_energy (P : Cone K) (a : Nat → R) :
    ∀ N m, m < N → Le K P (K.mul (a m) (a m)) (sumN K (fun n => K.mul (a n) (a n)) N)
  | 0, _, hm => absurd hm (Nat.not_lt_zero _)
  | N + 1, m, hm => by
    cases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ hm) with
    | inl hlt =>
      cases mode_le_energy P a N m hlt with
      | intro d hd =>
        refine ⟨K.add d (K.mul (a N) (a N)), P.nn_add _ _ hd.1 (P.nn_sq (a N)), ?_⟩
        show K.add (K.mul (a m) (a m)) (K.add d (K.mul (a N) (a N)))
          = K.add (sumN K (fun n => K.mul (a n) (a n)) N) (K.mul (a N) (a N))
        rw [← K.add_assoc, hd.2]
    | inr heq =>
      refine ⟨sumN K (fun n => K.mul (a n) (a n)) N, sq_sum_nonneg K P a N, ?_⟩
      rw [heq]
      exact K.add_comm _ _

/-- Global boundedness of a discrete viscous energy-neutral model: if every time step is a
    Crank–Nicolson step as in `cn_energy` (with h, ν, k_n ≥ 0), then for every time t and
    every mode m < N, a_t(m)² ≤ E(a_0). Uniform in t and in the truncation N. -/
theorem cn_global (P : Cone K) (a : Nat → Nat → R) (k : Nat → R) (Nn : Nat → Nat → R)
    (h nu : R) (N : Nat) (hh : P.nn h) (hnu : P.nn nu) (hk : ∀ n, P.nn (k n))
    (step : ∀ t n, K.add (a (t + 1) n) (K.neg (a t n))
      = K.mul h (K.add (K.neg (K.mul nu (K.mul (k n) (K.add (a (t + 1) n) (a t n))))) (Nn t n)))
    (neutral : ∀ t, sumN K (fun n => K.mul (K.add (a (t + 1) n) (a t n)) (Nn t n)) N = K.zero) :
    (∀ t, Le K P (sumN K (fun n => K.mul (a t n) (a t n)) N) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) ∧
    (∀ t m, m < N → Le K P (K.mul (a t m) (a t m)) (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N)) := by
  have hE : ∀ t, Le K P (sumN K (fun n => K.mul (a t n) (a t n)) N)
      (sumN K (fun n => K.mul (a 0 n) (a 0 n)) N) := by
    intro t
    induction t with
    | zero => exact le_refl' K P _
    | succ t ih =>
      exact le_trans' K P _ _ _
        (cn_decreasing K P (a t) (a (t + 1)) k (Nn t) h nu N hh hnu hk (step t) (neutral t)) ih
  exact ⟨hE, fun t m hm => le_trans' K P _ _ _ (mode_le_energy K P (a t) N m hm) (hE t)⟩

end Ring

/-! ## De Giorgi's iteration lemma (β = 2), in ℕ -/

/-- If Q·U_{k+1} ≤ C^k·U_k² for all k, C·U_0 ≤ Q and Q > 0, then C^k·U_k ≤ U_0 for all k:
    the level-set energies decay geometrically (U_k ≤ U_0 / C^k), so U_k → 0 when C ≥ 2.
    This is the step that turns ε-smallness into boundedness in De Giorgi's method;
    for Navier–Stokes the exponent is β = 5/3 (Vasseur 2007) instead of 2. -/
theorem degiorgi_iteration (U : Nat → Nat) (C Q : Nat) (hQ : 0 < Q)
    (hrec : ∀ k, Q * U (k + 1) ≤ C ^ k * (U k * U k)) (h0 : C * U 0 ≤ Q) :
    ∀ k, C ^ k * U k ≤ U 0
  | 0 => by rw [Nat.pow_zero, Nat.one_mul]; exact Nat.le_refl _
  | k + 1 => by
    have ih := degiorgi_iteration U C Q hQ hrec h0 k
    apply Nat.le_of_mul_le_mul_left _ hQ
    -- Q (C^{k+1} U_{k+1}) = C^{k+1} (Q U_{k+1}) ≤ C^{k+1} C^k U_k² = C (C^k U_k)(C^k U_k) ≤ C U0 U0 ≤ Q U0
    have e1 : Q * (C ^ (k + 1) * U (k + 1)) = C ^ (k + 1) * (Q * U (k + 1)) := by
      rw [← FTAChain.nmul_assoc, FTAChain.nmul_comm Q (C ^ (k + 1)), FTAChain.nmul_assoc]
    have e2 : C ^ (k + 1) * (C ^ k * (U k * U k)) = C * (C ^ k * U k) * (C ^ k * U k) := by
      have e3 : C ^ k * (C ^ k * (U k * U k)) = C ^ k * U k * (C ^ k * U k) := by
        rw [FTAChain.nmul_assoc (C ^ k) (U k), FTAChain.nmul_comm (U k) (C ^ k * U k),
          FTAChain.nmul_assoc (C ^ k) (U k) (U k)]
      rw [Nat.pow_succ', FTAChain.nmul_assoc, e3, ← FTAChain.nmul_assoc]
    rw [e1]
    apply Nat.le_trans (Nat.mul_le_mul_left (C ^ (k + 1)) (hrec k))
    rw [e2]
    apply Nat.le_trans (Nat.mul_le_mul (Nat.mul_le_mul_left C ih) ih)
    exact Nat.mul_le_mul_right (U 0) h0

/-! ## A finite check over ℤ-like integers: three shells, c_n = 2^n -/

def dyadicInt (c a : Nat → Int) (N : Nat) : Int :=
  (List.range N).foldl (fun acc n => acc + a (n + 1) * (c n * (a n * a n) - c (n + 1) * (a (n + 1) * a (n + 2)))) 0

def shells : Nat → Int
  | 1 => 3
  | 2 => -5
  | 3 => 7
  | _ => 0

theorem dyadic_example : dyadicInt (fun n => 2 ^ n) shells 3 = 0 := rfl

end NSChain

#print axioms NSChain.sumN_add
#print axioms NSChain.sumN_zero
#print axioms NSChain.sumN_ext
#print axioms NSChain.sumN_neg
#print axioms NSChain.sumN_mul_left
#print axioms NSChain.telescope
#print axioms NSChain.mul_neg'
#print axioms NSChain.dyadic_term
#print axioms NSChain.dyadic_energy_neutral
#print axioms NSChain.lsum_add
#print axioms NSChain.lsum_ext
#print axioms NSChain.lsum_zero
#print axioms NSChain.lsum_neg
#print axioms NSChain.lsum_mul_left
#print axioms NSChain.double_antisym
#print axioms NSChain.galerkin_neutral
#print axioms NSChain.sq_diff
#print axioms NSChain.cn_energy
#print axioms NSChain.le_refl'
#print axioms NSChain.le_trans'
#print axioms NSChain.sumN_nn
#print axioms NSChain.sq_sum_nonneg
#print axioms NSChain.dissipation_nonneg
#print axioms NSChain.cn_decreasing
#print axioms NSChain.mode_le_energy
#print axioms NSChain.cn_global
#print axioms NSChain.degiorgi_iteration
#print axioms NSChain.dyadic_example

Listing 194: numeric_ns.py: spectral Navier–Stokes on the GPU, dyadic model

numeric_ns.py

#!/usr/bin/env python3
"""T37 R3/R4 numerics: 3D incompressible Navier-Stokes on the periodic box [0, 2pi]^3.

Pseudo-spectral, 2/3 dealiasing, rotational form u x omega, Leray projection,
RK4 with the exact viscous integrating factor, CFL time step.  cupy on a GPU
(Kamiak H100), numpy otherwise.  Every quantity below is computed here; nothing
is taken from the literature.

Usage
  numeric_ns.py run  CASE N NU TMAX OUTBASE [PARAM [STATEDIR]]   one run, diagnostics every DTOUT;
                     OUTBASE.meas.jsonl, OUTBASE.ckpt.json (resumable), OUTBASE.json at the end
  numeric_ns.py scan OUTDIR N NU TMAX                   the anti-parallel family (R4 search)
  numeric_ns.py report DATADIR                          writes NUMERIC_NS.txt + figures

CASE: tg (Taylor-Green), ap (anti-parallel vortex tubes, PARAM = perturbation amplitude),
      kerr (ap with the Kerr-type smaller separation).

Diagnostics per output time
  E = 1/2 <|u|^2>, Z = 1/2 <|w|^2>, wmax = max|w|, u3 = ||u||_{L^3} (critical norm),
  umax, BKM integral of wmax, k_max * eta (resolution; eta = (nu^3/eps)^(1/4), eps = 2 nu Z),
  CF: max |grad xi| over the set {|w| > wmax/2}, xi = w/|w| (Constantin-Fefferman depletion),
      and the scale-free product cf* = max|grad xi| * sqrt(nu / wmax),
  self-similarity test: 1/wmax and 1/(wmax)^(1/2)... recorded so that a blowup rate
      wmax ~ (T - t)^(-1) shows as 1/wmax linear in t.
"""
import json
import math
import os
import sys
import time

try:
    import cupy as xp
    GPU = True
except Exception:  # pragma: no cover
    import numpy as xp
    GPU = False
import numpy as np


def grid(N):
    k1 = xp.fft.fftfreq(N, 1.0 / N)
    kz = xp.fft.rfftfreq(N, 1.0 / N)
    # broadcastable (N,1,1), (1,N,1), (1,1,N/2+1): saves 3 full arrays (cycle 100, N >= 640 on 80 GB)
    KX, KY, KZ = k1[:, None, None], k1[None, :, None], kz[None, None, :]
    K2 = KX ** 2 + KY ** 2 + KZ ** 2
    K2i = xp.where(K2 == 0, 1.0, K2)
    kc = N / 3.0
    dealias = (xp.abs(KX) < kc) & (xp.abs(KY) < kc) & (xp.abs(KZ) < kc)
    return KX, KY, KZ, K2, K2i, dealias


class NS:
    def __init__(self, N, nu):
        self.N, self.nu = N, nu
        self.KX, self.KY, self.KZ, self.K2, self.K2i, self.D = grid(N)
        x = xp.arange(N) * (2 * math.pi / N)
        self.X, self.Y, self.Z = x[:, None, None], x[None, :, None], x[None, None, :]

    def fwd(self, f):
        return xp.fft.rfftn(f)

    def inv(self, F):
        return xp.fft.irfftn(F, s=(self.N,) * 3, axes=(0, 1, 2))

    def proj(self, U):
        ux, uy, uz = U
        d = (self.KX * ux + self.KY * uy + self.KZ * uz) / self.K2i
        return xp.stack([ux - self.KX * d, uy - self.KY * d, uz - self.KZ * d])

    def curl_hat(self, U):
        ux, uy, uz = U
        return 1j * xp.stack([self.KY * uz - self.KZ * uy,
                              self.KZ * ux - self.KX * uz,
                              self.KX * uy - self.KY * ux])

    def nonlin(self, U, rec=False):
        u = [self.inv(U[i]) for i in range(3)]
        if rec:
            self.umax = float(xp.sqrt(u[0] ** 2 + u[1] ** 2 + u[2] ** 2).max())
        W = self.curl_hat(U)
        w = [self.inv(W[i]) for i in range(3)]
        c = [u[1] * w[2] - u[2] * w[1], u[2] * w[0] - u[0] * w[2], u[0] * w[1] - u[1] * w[0]]
        C = xp.stack([self.fwd(ci) for ci in c]) * self.D
        return self.proj(C)

    def step(self, U, dt):
        """RK4 with integrating factor exp(-nu k^2 t), stages accumulated in place (memory)."""
        E1 = xp.exp(-self.nu * self.K2 * (dt / 2))
        E2 = E1 * E1
        k = self.nonlin(U, rec=True)
        acc = E2 * k
        k = self.nonlin(E1 * (U + (dt / 2) * k))
        acc += 2 * E1 * k
        k = self.nonlin(E1 * U + (dt / 2) * k)
        acc += 2 * E1 * k
        k = self.nonlin(E2 * U + dt * E1 * k)
        acc += k
        del k
        acc *= dt / 6
        acc += E2 * U
        return acc

    def diag(self, U):
        u = xp.stack([self.inv(U[i]) for i in range(3)])
        w = xp.stack([self.inv(Wi) for Wi in self.curl_hat(U)])
        um = xp.sqrt((u ** 2).sum(0))
        wm = xp.sqrt((w ** 2).sum(0))
        vol = (2 * math.pi) ** 3
        E = 0.5 * float((um ** 2).mean())
        Z = 0.5 * float((wm ** 2).mean())
        wmax = float(wm.max())
        umax = float(um.max())
        u3 = float(((um ** 3).mean() * vol) ** (1.0 / 3.0))
        # vorticity direction xi = w/|w| and its gradient on {|w| > wmax/2}
        mask = wm > 0.5 * wmax
        xi = w / xp.where(wm > 1e-300, wm, 1.0)
        g2 = xp.zeros_like(wm)
        for i in range(3):
            Xi = self.fwd(xi[i])
            for Kd in (self.KX, self.KY, self.KZ):
                g2 += self.inv(1j * Kd * Xi * self.D) ** 2
        gxi = float(xp.sqrt(xp.where(mask, g2, 0.0)).max())
        eps = 2 * self.nu * Z
        eta = (self.nu ** 3 / eps) ** 0.25 if eps > 0 else float("inf")
        # spectrum tail: energy in the outer dealiased shell over total
        Ek = 0.5 * (xp.abs(U) ** 2).sum(0)
        kmag = xp.sqrt(self.K2)
        kc = self.N / 3.0
        tail = float((Ek * ((kmag > 0.8 * kc) & self.D)).sum() / max(float((Ek * self.D).sum()), 1e-300))
        hold = None
        if os.environ.get("NS_HOLDER") == "1":
            # R3 (Beirao da Veiga - Berselli): sup over x, x+y in {|w| > wmax/2}, 0 < |y| <= 4 dx, of
            # |sin angle(xi(x+y), xi(x))| / |y|^(1/2), and the same times (nu / wmax)^(1/4) (scale-free)
            dx = 2 * math.pi / self.N
            hold = 0.0
            for sx, sy, sz in [(1, 0, 0), (0, 1, 0), (0, 0, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 1, 1),
                               (2, 0, 0), (0, 2, 0), (0, 0, 2), (4, 0, 0), (0, 4, 0), (0, 0, 4)]:
                xs = xp.roll(xi, shift=(-sx, -sy, -sz), axis=(1, 2, 3))
                ms = xp.roll(mask, shift=(-sx, -sy, -sz), axis=(0, 1, 2))
                cr = xp.stack([xi[1] * xs[2] - xi[2] * xs[1], xi[2] * xs[0] - xi[0] * xs[2], xi[0] * xs[1] - xi[1] * xs[0]])
                sn = xp.sqrt((cr ** 2).sum(0))
                r = math.sqrt(sx * sx + sy * sy + sz * sz) * dx
                q = float(xp.where(mask & ms, sn, 0.0).max()) / math.sqrt(r)
                hold = max(hold, q)
        P = stretch = None
        if os.environ.get("NS_STRETCH") == "1":
            # T37 cycle 77: palinstrophy P = 1/2 <|grad w|^2> and stretching S = <w_i w_j d_j u_i>
            W = self.curl_hat(U)
            P = 0.0
            for i in range(3):
                for Kd in (self.KX, self.KY, self.KZ):
                    P += 0.5 * float((self.inv(1j * Kd * W[i] * self.D) ** 2).mean())
            S = xp.zeros_like(wm)
            for i in range(3):
                for j, Kd in enumerate((self.KX, self.KY, self.KZ)):
                    S += w[i] * w[j] * self.inv(1j * Kd * U[i] * self.D)
            stretch = float(S.mean())
        return dict(E=E, Z=Z, wmax=wmax, umax=umax, u3=u3, gxi=gxi, hold=hold, P=P, stretch=stretch,
                    cstar=(stretch / (Z ** 0.75 * P ** 0.75)) if (P and stretch is not None and Z > 0) else None,
                    holdstar=(hold * (self.nu / wmax) ** 0.25) if (hold is not None and self.nu > 0) else None,
                    cfstar=gxi * math.sqrt(self.nu / wmax) if self.nu > 0 else None,
                    keta=kc * eta, tail=tail)


def init(ns, case, param):
    X, Y, Z = ns.X, ns.Y, ns.Z
    if case == "tg":
        u = xp.stack([xp.sin(X) * xp.cos(Y) * xp.cos(Z), -xp.cos(X) * xp.sin(Y) * xp.cos(Z), 0 * X])
        U = xp.stack([ns.fwd(c) for c in u])
    else:
        # two anti-parallel Gaussian tubes along z, x = pi -+ d, y displaced by a sin(z) bump
        a = 0.25 * (2 * math.pi) / 6.0 if case == "ap" else 0.2
        d = 0.65 if case == "ap" else 0.45
        amp = param
        wz = xp.zeros((ns.N,) * 3)
        for s, x0 in ((1.0, math.pi - d), (-1.0, math.pi + d)):
            xc = x0 - s * amp * xp.cos(Z)          # tubes bend towards each other at z = 0
            r2 = (X - xc) ** 2 + (Y - math.pi) ** 2
            wz = wz + s * xp.exp(-r2 / a ** 2)
        # tilt: w = (dxc/dz-part along x) so that div w = 0 after projection
        Wz = ns.fwd(wz)
        Wx = xp.zeros_like(Wz)
        Wy = xp.zeros_like(Wz)
        W = ns.proj(xp.stack([Wx, Wy, Wz]))       # divergence-free vorticity
        # Biot-Savart: u_hat = i k x w_hat / k^2
        KX, KY, KZ, K2i = ns.KX, ns.KY, ns.KZ, ns.K2i
        U = 1j * xp.stack([KY * W[2] - KZ * W[1], KZ * W[0] - KX * W[2], KX * W[1] - KY * W[0]]) / K2i
        U[:, 0, 0, 0] = 0
        # normalise to wmax0 = 10 (nondimensional; Re = umax*1/nu reported)
        w0 = ns.diag(U)["wmax"]
        U = U * (10.0 / w0)
    return ns.proj(U) * ns.D


def save_state(U, path):
    tmp = path + ".tmp.npy"
    if GPU:
        np.save(tmp, xp.asnumpy(U))
    else:
        np.save(tmp, U)
    os.replace(tmp, path)


def run(case, N, nu, tmax, out, param=0.0, dtout=0.05, cfl=0.5, wall=None, state_dir=None, ck_every=20):
    """Writes OUT.meas.jsonl (one diagnostics line per output time, appended), OUT.ckpt.json
    (small: time, BKM integral, number of records, path of the field file) every ck_every outputs,
    the field itself to STATE_DIR/<name>.state.npy (scratch; large), and OUT.json at the end.
    A rerun with the same OUT resumes from the checkpoint if its field file still exists."""
    print("GPU", GPU, flush=True)
    ns = NS(N, nu)
    name = os.path.basename(out)
    meas, ckpt = out + ".meas.jsonl", out + ".ckpt.json"
    state = os.path.join(state_dir or os.path.dirname(out) or ".", name + ".state.npy")
    rec, t, bkm = [], 0.0, 0.0
    U = None
    if os.path.exists(ckpt) and os.path.exists(state) and os.path.exists(meas):
        with open(ckpt) as fh:
            C = json.load(fh)
        with open(meas) as fh:
            rec = [json.loads(line) for line in fh][:C["nrec"]]
        U = xp.asarray(np.load(state))
        t, bkm = C["t"], C["bkm"]
        print(f"resume from t={t:.3f} ({len(rec)} records)", flush=True)
        with open(meas, "w") as fh:
            for r in rec:
                fh.write(json.dumps(r) + "\n")
    if U is None:
        U = init(ns, case, param)
        d = ns.diag(U)
        d["t"], d["bkm"] = 0.0, 0.0
        rec = [d]
        with open(meas, "w") as fh:
            fh.write(json.dumps(d) + "\n")
    d = rec[-1]
    dx = 2 * math.pi / N
    tnext, last_w, start = t + dtout, d["wmax"], time.time()
    ns.umax = d["umax"]
    while t < tmax - 1e-12:
        dt = min(cfl * dx / max(ns.umax, 1e-6), tnext - t, 0.05)
        U = ns.step(U, dt)
        t += dt
        if t >= tnext - 1e-12:
            d = ns.diag(U)
            bkm += 0.5 * (last_w + d["wmax"]) * dtout
            last_w = d["wmax"]
            d["t"], d["bkm"] = t, bkm
            rec.append(d)
            with open(meas, "a") as fh:
                fh.write(json.dumps(d) + "\n")
            tnext += dtout
            print(f"t={t:.3f} E={d['E']:.5f} Z={d['Z']:.3f} wmax={d['wmax']:.3f} u3={d['u3']:.4f} "
                  f"gxi={d['gxi']:.2f} keta={d['keta']:.2f} tail={d['tail']:.1e} [{time.time()-start:.0f}s]",
                  flush=True)
            if len(rec) % ck_every == 0:
                save_state(U, state)
                tmpc = ckpt + ".tmp"
                with open(tmpc, "w") as fh:
                    json.dump(dict(t=t, bkm=bkm, nrec=len(rec), state=state), fh)
                os.replace(tmpc, ckpt)
            if not all(math.isfinite(v) for v in (d["E"], d["wmax"])):
                break
            if wall and time.time() - start > wall:
                break
    if t < tmax - 1e-12:
        return                                   # interrupted: resume later from OUT.ckpt.json
    meta = dict(case=case, N=N, nu=nu, tmax=tmax, param=param, gpu=GPU, dtout=dtout, cfl=cfl)
    tmp = out + ".json.tmp"
    with open(tmp, "w") as fh:
        json.dump(dict(meta=meta, rec=rec), fh)
    os.replace(tmp, out + ".json")
    if os.path.exists(state):
        os.remove(state)


# ------------------------------------------------------------------ analysis (numpy only)

def selfsim_fit(rec, frac=0.5):
    """Fit 1/wmax = c (T - t) on the growth phase where wmax > frac * peak, before the peak.
    Returns (T, c, rms relative residual, t_peak, peak). A finite-time blowup with rate (T-t)^-1
    gives a straight line whose zero T lies beyond the data; saturation shows as the peak."""
    t = np.array([r["t"] for r in rec])
    w = np.array([r["wmax"] for r in rec])
    ip = int(np.argmax(w))
    sel = (w > frac * w[ip]) & (np.arange(len(w)) <= ip)
    if sel.sum() < 4:
        return None
    A = np.vstack([t[sel], np.ones(sel.sum())]).T
    y = 1.0 / w[sel]
    (m, b), *_ = np.linalg.lstsq(A, y, rcond=None)
    T = -b / m if m != 0 else float("inf")
    res = float(np.sqrt(np.mean(((A @ np.array([m, b])) - y) ** 2)) / np.mean(y))
    return T, -m, res, float(t[ip]), float(w[ip])


def report(datadir):
    here = os.path.dirname(os.path.abspath(__file__))
    files = sorted(f for f in os.listdir(datadir) if f.endswith(".json") and f != "dyadic.json" and not f.endswith(".ckpt.json"))
    runs = []
    for f in files:
        with open(os.path.join(datadir, f)) as fh:
            runs.append((f, json.load(fh)))
    L = []
    L.append("NUMERIC_NS.txt  (T37; numeric_ns.py; 3D periodic Navier-Stokes, pseudo-spectral, RK4 + integrating factor)")
    L.append("columns: run | N | nu | t_end | peak wmax at t | E(t_end)/E0 | max u3 / u3(0) | BKM int | min k_max*eta | max tail")
    L.append("         | CF: max|grad xi| on {|w|>wmax/2} at peak, cf* = that * sqrt(nu/wmax) | fit 1/wmax = c(T-t): T, rms")
    for f, R in runs:
        m, rec = R["meta"], R["rec"]
        w = [r["wmax"] for r in rec]
        ip = int(np.argmax(w))
        fit = selfsim_fit(rec)
        u30 = rec[0]["u3"]
        L.append(f"{f[:-5]:<24} N={m['N']:<4} nu={m['nu']:.2e} t_end={rec[-1]['t']:.2f} "
                 f"peak={w[ip]:.2f}@{rec[ip]['t']:.2f} E/E0={rec[-1]['E']/rec[0]['E']:.3f} "
                 f"u3max/u3_0={max(r['u3'] for r in rec)/u30:.3f} BKM={rec[-1]['bkm']:.2f} "
                 f"keta_min={min(r['keta'] for r in rec[1:]):.2f} tail_max={max(r['tail'] for r in rec):.1e}")
        cfs = rec[ip]["cfstar"]
        L.append(f"{'':<24} CF gxi@peak={rec[ip]['gxi']:.2f} cf*={cfs if cfs is None else round(cfs, 3)}"
                 + (f"  fit: T={fit[0]:.3f} c={fit[1]:.4f} rms={fit[2]:.3f}" if fit else "  fit: -"))
    dpath = os.path.join(datadir, "dyadic.json")
    if os.path.exists(dpath):
        with open(dpath) as fh:
            D = json.load(fh)
        L.append("")
        L.append("Dyadic model (NSChain.dyN, lam = 2, nu = 1, a_1(0) = 20): a_n' = -nu lam^(2 alpha n) a_n + lam^(n-1) a_(n-1)^2 - lam^n a_n a_(n+1)")
        L.append("alpha | shells | peak H1 = (sum 4^n a_n^2)^(1/2) | t_peak | energy share of last 4 shells at peak | E(t_end)/E0 | t_end (steps)")
        for r in D:
            L.append(f"{r['alpha']:<5} {r['nsh']:>3}  {r['peak']:.4e}  {r['tpeak']:.4f}  {r['tail_at_peak']:.2e}  "
                     f"{r['E_end_over_E0']:.3e}  {r['t_end']:.3f} ({r['steps']})")
    txt = "\n".join(L) + "\n"
    with open(os.path.join(here, "NUMERIC_NS.txt"), "w") as fh:
        fh.write(txt)
    print(txt)
    try:
        import matplotlib
        matplotlib.use("Agg")
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots(1, 3, figsize=(12, 3.6))
        for f, R in runs:
            rec = R["rec"]
            t = [r["t"] for r in rec]
            lab = f[:-5]
            ax[0].plot(t, [r["wmax"] for r in rec], label=lab, lw=1)
            ax[1].plot(t, [r["u3"] / rec[0]["u3"] for r in rec], lw=1)
            ax[2].plot(t, [1 / r["wmax"] for r in rec], lw=1)
        ax[0].set_ylabel(r"$\|\omega\|_\infty$"); ax[0].set_yscale("log")
        ax[1].set_ylabel(r"$\|u\|_{L^3}/\|u_0\|_{L^3}$")
        ax[2].set_ylabel(r"$1/\|\omega\|_\infty$")
        for a in ax:
            a.set_xlabel("t")
        ax[0].legend(fontsize=6)
        fig.tight_layout()
        fig.savefig(os.path.join(here, "fig_ns_runs.pdf"), dpi=300)
    except ImportError:
        print("matplotlib missing: no figure")


# ------------------------------------------------------------------ dyadic model (R5, numpy)

def dyadic(alpha, nu=1.0, lam=2.0, nsh=20, a1=None, tmax=1.0, maxsteps=400000):
    """Viscous Desnyansky-Novikov / Katz-Pavlovic dyadic model (the system of NSChain.dyN):
    a_n' = -nu lam^(2 alpha n) a_n + lam^(n-1) a_(n-1)^2 - lam^n a_n a_(n+1),  n = 1..nsh, a_0 = 0.
    Exponential (integrating factor) RK2, dt from the largest nonlinear rate. Returns the
    peak over time of H1 = (sum lam^(2n) a_n^2)^(1/2), the time of the peak, the energy ratio,
    and the share of energy in the last 4 shells at the peak (truncation check)."""
    n = np.arange(1, nsh + 1)
    ln = lam ** n.astype(float)
    k = nu * lam ** (2 * alpha * n.astype(float))
    a = np.zeros(nsh)
    a[0] = a1 if a1 is not None else 20.0

    def F(a):
        am = np.concatenate([[0.0], a[:-1]])
        ap = np.concatenate([a[1:], [0.0]])
        return (ln / lam) * am ** 2 - ln * a * ap

    t, E0 = 0.0, float((a ** 2).sum())
    peak, tpeak, tailpk = 0.0, 0.0, 0.0
    steps = 0
    while t < tmax and steps < maxsteps:
        steps += 1
        rate = float(np.max(ln * (np.abs(a) + 1e-300))) + 1e-12
        dt = min(0.05 / rate, tmax - t, 1e-3)
        Ef = np.exp(-k * dt)
        f1 = F(a)
        b = Ef * (a + dt * f1)
        a = Ef * a + 0.5 * dt * (Ef * f1 + F(b))
        t += dt
        H1 = float(np.sqrt(((ln * a) ** 2).sum()))
        if H1 > peak:
            peak, tpeak = H1, t
            tailpk = float((a[-4:] ** 2).sum() / max((a ** 2).sum(), 1e-300))
        if not np.isfinite(H1):
            break
    return dict(alpha=alpha, peak=peak, tpeak=tpeak, E_end_over_E0=float((a ** 2).sum()) / E0,
                tail_at_peak=tailpk, nsh=nsh, t_end=t, steps=steps)


def dyadic_table():
    rows = []
    for alpha in (0.2, 0.3, 0.4, 0.5, 0.75, 1.0):
        for nsh in (16, 20):
            rows.append(dyadic(alpha, nsh=nsh))
    return rows


if __name__ == "__main__":
    cmd = sys.argv[1]
    if cmd == "run":
        case, N, nu, tmax, out = sys.argv[2], int(sys.argv[3]), float(sys.argv[4]), float(sys.argv[5]), sys.argv[6]
        param = float(sys.argv[7]) if len(sys.argv) > 7 else 0.0
        state_dir = sys.argv[8] if len(sys.argv) > 8 else None
        run(case, N, nu, tmax, out, param, state_dir=state_dir)
    elif cmd == "dyadic1":
        # one long run: numeric_ns.py dyadic1 ALPHA A1 NSH MAXSTEPS OUT.json  (Kamiak CPU)
        r = dyadic(float(sys.argv[2]), a1=float(sys.argv[3]), nsh=int(sys.argv[4]), maxsteps=int(sys.argv[5]))
        with open(sys.argv[6], "w") as fh:
            json.dump(r, fh)
        print(r)
    elif cmd == "dyadic":
        out = sys.argv[2]
        with open(out, "w") as fh:
            json.dump(dyadic_table(), fh)
    elif cmd == "report":
        report(sys.argv[2])

Listing 195: NUMERIC_NS.txt

NUMERIC_NS.txt

NUMERIC_NS.txt  (T37; numeric_ns.py; 3D periodic Navier-Stokes, pseudo-spectral, RK4 + integrating factor)
columns: run | N | nu | t_end | peak wmax at t | E(t_end)/E0 | max u3 / u3(0) | BKM int | min k_max*eta | max tail
         | CF: max|grad xi| on {|w|>wmax/2} at peak, cf* = that * sqrt(nu/wmax) | fit 1/wmax = c(T-t): T, rms
ap_N256_nu2e-3_a0.00     N=256  nu=2.00e-03 t_end=6.00 [email protected] E/E0=0.821 u3max/u3_0=1.000 BKM=45.39 keta_min=4.44 tail_max=3.0e-14
                         CF gxi@peak=5.73 cf*=0.081  fit: -
ap_N256_nu2e-3_a0.15     N=256  nu=2.00e-03 t_end=6.00 [email protected] E/E0=0.813 u3max/u3_0=1.000 BKM=47.03 keta_min=4.45 tail_max=6.1e-12
                         CF gxi@peak=3.24 cf*=0.046  fit: -
ap_N256_nu2e-3_a0.15_T20 N=256  nu=2.00e-03 t_end=20.00 [email protected] E/E0=0.570 u3max/u3_0=1.000 BKM=150.75 keta_min=4.45 tail_max=4.9e-08
                         CF gxi@peak=95.25 cf*=1.276  fit: T=204.877 c=0.0007 rms=0.176
ap_N256_nu2e-3_a0.30     N=256  nu=2.00e-03 t_end=6.00 [email protected] E/E0=0.788 u3max/u3_0=1.000 BKM=55.14 keta_min=4.48 tail_max=5.4e-10
                         CF gxi@peak=42.03 cf*=0.537  fit: T=41.705 c=0.0028 rms=0.075
ap_N256_nu2e-3_a0.30_T20 N=256  nu=2.00e-03 t_end=20.00 [email protected] E/E0=0.545 u3max/u3_0=1.000 BKM=151.56 keta_min=4.48 tail_max=1.7e-07
                         CF gxi@peak=114.47 cf*=1.19  fit: T=20.078 c=0.0057 rms=0.132
ap_N256_nu2e-3_a0.45     N=256  nu=2.00e-03 t_end=6.00 [email protected] E/E0=0.765 u3max/u3_0=1.000 BKM=61.15 keta_min=4.52 tail_max=7.3e-08
                         CF gxi@peak=99.79 cf*=1.147  fit: T=21.825 c=0.0053 rms=0.086
ap_N256_nu2e-3_a0.45_T20 N=256  nu=2.00e-03 t_end=20.00 [email protected] E/E0=0.495 u3max/u3_0=1.000 BKM=150.12 keta_min=4.52 tail_max=1.9e-07
                         CF gxi@peak=109.72 cf*=1.173  fit: T=16.667 c=0.0072 rms=0.103
ap_N512_nu2e-4_a0.45_T8  N=512  nu=2.00e-04 t_end=8.00 [email protected] E/E0=0.925 u3max/u3_0=1.000 BKM=464.35 keta_min=2.16 tail_max=4.8e-05
                         CF gxi@peak=947.57 cf*=0.882  fit: -
ap_N512_nu5e-4_a0.30_T12 N=512  nu=5.00e-04 t_end=12.00 [email protected] E/E0=0.850 u3max/u3_0=1.000 BKM=291.54 keta_min=4.03 tail_max=1.2e-06
                         CF gxi@peak=457.63 cf*=1.237  fit: T=7.362 c=0.0142 rms=0.040
ap_N512_nu5e-4_a0.45_T12 N=512  nu=5.00e-04 t_end=12.00 [email protected] E/E0=0.816 u3max/u3_0=1.000 BKM=296.29 keta_min=3.85 tail_max=3.7e-06
                         CF gxi@peak=641.92 cf*=1.379  fit: T=5.902 c=0.0195 rms=0.020
kerr_N512_nu5e-4_a0.30_T12 N=512  nu=5.00e-04 t_end=12.00 [email protected] E/E0=0.772 u3max/u3_0=1.000 BKM=240.76 keta_min=4.89 tail_max=3.6e-06
                         CF gxi@peak=579.48 cf*=1.746  fit: T=8.176 c=0.0147 rms=0.038
tg_N256_Re400            N=256  nu=2.50e-03 t_end=10.00 [email protected] E/E0=0.416 u3max/u3_0=1.000 BKM=80.90 keta_min=2.95 tail_max=1.2e-06
                         CF gxi@peak=29.73 cf*=0.385  fit: T=5.729 c=0.0687 rms=0.059
tg_N512_Re1600           N=512  nu=6.25e-04 t_end=10.00 [email protected] E/E0=0.596 u3max/u3_0=1.000 BKM=273.02 keta_min=2.00 tail_max=3.9e-05
                         CF gxi@peak=146.33 cf*=0.343  fit: T=24.907 c=0.0010 rms=0.128
tg_N512_Re1600_T14       N=512  nu=6.25e-04 t_end=14.00 [email protected] E/E0=0.331 u3max/u3_0=1.000 BKM=441.21 keta_min=2.00 tail_max=3.9e-05
                         CF gxi@peak=146.33 cf*=0.343  fit: T=24.907 c=0.0010 rms=0.128
tg_N512_Re3200           N=512  nu=3.13e-04 t_end=10.00 [email protected] E/E0=0.626 u3max/u3_0=1.000 BKM=530.26 keta_min=1.14 tail_max=7.9e-04
                         CF gxi@peak=177.91 cf*=0.211  fit: T=108.270 c=0.0001 rms=0.118
tg_N512_Re800            N=512  nu=1.25e-03 t_end=10.00 [email protected] E/E0=0.518 u3max/u3_0=1.000 BKM=142.64 keta_min=3.45 tail_max=3.3e-07
                         CF gxi@peak=79.67 cf*=0.526  fit: T=16.597 c=0.0050 rms=0.118

Dyadic model (NSChain.dyN, lam = 2, nu = 1, a_1(0) = 20): a_n' = -nu lam^(2 alpha n) a_n + lam^(n-1) a_(n-1)^2 - lam^n a_n a_(n+1)
alpha | shells | peak H1 = (sum 4^n a_n^2)^(1/2) | t_peak | energy share of last 4 shells at peak | E(t_end)/E0 | t_end (steps)
0.2    16  3.7683e+05  0.0556  1.26e-01  1.302e-01  0.128 (400000)
0.2    20  3.8422e+06  0.0500  4.60e-02  7.467e-01  0.050 (400000)
0.3    16  1.1419e+05  0.0478  1.37e-02  2.257e-04  1.000 (108842)
0.3    20  7.4082e+05  0.0467  2.13e-03  2.600e-01  0.085 (400000)
0.4    16  1.2077e+04  0.0471  1.08e-03  1.395e-04  1.000 (4122)
0.4    20  1.3305e+04  0.0471  9.04e-06  1.395e-04  1.000 (3893)
0.5    16  7.2588e+02  0.0509  2.53e-17  8.997e-05  1.000 (1217)
0.5    20  7.2588e+02  0.0509  8.30e-261  8.997e-05  1.000 (1217)
0.75   16  1.0634e+02  0.0450  0.00e+00  3.245e-05  1.000 (1009)
0.75   20  1.0634e+02  0.0450  0.00e+00  3.245e-05  1.000 (1009)
1.0    16  5.9350e+01  0.0320  0.00e+00  7.580e-06  1.000 (1000)
1.0    20  5.9350e+01  0.0320  0.00e+00  7.580e-06  1.000 (1000)

79.244 Time interpolation inside one step

For \(0\le \theta \le 1\) and \(d^2\le G\): \((\theta d)^2\le \theta G\), so the piecewise-linear interpolation keeps the Hölder-\(1/2\) modulus inside a step. \(|u(k+\theta )-a_k|^2\le \theta G\) when \(|a_{k+1}-a_k|^2\le G\).

ns/InterpStep.lean

/-
  ns/InterpStep.lean — T37 cycle 1720 (goal G850a): the time interpolation of the scheme inside one step keeps the
  Hölder-1/2 modulus. Over an ordered commutative ring (cone P): if 0 ≤ θ ≤ 1 and d² ≤ G then (θd)² ≤ θG, i.e.
  for u(k + θ) = a_k + θ(a_{k+1} − a_k) one has |u(k + θ) − a_k|² ≤ θ·G whenever |a_{k+1} − a_k|² ≤ G
  (GlobalHolder with k = 1). Summed over any list of modes as well. Lean 4 core, hand proofs, no axioms.
-/
import NSChain
import lib.RingTac

namespace InterpStep
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- θ ≤ 1 and 0 ≤ x give θx ≤ x. -/
theorem mul_le_self (θ x : R) (hθ1 : Le K P θ K.one) (hx : P.nn x) : Le K P (K.mul θ x) x := by
  cases hθ1 with
  | intro e he =>
    refine ⟨K.mul e x, P.nn_mul _ _ he.1 hx, ?_⟩
    have h : K.add (K.mul θ x) (K.mul e x) = K.mul (K.add θ e) x := by lib_ring
    rw [h, he.2, K.one_mul]

/-- **One step.** 0 ≤ θ ≤ 1 and d² ≤ G give (θd)² ≤ θG. -/
theorem interp_step (θ d G : R) (hθ : P.nn θ) (hθ1 : Le K P θ K.one) (hd : Le K P (K.mul d d) G) :
    Le K P (K.mul (K.mul θ d) (K.mul θ d)) (K.mul θ G) := by
  have h1 : Le K P (K.mul θ (K.mul d d)) (K.mul d d) := mul_le_self K P θ (K.mul d d) hθ1 (P.nn_sq d)
  have h2 : Le K P (K.mul θ (K.mul d d)) G := le_trans' K P _ _ _ h1 hd
  have e1 : K.mul (K.mul θ d) (K.mul θ d) = K.mul θ (K.mul θ (K.mul d d)) := by lib_ring
  rw [e1]
  cases h2 with
  | intro e he =>
    refine ⟨K.mul θ e, P.nn_mul _ _ hθ he.1, ?_⟩
    rw [← he.2]
    lib_ring

end Gen
end InterpStep

#print axioms InterpStep.mul_le_self
#print axioms InterpStep.interp_step

#print axioms: 2 theorems, no axioms.

79.245 Time interpolation across steps

\(|u(k+j+\theta ')-u(k+\theta )|^2\le 3\bigl ((1-\theta )G+M+\theta 'G\bigr )\) from \(|a_{k+1}-a_k|^2\le G\), \(|a_{k+j+1}-a_{k+j}|^2\le G\), \(|a_{k+j}-a_{k+1}|^2\le M\). With \(M=(j-1)G\) the bound is \(3|s'-s|G\): the interpolants are uniformly Hölder-\(1/2\) in continuous time.

ns/InterpCross.lean

/-
  ns/InterpCross.lean — T37 cycle 1722 (goal G850b): the piecewise-linear time interpolation of the scheme across
  steps. With u(k + θ) = a_k + θ(a_{k+1} − a_k), θ + e = 1 (θ, e ≥ 0), and for a later step
  u(k + j + θ') = b_0 + θ'(b_1 − b_0): if |a_1 − a_0|² ≤ G, |b_1 − b_0|² ≤ G and |b_0 − a_1|² ≤ M then
      |u(k + j + θ') − u(k + θ)|² ≤ 3(eG + M + θ'G).
  With M = (j − 1)G (GlobalHolder) the right side is 3|s' − s|G: the interpolants are uniformly Hölder-1/2 in
  continuous time. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpStep

namespace InterpCross
open DetChain NSChain LibRing InterpStep

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def three : R := K.add K.one (K.add K.one K.one)

theorem nn_one : P.nn K.one := by
  have h := P.nn_sq K.one
  rw [K.one_mul] at h
  exact h

theorem nn_three : P.nn (three K) :=
  P.nn_add _ _ (nn_one K P) (P.nn_add _ _ (nn_one K P) (nn_one K P))

theorem le_add2' {x X y Y : R} (h1 : Le K P x X) (h2 : Le K P y Y) : Le K P (K.add x y) (K.add X Y) := by
  cases h1 with
  | intro d1 h1 =>
    cases h2 with
    | intro d2 h2 =>
      refine ⟨K.add d1 d2, P.nn_add _ _ h1.1 h2.1, ?_⟩
      rw [← h1.2, ← h2.2]
      lib_ring

theorem le_mul_l {x X : R} (c : R) (hc : P.nn c) (h : Le K P x X) : Le K P (K.mul c x) (K.mul c X) := by
  cases h with
  | intro d hd =>
    refine ⟨K.mul c d, P.nn_mul _ _ hc hd.1, ?_⟩
    rw [← hd.2]
    lib_ring

/-- (x + y + z)² ≤ 3(x² + y² + z²). -/
theorem three_sq (x y z : R) :
    Le K P (K.mul (K.add x (K.add y z)) (K.add x (K.add y z)))
      (K.mul (three K) (K.add (K.mul x x) (K.add (K.mul y y) (K.mul z z)))) := by
  refine ⟨K.add (K.mul (sub K x y) (sub K x y)) (K.add (K.mul (sub K y z) (sub K y z)) (K.mul (sub K x z) (sub K x z))),
    P.nn_add _ _ (P.nn_sq _) (P.nn_add _ _ (P.nn_sq _) (P.nn_sq _)), ?_⟩
  unfold three
  lib_ring

/-- **Across steps.** -/
theorem cross_step (a0 a1 b0 b1 θ e θ' e' G M : R)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (ha : Le K P (K.mul (sub K a1 a0) (sub K a1 a0)) G) (hb : Le K P (K.mul (sub K b1 b0) (sub K b1 b0)) G)
    (hm : Le K P (K.mul (sub K b0 a1) (sub K b0 a1)) M) :
    Le K P (K.mul (sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0))))
                  (sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0)))))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have hθv : θ = sub K K.one e := by rw [← hθe]; lib_ring
  have e1 : sub K (K.add b0 (K.mul θ' (sub K b1 b0))) (K.add a0 (K.mul θ (sub K a1 a0)))
      = K.add (K.mul e (sub K a1 a0)) (K.add (sub K b0 a1) (K.mul θ' (sub K b1 b0))) := by
    rw [hθv]; lib_ring
  rw [e1]
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have hx := interp_step K P e (sub K a1 a0) G he he1 ha
  have hz := interp_step K P θ' (sub K b1 b0) G hθ' hθ1 hb
  have hs := le_add2' K P hx (le_add2' K P hm hz)
  exact le_trans' K P _ _ _ (three_sq K P _ _ _) (le_mul_l K P (three K) (nn_three K P) hs)

end Gen
end InterpCross

#print axioms InterpCross.nn_three
#print axioms InterpCross.le_add2'
#print axioms InterpCross.le_mul_l
#print axioms InterpCross.three_sq
#print axioms InterpCross.cross_step

#print axioms: 5 theorems, no axioms.

79.246 From the grid Hölder bound to the continuous-time interpolant

If \(|a_{k+j}-a_k|^2\le jG\) for all \(k,j\), the interpolant \(u(k+\theta )=a_k+\theta (a_{k+1}-a_k)\) satisfies \(|u(k+1+j+\theta ')-u(k+\theta )|^2\le 3(1-\theta +j+\theta ')G\). Inside one step \(|u(k+\theta )-a_k|^2\le \theta G\).

ns/InterpGrid.lean

/-
  ns/InterpGrid.lean — T37 cycle 1724 (goal G850c): from the grid Hölder bound of the scheme
  (GlobalHolder: |a_{k+j} − a_k|² ≤ j·G for all k, j) to the continuous-time interpolant
  u(k + θ) = a_k + θ(a_{k+1} − a_k): for θ + e = 1, θ' + e' = 1 (all ≥ 0) and every k, j,
      |u(k + 1 + j + θ') − u(k + θ)|² ≤ 3(e·G + j·G + θ'·G) = 3·|s' − s|·G,
  and inside one step |u(k + θ) − a_k|² ≤ θ·G. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpCross

namespace InterpGrid
open DetChain NSChain LibRing InterpStep InterpCross

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

/-- j · x as a repeated sum. -/
def nm : Nat → R → R
  | 0, _ => K.zero
  | j + 1, x => K.add x (nm j x)

/-- The interpolant at k + θ. -/
def interp (a : Nat → R) (k : Nat) (θ : R) : R := K.add (a k) (K.mul θ (sub K (a (k + 1)) (a k)))

theorem nm_one (x : R) : nm K 1 x = x := by
  show K.add x K.zero = x
  lib_ring

theorem grid_interp (a : Nat → R) (G : R)
    (hH : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (k j : Nat) (θ e θ' e' : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one) :
    Le K P (K.mul (sub K (interp K a (k + 1 + j) θ') (interp K a k θ)) (sub K (interp K a (k + 1 + j) θ') (interp K a k θ)))
      (K.mul (three K) (K.add (K.mul e G) (K.add (nm K j G) (K.mul θ' G)))) := by
  have ha := hH k 1
  rw [nm_one] at ha
  have hb := hH (k + 1 + j) 1
  rw [nm_one] at hb
  exact cross_step K P (a k) (a (k + 1)) (a (k + 1 + j)) (a (k + 1 + j + 1)) θ e θ' e' G (nm K j G)
    hθ he hθe hθ' he' hθe' ha hb (hH (k + 1) j)

theorem step_interp (a : Nat → R) (G : R)
    (hH : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (k : Nat) (θ e : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one) :
    Le K P (K.mul (sub K (interp K a k θ) (a k)) (sub K (interp K a k θ) (a k))) (K.mul θ G) := by
  have ha := hH k 1
  rw [nm_one] at ha
  have e1 : sub K (interp K a k θ) (a k) = K.mul θ (sub K (a (k + 1)) (a k)) := by
    unfold interp; lib_ring
  rw [e1]
  exact interp_step K P θ _ G hθ ⟨e, he, hθe⟩ ha

end Gen
end InterpGrid

#print axioms InterpGrid.nm_one
#print axioms InterpGrid.grid_interp
#print axioms InterpGrid.step_interp

#print axioms: 3 theorems, no axioms.

79.247 Time interpolation of the Galerkin scheme, summed over modes

With \(N(v)=\sum _r|v_r|^2\): \(N(a_{k+1}-a_k)\le G\), \(N(a_{k+j+2}-a_{k+j+1})\le G\), \(N(a_{k+j+1}-a_{k+1})\le M\) give \(N(u(k+1+j+\theta ')-u(k+\theta ))\le 3((1-\theta )G+M+\theta 'G)\). The norm of GlobalHolder; per coordinate \((x+y+z)^2\le 3(x^2+y^2+z^2)\), then the mode sum.

ns/InterpModes.lean

/-
  ns/InterpModes.lean — T37 cycle 1728 (goal G850d): the continuous-time interpolation for the Galerkin scheme
  itself, with the mode-sum norm N(v) = Σ_r |v_r|² of GlobalHolder. For the interpolant
  u_r(k + θ) = a_{k,r} + θ(a_{k+1,r} − a_{k,r}) and θ + e = 1, θ' + e' = 1 (all ≥ 0): if N(a_{k+1} − a_k) ≤ G,
  N(a_{k+j+2} − a_{k+j+1}) ≤ G and N(a_{k+j+1} − a_{k+1}) ≤ M then
      N(u(k + 1 + j + θ') − u(k + θ)) ≤ 3(eG + M + θ'G).
  Per component the difference is e(a₁ − a₀) + (b₀ − a₁) + θ'(b₁ − b₀) (θ = 1 − e), then (x + y + z)² ≤ 3(x² + y² + z²)
  and the sum over modes. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpCross
import ns.ForceBound
import ns.EnstDiff

namespace InterpModes
open DetChain NSChain LibRing FourierGalerkin InterpStep InterpCross

section Gen
variable {R : Type} (K : CRing R) (P : Cone K) {I : Type}

/-- The interpolant at k + θ, mode r. -/
def interp3 (a : Nat → I → V3 R) (k : Nat) (θ : R) (r : I) : V3 R :=
  ⟨K.add (a k r).x (K.mul θ (sub K (a (k + 1) r).x (a k r).x)),
   K.add (a k r).y (K.mul θ (sub K (a (k + 1) r).y (a k r).y)),
   K.add (a k r).z (K.mul θ (sub K (a (k + 1) r).z (a k r).z))⟩

def nq (l : List I) (v : I → V3 R) : R := lsum K (fun r => dot3 K (v r) (v r)) l

def sq (x : R) : R := K.mul x x

/-- One coordinate. -/
theorem comp_bound (α0 α1 β0 β1 θ e θ' : R) (hθe : K.add θ e = K.one) :
    Le K P (sq K (sub K (K.add β0 (K.mul θ' (sub K β1 β0))) (K.add α0 (K.mul θ (sub K α1 α0)))))
      (K.mul (three K) (K.add (K.mul (K.mul e e) (sq K (sub K α1 α0)))
        (K.add (sq K (sub K β0 α1)) (K.mul (K.mul θ' θ') (sq K (sub K β1 β0)))))) := by
  have hθv : θ = sub K K.one e := by rw [← hθe]; lib_ring
  have e1 : sub K (K.add β0 (K.mul θ' (sub K β1 β0))) (K.add α0 (K.mul θ (sub K α1 α0)))
      = K.add (K.mul e (sub K α1 α0)) (K.add (sub K β0 α1) (K.mul θ' (sub K β1 β0))) := by
    rw [hθv]; lib_ring
  have e2 : K.mul (three K) (K.add (K.mul (K.mul e e) (sq K (sub K α1 α0)))
        (K.add (sq K (sub K β0 α1)) (K.mul (K.mul θ' θ') (sq K (sub K β1 β0)))))
      = K.mul (three K) (K.add (K.mul (K.mul e (sub K α1 α0)) (K.mul e (sub K α1 α0)))
        (K.add (K.mul (sub K β0 α1) (sub K β0 α1)) (K.mul (K.mul θ' (sub K β1 β0)) (K.mul θ' (sub K β1 β0))))) := by
    unfold sq; lib_ring
  unfold sq at e2 ⊢
  rw [e1, e2]
  exact three_sq K P _ _ _

theorem regroup (xa ya za xb yb zb xc yc zc e θ' : R) :
    K.add (K.add (K.mul (three K) (K.add (K.mul (K.mul e e) xa) (K.add xb (K.mul (K.mul θ' θ') xc))))
        (K.mul (three K) (K.add (K.mul (K.mul e e) ya) (K.add yb (K.mul (K.mul θ' θ') yc)))))
      (K.mul (three K) (K.add (K.mul (K.mul e e) za) (K.add zb (K.mul (K.mul θ' θ') zc))))
    = K.mul (three K) (K.add (K.mul (K.mul e e) (K.add (K.add xa ya) za))
        (K.add (K.add (K.add xb yb) zb) (K.mul (K.mul θ' θ') (K.add (K.add xc yc) zc)))) := by
  lib_ring

/-- One mode. -/
theorem mode_bound (A0 A1 B0 B1 : V3 R) (θ e θ' : R) (hθe : K.add θ e = K.one) :
    Le K P (dot3 K (EnstDiff.sub3 K
        (⟨K.add B0.x (K.mul θ' (sub K B1.x B0.x)), K.add B0.y (K.mul θ' (sub K B1.y B0.y)), K.add B0.z (K.mul θ' (sub K B1.z B0.z))⟩ : V3 R)
        (⟨K.add A0.x (K.mul θ (sub K A1.x A0.x)), K.add A0.y (K.mul θ (sub K A1.y A0.y)), K.add A0.z (K.mul θ (sub K A1.z A0.z))⟩ : V3 R))
      (EnstDiff.sub3 K
        (⟨K.add B0.x (K.mul θ' (sub K B1.x B0.x)), K.add B0.y (K.mul θ' (sub K B1.y B0.y)), K.add B0.z (K.mul θ' (sub K B1.z B0.z))⟩ : V3 R)
        (⟨K.add A0.x (K.mul θ (sub K A1.x A0.x)), K.add A0.y (K.mul θ (sub K A1.y A0.y)), K.add A0.z (K.mul θ (sub K A1.z A0.z))⟩ : V3 R)))
      (K.mul (three K) (K.add (K.mul (K.mul e e) (dot3 K (EnstDiff.sub3 K A1 A0) (EnstDiff.sub3 K A1 A0)))
        (K.add (dot3 K (EnstDiff.sub3 K B0 A1) (EnstDiff.sub3 K B0 A1))
          (K.mul (K.mul θ' θ') (dot3 K (EnstDiff.sub3 K B1 B0) (EnstDiff.sub3 K B1 B0)))))) := by
  have hx := comp_bound K P A0.x A1.x B0.x B1.x θ e θ' hθe
  have hy := comp_bound K P A0.y A1.y B0.y B1.y θ e θ' hθe
  have hz := comp_bound K P A0.z A1.z B0.z B1.z θ e θ' hθe
  have s := le_add2' K P (le_add2' K P hx hy) hz
  rw [regroup] at s
  exact s

/-- e ≤ 1, 0 ≤ x ≤ G: e²x ≤ eG. -/
theorem scale_le (e x G : R) (he : P.nn e) (he1 : Le K P e K.one) (hx : P.nn x) (h : Le K P x G) :
    Le K P (K.mul (K.mul e e) x) (K.mul e G) := by
  have h1 : Le K P (K.mul e x) G := le_trans' K P _ _ _ (mul_le_self K P e x he1 hx) h
  have e1 : K.mul (K.mul e e) x = K.mul e (K.mul e x) := by lib_ring
  rw [e1]
  exact le_mul_l K P e he h1

/-- **Across steps, summed over modes.** -/
theorem modes_cross (l : List I) (a : Nat → I → V3 R) (k j : Nat) (θ e θ' e' G M : R)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (hA : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1) r) (a k r))) G)
    (hB : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r))) G)
    (hM : Le K P (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j) r) (a (k + 1) r))) M) :
    Le K P (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r)))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have s := ForceBound.lsum_le K P _ _
    (fun r => mode_bound K P (a k r) (a (k + 1) r) (a (k + 1 + j) r) (a (k + 1 + j + 1) r) θ e θ' hθe) l
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_add, lsum_mul_left] at s
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have nA := ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1) r) (a k r)) (EnstDiff.sub3 K (a (k + 1) r) (a k r)))
    (fun r => ForceBound.dot3_nn K P _) l
  have nC := ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)) (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))
    (fun r => ForceBound.dot3_nn K P _) l
  have b1 := scale_le K P e _ G he he1 nA hA
  have b3 := scale_le K P θ' _ G hθ' hθ1 nC hB
  have b := le_mul_l K P (three K) (nn_three K P) (le_add2' K P b1 (le_add2' K P hM b3))
  exact le_trans' K P _ _ _ s b

/-- **Weighted form** (w = ν³ in GlobalHolder): bounds on w·N give the bound on w·N of the interpolant difference. -/
theorem modes_cross_w (l : List I) (a : Nat → I → V3 R) (k j : Nat) (w θ e θ' e' G M : R) (hw : P.nn w)
    (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one)
    (hA : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1) r) (a k r)))) G)
    (hB : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))) G)
    (hM : Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (k + 1 + j) r) (a (k + 1) r)))) M) :
    Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r))))
      (K.mul (three K) (K.add (K.mul e G) (K.add M (K.mul θ' G)))) := by
  have s := ForceBound.lsum_le K P _ _
    (fun r => mode_bound K P (a k r) (a (k + 1) r) (a (k + 1 + j) r) (a (k + 1 + j + 1) r) θ e θ' hθe) l
  rw [lsum_mul_left, lsum_add, lsum_mul_left, lsum_add, lsum_mul_left] at s
  have sw := le_mul_l K P w hw s
  have he1 : Le K P e K.one := ⟨θ, hθ, by rw [← hθe]; lib_ring⟩
  have hθ1 : Le K P θ' K.one := ⟨e', he', hθe'⟩
  have nA := P.nn_mul _ _ hw (ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1) r) (a k r)) (EnstDiff.sub3 K (a (k + 1) r) (a k r)))
    (fun r => ForceBound.dot3_nn K P _) l)
  have nC := P.nn_mul _ _ hw (ForceBound.lsum_nn K P (fun r => dot3 K (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)) (EnstDiff.sub3 K (a (k + 1 + j + 1) r) (a (k + 1 + j) r)))
    (fun r => ForceBound.dot3_nn K P _) l)
  have b1 := scale_le K P e _ G he he1 nA hA
  have b3 := scale_le K P θ' _ G hθ' hθ1 nC hB
  have b := le_mul_l K P (three K) (nn_three K P) (le_add2' K P b1 (le_add2' K P hM b3))
  have e1 : ∀ A B C : R, K.mul w (K.mul (three K) (K.add (K.mul (K.mul e e) A) (K.add B (K.mul (K.mul θ' θ') C))))
      = K.mul (three K) (K.add (K.mul (K.mul e e) (K.mul w A)) (K.add (K.mul w B) (K.mul (K.mul θ' θ') (K.mul w C)))) := by
    intro A B C; unfold three; lib_ring
  rw [e1] at sw
  exact le_trans' K P _ _ _ sw b

/-- **Hölder modulus of the interpolants from a grid bound** (the conclusion of GlobalHolder.global_holder,
    w = ν³): w·N(a(T + k) − a(T)) ≤ k·C for all T, k gives, for s = k + θ and s' = k + 1 + j + θ',
    w·N(u(s') − u(s)) ≤ 3(e·C + j·C + θ'·C) = 3(s' − s)·C. -/
theorem holder_interp (l : List I) (a : Nat → I → V3 R) (w C : R) (hw : P.nn w)
    (HG : ∀ T k, Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (a (T + k) r) (a T r)))) (K.mul (castN K k) C))
    (k j : Nat) (θ e θ' e' : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one)
    (hθ' : P.nn θ') (he' : P.nn e') (hθe' : K.add θ' e' = K.one) :
    Le K P (K.mul w (nq K l (fun r => EnstDiff.sub3 K (interp3 K a (k + 1 + j) θ' r) (interp3 K a k θ r))))
      (K.mul (three K) (K.add (K.mul e (K.mul (castN K 1) C)) (K.add (K.mul (castN K j) C) (K.mul θ' (K.mul (castN K 1) C))))) :=
  modes_cross_w K P l a k j w θ e θ' e' (K.mul (castN K 1) C) (K.mul (castN K j) C) hw hθ he hθe hθ' he' hθe'
    (HG k 1) (HG (k + 1 + j) 1) (HG (k + 1) j)

end Gen
end InterpModes

#print axioms InterpModes.comp_bound
#print axioms InterpModes.regroup
#print axioms InterpModes.mode_bound
#print axioms InterpModes.scale_le
#print axioms InterpModes.modes_cross
#print axioms InterpModes.modes_cross_w
#print axioms InterpModes.holder_interp

#print axioms: 7 theorems, no axioms.

79.248 Pigeonhole: the discrete core of the compactness step

If \(f(0),\dots ,f(n)\) all lie in \(\{0,\dots ,n-1\}\) then \(f(i)=f(j)\) for some \(i<j\le n\). Values of the scheme quantised on a \(\delta \)-grid of a bounded interval: two truncations fall in the same box.

ns/Pigeon.lean

/-
  ns/Pigeon.lean — T37 cycle 1736 (goal G852a): the discrete core of Bolzano–Weierstrass used as a compactness
  substitute for the continuous-time limit: if f 0, …, f n all lie in {0, …, n − 1} (n boxes of a δ-grid of a
  bounded interval) then two of them coincide. Lean 4 core, hand proofs (bounded Boolean search), no axioms.
-/
namespace Pigeon

/-- Is v among f 0, …, f (k − 1)? -/
def search (f : Nat → Nat) (v : Nat) : Nat → Bool
  | 0 => false
  | k + 1 => search f v k || Nat.beq (f k) v

theorem beq_eq : ∀ a b : Nat, Nat.beq a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg (· + 1) (beq_eq a b h)

theorem beq_refl : ∀ a : Nat, Nat.beq a a = true
  | 0 => rfl
  | a + 1 => beq_refl a

theorem or_cases : ∀ {a b : Bool}, (a || b) = true → a = true ∨ b = true
  | true, _, _ => Or.inl rfl
  | false, _, h => Or.inr h

theorem search_true (f : Nat → Nat) (v : Nat) : ∀ k, search f v k = true → ∃ i, i < k ∧ f i = v
  | 0, h => Bool.noConfusion h
  | k + 1, h => by
    cases or_cases h with
    | inl h1 =>
      cases search_true f v k h1 with
      | intro i hi => exact ⟨i, Nat.lt_succ_of_lt hi.1, hi.2⟩
    | inr h2 => exact ⟨k, Nat.lt_succ_self k, beq_eq _ _ h2⟩

theorem search_false (f : Nat → Nat) (v : Nat) : ∀ k, search f v k = false → ∀ i, i < k → f i ≠ v
  | 0, _, _, hi, _ => absurd hi (Nat.not_lt_zero _)
  | k + 1, h, i, hi, e => by
    cases h1 : search f v k with
    | true =>
      have : search f v (k + 1) = true := by show (search f v k || Nat.beq (f k) v) = true; rw [h1]; rfl
      rw [h] at this; exact Bool.noConfusion this
    | false =>
      cases Nat.lt_or_ge i k with
      | inl hl => exact search_false f v k h1 i hl e
      | inr hg =>
        have hik : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) hg
        have hb : Nat.beq (f k) v = true := by rw [← hik, e]; exact beq_refl v
        have : search f v (k + 1) = true := by show (search f v k || Nat.beq (f k) v) = true; rw [h1, hb]; rfl
        rw [h] at this; exact Bool.noConfusion this

theorem lt_of_lt_succ_ne {a n : Nat} (h : a < n + 1) (hn : a ≠ n) : a < n := by
  cases Nat.lt_or_ge a n with
  | inl hl => exact hl
  | inr hg => exact absurd (Nat.le_antisymm (Nat.le_of_lt_succ h) hg) hn

/-- **Pigeonhole.** -/
theorem pigeon : ∀ (n : Nat) (f : Nat → Nat), (∀ i, i ≤ n → f i < n) → ∃ i j, i < j ∧ j ≤ n ∧ f i = f j
  | 0, f, h => absurd (h 0 (Nat.le_refl 0)) (Nat.not_lt_zero _)
  | n + 1, f, h => by
    cases hs : search f (f (n + 1)) (n + 1) with
    | true =>
      cases search_true f _ _ hs with
      | intro i hi => exact ⟨i, n + 1, hi.1, Nat.le_refl _, hi.2⟩
    | false =>
      have hne := search_false f _ _ hs
      let v := f (n + 1)
      let g : Nat → Nat := fun i => cond (Nat.beq (f i) n) v (f i)
      have hg : ∀ i, i ≤ n → g i < n := by
        intro i hi
        have hfi := h i (Nat.le_succ_of_le hi)
        have hv := h (n + 1) (Nat.le_refl _)
        cases e : Nat.beq (f i) n with
        | true =>
          have fin : f i = n := beq_eq _ _ e
          show cond (Nat.beq (f i) n) v (f i) < n
          rw [e]
          show f (n + 1) < n
          exact lt_of_lt_succ_ne hv (fun ev => hne i (Nat.lt_succ_of_le hi) (by rw [fin]; exact ev.symm))
        | false =>
          show cond (Nat.beq (f i) n) v (f i) < n
          rw [e]
          show f i < n
          exact lt_of_lt_succ_ne hfi (fun en => by rw [en, beq_refl] at e; exact Bool.noConfusion e)
      cases pigeon n g hg with
      | intro i hij =>
        cases hij with
        | intro j hj =>
          have hgij : g i = g j := hj.2.2
          refine ⟨i, j, hj.1, Nat.le_succ_of_le hj.2.1, ?_⟩
          cases ei : Nat.beq (f i) n with
          | true =>
            cases ej : Nat.beq (f j) n with
            | true => rw [beq_eq _ _ ei, beq_eq _ _ ej]
            | false =>
              have : v = f j := by
                have a : g i = v := by show cond (Nat.beq (f i) n) v (f i) = v; rw [ei]; rfl
                have b : g j = f j := by show cond (Nat.beq (f j) n) v (f j) = f j; rw [ej]; rfl
                rw [← a, ← b]; exact hgij
              exact absurd this.symm (hne j (Nat.lt_succ_of_le hj.2.1))
          | false =>
            cases ej : Nat.beq (f j) n with
            | true =>
              have : f i = v := by
                have a : g i = f i := by show cond (Nat.beq (f i) n) v (f i) = f i; rw [ei]; rfl
                have b : g j = v := by show cond (Nat.beq (f j) n) v (f j) = v; rw [ej]; rfl
                rw [← a, ← b]; exact hgij
              exact absurd this (hne i (Nat.lt_succ_of_le (Nat.le_of_lt (Nat.lt_of_lt_of_le hj.1 hj.2.1))))
            | false =>
              have a : g i = f i := by show cond (Nat.beq (f i) n) v (f i) = f i; rw [ei]; rfl
              have b : g j = f j := by show cond (Nat.beq (f j) n) v (f j) = f j; rw [ej]; rfl
              rw [← a, ← b]; exact hgij

end Pigeon

#print axioms Pigeon.search_true
#print axioms Pigeon.search_false
#print axioms Pigeon.pigeon

#print axioms: 3 theorems, no axioms.

79.249 Pigeonhole for quantised vectors

If \(F(0),\dots ,F(n^k)\) are lists of length \(k\) with entries \(<n\), then \(F(i)=F(j)\) for some \(i<j\le n^k\). Base-\(n\) code \(a+n\cdot \mathrm {code}(as)\), which is \(<n^k\) and injective, then the scalar pigeonhole.

ns/PigeonVec.lean

/-
  ns/PigeonVec.lean — T37 cycle 1738 (goal G852b): pigeonhole for vectors of k quantised coordinates.
  If F 0, …, F N are lists of length k with entries < n and N = n^k, two of them are equal: base-n code
  (code (a :: as) = a + n · code as) is < n^k and injective, then Pigeon.pigeon. For finitely many (mode, time)
  coordinates of bounded scheme values on a δ-grid: among more than n^k truncations two are δ-close everywhere.
  Lean 4 core, hand proofs, no axioms.
-/
import ns.Pigeon

namespace PigeonVec

def code (n : Nat) : List Nat → Nat
  | [] => 0
  | a :: as => a + n * code n as

/-- All entries < n. -/
def small (n : Nat) : List Nat → Prop
  | [] => True
  | a :: as => a < n ∧ small n as

theorem cancel_left : ∀ T a b : Nat, T + a = T + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | T + 1, a, b, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact cancel_left T a b (Nat.succ.inj h)

theorem code_lt (n : Nat) : ∀ (k : Nat) (v : List Nat), v.length = k → small n v → code n v < n ^ k
  | 0, [], _, _ => Nat.lt_succ_self 0
  | 0, _ :: _, h, _ => Nat.noConfusion h
  | _ + 1, [], h, _ => Nat.noConfusion h
  | k + 1, a :: as, h, hs => by
    have ih := code_lt n k as (Nat.succ.inj h) hs.2
    show a + n * code n as < n ^ k * n
    have h1 : a + n * code n as < n + n * code n as := Nat.add_lt_add_right hs.1 _
    have h2 : n + n * code n as = n * (code n as + 1) := by rw [Nat.mul_succ, Nat.add_comm]
    have h3 : n * (code n as + 1) ≤ n * n ^ k := Nat.mul_le_mul_left n ih
    rw [h2] at h1
    rw [Nat.mul_comm (n ^ k) n]
    exact Nat.lt_of_lt_of_le h1 h3

/-- a, b < n and x < y: a + n x < b + n y. -/
theorem lt_mix (n a b x y : Nat) (ha : a < n) (hxy : x < y) : a + n * x < b + n * y := by
  have h1 : a + n * x < n + n * x := Nat.add_lt_add_right ha _
  have h2 : n + n * x = n * (x + 1) := by rw [Nat.mul_succ, Nat.add_comm]
  have h3 : n * (x + 1) ≤ n * y := Nat.mul_le_mul_left n hxy
  have h4 : n * y ≤ b + n * y := Nat.le_add_left _ _
  rw [h2] at h1
  exact Nat.lt_of_lt_of_le h1 (Nat.le_trans h3 h4)

theorem lt_irr : ∀ m : Nat, ¬ m < m
  | 0, h => Nat.not_lt_zero 0 h
  | m + 1, h => lt_irr m (Nat.lt_of_succ_lt_succ h)

theorem code_inj (n : Nat) : ∀ u v : List Nat, u.length = v.length → small n u → small n v →
    code n u = code n v → u = v
  | [], [], _, _, _, _ => rfl
  | [], _ :: _, h, _, _, _ => Nat.noConfusion h
  | _ :: _, [], h, _, _, _ => Nat.noConfusion h
  | a :: as, b :: bs, hl, hu, hv, hc => by
    have hc' : a + n * code n as = b + n * code n bs := hc
    have ex : code n as = code n bs := by
      cases Nat.lt_or_ge (code n as) (code n bs) with
      | inl hlt => exact absurd hc' (fun e => lt_irr _ (by
          have := lt_mix n a b _ _ hu.1 hlt; rw [e] at this; exact this))
      | inr hge =>
        cases Nat.lt_or_ge (code n bs) (code n as) with
        | inl hlt => exact absurd hc' (fun e => lt_irr _ (by
            have := lt_mix n b a _ _ hv.1 hlt; rw [← e] at this; exact this))
        | inr hge' => exact Nat.le_antisymm hge' hge
    rw [ex, Nat.add_comm a, Nat.add_comm b] at hc'
    have ea : a = b := cancel_left _ _ _ hc'
    rw [ea, code_inj n as bs (Nat.succ.inj hl) hu.2 hv.2 ex]

/-- **Vector pigeonhole.** -/
theorem pigeon_vec (n k : Nat) (F : Nat → List Nat)
    (hF : ∀ i, i ≤ n ^ k → (F i).length = k ∧ small n (F i)) :
    ∃ i j, i < j ∧ j ≤ n ^ k ∧ F i = F j := by
  cases Pigeon.pigeon (n ^ k) (fun i => code n (F i))
      (fun i hi => code_lt n k (F i) (hF i hi).1 (hF i hi).2) with
  | intro i hij =>
    cases hij with
    | intro j hj =>
      refine ⟨i, j, hj.1, hj.2.1, ?_⟩
      have hi := hF i (Nat.le_of_lt (Nat.lt_of_lt_of_le hj.1 hj.2.1))
      have hj' := hF j hj.2.1
      exact code_inj n (F i) (F j) (by rw [hi.1, hj'.1]) hi.2 hj'.2 hj.2.2

end PigeonVec

#print axioms PigeonVec.code_lt
#print axioms PigeonVec.lt_mix
#print axioms PigeonVec.code_inj
#print axioms PigeonVec.pigeon_vec

#print axioms: 4 theorems, no axioms.

79.250 Values in one box are close

If \(L\le x\le L+\delta \) and \(L\le y\le L+\delta \) then \((x-y)^2\le \delta ^2\). The remainder is the product \((\delta -(x-y))(\delta +(x-y))\ge 0\).

ns/BoxClose.lean

/-
  ns/BoxClose.lean — T37 cycle 1740 (goal G852c): two values in the same box of a δ-grid are δ-close.
  Over any cone: if L ≤ x ≤ L + δ and L ≤ y ≤ L + δ then (x − y)² ≤ δ² (since δ − (x − y) ≥ 0 and
  δ + (x − y) ≥ 0, their product δ² − (x − y)² is ≥ 0). With PigeonVec: two truncations whose quantised
  coordinates agree are δ-close in every coordinate. Lean 4 core, hand proofs, no axioms.
-/
import NSChain
import lib.RingTac

namespace BoxClose
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem box_close (L δ x y : R) (hx1 : Le K P L x) (hx2 : Le K P x (K.add L δ))
    (hy1 : Le K P L y) (hy2 : Le K P y (K.add L δ)) :
    Le K P (K.mul (sub K x y) (sub K x y)) (K.mul δ δ) := by
  cases hx1 with
  | intro a ha =>
    cases hx2 with
    | intro b hb =>
      cases hy1 with
      | intro c hc =>
        cases hy2 with
        | intro d hd =>
          -- x = L + a, L + δ = x + b, y = L + c, L + δ = y + d
          -- δ − (x − y) = b + c,  δ + (x − y) = a + d
          refine ⟨K.mul (K.add b c) (K.add a d), P.nn_mul _ _ (P.nn_add _ _ hb.1 hc.1) (P.nn_add _ _ ha.1 hd.1), ?_⟩
          have ex : x = K.add L a := ha.2.symm
          have ey : y = K.add L c := hc.2.symm
          have eδ : δ = K.add a b := by
            have h := hb.2
            rw [ex] at h
            have : δ = K.add (K.add L δ) (K.neg L) := by lib_ring
            rw [this, ← h]; lib_ring
          have ed : d = K.add (K.add a b) (K.neg c) := by
            have h := hd.2
            rw [ey, eδ] at h
            have : d = K.add (K.add (K.add L c) d) (K.add (K.neg L) (K.neg c)) := by lib_ring
            rw [this, h]; lib_ring
          rw [ex, ey, eδ, ed]
          lib_ring

end Gen
end BoxClose

#print axioms BoxClose.box_close

#print axioms: 1 theorem, no axioms.

79.251 Two schemes close at a grid time stay close in the step

With the grid bound for both and \((a_k-b_k)^2\le D\): \((u_a(k+\theta )-u_b(k+\theta ))^2\le 3(2\theta G+D)\). The \(\varepsilon /3\) step of the compactness substitute (with PigeonVec, BoxClose).

ns/CloseInterp.lean

/-
  ns/CloseInterp.lean — T37 cycle 1742 (goal G852c): two schemes a, b with the grid Hölder bound
  |x_{k+j} − x_k|² ≤ j·G that are close at a grid time, (a_k − b_k)² ≤ D, stay close inside the step:
      (u_a(k + θ) − u_b(k + θ))² ≤ 3(θG + D + θG)    (0 ≤ θ ≤ 1).
  With PigeonVec + BoxClose (D = δ² at the grid times) and InterpGrid this is the ε/3 step of the
  Arzelà–Ascoli substitute. Over any cone; Lean 4 core, hand proofs, no axioms.
-/
import ns.InterpGrid

namespace CloseInterp
open DetChain NSChain LibRing InterpStep InterpCross InterpGrid

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem close_interp (a b : Nat → R) (G D : R)
    (ha : ∀ k j, Le K P (K.mul (sub K (a (k + j)) (a k)) (sub K (a (k + j)) (a k))) (nm K j G))
    (hb : ∀ k j, Le K P (K.mul (sub K (b (k + j)) (b k)) (sub K (b (k + j)) (b k))) (nm K j G))
    (k : Nat) (hD : Le K P (K.mul (sub K (a k) (b k)) (sub K (a k) (b k))) D)
    (θ e : R) (hθ : P.nn θ) (he : P.nn e) (hθe : K.add θ e = K.one) :
    Le K P (K.mul (sub K (interp K a k θ) (interp K b k θ)) (sub K (interp K a k θ) (interp K b k θ)))
      (K.mul (three K) (K.add (K.mul θ G) (K.add D (K.mul θ G)))) := by
  have h1 := step_interp K P a G ha k θ e hθ he hθe
  have h3 := step_interp K P b G hb k θ e hθ he hθe
  have e3 : K.mul (sub K (interp K b k θ) (b k)) (sub K (interp K b k θ) (b k))
      = K.mul (sub K (b k) (interp K b k θ)) (sub K (b k) (interp K b k θ)) := by lib_ring
  rw [e3] at h3
  have s := le_add2' K P h1 (le_add2' K P hD h3)
  have e1 : sub K (interp K a k θ) (interp K b k θ)
      = K.add (sub K (interp K a k θ) (a k)) (K.add (sub K (a k) (b k)) (sub K (b k) (interp K b k θ))) := by
    lib_ring
  rw [e1]
  exact le_trans' K P _ _ _ (three_sq K P _ _ _) (le_mul_l K P (three K) (nn_three K P) s)

end Gen
end CloseInterp

#print axioms CloseInterp.close_interp

#print axioms: 1 theorem, no axioms.

79.252 Generalized pigeonhole

A list of \(mN+1\) values below \(N\) takes some value at least \(m+1\) times. The form needed to iterate the extraction on nested grids.

ns/PigeonMany.lean

/-
  ns/PigeonMany.lean — T37 cycle 1744 (goal G852d): generalized pigeonhole, the form needed to iterate the
  extraction on nested grids: a list of m·N + 1 values below N takes some value at least m + 1 times.
  Σ_{v<N} #v(L) = |L| for entries < N, so if every count were ≤ m the sum would be ≤ m·N. The value is found
  by a bounded Boolean search. Lean 4 core, hand proofs, no axioms.
-/
import ns.Pigeon
import lib.Semi

namespace PigeonMany
open Pigeon

def cnt (v : Nat) : List Nat → Nat
  | [] => 0
  | y :: l => cond (Nat.beq v y) 1 0 + cnt v l

/-- Σ_{v < N} g v. -/
def sumB (g : Nat → Nat) : Nat → Nat
  | 0 => 0
  | N + 1 => sumB g N + g N

def allLt (N : Nat) : List Nat → Prop
  | [] => True
  | y :: l => y < N ∧ allLt N l

theorem lt_irr : ∀ m : Nat, ¬ m < m
  | 0, h => Nat.not_lt_zero 0 h
  | m + 1, h => lt_irr m (Nat.lt_of_succ_lt_succ h)

theorem beq_ne {a b : Nat} (h : a ≠ b) : Nat.beq a b = false := by
  cases e : Nat.beq a b with
  | false => rfl
  | true => exact absurd (beq_eq _ _ e) h

theorem delta_zero (y : Nat) : ∀ N, N ≤ y → sumB (fun v => cond (Nat.beq v y) 1 0) N = 0
  | 0, _ => rfl
  | N + 1, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0) N + cond (Nat.beq N y) 1 0 = 0
    have hne : N ≠ y := fun e => by rw [e] at h; exact lt_irr y h
    rw [delta_zero y N (Nat.le_of_succ_le h), beq_ne hne]
    rfl

theorem delta_one (y : Nat) : ∀ N, y < N → sumB (fun v => cond (Nat.beq v y) 1 0) N = 1
  | 0, h => absurd h (Nat.not_lt_zero _)
  | N + 1, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0) N + cond (Nat.beq N y) 1 0 = 1
    cases Nat.lt_or_ge y N with
    | inl hl =>
      have hne : N ≠ y := fun e => by rw [e] at hl; exact lt_irr y hl
      rw [delta_one y N hl, beq_ne hne]
      rfl
    | inr hg =>
      have e : y = N := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      rw [delta_zero y N hg, e, beq_refl]
      rfl

theorem sumB_add (f g : Nat → Nat) : ∀ N, sumB (fun v => f v + g v) N = sumB f N + sumB g N
  | 0 => rfl
  | N + 1 => by
    show sumB (fun v => f v + g v) N + (f N + g N) = (sumB f N + f N) + (sumB g N + g N)
    rw [sumB_add f g N]
    lib_nat

theorem sum_cnt (N : Nat) : ∀ L : List Nat, allLt N L → sumB (fun v => cnt v L) N = L.length
  | [], _ => by
    show sumB (fun _ => 0) N = 0
    induction N with
    | zero => rfl
    | succ N ih => show sumB (fun _ => 0) N + 0 = 0; rw [ih True.intro]
  | y :: l, h => by
    show sumB (fun v => cond (Nat.beq v y) 1 0 + cnt v l) N = l.length + 1
    rw [sumB_add, delta_one y N h.1, sum_cnt N l h.2, Nat.add_comm]

theorem sum_le (g : Nat → Nat) (m : Nat) : ∀ N, (∀ v, v < N → g v ≤ m) → sumB g N ≤ m * N
  | 0, _ => Nat.le_refl 0
  | N + 1, h => by
    show sumB g N + g N ≤ m * (N + 1)
    rw [Nat.mul_succ]
    exact Nat.add_le_add (sum_le g m N (fun v hv => h v (Nat.lt_succ_of_lt hv))) (h N (Nat.lt_succ_self N))

def searchM (L : List Nat) (m : Nat) : Nat → Bool
  | 0 => false
  | N + 1 => searchM L m N || Nat.blt m (cnt N L)

theorem searchM_true (L : List Nat) (m : Nat) : ∀ N, searchM L m N = true → ∃ v, v < N ∧ m + 1 ≤ cnt v L
  | 0, h => Bool.noConfusion h
  | N + 1, h => by
    cases or_cases h with
    | inl h1 =>
      cases searchM_true L m N h1 with
      | intro v hv => exact ⟨v, Nat.lt_succ_of_lt hv.1, hv.2⟩
    | inr h2 => exact ⟨N, Nat.lt_succ_self N, Nat.le_of_ble_eq_true h2⟩

theorem ble_of_le : ∀ a b : Nat, a ≤ b → Nat.ble a b = true
  | 0, _, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | a + 1, b + 1, h => ble_of_le a b (Nat.le_of_succ_le_succ h)

theorem searchM_false (L : List Nat) (m : Nat) : ∀ N, searchM L m N = false → ∀ v, v < N → cnt v L ≤ m
  | 0, _, _, hv => absurd hv (Nat.not_lt_zero _)
  | N + 1, h, v, hv => by
    have hN : Nat.blt m (cnt N L) = false := by
      cases e : Nat.blt m (cnt N L) with
      | false => rfl
      | true =>
        have : searchM L m (N + 1) = true := by
          show (searchM L m N || Nat.blt m (cnt N L)) = true; rw [e]; cases searchM L m N <;> rfl
        rw [h] at this; exact Bool.noConfusion this
    have hS : searchM L m N = false := by
      cases e : searchM L m N with
      | false => rfl
      | true =>
        have : searchM L m (N + 1) = true := by show (searchM L m N || Nat.blt m (cnt N L)) = true; rw [e]; rfl
        rw [h] at this; exact Bool.noConfusion this
    cases Nat.lt_or_ge v N with
    | inl hl => exact searchM_false L m N hS v hl
    | inr hg =>
      have e : v = N := Nat.le_antisymm (Nat.le_of_lt_succ hv) hg
      rw [e]
      cases Nat.lt_or_ge (cnt N L) (m + 1) with
      | inl hl => exact Nat.le_of_lt_succ hl
      | inr hg' =>
        have : Nat.blt m (cnt N L) = true := ble_of_le _ _ hg'
        rw [hN] at this; exact Bool.noConfusion this

/-- **Generalized pigeonhole.** -/
theorem pigeon_many (m N : Nat) (L : List Nat) (hl : m * N + 1 ≤ L.length) (hL : allLt N L) :
    ∃ v, v < N ∧ m + 1 ≤ cnt v L := by
  cases e : searchM L m N with
  | true => exact searchM_true L m N e
  | false =>
    have hs := sum_le (fun v => cnt v L) m N (searchM_false L m N e)
    rw [sum_cnt N L hL] at hs
    exact absurd (Nat.le_trans hl hs) (fun h => lt_irr _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) h))

end PigeonMany

#print axioms PigeonMany.delta_one
#print axioms PigeonMany.sum_cnt
#print axioms PigeonMany.sum_le
#print axioms PigeonMany.searchM_false
#print axioms PigeonMany.pigeon_many

#print axioms: 5 theorems, no axioms.

79.253 Finite-depth extraction on nested grids

If \(|S|\ge mN^d+1\) and \(b_\ell (i)<N\) for levels \(\ell <d\), some \(S'\subseteq S\) with \(|S'|\ge m+1\) lies in one box at every level. \(d\) applications of the generalized pigeonhole; the finite form of the diagonal extraction.

ns/NestExtract.lean

/-
  ns/NestExtract.lean — T37 cycle 1746 (goal G852d): finite-depth extraction on nested grids. Given box indices
  b ℓ i < N for levels ℓ and indices i (truncations) in S, if |S| ≥ m·N^d + 1 then some S' ⊆ S with |S'| ≥ m + 1
  has all its members in the same box at every level ℓ < d — by d applications of the generalized pigeonhole.
  With BoxClose and CloseInterp on grids δ_ℓ this is the finite form of the diagonal (Cauchy) extraction.
  Lean 4 core, hand proofs, no axioms.
-/
import ns.PigeonMany

namespace NestExtract
open Pigeon PigeonMany

def filt (p : Nat → Bool) : List Nat → List Nat
  | [] => []
  | x :: l => cond (p x) (x :: filt p l) (filt p l)

theorem mem_filt (p : Nat → Bool) (x : Nat) : ∀ l : List Nat, x ∈ filt p l → x ∈ l ∧ p x = true
  | [], h => nomatch h
  | y :: l, h => by
    cases e : p y with
    | true =>
      have h' : x ∈ y :: filt p l := by
        have : filt p (y :: l) = y :: filt p l := by show cond (p y) _ _ = _; rw [e]; rfl
        rw [this] at h; exact h
      cases h' with
      | head => exact ⟨List.Mem.head l, e⟩
      | tail _ h2 => exact ⟨List.Mem.tail y (mem_filt p x l h2).1, (mem_filt p x l h2).2⟩
    | false =>
      have h' : x ∈ filt p l := by
        have : filt p (y :: l) = filt p l := by show cond (p y) _ _ = _; rw [e]; rfl
        rw [this] at h; exact h
      exact ⟨List.Mem.tail y (mem_filt p x l h').1, (mem_filt p x l h').2⟩

theorem cnt_map (b : Nat → Nat) (v : Nat) : ∀ l : List Nat,
    cnt v (l.map b) = (filt (fun i => Nat.beq v (b i)) l).length
  | [] => rfl
  | y :: l => by
    show cond (Nat.beq v (b y)) 1 0 + cnt v (l.map b) = (cond (Nat.beq v (b y)) (y :: filt _ l) (filt _ l)).length
    rw [cnt_map b v l]
    cases Nat.beq v (b y) with
    | true => show 1 + _ = _ + 1; exact Nat.add_comm _ _
    | false => exact Nat.zero_add _

theorem allLt_map (N : Nat) (b : Nat → Nat) : ∀ l : List Nat, (∀ i, i ∈ l → b i < N) → allLt N (l.map b)
  | [], _ => True.intro
  | y :: l, h => ⟨h y (List.Mem.head l), allLt_map N b l (fun i hi => h i (List.Mem.tail y hi))⟩

theorem len_map (f : Nat → Nat) : ∀ l : List Nat, (l.map f).length = l.length
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_map f l)

theorem lt_cases {l d : Nat} (h : l < d + 1) : l < d ∨ l = d := by
  cases Nat.lt_or_ge l d with
  | inl hl => exact Or.inl hl
  | inr hg => exact Or.inr (Nat.le_antisymm (Nat.le_of_lt_succ h) hg)

/-- **Nested extraction.** -/
theorem extract (N m : Nat) (b : Nat → Nat → Nat) : ∀ (d : Nat) (S : List Nat),
    (∀ l i, i ∈ S → b l i < N) → m * N ^ d + 1 ≤ S.length →
    ∃ S' : List Nat, (∀ i, i ∈ S' → i ∈ S) ∧ m + 1 ≤ S'.length ∧
      ∀ i j, i ∈ S' → j ∈ S' → ∀ l, l < d → b l i = b l j
  | 0, S, _, hl => ⟨S, fun _ h => h, by
      have e : m * N ^ 0 = m := by show m * 0 + m = m; rw [Nat.mul_zero, Nat.zero_add]
      rw [e] at hl; exact hl,
      fun _ _ _ _ _ h => absurd h (Nat.not_lt_zero _)⟩
  | d + 1, S, hb, hl => by
    have hl' : m * N ^ d * N + 1 ≤ S.length := by
      have e : m * N ^ (d + 1) = m * N ^ d * N := by show m * (N ^ d * N) = m * N ^ d * N; lib_nat
      rw [← e]; exact hl
    cases pigeon_many (m * N ^ d) N (S.map (b d)) (by rw [len_map]; exact hl')
        (allLt_map N (b d) S (fun i hi => hb d i hi)) with
    | intro v hv =>
      rw [cnt_map] at hv
      let S1 := filt (fun i => Nat.beq v (b d i)) S
      have hS1 : ∀ i, i ∈ S1 → i ∈ S ∧ Nat.beq v (b d i) = true := fun i hi => mem_filt _ i S hi
      cases extract N m b d S1 (fun l i hi => hb l i (hS1 i hi).1) hv.2 with
      | intro S' hS' =>
        refine ⟨S', fun i hi => (hS1 i (hS'.1 i hi)).1, hS'.2.1, ?_⟩
        intro i j hi hj l hl2
        cases lt_cases hl2 with
        | inl hlt => exact hS'.2.2 i j hi hj l hlt
        | inr heq =>
          rw [heq]
          have ei := beq_eq _ _ (hS1 i (hS'.1 i hi)).2
          have ej := beq_eq _ _ (hS1 j (hS'.1 j hj)).2
          rw [← ei, ← ej]

end NestExtract

#print axioms NestExtract.mem_filt
#print axioms NestExtract.cnt_map
#print axioms NestExtract.extract

#print axioms: 3 theorems, no axioms.

79.254 A finite Arzelà–Ascoli statement for the interpolants

Among \(mN^d+1\) quantised truncations with the grid Hölder bound, \(m+1\) are pairwise close on \([0,d)\): \((u_M-u_{M'})^2\le 3(2\theta G+\delta ^2)\). NestExtract, BoxClose and CloseInterp combined.

ns/FiniteAA.lean

/-
  ns/FiniteAA.lean — T37 cycle 1750 (goal G852f): a finite Arzelà–Ascoli statement for the scheme's time
  interpolants. Truncations M ∈ S give scalar runs a_M (one coordinate) with the grid Hölder bound
  |a_M(k + j) − a_M(k)|² ≤ j·G, quantised at grid times k < d: L_k(b k M) ≤ a_M(k) ≤ L_k(b k M) + δ with box
  index b k M < N. If |S| ≥ m·N^d + 1, then m + 1 truncations of S are pairwise uniformly close on [0, d):
      (u_M(k + θ) − u_M'(k + θ))² ≤ 3(θG + δ² + θG)    for all k < d, 0 ≤ θ ≤ 1.
  Lean 4 core, hand proofs, no axioms (NestExtract, BoxClose, CloseInterp).
-/
import ns.NestExtract
import ns.BoxClose
import ns.CloseInterp

namespace FiniteAA
open DetChain NSChain LibRing InterpStep InterpCross InterpGrid

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

theorem finite_aa (N m d : Nat) (S : List Nat) (hS : m * N ^ d + 1 ≤ S.length)
    (a : Nat → Nat → R) (G δ : R) (Lb : Nat → Nat → R) (b : Nat → Nat → Nat)
    (hb : ∀ k M, M ∈ S → b k M < N)
    (hlo : ∀ k M, M ∈ S → Le K P (Lb k (b k M)) (a M k))
    (hhi : ∀ k M, M ∈ S → Le K P (a M k) (K.add (Lb k (b k M)) δ))
    (hH : ∀ M, M ∈ S → ∀ k j, Le K P (K.mul (sub K (a M (k + j)) (a M k)) (sub K (a M (k + j)) (a M k))) (nm K j G)) :
    ∃ S' : List Nat, (∀ M, M ∈ S' → M ∈ S) ∧ m + 1 ≤ S'.length ∧
      ∀ M M', M ∈ S' → M' ∈ S' → ∀ k, k < d → ∀ θ e : R, P.nn θ → P.nn e → K.add θ e = K.one →
        Le K P (K.mul (sub K (interp K (a M) k θ) (interp K (a M') k θ)) (sub K (interp K (a M) k θ) (interp K (a M') k θ)))
          (K.mul (three K) (K.add (K.mul θ G) (K.add (K.mul δ δ) (K.mul θ G)))) := by
  cases NestExtract.extract N m b d S (fun k M hM => hb k M hM) hS with
  | intro S' h =>
    refine ⟨S', h.1, h.2.1, ?_⟩
    intro M M' hM hM' k hk θ e hθ he hθe
    have hMS := h.1 M hM
    have hM'S := h.1 M' hM'
    have eb : b k M = b k M' := h.2.2 M M' hM hM' k hk
    have hD := BoxClose.box_close K P (Lb k (b k M)) δ (a M k) (a M' k) (hlo k M hMS) (hhi k M hMS)
      (by rw [eb]; exact hlo k M' hM'S) (by rw [eb]; exact hhi k M' hM'S)
    exact CloseInterp.close_interp K P (a M) (a M') G _ (hH M hMS) (hH M' hM'S) k hD θ e hθ he hθe

end Gen
end FiniteAA

#print axioms FiniteAA.finite_aa

#print axioms: 1 theorem, no axioms.

79.255 Gronwall step with a source

\(Q_{t+1}\le \nu Z_t+h(\alpha _tZ_t+\beta _tA_t)+hS_t\), \(\beta _t\le \nu ^2\) give \(\nu Q_{t+1}\le (\nu +h\alpha _t)Q_t+\nu hS_t\); with \(\nu +h\alpha _t\le \nu g\), \(S_t\le S\): \(Q_n\le g^n(Q_0+n\,hS)\). For comparing a truncation with a perturbed one.

ns/GronwallSrc.lean

/-
  ns/GronwallSrc.lean — T37 cycle 1764 (goal G852g, piece a): the enstrophy-difference Gronwall step with a source,
  for comparing a scheme with a perturbed one (truncation defect). Over an ordered field, Q_t = νZ_t + hν²A_t:
    one_step_src   Q_{t+1} ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·S_t,  β_t ≤ ν²   ⇒   νQ_{t+1} ≤ (ν + hα_t)Q_t + νhS_t
    gronwall_src   if moreover ν + hα_t ≤ νg, 1 ≤ g, S_t ≤ S (ν ≠ 0)   ⇒   Q_n ≤ gⁿ·(Q_0 + n·hS)
  Lean 4 core plus ns/EnstGronwall, ns/ErrRec.
-/
import ns.EnstGronwall
import ns.ErrRec

namespace GronwallSrc
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_step_src (ν h : R) (Z A α β S : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hA : ∀ t, O.nn (A t))
    (hα : ∀ t, O.nn (α t)) (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t))))
    (t : Nat) :
    Le K O.toCone (K.mul ν (Q K ν h Z A (t + 1)))
      (K.add (K.mul (K.add ν (K.mul h (α t))) (Q K ν h Z A t)) (K.mul ν (K.mul h (S t)))) := by
  have b1 := ForceSob.le_mul_right' K O.toCone (hβ t) (hA t)
  have b2 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (α t) (Z t))) b1)
  have s1 := le_trans' K O.toCone _ _ _ (step t)
    (le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z t))) b2)
      (le_refl' K O.toCone (K.mul h (S t))))
  have s2 := le_mul_nn K O.toCone ν _ _ hν s1
  refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)),
    O.nn_mul _ _ (O.nn_mul _ _ hh (hα t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t)), ?_⟩
  show K.add (K.mul ν (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (K.mul ν ν) (A t)))))
        (K.mul h (S t))))
      (K.mul (K.mul h (α t)) (K.mul (K.mul h (K.mul ν ν)) (A t)))
    = K.add (K.mul (K.add ν (K.mul h (α t))) (K.add (K.mul ν (Z t)) (K.mul (K.mul h (K.mul ν ν)) (A t))))
        (K.mul ν (K.mul h (S t)))
  lib_ring

theorem gronwall_src (ν h g Sb : R) (Z A α β S : Nat → R) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hZ : ∀ t, O.nn (Z t)) (hA : ∀ t, O.nn (A t)) (hα : ∀ t, O.nn (α t))
    (hβ : ∀ t, Le K O.toCone (β t) (K.mul ν ν))
    (hg : Le K O.toCone K.one g) (hαg : ∀ t, Le K O.toCone (K.add ν (K.mul h (α t))) (K.mul ν g))
    (hS : ∀ t, Le K O.toCone (S t) Sb) (hSb : O.nn Sb)
    (step : ∀ t, Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (α t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (S t)))) :
    ∀ n, Le K O.toCone (Q K ν h Z A n)
      (K.mul (pwr K g n) (K.add (Q K ν h Z A 0) (K.mul (castN K n) (K.mul h Sb)))) := by
  have hQ : ∀ t, O.nn (Q K ν h Z A t) := fun t =>
    O.nn_add _ _ (O.nn_mul _ _ hν (hZ t)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA t))
  apply ErrRec.rec_src K O g (K.mul h Sb) (Q K ν h Z A) hg (O.nn_mul _ _ hh hSb)
  intro t
  have o := one_step_src K O ν h Z A α β S hν hh hA hα hβ step t
  have c1 := ForceSob.le_mul_right' K O.toCone (hαg t) (hQ t)
  have c2 := le_mul_nn K O.toCone ν _ _ hν (le_mul_nn K O.toCone h _ _ hh (hS t))
  have c := le_trans' K O.toCone _ _ _ o (le_add' K O.toCone _ _ _ _ c1 c2)
  apply EnstGlobal.le_of_mul_le K O ν _ _ hν hν0
  refine le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

end Gen
end GronwallSrc

#print axioms GronwallSrc.one_step_src
#print axioms GronwallSrc.gronwall_src

#print axioms: 2 theorems, no axioms.

79.256 Enstrophy-difference step with a force defect

If \(\sum |N^a_t-N^b_t|^2\le 2\sum |N_l(a_t)-N_l(b_t)|^2+2D_t\), then \(Q_{t+1}(a-b)\le \nu Z_t+h(\alpha _tZ_t+\beta _tA_t)+2hD_t\). The step hypothesis of the Gronwall estimate with a source, for a truncation against a finer one.

ns/ForcedDiffStep.lean

/-
  ns/ForcedDiffStep.lean — T37 cycle 1766 (goal G852g, piece b): one step of the enstrophy-difference estimate when
  the forces differ from the Galerkin nonlinearities by a defect: Σ|N^a_t − N^b_t|² ≤ 2Σ|Nl(a_t) − Nl(b_t)|² + 2D_t
  (e.g. b = the low-mode projection of a finer truncation, D_t its truncation defect, TruncBound). Then
      Q_{t+1}(a − b) ≤ νZ_t + h(α_tZ_t + β_tA_t) + h·(2D_t)
  with α_t, β_t twice those of EnstLipschitz — exactly the step hypothesis of GronwallSrc.gronwall_src.
  Lean 4 core plus ns/EnstLipschitz.
-/
import ns.EnstLipschitz

namespace ForcedDiffStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff EnstGronwall LatticeSum EnstLipschitz

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem forced_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a b Na Nb : Nat → I → V3 R) (D : Nat → R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (sub3 K (Na t r) (Nb t r)) (sub3 K (Na t r) (Nb t r))) l)
      (K.add (K.mul (two K) (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (b t) (b t) r))) l)) (K.mul (two K) (D t))))
    (ha : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (Na t r))
    (hb : ∀ t r, Step K h ν (wt K kz r) (b (t + 1) r) (b t r) (Nb t r)) (s : Nat) :
    Le K O.toCone (Q K ν h (Zs K l kz (dS K a b)) (As K l kz (dS K a b)) (s + 1))
      (K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s))
        (K.mul h (K.add
          (K.mul (K.mul (two K) (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s)))))) (Zs K l kz (dS K a b) s))
          (K.mul (K.mul (two K) (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))
            (As K l kz (dS K a b) s)))))
        (K.mul h (K.mul (two K) (D s)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have n2 : O.nn (two K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have st := enst_step K O.toCone l h ν hh hν (wt K kz) hw (dS K a b (s + 1)) (dS K a b s)
    (fun r => sub3 K (Na s r) (Nb s r)) (fun r => step_sub K h ν (wt K kz r) _ _ _ _ _ _ (ha s r) (hb s r))
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι (a s) (b s)
  have fd2 := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two K) _ _ n2 fd) (le_refl' K O.toCone (K.mul (two K) (D s)))
  have hb2 := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ (hN s) fd2)
  have st2 := le_trans' K O.toCone _ _ _ st (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb2)
  refine le_trans' K O.toCone _ _ _ st2 ⟨K.zero, O.nn_zero, ?_⟩
  show K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s)) (K.mul h (K.add (K.mul (two K) (K.add
      (K.mul (two K) (K.add
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (Zs K l kz (dS K a b) s) (K.mul (C1 K inv Rc) (Zs K l kz a s)))))
        (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz (dS K a b) s) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))))
      (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (As K l kz b s) (K.mul (castN K 135) (Zs K l kz (dS K a b) s)))))))
      (K.mul (two K) (D s))))) K.zero
    = K.add (K.add (K.mul ν (Zs K l kz (dS K a b) s))
        (K.mul h (K.add
          (K.mul (K.mul (two K) (K.add (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C1 K inv Rc) (Zs K l kz a s))))
            (K.mul (two K) (K.mul (K.mul ι ι) (K.mul (castN K 135) (As K l kz b s)))))) (Zs K l kz (dS K a b) s))
          (K.mul (K.mul (two K) (K.mul (two K) (K.mul (K.mul (two K) (K.mul ι ι)) (K.mul (C2 K inv Rc t0) (Zs K l kz a s)))))
            (As K l kz (dS K a b) s)))))
        (K.mul h (K.mul (two K) (D s)))
  lib_ring

end Gen
end ForcedDiffStep

#print axioms ForcedDiffStep.forced_step

#print axioms: 1 theorem, no axioms.

79.257 Time-uniform truncation estimate from a nonzero initial difference

\(\nu ^2Q_d(t_2+n)\le \nu ^2Q_d(t_2)+i_R\kappa \,2Q_2(A,t_2)\) for every \(n\), for small states at \(t_2\). The coarse scheme need not start at the restriction of the fine one.

ns/TruncUniform0.lean

/-
  ns/TruncUniform0.lean — T37 cycle 1866 (goal G859b, piece b): TruncUniform without B_0 = A_0|_N.
    trunc_uniform0       the coarse scheme B starts from any small state (cH·ι²·Q₂(B, 0) ≤ ν³, Q₂(B, 0) ≤ ν·Aa):
                         ν²·Q_d(n) ≤ ν²·Q_d(0) + iR·κ·2Q₂(A, 0)  for every n
    trunc_uniform0_from  the same for the schemes shifted by t₂ (A(t₂ + ·), B(t₂ + ·)), so the initial difference
                         Q_d(0) is the difference at time t₂ left by a finite-time estimate on [0, t₂].
  Lean 4 core plus ns/TruncUniform, ns/TruncDamp0.
-/
import ns.TruncUniform
import ns.TruncDamp0

namespace TruncUniform0
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_uniform0 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) B 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) B 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n))
      (K.add (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A   -- (+11 chars; full line in the Zenodo source)
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ two (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have ncH : O.nn (EnstH2.cH K) := O.nn_add _ _ (O.nn_mul _ _ two (O.nn_mul _ _ three n135)) (O.nn_mul _ _ two n135)
  have nc : O.nn (K.mul (EnstH2.cH K) (K.mul ι ι)) := O.nn_mul _ _ ncH (O.nn_sq ι)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQ0 : O.nn (QH K ν h kz l A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have smallA : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0)) (K.mul ν (K.mul ν ν)) :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz l A 0), O.nn_mul _ _ nc hQ0,
      by lib_ring⟩ small2
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have nkap : O.nn (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) :=
    O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC2 hAa))))) (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))
  have hS : ∀ t, Le K O.toCone (St K inv kz l R' t0 ι Aa iR A t) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (Bl K kz l (A t))) := by
    intro t
    obtain ⟨d, hdn, hde⟩ := A_le_B K O kz h1 l (A t)
    refine ⟨K.mul (K.mul iR (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa)))
      (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) d,
      O.nn_mul _ _ (O.nn_mul _ _ hiRn (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
        (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))) hdn, ?_⟩
    unfold St
    rw [← hde]
    generalize Al K kz l (A t) = Av
    generalize castN K 135 = c135
    lib_ring
  have t := TruncDamp0.trunc_damp_sum0 K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B hA hB smallA hQA smallB hQB hsm n
  have hs := IntForce.tsum_le K O h hh _ _ hS n
  rw [tsum_hc K h (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (fun t => Bl K kz l (A t)) n] at hs
  have d : Le K O.toCone (K.mul (K.mul h (K.mul ν ν)) (sumN K (fun t => Bl K kz l (A t)) n))
      (K.mul (K.add K.one K.one) (QH K ν h kz l A 0)) := diss_B K O inv mul_inv h10 l kz hd h1 M hM ι h ν hh hν hν0 A hA small2 n
  have u := le_trans' K O.toCone _ _ _ t (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hs)
  have t2 := le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) u
  refine le_trans' K O.toCone _ _ _ t2 (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
    (le_mul_nn K O.toCone (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) _ _ (O.nn_mul _ _ hiRn nkap) d)))
  generalize sumN K (fun t => Bl K kz l (A t)) n = X
  generalize castN K 135 = c135
  lib_ring

include mul_inv h10 in
theorem trunc_uniform0_from (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R) (t2 : Nat)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l (fun t => A (t2 + t)) 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l (fun t => A (t2 + t)) 0))
      (K.mul ν (K.mul ν ν))) :
    Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) n))
      (K.add (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) 0))
      (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l (fun t => A (t2 + t)) 0)))) :=
  trunc_uniform0 K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR
    (fun t => A (t2 + t)) (fun t => B (t2 + t)) (fun t r => hA (t2 + t) r) (fun t r => hB (t2 + t) r) hQA smallB hQB hsm n small2

end Gen
end TruncUniform0

#print axioms TruncUniform0.trunc_uniform0
#print axioms TruncUniform0.trunc_uniform0_from

#print axioms: 2 theorems, no axioms.

79.258 Gluing a finite-time truncation estimate to the time-uniform one

\(\nu ^{t_2}Q_d(t_2)\le R_r\Rightarrow \nu ^{t_2}\nu ^2Q_d(t_2+n)\le \nu ^2R_r+\nu ^{t_2}i_R\kappa \,2Q_2(A,t_2)\) for every \(n\). Conditional on the finite-time bound \(R_r\) on \([0,t_2]\).

ns/TruncGlue.lean

/-
  ns/TruncGlue.lean — T37 cycle 1866 (goal G859c, conditional gluing): a finite-time truncation estimate on [0, t₂]
  and the time-uniform estimate after t₂ (TruncUniform0.trunc_uniform0_from) combine to an estimate on [0, ∞):
    glue_alg    V·X ≤ V·Y + K, P·Y ≤ Rr  ⇒  P·(V·X) ≤ V·Rr + P·K   (P, V ≥ 0)
    trunc_glue  if ν^{t₂}·Q_d(t₂) ≤ Rr (any finite-time estimate, e.g. TruncRate3.trunc_rate3 at T = t₂) and the
                smallness hypotheses of trunc_uniform0_from hold at t₂, then for every n
                ν^{t₂}·ν²·Q_d(t₂ + n) ≤ ν²·Rr + ν^{t₂}·iR·κ·2Q₂(A, t₂).
  The finite-time bound Rr is a hypothesis: for large data it needs H² bounds on [0, t₂] uniform in the fine
  truncation, which is not proved here.
  Lean 4 core plus ns/TruncUniform0.
-/
import ns.TruncUniform0

namespace TruncGlue
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform TruncUniform0

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem glue_alg (P V X Y Kc Rr : R) (hP : O.nn P) (hV : O.nn V) (u : Le K O.toCone (K.mul V X) (K.add (K.mul V Y) Kc))
    (r : Le K O.toCone (K.mul P Y) Rr) : Le K O.toCone (K.mul P (K.mul V X)) (K.add (K.mul V Rr) (K.mul P Kc)) := by
  refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone P _ _ hP u) (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone V _ _ hV r) (le_refl' K O.toCone _)))
  lib_ring

include mul_inv h10 in
theorem trunc_glue (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa iR : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (hiRn : O.nn iR) (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R) (t2 : Nat)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (hQA : Le K O.toCone (QH K ν h kz l (fun t => A (t2 + t)) 0) (K.mul ν Aa))
    (smallB : Le K O.toCone (K.mul (K.mul (EnstH2.cH K) (K.mul ι ι)) (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0)) (K.mul ν (K.mul ν ν)))
    (hQB : Le K O.toCone (QH K ν h kz (l.filter (lowB Rc kz)) (fun t => B (t2 + t)) 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν)) (n : Nat)
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l (fun t => A (t2 + t)) 0))
      (K.mul ν (K.mul ν ν)))
    (Rr : R) (hfin : Le K O.toCone (K.mul (pwr K ν t2) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) t2)) Rr) :
    Le K O.toCone (K.mul (pwr K ν t2) (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t))))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A (t2 + t))) (B (t2 + t)))) n)))
      (K.add (K.mul (K.mul ν ν) Rr) (K.mul (pwr K ν t2) (K.mul (K.mul iR (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l (fun t => A (t2 + t)) 0))))) :=
  glue_alg K O (pwr K ν t2) (K.mul ν ν) _ _ _ Rr (EnstGronwall.pwr_nn K O ν hν t2) (O.nn_sq ν)
    (trunc_uniform0_from K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B t2
      hA hB hQA smallB hQB hsm n small2) hfin

end Gen
end TruncGlue

#print axioms TruncGlue.glue_alg
#print axioms TruncGlue.trunc_glue

#print axioms: 2 theorems, no axioms.

79.259 Metastable windows from the truncation rate

\(i_N\kappa \,2Q_2\le \varepsilon \Rightarrow \) for every \(F:\mathbb N\to \mathbb N\) some \(N_0\) has every cutoff \(R\in [N_0,F(N_0)]\) with \(\nu ^2Q_d(n)\le \varepsilon \) for all \(n\). Small data; \(N_0=N\) explicit.

ns/TruncWindow.lean

/-
  ns/TruncWindow.lean — T37 cycle 1866 (goal G852e, restated): metastability from a convergence modulus.
  The window form 'for every ε and every F : ℕ → ℕ some window [N, F(N)] of cutoffs is ε-close' needs a rate; with
  the rate of TruncUniform (error ≤ iR·κ·2Q₂(A, 0), iR = 1/(R+1)², κ independent of R) it holds with N explicit:
    inv_anti      a ≤ b, a·ia = 1, b·ib = 1, ia, ib ≥ 0  ⇒  ib ≤ ia
    iR_anti       N ≤ R  ⇒  1/(R+1)² ≤ 1/(N+1)²
    trunc_tail    iN·κ·2Q₂ ≤ ε  ⇒  every cutoff R ≥ N has ν²·Q_d(n) ≤ ε for all times n
    trunc_window  for every F : ℕ → ℕ there is N₀ (= N) with every cutoff R in [N₀, F(N₀)] ε-close at all times
  (small data: the smallness hypotheses of TruncUniform). The window statement without a rate (from compactness
  alone) is the convergence question itself and stays with the main problem.
  Lean 4 core plus ns/TruncUniform.
-/
import ns.TruncUniform

namespace TruncWindow
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncSmall
  TruncStart TruncRate3 TruncStart3 SchemeTele TruncDampSum TruncUniform

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem inv_anti (a b ia ib : R) (hia : O.nn ia) (hib : O.nn ib) (hab : Le K O.toCone a b)
    (ha : K.mul a ia = K.one) (hb : K.mul b ib = K.one) : Le K O.toCone ib ia := by
  cases hab with
  | intro d hd =>
    refine ⟨K.mul (K.mul d ia) ib, O.nn_mul _ _ (O.nn_mul _ _ hd.1 hia) hib, ?_⟩
    have e1 : K.add ib (K.mul (K.mul d ia) ib) = K.add (K.mul (K.mul a ia) ib) (K.mul (K.mul d ia) ib) := by
      rw [ha]; lib_ring
    have e2 : K.add (K.mul (K.mul a ia) ib) (K.mul (K.mul d ia) ib) = K.mul (K.mul (K.add a d) ib) ia := by lib_ring
    rw [e1, e2, hd.2, hb]; lib_ring

theorem iR_anti (N Rc : Nat) (hN : N ≤ Rc) (iN iR : R) (hiNn : O.nn iN) (hiRn : O.nn iR)
    (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one) : Le K O.toCone iR iN :=
  inv_anti K O _ _ iN iR hiNn hiRn
    (EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
      (RatModel.cast_mono K O.toCone _ _ (Nat.succ_le_succ hN))) hiN hiR

include mul_inv h10 in
theorem trunc_tail (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (A : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν))
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν)))
    (N : Nat) (iN eps : R) (hiNn : O.nn iN) (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (heps : Le K O.toCone (K.mul (K.mul iN (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A 0))) eps) :
    ∀ Rc, N ≤ Rc → ∀ (iR : R), O.nn iR → K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one →
      ∀ (B : Nat → I → V3 R), (∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)) →
      B 0 = restr K (lowB Rc kz) (A 0) →
      ∀ n, Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)) eps := by
  intro Rc hN iR hiRn hiR B hB hB0 n
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hQ0 : O.nn (QH K ν h kz l A 0) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l))
      (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν))
        (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (hw p)) (dot3_nn K O.toCone _)) l))
  have n2 : O.nn (two' K) := two
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have nkap : O.nn (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135)))))) :=
    O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hAa)))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nC2 hAa))))) (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa n135))))
  have u := trunc_uniform K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Aa iR hh hν hν0 hAa hiRn hiR A B hA hB
    hQA hB0 hsm n small2
  have m := le_mul_right' K O.toCone (le_mul_right' K O.toCone (iR_anti K O N Rc hN iN iR hiNn hiRn hiN hiR) nkap)
    (O.nn_mul _ _ two hQ0)
  exact le_trans' K O.toCone _ _ _ u (le_trans' K O.toCone _ _ _ m heps)

include mul_inv h10 in
theorem trunc_window (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Aa : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hAa : O.nn Aa)
    (A : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hQA : Le K O.toCone (QH K ν h kz l A 0) (K.mul ν Aa))
    (hsm : Le K O.toCone (K.add (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Aa))))
      (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa)))) (K.mul ν ν))
    (small2 : Le K O.toCone (K.mul (K.add (K.mul (EnstH2.cH K) (K.mul ι ι)) (K.mul (EnstH2.cH K) (K.mul ι ι))) (QH K ν h kz l A 0))
      (K.mul ν (K.mul ν ν)))
    (N : Nat) (iN eps : R) (hiNn : O.nn iN) (hiN : K.mul (K.mul (castN K (N + 1)) (castN K (N + 1))) iN = K.one)
    (heps : Le K O.toCone (K.mul (K.mul iN (K.mul (two' K) (K.add (K.mul (two' K) (K.add (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Aa))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Aa))))) (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (castN K 135))))))) (K.mul (K.add K.one K.one) (QH K ν h kz l A 0))) eps) :
    ∀ F : Nat → Nat, ∃ N0, ∀ Rc, N0 ≤ Rc → Rc ≤ F N0 → ∀ (iR : R), O.nn iR → K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one →
      ∀ (B : Nat → I → V3 R), (∀ t r, Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)) →
      B 0 = restr K (lowB Rc kz) (A 0) →
      ∀ n, Le K O.toCone (K.mul (K.mul ν ν) (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
        (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) n)) eps :=
  fun _ => ⟨N, fun Rc hN _ => trunc_tail K O inv mul_inv h10 l kz hd h1 M hM R' t0 hRM ι h ν Aa hh hν hν0 hAa A hA hQA
    hsm small2 N iN eps hiNn hiN heps Rc hN⟩

end Gen
end TruncWindow

#print axioms TruncWindow.inv_anti
#print axioms TruncWindow.iR_anti
#print axioms TruncWindow.trunc_tail
#print axioms TruncWindow.trunc_window

#print axioms: 4 theorems, no axioms.

79.260 Cauchy rates in the NS ring as Cauchy reals

\((s_{j+J}-s_j)^2\le b_j\) and an Archimedean choice of \(N\) give a Cauchy sequence of the shared Cauchy-real library. The NS ordered ring is a positive cone of that library.

ns/RealBridge.lean

/-
  ns/RealBridge.lean — T37 cycle 1866 (goal G860a, real layer): the NS ordered ring (OrdR, cone order Le) as a positive
  cone of the shared Cauchy-real library (lib/CauchySeq), and a squared rate with an Archimedean hypothesis gives a
  Cauchy sequence in the library sense:
    posO              the cone of O as LibCS.Pos
    le_of_Le          Le x y  ⇒  le x y
    close_of_sq       (a − b)² ≤ q², q ≥ 0  ⇒  Close q a b
    close_mono        Close e a b, e ≤ e'  ⇒  Close e' a b
    cauchy_of_sq_rate (s_{j+J} − s_j)² ≤ b_j for all j, J, and for every e > 0 some q, N with q + q ≤ e and b_N ≤ q²
                      ⇒  Cauchy s
  This turns the explicit moduli of DyadicCoef/StepCauchy into elements of the Cauchy reals (lib/CReal).
  Lean 4 core plus ns/DyadicSmallGlobal, lib/CauchySeq.
-/
import ns.DyadicSmallGlobal
import lib.CauchySeq

namespace RealBridge
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def posO : Pos K := { nn := O.nn, nn_sq := O.nn_sq, nn_add := O.nn_add, nn_mul := O.nn_mul, nn_zero := O.nn_zero }

theorem le_of_Le {x y : R} (h : Le K O.toCone x y) : LibGron.le K (posO K O) x y := by
  cases h with
  | intro d hd =>
    show O.nn (sub K y x)
    rw [← hd.2, show sub K (K.add x d) x = d by lib_ring]
    exact hd.1

theorem close_of_sq (a b q : R) (hq : O.nn q)
    (h : Le K O.toCone (K.mul (sub K a b) (sub K a b)) (K.mul q q)) : Close K (posO K O) q a b := by
  have h1 := le_of_sq_le K O (sub K a b) q hq h
  have e : K.mul (sub K b a) (sub K b a) = K.mul (sub K a b) (sub K a b) := by lib_ring
  have h2 := le_of_sq_le K O (sub K b a) q hq (by rw [e]; exact h)
  exact ⟨le_of_Le K O h1, le_of_Le K O h2⟩

theorem le_dest' {m p : Nat} (h : m ≤ p) : ∃ d, m + d = p := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    cases ih with
    | intro d hd => exact ⟨d + 1, congrArg Nat.succ hd⟩

theorem close_mono (P : Pos K) {e e' a b : R} (h : Close K P e a b) (he : LibGron.le K P e e') : Close K P e' a b :=
  ⟨LibGron.le_trans K P h.1 he, LibGron.le_trans K P h.2 he⟩

theorem cauchy_of_sq_rate (s b : Nat → R)
    (hb : ∀ j J, Le K O.toCone (K.mul (sub K (s (j + J)) (s j)) (sub K (s (j + J)) (s j))) (b j))
    (arch : ∀ e, PosE K (posO K O) e → ∃ q N, O.nn q ∧ Le K O.toCone (K.add q q) e ∧ Le K O.toCone (b N) (K.mul q q)) :
    Cauchy K (posO K O) s := by
  intro e he
  cases arch e he with
  | intro q hq' =>
    cases hq' with
    | intro N hN =>
      refine ⟨N, fun m n hm hn => ?_⟩
      cases le_dest' hm with
      | intro k hk =>
        cases le_dest' hn with
        | intro k' hk' =>
          have c1 := close_of_sq K O (s (N + k)) (s N) q hN.1 (le_trans' K O.toCone _ _ _ (hb N k) hN.2.2)
          have c2 := close_of_sq K O (s (N + k')) (s N) q hN.1 (le_trans' K O.toCone _ _ _ (hb N k') hN.2.2)
          rw [hk] at c1
          rw [hk'] at c2
          exact close_mono K (posO K O) (close_tri K (posO K O) c1 (close_symm K (posO K O) c2)) (le_of_Le K O hN.2.1)

end Gen
end RealBridge

#print axioms RealBridge.le_of_Le
#print axioms RealBridge.close_of_sq
#print axioms RealBridge.le_dest'
#print axioms RealBridge.close_mono
#print axioms RealBridge.cauchy_of_sq_rate

#print axioms: 5 theorems, no axioms.

79.261 The integrated Galerkin equation in the limit

\(\lim _j a_j(n_j)-\lim _j a_j(0)\approx \lim _j\sum _{k<n_j}h_j(N_{j,k}-\nu w\,a_j(k+1))\) in the Cauchy reals. The Riemann sums converge whenever the coefficients do.

ns/RealIntegral.lean

/-
  ns/RealIntegral.lean — T37 cycle 1868 (goal G860b, real layer): the integrated Galerkin equation passes to the
  limit in the Cauchy reals.
    ofO           the NS ordered ring with an inverse as an ordered field of lib/OField (LibOF.OF)
    lim_diff      x, y Cauchy and x_j − y_j = S_j for every j  ⇒  S Cauchy and x − y ≈ S
    lim_integral  for step-halving solutions a_j (StepC with step h_j) and grid times n_j: if j ↦ a_j(n_j) and
                  j ↦ a_j(0) are Cauchy, the Riemann sums S_j = Σ_{k<n_j} h_j(N_{j,k} − νw·a_j(k+1)) are Cauchy and
                  lim a_j(n_j) − lim a_j(0) ≈ lim S_j   (the limit of the Riemann sums is the time integral).
  Lean 4 core plus ns/RealBridge, ns/IntegralForm.
-/
import ns.RealBridge
import ns.IntegralForm
import lib.CReal

namespace RealIntegral
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

def ofO : LibOF.OF K (posO K O) where
  inv := inv
  mul_inv := mul_inv
  total := fun x => by
    cases O.total K.zero x with
    | inl h =>
      cases h with
      | intro d hd =>
        left
        show O.nn x
        rw [← hd.2, K.zero_add]
        exact hd.1
    | inr h =>
      cases h with
      | intro d hd =>
        right
        show O.nn (K.neg x)
        have e : K.neg x = d := by
          rw [show K.neg x = K.add (K.neg x) (K.add x d) by rw [hd.2]; lib_ring]
          lib_ring
        rw [e]
        exact hd.1
  antisymm := fun x h1 h2 =>
    O.antisym x K.zero ⟨K.neg x, h2, by lib_ring⟩ ⟨x, h1, K.zero_add x⟩

theorem lim_diff (P : Pos K) (F : LibOF.OF K P) (x y S : Nat → R) (hx : Cauchy K P x) (hy : Cauchy K P y)
    (h : ∀ j, K.add (x j) (K.neg (y j)) = S j) :
    Cauchy K P S ∧ Eqv K P (addS K x (negS K y)) S := by
  have c := cauchy_add K P F hx (cauchy_neg K P hy)
  refine ⟨fun e he => ?_, LibCReal.eqv_of_pt K P h⟩
  cases c e he with
  | intro N hN =>
    refine ⟨N, fun m n hm hn => ?_⟩
    have t := hN m n hm hn
    rw [show addS K x (negS K y) m = S m from h m, show addS K x (negS K y) n = S n from h n] at t
    exact t

include mul_inv in
theorem lim_integral (ν w : R) (hj : Nat → R) (a N : Nat → Nat → R) (nT : Nat → Nat)
    (hs : ∀ j k, EnstStep.StepC K (hj j) ν w (a j (k + 1)) (a j k) (N j k))
    (hT : Cauchy K (posO K O) (fun j => a j (nT j))) (h0 : Cauchy K (posO K O) (fun j => a j 0)) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (hj j) (K.add (N j k) (K.neg (K.mul (K.mul ν w) (a j (k + 1)))))) (nT j))
    ∧ Eqv K (posO K O) (addS K (fun j => a j (nT j)) (negS K (fun j => a j 0)))
        (fun j => sumN K (fun k => K.mul (hj j) (K.add (N j k) (K.neg (K.mul (K.mul ν w) (a j (k + 1)))))) (nT j)) :=
  lim_diff K (posO K O) (ofO K O inv mul_inv) _ _ _ hT h0
    (fun j => IntegralForm.integral K (hj j) ν w (a j) (N j) (hs j) (nT j))

end Gen
end RealIntegral

#print axioms RealIntegral.ofO
#print axioms RealIntegral.lim_diff
#print axioms RealIntegral.lim_integral

#print axioms: 3 theorems, no axioms.

79.262 Halving rates are Cauchy in an Archimedean ordered field

\((s_{j+J}-s_j)^2\le D\,h^j\) with \(2h=1\) and every element below some \(n\cdot 1\) \(\Rightarrow \) \(s\) is Cauchy. Uses \(n\cdot 1\le 2^n\).

ns/RealArch.lean

/-
  ns/RealArch.lean — T37 cycle 1870 (goal G860c, generic part): in an ordered ring with inverses where every element
  is below some n·1 (Archimedean), a halving rate D·(1/2)^j gives the Archimedean choice of RealBridge:
    pwr_mul2      aⁿ·bⁿ = (ab)ⁿ
    pwr_one'      1ⁿ = 1
    cast_le_pow   1 ≤ 2ⁿ and n·1 ≤ 2ⁿ
    arch_half     for every e > 0 some q, N with q + q ≤ e and D·hᴺ ≤ q²   (h·2 = 1, q = e·h)
    cauchy_half   (s_{j+J} − s_j)² ≤ D·hʲ for all j, J  ⇒  s is Cauchy (lib/CauchySeq)
  The Archimedean hypothesis for ℚ itself is G860c2.
  Lean 4 core plus ns/RealBridge, ns/EnstGronwall, ns/DyadicLarge, ns/RatModel.
-/
import ns.RealBridge
import ns.EnstGronwall
import ns.DyadicLarge
import ns.RatModel

namespace RealArch
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge EnstGronwall DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def tw : R := K.add K.one K.one

theorem pwr_mul2 (a b : R) : ∀ n, K.mul (pwr K a n) (pwr K b n) = pwr K (K.mul a b) n
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul a (pwr K a n)) (K.mul b (pwr K b n)) = K.mul (K.mul a b) (pwr K (K.mul a b) n)
    rw [← pwr_mul2 a b n]; lib_ring

theorem pwr_one' : ∀ n, pwr K K.one n = K.one
  | 0 => rfl
  | n + 1 => by
    show K.mul K.one (pwr K K.one n) = K.one
    rw [pwr_one' n]; lib_ring

theorem cast_le_pow : ∀ n, Le K O.toCone K.one (pwr K (tw K) n) ∧ Le K O.toCone (castN K n) (pwr K (tw K) n)
  | 0 => ⟨le_refl' K O.toCone _, ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add K.zero K.one = K.one; lib_ring⟩⟩
  | n + 1 => by
    cases cast_le_pow n with
    | intro h1 h2 =>
      cases h1 with
      | intro d1 hd1 =>
        cases h2 with
        | intro d2 hd2 =>
          constructor
          · refine ⟨K.add (pwr K (tw K) n) d1, O.nn_add _ _ (pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone)
              (RatModel.nn_one' K O.toCone)) n) hd1.1, ?_⟩
            show K.add K.one (K.add (pwr K (tw K) n) d1) = K.mul (tw K) (pwr K (tw K) n)
            rw [← hd1.2]; unfold tw; lib_ring
          · refine ⟨K.add d1 d2, O.nn_add _ _ hd1.1 hd2.1, ?_⟩
            show K.add (K.add K.one (castN K n)) (K.add d1 d2) = K.mul (tw K) (pwr K (tw K) n)
            calc K.add (K.add K.one (castN K n)) (K.add d1 d2)
                = K.add (K.add K.one d1) (K.add (castN K n) d2) := by lib_ring
              _ = K.add (pwr K (tw K) n) (pwr K (tw K) n) := by rw [hd1.2, hd2.2]
              _ = K.mul (tw K) (pwr K (tw K) n) := by unfold tw; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem arch_half (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (D hh : R) (hhn : O.nn hh)
    (hh2 : K.mul hh (tw K) = K.one) :
    ∀ e, PosE K (posO K O) e → ∃ q N, O.nn q ∧ Le K O.toCone (K.add q q) e
      ∧ Le K O.toCone (K.mul D (pwr K hh N)) (K.mul q q) := by
  intro e he
  have hen : O.nn e := he.1
  have hE0 : K.mul (K.mul e e) (K.mul hh hh) ≠ K.zero := by
    intro z
    cases O.noZD _ _ z with
    | inl a =>
      cases O.noZD _ _ a with
      | inl b => exact he.2 b
      | inr b => exact he.2 b
    | inr a =>
      have hz : hh = K.zero := by
        cases O.noZD _ _ a with
        | inl b => exact b
        | inr b => exact b
      apply h10
      rw [← hh2, hz]; lib_ring
  have hEn : O.nn (K.mul (K.mul e e) (K.mul hh hh)) := O.nn_mul _ _ (O.nn_sq e) (O.nn_sq hh)
  have hy := mul_inv _ hE0
  cases arch (K.mul D (inv (K.mul (K.mul e e) (K.mul hh hh)))) with
  | intro n hn =>
    have h := le_trans' K O.toCone _ _ _ hn (cast_le_pow K O n).2
    have m1 := le_mul_nn K O.toCone (pwr K hh n) _ _ (pwr_nn K O hh hhn n)
      (le_mul_nn K O.toCone (K.mul (K.mul e e) (K.mul hh hh)) _ _ hEn h)
    have e1 : K.mul (pwr K hh n) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (K.mul D (inv (K.mul (K.mul e e) (K.mul hh hh)))))
        = K.mul D (pwr K hh n) := by
      calc _ = K.mul (K.mul D (pwr K hh n)) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (inv (K.mul (K.mul e e) (K.mul hh hh)))) := by
            lib_ring
        _ = K.mul D (pwr K hh n) := by rw [hy]; lib_ring
    have e2 : K.mul (pwr K hh n) (K.mul (K.mul (K.mul e e) (K.mul hh hh)) (pwr K (tw K) n)) = K.mul (K.mul e hh) (K.mul e hh) := by
      calc _ = K.mul (K.mul (K.mul e e) (K.mul hh hh)) (K.mul (pwr K hh n) (pwr K (tw K) n)) := by lib_ring
        _ = K.mul (K.mul (K.mul e e) (K.mul hh hh)) K.one := by rw [pwr_mul2, hh2, pwr_one']
        _ = K.mul (K.mul e hh) (K.mul e hh) := by lib_ring
    rw [e1, e2] at m1
    refine ⟨K.mul e hh, n, O.nn_mul _ _ hen hhn, ⟨K.zero, O.nn_zero, ?_⟩, m1⟩
    calc K.add (K.add (K.mul e hh) (K.mul e hh)) K.zero = K.mul e (K.mul hh (tw K)) := by unfold tw; lib_ring
      _ = e := by rw [hh2]; lib_ring

include mul_inv h10 in
theorem cauchy_half (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (D hh : R) (hhn : O.nn hh)
    (hh2 : K.mul hh (tw K) = K.one) (s : Nat → R)
    (hb : ∀ j J, Le K O.toCone (K.mul (sub K (s (j + J)) (s j)) (sub K (s (j + J)) (s j))) (K.mul D (pwr K hh j))) :
    Cauchy K (posO K O) s :=
  cauchy_of_sq_rate K O s (fun j => K.mul D (pwr K hh j)) hb (arch_half K O inv mul_inv h10 arch D hh hhn hh2)

end Gen
end RealArch

#print axioms RealArch.pwr_mul2
#print axioms RealArch.pwr_one'
#print axioms RealArch.cast_le_pow
#print axioms RealArch.arch_half
#print axioms RealArch.cauchy_half

#print axioms: 5 theorems, no axioms.

79.263 \(\mathbb Q\) is Archimedean

Every \(x\in \mathbb Q\) satisfies \(x\le |\mathrm {num}\,x|\cdot 1\); hence halving rates over \(\mathbb Q\) give Cauchy sequences. Closes the Archimedean hypothesis for rational schemes.

ns/RatArch.lean

/-
  ns/RatArch.lean — T37 cycle 1870 (goal G860c2): ℚ is Archimedean, so halving rates over ℚ are Cauchy.
    cast_q        n·1 = n/1 in lib/Rat (normal form)
    rat_arch      every x ∈ ℚ satisfies x ≤ |num x|·1   (ratOrd of ns/RatModel)
    rat_cauchy    (s_{j+J} − s_j)² ≤ D·hʲ over ℚ with 2h = 1  ⇒  s is a Cauchy sequence (lib/CauchySeq)
  With RealArch this closes the Archimedean hypothesis of RealBridge for rational schemes.
  Lean 4 core plus ns/RealArch, ns/RatModel, lib/CReal.
-/
import ns.RealArch
import lib.CReal

namespace RatArch
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch LibRat LibInt RatModel

theorem one_pos' : 0 < 1 := Nat.succ_le_succ (Nat.zero_le 0)

theorem cast_q : ∀ k : Nat, castN ratR k = normQ (Int.ofNat k) 1 one_pos'
  | 0 => rfl
  | k + 1 => by
    show addQ oneQ (castN ratR k) = normQ (Int.ofNat (k + 1)) 1 one_pos'
    rw [cast_q k]
    show addQ (normQ 1 1 one_pos') (normQ (Int.ofNat k) 1 one_pos') = normQ (Int.ofNat (k + 1)) 1 one_pos'
    rw [add_nn]
    apply (normQ_eq_iff _ _ _ _ _ _).2
    have e1 : Int.ofNat (k + 1) = Int.ofNat k + 1 := rfl
    have e2 : Int.ofNat (1 * 1) = 1 := rfl
    rw [e1, e2]
    lib_int

theorem nn_of (n : Nat) (num : Int) (den : Nat) (hd : 0 < den) (hn : n = absN num) :
    ∃ k, Int.ofNat n * Int.ofNat den + -num * Int.ofNat 1 = Int.ofNat k := by
  cases den with
  | zero => exact absurd hd (Nat.lt_irrefl 0)
  | succ e =>
    cases num with
    | ofNat a =>
      rw [hn]
      refine ⟨a * e, ?_⟩
      show Int.ofNat a * Int.ofNat (e + 1) + -(Int.ofNat a) * Int.ofNat 1 = Int.ofNat a * Int.ofNat e
      have e1 : Int.ofNat (e + 1) = Int.ofNat e + 1 := rfl
      rw [e1]; lib_int
    | negSucc a =>
      rw [hn]
      refine ⟨(a + 1) * (e + 1) + (a + 1), ?_⟩
      show Int.ofNat (a + 1) * Int.ofNat (e + 1) + -(Int.negSucc a) * Int.ofNat 1
        = Int.ofNat (a + 1) * Int.ofNat (e + 1) + Int.ofNat (a + 1)
      have e1 : Int.negSucc a = -(Int.ofNat (a + 1)) := rfl
      rw [e1]; lib_int

theorem rat_arch (x : Q) : ∃ n, Le ratR ratOrd.toCone x (castN ratR n) := by
  refine ⟨absN x.num, ratR.add (castN ratR (absN x.num)) (ratR.neg x), ?_, by lib_ring⟩
  show NN (addQ (castN ratR (absN x.num)) (negQ x))
  have hx : negQ x = negQ (normQ x.num x.den x.den_pos) := congrArg negQ (self_norm x)
  rw [cast_q, hx, neg_n, add_nn]
  exact nn_normQ _ (nn_of (absN x.num) x.num x.den x.den_pos rfl)

theorem rat_cauchy (D hh : Q) (hhn : ratOrd.nn hh) (hh2 : ratR.mul hh (tw ratR) = ratR.one) (s : Nat → Q)
    (hb : ∀ j J, Le ratR ratOrd.toCone (ratR.mul (sub ratR (s (j + J)) (s j)) (sub ratR (s (j + J)) (s j)))
      (ratR.mul D (EnstGronwall.pwr ratR hh j))) :
    Cauchy ratR (posO ratR ratOrd) s :=
  cauchy_half ratR ratOrd invQ mul_invQ LibRat.one_ne_zero rat_arch D hh hhn hh2 s hb

end RatArch

#print axioms RatArch.cast_q
#print axioms RatArch.nn_of
#print axioms RatArch.rat_arch
#print axioms RatArch.rat_cauchy

#print axioms: 4 theorems, no axioms.

79.264 Limit Fourier coefficients as Cauchy reals

Under the hypotheses of the dyadic coefficient estimate with \(h=1/2\) and \(\mu _r\ge 1\), \(j\mapsto x_j(r)\) is Cauchy componentwise. In any Archimedean ordered field, in particular \(\mathbb Q\).

ns/RealCoef.lean

/-
  ns/RealCoef.lean — T37 cycle 1872 (goal G860c3): every Fourier coefficient component of the step-halving sequence
  is a Cauchy sequence of the shared Cauchy-real library, in any Archimedean ordered field (in particular ℚ).
    comp_x/y/z     (v.x)², (v.y)², (v.z)² ≤ |v|²
    le_mu          μ ≥ 1, d ≥ 0  ⇒  d ≤ μ·d
    coef_real      with the hypotheses of DyadicCoef.coef_cauchy, h·2 = 1 and μ_r ≥ 1, for any component selector f
                   (f(a − b) = f a − f b, (f v)² ≤ |v|²): j ↦ f(x_j(r)) is Cauchy
    coef_real_x/y/z  the three components
  So the limit coefficients of NS_LOG cycle 1175 exist as elements of the Cauchy reals (lib/CReal) — no choice.
  Lean 4 core plus ns/DyadicCoef, ns/RealArch.
-/
import ns.DyadicCoef
import ns.RealArch

namespace RealCoef
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff TimeLipN WCS EnstGronwall
  ForceBilin ForceDiff Dyadic LibCS LibGron LibCauchy RealBridge RealArch DyadicCoef

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem comp_x (v : V3 R) : Le K O.toCone (K.mul v.x v.x) (dot3 K v v) :=
  ⟨K.add (K.mul v.y v.y) (K.mul v.z v.z), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩
theorem comp_y (v : V3 R) : Le K O.toCone (K.mul v.y v.y) (dot3 K v v) :=
  ⟨K.add (K.mul v.x v.x) (K.mul v.z v.z), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩
theorem comp_z (v : V3 R) : Le K O.toCone (K.mul v.z v.z) (dot3 K v v) :=
  ⟨K.add (K.mul v.x v.x) (K.mul v.y v.y), O.nn_add _ _ (O.nn_sq _) (O.nn_sq _), by unfold dot3; lib_ring⟩

theorem le_mu (μ d : R) (hμ : Le K O.toCone K.one μ) (hd : O.nn d) : Le K O.toCone d (K.mul μ d) := by
  cases hμ with
  | intro e he =>
    refine ⟨K.mul e d, O.nn_mul _ _ he.1 hd, ?_⟩
    rw [← he.2]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem coef_real (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (f : V3 R → R) (hf : ∀ a b, f (sub3 K a b) = sub K (f a) (f b)) (hfc : ∀ v, Le K O.toCone (K.mul (f v) (f v)) (dot3 K v v))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => f (x j r)) := by
  refine cauchy_half K O inv mul_inv h10 arch (K.mul (K.mul (K.mul cp C) k0) cr) h hh hh2 (fun j => f (x j r)) ?_
  intro j J
  have c := coef_cauchy K O l μ hμ x C k0 h p q cp cr hC hk0 hh hp hq hcp hcr hpq hgp hgr hd j J r hr
  have s1 := hfc (sub3 K (x (j + J) r) (x j r))
  rw [hf] at s1
  have s2 := le_mu K O (μ r) _ hμ1 (dot3_nn K O.toCone (sub3 K (x (j + J) r) (x j r)))
  refine le_trans' K O.toCone _ _ _ s1 (le_trans' K O.toCone _ _ _ s2 (le_trans' K O.toCone _ _ _ c ⟨K.zero, O.nn_zero, ?_⟩))
  lib_ring

include mul_inv h10 in
theorem coef_real_x (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).x) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.x) (fun _ _ => rfl) (comp_x K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

include mul_inv h10 in
theorem coef_real_y (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).y) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.y) (fun _ _ => rfl) (comp_y K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

include mul_inv h10 in
theorem coef_real_z (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (μ : I → R) (hμ : ∀ r, O.nn (μ r)) (x : Nat → I → V3 R) (C k0 h p q cp cr : R)
    (hC : O.nn C) (hk0 : O.nn k0) (hh : O.nn h) (hp : O.nn p) (hq : O.nn q) (hcp : O.nn cp) (hcr : O.nn cr)
    (hpq : K.mul p q = K.one) (hgp : K.mul cp (K.add K.one (K.neg p)) = K.one)
    (hgr : K.mul cr (K.add K.one (K.neg (K.mul q h))) = K.one)
    (hd : ∀ i, Le K O.toCone (lsum K (fun r => K.mul (μ r) (dot3 K (dF K (x (i + 1)) (x i) r) (dF K (x (i + 1)) (x i) r))) l)
      (K.mul C (K.mul k0 (pwr K h i))))
    (hh2 : K.mul h (tw K) = K.one) (r : I) (hr : r ∈ l) (hμ1 : Le K O.toCone K.one (μ r)) :
    Cauchy K (posO K O) (fun j => (x j r).z) :=
  coef_real K O inv mul_inv h10 arch (fun v => v.z) (fun _ _ => rfl) (comp_z K O) l μ hμ x C k0 h p q cp cr
    hC hk0 hh hp hq hcp hcr hpq hgp hgr hd hh2 r hr hμ1

end Gen
end RealCoef

#print axioms RealCoef.comp_x
#print axioms RealCoef.le_mu
#print axioms RealCoef.coef_real
#print axioms RealCoef.coef_real_x
#print axioms RealCoef.coef_real_y
#print axioms RealCoef.coef_real_z

#print axioms: 6 theorems, no axioms.

79.265 The Galerkin force of Cauchy coefficients

On a fixed finite mode list, componentwise Cauchy coefficients \(a_j(p)\) give componentwise Cauchy forces \(N_r(a_j,a_j)\). In the Cauchy reals this is \(N\) at the limit coefficients.

ns/RealForce.lean

/-
  ns/RealForce.lean — T37 cycle 1872 (goal G860d1): the Galerkin force of Cauchy coefficients is Cauchy. On a fixed
  finite mode list l, N_r(a, a) is a finite sum of products of coefficient components with fixed wave vectors, so
  componentwise Cauchy coefficients a_j(p) (p ∈ l, e.g. from RealCoef) give componentwise Cauchy forces N_r(a_j, a_j):
    c_lsum      finite sums of Cauchy sequences are Cauchy
    c_dot3      j ↦ ⟨u_j, w⟩ is Cauchy for a fixed vector w
    nl_cauchy   every component of j ↦ N_r(a_j, a_j) is Cauchy
  In the Cauchy reals (lib/CReal), whose operations are pointwise, this is N evaluated at the limit coefficients.
  Lean 4 core plus ns/RealIntegral (ofO), ns/GalerkinForce, lib/CauchyMul.
-/
import ns.RealIntegral
import ns.GalerkinForce
import lib.CauchyMul

namespace RealForce
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealIntegral FourierGalerkin
  GalerkinForce

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def CauchyV (u : Nat → V3 R) : Prop :=
  Cauchy K (posO K O) (fun j => (u j).x) ∧ Cauchy K (posO K O) (fun j => (u j).y) ∧ Cauchy K (posO K O) (fun j => (u j).z)

include mul_inv in
theorem c_add {s t : Nat → R} (hs : Cauchy K (posO K O) s) (ht : Cauchy K (posO K O) t) :
    Cauchy K (posO K O) (fun j => K.add (s j) (t j)) :=
  cauchy_add K (posO K O) (ofO K O inv mul_inv) hs ht

include mul_inv h10 in
theorem c_mul {s t : Nat → R} (hs : Cauchy K (posO K O) s) (ht : Cauchy K (posO K O) t) :
    Cauchy K (posO K O) (fun j => K.mul (s j) (t j)) :=
  cauchy_mul K (posO K O) (ofO K O inv mul_inv) h10 hs ht

include mul_inv h10 in
theorem c_cmul (c : R) {s : Nat → R} (hs : Cauchy K (posO K O) s) : Cauchy K (posO K O) (fun j => K.mul c (s j)) :=
  c_mul K O inv mul_inv h10 (cauchy_const K (posO K O) c) hs

include mul_inv in
theorem c_lsum (l : List I) (f : I → Nat → R) :
    (∀ p, p ∈ l → Cauchy K (posO K O) (f p)) → Cauchy K (posO K O) (fun j => lsum K (fun p => f p j) l) := by
  induction l with
  | nil => intro _; exact cauchy_const K (posO K O) K.zero
  | cons x l ih =>
    intro h
    exact c_add K O inv mul_inv (h x (List.Mem.head _)) (ih (fun p hp => h p (List.Mem.tail _ hp)))

include mul_inv h10 in
theorem c_dot3 (u : Nat → V3 R) (w : V3 R) (hu : CauchyV K O u) : Cauchy K (posO K O) (fun j => dot3 K (u j) w) := by
  have m := fun (c : R) (s : Nat → R) (hs : Cauchy K (posO K O) s) =>
    c_mul K O inv mul_inv h10 hs (cauchy_const K (posO K O) c)
  exact c_add K O inv mul_inv (c_add K O inv mul_inv (m w.x _ hu.1) (m w.y _ hu.2.1)) (m w.z _ hu.2.2)

include mul_inv h10 in
theorem c_coef (l : List I) (δ : I → I → I → R) (ι : R) (k : I → V3 R) (a : Nat → I → V3 R)
    (ha : ∀ p, p ∈ l → CauchyV K O (fun j => a j p)) (p q r : I) (hp : p ∈ l) :
    Cauchy K (posO K O) (fun j => coef K δ ι k (a j) p q r) :=
  c_cmul K O inv mul_inv h10 (δ p q r) (c_cmul K O inv mul_inv h10 ι (c_dot3 K O inv mul_inv h10 (fun j => a j p) (k q) (ha p hp)))

include mul_inv h10 in
theorem nl_cauchy (l : List I) (δ : I → I → I → R) (ι : R) (k : I → V3 R) (a : Nat → I → V3 R)
    (ha : ∀ p, p ∈ l → CauchyV K O (fun j => a j p)) (r : I) :
    CauchyV K O (fun j => Nl K l δ ι k (a j) (a j) r) := by
  refine ⟨?_, ?_, ?_⟩
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).1))
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).2.1))
  · exact c_lsum K O inv mul_inv l _ (fun p hp => c_lsum K O inv mul_inv l _ (fun q hq =>
      c_mul K O inv mul_inv h10 (c_coef K O inv mul_inv h10 l δ ι k a ha p q r hp) (ha q hq).2.2))

end Gen
end RealForce

#print axioms RealForce.c_lsum
#print axioms RealForce.c_dot3
#print axioms RealForce.c_coef
#print axioms RealForce.nl_cauchy

#print axioms: 4 theorems, no axioms.

79.266 Riemann sums under dyadic refinement

\(\sum _{i<2n}h'g_i-\sum _{k<n}2h'f_k=\sum _{k<n}h'\big ((g_{2k}-f_k)+(g_{2k+1}-f_k)\big )\) over any commutative ring. The change between levels is a sum of local differences on coarse cells.

ns/RiemannRefine.lean

/-
  ns/RiemannRefine.lean — T37 cycle 1876 (goal G860d2, piece a): Riemann sums under dyadic refinement, as exact
  identities over any commutative ring.
    sumN_pair     Σ_{i<2n} g_i = Σ_{k<n} (g_{2k} + g_{2k+1})
    refine_diff   with the fine step h' and the coarse step h' + h':
                  Σ_{i<2n} h'·g_i − Σ_{k<n} (h' + h')·f_k = Σ_{k<n} h'·((g_{2k} − f_k) + (g_{2k+1} − f_k))
  so the change of the Riemann sum from level j to level j + 1 is a sum of local differences of the integrands on
  each coarse cell — bounded by the coefficient convergence (RealCoef) and the time-Lipschitz bound (TimeLipN).
  Lean 4 core plus ns/AbelCount (sumN_congr).
-/
import ns.AbelCount

namespace RiemannRefine
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R)

theorem sumN_pair (g : Nat → R) : ∀ n, sumN K g (2 * n) = sumN K (fun k => K.add (g (2 * k)) (g (2 * k + 1))) n
  | 0 => rfl
  | n + 1 => by
    show K.add (K.add (sumN K g (2 * n)) (g (2 * n))) (g (2 * n + 1))
      = K.add (sumN K (fun k => K.add (g (2 * k)) (g (2 * k + 1))) n) (K.add (g (2 * n)) (g (2 * n + 1)))
    rw [sumN_pair g n]; lib_ring

theorem sumN_lin (f g : Nat → R) : ∀ n, K.add (sumN K f n) (K.neg (sumN K g n)) = sumN K (fun k => K.add (f k) (K.neg (g k))) n
  | 0 => by show K.add K.zero (K.neg K.zero) = K.zero; lib_ring
  | n + 1 => by
    show K.add (K.add (sumN K f n) (f n)) (K.neg (K.add (sumN K g n) (g n)))
      = K.add (sumN K (fun k => K.add (f k) (K.neg (g k))) n) (K.add (f n) (K.neg (g n)))
    rw [← sumN_lin f g n]; lib_ring

theorem refine_diff (h' : R) (f g : Nat → R) (n : Nat) :
    K.add (sumN K (fun i => K.mul h' (g i)) (2 * n)) (K.neg (sumN K (fun k => K.mul (K.add h' h') (f k)) n))
      = sumN K (fun k => K.mul h' (K.add (K.add (g (2 * k)) (K.neg (f k))) (K.add (g (2 * k + 1)) (K.neg (f k))))) n := by
  rw [sumN_pair K (fun i => K.mul h' (g i)) n, sumN_lin]
  exact AbelCount.sumN_congr K _ _ (fun k => by lib_ring) n

end Gen
end RiemannRefine

#print axioms RiemannRefine.sumN_pair
#print axioms RiemannRefine.sumN_lin
#print axioms RiemannRefine.refine_diff

#print axioms: 3 theorems, no axioms.

79.267 A Riemann-sum refinement bound

\(|g_{2k}-f_k|\le \varepsilon \), \(|g_{2k+1}-f_k|\le \delta \), \(h'\ge 0\) \(\Rightarrow \) \(|\sum _{i<2n}h'g_i-\sum _{k<n}2h'f_k|\le n\,h'(\varepsilon +\delta )\). Over any commutative ring with a positive cone.

ns/RiemannStep.lean

/-
  ns/RiemannStep.lean — T37 cycle 1878 (goal G860d2, piece b): the change of a Riemann sum under dyadic refinement is
  bounded by the local differences, over any commutative ring with a positive cone (lib/CauchySeq's Close):
    close_cmul    Close e x y, c ≥ 0  ⇒  Close (c·e) (c·x) (c·y)
    close_sumN    Close (e_k) (x_k) (y_k) for k < n  ⇒  Close (Σ e_k) (Σ x_k) (Σ y_k)
    riemann_step  h' ≥ 0, |g_{2k} − f_k| ≤ ε, |g_{2k+1} − f_k| ≤ δ for k < n  ⇒
                  |Σ_{i<2n} h'·g_i − Σ_{k<n} 2h'·f_k| ≤ Σ_{k<n} h'·(ε + δ)
  With ε = the level-to-level coefficient bound at a fixed grid time and δ = ε + one fine step of TimeLipN, the
  Riemann sums S_j form a Cauchy sequence whose limit is the time integral on dyadic grids.
  Lean 4 core plus ns/RiemannRefine, lib/CauchyMul.
-/
import ns.RiemannRefine
import lib.CauchyMul

namespace RiemannStep
open DetChain NSChain LibRing LibCS LibGron LibCauchy RiemannRefine

section Gen
variable {R : Type} (K : CRing R) (P : Pos K)

theorem close_cmul {e x y c : R} (hc : P.nn c) (h : Close K P e x y) : Close K P (K.mul c e) (K.mul c x) (K.mul c y) := by
  constructor
  · rw [show sub K (K.mul c x) (K.mul c y) = K.mul c (sub K x y) by lib_ring]; exact le_mul_left K P hc h.1
  · rw [show sub K (K.mul c y) (K.mul c x) = K.mul c (sub K y x) by lib_ring]; exact le_mul_left K P hc h.2

theorem close_sumN (e x y : Nat → R) : ∀ n, (∀ k, k < n → Close K P (e k) (x k) (y k)) →
    Close K P (sumN K e n) (sumN K x n) (sumN K y n)
  | 0, _ => close_refl K P P.nn_zero K.zero
  | n + 1, h => close_add K P (close_sumN e x y n (fun k hk => h k (Nat.lt_succ_of_lt hk))) (h n (Nat.lt_succ_self n))

theorem sumN_zero : ∀ n, K.zero = sumN K (fun _ => K.zero) n
  | 0 => rfl
  | n + 1 => by show K.zero = K.add (sumN K (fun _ => K.zero) n) K.zero; rw [← sumN_zero n]; lib_ring

theorem riemann_step (h' ε δ : R) (hh : P.nn h') (f g : Nat → R) (n : Nat)
    (h1 : ∀ k, k < n → Close K P ε (g (2 * k)) (f k)) (h2 : ∀ k, k < n → Close K P δ (g (2 * k + 1)) (f k)) :
    Close K P (sumN K (fun _ => K.mul h' (K.add ε δ)) n)
      (sumN K (fun i => K.mul h' (g i)) (2 * n)) (sumN K (fun k => K.mul (K.add h' h') (f k)) n) := by
  apply close_of_sub K P
  have e := refine_diff K h' f g n
  rw [show sub K (sumN K (fun i => K.mul h' (g i)) (2 * n)) (sumN K (fun k => K.mul (K.add h' h') (f k)) n)
      = K.add (sumN K (fun i => K.mul h' (g i)) (2 * n)) (K.neg (sumN K (fun k => K.mul (K.add h' h') (f k)) n)) from rfl, e]
  rw [sumN_zero K n]
  refine close_sumN K P _ _ _ n (fun k hk => ?_)
  have a := close_sub K P (h1 k hk)
  have b := close_sub K P (h2 k hk)
  have c := close_cmul K P hh (close_add K P a b)
  rw [show K.mul h' (K.add K.zero K.zero) = K.zero by lib_ring] at c
  exact c

end Gen
end RiemannStep

#print axioms RiemannStep.close_cmul
#print axioms RiemannStep.close_sumN
#print axioms RiemannStep.riemann_step

#print axioms: 3 theorems, no axioms.

79.268 Linear geometric rates are Cauchy

\(|s_{i+1}-s_i|\le D\,h^i\) with \(2h=1\) in an Archimedean ordered field \(\Rightarrow \) \(|s_{j+J}-s_j|\le 2D\,h^j\) and \(s\) is Cauchy. Uses \(\sum _{i<J}h^i+2h^J=2\).

ns/RealLinear.lean

/-
  ns/RealLinear.lean — T37 cycle 1880 (goal G860d2c, piece): a linear geometric rate gives a Cauchy sequence in an
  Archimedean ordered field (lib/CauchySeq's Close, the cone of RealBridge):
    geo_id      2h = 1  ⇒  Σ_{i<J} hⁱ + 2hᴶ = 2
    close_tele  |s_{i+1} − s_i| ≤ D·hⁱ for all i  ⇒  |s_{j+J} − s_j| ≤ Σ_{i<J} D·h^{j+i}
    close_geo   …  ≤ 2D·hʲ
    cauchy_lin  |s_{i+1} − s_i| ≤ D·hⁱ (2h = 1, Archimedean)  ⇒  s is Cauchy
  For the Riemann sums of the step-halving scheme (RiemannStep: |S_{j+1} − S_j| ≤ T(ε_j + δ_j) = O(h_j)).
  Lean 4 core plus ns/RealArch, ns/RiemannStep.
-/
import ns.RealArch
import ns.RiemannStep

namespace RealLinear
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem geo_id (h : R) (hh2 : K.mul h (tw K) = K.one) : ∀ J,
    K.add (sumN K (fun i => pwr K h i) J) (K.mul (tw K) (pwr K h J)) = tw K
  | 0 => by show K.add K.zero (K.mul (tw K) K.one) = tw K; lib_ring
  | J + 1 => by
    show K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (tw K) (K.mul h (pwr K h J))) = tw K
    have e : K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (tw K) (K.mul h (pwr K h J)))
        = K.add (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J)) (K.mul (K.mul h (tw K)) (pwr K h J)) := by lib_ring
    rw [e, hh2, ← geo_id h hh2 J]; unfold tw; lib_ring

theorem close_tele (s : Nat → R) (D h : R) (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i))
    (j : Nat) : ∀ J, Close K (posO K O) (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (s (j + J)) (s j)
  | 0 => close_refl K (posO K O) O.nn_zero (s j)
  | J + 1 => by
    have a := close_tele s D h hs j J
    have b := hs (j + J)
    have c' := close_tri K (posO K O) b a
    show Close K (posO K O) (K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))) (s (j + J + 1)) (s j)
    rw [show K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))
        = K.add (K.mul D (pwr K h (j + J))) (sumN K (fun i => K.mul D (pwr K h (j + i))) J) by lib_ring]
    exact c'

theorem pwr_add' (h : R) (j : Nat) : ∀ i, pwr K h (j + i) = K.mul (pwr K h j) (pwr K h i)
  | 0 => by show pwr K h j = K.mul (pwr K h j) K.one; lib_ring
  | i + 1 => by
    show K.mul h (pwr K h (j + i)) = K.mul (pwr K h j) (K.mul h (pwr K h i))
    rw [pwr_add' h j i]; lib_ring

theorem sum_scale (D h : R) (j : Nat) : ∀ J,
    sumN K (fun i => K.mul D (pwr K h (j + i))) J = K.mul (K.mul D (pwr K h j)) (sumN K (fun i => pwr K h i) J)
  | 0 => by show K.zero = K.mul (K.mul D (pwr K h j)) K.zero; lib_ring
  | J + 1 => by
    show K.add (sumN K (fun i => K.mul D (pwr K h (j + i))) J) (K.mul D (pwr K h (j + J)))
      = K.mul (K.mul D (pwr K h j)) (K.add (sumN K (fun i => pwr K h i) J) (pwr K h J))
    rw [sum_scale D h j J, pwr_add' K h j J]; lib_ring

theorem sum_nn (h : R) (hh : O.nn h) : ∀ J, O.nn (sumN K (fun i => pwr K h i) J)
  | 0 => O.nn_zero
  | J + 1 => O.nn_add _ _ (sum_nn h hh J) (pwr_nn K O h hh J)

theorem close_geo (s : Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i)) (j J : Nat) :
    Close K (posO K O) (K.mul (tw K) (K.mul D (pwr K h j))) (s (j + J)) (s j) := by
  have a := close_tele K O s D h hs j J
  rw [sum_scale K D h j J] at a
  refine close_mono K (posO K O) a (le_of_Le K O ⟨K.mul (K.mul D (pwr K h j)) (K.mul (tw K) (pwr K h J)),
    O.nn_mul _ _ (O.nn_mul _ _ hD (pwr_nn K O h hh j)) (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone)
      (RatModel.nn_one' K O.toCone)) (pwr_nn K O h hh J)), ?_⟩)
  calc K.add (K.mul (K.mul D (pwr K h j)) (sumN K (fun i => pwr K h i) J)) (K.mul (K.mul D (pwr K h j)) (K.mul (tw K) (pwr K h J)))
      = K.mul (K.mul D (pwr K h j)) (K.add (sumN K (fun i => pwr K h i) J) (K.mul (tw K) (pwr K h J))) := by lib_ring
    _ = K.mul (tw K) (K.mul D (pwr K h j)) := by rw [geo_id K h hh2 J]; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv in
theorem cauchy_lin (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (s : Nat → R) (D h : R) (hD : O.nn D)
    (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ i, Close K (posO K O) (K.mul D (pwr K h i)) (s (i + 1)) (s i)) : Cauchy K (posO K O) s := by
  intro e he
  have hen : O.nn e := he.1
  have hy := mul_inv e he.2
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  cases arch (K.mul (K.mul (K.mul (tw K) (tw K)) D) (inv e)) with
  | intro n hn =>
    have h1 := le_trans' K O.toCone _ _ _ hn (cast_le_pow K O n).2
    have m1 := le_mul_nn K O.toCone (K.mul e (pwr K h n)) _ _ (O.nn_mul _ _ hen (pwr_nn K O h hh n)) h1
    have e1 : K.mul (K.mul e (pwr K h n)) (K.mul (K.mul (K.mul (tw K) (tw K)) D) (inv e))
        = K.mul (K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)) (K.mul e (inv e)) := by lib_ring
    have e2 : K.mul (K.mul e (pwr K h n)) (pwr K (tw K) n) = K.mul e (pwr K (K.mul h (tw K)) n) := by
      rw [← pwr_mul2 K h (tw K) n]; lib_ring
    rw [e1, hy, e2, hh2, pwr_one'] at m1
    rw [show K.mul (K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)) K.one
        = K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n) by lib_ring, show K.mul e K.one = e by lib_ring] at m1
    refine ⟨n, fun a b ha hb => ?_⟩
    cases le_dest' ha with
    | intro k hk =>
      cases le_dest' hb with
      | intro k' hk' =>
        have c1 := close_geo K O s D h hD hh hh2 hs n k
        have c2 := close_geo K O s D h hD hh hh2 hs n k'
        rw [hk] at c1
        rw [hk'] at c2
        have c := close_tri K (posO K O) c1 (close_symm K (posO K O) c2)
        refine close_mono K (posO K O) c (le_of_Le K O (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m1))
        show K.add (K.add (K.mul (tw K) (K.mul D (pwr K h n))) (K.mul (tw K) (K.mul D (pwr K h n)))) K.zero
          = K.mul (K.mul (K.mul (tw K) (tw K)) D) (pwr K h n)
        unfold tw; lib_ring

end Gen
end RealLinear

#print axioms RealLinear.geo_id
#print axioms RealLinear.close_tele
#print axioms RealLinear.close_geo
#print axioms RealLinear.cauchy_lin

#print axioms: 4 theorems, no axioms.

79.269 Riemann sums on dyadic grids converge

Local bounds \(\varepsilon _j,\delta _j\) on each coarse cell with budget \(\sum _k h'_j(\varepsilon _j+\delta _j)\le D\,h^j\) make \(j\mapsto S_j\) Cauchy. In any Archimedean ordered field.

ns/RiemannCauchy.lean

/-
  ns/RiemannCauchy.lean — T37 cycle 1888 (goal G860d2c, abstract part): Riemann sums on dyadic grids converge.
  Level j has n_j cells of width 2h'_j (h'_j the step of level j + 1) and integrand f_j; level j + 1 has 2n_j cells of
  width h'_j and integrand f_{j+1} = g_j. If on each coarse cell |g_j(2k) − f_j(k)| ≤ ε_j and |g_j(2k+1) − f_j(k)| ≤ δ_j
  and the budget Σ_{k<n_j} h'_j(ε_j + δ_j) is at most D·hʲ (2h = 1), then j ↦ S_j is Cauchy (Archimedean field):
    riemann_cauchy   (RiemannStep.riemann_step + RealLinear.cauchy_lin)
  The NS instance takes ε_j from the step-size estimate (StepUniformS) and δ_j from one fine step (TimeLip).
  Lean 4 core plus ns/RealLinear.
-/
import ns.RealLinear

namespace RiemannCauchy
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_cauchy (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (S : Nat → R) (n : Nat → Nat)
    (hp ε δ : Nat → R) (f : Nat → Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j))
    (hS0 : ∀ j, S j = sumN K (fun k => K.mul (K.add (hp j) (hp j)) (f j k)) (n j))
    (hS1 : ∀ j, S (j + 1) = sumN K (fun i => K.mul (hp j) (f (j + 1) i)) (2 * n j))
    (h1 : ∀ j k, k < n j → Close K (posO K O) (ε j) (f (j + 1) (2 * k)) (f j k))
    (h2 : ∀ j k, k < n j → Close K (posO K O) (δ j) (f (j + 1) (2 * k + 1)) (f j k))
    (hb : ∀ j, Le K O.toCone (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j)) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) S := by
  refine cauchy_lin K O inv mul_inv arch S D h hD hh hh2 (fun j => ?_)
  have c := riemann_step K (posO K O) (hp j) (ε j) (δ j) (hpn j) (f j) (f (j + 1)) (n j) (h1 j) (h2 j)
  rw [← hS0 j, ← hS1 j] at c
  exact close_mono K (posO K O) c (le_of_Le K O (hb j))

end Gen
end RiemannCauchy

#print axioms RiemannCauchy.riemann_cauchy

#print axioms: 1 theorem, no axioms.

79.270 Squared linear rates are Cauchy

\((s_{j+1}-s_j)^2\le D\,h^j\) with \(2h=1\) in an Archimedean ordered field \(\Rightarrow \) \(s\) is Cauchy. Levels are grouped in pairs, so no square roots are needed.

ns/RealSqrtRate.lean

/-
  ns/RealSqrtRate.lean — T37 cycle 1890 (goal G860d2c, rate √(1/2)): a squared linear rate still gives a Cauchy
  sequence. The step-size estimate (StepUniformS) bounds squared distances by O(k_j) = O(2^{−j}), so coefficient
  differences per level are O(2^{−j/2}) — irrational ratio. Grouping levels in pairs avoids square roots:
    sq_bound     D·h^{2i} ≤ ((D + 1)·hⁱ)²   (D ≥ 0)
    pwr_le_one   0 ≤ h, 2h = 1  ⇒  hᵏ ≤ 1;   pwr_anti  h^{N+k} ≤ hᴺ
    parity       every m is i + i or i + i + 1;  half_ge   N + N ≤ m = i + i (+1)  ⇒  N ≤ i
    cauchy_sq    (s_{j+1} − s_j)² ≤ D·hʲ for all j (2h = 1, Archimedean)  ⇒  s is Cauchy
  (even subsequence t_i = s_{2i} has the linear rate 2(D + 1)hⁱ → RealLinear.close_geo; odd terms are within
  (D + 1)hⁱ of the even ones).
  Lean 4 core plus ns/RealLinear.
-/
import ns.RealLinear

namespace RealSqrtRate
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear

theorem parity : ∀ m : Nat, ∃ i, m = i + i ∨ m = i + i + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | m + 1 => by
    cases parity m with
    | intro i h =>
      cases h with
      | inl h => exact ⟨i, Or.inr (by rw [h])⟩
      | inr h => exact ⟨i + 1, Or.inl (by rw [h]; exact congrArg Nat.succ (Nat.succ_add i i).symm)⟩

theorem half_ge (N m i : Nat) (hm : N + N ≤ m) (h : m = i + i ∨ m = i + i + 1) : N ≤ i := by
  cases Nat.lt_or_ge i N with
  | inr g => exact g
  | inl l =>
    exfalso
    have a : (i + 1) + (i + 1) ≤ N + N := Nat.add_le_add l l
    have e : (i + 1) + (i + 1) = i + i + 1 + 1 := congrArg Nat.succ (Nat.succ_add i i)
    have hm' : m ≤ i + i + 1 := by
      cases h with
      | inl h => rw [h]; exact Nat.le_succ _
      | inr h => rw [h]; exact Nat.le_refl _
    rw [e] at a
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_lt hm' (Nat.lt_succ_self _)) (Nat.le_trans a hm))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_bound (D x : R) (hD : O.nn D) :
    Le K O.toCone (K.mul D (K.mul x x)) (K.mul (K.mul (K.add D K.one) x) (K.mul (K.add D K.one) x)) :=
  ⟨K.mul (K.add (K.mul D D) (K.add D K.one)) (K.mul x x),
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_sq D) (O.nn_add _ _ hD (RatModel.nn_one' K O.toCone))) (O.nn_sq x), by lib_ring⟩

theorem h_le_one (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : Le K O.toCone h K.one :=
  ⟨h, hh, by rw [← hh2]; unfold tw; lib_ring⟩

theorem pwr_le_one (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : ∀ k, Le K O.toCone (pwr K h k) K.one
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have a := le_mul_nn K O.toCone h _ _ hh (pwr_le_one h hh hh2 k)
    rw [show K.mul h K.one = h by lib_ring] at a
    exact le_trans' K O.toCone _ _ _ a (h_le_one K O h hh hh2)

theorem pwr_anti (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) (N k : Nat) :
    Le K O.toCone (pwr K h (N + k)) (pwr K h N) := by
  rw [pwr_add' K h N k]
  have a := le_mul_nn K O.toCone (pwr K h N) _ _ (pwr_nn K O h hh N) (pwr_le_one K O h hh hh2 k)
  rw [show K.mul (pwr K h N) K.one = pwr K h N by lib_ring] at a
  exact a

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem cauchy_sq (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (s : Nat → R) (D h : R) (hD : O.nn D)
    (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hs : ∀ j, Le K O.toCone (K.mul (sub K (s (j + 1)) (s j)) (sub K (s (j + 1)) (s j))) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) s := by
  have hq : O.nn (K.add D K.one) := O.nn_add _ _ hD (RatModel.nn_one' K O.toCone)
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- odd step: |s_{2i+1} − s_{2i}| ≤ q hⁱ;  even step: |s_{2i+2} − s_{2i+1}| ≤ q hⁱ
  have c1 : ∀ i, Close K (posO K O) (K.mul (K.add D K.one) (pwr K h i)) (s (i + i + 1)) (s (i + i)) := by
    intro i
    refine close_of_sq K O _ _ _ (O.nn_mul _ _ hq (pwr_nn K O h hh i)) (le_trans' K O.toCone _ _ _ (hs (i + i)) ?_)
    rw [pwr_add' K h i i]; exact sq_bound K O D (pwr K h i) hD
  have c2 : ∀ i, Close K (posO K O) (K.mul (K.add D K.one) (pwr K h i)) (s (i + i + 1 + 1)) (s (i + i + 1)) := by
    intro i
    refine close_of_sq K O _ _ _ (O.nn_mul _ _ hq (pwr_nn K O h hh i)) (le_trans' K O.toCone _ _ _ (hs (i + i + 1)) ?_)
    refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone D _ _ hD (pwr_anti K O h hh hh2 (i + i) 1)) ?_
    rw [pwr_add' K h i i]; exact sq_bound K O D (pwr K h i) hD
  -- the even subsequence t_i = s_{2i} has the linear rate (tw·q)·hⁱ
  have ct : ∀ i, Close K (posO K O) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h i)) (s ((i + 1) + (i + 1))) (s (i + i)) := by
    intro i
    have c := close_tri K (posO K O) (c2 i) (c1 i)
    rw [show (i + 1) + (i + 1) = i + i + 1 + 1 from congrArg Nat.succ (Nat.succ_add i i)]
    rw [show K.add (K.mul (K.add D K.one) (pwr K h i)) (K.mul (K.add D K.one) (pwr K h i))
        = K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h i) by unfold tw; lib_ring] at c
    exact c
  have geo := fun N J => close_geo K O (fun i => s (i + i)) (K.mul (tw K) (K.add D K.one)) h
    (O.nn_mul _ _ ntw hq) hh hh2 ct N J
  -- choose N with Kc·hᴺ ≤ e, Kc = (q + tw·(tw·q)) + (tw·(tw·q) + q)
  intro e he
  have hen : O.nn e := he.1
  have hy := mul_inv e he.2
  cases arch (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (inv e)) with
  | intro N hN =>
    have h1 := le_trans' K O.toCone _ _ _ hN (cast_le_pow K O N).2
    have m1 := le_mul_nn K O.toCone (K.mul e (pwr K h N)) _ _ (O.nn_mul _ _ hen (pwr_nn K O h hh N)) h1
    have e2 : K.mul (K.mul e (pwr K h N)) (pwr K (tw K) N) = K.mul e (pwr K (K.mul h (tw K)) N) := by
      rw [← pwr_mul2 K h (tw K) N]; lib_ring
    rw [e2, hh2, pwr_one', show K.mul e K.one = e by lib_ring,
      show K.mul (K.mul e (pwr K h N)) (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (inv e))
        = K.mul (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N)) (K.mul e (inv e)) by lib_ring,
      hy, show K.mul (K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N)) K.one
        = K.mul (K.mul (tw K) (K.add (K.add D K.one) (K.mul (tw K) (K.mul (tw K) (K.add D K.one))))) (pwr K h N) by lib_ring] at m1
    -- every index m ≥ N + N is within q·hᴺ + tw·(tw·q)·hᴺ of s_{2N}
    have near : ∀ m, N + N ≤ m → Close K (posO K O)
        (K.add (K.mul (K.add D K.one) (pwr K h N)) (K.mul (tw K) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h N)))) (s m) (s (N + N)) := by
      intro m hm
      cases parity m with
      | intro i hi =>
        have hNi := half_ge N m i hm hi
        cases le_dest' hNi with
        | intro J hJ =>
          have g := geo N J
          rw [hJ] at g
          have g' : Close K (posO K O) (K.mul (tw K) (K.mul (K.mul (tw K) (K.add D K.one)) (pwr K h N))) (s (i + i)) (s (N + N)) := g
          have qa : Le K O.toCone (K.mul (K.add D K.one) (pwr K h i)) (K.mul (K.add D K.one) (pwr K h N)) := by
            rw [← hJ]; exact le_mul_nn K O.toCone _ _ _ hq (pwr_anti K O h hh hh2 N J)
          cases hi with
          | inl e0 =>
            rw [e0]
            exact close_mono K (posO K O) g' (le_of_Le K O ⟨K.mul (K.add D K.one) (pwr K h N),
              O.nn_mul _ _ hq (pwr_nn K O h hh N), by lib_ring⟩)
          | inr e1 =>
            rw [e1]
            have c := close_tri K (posO K O) (close_mono K (posO K O) (c1 i) (le_of_Le K O qa)) g'
            exact c
    refine ⟨N + N, fun m n hm hn => ?_⟩
    have c := close_tri K (posO K O) (near m hm) (close_symm K (posO K O) (near n hn))
    refine close_mono K (posO K O) c (le_of_Le K O (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ m1))
    unfold tw; lib_ring

end Gen
end RealSqrtRate

#print axioms RealSqrtRate.parity
#print axioms RealSqrtRate.half_ge
#print axioms RealSqrtRate.sq_bound
#print axioms RealSqrtRate.pwr_anti
#print axioms RealSqrtRate.cauchy_sq

#print axioms: 5 theorems, no axioms.

79.271 Riemann sums converge under a squared budget

Local cell bounds with \(B_j=\sum _k h'_j(\varepsilon _j+\delta _j)\ge 0\) and \(B_j^2\le D\,h^j\) make \(j\mapsto S_j\) Cauchy. Matches the step-size estimate, whose distances are squared.

ns/RiemannCauchySq.lean

/-
  ns/RiemannCauchySq.lean — T37 cycle 1894 (goal G860d2c): Riemann sums converge under a squared budget, matching
  the step-size estimate (squared distances O(2^{−j})):
    sq_of_close        |x − y| ≤ b  ⇒  (x − y)² ≤ b²
    riemann_cauchy_sq  local cell bounds ε_j, δ_j as in RiemannCauchy, with B_j = Σ_{k<n_j} h'_j(ε_j + δ_j) ≥ 0
                       and B_j² ≤ D·hʲ (2h = 1)  ⇒  j ↦ S_j is Cauchy (RiemannStep + RealSqrtRate.cauchy_sq)
  Lean 4 core plus ns/RealSqrtRate, ns/RiemannCauchy.
-/
import ns.RealSqrtRate
import ns.RiemannCauchy

namespace RiemannCauchySq
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_of_close (b x y : R) (c : Close K (posO K O) b x y) :
    Le K O.toCone (K.mul (sub K x y) (sub K x y)) (K.mul b b) := by
  have h1 : O.nn (sub K b (sub K x y)) := c.1
  have h2 : O.nn (sub K b (sub K y x)) := c.2
  refine ⟨K.mul (sub K b (sub K x y)) (sub K b (sub K y x)), O.nn_mul _ _ h1 h2, ?_⟩
  unfold sub; lib_ring

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_cauchy_sq (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (S : Nat → R) (n : Nat → Nat)
    (hp ε δ : Nat → R) (f : Nat → Nat → R) (D h : R) (hD : O.nn D) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j))
    (hS0 : ∀ j, S j = sumN K (fun k => K.mul (K.add (hp j) (hp j)) (f j k)) (n j))
    (hS1 : ∀ j, S (j + 1) = sumN K (fun i => K.mul (hp j) (f (j + 1) i)) (2 * n j))
    (h1 : ∀ j k, k < n j → Close K (posO K O) (ε j) (f (j + 1) (2 * k)) (f j k))
    (h2 : ∀ j k, k < n j → Close K (posO K O) (δ j) (f (j + 1) (2 * k + 1)) (f j k))
    (hb : ∀ j, Le K O.toCone (K.mul (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j))
      (sumN K (fun _ => K.mul (hp j) (K.add (ε j) (δ j))) (n j))) (K.mul D (pwr K h j))) :
    Cauchy K (posO K O) S := by
  refine cauchy_sq K O inv mul_inv arch S D h hD hh hh2 (fun j => ?_)
  have c := riemann_step K (posO K O) (hp j) (ε j) (δ j) (hpn j) (f j) (f (j + 1)) (n j) (h1 j) (h2 j)
  rw [← hS0 j, ← hS1 j] at c
  exact le_trans' K O.toCone _ _ _ (sq_of_close K O _ _ _ c) (hb j)

end Gen
end RiemannCauchySq

#print axioms RiemannCauchySq.sq_of_close
#print axioms RiemannCauchySq.riemann_cauchy_sq

#print axioms: 2 theorems, no axioms.

79.272 Riemann sums of force minus dissipation converge

If \(F\) and \(A\) have squared level-to-level differences at most \(P\,h^j\) and \(Q\,h^j\) on every cell, the sums \(\sum _k 2h'_j\,(F_j(k)-c\,A_j(k+1))\) are Cauchy. The bounds \(\varepsilon _j=(M+1)h^{\lfloor j/2\rfloor }\) are rational, with no square roots.

ns/RiemannInst.lean

/-
  ns/RiemannInst.lean — T37 cycle 1896 (goal G860e, piece a): Riemann sums of an integrand f_j(k) = F_j(k) − c·A_j(k+1)
  (force minus implicit dissipation) converge when F and A have squared level-to-level differences O(hʲ) (2h = 1):
    sq_diff2      (x − c y)² ≤ 2x² + 2c²y²
    g             g 0 = g 1 = 1, g (j+2) = h·g j   (g j = h^⌊j/2⌋, no division)
    g_lower       hʲ ≤ (g j)²;   g_upper  (g j)² ≤ 2hʲ
    riemann_inst  hypotheses: n_j cells of width 2h'_j with n_j·h'_j = T', and for k < n_j
                    (F_{j+1}(2k) − F_j(k))², (F_{j+1}(2k+1) − F_j(k))² ≤ P·hʲ,
                    (A_{j+1}(2k+1) − A_j(k+1))², (A_{j+1}(2k+2) − A_j(k+1))² ≤ Q·hʲ
                  ⇒  S_j = Σ_{k<n_j} 2h'_j f_j(k) is Cauchy   (ε_j = δ_j = (M + 1)·g j, M = 2P + 2c²Q;
                  RiemannCauchySq with D = 8T'²(M + 1)²).
  Lean 4 core plus ns/RiemannCauchySq.
-/
import ns.RiemannCauchySq

namespace RiemannInst
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate RiemannCauchySq

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem sq_diff2 (x y c : R) :
    Le K O.toCone (K.mul (sub K x (K.mul c y)) (sub K x (K.mul c y)))
      (K.add (K.mul (tw K) (K.mul x x)) (K.mul (tw K) (K.mul (K.mul c c) (K.mul y y)))) :=
  ⟨K.mul (K.add x (K.mul c y)) (K.add x (K.mul c y)), O.nn_sq _, by unfold sub tw; lib_ring⟩

def g (h : R) : Nat → R
  | 0 => K.one
  | 1 => K.one
  | j + 2 => K.mul h (g h j)

theorem g_nn (h : R) (hh : O.nn h) : ∀ j, O.nn (g K h j)
  | 0 => RatModel.nn_one' K O.toCone
  | 1 => RatModel.nn_one' K O.toCone
  | j + 2 => O.nn_mul _ _ hh (g_nn h hh j)

theorem g_lower (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) : ∀ j, Le K O.toCone (pwr K h j) (K.mul (g K h j) (g K h j))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.one K.zero = K.mul K.one K.one; lib_ring⟩
  | 1 => by
    have a := h_le_one K O h hh hh2
    show Le K O.toCone (K.mul h K.one) (K.mul K.one K.one)
    rw [show K.mul h K.one = h by lib_ring, show K.mul K.one K.one = K.one by lib_ring]; exact a
  | j + 2 => by
    have a := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (g_lower h hh hh2 j)
    show Le K O.toCone (K.mul h (K.mul h (pwr K h j))) (K.mul (K.mul h (g K h j)) (K.mul h (g K h j)))
    rw [show K.mul h (K.mul h (pwr K h j)) = K.mul (K.mul h h) (pwr K h j) by lib_ring,
      show K.mul (K.mul h (g K h j)) (K.mul h (g K h j)) = K.mul (K.mul h h) (K.mul (g K h j) (g K h j)) by lib_ring]
    exact a

theorem g_upper (h : R) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one) :
    ∀ j, Le K O.toCone (K.mul (g K h j) (g K h j)) (K.mul (tw K) (pwr K h j))
  | 0 => ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add (K.mul K.one K.one) K.one = K.mul (tw K) K.one; unfold tw; lib_ring⟩
  | 1 => ⟨K.zero, O.nn_zero, by
      show K.add (K.mul K.one K.one) K.zero = K.mul (tw K) (K.mul h K.one)
      rw [show K.mul (tw K) (K.mul h K.one) = K.mul h (tw K) by lib_ring, hh2]; lib_ring⟩
  | j + 2 => by
    have a := le_mul_nn K O.toCone (K.mul h h) _ _ (O.nn_sq h) (g_upper h hh hh2 j)
    show Le K O.toCone (K.mul (K.mul h (g K h j)) (K.mul h (g K h j))) (K.mul (tw K) (K.mul h (K.mul h (pwr K h j))))
    rw [show K.mul (K.mul h (g K h j)) (K.mul h (g K h j)) = K.mul (K.mul h h) (K.mul (g K h j) (g K h j)) by lib_ring,
      show K.mul (tw K) (K.mul h (K.mul h (pwr K h j))) = K.mul (K.mul h h) (K.mul (tw K) (pwr K h j)) by lib_ring]
    exact a

theorem sumN_const (x : R) : ∀ n, sumN K (fun _ => x) n = K.mul (castN K n) x
  | 0 => by show K.zero = K.mul K.zero x; lib_ring
  | n + 1 => by
    show K.add (sumN K (fun _ => x) n) x = K.mul (K.add K.one (castN K n)) x
    rw [sumN_const x n]; lib_ring

/-- a local cell bound from squared bounds on F and A -/
theorem cell_close (x x' y y' c P Q e : R) (hP : Le K O.toCone (K.mul (sub K x' x) (sub K x' x)) P)
    (hQ : Le K O.toCone (K.mul (sub K y' y) (sub K y' y)) Q) (he : O.nn e)
    (hM : Le K O.toCone (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (K.mul e e)) :
    Close K (posO K O) e (sub K x' (K.mul c y')) (sub K x (K.mul c y)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have s := sq_diff2 K O (sub K x' x) (sub K y' y) c
  rw [show sub K (sub K x' x) (K.mul c (sub K y' y)) = sub K (sub K x' (K.mul c y')) (sub K x (K.mul c y)) by unfold sub; lib_ring] at s
  refine close_of_sq K O _ _ _ he (le_trans' K O.toCone _ _ _ s (le_trans' K O.toCone _ _ _ ?_ hM))
  exact le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ ntw hP)
    (le_mul_nn K O.toCone _ _ _ ntw (le_mul_nn K O.toCone _ _ _ (O.nn_sq c) hQ))

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_inst (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (F A : Nat → Nat → R) (c P Q h T' : R)
    (n : Nat → Nat) (hp : Nat → R) (hP : O.nn P) (hQ : O.nn Q) (hh : O.nn h) (hh2 : K.mul h (tw K) = K.one)
    (hpn : ∀ j, O.nn (hp j)) (hnT : ∀ j, K.mul (castN K (n j)) (hp j) = T')
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hp2 : ∀ j, K.add (hp (j + 1)) (hp (j + 1)) = hp j)
    (hF0 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k)) (F j k)) (sub K (F (j + 1) (2 * k)) (F j k))) (K.mul P (pwr K h j)))
    (hF1 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k + 1)) (F j k)) (sub K (F (j + 1) (2 * k + 1)) (F j k))) (K.mul P (pwr K h j)))
    (hA0 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1)) (A j (k + 1)))) (K.mul Q (pwr K h j)))
    (hA1 : ∀ j k, k < n j → Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1)))) (K.mul Q (pwr K h j))) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (hp j) (hp j)) (sub K (F j k) (K.mul c (A j (k + 1))))) (n j)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  -- M = 2P + 2c²Q, e_j = (M + 1)·g j
  have hM : O.nn (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) :=
    O.nn_add _ _ (O.nn_mul _ _ ntw hP) (O.nn_mul _ _ ntw (O.nn_mul _ _ (O.nn_sq c) hQ))
  have hM1 : O.nn (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) :=
    O.nn_add _ _ hM (RatModel.nn_one' K O.toCone)
  have he : ∀ j, O.nn (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j)) :=
    fun j => O.nn_mul _ _ hM1 (g_nn K O h hh j)
  -- the local bound: (2P + 2c²Q)·hʲ ≤ ((M + 1)·g j)²
  have hloc : ∀ j, Le K O.toCone
      (K.add (K.mul (tw K) (K.mul P (pwr K h j))) (K.mul (tw K) (K.mul (K.mul c c) (K.mul Q (pwr K h j)))))
      (K.mul (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))) := by
    intro j
    have a := le_mul_nn K O.toCone (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) _ _ hM (g_lower K O h hh hh2 j)
    have b := sq_bound K O (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (g K h j) hM
    rw [show K.add (K.mul (tw K) (K.mul P (pwr K h j))) (K.mul (tw K) (K.mul (K.mul c c) (K.mul Q (pwr K h j))))
        = K.mul (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) (pwr K h j) by lib_ring]
    exact le_trans' K O.toCone _ _ _ a b
  refine riemann_cauchy_sq K O inv mul_inv arch _ n hp
    (fun j => K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
    (fun j => K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
    (fun j k => sub K (F j k) (K.mul c (A j (k + 1))))
    (K.mul (K.mul (K.mul (tw K) (tw K)) (K.mul (tw K) (K.mul T' T')))
      (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)
        (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)))
    h (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ ntw ntw) (O.nn_mul _ _ ntw (O.nn_sq T'))) (O.nn_sq _)) hh hh2 hpn
    (fun j => rfl) (fun j => by show _ = _; rw [hn2 j, hp2 j])
    (fun j k hk => cell_close K O _ _ _ _ c _ _ _ (hF0 j k hk) (hA0 j k hk) (he j) (hloc j))
    (fun j k hk => cell_close K O _ _ _ _ c _ _ _ (hF1 j k hk) (hA1 j k hk) (he j) (hloc j)) (fun j => ?_)
  rw [sumN_const K _ (n j)]
  rw [show K.mul (castN K (n j)) (K.mul (hp j) (K.add
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))))
      = K.mul (K.mul (castN K (n j)) (hp j)) (K.mul (tw K)
        
        (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one) (g K h j))) by unfold tw; lib_ring, hnT j]
  have u := le_mul_nn K O.toCone (K.mul (K.mul (K.mul (tw K) (tw K)) (K.mul T' T'))
      (K.mul (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one)
        (K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one))) _ _
    (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ ntw ntw) (O.nn_sq T')) (O.nn_sq _)) (g_upper K O h hh hh2 j)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ u ⟨K.zero, O.nn_zero, ?_⟩)
  · generalize g K h j = G
    generalize K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one = M1
    lib_ring
  · generalize g K h j = G
    generalize K.add (K.add (K.mul (tw K) P) (K.mul (tw K) (K.mul (K.mul c c) Q))) K.one = M1
    lib_ring

end Gen
end RiemannInst

#print axioms RiemannInst.sq_diff2
#print axioms RiemannInst.g_lower
#print axioms RiemannInst.g_upper
#print axioms RiemannInst.cell_close
#print axioms RiemannInst.riemann_inst

#print axioms: 5 theorems, no axioms.

79.273 The step-size distance controls each coefficient component

\(\nu \,(u_r.x-v_r.x)^2\le Q_e(u,v)\) for \(r\in l\) with \(|k_r|^2\ge 1\), and likewise for \(y,z\). Turns the step-size estimate into the squared component bounds of the Riemann-sum theorem.

ns/QeComp.lean

/-
  ns/QeComp.lean — T37 cycle 1896 (goal G860e, piece b1): the step-size distance Qe controls each coefficient
  component: for r ∈ l with |k_r|² ≥ 1,
    qe_comp_x/y/z   ν·(u_r.x − v_r.x)² ≤ Qe(u, v)     (likewise y, z)
    div_nu          ν·y ≤ X, ν·iν = 1, iν ≥ 0  ⇒  y ≤ iν·X
  so a level-to-level bound Qe ≤ X·hʲ (StepUniformS) gives the squared component bounds (iν·X)·hʲ needed by
  RiemannInst for the dissipation term A.
  Lean 4 core plus ns/StepCauchy, ns/RealCoef.
-/
import ns.StepCauchy
import ns.RealCoef

namespace QeComp
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin ForceBound EnstDiff EnstGalerkin ForceDiff
  StepCauchy RealCoef DyadicCoef ModeCount CubeCount

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qe_term (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (dot3 K (dF K u v r) (dF K u v r))) (Qe K ν k l kz u v) := by
  have a := le_mu K O (wt K kz r) _ hw (dot3_nn K O.toCone (dF K u v r))
  have b := term_le K O (fun r => K.mul (wt K kz r) (dot3 K (dF K u v r) (dF K u v r)))
    (fun r => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l r hr
  have c := le_mul_nn K O.toCone ν _ _ hν (le_trans' K O.toCone _ _ _ a b)
  refine le_trans' K O.toCone _ _ _ c ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν))
    (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l), rfl⟩

theorem qe_comp (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) (f : V3 R → R)
    (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w)) :
    Le K O.toCone (K.mul ν (K.mul (f (dF K u v r)) (f (dF K u v r)))) (Qe K ν k l kz u v) :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone ν _ _ hν (hfc _)) (qe_term K O ν k hν hk l kz u v r hr hw)

theorem qe_comp_x (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).x (v r).x) (sub K (u r).x (v r).x))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.x) (comp_x K O)

theorem qe_comp_y (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).y (v r).y) (sub K (u r).y (v r).y))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.y) (comp_y K O)

theorem qe_comp_z (ν k : R) (hν : O.nn ν) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) (r : I)
    (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r)) :
    Le K O.toCone (K.mul ν (K.mul (sub K (u r).z (v r).z) (sub K (u r).z (v r).z))) (Qe K ν k l kz u v) :=
  qe_comp K O ν k hν hk l kz u v r hr hw (fun w => w.z) (comp_z K O)

theorem div_nu (ν iν y X : R) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (h : Le K O.toCone (K.mul ν y) X) :
    Le K O.toCone y (K.mul iν X) := by
  have a := le_mul_nn K O.toCone iν _ _ hi h
  rw [show K.mul iν (K.mul ν y) = K.mul (K.mul ν iν) y by lib_ring, hνi, show K.mul K.one y = y by lib_ring] at a
  exact a

end Gen
end QeComp

#print axioms QeComp.qe_term
#print axioms QeComp.qe_comp_x
#print axioms QeComp.qe_comp_y
#print axioms QeComp.qe_comp_z
#print axioms QeComp.div_nu

#print axioms: 5 theorems, no axioms.

79.274 The Galerkin force is Lipschitz in enstrophy on a fixed mode list

\(\sum _r|N_r(a,a)-N_r(b,b)|^2\le L\,Z(a-b)\) with \(L\) from \(Z(a)\le Z_a\), \(A(b)\le A_b\) and the largest weight \(W\) on \(l\). Uses \(A(d)\le W\,Z(d)\).

ns/ForceLipZ.lean

/-
  ns/ForceLipZ.lean — T37 cycle 1898 (goal G860e, piece b2): on a fixed finite mode list the Galerkin force is
  Lipschitz in the enstrophy distance. With W ≥ every weight |k_p|² on l, Z(a) ≤ Za and A(b) ≤ Ab (H² bounds):
    lsum_le_mem   termwise bounds on the members of l only
    ad_le         A(d) ≤ W·Z(d)
    force_lip     Σ_r |N_r(a,a) − N_r(b,b)|² ≤ L·Z(a − b),
                  L = 2(2ι²·C₁·Za) + 2(2ι²·W·C₂·Za) + 2ι²·Ab·135      (ForceDiff.force_diff)
    force_comp    (N_r(a,a).x − N_r(b,b).x)² ≤ L·Z(a − b) for r ∈ l (any component selector)
  The constant depends on l through W (the time-step limit is taken at a fixed truncation; the truncation limit is
  TruncUniform's). With QeComp, Z(a − b) ≤ Qe(a, b)/ν.
  Lean 4 core plus ns/ForceDiff, ns/QeComp.
-/
import ns.ForceDiff
import ns.QeComp

namespace ForceLipZ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_le_mem (f g : I → R) : ∀ l : List I, (∀ p, p ∈ l → Le K O.toCone (f p) (g p)) →
    Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_mem f g l (fun p hp => h p (List.Mem.tail _ hp)))

theorem nn_of_le (x y : R) (hx : O.nn x) (h : Le K O.toCone x y) : O.nn y := by
  cases h with
  | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hx hd.1

theorem ad_le (kz : I → Mode) (l : List I) (W : R) (hW : ∀ p, p ∈ l → Le K O.toCone (wt K kz p) W) (d : I → V3 R) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (d p) (d p))) l)
      (K.mul W (lsum K (fun p => K.mul (wt K kz p) (dot3 K (d p) (d p))) l)) := by
  rw [← lsum_mul_left K W]
  refine lsum_le_mem K O _ _ l (fun p hp => ?_)
  have a := le_mul_right' K O.toCone (z := K.mul (wt K kz p) (dot3 K (d p) (d p))) (hW p hp)
    (O.nn_mul _ _ (dot3_nn K O.toCone (kv K kz p)) (dot3_nn K O.toCone (d p)))
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ a ⟨K.zero, O.nn_zero, ?_⟩) <;> lib_ring

include mul_inv h10 in
theorem force_lip (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab : R) (hW : ∀ p, p ∈ l → Le K O.toCone (wt K kz p) W) (a b : I → V3 R)
    (hZa : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a q) (a q))) l) Za)
    (hAb : Le K O.toCone (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (b p) (b p))) l) Ab) :
    Le K O.toCone
      (lsum K (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
        (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))) l)
      (K.mul (K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
        (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l)) := by
  have fd := force_diff K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι a b
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
  have nC1 : O.nn (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
    O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
      (K.mul (castN K 135) (iv K inv (Rc + 1)))) :=
    O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _)) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZd : O.nn (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l
  have nAd : O.nn (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K a b p) (dF K a b p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nZa : O.nn Za := nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (hw p) (dot3_nn K O.toCone _)) l) hZa
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have hAd := ad_le K O kz l W hW (dF K a b)
  have t1 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_mul_nn K O.toCone _ _ _ nZd (le_mul_nn K O.toCone _ _ _ nC1 hZa)))
  have t2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ nAd (le_mul_nn K O.toCone _ _ _ nC2 hZa))
      (le_mul_right' K O.toCone hAd (O.nn_mul _ _ nC2 nZa))))
  have t3 := le_mul_nn K O.toCone (K.add K.one K.one) _ _ two (le_mul_nn K O.toCone (K.mul ι ι) _ _ nii
    (le_mul_right' K O.toCone hAb (O.nn_mul _ _ n135 nZd)))
  refine le_trans' K O.toCone _ _ _ fd (le_trans' K O.toCone _ _ _
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_add' K O.toCone _ _ _ _ t1 t2)) t3) ⟨K.zero, O.nn_zero, ?_⟩)
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K a b p) (dF K a b p))) l = Zd
  generalize castN K 135 = c135
  generalize K.add (K.mul (castN K (cube Rc)) K.one) (K.mul c135 (iv K inv (Rc + 1))) = C1
  generalize K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1))))) (K.mul c135 (iv K inv (Rc + 1))) = C2
  lib_ring

end Gen
end ForceLipZ

#print axioms ForceLipZ.lsum_le_mem
#print axioms ForceLipZ.ad_le
#print axioms ForceLipZ.force_lip

#print axioms: 3 theorems, no axioms.

79.275 Per-level bounds for the Riemann sums

Squared bounds \(\alpha h^j,\varphi h^j\) at equal times and \(\beta h^j,\psi h^j\) over one fine step make the Riemann sums of \(F-c\,A(+1)\) Cauchy. Uses \((x-z)^2\le 2(x-y)^2+2(y-z)^2\).

ns/RiemannLevel.lean

/-
  ns/RiemannLevel.lean — T37 cycle 1904 (goal G860e, piece b3): RiemannInst's four cell hypotheses from four simpler
  per-level bounds (2h = 1, every k, m):
    qa  (A_{j+1}(2k) − A_j(k))² ≤ α·hʲ        coefficient, same time      (StepUniformS via QeComp)
    qf  (F_{j+1}(2k) − F_j(k))² ≤ φ·hʲ        force, same time            (ForceLipZ + QeComp + StepUniformS)
    sa  (A_{j+1}(m+1) − A_{j+1}(m))² ≤ β·hʲ   one fine step               (TimeLip.comp_lip)
    sf  (F_{j+1}(m+1) − F_{j+1}(m))² ≤ ψ·hʲ   force over one fine step    (ForceLipZ + TimeLip)
    tri_sq        (x − z)² ≤ 2(x − y)² + 2(y − z)²
    riemann_level ⇒ the Riemann sums of F − c·A(+1) are Cauchy   (RiemannInst with P = 2φ + 2ψ, Q = 2α + 2β)
  Lean 4 core plus ns/RiemannInst.
-/
import ns.RiemannInst

namespace RiemannLevel
open DetChain NSChain DyadicSmallGlobal LibRing LibCS LibGron LibCauchy RealBridge RealArch EnstGronwall DyadicLarge
  RiemannStep RealLinear RealSqrtRate RiemannCauchySq RiemannInst

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem tri_sq (x y z : R) :
    Le K O.toCone (K.mul (sub K x z) (sub K x z))
      (K.add (K.mul (tw K) (K.mul (sub K x y) (sub K x y))) (K.mul (tw K) (K.mul (sub K y z) (sub K y z)))) :=
  ⟨K.mul (sub K (K.add x z) (K.add y y)) (sub K (K.add x z) (K.add y y)), O.nn_sq _, by unfold sub tw; lib_ring⟩

theorem two_bound (x y z u v : R) (hu : Le K O.toCone (K.mul (sub K x y) (sub K x y)) u)
    (hv : Le K O.toCone (K.mul (sub K y z) (sub K y z)) v) :
    Le K O.toCone (K.mul (sub K x z) (sub K x z)) (K.add (K.mul (tw K) u) (K.mul (tw K) v)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  exact le_trans' K O.toCone _ _ _ (tri_sq K O x y z)
    (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ ntw hu) (le_mul_nn K O.toCone _ _ _ ntw hv))

theorem sq_symm (x y : R) : K.mul (sub K x y) (sub K x y) = K.mul (sub K y x) (sub K y x) := by unfold sub; lib_ring

theorem widen (s a b h : R) (hb : O.nn b) (hh : O.nn h) (ha : Le K O.toCone s (K.mul a h)) :
    Le K O.toCone s (K.mul (K.add a b) h) :=
  le_trans' K O.toCone _ _ _ ha ⟨K.mul b h, O.nn_mul _ _ hb hh, by lib_ring⟩

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem riemann_level (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n)) (F A : Nat → Nat → R) (c α φ β ψ h T' : R)
    (n : Nat → Nat) (hp : Nat → R) (hα : O.nn α) (hφ : O.nn φ) (hβ : O.nn β) (hψ : O.nn ψ) (hh : O.nn h)
    (hh2 : K.mul h (tw K) = K.one) (hpn : ∀ j, O.nn (hp j)) (hnT : ∀ j, K.mul (castN K (n j)) (hp j) = T')
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hp2 : ∀ j, K.add (hp (j + 1)) (hp (j + 1)) = hp j)
    (qa : ∀ j k, Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k)) (A j k)) (sub K (A (j + 1) (2 * k)) (A j k))) (K.mul α (pwr K h j)))
    (qf : ∀ j k, Le K O.toCone (K.mul (sub K (F (j + 1) (2 * k)) (F j k)) (sub K (F (j + 1) (2 * k)) (F j k))) (K.mul φ (pwr K h j)))
    (sa : ∀ j m, Le K O.toCone (K.mul (sub K (A (j + 1) (m + 1)) (A (j + 1) m)) (sub K (A (j + 1) (m + 1)) (A (j + 1) m))) (K.mul β (pwr K h j)))
    (sf : ∀ j m, Le K O.toCone (K.mul (sub K (F (j + 1) (m + 1)) (F (j + 1) m)) (sub K (F (j + 1) (m + 1)) (F (j + 1) m))) (K.mul ψ (pwr K h j))) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (hp j) (hp j)) (sub K (F j k) (K.mul c (A j (k + 1))))) (n j)) := by
  have ntw : O.nn (tw K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have nP : O.nn (K.add (K.mul (tw K) φ) (K.mul (tw K) ψ)) := O.nn_add _ _ (O.nn_mul _ _ ntw hφ) (O.nn_mul _ _ ntw hψ)
  have nQ : O.nn (K.add (K.mul (tw K) α) (K.mul (tw K) β)) := O.nn_add _ _ (O.nn_mul _ _ ntw hα) (O.nn_mul _ _ ntw hβ)
  have ph : ∀ j, O.nn (pwr K h j) := pwr_nn K O h hh
  -- the two-term bound, rearranged to (2u + 2v)·hʲ
  have tb : ∀ (j : Nat) (x y z u v : R), Le K O.toCone (K.mul (sub K x y) (sub K x y)) (K.mul u (pwr K h j)) →
      Le K O.toCone (K.mul (sub K y z) (sub K y z)) (K.mul v (pwr K h j)) →
      Le K O.toCone (K.mul (sub K x z) (sub K x z)) (K.mul (K.add (K.mul (tw K) u) (K.mul (tw K) v)) (pwr K h j)) := by
    intro j x y z u v h1 h2
    refine le_trans' K O.toCone _ _ _ (two_bound K O x y z _ _ h1 h2) ⟨K.zero, O.nn_zero, ?_⟩
    lib_ring
  refine riemann_inst K O inv mul_inv arch F A c (K.add (K.mul (tw K) φ) (K.mul (tw K) ψ))
    (K.add (K.mul (tw K) α) (K.mul (tw K) β)) h T' n hp nP nQ hh hh2 hpn hnT hn2 hp2 ?_ ?_ ?_ ?_
  · -- hF0
    intro j k _
    refine le_trans' K O.toCone _ _ _ (widen K O _ φ (K.add φ (K.mul (tw K) ψ)) _ (O.nn_add _ _ hφ (O.nn_mul _ _ ntw hψ)) (ph j) (qf j k))
      ⟨K.zero, O.nn_zero, ?_⟩
    unfold tw; lib_ring
  · -- hF1: via F_{j+1}(2k)
    intro j k _
    exact tb j _ _ _ ψ φ (sf j (2 * k)) (qf j k) |> fun t => le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, by lib_ring⟩
  · -- hA0: via A_{j+1}(2k+2)
    intro j k _
    have q := qa j (k + 1)
    have s := sa j (2 * k + 1)
    rw [sq_symm K (A (j + 1) (2 * k + 1 + 1)) (A (j + 1) (2 * k + 1))] at s
    exact le_trans' K O.toCone _ _ _ (tb j _ (A (j + 1) (2 * k + 1 + 1)) _ β α s q) ⟨K.zero, O.nn_zero, by lib_ring⟩
  · -- hA1
    intro j k _
    have q : Le K O.toCone (K.mul (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))) (sub K (A (j + 1) (2 * k + 1 + 1)) (A j (k + 1))))
        (K.mul α (pwr K h j)) := qa j (k + 1)
    refine le_trans' K O.toCone _ _ _ (widen K O _ α (K.add α (K.mul (tw K) β)) _ (O.nn_add _ _ hα (O.nn_mul _ _ ntw hβ)) (ph j) q)
      ⟨K.zero, O.nn_zero, ?_⟩
    unfold tw; lib_ring

end Gen
end RiemannLevel

#print axioms RiemannLevel.tri_sq
#print axioms RiemannLevel.two_bound
#print axioms RiemannLevel.riemann_level

#print axioms: 3 theorems, no axioms.

79.276 One implicit step moves a coefficient by the step size

\(s^2\le S_0p\), \(n^2\le B_n\), \(a^2\le B_a\), \(0\le w\le W\) \(\Rightarrow \) \((a'-a)^2\le S_0(2B_n+2\nu ^2W^2B_a)\,p\). The one-fine-step bound of the Riemann-sum argument.

ns/StepLevel.lean

/-
  ns/StepLevel.lean — T37 cycle 1906 (goal G860e, piece b4a): one implicit-viscosity step of size s moves a
  coefficient component by O(s), so with s² ≤ S₀·hʲ the per-level bound sa of RiemannLevel holds:
    mul_le2    0 ≤ x ≤ X, 0 ≤ y ≤ Y  ⇒  xy ≤ XY
    sa_bound   StepC s ν w a' a n, n² ≤ Bn, a² ≤ Ba, 0 ≤ w ≤ W, s² ≤ S₀·p  ⇒
               (a' − a)² ≤ (S₀·(2Bn + 2(ν²W²)Ba))·p
  (TimeLip.comp_lip: (a' − a)² ≤ 2s²n² + 2(sνw)²a²).
  Lean 4 core plus ns/TimeLip, ns/RiemannLevel.
-/
import ns.TimeLip
import ns.RiemannLevel

namespace StepLevel
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstStep RiemannLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem mul_le2 (x X y Y : R) (hx : O.nn x) (hY : O.nn Y) (h1 : Le K O.toCone x X) (h2 : Le K O.toCone y Y) :
    Le K O.toCone (K.mul x y) (K.mul X Y) := by
  have a := le_mul_nn K O.toCone x _ _ hx h2
  have b := le_mul_nn K O.toCone Y _ _ hY h1
  rw [show K.mul Y x = K.mul x Y by lib_ring, show K.mul Y X = K.mul X Y by lib_ring] at b
  exact le_trans' K O.toCone _ _ _ a b

theorem sa_bound (s ν w a' a n Bn Ba W S0 p : R) (hs : StepC K s ν w a' a n) (hsn : O.nn s) (hν : O.nn ν)
    (hw : O.nn w) (hBn : O.nn Bn) (hBa : O.nn Ba) (hWW : Le K O.toCone w W)
    (hn : Le K O.toCone (K.mul n n) Bn) (ha : Le K O.toCone (K.mul a a) Ba) (hss : Le K O.toCone (K.mul s s) (K.mul S0 p)) :
    Le K O.toCone (K.mul (sub K a' a) (sub K a' a))
      (K.mul (K.mul S0 (K.add (K.mul (K.add K.one K.one) Bn)
        (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Ba)))) p) := by
  have c := TimeLip.comp_lip K O.toCone s ν w a' a n hsn hν hw hs
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hWn : O.nn W := by cases hWW with | intro d hd => rw [← hd.2]; exact O.nn_add _ _ hw hd.1
  have ww := mul_le2 K O w W w W hw hWn hWW hWW
  have b1 := mul_le2 K O (K.mul s s) (K.mul S0 p) (K.mul n n) Bn (O.nn_sq s) hBn hss hn
  have vw := mul_le2 K O (K.mul ν ν) (K.mul ν ν) (K.mul w w) (K.mul W W) (O.nn_sq ν) (O.nn_mul _ _ hWn hWn) (le_refl' K O.toCone _) ww
  have b2 := mul_le2 K O (K.mul (K.mul ν ν) (K.mul w w)) (K.mul (K.mul ν ν) (K.mul W W)) (K.mul a a) Ba
    (O.nn_mul _ _ (O.nn_sq ν) (O.nn_sq w)) hBa vw ha
  have b3 := mul_le2 K O (K.mul s s) (K.mul S0 p) (K.mul (K.mul (K.mul ν ν) (K.mul w w)) (K.mul a a))
    (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Ba) (O.nn_sq s) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ν) (O.nn_mul _ _ hWn hWn)) hBa) hss b2
  have s1 := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two b1) (le_mul_nn K O.toCone _ _ _ two b3)
  refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _ c (le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩
    (le_trans' K O.toCone _ _ _ s1 ⟨K.zero, O.nn_zero, ?_⟩)))
  · unfold sub; lib_ring
  · lib_ring
  · lib_ring

end Gen
end StepLevel

#print axioms StepLevel.mul_le2
#print axioms StepLevel.sa_bound

#print axioms: 2 theorems, no axioms.

79.277 Equal-time level bounds from the step-size distance

\(Q_e(a,b)\le Xp\) gives \((f(b_r)-f(a_r))^2\le \nu ^{-1}Xp\) and \((f(N_r(b))-f(N_r(a)))^2\le L\nu ^{-1}Xp\) for every component \(f\). The bounds \(q_a,q_f\) of the Riemann-sum argument.

ns/LevelQ.lean

/-
  ns/LevelQ.lean — T37 cycle 1908 (goal G860e, piece b4b): the equal-time per-level bounds qa, qf of RiemannLevel from
  the step-size distance. If Qe(a, b) ≤ X·p (a, b the level-j and level-(j+1) states at the same time; StepUniformS),
  ν·iν = 1, and r ∈ l with |k_r|² ≥ 1:
    qa_comp   (f(b_r) − f(a_r))² ≤ (iν·X)·p                          (any component selector f)
    z_le_qe   ν·Z(u − v) ≤ Qe(u, v)
    qf_comp   (f(N_r(b,b)) − f(N_r(a,a)))² ≤ (L·(iν·X))·p           (ForceLipZ with Z(b) ≤ Za, A(a) ≤ Ab)
  Lean 4 core plus ns/ForceLipZ, ns/RHUniform.
-/
import ns.ForceLipZ
import ns.RHUniform

namespace LevelQ
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem qa_comp (ν k iν X p : R) (hν : O.nn ν) (hk : O.nn k) (hi : O.nn iν) (hνi : K.mul ν iν = K.one)
    (l : List I) (kz : I → Mode) (a b : I → V3 R) (r : I) (hr : r ∈ l) (hw : Le K O.toCone K.one (wt K kz r))
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v))
    (hQ : Le K O.toCone (Qe K ν k l kz a b) (K.mul X p)) :
    Le K O.toCone (K.mul (sub K (f (b r)) (f (a r))) (sub K (f (b r)) (f (a r)))) (K.mul (K.mul iν X) p) := by
  have c := qe_comp K O ν k hν hk l kz b a r hr hw f hfc
  rw [RHUniform.qe_symm K ν k l kz b a] at c
  have d := div_nu K O ν iν _ _ hi hνi (le_trans' K O.toCone _ _ _ c hQ)
  rw [show f (dF K b a r) = sub K (f (b r)) (f (a r)) from hf _ _] at d
  exact le_trans' K O.toCone _ _ _ d ⟨K.zero, O.nn_zero, by lib_ring⟩

theorem z_le_qe (ν k : R) (hk : O.nn k) (l : List I) (kz : I → Mode) (u v : I → V3 R) :
    Le K O.toCone (K.mul ν (lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K u v p) (dF K u v p))) l)) (Qe K ν k l kz u v) :=
  ⟨_, O.nn_mul _ _ (O.nn_mul _ _ hk (O.nn_sq ν))
    (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l), rfl⟩

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem qf_comp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab ν k iν X p Lc : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (a b : I → V3 R)
    (hk : O.nn k) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (hLc : O.nn Lc)
    (hZb : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (b q) (b q))) l) Za)
    (hAa : Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a q) (a q))) l) Ab)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (hQ : Le K O.toCone (Qe K ν k l kz a b) (K.mul X p)) (r : I) (hr : r ∈ l)
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v)) :
    Le K O.toCone
      (K.mul (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)))
        (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))))
      (K.mul (K.mul Lc (K.mul iν X)) p) := by
  have fl := force_lip K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab hW b a hZb hAa
  rw [← hL] at fl
  have tl := term_le K O (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)))
    (fun r => dot3_nn K O.toCone _) l r hr
  have cf := hfc (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))
  rw [hf] at cf
  have zq := z_le_qe K O ν k hk l kz b a
  rw [RHUniform.qe_symm K ν k l kz b a] at zq
  have zd := div_nu K O ν iν _ _ hi hνi (le_trans' K O.toCone _ _ _ zq hQ)
  refine le_trans' K O.toCone _ _ _ cf (le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ fl
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone Lc _ _ hLc zd) ⟨K.zero, O.nn_zero, by lib_ring⟩)))

end Gen
end LevelQ

#print axioms LevelQ.qa_comp
#print axioms LevelQ.z_le_qe
#print axioms LevelQ.qf_comp

#print axioms: 3 theorems, no axioms.

79.278 The force over one fine step

A Galerkin step of size \(s\) with \(s^2\le S_0p\) moves each force component by \((\Delta f(N_r))^2\le L\,W\,S_0(2B_N+2\nu ^2B_A)\,p\). The bound \(s_f\) of the Riemann-sum argument.

ns/LevelS.lean

/-
  ns/LevelS.lean — T37 cycle 1910 (goal G860e, piece b5a): the force moves by O(step) over one fine step, giving the
  bound sf of RiemannLevel. For a Galerkin step b(m) → b(m+1) of size s with s² ≤ S₀·p, Σ_r|N_r(b(m))|² ≤ BN,
  Σ_r w_r²|b_r(m)|² ≤ BA, weights ≤ W on l:
    z_le_w    Z(d) ≤ W·Σ_r |d_r|²
    sf_comp   (f(N_r(b(m+1))) − f(N_r(b(m))))² ≤ (L·(W·(S₀·(2BN + 2ν²BA))))·p
  (ForceLipZ.force_lip along the step, TimeLip.time_lip for Σ|b(m+1) − b(m)|²).
  Lean 4 core plus ns/LevelQ, ns/TimeLip, ns/StepLevel.
-/
import ns.LevelQ
import ns.TimeLip
import ns.StepLevel

namespace LevelS
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}

theorem z_le_w (kz : I → Mode) (l : List I) (W : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (d : I → V3 R) :
    Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (d q) (d q))) l) (K.mul W (lsum K (fun q => dot3 K (d q) (d q)) l)) := by
  rw [← lsum_mul_left K W]
  exact lsum_le_mem K O _ _ l (fun q hq => le_mul_right' K O.toCone (hW q hq) (dot3_nn K O.toCone (d q)))

variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem sf_comp (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι W Za Ab ν s S0 p BN BA Lc : R) (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W) (hWn : O.nn W) (hLc : O.nn Lc)
    (hs0 : O.nn s) (hν : O.nn ν) (b' b : I → V3 R)
    (hstep : ∀ r, Step K s ν (wt K kz r) (b' r) (b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
    (hZb : Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (b' q) (b' q))) l) Za)
    (hAa : Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (b q) (b q))) l) Ab)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (hBN : Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) l) BN)
    (hBA : Le K O.toCone (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (b r) (b r))) l) BA)
    (hss : Le K O.toCone (K.mul s s) (K.mul S0 p)) (r : I) (hr : r ∈ l)
    (f : V3 R → R) (hfc : ∀ w, Le K O.toCone (K.mul (f w) (f w)) (dot3 K w w))
    (hf : ∀ u v, f (sub3 K u v) = sub K (f u) (f v)) :
    Le K O.toCone
      (K.mul (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))
        (sub K (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r)) (f (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))))
      (K.mul (K.mul Lc (K.mul W (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))))) p) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have fl := force_lip K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab hW b' b hZb hAa
  rw [← hL] at fl
  have tl := term_le K O (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)))
    (fun r => dot3_nn K O.toCone _) l r hr
  have cf := hfc (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b' b' r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r))
  rw [hf] at cf
  have zw := z_le_w K O kz l W hW (dF K b' b)
  have tlip := TimeLip.time_lip K O.toCone l s ν hs0 hν (wt K kz) (fun r => dot3_nn K O.toCone _) b' b
    (fun r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) hstep
  have hBNn : O.nn BN := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => dot3_nn K O.toCone _) l) hBN
  have hBAn : O.nn BA := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l) hBA
  -- Σ|b' − b|² ≤ 2s²·BN + 2s²ν²·BA ≤ S₀p·(2BN + 2ν²BA)
  have t2 : Le K O.toCone (K.add (K.mul (K.mul (K.add K.one K.one) (K.mul s s)) (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) b b r)) l))
        (K.mul (K.mul (K.add K.one K.one) (K.mul (K.mul s s) (K.mul ν ν))) (lsum K (fun r => K.mul (K.mul (wt K kz r) (wt K kz r)) (dot3 K (b r) (b r))) l)))
      (K.mul (K.mul S0 p) (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))) := by
    have u1 := StepLevel.mul_le2 K O (K.mul s s) (K.mul S0 p) _ BN (O.nn_sq s) hBNn hss hBN
    have u2 := StepLevel.mul_le2 K O (K.mul s s) (K.mul S0 p) _ (K.mul (K.mul ν ν) BA) (O.nn_sq s) (O.nn_mul _ _ (O.nn_sq ν) hBAn) hss
      (le_mul_nn K O.toCone (K.mul ν ν) _ _ (O.nn_sq ν) hBA)
    refine le_trans' K O.toCone _ _ _ ⟨K.zero, O.nn_zero, ?_⟩ (le_trans' K O.toCone _ _ _
      (le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two u1) (le_mul_nn K O.toCone _ _ _ two u2)) ⟨K.zero, O.nn_zero, ?_⟩)
    · lib_ring
    · lib_ring
  have dsum : Le K O.toCone (lsum K (fun q => dot3 K (dF K b' b q) (dF K b' b q)) l)
      (K.mul (K.mul S0 p) (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) BA)))) :=
    le_trans' K O.toCone _ _ _ tlip t2
  refine le_trans' K O.toCone _ _ _ cf (le_trans' K O.toCone _ _ _ tl (le_trans' K O.toCone _ _ _ fl
    (le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone Lc _ _ hLc (le_trans' K O.toCone _ _ _ zw (le_mul_nn K O.toCone W _ _ hWn dsum)))
      ⟨K.zero, O.nn_zero, by lib_ring⟩)))

end Gen
end LevelS

#print axioms LevelS.z_le_w
#print axioms LevelS.sf_comp

#print axioms: 2 theorems, no axioms.

79.279 Riemann sums of the Galerkin equation converge

For a step-halving Galerkin family with the step-size bound \(Q_e\le X h^j\), an \(H^2\) ball and \(s_{j+1}^2\le S_0h^j\), the sums \(\sum _k s_j\big (N_r(a_j(k)).x-c\,a_j(k+1)_r.x\big )\) are Cauchy. With the integrated identity, the limit coefficients satisfy the integrated Galerkin equation.

ns/RiemannGalerkin.lean

/-
  ns/RiemannGalerkin.lean — T37 cycle 1912 (goal G860e, piece b5b; G764b): the Riemann sums of the integrated
  Galerkin equation converge with an explicit level-to-level residual. For a step-halving family a_j (level j has step
  s_j, s_{j+1} + s_{j+1} = s_j, n_{j+1} = 2n_j, n_j·s_{j+1} = T'), each level a Galerkin implicit-viscosity scheme on l,
  with
    the step-size bound  Qe(a_j(k), a_{j+1}(2k)) ≤ X·hʲ          (StepUniformS),
    the H² ball          Z(a_j(m)) ≤ Za, A(a_j(m)) ≤ Ab, Σ_r|N_r(a_j(m))|² ≤ BN,
    step sizes           s_{j+1}² ≤ S₀·hʲ,   weights 1 ≤ w_r, w_q ≤ W on l,
  the sums S_j = Σ_{k<n_j} s_j·(N_r(a_j(k)).x − c·a_j(k+1)_r.x) are Cauchy (2h = 1, Archimedean field):
    riemann_galerkin   RiemannLevel.riemann_level with qa, qf (LevelQ), sa (StepLevel), sf (LevelS).
  With RealIntegral (lim a(T) − lim a(0) ≈ lim S_j for c = νw_r) the limit coefficients satisfy the integrated Galerkin
  equation, the Riemann residual between levels being O(2^{−j/2}) explicitly.
  Lean 4 core plus ns/LevelS.
-/
import ns.LevelS

namespace RiemannGalerkin
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceBilin EnstDiff LatticeSum ForceDiff
  RealCoef DyadicCoef StepCauchy QeComp ForceLipZ EnstStep LibCS LibCauchy RealBridge RealArch EnstGronwall RiemannLevel

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem riemann_galerkin (arch : ∀ x, ∃ n, Le K O.toCone x (castN K n))
    (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι ν iν W Za Ab BN X S0 h T' c Lc : R) (hν : O.nn ν) (hi : O.nn iν) (hνi : K.mul ν iν = K.one) (hWn : O.nn W)
    (hX : O.nn X) (hS0 : O.nn S0) (hBN : O.nn BN) (hAb : O.nn Ab) (hLc : O.nn Lc) (hh : O.nn h)
    (hh2 : K.mul h (tw K) = K.one)
    (hW : ∀ q, q ∈ l → Le K O.toCone (wt K kz q) W)
    (hL : Lc = K.add (K.add
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι)
            (K.mul (K.add (K.mul (castN K (cube Rc)) K.one) (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))
          (K.mul (K.add K.one K.one) (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul W
            (K.mul (K.add (K.mul (castN K (cube Rc)) (K.mul (inv (castN K (t0 + 1))) (inv (castN K (t0 + 1)))))
                (K.mul (castN K 135) (iv K inv (Rc + 1)))) Za))))))
          (K.mul (K.add K.one K.one) (K.mul (K.mul ι ι) (K.mul Ab (castN K 135)))))
    (a : Nat → Nat → I → V3 R) (s : Nat → R) (n : Nat → Nat) (hsn : ∀ j, O.nn (s j))
    (hstep : ∀ j m r, Step K (s j) ν (wt K kz r) (a j (m + 1) r) (a j m r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r))
    (hn2 : ∀ j, n (j + 1) = 2 * n j) (hs2 : ∀ j, K.add (s (j + 2)) (s (j + 2)) = s (j + 1))
    (hnT : ∀ j, K.mul (castN K (n j)) (s (j + 1)) = T')
    (hss : ∀ j, Le K O.toCone (K.mul (s (j + 1)) (s (j + 1))) (K.mul S0 (pwr K h j)))
    (hQ : ∀ j k, Le K O.toCone (Qe K ν (s (j + 1)) l kz (a j k) (a (j + 1) (2 * k))) (K.mul X (pwr K h j)))
    (hZ : ∀ j m, Le K O.toCone (lsum K (fun q => K.mul (wt K kz q) (dot3 K (a j m q) (a j m q))) l) Za)
    (hA : ∀ j m, Le K O.toCone (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (a j m q) (a j m q))) l) Ab)
    (hN : ∀ j m, Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)) l) BN)
    (r : I) (hr : r ∈ l) (hw1 : Le K O.toCone K.one (wt K kz r)) :
    Cauchy K (posO K O) (fun j => sumN K (fun k => K.mul (K.add (s (j + 1)) (s (j + 1)))
      (sub K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j k) (a j k) r).x (K.mul c (a j (k + 1) r).x))) (n j)) := by
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hx : ∀ u v : V3 R, (sub3 K u v).x = sub K u.x v.x := fun _ _ => rfl
  have hZa : O.nn Za := ForceLipZ.nn_of_le K O _ _ (lsum_nn K O.toCone _ (fun _ => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l) (hZ 0 0)
  -- component bounds: (N_r.x)² ≤ BN, (a_r.x)² ≤ Za
  have nB : ∀ j m, Le K O.toCone (K.mul (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r).x
      (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r).x) BN := fun j m =>
    le_trans' K O.toCone _ _ _ (comp_x K O _) (le_trans' K O.toCone _ _ _
      (term_le K O (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j m) (a j m) r)) (fun _ => dot3_nn K O.toCone _) l r hr) (hN j m))
  have aB : ∀ j m, Le K O.toCone (K.mul (a j m r).x (a j m r).x) Za := fun j m =>
    le_trans' K O.toCone _ _ _ (comp_x K O _) (le_trans' K O.toCone _ _ _ (le_mu K O _ _ hw1 (dot3_nn K O.toCone _))
      (le_trans' K O.toCone _ _ _ (term_le K O (fun q => K.mul (wt K kz q) (dot3 K (a j m q) (a j m q)))
        (fun _ => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l r hr) (hZ j m)))
  refine riemann_level K O inv mul_inv arch
    (fun j k => (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a j k) (a j k) r).x) (fun j k => (a j k r).x) c
    (K.mul iν X) (K.mul Lc (K.mul iν X))
    (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul ν ν) (K.mul W W)) Za))))
    (K.mul Lc (K.mul W (K.mul S0 (K.add (K.mul (K.add K.one K.one) BN) (K.mul (K.add K.one K.one) (K.mul (K.mul ν ν) Ab))))))
    h T' n (fun j => s (j + 1)) (O.nn_mul _ _ hi hX) (O.nn_mul _ _ hLc (O.nn_mul _ _ hi hX))
    (O.nn_mul _ _ hS0 (O.nn_add _ _ (O.nn_mul _ _ two hBN) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq ν) (O.nn_mul _ _ hWn hWn)) hZa))))
    (O.nn_mul _ _ hLc (O.nn_mul _ _ hWn (O.nn_mul _ _ hS0 (O.nn_add _ _ (O.nn_mul _ _ two hBN) (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) hAb))))))
    hh hh2 (fun j => hsn (j + 1)) hnT hn2 hs2 ?_ ?_ ?_ ?_
  · intro j k
    exact LevelQ.qa_comp K O ν (s (j + 1)) iν X (pwr K h j) hν (hsn (j + 1)) hi hνi l kz (a j k) (a (j + 1) (2 * k)) r hr hw1
      (fun v => v.x) (comp_x K O) hx (hQ j k)
  · intro j k
    exact LevelQ.qf_comp K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab ν (s (j + 1)) iν X (pwr K h j) Lc hW
      (a j k) (a (j + 1) (2 * k)) (hsn (j + 1)) hi hνi hLc (hZ (j + 1) (2 * k)) (hA j k) hL (hQ j k) r hr
      (fun v => v.x) (comp_x K O) hx
  · intro j m
    exact StepLevel.sa_bound K O (s (j + 1)) ν (wt K kz r) _ _ _ BN Za W S0 (pwr K h j) (hstep (j + 1) m r).1
      (hsn (j + 1)) hν (dot3_nn K O.toCone _) hBN hZa (hW r hr) (nB (j + 1) m) (aB (j + 1) m) (hss j)
  · intro j m
    exact LevelS.sf_comp K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι W Za Ab ν (s (j + 1)) S0 (pwr K h j) BN Ab Lc
      hW hWn hLc (hsn (j + 1)) hν (a (j + 1) (m + 1)) (a (j + 1) m) (hstep (j + 1) m) (hZ (j + 1) (m + 1)) (hA (j + 1) m) hL
      (hN (j + 1) m) (hA (j + 1) m) (hss j) r hr (fun v => v.x) (comp_x K O) hx

end Gen
end RiemannGalerkin

#print axioms RiemannGalerkin.riemann_galerkin

#print axioms: 1 theorem, no axioms.

79.280 The shell inequality for the weight \(|k|^{-2}\)

A shell of at most \((2j+1)^3\) modes contributes at most \(81\) to the Abel sum of \(|k|_\infty ^{-2}\).

ns/LatticeStep2.lean

/-
  ns/LatticeStep2.lean — T37 cycle 1952 (goal G859c2b1, piece a): the per-shell inequality for the weight |k|^{-2}
  (the low-shell weight of the Agmon cut), over a commutative ring with a nonnegative cone. With A·iA = 1, B·iB = 1,
  B = A + 1 and a nonnegative W with C·(A + B) + W = 81·A²B²:
    inv2_diff   iA² − iB² = (A + B)·iA²·iB²
    step2_le    C·(iA² − iB²) ≤ 81
  i.e. a shell of at most (2j+1)³ modes contributes at most 81 to the Abel sum of |k|^{-2}: Σ_{|k|∞ ≤ K} |k|∞^{-2} ≤ 81K.

  Lean 4 core plus ns/LatticeStep, lib/RingTac.
-/
import ns.LatticeStep

namespace LatticeStep2
open DetChain NSChain LibRing

section Gen
variable {R : Type} (K : CRing R) (P : Cone K)

def p2 (x : R) : R := K.mul x x

theorem inv2_diff (A B iA iB : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one) :
    K.add (p2 K iA) (K.neg (p2 K iB)) = K.mul (K.add A B) (K.mul (p2 K iA) (p2 K iB)) := by
  unfold p2
  have e1 : K.mul (K.add A B) (K.mul (K.mul iA iA) (K.mul iB iB))
      = K.add (K.mul (K.mul (K.mul B iB) (K.mul B iB)) (K.mul iA iA))
          (K.neg (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul iB iB))) := by
    rw [hBA]; lib_ring
  rw [e1, hA, hB]; lib_ring

theorem step2_le (A B iA iB C W : R) (hA : K.mul A iA = K.one) (hB : K.mul B iB = K.one) (hBA : B = K.add A K.one)
    (hW : P.nn W) (hCW : K.add (K.mul C (K.add A B)) W = K.mul (castN K 81) (K.mul (p2 K A) (p2 K B))) :
    Le K P (K.mul C (K.add (p2 K iA) (K.neg (p2 K iB)))) (castN K 81) := by
  have hq : P.nn (K.mul (p2 K iA) (p2 K iB)) := P.nn_mul _ _ (P.nn_sq iA) (P.nn_sq iB)
  refine ⟨K.mul W (K.mul (p2 K iA) (p2 K iB)), P.nn_mul _ _ hW hq, ?_⟩
  rw [inv2_diff K A B iA iB hA hB hBA]
  have e2 : K.add (K.mul C (K.mul (K.add A B) (K.mul (p2 K iA) (p2 K iB)))) (K.mul W (K.mul (p2 K iA) (p2 K iB)))
      = K.mul (K.add (K.mul C (K.add A B)) W) (K.mul (p2 K iA) (p2 K iB)) := by lib_ring
  rw [e2, hCW]
  unfold p2
  have e3 : K.mul (K.mul (castN K 81) (K.mul (K.mul A A) (K.mul B B))) (K.mul (K.mul iA iA) (K.mul iB iB))
      = K.mul (castN K 81) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul (K.mul B iB) (K.mul B iB))) := by lib_ring
  rw [e3, hA, hB]; lib_ring

end Gen

end LatticeStep2

#print axioms LatticeStep2.inv2_diff
#print axioms LatticeStep2.step2_le

#print axioms: 2 theorems, no axioms.

79.281 The Abel shell sum for the weight \(|k|^{-2}\)

\(\sum _{j<M}\mathrm {cnt}_j\,(j^{-2}-(j+1)^{-2})\le 81M\) when every shell count is at most \((2j+1)^3\).

ns/LatticeSum2.lean

/-
  ns/LatticeSum2.lean — T37 cycle 1954 (goal G859c2b1, piece b): the Abel shell sum for the weight |k|^{-2}, over an
  ordered field. With f2 j = (1/j)² and a list L of naturals ≥ 1 with cnt(L, j) ≤ (2j+1)³ (ModeCount.cnt_cube):
    poly2        c ≤ (2a+1)³, a ≥ 1  ⇒  c·(2a+1) ≤ 81·a²(a+1)²
    shell2_term  cnt(L, i+1)·(f2(i+1) − f2(i+2)) ≤ 81
    shell2_sum   Σ_{j<M} cnt(L, j)·(f2 j − f2(j+1)) ≤ M·81
  (no telescoping: the low Agmon weight Σ_{|k|∞ ≤ K}|k|∞^{-2} grows linearly in the cut K).

  Lean 4 core plus ns/LatticeSum, ns/LatticeStep2.
-/
import ns.LatticeSum
import ns.LatticeStep2

namespace LatticeSum2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum LatticeStep2

theorem poly2 (a c : Nat) (ha : 1 ≤ a) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1)))) :
    c * (a + (a + 1)) ≤ 81 * (a * a) * ((a + 1) * (a + 1)) := by
  have hs : a + (a + 1) ≤ 3 * a := by
    rw [show 3 * a = a + (a + a) by lib_nat]
    exact Nat.add_le_add_left (Nat.add_le_add_left ha a) a
  have h1 : c * (a + (a + 1)) ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))) * (a + (a + 1)) :=
    Nat.mul_le_mul_right _ hc
  have h2 : (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))) * (a + (a + 1)) ≤ (3 * a) * ((3 * a) * (3 * a)) * (3 * a) :=
    Nat.mul_le_mul (Nat.mul_le_mul hs (Nat.mul_le_mul hs hs)) hs
  have h3 : (3 * a) * ((3 * a) * (3 * a)) * (3 * a) ≤ 81 * (a * a) * ((a + 1) * (a + 1)) := by
    rw [show (3 * a) * ((3 * a) * (3 * a)) * (3 * a) = 81 * (a * a) * (a * a) by lib_nat]
    have ha1 : a ≤ a + 1 := Nat.le_succ a
    exact Nat.mul_le_mul_left _ (Nat.mul_le_mul ha1 ha1)
  exact Nat.le_trans h1 (Nat.le_trans h2 h3)

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def f2 (j : Nat) : R := p2 K (iv K inv j)

/-- one shell: c·(iA² − iB²) ≤ 81 for A = a, B = a + 1 -/
theorem shell2_core (a : Nat) (ha : 1 ≤ a) (c : Nat) (hc : c ≤ (a + (a + 1)) * ((a + (a + 1)) * (a + (a + 1))))
    (iA iB : R) (hA : K.mul (castN K a) iA = K.one) (hB : K.mul (castN K (a + 1)) iB = K.one) :
    Le K O.toCone (K.mul (castN K c) (K.add (p2 K iA) (K.neg (p2 K iB)))) (castN K 81) := by
  have pb := poly2 a c ha hc
  have ⟨w, hw⟩ := DyadicModelZ.le_dest pb
  have hcast := congrArg (castN K) hw
  have cB : castN K (a + 1) = K.add (castN K a) K.one := by show K.add K.one (castN K a) = _; lib_ring
  have cS : castN K (a + (a + 1)) = K.add (castN K a) (K.add (castN K a) K.one) := by
    rw [RatModel.cast_add K a (a + 1), cB]
  rw [RatModel.cast_add, castN_mul K c, castN_mul K (81 * (a * a)), castN_mul K 81, castN_mul K a a,
    castN_mul K (a + 1) (a + 1), cS, cB] at hcast
  rw [cB] at hB
  generalize castN K a = A at hA hB hcast
  exact step2_le K O.toCone A (K.add A K.one) iA iB (castN K c) (castN K w) hA hB rfl (RatModel.nn_cast K O.toCone w)
    (by unfold p2; rw [hcast]; lib_ring)

include mul_inv h10 in
theorem shell2_term (L : List Nat) (i : Nat)
    (hc : cnt L (i + 1) ≤ (2 * (i + 1) + 1) * ((2 * (i + 1) + 1) * (2 * (i + 1) + 1))) :
    Le K O.toCone (K.mul (castN K (cnt L (i + 1))) (gd K (f2 K inv) (i + 1))) (castN K 81) := by
  have e2 : 2 * (i + 1) + 1 = (i + 1) + ((i + 1) + 1) := by lib_nat
  rw [e2] at hc
  exact shell2_core K O (i + 1) (Nat.succ_le_succ (Nat.zero_le i)) _ hc _ _
    (iv_mul K O inv mul_inv h10 i) (iv_mul K O inv mul_inv h10 (i + 1))

theorem sum_const_le (f : Nat → R) (C : R) (h : ∀ j, Le K O.toCone (f j) C) : ∀ n,
    Le K O.toCone (sumN K f n) (K.mul (castN K n) C)
  | 0 => ⟨K.zero, O.nn_zero, by show K.add K.zero K.zero = K.mul K.zero C; lib_ring⟩
  | n + 1 => by
    have ih := sum_const_le f C h n
    have s := le_add' K O.toCone _ _ _ _ ih (h n)
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (castN K n) C) C) K.zero = K.mul (K.add K.one (castN K n)) C
    lib_ring

include mul_inv h10 in
theorem shell2_sum (L : List Nat) (h1 : ∀ n, n ∈ L → 1 ≤ n)
    (hc : ∀ j, cnt L j ≤ (2 * j + 1) * ((2 * j + 1) * (2 * j + 1))) (M : Nat) :
    Le K O.toCone (sumN K (fun j => K.mul (castN K (cnt L j)) (gd K (f2 K inv) j)) M)
      (K.mul (castN K M) (castN K 81)) := by
  apply sum_const_le K O
  intro j
  cases j with
  | zero =>
    rw [cnt_zero L h1]
    refine ⟨castN K 81, RatModel.nn_cast K O.toCone 81, ?_⟩
    show K.add (K.mul K.zero (gd K (f2 K inv) 0)) (castN K 81) = castN K 81
    lib_ring
  | succ i => exact shell2_term K O inv mul_inv h10 L i (hc (i + 1))

end Gen

end LatticeSum2

#print axioms LatticeSum2.poly2
#print axioms LatticeSum2.shell2_core
#print axioms LatticeSum2.shell2_term
#print axioms LatticeSum2.sum_const_le
#print axioms LatticeSum2.shell2_sum

#print axioms: 5 theorems, no axioms.

79.282 The low Agmon weight

For every duplicate-free list of nonzero modes with \(|k|_\infty \le M+1\): \(\sum _k|k|_\infty ^{-2}\le 108(M+1)\).

ns/LatticeBound2.lean

/-
  ns/LatticeBound2.lean — T37 cycle 1956 (goal G859c2b1, piece c): the low Agmon weight. Over an ordered field, for
  every duplicate-free list l of nonzero modes k ∈ ℤ³ with |k|_∞ ≤ M + 1:
    last_term2      |l|·(M+1)^{−2} ≤ 27·(M+1)        (|l| ≤ (2M+3)³ ≤ 27(M+1)³)
    lattice_bound2  Σ_{k∈l} |k|_∞^{−2} ≤ 108·(M+1)
  (Abel identity AbelCount.abel_list, shell sum LatticeSum2.shell2_sum ≤ 81(M+1), last term ≤ 27(M+1)). This is the
  factor Σ_p ν_p² of ForceSob on the low shells of the Agmon cut, linear in the cut.

  Lean 4 core plus ns/LatticeBound, ns/LatticeSum2.
-/
import ns.LatticeBound
import ns.LatticeSum2

namespace LatticeBound2
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound LatticeStep2 LatticeSum2

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem last_term2 (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f2 K inv (M + 1))) (K.mul (castN K 27) (castN K (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f2e : f2 K inv (M + 1) = p2 K (iv K inv (M + 1)) := rfl
  rw [f2e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0 ⊢
  have p2n : O.nn (p2 K iA) := O.nn_sq _
  have s1 := le_mul_nn K O.toCone _ _ _ p2n m0
  rw [K.mul_comm (p2 K iA), K.mul_comm (p2 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p2 K iA) = K.mul (castN K 27) A := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p2 K iA)
        = K.mul (K.mul (castN K 27) A) (K.mul (K.mul A iA) (K.mul A iA)) := by unfold p2; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
/-- Σ_{k∈l} |k|_∞^{−2} ≤ 108·(M+1) for every duplicate-free list of nonzero modes with |k|_∞ ≤ M + 1 -/
theorem lattice_bound2 (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l)
    (h1 : ∀ k, k ∈ l → 1 ≤ nrm k) (M : Nat) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f2 K inv) (norms l)) (K.mul (castN K 108) (castN K (M + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f2 K inv) (M + 1) (norms l) hb]
  have S := shell2_sum K O inv mul_inv h10 (norms l) h1' (fun j => cnt_cube l hd j) (M + 1)
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term2 K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ S T
  refine le_trans' K O.toCone _ _ _ U ⟨K.zero, O.nn_zero, ?_⟩
  rw [show castN K 108 = K.add (castN K 81) (castN K 27) from RatModel.cast_add K 81 27]
  lib_ring

end Gen

end LatticeBound2

#print axioms LatticeBound2.last_term2
#print axioms LatticeBound2.lattice_bound2

#print axioms: 2 theorems, no axioms.

79.283 The high Agmon weight

For every duplicate-free list of modes with \(K+1\le |k|_\infty \le M+1\): \(\sum _k|k|_\infty ^{-4}\le 108/(K+1)\).

ns/LatticeTail4.lean

/-
  ns/LatticeTail4.lean — T37 cycle 1956 (goal G859c2b1, piece d): the high Agmon weight. Over an ordered field, for
  every duplicate-free list l of modes with K + 1 ≤ |k|_∞ ≤ M + 1 (K ≤ M):
    lattice_tail4   Σ_{k∈l} |k|_∞^{−4} ≤ 108·(K+1)^{−1}
  Abel identity; the shells j ≤ K are empty (cnt_below), the shells j > K telescope against v j = (max(j, K+1))^{−1}
  (LatticeSum.shell_term), and the last term is |l|·(M+1)^{−4} ≤ 27·(M+1)^{−1} (last_term').

  Lean 4 core plus ns/LatticeBound.
-/
import ns.LatticeBound

namespace LatticeTail4
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge AbelCount LatticeStep LatticeSum ModeCount CubeCount
  LatticeBound

theorem ble_of_le : ∀ a b : Nat, a ≤ b → Nat.ble a b = true
  | 0, _, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | a + 1, b + 1, h => ble_of_le a b (Nat.le_of_succ_le_succ h)

theorem cnt_below (j : Nat) : ∀ (L : List Nat), (∀ n, n ∈ L → j < n) → cnt L j = 0
  | [], _ => rfl
  | n :: L, h => by
    have hb : Nat.ble n j = false := AbelCount.ble_false (h n (List.Mem.head _))
    show cond (Nat.ble n j) (cnt L j + 1) (cnt L j) = 0
    rw [hb]; exact cnt_below j L (fun x hx => h x (List.Mem.tail _ hx))

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def v (Kc : Nat) (j : Nat) : R := cond (Nat.ble j Kc) (iv K inv (Kc + 1)) (iv K inv j)

theorem v_low (Kc j : Nat) (h : j ≤ Kc) : v K inv Kc j = iv K inv (Kc + 1) := by
  unfold v; rw [ble_of_le j Kc h]; rfl

theorem v_high (Kc j : Nat) (h : Kc < j) : v K inv Kc j = iv K inv j := by
  unfold v; rw [AbelCount.ble_false h]; rfl

include mul_inv h10 in
theorem last_term' (M c : Nat) (hc : c ≤ (2 * (M + 1) + 1) * ((2 * (M + 1) + 1) * (2 * (M + 1) + 1))) :
    Le K O.toCone (K.mul (castN K c) (f4 K inv (M + 1))) (K.mul (castN K 27) (iv K inv (M + 1))) := by
  have hc27 := Nat.le_trans hc (cube27 (M + 1) (Nat.succ_le_succ (Nat.zero_le M)))
  have m0 := RatModel.cast_mono K O.toCone _ _ hc27
  have hA := iv_mul K O inv mul_inv h10 M
  have cR : castN K (27 * ((M + 1) * (M + 1) * (M + 1)))
      = K.mul (castN K 27) (K.mul (K.mul (castN K (M + 1)) (castN K (M + 1))) (castN K (M + 1))) := by
    rw [castN_mul K 27, castN_mul K ((M + 1) * (M + 1)), castN_mul K (M + 1) (M + 1)]
  rw [cR] at m0
  have f4e : f4 K inv (M + 1) = p4 K (iv K inv (M + 1)) := rfl
  rw [f4e]
  generalize iv K inv (M + 1) = iA at hA ⊢
  generalize castN K (M + 1) = A at hA m0 ⊢
  have p4n : O.nn (p4 K iA) := by unfold p4; exact O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  have s1 := le_mul_nn K O.toCone _ _ _ p4n m0
  rw [K.mul_comm (p4 K iA), K.mul_comm (p4 K iA)] at s1
  have e : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA) = K.mul (castN K 27) iA := by
    have : K.mul (K.mul (castN K 27) (K.mul (K.mul A A) A)) (p4 K iA)
        = K.mul (K.mul (castN K 27) (K.mul (K.mul (K.mul A iA) (K.mul A iA)) (K.mul A iA))) iA := by unfold p4; lib_ring
    rw [this, hA]; lib_ring
  rw [e] at s1
  exact s1

include mul_inv h10 in
theorem lattice_tail4 (l : List Mode) (hd : List.Pairwise (fun a b => a ≠ b) l) (Kc M : Nat) (hKM : Kc ≤ M)
    (h1 : ∀ k, k ∈ l → Kc + 1 ≤ nrm k) (hM : ∀ k, k ∈ l → nrm k ≤ M + 1) :
    Le K O.toCone (lsumN K (f4 K inv) (norms l)) (K.mul (castN K 108) (iv K inv (Kc + 1))) := by
  have hb : ∀ n, n ∈ norms l → n ≤ M + 1 := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact hM k hk
  have h1' : ∀ n, n ∈ norms l → Kc + 1 ≤ n := fun n hn => by
    have ⟨k, hk, e⟩ := mem_norms n l hn
    rw [e]; exact h1 k hk
  rw [abel_list K (f4 K inv) (M + 1) (norms l) hb]
  have term : ∀ j, Le K O.toCone (K.mul (castN K (cnt (norms l) j)) (gd K (f4 K inv) j))
      (K.mul (castN K 108) (K.add (v K inv Kc j) (K.neg (v K inv Kc (j + 1))))) := by
    intro j
    by_cases hj : j ≤ Kc
    · rw [cnt_below j (norms l) (fun n hn => Nat.lt_of_lt_of_le (Nat.lt_succ_of_le hj) (h1' n hn)),
        v_low K inv Kc j hj]
      have e : v K inv Kc (j + 1) = iv K inv (Kc + 1) := by
        by_cases hj' : j + 1 ≤ Kc
        · exact v_low K inv Kc (j + 1) hj'
        · have e1 : j + 1 = Kc + 1 := Nat.le_antisymm (Nat.succ_le_succ hj) (Nat.gt_of_not_le hj')
          rw [e1]; exact v_high K inv Kc (Kc + 1) (Nat.lt_succ_self Kc)
      rw [e]
      refine ⟨K.zero, O.nn_zero, ?_⟩
      show K.add (K.mul K.zero (gd K (f4 K inv) j)) K.zero
        = K.mul (castN K 108) (K.add (iv K inv (Kc + 1)) (K.neg (iv K inv (Kc + 1))))
      lib_ring
    · have hlt : Kc < j := Nat.gt_of_not_le hj
      cases j with
      | zero => exact absurd hlt (Nat.not_lt_zero Kc)
      | succ i =>
        rw [v_high K inv Kc (i + 1) hlt, v_high K inv Kc (i + 1 + 1) (Nat.lt_succ_of_lt hlt)]
        exact shell_term K O inv mul_inv h10 (norms l) i (cnt_cube l hd (i + 1))
  have hs := sum_le K O.toCone _ _ term (M + 1)
  rw [sumN_mul_left, telescope] at hs
  have hlen : (norms l).length = cnt (norms l) (M + 1) := (cnt_all (M + 1) (norms l) hb).symm
  have T := last_term' K O inv mul_inv h10 M (norms l).length (by rw [hlen]; exact cnt_cube l hd (M + 1))
  have U := le_add' K O.toCone _ _ _ _ hs T
  rw [v_low K inv Kc 0 (Nat.zero_le Kc), v_high K inv Kc (M + 1) (Nat.lt_succ_of_le hKM)] at U
  refine le_trans' K O.toCone _ _ _ U ⟨K.mul (castN K 81) (iv K inv (M + 1)),
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 81) (iv_nn K O inv mul_inv h10 M), ?_⟩
  rw [show castN K 108 = K.add (castN K 81) (castN K 27) from RatModel.cast_add K 81 27]
  lib_ring

end Gen

end LatticeTail4

#print axioms LatticeTail4.ble_of_le
#print axioms LatticeTail4.cnt_below
#print axioms LatticeTail4.v_low
#print axioms LatticeTail4.v_high
#print axioms LatticeTail4.last_term'
#print axioms LatticeTail4.lattice_tail4

#print axioms: 6 theorems, no axioms.

79.284 The Galerkin force with the Agmon cut

For integer modes with \(|k|_\infty \le M+1\) and a cut \(K_c+1\le M\): \(\sum _r|N_r(u,v)|^2\le \iota ^2(Z(u)+A(u)/(K_c+2)^2)(108(K_c+1)+108(K_c+2))Z(v)\).

ns/ForceAgmon.lean

/-
  ns/ForceAgmon.lean — T37 cycle 1958 (goal G859c2b1, piece e): the Galerkin force bound with the Agmon cut, uniform
  in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ M + 1) and a cut
  Kc + 1 ≤ M, with the weights μ_p = |k_p|_∞ (|k_p|_∞ ≤ Kc + 1) and μ_p = |k_p|_∞²/(Kc + 2) (above), ν_p = 1/μ_p:
    force_agmon   Σ_r |N_r(u,v)|² ≤ ι²·(Z(u) + A(u)/(Kc+2)²)·((108(Kc+1) + 108(Kc+2))·Z(v)),
  Z = Σ w|·|², A = Σ w²|·|² (w = |k|²). With (Kc + 2)² ≈ A/Z this is the discrete ‖(u·∇)v‖² ≲ ‖∇u‖‖Δu‖‖∇v‖²
  (Agmon), the force bound that lets the H² step close on large data (G859c2).
  ForceSob.total_sob with the cut μ; Σν² from LatticeBound2 (low) and LatticeTail4 (high).

  Lean 4 core plus ns/ForceH2, ns/LatticeBound2, ns/LatticeTail4, ns/WeightedCS.
-/
import ns.ForceH2
import ns.LatticeBound2
import ns.LatticeTail4
import ns.WeightedCS

namespace ForceAgmon
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4

theorem ble_le : ∀ a b : Nat, Nat.ble a b = true → a ≤ b
  | 0, _, _ => Nat.zero_le _
  | _ + 1, 0, h => nomatch h
  | a + 1, b + 1, h => Nat.succ_le_succ (ble_le a b h)

theorem gt_of_ble_false : ∀ a b : Nat, Nat.ble a b = false → b < a
  | 0, _, h => nomatch h
  | a + 1, 0, _ => Nat.succ_pos a
  | a + 1, b + 1, h => Nat.succ_lt_succ (gt_of_ble_false a b h)

section Lists
variable {I : Type}

theorem mem_filter_inv (q : I → Bool) (x : I) : ∀ l : List I, x ∈ l.filter q → q x = true ∧ x ∈ l
  | [], h => nomatch h
  | y :: l, h => by
    cases hy : q y with
    | true =>
      have h' : x ∈ y :: l.filter q := by
        have e : (y :: l).filter q = y :: l.filter q := by
          show (match q y with | true => y :: l.filter q | false => l.filter q) = _
          rw [hy]
        rw [e] at h; exact h
      cases h' with
      | head => exact ⟨hy, List.Mem.head _⟩
      | tail _ h2 => have ⟨a, b⟩ := mem_filter_inv q x l h2; exact ⟨a, List.Mem.tail _ b⟩
    | false =>
      have h' : x ∈ l.filter q := by
        have e : (y :: l).filter q = l.filter q := by
          show (match q y with | true => y :: l.filter q | false => l.filter q) = _
          rw [hy]
        rw [e] at h; exact h
      have ⟨a, b⟩ := mem_filter_inv q x l h'
      exact ⟨a, List.Mem.tail _ b⟩

theorem pw_filter (Rl : I → I → Prop) (q : I → Bool) : ∀ l : List I, List.Pairwise Rl l → List.Pairwise Rl (l.filter q)
  | [], _ => List.Pairwise.nil
  | x :: l, h => by
    cases h with
    | cons hx hl =>
      cases hq : q x with
      | true =>
        have e : (x :: l).filter q = x :: l.filter q := by
          show (match q x with | true => x :: l.filter q | false => l.filter q) = _
          rw [hq]
        rw [e]
        exact List.Pairwise.cons (fun y hy => hx y (mem_filter_inv q y l hy).2) (pw_filter Rl q l hl)
      | false =>
        have e : (x :: l).filter q = l.filter q := by
          show (match q x with | true => x :: l.filter q | false => l.filter q) = _
          rw [hq]
        rw [e]
        exact pw_filter Rl q l hl

end Lists

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_split (q : I → Bool) (f : I → R) : ∀ l : List I,
    lsum K f l = K.add (lsum K f (l.filter q)) (lsum K f (l.filter (fun p => !(q p))))
  | [] => by show K.zero = K.add K.zero K.zero; lib_ring
  | x :: l => by
    have ih := lsum_split q f l
    cases hq : q x with
    | true =>
      have e1 : (x :: l).filter q = x :: l.filter q := by
        show (match q x with | true => x :: l.filter q | false => l.filter q) = _
        rw [hq]
      have e2 : (x :: l).filter (fun p => !(q p)) = l.filter (fun p => !(q p)) := by
        show (match !(q x) with | true => x :: l.filter (fun p => !(q p)) | false => l.filter (fun p => !(q p))) = _
        rw [hq]; rfl
      rw [e1, e2]
      show K.add (f x) (lsum K f l) = K.add (K.add (f x) (lsum K f (l.filter q))) (lsum K f (l.filter (fun p => !(q p))))
      rw [ih]; lib_ring
    | false =>
      have e1 : (x :: l).filter q = l.filter q := by
        show (match q x with | true => x :: l.filter q | false => l.filter q) = _
        rw [hq]
      have e2 : (x :: l).filter (fun p => !(q p)) = x :: l.filter (fun p => !(q p)) := by
        show (match !(q x) with | true => x :: l.filter (fun p => !(q p)) | false => l.filter (fun p => !(q p))) = _
        rw [hq]; rfl
      rw [e1, e2]
      show K.add (f x) (lsum K f l) = K.add (lsum K f (l.filter q)) (K.add (f x) (lsum K f (l.filter (fun p => !(q p)))))
      rw [ih]; lib_ring

theorem lsum_congr_mem (f g : I → R) : ∀ l : List I, (∀ x, x ∈ l → f x = g x) → lsum K f l = lsum K g l
  | [], _ => rfl
  | x :: l, h => by
    show K.add (f x) (lsum K f l) = K.add (g x) (lsum K g l)
    rw [h x (List.Mem.head _), lsum_congr_mem f g l (fun y hy => h y (List.Mem.tail _ hy))]

/-- the low/high predicate of the cut -/
def low (kz : I → Mode) (Kc : Nat) (p : I) : Bool := Nat.ble (nrm (kz p)) (Kc + 1)

def mu (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (nn K kz p) (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))

def nu (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (ivn K inv kz p) (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))

include O mul_inv h10 in
theorem munu (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc : Nat) (p : I) :
    K.mul (mu K inv kz Kc p) (nu K inv kz Kc p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold mu nu
  cases low kz Kc p with
  | true => exact e
  | false =>
    have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
    show K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
      (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2))) = K.one
    calc _ = K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
          (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
      _ = K.one := by rw [e, ec]; lib_ring

/-- the u-side weight: μ²|u|² ≤ w|u|² + iv(Kc+2)²·w²|u|² -/
theorem mu_le (kz : I → Mode) (Kc : Nat) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu K inv kz Kc p) (mu K inv kz Kc p)) (dot3 K (u p) (u p)))
      (K.add (K.mul (wt K kz p) (dot3 K (u p) (u p)))
        (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))))) := by
  have hw := nrm_le_dot K O.toCone (kz p)
  have hu : O.nn (dot3 K (u p) (u p)) := dot3_nn K O.toCone _
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have hn2 : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have nnT : O.nn (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p)))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ (O.nn_sq _) hu)
  unfold mu
  cases low kz Kc p with
  | true =>
    have s := le_mul_right' K O.toCone hw hu
    refine le_trans' K O.toCone _ _ _ s ⟨_, nnT, rfl⟩
  | false =>
    have s4 := EnstLocal.sq_mono K O _ _ hn2 hwn hw
    have s := le_mul_right' K O.toCone s4 (O.nn_mul _ _ (O.nn_sq (iv K inv (Kc + 2))) hu)
    have e1 : K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
        (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))) (dot3 K (u p) (u p))
        = K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (dot3 K (u p) (u p))) := by lib_ring
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))
        (K.mul (K.mul (nn K kz p) (nn K kz p)) (iv K inv (Kc + 2)))) (dot3 K (u p) (u p))) _
    rw [e1]
    refine le_trans' K O.toCone _ _ _ s ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), O.nn_mul _ _ hwn hu, ?_⟩
    lib_ring

include mul_inv h10 in
/-- Σν² ≤ 108(Kc+1) + 108(Kc+2) -/
theorem nu_sum (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) l)
      (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
  rw [lsum_split K (low kz Kc)]
  -- low part
  have eL : lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) (l.filter (low kz Kc))
      = lsum K (fun p => f2 K inv (nrm (kz p))) (l.filter (low kz Kc)) := by
    apply lsum_congr_mem
    intro p hp
    have hq := (mem_filter_inv _ p l hp).1
    unfold nu; rw [hq]; rfl
  have eH : lsum K (fun p => K.mul (nu K inv kz Kc p) (nu K inv kz Kc p)) (l.filter (fun p => !(low kz Kc p)))
      = K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (lsum K (fun p => f4 K inv (nrm (kz p))) (l.filter (fun p => !(low kz Kc p)))) := by
    rw [← lsum_mul_left]
    apply lsum_congr_mem
    intro p hp
    have hq := (mem_filter_inv _ p l hp).1
    have hq' : low kz Kc p = false := by
      cases e : low kz Kc p with
      | false => rfl
      | true => rw [e] at hq; exact nomatch hq
    unfold nu; rw [hq']
    show K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (castN K (Kc + 2)))
      = K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (p4 K (iv K inv (nrm (kz p))))
    unfold p4 ivn iv; lib_ring
  rw [eL, eH, lsum_norms K (f2 K inv) kz, lsum_norms K (f4 K inv) kz]
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact ble_le _ _ (mem_filter_inv _ q l hq).1)
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]
      have hq1 := (mem_filter_inv _ q l hq).1
      have hq' : low kz Kc q = false := by
        cases e2 : low kz Kc q with
        | false => rfl
        | true => rw [e2] at hq1; exact nomatch hq1
      exact gt_of_ble_false _ _ hq')
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have c2 : O.nn (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) := O.nn_sq _
  have L2' := le_mul_nn K O.toCone _ _ _ c2 L2
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e3 : K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 108) (castN K (Kc + 2)) := by
    calc _ = K.mul (K.mul (castN K 108) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by
          lib_ring
      _ = _ := by rw [ec]; lib_ring
  have L2e : Le K O.toCone (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2'
  rw [e3] at L2e
  exact le_add' K O.toCone _ _ _ _ L1 L2e

include mul_inv h10 in
/-- **The force bound with the Agmon cut.** -/
theorem force_agmon (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) (Kc M : Nat) (hKM : Kc + 1 ≤ M)
    (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)) l)
      (K.mul (K.mul ι ι)
        (K.mul (K.add (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
            (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))
              (lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (u p) (u p))) l)))
          (K.mul (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))) := by
  have T := total_sob K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v (mu K inv kz Kc) (nu K inv kz Kc)
    (delta01 K kz) (munu K O inv mul_inv h10 kz h1 Kc)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => mu_le K O inv kz Kc u p) l
  rw [lsum_add, lsum_mul_left] at hH
  have hN := nu_sum K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have hZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hSZ := le_mul_right' K O.toCone hN hZv
  have hHn : O.nn (lsum K (fun p => K.mul (K.mul (mu K inv kz Kc p) (mu K inv kz Kc p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hS'n : O.nn (K.mul (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
      (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)) :=
    O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone _))
      (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone _))) hZv
  have hB := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hHn hS'n hH hSZ
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) hB

end Gen

end ForceAgmon

#print axioms ForceAgmon.ble_le
#print axioms ForceAgmon.gt_of_ble_false
#print axioms ForceAgmon.mem_filter_inv
#print axioms ForceAgmon.pw_filter
#print axioms ForceAgmon.lsum_split
#print axioms ForceAgmon.lsum_congr_mem
#print axioms ForceAgmon.munu
#print axioms ForceAgmon.mu_le
#print axioms ForceAgmon.nu_sum
#print axioms ForceAgmon.force_agmon

#print axioms: 10 theorems, no axioms.

79.285 The weighted force with the cubic cut

With \(\mu =|k|_\infty \) below the cut and \(|k|_\infty ^3/(K_c+2)^2\) above: \(\sum _r w_r|N_r(u,v)|^2\le \iota ^2(Z(u)+B(u)/(K_c+2)^4)(2S_1Z(v)+2S_0A(v))\).

ns/ForceAgmonW.lean

/-
  ns/ForceAgmonW.lean — T37 (goal G859c2b2, piece a): the H¹-weighted Galerkin force bound with a cubic cut,
  uniform in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ M + 1) and a
  cut Kc + 1 ≤ M, with μ_p = |k_p|_∞ (|k_p|_∞ ≤ Kc + 1), μ_p = |k_p|_∞³/(Kc+2)² (above), ν_p = 1/μ_p:
    force_agmonW   Σ_r w_r|N_r(u,v)|² ≤ ι²·(Z(u) + B(u)/(Kc+2)⁴)·(2·S₁·Z(v) + 2·S₀·A(v)),
      S₁ = 3·(108(Kc+1)³ + 108(Kc+2)³),  S₀ = 108(Kc+1) + 108(Kc+2),
  Z = Σ w|·|², A = Σ w²|·|², B = Σ w³|·|² (w = |k|²). With (Kc+2)⁴ ≈ B/Z the right side is
  ≲ Z^{5/4}B^{3/4} + Z^{3/4}·A·B^{1/4}, the weighted force bound for the uniform H² step on large data (G859c2c).
  ForceSobW.total_sobW with the cut; Σν² and Σν²w from LatticeBound2 (low) and LatticeTail4 (high).

  Lean 4 core plus ns/ForceAgmon.
-/
import ns.ForceAgmon

namespace ForceAgmonW
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4 ForceAgmon

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem lsum_le_on (f g : I → R) : ∀ l : List I, (∀ p, p ∈ l → Le K O.toCone (f p) (g p)) →
    Le K O.toCone (lsum K f l) (lsum K g l)
  | [], _ => le_refl' K O.toCone _
  | x :: l, h => le_add' K O.toCone _ _ _ _ (h x (List.Mem.head _)) (lsum_le_on f g l (fun p hp => h p (List.Mem.tail _ hp)))

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

def mu3 (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (nn K kz p)
    (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))

def nu3 (kz : I → Mode) (Kc : Nat) (p : I) : R :=
  cond (low kz Kc p) (ivn K inv kz p)
    (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))

include O mul_inv h10 in
theorem munu3c (kz : I → Mode) (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc : Nat) (p : I) :
    K.mul (mu3 K inv kz Kc p) (nu3 K inv kz Kc p) = K.one := by
  have e := nn_ivn K O inv mul_inv h10 kz h1 p
  unfold mu3 nu3
  cases low kz Kc p with
  | true => exact e
  | false =>
    have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
    show K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
      (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
      = K.one
    calc _ = K.mul (K.mul (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p)))
            (K.mul (nn K kz p) (ivn K inv kz p)))
          (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)))) := by
          lib_ring
      _ = K.one := by rw [e, ec]; lib_ring

/-- the u-side weight: μ²|u|² ≤ w|u|² + iv(Kc+2)⁴·w³|u|² -/
theorem mu3_le (kz : I → Mode) (Kc : Nat) (u : I → V3 R) (p : I) :
    Le K O.toCone (K.mul (K.mul (mu3 K inv kz Kc p) (mu3 K inv kz Kc p)) (dot3 K (u p) (u p)))
      (K.add (K.mul (wt K kz p) (dot3 K (u p) (u p)))
        (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
          (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))))) := by
  have hw : Le K O.toCone (K.mul (nn K kz p) (nn K kz p)) (wt K kz p) := nrm_le_dot K O.toCone (kz p)
  have hu : O.nn (dot3 K (u p) (u p)) := dot3_nn K O.toCone _
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have hn2 : O.nn (K.mul (nn K kz p) (nn K kz p)) := O.nn_sq _
  have nnT : O.nn (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
      (K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p)))) :=
    O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) hwn) hu)
  unfold mu3
  cases low kz Kc p with
  | true =>
    have s := le_mul_right' K O.toCone hw hu
    exact le_trans' K O.toCone _ _ _ s ⟨_, nnT, rfl⟩
  | false =>
    have s4 := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hn2 hwn hw hw
    have s6 := WeightedCS.le_mul_both' K O.toCone _ _ _ _ (O.nn_mul _ _ hn2 hn2) hwn s4 hw
    have s := le_mul_right' K O.toCone s6
      (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq (iv K inv (Kc + 2))) (O.nn_sq (iv K inv (Kc + 2)))) hu)
    have e1 : K.mul (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
        (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (dot3 K (u p) (u p))
        = K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (K.mul (nn K kz p) (nn K kz p))) (K.mul (nn K kz p) (nn K kz p)))
          (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
            (dot3 K (u p) (u p))) := by lib_ring
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
        (K.mul (K.mul (K.mul (nn K kz p) (nn K kz p)) (nn K kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (dot3 K (u p) (u p))) _
    rw [e1]
    refine le_trans' K O.toCone _ _ _ s ⟨K.mul (wt K kz p) (dot3 K (u p) (u p)), O.nn_mul _ _ hwn hu, ?_⟩
    lib_ring

include mul_inv h10 in
/-- above the cut, 1/|k|_∞² ≤ 1/(Kc+2)² -/
theorem ivn_sq_le (kz : I → Mode) (Kc : Nat) (p : I) (h : Kc + 2 ≤ nrm (kz p)) :
    Le K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) := by
  have e : ivn K inv kz p = iv K inv (nrm (kz p)) := rfl
  rw [e]
  cases hn : nrm (kz p) with
  | zero => rw [hn] at h; exact absurd h (Nat.not_succ_le_zero _)
  | succ b =>
    rw [hn] at h
    have hb : Kc + 1 ≤ b := Nat.le_of_succ_le_succ h
    have a := LatticeTail.iv_anti K O inv mul_inv h10 (Kc + 1) b hb
    exact EnstLocal.sq_mono K O _ _ (iv_nn K O inv mul_inv h10 b) (iv_nn K O inv mul_inv h10 (Kc + 1)) a

theorem low_true (kz : I → Mode) (Kc : Nat) (l : List I) (p : I) (hp : p ∈ l.filter (low kz Kc)) :
    low kz Kc p = true := (mem_filter_inv _ p l hp).1

theorem low_false (kz : I → Mode) (Kc : Nat) (l : List I) (p : I) (hp : p ∈ l.filter (fun p => !(low kz Kc p))) :
    low kz Kc p = false := by
  have hq1 := (mem_filter_inv _ p l hp).1
  cases e2 : low kz Kc p with
  | false => rfl
  | true => rw [e2] at hq1; exact nomatch hq1

theorem low_nrm (kz : I → Mode) (Kc : Nat) (p : I) (h : low kz Kc p = true) : nrm (kz p) ≤ Kc + 1 :=
  ble_le _ _ h

theorem high_nrm (kz : I → Mode) (Kc : Nat) (p : I) (h : low kz Kc p = false) : Kc + 2 ≤ nrm (kz p) :=
  gt_of_ble_false _ _ h

include mul_inv h10 in
/-- Σν² ≤ 108(Kc+1) + 108(Kc+2) for the cubic cut -/
theorem nu3_sum0 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) l)
      (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
  rw [lsum_split K (low kz Kc)]
  have eL : lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (l.filter (low kz Kc))
      = lsum K (fun p => f2 K inv (nrm (kz p))) (l.filter (low kz Kc)) := by
    apply lsum_congr_mem
    intro p hp
    unfold nu3; rw [low_true kz Kc l p hp]; rfl
  have c4 : O.nn (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) :=
    O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have hH : Le K O.toCone
      (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (l.filter (fun p => !(low kz Kc p))))
      (lsum K (fun p => K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (f4 K inv (nrm (kz p)))) (l.filter (fun p => !(low kz Kc p)))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_false kz Kc l p hp
    have hiv := ivn_sq_le K O inv mul_inv h10 kz Kc p (high_nrm kz Kc p hl)
    have cn : O.nn (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))) :=
      O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
    have m := le_mul_nn K O.toCone _ _ _ cn hiv
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
      (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
    have e1 : K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        = K.mul (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
          (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (ivn K inv kz p) (ivn K inv kz p)) := by lib_ring
    have e2 : K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
        = K.mul (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
          (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
    rw [e1, e2]; exact m
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz] at hH
  rw [eL, lsum_norms K (f2 K inv) kz]
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact low_nrm kz Kc q (low_true kz Kc l q hq))
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact high_nrm kz Kc q (low_false kz Kc l q hq))
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have L2'' := le_mul_nn K O.toCone _ _ _ c4 L2
  have L2' : Le K O.toCone (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2''
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e3 : K.mul (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
        (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 108) (castN K (Kc + 2)) := by
    calc _ = K.mul (K.mul (castN K 108) (castN K (Kc + 2)))
          (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)))
            (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))))) := by lib_ring
      _ = _ := by rw [ec]; lib_ring
  rw [e3] at L2'
  exact le_add' K O.toCone _ _ _ _ L1 (le_trans' K O.toCone _ _ _ hH L2')

include mul_inv h10 in
/-- Σν²w ≤ 3·(108(Kc+1)³ + 108(Kc+2)³) for the cubic cut -/
theorem nu3_sum1 (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Kc M : Nat) (hKM : Kc + 1 ≤ M) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) l)
      (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
        (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))))) := by
  have e3c : castN K 3 = K.add K.one (K.add K.one (K.add K.one K.zero)) := rfl
  rw [lsum_split K (low kz Kc)]
  -- low part: ν²w ≤ 3(Kc+1)²·|k|_∞^{-2}
  have hL : Le K O.toCone
      (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) (l.filter (low kz Kc)))
      (lsum K (fun p => K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (f2 K inv (nrm (kz p))))
        (l.filter (low kz Kc))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_true kz Kc l p hp
    have c1 := RatModel.cast_mono K O.toCone _ _ (low_nrm kz Kc p hl)
    have s2 := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c1
    have w3 := dot_le_3nrm K O (kz p)
    have s3 := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s2 s2) s2
    have w3' := le_trans' K O.toCone _ _ _ w3 s3
    have m := le_mul_nn K O.toCone (K.mul (ivn K inv kz p) (ivn K inv kz p)) _ _ (O.nn_sq _) w3'
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (dot3 K (kvec K (kz p)) (kvec K (kz p))))
      (K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (K.mul (ivn K inv kz p) (ivn K inv kz p)))
    have e : K.mul (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) (K.mul (ivn K inv kz p) (ivn K inv kz p))
        = K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p))
          (K.add (K.add (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))))
            (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) := by rw [e3c]; lib_ring
    rw [e]; exact m
  -- high part: ν²w ≤ 3(Kc+2)⁴·|k|_∞^{-4}
  have hH : Le K O.toCone
      (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) (l.filter (fun p => !(low kz Kc p))))
      (lsum K (fun p => K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
          (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (f4 K inv (nrm (kz p)))) (l.filter (fun p => !(low kz Kc p)))) := by
    apply lsum_le_on K O
    intro p hp
    have hl := low_false kz Kc l p hp
    have e := nn_ivn K O inv mul_inv h10 kz h1 p
    have w3 := dot_le_3nrm K O (kz p)
    have cn : O.nn (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) :=
      O.nn_sq _
    have m := le_mul_nn K O.toCone _ _ _ cn w3
    unfold nu3; rw [hl]
    show Le K O.toCone (K.mul (K.mul (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p))
        (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))
        (K.mul (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (ivn K inv kz p)) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (dot3 K (kvec K (kz p)) (kvec K (kz p))))
      (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
    refine le_trans' K O.toCone _ _ _ m ?_
    apply le_eq K O
    calc _ = K.mul (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
            (K.mul (K.mul (ivn K inv kz p) (ivn K inv kz p)) (K.mul (ivn K inv kz p) (ivn K inv kz p))))
          (K.mul (K.mul (nn K kz p) (ivn K inv kz p)) (K.mul (nn K kz p) (ivn K inv kz p))) := by
          show K.mul _ (K.add (K.add (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))
              (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))) (K.mul (castN K (nrm (kz p))) (castN K (nrm (kz p))))) = _
          rw [e3c]; unfold nn; lib_ring
      _ = _ := by rw [e]; lib_ring
  rw [lsum_mul_left, lsum_norms K (f2 K inv) kz] at hL
  rw [lsum_mul_left, lsum_norms K (f4 K inv) kz] at hH
  have L1 := lattice_bound2 K O inv mul_inv h10 ((l.filter (low kz Kc)).map kz)
    (pw_map kz _ (pw_filter _ _ l hd))
    (fun k hk => by have ⟨q, _, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact h1 q) Kc
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact low_nrm kz Kc q (low_true kz Kc l q hq))
  have L2 := lattice_tail4 K O inv mul_inv h10 ((l.filter (fun p => !(low kz Kc p))).map kz)
    (pw_map kz _ (pw_filter _ _ l hd)) (Kc + 1) M hKM
    (fun k hk => by
      have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk
      rw [← e]; exact high_nrm kz Kc q (low_false kz Kc l q hq))
    (fun k hk => by have ⟨q, hq, e⟩ := mem_map_inv kz k _ hk; rw [← e]; exact hM q (mem_filter_inv _ q l hq).2)
  have n3 : O.nn (castN K 3) := RatModel.nn_cast K O.toCone 3
  have L1' := le_mul_nn K O.toCone (K.mul (castN K 3) (K.mul (castN K (Kc + 1)) (castN K (Kc + 1)))) _ _
    (O.nn_mul _ _ n3 (O.nn_sq _)) L1
  have L2'' := le_mul_nn K O.toCone (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) _ _
    (O.nn_mul _ _ n3 (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))) L2
  have L2' : Le K O.toCone (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (AbelCount.lsumN K (f4 K inv) (norms ((l.filter (fun p => !(low kz Kc p))).map kz))))
      (K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))
      (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))))) (K.mul (castN K 108) (iv K inv (Kc + 2)))) := L2''
  have ec : K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2)) = K.one := iv_mul K O inv mul_inv h10 (Kc + 1)
  have e4 : K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K (Kc + 2)) (castN K (Kc + 2)))))
        (K.mul (castN K 108) (iv K inv (Kc + 2)))
      = K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))) := by
    calc _ = K.mul (K.mul (castN K 3) (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2)))))
          (K.mul (castN K (Kc + 2)) (iv K inv (Kc + 2))) := by lib_ring
      _ = _ := by rw [ec]; lib_ring
  rw [e4] at L2'
  have T := le_add' K O.toCone _ _ _ _ (le_trans' K O.toCone _ _ _ hL L1') (le_trans' K O.toCone _ _ _ hH L2')
  refine le_trans' K O.toCone _ _ _ T ?_
  apply le_eq K O
  lib_ring

include mul_inv h10 in
/-- **The H¹-weighted force bound with the cubic cut.** -/
theorem force_agmonW (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (u v : I → V3 R) (Kc M : Nat) (hKM : Kc + 1 ≤ M)
    (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) u v r))) l)
      (K.mul (K.mul ι ι)
        (K.mul (K.add (lsum K (fun p => K.mul (wt K kz p) (dot3 K (u p) (u p))) l)
            (K.mul (K.mul (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))) (K.mul (iv K inv (Kc + 2)) (iv K inv (Kc + 2))))
              (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (u p) (u p))) l)))
          (K.add
            (K.mul (K.add K.one K.one) (K.mul
              (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
                (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2))))))
              (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
            (K.mul (K.add K.one K.one) (K.mul
              (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
              (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))))) := by
  have T := total_sobW K O.toCone l (TriadSparse.delta K kz) ι (kv K kz) u v
    (mu3 K inv kz Kc) (nu3 K inv kz Kc) (wt K kz) (fun r => dot3_nn K O.toCone _)
    (fun p q r => delta_tri K O.toCone kz p q r) (delta01 K kz) (munu3c K O inv mul_inv h10 kz h1 Kc)
    (fun p r => TriadSparse.spA K O.toCone kz l hd p r) (fun p q => TriadSparse.spB K O.toCone kz l hd p q)
  refine le_trans' K O.toCone _ _ _ T ?_
  have hH := lsum_le K O.toCone _ _ (fun p => mu3_le K O inv kz Kc u p) l
  rw [lsum_add, lsum_mul_left] at hH
  have e : lsum K (fun p => lsum K (fun q => K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (dot3 K (kv K kz q) (kv K kz q)) (dot3 K (v q) (v q)))) l) l
      = K.add (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p)) l)
            (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
          (K.mul (K.add K.one K.one) (K.mul (lsum K (fun p => K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) l)
            (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l))) := by
    rw [← double_factor, ← double_factor, ← lsum_add]
    apply lsum_ext; intro p
    rw [← lsum_add]
    apply lsum_ext; intro q
    show K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
        (K.add (K.add (wt K kz p) (wt K kz p)) (K.add (wt K kz q) (wt K kz q))))
        (K.mul (wt K kz q) (dot3 K (v q) (v q)))
      = K.add (K.mul (K.add K.one K.one) (K.mul (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p)) (wt K kz p))
          (K.mul (wt K kz q) (dot3 K (v q) (v q)))))
        (K.mul (K.add K.one K.one) (K.mul (K.mul (nu3 K inv kz Kc p) (nu3 K inv kz Kc p))
          (K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q)))))
    lib_ring
  rw [e]
  have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hZv : O.nn (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hAv : O.nn (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have S1 := nu3_sum1 K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have S0 := nu3_sum0 K O inv mul_inv h10 l kz hd h1 Kc M hKM hM
  have hD := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ two (le_mul_right' K O.toCone S1 hZv))
    (le_mul_nn K O.toCone _ _ _ two (le_mul_right' K O.toCone S0 hAv))
  have hHn : O.nn (lsum K (fun p => K.mul (K.mul (mu3 K inv kz Kc p) (mu3 K inv kz Kc p)) (dot3 K (u p) (u p))) l) :=
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have n108 := RatModel.nn_cast K O.toCone 108
  have nC := fun n => RatModel.nn_cast K O.toCone n
  have hDn : O.nn (K.add
      (K.mul (K.add K.one K.one) (K.mul
        (K.mul (castN K 3) (K.add (K.mul (K.mul (castN K (Kc + 1)) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 1))))
          (K.mul (K.mul (castN K (Kc + 2)) (castN K (Kc + 2))) (K.mul (castN K 108) (castN K (Kc + 2))))))
        (lsum K (fun q => K.mul (wt K kz q) (dot3 K (v q) (v q))) l)))
      (K.mul (K.add K.one K.one) (K.mul
        (K.add (K.mul (castN K 108) (castN K (Kc + 1))) (K.mul (castN K 108) (castN K (Kc + 2))))
        (lsum K (fun q => K.mul (K.mul (wt K kz q) (wt K kz q)) (dot3 K (v q) (v q))) l)))) :=
    O.nn_add _ _
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_mul _ _ (nC 3) (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 (nC _)))
        (O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 (nC _))))) hZv))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ n108 (nC _)) (O.nn_mul _ _ n108 (nC _))) hAv))
  have hB := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hHn hDn hH hD
  exact le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) hB

end Gen

end ForceAgmonW

#print axioms ForceAgmonW.lsum_le_on
#print axioms ForceAgmonW.le_eq
#print axioms ForceAgmonW.munu3c
#print axioms ForceAgmonW.mu3_le
#print axioms ForceAgmonW.ivn_sq_le
#print axioms ForceAgmonW.low_true
#print axioms ForceAgmonW.low_false
#print axioms ForceAgmonW.low_nrm
#print axioms ForceAgmonW.high_nrm
#print axioms ForceAgmonW.nu3_sum0
#print axioms ForceAgmonW.nu3_sum1
#print axioms ForceAgmonW.force_agmonW

#print axioms: 12 theorems, no axioms.

79.286 Choosing the cut

If \(B\le 9(M_m+2)^4Z\) there is \(K_c\le M_m\) with \(B\le 9(K_c+2)^4Z\) and (\(K_c=0\) or \(9(K_c+1)^4Z\le B\)).

ns/CutChoose.lean

/-
  ns/CutChoose.lean — T37 (goal G859c2b2, piece b): choosing the Agmon cut, over an ordered field. For Z, B and a
  largest admissible index Mm with B ≤ 9(Mm+2)⁴Z, there is a cut Kc ≤ Mm with
    B ≤ 9(Kc+2)⁴Z     and     (Kc = 0  or  9(Kc+1)⁴Z ≤ B),
  i.e. (Kc+2)⁴ ≈ B/Z up to the factor (Kc+2)⁴ ≤ (2(Kc+1))⁴ (cut_ratio). Constructive: a scan over the indices using
  only the totality of the order (no decidability). This is the choice of K in the H² force bound
  ForceAgmonW.force_agmonW (Kc + 1 ≤ M with M = Mm + 1).

  Lean 4 core plus ns/LatticeStep, ns/EnstLocal.
-/
import ns.LatticeStep
import ns.EnstLocal

namespace CutChoose
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge LatticeStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- the cut j is high enough: B ≤ 9(j+2)⁴Z -/
def Pc (Z B : R) (j : Nat) : Prop := Le K O.toCone B (K.mul (castN K 9) (K.mul (p4 K (castN K (j + 2))) Z))

/-- the cut j − 1 was too low: 9(j+1)⁴Z ≤ B -/
def Qc (Z B : R) (j : Nat) : Prop := Le K O.toCone (K.mul (castN K 9) (K.mul (p4 K (castN K (j + 1))) Z)) B

theorem cut_scan (Z B : R) : ∀ n : Nat, (∃ j, j ≤ n ∧ Pc K O Z B j ∧ (j = 0 ∨ Qc K O Z B j)) ∨ Qc K O Z B (n + 1)
  | 0 => by
    cases O.total B (K.mul (castN K 9) (K.mul (p4 K (castN K (0 + 2))) Z)) with
    | inl h => exact Or.inl ⟨0, Nat.le_refl 0, h, Or.inl rfl⟩
    | inr h => exact Or.inr h
  | n + 1 => by
    cases cut_scan Z B n with
    | inl h =>
      have ⟨j, hj, hp, hq⟩ := h
      exact Or.inl ⟨j, Nat.le_succ_of_le hj, hp, hq⟩
    | inr hq =>
      cases O.total B (K.mul (castN K 9) (K.mul (p4 K (castN K (n + 1 + 2))) Z)) with
      | inl h => exact Or.inl ⟨n + 1, Nat.le_refl _, h, Or.inr hq⟩
      | inr h => exact Or.inr h

/-- **The cut exists.** -/
theorem cut_choose (Z B : R) (Mm : Nat) (hBM : Pc K O Z B Mm) :
    ∃ Kc, Kc ≤ Mm ∧ Pc K O Z B Kc ∧ (Kc = 0 ∨ Qc K O Z B Kc) := by
  cases Mm with
  | zero => exact ⟨0, Nat.le_refl 0, hBM, Or.inl rfl⟩
  | succ m =>
    cases cut_scan K O Z B m with
    | inl h =>
      have ⟨j, hj, hp, hq⟩ := h
      exact ⟨j, Nat.le_succ_of_le hj, hp, hq⟩
    | inr hq => exact ⟨m + 1, Nat.le_refl _, hBM, Or.inr hq⟩

/-- (j+2)⁴ ≤ (2(j+1))⁴ -/
theorem cut_ratio (j : Nat) :
    Le K O.toCone (p4 K (castN K (j + 2))) (p4 K (K.add (castN K (j + 1)) (castN K (j + 1)))) := by
  have e : castN K (j + 2) = K.add (castN K (j + 1)) (castN K 1) := RatModel.cast_add K (j + 1) 1
  have h1 : Le K O.toCone (castN K 1) (castN K (j + 1)) :=
    RatModel.cast_mono K O.toCone 1 (j + 1) (Nat.succ_le_succ (Nat.zero_le j))
  have hx : Le K O.toCone (castN K (j + 2)) (K.add (castN K (j + 1)) (castN K (j + 1))) := by
    rw [e]; exact le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) h1
  have n1 : O.nn (castN K (j + 2)) := RatModel.nn_cast K O.toCone _
  have n2 : O.nn (K.add (castN K (j + 1)) (castN K (j + 1))) :=
    O.nn_add _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
  have s := EnstLocal.sq_mono K O _ _ n1 n2 hx
  exact EnstLocal.sq_mono K O _ _ (O.nn_sq _) (O.nn_sq _) s

end Gen

end CutChoose

#print axioms CutChoose.cut_scan
#print axioms CutChoose.cut_choose
#print axioms CutChoose.cut_ratio

#print axioms: 3 theorems, no axioms.

79.287 Root-free Young inequalities

For \(a,D\ge 0\): \(T^2\le 4aD\) or \(T^4\le 4a^3D\) implies \(T\le a+D\).

ns/RootYoung.lean

/-
  ns/RootYoung.lean — T37 (goal G859c2b2, piece c): root-free Young inequalities over an ordered field, for absorbing
  the Agmon force terms into the dissipation. For a, D, T ≥ 0:
    root2   T² ≤ 4aD   ⇒  T ≤ a + D        ((a + D)² − 4aD = (a − D)²)
    root4   T⁴ ≤ 4a³D  ⇒  T ≤ a + D        ((a + D)⁴ − 4a³D = a⁴ + 6a²D² + 4aD³ + D⁴)
  Use: both Agmon terms x³Z² and xZA satisfy T⁴ ≤ c·Z⁵B³ once x⁴Z ≲ B and A² ≤ ZB; with a = εB this gives
  T ≤ εB + c Z⁵/(4ε³), the H² step with an additive constant.

  Lean 4 core plus ns/DyadicSmallGlobal, ns/DyadicLarge.
-/
import ns.DyadicSmallGlobal
import ns.DyadicLarge
import ns.RatModel

namespace RootYoung
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def four : R := K.add (K.add K.one K.one) (K.add K.one K.one)

theorem four_nn : O.nn (four K) :=
  O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))

/-- **T² ≤ 4aD ⇒ T ≤ a + D** -/
theorem root2 (a D T : R) (ha : O.nn a) (hD : O.nn D)
    (h : Le K O.toCone (K.mul T T) (K.mul (four K) (K.mul a D))) : Le K O.toCone T (K.add a D) := by
  have hq : O.nn (K.add a D) := O.nn_add _ _ ha hD
  have e : K.mul (K.add a D) (K.add a D)
      = K.add (K.mul (four K) (K.mul a D)) (K.mul (K.add a (K.neg D)) (K.add a (K.neg D))) := by
    unfold four; lib_ring
  have h2 : Le K O.toCone (K.mul T T) (K.mul (K.add a D) (K.add a D)) := by
    rw [e]; exact le_trans' K O.toCone _ _ _ h ⟨_, O.nn_sq _, rfl⟩
  exact le_of_sq_le K O T (K.add a D) hq h2

/-- **T⁴ ≤ 4a³D ⇒ T ≤ a + D** -/
theorem root4 (a D T : R) (ha : O.nn a) (hD : O.nn D)
    (h : Le K O.toCone (K.mul (K.mul T T) (K.mul T T)) (K.mul (four K) (K.mul (K.mul (K.mul a a) a) D))) :
    Le K O.toCone T (K.add a D) := by
  have hq : O.nn (K.add a D) := O.nn_add _ _ ha hD
  have hq2 : O.nn (K.mul (K.add a D) (K.add a D)) := O.nn_sq _
  have rest : O.nn (K.add (K.add (K.mul (K.mul a a) (K.mul a a))
      (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (K.mul a a) (K.mul D D))))
      (K.add (K.mul (four K) (K.mul a (K.mul (K.mul D D) D))) (K.mul (K.mul D D) (K.mul D D)))) := by
    have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_add _ _ (O.nn_add _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
        (O.nn_mul _ _ (O.nn_add _ _ (four_nn K O) n2) (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))))
      (O.nn_add _ _ (O.nn_mul _ _ (four_nn K O) (O.nn_mul _ _ ha (O.nn_mul _ _ (O.nn_sq _) hD)))
        (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)))
  have e : K.mul (K.mul (K.add a D) (K.add a D)) (K.mul (K.add a D) (K.add a D))
      = K.add (K.mul (four K) (K.mul (K.mul (K.mul a a) a) D)) (K.add (K.add (K.mul (K.mul a a) (K.mul a a))
      (K.mul (K.add (K.add (K.add K.one K.one) (K.add K.one K.one)) (K.add K.one K.one)) (K.mul (K.mul a a) (K.mul D D))))
      (K.add (K.mul (four K) (K.mul a (K.mul (K.mul D D) D))) (K.mul (K.mul D D) (K.mul D D)))) := by
    unfold four; lib_ring
  have h4 : Le K O.toCone (K.mul (K.mul T T) (K.mul T T))
      (K.mul (K.mul (K.add a D) (K.add a D)) (K.mul (K.add a D) (K.add a D))) := by
    rw [e]; exact le_trans' K O.toCone _ _ _ h ⟨_, rest, rfl⟩
  have h2 := le_of_sq_le K O (K.mul T T) (K.mul (K.add a D) (K.add a D)) hq2 h4
  exact le_of_sq_le K O T (K.add a D) hq h2

end Gen

end RootYoung

#print axioms RootYoung.four_nn
#print axioms RootYoung.root2
#print axioms RootYoung.root4

#print axioms: 3 theorems, no axioms.

79.288 Absorbing the Agmon terms

With \(9x^4Z\le 16B\) and \(A^2\le ZB\): \(9^3(x^3Z^2)^4\le 16^3Z^5B^3\), \(9(xZA)^4\le 16Z^5B^3\), hence each term is at most \(eB+D\).

ns/AgmonAbsorb.lean

/-
  ns/AgmonAbsorb.lean — T37 (goal G859c2b2, piece d1): absorbing the two Agmon force terms into the H³ dissipation,
  over an ordered field (scalar lemmas). With x = Kc + 2 chosen by CutChoose (9x⁴Z ≤ 16B above the first cut) and
  the interpolation A² ≤ ZB:
    t1_quart   9³·(x³Z²)⁴ ≤ 16³·Z⁵B³
    t2_quart   9·(xZA)⁴ ≤ 16·Z⁵B³
    absorb     m·T⁴ ≤ c·Z⁵B³ and c·Z⁵ ≤ m·4e³D  ⇒  T ≤ e·B + D          (RootYoung.root4)
    absorb2    T² ≤ Z³B and Z³ ≤ 4eD  ⇒  T ≤ e·B + D                     (RootYoung.root2; the first cut, x = 2)
    za_sq      A² ≤ ZB  ⇒  (ZA)² ≤ Z³B
  so every force term is ≤ εB + C(ε)·Z^k: the H² step closes with an additive constant (G859c2c).

  Lean 4 core plus ns/RootYoung, ns/EnstGlobal, ns/ForceSob, ns/WeightedCS.
-/
import ns.RootYoung
import ns.EnstLocal
import ns.WeightedCS

namespace AgmonAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

def q4 (x : R) : R := K.mul (K.mul x x) (K.mul x x)
def c3 (x : R) : R := K.mul (K.mul x x) x
def q5 (x : R) : R := K.mul x (q4 K x)

theorem cube_mono (X Y : R) (hX : O.nn X) (hY : O.nn Y) (h : Le K O.toCone X Y) :
    Le K O.toCone (c3 K X) (c3 K Y) := by
  have s := WeightedCS.le_mul_both' K O.toCone X Y X Y hX hY h h
  exact WeightedCS.le_mul_both' K O.toCone (K.mul X X) (K.mul Y Y) X Y (O.nn_sq X) hY s h

/-- 9³·(x³Z²)⁴ ≤ 16³·Z⁵B³ -/
theorem t1_quart (x Z B : R) (hZ : O.nn Z) (hB : O.nn B)
    (h : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (castN K 16) B)) :
    Le K O.toCone (K.mul (c3 K (castN K 9)) (q4 K (K.mul (c3 K x) (K.mul Z Z))))
      (K.mul (c3 K (castN K 16)) (K.mul (q5 K Z) (c3 K B))) := by
  have hX : O.nn (K.mul (castN K 9) (K.mul (q4 K x) Z)) :=
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 9) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hZ)
  have hY : O.nn (K.mul (castN K 16) B) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone 16) hB
  have s := cube_mono K O _ _ hX hY h
  have hZ5 : O.nn (q5 K Z) := O.nn_mul _ _ hZ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have s5 := ForceSob.le_mul_right' K O.toCone s hZ5
  have e1 : K.mul (c3 K (castN K 9)) (q4 K (K.mul (c3 K x) (K.mul Z Z)))
      = K.mul (c3 K (K.mul (castN K 9) (K.mul (q4 K x) Z))) (q5 K Z) := by
    unfold q5 q4 c3; lib_ring
  have e2 : K.mul (c3 K (castN K 16)) (K.mul (q5 K Z) (c3 K B)) = K.mul (c3 K (K.mul (castN K 16) B)) (q5 K Z) := by
    unfold q5 q4 c3; lib_ring
  rw [e1, e2]; exact s5

/-- 9·(xZA)⁴ ≤ 16·Z⁵B³ -/
theorem t2_quart (x Z A B : R) (hZ : O.nn Z) (hB : O.nn B)
    (h : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (castN K 16) B))
    (hA : Le K O.toCone (K.mul A A) (K.mul Z B)) :
    Le K O.toCone (K.mul (castN K 9) (q4 K (K.mul x (K.mul Z A))))
      (K.mul (castN K 16) (K.mul (q5 K Z) (c3 K B))) := by
  have hX : O.nn (K.mul (castN K 9) (K.mul (q4 K x) Z)) :=
    O.nn_mul _ _ (RatModel.nn_cast K O.toCone 9) (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) hZ)
  have hZB : O.nn (K.mul Z B) := O.nn_mul _ _ hZ hB
  have s2 := EnstLocal.sq_mono K O _ _ (O.nn_sq A) hZB hA
  have s := WeightedCS.le_mul_both' K O.toCone _ _ _ _ hX (O.nn_sq _) h s2
  have hZ3 : O.nn (c3 K Z) := O.nn_mul _ _ (O.nn_sq _) hZ
  have s3 := ForceSob.le_mul_right' K O.toCone s hZ3
  have e1 : K.mul (castN K 9) (q4 K (K.mul x (K.mul Z A)))
      = K.mul (K.mul (K.mul (castN K 9) (K.mul (q4 K x) Z)) (K.mul (K.mul A A) (K.mul A A))) (c3 K Z) := by
    unfold c3 q4; lib_ring
  have e2 : K.mul (castN K 16) (K.mul (q5 K Z) (c3 K B))
      = K.mul (K.mul (K.mul (castN K 16) B) (K.mul (K.mul Z B) (K.mul Z B))) (c3 K Z) := by
    unfold q5 q4 c3; lib_ring
  rw [e1, e2]; exact s3

/-- **m·T⁴ ≤ c·Z⁵B³, c·Z⁵ ≤ m·4e³D ⇒ T ≤ eB + D** -/
theorem absorb (T Z B c m e D : R) (hm : O.nn m) (hm0 : m ≠ K.zero) (hB : O.nn B) (he : O.nn e) (hD : O.nn D)
    (hT : Le K O.toCone (K.mul m (q4 K T)) (K.mul c (K.mul (q5 K Z) (c3 K B))))
    (hcD : Le K O.toCone (K.mul c (q5 K Z)) (K.mul m (K.mul (four K) (K.mul (c3 K e) D)))) :
    Le K O.toCone T (K.add (K.mul e B) D) := by
  have hB3 : O.nn (c3 K B) := O.nn_mul _ _ (O.nn_sq _) hB
  have s := ForceSob.le_mul_right' K O.toCone hcD hB3
  have e1 : K.mul c (K.mul (q5 K Z) (c3 K B)) = K.mul (K.mul c (q5 K Z)) (c3 K B) := by lib_ring
  have e2 : K.mul (K.mul m (K.mul (four K) (K.mul (c3 K e) D))) (c3 K B)
      = K.mul m (K.mul (four K) (K.mul (K.mul (K.mul (K.mul e B) (K.mul e B)) (K.mul e B)) D)) := by
    unfold c3; lib_ring
  rw [e1] at hT
  rw [e2] at s
  have t := le_trans' K O.toCone _ _ _ hT s
  have t' := EnstGlobal.le_of_mul_le K O m _ _ hm hm0 t
  exact root4 K O (K.mul e B) D T (O.nn_mul _ _ he hB) hD t'

/-- (ZA)² ≤ Z³B from A² ≤ ZB -/
theorem za_sq (Z A B : R) (hA : Le K O.toCone (K.mul A A) (K.mul Z B)) :
    Le K O.toCone (K.mul (K.mul Z A) (K.mul Z A)) (K.mul (c3 K Z) B) := by
  have s := le_mul_nn K O.toCone (K.mul Z Z) _ _ (O.nn_sq Z) hA
  have e1 : K.mul (K.mul Z A) (K.mul Z A) = K.mul (K.mul Z Z) (K.mul A A) := by lib_ring
  have e2 : K.mul (c3 K Z) B = K.mul (K.mul Z Z) (K.mul Z B) := by unfold c3; lib_ring
  rw [e1, e2]; exact s

/-- **T² ≤ Z³B, Z³ ≤ 4eD ⇒ T ≤ eB + D** -/
theorem absorb2 (T Z B e D : R) (hB : O.nn B) (he : O.nn e) (hD : O.nn D)
    (hT : Le K O.toCone (K.mul T T) (K.mul (c3 K Z) B))
    (hZD : Le K O.toCone (c3 K Z) (K.mul (four K) (K.mul e D))) :
    Le K O.toCone T (K.add (K.mul e B) D) := by
  have s := ForceSob.le_mul_right' K O.toCone hZD hB
  have e2 : K.mul (K.mul (four K) (K.mul e D)) B = K.mul (four K) (K.mul (K.mul e B) D) := by lib_ring
  rw [e2] at s
  exact root2 K O (K.mul e B) D T (O.nn_mul _ _ he hB) hD (le_trans' K O.toCone _ _ _ hT s)

end Gen

end AgmonAbsorb

#print axioms AgmonAbsorb.cube_mono
#print axioms AgmonAbsorb.t1_quart
#print axioms AgmonAbsorb.t2_quart
#print axioms AgmonAbsorb.absorb
#print axioms AgmonAbsorb.za_sq
#print axioms AgmonAbsorb.absorb2

#print axioms: 6 theorems, no axioms.

79.289 Collapsing the weighted force bound

Under \(B\le 9x^4Z\) the right side of the weighted force bound is at most \(\iota ^2(k_1x^3Z^2+k_2xZA)\).

ns/AgmonCollapse.lean

/-
  ns/AgmonCollapse.lean — T37 (goal G859c2b2, piece d2): collapsing the right side of ForceAgmonW.force_agmonW to two
  Agmon terms, over an ordered field (scalar lemma). With x = Kc + 2, C₁ = Kc + 1 ≤ x, x·i = 1 and the cut condition
  B ≤ 9x⁴Z (CutChoose), with the numerals 3, 9, 108 kept as parameters t₃, t₉, t₁₀₈ ≥ 0 (instantiated by castN):
    collapse   ι²(Z + i⁴B)(2·3(C₁²·108C₁ + x²·108x)·Z + 2(108C₁ + 108x)·A)
                 ≤ ι²·(k₁·x³Z² + k₂·xZA),   k₁ = (1+9)·2·3·(108+108),  k₂ = (1+9)·2·(108+108),
  the two terms that AgmonAbsorb bounds by εB + C(ε)Z⁵.

  Lean 4 core plus ns/AgmonAbsorb.
-/
import ns.AgmonAbsorb

namespace AgmonCollapse
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

def two : R := K.add K.one K.one

def k1 (t3 t9 t108 : R) : R := K.mul (K.add K.one (t9)) (K.mul (two K) (K.mul (t3) (K.add (t108) (t108))))
def k2 (t9 t108 : R) : R := K.mul (K.add K.one (t9)) (K.mul (two K) (K.add (t108) (t108)))

/-- **The collapse of the Agmon force bound.** -/
theorem collapse (t3 t9 t108 : R) (h3 : O.nn t3) (h9 : O.nn t9) (h108 : O.nn t108) (ι Z A B x C1 i : R) (hZ : O.nn Z) (hA : O.nn A) (hB : O.nn B) (hC1 : O.nn C1) (hx : O.nn x)
    (hC : Le K O.toCone C1 x) (hxi : K.mul x i = K.one)
    (hP : Le K O.toCone B (K.mul (t9) (K.mul (q4 K x) Z))) :
    Le K O.toCone
      (K.mul (K.mul ι ι)
        (K.mul (K.add Z (K.mul (K.mul (K.mul i i) (K.mul i i)) B))
          (K.add
            (K.mul (K.add K.one K.one) (K.mul
              (K.mul (t3) (K.add (K.mul (K.mul C1 C1) (K.mul (t108) C1)) (K.mul (K.mul x x) (K.mul (t108) x))))
              Z))
            (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (t108) C1) (K.mul (t108) x)) A)))))
      (K.mul (K.mul ι ι) (K.add (K.mul (k1 K t3 t9 t108) (K.mul (c3 K x) (K.mul Z Z))) (K.mul (k2 K t9 t108) (K.mul x (K.mul Z A))))) := by
  have n108 : O.nn (t108) := h108
  have n3 : O.nn (t3) := h3
  have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have ni4 : O.nn (K.mul (K.mul i i) (K.mul i i)) := O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)
  -- first factor: Z + i⁴B ≤ Z + 9Z
  have f1 := le_mul_nn K O.toCone _ _ _ ni4 hP
  have e9 : K.mul (K.mul (K.mul i i) (K.mul i i)) (K.mul (t9) (K.mul (q4 K x) Z))
      = K.mul (t9) (K.mul (q4 K (K.mul x i)) Z) := by unfold q4; lib_ring
  rw [e9, hxi] at f1
  have e1 : K.mul (t9) (K.mul (q4 K K.one) Z) = K.mul (t9) Z := by unfold q4; lib_ring
  rw [e1] at f1
  have F1 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone Z) f1
  -- second factor
  have c3le := cube_mono K O _ _ hC1 hx hC
  have g1 : Le K O.toCone (K.mul (K.mul C1 C1) (K.mul (t108) C1)) (K.mul (K.mul x x) (K.mul (t108) x)) := by
    have s := le_mul_nn K O.toCone _ _ _ n108 c3le
    have ea : K.mul (K.mul C1 C1) (K.mul (t108) C1) = K.mul (t108) (c3 K C1) := by unfold c3; lib_ring
    have eb : K.mul (K.mul x x) (K.mul (t108) x) = K.mul (t108) (c3 K x) := by unfold c3; lib_ring
    rw [ea, eb]; exact s
  have g2 := le_add' K O.toCone _ _ _ _ g1 (le_refl' K O.toCone (K.mul (K.mul x x) (K.mul (t108) x)))
  have g3 := le_mul_nn K O.toCone _ _ _ n2 (ForceSob.le_mul_right' K O.toCone (le_mul_nn K O.toCone _ _ _ n3 g2) hZ)
  have h1 := le_mul_nn K O.toCone _ _ _ n108 hC
  have h2 := le_add' K O.toCone _ _ _ _ h1 (le_refl' K O.toCone (K.mul (t108) x))
  have h3 := le_mul_nn K O.toCone _ _ _ n2 (ForceSob.le_mul_right' K O.toCone h2 hA)
  have F2 := le_add' K O.toCone _ _ _ _ g3 h3
  have nZ9 : O.nn (K.add Z (K.mul (t9) Z)) := O.nn_add _ _ hZ (O.nn_mul _ _ (h9) hZ)
  have nF1 : O.nn (K.add Z (K.mul (K.mul (K.mul i i) (K.mul i i)) B)) := O.nn_add _ _ hZ (O.nn_mul _ _ ni4 hB)
  have nx3 : O.nn (K.mul (K.mul x x) (K.mul (t108) x)) := O.nn_mul _ _ (O.nn_sq _) (O.nn_mul _ _ n108 hx)
  have nS2 : O.nn (K.add
      (K.mul (K.add K.one K.one) (K.mul (K.mul (t3) (K.add (K.mul (K.mul x x) (K.mul (t108) x))
        (K.mul (K.mul x x) (K.mul (t108) x)))) Z))
      (K.mul (K.add K.one K.one) (K.mul (K.add (K.mul (t108) x) (K.mul (t108) x)) A))) :=
    O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_mul _ _ n3 (O.nn_add _ _ nx3 nx3)) hZ))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_add _ _ (O.nn_mul _ _ n108 hx) (O.nn_mul _ _ n108 hx)) hA))
  have P := WeightedCS.le_mul_both' K O.toCone _ _ _ _ nF1 nS2 F1 F2
  refine le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) P) (le_eq K O _ _ ?_)
  unfold k1 k2 two c3; lib_ring

end Gen

end AgmonCollapse

#print axioms AgmonCollapse.le_eq
#print axioms AgmonCollapse.collapse

#print axioms: 2 theorems, no axioms.

79.290 The weighted force absorbed into \(B\)

For any admissible \(e_i,D_i\): \(\sum _r w_r|N_r(a,a)|^2\le \iota ^2(k_1(e_1B+D_1+8Z^2)+k_2(e_2B+D_2+2(e_3B+D_3)))\), uniformly in the truncation.

ns/ForceAbsorb.lean

/-
  ns/ForceAbsorb.lean — T37 (goal G859c2b2, piece d3): the H¹-weighted Galerkin force absorbed into the H³ norm,
  uniform in the truncation, over an ordered field. For integer modes (nonzero, distinct on l, |k|_∞ ≤ Mm + 2) and
  any e₁, D₁, e₂, D₂, e₃, D₃ ≥ 0 with 16³Z⁵ ≤ 9³·4e₁³D₁, 16Z⁵ ≤ 9·4e₂³D₂, Z³ ≤ 4e₃D₃:
    force_absorb   Σ_r w_r|N_r(a,a)|² ≤ ι²·(k₁(e₁B + D₁ + 8Z²) + k₂(e₂B + D₂ + 2(e₃B + D₃))),
  Z = Σw|a|², B = Σw³|a|². The cut Kc is chosen by CutChoose (B ≤ 9(Kc+2)⁴Z, and Kc = 0 or 9(Kc+1)⁴Z ≤ B);
  ForceAgmonW and AgmonCollapse give ι²(k₁x³Z² + k₂xZA); AgmonAbsorb bounds both terms (A² ≤ ZB by WeightedJensen).
  With e's small this is Σw|N|² ≤ εB + C(ε)(Z⁵ + Z³ + Z²): the H² step closes with an additive constant (G859c2c).

  Lean 4 core plus ns/ForceAgmonW, ns/CutChoose, ns/AgmonCollapse, ns/WeightedJensen.
-/
import ns.ForceAgmonW
import ns.CutChoose
import ns.AgmonCollapse
import ns.WeightedJensen

namespace ForceAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  LatticeBound LatticeStep LatticeStep2 LatticeSum2 LatticeBound2 LatticeTail4 ForceAgmon ForceAgmonW AbelCount
  RootYoung AgmonAbsorb AgmonCollapse CutChoose

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

def Zs (kz : I → Mode) (l : List I) (a : I → V3 R) : R := lsum K (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) l
def As (kz : I → Mode) (l : List I) (a : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (a p) (a p))) l
def Bs (kz : I → Mode) (l : List I) (a : I → V3 R) : R :=
  lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p))) l

include O in
theorem ne_c3 (y : R) (hy : y ≠ K.zero) : c3 K y ≠ K.zero := by
  intro h
  cases O.noZD _ _ h with
  | inl h2 => cases O.noZD _ _ h2 with
    | inl h3 => exact hy h3
    | inr h3 => exact hy h3
  | inr h2 => exact hy h2

theorem e16 : castN K 16 = q4 K (K.add K.one K.one) := by
  have e : castN K 16 = K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.zero)))))))))))))))) := rfl
  rw [e]; unfold q4; lib_ring

include h10 in
/-- A² ≤ ZB (weighted Cauchy–Schwarz) -/
theorem a_sq (kz : I → Mode) (l : List I) (a : I → V3 R) :
    Le K O.toCone (K.mul (As K kz l a) (As K kz l a)) (K.mul (Zs K kz l a) (Bs K kz l a)) := by
  have W : Le K O.toCone
      (K.add (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l))
        (K.mul (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l)))
      (K.add (K.mul (Zs K kz l a)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l))
        (K.mul (Zs K kz l a)
          (lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l))) :=
    WeightedJensen.wcs2 K O.toCone (fun p => K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz) l
      (fun p => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _))
  have eA : lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (wt K kz p)) l = As K kz l a :=
    lsum_ext K _ _ (fun p => by lib_ring) l
  have eB : lsum K (fun p => K.mul (K.mul (wt K kz p) (dot3 K (a p) (a p))) (K.mul (wt K kz p) (wt K kz p))) l
      = Bs K kz l a := lsum_ext K _ _ (fun p => by lib_ring) l
  rw [eA, eB] at W
  have e2 : castN K 2 = K.add K.one K.one := by show K.add K.one (K.add K.one K.zero) = _; lib_ring
  have W' : Le K O.toCone (K.mul (castN K 2) (K.mul (As K kz l a) (As K kz l a)))
      (K.mul (castN K 2) (K.mul (Zs K kz l a) (Bs K kz l a))) := by
    rw [e2]
    have x1 : K.mul (K.add K.one K.one) (K.mul (As K kz l a) (As K kz l a))
        = K.add (K.mul (As K kz l a) (As K kz l a)) (K.mul (As K kz l a) (As K kz l a)) := by lib_ring
    have x2 : K.mul (K.add K.one K.one) (K.mul (Zs K kz l a) (Bs K kz l a))
        = K.add (K.mul (Zs K kz l a) (Bs K kz l a)) (K.mul (Zs K kz l a) (Bs K kz l a)) := by unfold Zs Bs; lib_ring
    rw [x1, x2]; exact W
  exact EnstGlobal.le_of_mul_le K O _ _ _ (RatModel.nn_cast K O.toCone 2) (GoodModesQ.cast_succ_ne K O h10 1) W'

/-- B ≤ 9(Mm+2)⁴Z for |k|_∞ ≤ Mm + 2 -/
theorem b_le (kz : I → Mode) (l : List I) (a : I → V3 R) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) :
    Pc K O (Zs K kz l a) (Bs K kz l a) Mm := by
  unfold Pc Bs Zs
  rw [← lsum_mul_left, ← lsum_mul_left]
  apply lsum_le_on K O
  intro p hp
  have hw := dot_le_3nrm K O (kz p)
  have c := RatModel.cast_mono K O.toCone _ _ (hM p hp)
  have s := EnstLocal.sq_mono K O _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _) c
  have s3 := le_add' K O.toCone _ _ _ _ (le_add' K O.toCone _ _ _ _ s s) s
  have w3 := le_trans' K O.toCone _ _ _ hw s3
  have hwn : O.nn (wt K kz p) := dot3_nn K O.toCone _
  have n3 : O.nn (K.add (K.add (K.mul (castN K (Mm + 2)) (castN K (Mm + 2))) (K.mul (castN K (Mm + 2)) (castN K (Mm + 2))))
      (K.mul (castN K (Mm + 2)) (castN K (Mm + 2)))) :=
    O.nn_add _ _ (O.nn_add _ _ (O.nn_sq _) (O.nn_sq _)) (O.nn_sq _)
  have w2 := EnstLocal.sq_mono K O _ _ hwn n3 w3
  have hd : O.nn (K.mul (wt K kz p) (dot3 K (a p) (a p))) := O.nn_mul _ _ hwn (dot3_nn K O.toCone _)
  have w4 := ForceSob.le_mul_right' K O.toCone w2 hd
  have e9 : castN K 9 = K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one (K.add K.one
      (K.add K.one (K.add K.one K.zero)))))))) := rfl
  refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _ w4 ?_)
  · apply ForceAgmonW.le_eq K O
    show K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (a p) (a p)) = _
    lib_ring
  · apply ForceAgmonW.le_eq K O
    show _ = K.mul (castN K 9) (K.mul (p4 K (castN K (Mm + 2))) (K.mul (wt K kz p) (dot3 K (a p) (a p))))
    rw [e9]; unfold p4; lib_ring

include mul_inv h10 in
/-- **The H¹-weighted force absorbed into B.** -/
theorem force_absorb (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (ι : R) (a : I → V3 R) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2)
    (e1 D1 e2 D2 e3 D3 : R) (he1 : O.nn e1) (hD1 : O.nn D1) (he2 : O.nn e2) (hD2 : O.nn D2)
    (he3 : O.nn e3) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K (Zs K kz l a)))
      (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e1) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (q5 K (Zs K kz l a)))
      (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e2) D2))))
    (H3 : Le K O.toCone (c3 K (Zs K kz l a)) (K.mul (four K) (K.mul e3 D3))) :
    Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) a a r))) l)
      (K.mul (K.mul ι ι)
        (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add (K.add (K.mul e1 (Bs K kz l a)) D1) (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a)))))
          (K.mul (k2 K (castN K 9) (castN K 108)) (K.add (K.add (K.mul e2 (Bs K kz l a)) D2) (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3)))))) := by
  have nZ : O.nn (Zs K kz l a) := lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have nA : O.nn (As K kz l a) := lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have nB : O.nn (Bs K kz l a) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  have hAZB := a_sq K O h10 kz l a
  have ⟨Kc, hKc, hP, hQ⟩ := cut_choose K O (Zs K kz l a) (Bs K kz l a) Mm (b_le K O kz l a Mm hM)
  have F := force_agmonW K O inv mul_inv h10 l kz hd h1 ι a a Kc (Mm + 1) (Nat.succ_le_succ hKc) hM
  have Cl := collapse K O (castN K 3) (castN K 9) (castN K 108) (RatModel.nn_cast K O.toCone 3)
    (RatModel.nn_cast K O.toCone 9) (RatModel.nn_cast K O.toCone 108) ι (Zs K kz l a) (As K kz l a) (Bs K kz l a) (castN K (Kc + 2)) (castN K (Kc + 1))
    (iv K inv (Kc + 2)) nZ nA nB (RatModel.nn_cast K O.toCone _) (RatModel.nn_cast K O.toCone _)
    (RatModel.cast_mono K O.toCone _ _ (Nat.le_succ _)) (iv_mul K O inv mul_inv h10 (Kc + 1)) hP
  have F2 := le_trans' K O.toCone _ _ _ F Cl
  have nE1 : O.nn (K.add (K.mul e1 (Bs K kz l a)) D1) := O.nn_add _ _ (O.nn_mul _ _ he1 nB) hD1
  have nE2 : O.nn (K.add (K.mul e2 (Bs K kz l a)) D2) := O.nn_add _ _ (O.nn_mul _ _ he2 nB) hD2
  have n2 : O.nn (castN K 2) := RatModel.nn_cast K O.toCone 2
  have n9 : castN K 9 ≠ K.zero := GoodModesQ.cast_succ_ne K O h10 8
  -- the term x·Z·A at x = 2 and the bound of Z·A
  have za := absorb2 K O (K.mul (Zs K kz l a) (As K kz l a)) (Zs K kz l a) (Bs K kz l a) e3 D3 nB he3 hD3
    (za_sq K O (Zs K kz l a) (As K kz l a) (Bs K kz l a) hAZB) H3
  have hT : Le K O.toCone (K.mul (c3 K (castN K (Kc + 2))) (K.mul (Zs K kz l a) (Zs K kz l a)))
        (K.add (K.add (K.mul e1 (Bs K kz l a)) D1) (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))))
      ∧ Le K O.toCone (K.mul (castN K (Kc + 2)) (K.mul (Zs K kz l a) (As K kz l a)))
        (K.add (K.add (K.mul e2 (Bs K kz l a)) D2) (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3))) := by
    cases hQ with
    | inl h0 =>
      subst h0
      have nT := O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ he3 nB) hD3)
      refine ⟨⟨K.add (K.mul e1 (Bs K kz l a)) D1, nE1, ?_⟩, ?_⟩
      · show K.add (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))) (K.add (K.mul e1 (Bs K kz l a)) D1) = _
        lib_ring
      · have m := le_mul_nn K O.toCone _ _ _ n2 za
        exact le_trans' K O.toCone _ _ _ m ⟨K.add (K.mul e2 (Bs K kz l a)) D2, nE2, by lib_ring⟩
    | inr hq =>
      -- 9x⁴Z ≤ 16B
      have r := cut_ratio K O Kc
      have r9 := le_mul_nn K O.toCone (castN K 9) _ _ (RatModel.nn_cast K O.toCone 9)
        (ForceSob.le_mul_right' K O.toCone r nZ)
      have q16 := le_mul_nn K O.toCone (castN K 16) _ _ (RatModel.nn_cast K O.toCone 16) hq
      have e : K.mul (castN K 9) (K.mul (p4 K (K.add (castN K (Kc + 1)) (castN K (Kc + 1)))) (Zs K kz l a))
          = K.mul (castN K 16) (K.mul (castN K 9) (K.mul (p4 K (castN K (Kc + 1))) (Zs K kz l a))) := by
        rw [e16 K]; unfold p4 q4; lib_ring
      rw [e] at r9
      have h16 : Le K O.toCone (K.mul (castN K 9) (K.mul (q4 K (castN K (Kc + 2))) (Zs K kz l a)))
          (K.mul (castN K 16) (Bs K kz l a)) := le_trans' K O.toCone _ _ _ r9 q16
      have t1 := t1_quart K O (castN K (Kc + 2)) (Zs K kz l a) (Bs K kz l a) nZ nB h16
      have t2 := t2_quart K O (castN K (Kc + 2)) (Zs K kz l a) (As K kz l a) (Bs K kz l a) nZ nB h16 hAZB
      have T1 := absorb K O _ (Zs K kz l a) (Bs K kz l a) _ _ e1 D1
        (O.nn_mul _ _ (O.nn_sq _) (RatModel.nn_cast K O.toCone 9)) (ne_c3 K O _ n9) nB he1 hD1 t1 H1
      have T2 := absorb K O _ (Zs K kz l a) (Bs K kz l a) _ _ e2 D2
        (RatModel.nn_cast K O.toCone 9) n9 nB he2 hD2 t2 H2
      have nX : O.nn (K.mul (c3 K (castN K 2)) (K.mul (Zs K kz l a) (Zs K kz l a))) :=
        O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) n2) (O.nn_sq _)
      have nY : O.nn (K.mul (castN K 2) (K.add (K.mul e3 (Bs K kz l a)) D3)) :=
        O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ he3 nB) hD3)
      exact ⟨le_trans' K O.toCone _ _ _ T1 ⟨_, nX, rfl⟩, le_trans' K O.toCone _ _ _ T2 ⟨_, nY, rfl⟩⟩
  have nk1 : O.nn (k1 K (castN K 3) (castN K 9) (castN K 108)) := by
    unfold k1 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone 9))
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 3)
          (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108))))
  have nk2 : O.nn (k2 K (castN K 9) (castN K 108)) := by
    unfold k2 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_cast K O.toCone 9))
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108)))
  have G := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone _ _ _ nk1 hT.1) (le_mul_nn K O.toCone _ _ _ nk2 hT.2)
  exact le_trans' K O.toCone _ _ _ F2 (le_mul_nn K O.toCone _ _ _ (O.nn_sq ι) G)

end Gen

end ForceAbsorb

#print axioms ForceAbsorb.ne_c3
#print axioms ForceAbsorb.e16
#print axioms ForceAbsorb.a_sq
#print axioms ForceAbsorb.b_le
#print axioms ForceAbsorb.force_absorb

#print axioms: 5 theorems, no axioms.

79.291 Linear growth of the \(H^2\) energy

If \(\nu A_{s+1}+h\nu ^2B_{s+1}\le E_s+hD\) with \(E=\nu A+h\varepsilon B\), \(\varepsilon \le \nu ^2\), then \(E_n\le E_0+nhD\).

ns/H2Linear.lean

/-
  ns/H2Linear.lean — T37 (goal G859c2c, piece a): linear growth of the H² energy of the implicit-viscosity scheme
  once the force is absorbed, over an ordered field (scalar lemma). If, for every step s,
      ν·A_{s+1} + hν²·B_{s+1} ≤ ν·A_s + hε·B_s + h·D      with ε ≤ ν², h ≥ 0, B ≥ 0,
  (EnstStepV.enst_stepV with ForceAbsorb.force_absorb: ε = ι²(k₁e₁ + k₂e₂ + 2k₂e₃), D = D(Z_s) bounded on the
  EnstLocalTime interval), then E_s = ν·A_s + hε·B_s satisfies
    h2_linear   E_n ≤ E_0 + n·h·D,
  i.e. the H² norm grows at most linearly on [0, t], uniformly in the truncation.

  Lean 4 core plus ns/ForceSob, ns/DyadicLarge.
-/
import ns.ForceSob
import ns.DyadicLarge

namespace H2Linear
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem le_eq (x y : R) (h : x = y) : Le K O.toCone x y := by
  rw [h]; exact le_refl' K O.toCone y

def En (ν h ε : R) (A B : Nat → R) (s : Nat) : R := K.add (K.mul ν (A s)) (K.mul (K.mul h ε) (B s))

/-- **Linear growth of the H² energy.** -/
theorem h2_linear (ν h ε D : R) (A B : Nat → R) (hh : O.nn h) (hB : ∀ s, O.nn (B s))
    (hε : Le K O.toCone ε (K.mul ν ν))
    (hs : ∀ s, Le K O.toCone (K.add (K.mul ν (A (s + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (s + 1))))
      (K.add (En K ν h ε A B s) (K.mul h D))) :
    ∀ n, Le K O.toCone (En K ν h ε A B n) (K.add (En K ν h ε A B 0) (K.mul (castN K n) (K.mul h D)))
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (En K ν h ε A B 0) K.zero = K.add (En K ν h ε A B 0) (K.mul K.zero (K.mul h D)); lib_ring⟩
  | n + 1 => by
    have ih := h2_linear ν h ε D A B hh hB hε hs n
    -- E_{n+1} ≤ ν A_{n+1} + hν² B_{n+1}
    have m1 := le_mul_nn K O.toCone h _ _ hh (ForceSob.le_mul_right' K O.toCone hε (hB (n + 1)))
    have s1 : Le K O.toCone (En K ν h ε A B (n + 1))
        (K.add (K.mul ν (A (n + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (n + 1)))) := by
      refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _
        (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (A (n + 1)))) m1) ?_)
      · apply le_eq K O; unfold En; lib_ring
      · apply le_eq K O; lib_ring
    have s2 := le_trans' K O.toCone _ _ _ s1 (hs n)
    have s3 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h D))
    refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ s2 s3) ?_
    apply le_eq K O
    show K.add (K.add (En K ν h ε A B 0) (K.mul (castN K n) (K.mul h D))) (K.mul h D)
      = K.add (En K ν h ε A B 0) (K.mul (K.add K.one (castN K n)) (K.mul h D))
    lib_ring

end Gen

end H2Linear

#print axioms H2Linear.le_eq
#print axioms H2Linear.h2_linear

#print axioms: 2 theorems, no axioms.

79.292 The \(H^2\) energy while the enstrophy is bounded

For the implicit-viscosity Galerkin scheme with \(Z_s\le Z_b\) (\(s<n\)): \(\nu A_n+h\varepsilon B_n\le \nu A_0+h\varepsilon B_0+nhD\), uniformly in the truncation.

ns/H2LocalUniform.lean

/-
  ns/H2LocalUniform.lean — T37 (goal G859c2c, piece b): the H² energy of the implicit-viscosity Galerkin scheme grows at
  most linearly while the enstrophy stays below Zb, uniformly in the truncation, over an ordered field. For integer
  modes (nonzero, distinct on l, |k|_∞ ≤ Mm + 2), a force with Σw|N|² ≤ Σw|Nl|², parameters e, D₁, D₂, D₃ ≥ 0 with
  16³Zb⁵ ≤ 9³·4e³D₁, 16Zb⁵ ≤ 9·4e³D₂, Zb³ ≤ 4eD₃ and ε = ι²(k₁e + k₂(e + 2·e)) ≤ ν²:
    h2_local_uniform   Z_s ≤ Zb for s < n  ⇒  νA_n + hεB_n ≤ νA_0 + hεB_0 + n·h·D,
      D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)).
  EnstStepV.enst_stepV (the step), ForceAbsorb.force_absorb (the force), and the bounded form of H2Linear.

  Lean 4 core plus ns/ForceAbsorb, ns/H2Linear, ns/EnstStepV.
-/
import ns.ForceAbsorb
import ns.H2Linear
import ns.EnstStepV

namespace H2LocalUniform
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale ForceSobW CastZAdd WeightH2 LatticeSum
  RootYoung AgmonAbsorb AgmonCollapse ForceAbsorb EnstStep EnstStepV

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem q5_mono (x y : R) (hx : O.nn x) (hy : O.nn y) (h : Le K O.toCone x y) : Le K O.toCone (q5 K x) (q5 K y) := by
  have s2 := EnstLocal.sq_mono K O _ _ hx hy h
  have s4 := EnstLocal.sq_mono K O _ _ (O.nn_sq x) (O.nn_sq y) s2
  exact WeightedCS.le_mul_both' K O.toCone _ _ _ _ hx (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _)) h s4

/-- the bounded linear-growth lemma -/
theorem lin_upto (ν h ε D : R) (A B : Nat → R) (hh : O.nn h) (hB : ∀ s, O.nn (B s))
    (hε : Le K O.toCone ε (K.mul ν ν)) (n : Nat)
    (hs : ∀ s, s < n → Le K O.toCone (K.add (K.mul ν (A (s + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (s + 1))))
      (K.add (H2Linear.En K ν h ε A B s) (K.mul h D))) :
    ∀ m, m ≤ n → Le K O.toCone (H2Linear.En K ν h ε A B m)
      (K.add (H2Linear.En K ν h ε A B 0) (K.mul (castN K m) (K.mul h D)))
  | 0, _ => ⟨K.zero, O.nn_zero, by
      show K.add (H2Linear.En K ν h ε A B 0) K.zero = K.add (H2Linear.En K ν h ε A B 0) (K.mul K.zero (K.mul h D)); lib_ring⟩
  | m + 1, hm => by
    have ih := lin_upto ν h ε D A B hh hB hε n hs m (Nat.le_of_succ_le hm)
    have m1 := le_mul_nn K O.toCone h _ _ hh (ForceSob.le_mul_right' K O.toCone hε (hB (m + 1)))
    have s1 : Le K O.toCone (H2Linear.En K ν h ε A B (m + 1))
        (K.add (K.mul ν (A (m + 1))) (K.mul (K.mul h (K.mul ν ν)) (B (m + 1)))) := by
      refine le_trans' K O.toCone _ _ _ ?_ (le_trans' K O.toCone _ _ _
        (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (A (m + 1)))) m1) ?_)
      · apply H2Linear.le_eq K O; unfold H2Linear.En; lib_ring
      · apply H2Linear.le_eq K O; lib_ring
    have s2 := le_trans' K O.toCone _ _ _ s1 (hs m hm)
    have s3 := le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone (K.mul h D))
    refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ s2 s3) ?_
    apply H2Linear.le_eq K O
    show K.add (K.add (H2Linear.En K ν h ε A B 0) (K.mul (castN K m) (K.mul h D))) (K.mul h D)
      = K.add (H2Linear.En K ν h ε A B 0) (K.mul (K.add K.one (castN K m)) (K.mul h D))
    lib_ring

include mul_inv h10 in
/-- **The H² energy grows at most linearly while Z ≤ Zb, uniformly in the truncation.** -/
theorem h2_local_uniform (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2)
    (ι h ν : R) (hh : O.nn h) (hν : O.nn ν) (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (Zb e D1 D2 D3 : R) (hZb : O.nn Zb) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb)) (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (q5 K Zb)) (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) D2))))
    (H3 : Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (n : Nat) (hZ : ∀ s, s < n → Le K O.toCone (Zs K kz l (a s)) Zb) :
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) n)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) 0)
        (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add D1 (K.mul (c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nBs : ∀ s, O.nn (Bs K kz l (a s)) := fun s =>
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) l
  refine lin_upto K O ν h _ _ (fun s => As K kz l (a s)) (fun s => Bs K kz l (a s)) hh nBs hε n ?_ n (Nat.le_refl n)
  intro s hsn
  have nZ : O.nn (Zs K kz l (a s)) :=
    lsum_nn K O.toCone _ (fun q => O.nn_mul _ _ (dot3_nn K O.toCone _) (dot3_nn K O.toCone _)) l
  have hZs := hZ s hsn
  have n16 := RatModel.nn_cast K O.toCone 16
  have n9 := RatModel.nn_cast K O.toCone 9
  have q5m := q5_mono K O _ _ nZ hZb hZs
  have H1' := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone (c3 K (castN K 16)) _ _ (O.nn_mul _ _ (O.nn_sq _) n16) q5m) H1
  have H2' := le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ n16 q5m) H2
  have H3' := le_trans' K O.toCone _ _ _ (cube_mono K O _ _ nZ hZb hZs) H3
  have F := force_absorb K O inv mul_inv h10 l kz hd h1 ι (a s) Mm hM e D1 e D2 e D3 he hD1 he hD2 he hD3 H1' H2' H3'
  have st := enst_stepV K O.toCone l h ν hh hν (wt K kz) (wt K kz) hw hw (a (s + 1)) (a s) (N s) (hs s)
  -- Z_s² ≤ Zb² inside the force bound
  have z2 := EnstLocal.sq_mono K O _ _ nZ hZb hZs
  have nk1 : O.nn (k1 K (castN K 3) (castN K 9) (castN K 108)) := by
    unfold k1 two
    exact O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) n9)
      (O.nn_mul _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 3)
          (O.nn_add _ _ (RatModel.nn_cast K O.toCone 108) (RatModel.nn_cast K O.toCone 108))))
  have n8 : O.nn (c3 K (castN K 2)) := O.nn_mul _ _ (O.nn_sq _) (RatModel.nn_cast K O.toCone 2)
  have z8 := le_mul_nn K O.toCone (c3 K (castN K 2)) _ _ n8 z2
  have g1 := le_mul_nn K O.toCone _ _ _ nk1
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.add (K.mul e (Bs K kz l (a s))) D1)) z8)
  have G := le_mul_nn K O.toCone _ _ _ (O.nn_sq ι)
    (le_add' K O.toCone _ _ _ _ g1 (le_refl' K O.toCone (K.mul (k2 K (castN K 9) (castN K 108))
      (K.add (K.add (K.mul e (Bs K kz l (a s))) D2) (K.mul (castN K 2) (K.add (K.mul e (Bs K kz l (a s))) D3))))))
  have Fs := le_trans' K O.toCone _ _ _ (hN s) (le_trans' K O.toCone _ _ _ F G)
  have Fh := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (As K kz l (a s)))) (le_mul_nn K O.toCone _ _ _ hh Fs)
  refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ st Fh) ?_
  apply H2Linear.le_eq K O
  unfold H2Linear.En
  show _ = K.add (K.add (K.mul ν (As K kz l (a s))) (K.mul (K.mul h (K.mul (K.mul ι ι)
      (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))))
      (Bs K kz l (a s)))) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (k1 K (castN K 3) (castN K 9) (castN K 108)) (K.add D1 (K.mul (c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
  unfold ForceAbsorb.As ForceAbsorb.Bs
  lib_ring

end Gen

end H2LocalUniform

#print axioms H2LocalUniform.q5_mono
#print axioms H2LocalUniform.lin_upto
#print axioms H2LocalUniform.h2_local_uniform

#print axioms: 3 theorems, no axioms.

79.293 The large-data local \(H^2\) bound

On the local-time interval of the enstrophy (\(t\le n\)): \(Z_t\le Z_b\) and \(\nu A_t+h\varepsilon B_t\le \nu A_0+h\varepsilon B_0+t\,h\,D\), uniformly in the truncation.

ns/H2Large.lean

/-
  ns/H2Large.lean — T37 (goal G859c2d): the large-data local H² bound, uniform in the truncation, over an ordered
  field. Under the hypotheses of EnstLocalTime.local_time (cutoffs Rc ≤ M, step count n with n·h·c₁Zb² ≤ Q₀,
  ν·Zb = 2Q₀) and those of H2LocalUniform.h2_local_uniform (e, D₁, D₂, D₃ for Zb, ε ≤ ν²), for every t ≤ n:
    h2_large   Z_t ≤ Zb   and   νA_t + hεB_t ≤ νA_0 + hεB_0 + t·h·D,
  D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)): on the local-time interval both the enstrophy and the H² energy are bounded
  independently of the Galerkin truncation — the H² bound R_r of G859c2 on [0, t₂] for t₂ = n·h.

  Lean 4 core plus ns/EnstLocalTime, ns/H2LocalUniform.
-/
import ns.EnstLocalTime
import ns.H2LocalUniform

namespace H2Large
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The large-data local H² bound, uniform in the truncation.** -/
theorem h2_large (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc))
      (K.mul Zb Zb)))) (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have loc := fun T (hT : T ≤ n) => EnstLocalTime.local_time K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc t0 hRM
    ι h ν Zb hh hν hν0 hZb a N hN0 hs choice hZ0 n hn T hT
  refine ⟨(loc t ht).2, ?_⟩
  exact H2LocalUniform.h2_local_uniform K O inv mul_inv h10 l kz hd h1 Mm hM ι h ν hh hν a N hN hs Zb e D1 D2 D3
    hZb he hD1 hD2 hD3 H1 H2 H3 hε t (fun s hst => (loc s (Nat.le_trans (Nat.le_of_lt hst) ht)).2)

end Gen

end H2Large

#print axioms H2Large.h2_large

#print axioms: 1 theorem, no axioms.

79.294 Gronwall with a source on a window

If the step inequality and the coefficient bounds hold for \(t<n\), then \(\nu ^TQ_T\le (\nu +h\alpha )^T(Q_0+ThS)\) for \(T\le n\).

ns/GronwallUpto.lean

/-
  ns/GronwallUpto.lean — T37 (goal G859c2g, piece b1): discrete Gronwall with a source on a finite window, over an
  ordered ring. The hypotheses of GronwallSource.gronwall_src (coefficient bounds and the step inequality) are asked
  only for t < n; the conclusion
    gronwall_upto   ν^T·Q_T ≤ (ν + hα)^T·(Q_0 + T·h·S)   for T ≤ n
  holds on the window. This is what the large-data local bounds (H2LargeAll: Z_t ≤ Zb, A_t ≤ Aa only for t ≤ n) feed
  into the truncation Cauchy estimate (TruncCauchy) — same proof as gronwall_src.

  Lean 4 core plus ns/GronwallSource.
-/
import ns.GronwallSource

namespace GronwallUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGlobal EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem gronwall_upto (ν h α S : R) (Z A αs β Ss : Nat → R) (hν : O.nn ν) (hh : O.nn h) (hα : O.nn α) (hS : O.nn S)
    (hA : ∀ t, O.nn (A t)) (n : Nat) (hαs : ∀ t, t < n → O.nn (αs t)) (hαle : ∀ t, t < n → Le K O.toCone (αs t) α)
    (hSs : ∀ t, t < n → O.nn (Ss t)) (hSle : ∀ t, t < n → Le K O.toCone (Ss t) S)
    (hβ : ∀ t, t < n → Le K O.toCone (β t) (K.mul ν ν))
    (hZ : ∀ t, O.nn (Z t))
    (step : ∀ t, t < n → Le K O.toCone (Q K ν h Z A (t + 1))
      (K.add (K.add (K.mul ν (Z t)) (K.mul h (K.add (K.mul (αs t) (Z t)) (K.mul (β t) (A t))))) (K.mul h (Ss t)))) :
    ∀ T, T ≤ n → Le K O.toCone (K.mul (pwr K ν T) (Q K ν h Z A T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T) (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S))))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one _) K.zero = K.mul K.one (K.add _ (K.mul K.zero _)); lib_ring⟩
  | T + 1, hT => by
    have hTn : T < n := hT
    have ih := gronwall_upto ν h α S Z A αs β Ss hν hh hα hS hA n hαs hαle hSs hSle hβ hZ step T (Nat.le_of_lt hTn)
    have hQ : O.nn (Q K ν h Z A T) := O.nn_add _ _ (O.nn_mul _ _ hν (hZ T)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T))
    -- one step: νQ_{T+1} ≤ (ν + hα)Q_T + hνS
    have b1 := ForceSob.le_mul_right' K O.toCone (hβ T hTn) (hA T)
    have a1 := ForceSob.le_mul_right' K O.toCone (hαle T hTn) (hZ T)
    have c1 := le_mul_nn K O.toCone h _ _ hh (le_add' K O.toCone _ _ _ _ a1 b1)
    have s1 := le_trans' K O.toCone _ _ _ (step T hTn) (le_add' K O.toCone _ _ _ _
      (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul ν (Z T))) c1)
      (le_mul_nn K O.toCone h _ _ hh (hSle T hTn)))
    have s2 := le_mul_nn K O.toCone ν _ _ hν s1
    have os : Le K O.toCone (K.mul ν (Q K ν h Z A (T + 1)))
        (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S))) := by
      refine le_trans' K O.toCone _ _ _ s2 ⟨K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)),
        O.nn_mul _ _ (O.nn_mul _ _ hh hα) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hA T)), ?_⟩
      show K.add (K.mul ν (K.add (K.add (K.mul ν (Z T)) (K.mul h (K.add (K.mul α (Z T)) (K.mul (K.mul ν ν) (A T))))) (K.mul h S)))
          (K.mul (K.mul h α) (K.mul (K.mul h (K.mul ν ν)) (A T)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.add (K.mul ν (Z T)) (K.mul (K.mul h (K.mul ν ν)) (A T)))) (K.mul ν (K.mul h S))
      lib_ring
    -- ν^{T+1}Q_{T+1} ≤ (ν+hα)·ν^T Q_T + ν^{T+1} hS ≤ (ν+hα)^{T+1}(Q_0 + T hνS) + (ν+hα)^{T+1} hνS
    have hνα : O.nn (K.add ν (K.mul h α)) := O.nn_add _ _ hν (O.nn_mul _ _ hh hα)
    have m1 := le_mul_nn K O.toCone (pwr K ν T) _ _ (pwr_nn K O ν hν T) os
    have pm := GronwallSource.pwr_mono K O ν (K.add ν (K.mul h α)) hν ⟨K.mul h α, O.nn_mul _ _ hh hα, rfl⟩ (T + 1)
    have hhS : O.nn (K.mul h S) := O.nn_mul _ _ hh hS
    have src := ForceSob.le_mul_right' K O.toCone pm hhS
    have main := le_mul_nn K O.toCone (K.add ν (K.mul h α)) _ _ hνα ih
    have e0 : K.mul (pwr K ν (T + 1)) (Q K ν h Z A (T + 1)) = K.mul (pwr K ν T) (K.mul ν (Q K ν h Z A (T + 1))) := by
      show K.mul (K.mul ν (pwr K ν T)) _ = _; lib_ring
    rw [e0]
    refine le_trans' K O.toCone _ _ _ m1 ?_
    have e1 : K.mul (pwr K ν T) (K.add (K.mul (K.add ν (K.mul h α)) (Q K ν h Z A T)) (K.mul ν (K.mul h S)))
        = K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K ν T) (Q K ν h Z A T))) (K.mul (pwr K ν (T + 1)) (K.mul h S)) := by
      show _ = K.add _ (K.mul (K.mul ν (pwr K ν T)) _); lib_ring
    rw [e1]
    refine le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ main src) ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul (K.add ν (K.mul h α)) (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h Z A 0) (K.mul (castN K T) (K.mul h S)))))
        (K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T)) (K.mul h S))) K.zero
      = K.mul (K.mul (K.add ν (K.mul h α)) (pwr K (K.add ν (K.mul h α)) T))
        (K.add (Q K ν h Z A 0) (K.mul (K.add K.one (castN K T)) (K.mul h S)))
    lib_ring

end Gen

end GronwallUpto

#print axioms GronwallUpto.gronwall_upto

#print axioms: 1 theorem, no axioms.

79.295 The truncation Cauchy estimate on a window

The comparison of two Galerkin truncations with the coefficient and error bounds asked only for \(t<n\).

ns/TruncCauchyUpto.lean

/-
  ns/TruncCauchyUpto.lean — T37 (goal G859c2g, piece b2): the truncation Cauchy estimate on a finite window, over an
  ordered field. Same as TruncCauchy.trunc_cauchy with the coefficient and error bounds asked only for t < n:
    trunc_cauchy_upto   ν^T·Q_d(T) ≤ (ν + hα)^T·(Q_d(0) + T·h·S)   for T ≤ n
  (GronwallUpto.gronwall_upto in place of gronwall_src). The window bounds come from the large-data local bounds.

  Lean 4 core plus ns/TruncCauchy, ns/GronwallUpto.
-/
import ns.TruncCauchy
import ns.GronwallUpto

namespace TruncCauchyUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy

section Main
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_cauchy_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν α S : R) (hh : O.nn h) (hν : O.nn ν) (hα0 : O.nn α) (hS0 : O.nn S) (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hα : ∀ t, t < n → Le K O.toCone (alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t)) α)
    (hβ : ∀ t, t < n → Le K O.toCone (bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t))) (K.mul ν ν))
    (hS : ∀ t, t < n → Le K O.toCone (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) S) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h α)) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h S)))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have hdN := pw_filter kz (lowB Rc kz) l hd
  have hMN : ∀ p, p ∈ l.filter (lowB Rc kz) → nrm (kz p) ≤ M + 1 := fun p hp => hM p (mem_of_mem_filter (lowB Rc kz) p l hp)
  have hSn : ∀ t, O.nn (K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) := fun t =>
    O.nn_mul _ _ n2 (O.nn_add _ _
      (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hZn _ _) (O.nn_mul _ _ nC1 (hZn _ _)))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ nC2 (hZn _ _)))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (hAn _ _) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hZn _ _))))))
  refine GronwallUpto.gronwall_upto K O ν h α S _ _ (fun t => alph K inv kz (l.filter (lowB Rc kz)) R' ι (restr K (lowB Rc kz) (A t)) (B t))
    (fun t => bet K inv kz (l.filter (lowB Rc kz)) R' t0 ι (restr K (lowB Rc kz) (A t)))
    (fun t => K.mul (two' K) (EB K inv kz l Rc R' t0 ι (A t))) hν hh hα0 hS0 (fun t => hAn _ _) n
    (fun t _ => O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ nC1 (hZn _ _))))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (hAn _ _)))))
    hα (fun t _ => hSn t)
    hS hβ (fun t => hZn _ _) ?_ T hT
  intro s _
  have hsd : ∀ r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r)
      (dF K (restr K (lowB Rc kz) (A (s + 1))) (B (s + 1)) r) (dF K (restr K (lowB Rc kz) (A s)) (B s) r)
      (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r)) := fun r hr =>
    step_sub K h ν (wt K kz r) _ _ _ _ _ _
      (restr_step K l (lowB Rc kz) (TriadSparse.delta K kz) ι h ν (wt K kz) (kv K kz) A hA s r
        (mem_filter_true (lowB Rc kz) r l hr)) (hB s r hr)
  have es := enst_step_on K O.toCone (l.filter (lowB Rc kz)) h ν hh hν (wt K kz) hw _ _ _ hsd
  -- |G|² ≤ 2|X|² + 2|E|²
  have hG := lsum_le_on K O.toCone (l.filter (lowB Rc kz)) (fun r _ => sq2' K O
    (sub3 K (add3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
          (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
            (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)))
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (restr K (lowB Rc kz) (A s)) (restr K (lowB Rc kz) (A s)) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B s) (B s) r))
    (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring)
    (by show K.add (K.add _ (K.add _ (K.neg _))) (K.neg _) = K.add (K.add _ (K.neg _)) (K.add _ (K.neg _)); lib_ring))
  rw [lsum_add, lsum_mul_left, lsum_mul_left] at hG
  have fd := force_diff K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdN h1 M hMN R' t0 hRM ι (restr K (lowB Rc kz) (A s)) (B s)
  have eb0 := lsum_filter_le K O.toCone (lowB Rc kz)
    (fun r => dot3 K (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))
      (sub3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r)
        (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (A s) (A s) r))) (fun _ => dot3_nn K O.toCone _) l
  have eb := le_trans' K O.toCone _ _ _ eb0 (err_bound K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι (A s))
  have sum := le_add' K O.toCone _ _ _ _ (le_mul_nn K O.toCone (two' K) _ _ n2 fd) (le_mul_nn K O.toCone (two' K) _ _ n2 eb)
  have hb := le_mul_nn K O.toCone h _ _ hh (le_trans' K O.toCone _ _ _ hG sum)
  have st := le_trans' K O.toCone _ _ _ es (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) hb)
  refine le_trans' K O.toCone _ _ _ st ⟨K.zero, O.nn_zero, ?_⟩
  unfold alph bet EB two' Zl Al C1' C2'
  generalize castN K 135 = c135
  generalize castN K (cube R') = cb
  generalize iv K inv (R' + 1) = ivR
  generalize inv (castN K (t0 + 1)) = it
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Zd
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (dF K (restr K (lowB Rc kz) (A s)) (B s) p)
    (dF K (restr K (lowB Rc kz) (A s)) (B s) p))) (List.filter (lowB Rc kz) l) = Ad
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (restr K (lowB Rc kz) (A s) q) (restr K (lowB Rc kz) (A s) q)))
    (List.filter (lowB Rc kz) l) = Za
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (B s p) (B s p))) (List.filter (lowB Rc kz) l) = AB
  generalize lsum K (fun p => K.mul (wt K kz p) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Zh
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (hiF K (lowB Rc kz) (A s) p) (hiF K (lowB Rc kz) (A s) p))) l = Ah
  generalize lsum K (fun q => K.mul (wt K kz q) (dot3 K (A s q) (A s q))) l = ZA
  generalize lsum K (fun p => K.mul (K.mul (wt K kz p) (wt K kz p)) (dot3 K (A s p) (A s p))) l = AA
  lib_ring

end Main

end TruncCauchyUpto

#print axioms TruncCauchyUpto.trunc_cauchy_upto

#print axioms: 1 theorem, no axioms.

79.296 The truncation Cauchy estimate with constants on a window

With \(Z(A_t)\le Z_b\), \(A(A_t)\le A_a\), \(A(B_t)\le A_b\) for \(t<n\), the explicit truncation Cauchy bound holds for \(T\le n\).

ns/TruncRateUpto.lean

/-
  ns/TruncRateUpto.lean — T37 (goal G859c2g, piece b3): the truncation Cauchy estimate with explicit constants on a
  finite window, over an ordered field. TruncRate.trunc_rate with the bounds Z(A_t) ≤ Zb, A(A_t) ≤ Aa, A(B_t) ≤ Ab
  asked only for t < n; the conclusion holds for T ≤ n (TruncCauchyUpto.trunc_cauchy_upto). With H2LargeAll and
  H2ToA these bounds hold on the large-data local-time window, uniformly in the truncation.

  Lean 4 core plus ns/TruncRate, ns/TruncCauchyUpto.
-/
import ns.TruncRate
import ns.TruncCauchyUpto

namespace TruncRateUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
theorem trunc_rate_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A t)) Zb) (hAA : ∀ t, t < n → Le K O.toCone (Al K kz l (A t)) Aa)
    (hAB : ∀ t, t < n → Le K O.toCone (Al K kz (l.filter (lowB Rc kz)) (B t)) Ab)
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have n2 : O.nn (two' K) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
  have hiv : O.nn (iv K inv (R' + 1)) := iv_nn K O inv mul_inv h10 R'
  have nC1 : O.nn (C1' K inv R') := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have nC2 : O.nn (C2' K inv R' t0) := O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (O.nn_sq _))
    (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv)
  have n135 : O.nn (castN K 135) := RatModel.nn_cast K O.toCone 135
  have nii : O.nn (K.mul ι ι) := O.nn_sq ι
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have refl := fun (x : R) => le_refl' K O.toCone x
  have hZa : ∀ t, t < n → Le K O.toCone (Zl K kz (l.filter (lowB Rc kz)) (restr K (lowB Rc kz) (A t))) Zb := fun t ht =>
    le_trans' K O.toCone _ _ _ (Z_restr_le K O l kz (lowB Rc kz) (A t)) (hZA t ht)
  have hZh : ∀ t, t < n → Le K O.toCone (Zl K kz l (hiF K (lowB Rc kz) (A t))) (K.mul Aa iR) := fun t ht =>
    Zh_le K O l kz Rc (A t) Aa (hAA t ht) iR hiR hiRn
  have hAh : ∀ t, t < n → Le K O.toCone (Al K kz l (hiF K (lowB Rc kz) (A t))) Aa := fun t ht =>
    le_trans' K O.toCone _ _ _ (lsum_le K O.toCone _ _ (fun p => partH_leW K O (K.mul (wt K kz p) (wt K kz p)) (O.nn_sq _) (A t) p (lowB Rc kz p)) l) (hAA t ht)
  have nZh : O.nn (K.mul Aa iR) := O.nn_mul _ _ hAa hiRn
  refine TruncCauchyUpto.trunc_cauchy_upto K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν _ _ hh hν
    (O.nn_add _ _ (O.nn_mul _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (O.nn_mul _ _ nii (O.nn_mul _ _ nC1 hZb)))
      (O.nn_mul _ _ (O.nn_mul _ _ n2 n2) (O.nn_mul _ _ nii (O.nn_mul _ _ n135 hAb))))
    (O.nn_mul _ _ n2 (O.nn_add _ _ (O.nn_mul _ _ n2 (O.nn_add _ _
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ nZh (O.nn_mul _ _ nC1 hZb))))
        (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ nC2 hZb))))))
      (O.nn_mul _ _ n2 (O.nn_mul _ _ nii (O.nn_mul _ _ hAa (O.nn_mul _ _ n135 nZh))))))
    A B hA hB n ?_ ?_ ?_ T hT
  · intro t ht
    exact le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2)) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ nC1 (hZa t ht))))
      (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 n2) (le_mul_nn K O.toCone _ _ _ nii
        (le_mul_nn K O.toCone _ _ _ n135 (hAB t ht))))
  · intro t ht
    exact le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ n2 (O.nn_mul _ _ n2 n2))
      (le_mul_nn K O.toCone _ _ _ nii (le_mul_nn K O.toCone _ _ _ nC2 (hZa t ht)))) hβc
  · intro t ht
    refine le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
      (le_mul_nn K O.toCone _ _ _ n2 (le_add' K O.toCone _ _ _ _
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hZn _ _) (O.nn_mul _ _ nC1 hZb) (hZh t ht) (le_mul_nn K O.toCone _ _ _ nC1 (hZA t ht)))))
        (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
          (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ nC2 hZb) (hAh t ht) (le_mul_nn K O.toCone _ _ _ nC2 (hZA t ht)))))))
      (le_mul_nn K O.toCone _ _ _ n2 (le_mul_nn K O.toCone _ _ _ nii
        (mm K O _ _ _ _ (hAn _ _) (O.nn_mul _ _ n135 nZh) (hAA t ht) (le_mul_nn K O.toCone _ _ _ n135 (hZh t ht))))))

end Gen

end TruncRateUpto

#print axioms TruncRateUpto.trunc_rate_upto

#print axioms: 1 theorem, no axioms.

79.297 Explicit absorption choices

For \(X\ne 0\) the choice \(D=YX^{-1}\) gives \(XD=Y\); products of nonzero elements are nonzero and \(4\ne 0\), so the absorption hypotheses hold with equality.

ns/ChooseAbsorb.lean

/-
  ns/ChooseAbsorb.lean — T37 (goal G859c2e): explicit choices of the absorption parameters of H2Large, over an
  ordered field with inverses. For X ≠ 0 the choice D = Y·X⁻¹ makes X·D = Y (fill), products of nonzero elements are
  nonzero (nz_mul), and 4 ≠ 0 (four_ne); so with e = ν²·S⁻¹ (S = ι²(k₁ + k₂(1 + 2)) ≠ 0) and
  D₁ = 16³Zb⁵·(9³·4e³)⁻¹, D₂ = 16Zb⁵·(9·4e³)⁻¹, D₃ = Zb³·(4e)⁻¹ the hypotheses H1–H3 of H2Large hold with equality.

  Lean 4 core plus ns/AgmonAbsorb.
-/
import ns.AgmonAbsorb

namespace ChooseAbsorb
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include mul_inv in
theorem fill (X Y : R) (hX : X ≠ K.zero) : K.mul X (K.mul Y (inv X)) = Y := by
  have e := mul_inv X hX
  calc K.mul X (K.mul Y (inv X)) = K.mul Y (K.mul X (inv X)) := by lib_ring
    _ = Y := by rw [e]; lib_ring

include O in
theorem nz_mul (x y : R) (hx : x ≠ K.zero) (hy : y ≠ K.zero) : K.mul x y ≠ K.zero := by
  intro h
  cases O.noZD _ _ h with
  | inl h1 => exact hx h1
  | inr h1 => exact hy h1

include O in
theorem four_ne (h10 : K.one ≠ K.zero) : four K ≠ K.zero := by
  intro h
  have n1 : O.nn K.one := RatModel.nn_one' K O.toCone
  have n3 : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ n1 n1) n1
  have e : K.add K.one (K.add (K.add K.one K.one) K.one) = K.zero := by
    rw [← h]; unfold four; lib_ring
  exact h10 (nn_sum_zero K O _ _ n1 n3 e)

include O mul_inv in
/-- H1-type hypothesis with equality: c·Z ≤ m·(4·(e³·D)) for D = c·Z·(m·4e³)⁻¹ -/
theorem choose_H (c m e Z : R) (h10 : K.one ≠ K.zero) (hm : m ≠ K.zero) (he : e ≠ K.zero) :
    Le K O.toCone (K.mul c Z)
      (K.mul m (K.mul (four K) (K.mul (c3 K e) (K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e)))))))) := by
  have hX : K.mul m (K.mul (four K) (c3 K e)) ≠ K.zero :=
    nz_mul K O _ _ hm (nz_mul K O _ _ (four_ne K O h10) (nz_mul K O _ _ (nz_mul K O _ _ he he) he))
  have f := fill K inv mul_inv _ (K.mul c Z) hX
  refine ⟨K.zero, O.nn_zero, ?_⟩
  calc K.add (K.mul c Z) K.zero = K.mul c Z := by lib_ring
    _ = K.mul (K.mul m (K.mul (four K) (c3 K e))) (K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e))))) := f.symm
    _ = _ := by lib_ring

include O mul_inv in
/-- the choice e = ν²·S⁻¹ gives S·e ≤ ν² (with equality) -/
theorem choose_eps (S ν : R) (hS : S ≠ K.zero) :
    Le K O.toCone (K.mul S (K.mul (K.mul ν ν) (inv S))) (K.mul ν ν) := by
  rw [fill K inv mul_inv S (K.mul ν ν) hS]
  exact le_refl' K O.toCone _

end Gen

end ChooseAbsorb

#print axioms ChooseAbsorb.fill
#print axioms ChooseAbsorb.nz_mul
#print axioms ChooseAbsorb.four_ne
#print axioms ChooseAbsorb.choose_H
#print axioms ChooseAbsorb.choose_eps

#print axioms: 5 theorems, no axioms.

79.298 The absorption parameters in closed form

For \(S=\iota ^2(k_1+3k_2)\ne 0\) and \(\nu \ne 0\) there are \(e,D_1,D_2,D_3\ge 0\) (\(e=\nu ^2S^{-1}\)) satisfying all hypotheses of the local \(H^2\) bound.

ns/H2LargeExplicit.lean

/-
  ns/H2LargeExplicit.lean — T37 (goal G859c2e, piece b): the absorption parameters of H2Large in closed form, over an
  ordered field with inverses. For S = ι²(k₁ + k₂(1 + 2)) ≠ 0 and ν ≠ 0, with
    e = ν²·S⁻¹,  D₁ = 16³Zb⁵·(9³·4e³)⁻¹,  D₂ = 16Zb⁵·(9·4e³)⁻¹,  D₃ = Zb³·(4e)⁻¹,
  the hypotheses H1, H2, H3 and ε ≤ ν² of H2Large.h2_large hold (h1_eq, h2_eq, h3_eq, eps_eq); so the local H² bound
  holds with D = ι²(k₁(D₁ + 8Zb²) + k₂(D₂ + 2D₃)), a polynomial in Zb, ι, ν⁻¹.

  Lean 4 core plus ns/ChooseAbsorb.
-/
import ns.ChooseAbsorb
import ns.ForceAgmonW
import ns.AgmonCollapse

namespace H2LargeExplicit
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge RootYoung AgmonAbsorb ChooseAbsorb

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- the inverse of a nonnegative nonzero element is nonnegative -/
theorem inv_nn (y : R) (h10 : K.one ≠ K.zero) (hy : O.nn y) (hy0 : y ≠ K.zero) : O.nn (inv y) := by
  cases O.total K.zero (inv y) with
  | inl h =>
    have ⟨d, hd, e⟩ := h
    have e' : d = inv y := by rw [← e]; lib_ring
    rw [← e']; exact hd
  | inr h =>
    have ⟨d, hd, e⟩ := h
    have hm := mul_inv y hy0
    have e2 : K.add K.one (K.mul y d) = K.zero := by
      calc K.add K.one (K.mul y d) = K.add (K.mul y (inv y)) (K.mul y d) := by rw [hm]
        _ = K.mul y (K.add (inv y) d) := by lib_ring
        _ = K.zero := by rw [e]; lib_ring
    exact absurd (nn_sum_zero K O _ _ (RatModel.nn_one' K O.toCone) (O.nn_mul _ _ hy hd) e2) h10

def Dfor (c m e Z : R) : R := K.mul (K.mul c Z) (inv (K.mul m (K.mul (four K) (c3 K e))))

include O mul_inv in
theorem dfor_nn (c m e Z : R) (h10 : K.one ≠ K.zero) (hc : O.nn c) (hm : O.nn m) (he : O.nn e) (hZ : O.nn Z)
    (hm0 : m ≠ K.zero) (he0 : e ≠ K.zero) : O.nn (Dfor K inv c m e Z) := by
  have hX : K.mul m (K.mul (four K) (c3 K e)) ≠ K.zero :=
    nz_mul K O _ _ hm0 (nz_mul K O _ _ (four_ne K O h10) (nz_mul K O _ _ (nz_mul K O _ _ he0 he0) he0))
  have nX : O.nn (K.mul m (K.mul (four K) (c3 K e))) :=
    O.nn_mul _ _ hm (O.nn_mul _ _ (four_nn K O) (O.nn_mul _ _ (O.nn_sq _) he))
  exact O.nn_mul _ _ (O.nn_mul _ _ hc hZ) (inv_nn K O inv mul_inv _ h10 nX hX)

include O mul_inv in
theorem h1_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) (h9 : castN K 9 ≠ K.zero) :
    Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb))
      (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) (Dfor K inv (c3 K (castN K 16)) (c3 K (castN K 9)) e (q5 K Zb))))) :=
  choose_H K O inv mul_inv _ _ e _ h10 (nz_mul K O _ _ (nz_mul K O _ _ h9 h9) h9) he

include O mul_inv in
theorem h2_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) (h9 : castN K 9 ≠ K.zero) :
    Le K O.toCone (K.mul (castN K 16) (q5 K Zb))
      (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) (Dfor K inv (castN K 16) (castN K 9) e (q5 K Zb))))) :=
  choose_H K O inv mul_inv _ _ e _ h10 h9 he

include O mul_inv in
theorem h3_eq (e Zb : R) (h10 : K.one ≠ K.zero) (he : e ≠ K.zero) :
    Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e (K.mul (c3 K Zb) (inv (K.mul (four K) e))))) := by
  have hX : K.mul (four K) e ≠ K.zero := nz_mul K O _ _ (four_ne K O h10) he
  have f := fill K inv mul_inv _ (c3 K Zb) hX
  refine ⟨K.zero, O.nn_zero, ?_⟩
  calc K.add (c3 K Zb) K.zero = c3 K Zb := by lib_ring
    _ = K.mul (K.mul (four K) e) (K.mul (c3 K Zb) (inv (K.mul (four K) e))) := f.symm
    _ = _ := by lib_ring

include O mul_inv in
/-- **The absorption parameters of H2Large exist (in closed form).** -/
theorem params (ι ν Zb : R) (h10 : K.one ≠ K.zero) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb) (h9 : castN K 9 ≠ K.zero)
    (S : R) (hS : S = K.mul (K.mul ι ι) (K.add (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add K.one (castN K 2)))))
    (hSn : O.nn S) (hS0 : S ≠ K.zero) :
    ∃ e D1 D2 D3 : R, O.nn e ∧ O.nn D1 ∧ O.nn D2 ∧ O.nn D3 ∧
      Le K O.toCone (K.mul (c3 K (castN K 16)) (q5 K Zb)) (K.mul (c3 K (castN K 9)) (K.mul (four K) (K.mul (c3 K e) D1))) ∧
      Le K O.toCone (K.mul (castN K 16) (q5 K Zb)) (K.mul (castN K 9) (K.mul (four K) (K.mul (c3 K e) D2))) ∧
      Le K O.toCone (c3 K Zb) (K.mul (four K) (K.mul e D3)) ∧
      Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν) := by
  have hiS : inv S ≠ K.zero := by
    intro h; have m := mul_inv S hS0; rw [h] at m; exact h10 (by rw [← m]; lib_ring)
  have he0 : K.mul (K.mul ν ν) (inv S) ≠ K.zero := nz_mul K O _ _ (nz_mul K O _ _ hν0 hν0) hiS
  have hen : O.nn (K.mul (K.mul ν ν) (inv S)) := O.nn_mul _ _ (O.nn_sq ν) (inv_nn K O inv mul_inv S h10 hSn hS0)
  have n16 := RatModel.nn_cast K O.toCone 16
  have n9 := RatModel.nn_cast K O.toCone 9
  have hZ5 : O.nn (q5 K Zb) := O.nn_mul _ _ hZb (O.nn_mul _ _ (O.nn_sq _) (O.nn_sq _))
  have hX3 : K.mul (four K) (K.mul (K.mul ν ν) (inv S)) ≠ K.zero := nz_mul K O _ _ (four_ne K O h10) he0
  refine ⟨K.mul (K.mul ν ν) (inv S),
    Dfor K inv (c3 K (castN K 16)) (c3 K (castN K 9)) (K.mul (K.mul ν ν) (inv S)) (q5 K Zb),
    Dfor K inv (castN K 16) (castN K 9) (K.mul (K.mul ν ν) (inv S)) (q5 K Zb),
    K.mul (c3 K Zb) (inv (K.mul (four K) (K.mul (K.mul ν ν) (inv S)))), hen,
    dfor_nn K O inv mul_inv _ _ _ _ h10 (O.nn_mul _ _ (O.nn_sq _) n16) (O.nn_mul _ _ (O.nn_sq _) n9) hen hZ5
      (nz_mul K O _ _ (nz_mul K O _ _ h9 h9) h9) he0,
    dfor_nn K O inv mul_inv _ _ _ _ h10 n16 n9 hen hZ5 h9 he0,
    O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) hZb)
      (inv_nn K O inv mul_inv _ h10 (O.nn_mul _ _ (four_nn K O) hen) hX3),
    h1_eq K O inv mul_inv _ Zb h10 he0 h9, h2_eq K O inv mul_inv _ Zb h10 he0 h9, h3_eq K O inv mul_inv _ Zb h10 he0, ?_⟩
  have c := choose_eps K O inv mul_inv S ν hS0
  refine le_trans' K O.toCone _ _ _ (ForceAgmonW.le_eq K O _ _ ?_) c
  rw [hS]; lib_ring

end Gen

end H2LargeExplicit

#print axioms H2LargeExplicit.inv_nn
#print axioms H2LargeExplicit.dfor_nn
#print axioms H2LargeExplicit.h1_eq
#print axioms H2LargeExplicit.h2_eq
#print axioms H2LargeExplicit.h3_eq
#print axioms H2LargeExplicit.params

#print axioms: 6 theorems, no axioms.

79.299 From the \(H^2\) energy to the \(H^2\) norm

If \(\nu >0\) and \(\nu A+h\varepsilon B\le X\) with \(h,\varepsilon ,B\ge 0\), then \(A\le X\nu ^{-1}\).

ns/H2ToA.lean

/-
  ns/H2ToA.lean — T37 (goal G859c2g, piece a): from the H² energy bound to an H² bound, over an ordered field with
  inverses. If ν > 0 (nonnegative, nonzero), h, ε, B ≥ 0 and νA + hεB ≤ X, then
    a_of_E   A ≤ X·ν⁻¹,
  the bound Aa (large truncation) and Ab (small truncation) that TruncRate.trunc_cauchy needs, from H2LargeAll.

  Lean 4 core plus ns/H2LargeExplicit.
-/
import ns.H2LargeExplicit

namespace H2ToA
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- **νA + hεB ≤ X ⇒ A ≤ X/ν** -/
theorem a_of_E (ν h ε A B X : R) (h10 : K.one ≠ K.zero) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hh : O.nn h)
    (hε : O.nn ε) (hB : O.nn B)
    (hE : Le K O.toCone (K.add (K.mul ν A) (K.mul (K.mul h ε) B)) X) : Le K O.toCone A (K.mul X (inv ν)) := by
  have s1 : Le K O.toCone (K.mul ν A) X :=
    le_trans' K O.toCone _ _ _ ⟨K.mul (K.mul h ε) B, O.nn_mul _ _ (O.nn_mul _ _ hh hε) hB, rfl⟩ hE
  have hi := H2LargeExplicit.inv_nn K O inv mul_inv ν h10 hν hν0
  have s2 := le_mul_nn K O.toCone (inv ν) _ _ hi s1
  have m := mul_inv ν hν0
  have e1 : K.mul (inv ν) (K.mul ν A) = A := by
    calc K.mul (inv ν) (K.mul ν A) = K.mul (K.mul ν (inv ν)) A := by lib_ring
      _ = A := by rw [m]; lib_ring
  have e2 : K.mul (inv ν) X = K.mul X (inv ν) := by lib_ring
  rw [e1, e2] at s2
  exact s2

end Gen

end H2ToA

#print axioms H2ToA.a_of_E

#print axioms: 1 theorem, no axioms.

79.300 One \(H^2\) constant on the window

If \(\nu A_t+h\varepsilon B_t\le E_0+t\,hD\) for \(t\le n\), then \(A_t\le (E_0+n\,hD)\nu ^{-1}\) for \(t\le n\).

ns/WindowA.lean

/-
  ns/WindowA.lean — T37 (goal G859c2g, piece c1): the window H² bound in the form TruncRateUpto needs, over an
  ordered field with inverses. If for every t ≤ n
    νA_t + hεB_t ≤ E_0 + t·h·D       (H2LargeAll; h, D ≥ 0, ε, B_t ≥ 0)
  then for every t ≤ n
    window_a   A_t ≤ (E_0 + n·h·D)·ν⁻¹,
  a single constant Aa on the whole window (t·h·D ≤ n·h·D).

  Lean 4 core plus ns/H2ToA.
-/
import ns.H2ToA

namespace WindowA
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one)

include O mul_inv in
/-- **One H² constant on the window.** -/
theorem window_a (ν h ε D E0 : R) (A B : Nat → R) (h10 : K.one ≠ K.zero) (hν : O.nn ν) (hν0 : ν ≠ K.zero)
    (hh : O.nn h) (hε : O.nn ε) (hD : O.nn D) (hB : ∀ t, O.nn (B t)) (n : Nat)
    (hE : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (A t)) (K.mul (K.mul h ε) (B t)))
      (K.add E0 (K.mul (castN K t) (K.mul h D)))) :
    ∀ t, t ≤ n → Le K O.toCone (A t) (K.mul (K.add E0 (K.mul (castN K n) (K.mul h D))) (inv ν)) := by
  intro t ht
  have c := RatModel.cast_mono K O.toCone _ _ ht
  have m := ForceSob.le_mul_right' K O.toCone c (O.nn_mul _ _ hh hD)
  have X := le_trans' K O.toCone _ _ _ (hE t ht) (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone E0) m)
  exact H2ToA.a_of_E K O inv mul_inv ν h ε (A t) (B t) _ h10 hν hν0 hh hε (hB t) X

end Gen

end WindowA

#print axioms WindowA.window_a

#print axioms: 1 theorem, no axioms.

79.301 The large-data local \(H^2\) bound with chosen parameters

Under the local-time hypotheses there are \(e\ge 0\) and \(D\) with \(Z_t\le Z_b\) and \(\nu A_t+h\varepsilon B_t\le \nu A_0+h\varepsilon B_0+t\,hD\) for \(t\le n\), uniformly in the truncation.

ns/H2LargeAll.lean

/-
  ns/H2LargeAll.lean — T37 (goal G859c2f): the large-data local H² bound without free absorption parameters, over an
  ordered field with inverses. Under the hypotheses of EnstLocalTime.local_time, with ν ≠ 0 and
  S = ι²(k₁ + k₂(1 + 2)) ≠ 0, there are e ≥ 0 and D such that for every t ≤ n
    h2_large_all   Z_t ≤ Zb   and   νA_t + hεB_t ≤ νA_0 + hεB_0 + t·h·D,   ε = ι²(k₁e + k₂(e + 2e)) ≤ ν²
  (e, D in closed form by H2LargeExplicit.params).

  Lean 4 core plus ns/H2Large, ns/H2LargeExplicit.
-/
import ns.H2Large
import ns.H2LargeExplicit

namespace H2LargeAll
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The large-data local H² bound, uniform in the truncation, parameters chosen.** -/
theorem h2_large_all (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (hZ0 : K.mul ν Zb = K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (n : Nat) (hn : Le K O.toCone (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc))
      (K.mul Zb Zb)))) (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0))
    (h9 : castN K 9 ≠ K.zero)
    (S : R) (hS : S = K.mul (K.mul ι ι) (K.add (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add K.one (castN K 2)))))
    (hSn : O.nn S) (hS0 : S ≠ K.zero) :
    ∃ e D : R, O.nn e ∧ ∀ t, t ≤ n →
      Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
      Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
          (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
        (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e) (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
          (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
          (K.mul (castN K t) (K.mul h D))) := by
  have ⟨e, D1, D2, D3, he, hD1, hD2, hD3, H1, H2, H3, hε⟩ :=
    H2LargeExplicit.params K O inv mul_inv ι ν Zb h10 hν0 hZb h9 S hS hSn hS0
  exact ⟨e, _, he, fun t ht => H2Large.h2_large K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    a N hN0 hN hs choice hZ0 n hn e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht⟩

end Gen

end H2LargeAll

#print axioms H2LargeAll.h2_large_all

#print axioms: 1 theorem, no axioms.

79.302 A root-free power bound

For \(a\ge 0\): \((1+a)^m(1-ma)\le 1\); hence \(2ma\le 1\) implies \((1+a)^m\le 2\).

ns/PowBound.lean

/-
  ns/PowBound.lean — T37 (goal G859c2h, piece b): a root-free bound for (1 + a)^m, over an ordered ring. For a ≥ 0:
    weier      (1 + a)^m · (1 − m·a) ≤ 1                   (Weierstrass; the step loses (m + 1)a²(1 + a)^m ≥ 0)
    pow_le_two 2·m·a ≤ 1  ⇒  (1 + a)^m ≤ 2
  so (1 + hα/ν)^n ≤ 2^⌈2nhα/ν⌉: the Gronwall factor (ν + hα)^T/ν^T of TruncRateUpto stays bounded uniformly as
  h → 0 with T·h fixed (G859c2h).

  Lean 4 core plus ns/EnstGronwall (pwr).
-/
import ns.EnstGronwall

namespace PowBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(1 + a)^m (1 − m a) ≤ 1** -/
theorem weier (a : R) (ha : O.nn a) : ∀ m : Nat,
    Le K O.toCone (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))) K.one
  | 0 => ⟨K.zero, O.nn_zero, by show K.add (K.mul K.one (K.add K.one (K.neg (K.mul K.zero a)))) K.zero = K.one; lib_ring⟩
  | m + 1 => by
    have ih := weier a ha m
    have hP : O.nn (pwr K (K.add K.one a) m) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) m
    -- loss term (m + 1) a² (1 + a)^m ≥ 0
    have hL : O.nn (K.mul (K.add K.one (castN K m)) (K.mul (K.mul a a) (pwr K (K.add K.one a) m))) :=
      O.nn_mul _ _ (RatModel.nn_cast K O.toCone (m + 1)) (O.nn_mul _ _ (O.nn_sq a) hP)
    refine le_trans' K O.toCone _ _ _ ⟨_, hL, ?_⟩ ih
    show K.add (K.mul (K.mul (K.add K.one a) (pwr K (K.add K.one a) m))
        (K.add K.one (K.neg (K.mul (K.add K.one (castN K m)) a))))
        (K.mul (K.add K.one (castN K m)) (K.mul (K.mul a a) (pwr K (K.add K.one a) m)))
      = K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))
    lib_ring

/-- **2ma ≤ 1 ⇒ (1 + a)^m ≤ 2** -/
theorem pow_le_two (a : R) (ha : O.nn a) (m : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) K.one) :
    Le K O.toCone (pwr K (K.add K.one a) m) (K.add K.one K.one) := by
  have w := weier K O a ha m
  have hP : O.nn (pwr K (K.add K.one a) m) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) m
  -- 2ma + d = 1, hence P + P·d = 2·P(1 − ma) ≤ 2
  have ⟨d, hd, e⟩ := hm
  have h2 := le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) w
  refine le_trans' K O.toCone _ _ _ (le_trans' K O.toCone _ _ _ ⟨K.mul (pwr K (K.add K.one a) m) d, O.nn_mul _ _ hP hd, ?_⟩ h2)
    ⟨K.zero, O.nn_zero, by lib_ring⟩
  calc K.add (pwr K (K.add K.one a) m) (K.mul (pwr K (K.add K.one a) m) d)
      = K.add (K.mul (K.add K.one K.one) (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))))
          (K.mul (pwr K (K.add K.one a) m) (K.add (K.add (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) d)
            (K.neg K.one))) := by lib_ring
    _ = K.mul (K.add K.one K.one) (K.mul (pwr K (K.add K.one a) m) (K.add K.one (K.neg (K.mul (castN K m) a)))) := by
          rw [e]; lib_ring

end Gen

end PowBound

#print axioms PowBound.weier
#print axioms PowBound.pow_le_two

#print axioms: 2 theorems, no axioms.

79.303 Splitting the Gronwall factor

With \(\nu \nu ^{-1}=1\): \((\nu +h\alpha )^T=\nu ^T(1+h\alpha \nu ^{-1})^T\), and \(x^{T_1+T_2}=x^{T_1}x^{T_2}\).

ns/PowFactor.lean

/-
  ns/PowFactor.lean — T37 (goal G859c2h, piece c): the Gronwall factor of TruncRateUpto split off, over an ordered
  field with inverses. With ν·ν⁻¹ = 1:
    pwr_factor   (ν + hα)^T = ν^T·(1 + hαν⁻¹)^T,
    pwr_mul      (x)^(T₁ + T₂) = x^T₁·x^T₂,
  so with PowBound.pow_le_two, (ν + hα)^T ≤ ν^T·2^k whenever T is cut into k pieces of length m with 2m·hαν⁻¹ ≤ 1 —
  a bound depending only on T·h, uniform as h → 0.

  Lean 4 core plus ns/EnstGronwall (pwr).
-/
import ns.EnstGronwall

namespace PowFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R)

theorem pwr_factor (ν hα iν : R) (hν : K.mul ν iν = K.one) : ∀ T : Nat,
    pwr K (K.add ν hα) T = K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)
  | 0 => by show K.one = K.mul K.one K.one; lib_ring
  | T + 1 => by
    show K.mul (K.add ν hα) (pwr K (K.add ν hα) T)
      = K.mul (K.mul ν (pwr K ν T)) (K.mul (K.add K.one (K.mul hα iν)) (pwr K (K.add K.one (K.mul hα iν)) T))
    rw [pwr_factor ν hα iν hν T]
    calc K.mul (K.add ν hα) (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))
        = K.add (K.mul ν (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)))
            (K.mul hα (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))) := by lib_ring
      _ = K.add (K.mul ν (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T)))
            (K.mul (K.mul hα (K.mul ν iν)) (K.mul (pwr K ν T) (pwr K (K.add K.one (K.mul hα iν)) T))) := by
          rw [hν]; lib_ring
      _ = _ := by lib_ring

theorem pwr_mul (x : R) (T₁ : Nat) : ∀ T₂ : Nat, pwr K x (T₁ + T₂) = K.mul (pwr K x T₁) (pwr K x T₂)
  | 0 => by show pwr K x T₁ = K.mul (pwr K x T₁) K.one; lib_ring
  | T₂ + 1 => by
    show K.mul x (pwr K x (T₁ + T₂)) = K.mul (pwr K x T₁) (K.mul x (pwr K x T₂))
    rw [pwr_mul x T₁ T₂]; lib_ring

end Gen

end PowFactor

#print axioms PowFactor.pwr_factor
#print axioms PowFactor.pwr_mul

#print axioms: 2 theorems, no axioms.

79.304 The Gronwall factor by the window length

For \(a\ge 0\) and \(2ma\le 1\): \((1+a)^{mk}\le 2^k\).

ns/GronwallFactor.lean

/-
  ns/GronwallFactor.lean — T37 (goal G859c2h, piece d): the Gronwall factor bounded by the window length, over an
  ordered ring. For a ≥ 0 and 2m·a ≤ 1:
    factor_pow   (1 + a)^(m·k) ≤ 2^k,
  so with PowFactor.pwr_factor, (ν + hα)^(m·k) ≤ ν^(m·k)·2^k where k ≈ 2·T·hα/ν depends only on T·h — the constant of
  TruncRateUpto stays bounded as h → 0 at fixed window length (G859c2h).

  Lean 4 core plus ns/PowBound, ns/PowFactor, ns/WeightedCS.
-/
import ns.PowBound
import ns.PowFactor
import ns.WeightedCS

namespace GronwallFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(1 + a)^(m·k) ≤ 2^k** -/
theorem factor_pow (a : R) (ha : O.nn a) (m : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) a)) K.one) :
    ∀ k : Nat, Le K O.toCone (pwr K (K.add K.one a) (m * k)) (pwr K (K.add K.one K.one) k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have ih := factor_pow a ha m hm k
    have h1 := PowBound.pow_le_two K O a ha m hm
    have e : pwr K (K.add K.one a) (m * (k + 1)) = K.mul (pwr K (K.add K.one a) (m * k)) (pwr K (K.add K.one a) m) :=
      PowFactor.pwr_mul K (K.add K.one a) (m * k) m
    rw [e]
    have nP : O.nn (pwr K (K.add K.one a) (m * k)) :=
      pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) ha) _
    have n2 : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have s := WeightedCS.le_mul_both' K O.toCone _ _ _ _ nP n2 ih h1
    refine le_trans' K O.toCone _ _ _ s ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.mul (pwr K (K.add K.one K.one) k) (K.add K.one K.one)) K.zero
      = K.mul (K.add K.one K.one) (pwr K (K.add K.one K.one) k)
    lib_ring

end Gen

end GronwallFactor

#print axioms GronwallFactor.factor_pow

#print axioms: 1 theorem, no axioms.

79.305 Truncation Cauchy on the large-data window

With the window \(H^2\) energy bounds for the large and the small truncation and \(Z\le Z_b\), the explicit truncation Cauchy bound holds for every \(T\le n\).

ns/LargeCauchy.lean

/-
  ns/LargeCauchy.lean — T37 (goal G859c2g, piece c2): the truncation Cauchy estimate on the large-data window, over an
  ordered field with inverses. If the large truncation A and the small truncation B satisfy the window H² energy
  bounds of H2LargeAll (νA_t + hε B_t ≤ E + t·h·D for t ≤ n) and Z(A_t) ≤ Zb for t < n, then with
  Aa = (E_A + n·h·D_A)/ν and Ab = (E_B + n·h·D_B)/ν (WindowA) the explicit truncation Cauchy bound of
  TruncRateUpto.trunc_rate_upto holds for every T ≤ n:
    large_cauchy   ν^T Q_d(T) ≤ (ν + hα)^T (Q_d(0) + T·h·S),  S → 0 as the cutoff grows,
  i.e. on the large-data local-time window the Galerkin truncations form a Cauchy family.

  Lean 4 core plus ns/TruncRateUpto, ns/WindowA.
-/
import ns.TruncRateUpto
import ns.WindowA

namespace LargeCauchy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy on the large-data window.** -/
theorem large_cauchy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A t)) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A t)))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (A t p) (A t p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B t)))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B t p) (B t p)))
        (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) (T : Nat) (hT : T ≤ n) :
    Le K O.toCone (K.mul (pwr K ν T)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) T))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) T)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) 0)
          (K.mul (castN K T) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) := by
  have nB3 : ∀ (L : List I) (u : I → V3 R), O.nn (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
      (dot3 K (u p) (u p))) L) := fun L u =>
    lsum_nn K O.toCone _ (fun p => O.nn_mul _ _ (O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) (dot3_nn K O.toCone _)) L
  have wa := WindowA.window_a K O inv mul_inv ν h εA DA EA (fun t => Al K kz l (A t))
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (A t p) (A t p))) l)
    h10 hν hν0 hh hεA hDA (fun t => nB3 l (A t)) n hEA
  have wb := WindowA.window_a K O inv mul_inv ν h εB DB EB (fun t => Al K kz (l.filter (lowB Rc kz)) (B t))
    (fun t => lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p)) (dot3 K (B t p) (B t p)))
      (l.filter (lowB Rc kz)))
    h10 hν hν0 hh hεB hDB (fun t => nB3 _ (B t)) n hEB
  exact TruncRateUpto.trunc_rate_upto K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR A B hA hB n hZA (fun t ht => by rw [hAa_def]; exact wa t (Nat.le_of_lt ht))
    (fun t ht => by rw [hAb_def]; exact wb t (Nat.le_of_lt ht)) hβc T hT

end Gen

end LargeCauchy

#print axioms LargeCauchy.large_cauchy

#print axioms: 1 theorem, no axioms.

79.306 The Gronwall factor uniform in the time step

With \(\nu \nu ^{-1}=1\) and \(2m\,h\alpha \nu ^{-1}\le 1\): \((\nu +h\alpha )^{mk}\le \nu ^{mk}2^k\).

ns/GronwallH.lean

/-
  ns/GronwallH.lean — T37 (goal G859c2h, piece e): the Gronwall factor of the truncation Cauchy estimate, uniform in
  the time step, over an ordered field with inverses. For ν ≥ 0 with ν·ν⁻¹ = 1, hα·ν⁻¹ ≥ 0 and 2m·hαν⁻¹ ≤ 1:
    gronwall_h   (ν + hα)^(m·k) ≤ ν^(m·k)·2^k.
  With T = m·k steps and m ≈ ν/(2hα), k ≈ 2Thα/ν = 2(T·h)α/ν depends only on the time T·h: the factor in
  LargeCauchy.large_cauchy stays bounded as h → 0 at fixed window length.

  Lean 4 core plus ns/GronwallFactor.
-/
import ns.GronwallFactor

namespace GronwallH
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(ν + hα)^(m·k) ≤ ν^(m·k)·2^k** -/
theorem gronwall_h (ν hα iν : R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one) (ha : O.nn (K.mul hα iν)) (m k : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one) :
    Le K O.toCone (pwr K (K.add ν hα) (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k)) := by
  rw [PowFactor.pwr_factor K ν hα iν hνi (m * k)]
  exact le_mul_nn K O.toCone _ _ _ (pwr_nn K O ν hν _) (GronwallFactor.factor_pow K O (K.mul hα iν) ha m hm k)

end Gen

end GronwallH

#print axioms GronwallH.gronwall_h

#print axioms: 1 theorem, no axioms.

79.307 Chaining window estimates

If \(d_{k+1}\le c\,d_k+s\) with \(c\ge 0\), then \(d_K\le c^Kd_0+s\sum _{j<K}c^j\).

ns/WindowChain.lean

/-
  ns/WindowChain.lean — T37 (goal G795b, piece a): chaining window estimates, over an ordered ring. If on consecutive
  windows the truncation difference energy obeys d_{k+1} ≤ c·d_k + s (c, s ≥ 0; LargeCauchy on window k, with the
  restart at the window start), then
    window_chain   d_K ≤ c^K·d_0 + s·Σ_{j<K} c^j.
  With c and s from LargeCauchy/GronwallH (c ≈ 2^k, s → 0 as the cutoff grows) the Cauchy property passes from one
  window to K windows — as long as each window's local-time hypotheses hold (bounded Q at the window starts).

  Lean 4 core plus ns/EnstGronwall (pwr), NSChain (sumN).
-/
import ns.EnstGronwall

namespace WindowChain
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- c·Σ_{j<k} c^j + 1 = Σ_{j<k+1} c^j -/
theorem geom_shift (c : R) : ∀ k : Nat,
    K.add (K.mul c (sumN K (pwr K c) k)) K.one = sumN K (pwr K c) (k + 1)
  | 0 => by show K.add (K.mul c K.zero) K.one = K.add K.zero K.one; lib_ring
  | k + 1 => by
    have ih := geom_shift c k
    show K.add (K.mul c (K.add (sumN K (pwr K c) k) (pwr K c k))) K.one
      = K.add (sumN K (pwr K c) (k + 1)) (K.mul c (pwr K c k))
    rw [← ih]; lib_ring

/-- **d_K ≤ c^K d_0 + s Σ_{j<K} c^j** -/
theorem window_chain (c s : R) (d : Nat → R) (hc : O.nn c)
    (hstep : ∀ k, Le K O.toCone (d (k + 1)) (K.add (K.mul c (d k)) s)) :
    ∀ K' : Nat, Le K O.toCone (d K') (K.add (K.mul (pwr K c K') (d 0)) (K.mul s (sumN K (pwr K c) K'))) := by
  intro K'
  induction K' with
  | zero => exact ⟨K.zero, O.nn_zero, by show K.add (d 0) K.zero = K.add (K.mul K.one (d 0)) (K.mul s K.zero); lib_ring⟩
  | succ k ih =>
    have m := le_mul_nn K O.toCone c _ _ hc ih
    have t := le_trans' K O.toCone _ _ _ (hstep k) (le_add' K O.toCone _ _ _ _ m (le_refl' K O.toCone s))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul c (K.add (K.mul (pwr K c k) (d 0)) (K.mul s (sumN K (pwr K c) k)))) s) K.zero
      = K.add (K.mul (K.mul c (pwr K c k)) (d 0)) (K.mul s (sumN K (pwr K c) (k + 1)))
    rw [← geom_shift K c k]; lib_ring

end Gen

end WindowChain

#print axioms WindowChain.geom_shift
#print axioms WindowChain.window_chain

#print axioms: 2 theorems, no axioms.

79.308 Restarting the truncation estimate on consecutive windows

LargeCauchy on \([nk,n(k+1)]\) with \(d_k=Q_d(nk)\) gives \(\nu ^nd_{k+1}\le P(d_k+s_0)\), hence \(d_K\le c^Kd_0+cs_0\sum _{j<K}c^j\), \(c=P\nu ^{-n}\).

ns/WindowShift.lean

/-
  ns/WindowShift.lean — T37 (goal G795b, piece b): the large-data truncation Cauchy estimate carried across
  consecutive windows of n steps, over an ordered field with inverses.
    pwr_inv        ν^n·(ν⁻¹)^n = 1,
    scale_chain    ν^n d_{k+1} ≤ P (d_k + s₀) for all k  ⇒  d_K ≤ c^K d_0 + c s₀ Σ_{j<K} c^j,  c = P (ν⁻¹)^n,
    large_window   LargeCauchy.large_cauchy restarted at time n·k (the Galerkin step hypotheses hold at every time,
                   so the shifted sequences A(n·k + ·), B(n·k + ·) satisfy them unchanged): with d_k = Q_d(n·k),
                   ν^n d_{k+1} ≤ P (d_k + n·h·S),
    large_chain    the two combined: if every window start satisfies the local-time hypotheses (Z ≤ Zb and the
                   window H² energy bounds with the same E, D), then d_K ≤ c^K d_0 + c·n·h·S·Σ_{j<K} c^j.
  The remaining hypothesis — the same Zb, E at each window start — is the discrete form of "Q bounded on [0, t₄]".

  Lean 4 core plus ns/LargeCauchy, ns/WindowChain.
-/
import ns.LargeCauchy
import ns.WindowChain

namespace WindowShift
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- ν^n·(ν⁻¹)^n = 1 -/
theorem pwr_inv (ν iν : R) (hνi : K.mul ν iν = K.one) : ∀ n : Nat, K.mul (pwr K ν n) (pwr K iν n) = K.one
  | 0 => by show K.mul K.one K.one = K.one; lib_ring
  | n + 1 => by
    show K.mul (K.mul ν (pwr K ν n)) (K.mul iν (pwr K iν n)) = K.one
    calc K.mul (K.mul ν (pwr K ν n)) (K.mul iν (pwr K iν n))
        = K.mul (K.mul ν iν) (K.mul (pwr K ν n) (pwr K iν n)) := by lib_ring
      _ = K.mul K.one K.one := by rw [hνi, pwr_inv ν iν hνi n]
      _ = K.one := by lib_ring

/-- one window, rescaled: ν^n x ≤ P (y + s₀) ⇒ x ≤ c y + c s₀ with c = P (ν⁻¹)^n -/
theorem scale_step (ν iν P s0 x y : R) (n : Nat) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (h : Le K O.toCone (K.mul (pwr K ν n) x) (K.mul P (K.add y s0))) :
    Le K O.toCone x (K.add (K.mul (K.mul P (pwr K iν n)) y) (K.mul (K.mul P (pwr K iν n)) s0)) := by
  have m := le_mul_nn K O.toCone (pwr K iν n) _ _ (pwr_nn K O iν hiν n) h
  have e1 : K.mul (pwr K iν n) (K.mul (pwr K ν n) x) = x := by
    calc K.mul (pwr K iν n) (K.mul (pwr K ν n) x) = K.mul (K.mul (pwr K ν n) (pwr K iν n)) x := by lib_ring
      _ = K.mul K.one x := by rw [pwr_inv K ν iν hνi n]
      _ = x := by lib_ring
  have e2 : K.mul (pwr K iν n) (K.mul P (K.add y s0))
      = K.add (K.mul (K.mul P (pwr K iν n)) y) (K.mul (K.mul P (pwr K iν n)) s0) := by lib_ring
  rw [e1, e2] at m
  exact m

/-- **window chain from the unscaled window bound** -/
theorem scale_chain (ν iν P s0 : R) (n : Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hP : O.nn P) (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν n) (d (k + 1))) (K.mul P (K.add (d k) s0))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) K') (d 0))
        (K.mul (K.mul (K.mul P (pwr K iν n)) s0) (sumN K (pwr K (K.mul P (pwr K iν n))) K'))) :=
  WindowChain.window_chain K O (K.mul P (pwr K iν n)) (K.mul (K.mul P (pwr K iν n)) s0) d
    (O.nn_mul _ _ hP (pwr_nn K O iν hiν n))
    (fun k => scale_step K O ν iν P s0 (d (k + 1)) (d k) n hνi hiν (hwin k))

end Ring

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **LargeCauchy on the window [n·k, n·(k+1)]** -/
theorem large_window (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n k : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) :
    Le K O.toCone (K.mul (pwr K ν n)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * (k + 1))))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
          (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) :=
  LargeCauchy.large_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR (fun t => A (n * k + t)) (fun t => B (n * k + t)) (fun t r => hA (n * k + t) r)
    (fun t r hr => hB (n * k + t) r hr) n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    n (Nat.le.refl)

include mul_inv h10 in
/-- **Truncation Cauchy bound across K' windows of the large-data problem.** -/
theorem large_chain (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ K' : Nat, Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * K'))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) K')
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) K'))) := by
  subst hPdef
  subst hS0def
  exact scale_chain K O ν iν _ _ n
    (fun k => Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
      (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
    hνi hiν hP
    (fun k => large_window K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
      hiRn hiR A B hA hB n k (hZA k) εA εB DA DB EA EB hεA hεB hDA hDB hν0 (hEA k) (hEB k) hAa_def hAb_def hβc)

end Gen

end WindowShift

#print axioms WindowShift.pwr_inv
#print axioms WindowShift.scale_step
#print axioms WindowShift.scale_chain
#print axioms WindowShift.large_window
#print axioms WindowShift.large_chain

#print axioms: 5 theorems, no axioms.

79.309 The window factor by the window length

With \(n=mk\) and \(2m\,h\alpha \nu ^{-1}\le 1\): \((\nu +h\alpha )^{mk}\nu ^{-mk}\le 2^k\).

ns/WindowFactor.lean

/-
  ns/WindowFactor.lean — T37 (goal G795b, piece c): the per-window factor of WindowShift.large_chain is bounded by a
  power of two depending only on the window length in time, over an ordered field with inverses. With n = m·k steps,
  ν·ν⁻¹ = 1, hα·ν⁻¹ ≥ 0 and 2m·hαν⁻¹ ≤ 1 (GronwallH):
    window_factor   (ν + hα)^(m·k)·(ν⁻¹)^(m·k) ≤ 2^k,
  so c = P (ν⁻¹)^n in large_chain is at most 2^k with k ≈ 2(n·h)α/ν, uniformly as h → 0.

  Lean 4 core plus ns/GronwallH, ns/WindowShift.
-/
import ns.GronwallH
import ns.WindowShift

namespace WindowFactor
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **(ν + hα)^(m·k)·(ν⁻¹)^(m·k) ≤ 2^k** -/
theorem window_factor (ν hα iν : R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (ha : O.nn (K.mul hα iν)) (m k : Nat)
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one) :
    Le K O.toCone (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) (pwr K (K.add K.one K.one) k) := by
  have g := GronwallH.gronwall_h K O ν hα iν hν hνi ha m k hm
  have s := le_mul_nn K O.toCone (pwr K iν (m * k)) _ _ (pwr_nn K O iν hiν (m * k)) g
  have e1 : K.mul (pwr K iν (m * k)) (pwr K (K.add ν hα) (m * k))
      = K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)) := by lib_ring
  have e2 : K.mul (pwr K iν (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k))
      = pwr K (K.add K.one K.one) k := by
    calc K.mul (pwr K iν (m * k)) (K.mul (pwr K ν (m * k)) (pwr K (K.add K.one K.one) k))
        = K.mul (K.mul (pwr K ν (m * k)) (pwr K iν (m * k))) (pwr K (K.add K.one K.one) k) := by lib_ring
      _ = K.mul K.one (pwr K (K.add K.one K.one) k) := by rw [WindowShift.pwr_inv K ν iν hνi (m * k)]
      _ = pwr K (K.add K.one K.one) k := by lib_ring
  rw [e1, e2] at s
  exact s

end Gen

end WindowFactor

#print axioms WindowFactor.window_factor

#print axioms: 1 theorem, no axioms.

79.310 Monotonicity of the chained bound

For \(0\le c\le c'\) and \(d_0,s_0\ge 0\) the chained bound with factor \(c\) is at most the one with \(c'\); with the window factor, \(c'=2^k\).

ns/WindowBound.lean

/-
  ns/WindowBound.lean — T37 (goal G795b, piece d): the chained window bound is monotone in the per-window factor,
  over an ordered ring. For 0 ≤ c ≤ c′ and d₀, s₀ ≥ 0:
    le_mul2      0 ≤ a ≤ a′, 0 ≤ b ≤ b′ ⇒ ab ≤ a′b′,
    pwr_mono     c^K ≤ c′^K,
    chain_mono   c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c′^K d₀ + c′ s₀ Σ_{j<K} c′^j,
    chain_bound  a bound of WindowShift.scale_chain form with factor c passes to any c′ ≥ c,
  so with WindowFactor.window_factor (c ≤ 2^k) the large-data window chain has the explicit factor 2^k per window.

    factor_chain with WindowFactor: a scale_chain bound with c = (ν + hα)^(m·k)(ν⁻¹)^(m·k) holds with c′ = 2^k.

  Lean 4 core plus ns/WindowChain, ns/ForceSob, ns/WindowFactor.
-/
import ns.WindowChain
import ns.ForceSob
import ns.WindowFactor

namespace WindowBound
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- 0 ≤ a ≤ a′, 0 ≤ b ≤ b′ ⇒ ab ≤ a′b′ -/
theorem le_mul2 (a a' b b' : R) (ha : O.nn a) (hb' : O.nn b') (hab : Le K O.toCone a a') (hbb : Le K O.toCone b b') :
    Le K O.toCone (K.mul a b) (K.mul a' b') :=
  le_trans' K O.toCone _ _ _ (le_mul_nn K O.toCone a _ _ ha hbb) (ForceSob.le_mul_right' K O.toCone hab hb')

/-- c^K ≤ c′^K -/
theorem pwr_mono (c c' : R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c') :
    ∀ n : Nat, Le K O.toCone (pwr K c n) (pwr K c' n)
  | 0 => le_refl' K O.toCone _
  | n + 1 => le_mul2 K O c c' (pwr K c n) (pwr K c' n) hc (pwr_nn K O c' hc' n) h (pwr_mono c c' hc hc' h n)

theorem sum_pwr_nn (c : R) (hc : O.nn c) : ∀ n : Nat, O.nn (sumN K (pwr K c) n)
  | 0 => O.nn_zero
  | n + 1 => O.nn_add _ _ (sum_pwr_nn c hc n) (pwr_nn K O c hc n)

/-- **the chained bound is monotone in the factor** -/
theorem chain_mono (c c' d0 s0 : R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c') (hd0 : O.nn d0)
    (hs0 : O.nn s0) (n : Nat) :
    Le K O.toCone (K.add (K.mul (pwr K c n) d0) (K.mul (K.mul c s0) (sumN K (pwr K c) n)))
      (K.add (K.mul (pwr K c' n) d0) (K.mul (K.mul c' s0) (sumN K (pwr K c') n))) :=
  le_add' K O.toCone _ _ _ _
    (ForceSob.le_mul_right' K O.toCone (pwr_mono K O c c' hc hc' h n) hd0)
    (le_mul2 K O _ _ _ _ (O.nn_mul _ _ hc hs0) (sum_pwr_nn K O c' hc' n)
      (ForceSob.le_mul_right' K O.toCone h hs0)
      (sum_le K O.toCone _ _ (pwr_mono K O c c' hc hc' h) n))

/-- **pass a chained window bound to a larger factor** -/
theorem chain_bound (c c' s0 : R) (d : Nat → R) (hc : O.nn c) (hc' : O.nn c') (h : Le K O.toCone c c')
    (hd0 : O.nn (d 0)) (hs0 : O.nn s0)
    (hch : ∀ n : Nat, Le K O.toCone (d n) (K.add (K.mul (pwr K c n) (d 0)) (K.mul (K.mul c s0) (sumN K (pwr K c) n))))
    (n : Nat) :
    Le K O.toCone (d n) (K.add (K.mul (pwr K c' n) (d 0)) (K.mul (K.mul c' s0) (sumN K (pwr K c') n))) :=
  le_trans' K O.toCone _ _ _ (hch n) (chain_mono K O c c' (d 0) s0 hc hc' h hd0 hs0 n)

/-- **the window chain with the explicit factor 2^k per window** -/
theorem factor_chain (ν hα iν s0 : R) (m k : Nat) (d : Nat → R) (hν : O.nn ν) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (ha : O.nn (K.mul hα iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul hα iν))) K.one)
    (hc : O.nn (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)))) (hd0 : O.nn (d 0)) (hs0 : O.nn s0)
    (hch : ∀ n : Nat, Le K O.toCone (d n)
      (K.add (K.mul (pwr K (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) n) (d 0))
        (K.mul (K.mul (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k))) s0)
          (sumN K (pwr K (K.mul (pwr K (K.add ν hα) (m * k)) (pwr K iν (m * k)))) n))))
    (n : Nat) :
    Le K O.toCone (d n) (K.add (K.mul (pwr K (pwr K (K.add K.one K.one) k) n) (d 0))
      (K.mul (K.mul (pwr K (K.add K.one K.one) k) s0) (sumN K (pwr K (pwr K (K.add K.one K.one) k)) n))) :=
  chain_bound K O _ _ s0 d hc
    (pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k)
    (WindowFactor.window_factor K O ν hα iν hν hνi hiν ha m k hm) hd0 hs0 hch n

end Gen

end WindowBound

#print axioms WindowBound.le_mul2
#print axioms WindowBound.pwr_mono
#print axioms WindowBound.sum_pwr_nn
#print axioms WindowBound.chain_mono
#print axioms WindowBound.chain_bound
#print axioms WindowBound.factor_chain

#print axioms: 6 theorems, no axioms.

79.311 The large-data chain with factor \(2^k\)

Under the local-time hypotheses at every window start: \(d_{K'}\le (2^k)^{K'}d_0+2^kS_0\sum _{j<K'}(2^k)^j\).

ns/WindowLarge.lean

/-
  ns/WindowLarge.lean — T37 (goal G795b, piece e): the large-data truncation Cauchy bound across K′ windows with the
  explicit factor 2^k per window, over an ordered field with inverses. Windows have n = m·k steps with
  2m·hα·ν⁻¹ ≤ 1 (α the Gronwall rate of LargeCauchy); with d_j = Q_d(n·j) and S₀ = n·h·S:
    large_explicit   d_K′ ≤ (2^k)^K′ d_0 + 2^k S₀ Σ_{j<K′} (2^k)^j,
  provided every window start satisfies the local-time hypotheses with the same Zb, E_A, E_B
  (WindowShift.large_chain + WindowBound.factor_chain).

  Lean 4 core plus ns/WindowShift, ns/WindowBound.
-/
import ns.WindowShift
import ns.WindowBound

namespace WindowLarge
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Large-data truncation Cauchy bound over K′ windows, factor 2^k per window.** -/
theorem large_explicit (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0) :
    ∀ K' : Nat, Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.add (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)))
        (K.mul (K.mul (pwr K (K.add K.one K.one) k) S0) (sumN K (pwr K (pwr K (K.add K.one K.one) k)) K'))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hZn : ∀ (L : List I) (u : I → V3 R), O.nn (Zl K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) L
  have hAn : ∀ (L : List I) (u : I → V3 R), O.nn (Al K kz L u) := fun L u =>
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) L
  have hd0 : O.nn ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) :=
    O.nn_add _ _ (O.nn_mul _ _ hν (hZn _ _)) (O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) (hAn _ _))
  -- 0 ≤ hα: hα = (hα·ν⁻¹)·ν
  have hhα : O.nn (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) := by
    have e : K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) = K.mul (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν) ν := by
      calc K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))) = K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) K.one := by lib_ring
        _ = K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) (K.mul ν iν) := by rw [hνi]
        _ = _ := by lib_ring
    rw [e]; exact O.nn_mul _ _ ha hν
  have hc : O.nn (K.mul (pwr K (K.add ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (m * k)) (pwr K iν (m * k))) :=
    O.nn_mul _ _ (pwr_nn K O _ (O.nn_add _ _ hν hhα) _) (pwr_nn K O iν hiν _)
  have ch := WindowShift.large_chain K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa
    hAb hiRn hiR A B hA hB n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    (pwr K (K.add ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n) S0 rfl hS0def
    (pwr_nn K O _ (O.nn_add _ _ hν hhα) _) iν hνi hiν
  intro K'
  subst hn
  exact WindowBound.factor_chain K O ν (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν S0 m k
    (fun j => (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (m * k * j)) hν hνi hiν ha hm hc hd0 hS0 ch K'

end Gen

end WindowLarge

#print axioms WindowLarge.large_explicit

#print axioms: 1 theorem, no axioms.

79.312 The geometric sum in closed form

For \(c\ge 2\): \(\sum _{j<K}c^j\le c^K\) and \(c^Kd_0+cs_0\sum _{j<K}c^j\le c^K(d_0+cs_0)\).

ns/WindowGeom.lean

/-
  ns/WindowGeom.lean — T37 (goal G795b, piece f): the geometric sum of the window chain in closed form, over an
  ordered ring. For c ≥ 2:
    geom_le       Σ_{j<K} c^j ≤ c^K,
    chain_closed  c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c^K (d₀ + c s₀)   (s₀ ≥ 0),
    two_pow_ge    2^(k+1) ≥ 2,
  so the large-data window chain (WindowLarge.large_explicit, factor 2^(k+1)) gives d_K ≤ 2^((k+1)K) (d₀ + 2^(k+1) S₀).

  Lean 4 core plus ns/WindowBound.
-/
import ns.WindowBound

namespace WindowGeom
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **Σ_{j<K} c^j ≤ c^K for c ≥ 2** -/
theorem geom_le (c : R) (hc : O.nn c) (hc2 : Le K O.toCone (K.add K.one K.one) c) :
    ∀ n : Nat, Le K O.toCone (sumN K (pwr K c) n) (pwr K c n)
  | 0 => ⟨K.one, RatModel.nn_one' K O.toCone, by show K.add K.zero K.one = K.one; lib_ring⟩
  | n + 1 => by
    have s := le_add' K O.toCone _ _ _ _ (geom_le c hc hc2 n) (le_refl' K O.toCone (pwr K c n))
    have m := ForceSob.le_mul_right' K O.toCone hc2 (pwr_nn K O c hc n)
    refine le_trans' K O.toCone _ _ _ s ?_
    have e : K.add (pwr K c n) (pwr K c n) = K.mul (K.add K.one K.one) (pwr K c n) := by lib_ring
    rw [e]; exact m

/-- **c^K d₀ + c s₀ Σ_{j<K} c^j ≤ c^K (d₀ + c s₀)** -/
theorem chain_closed (c d0 s0 : R) (hc : O.nn c) (hc2 : Le K O.toCone (K.add K.one K.one) c) (hs0 : O.nn s0)
    (n : Nat) :
    Le K O.toCone (K.add (K.mul (pwr K c n) d0) (K.mul (K.mul c s0) (sumN K (pwr K c) n)))
      (K.mul (pwr K c n) (K.add d0 (K.mul c s0))) := by
  have t := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (K.mul (pwr K c n) d0))
    (le_mul_nn K O.toCone (K.mul c s0) _ _ (O.nn_mul _ _ hc hs0) (geom_le K O c hc hc2 n))
  refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
  lib_ring

theorem one_le_two_pow : ∀ k : Nat, Le K O.toCone K.one (pwr K (K.add K.one K.one) k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    have ih := one_le_two_pow k
    have n1 : O.nn (pwr K (K.add K.one K.one) k) :=
      pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k
    refine le_trans' K O.toCone _ _ _ ih ⟨pwr K (K.add K.one K.one) k, n1, ?_⟩
    show K.add (pwr K (K.add K.one K.one) k) (pwr K (K.add K.one K.one) k)
      = K.mul (K.add K.one K.one) (pwr K (K.add K.one K.one) k)
    lib_ring

/-- 2^(k+1) ≥ 2 -/
theorem two_pow_ge (k : Nat) : Le K O.toCone (K.add K.one K.one) (pwr K (K.add K.one K.one) (k + 1)) := by
  have m := le_mul_nn K O.toCone (K.add K.one K.one) _ _
    (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (one_le_two_pow K O k)
  have e : K.mul (K.add K.one K.one) K.one = K.add K.one K.one := by lib_ring
  rw [e] at m; exact m

end Gen

end WindowGeom

#print axioms WindowGeom.geom_le
#print axioms WindowGeom.chain_closed
#print axioms WindowGeom.one_le_two_pow
#print axioms WindowGeom.two_pow_ge

#print axioms: 4 theorems, no axioms.

79.313 The large-data truncation bound over many windows

For \(k\ge 1\): \(Q_d(nK')\le (2^k)^{K'}\bigl (Q_d(0)+2^kS_0\bigr )\), \(S_0\to 0\) as the cutoff grows.

ns/WindowClosed.lean

/-
  ns/WindowClosed.lean — T37 (goal G795b, piece g): the large-data truncation Cauchy bound over K′ windows in closed
  form. Under the hypotheses of WindowLarge.large_explicit with k ≥ 1 (windows of n = m·k steps):
    large_closed   Q_d(n·K′) ≤ (2^k)^K′ (Q_d(0) + 2^k S₀),
  with S₀ = n·h·S → 0 as the cutoff grows (TruncRate): the truncation difference stays small on any fixed number of
  windows, provided the local-time hypotheses hold at every window start.

  Lean 4 core plus ns/WindowLarge, ns/WindowGeom.
-/
import ns.WindowLarge
import ns.WindowGeom

namespace WindowClosed
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Closed-form large-data truncation Cauchy bound over K′ windows.** -/
theorem large_closed (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (hZA : ∀ k t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0)
    (hk : 1 ≤ k) (K' : Nat) :
    Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') (K.add ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) (K.mul (pwr K (K.add K.one K.one) k) S0))) := by
  have e := WindowLarge.large_explicit K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
    hAa hAb hiRn hiR A B hA hB n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc S0 hS0def
    iν hνi hiν m k hn ha hm hS0 K'
  have h2 : Le K O.toCone (K.add K.one K.one) (pwr K (K.add K.one K.one) k) := by
    cases k with
    | zero => exact absurd hk (Nat.not_succ_le_zero 0)
    | succ k' => exact WindowGeom.two_pow_ge K O k'
  have hc : O.nn (pwr K (K.add K.one K.one) k) := pwr_nn K O _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) k
  exact le_trans' K O.toCone _ _ _ e (WindowGeom.chain_closed K O _ _ S0 hc h2 hS0 K')

end Gen

end WindowClosed

#print axioms WindowClosed.large_closed

#print axioms: 1 theorem, no axioms.

79.314 The local bound with the budget as an inequality

With \(Q_0+n\,h\,c_1Z_b^2\le \nu Z_b\): \(Z_t\le Z_b\) for \(t\le n\); restarted at \(nk\) from one bound \(Q(nk)\le Q_{\max }\).

ns/LocalTimeLe.lean

/-
  ns/LocalTimeLe.lean — T37 (goal G795b-h, piece a): the large-data local bound with the enstrophy level as an upper
  bound instead of an equation, and restarted at any window start. With cutoffs c₂Zb ≤ ν² (as in
  EnstLocalTime.local_time) and the budget Q_0 + n·h·c₁Zb² ≤ ν·Zb (no equation νZb = 2Q_0 needed):
    local_time_le   Z_t ≤ Zb for every t ≤ n,
    window_z        if Q(n·k) ≤ Qmax and Qmax + n·h·c₁Zb² ≤ ν·Zb, then Z_{n·k+t} ≤ Zb for every t ≤ n.
  So one bound Qmax on Q at the window starts fixes one Zb for every window (the hZA hypothesis of
  WindowShift.large_chain).

  Lean 4 core plus ns/EnstLocalGalerkin.
-/
import ns.EnstLocalGalerkin

namespace LocalTimeLe
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale EnstLocal
  EnstLocalGalerkin LatticeSum

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Z_t ≤ Zb on t ≤ n from the budget Q_0 + n h c₁ Zb² ≤ ν Zb** -/
theorem local_time_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (Q K ν h (Zs K l kz a) (As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))))) (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) : Le K O.toCone (Zs K l kz a t) Zb := by
  have q := local_galerkin K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN hs choice n
    budget t ht
  have hD : O.nn (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb))) := by
    have hiv : O.nn (iv K inv (Rc + 1)) := iv_nn K O inv mul_inv h10 Rc
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    exact O.nn_mul _ _ hh (O.nn_mul _ _ (O.nn_mul _ _ (O.nn_mul _ _ two (O.nn_sq ι))
      (O.nn_add _ _ (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) (RatModel.nn_one' K O.toCone))
        (O.nn_mul _ _ (RatModel.nn_cast K O.toCone _) hiv))) (O.nn_sq Zb))
  have tn := ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone t n ht) hD
  have q2 := le_trans' K O.toCone _ _ _ q (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _) tn)
  have q3 := le_trans' K O.toCone _ _ _ q2 budget
  have hA : O.nn (As K l kz a t) :=
    lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (O.nn_sq _) (dot3_nn K O.toCone _)) l
  have hz : Le K O.toCone (K.mul ν (Zs K l kz a t)) (Q K ν h (Zs K l kz a) (As K l kz a) t) :=
    ⟨K.mul (K.mul h (K.mul ν ν)) (As K l kz a t), O.nn_mul _ _ (O.nn_mul _ _ hh (O.nn_sq ν)) hA, rfl⟩
  exact le_of_mul_le K O ν _ _ hν hν0 (le_trans' K O.toCone _ _ _ hz q3)

include mul_inv h10 in
/-- **one Qmax at the window starts gives one Zb on every window** -/
theorem window_z (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc t0 : Nat) (hRM : Rc ≤ M)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (cc K ι (C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax : R) (hQ : Le K O.toCone (Q K ν h (Zs K l kz a) (As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax (K.mul (castN K n) (K.mul h (K.mul (cc K ι (C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (t : Nat) (ht : t ≤ n) : Le K O.toCone (Zs K l kz a (n * k + t)) Zb :=
  local_time_le K O inv mul_inv h10 l kz hd h1 M hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN (n * k + s)) (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud) t ht

end Gen

end LocalTimeLe

#print axioms LocalTimeLe.local_time_le
#print axioms LocalTimeLe.window_z

#print axioms: 2 theorems, no axioms.

79.315 The large-data \(H^2\) bound with the budget as an inequality

H2Large with \(Q_0+n\,h\,c_1Z_b^2\le \nu Z_b\) in place of \(\nu Z_b=2Q_0\).

ns/H2LargeLe.lean

/-
  ns/H2LargeLe.lean — T37 (goal G795b-h, piece b): H2Large.h2_large with the budget Q₀ + n·h·c₁Zb² ≤ ν·Zb in
  place of the equation νZb = 2Q₀ (LocalTimeLe.local_time_le):
    h2_large_le   Z_t ≤ Zb and the window H² energy bound for every t ≤ n,
  so the H² energy bound of every window can be taken with one Zb fixed by a bound Qmax on Q at the window starts.

  Lean 4 core plus ns/H2Large, ns/LocalTimeLe.
-/
import ns.H2Large
import ns.LocalTimeLe

namespace H2LargeLe
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **H2Large with the budget as an inequality** -/
theorem h2_large_le (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have loc := fun T (hT : T ≤ n) => LocalTimeLe.local_time_le K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc t0 hRM
    ι h ν Zb hh hν hν0 hZb a N hN0 hs choice n budget T hT
  refine ⟨loc t ht, ?_⟩
  exact H2LocalUniform.h2_local_uniform K O inv mul_inv h10 l kz hd h1 Mm hM ι h ν hh hν a N hN hs Zb e D1 D2 D3
    hZb he hD1 hD2 hD3 H1 H2 H3 hε t (fun s hst => loc s (Nat.le_trans (Nat.le_of_lt hst) ht))

end Gen

end H2LargeLe

#print axioms H2LargeLe.h2_large_le

#print axioms: 1 theorem, no axioms.

79.316 Window chains on finitely many windows

The window chain, the rescaled chain and the linear energy growth \(E_j\le E_0+jD\) with the step hypotheses only for \(k<K'\).

ns/WindowChainUpto.lean

/-
  ns/WindowChainUpto.lean — T37 (goal G795b-i, piece a): WindowChain.window_chain with the step hypothesis only on
  the first K′ windows, over an ordered ring:
    window_chain_upto   d_{k+1} ≤ c·d_k + s for k < K′  ⇒  d_j ≤ c^j d_0 + s·Σ_{i<j} c^i for j ≤ K′,
    lin_upto            E_{k+1} ≤ E_k + D for k < K′  ⇒  E_j ≤ E_0 + j·D for j ≤ K′,
  so on K′ windows the window-start H² energies are bounded by E_0 + K′·D without an extra hypothesis.

    scale_chain_upto    WindowShift.scale_chain on the first K′ windows.

  Lean 4 core plus ns/WindowChain, ns/WindowShift.
-/
import ns.WindowChain
import ns.WindowShift

namespace WindowChainUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K)

/-- **d_j ≤ c^j d_0 + s Σ_{i<j} c^i for j ≤ K′** -/
theorem window_chain_upto (c s : R) (d : Nat → R) (hc : O.nn c) (K' : Nat)
    (hstep : ∀ k, k < K' → Le K O.toCone (d (k + 1)) (K.add (K.mul c (d k)) s)) :
    ∀ j : Nat, j ≤ K' →
      Le K O.toCone (d j) (K.add (K.mul (pwr K c j) (d 0)) (K.mul s (sumN K (pwr K c) j)))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (d 0) K.zero = K.add (K.mul K.one (d 0)) (K.mul s K.zero); lib_ring⟩
  | k + 1, hk => by
    have ih := window_chain_upto c s d hc K' hstep k (Nat.le_of_succ_le hk)
    have m := le_mul_nn K O.toCone c _ _ hc ih
    have t := le_trans' K O.toCone _ _ _ (hstep k hk) (le_add' K O.toCone _ _ _ _ m (le_refl' K O.toCone s))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.mul c (K.add (K.mul (pwr K c k) (d 0)) (K.mul s (sumN K (pwr K c) k)))) s) K.zero
      = K.add (K.mul (K.mul c (pwr K c k)) (d 0)) (K.mul s (sumN K (pwr K c) (k + 1)))
    rw [← WindowChain.geom_shift K c k]; lib_ring

/-- **E_j ≤ E_0 + j·D for j ≤ K′** -/
theorem lin_upto (E : Nat → R) (D : R) (K' : Nat) (hstep : ∀ k, k < K' → Le K O.toCone (E (k + 1)) (K.add (E k) D)) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone (E j) (K.add (E 0) (K.mul (castN K j) D))
  | 0, _ => ⟨K.zero, O.nn_zero, by show K.add (E 0) K.zero = K.add (E 0) (K.mul K.zero D); lib_ring⟩
  | k + 1, hk => by
    have ih := lin_upto E D K' hstep k (Nat.le_of_succ_le hk)
    have t := le_trans' K O.toCone _ _ _ (hstep k hk) (le_add' K O.toCone _ _ _ _ ih (le_refl' K O.toCone D))
    refine le_trans' K O.toCone _ _ _ t ⟨K.zero, O.nn_zero, ?_⟩
    show K.add (K.add (K.add (E 0) (K.mul (castN K k) D)) D) K.zero = K.add (E 0) (K.mul (K.add K.one (castN K k)) D)
    lib_ring

/-- **scale_chain on the first K′ windows** -/
theorem scale_chain_upto (ν iν P s0 : R) (n : Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hP : O.nn P) (K' : Nat)
    (hwin : ∀ k, k < K' → Le K O.toCone (K.mul (pwr K ν n) (d (k + 1))) (K.mul P (K.add (d k) s0))) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone (d j)
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j) (d 0))
        (K.mul (K.mul (K.mul P (pwr K iν n)) s0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) :=
  window_chain_upto K O (K.mul P (pwr K iν n)) (K.mul (K.mul P (pwr K iν n)) s0) d
    (O.nn_mul _ _ hP (pwr_nn K O iν hiν n)) K'
    (fun k hk => WindowShift.scale_step K O ν iν P s0 (d (k + 1)) (d k) n hνi hiν (hwin k hk))

end Gen

end WindowChainUpto

#print axioms WindowChainUpto.window_chain_upto
#print axioms WindowChainUpto.lin_upto
#print axioms WindowChainUpto.scale_chain_upto

#print axioms: 3 theorems, no axioms.

79.317 The window hypotheses from bounds at the window start

If \(Q(nk)\le Q_{\max }\), \(Q_{\max }+n\,h\,c_1Z_b^2\le \nu Z_b\) and the \(H^2\) energy at \(nk\) is at most \(E_{\max }\), then \(Z\le Z_b\) and the \(H^2\) energy is at most \(E_{\max }+t\,h\,D\) on the window.

ns/WindowHyps.lean

/-
  ns/WindowHyps.lean — T37 (goal G795b-h, piece c): the per-window hypotheses of WindowShift.large_chain from two
  bounds at the window starts. If Q(n·k) ≤ Qmax with Qmax + n·h·c₁Zb² ≤ ν·Zb, and the H² energy at the window start
  is at most Emax, then on the window [n·k, n·k + n]
    window_hyps   Z_{n·k+t} ≤ Zb   and   νA_{n·k+t} + hεB_{n·k+t} ≤ Emax + t·h·D   (t ≤ n),
  which are hZA and hEA of large_chain with E_A = Emax (A = Al, B = Σ w³|u|² definitionally).

  Lean 4 core plus ns/H2LargeLe.
-/
import ns.H2LargeLe

namespace WindowHyps
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **the window hypotheses from Qmax and Emax at the window start** -/
theorem window_hyps (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax Emax : R)
    (hQ : Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) Emax)
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a (n * k + t)) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k + t))
      (K.add Emax (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have w := H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  exact ⟨w.1, le_trans' K O.toCone _ _ _ w.2 (le_add' K O.toCone _ _ _ _ hE (le_refl' K O.toCone _))⟩

end Gen

end WindowHyps

#print axioms WindowHyps.window_hyps

#print axioms: 1 theorem, no axioms.

79.318 The large-data chain under bounds at the window starts

If \(Q(nj)\le Q_{\max }\) and the \(H^2\) energy at \(nj\) is at most \(E_{\max }\) for every \(j\): \(Q_d(nK')\le (2^k)^{K'}(Q_d(0)+2^kS_0)\).

ns/WindowCond.lean

/-
  ns/WindowCond.lean — T37 (goal G795b-h, piece d): the closed-form truncation bound over many windows under two
  bounds at the window starts only: if Q(n·j) ≤ Qmax and the H² energy at n·j is at most Emax for every window
  start, with Qmax + n·h·c₁Zb² ≤ ν·Zb and the absorption parameters of H2Large, then
    large_cond   Q_d(n·K′) ≤ (2^k)^K′ (Q_d(0) + 2^k S₀)
  (the small-truncation energy hypothesis hEB is kept). This is the conditional form: bounded Q and H² energy at the
  window starts ⇒ the Galerkin truncations are Cauchy on every finite time interval.

  Lean 4 core plus ns/WindowClosed, ns/WindowHyps.
-/
import ns.WindowClosed
import ns.WindowHyps

namespace WindowCond
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Conditional large-data truncation Cauchy bound over many windows.** -/
theorem large_cond (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax Emax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : ∀ j, Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * j)) Emax)
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))) (hEAdef : EA = Emax)
    (hEB : ∀ k t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (S0 : R)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) (m k : Nat) (hn : n = m * k)
    (ha : O.nn (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))
    (hm : Le K O.toCone (K.mul (K.add K.one K.one) (K.mul (castN K m) (K.mul (K.mul h (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab))))) iν))) K.one)
    (hS0 : O.nn S0)
    (hk : 1 ≤ k) (K' : Nat) :
    Le K O.toCone ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * K'))
      (K.mul (pwr K (pwr K (K.add K.one K.one) k) K') (K.add ((Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))) (n * 0)) (K.mul (pwr K (K.add K.one K.one) k) S0))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  have W := fun j t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν
    hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax EA (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j) t ht
  exact WindowClosed.large_closed K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
    hAa hAb hiRn hiR A B hA hB n (fun j t ht => (W j t (Nat.le_of_lt ht)).1) _ εB _ DB _ EB hεA hεB hDA hDB hν0
    (fun j t ht => (W j t ht).2) hEB hAa_def hAb_def hβc S0 hS0def iν hνi hiν m k hn ha hm hS0 hk K'

end Gen

end WindowCond

#print axioms WindowCond.large_cond

#print axioms: 1 theorem, no axioms.

79.319 The large-data chain on finitely many windows

large_chain with the window hypotheses only for \(k<K'\).

ns/WindowShiftUpto.lean

/-
  ns/WindowShiftUpto.lean — T37 (goal G795b-i, piece b): WindowShift.large_chain with the per-window hypotheses
  only on the first K′ windows:
    large_chain_upto   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′  (d_j = Q_d(n·j), c = P (ν⁻¹)^n),
  so the window-start energy bound may depend on K′ (E ≤ E_0 + K′·n·h·D, WindowChainUpto.lin_upto).

  Lean 4 core plus ns/WindowShift, ns/WindowChainUpto.
-/
import ns.WindowShift
import ns.WindowChainUpto

namespace WindowShiftUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **large_chain on the first K′ windows** -/
theorem large_chain_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat) (hZA : ∀ k, k < K' → ∀ t, t < n → Le K O.toCone (Zl K kz l (A (n * k + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (n * k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (n * k + t) p) (A (n * k + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hPdef
  subst hS0def
  exact WindowChainUpto.scale_chain_upto K O ν iν _ _ n
    (fun k => Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
      (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * k))
    hνi hiν hP K'
    (fun k hk => WindowShift.large_window K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb
      hAa hAb hiRn hiR A B hA hB n k (hZA k hk) εA εB DA DB EA EB hεA hεB hDA hDB hν0 (hEA k hk) (hEB k hk)
      hAa_def hAb_def hβc)

end Gen

end WindowShiftUpto

#print axioms WindowShiftUpto.large_chain_upto

#print axioms: 1 theorem, no axioms.

79.320 Linear growth of the window-start \(H^2\) energy

From \(Q(nk)\le Q_{\max }\): \(E(n(k+1))\le E(nk)+n\,h\,D\), hence \(E(nj)\le E(0)+j\,n\,h\,D\).

ns/WindowEnergy.lean

/-
  ns/WindowEnergy.lean — T37 (goal G795b-i, piece c): the H² energy at the window starts grows at most linearly,
  from the one bound Q(n·k) ≤ Qmax at every window start (with Qmax + n·h·c₁Zb² ≤ ν·Zb):
    window_energy   En(n·(k + 1)) ≤ En(n·k) + n·h·D,
    energy_upto     En(n·j) ≤ En(0) + j·n·h·D   (j ≤ K′),
  so on K′ windows the energy hypothesis of WindowHyps holds with Emax = En(0) + K′·n·h·D: no separate H² bound
  at the window starts is needed.

  Lean 4 core plus ns/H2LargeLe, ns/WindowChainUpto.
-/
import ns.H2LargeLe
import ns.WindowChainUpto

namespace WindowEnergy
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **one window: En(n(k+1)) ≤ En(nk) + n h D** -/
theorem window_energy (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (k : Nat) :
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * (k + 1)))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) :=
  (H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    (fun s r => hs (n * k + s) r) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ (hQ k) (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε n (Nat.le_refl n)).2

include mul_inv h10 in
/-- **K′ windows: En(nj) ≤ En(0) + j n h D** -/
theorem energy_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (K' : Nat) :
    ∀ j, j ≤ K' → Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * 0)) (K.mul (castN K j) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))))) :=
  WindowChainUpto.lin_upto K O (fun j => (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))) _ K'
    (fun k _ => window_energy K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb a N hN0 hN hs
      choice n Qmax hQ hbud e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε k)

end Gen

end WindowEnergy

#print axioms WindowEnergy.window_energy
#print axioms WindowEnergy.energy_upto

#print axioms: 2 theorems, no axioms.

79.321 The large-data chain from one enstrophy bound

On \(K'\) windows, from \(Q(nj)\le Q_{\max }\) alone (the window-start \(H^2\) energy from its linear growth): \(d_j\le c^jd_0+cs_0\sum _{i<j}c^i\), \(j\le K'\).

ns/WindowCondUpto.lean

/-
  ns/WindowCondUpto.lean — T37 (goal G795b-i, piece d): the truncation Cauchy chain over K′ windows from the one
  bound Q(n·j) ≤ Qmax at the window starts (Qmax + n·h·c₁Zb² ≤ ν·Zb), with the window-start H² energy bound
  E_A = En(0) + K′·n·h·D supplied by WindowEnergy.energy_upto (no separate H² hypothesis):
    large_cond_upto   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′.
  The small-truncation energy hypothesis hEB is kept.

  Lean 4 core plus ns/WindowShiftUpto, ns/WindowHyps, ns/WindowEnergy.
-/
import ns.WindowShiftUpto
import ns.WindowHyps
import ns.WindowEnergy

namespace WindowCondUpto
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy chain over K′ windows from Qmax alone.** -/
theorem large_cond_upto (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEAdef : EA = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DA))))
    (hEB : ∀ k, k < K' → ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (n * k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (n * k + t) p) (B (n * k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  have hX : O.nn (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone n) (O.nn_mul _ _ hh hDA)
  have hE := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν hν0 hZb A
      (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n Qmax hQ hbud
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have W := fun j (hj : j < K') t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1
    hRl ι h ν Zb hh hν hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax _ (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j hj) t ht
  exact WindowShiftUpto.large_chain_upto K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR
    hh hν hZb hAa hAb hiRn hiR A B hA hB n K' (fun j hj t ht => (W j hj t (Nat.le_of_lt ht)).1) _ εB _ DB _ EB
    hεA hεB hDA hDB hν0 (fun j hj t ht => (W j hj t ht).2) hEB hAa_def hAb_def hβc P S0 hPdef hS0def hP iν hνi hiν

end Gen

end WindowCondUpto

#print axioms WindowCondUpto.large_cond_upto

#print axioms: 1 theorem, no axioms.

79.322 The Step hypothesis on the modes of the list

Extending a system by \(0\) off the list \(l\), the \(H^2\) window bounds hold when Step is assumed only for \(r\in l\).

ns/StepZeroExt.lean

/-
  ns/StepZeroExt.lean — T37 (goal G795b-j): the Galerkin Step hypothesis only on the modes of the list.
  A system that satisfies Step only for r ∈ l (e.g. the low-mode part on l.filter (lowB Rc)) is extended by 0 off
  the list (extT; membership given by a Boolean test memb with memb r = true ↔ r ∈ l, no decidability axiom).
  Off the list Step 0 0 0 holds, and every quantity of the chain (lsum over l, Nl, Zs, As, Bs, En, Q) only reads
  values on l, so the existing theorems apply to the extension unchanged.
    lsum_congr_mem   f = g on l  ⇒  lsum f l = lsum g l
    nl_ext           Nl over l of the extension = Nl of the original
    step_ext         Step on l  ⇒  Step of the extension everywhere
    h2_large_le_mem  H2LargeLe.h2_large_le with Step assumed only on l
    energy_upto_mem  WindowEnergy.energy_upto with Step assumed only on l
  Lean 4 core plus ns/H2LargeLe, ns/WindowEnergy.
-/
import ns.H2LargeLe
import ns.WindowEnergy

namespace StepZeroExt
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Basic
variable {R : Type} (K : CRing R) {I : Type}

/-- the extension by 0 off the modes with memb r = false -/
def extT (memb : I → Bool) (a : Nat → I → V3 R) (t : Nat) (r : I) : V3 R :=
  match memb r with
  | true => a t r
  | false => ⟨K.zero, K.zero, K.zero⟩

theorem lsum_congr_mem (f g : I → R) : ∀ l : List I, (∀ r, r ∈ l → f r = g r) → lsum K f l = lsum K g l
  | [] => fun _ => rfl
  | x :: l => fun h => by
    show K.add (f x) (lsum K f l) = K.add (g x) (lsum K g l)
    rw [h x (List.Mem.head _), lsum_congr_mem f g l (fun r hr => h r (List.Mem.tail _ hr))]

theorem extT_eq (memb : I → Bool) (l : List I) (hm : ∀ r, r ∈ l → memb r = true) (a : Nat → I → V3 R)
    (t : Nat) (r : I) (hr : r ∈ l) : extT K memb a t r = a t r := by
  unfold extT; rw [hm r hr]

theorem nl_ext (memb : I → Bool) (l : List I) (hm : ∀ r, r ∈ l → memb r = true) (δ : I → I → I → R) (ι : R)
    (k : I → V3 R) (a : Nat → I → V3 R) (t : Nat) (r : I) :
    Nl K l δ ι k (extT K memb a t) (extT K memb a t) r = Nl K l δ ι k (a t) (a t) r := by
  have hc : ∀ p q, p ∈ l → coef K δ ι k (extT K memb a t) p q r = coef K δ ι k (a t) p q r := fun p q hp => by
    unfold coef; rw [extT_eq K memb l hm a t p hp]
  have e : ∀ sel : V3 R → R,
      lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (extT K memb a t) p q r) (sel (extT K memb a t q))) l) l
        = lsum K (fun p => lsum K (fun q => K.mul (coef K δ ι k (a t) p q r) (sel (a t q))) l) l := fun sel =>
    lsum_congr_mem K _ _ l (fun p hp => lsum_congr_mem K _ _ l (fun q hq => by
      rw [hc p q hp, extT_eq K memb l hm a t q hq]))
  unfold Nl
  rw [e V3.x, e V3.y, e V3.z]

theorem step_zero (h ν w : R) : Step K h ν w ⟨K.zero, K.zero, K.zero⟩ ⟨K.zero, K.zero, K.zero⟩ ⟨K.zero, K.zero, K.zero⟩ := by
  have e : StepC K h ν w K.zero K.zero K.zero := by unfold StepC; lib_ring
  exact ⟨e, e, e⟩

theorem step_ext (memb : I → Bool) (l : List I) (hm' : ∀ r, memb r = true → r ∈ l) (h ν : R) (w : I → R)
    (a N : Nat → I → V3 R) (hs : ∀ t r, r ∈ l → Step K h ν (w r) (a (t + 1) r) (a t r) (N t r)) :
    ∀ t r, Step K h ν (w r) (extT K memb a (t + 1) r) (extT K memb a t r) (extT K memb N t r) := by
  intro t r
  unfold extT
  cases e : memb r with
  | true => exact hs t r (hm' r e)
  | false => exact step_zero K h ν (w r)

theorem le_congr (P : Cone K) {x x' y y' : R} (e1 : x = x') (e2 : y = y') (h : Le K P x y) : Le K P x' y' := by
  rw [← e1, ← e2]; exact h

theorem en_congr (ν h ε : R) (A A' B B' : Nat → R) (s : Nat) (hA : A s = A' s) (hB : B s = B' s) :
    H2Linear.En K ν h ε A B s = H2Linear.En K ν h ε A' B' s := by
  unfold H2Linear.En; rw [hA, hB]

end Basic

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **H2LargeLe.h2_large_le with the Step hypothesis only on the modes of the list** -/
theorem h2_large_le_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (budget : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a t) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) t)
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) 0)
        (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have ea : ∀ s r, r ∈ l → extT K memb a s r = a s r := fun s r hr => extT_eq K memb l hm a s r hr
  have eN : ∀ s r, r ∈ l → extT K memb N s r = N s r := fun s r hr => extT_eq K memb l hm N s r hr
  have eZs : ∀ s, EnstGalerkin.Zs K l kz a s = EnstGalerkin.Zs K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.Zs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eAs : ∀ s, EnstGalerkin.As K l kz a s = EnstGalerkin.As K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFA : ∀ s, ForceAbsorb.As K kz l (a s) = ForceAbsorb.As K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFB : ∀ s, ForceAbsorb.Bs K kz l (a s) = ForceAbsorb.Bs K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.Bs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eNl : ∀ s r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r
      = Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r := fun s r =>
    (nl_ext K memb l hm (TriadSparse.delta K kz) ι (kv K kz) a s r).symm
  have hN0' : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (extT K memb N s r) (extT K memb N s r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN0 s)
  have hN' : ∀ s, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (extT K memb N s r) (extT K memb N s r))) l)
      (lsum K (fun r => K.mul (wt K kz r)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r))) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN s)
  have budget' : Le K O.toCone (K.add (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz (extT K memb a))
      (EnstGalerkin.As K l kz (extT K memb a)) 0)
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb) :=
    le_congr K O.toCone (by unfold EnstGlobal.Q; rw [eZs 0, eAs 0]) rfl budget
  have r := H2LargeLe.h2_large_le K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (extT K memb a) (extT K memb N) hN0' hN' (step_ext K memb l hm' h ν (wt K kz) a N hs) choice n budget'
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  have eEn : ∀ ε s, H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) s
      = H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (extT K memb a s))
          (fun s => ForceAbsorb.Bs K kz l (extT K memb a s)) s := fun ε s =>
    en_congr K ν h ε _ _ _ _ s (eFA s) (eFB s)
  exact ⟨le_congr K O.toCone (eZs t).symm rfl r.1,
    le_congr K O.toCone (eEn _ t).symm (by rw [eEn _ 0]) r.2⟩

include mul_inv h10 in
/-- **WindowEnergy.energy_upto with the Step hypothesis only on the modes of the list** -/
theorem energy_upto_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n : Nat) (Qmax : R)
    (hQ : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (K' : Nat) :
    ∀ j, j ≤ K' → Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * j))
      (K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * 0)) (K.mul (castN K j) (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))))) := by
  have ea : ∀ s r, r ∈ l → extT K memb a s r = a s r := fun s r hr => extT_eq K memb l hm a s r hr
  have eN : ∀ s r, r ∈ l → extT K memb N s r = N s r := fun s r hr => extT_eq K memb l hm N s r hr
  have eZs : ∀ s, EnstGalerkin.Zs K l kz a s = EnstGalerkin.Zs K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.Zs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eAs : ∀ s, EnstGalerkin.As K l kz a s = EnstGalerkin.As K l kz (extT K memb a) s := fun s => by
    unfold EnstGalerkin.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFA : ∀ s, ForceAbsorb.As K kz l (a s) = ForceAbsorb.As K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.As; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eFB : ∀ s, ForceAbsorb.Bs K kz l (a s) = ForceAbsorb.Bs K kz l (extT K memb a s) := fun s => by
    unfold ForceAbsorb.Bs; exact lsum_congr_mem K _ _ l (fun r hr => by rw [ea s r hr])
  have eNl : ∀ s r, Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a s) (a s) r
      = Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r := fun s r =>
    (nl_ext K memb l hm (TriadSparse.delta K kz) ι (kv K kz) a s r).symm
  have hN0' : ∀ s, Le K O.toCone (lsum K (fun r => dot3 K (extT K memb N s r) (extT K memb N s r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN0 s)
  have hN' : ∀ s, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (extT K memb N s r) (extT K memb N s r))) l)
      (lsum K (fun r => K.mul (wt K kz r)
        (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r)
          (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (extT K memb a s) (extT K memb a s) r))) l) := fun s =>
    le_congr K O.toCone (lsum_congr_mem K _ _ l (fun r hr => by rw [eN s r hr]))
      (lsum_congr_mem K _ _ l (fun r _ => by rw [eNl s r])) (hN s)
  have hQ' : ∀ k, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz (extT K memb a))
      (EnstGalerkin.As K l kz (extT K memb a)) (n * k)) Qmax := fun k =>
    le_congr K O.toCone (by unfold EnstGlobal.Q; rw [eZs (n * k), eAs (n * k)]) rfl (hQ k)
  have r := WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (extT K memb a) (extT K memb N) hN0' hN' (step_ext K memb l hm' h ν (wt K kz) a N hs) choice n Qmax hQ' hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K'
  have eEn : ∀ ε s, H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) s
      = H2Linear.En K ν h ε (fun s => ForceAbsorb.As K kz l (extT K memb a s))
          (fun s => ForceAbsorb.Bs K kz l (extT K memb a s)) s := fun ε s =>
    en_congr K ν h ε _ _ _ _ s (eFA s) (eFB s)
  exact fun j hj => le_congr K O.toCone (eEn _ (n * j)).symm (by rw [eEn _ (n * 0)]) (r j hj)

end Gen

end StepZeroExt

#print axioms StepZeroExt.lsum_congr_mem
#print axioms StepZeroExt.extT_eq
#print axioms StepZeroExt.nl_ext
#print axioms StepZeroExt.step_zero
#print axioms StepZeroExt.step_ext
#print axioms StepZeroExt.le_congr
#print axioms StepZeroExt.en_congr
#print axioms StepZeroExt.h2_large_le_mem
#print axioms StepZeroExt.energy_upto_mem

#print axioms: 9 theorems, no axioms.

79.323 Window hypotheses from the list membership

The window energy hypotheses of the truncated system on a filtered list follow from the Step hypothesis on its modes.

ns/WindowHypsMem.lean

/-
  ns/WindowHypsMem.lean — T37 (goal G795b-j): WindowHyps.window_hyps with the Step hypothesis only on the modes of
  the list (StepZeroExt.h2_large_le_mem in place of H2LargeLe.h2_large_le), for the small-truncation system B on
  l.filter (lowB Rc).
    window_hyps_mem   Z_{n·k+t} ≤ Zb and νA_{n·k+t} + hεB_{n·k+t} ≤ Emax + t·h·D (t ≤ n)

  Lean 4 core plus ns/StepZeroExt, ns/WindowHyps.
-/
import ns.StepZeroExt
import ns.WindowHyps

namespace WindowHypsMem
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstGalerkin ForceTwoScale EnstStep

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **the window hypotheses from Qmax and Emax at the window start** -/
theorem window_hyps_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (Mm : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ Mm + 2) (Rc t0 : Nat) (hRM : Rc ≤ Mm + 1)
    (ι h ν Zb : R) (hh : O.nn h) (hν : O.nn ν) (hν0 : ν ≠ K.zero) (hZb : O.nn Zb)
    (a N : Nat → I → V3 R)
    (hN0 : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hN : ∀ t, Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (N t r) (N t r))) l)
      (lsum K (fun r => K.mul (wt K kz r) (dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r))) l))
    (memb : I → Bool) (hm : ∀ r, r ∈ l → memb r = true) (hm' : ∀ r, memb r = true → r ∈ l)
    (hs : ∀ t r, r ∈ l → Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rc t0)) Zb) (K.mul ν ν))
    (n k : Nat) (Qmax Emax : R)
    (hQ : Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz a) (EnstGalerkin.As K l kz a) (n * k)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rc)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
      (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hE : Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k)) Emax)
    (t : Nat) (ht : t ≤ n) :
    Le K O.toCone (EnstGalerkin.Zs K l kz a (n * k + t)) Zb ∧
    Le K O.toCone (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (a s)) (fun s => ForceAbsorb.Bs K kz l (a s)) (n * k + t))
      (K.add Emax (K.mul (castN K t) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3)))))))) := by
  have w := StepZeroExt.h2_large_le_mem K O inv mul_inv h10 l kz hd h1 Mm hM Rc t0 hRM ι h ν Zb hh hν hν0 hZb
    (fun s => a (n * k + s)) (fun s => N (n * k + s)) (fun s => hN0 (n * k + s)) (fun s => hN (n * k + s))
    memb hm hm' (fun s r hr => hs (n * k + s) r hr) choice n
    (le_trans' K O.toCone _ _ _ (le_add' K O.toCone _ _ _ _ hQ (le_refl' K O.toCone _)) hbud)
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε t ht
  exact ⟨w.1, le_trans' K O.toCone _ _ _ w.2 (le_add' K O.toCone _ _ _ _ hE (le_refl' K O.toCone _))⟩

end Gen

end WindowHypsMem

#print axioms WindowHypsMem.window_hyps_mem

#print axioms: 1 theorem, no axioms.

79.324 Truncation Cauchy chain from two energy maxima

The truncation Cauchy chain over the windows holds from the bounds \(Q_{\max ,A}\) and \(Q_{\max ,B}\) alone.

ns/WindowCondMem.lean

/-
  ns/WindowCondMem.lean — T37 (goal G795b-j): WindowCondUpto.large_cond_upto without the small-truncation energy
  hypothesis hEB. The system B on l.filter (lowB Rc) satisfies Step only on that list; extended by 0 off the list
  (StepZeroExt) it gets its window energy bound from a bound Q_B(n·j) ≤ Qmax_B at the window starts, as A does:
    large_cond_mem   d_j ≤ c^j d_0 + c s₀ Σ_{i<j} c^i for j ≤ K′, from Qmax_A and Qmax_B
  (and a Boolean membership test for l.filter (lowB Rc)).
    mem_filter_of, pairwise_filter   the filtered list inherits membership in l and pairwise distinct modes.

  Lean 4 core plus ns/WindowCondUpto, ns/StepZeroExt, ns/WindowHypsMem.
-/
import ns.WindowCondUpto
import ns.StepZeroExt
import ns.WindowHypsMem

namespace WindowCondMem
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate

theorem mem_filter_of {I : Type} (f : I → Bool) : ∀ (l : List I) (r : I), r ∈ l.filter f → r ∈ l
  | [], _, h => nomatch h
  | x :: l, r, h => by
    cases e : f x with
    | true =>
      have h' : r ∈ x :: l.filter f := by rw [List.filter, e] at h; exact h
      cases h' with
      | head => exact List.Mem.head _
      | tail _ h2 => exact List.Mem.tail _ (mem_filter_of f l r h2)
    | false =>
      have h' : r ∈ l.filter f := by rw [List.filter, e] at h; exact h
      exact List.Mem.tail _ (mem_filter_of f l r h')

theorem pairwise_filter {I : Type} (P : I → I → Prop) (f : I → Bool) :
    ∀ l : List I, List.Pairwise P l → List.Pairwise P (l.filter f)
  | [], _ => List.Pairwise.nil
  | x :: l, hp => by
    cases hp with
    | cons hx hl =>
      cases e : f x with
      | true =>
        rw [List.filter, e]
        exact List.Pairwise.cons (fun y hy => hx y (mem_filter_of f l y hy)) (pairwise_filter P f l hl)
      | false =>
        rw [List.filter, e]
        exact pairwise_filter P f l hl

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **Truncation Cauchy chain over K′ windows from Qmax_A and Qmax_B alone.** -/
theorem large_cond_mem (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M Mm : Nat) (hMm : M = Mm + 1) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n K' : Nat)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (Rl t1 : Nat) (hRl : Rl ≤ M)
    (choice : Le K O.toCone (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C2 K inv Rl t1)) Zb) (K.mul ν ν))
    (Qmax : R)
    (hQ : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K l kz A) (EnstGalerkin.As K l kz A) (n * j)) Qmax)
    (hbud : Le K O.toCone (K.add Qmax
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (e D1 D2 D3 : R) (he : O.nn e) (hD1 : O.nn D1) (hD2 : O.nn D2) (hD3 : O.nn D3)
    (H1 : Le K O.toCone (K.mul (AgmonAbsorb.c3 K (castN K 16)) (AgmonAbsorb.q5 K Zb))
      (K.mul (AgmonAbsorb.c3 K (castN K 9)) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D1))))
    (H2 : Le K O.toCone (K.mul (castN K 16) (AgmonAbsorb.q5 K Zb))
      (K.mul (castN K 9) (K.mul (RootYoung.four K) (K.mul (AgmonAbsorb.c3 K e) D2))))
    (H3 : Le K O.toCone (AgmonAbsorb.c3 K Zb) (K.mul (RootYoung.four K) (K.mul e D3)))
    (hε : Le K O.toCone (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e))))) (K.mul ν ν))
    (hεAdef : εA = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDAdef : DA = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEAdef : EA = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz l (A s)) (fun s => ForceAbsorb.Bs K kz l (A s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DA))))
    (QmaxB : R)
    (hQB : ∀ j, Le K O.toCone (EnstGlobal.Q K ν h (EnstGalerkin.Zs K (l.filter (lowB Rc kz)) kz B) (EnstGalerkin.As K (l.filter (lowB Rc kz)) kz B) (n * j)) QmaxB)
    (hbudB : Le K O.toCone (K.add QmaxB
      (K.mul (castN K n) (K.mul h (K.mul (EnstLocalGalerkin.cc K ι (EnstLocalGalerkin.C1 K inv Rl)) (K.mul Zb Zb)))))
      (K.mul ν Zb))
    (memb : I → Bool) (hm : ∀ r, r ∈ (l.filter (lowB Rc kz)) → memb r = true) (hm' : ∀ r, memb r = true → r ∈ (l.filter (lowB Rc kz)))
    (hεBdef : εB = (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))) (hDBdef : DB = (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))
    (hEBdef : EB = K.add (H2Linear.En K ν h (K.mul (K.mul ι ι) (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108)) e)
        (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add e (K.mul (castN K 2) e)))))
        (fun s => ForceAbsorb.As K kz (l.filter (lowB Rc kz)) (B s)) (fun s => ForceAbsorb.Bs K kz (l.filter (lowB Rc kz)) (B s)) (n * 0)) (K.mul (castN K K') (K.mul (castN K n) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (P S0 : R)
    (hPdef : P = pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
    (hS0def : S0 = K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))
    (hP : O.nn P) (iν : R) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν) :
    ∀ j : Nat, j ≤ K' → Le K O.toCone
      (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * j))
      (K.add (K.mul (pwr K (K.mul P (pwr K iν n)) j)
          (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
            (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (n * 0)))
        (K.mul (K.mul (K.mul P (pwr K iν n)) S0) (sumN K (pwr K (K.mul P (pwr K iν n))) j))) := by
  subst hMm
  subst hεAdef
  subst hDAdef
  subst hEAdef
  subst hDBdef
  subst hεBdef
  subst hEBdef
  have hX : O.nn (K.mul (castN K n) (K.mul h (K.mul (K.mul ι ι)
          (K.add (K.mul (AgmonCollapse.k1 K (castN K 3) (castN K 9) (castN K 108))
              (K.add D1 (K.mul (AgmonAbsorb.c3 K (castN K 2)) (K.mul Zb Zb))))
            (K.mul (AgmonCollapse.k2 K (castN K 9) (castN K 108)) (K.add D2 (K.mul (castN K 2) D3))))))) := O.nn_mul _ _ (RatModel.nn_cast K O.toCone n) (O.nn_mul _ _ hh hDA)
  have hE := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (WindowEnergy.energy_upto K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1 hRl ι h ν Zb hh hν hν0 hZb A
      (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n Qmax hQ hbud
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have W := fun j (hj : j < K') t (ht : t ≤ n) => WindowHyps.window_hyps K O inv mul_inv h10 l kz hd h1 Mm hM Rl t1
    hRl ι h ν Zb hh hν hν0 hZb A (fun t r => Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) hA choice n j Qmax _ (hQ j) hbud
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hE j hj) t ht
  have hdL : List.Pairwise (fun a b => kz a ≠ kz b) (l.filter (lowB Rc kz)) := pairwise_filter _ (lowB Rc kz) l hd
  have hML : ∀ p, p ∈ (l.filter (lowB Rc kz)) → nrm (kz p) ≤ Mm + 1 + 1 := fun p hp => hM p (mem_filter_of (lowB Rc kz) l p hp)
  have hEb := fun j (hj : j < K') => le_trans' K O.toCone _ _ _
    (StepZeroExt.energy_upto_mem K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdL h1 Mm hML Rl t1 hRl ι h ν Zb hh hν hν0 hZb B
      (fun t r => Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)
      (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) memb hm hm' hB choice n QmaxB hQB hbudB
      e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε K' j (Nat.le_of_lt hj))
    (le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (ForceSob.le_mul_right' K O.toCone (RatModel.cast_mono K O.toCone j K' (Nat.le_of_lt hj)) hX))
  have WB := fun j (hj : j < K') t (ht : t ≤ n) => WindowHypsMem.window_hyps_mem K O inv mul_inv h10 (l.filter (lowB Rc kz)) kz hdL h1 Mm
    hML Rl t1 hRl ι h ν Zb hh hν hν0 hZb B (fun t r => Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r)
    (fun t => le_refl' K O.toCone _) (fun t => le_refl' K O.toCone _) memb hm hm' hB choice n j QmaxB _ (hQB j) hbudB
    e D1 D2 D3 he hD1 hD2 hD3 H1 H2 H3 hε (hEb j hj) t ht
  exact WindowShiftUpto.large_chain_upto K O inv mul_inv h10 l kz hd h1 (Mm + 1) hM Rc R' t0 hRM ι h ν Zb Aa Ab iR
    hh hν hZb hAa hAb hiRn hiR A B hA hB n K' (fun j hj t ht => (W j hj t (Nat.le_of_lt ht)).1) _ _ _ _ _ _
    hεA hεB hDA hDB hν0 (fun j hj t ht => (W j hj t ht).2) (fun j hj t ht => (WB j hj t ht).2) hAa_def hAb_def hβc P S0 hPdef hS0def hP iν hνi hiν

end Gen

end WindowCondMem

#print axioms WindowCondMem.mem_filter_of
#print axioms WindowCondMem.pairwise_filter
#print axioms WindowCondMem.large_cond_mem

#print axioms: 3 theorems, no axioms.

79.325 Few steps with large enstrophy

For arbitrary data, the number of steps with enstrophy at least \(b\) times \(h\nu b\) is bounded by \(E_0 + h\nu Z_0\), uniformly in the truncation.

ns/BadCount.lean

/-
  ns/BadCount.lean — T37 (goal G859c2-s1): the discrete form of Leray's structure statement — the number of time
  steps with large enstrophy is bounded by the data, with no dependence on the truncation.
    ind, countB        indicator of a Boolean flag and the number of flagged steps s < n
    le_of_eq''         equality gives ≤
    bad_sum            flagged steps have c ≤ Y_s, all Y_s ≥ 0  ⇒  #flagged·c ≤ Σ_{s<n} Y_s
    bad_count          the same with Σ_{s<n} Y_s ≤ S  ⇒  #flagged·c ≤ S
    count_split        #flagged + #unflagged = n
    diss_total         under the cfl_global hypotheses: Σ_{t ≤ n} hν·Z_{t+1} ≤ E_0 + hν·Z_0
    leray_bad_count    every step t ≤ n flagged only when b ≤ Z_{t+1} satisfies
                       #flagged·(hν·b) ≤ E_0 + hν·Z_0.
  Lean 4 core plus ns/EventSmall.
-/
import ns.EventSmall

namespace BadCount
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall

/-- indicator of a Boolean flag -/
def ind : Bool → Nat
  | true => 1
  | false => 0

/-- number of flagged steps among s < n -/
def countB (bad : Nat → Bool) : Nat → Nat
  | 0 => 0
  | n + 1 => countB bad n + ind (bad n)

theorem count_split (bad : Nat → Bool) : ∀ n, countB bad n + countB (fun s => !(bad s)) n = n
  | 0 => rfl
  | n + 1 => by
    have ih := count_split bad n
    show countB bad n + ind (bad n) + (countB (fun s => !(bad s)) n + ind (!(bad n))) = n + 1
    cases bad n with
    | true =>
      show countB bad n + 1 + countB (fun s => !(bad s)) n = n + 1
      rw [Nat.add_right_comm, ih]
    | false =>
      show countB bad n + (countB (fun s => !(bad s)) n + 1) = n + 1
      rw [← Nat.add_assoc, ih]

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem le_of_eq'' (P : Cone K) (x y : R) (e : x = y) : Le K P x y :=
  ⟨K.zero, P.nn_zero, by rw [add_zero' K]; exact e⟩

theorem bad_sum (P : Cone K) (bad : Nat → Bool) (Y : Nat → R) (c : R) (hY : ∀ s, P.nn (Y s)) :
    ∀ n, (∀ s, s < n → bad s = true → Le K P c (Y s)) →
      Le K P (K.mul (castN K (countB bad n)) c) (sumN K Y n)
  | 0, _ => ⟨K.zero, P.nn_zero, by show K.add (K.mul K.zero c) K.zero = K.zero; lib_ring⟩
  | n + 1, h => by
    have ih := bad_sum P bad Y c hY n (fun s hs hb => h s (Nat.lt_succ_of_lt hs) hb)
    show Le K P (K.mul (castN K (countB bad n + ind (bad n))) c) (K.add (sumN K Y n) (Y n))
    cases hb : bad n with
    | true =>
      have s := le_add' K P _ _ _ _ ih (h n (Nat.lt_succ_self n) hb)
      refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s
      show K.mul (K.add K.one (castN K (countB bad n))) c = K.add (K.mul (castN K (countB bad n)) c) c
      lib_ring
    | false =>
      have s := le_add' K P _ _ _ _ ih (⟨Y n, hY n, K.zero_add _⟩ : Le K P K.zero (Y n))
      refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s
      show K.mul (castN K (countB bad n)) c = K.add (K.mul (castN K (countB bad n)) c) K.zero
      lib_ring

theorem bad_count (P : Cone K) (bad : Nat → Bool) (Y : Nat → R) (c S : R) (hY : ∀ s, P.nn (Y s)) (n : Nat)
    (h : ∀ s, s < n → bad s = true → Le K P c (Y s)) (hS : Le K P (sumN K Y n) S) :
    Le K P (K.mul (castN K (countB bad n)) c) S :=
  le_trans' K P _ _ _ (bad_sum K P bad Y c hY n h) hS

include mul_inv h10 in
/-- the integrated enstrophy over the first n + 1 steps is bounded by the data -/
theorem diss_total (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat) :
    Le K O.toCone
      (sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have nZ : ∀ t, O.nn (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a t r) (a t r))) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l
  have nE : ∀ t, O.nn (lsum K (fun r => dot3 K (a t r) (a t r)) l) :=
    fun t => lsum_nn K O.toCone _ (fun r => dot3_nn K O.toCone _) l
  have nβ : O.nn (beta K M ι h ν Es) := by
    have three : O.nn (K.add (K.add K.one K.one) K.one) := O.nn_add _ _ (O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)) (RatModel.nn_one' K O.toCone)
    have two : O.nn (K.add K.one K.one) := O.nn_add _ _ (RatModel.nn_one' K O.toCone) (RatModel.nn_one' K O.toCone)
    have nc := cM_nn K O M
    exact O.nn_mul _ _ (O.nn_sq h) (O.nn_add _ _ (O.nn_mul _ _ three (O.nn_mul _ _ (O.nn_sq ι) (O.nn_mul _ _ (RatModel.nn_cast K O.toCone 135) (O.nn_mul _ _ (O.nn_sq _) hEs))))
      (O.nn_mul _ _ two (O.nn_mul _ _ (O.nn_sq ν) nc)))
  have g := cfl_global K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 (n + 1)
  have e1 : Le K O.toCone (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)))
      (K.add (K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l)
        (K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1))))
        (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l))) :=
    ⟨K.add (lsum K (fun r => dot3 K (a (n + 1) r) (a (n + 1) r)) l) (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (n + 1) r) (a (n + 1) r))) l)),
      O.nn_add _ _ (nE (n + 1)) (O.nn_mul _ _ nβ (nZ (n + 1))), by lib_ring⟩
  have e2 := le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l))
    (le_mul_right' K O.toCone hcfl (nZ 0))
  have tot := le_trans' K O.toCone _ _ _ e1 (le_trans' K O.toCone _ _ _ g e2)
  have sm : sumN K (fun t => K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) (n + 1)
      = K.mul (K.mul h ν) (sumN K (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l) (n + 1)) :=
    NSChain.sumN_mul_left K _ _ (n + 1)
  rw [← sm] at tot
  exact tot

include mul_inv h10 in
/-- Leray's structure statement, discrete: steps with enstrophy at least b are few, uniformly in the truncation -/
theorem leray_bad_count (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (bad : Nat → Bool) (b : R)
    (hbad : ∀ t, t < n + 1 → bad t = true →
      Le K O.toCone b (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :
    Le K O.toCone (K.mul (castN K (countB bad (n + 1))) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hY : ∀ t, O.nn (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :=
    fun t => O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
  exact bad_count K O.toCone bad _ _ _ hY (n + 1)
    (fun t ht hb => le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hh hν) (hbad t ht hb))
    (diss_total K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n)

end Gen

end BadCount

#print axioms BadCount.count_split
#print axioms BadCount.le_of_eq''
#print axioms BadCount.bad_sum
#print axioms BadCount.bad_count
#print axioms BadCount.diss_total
#print axioms BadCount.leray_bad_count

#print axioms: 6 theorems, no axioms.

79.326 Runs of steps with large enstrophy are short

If the enstrophy is at least \(b\) on \(L\) consecutive steps, then \(L\,h\nu b \le E_0 + h\nu Z_0\), uniformly in the truncation.

ns/BadRun.lean

/-
  ns/BadRun.lean — T37 (goal G859c2-s2): a run of consecutive steps with large enstrophy is short, uniformly in the
  truncation — the discrete form of "the singular times of a Leray solution have finite measure".
    sum_mono       nonnegative terms: Σ_{s<m} Y_s ≤ Σ_{s<m+k} Y_s
    run_sum        c ≤ Y_{s+t} for t < L, all Y ≥ 0  ⇒  L·c ≤ Σ_{t < s+L} Y_t
    leray_run      under the cfl_global hypotheses, if Z_{t+1} ≥ b for s ≤ t < s + L (s + L ≤ n + 1), then
                   L·(hν·b) ≤ E_0 + hν·Z_0.
  Lean 4 core plus ns/BadCount.
-/
import ns.BadCount

namespace BadRun
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall BadCount

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem sum_mono (P : Cone K) (Y : Nat → R) (hY : ∀ s, P.nn (Y s)) (m : Nat) :
    ∀ k, Le K P (sumN K Y m) (sumN K Y (m + k))
  | 0 => le_refl' K P _
  | k + 1 => by
    have ih := sum_mono P Y hY m k
    show Le K P (sumN K Y m) (K.add (sumN K Y (m + k)) (Y (m + k)))
    exact le_trans' K P _ _ _ ih ⟨Y (m + k), hY (m + k), rfl⟩

theorem run_sum (P : Cone K) (Y : Nat → R) (c : R) (hY : ∀ s, P.nn (Y s)) (s : Nat) :
    ∀ L, (∀ t, t < L → Le K P c (Y (s + t))) → Le K P (K.mul (castN K L) c) (sumN K Y (s + L))
  | 0, _ => by
    refine le_trans' K P _ _ _ (le_of_eq'' K P _ K.zero ?_) ⟨_, sumN_nn K P Y hY s, K.zero_add _⟩
    show K.mul K.zero c = K.zero
    lib_ring
  | L + 1, h => by
    have ih := run_sum P Y c hY s L (fun t ht => h t (Nat.lt_succ_of_lt ht))
    have s2 := le_add' K P _ _ _ _ ih (h L (Nat.lt_succ_self L))
    show Le K P (K.mul (K.add K.one (castN K L)) c) (K.add (sumN K Y (s + L)) (Y (s + L)))
    refine le_trans' K P _ _ _ (le_of_eq'' K P _ _ ?_) s2
    lib_ring

theorem le_split {a b : Nat} (h : a ≤ b) : ∃ k, b = a + k := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih =>
    obtain ⟨k, hk⟩ := ih
    exact ⟨k + 1, by rw [hk]; rfl⟩

include mul_inv h10 in
/-- **A run of steps with enstrophy ≥ b has length L with L·hν·b ≤ E_0 + hνZ_0.** -/
theorem leray_run (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (b : R) (s L : Nat) (hsL : s + L ≤ n + 1)
    (hrun : ∀ t, t < L →
      Le K O.toCone b (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l)) :
    Le K O.toCone (K.mul (castN K L) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) := by
  have hw : ∀ r, O.nn (wt K kz r) := fun r => dot3_nn K O.toCone _
  have hY : ∀ t, O.nn (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (t + 1) r) (a (t + 1) r))) l)) :=
    fun t => O.nn_mul _ _ (O.nn_mul _ _ hh hν) (lsum_nn K O.toCone _ (fun r => O.nn_mul _ _ (hw r) (dot3_nn K O.toCone _)) l)
  have r1 := run_sum K O.toCone _ (K.mul (K.mul h ν) b) hY s L
    (fun t ht => le_mul_nn K O.toCone _ _ _ (O.nn_mul _ _ hh hν) (hrun t ht))
  obtain ⟨k, hk⟩ := le_split hsL
  have r2 := sum_mono K O.toCone _ hY (s + L) k
  rw [← hk] at r2
  exact le_trans' K O.toCone _ _ _ r1 (le_trans' K O.toCone _ _ _ r2
    (diss_total K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n))

end Gen

end BadRun

#print axioms BadRun.sum_mono
#print axioms BadRun.run_sum
#print axioms BadRun.le_split
#print axioms BadRun.leray_run

#print axioms: 4 theorems, no axioms.

79.327 Every long stretch contains a good step

If \(L\,h\nu b > E_0 + h\nu Z_0\), then among any \(L\) consecutive steps one has enstrophy at most \(b\).

ns/GoodStep.lean

/-
  ns/GoodStep.lean — T37 (goal G859c2-s3a): every long enough stretch of steps contains a step with small enstrophy,
  for arbitrary data and uniformly in the truncation (the corollary of ns/BadRun used to place window starts).
    split_run      for a family Y and a bound b over an ordered ring: either Y_t ≤ b for some t < L, or b ≤ Y_t for all t < L
                   (finite case split on the total order, no choice)
    good_step      under the cfl_global hypotheses, if L·hν·b ≤ E_0 + hν·Z_0 fails and s + L ≤ n + 1, then some
                   s ≤ t < s + L has Z_{t+1} ≤ b.
  Lean 4 core plus ns/BadRun.
-/
import ns.BadRun

namespace GoodStep
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGalerkin CFLStep CFLGlobal EventSmall BadCount BadRun

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

theorem split_run (Y : Nat → R) (b : R) :
    ∀ L, (∃ t, t < L ∧ Le K O.toCone (Y t) b) ∨ (∀ t, t < L → Le K O.toCone b (Y t))
  | 0 => Or.inr (fun t ht => absurd ht (Nat.not_lt_zero t))
  | L + 1 => by
    cases split_run Y b L with
    | inl h =>
      obtain ⟨t, ht, hb⟩ := h
      exact Or.inl ⟨t, Nat.lt_succ_of_lt ht, hb⟩
    | inr h =>
      cases O.total (Y L) b with
      | inl hb => exact Or.inl ⟨L, Nat.lt_succ_self L, hb⟩
      | inr hb =>
        refine Or.inr (fun t ht => ?_)
        cases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ ht) with
        | inl h' => exact h t h'
        | inr h' => rw [h']; exact hb

include mul_inv h10 in
/-- **A long stretch contains a good step.** -/
theorem good_step (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1)
    (ι h ν Es : R) (hh : O.nn h) (hν : O.nn ν) (hEs : O.nn Es) (a N : Nat → I → V3 R)
    (hs : ∀ t r, Step K h ν (wt K kz r) (a (t + 1) r) (a t r) (N t r))
    (hneut : ∀ t, lsum K (fun r => dot3 K (N t r) (a t r)) l = K.zero)
    (hP : ∀ t, Le K O.toCone (lsum K (fun r => dot3 K (N t r) (N t r)) l)
      (lsum K (fun r => dot3 K (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)
        (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (a t) (a t) r)) l))
    (hcfl : Le K O.toCone (beta K M ι h ν Es) (K.mul h ν))
    (h0 : Le K O.toCone (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
      (K.mul (beta K M ι h ν Es) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l))) Es) (n : Nat)
    (b : R) (s L : Nat) (hsL : s + L ≤ n + 1)
    (hL : ¬ Le K O.toCone (K.mul (castN K L) (K.mul (K.mul h ν) b))
      (K.add (lsum K (fun r => dot3 K (a 0 r) (a 0 r)) l)
        (K.mul (K.mul h ν) (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a 0 r) (a 0 r))) l)))) :
    ∃ t, t < L ∧
      Le K O.toCone (lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l) b := by
  cases split_run K O (fun t => lsum K (fun r => K.mul (wt K kz r) (dot3 K (a (s + t + 1) r) (a (s + t + 1) r))) l) b L with
  | inl h' => exact h'
  | inr h' =>
    exact absurd (leray_run K O inv mul_inv h10 l kz hd h1 M hM ι h ν Es hh hν hEs a N hs hneut hP hcfl h0 n b s L hsL h') hL

end Gen

end GoodStep

#print axioms GoodStep.split_run
#print axioms GoodStep.good_step

#print axioms: 2 theorems, no axioms.

79.328 The large-data window step at any start

The truncation Cauchy step of a window \([m, m+n]\) for an arbitrary start \(m\).

ns/WindowAt.lean

/-
  ns/WindowAt.lean — T37 (goal G859c2-s3b, first piece): the large-data truncation Cauchy step on a window [m, m + n]
  with an arbitrary start m (ns/WindowShift.large_window has m = n·k): window starts can be placed at good steps
  (ns/GoodStep), with windows of different lengths.
    large_window_at   ν^n Q_d(m + n) ≤ (ν + hα)^n (Q_d(m) + n·h·S) under the local hypotheses on [m, m + n]
  Lean 4 core plus ns/WindowShift.
-/
import ns.WindowShift

namespace WindowAt
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate
  WindowShift

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **LargeCauchy on the window [m, m + n], any start m** -/
theorem large_window_at (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n m : Nat) (hZA : ∀ t, t < n → Le K O.toCone (Zl K kz l (A (m + t))) Zb)
    (εA εB DA DB EA EB : R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (m + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (m + t) p) (A (m + t) p))) l)))
      (K.add EA (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ t, t ≤ n → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (m + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (m + t) p) (B (m + t) p))) (l.filter (lowB Rc kz)))))
      (K.add EB (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : Aa = K.mul (K.add EA (K.mul (castN K n) (K.mul h DA))) (inv ν))
    (hAb_def : Ab = K.mul (K.add EB (K.mul (castN K n) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν)) :
    Le K O.toCone (K.mul (pwr K ν n)
        (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (m + n)))
      (K.mul (pwr K (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) n)
        (K.add (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (m))
          (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))))) :=
  LargeCauchy.large_cauchy K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
    hiRn hiR (fun t => A (m + t)) (fun t => B (m + t)) (fun t r => hA (m + t) r)
    (fun t r hr => hB (m + t) r hr) n hZA εA εB DA DB EA EB hεA hεB hDA hDB hν0 hEA hEB hAa_def hAb_def hβc
    n (Nat.le.refl)

end Gen

end WindowAt

#print axioms WindowAt.large_window_at

#print axioms: 1 theorem, no axioms.

79.329 Window factors of variable length

Powers of a factor at least \(1\) are monotone in the exponent, so windows of length at most \(n\) have factor at most the length-\(n\) factor.

ns/PowOne.lean

/-
  ns/PowOne.lean — T37 (goal G859c2-s3b, second piece): powers of a factor ≥ 1 are monotone in the exponent, so the
  window factor c_k = ((ν + hα)·ν⁻¹)^{n_k} of a window of length n_k ≤ n is at most the factor of length n — a uniform
  c for the variable-length window chain.
    one_le_mul     1 ≤ x, 1 ≤ y ⇒ 1 ≤ x·y
    one_le_pwr     1 ≤ x ⇒ 1 ≤ x^k
    pwr_le_add     1 ≤ x ⇒ x^a ≤ x^{a+k}
    pwr_le_of_le   1 ≤ x, a ≤ b ⇒ x^a ≤ x^b
    factor_le      1 ≤ (ν + hα)·ν⁻¹, n_k ≤ n ⇒ (ν + hα)^{n_k}(ν⁻¹)^{n_k} ≤ ((ν + hα)·ν⁻¹)^n
  Lean 4 core plus ns/DyadicCoef, ns/Dyadic, ns/BadRun.
-/
import ns.DyadicCoef
import ns.BadRun

namespace PowOne
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem one_le_mul (x y : R) (hx : Le K O.toCone K.one x) (hy : Le K O.toCone K.one y) :
    Le K O.toCone K.one (K.mul x y) := by
  obtain ⟨a, ha, ea⟩ := hx
  obtain ⟨b, hb, eb⟩ := hy
  refine ⟨K.add (K.add a b) (K.mul a b), O.nn_add _ _ (O.nn_add _ _ ha hb) (O.nn_mul _ _ ha hb), ?_⟩
  rw [← ea, ← eb]
  lib_ring

theorem one_le_pwr (x : R) (hx : Le K O.toCone K.one x) : ∀ k, Le K O.toCone K.one (pwr K x k)
  | 0 => le_refl' K O.toCone _
  | k + 1 => one_le_mul K O x (pwr K x k) hx (one_le_pwr x hx k)

theorem pwr_le_add (x : R) (hx : Le K O.toCone K.one x) (a k : Nat) :
    Le K O.toCone (pwr K x a) (pwr K x (a + k)) := by
  obtain ⟨d, hd, ed⟩ := one_le_pwr K O x hx k
  obtain ⟨e, he, ee⟩ := one_le_pwr K O x hx a
  refine ⟨K.mul (pwr K x a) d, O.nn_mul _ _ ?_ hd, ?_⟩
  · rw [← ee]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) he
  · rw [DyadicCoef.pwr_add K x a k, ← ed]
    lib_ring

theorem pwr_le_of_le (x : R) (hx : Le K O.toCone K.one x) (a b : Nat) (hab : a ≤ b) :
    Le K O.toCone (pwr K x a) (pwr K x b) := by
  obtain ⟨k, hk⟩ := BadRun.le_split hab
  rw [hk]
  exact pwr_le_add K O x hx a k

theorem factor_le (u iν : R) (hu : Le K O.toCone K.one (K.mul u iν)) (nk n : Nat) (hn : nk ≤ n) :
    Le K O.toCone (K.mul (pwr K u nk) (pwr K iν nk)) (pwr K (K.mul u iν) n) := by
  rw [Dyadic.pwr_mul K u iν nk]
  exact pwr_le_of_le K O _ hu nk n hn

end Ring

end PowOne

#print axioms PowOne.one_le_mul
#print axioms PowOne.one_le_pwr
#print axioms PowOne.pwr_le_add
#print axioms PowOne.pwr_le_of_le
#print axioms PowOne.factor_le

#print axioms: 5 theorems, no axioms.

79.330 The window chain with variable window lengths

From \(\nu ^{n_k} d_{k+1} \le u^{n_k}(d_k + s_0)\) with \(n_k \le n\): \(d_K \le c^K d_0 + c s_0 \sum _{j<K} c^j\), \(c = (u\nu ^{-1})^n\).

ns/VarChain.lean

/-
  ns/VarChain.lean — T37 (goal G859c2-s3b, third piece): the window chain for windows of variable lengths n_k ≤ n
  (window starts at good steps, ns/GoodStep): from ν^{n_k} d_{k+1} ≤ u^{n_k}(d_k + s₀) on every window,
      d_K ≤ c^K d_0 + c s₀ Σ_{j<K} c^j,   c = (u·ν⁻¹)^n   (u·ν⁻¹ ≥ 1),
  the same closed form as the fixed-length chain (ns/WindowShift.scale_chain), with the uniform factor of
  ns/PowOne.factor_le.
    mul_right_le   a ≤ b, x ≥ 0 ⇒ a·x ≤ b·x
    var_step       one window of length n_k ≤ n
    var_chain      the chain
  Lean 4 core plus ns/WindowShift, ns/PowOne.
-/
import ns.WindowShift
import ns.PowOne

namespace VarChain
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall BadCount PowOne WindowShift

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem mul_right_le (a b x : R) (h : Le K O.toCone a b) (hx : O.nn x) : Le K O.toCone (K.mul a x) (K.mul b x) := by
  have m := le_mul_nn K O.toCone x a b hx h
  refine le_trans' K O.toCone _ _ _ (le_of_eq'' K O.toCone _ _ ?_) (le_trans' K O.toCone _ _ _ m (le_of_eq'' K O.toCone _ _ ?_))
  · lib_ring
  · lib_ring

theorem var_step (ν iν u s0 x y : R) (n nk : Nat) (hνi : K.mul ν iν = K.one) (hiν : O.nn iν)
    (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : nk ≤ n) (hy : O.nn y) (hs0 : O.nn s0)
    (h : Le K O.toCone (K.mul (pwr K ν nk) x) (K.mul (pwr K u nk) (K.add y s0))) :
    Le K O.toCone x (K.add (K.mul (pwr K (K.mul u iν) n) y) (K.mul (pwr K (K.mul u iν) n) s0)) := by
  have s := scale_step K O ν iν (pwr K u nk) s0 x y nk hνi hiν h
  have f := factor_le K O u iν hc1 nk n hn
  exact le_trans' K O.toCone _ _ _ s
    (le_add' K O.toCone _ _ _ _ (mul_right_le K O _ _ y f hy) (mul_right_le K O _ _ s0 f hs0))

theorem var_chain (ν iν u s0 : R) (n : Nat) (nk : Nat → Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : ∀ k, nk k ≤ n) (hd : ∀ k, O.nn (d k))
    (hs0 : O.nn s0)
    (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν (nk k)) (d (k + 1))) (K.mul (pwr K u (nk k)) (K.add (d k) s0))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (pwr K (K.mul u iν) n) K') (d 0))
        (K.mul (K.mul (pwr K (K.mul u iν) n) s0) (sumN K (pwr K (pwr K (K.mul u iν) n)) K'))) := by
  have hc : O.nn (pwr K (K.mul u iν) n) := by
    obtain ⟨e, he, ee⟩ := one_le_pwr K O (K.mul u iν) hc1 n
    rw [← ee]; exact O.nn_add _ _ (RatModel.nn_one' K O.toCone) he
  exact WindowChain.window_chain K O (pwr K (K.mul u iν) n) (K.mul (pwr K (K.mul u iν) n) s0) d hc
    (fun k => var_step K O ν iν u s0 (d (k + 1)) (d k) n (nk k) hνi hiν hc1 (hn k) (hd k) hs0 (hwin k))

end Ring

end VarChain

#print axioms VarChain.mul_right_le
#print axioms VarChain.var_step
#print axioms VarChain.var_chain

#print axioms: 3 theorems, no axioms.

79.331 Variable windows with the window source term

With the source \(n_k\sigma \) of a window of length \(n_k \le n\) the chain bound holds with \(s_0 = n\sigma \).

ns/VarChainS.lean

/-
  ns/VarChainS.lean — T37 (goal G859c2-s3b, fourth piece): the variable-length window chain with the source term of
  ns/WindowAt.large_window_at, n_k·σ on a window of length n_k (σ = h·S): since n_k ≤ n, it is bounded by n·σ, and
  ns/VarChain.var_chain applies with s₀ = n·σ.
    castN_le       a ≤ b ⇒ a ≤ b in the ordered ring
    var_chain_s    ν^{n_k} d_{k+1} ≤ u^{n_k}(d_k + n_k σ) for all k  ⇒  d_K ≤ c^K d_0 + c n σ Σ_{j<K} c^j
  Lean 4 core plus ns/VarChain.
-/
import ns.VarChain

namespace VarChainS
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge EnstGronwall BadCount PowOne VarChain

section Ring
variable {R : Type} (K : CRing R) (O : OrdR K)

theorem castN_le_add (a : Nat) : ∀ k, Le K O.toCone (castN K a) (castN K (a + k))
  | 0 => le_refl' K O.toCone _
  | k + 1 => by
    refine le_trans' K O.toCone _ _ _ (castN_le_add a k) ⟨K.one, RatModel.nn_one' K O.toCone, ?_⟩
    show K.add (castN K (a + k)) K.one = K.add K.one (castN K (a + k))
    lib_ring

theorem castN_le (a b : Nat) (h : a ≤ b) : Le K O.toCone (castN K a) (castN K b) := by
  obtain ⟨k, hk⟩ := BadRun.le_split h
  rw [hk]; exact castN_le_add K O a k

theorem var_chain_s (ν iν u σ : R) (n : Nat) (nk : Nat → Nat) (d : Nat → R) (hνi : K.mul ν iν = K.one)
    (hiν : O.nn iν) (hu : O.nn u) (hc1 : Le K O.toCone K.one (K.mul u iν)) (hn : ∀ k, nk k ≤ n)
    (hd : ∀ k, O.nn (d k)) (hσ : O.nn σ)
    (hwin : ∀ k, Le K O.toCone (K.mul (pwr K ν (nk k)) (d (k + 1)))
      (K.mul (pwr K u (nk k)) (K.add (d k) (K.mul (castN K (nk k)) σ)))) :
    ∀ K' : Nat, Le K O.toCone (d K')
      (K.add (K.mul (pwr K (pwr K (K.mul u iν) n) K') (d 0))
        (K.mul (K.mul (pwr K (K.mul u iν) n) (K.mul (castN K n) σ)) (sumN K (pwr K (pwr K (K.mul u iν) n)) K'))) := by
  have hcast : O.nn (castN K n) := RatModel.nn_cast K O.toCone n
  refine var_chain K O ν iν u (K.mul (castN K n) σ) n nk d hνi hiν hc1 hn hd (O.nn_mul _ _ hcast hσ) (fun k => ?_)
  have hs : Le K O.toCone (K.add (d k) (K.mul (castN K (nk k)) σ)) (K.add (d k) (K.mul (castN K n) σ)) :=
    le_add' K O.toCone _ _ _ _ (le_refl' K O.toCone _)
      (mul_right_le K O _ _ σ (castN_le K O (nk k) n (hn k)) hσ)
  exact le_trans' K O.toCone _ _ _ (hwin k) (le_mul_nn K O.toCone _ _ _ (Dyadic.pwr_nn K O u hu (nk k)) hs)

end Ring

end VarChainS

#print axioms VarChainS.castN_le_add
#print axioms VarChainS.castN_le
#print axioms VarChainS.var_chain_s

#print axioms: 3 theorems, no axioms.

79.332 The variable-window truncation Cauchy chain

Over windows of lengths \(n_k \le n\) at arbitrary starts, under the local hypotheses on each window: \(Q_d(s_K) \le c^K Q_d(s_0) + c\,n\sigma \sum _{j<K}c^j\).

ns/VarWindows.lean

/-
  ns/VarWindows.lean — T37 (goal G859c2-s3b, fifth piece): the conditional large-data truncation Cauchy chain over
  windows of variable lengths n_k ≤ n starting at arbitrary steps s_k (s_{k+1} = s_k + n_k): if every window
  satisfies the local hypotheses of ns/WindowAt.large_window_at (enstrophy ≤ Zb, H² energy bounds), then
      Q_d(s_K) ≤ c^K Q_d(s_0) + c·n·σ·Σ_{j<K} c^j,   c = ((ν + hα)ν⁻¹)^n,
  with the same α, σ as in a single window. The window starts can be the good steps of ns/GoodStep (gaps ≤ L);
  what remains open is that the local hypotheses hold on each window (the local time from a good step ≥ L).
    var_windows
  Lean 4 core plus ns/WindowAt, ns/VarChainS.
-/
import ns.WindowAt
import ns.VarChainS

namespace VarWindows
open DetChain NSChain DyadicSmallGlobal LibRing DyadicLarge FourierGalerkin GalerkinForce ForceBound ForceSob
  ModeCount CubeCount ForceUniform NormCompare EnstStep EnstGlobal EnstGalerkin ForceTwoScale ForceBilin EnstDiff
  ForceDiff TruncError TruncBound RestrStep GronwallSource EnstGronwall WeightSplit LatticeSum TruncCauchy TruncRate
  WindowShift
  WindowShift

section Gen
variable {R : Type} (K : CRing R) (O : OrdR K) {I : Type}
variable (inv : R → R) (mul_inv : ∀ y, y ≠ K.zero → K.mul y (inv y) = K.one) (h10 : K.one ≠ K.zero)

include mul_inv h10 in
/-- **The variable-window truncation Cauchy chain (conditional on the window hypotheses).** -/
theorem var_windows (l : List I) (kz : I → Mode) (hd : List.Pairwise (fun a b => kz a ≠ kz b) l)
    (h1 : ∀ p, 1 ≤ nrm (kz p)) (M : Nat) (hM : ∀ p, p ∈ l → nrm (kz p) ≤ M + 1) (Rc R' t0 : Nat) (hRM : R' ≤ M)
    (ι h ν Zb Aa Ab iR : R) (hh : O.nn h) (hν : O.nn ν) (hZb : O.nn Zb) (hAa : O.nn Aa) (hAb : O.nn Ab) (hiRn : O.nn iR)
    (hiR : K.mul (K.mul (castN K (Rc + 1)) (castN K (Rc + 1))) iR = K.one)
    (A B : Nat → I → V3 R)
    (hA : ∀ t r, Step K h ν (wt K kz r) (A (t + 1) r) (A t r) (Nl K l (TriadSparse.delta K kz) ι (kv K kz) (A t) (A t) r))
    (hB : ∀ t r, r ∈ l.filter (lowB Rc kz) → Step K h ν (wt K kz r) (B (t + 1) r) (B t r)
      (Nl K (l.filter (lowB Rc kz)) (TriadSparse.delta K kz) ι (kv K kz) (B t) (B t) r))
    (n : Nat) (s nk : Nat → Nat) (hs : ∀ k, s (k + 1) = s k + nk k) (hn : ∀ k, nk k ≤ n)
    (hZA : ∀ k t, t < nk k → Le K O.toCone (Zl K kz l (A (s k + t))) Zb)
    (εA εB DA DB : R) (EA EB : Nat → R) (hεA : O.nn εA) (hεB : O.nn εB) (hDA : O.nn DA) (hDB : O.nn DB) (hν0 : ν ≠ K.zero)
    (hEA : ∀ k t, t ≤ nk k → Le K O.toCone (K.add (K.mul ν (Al K kz l (A (s k + t))))
      (K.mul (K.mul h εA) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (A (s k + t) p) (A (s k + t) p))) l)))
      (K.add (EA k) (K.mul (castN K t) (K.mul h DA))))
    (hEB : ∀ k t, t ≤ nk k → Le K O.toCone (K.add (K.mul ν (Al K kz (l.filter (lowB Rc kz)) (B (s k + t))))
      (K.mul (K.mul h εB) (lsum K (fun p => K.mul (K.mul (K.mul (wt K kz p) (wt K kz p)) (wt K kz p))
        (dot3 K (B (s k + t) p) (B (s k + t) p))) (l.filter (lowB Rc kz)))))
      (K.add (EB k) (K.mul (castN K t) (K.mul h DB))))
    (hAa_def : ∀ k, Aa = K.mul (K.add (EA k) (K.mul (castN K (nk k)) (K.mul h DA))) (inv ν))
    (hAb_def : ∀ k, Ab = K.mul (K.add (EB k) (K.mul (castN K (nk k)) (K.mul h DB))) (inv ν))
    (hβc : Le K O.toCone (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C2' K inv R' t0) Zb)))
      (K.mul ν ν))
    (hiν : O.nn (inv ν)) (hu : O.nn (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))))
    (hc1 : Le K O.toCone K.one (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)))
    (hdn : ∀ k, O.nn (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s k)))
    (hσ : O.nn (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR))))))))) :
    ∀ K' : Nat, Le K O.toCone (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s K'))
      (K.add (K.mul (pwr K (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n) K') (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s 0)))
        (K.mul (K.mul (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n) (K.mul (castN K n) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))))) (sumN K (pwr K (pwr K (K.mul (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (inv ν)) n)) K'))) :=
  VarChainS.var_chain_s K O ν (inv ν) (K.add ν (K.mul h
          (K.add (K.mul (K.mul (two' K) (K.mul (two' K) (two' K))) (K.mul (K.mul ι ι) (K.mul (C1' K inv R') Zb)))
            (K.mul (K.mul (two' K) (two' K)) (K.mul (K.mul ι ι) (K.mul (castN K 135) Ab)))))) (K.mul h (K.mul (two' K)
            (K.add
              (K.mul (two' K) (K.add
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul (K.mul Aa iR) (K.mul (C1' K inv R') Zb))))
                (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (C2' K inv R' t0) Zb))))))
              (K.mul (two' K) (K.mul (K.mul ι ι) (K.mul Aa (K.mul (castN K 135) (K.mul Aa iR)))))))) n nk
    (fun k => (Q K ν h (fun t => Zl K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t)))
          (fun t => Al K kz (l.filter (lowB Rc kz)) (dF K (restr K (lowB Rc kz) (A t)) (B t))) (s k))) (mul_inv ν hν0) hiν hu hc1 hn hdn hσ (fun k => by
      have hw := WindowAt.large_window_at K O inv mul_inv h10 l kz hd h1 M hM Rc R' t0 hRM ι h ν Zb Aa Ab iR hh hν hZb hAa hAb
        hiRn hiR A B hA hB (nk k) (s k) (hZA k) εA εB DA DB (EA k) (EB k) hεA hεB hDA hDB hν0 (hEA k) (hEB k)
        (hAa_def k) (hAb_def k) hβc
      rw [← hs k] at hw
      exact hw)

end Gen

end VarWindows

#print axioms VarWindows.var_windows

#print axioms: 1 theorem, no axioms.