interplane.Get Omega
Read in your language

yang

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

yang / proofs / DyadicTrig.lean

247 lines · 12524 bytes Raw Blame History
1import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
2import Mathlib.Data.Nat.Sqrt
3import Mathlib.Analysis.Real.Pi.Bounds
4
5namespace YangMills.DyadicTrig
6
7/-- Integer endpoints represent multiples of 2⁻³⁰. -/
8abbrev Bounds := ℤ × ℤ
9
10def scale : ℤ := 1073741824
11
12def Contains (b : Bounds) (x : ℝ) : Prop :=
13 (b.1 : ℝ) ≤ scale * x ∧ scale * x ≤ (b.2 : ℝ)
14
15def halfRoot (a : ℤ) : ℕ := ((scale + a).toNat * 536870912).sqrt
16
17def half (b : Bounds) : Bounds :=
18 (halfRoot b.1, min scale ((halfRoot b.2 : ℤ) + 1))
19
20def reflect (b : Bounds) : Bounds := (-b.2, -b.1)
21
22/-- Half-angle enclosure. Truncation to Nat also permits a lower endpoint below -scale. -/
23theorem half_sound {b : Bounds} {t : ℝ} (ht : 0 ≤ Real.cos t)
24 (h : Contains b (Real.cos (2*t))) : Contains (half b) (Real.cos t) := by
25 have id := Real.cos_two_mul t
26 have hb := Real.cos_le_one t
27 obtain ⟨hl, hu⟩ := h
28 simp only [scale, Int.cast_ofNat] at hl hu
29 have lower := Nat.sqrt_le' ((scale + b.1).toNat * 536870912)
30 have upper := Nat.lt_succ_sqrt' ((scale + b.2).toNat * 536870912)
31 change (halfRoot b.1)^2 ≤ _ at lower
32 change _ < (halfRoot b.2).succ ^ 2 at upper
33 have hu0 : 0 ≤ scale + b.2 := by
34 have : (0:ℝ) ≤ 1073741824 + b.2 := by linarith [Real.neg_one_le_cos (2*t)]
35 exact_mod_cast this
36 have huCast : (((scale + b.2).toNat : ℕ) : ℝ) = 1073741824 + b.2 := by
37 exact_mod_cast Int.toNat_of_nonneg hu0
38 have hU : (((scale+b.2).toNat : ℕ) : ℝ) * 536870912 < ((halfRoot b.2 : ℝ)+1)^2 := by exact_mod_cast upper
39 rw [huCast] at hU
40 simp only [Contains, half, scale, Int.cast_natCast, Int.cast_ofNat, Int.cast_min, Int.cast_add, Int.cast_one]
41 constructor
42 · by_cases hp : 0 ≤ scale + b.1
43 · have hCast : (((scale + b.1).toNat : ℕ) : ℝ) = 1073741824 + b.1 := by
44 exact_mod_cast Int.toNat_of_nonneg hp
45 have hL : (halfRoot b.1 : ℝ)^2 ≤ (((scale+b.1).toNat : ℕ) : ℝ) * 536870912 := by exact_mod_cast lower
46 rw [hCast] at hL
47 apply le_of_sq_le_sq _ (by positivity)
48 nlinarith [hl, id]
49 · have hz : (scale+b.1).toNat = 0 := Int.toNat_of_nonpos (by omega)
50 simp [halfRoot, hz, ht]
51 · apply le_min (by linarith)
52 apply le_of_sq_le_sq _ (by positivity)
53 nlinarith [hu, id]
54
55noncomputable def angle (n i : ℕ) : ℝ := (i : ℝ) * Real.pi / (2:ℝ)^n
56
57/-- The cosine of the reflected angle changes sign. Quarter-turn endpoints are exact. -/
58def cosBounds : ℕ → ℕ → Bounds
59 | 0, i => if i = 0 then (scale, scale) else (-scale, -scale)
60 | n+1, i =>
61 if i = 0 then (scale, scale)
62 else if i = 2^(n+1) then (-scale, -scale)
63 else if i = 2^n then (0, 0)
64 else if i < 2^n then half (cosBounds n i)
65 else reflect (half (cosBounds n (2^(n+1)-i)))
66
67private theorem half_step {n i : ℕ} (hi : i ≤ 2^n)
68 (h : Contains (cosBounds n i) (Real.cos (angle n i))) :
69 Contains (half (cosBounds n i)) (Real.cos (angle (n+1) i)) := by
70 apply half_sound
71 · apply Real.cos_nonneg_of_mem_Icc
72 constructor
73 · have : 0 ≤ angle (n+1) i := by unfold angle; positivity
74 linarith [Real.pi_pos]
75 · unfold angle
76 apply (div_le_iff₀ (by positivity)).2
77 have hi' : (i:ℝ) ≤ (2:ℝ)^n := by exact_mod_cast hi
78 rw [pow_succ]
79 nlinarith [mul_le_mul_of_nonneg_right hi' Real.pi_pos.le]
80 · convert h using 2
81 unfold angle
82 rw [pow_succ]
83 field_simp
84
85theorem cosBounds_sound (n i : ℕ) (hi : i ≤ 2^n) :
86 Contains (cosBounds n i) (Real.cos (angle n i)) := by
87 induction n generalizing i with
88 | zero =>
89 have : i = 0 ∨ i = 1 := by simp at hi; omega
90 rcases this with rfl | rfl <;> simp [cosBounds, angle, Contains, scale]
91 | succ n ih =>
92 by_cases h0 : i = 0
93 · subst i; simp [cosBounds, angle, Contains, scale]
94 by_cases hend : i = 2^(n+1)
95 · subst i; simp [cosBounds, angle, Contains, scale, ne_of_gt (pow_pos (by norm_num : (0:ℝ)<2) _)]
96 by_cases hmid : i = 2^n
97 · subst i
98 have heq : angle (n+1) (2^n) = Real.pi/2 := by simp [angle, pow_succ]; field_simp
99 simp [cosBounds, hend, heq, Contains]
100 by_cases hlt : i < 2^n
101 · simpa [cosBounds, h0, hend, hmid, hlt] using half_step hlt.le (ih i hlt.le)
102 · let j := 2^(n+1) - i
103 have hj : j ≤ 2^n := by dsimp [j]; rw [pow_succ]; omega
104 have hjAngle : angle (n+1) i = Real.pi - angle (n+1) j := by
105 simp only [angle, j, Nat.cast_sub hi, Nat.cast_pow, Nat.cast_ofNat]
106 field_simp
107 ring
108 obtain ⟨lo, up⟩ := half_step hj (ih j hj)
109 rw [hjAngle, Real.cos_pi_sub]
110 simpa [cosBounds, h0, hend, hmid, hlt, reflect, Contains, j] using
111 And.intro (neg_le_neg up) (neg_le_neg lo)
112
113def sinLowIdx (i : ℕ) := if i < 1024 then 1024-i else i-1023
114def sinHighIdx (i : ℕ) := if i < 1024 then 1023-i else i-1024
115
116def sinSqBounds (i : ℕ) : Bounds :=
117 let l := max 0 (cosBounds 11 (sinLowIdx i)).1
118 let h := (cosBounds 11 (sinHighIdx i)).2
119 (l^2/1024, (h^2+1023)/1024)
120
121/-- Bounds throughout the cell, including the regridding of the squared sine to 2⁻⁵⁰. -/
122theorem grid_cell_sound {i : ℕ} (hi : i < 2048) {t : ℝ}
123 (ht : t ∈ Set.Icc (angle 11 i) (angle 11 (i+1))) :
124 ((cosBounds 11 (i+1)).1 : ℝ) ≤ scale * Real.cos t ∧
125 scale * Real.cos t ≤ ((cosBounds 11 i).2 : ℝ) ∧
126 ((sinSqBounds i).1 : ℝ) ≤ 1125899906842624 * Real.sin t ^ 2 ∧
127 1125899906842624 * Real.sin t ^ 2 ≤ ((sinSqBounds i).2 : ℝ) := by
128 have ends (k : ℕ) (hk : k ≤ 2048) : angle 11 k ∈ Set.Icc 0 Real.pi := by
129 constructor
130 · unfold angle; positivity
131 · have hk' : (k:ℝ) ≤ 2048 := by exact_mod_cast hk
132 unfold angle; norm_num
133 nlinarith [mul_le_mul_of_nonneg_right hk' Real.pi_pos.le]
134 have left := ends i (by omega)
135 have right := ends (i+1) (by omega)
136 have ht0 := left.1.trans ht.1
137 have htπ := ht.2.trans right.2
138 have hQ : (0:ℝ) ≤ scale := by norm_num [scale]
139 refine ⟨(cosBounds_sound 11 (i+1) (by norm_num; omega)).1.trans
140 (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi ht0 right.2 ht.2) hQ),
141 (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi left.1 htπ ht.1) hQ).trans
142 (cosBounds_sound 11 i (by norm_num; omega)).2, ?_⟩
143 let a := |Real.pi/2 - t|
144 have ha : angle 11 (sinHighIdx i) ≤ a ∧ a ≤ angle 11 (sinLowIdx i) := by
145 obtain ⟨hl,hu⟩ := ht
146 norm_num [angle] at hl hu
147 by_cases h : i < 1024
148 · have hcast : (i:ℝ)+1 ≤ 1024 := by exact_mod_cast (show i+1 ≤ 1024 by omega)
149 have hab : 0 ≤ Real.pi/2-t := by nlinarith [mul_le_mul_of_nonneg_right hcast Real.pi_pos.le]
150 simp [a, abs_of_nonneg hab, sinHighIdx, sinLowIdx, h, angle, Nat.cast_sub (show i ≤ 1023 by omega),
151 Nat.cast_sub (show i ≤ 1024 by omega)]
152 constructor <;> linarith
153 · have hcast : 1024 ≤ (i:ℝ) := by exact_mod_cast (show 1024 ≤ i by omega)
154 have hab : Real.pi/2-t ≤ 0 := by nlinarith [mul_le_mul_of_nonneg_right hcast Real.pi_pos.le]
155 simp only [a, abs_of_nonpos hab, sinHighIdx, sinLowIdx, if_neg h, angle,
156 Nat.cast_sub (show 1024 ≤ i by omega), Nat.cast_sub (show 1023 ≤ i by omega), Nat.cast_ofNat]
157 norm_num
158 constructor <;> linarith
159 have jl : sinLowIdx i ≤ 2048 := by unfold sinLowIdx; split <;> omega
160 have jh : sinHighIdx i ≤ 2048 := by unfold sinHighIdx; split <;> omega
161 have hs : Real.cos a = Real.sin t := by rw [Real.cos_abs, Real.cos_pi_div_two_sub]
162 have hsin := Real.sin_nonneg_of_nonneg_of_le_pi ht0 htπ
163 have lower := (cosBounds_sound 11 _ (by simpa using jl)).1.trans
164 (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi (abs_nonneg _) (ends _ jl).2 ha.2) hQ)
165 have upper := (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi (ends _ jh).1
166 (ha.2.trans (ends _ jl).2) ha.1) hQ).trans (cosBounds_sound 11 _ (by simpa using jh)).2
167 rw [hs] at lower upper
168 let l := max 0 (cosBounds 11 (sinLowIdx i)).1
169 let h := (cosBounds 11 (sinHighIdx i)).2
170 have hl : (l:ℝ) ≤ scale * Real.sin t := by simpa [l] using max_le (mul_nonneg hQ hsin) lower
171 have hln : (0:ℝ) ≤ l := by simp [l]
172 have sl := pow_le_pow_left₀ hln hl 2
173 have su := pow_le_pow_left₀ (mul_nonneg hQ hsin) upper 2
174 have fl : (l^2/1024)*1024 ≤ l^2 := by omega
175 have ce : h^2 ≤ ((h^2+1023)/1024)*1024 := by omega
176 have fl' : ((l^2/1024 : ℤ):ℝ)*1024 ≤ (l:ℝ)^2 := by exact_mod_cast fl
177 have ce' : (h:ℝ)^2 ≤ (((h^2+1023)/1024 : ℤ):ℝ)*1024 := by exact_mod_cast ce
178 change ((l^2/1024 : ℤ):ℝ) ≤ _ ∧ _ ≤ (((h^2+1023)/1024 : ℤ):ℝ)
179 change _ ≤ (h:ℝ)^2 at su
180 norm_num [scale] at sl su
181 constructor <;> nlinarith
182
183/-- Integer angle indices for the first-quadrant star-cell cutoffs entered in radians. -/
184def radialLo (a : ℕ) := a*204800000/(314160*268435456)
185def radialHi (a : ℕ) := ((a+1)*204800000 + (314159*268435456-1))/(314159*268435456)
186
187private theorem rounded_up_mul (a d : ℕ) (hd : 0 < d) :
188 a ≤ ((a+(d-1))/d)*d := by
189 have h := Nat.lt_mul_div_succ (a+(d-1)) hd
190 rw [Nat.mul_succ, Nat.mul_comm d] at h
191 omega
192
193theorem radial_indices_sound {a : ℕ} {t : ℝ} (ht : t ≤ 3/2)
194 (hl : (a:ℝ) ≤ t*268435456) (hu : t*268435456 ≤ a+1) :
195 radialHi a ≤ 1024 ∧ angle 11 (radialLo a) ≤ t ∧ t ≤ angle 11 (radialHi a) := by
196 have ha : a ≤ 402653184 := by exact_mod_cast (show (a:ℝ) ≤ 402653184 by linarith)
197 have hL : radialLo a*(314160*268435456) ≤ a*204800000 := by norm_num [radialLo]; omega
198 have hH : (a+1)*204800000 ≤ radialHi a*(314159*268435456) := rounded_up_mul _ _ (by decide)
199 have hL' : (radialLo a:ℝ)*(314160*268435456) ≤ (a:ℝ)*204800000 := by exact_mod_cast hL
200 have hH' : ((a:ℝ)+1)*204800000 ≤ (radialHi a:ℝ)*(314159*268435456) := by exact_mod_cast hH
201 refine ⟨?_, ?_, ?_⟩
202 · apply Nat.le_of_lt_succ
203 apply (Nat.div_lt_iff_lt_mul (by decide : 0 < 314159*268435456)).2
204 omega
205 · have hp := mul_le_mul_of_nonneg_left Real.pi_lt_d4.le (show 0 ≤ (radialLo a:ℝ) by positivity)
206 norm_num [angle]; linarith
207 · have hp := mul_le_mul_of_nonneg_left Real.pi_gt_d6.le (show 0 ≤ (radialHi a:ℝ) by positivity)
208 have hq := mul_le_mul_of_nonneg_left (show (3.14159:ℝ) ≤ 3.141592 by norm_num)
209 (show 0 ≤ (radialHi a:ℝ) by positivity)
210 norm_num [angle]; linarith
211
212theorem radial_trig_sound {a : ℕ} {t : ℝ} (ht : t ≤ 3/2)
213 (hl : (a:ℝ) ≤ t*268435456) (hu : t*268435456 ≤ a+1) :
214 ((cosBounds 11 (radialHi a)).1 : ℝ) ≤ scale*Real.cos t ∧
215 scale*Real.cos t ≤ ((cosBounds 11 (radialLo a)).2 : ℝ) ∧
216 ((cosBounds 11 (1024-radialLo a)).1 : ℝ) ≤ scale*Real.sin t ∧
217 scale*Real.sin t ≤ ((cosBounds 11 (1024-radialHi a)).2 : ℝ) := by
218 obtain ⟨hh,hlt,hut⟩ := radial_indices_sound ht hl hu
219 have ht0 : 0 ≤ t := by linarith [(Nat.cast_nonneg a : 0 ≤ (a:ℝ))]
220 have hlow : radialLo a ≤ 1024 := by
221 have ha : a ≤ 402653184 := by exact_mod_cast (show (a:ℝ) ≤ 402653184 by linarith)
222 unfold radialLo; omega
223 have endmem {j : ℕ} (hj : j ≤ 1024) : angle 11 j ∈ Set.Icc 0 (Real.pi/2) := by
224 have hj' : (j:ℝ) ≤ 1024 := by exact_mod_cast hj
225 norm_num [angle]; constructor
226 · positivity
227 · linarith [mul_le_mul_of_nonneg_right hj' Real.pi_pos.le]
228 have hc {j k : ℕ} {u : ℝ} (hj : j ≤ 1024) (hk : k ≤ 1024)
229 (hj' : angle 11 j ≤ u) (hk' : u ≤ angle 11 k) :
230 ((cosBounds 11 k).1:ℝ) ≤ scale*Real.cos u ∧ scale*Real.cos u ≤ ((cosBounds 11 j).2:ℝ) := by
231 have hkπ : angle 11 k ≤ Real.pi := by linarith [(endmem hk).2, Real.pi_pos]
232 have hQ : 0 ≤ (scale:ℝ) := by norm_num [scale]
233 exact ⟨(cosBounds_sound 11 k (by norm_num; omega)).1.trans
234 (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi ((endmem hj).1.trans hj') hkπ hk') hQ),
235 (mul_le_mul_of_nonneg_left (Real.cos_le_cos_of_nonneg_of_le_pi (endmem hj).1 (hk'.trans hkπ) hj') hQ).trans
236 (cosBounds_sound 11 j (by norm_num; omega)).2⟩
237 have refl {j : ℕ} (hj : j ≤ 1024) : angle 11 (1024-j) = Real.pi/2 - angle 11 j := by
238 simp [angle, Nat.cast_sub hj]; ring
239 have hs := hc (j := 1024-radialHi a) (k := 1024-radialLo a) (u := Real.pi/2-t)
240 (by omega) (by omega) (by rw [refl hh]; linarith) (by rw [refl hlow]; linarith)
241 rw [Real.cos_pi_div_two_sub] at hs
242 exact ⟨(hc hlow hh hlt hut).1, (hc hlow hh hlt hut).2, hs⟩
243
244#print axioms radial_trig_sound
245#print axioms grid_cell_sound
246#print axioms cosBounds_sound
247end YangMills.DyadicTrig