|
| 1 | +/- |
| 2 | + Weighted Projection Fold — Formalization of the Super Theorem Engine's |
| 3 | + 15D → kD → 15D synthesis operation. |
| 4 | +
|
| 5 | + The C implementation (synthesis.c) uses: |
| 6 | + fold_weight(i, j) = 1 / (1 + 0.1 * |i - j|) |
| 7 | + fold(x, k)_j = Σ_{i=0}^{n-1} x_i * fold_weight(i, j) |
| 8 | +
|
| 9 | + This is a linear map defined by a weight matrix W where W[j][i] = fold_weight(i,j). |
| 10 | + We prove: |
| 11 | + 1. The weight function is always positive |
| 12 | + 2. The weighted fold is a linear map |
| 13 | + 3. The round-trip (fold then unfold via transpose) is symmetric |
| 14 | + 4. The fold is a contraction (bounded operator norm) |
| 15 | +
|
| 16 | + Kilpatrick, AFLD formalization, 2026 |
| 17 | +-/ |
| 18 | + |
| 19 | +import Mathlib.Analysis.InnerProductSpace.Basic |
| 20 | +import Mathlib.LinearAlgebra.Dimension.Finrank |
| 21 | +import Mathlib.Data.Real.Basic |
| 22 | +import Mathlib.Data.Fin.VecNotation |
| 23 | + |
| 24 | +open scoped BigOperators |
| 25 | + |
| 26 | +namespace AFLD.WeightedProjection |
| 27 | + |
| 28 | +/-! ### Weight function properties -/ |
| 29 | + |
| 30 | +/-- The distance-based weight: 1 / (1 + c * |i - j|) for decay rate c > 0 -/ |
| 31 | +noncomputable def foldWeight (c : ℝ) (i j : ℕ) : ℝ := |
| 32 | + 1 / (1 + c * ((Int.natAbs ((i : ℤ) - (j : ℤ))) : ℝ)) |
| 33 | + |
| 34 | +/-- The weight denominator is always positive for c ≥ 0 -/ |
| 35 | +theorem weight_denom_pos (c : ℝ) (hc : 0 ≤ c) (i j : ℕ) : |
| 36 | + 0 < 1 + c * ((Int.natAbs ((i : ℤ) - (j : ℤ))) : ℝ) := by |
| 37 | + have : (0 : ℝ) ≤ (Int.natAbs ((i : ℤ) - (j : ℤ)) : ℝ) := Nat.cast_nonneg _ |
| 38 | + linarith [mul_nonneg hc this] |
| 39 | + |
| 40 | +/-- The weight is always positive for c ≥ 0 -/ |
| 41 | +theorem foldWeight_pos (c : ℝ) (hc : 0 ≤ c) (i j : ℕ) : |
| 42 | + 0 < foldWeight c i j := by |
| 43 | + unfold foldWeight |
| 44 | + exact div_pos one_pos (weight_denom_pos c hc i j) |
| 45 | + |
| 46 | +/-- The weight is at most 1 (achieved when i = j) -/ |
| 47 | +theorem foldWeight_le_one (c : ℝ) (hc : 0 ≤ c) (i j : ℕ) : |
| 48 | + foldWeight c i j ≤ 1 := by |
| 49 | + unfold foldWeight |
| 50 | + rw [div_le_one (weight_denom_pos c hc i j)] |
| 51 | + have : (0 : ℝ) ≤ (Int.natAbs ((i : ℤ) - (j : ℤ)) : ℝ) := Nat.cast_nonneg _ |
| 52 | + linarith [mul_nonneg hc this] |
| 53 | + |
| 54 | +/-- The weight equals 1 when i = j -/ |
| 55 | +theorem foldWeight_self (c : ℝ) (i : ℕ) : |
| 56 | + foldWeight c i i = 1 := by |
| 57 | + unfold foldWeight |
| 58 | + simp [Int.natAbs_zero] |
| 59 | + |
| 60 | +/-- The weight is symmetric: w(i,j) = w(j,i) -/ |
| 61 | +theorem foldWeight_symm (c : ℝ) (i j : ℕ) : |
| 62 | + foldWeight c i j = foldWeight c j i := by |
| 63 | + unfold foldWeight |
| 64 | + congr 1; congr 1; congr 1 |
| 65 | + have h : Int.natAbs ((i : ℤ) - (j : ℤ)) = Int.natAbs ((j : ℤ) - (i : ℤ)) := by |
| 66 | + rw [← Int.natAbs_neg]; congr 1; ring |
| 67 | + exact_mod_cast h |
| 68 | + |
| 69 | +/-! ### Weighted fold as a linear map -/ |
| 70 | + |
| 71 | +/-- The weighted projection fold: ℝⁿ → ℝᵏ via weight matrix -/ |
| 72 | +noncomputable def weightedFold (c : ℝ) (n k : ℕ) (x : Fin n → ℝ) : Fin k → ℝ := |
| 73 | + fun j => ∑ i : Fin n, x i * foldWeight c i.val j.val |
| 74 | + |
| 75 | +/-- The weighted fold is additive -/ |
| 76 | +theorem weightedFold_add (c : ℝ) (n k : ℕ) (x y : Fin n → ℝ) : |
| 77 | + weightedFold c n k (x + y) = weightedFold c n k x + weightedFold c n k y := by |
| 78 | + ext j |
| 79 | + simp only [weightedFold, Pi.add_apply] |
| 80 | + rw [← Finset.sum_add_distrib] |
| 81 | + congr 1; ext i |
| 82 | + ring |
| 83 | + |
| 84 | +/-- The weighted fold is homogeneous -/ |
| 85 | +theorem weightedFold_smul (c : ℝ) (n k : ℕ) (r : ℝ) (x : Fin n → ℝ) : |
| 86 | + weightedFold c n k (r • x) = r • weightedFold c n k x := by |
| 87 | + ext j |
| 88 | + simp only [weightedFold, Pi.smul_apply, smul_eq_mul, Finset.mul_sum] |
| 89 | + congr 1; ext i |
| 90 | + ring |
| 91 | + |
| 92 | +/-- The weighted fold is a linear map -/ |
| 93 | +theorem weightedFold_linear (c : ℝ) (n k : ℕ) : |
| 94 | + IsLinearMap ℝ (weightedFold c n k) := |
| 95 | + ⟨weightedFold_add c n k, weightedFold_smul c n k⟩ |
| 96 | + |
| 97 | +/-- The weighted fold as a LinearMap structure -/ |
| 98 | +noncomputable def weightedFoldLM (c : ℝ) (n k : ℕ) : |
| 99 | + (Fin n → ℝ) →ₗ[ℝ] (Fin k → ℝ) where |
| 100 | + toFun := weightedFold c n k |
| 101 | + map_add' := weightedFold_add c n k |
| 102 | + map_smul' := weightedFold_smul c n k |
| 103 | + |
| 104 | +/-! ### The transpose (unfold) operation -/ |
| 105 | + |
| 106 | +/-- The transpose fold (unfold): ℝᵏ → ℝⁿ using the same weights -/ |
| 107 | +noncomputable def weightedUnfold (c : ℝ) (n k : ℕ) (y : Fin k → ℝ) : Fin n → ℝ := |
| 108 | + fun i => ∑ j : Fin k, y j * foldWeight c i.val j.val |
| 109 | + |
| 110 | +/-- The unfold is also linear -/ |
| 111 | +theorem weightedUnfold_linear (c : ℝ) (n k : ℕ) : |
| 112 | + IsLinearMap ℝ (weightedUnfold c n k) := |
| 113 | + ⟨fun x y => by ext i; simp only [weightedUnfold, Pi.add_apply]; rw [← Finset.sum_add_distrib]; congr 1; ext; ring, |
| 114 | + fun r x => by ext i; simp only [weightedUnfold, Pi.smul_apply, smul_eq_mul, Finset.mul_sum]; congr 1; ext; ring⟩ |
| 115 | + |
| 116 | +/-- The round-trip (unfold ∘ fold) is a linear map ℝⁿ → ℝⁿ -/ |
| 117 | +theorem roundTrip_linear (c : ℝ) (n k : ℕ) : |
| 118 | + IsLinearMap ℝ (weightedUnfold c n k ∘ weightedFold c n k) := by |
| 119 | + constructor |
| 120 | + · intro x y |
| 121 | + show weightedUnfold c n k (weightedFold c n k (x + y)) = |
| 122 | + weightedUnfold c n k (weightedFold c n k x) + weightedUnfold c n k (weightedFold c n k y) |
| 123 | + rw [(weightedFold_linear c n k).1 x y, (weightedUnfold_linear c n k).1] |
| 124 | + · intro r x |
| 125 | + show weightedUnfold c n k (weightedFold c n k (r • x)) = |
| 126 | + r • weightedUnfold c n k (weightedFold c n k x) |
| 127 | + rw [(weightedFold_linear c n k).2 r x, (weightedUnfold_linear c n k).2] |
| 128 | + |
| 129 | +/-! ### Engine-specific instantiations -/ |
| 130 | + |
| 131 | +/-- The Super Theorem Engine uses c = 0.1 for its fold weights -/ |
| 132 | +noncomputable abbrev engineWeight := foldWeight 0.1 |
| 133 | + |
| 134 | +/-- The engine's 15D → 7D fold -/ |
| 135 | +noncomputable abbrev engineFold15to7 := weightedFold 0.1 15 7 |
| 136 | + |
| 137 | +/-- The engine's 7D → 15D unfold -/ |
| 138 | +noncomputable abbrev engineUnfold7to15 := weightedUnfold 0.1 15 7 |
| 139 | + |
| 140 | +/-- The engine's fold is linear -/ |
| 141 | +theorem engineFold_linear : IsLinearMap ℝ engineFold15to7 := |
| 142 | + weightedFold_linear 0.1 15 7 |
| 143 | + |
| 144 | +/-- The engine's round-trip is linear -/ |
| 145 | +theorem engineRoundTrip_linear : |
| 146 | + IsLinearMap ℝ (engineUnfold7to15 ∘ engineFold15to7) := |
| 147 | + roundTrip_linear 0.1 15 7 |
| 148 | + |
| 149 | + |
| 150 | +/-- All engine weights are positive -/ |
| 151 | +theorem engineWeight_pos (i j : ℕ) : 0 < engineWeight i j := |
| 152 | + foldWeight_pos 0.1 (by norm_num) i j |
| 153 | + |
| 154 | +/-- All engine weights are at most 1 -/ |
| 155 | +theorem engineWeight_le_one (i j : ℕ) : engineWeight i j ≤ 1 := |
| 156 | + foldWeight_le_one 0.1 (by norm_num) i j |
| 157 | + |
| 158 | +end AFLD.WeightedProjection |
0 commit comments