math / data /lean /OAI /NumberTheory /GaussianMoat /WalkGeometry.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
28.4 kB
import OAI.NumberTheory.GaussianMoat.FiniteLaw
namespace OAI
namespace GaussianMoat
section
open scoped BigOperators
def colorEdge (a b : Fin 3) : β„€ :=
if a = 0 ∧ b = 1 then 1 else if a = 1 ∧ b = 0 then -1 else 0
def Tricolored (a b c : Fin 3) : Prop := a β‰  b ∧ b β‰  c ∧ a β‰  c
theorem colorEdge_add {a b c : Fin 3} (h : Β¬ Tricolored a b c) :
colorEdge a b + colorEdge b c = colorEdge a c := by
unfold Tricolored at h
revert a b c
decide
theorem colorEdge_zero_left (a b : Fin 3) (ha : a β‰  0) (hb : b β‰  0) :
colorEdge a b = 0 := by simp [colorEdge,ha,hb]
theorem colorEdge_zero_one (a b : Fin 3) (ha : a β‰  1) (hb : b β‰  1) :
colorEdge a b = 0 := by simp [colorEdge,ha,hb]
theorem colorEdge_binary (a b : Fin 3) (ha : a β‰  2) (hb : b β‰  2) :
colorEdge a b = (b.val : β„€) - a.val := by
revert a b
decide
theorem discrete_rectangle_sperner (c : β„• β†’ β„• β†’ Fin 3) (m n : β„•)
(hbottom : βˆ€ i ≀ m, c i 0 = 0)
(htop : βˆ€ i ≀ m, c i n β‰  0)
(hleft : βˆ€ j ≀ n, c 0 j β‰  2)
(hright : βˆ€ j ≀ n, c m j β‰  1) :
βˆƒ i < m, βˆƒ j < n,
Tricolored (c i j) (c (i+1) j) (c (i+1) (j+1)) ∨
Tricolored (c i j) (c i (j+1)) (c (i+1) (j+1)) := by
by_contra h
have hcell (i j : β„•) (hi : i < m) (hj : j < n) :
colorEdge (c i j) (c (i+1) j) - colorEdge (c i (j+1)) (c (i+1) (j+1)) =
colorEdge (c i j) (c i (j+1)) - colorEdge (c (i+1) j) (c (i+1) (j+1)) := by
have h1 : Β¬ Tricolored (c i j) (c (i+1) j) (c (i+1) (j+1)) :=
fun hh => h ⟨i,hi,j,hj,Or.inl hh⟩
have h2 : Β¬ Tricolored (c i j) (c i (j+1)) (c (i+1) (j+1)) :=
fun hh => h ⟨i,hi,j,hj,Or.inr hh⟩
have he1 := colorEdge_add h1
have he2 := colorEdge_add h2
omega
have hsum := Finset.sum_congr (s₁ := Finset.range n) rfl (fun j hj =>
Finset.sum_congr (s₁ := Finset.range m) rfl (fun i hi =>
hcell i j (Finset.mem_range.mp hi) (Finset.mem_range.mp hj)))
have hl : (βˆ‘ j ∈ Finset.range n, βˆ‘ i ∈ Finset.range m,
(colorEdge (c i j) (c (i+1) j) - colorEdge (c i (j+1)) (c (i+1) (j+1)))) = 0 := by
rw [Finset.sum_comm]
apply Finset.sum_eq_zero
intro i hi
rw [Finset.sum_range_sub']
rw [hbottom i (Nat.le_of_lt (Finset.mem_range.mp hi)),
hbottom (i+1) (Finset.mem_range.mp hi)]
rw [colorEdge_zero_left _ _ (htop i (Nat.le_of_lt (Finset.mem_range.mp hi)))
(htop (i+1) (Finset.mem_range.mp hi))]
rfl
have hr : (βˆ‘ j ∈ Finset.range n, βˆ‘ i ∈ Finset.range m,
(colorEdge (c i j) (c i (j+1)) - colorEdge (c (i+1) j) (c (i+1) (j+1)))) = 1 := by
have he (j : β„•) (hj : j ∈ Finset.range n) :
(βˆ‘ i ∈ Finset.range m, (colorEdge (c i j) (c i (j+1)) -
colorEdge (c (i+1) j) (c (i+1) (j+1)))) = ((c 0 (j+1)).val : β„€) - (c 0 j).val := by
rw [Finset.sum_range_sub']
rw [colorEdge_zero_one _ _ (hright j (Nat.le_of_lt (Finset.mem_range.mp hj)))
(hright (j+1) (Finset.mem_range.mp hj)),sub_zero,
colorEdge_binary _ _ (hleft j (Nat.le_of_lt (Finset.mem_range.mp hj)))
(hleft (j+1) (Finset.mem_range.mp hj))]
rw [Finset.sum_congr rfl he,Finset.sum_range_sub (fun j => ((c 0 j).val : β„€))]
have hend : c 0 n = 1 := by
have h0 := htop 0 (Nat.zero_le m)
have h2 := hleft n le_rfl
have hone : βˆ€ x : Fin 3, x β‰  0 β†’ x β‰  2 β†’ x = 1 := by decide
exact hone _ h0 h2
rw [hend,hbottom 0 (Nat.zero_le m)]
norm_num
rw [hl,hr] at hsum
omega
noncomputable def pointColor (z : β„‚) : Fin 3 := if z.im < 0 then 0 else if z.re < 0 then 1 else 2
theorem pointColor_zero {z : β„‚} (h : pointColor z = 0) : z.im < 0 := by
unfold pointColor at h
split_ifs at h with hi hr
Β· exact hi
Β· exact False.elim ((by decide : (1 : Fin 3) β‰  0) h)
Β· exact False.elim ((by decide : (2 : Fin 3) β‰  0) h)
theorem pointColor_one {z : β„‚} (h : pointColor z = 1) : z.re < 0 ∧ 0 ≀ z.im := by
unfold pointColor at h
split_ifs at h with hi hr
Β· exact False.elim ((by decide : (0 : Fin 3) β‰  1) h)
· exact ⟨hr,le_of_not_gt hi⟩
Β· exact False.elim ((by decide : (2 : Fin 3) β‰  1) h)
theorem pointColor_two {z : β„‚} (h : pointColor z = 2) : 0 ≀ z.re ∧ 0 ≀ z.im := by
unfold pointColor at h
split_ifs at h with hi hr
Β· exact False.elim ((by decide : (0 : Fin 3) β‰  2) h)
Β· exact False.elim ((by decide : (1 : Fin 3) β‰  2) h)
· exact ⟨le_of_not_gt hr,le_of_not_gt hi⟩
theorem tricolored_covers {a b c : Fin 3} (h : Tricolored a b c) (q : Fin 3) :
q = a ∨ q = b ∨ q = c := by
unfold Tricolored at h
revert a b c q
decide
theorem tricolored_norm_bound {x y z : β„‚} {r : ℝ} (hr : 0 ≀ r)
(hxy : dist x y ≀ r) (hxz : dist x z ≀ r)
(hc : Tricolored (pointColor x) (pointColor y) (pointColor z)) : β€–xβ€– ≀ 2*r := by
have he (q : Fin 3) : βˆƒ w : β„‚, pointColor w = q ∧ dist x w ≀ r := by
rcases tricolored_covers hc q with h | h | h
· exact ⟨x,h.symm,by simpa using hr⟩
· exact ⟨y,h.symm,hxy⟩
· exact ⟨z,h.symm,hxz⟩
obtain ⟨w0,h0,hd0⟩ := he 0
obtain ⟨w1,h1,hd1⟩ := he 1
obtain ⟨w2,h2,hd2⟩ := he 2
have hc0 := pointColor_zero h0
have hc1 := pointColor_one h1
have hc2 := pointColor_two h2
have hRe (w : β„‚) (hd : dist x w ≀ r) : -r ≀ x.re-w.re ∧ x.re-w.re ≀ r := by
apply abs_le.mp
have hh : |x.re-w.re| ≀ dist x w := by simpa only [Complex.sub_re,dist_eq_norm] using Complex.abs_re_le_norm (x-w)
exact hh.trans hd
have hIm (w : β„‚) (hd : dist x w ≀ r) : -r ≀ x.im-w.im ∧ x.im-w.im ≀ r := by
apply abs_le.mp
have hh : |x.im-w.im| ≀ dist x w := by simpa only [Complex.sub_im,dist_eq_norm] using Complex.abs_im_le_norm (x-w)
exact hh.trans hd
have hR1 := hRe w1 hd1
have hR2 := hRe w2 hd2
have hI0 := hIm w0 hd0
have hI1 := hIm w1 hd1
have hrx : |x.re| ≀ r := abs_le.mpr ⟨by linarith,by linarith⟩
have hix : |x.im| ≀ r := abs_le.mpr ⟨by linarith,by linarith⟩
have h := Complex.norm_le_abs_re_add_abs_im x
linarith
theorem discrete_rectangle_approx_zero (f : β„• β†’ β„• β†’ β„‚) (m n : β„•) {D : ℝ} (hD : 0 ≀ D)
(hhor : βˆ€ i < m, βˆ€ j ≀ n, dist (f i j) (f (i+1) j) ≀ D)
(hver : βˆ€ i ≀ m, βˆ€ j < n, dist (f i j) (f i (j+1)) ≀ D)
(hbottom : βˆ€ i ≀ m, (f i 0).im < 0)
(htop : βˆ€ i ≀ m, 0 ≀ (f i n).im)
(hleft : βˆ€ j ≀ n, (f 0 j).re < 0)
(hright : βˆ€ j ≀ n, 0 ≀ (f m j).re) :
βˆƒ i ≀ m, βˆƒ j ≀ n, β€–f i jβ€– ≀ 4*D := by
have hb : βˆ€ i ≀ m, pointColor (f i 0) = 0 := by
intro i hi
simp [pointColor,hbottom i hi]
have ht : βˆ€ i ≀ m, pointColor (f i n) β‰  0 := by
intro i hi hh
exact (not_lt_of_ge (htop i hi)) (pointColor_zero hh)
have hl : βˆ€ j ≀ n, pointColor (f 0 j) β‰  2 := by
intro j hj hh
exact (not_le_of_gt (hleft j hj)) (pointColor_two hh).1
have hr : βˆ€ j ≀ n, pointColor (f m j) β‰  1 := by
intro j hj hh
exact (not_lt_of_ge (hright j hj)) (pointColor_one hh).1
obtain ⟨i,hi,j,hj,hc⟩ := discrete_rectangle_sperner (fun i j => pointColor (f i j)) m n hb ht hl hr
refine ⟨i,hi.le,j,hj.le,?_⟩
have hdiag : dist (f i j) (f (i+1) (j+1)) ≀ 2*D := by
have h := dist_triangle (f i j) (f (i+1) j) (f (i+1) (j+1))
have h1 := hhor i hi j hj.le
have h2 := hver (i+1) hi j hj
linarith
rcases hc with hc | hc
Β· have hh := tricolored_norm_bound (show 0 ≀ 2*D by positivity)
((hhor i hi j hj.le).trans (by linarith : D ≀ 2*D)) hdiag hc
linarith
Β· have hh := tricolored_norm_bound (show 0 ≀ 2*D by positivity)
((hver i hi.le j hj).trans (by linarith : D ≀ 2*D)) hdiag hc
linarith
noncomputable def repeatPath (v : β„• β†’ β„‚) (n : β„•) (a : β„‚) (t : β„•) : β„‚ :=
v (t % n) + ((t / n : β„•) : β„‚) * a
@[simp] theorem repeatPath_zero (v : β„• β†’ β„‚) (n : β„•) (a : β„‚) :
repeatPath v n a 0 = v 0 := by simp [repeatPath]
@[simp] theorem repeatPath_multiple (v : β„• β†’ β„‚) {n : β„•} (hn : 0 < n) (a : β„‚) (k : β„•) :
repeatPath v n a (k*n) = v 0 + (k : β„‚)*a := by simp [repeatPath,hn.ne']
theorem repeatPath_step (v : β„• β†’ β„‚) {n : β„•} (hn : 0 < n) (a : β„‚)
(hv0 : v 0 = 0) (hvn : v n = a) {D : ℝ}
(hstep : βˆ€ i < n, dist (v i) (v (i+1)) ≀ D) (t : β„•) :
dist (repeatPath v n a t) (repeatPath v n a (t+1)) ≀ D := by
have hr := Nat.mod_lt t hn
have hdiv := Nat.mod_add_div t n
by_cases hh : t % n + 1 = n
Β· have ht : t+1 = (t/n+1)*n := by nlinarith
rw [ht,repeatPath_multiple v hn]
have he : repeatPath v n a t = v (t%n) + ((t/n:β„•):β„‚)*a := rfl
rw [he,hv0,zero_add,Nat.cast_add,Nat.cast_one,add_mul,one_mul,add_comm (((t/n:β„•):β„‚)*a) a]
rw [dist_add_right]
simpa [hh,hvn] using hstep (t%n) hr
Β· have hr' : t%n+1 < n := by omega
have ht : t+1 = t%n+1+(t/n)*n := by nlinarith
have hmod : (t+1)%n = t%n+1 := by rw [ht,Nat.add_mul_mod_self_right,Nat.mod_eq_of_lt hr']
have hquo : (t+1)/n = t/n := by rw [ht,Nat.add_mul_div_right _ _ hn,Nat.div_eq_of_lt hr',zero_add]
simp only [repeatPath,hmod,hquo,dist_add_right]
exact hstep (t%n) hr
theorem discrete_rectangle_approx_zero_rev (f : β„• β†’ β„• β†’ β„‚) (m n : β„•) {D : ℝ} (hD : 0 ≀ D)
(hhor : βˆ€ i < m, βˆ€ j ≀ n, dist (f i j) (f (i+1) j) ≀ D)
(hver : βˆ€ i ≀ m, βˆ€ j < n, dist (f i j) (f i (j+1)) ≀ D)
(hbottom : βˆ€ i ≀ m, 0 < (f i 0).im)
(htop : βˆ€ i ≀ m, (f i n).im ≀ 0)
(hleft : βˆ€ j ≀ n, (f 0 j).re < 0)
(hright : βˆ€ j ≀ n, 0 ≀ (f m j).re) :
βˆƒ i ≀ m, βˆƒ j ≀ n, β€–f i jβ€– ≀ 4*D := by
have hh : βˆ€ i < m, βˆ€ j ≀ n, dist (star (f i j)) (star (f (i+1) j)) ≀ D := by
intro i hi j hj
simpa only [dist_star_star] using hhor i hi j hj
have hv : βˆ€ i ≀ m, βˆ€ j < n, dist (star (f i j)) (star (f i (j+1))) ≀ D := by
intro i hi j hj
simpa only [dist_star_star] using hver i hi j hj
have hb : βˆ€ i ≀ m, (star (f i 0)).im < 0 := by
intro i hi
simpa only [Complex.star_def,Complex.conj_im,neg_lt_zero] using hbottom i hi
have ht : βˆ€ i ≀ m, 0 ≀ (star (f i n)).im := by
intro i hi
simpa only [Complex.star_def,Complex.conj_im,neg_nonneg] using htop i hi
obtain ⟨i,hi,j,hj,hz⟩ := discrete_rectangle_approx_zero (fun i j => star (f i j)) m n
hD hh hv hb ht (by simpa only [Complex.star_def,Complex.conj_re] using hleft)
(by simpa only [Complex.star_def,Complex.conj_re] using hright)
exact ⟨i,hi,j,hj,by simpa only [norm_star] using hz⟩
theorem path_difference_cover (v u : β„• β†’ β„‚) {n m : β„•} (hn : 0 < n) (hm : 0 < m)
(a b : β„‚) (hv0 : v 0 = 0) (hu0 : u 0 = 0) (hvn : v n = a) (hum : u m = b)
(ha : 0 < a.re) (hai : a.im = 0) (hb : 0 < b.im) {D : ℝ} (hD : 0 ≀ D)
(hv : βˆ€ i < n, dist (v i) (v (i+1)) ≀ D)
(hu : βˆ€ j < m, dist (u j) (u (j+1)) ≀ D) (w : β„‚) :
βˆƒ i < n, βˆƒ j < m, βˆƒ s t : β„€,
β€–v i-u j+(s:β„‚)*a+(t:β„‚)*b-wβ€– ≀ 4*D := by
let H : ℝ := (βˆ‘ i ∈ Finset.range n, β€–v iβ€–) + (βˆ‘ j ∈ Finset.range m, β€–u jβ€–) + β€–wβ€–
have hH : 0 ≀ H := by dsimp [H]; positivity
have hvb (i : β„•) : β€–v (i%n)β€– ≀ βˆ‘ k ∈ Finset.range n, β€–v kβ€– :=
Finset.single_le_sum (fun k _ => norm_nonneg (v k)) (Finset.mem_range.mpr (Nat.mod_lt i hn))
have hub (j : β„•) : β€–u (j%m)β€– ≀ βˆ‘ k ∈ Finset.range m, β€–u kβ€– :=
Finset.single_le_sum (fun k _ => norm_nonneg (u k)) (Finset.mem_range.mpr (Nat.mod_lt j hm))
have herr (i j : β„•) : β€–v (i%n)-u (j%m)-wβ€– ≀ H := by
have h1 := norm_sub_le (v (i%n)) (u (j%m))
have h2 := norm_sub_le (v (i%n)-u (j%m)) w
dsimp [H]
linarith [hvb i,hub j]
have hrerr (i j : β„•) : -H ≀ (v (i%n)-u (j%m)-w).re ∧
(v (i%n)-u (j%m)-w).re ≀ H :=
abs_le.mp ((Complex.abs_re_le_norm _).trans (herr i j))
have hierr (i j : β„•) : -H ≀ (v (i%n)-u (j%m)-w).im ∧
(v (i%n)-u (j%m)-w).im ≀ H :=
abs_le.mp ((Complex.abs_im_le_norm _).trans (herr i j))
obtain ⟨N,hN⟩ := exists_nat_gt (H/b.im)
have hN' : H < (N:ℝ)*b.im := (div_lt_iffβ‚€ hb).mp hN
obtain ⟨M,hM⟩ := exists_nat_gt ((H+(N:ℝ)*|b.re|)/a.re)
have hM' : H+(N:ℝ)*|b.re| < (M:ℝ)*a.re := (div_lt_iffβ‚€ ha).mp hM
let f (i j : β„•) : β„‚ := repeatPath v n a i-repeatPath u m b j-(M:β„‚)*a+(N:β„‚)*b-w
have hf (i j : β„•) : f i j = v (i%n)-u (j%m)-w +
(((i/n:β„•):β„‚)-(M:β„‚))*a + ((N:β„‚)-((j/m:β„•):β„‚))*b := by
dsimp [f,repeatPath]
ring
have hfr (i j : β„•) : (f i j).re = (v (i%n)-u (j%m)-w).re +
(((i/n:β„•):ℝ)-(M:ℝ))*a.re + ((N:ℝ)-((j/m:β„•):ℝ))*b.re := by
rw [hf]
simp
have hfi (i j : β„•) : (f i j).im = (v (i%n)-u (j%m)-w).im +
((N:ℝ)-((j/m:β„•):ℝ))*b.im := by
rw [hf]
simp [hai]
have hhor (i : β„•) (_ : i < 2*M*n) (j : β„•) (_ : j ≀ 2*N*m) :
dist (f i j) (f (i+1) j) ≀ D := by
simpa only [f,dist_sub_right,dist_add_right] using repeatPath_step v hn a hv0 hvn hv i
have hver (i : β„•) (_ : i ≀ 2*M*n) (j : β„•) (_ : j < 2*N*m) :
dist (f i j) (f i (j+1)) ≀ D := by
simpa only [f,dist_sub_right,dist_add_right,dist_sub_left] using repeatPath_step u hm b hu0 hum hu j
have hbottom (i : β„•) (_ : i ≀ 2*M*n) : 0 < (f i 0).im := by
rw [hfi]
simpa only [Nat.zero_div,Nat.cast_zero,sub_zero] using (show
0 < (v (i%n)-u (0%m)-w).im+(N:ℝ)*b.im by linarith [(hierr i 0).1])
have htop (i : β„•) (_ : i ≀ 2*M*n) : (f i (2*N*m)).im ≀ 0 := by
rw [hfi,Nat.mul_div_cancel _ hm,Nat.cast_mul,Nat.cast_ofNat]
linarith [(hierr i (2*N*m)).2]
have hcoef (j : β„•) (hj : j ≀ 2*N*m) :
|(N:ℝ)-((j/m:β„•):ℝ)| ≀ N := by
have hq : j/m ≀ 2*N := (Nat.div_le_div_right hj).trans_eq (Nat.mul_div_cancel _ hm)
have hqr : ((j/m:β„•):ℝ) ≀ 2*(N:ℝ) := by exact_mod_cast hq
exact abs_le.mpr ⟨by linarith,by linarith [Nat.cast_nonneg (Ξ± := ℝ) (j/m)]⟩
have hprod (j : β„•) (hj : j ≀ 2*N*m) :
-(N:ℝ)*|b.re| ≀ ((N:ℝ)-((j/m:β„•):ℝ))*b.re ∧
((N:ℝ)-((j/m:β„•):ℝ))*b.re ≀ (N:ℝ)*|b.re| := by
have hh : |((N:ℝ)-((j/m:β„•):ℝ))*b.re| ≀ (N:ℝ)*|b.re| := by
rw [abs_mul]
exact mul_le_mul_of_nonneg_right (hcoef j hj) (abs_nonneg _)
simpa only [neg_mul] using abs_le.mp hh
have hleft (j : β„•) (hj : j ≀ 2*N*m) : (f 0 j).re < 0 := by
rw [hfr]
simp only [Nat.zero_div,Nat.cast_zero,zero_sub]
nlinarith [(hrerr 0 j).2,(hprod j hj).2]
have hright (j : β„•) (hj : j ≀ 2*N*m) : 0 ≀ (f (2*M*n) j).re := by
rw [hfr,Nat.mul_div_cancel _ hn,Nat.cast_mul,Nat.cast_ofNat]
nlinarith [(hrerr (2*M*n) j).1,(hprod j hj).1]
obtain ⟨i,_,j,_,hz⟩ := discrete_rectangle_approx_zero_rev f (2*M*n) (2*N*m)
hD hhor hver hbottom htop hleft hright
refine ⟨i%n,Nat.mod_lt i hn,j%m,Nat.mod_lt j hm,((i/n:β„•):β„€)-(M:β„€),(N:β„€)-((j/m:β„•):β„€),?_⟩
convert hz using 1
rw [hf]
simp only [Int.cast_sub,Int.cast_natCast]
congr 1
ring
theorem path_difference_cover_horizontal (v u : β„• β†’ β„‚) {n m : β„•} (hn : 0 < n) (hm : 0 < m)
(a b : β„‚) (hv0 : v 0 = 0) (hu0 : u 0 = 0) (hvn : v n = a) (hum : u m = b)
(ha : 0 < a.re) (hai : a.im = 0) (hb : b.im β‰  0) {D : ℝ} (hD : 0 ≀ D)
(hv : βˆ€ i < n, dist (v i) (v (i+1)) ≀ D)
(hu : βˆ€ j < m, dist (u j) (u (j+1)) ≀ D) (w : β„‚) :
βˆƒ i < n, βˆƒ j < m, βˆƒ s t : β„€,
β€–v i-u j+(s:β„‚)*a+(t:β„‚)*b-wβ€– ≀ 4*D := by
rcases hb.lt_or_gt with hb | hb
Β· have hzv : (fun i => star (v i)) 0 = 0 := by simp [hv0]
have hzu : (fun i => star (u i)) 0 = 0 := by simp [hu0]
have hva : (fun i => star (v i)) n = star a := by simp only [hvn]
have hub : (fun i => star (u i)) m = star b := by simp only [hum]
have hbr : 0 < (star b).im := by simpa using hb
have hv' : βˆ€ i < n, dist (star (v i)) (star (v (i+1))) ≀ D := by simpa using hv
have hu' : βˆ€ j < m, dist (star (u j)) (star (u (j+1))) ≀ D := by simpa using hu
obtain ⟨i,hi,j,hj,s,t,hh⟩ := path_difference_cover (fun i => star (v i))
(fun j => star (u j)) hn hm (star a) (star b) hzv hzu hva hub
(by simpa using ha) (by simpa using hai) hbr hD hv' hu' (star w)
refine ⟨i,hi,j,hj,s,t,?_⟩
have he : β€–star (v i-u j+(s:β„‚)*a+(t:β„‚)*b-w)β€– ≀ 4*D := by
simpa only [star_sub,star_add,star_mul,star_intCast,mul_comm] using hh
simpa only [norm_star] using he
Β· exact path_difference_cover v u hn hm a b hv0 hu0 hvn hum ha hai hb hD hv hu w
theorem path_difference_cover_general (v u : β„• β†’ β„‚) {n m : β„•} (hn : 0 < n) (hm : 0 < m)
(a b : β„‚) (hv0 : v 0 = 0) (hu0 : u 0 = 0) (hvn : v n = a) (hum : u m = b)
(ha : a β‰  0) (hb : (b/a).im β‰  0) {D : ℝ} (hD : 0 ≀ D)
(hv : βˆ€ i < n, dist (v i) (v (i+1)) ≀ D)
(hu : βˆ€ j < m, dist (u j) (u (j+1)) ≀ D) (w : β„‚) :
βˆƒ i < n, βˆƒ j < m, βˆƒ s t : β„€,
β€–v i-u j+(s:β„‚)*a+(t:β„‚)*b-wβ€– ≀ 4*D := by
have hna : 0 < β€–aβ€– := norm_pos_iff.mpr ha
have hv' : βˆ€ i < n, dist (v i/a) (v (i+1)/a) ≀ D/β€–aβ€– := by
intro i hi
simp only [dist_eq_norm,← sub_div,norm_div]
exact div_le_div_of_nonneg_right (by simpa only [dist_eq_norm] using hv i hi) hna.le
have hu' : βˆ€ j < m, dist (u j/a) (u (j+1)/a) ≀ D/β€–aβ€– := by
intro j hj
simp only [dist_eq_norm,← sub_div,norm_div]
exact div_le_div_of_nonneg_right (by simpa only [dist_eq_norm] using hu j hj) hna.le
obtain ⟨i,hi,j,hj,s,t,hh⟩ := path_difference_cover_horizontal (fun i => v i/a)
(fun j => u j/a) hn hm 1 (b/a) (by simp [hv0]) (by simp [hu0])
(by simp [hvn,ha]) (by simp [hum]) (by norm_num) (by simp) hb
(by positivity : 0 ≀ D/β€–aβ€–) hv' hu' (w/a)
refine ⟨i,hi,j,hj,s,t,?_⟩
have he : v i/a-u j/a+(s:β„‚)*1+(t:β„‚)*(b/a)-w/a =
(v i-u j+(s:β„‚)*a+(t:β„‚)*b-w)/a := by field_simp
rw [he,norm_div] at hh
have hh' := (div_le_iffβ‚€ hna).mp hh
calc
_ ≀ 4 * (D/β€–aβ€–) * β€–aβ€– := hh'
_ = 4*D := by field_simp
end
def latticeCoords : GaussianInt ≃ₗ[β„€] (Fin 2 β†’ β„€) where
toFun z := ![z.re,z.im]
invFun v := ⟨v 0,v 1⟩
left_inv z := by ext <;> rfl
right_inv v := by ext i; fin_cases i <;> rfl
map_add' z w := by ext i; fin_cases i <;> rfl
map_smul' c z := by
ext i
fin_cases i <;> simp [zsmul_eq_mul]
noncomputable def latticeBasis : Module.Basis (Fin 2) β„€ GaussianInt :=
(Pi.basisFun β„€ (Fin 2)).map latticeCoords.symm
noncomputable instance : Module.Free β„€ GaussianInt := Module.Free.of_basis latticeBasis
instance : Module.Finite β„€ GaussianInt := Module.Finite.of_basis latticeBasis
noncomputable def twoVectorMap (a b : GaussianInt) : GaussianInt β†’β‚—[β„€] GaussianInt :=
Matrix.toLin latticeBasis latticeBasis !![a.re,b.re; a.im,b.im]
@[simp] theorem twoVectorMap_apply (a b z : GaussianInt) :
twoVectorMap a b z = z.re β€’ a + z.im β€’ b := by
apply latticeCoords.injective
change latticeBasis.repr (twoVectorMap a b z) = latticeCoords (z.re β€’ a+z.im β€’ b)
rw [twoVectorMap,Matrix.toLin_apply]
simp only [map_sum,map_smul,Module.Basis.repr_self]
ext i
fin_cases i <;> simp [latticeCoords,latticeBasis,zsmul_eq_mul,Fin.sum_univ_two,Pi.basisFun_repr,Matrix.vecHead,Matrix.vecTail] <;> ring
@[simp] theorem twoVectorMap_det (a b : GaussianInt) :
LinearMap.det (twoVectorMap a b) = a.re*b.im-a.im*b.re := by
rw [← LinearMap.det_toMatrix latticeBasis,twoVectorMap,LinearMap.toMatrix_toLin]
simp [Matrix.det_fin_two,mul_comm]
theorem twoVectorMap_injective {a b : GaussianInt} (h : a.re*b.im-a.im*b.re β‰  0) :
Function.Injective (twoVectorMap a b) := by
intro z w hzw
have h1 := congrArg Zsqrtd.re hzw
have h2 := congrArg Zsqrtd.im hzw
simp only [twoVectorMap_apply,zsmul_eq_mul,Zsqrtd.re_add,Zsqrtd.im_add,
Zsqrtd.re_smul,Zsqrtd.im_smul] at h1 h2
have hr : (a.re*b.im-a.im*b.re)*(z.re-w.re) = 0 := by linear_combination b.im*h1-b.re*h2
have hi : (a.re*b.im-a.im*b.re)*(z.im-w.im) = 0 := by linear_combination a.re*h2-a.im*h1
ext
Β· exact sub_eq_zero.mp ((mul_eq_zero.mp hr).resolve_left h)
Β· exact sub_eq_zero.mp ((mul_eq_zero.mp hi).resolve_left h)
theorem twoVectorMap_quotient_card {a b : GaussianInt} (h : a.re*b.im-a.im*b.re β‰  0) :
Nat.card (GaussianInt β§Έ (twoVectorMap a b).range) = (a.re*b.im-a.im*b.re).natAbs := by
have hh := Submodule.natAbs_det_equiv (twoVectorMap a b).range
(LinearEquiv.ofInjective (twoVectorMap a b) (twoVectorMap_injective h))
have he : (twoVectorMap a b).range.subtype βˆ˜β‚—
AddMonoidHom.toIntLinearMap ((LinearEquiv.ofInjective (twoVectorMap a b)
(twoVectorMap_injective h)) : GaussianInt β†’+ (twoVectorMap a b).range) = twoVectorMap a b := by
ext z <;> rfl
rw [he,twoVectorMap_det] at hh
exact hh.symm
noncomputable def latticeBall (D : ℝ) : Finset GaussianInt :=
(finite_dist_le 0 D).toFinset
@[simp] theorem mem_latticeBall (D : ℝ) (z : GaussianInt) :
z ∈ latticeBall D ↔ β€–(z:β„‚)β€– ≀ D := by simp [latticeBall]
theorem complex_div_im_ne_zero {a b : GaussianInt} (h : a.re*b.im-a.im*b.re β‰  0) :
((b:β„‚)/(a:β„‚)).im β‰  0 := by
have ha : a β‰  0 := by intro hh; simp [hh] at h
have hac : (a:β„‚) β‰  0 := by simpa using ha
have hn : Complex.normSq (a:β„‚) β‰  0 := (Complex.normSq_pos.mpr hac).ne'
rw [Complex.div_im,← sub_div]
intro hh
have hh' := (div_eq_zero_iff.mp hh).resolve_right hn
have hz : (a.re:ℝ)*(b.im:ℝ)-(a.im:ℝ)*(b.re:ℝ) = 0 := by
simpa only [← GaussianInt.intCast_re,← GaussianInt.intCast_im,mul_comm] using hh'
apply h
exact_mod_cast hz
theorem lattice_path_difference_cover (v u : β„• β†’ GaussianInt) {n m : β„•}
(hn : 0 < n) (hm : 0 < m) (a b : GaussianInt)
(hv0 : v 0 = 0) (hu0 : u 0 = 0) (hvn : v n = a) (hum : u m = b)
(hab : a.re*b.im-a.im*b.re β‰  0) {D : ℝ} (hD : 0 ≀ D)
(hv : βˆ€ i < n, dist (v i:β„‚) (v (i+1):β„‚) ≀ D)
(hu : βˆ€ j < m, dist (u j:β„‚) (u (j+1):β„‚) ≀ D) (w : GaussianInt) :
βˆƒ i < n, βˆƒ j < m, βˆƒ s t : β„€,
β€–((v i-u j+(s:GaussianInt)*a+(t:GaussianInt)*b-w:GaussianInt):β„‚)β€– ≀ 4*D := by
have ha : a β‰  0 := by intro hh; simp [hh] at hab
obtain ⟨i,hi,j,hj,s,t,hh⟩ := path_difference_cover_general
(fun i => (v i:β„‚)) (fun j => (u j:β„‚)) hn hm (a:β„‚) (b:β„‚)
(by simp [hv0]) (by simp [hu0]) (by simp [hvn]) (by simp [hum])
(by simpa using ha) (complex_div_im_ne_zero hab) hD hv hu (w:β„‚)
exact ⟨i,hi,j,hj,s,t,by simpa using hh⟩
theorem lattice_path_difference_card (v u : β„• β†’ GaussianInt) {n m : β„•}
(hn : 0 < n) (hm : 0 < m) (a b : GaussianInt)
(hv0 : v 0 = 0) (hu0 : u 0 = 0) (hvn : v n = a) (hum : u m = b)
(hab : a.re*b.im-a.im*b.re β‰  0) {D : ℝ} (hD : 0 ≀ D)
(hv : βˆ€ i < n, dist (v i:β„‚) (v (i+1):β„‚) ≀ D)
(hu : βˆ€ j < m, dist (u j:β„‚) (u (j+1):β„‚) ≀ D)
(E : Finset GaussianInt) (hE : βˆ€ i < n, βˆ€ j < m, v i-u j ∈ E) :
(a.re*b.im-a.im*b.re).natAbs ≀ E.card*(latticeBall (4*D)).card := by
classical
let Q := (twoVectorMap a b).range
have hcard : Nat.card (GaussianInt β§Έ Q) = (a.re*b.im-a.im*b.re).natAbs :=
twoVectorMap_quotient_card hab
have : Finite (GaussianInt β§Έ Q) := Nat.finite_of_card_ne_zero (by rw [hcard]; simpa using hab)
let : Fintype (GaussianInt β§Έ Q) := Fintype.ofFinite _
let F : E Γ— (latticeBall (4*D)) β†’ GaussianInt β§Έ Q :=
fun x => Q.mkQ (x.1.val+x.2.val)
have hf : Function.Surjective F := by
intro q
obtain ⟨w,rfl⟩ := Q.mkQ_surjective q
obtain ⟨i,hi,j,hj,s,t,hh⟩ := lattice_path_difference_cover v u hn hm a b hv0 hu0 hvn hum hab hD hv hu w
let r : GaussianInt := -(v i-u j+(s:GaussianInt)*a+(t:GaussianInt)*b-w)
have hr : r ∈ latticeBall (4*D) := by
rw [mem_latticeBall]
dsimp only [r]
rw [map_neg,norm_neg]
exact hh
refine ⟨(⟨v i-u j,hE i hi j hj⟩,⟨r,hr⟩),?_⟩
apply (Submodule.Quotient.eq Q).mpr
change (v i-u j)+r-w ∈ (twoVectorMap a b).range
refine ⟨⟨-s,-t⟩,?_⟩
rw [twoVectorMap_apply]
change (-s) β€’ a + (-t) β€’ b = _
simp only [zsmul_eq_mul,Int.cast_neg]
dsimp [r]
ring
have hh := Fintype.card_le_of_surjective F hf
have hQ : Fintype.card (GaussianInt β§Έ Q) = (a.re*b.im-a.im*b.re).natAbs := by
rw [← Nat.card_eq_fintype_card,hcard]
simpa only [Fintype.card_prod,Fintype.card_coe,hQ] using hh
theorem lattice_norm_ge_one {z : GaussianInt} (hz : z β‰  0) : 1 ≀ β€–(z:β„‚)β€– := by
have hi : 1 ≀ z.norm := by have := GaussianInt.norm_pos.mpr hz; omega
have hr : (1:ℝ) ≀ (z.norm:ℝ) := by exact_mod_cast hi
rw [GaussianInt.intCast_real_norm,Complex.normSq_eq_norm_sq] at hr
nlinarith [norm_nonneg (z:β„‚)]
theorem lattice_dist_ge_one {z w : GaussianInt} (h : z β‰  w) :
1 ≀ dist (z:β„‚) (w:β„‚) := by
simpa only [map_sub,dist_eq_norm] using lattice_norm_ge_one (sub_ne_zero.mpr h)
noncomputable def latticePixel (e c : β„‚) (z : GaussianInt) : β„€ Γ— β„€ :=
(⌊2*(e*((z:β„‚)-c)).reβŒ‹,⌊2*(e*((z:β„‚)-c)).imβŒ‹)
theorem abs_sub_lt_half_of_pixel_eq {x y : ℝ} (h : ⌊2*xβŒ‹ = ⌊2*yβŒ‹) : |x-y| < 1/2 := by
have h1 := Int.floor_le (2*x)
have h2 := Int.lt_floor_add_one (2*x)
have h3 := Int.floor_le (2*y)
have h4 := Int.lt_floor_add_one (2*y)
rw [h] at h1 h2
exact abs_lt.mpr ⟨by linarith,by linarith⟩
theorem latticePixel_injective {e : β„‚} (he : β€–eβ€– = 1) (c : β„‚) :
Function.Injective (latticePixel e c) := by
intro z w h
have hr := abs_sub_lt_half_of_pixel_eq (congrArg Prod.fst h)
have hi := abs_sub_lt_half_of_pixel_eq (congrArg Prod.snd h)
have hn : β€–e*((z:β„‚)-c)-e*((w:β„‚)-c)β€– < 1 := by
have hh := Complex.norm_le_abs_re_add_abs_im (e*((z:β„‚)-c)-e*((w:β„‚)-c))
simp only [Complex.sub_re,Complex.sub_im] at hh
change |(e*((z:β„‚)-c)).re-(e*((w:β„‚)-c)).re| < 1/2 at hr
change |(e*((z:β„‚)-c)).im-(e*((w:β„‚)-c)).im| < 1/2 at hi
linarith
have heq : e*((z:β„‚)-c)-e*((w:β„‚)-c) = e*((z:β„‚)-(w:β„‚)) := by ring
rw [heq,norm_mul,he,one_mul] at hn
by_contra hne
exact (not_lt_of_ge (lattice_dist_ge_one hne)) (by simpa only [dist_eq_norm] using hn)
theorem lattice_rectangle_card (E : Finset GaussianInt) {e c : β„‚} (he : β€–eβ€– = 1)
{R W : ℝ} (hR : 1 ≀ R) (hW : 1 ≀ W)
(hE : βˆ€ z ∈ E, 0 ≀ (e*((z:β„‚)-c)).re ∧ (e*((z:β„‚)-c)).re ≀ R ∧
|(e*((z:β„‚)-c)).im| ≀ W) :
(E.card:ℝ) ≀ 18*R*W := by
classical
let B : Finset (β„€ Γ— β„€) := Finset.Icc 0 ⌊2*RβŒ‹ Γ—Λ’ Finset.Icc ⌊-2*WβŒ‹ ⌊2*WβŒ‹
have hb : E.image (latticePixel e c) βŠ† B := by
intro x hx
obtain ⟨z,hz,rfl⟩ := Finset.mem_image.mp hx
obtain ⟨hz0,hzR,hzW⟩ := hE z hz
have hzw := abs_le.mp hzW
simp only [B,Finset.mem_product,Finset.mem_Icc,latticePixel]
refine ⟨⟨?_,Int.floor_mono (by linarith)⟩,
⟨Int.floor_mono (by linarith),Int.floor_mono (by linarith)⟩⟩
exact Int.floor_nonneg.mpr (by linarith)
have hcard : E.card ≀ B.card := by
rw [← Finset.card_image_iff.mpr (latticePixel_injective he c).injOn]
exact Finset.card_le_card hb
have hR0 : (0:β„€) ≀ ⌊2*RβŒ‹+1 := by
have hh : (0:β„€) ≀ ⌊2*RβŒ‹ := Int.floor_nonneg.mpr (by linarith)
omega
have hW0 : ⌊-2*WβŒ‹ ≀ ⌊2*WβŒ‹+1 := by
have hh : ⌊-2*WβŒ‹ ≀ ⌊2*WβŒ‹ := Int.floor_mono (by linarith)
omega
have hcr : ((Finset.Icc (0:β„€) ⌊2*RβŒ‹).card:ℝ) = (⌊2*RβŒ‹:ℝ)+1 := by
have h := Int.card_Icc_of_le 0 ⌊2*RβŒ‹ hR0
simp only [sub_zero] at h
exact_mod_cast h
have hcw : ((Finset.Icc ⌊-2*WβŒ‹ ⌊2*WβŒ‹).card:ℝ) = (⌊2*WβŒ‹:ℝ)+1-(⌊-2*WβŒ‹:ℝ) := by
exact_mod_cast Int.card_Icc_of_le ⌊-2*WβŒ‹ ⌊2*WβŒ‹ hW0
have hrb : ((Finset.Icc (0:β„€) ⌊2*RβŒ‹).card:ℝ) ≀ 3*R := by
rw [hcr]
linarith [Int.floor_le (2*R)]
have hwb : ((Finset.Icc ⌊-2*WβŒ‹ ⌊2*WβŒ‹).card:ℝ) ≀ 6*W := by
rw [hcw]
linarith [Int.floor_le (2*W),Int.lt_floor_add_one (-2*W)]
have hh : (E.card:ℝ) ≀ ((Finset.Icc (0:β„€) ⌊2*RβŒ‹).card:ℝ)*
((Finset.Icc ⌊-2*WβŒ‹ ⌊2*WβŒ‹).card:ℝ) := by
dsimp only [B] at hcard
rw [Finset.card_product] at hcard
exact_mod_cast hcard
calc
(E.card:ℝ) ≀ _ := hh
_ ≀ (3*R)*(6*W) := mul_le_mul hrb hwb (Nat.cast_nonneg _) (by linarith)
_ = 18*R*W := by ring
end GaussianMoat
end OAI