math / data /lean /OAI /NumberTheory /GaussianMoat /DifferenceSampling.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
11.4 kB
import OAI.NumberTheory.GaussianMoat.WalkGeometry
namespace OAI
namespace GaussianMoat
noncomputable def walkSegment (z : β„• β†’ GaussianInt) (n : β„•) : Finset GaussianInt :=
(Finset.range (n+1)).image z
noncomputable def differences (E : Finset GaussianInt) : Finset GaussianInt :=
(E Γ—Λ’ E).image (fun p => p.1-p.2)
@[simp] theorem mem_walkSegment (z : β„• β†’ GaussianInt) (n : β„•) (w : GaussianInt) :
w ∈ walkSegment z n ↔ βˆƒ i ≀ n, z i = w := by
simp [walkSegment,Finset.mem_image]
@[simp] theorem mem_differences (E : Finset GaussianInt) (w : GaussianInt) :
w ∈ differences E ↔ βˆƒ a ∈ E, βˆƒ b ∈ E, a-b = w := by
simp [differences,Finset.mem_image,Prod.exists,and_assoc]
theorem card_le_card_differences {E : Finset GaussianInt} (hE : E.Nonempty) :
E.card ≀ (differences E).card := by
classical
obtain ⟨a,ha⟩ := hE
have hs : E.image (fun b => b-a) βŠ† differences E := by
intro w hw
obtain ⟨b,hb,rfl⟩ := Finset.mem_image.mp hw
exact mem_differences E (b-a) |>.mpr ⟨b,hb,a,ha,rfl⟩
have hi : Function.Injective (fun b : GaussianInt => b-a) := sub_left_injective
rw [← Finset.card_image_of_injective E hi]
exact Finset.card_le_card hs
theorem walkSegment_dist_le (z : β„• β†’ GaussianInt) {n : β„•} {D : ℝ} (hD : 0 ≀ D)
(hz : βˆ€ i < n, dist (z i:β„‚) (z (i+1):β„‚) ≀ D) {i j : β„•} (hi : i ≀ n) (hj : j ≀ n) :
dist (z i:β„‚) (z j:β„‚) ≀ D*n := by
wlog hij : i ≀ j generalizing i j
Β· rw [dist_comm]
exact this hj hi (by omega)
have h := dist_le_Ico_sum_of_dist_le (f := fun k => (z k:β„‚)) hij
(d := fun _ => D) (fun {k} _ hk => hz k (lt_of_lt_of_le hk hj))
simp only [Finset.sum_const,Nat.card_Ico,nsmul_eq_mul] at h
calc
dist (z i:β„‚) (z j:β„‚) ≀ (j-i:β„•)*D := h
_ ≀ (n:ℝ)*D := mul_le_mul_of_nonneg_right (by exact_mod_cast (show j-i ≀ n by omega)) hD
_ = D*n := mul_comm _ _
def joinedPath (z : β„• β†’ GaussianInt) (t : β„•) (i : β„•) : GaussianInt :=
z (Nat.dist t i)-z t
@[simp] theorem joinedPath_zero (z : β„• β†’ GaussianInt) (t : β„•) : joinedPath z t 0 = 0 := by
simp [joinedPath,Nat.dist]
@[simp] theorem joinedPath_end (z : β„• β†’ GaussianInt) (t s : β„•) :
joinedPath z t (t+s) = z s-z t := by simp [joinedPath,Nat.dist]
theorem joinedPath_step (z : β„• β†’ GaussianInt) {n t s : β„•} (ht : t ≀ n) (hs : s ≀ n)
{D : ℝ} (hz : βˆ€ i < n, dist (z i:β„‚) (z (i+1):β„‚) ≀ D) :
βˆ€ i < t+s, dist (joinedPath z t i:β„‚) (joinedPath z t (i+1):β„‚) ≀ D := by
intro i hi
simp only [joinedPath,map_sub,dist_sub_right]
by_cases hit : i < t
Β· have he : Nat.dist t i = Nat.dist t (i+1)+1 := by simp only [Nat.dist]; omega
rw [he,dist_comm]
exact hz _ (by simp only [Nat.dist]; omega)
Β· have he : Nat.dist t (i+1) = Nat.dist t i+1 := by simp only [Nat.dist]; omega
rw [he]
exact hz _ (by simp only [Nat.dist]; omega)
theorem differences_card_of_triangle (z : β„• β†’ GaussianInt) {n t s u : β„•}
(ht : t ≀ n) (hs : s ≀ n) (hu : u ≀ n) {D : ℝ} (hD : 0 ≀ D)
(hz : βˆ€ i < n, dist (z i:β„‚) (z (i+1):β„‚) ≀ D)
(hab : (z s-z t).re*(z u-z t).im-(z s-z t).im*(z u-z t).re β‰  0) :
((z s-z t).re*(z u-z t).im-(z s-z t).im*(z u-z t).re).natAbs ≀
(differences (walkSegment z n)).card * (latticeBall (4*D)).card := by
have hts : 0 < t+s := by
by_contra hh
have ht0 : t=0 := by omega
have hs0 : s=0 := by omega
simp [ht0,hs0] at hab
have htu : 0 < t+u := by
by_contra hh
have ht0 : t=0 := by omega
have hu0 : u=0 := by omega
simp [ht0,hu0] at hab
apply lattice_path_difference_card (joinedPath z t) (joinedPath z t) hts htu
(z s-z t) (z u-z t) (joinedPath_zero z t) (joinedPath_zero z t)
(joinedPath_end z t s) (joinedPath_end z t u) hab hD
(joinedPath_step z ht hs hz) (joinedPath_step z ht hu hz)
intro i hi j hj
apply (mem_differences _ _).mpr
refine ⟨z (Nat.dist t i),(mem_walkSegment _ _ _).mpr ⟨_,?_,rfl⟩,
z (Nat.dist t j),(mem_walkSegment _ _ _).mpr ⟨_,?_,rfl⟩,?_⟩
Β· simp only [Nat.dist]; omega
Β· simp only [Nat.dist]; omega
Β· simp only [joinedPath]; ring
noncomputable def diameterRotation (a : β„‚) : β„‚ := star a / (β€–aβ€–:β„‚)
theorem diameterRotation_norm {a : β„‚} (ha : a β‰  0) : β€–diameterRotation aβ€– = 1 := by
simp [diameterRotation,Complex.norm_real,norm_ne_zero_iff.mpr ha]
theorem diameterRotation_mul (a : β„‚) : diameterRotation a*a = (β€–aβ€–:β„‚) := by
by_cases ha : a=0
Β· simp [ha,diameterRotation]
have hn : (β€–aβ€–:β„‚) β‰  0 := by exact_mod_cast norm_ne_zero_iff.mpr ha
have hm : star a*a = (β€–aβ€–:β„‚)^2 := Complex.conj_mul' a
rw [diameterRotation,div_mul_eq_mul_div,hm]
field_simp
theorem diameter_rectangle {a v : β„‚} (ha : a β‰  0) (hv : β€–vβ€– ≀ β€–aβ€–)
(hva : β€–v-aβ€– ≀ β€–aβ€–) :
0 ≀ (diameterRotation a*v).re ∧ (diameterRotation a*v).re ≀ β€–aβ€– ∧
|(diameterRotation a*v).im| ≀ β€–aβ€– := by
let u := diameterRotation a*v
have hn : β€–uβ€– ≀ β€–aβ€– := by simpa [u,norm_mul,diameterRotation_norm ha] using hv
have hna : β€–u-(β€–aβ€–:β„‚)β€– ≀ β€–aβ€– := by
rw [← diameterRotation_mul a,← mul_sub]
simpa [norm_mul,diameterRotation_norm ha] using hva
have hr := Complex.abs_re_le_norm u
have hi := Complex.abs_im_le_norm u
have hra := Complex.abs_re_le_norm (u-(β€–aβ€–:β„‚))
simp only [Complex.sub_re,Complex.ofReal_re] at hra
have hl := (abs_le.mp (hra.trans hna)).1
exact ⟨by dsimp [u] at *; linarith, (le_abs_self u.re).trans (hr.trans hn), hi.trans hn⟩
theorem diameter_height (a b : GaussianInt) :
|((a.re*b.im-a.im*b.re:β„€):ℝ)| =
β€–(a:β„‚)β€–*|(diameterRotation (a:β„‚)*(b:β„‚)).im| := by
by_cases ha : a=0
Β· simp [ha,diameterRotation]
have hna : β€–(a:β„‚)β€– β‰  0 := norm_ne_zero_iff.mpr (by simpa using ha)
have he : (diameterRotation (a:β„‚)*(b:β„‚)).im =
((a.re:ℝ)*(b.im:ℝ)-(a.im:ℝ)*(b.re:ℝ))/β€–(a:β„‚)β€– := by
simp only [diameterRotation,div_mul_eq_mul_div,Complex.div_ofReal_im,
Complex.mul_im,Complex.star_def,Complex.conj_re,Complex.conj_im,
← GaussianInt.intCast_re,← GaussianInt.intCast_im]
ring
rw [he,abs_div,abs_of_nonneg (norm_nonneg (a:β„‚))]
push_cast
field_simp
theorem walk_many_differences (z : β„• β†’ GaussianInt) {n : β„•} (hn : 1 ≀ n)
(hinj : Function.Injective z) {D : ℝ} (hD : 1 ≀ D)
(hz : βˆ€ i < n, dist (z i:β„‚) (z (i+1):β„‚) ≀ D) :
βˆƒ (R W : ℝ) (e c : β„‚), 1 ≀ W ∧ W ≀ R ∧ β€–eβ€– = 1 ∧
(βˆ€ p ∈ walkSegment z n, 0 ≀ (e*((p:β„‚)-c)).re ∧
(e*((p:β„‚)-c)).re ≀ R ∧ |(e*((p:β„‚)-c)).im| ≀ W) ∧
(n:ℝ) ≀ 18*(R*W) ∧ R*W ≀ D^2*(n:ℝ)^2 ∧
R*W ≀ max D ((latticeBall (4*D)).card:ℝ)*(differences (walkSegment z n)).card := by
classical
let I := Finset.range (n+1)
have hI : I.Nonempty := ⟨0,by simp [I]⟩
obtain ⟨⟨t,s⟩,hts,hmax⟩ := Finset.exists_max_image (I Γ—Λ’ I)
(fun q => dist (z q.1:β„‚) (z q.2:β„‚)) (hI.product hI)
have ht : t ≀ n := by simpa [I] using (Finset.mem_product.mp hts).1
have hs : s ≀ n := by simpa [I] using (Finset.mem_product.mp hts).2
let a := z s-z t
let R := β€–(a:β„‚)β€–
have hdiam : βˆ€ i ≀ n, βˆ€ j ≀ n, dist (z i:β„‚) (z j:β„‚) ≀ R := by
intro i hi j hj
have h := hmax (i,j) (by simp [I,hi,hj])
simpa [R,a,map_sub,← dist_eq_norm,dist_comm] using h
have hR : 1 ≀ R := (lattice_dist_ge_one (hinj.ne (by omega : 0 β‰  1))).trans (hdiam 0 (by omega) 1 hn)
have ha : a β‰  0 := by intro hh; simp [R,hh] at hR; linarith
have hac : (a:β„‚) β‰  0 := by simpa using ha
let e := diameterRotation (a:β„‚)
let c := (z t:β„‚)
have he : β€–eβ€– = 1 := diameterRotation_norm hac
have hr : βˆ€ i ≀ n, 0 ≀ (e*((z i:β„‚)-c)).re ∧
(e*((z i:β„‚)-c)).re ≀ R ∧ |(e*((z i:β„‚)-c)).im| ≀ R := by
intro i hi
have hv : β€–(z i:β„‚)-cβ€– ≀ β€–(a:β„‚)β€– := by simpa only [← dist_eq_norm] using hdiam i hi t ht
have hva : β€–((z i:β„‚)-c)-(a:β„‚)β€– ≀ β€–(a:β„‚)β€– := by
have hh : ((z i:β„‚)-c)-(a:β„‚) = (z i:β„‚)-(z s:β„‚) := by simp [a,c]
rw [hh,← dist_eq_norm]
exact hdiam i hi s hs
exact diameter_rectangle hac hv hva
obtain ⟨u,hu,humax⟩ := Finset.exists_max_image I
(fun i => |(e*((z i:β„‚)-c)).im|) hI
have hu' : u ≀ n := by simpa [I] using hu
let W := max 1 |(e*((z u:β„‚)-c)).im|
have hW : 1 ≀ W := le_max_left _ _
have hWR : W ≀ R := max_le hR (hr u hu').2.2
have hrect : βˆ€ p ∈ walkSegment z n, 0 ≀ (e*((p:β„‚)-c)).re ∧
(e*((p:β„‚)-c)).re ≀ R ∧ |(e*((p:β„‚)-c)).im| ≀ W := by
intro p hp
obtain ⟨i,hi,rfl⟩ := (mem_walkSegment _ _ _).mp hp
exact ⟨(hr i hi).1,(hr i hi).2.1,(humax i (by simpa [I] using hi)).trans (le_max_right _ _)⟩
have hcard : (walkSegment z n).card = n+1 := by
rw [walkSegment,Finset.card_image_of_injective _ hinj,Finset.card_range]
have hcount : (n:ℝ)+1 ≀ 18*R*W := by
have h := lattice_rectangle_card (walkSegment z n) he hR hW hrect
simpa only [hcard,Nat.cast_add,Nat.cast_one] using h
have hRD : R ≀ D*n := by
simpa only [R,a,map_sub,← dist_eq_norm] using walkSegment_dist_le z (by linarith) hz hs ht
have hAupper : R*W ≀ D^2*(n:ℝ)^2 := by
have hn0 : (0:ℝ) ≀ n := Nat.cast_nonneg _
have hm := mul_le_mul hRD hRD (by linarith : 0 ≀ R) (by positivity : 0 ≀ D*n)
have hw := mul_le_mul_of_nonneg_left hWR (by linarith : 0 ≀ R)
nlinarith
refine ⟨R,W,e,c,hW,hWR,he,hrect,by nlinarith,hAupper,?_⟩
have hdif := card_le_card_differences (E := walkSegment z n)
⟨z 0,(mem_walkSegment _ _ _).mpr ⟨0,by omega,rfl⟩⟩
have hcountdif : (n:ℝ)+1 ≀ ((differences (walkSegment z n)).card:ℝ) := by
rw [hcard] at hdif
exact_mod_cast hdif
by_cases hthin : |(e*((z u:β„‚)-c)).im| ≀ 1
Β· have hWeq : W = 1 := max_eq_left hthin
rw [hWeq,mul_one]
have hdn : D*(n:ℝ) ≀ D*((differences (walkSegment z n)).card:ℝ) :=
mul_le_mul_of_nonneg_left (by linarith) (by linarith)
exact (hRD.trans hdn).trans (mul_le_mul_of_nonneg_right (le_max_left _ _) (by positivity))
Β· have hWeq : W = |(e*((z u:β„‚)-c)).im| := max_eq_right (by linarith)
let b := z u-z t
have hdet : |((a.re*b.im-a.im*b.re:β„€):ℝ)| = R*W := by
rw [diameter_height,hWeq]
simp only [R,e,b,map_sub,c]
have hab : a.re*b.im-a.im*b.re β‰  0 := by
intro hh
rw [hh,Int.cast_zero,abs_zero] at hdet
have : 0 < R*W := mul_pos (by linarith) (by linarith)
linarith
have hcd := differences_card_of_triangle z ht hs hu' (by linarith) hz hab
have hcd' : |((a.re*b.im-a.im*b.re:β„€):ℝ)| ≀
((differences (walkSegment z n)).card:ℝ)*((latticeBall (4*D)).card:ℝ) := by
have hh : ((a.re*b.im-a.im*b.re).natAbs:ℝ) ≀
((differences (walkSegment z n)).card:ℝ)*((latticeBall (4*D)).card:ℝ) := by
exact_mod_cast hcd
simpa only [Nat.cast_natAbs,Int.cast_abs] using hh
rw [hdet] at hcd'
calc
R*W ≀ ((differences (walkSegment z n)).card:ℝ)*((latticeBall (4*D)).card:ℝ) := hcd'
_ = ((latticeBall (4*D)).card:ℝ)*((differences (walkSegment z n)).card:ℝ) := mul_comm _ _
_ ≀ _ := mul_le_mul_of_nonneg_right (le_max_right _ _) (by positivity)
end GaussianMoat
end OAI