Download data/lean/OAI/NumberTheory/GaussianMoat/WalkGeometry.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 28.4 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/NumberTheory/GaussianMoat/WalkGeometry.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/NumberTheory/GaussianMoat/WalkGeometry.lean
-
curl -L -o WalkGeometry.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/NumberTheory/GaussianMoat/WalkGeometry.lean
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 | |
| theorem repeatPath_zero (v : β β β) (n : β) (a : β) : | |
| repeatPath v n a 0 = v 0 := by simp [repeatPath] | |
| 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] | |
| 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 | |
| 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 | |
| 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 | |