yang
This project has not described itself yet.
Brought by drakestapleton
yang / proofs / DyadicExp.lean
| 1 | import Mathlib.Analysis.SpecialFunctions.Exp |
| 2 | import Mathlib.Tactic |
| 3 | |
| 4 | namespace YangMills.DyadicExp |
| 5 | |
| 6 | def q : ℤ := 268435456 |
| 7 | |
| 8 | /-- Downward-rounded positive squaring on the OM-2 dyadic grid. -/ |
| 9 | def squareDown : ℕ → ℤ → ℤ |
| 10 | | 0, v => v |
| 11 | | n+1, v => let w := squareDown n v; w*w/q |
| 12 | |
| 13 | def expLo (x : ℤ) := squareDown 12 (q + x/4096) |
| 14 | def ceilDiv (n d : ℤ) := -((-n)/d) |
| 15 | def expHi (x : ℤ) := ceilDiv (q*q) (expLo (-x)) |
| 16 | |
| 17 | private theorem floor_spec (a b : ℤ) (hb : 0 < b) : |
| 18 | ((a/b : ℤ) : ℝ) ≤ (a : ℝ)/(b:ℝ) := by |
| 19 | apply (le_div_iff₀ (by exact_mod_cast hb)).2 |
| 20 | exact_mod_cast Int.ediv_mul_le a (ne_of_gt hb) |
| 21 | |
| 22 | private theorem ceil_spec (a b : ℤ) (hb : 0 < b) : |
| 23 | (a : ℝ)/(b : ℝ) ≤ (ceilDiv a b : ℝ) := by |
| 24 | unfold ceilDiv |
| 25 | push_cast |
| 26 | have := floor_spec (-a) b hb |
| 27 | push_cast at this |
| 28 | rw [neg_div] at this |
| 29 | linarith |
| 30 | |
| 31 | private theorem squares_le {v : ℤ} {b : ℝ} (hv : 0 ≤ v) (hb : (v:ℝ) / q ≤ b) |
| 32 | (n : ℕ) : 0 ≤ squareDown n v ∧ (squareDown n v : ℝ)/q ≤ b^(2^n) := by |
| 33 | induction n with |
| 34 | | zero => simpa [squareDown] using And.intro hv hb |
| 35 | | succ n ih => |
| 36 | let w := squareDown n v |
| 37 | have hw : 0 ≤ w := ih.1 |
| 38 | refine ⟨Int.ediv_nonneg (mul_nonneg hw hw) (by decide), ?_⟩ |
| 39 | have hh := floor_spec (w*w) q (by decide) |
| 40 | push_cast at hh |
| 41 | have hpow := pow_le_pow_left₀ (show 0 ≤ (w:ℝ)/q by unfold q; positivity) ih.2 2 |
| 42 | rw [pow_succ 2, pow_mul] |
| 43 | change ((w*w/q : ℤ):ℝ)/q ≤ _ |
| 44 | calc _ ≤ ((w:ℝ)/q)^2 := by dsimp [q] at hh ⊢; linarith |
| 45 | _ ≤ _ := hpow |
| 46 | |
| 47 | theorem expLo_sound {x : ℤ} (hx : -(2*q) ≤ x) : |
| 48 | (expLo x : ℝ)/q ≤ Real.exp ((x:ℝ)/q) := by |
| 49 | have hf := floor_spec x 4096 (by decide) |
| 50 | have hv : 0 ≤ q + x/4096 := by dsimp [q] at *; omega |
| 51 | have hp := Real.add_one_le_exp ((x:ℝ)/(q*4096)) |
| 52 | have hbase : ((q+x/4096 : ℤ):ℝ)/q ≤ Real.exp ((x:ℝ)/(q*4096)) := by |
| 53 | push_cast |
| 54 | dsimp [q] at *; linarith |
| 55 | have hh := (squares_le hv hbase 12).2 |
| 56 | rw [← Real.exp_nat_mul] at hh |
| 57 | convert hh using 2 <;> norm_num [expLo, q] |
| 58 | ring |
| 59 | |
| 60 | private theorem squareDown_mono {v w : ℤ} (hv : 0 ≤ v) (hvw : v ≤ w) (n : ℕ) : |
| 61 | 0 ≤ squareDown n v ∧ squareDown n v ≤ squareDown n w := by |
| 62 | induction n with |
| 63 | | zero => exact ⟨hv,hvw⟩ |
| 64 | | succ n ih => |
| 65 | have hw := ih.1.trans ih.2 |
| 66 | exact ⟨Int.ediv_nonneg (mul_nonneg ih.1 ih.1) (by decide), |
| 67 | Int.ediv_le_ediv (by decide) (mul_le_mul ih.2 ih.2 ih.1 hw)⟩ |
| 68 | |
| 69 | theorem expLo_pos {x : ℤ} (hx : -(2*q) ≤ x) : 0 < expLo x := by |
| 70 | have hv : 0 ≤ q + (-(2*q))/4096 := by decide |
| 71 | have hh : q + (-(2*q))/4096 ≤ q + x/4096 := |
| 72 | add_le_add_right (Int.ediv_le_ediv (by decide) hx) q |
| 73 | exact (show 0 < expLo (-(2*q)) by decide).trans_le (squareDown_mono hv hh 12).2 |
| 74 | |
| 75 | theorem expHi_sound {x : ℤ} (hx : x ≤ 2*q) : |
| 76 | Real.exp ((x:ℝ)/q) ≤ (expHi x : ℝ)/q := by |
| 77 | have hd := expLo_pos (x := -x) (by omega) |
| 78 | have hl := expLo_sound (x := -x) (by omega) |
| 79 | push_cast at hl |
| 80 | rw [neg_div, Real.exp_neg] at hl |
| 81 | have he := (le_div_iff₀ (Real.exp_pos ((x:ℝ)/q))).mp (by simpa [one_div] using hl : |
| 82 | (expLo (-x) : ℝ)/q ≤ 1/Real.exp ((x:ℝ)/q)) |
| 83 | have hden : 0 < (expLo (-x) : ℝ) := by exact_mod_cast hd |
| 84 | have hu := ceil_spec (q*q) (expLo (-x)) hd |
| 85 | push_cast at hu |
| 86 | have hinv : Real.exp ((x:ℝ)/q) ≤ (q:ℝ)/(expLo (-x):ℝ) := by |
| 87 | apply (le_div_iff₀ hden).2 |
| 88 | dsimp [q] at he ⊢; linarith |
| 89 | apply hinv.trans |
| 90 | apply (le_div_iff₀ (show (0:ℝ)<q by norm_num [q])).2 |
| 91 | rw [div_mul_eq_mul_div] |
| 92 | exact hu |
| 93 | |
| 94 | #print axioms expLo_sound |
| 95 | #print axioms expHi_sound |
| 96 | end YangMills.DyadicExp |