import F02NavierStokes.LinearAlgebra3D import Mathlib.Analysis.Calculus.MeanValue import Mathlib.Analysis.Calculus.Deriv.Pow import Mathlib.Analysis.SpecialFunctions.Log.Basic import Mathlib.Tactic.Linarith import Mathlib.Tactic.Positivity import Mathlib.Tactic.Ring import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.NoncommRing /-! # Compensated Histories, Glaeser Square-Root Cutoff, Adapted Martingale Trees, # Spectral Inverse Contact Algebra, and ESM3 Schur / UP17–UP18 Certificates Formalizes Section 9.0--9.1 (`prop:rev-comp`, `eq:rev-comp`--`eq:rev-inverse`), `U1`--`U10`, `CP1`--`CP6`, `CP13`--`CP14`, `R6a`--`R6b`, and the ESM3 exact algebra (`check_schur_envelope.py`, `check_joint_cancellation.py`, `check_source_coercivity.py`): - `complete_cap_compensation` & `shifted_shell_lower_coercivity` (`prop:rev-comp`, `U1`--`U2`, `CP1`): pathwise `Ahat = (K₀ + P_cap)I - N₀ - N₁ - N₂ = K₀ I + Ω + Q - N₀`, `A ⪰ Ω ⪰ 0`, and `|ω|⁴ ≤ tr(A²)` (`eq:rev-energy`). - `glaeser_sqrt_cutoff_bound` (`prop:rev-comp` line 1514, `U4`): real-calculus proof that any nonnegative `C²` cutoff `χ : ℝ → ℝ` with `|χ''| ≤ M` satisfies `(χ'(x))² ≤ 2 M χ(x)`. - `mixed_history_gram_identity` & `CompensatedTree` (`eq:rev-history`, `U1`, `U3`, `U4`): exact arbitrary-depth adapted binary martingale tree formalization proving `D_tree = Ω + Q_tree - C_tree = -crossing_tree = E[V Vᵀ - U Vᵀ - V Uᵀ]` and orthogonal endpoint frame congruence for ALL adapted trees. - `nine_products_trace_expansion` & `trace_sq_deficit_identity` (`U5`, `CP13`, `CP14`): noncommuting nine-product trace expansion and `tr(U²) - tr(A²) = tr(L (U + A)) ≥ 0`. - `log_weighted_cap` (`R6a`--`R6b`): `k * (-log k) ≤ exp (-1)` for all `k > 0`. - `matrix_inverse_second_deriv_jet` (`eq:rev-inverse`, `CP3`, `U8`), `spectral_reciprocal_cancellation` (`CP4`), and basis-free `repeated_eigenvalue_contact_operator` (`CP6`, `U9`) proving `P [L_phys J + 2ν ∑_j Y_j G_J⊥ Y_j] P = -μ² P [T - 2ν ∑_j X_j G_⊥ X_j] P` with NO eigenbasis choice and NO division by vanishing top gaps. - ESM3 exact certificates: `both_outcome_return_identity`, `effective_score_cancellation`, `schur_complete_square`, `schur_scalar_optimum`, `up17_factorization_and_link_covariance`. -/ namespace F02NavierStokes.CompensatedHistoryAndInverse open scoped Matrix BigOperators open Matrix F02NavierStokes.LinearAlgebra3D noncomputable section /-- Rank-one outer product `u vᵀ` of two 3D vectors. -/ def outer₂ (u v : Vec3) : Mat3 := Matrix.of fun i j => u i * v j /-! ## 1. Complete cap compensation and lower coercivity (`prop:rev-comp`, `U1`--`U2`, `CP1`) -/ /-- Exact pathwise cap compensation (`U1`, `CP1`, `manuscript.tex` lines 1520--1529): both scalar caps `E_R |ω|²` and `E_R J_src` cancel identically in `Ahat = (K₀ + P_cap) I - N₀ - N₁ - N₂ = K₀ I + Ω + Q_path - N₀`. -/ theorem complete_cap_compensation (K₀ E_R ωSq J_src : ℝ) (Ω Q_path N₀ : Mat3) : let P_cap := E_R * (ωSq + J_src) let N₁ := (E_R * ωSq) • (1 : Mat3) - Ω let N₂ := (E_R * J_src) • (1 : Mat3) - Q_path (K₀ + P_cap) • (1 : Mat3) - N₀ - N₁ - N₂ = K₀ • (1 : Mat3) + Ω + Q_path - N₀ := by intro P_cap N₁ N₂ ext i j simp only [P_cap, N₁, N₂, Matrix.sub_apply, Matrix.add_apply, Matrix.smul_apply, smul_eq_mul] ring /-- Lower coercivity of the shifted shell matrix `A = K₀ I + D_R` (`prop:rev-comp`, `eq:rev-comp`, `eq:rev-energy`): from `D_R = Ω + Q - C`, `0 ⪯ C ⪯ K₀ I`, `Q ⪰ 0`, `Ω = outer ω`, we obtain `A ⪰ Ω ⪰ 0` and `|ω|⁴ ≤ tr(A²)` without matrix commutativity. -/ theorem shifted_shell_lower_coercivity (K₀ : ℝ) (ω : Vec3) (Q C : Mat3) (hQ : IsPSD Q) (hCcap : C ⪯ₗ K₀ • (1 : Mat3)) : let Ω := outer ω let D_R := Ω + Q - C let A := K₀ • (1 : Mat3) + D_R IsPSD Ω ∧ Ω ⪯ₗ A ∧ IsPSD A ∧ (vecSqNorm ω) ^ 2 ≤ (A * A).trace := by intro Ω D_R A have hΩpsd : IsPSD Ω := outer_isPSD ω set P : Mat3 := Q + (K₀ • (1 : Mat3) - C) have hPpsd : IsPSD P := isPSD_add hQ hCcap have hAeq : A = Ω + P := by dsimp only [A, D_R, P] ext i j simp only [Matrix.add_apply, Matrix.sub_apply] ring have hΩleA : Ω ⪯ₗ A := by change IsPSD (A - Ω) have : A - Ω = P := by rw [hAeq]; abel rwa [this] have hApsd : IsPSD A := by rw [hAeq]; exact isPSD_add hΩpsd hPpsd have htr : (vecSqNorm ω) ^ 2 ≤ (A * A).trace := by rw [hAeq] have hexp : ((Ω + P) * (Ω + P)).trace = (Ω * Ω).trace + 2 * (Ω * P).trace + (P * P).trace := by rw [Matrix.add_mul Ω P (Ω + P), Matrix.mul_add Ω Ω P, Matrix.mul_add P Ω P, Matrix.trace_add, Matrix.trace_add, Matrix.trace_add, Matrix.trace_mul_comm P Ω] ring have hΩ2 : (Ω * Ω).trace = (vecSqNorm ω) ^ 2 := by have hsq : Ω * Ω = (vecSqNorm ω) • Ω := by ext i j simp only [Ω, outer, Matrix.mul_apply, Matrix.of_apply, Matrix.smul_apply, smul_eq_mul, vecSqNorm, Fin.sum_univ_three] ring rw [hsq, Matrix.trace_smul, smul_eq_mul, outer_trace, pow_two] have hΩP : 0 ≤ (Ω * P).trace := trace_mul_nonneg_of_isPSD hΩpsd hPpsd have hPP : 0 ≤ (P * P).trace := trace_mul_nonneg_of_isPSD hPpsd hPpsd linarith exact ⟨hΩpsd, hΩleA, hApsd, htr⟩ /-! ## 2. Glaeser's inequality for nonnegative C² cutoffs (`prop:rev-comp`, line 1514) -/ /-- Real-calculus proof of Glaeser's inequality (`prop:rev-comp` line 1514, `U4`): if `χ : ℝ → ℝ` is nonnegative everywhere, differentiable with derivative `χ'`, and `χ'` is differentiable with derivative `χ''` satisfying `χ''(y) ≤ M` (`M ≥ 0`) for all `y ∈ ℝ`, then `(χ'(x))² ≤ 2 M χ(x)` for every `x ∈ ℝ`. -/ theorem glaeser_sqrt_cutoff_bound {χ χ' χ'' : ℝ → ℝ} {M : ℝ} (hM : 0 ≤ M) (hnonneg : ∀ y : ℝ, 0 ≤ χ y) (h1 : ∀ y : ℝ, HasDerivAt χ (χ' y) y) (h2 : ∀ y : ℝ, HasDerivAt χ' (χ'' y) y) (hbound : ∀ y : ℝ, χ'' y ≤ M) : ∀ x : ℝ, (χ' x) ^ 2 ≤ 2 * M * χ x := by intro x -- Step 1: Quadratic Taylor upper bound χ(x + h) ≤ χ(x) + χ'(x) h + (M/2) h² for all h ∈ ℝ. have hquad_pos : ∀ h : ℝ, 0 ≤ h → χ (x + h) ≤ χ x + χ' x * h + (M / 2) * h ^ 2 := by intro h hh let g : ℝ → ℝ := fun t => χ (x + t) - χ x - χ' x * t - (M / 2) * t ^ 2 let g' : ℝ → ℝ := fun t => χ' (x + t) - χ' x - M * t have hg'_deriv : ∀ t : ℝ, HasDerivAt g' (χ'' (x + t) - M) t := by intro t have ha : HasDerivAt (fun u : ℝ => x + u) 1 t := by simpa using (hasDerivAt_id t).const_add x have hc : HasDerivAt (fun u : ℝ => χ' (x + u)) (χ'' (x + t) * 1) t := (h2 (x + t)).comp t ha have hm : HasDerivAt (fun u : ℝ => M * u) M t := by simpa using (hasDerivAt_id t).const_mul M simpa [g', mul_one] using (hc.sub_const (χ' x)).fun_sub hm have hg_deriv : ∀ t : ℝ, HasDerivAt g (g' t) t := by intro t have ha : HasDerivAt (fun u : ℝ => x + u) 1 t := by simpa using (hasDerivAt_id t).const_add x have hc : HasDerivAt (fun u : ℝ => χ (x + u)) (χ' (x + t) * 1) t := (h1 (x + t)).comp t ha have hl : HasDerivAt (fun u : ℝ => χ' x * u) (χ' x) t := by simpa using (hasDerivAt_id t).const_mul (χ' x) have hq : HasDerivAt (fun u : ℝ => (M / 2) * u ^ 2) (M * t) t := by have hp : HasDerivAt (fun u : ℝ => u ^ 2) (2 * t) t := by simpa using hasDerivAt_pow 2 t have := hp.const_mul (M / 2) have hval : (M / 2) * (2 * t) = M * t := by ring rwa [hval] at this simpa [g, g', mul_one] using ((hc.sub_const (χ x)).fun_sub hl).fun_sub hq have hg'_le : ∀ t ∈ Set.Icc 0 h, g' t ≤ 0 := by have hg'0 : g' 0 = 0 := by simp [g'] have hcont : ContinuousOn g' (Set.Icc 0 h) := fun t _ => (hg'_deriv t).continuousAt.continuousWithinAt have hle := image_le_of_deriv_right_le_deriv_boundary hcont (f' := fun t => χ'' (x + t) - M) (fun t _ => (hg'_deriv t).hasDerivWithinAt) (B := fun _ => (0 : ℝ)) (B' := fun _ => 0) (by linarith) continuousOn_const (fun t _ => hasDerivWithinAt_const t (Set.Ici t) 0) (fun t _ => by linarith [hbound (x + t)]) exact fun t ht => hle ht have hg_le : g h ≤ 0 := by have hg0 : g 0 = 0 := by simp [g] have hcont : ContinuousOn g (Set.Icc 0 h) := fun t _ => (hg_deriv t).continuousAt.continuousWithinAt have hle := image_le_of_deriv_right_le_deriv_boundary hcont (f' := g') (fun t _ => (hg_deriv t).hasDerivWithinAt) (B := fun _ => (0 : ℝ)) (B' := fun _ => 0) (by linarith) continuousOn_const (fun t _ => hasDerivWithinAt_const t (Set.Ici t) 0) (fun t ht => hg'_le t ⟨ht.1, le_of_lt ht.2⟩) exact hle ⟨hh, le_refl h⟩ dsimp [g] at hg_le linarith have hquad_neg : ∀ s : ℝ, 0 ≤ s → χ (x - s) ≤ χ x - χ' x * s + (M / 2) * s ^ 2 := by intro s hs let g : ℝ → ℝ := fun t => χ (x - t) - χ x + χ' x * t - (M / 2) * t ^ 2 let g' : ℝ → ℝ := fun t => -χ' (x - t) + χ' x - M * t have hg'_deriv : ∀ t : ℝ, HasDerivAt g' (χ'' (x - t) - M) t := by intro t have ha : HasDerivAt (fun u : ℝ => x - u) (-1) t := by simpa using (hasDerivAt_const t x).fun_sub (hasDerivAt_id t) have hc : HasDerivAt (fun u : ℝ => χ' (x - u)) (χ'' (x - t) * (-1)) t := (h2 (x - t)).comp t ha have hneg := hc.neg have hm : HasDerivAt (fun u : ℝ => M * u) M t := by simpa using (hasDerivAt_id t).const_mul M have hval : -(χ'' (x - t) * -1) - M = χ'' (x - t) - M := by ring simpa [g', hval] using (hneg.add_const (χ' x)).fun_sub hm have hg_deriv : ∀ t : ℝ, HasDerivAt g (g' t) t := by intro t have ha : HasDerivAt (fun u : ℝ => x - u) (-1) t := by simpa using (hasDerivAt_const t x).fun_sub (hasDerivAt_id t) have hc : HasDerivAt (fun u : ℝ => χ (x - u)) (χ' (x - t) * (-1)) t := (h1 (x - t)).comp t ha have hl : HasDerivAt (fun u : ℝ => χ' x * u) (χ' x) t := by simpa using (hasDerivAt_id t).const_mul (χ' x) have hq : HasDerivAt (fun u : ℝ => (M / 2) * u ^ 2) (M * t) t := by have hp : HasDerivAt (fun u : ℝ => u ^ 2) (2 * t) t := by simpa using hasDerivAt_pow 2 t have := hp.const_mul (M / 2) have hval : (M / 2) * (2 * t) = M * t := by ring rwa [hval] at this have hval : χ' (x - t) * -1 + χ' x - M * t = g' t := by dsimp [g']; ring simpa [g, hval] using ((hc.sub_const (χ x)).fun_add hl).fun_sub hq have hg'_le : ∀ t ∈ Set.Icc 0 s, g' t ≤ 0 := by have hg'0 : g' 0 = 0 := by simp [g'] have hcont : ContinuousOn g' (Set.Icc 0 s) := fun t _ => (hg'_deriv t).continuousAt.continuousWithinAt have hle := image_le_of_deriv_right_le_deriv_boundary hcont (f' := fun t => χ'' (x - t) - M) (fun t _ => (hg'_deriv t).hasDerivWithinAt) (B := fun _ => (0 : ℝ)) (B' := fun _ => 0) (by linarith) continuousOn_const (fun t _ => hasDerivWithinAt_const t (Set.Ici t) 0) (fun t _ => by linarith [hbound (x - t)]) exact fun t ht => hle ht have hg_le : g s ≤ 0 := by have hg0 : g 0 = 0 := by simp [g] have hcont : ContinuousOn g (Set.Icc 0 s) := fun t _ => (hg_deriv t).continuousAt.continuousWithinAt have hle := image_le_of_deriv_right_le_deriv_boundary hcont (f' := g') (fun t _ => (hg_deriv t).hasDerivWithinAt) (B := fun _ => (0 : ℝ)) (B' := fun _ => 0) (by linarith) continuousOn_const (fun t _ => hasDerivWithinAt_const t (Set.Ici t) 0) (fun t ht => hg'_le t ⟨ht.1, le_of_lt ht.2⟩) exact hle ⟨hs, le_refl s⟩ dsimp [g] at hg_le linarith have hquad : ∀ h : ℝ, 0 ≤ χ x + χ' x * h + (M / 2) * h ^ 2 := by intro h by_cases hh : 0 ≤ h · exact (hnonneg (x + h)).trans (hquad_pos h hh) · have hs : 0 ≤ -h := by linarith have h1 := (hnonneg (x - (-h))).trans (hquad_neg (-h) hs) linarith [show (-h) ^ 2 = h ^ 2 by ring] rcases eq_or_lt_of_le hM with rfl | hMpos · -- M = 0: χ x + χ' x * h ≥ 0 for all h ∈ ℝ implies χ' x = 0. have h0 : χ' x = 0 := by by_contra hne have hspec := hquad (-(χ x + 1) / χ' x) have hcancel : χ' x * (-(χ x + 1) / χ' x) = -(χ x + 1) := by field_simp [hne] linarith rw [h0]; norm_num · -- M > 0: substitute h = -χ'(x) / M. have hspec := hquad (-(χ' x) / M) have halg : χ x + χ' x * (-(χ' x) / M) + (M / 2) * (-(χ' x) / M) ^ 2 = χ x - (χ' x) ^ 2 / (2 * M) := by field_simp [ne_of_gt hMpos]; ring rw [halg] at hspec have : (χ' x) ^ 2 / (2 * M) ≤ χ x := by linarith have hmul := (div_le_iff₀ (by linarith : 0 < 2 * M)).mp this linarith /-! ## 3. Mixed-history Gram identity and arbitrary-depth adapted martingale trees (`U1`, `U3`, `U4`) -/ /-- Exact mixed-history Gram identity (`eq:rev-history`, `U4`): for `Y = U - V`, `Y Yᵀ - U Uᵀ = V Vᵀ - U Vᵀ - V Uᵀ`. -/ theorem mixed_history_gram_identity (U V : Vec3) : outer (U - V) - outer U = outer V - outer₂ U V - outer₂ V U := by ext i j simp only [outer, outer₂, Matrix.sub_apply, Matrix.of_apply, Pi.sub_apply] ring /-- Arbitrary-depth adapted binary martingale tree (generalizing `CHECK_COMPENSATED_SOURCE.py` from finite numerical trees to ALL real adapted binary trees of arbitrary depth). At each internal node with state `z`, cutoff weight `a = √k` (`k = a²`), and symmetric step increment `±g` (so `E[z_child] = z`), we track exact terminal values, accumulated bracket `Q`, signed crossing `-∑ Φ Δk`, and compensated history `V = ∑ Z Δa`. -/ inductive MartingaleTree where | leaf (z : Vec3) (a : ℝ) | branch (z : Vec3) (a : ℝ) (g : Vec3) (left right : MartingaleTree) /-- Adaptedness predicate for a binary martingale tree: at every branch node `(z, a, g, L, R)`, the children start at `z + g` and `z - g`. -/ def MartingaleTree.rootZ : MartingaleTree → Vec3 | .leaf z _ => z | .branch z _ _ _ _ => z def MartingaleTree.rootA : MartingaleTree → ℝ | .leaf _ a => a | .branch _ a _ _ _ => a def MartingaleTree.Adapted : MartingaleTree → Prop | .leaf _ _ => True | .branch z _ g L R => L.rootZ = z + g ∧ R.rootZ = z - g ∧ L.Adapted ∧ R.Adapted /-- Expected terminal weighted covariance `C(T) = E[a_end² Z_end Z_endᵀ]`. -/ def MartingaleTree.evalC : MartingaleTree → Mat3 | .leaf z a => (a ^ 2) • outer z | .branch _ _ _ L R => (1 / 2 : ℝ) • (L.evalC + R.evalC) /-- Expected weighted martingale bracket `Q(T) = E[∑ a_node² g_node g_nodeᵀ]`. -/ def MartingaleTree.evalQ : MartingaleTree → Mat3 | .leaf _ _ => 0 | .branch _ a g L R => (a ^ 2) • outer g + (1 / 2 : ℝ) • (L.evalQ + R.evalQ) /-- Expected signed crossing matrix `D_cross(T) = -E[∑ Φ_child (a_child² - a_parent²)]`. -/ def MartingaleTree.evalDcross : MartingaleTree → Mat3 | .leaf _ _ => 0 | .branch _ a _ L R => (1 / 2 : ℝ) • ((-(L.rootA ^ 2 - a ^ 2)) • outer L.rootZ + L.evalDcross + (-(R.rootA ^ 2 - a ^ 2)) • outer R.rootZ + R.evalDcross) /-- Exact compensated terminal expectation `E[(U_end - V_end)(U_end - V_end)ᵀ]` starting with accumulated prefix `V_pre` (`U_end = a_end Z_end`, `V_end = V_pre + ∑ Z_child (a_child - a_parent)`). -/ def MartingaleTree.evalYsq (V_pre : Vec3) : MartingaleTree → Mat3 | .leaf z a => outer (a • z - V_pre) | .branch _ a _ L R => let VL := V_pre + (L.rootA - a) • L.rootZ let VR := V_pre + (R.rootA - a) • R.rootZ (1 / 2 : ℝ) • (L.evalYsq VL + R.evalYsq VR) /-- Exact compensated terminal mean `E[U_end - V_end]` starting from prefix `V_pre`. -/ def MartingaleTree.evalYmean (V_pre : Vec3) : MartingaleTree → Vec3 | .leaf z a => a • z - V_pre | .branch _ a _ L R => let VL := V_pre + (L.rootA - a) • L.rootZ let VR := V_pre + (R.rootA - a) • R.rootZ (1 / 2 : ℝ) • (L.evalYmean VL + R.evalYmean VR) /-- Fundamental martingale identity on arbitrary adapted trees (`U3` / `prop:rev-comp`): `evalDcross T = a_root² outer(z_root) + evalQ T - evalC T`. When `a_root = 1` (`k_0 = 1`), this is `D_R = Ω + Q - C` (`U2` & `U3`). -/ theorem martingaleTree_U2_U3 (T : MartingaleTree) (hA : T.Adapted) : T.evalDcross = (T.rootA ^ 2) • outer T.rootZ + T.evalQ - T.evalC := by induction T with | leaf z a => simp [MartingaleTree.evalDcross, MartingaleTree.evalQ, MartingaleTree.evalC, MartingaleTree.rootA, MartingaleTree.rootZ] | branch z a g L R ihL ihR => rcases hA with ⟨hLz, hRz, hLA, hRA⟩ have hL := ihL hLA have hR := ihR hRA have ha0 : (MartingaleTree.branch z a g L R).rootA = a := rfl have hz0 : (MartingaleTree.branch z a g L R).rootZ = z := rfl rw [ha0, hz0] dsimp only [MartingaleTree.evalDcross, MartingaleTree.evalQ, MartingaleTree.evalC] rw [hL, hR, hLz, hRz] ext i j simp only [outer, Matrix.add_apply, Matrix.sub_apply, Matrix.smul_apply, Matrix.of_apply, Pi.add_apply, Pi.sub_apply, smul_eq_mul] ring /-- Mean-preservation lemma for the compensated martingale `Y = U - V` on arbitrary trees: `E[U_end - V_end] = a_root • z_root - V_pre`. -/ theorem martingaleTree_Ymean (T : MartingaleTree) (hA : T.Adapted) (V_pre : Vec3) : T.evalYmean V_pre = T.rootA • T.rootZ - V_pre := by induction T generalizing V_pre with | leaf z a => simp [MartingaleTree.evalYmean, MartingaleTree.rootA, MartingaleTree.rootZ] | branch z a g L R ihL ihR => rcases hA with ⟨hLz, hRz, hLA, hRA⟩ have ha0 : (MartingaleTree.branch z a g L R).rootA = a := rfl have hz0 : (MartingaleTree.branch z a g L R).rootZ = z := rfl rw [ha0, hz0] dsimp only [MartingaleTree.evalYmean] rw [ihL hLA, ihR hRA, hLz, hRz] funext i simp only [Pi.smul_apply, Pi.add_apply, Pi.sub_apply, smul_eq_mul] ring /-- Second-moment isometry for the compensated martingale `Y = U - V` on arbitrary trees (`U4`): `E[(U_end - V_end)(U_end - V_end)ᵀ] = outer (a_root • z_root - V_pre) + evalQ T`. -/ theorem martingaleTree_Ysq (T : MartingaleTree) (hA : T.Adapted) (V_pre : Vec3) : T.evalYsq V_pre = outer (T.rootA • T.rootZ - V_pre) + T.evalQ := by induction T generalizing V_pre with | leaf z a => simp [MartingaleTree.evalYsq, MartingaleTree.evalQ, MartingaleTree.rootA, MartingaleTree.rootZ] | branch z a g L R ihL ihR => rcases hA with ⟨hLz, hRz, hLA, hRA⟩ have ha0 : (MartingaleTree.branch z a g L R).rootA = a := rfl have hz0 : (MartingaleTree.branch z a g L R).rootZ = z := rfl rw [ha0, hz0] dsimp only [MartingaleTree.evalYsq, MartingaleTree.evalQ] rw [ihL hLA, ihR hRA, hLz, hRz] ext i j simp only [outer, Matrix.add_apply, Matrix.smul_apply, Matrix.of_apply, Pi.add_apply, Pi.sub_apply, Pi.smul_apply, smul_eq_mul] ring /-- Complete `U2` / `U3` / `U4` equivalence on arbitrary-depth adapted martingale trees with `a_root = 1`: `D_cross = Ω + Q - C = E[(U - V)(U - V)ᵀ] - C = E[V Vᵀ - U Vᵀ - V Uᵀ]`. -/ theorem martingaleTree_complete_U4 (T : MartingaleTree) (hA : T.Adapted) (hroot : T.rootA = 1) : T.evalDcross = outer T.rootZ + T.evalQ - T.evalC ∧ T.evalYsq 0 - T.evalC = T.evalDcross := by have h23 := martingaleTree_U2_U3 T hA have hY := martingaleTree_Ysq T hA 0 rw [hroot, one_pow, one_smul] at h23 rw [hroot, one_smul, sub_zero] at hY exact ⟨h23, by rw [hY, h23]⟩ /-! ## 4. Nine-product trace expansion (`U5`, `CP13`), deficit identity (`CP14`), and log cap (`R6`) -/ /-- Exact nine-product expansion of `tr(A²)` without matrix commutativity (`U5`, `CP13`): for `A = c I - N₀ - N₁ - N₂`, `tr(A²) = 3 c² - 2 c ∑_a tr(N_a) + ∑_{a,b} tr(N_a N_b)`. -/ theorem nine_products_trace_expansion (c : ℝ) (N₀ N₁ N₂ : Mat3) : let A : Mat3 := c • (1 : Mat3) - N₀ - N₁ - N₂ (A * A).trace = 3 * c ^ 2 - 2 * c * (N₀.trace + N₁.trace + N₂.trace) + ((N₀ * N₀).trace + (N₁ * N₁).trace + (N₂ * N₂).trace + 2 * (N₀ * N₁).trace + 2 * (N₀ * N₂).trace + 2 * (N₁ * N₂).trace) := by intro A have htr1 : (1 : Mat3).trace = 3 := by simp [Matrix.trace, Matrix.diag] have h10 : (N₁ * N₀).trace = (N₀ * N₁).trace := Matrix.trace_mul_comm N₁ N₀ have h20 : (N₂ * N₀).trace = (N₀ * N₂).trace := Matrix.trace_mul_comm N₂ N₀ have h21 : (N₂ * N₁).trace = (N₁ * N₂).trace := Matrix.trace_mul_comm N₂ N₁ have hexp : A * A = (c ^ 2) • (1 : Mat3) - (2 * c) • N₀ - (2 * c) • N₁ - (2 * c) • N₂ + N₀ * N₀ + N₀ * N₁ + N₀ * N₂ + N₁ * N₀ + N₁ * N₁ + N₁ * N₂ + N₂ * N₀ + N₂ * N₁ + N₂ * N₂ := by dsimp only [A] ext i j simp only [Matrix.mul_apply, Matrix.sub_apply, Matrix.add_apply, Matrix.smul_apply, Matrix.one_apply, smul_eq_mul, Fin.sum_univ_three] fin_cases i <;> fin_cases j <;> (simp; ring) rw [hexp] simp only [Matrix.trace_add, Matrix.trace_sub, Matrix.trace_smul, smul_eq_mul, htr1, h10, h20, h21] ring /-- Compensated `U5` expansion (`manuscript.tex` line 1558): for `A = K₀ I + Ω + Q - C`, without assuming any matrices commute. -/ theorem compensated_U5_trace_expansion (K₀ : ℝ) (Ω Q C : Mat3) : let A : Mat3 := K₀ • (1 : Mat3) + Ω + Q - C (A * A).trace = 3 * K₀ ^ 2 + 2 * K₀ * (Ω + Q - C).trace + (Ω * Ω).trace + (Q * Q).trace + (C * C).trace + 2 * (Ω * Q).trace - 2 * (Ω * C).trace - 2 * (Q * C).trace := by intro A have h := nine_products_trace_expansion K₀ (-Ω) (-Q) C have hAeq : A = K₀ • (1 : Mat3) - (-Ω) - (-Q) - C := by dsimp only [A]; abel rw [hAeq, h] simp only [Matrix.trace_add, Matrix.trace_sub, Matrix.trace_neg, Matrix.neg_mul, Matrix.mul_neg, neg_neg] ring /-- Noncommuting quadratic deficit identity (`CP14`, `prop:rev-abc` line 1638): for `U = A + L`, `tr(U²) - tr(A²) = tr(L (U + A))`, and when `A, L ⪰ 0`, `tr(A²) ≤ tr(U²)`. -/ theorem trace_sq_deficit_identity (A L : Mat3) : let U := A + L (U * U).trace - (A * A).trace = (L * (U + A)).trace := by intro U dsimp only [U] rw [Matrix.add_mul, Matrix.mul_add, Matrix.mul_add, Matrix.mul_add, Matrix.mul_add, Matrix.trace_add, Matrix.trace_add, Matrix.trace_add, Matrix.trace_add, Matrix.trace_add, Matrix.trace_mul_comm A L] ring theorem trace_sq_le_of_loewner (A L : Mat3) (hA : IsPSD A) (hL : IsPSD L) : (A * A).trace ≤ ((A + L) * (A + L)).trace := by have hdef := trace_sq_deficit_identity A L have hUA : IsPSD ((A + L) + A) := isPSD_add (isPSD_add hA hL) hA have hnn : 0 ≤ (L * ((A + L) + A)).trace := trace_mul_nonneg_of_isPSD hL hUA linarith /-- Elementary log-weighted cap (`boundedness-RESULT.txt` R6a--R6b): for all `k > 0`, `k * (-Real.log k) ≤ Real.exp (-1)`, with equality at `k = e⁻¹`. -/ theorem log_weighted_cap {k : ℝ} (hk : 0 < k) : k * (-Real.log k) ≤ Real.exp (-1) := by have hexp : (-1 - Real.log k) + 1 ≤ Real.exp (-1 - Real.log k) := Real.add_one_le_exp (-1 - Real.log k) have hrew : Real.exp (-1 - Real.log k) = Real.exp (-1) / k := by rw [Real.exp_sub, Real.exp_log hk] rw [hrew] at hexp have hle : -Real.log k ≤ Real.exp (-1) / k := by linarith have hmul := mul_le_mul_of_nonneg_left hle (le_of_lt hk) rwa [mul_div_cancel₀ _ (ne_of_gt hk)] at hmul /-! ## 5. Physical inverse second-derivative jet & basis-free spectral contact operator (`CP3`--`CP6`, `U8`--`U10`) -/ /-- Exact first- and second-derivative jet identities for the matrix inverse `J = M⁻¹` in arbitrary finite dimension `n` (`eq:rev-inverse`, `CP3`, `U8`): differentiating `M J = I` once gives `dJ = -J dM J`; differentiating twice gives `d²J = -J d²M J + 2 J dM J dM J`; assembling `L_phys = ∂_t + u·∇ - ν ∑_j ∂_j²` gives `L_phys J = -J T J - 2ν ∑_j J X_j J X_j J`. -/ theorem matrix_inverse_first_jet {n : Type*} [Fintype n] [DecidableEq n] {M J dM dJ : Matrix n n ℝ} (hJM : J * M = 1) (h1 : dM * J + M * dJ = 0) : dJ = -(J * dM * J) := by have h := congrArg (fun X => J * X) h1 simp only [Matrix.mul_add, Matrix.mul_zero, ← Matrix.mul_assoc, hJM, Matrix.one_mul] at h exact eq_neg_of_add_eq_zero_right h theorem matrix_inverse_second_jet {n : Type*} [Fintype n] [DecidableEq n] {M J dM dJ d2M d2J : Matrix n n ℝ} (hJM : J * M = 1) (h1 : dM * J + M * dJ = 0) (h2 : d2M * J + (2 : ℝ) • (dM * dJ) + M * d2J = 0) : d2J = -(J * d2M * J) + (2 : ℝ) • (J * dM * J * dM * J) := by have hdJ : dJ = -(J * dM * J) := matrix_inverse_first_jet hJM h1 have h := congrArg (fun X => J * X) h2 rw [hdJ] at h simp only [Matrix.mul_add, Matrix.mul_smul, Matrix.mul_neg, Matrix.mul_zero, ← Matrix.mul_assoc, hJM, Matrix.one_mul] at h have hstep : J * d2M * J - (2 : ℝ) • (J * dM * J * dM * J) + d2J = 0 := by simpa [sub_eq_add_neg, smul_neg] using h have hsol : d2J = -(J * d2M * J - (2 : ℝ) • (J * dM * J * dM * J)) := eq_neg_of_add_eq_zero_right hstep rw [hsol] abel /-- Full parabolic operator on `J = M⁻¹` (`eq:rev-inverse`, `CP3`, `U8`): given drift jet `T_M = (∂_t + u·∇)M`, spatial gradients `X_j = ∂_j M`, and second derivatives `H_j = ∂_j² M` with `T = T_M - ν ∑_j H_j`, we obtain `L_phys J = -J T J - 2ν ∑_j J X_j J X_j J`. -/ theorem matrix_inverse_parabolic_law {n : Type*} [Fintype n] [DecidableEq n] {ν : ℝ} {M J T_M T_J : Matrix n n ℝ} {X dJ H d2J : Fin 3 → Matrix n n ℝ} (hJM : J * M = 1) (hdrift : T_M * J + M * T_J = 0) (hfirst : ∀ j : Fin 3, X j * J + M * dJ j = 0) (hsecond : ∀ j : Fin 3, H j * J + (2 : ℝ) • (X j * dJ j) + M * d2J j = 0) : let T := T_M - ν • ∑ j : Fin 3, H j T_J - ν • ∑ j : Fin 3, d2J j = -(J * T * J) - (2 * ν) • ∑ j : Fin 3, J * X j * J * X j * J := by intro T have hTJ : T_J = -(J * T_M * J) := matrix_inverse_first_jet hJM hdrift have hd2J : ∀ j : Fin 3, d2J j = -(J * H j * J) + (2 : ℝ) • (J * X j * J * X j * J) := fun j => matrix_inverse_second_jet hJM (hfirst j) (hsecond j) rw [hTJ] simp_rw [hd2J, Finset.sum_add_distrib, Finset.sum_neg_distrib, ← Finset.smul_sum] dsimp only [T] rw [Matrix.mul_sub, Matrix.sub_mul, Matrix.mul_smul, Matrix.smul_mul, Finset.mul_sum, Finset.sum_mul] ext i k simp only [Matrix.sub_apply, Matrix.add_apply, Matrix.neg_apply, Matrix.smul_apply, smul_eq_mul] ring /-- Scalar reciprocal spectral cancellation (`CP4`, `manuscript.tex` line 1581): for `m > 0`, `m_k > 0`, `m ≠ m_k`, `μ = 1/m`, `μ_k = 1/m_k`, `-μ_k + μ_k² / (μ_k - μ) = 1 / (m - m_k)`. -/ theorem spectral_reciprocal_cancellation (m m_k : ℝ) (hm : m ≠ 0) (hmk : m_k ≠ 0) (hne : m ≠ m_k) : let μ := m⁻¹ let μ_k := m_k⁻¹ (-μ_k) + μ_k ^ 2 / (μ_k - μ) = 1 / (m - m_k) := by intro μ μ_k have hdiff : m - m_k ≠ 0 := sub_ne_zero.mpr hne have hμdiff : μ_k - μ ≠ 0 := by dsimp [μ, μ_k] intro h have : m_k⁻¹ = m⁻¹ := sub_eq_zero.mp h exact hne (inv_injective this).symm dsimp [μ, μ_k] field_simp [hm, hmk, hdiff, hμdiff] ring /-- Basis-free reduced-resolvent equivalence (`CP6`, `U9`): for invertible `M` with `J = M⁻¹`, top eigenvalue `m ≠ 0` (`μ = m⁻¹`), spectral projector `P` (`P² = P`, `P M = M P = m P`, `P J = J P = μ P`), and complementary reduced resolvents `G_⊥` (`(m I - M) G_⊥ = I - P`, `G_⊥ (m I - M) = I - P`, `P G_⊥ = G_⊥ P = 0`) and `G_J⊥` (`(J - μ I) G_J⊥ = I - P`, `P G_J⊥ = G_J⊥ P = 0`), we have the EXACT basis-free matrix identity `J G_J⊥ J - (1 - P) J = G_⊥`! -/ theorem reduced_resolvent_inverse_identity {n : Type*} [Fintype n] [DecidableEq n] {M J P G_perp G_Jperp : Matrix n n ℝ} {m : ℝ} (hm : m ≠ 0) (hMJ : M * J = 1) (hP2 : P * P = P) (hPJ : P * J = m⁻¹ • P) (hJP : J * P = m⁻¹ • P) (hG_left : G_perp * (m • (1 : Matrix n n ℝ) - M) = 1 - P) (hGP : G_perp * P = 0) (hGJ_right : (J - m⁻¹ • (1 : Matrix n n ℝ)) * G_Jperp = 1 - P) (hPGJ : P * G_Jperp = 0) : J * G_Jperp * J - (1 - P) * J = G_perp := by have hkey : (m • (1 : Matrix n n ℝ) - M) * (J * G_Jperp * J - (1 - P) * J) = 1 - P := by have h1 : (m • (1 : Matrix n n ℝ) - M) * J = m • (J - m⁻¹ • (1 : Matrix n n ℝ)) := by rw [Matrix.sub_mul, Matrix.smul_mul, Matrix.one_mul, hMJ, smul_sub, smul_smul, mul_inv_cancel₀ hm, one_smul] have h2 : (m • (1 : Matrix n n ℝ) - M) * (J * G_Jperp * J) = m • ((1 - P) * J) := by calc (m • (1 : Matrix n n ℝ) - M) * (J * G_Jperp * J) = ((m • (1 : Matrix n n ℝ) - M) * J) * G_Jperp * J := by simp only [Matrix.mul_assoc] _ = m • ((J - m⁻¹ • (1 : Matrix n n ℝ)) * G_Jperp) * J := by rw [h1, Matrix.smul_mul] _ = m • ((1 - P) * J) := by rw [hGJ_right, Matrix.smul_mul] have h3 : (m • (1 : Matrix n n ℝ) - M) * ((1 - P) * J) = m • ((1 - P) * J) - (1 - P) := by have hPcomm : (m • (1 : Matrix n n ℝ) - M) * P = 0 := by rw [Matrix.sub_mul, Matrix.smul_mul, Matrix.one_mul] have hMP : M * P = m • P := by calc M * P = M * ((m * m⁻¹) • P) := by rw [mul_inv_cancel₀ hm, one_smul] _ = m • (M * (m⁻¹ • P)) := by rw [← smul_smul, Matrix.mul_smul] _ = m • (M * (J * P)) := by rw [hJP] _ = m • ((M * J) * P) := by rw [Matrix.mul_assoc] _ = m • P := by rw [hMJ, Matrix.one_mul] rw [hMP, sub_self] have hsub : (m • (1 : Matrix n n ℝ) - M) * (1 - P) = m • (1 : Matrix n n ℝ) - M := by rw [Matrix.mul_sub, Matrix.mul_one, hPcomm, sub_zero] calc (m • (1 : Matrix n n ℝ) - M) * ((1 - P) * J) = ((m • (1 : Matrix n n ℝ) - M) * (1 - P)) * J := by rw [Matrix.mul_assoc] _ = (m • (1 : Matrix n n ℝ) - M) * J := by rw [hsub] _ = m • J - 1 := by rw [Matrix.sub_mul, Matrix.smul_mul, Matrix.one_mul, hMJ] _ = m • ((1 - P) * J) - (1 - P) := by rw [Matrix.sub_mul, Matrix.one_mul, hPJ, smul_sub, smul_smul, mul_inv_cancel₀ hm, one_smul] abel rw [Matrix.mul_sub, h2, h3] abel have hproj_lhs : (1 - P) * (J * G_Jperp * J - (1 - P) * J) = J * G_Jperp * J - (1 - P) * J := by have hP1 : P * (J * G_Jperp * J) = 0 := by calc P * (J * G_Jperp * J) = (P * J) * G_Jperp * J := by simp only [Matrix.mul_assoc] _ = m⁻¹ • (P * G_Jperp) * J := by rw [hPJ, Matrix.smul_mul] _ = 0 := by rw [hPGJ, smul_zero, Matrix.zero_mul] have hP2' : P * ((1 - P) * J) = 0 := by calc P * ((1 - P) * J) = (P * (1 - P)) * J := by rw [Matrix.mul_assoc] _ = (P - P * P) * J := by rw [Matrix.mul_sub, Matrix.mul_one] _ = 0 := by rw [hP2, sub_self, Matrix.zero_mul] rw [Matrix.sub_mul, Matrix.one_mul, Matrix.mul_sub, hP1, hP2', sub_zero, sub_zero] calc J * G_Jperp * J - (1 - P) * J = (1 - P) * (J * G_Jperp * J - (1 - P) * J) := hproj_lhs.symm _ = (G_perp * (m • (1 : Matrix n n ℝ) - M)) * (J * G_Jperp * J - (1 - P) * J) := by rw [hG_left] _ = G_perp * ((m • (1 : Matrix n n ℝ) - M) * (J * G_Jperp * J - (1 - P) * J)) := by rw [Matrix.mul_assoc] _ = G_perp * (1 - P) := by rw [hkey] _ = G_perp := by rw [Matrix.mul_sub, Matrix.mul_one, hGP, sub_zero] /-- Exact repeated-eigenvalue inverse contact operator (`CP6`, `U9`, `manuscript.tex` lines 1586--1588): at any repeated-top-eigenvalue spatial contact where `P X_j P = 0` (`Y_j = -J X_j J`), the inverse contact operator `P [L_phys J + 2ν ∑_j Y_j G_J⊥ Y_j] P` equals `-μ² P [T - 2ν ∑_j X_j G_⊥ X_j] P` identically, with NO global eigenframe and NO division by any vanishing top gap! -/ theorem repeated_eigenvalue_contact_operator {n : Type*} [Fintype n] [DecidableEq n] {ν m : ℝ} (hm : m ≠ 0) {M J P G_perp G_Jperp T : Matrix n n ℝ} {X : Fin 3 → Matrix n n ℝ} (hMJ : M * J = 1) (hP2 : P * P = P) (hPJ : P * J = m⁻¹ • P) (hJP : J * P = m⁻¹ • P) (hG_left : G_perp * (m • (1 : Matrix n n ℝ) - M) = 1 - P) (hGP : G_perp * P = 0) (hGJ_right : (J - m⁻¹ • (1 : Matrix n n ℝ)) * G_Jperp = 1 - P) (hPGJ : P * G_Jperp = 0) (hcontact : ∀ j : Fin 3, P * X j * P = 0) : let μ := m⁻¹ let Y := fun j => -(J * X j * J) let L_J := -(J * T * J) - (2 * ν) • ∑ j : Fin 3, J * X j * J * X j * J P * (L_J + (2 * ν) • ∑ j : Fin 3, Y j * G_Jperp * Y j) * P = -(μ ^ 2) • (P * (T - (2 * ν) • ∑ j : Fin 3, X j * G_perp * X j) * P) := by intro μ Y L_J have hres : J * G_Jperp * J - (1 - P) * J = G_perp := reduced_resolvent_inverse_identity hm hMJ hP2 hPJ hJP hG_left hGP hGJ_right hPGJ have hterm : ∀ j : Fin 3, P * (-(J * X j * J * X j * J) + Y j * G_Jperp * Y j) * P = (μ ^ 2) • (P * (X j * G_perp * X j) * P) := by intro j have hPXQ : P * X j * (1 - P) = P * X j := by rw [Matrix.mul_sub, Matrix.mul_one, hcontact j, sub_zero] have h1 : P * (J * X j * J * X j * J) * P = (μ ^ 2) • (P * X j * ((1 - P) * J) * X j * P) := by calc P * (J * X j * J * X j * J) * P = (P * J) * X j * J * X j * (J * P) := by simp only [Matrix.mul_assoc] _ = (μ • P) * X j * J * X j * (μ • P) := by rw [hPJ, hJP] _ = (μ ^ 2) • (P * X j * J * X j * P) := by simp only [Matrix.smul_mul, Matrix.mul_smul, smul_smul, pow_two] _ = (μ ^ 2) • ((P * X j * (1 - P)) * J * X j * P) := by rw [hPXQ] _ = (μ ^ 2) • (P * X j * ((1 - P) * J) * X j * P) := by simp only [Matrix.mul_assoc] have h2 : P * (Y j * G_Jperp * Y j) * P = (μ ^ 2) • (P * X j * (J * G_Jperp * J) * X j * P) := by dsimp only [Y] calc P * (-(J * X j * J) * G_Jperp * -(J * X j * J)) * P = (P * J) * X j * (J * G_Jperp * J) * X j * (J * P) := by simp only [Matrix.neg_mul, Matrix.mul_neg, neg_neg, Matrix.mul_assoc] _ = (μ • P) * X j * (J * G_Jperp * J) * X j * (μ • P) := by rw [hPJ, hJP] _ = (μ ^ 2) • (P * X j * (J * G_Jperp * J) * X j * P) := by simp only [Matrix.smul_mul, Matrix.mul_smul, smul_smul, pow_two] calc P * (-(J * X j * J * X j * J) + Y j * G_Jperp * Y j) * P = -(P * (J * X j * J * X j * J) * P) + P * (Y j * G_Jperp * Y j) * P := by rw [Matrix.mul_add, Matrix.add_mul, Matrix.mul_neg, Matrix.neg_mul] _ = (μ ^ 2) • (P * X j * (J * G_Jperp * J) * X j * P - P * X j * ((1 - P) * J) * X j * P) := by rw [h1, h2, smul_sub]; abel _ = (μ ^ 2) • (P * X j * (J * G_Jperp * J - (1 - P) * J) * X j * P) := by congr 1 simp only [Matrix.mul_sub, Matrix.sub_mul, Matrix.mul_assoc] _ = (μ ^ 2) • (P * (X j * G_perp * X j) * P) := by rw [hres]; simp only [Matrix.mul_assoc] have hTpart : P * (-(J * T * J)) * P = -(μ ^ 2) • (P * T * P) := by calc P * (-(J * T * J)) * P = -((P * J) * T * (J * P)) := by simp only [Matrix.mul_neg, Matrix.neg_mul, Matrix.mul_assoc] _ = -((μ • P) * T * (μ • P)) := by rw [hPJ, hJP] _ = -(μ ^ 2) • (P * T * P) := by simp only [Matrix.smul_mul, Matrix.mul_smul, smul_smul, pow_two, neg_smul] -- Assemble LHS and RHS dsimp only [L_J] have hcomb : (-(J * T * J) - (2 * ν) • ∑ j : Fin 3, J * X j * J * X j * J) + (2 * ν) • ∑ j : Fin 3, Y j * G_Jperp * Y j = -(J * T * J) + (2 * ν) • ∑ j : Fin 3, (-(J * X j * J * X j * J) + Y j * G_Jperp * Y j) := by rw [Finset.sum_add_distrib, Finset.sum_neg_distrib, smul_add, smul_neg] abel rw [hcomb, Matrix.mul_add, Matrix.add_mul, hTpart, Matrix.mul_smul, Matrix.smul_mul, Finset.mul_sum, Finset.sum_mul] simp_rw [hterm, ← Finset.smul_sum] rw [Matrix.mul_sub, Matrix.sub_mul, Matrix.mul_smul, Matrix.smul_mul, Finset.mul_sum, Finset.sum_mul, smul_sub] ext i k simp only [Matrix.add_apply, Matrix.sub_apply, Matrix.smul_apply, smul_eq_mul] ring /-! ## 6. ESM3 Exact Algebra: Both-Outcome Return, Effective Score, Schur Square, and UP17 -/ /-- Both-outcome return matrix identity (`check_joint_cancellation.py` line 108) and law of total covariance (`line 111`): for return probability `p`, nonreturn `q = 1 - p`, conditional means `μ_c, μ_u`, total mean `μ = p μ_c + q μ_u`, and conditional covariances `Cov_c = S_c - outer μ_c`, `Cov_u = S_u - outer μ_u`: 1. `outer μ - p • S_c = q • outer μ_u - p • Cov_c - (p * q) • outer (μ_c - μ_u)`. 2. `(p • S_c + q • S_u) - outer μ = p • Cov_c + q • Cov_u + (p * q) • outer (μ_c - μ_u)`. -/ theorem both_outcome_return_identity (p : ℝ) (μ_c μ_u : Vec3) (S_c S_u : Mat3) : let q := 1 - p let μ := p • μ_c + q • μ_u let Cov_c := S_c - outer μ_c let Cov_u := S_u - outer μ_u (outer μ - p • S_c = q • outer μ_u - p • Cov_c - (p * q) • outer (μ_c - μ_u)) ∧ ((p • S_c + q • S_u) - outer μ = p • Cov_c + q • Cov_u + (p * q) • outer (μ_c - μ_u)) := by intro q μ Cov_c Cov_u constructor <;> · ext i j simp only [q, μ, Cov_c, Cov_u, outer, Matrix.sub_apply, Matrix.add_apply, Matrix.smul_apply, Matrix.of_apply, Pi.add_apply, Pi.sub_apply, Pi.smul_apply, smul_eq_mul] ring /-- Effective-score cancellation and full-order `H`-congruence (`check_joint_cancellation.py` lines 141--146, `UP17`): for `G = c H K`, `Z = H w`, `r² = p` (`r ≠ 0`), `W(γ) = r G + r⁻¹ outer₂ Z γ`, `J(γ) = (|γ|² / p) outer Z`, `N(γ) = W(γ) W(γ)ᵀ`: `J(γ) - N(γ) = - H (c² p K Kᵀ + c (outer₂ (K γ) w + outer₂ w (K γ))) Hᵀ`! Consequently any component `γ_⊥ ∈ ker(K)` cancels identically between `J(γ)` and `N(γ)`! -/ theorem up17_effective_score_and_factorization (H K : Mat3) (w γ : Vec3) (r c : ℝ) (hr : r ≠ 0) : let p := r ^ 2 let G := c • (H * K) let Z := H *ᵥ w let W := r • G + r⁻¹ • outer₂ Z γ let J := (vecSqNorm γ / p) • outer Z let N := W * Wᵀ let b₀ := K *ᵥ γ let C := (c ^ 2 * p) • (K * Kᵀ) + c • (outer₂ b₀ w + outer₂ w b₀) J - N = -(H * C * Hᵀ) := by intro p G Z W J N b₀ C have h1 : outer₂ Z γ * (outer₂ Z γ)ᵀ = vecSqNorm γ • outer Z := by ext i j simp only [outer₂, outer, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.smul_apply, smul_eq_mul, vecSqNorm, Fin.sum_univ_three] ring have h2 : G * (outer₂ Z γ)ᵀ = outer₂ (G *ᵥ γ) Z := by ext i j simp only [outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have h3 : outer₂ Z γ * Gᵀ = outer₂ Z (G *ᵥ γ) := by ext i j simp only [outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have h4 : G *ᵥ γ = c • (H *ᵥ b₀) := by simp only [G, b₀, Matrix.smul_mulVec, Matrix.mulVec_mulVec] have h5 : H * outer₂ b₀ w * Hᵀ = outer₂ (H *ᵥ b₀) Z := by ext i j simp only [Z, outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have h6 : H * outer₂ w b₀ * Hᵀ = outer₂ Z (H *ᵥ b₀) := by ext i j simp only [Z, outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have hN : N = p • (G * Gᵀ) + outer₂ (G *ᵥ γ) Z + outer₂ Z (G *ᵥ γ) + (vecSqNorm γ / p) • outer Z := by have hmul : ∀ A B : Mat3, (r • A + r⁻¹ • B) * (r • A + r⁻¹ • B)ᵀ = (r * r) • (A * Aᵀ) + (r * r⁻¹) • (A * Bᵀ) + (r⁻¹ * r) • (B * Aᵀ) + (r⁻¹ * r⁻¹) • (B * Bᵀ) := fun A B => by rw [Matrix.transpose_add, Matrix.transpose_smul, Matrix.transpose_smul, Matrix.add_mul, Matrix.mul_add, Matrix.mul_add, Matrix.smul_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_smul] simp only [smul_smul] abel have hrr : r * r = p := by dsimp [p]; ring have hrinv : r * r⁻¹ = 1 := mul_inv_cancel₀ hr have hrinv' : r⁻¹ * r = 1 := inv_mul_cancel₀ hr have hrinv2 : r⁻¹ * r⁻¹ * vecSqNorm γ = vecSqNorm γ / p := by dsimp [p]; field_simp [hr] change (r • G + r⁻¹ • outer₂ Z γ) * (r • G + r⁻¹ • outer₂ Z γ)ᵀ = _ rw [hmul G (outer₂ Z γ), h1, h2, h3, hrr, hrinv, hrinv', one_smul, one_smul, smul_smul, hrinv2] have hGGt : G * Gᵀ = (c ^ 2) • (H * (K * Kᵀ) * Hᵀ) := by dsimp only [G] rw [Matrix.transpose_smul, Matrix.transpose_mul, Matrix.smul_mul, Matrix.mul_smul, smul_smul] simp only [pow_two, Matrix.mul_assoc] have hHCH : H * C * Hᵀ = (c ^ 2 * p) • (H * (K * Kᵀ) * Hᵀ) + c • outer₂ (H *ᵥ b₀) Z + c • outer₂ Z (H *ᵥ b₀) := by dsimp only [C] rw [Matrix.mul_add, Matrix.add_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_add, Matrix.add_mul, h5, h6, smul_add, add_assoc] rw [hN, hHCH, hGGt, h4] have hsmul1 : outer₂ (c • (H *ᵥ b₀)) Z = c • outer₂ (H *ᵥ b₀) Z := by ext i j; simp [outer₂]; ring have hsmul2 : outer₂ Z (c • (H *ᵥ b₀)) = c • outer₂ Z (H *ᵥ b₀) := by ext i j; simp [outer₂]; ring rw [hsmul1, hsmul2, smul_smul] dsimp only [J] rw [mul_comm p (c ^ 2)] abel /-- Source/noise link covariance of the `UP17` source matrix (`check_source_coercivity.py` lines 152--154): under orthogonal source link `Q` (`Qᵀ Q = Q Qᵀ = 1`) and noise link `V` (`Vᵀ V = V Vᵀ = 1`), `K_L = Q K Vᵀ`, `w_L = Q w`, `γ_L = V γ`, `H_L = H Qᵀ`, we have `C_L = Q C Qᵀ` and `H_L C_L H_Lᵀ = H C Hᵀ` identically! -/ theorem up17_link_covariance (H K Q V : Mat3) (w γ : Vec3) (p c : ℝ) (hQorth : Qᵀ * Q = 1) (hVorth : Vᵀ * V = 1) : let b₀ := K *ᵥ γ let C := (c ^ 2 * p) • (K * Kᵀ) + c • (outer₂ b₀ w + outer₂ w b₀) let K_L := Q * K * Vᵀ let w_L := Q *ᵥ w let γ_L := V *ᵥ γ let b_L := K_L *ᵥ γ_L let C_L := (c ^ 2 * p) • (K_L * K_Lᵀ) + c • (outer₂ b_L w_L + outer₂ w_L b_L) let H_L := H * Qᵀ C_L = Q * C * Qᵀ ∧ H_L * C_L * H_Lᵀ = H * C * Hᵀ := by intro b₀ C K_L w_L γ_L b_L C_L H_L have hbL : b_L = Q *ᵥ b₀ := by dsimp only [b_L, K_L, γ_L, b₀] rw [Matrix.mulVec_mulVec, Matrix.mul_assoc, Matrix.mul_assoc, hVorth, Matrix.mul_one, ← Matrix.mulVec_mulVec] have hKK : K_L * K_Lᵀ = Q * (K * Kᵀ) * Qᵀ := by dsimp only [K_L] rw [Matrix.transpose_mul, Matrix.transpose_mul, Matrix.transpose_transpose] calc Q * K * Vᵀ * (V * (Kᵀ * Qᵀ)) = Q * K * (Vᵀ * V) * Kᵀ * Qᵀ := by simp only [Matrix.mul_assoc] _ = Q * (K * Kᵀ) * Qᵀ := by rw [hVorth, Matrix.mul_one]; simp only [Matrix.mul_assoc] have ho1 : outer₂ (Q *ᵥ b₀) (Q *ᵥ w) = Q * outer₂ b₀ w * Qᵀ := by ext i j simp only [outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have ho2 : outer₂ (Q *ᵥ w) (Q *ᵥ b₀) = Q * outer₂ w b₀ * Qᵀ := by ext i j simp only [outer₂, Matrix.mul_apply, Matrix.transpose_apply, Matrix.of_apply, Matrix.mulVec, dotProduct, Fin.sum_univ_three] ring have hCL : C_L = Q * C * Qᵀ := by dsimp only [C_L, C, w_L] rw [hbL, hKK, ho1, ho2, Matrix.mul_add, Matrix.add_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_smul, Matrix.smul_mul, Matrix.mul_add, Matrix.add_mul, smul_add] refine ⟨hCL, ?_⟩ rw [hCL] dsimp only [H_L] rw [Matrix.transpose_mul, Matrix.transpose_transpose] calc H * Qᵀ * (Q * C * Qᵀ) * (Q * Hᵀ) = H * (Qᵀ * Q) * C * (Qᵀ * Q) * Hᵀ := by simp only [Matrix.mul_assoc] _ = H * C * Hᵀ := by rw [hQorth, Matrix.mul_one, Matrix.mul_one] /-- Rectangular Schur complete-square identity (`check_joint_cancellation.py` lines 164--165): for any `m × n` real matrix `A`, `L = I_n - Aᵀ A`, `G = I_m - A Aᵀ`, with inverses `L_inv` (`L * L_inv = I_n`, `L_invᵀ = L_inv`) and `G_inv` (`G * G_inv = I_m`) satisfying the push-through identity `I_m + A L_inv Aᵀ = G_inv`: `‖A y + e‖² - ‖y‖² = ⟨e, G_inv e⟩ - ⟨y - L_inv Aᵀ e, L (y - L_inv Aᵀ e)⟩`. When `L ⪰ 0`, this immediately gives `‖A y + e‖² - ‖y‖² ≤ ⟨e, G_inv e⟩`! -/ theorem schur_complete_square {m n : Type*} [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n] (A : Matrix m n ℝ) (L_inv : Matrix n n ℝ) (G_inv : Matrix m m ℝ) (hLsym : L_invᵀ = L_inv) (hLLinv : (1 - Aᵀ * A) * L_inv = 1) (hPush : 1 + A * L_inv * Aᵀ = G_inv) (y : n → ℝ) (e : m → ℝ) : let L := (1 : Matrix n n ℝ) - Aᵀ * A let c := L_inv *ᵥ (Aᵀ *ᵥ e) let r := y - c (A *ᵥ y + e) ⬝ᵥ (A *ᵥ y + e) - y ⬝ᵥ y = e ⬝ᵥ (G_inv *ᵥ e) - r ⬝ᵥ (L *ᵥ r) := by intro L c r have hLinvL : L_inv * L = 1 := by have hL_sym : Lᵀ = L := by simp [L, Matrix.transpose_sub, Matrix.transpose_mul] calc L_inv * L = L_invᵀ * Lᵀ := by rw [hLsym, hL_sym] _ = (L * L_inv)ᵀ := (Matrix.transpose_mul L L_inv).symm _ = 1 := by rw [hLLinv, Matrix.transpose_one] have hLc : L *ᵥ c = Aᵀ *ᵥ e := by calc L *ᵥ (L_inv *ᵥ (Aᵀ *ᵥ e)) = (L * L_inv) *ᵥ (Aᵀ *ᵥ e) := Matrix.mulVec_mulVec (Aᵀ *ᵥ e) L L_inv _ = Aᵀ *ᵥ e := by rw [hLLinv, Matrix.one_mulVec] have hL_sym_dot : ∀ u v : n → ℝ, u ⬝ᵥ (L *ᵥ v) = v ⬝ᵥ (L *ᵥ u) := by intro u v have hL_sym : Lᵀ = L := by simp [L, Matrix.transpose_sub, Matrix.transpose_mul] calc u ⬝ᵥ (L *ᵥ v) = (u ᵥ* L) ⬝ᵥ v := Matrix.dotProduct_mulVec u L v _ = (Lᵀ *ᵥ u) ⬝ᵥ v := by rw [Matrix.mulVec_transpose] _ = v ⬝ᵥ (L *ᵥ u) := by rw [hL_sym, dotProduct_comm] have hAy_dot : (A *ᵥ y) ⬝ᵥ (A *ᵥ y) = y ⬝ᵥ ((Aᵀ * A) *ᵥ y) := by calc (A *ᵥ y) ⬝ᵥ (A *ᵥ y) = y ⬝ᵥ (Aᵀ *ᵥ (A *ᵥ y)) := by rw [dotProduct_comm, Matrix.dotProduct_mulVec, ← Matrix.mulVec_transpose, dotProduct_comm] _ = y ⬝ᵥ ((Aᵀ * A) *ᵥ y) := by rw [Matrix.mulVec_mulVec] have hAy_e : (A *ᵥ y) ⬝ᵥ e = y ⬝ᵥ (Aᵀ *ᵥ e) := by rw [dotProduct_comm, Matrix.dotProduct_mulVec, ← Matrix.mulVec_transpose, dotProduct_comm] have hrLr : r ⬝ᵥ (L *ᵥ r) = y ⬝ᵥ (L *ᵥ y) - 2 * (y ⬝ᵥ (Aᵀ *ᵥ e)) + c ⬝ᵥ (Aᵀ *ᵥ e) := by dsimp only [r] rw [Matrix.mulVec_sub, sub_dotProduct, dotProduct_sub, dotProduct_sub, hL_sym_dot c y, hLc] ring have hyLy : y ⬝ᵥ (L *ᵥ y) = y ⬝ᵥ y - (A *ᵥ y) ⬝ᵥ (A *ᵥ y) := by dsimp only [L] rw [Matrix.sub_mulVec, Matrix.one_mulVec, dotProduct_sub, hAy_dot] have heGe : e ⬝ᵥ (G_inv *ᵥ e) = e ⬝ᵥ e + c ⬝ᵥ (Aᵀ *ᵥ e) := by rw [← hPush, Matrix.add_mulVec, Matrix.one_mulVec, dotProduct_add] congr 1 calc e ⬝ᵥ ((A * L_inv * Aᵀ) *ᵥ e) = e ⬝ᵥ (A *ᵥ (L_inv *ᵥ (Aᵀ *ᵥ e))) := by simp only [Matrix.mulVec_mulVec, Matrix.mul_assoc] _ = (Aᵀ *ᵥ e) ⬝ᵥ c := by dsimp only [c] rw [Matrix.dotProduct_mulVec, ← Matrix.mulVec_transpose] _ = c ⬝ᵥ (Aᵀ *ᵥ e) := dotProduct_comm _ _ have hLHS : (A *ᵥ y + e) ⬝ᵥ (A *ᵥ y + e) - y ⬝ᵥ y = (A *ᵥ y) ⬝ᵥ (A *ᵥ y) + 2 * (y ⬝ᵥ (Aᵀ *ᵥ e)) + e ⬝ᵥ e - y ⬝ᵥ y := by rw [add_dotProduct, dotProduct_add, dotProduct_add, dotProduct_comm e (A *ᵥ y), hAy_e] ring linarith [hLHS, hrLr, hyLy, heGe] /-- Exact scalar Schur envelope optimum (`check_schur_envelope.py` lines 58--111): for `G ≥ 0`, `H ≥ 0`, `0 ≤ b < 1`, the minimum of `(G - c H)² / (1 - c²)` over `|c| ≤ b` is: - `0` if `G ≤ b H` (attained at `c = G / H`), - `G² - H²` if `H ≤ b G` (attained at `c = H / G`), - `(G - b H)² / (1 - b²)` otherwise (attained at `c = b`), and in ALL cases dominates `max (G² - H²) 0`! -/ theorem schur_scalar_lower_bound {G H c : ℝ} (hc : c ^ 2 < 1) : max (G ^ 2 - H ^ 2) 0 ≤ (G - c * H) ^ 2 / (1 - c ^ 2) := by have hden : 0 < 1 - c ^ 2 := sub_pos.mpr hc apply max_le · rw [le_div_iff₀ hden] have hid : (G - c * H) ^ 2 - (G ^ 2 - H ^ 2) * (1 - c ^ 2) = (c * G - H) ^ 2 := by ring nlinarith [sq_nonneg (c * G - H)] · exact div_nonneg (sq_nonneg _) (le_of_lt hden) theorem schur_scalar_fixed_margin_bound {G H b c : ℝ} (hG : 0 ≤ G) (hH : 0 ≤ H) (hb0 : 0 ≤ b) (hb1 : b < 1) (hGgt : b * H ≤ G) (hHgt : b * G ≤ H) (hc : |c| ≤ b) : (G - b * H) ^ 2 / (1 - b ^ 2) ≤ (G - c * H) ^ 2 / (1 - c ^ 2) := by have hcb : c ≤ b := (abs_le.mp hc).2 have hc2_le : c ^ 2 ≤ b ^ 2 := by rw [← sq_abs c] exact sq_le_sq' (by linarith [abs_nonneg c]) hc have hb2_lt : b ^ 2 < 1 := by nlinarith have hden_b : 0 < 1 - b ^ 2 := sub_pos.mpr hb2_lt have hden_c : 0 < 1 - c ^ 2 := by linarith rw [div_le_div_iff₀ hden_b hden_c] have hfac : (G - c * H) ^ 2 * (1 - b ^ 2) - (G - b * H) ^ 2 * (1 - c ^ 2) = (b - c) * ((G - c * H) * (H - b * G) + (G - b * H) * (H - c * G)) := by ring have h1 : 0 ≤ b - c := by linarith have h2 : 0 ≤ G - b * H := by linarith have h3 : 0 ≤ H - b * G := by linarith have h4 : 0 ≤ G - c * H := by nlinarith have h5 : 0 ≤ H - c * G := by nlinarith have hpos : 0 ≤ (b - c) * ((G - c * H) * (H - b * G) + (G - b * H) * (H - c * G)) := mul_nonneg h1 (add_nonneg (mul_nonneg h4 h3) (mul_nonneg h2 h5)) linarith [hfac, hpos] end end F02NavierStokes.CompensatedHistoryAndInverse