| | @@ -0,0 +1,270 @@ |
| 1 | +import ErdosSwarm.Erdos530BoxSpan |
| 2 | +import ErdosSwarm.Erdos530DigitLevel |
| 3 | +import Mathlib.Analysis.SpecificLimits.Normed |
| 4 | + |
| 5 | +/-! |
| 6 | +# Typical affine spans in the eight-digit box |
| 7 | + |
| 8 | +Consider an ordered tuple of `k` rows in `{0, …, 7}^r`. If its columns include |
| 9 | +both unit and sevenfold unit vectors in each row direction, those columns |
| 10 | +recover the coefficients of every affine combination. Over the prime field |
| 11 | +of order `p > max 49 k`, a combination lying in the digit box can only be |
| 12 | +one of the original rows. Counting tuples that miss a required column gives |
| 13 | +a uniform estimate for dense subsets and every maximizing squared-norm level. |
| 14 | +-/ |
| 15 | + |
| 16 | +namespace ErdosSwarm.Erdos530DigitTuple |
| 17 | + |
| 18 | +open Finset Filter |
| 19 | +open scoped Topology |
| 20 | + |
| 21 | +variable {k r p : ℕ} |
| 22 | + |
| 23 | +/-- A column with digit `d` in row `i` and zero in the other rows. -/ |
| 24 | +def axisWord (i : Fin k) (d : Fin 8) (j : Fin k) : Fin 8 := |
| 25 | + if j = i then d else 0 |
| 26 | + |
| 27 | +/-- Each row has a unit column and a sevenfold unit column. -/ |
| 28 | +def HasAxisColumns (a : Fin k → Fin r → Fin 8) : Prop := |
| 29 | + ∀ i, ∀ d ∈ ({1, 7} : Finset (Fin 8)), axisWord i d ∈ univ.image (Function.swap a) |
| 30 | + |
| 31 | +instance (a : Fin k → Fin r → Fin 8) : Decidable (HasAxisColumns a) := |
| 32 | + inferInstanceAs (Decidable (∀ i, ∀ d ∈ ({1, 7} : Finset (Fin 8)), |
| 33 | + axisWord i d ∈ univ.image (Function.swap a))) |
| 34 | + |
| 35 | +/-- The vector over the prime field represented by one row of digits. -/ |
| 36 | +def modRow (p : ℕ) (row : Fin r → Fin 8) (c : Fin r) : ZMod p := |
| 37 | + (row c).val |
| 38 | + |
| 39 | +private theorem sum_at_axis {a : Fin k → Fin r → Fin 8} {i : Fin k} {d : Fin 8} |
| 40 | + {c : Fin r} (hc : Function.swap a c = axisWord i d) (s : Finset (Fin k)) |
| 41 | + (hi : i ∈ s) (w : Fin k → ZMod p) : |
| 42 | + (∑ j ∈ s, w j • modRow p (a j)) c = w i * (d.val : ZMod p) := by |
| 43 | + rw [Finset.sum_apply, sum_eq_single_of_mem i hi] |
| 44 | + · have h : a i c = d := by simpa [Function.swap, axisWord] using congrFun hc i |
| 45 | + simp [modRow, h] |
| 46 | + · intro j _ hji |
| 47 | + have h : a j c = 0 := by simpa [Function.swap, axisWord, hji] using congrFun hc j |
| 48 | + simp [modRow, h] |
| 49 | + |
| 50 | +/-- The unit columns already witness linear independence, hence affine |
| 51 | +independence, after reduction to the prime field. -/ |
| 52 | +theorem affineIndependent_of_hasAxisColumns [Fact p.Prime] {a : Fin k → Fin r → Fin 8} |
| 53 | + (ha : HasAxisColumns a) : AffineIndependent (ZMod p) (fun i => modRow p (a i)) := by |
| 54 | + apply LinearIndependent.affineIndependent |
| 55 | + apply Fintype.linearIndependent_iff.mpr |
| 56 | + intro w hw i |
| 57 | + obtain ⟨c, _, hc⟩ := mem_image.mp (ha i 1 (by simp)) |
| 58 | + have he := sum_at_axis hc univ (mem_univ i) w |
| 59 | + rw [hw] at he |
| 60 | + simpa using he.symm |
| 61 | + |
| 62 | +private theorem weight_eq_zero_or_one [Fact p.Prime] (hp : 49 < p) {w : ZMod p} |
| 63 | + (hsmall : w.val < 8) (hseven : (w * 7).val < 8) : w = 0 ∨ w = 1 := by |
| 64 | + have h7 : (7 : ZMod p).val = 7 := ZMod.val_natCast_of_lt (by omega : 7 < p) |
| 65 | + rw [ZMod.val_mul_of_lt (by rw [h7]; omega), h7] at hseven |
| 66 | + have hw : w.val = 0 ∨ w.val = 1 := by omega |
| 67 | + rw [← ZMod.natCast_zmod_val w] |
| 68 | + rcases hw with h | h <;> simp [h] |
| 69 | + |
| 70 | +/-- At a prime above both 49 and the number of rows, a modular digit vector |
| 71 | +belongs to the affine span of a tuple with the specified columns exactly |
| 72 | +when it is one of its rows. The box point may come from outside the sample. -/ |
| 73 | +theorem mem_affineSpan_iff_of_hasAxisColumns [Fact p.Prime] (hp : 49 < p) (hk : k < p) |
| 74 | + {a : Fin k → Fin r → Fin 8} (ha : HasAxisColumns a) {x : Fin r → ZMod p} |
| 75 | + (hx : ∀ c, (x c).val < 8) : |
| 76 | + x ∈ affineSpan (ZMod p) (Set.range fun i => modRow p (a i)) ↔ |
| 77 | + x ∈ Set.range fun i => modRow p (a i) := by |
| 78 | + refine ⟨?_, fun h => subset_affineSpan _ _ h⟩ |
| 79 | + intro hspan |
| 80 | + obtain ⟨s, w, hw, hcomb⟩ := eq_affineCombination_of_mem_affineSpan hspan |
| 81 | + rw [s.affineCombination_eq_linear_combination _ _ hw] at hcomb |
| 82 | + have hweight (i) (hi : i ∈ s) : w i = 0 ∨ w i = 1 := by |
| 83 | + have hcoord (d : Fin 8) (hd : d ∈ ({1, 7} : Finset (Fin 8))) : |
| 84 | + (w i * (d.val : ZMod p)).val < 8 := by |
| 85 | + obtain ⟨c, _, hc⟩ := mem_image.mp (ha i d hd) |
| 86 | + rw [← sum_at_axis hc s hi w, ← hcomb] |
| 87 | + exact hx c |
| 88 | + apply weight_eq_zero_or_one hp |
| 89 | + · simpa using hcoord 1 (by simp) |
| 90 | + · exact hcoord 7 (by simp) |
| 91 | + let ones := s.filter fun i => w i = 1 |
| 92 | + have hcard : (ones.card : ZMod p) = 1 := by |
| 93 | + calc (ones.card : ZMod p) = ∑ i ∈ s, if w i = 1 then 1 else 0 := by simp [ones] |
| 94 | + _ = ∑ i ∈ s, w i := sum_congr rfl fun i hi => by rcases hweight i hi with h | h <;> simp [h] |
| 95 | + _ = 1 := hw |
| 96 | + have hlt : ones.card < p := lt_of_le_of_lt (by simpa using card_le_univ ones) hk |
| 97 | + have hone : ones.card = 1 := by |
| 98 | + simpa [ZMod.val_natCast_of_lt hlt, ZMod.val_one'' (by omega : p ≠ 1)] using |
| 99 | + congrArg ZMod.val hcard |
| 100 | + obtain ⟨i, hi⟩ := card_eq_one.mp hone |
| 101 | + have himem : i ∈ ones := hi ▸ mem_singleton_self i |
| 102 | + obtain ⟨his, hwi⟩ := mem_filter.mp himem |
| 103 | + refine ⟨i, ?_⟩ |
| 104 | + rw [hcomb, sum_eq_single_of_mem i his, hwi, one_smul] |
| 105 | + intro j hj hji |
| 106 | + have hjout : j ∉ ones := by simp [hi, hji] |
| 107 | + have hwj : w j = 0 := (hweight j hj).resolve_right fun h => hjout (mem_filter.mpr ⟨hj, h⟩) |
| 108 | + rw [hwj, zero_smul] |
| 109 | + |
| 110 | +/-- Intersecting the affine span with the image of the whole digit box |
| 111 | +recovers exactly the set of defining rows. -/ |
| 112 | +theorem affineSpan_inter_box_eq [Fact p.Prime] (hp : 49 < p) (hk : k < p) |
| 113 | + {a : Fin k → Fin r → Fin 8} (ha : HasAxisColumns a) : |
| 114 | + (affineSpan (ZMod p) (Set.range fun i => modRow p (a i)) : Set (Fin r → ZMod p)) ∩ |
| 115 | + Set.range (modRow p) = Set.range fun i => modRow p (a i) := by |
| 116 | + ext x |
| 117 | + constructor |
| 118 | + · rintro ⟨hspan, row, rfl⟩ |
| 119 | + apply (mem_affineSpan_iff_of_hasAxisColumns hp hk ha ?_).mp hspan |
| 120 | + intro c |
| 121 | + change ((row c).val : ZMod p).val < 8 |
| 122 | + rw [ZMod.val_natCast_of_lt ((row c).isLt.trans (by omega : 8 < p))] |
| 123 | + exact (row c).isLt |
| 124 | + · rintro ⟨i, rfl⟩ |
| 125 | + exact ⟨subset_affineSpan _ _ (Set.mem_range_self i), ⟨a i, rfl⟩⟩ |
| 126 | + |
| 127 | +/-- Ordered tuples from `X` that omit at least one of the required columns. -/ |
| 128 | +def exceptionalTuples (k : ℕ) (X : Finset (Fin r → Fin 8)) : |
| 129 | + Finset (Fin k → Fin r → Fin 8) := |
| 130 | + (Fintype.piFinset fun _ : Fin k => X).filter fun a => ¬ HasAxisColumns a |
| 131 | + |
| 132 | +/-- Transposition gives exactly one choice from the other `8^k − 1` words |
| 133 | +in each coordinate when the specified column is absent. -/ |
| 134 | +theorem card_omit_column (w : Fin k → Fin 8) : |
| 135 | + (univ.filter fun a : Fin k → Fin r → Fin 8 => w ∉ univ.image (Function.swap a)).card = |
| 136 | + (8 ^ k - 1) ^ r := by |
| 137 | + calc |
| 138 | + _ = (Fintype.piFinset fun _ : Fin r => univ.erase w).card := by |
| 139 | + apply card_nbij' Function.swap Function.swap _ _ (fun _ _ => rfl) (fun _ _ => rfl) |
| 140 | + · intro a ha |
| 141 | + apply Fintype.mem_piFinset.mpr |
| 142 | + intro c |
| 143 | + apply mem_erase.mpr |
| 144 | + exact ⟨fun h => (mem_filter.mp ha).2 (mem_image.mpr ⟨c, mem_univ c, h⟩), mem_univ _⟩ |
| 145 | + · intro b hb |
| 146 | + refine mem_filter.mpr ⟨mem_univ _, ?_⟩ |
| 147 | + rintro h |
| 148 | + obtain ⟨c, _, hc⟩ := mem_image.mp h |
| 149 | + exact (mem_erase.mp (Fintype.mem_piFinset.mp hb c)).1 hc |
| 150 | + _ = _ := by simp [card_erase_of_mem] |
| 151 | + |
| 152 | +/-- A union bound over the two required column words per row. The same |
| 153 | +absolute bound applies to tuples in every subset of the digit box. -/ |
| 154 | +theorem card_exceptionalTuples_le (k : ℕ) (X : Finset (Fin r → Fin 8)) : |
| 155 | + (exceptionalTuples k X).card ≤ 2 * k * (8 ^ k - 1) ^ r := by |
| 156 | + let labels := (univ : Finset (Fin k)) ×ˢ ({1, 7} : Finset (Fin 8)) |
| 157 | + let missing (t : Fin k × Fin 8) := univ.filter fun a : Fin k → Fin r → Fin 8 => |
| 158 | + axisWord t.1 t.2 ∉ univ.image (Function.swap a) |
| 159 | + have hcover : exceptionalTuples k X ⊆ labels.biUnion missing := by |
| 160 | + intro a ha |
| 161 | + by_contra hout |
| 162 | + apply (mem_filter.mp ha).2 |
| 163 | + intro i d hd |
| 164 | + by_contra hmiss |
| 165 | + exact hout (mem_biUnion.mpr ⟨(i, d), mem_product.mpr ⟨mem_univ _, hd⟩, |
| 166 | + mem_filter.mpr ⟨mem_univ _, hmiss⟩⟩) |
| 167 | + calc |
| 168 | + _ ≤ (labels.biUnion missing).card := card_le_card hcover |
| 169 | + _ ≤ ∑ t ∈ labels, (missing t).card := card_biUnion_le |
| 170 | + _ = ∑ _t ∈ labels, (8 ^ k - 1) ^ r := sum_congr rfl fun t _ => card_omit_column _ |
| 171 | + _ = _ := by simp [labels, card_product, mul_comm k 2] |
| 172 | + |
| 173 | +private theorem missing_word_ratio (k : ℕ) : |
| 174 | + ((8 ^ k - 1 : ℕ) : ℝ) / (8 : ℝ) ^ k = 1 - 1 / (8 : ℝ) ^ k := by |
| 175 | + have hbase : 1 ≤ (8 : ℕ) ^ k := Nat.one_le_pow _ _ (by decide) |
| 176 | + rw [Nat.cast_sub hbase, Nat.cast_pow, Nat.cast_one, Nat.cast_ofNat, sub_div, |
| 177 | + div_self (by positivity)] |
| 178 | + |
| 179 | +/-- With density at least `1/M` in the full box, this finite bound for the |
| 180 | +fraction of exceptional ordered tuples depends only on `M`, `k` and `r`. -/ |
| 181 | +theorem exceptional_fraction_le (k M : ℕ) (X : Finset (Fin r → Fin 8)) |
| 182 | + (hX : 8 ^ r ≤ M * X.card) : |
| 183 | + (exceptionalTuples k X).card / (X.card : ℝ) ^ k ≤ |
| 184 | + 2 * k * (M : ℝ) ^ k * (1 - 1 / (8 : ℝ) ^ k) ^ r := by |
| 185 | + have hpos : 0 < X.card := by |
| 186 | + by_contra! h |
| 187 | + simp [show X.card = 0 by omega] at hX |
| 188 | + have hXpos : (0 : ℝ) < X.card := by exact_mod_cast hpos |
| 189 | + have hratio : (8 : ℝ) ^ r / X.card ≤ M := by |
| 190 | + apply (div_le_iff₀ hXpos).2 |
| 191 | + exact_mod_cast hX |
| 192 | + have hcount : ((exceptionalTuples k X).card : ℝ) ≤ |
| 193 | + 2 * k * ((8 ^ k - 1 : ℕ) : ℝ) ^ r := by exact_mod_cast card_exceptionalTuples_le k X |
| 194 | + calc |
| 195 | + _ ≤ 2 * k * ((8 ^ k - 1 : ℕ) : ℝ) ^ r / (X.card : ℝ) ^ k := |
| 196 | + div_le_div_of_nonneg_right hcount (by positivity) |
| 197 | + _ = 2 * k * (1 - 1 / (8 : ℝ) ^ k) ^ r * ((8 : ℝ) ^ r / X.card) ^ k := by |
| 198 | + rw [← missing_word_ratio] |
| 199 | + simp only [div_pow, ← pow_mul, Nat.mul_comm k r] |
| 200 | + field_simp |
| 201 | + _ ≤ 2 * k * (1 - 1 / (8 : ℝ) ^ k) ^ r * (M : ℝ) ^ k := by |
| 202 | + rw [← missing_word_ratio] |
| 203 | + gcongr |
| 204 | + _ = _ := by ring |
| 205 | + |
| 206 | +/-- The explicit upper bound tends to zero for polynomial density loss |
| 207 | +`M = C r^m + b`. In particular one can take `M = 49r+1`. -/ |
| 208 | +theorem tendsto_exceptional_fraction_bound (k C m b : ℕ) : |
| 209 | + Tendsto (fun r : ℕ => 2 * k * ((C * r ^ m + b : ℕ) : ℝ) ^ k * |
| 210 | + (1 - 1 / (8 : ℝ) ^ k) ^ r) atTop (𝓝 0) := by |
| 211 | + let q := 1 - 1 / (8 : ℝ) ^ k |
| 212 | + have hq : 0 ≤ q := by dsimp [q]; rw [← missing_word_ratio]; positivity |
| 213 | + have hq' : q < 1 := by dsimp [q]; exact sub_lt_self _ (by positivity) |
| 214 | + have hlim : Tendsto (fun r : ℕ => (2 * k * (C + b) ^ k : ℝ) * ((r : ℝ) ^ (m * k) * q ^ r)) |
| 215 | + atTop (𝓝 0) := by |
| 216 | + simpa using (tendsto_pow_const_mul_const_pow_of_lt_one (m * k) hq hq').const_mul |
| 217 | + (2 * k * (C + b) ^ k : ℝ) |
| 218 | + apply squeeze_zero' (Eventually.of_forall fun r => by |
| 219 | + change 0 ≤ 2 * k * ((C * r ^ m + b : ℕ) : ℝ) ^ k * q ^ r |
| 220 | + positivity) ?_ hlim |
| 221 | + filter_upwards [eventually_ge_atTop 1] with r hr |
| 222 | + have hpow : 1 ≤ (r : ℝ) ^ m := one_le_pow₀ (by exact_mod_cast hr) |
| 223 | + have hpoly : C * (r : ℝ) ^ m + b ≤ (C + b) * (r : ℝ) ^ m := by |
| 224 | + rw [add_mul] |
| 225 | + exact add_le_add le_rfl (le_mul_of_one_le_right (Nat.cast_nonneg b) hpow) |
| 226 | + calc |
| 227 | + _ ≤ 2 * k * ((C + b) * (r : ℝ) ^ m) ^ k * q ^ r := by push_cast; gcongr |
| 228 | + _ = _ := by rw [mul_pow, ← pow_mul]; ring |
| 229 | + |
| 230 | +/-- For fixed tuple length and polynomial density loss, a single dimension |
| 231 | +threshold controls the exceptional fraction in every eligible subset. |
| 232 | +The fraction uses all ordered tuples, with replacement. -/ |
| 233 | +theorem eventually_exceptional_fraction_lt (k C m b : ℕ) {ε : ℝ} (hε : 0 < ε) : |
| 234 | + ∀ᶠ r : ℕ in atTop, ∀ X : Finset (Fin r → Fin 8), 8 ^ r ≤ (C * r ^ m + b) * X.card → |
| 235 | + (exceptionalTuples k X).card / (X.card : ℝ) ^ k < ε := by |
| 236 | + filter_upwards [(tendsto_exceptional_fraction_bound k C m b).eventually_lt_const hε] with r hr |
| 237 | + exact fun X hX => (exceptional_fraction_le k _ X hX).trans_lt hr |
| 238 | + |
| 239 | +/-- The digit vectors whose base-16 encodings make up `digitLevel r t`. -/ |
| 240 | +def levelRows (r t : ℕ) : Finset (Fin r → Fin 8) := |
| 241 | + univ.filter fun row => ∑ c, (row c).val ^ 2 = t |
| 242 | + |
| 243 | +/-- Reading the coordinates as digits preserves the cardinality of the |
| 244 | +accepted base-16 level. -/ |
| 245 | +theorem card_levelRows (r t : ℕ) : |
| 246 | + (levelRows r t).card = (Erdos530DigitLevel.digitLevel r t).card := by |
| 247 | + rw [Erdos530DigitLevel.card_digitLevel] |
| 248 | + apply card_bij (fun row _ c => (row c).val) |
| 249 | + · intro row hrow |
| 250 | + exact mem_filter.mpr ⟨Behrend.mem_box.mpr fun c => (row c).isLt, (mem_filter.mp hrow).2⟩ |
| 251 | + · intro row _ row' _ h |
| 252 | + exact funext fun c => Fin.ext (congrFun h c) |
| 253 | + · intro v hv |
| 254 | + have hbox := Behrend.mem_box.mp (Behrend.sphere_subset_box hv) |
| 255 | + exact ⟨fun c => ⟨v c, hbox c⟩, mem_filter.mpr ⟨mem_univ _, (mem_filter.mp hv).2⟩, rfl⟩ |
| 256 | + |
| 257 | +/-- One dimension threshold applies to every largest squared-norm level, |
| 258 | +including ties. -/ |
| 259 | +theorem eventually_exceptional_fraction_lt_of_max_digitLevel (k : ℕ) {ε : ℝ} (hε : 0 < ε) : |
| 260 | + ∀ᶠ r : ℕ in atTop, ∀ t, (∀ s, (Erdos530DigitLevel.digitLevel r s).card ≤ |
| 261 | + (Erdos530DigitLevel.digitLevel r t).card) → |
| 262 | + (exceptionalTuples k (levelRows r t)).card / ((levelRows r t).card : ℝ) ^ k < ε := by |
| 263 | + filter_upwards [eventually_exceptional_fraction_lt k 49 1 1 hε] with r hfrac |
| 264 | + intro t hmax |
| 265 | + apply hfrac |
| 266 | + obtain ⟨s, _, _, hs⟩ := Erdos530DigitLevel.exists_max_digitLevel r |
| 267 | + rw [pow_one, card_levelRows] |
| 268 | + exact hs.trans (Nat.mul_le_mul_left _ (hmax s)) |
| 269 | + |
| 270 | +end ErdosSwarm.Erdos530DigitTuple |