math / data /lean /OAI /NumberTheory /GaussianMoat /SignedGeometry.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
33.1 kB
import OAI.NumberTheory.GaussianMoat.BooleanCube
namespace OAI
universe uIota
namespace GaussianMoat
open MeasureTheory ProbabilityTheory
open scoped NNReal
def cubeEquiv : (n : β„•) β†’ Cube n ≃ (Fin n β†’ Bool)
| 0 => {
toFun := fun _ i => Fin.elim0 i
invFun := fun _ => ()
left_inv := fun x => by cases x; rfl
right_inv := fun f => by funext i; exact Fin.elim0 i }
| n+1 => (Equiv.prodCongr (Equiv.refl Bool) (cubeEquiv n)).trans (Fin.consEquiv (fun _ => Bool))
noncomputable def cubeSignEquiv (ΞΉ : Type uIota) [Fintype ΞΉ] : Cube (Fintype.card ΞΉ) ≃ (ΞΉ β†’ Bool) :=
(cubeEquiv (Fintype.card ΞΉ)).trans
(Equiv.arrowCongr (Fintype.equivFin ΞΉ).symm (Equiv.refl Bool))
noncomputable def signDist {ΞΉ : Type uIota} [Fintype ΞΉ] (Οƒ Ο„ : ΞΉ β†’ Bool) : β„• := by
classical
exact βˆ‘ i, if Οƒ i = Ο„ i then 0 else 1
theorem cubeEquiv_dist (n : β„•) (x y : Cube n) :
signDist (cubeEquiv n x) (cubeEquiv n y) = cubeDist n x y := by
classical
induction n with
| zero => simp [signDist, cubeDist]
| succ n ih =>
obtain ⟨b,x⟩ := x
obtain ⟨c,y⟩ := y
simp only [signDist, Fin.sum_univ_succ, cubeEquiv, Equiv.trans_apply,
Equiv.prodCongr_apply, Fin.consEquiv_apply, Fin.cons_zero,
Fin.cons_succ, cubeDist]
congr 1
exact ih x y
theorem cubeSignEquiv_dist (ΞΉ : Type uIota) [Fintype ΞΉ] (x y : Cube (Fintype.card ΞΉ)) :
signDist (cubeSignEquiv ΞΉ x) (cubeSignEquiv ΞΉ y) = cubeDist (Fintype.card ΞΉ) x y := by
classical
rw [← cubeEquiv_dist]
unfold signDist cubeSignEquiv
simp only [Equiv.trans_apply, Equiv.arrowCongr_apply, Equiv.symm_symm]
exact (Fintype.equivFin ΞΉ).sum_comp (fun i => if cubeEquiv (Fintype.card ΞΉ) x i =
cubeEquiv (Fintype.card ΞΉ) y i then (0 : β„•) else 1)
theorem signMeasure_real_singleton {ΞΉ : Type uIota} [Fintype ΞΉ] (Οƒ : ΞΉ β†’ Bool) :
(signMeasure ΞΉ).real {Οƒ} = ((2 : ℝ)^Fintype.card ΞΉ)⁻¹ := by
simp [measureReal_def, signMeasure, Measure.pi_singleton, fairCoin,
PMF.uniformOfFintype_apply]
open scoped Classical in
theorem signMeasure_real_set {ΞΉ : Type uIota} [Fintype ΞΉ] (S : Set (ΞΉ β†’ Bool)) :
(signMeasure ΞΉ).real S = (βˆ‘ Οƒ, if Οƒ ∈ S then (1 : ℝ) else 0) /
(2 : ℝ)^Fintype.card ΞΉ := by
classical
have he : S = (↑(Finset.univ.filter (Β· ∈ S)) : Set (ΞΉ β†’ Bool)) := by ext; simp
conv_lhs => rw [he, ← sum_measureReal_singleton]
rw [Finset.sum_filter, Finset.sum_div]
apply Finset.sum_congr rfl
intro Οƒ _
by_cases hΟƒ : Οƒ ∈ S <;> simp [hΟƒ, signMeasure_real_singleton]
theorem cubeSignEquiv_measure {ΞΉ : Type uIota} [Fintype ΞΉ] (S : (ΞΉ β†’ Bool) β†’ Prop) :
(signMeasure ΞΉ).real {Οƒ | S Οƒ} =
(cubeSize (fun x => S (cubeSignEquiv ΞΉ x)) : ℝ) / (2 : ℝ)^Fintype.card ΞΉ := by
classical
rw [signMeasure_real_set]
congr 1
simp only [Set.mem_ofPred_eq]
rw [← (cubeSignEquiv ΞΉ).sum_comp (fun Οƒ => if S Οƒ then (1 : ℝ) else 0)]
simp only [cubeSize, Nat.cast_sum, Nat.cast_ite, Nat.cast_one, Nat.cast_zero]
noncomputable def SplitSieve.extendSigns (s : SplitSieve) (Οƒ : s.primes β†’ Bool) (p : β„•) : Bool := by
classical
exact if hp : p ∈ s.primes then Οƒ ⟨p,hp⟩ else false
@[simp] theorem SplitSieve.extendSigns_mem (s : SplitSieve) (Οƒ : s.primes β†’ Bool)
(p : β„•) (hp : p ∈ s.primes) : s.extendSigns Οƒ p = Οƒ ⟨p,hp⟩ := by
simp [SplitSieve.extendSigns, hp]
noncomputable def SplitSieve.signedProduct (s : SplitSieve) (Οƒ : s.primes β†’ Bool) : GaussianInt :=
∏ p ∈ s.primes, signedFactor (s.factor p) (s.extendSigns Οƒ p)
theorem SplitSieve.line_witness_log_lower (s : SplitSieve) (Οƒ : s.primes β†’ Bool)
{v w : GaussianInt} (hv : PrimitiveDirection v) (hv0 : v β‰  0) (hw : w β‰  0)
(hline : determinant v w = 0) (hdiv : s.signedProduct Οƒ ∣ w)
{B : ℝ} (hbound : (w.norm : ℝ) ≀ B) :
Real.log (v.norm : ℝ) / 2 + (βˆ‘ p : s.primes, Real.log (p : ℝ)) - Real.log B / 2 ≀
βˆ‘ p : s.primes, s.lineLog v p (Οƒ p) := by
classical
let t := s.primes.filter (fun p => Β¬signedFactor (s.factor p) (s.extendSigns Οƒ p) ∣ v)
have hp (p : β„•) (hp : p ∈ t) : 0 < (p : ℝ) := by
exact_mod_cast (s.prime_mem p (Finset.mem_filter.mp hp).1).pos
have hprod : 0 < ∏ p ∈ t, (p : ℝ) := Finset.prod_pos hp
have hvpos : 0 < (v.norm : ℝ) := by exact_mod_cast (GaussianInt.norm_pos.mpr hv0)
have hlo := s.line_witness_norm_lower s.primes (fun _ h => h) (s.extendSigns Οƒ) hv hw hline hdiv
have hlow : (v.norm : ℝ) * (∏ p ∈ t, (p : ℝ))^2 ≀ B := by
apply le_trans ?_ hbound
have hc : ((v.norm * (∏ p ∈ t, (p : β„€))^2 : β„€) : ℝ) ≀ (w.norm : ℝ) := by exact_mod_cast hlo
push_cast at hc
exact hc
have hlog := Real.log_le_log (mul_pos hvpos (sq_pos_of_pos hprod)) hlow
rw [Real.log_mul hvpos.ne' (sq_pos_of_pos hprod).ne', Real.log_pow,
Real.log_prod (fun p hp' => (hp p hp').ne')] at hlog
have hsum : (βˆ‘ p ∈ t, Real.log (p : ℝ)) =
(βˆ‘ p : s.primes, Real.log (p : ℝ)) - βˆ‘ p : s.primes, s.lineLog v p (Οƒ p) := by
calc
_ = βˆ‘ p ∈ s.primes, (Real.log (p : ℝ) - s.lineLog v p (s.extendSigns Οƒ p)) := by
unfold t
rw [Finset.sum_filter]
apply Finset.sum_congr rfl
intro p hp'
unfold SplitSieve.lineLog
split_ifs <;> simp_all
_ = _ := by
rw [← Finset.sum_coe_sort s.primes
(fun p => Real.log (p : ℝ) - s.lineLog v p (s.extendSigns Οƒ p))]
simp [SplitSieve.extendSigns, Finset.sum_sub_distrib]
rw [hsum] at hlog
norm_num only [Nat.cast_ofNat] at hlog
linarith only [hlog]
def SplitSieve.lineEvent (s : SplitSieve) (Q : Set GaussianInt) (v : GaussianInt)
(Οƒ : s.primes β†’ Bool) : Prop :=
βˆƒ w ∈ Q, w β‰  0 ∧ determinant v w = 0 ∧ s.signedProduct Οƒ ∣ w
theorem SplitSieve.line_event_tail (s : SplitSieve) {Q : Set GaussianInt}
{v : GaussianInt} (hv : PrimitiveDirection v) (hv0 : v β‰  0) {B Ξ΅ : ℝ}
(hQ : βˆ€ w ∈ Q, (w.norm : ℝ) ≀ B) (hΞ΅ : 0 ≀ Ξ΅)
(hexcess : Ξ΅ ≀ (βˆ‘ p : s.primes, Real.log (p : ℝ)) - Real.log B / 2) :
(signMeasure s.primes).real {Οƒ | s.lineEvent Q v Οƒ} ≀
Real.exp (-Ξ΅^2 / (2 * βˆ‘ p : s.primes,
(((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ))) := by
apply le_trans (measureReal_mono (sβ‚‚ := {Οƒ |
Real.log (v.norm : ℝ) / 2 + Ξ΅ ≀ βˆ‘ p : s.primes, s.lineLog v p (Οƒ p)}) ?_)
Β· exact s.lineLog_tail hv hv0 hΞ΅
Β· rintro Οƒ ⟨w,hwQ,hw,hline,hd⟩
have hl := s.line_witness_log_lower Οƒ hv hv0 hw hline hd (hQ w hwQ)
change Real.log (v.norm : ℝ) / 2 + Ξ΅ ≀ _
linarith
open scoped Classical in
noncomputable def SplitSieve.commonProduct (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool) : GaussianInt :=
∏ p ∈ s.primes.filter (fun p => s.extendSigns Οƒ p = s.extendSigns Ο„ p),
signedFactor (s.factor p) (s.extendSigns Οƒ p)
theorem SplitSieve.commonProduct_dvd_left (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool) :
s.commonProduct Οƒ Ο„ ∣ s.signedProduct Οƒ := by
classical
exact Finset.prod_dvd_prod_of_subset _ _ _ (Finset.filter_subset _ _)
theorem SplitSieve.commonProduct_comm (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool) :
s.commonProduct Οƒ Ο„ = s.commonProduct Ο„ Οƒ := by
classical
unfold SplitSieve.commonProduct
apply Finset.prod_congr
Β· ext p
simp only [Finset.mem_filter, eq_comm]
Β· intro p hp
rw [(Finset.mem_filter.mp hp).2]
theorem SplitSieve.commonProduct_dvd_right (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool) :
s.commonProduct Οƒ Ο„ ∣ s.signedProduct Ο„ := by
rw [s.commonProduct_comm]
exact s.commonProduct_dvd_left Ο„ Οƒ
open scoped Classical in
theorem SplitSieve.signDist_eq_card (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool) :
signDist Οƒ Ο„ = (s.primes.filter (fun p => s.extendSigns Οƒ p β‰  s.extendSigns Ο„ p)).card := by
classical
rw [Finset.card_eq_sum_ones, Finset.sum_filter, ← Finset.sum_coe_sort s.primes
(fun p => if s.extendSigns Οƒ p β‰  s.extendSigns Ο„ p then 1 else 0)]
unfold signDist
apply Finset.sum_congr rfl
intro p _
simp [SplitSieve.extendSigns]
theorem SplitSieve.period_le_common_norm (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool)
{U : ℝ} (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U) :
(s.period : ℝ) ≀ ((s.commonProduct Οƒ Ο„).norm : ℝ) * U^(signDist Οƒ Ο„) := by
classical
let c := s.primes.filter (fun p => s.extendSigns Οƒ p = s.extendSigns Ο„ p)
let d := s.primes.filter (fun p => s.extendSigns Οƒ p β‰  s.extendSigns Ο„ p)
have hc : ((s.commonProduct Οƒ Ο„).norm : ℝ) = ∏ p ∈ c, (p : ℝ) := by
unfold SplitSieve.commonProduct
rw [norm_finset_prod, Int.cast_prod]
apply Finset.prod_congr rfl
intro p hp
rw [signedFactor_norm, s.factor_norm p (Finset.mem_filter.mp hp).1]
exact_mod_cast (rfl : (p : ℝ) = p)
have hd : (∏ p ∈ d, (p : ℝ)) ≀ U^d.card := by
rw [← Finset.prod_const]
exact Finset.prod_le_prodβ‚€ (fun p _ => Nat.cast_nonneg p)
(fun p hp => hpU p (Finset.mem_filter.mp hp).1)
have hp : (s.period : ℝ) = (∏ p ∈ c, (p : ℝ)) * (∏ p ∈ d, (p : ℝ)) := by
unfold SplitSieve.period
push_cast
exact (Finset.prod_filter_mul_prod_filter_not s.primes
(fun p => s.extendSigns Οƒ p = s.extendSigns Ο„ p) (fun p => (p : ℝ))).symm
rw [hp, hc, s.signDist_eq_card]
exact mul_le_mul_of_nonneg_left hd (Finset.prod_nonneg (fun p _ => Nat.cast_nonneg p))
theorem SplitSieve.near_witnesses_collinear (s : SplitSieve) (Οƒ Ο„ : s.primes β†’ Bool)
{w w' : GaussianInt} (hw : s.signedProduct Οƒ ∣ w) (hw' : s.signedProduct Ο„ ∣ w')
{U B : ℝ} (hU : 0 < U) (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U)
(hdet : (|determinant w w'| : ℝ) ≀ B)
(hsmall : B * U^(signDist Οƒ Ο„) < (s.period : ℝ)) : determinant w w' = 0 := by
have hbound := lt_of_lt_of_le hsmall (s.period_le_common_norm Οƒ Ο„ hpU)
have hlt : B < ((s.commonProduct Οƒ Ο„).norm : ℝ) :=
(mul_lt_mul_iff_leftβ‚€ (pow_pos hU _)).mp (by simpa only [mul_comm] using hbound)
apply determinant_eq_zero_of_common_factor
((s.commonProduct_dvd_left Οƒ Ο„).trans hw) ((s.commonProduct_dvd_right Οƒ Ο„).trans hw')
exact_mod_cast hdet.trans_lt hlt
theorem determinant_zero_trans {u v w : GaussianInt} (hv : v β‰  0)
(huv : determinant u v = 0) (hvw : determinant v w = 0) :
determinant u w = 0 := by
have hcoord : v.re β‰  0 ∨ v.im β‰  0 := by
contrapose! hv
ext <;> simp [hv.1, hv.2]
unfold determinant at *
rcases hcoord with hr | hi
Β· apply (mul_eq_zero.mp (show v.re * (u.re * w.im - u.im * w.re) = 0 by
nlinarith [congrArg (fun x : β„€ => x * w.re) huv,
congrArg (fun x : β„€ => u.re * x) hvw])).resolve_left hr
Β· apply (mul_eq_zero.mp (show v.im * (u.re * w.im - u.im * w.re) = 0 by
nlinarith [congrArg (fun x : β„€ => x * w.im) huv,
congrArg (fun x : β„€ => u.im * x) hvw])).resolve_left hi
@[simp] theorem determinant_self (v : GaussianInt) : determinant v v = 0 := by
unfold determinant
ring
theorem determinant_swap (u v : GaussianInt) : determinant u v = -determinant v u := by
unfold determinant
ring
def witnessLine (v : GaussianInt) : Set GaussianInt := {w | determinant v w = 0}
theorem witnessLine_eq {v w : GaussianInt} (hv : v β‰  0) (hw : w β‰  0)
(hvw : determinant v w = 0) : witnessLine v = witnessLine w := by
ext u
constructor
Β· intro hu
exact determinant_zero_trans hv (by rw [determinant_swap, hvw, neg_zero]) hu
Β· exact determinant_zero_trans hw hvw
def SplitSieve.badSign (s : SplitSieve) (Q : Set GaussianInt) (Οƒ : s.primes β†’ Bool) : Prop :=
βˆƒ w ∈ Q, w β‰  0 ∧ s.signedProduct Οƒ ∣ w
noncomputable def SplitSieve.badWitness (s : SplitSieve) (Q : Set GaussianInt)
(Οƒ : {Οƒ : s.primes β†’ Bool // s.badSign Q Οƒ}) : GaussianInt := Οƒ.property.choose
theorem SplitSieve.badWitness_spec (s : SplitSieve) (Q : Set GaussianInt)
(Οƒ : {Οƒ : s.primes β†’ Bool // s.badSign Q Οƒ}) :
s.badWitness Q Οƒ ∈ Q ∧ s.badWitness Q Οƒ β‰  0 ∧ s.signedProduct Οƒ.val ∣ s.badWitness Q Οƒ :=
Οƒ.property.choose_spec
def SplitSieve.badLines (s : SplitSieve) (Q : Set GaussianInt) : Set (Set GaussianInt) :=
Set.range (fun Οƒ : {Οƒ : s.primes β†’ Bool // s.badSign Q Οƒ} => witnessLine (s.badWitness Q Οƒ))
instance SplitSieve.badLines_finite (s : SplitSieve) (Q : Set GaussianInt) :
Finite (s.badLines Q) := by
unfold SplitSieve.badLines
infer_instance
theorem SplitSieve.badLine_primitive (s : SplitSieve) (Q : Set GaussianInt)
(L : s.badLines Q) : βˆƒ v : GaussianInt, PrimitiveDirection v ∧ v β‰  0 ∧ witnessLine v = L.val := by
obtain βŸ¨Οƒ, hΟƒβŸ© := L.property
have hw := s.badWitness_spec Q Οƒ
obtain ⟨v,hv,hv0,a,_hanz,ha⟩ := exists_primitive_direction hw.2.1
refine ⟨v,hv,hv0, ?_⟩
rw [← hΟƒ]
apply witnessLine_eq hv0 hw.2.1
rw [ha]
unfold determinant
simp only [Zsqrtd.re_mul, Zsqrtd.im_mul, Zsqrtd.re_intCast, Zsqrtd.im_intCast]
ring
def SplitSieve.lineClass (s : SplitSieve) (Q : Set GaussianInt) (L : s.badLines Q)
(Οƒ : s.primes β†’ Bool) : Prop :=
βˆƒ hΟƒ : s.badSign Q Οƒ, witnessLine (s.badWitness Q βŸ¨Οƒ,hΟƒβŸ©) = L.val
theorem SplitSieve.badSign_iff_class (s : SplitSieve) (Q : Set GaussianInt) (Οƒ : s.primes β†’ Bool) :
s.badSign Q Οƒ ↔ βˆƒ L : s.badLines Q, s.lineClass Q L Οƒ := by
constructor
· intro hσ
exact ⟨⟨witnessLine (s.badWitness Q βŸ¨Οƒ,hΟƒβŸ©), βŸ¨βŸ¨Οƒ,hΟƒβŸ©,rfl⟩⟩, hΟƒ, rfl⟩
Β· rintro ⟨L,hΟƒ,_⟩
exact hσ
theorem SplitSieve.lineClass_subset_event (s : SplitSieve) (Q : Set GaussianInt)
(L : s.badLines Q) {v : GaussianInt} (hvL : witnessLine v = L.val) :
{Οƒ | s.lineClass Q L Οƒ} βŠ† {Οƒ | s.lineEvent Q v Οƒ} := by
rintro Οƒ ⟨hΟƒ,hL⟩
have hw := s.badWitness_spec Q βŸ¨Οƒ,hΟƒβŸ©
refine ⟨_,hw.1,hw.2.1,?_,hw.2.2⟩
change s.badWitness Q βŸ¨Οƒ,hΟƒβŸ© ∈ witnessLine v
rw [hvL, ← hL]
exact determinant_self _
theorem SplitSieve.lineClasses_separated (s : SplitSieve) (Q : Set GaussianInt)
{U B : ℝ} (hU : 1 ≀ U) (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U)
(hdet : βˆ€ w ∈ Q, βˆ€ w' ∈ Q, (|determinant w w'| : ℝ) ≀ B)
{h : β„•} (hsmall : B * U^(2*h) < (s.period : ℝ)) (hB : 0 ≀ B) :
βˆ€ L L' : s.badLines Q, L β‰  L' β†’ βˆ€ Οƒ, s.lineClass Q L Οƒ β†’
βˆ€ Ο„, s.lineClass Q L' Ο„ β†’ 2*h < signDist Οƒ Ο„ := by
intro L L' hLL Οƒ ⟨hΟƒ,hΟƒL⟩ Ο„ ⟨hΟ„,hΟ„L⟩
by_contra hn
have hd : signDist Οƒ Ο„ ≀ 2*h := le_of_not_gt hn
have hw := s.badWitness_spec Q βŸ¨Οƒ,hΟƒβŸ©
have hw' := s.badWitness_spec Q βŸ¨Ο„,hΟ„βŸ©
have hsm : B * U^(signDist Οƒ Ο„) < (s.period : ℝ) :=
(mul_le_mul_of_nonneg_left (pow_le_pow_rightβ‚€ hU hd) hB).trans_lt hsmall
have hcol := s.near_witnesses_collinear Οƒ Ο„ hw.2.2 hw'.2.2 (by linarith) hpU
(hdet _ hw.1 _ hw'.1) hsm
apply hLL
apply Subtype.ext
rw [← hΟƒL, ← hΟ„L]
exact witnessLine_eq hw.2.1 hw'.2.1 hcol
theorem cubeSize_exists_le_sum {n : β„•} {ΞΉ : Type uIota} [Fintype ΞΉ]
(S : ΞΉ β†’ Cube n β†’ Prop) : cubeSize (fun x => βˆƒ i, S i x) ≀ βˆ‘ i, cubeSize (S i) := by
classical
unfold cubeSize
rw [Finset.sum_comm]
apply Finset.sum_le_sum
intro x _
by_cases hx : βˆƒ i, S i x
· obtain ⟨i, hi⟩ := hx
rw [ite_eq_left ⟨i,hi⟩]
have h : (if S i x then 1 else 0) ≀ βˆ‘ j : ΞΉ, if S j x then 1 else 0 :=
Finset.single_le_sum (f := fun j => if S j x then (1 : β„•) else 0)
(fun _ _ => Nat.zero_le _) (Finset.mem_univ i)
simpa only [ite_eq_left hi] using h
Β· simp only [ite_eq_right hx]
exact Nat.zero_le _
theorem SplitSieve.signed_region_bound (s : SplitSieve) (Q : Set GaussianInt)
(hr : 0 < s.primes.card) {U B N Ξ΅ a E A : ℝ} {h : β„•}
(hU : 1 ≀ U) (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U)
(hB : 0 ≀ B) (hdet : βˆ€ w ∈ Q, βˆ€ w' ∈ Q, (|determinant w w'| : ℝ) ≀ B)
(hsmall : B * U^(2*h) < (s.period : ℝ))
(hQ : βˆ€ w ∈ Q, (w.norm : ℝ) ≀ N) (hΞ΅ : 0 ≀ Ξ΅)
(hexcess : Ξ΅ ≀ (βˆ‘ p : s.primes, Real.log (p : ℝ)) - Real.log N / 2)
(ha : 0 ≀ a) (hE : 0 < E) (hA : 0 < A)
(hlogE : Real.log E ≀ (s.primes.card : ℝ) * Real.log 2 - a)
(hseed : A * Real.exp (-Ξ΅^2 / (2 * βˆ‘ p : s.primes,
(((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ))) * (2 : ℝ)^s.primes.card ≀ E)
(hfactor : A ≀ (1 + a / ((s.primes.card : ℝ)*Real.log 2))^h) :
(signMeasure s.primes).real {Οƒ | s.badSign Q Οƒ} ≀ A⁻¹ := by
classical
let : Fintype (s.badLines Q) := Fintype.ofFinite _
let S : s.badLines Q β†’ Cube (Fintype.card s.primes) β†’ Prop :=
fun L x => s.lineClass Q L (cubeSignEquiv s.primes x)
have hcard : Fintype.card s.primes = s.primes.card := Fintype.card_coe _
have hpow : 0 < (2 : ℝ)^Fintype.card s.primes := pow_pos (by norm_num) _
have hseed' (L : s.badLines Q) : A * cubeSize (S L) ≀ E := by
obtain ⟨v,hv,hv0,hvL⟩ := s.badLine_primitive Q L
have hm := (measureReal_mono (s.lineClass_subset_event Q L hvL)).trans
(s.line_event_tail hv hv0 hQ hΞ΅ hexcess)
rw [cubeSignEquiv_measure] at hm
have hc : (cubeSize (S L) : ℝ) ≀ Real.exp (-Ξ΅^2 / (2 * βˆ‘ p : s.primes,
(((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ))) * (2 : ℝ)^Fintype.card s.primes :=
(div_le_iffβ‚€ hpow).mp hm
have hh := mul_le_mul_of_nonneg_left hc hA.le
exact hh.trans (by simpa only [hcard, mul_assoc] using hseed)
have hsep : βˆ€ L L' : s.badLines Q, L β‰  L' β†’ βˆ€ x, S L x β†’ βˆ€ y, S L' y β†’
2*h < cubeDist (Fintype.card s.primes) x y := by
intro L L' hLL x hx y hy
have hh := s.lineClasses_separated Q hU hpU hdet hsmall hB L L' hLL
(cubeSignEquiv s.primes x) hx (cubeSignEquiv s.primes y) hy
simpa only [cubeSignEquiv_dist] using hh
have hpack := cube_separated_bound (n := Fintype.card s.primes) (h := h)
(by simpa only [hcard] using hr) S ha hE (by simpa only [hcard] using hlogE)
hseed' (by simpa only [hcard] using hfactor) hsep
have hunion : (cubeSize (fun x => s.badSign Q (cubeSignEquiv s.primes x)) : ℝ) ≀
βˆ‘ L, (cubeSize (S L) : ℝ) := by
have he : (fun x => s.badSign Q (cubeSignEquiv s.primes x)) = (fun x => βˆƒ L, S L x) := by
funext x
exact propext (s.badSign_iff_class Q _)
rw [he]
exact_mod_cast cubeSize_exists_le_sum S
rw [cubeSignEquiv_measure, div_le_iffβ‚€ hpow]
have hh := (mul_le_mul_of_nonneg_left hunion hA.le).trans hpack
calc
_ ≀ (2 : ℝ)^Fintype.card s.primes / A := (le_div_iffβ‚€ hA).mpr (by simpa only [mul_comm] using hh)
_ = _ := by rw [div_eq_mul_inv, mul_comm]
theorem SplitSieve.signed_region_exp_bound (s : SplitSieve) (Q : Set GaussianInt)
(hr : 0 < s.primes.card) {U B N Ξ΅ a b : ℝ} {h : β„•}
(hU : 1 ≀ U) (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U)
(hB : 0 ≀ B) (hdet : βˆ€ w ∈ Q, βˆ€ w' ∈ Q, (|determinant w w'| : ℝ) ≀ B)
(hsmall : B * U^(2*h) < (s.period : ℝ))
(hQ : βˆ€ w ∈ Q, (w.norm : ℝ) ≀ N) (hΞ΅ : 0 ≀ Ξ΅)
(hexcess : Ξ΅ ≀ (βˆ‘ p : s.primes, Real.log (p : ℝ)) - Real.log N / 2)
(ha : 0 ≀ a) (hb : b ≀ a)
(htail : 2*a ≀ Ξ΅^2 / (2 * βˆ‘ p : s.primes,
(((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ)))
(hgrowth : b ≀ (h : ℝ) * Real.log (1 + a / ((s.primes.card : ℝ)*Real.log 2))) :
(signMeasure s.primes).real {Οƒ | s.badSign Q Οƒ} ≀ Real.exp (-b) := by
have htwo : 0 < Real.log 2 := Real.log_pos (by norm_num)
have hr' : 0 < (s.primes.card : ℝ) := by exact_mod_cast hr
have hbase : 0 < 1 + a / ((s.primes.card : ℝ)*Real.log 2) := by
have := div_nonneg ha (mul_pos hr' htwo).le
linarith
have hbexp : Real.exp b ≀ (1 + a / ((s.primes.card : ℝ)*Real.log 2))^h := by
rw [← Real.exp_log (pow_pos hbase h)]
apply Real.exp_le_exp.mpr
simpa only [Real.log_pow] using hgrowth
have hh := s.signed_region_bound Q hr hU hpU hB hdet hsmall hQ hΞ΅ hexcess ha
(E := (2 : ℝ)^s.primes.card * Real.exp (-a))
(A := Real.exp b) (h := h) (mul_pos (pow_pos (by norm_num) _) (Real.exp_pos _))
(Real.exp_pos _) (by
rw [Real.log_mul (pow_pos (by norm_num : (0 : ℝ) < 2) _).ne' (Real.exp_pos _).ne',
Real.log_pow, Real.log_exp]
linarith) (by
rw [← Real.exp_add]
have he : b + -Ξ΅^2 / (2 * βˆ‘ p : s.primes,
(((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ)) ≀ -a := by
rw [neg_div]
linarith
exact (mul_le_mul_of_nonneg_right (Real.exp_le_exp.mpr he)
(pow_nonneg (by norm_num) _)).trans_eq (mul_comm _ _)) hbexp
simpa only [Real.exp_neg] using hh
def orientedRectangle (e : β„‚) (R W : ℝ) : Set GaussianInt :=
{z | |(star e * (z : β„‚)).re| ≀ R ∧ |(star e * (z : β„‚)).im| ≀ W}
theorem rectangle_norm_bound {e : β„‚} (he : β€–eβ€– = 1) {R W : ℝ}
(hW : 0 ≀ W) (hWR : W ≀ R) {z : GaussianInt} (hz : z ∈ orientedRectangle e R W) :
(z.norm : ℝ) ≀ 2 * R^2 := by
have hnorm : β€–star e * (z : β„‚)β€– = β€–(z : β„‚)β€– := by
rw [norm_mul, norm_star, he, one_mul]
rw [norm_square_complex, ← hnorm, ← Complex.normSq_eq_norm_sq, Complex.normSq_apply]
have hx : (star e * (z : β„‚)).re ^ 2 ≀ R^2 := by
simpa only [sq_abs] using (sq_le_sqβ‚€ (abs_nonneg ((star e * (z : β„‚)).re))
(hW.trans hWR)).mpr hz.1
have hy : (star e * (z : β„‚)).im ^ 2 ≀ W^2 := by
simpa only [sq_abs] using (sq_le_sqβ‚€ (abs_nonneg ((star e * (z : β„‚)).im)) hW).mpr hz.2
have hww := (sq_le_sqβ‚€ hW (hW.trans hWR)).mpr hWR
nlinarith
theorem determinant_rotated (e : β„‚) (he : β€–eβ€– = 1) (z w : GaussianInt) :
(determinant z w : ℝ) =
(star e * (z : β„‚)).re * (star e * (w : β„‚)).im -
(star e * (z : β„‚)).im * (star e * (w : β„‚)).re := by
have hsq : e.re^2 + e.im^2 = 1 := by
have hh := Complex.normSq_eq_norm_sq e
rw [he] at hh
simpa [Complex.normSq_apply, pow_two] using hh
simp only [Complex.mul_re, Complex.mul_im, Complex.star_def, Complex.conj_re, Complex.conj_im,
← GaussianInt.intCast_re, ← GaussianInt.intCast_im, determinant, Int.cast_sub, Int.cast_mul]
nlinarith [congrArg (fun x : ℝ => x * ((z.re : ℝ) * w.im - z.im * w.re)) hsq]
theorem rectangle_determinant_bound {e : β„‚} (he : β€–eβ€– = 1) {R W : ℝ}
(hR : 0 ≀ R) (hW : 0 ≀ W) {z w : GaussianInt}
(hz : z ∈ orientedRectangle e R W) (hw : w ∈ orientedRectangle e R W) :
(|determinant z w| : ℝ) ≀ 2*R*W := by
rw [determinant_rotated e he]
apply (abs_sub _ _).trans
rw [abs_mul, abs_mul]
have h1 := mul_le_mul hz.1 hw.2 (abs_nonneg _) hR
have h2 := mul_le_mul hz.2 hw.1 (abs_nonneg _) hW
nlinarith
theorem rectangle_cube_growth {r : β„•} {d : ℝ} (hd : 0 < d) (hd1 : d ≀ 1/4)
(hlarge : 1000 ≀ d^3 * r) :
let h := ⌊d * r / 10βŒ‹β‚Š
d^3 * r / 5120 ≀ (h : ℝ) * Real.log (1 + (d^2 * r / 64) / ((r : ℝ) * Real.log 2)) := by
dsimp only
have hr : 0 < (r : ℝ) := by
by_contra h
have hr0 : (r : ℝ) = 0 := le_antisymm (le_of_not_gt h) (Nat.cast_nonneg _)
rw [hr0, mul_zero] at hlarge
norm_num at hlarge
have loglo : (1/2 : ℝ) ≀ Real.log 2 := by linarith [Real.log_two_gt_d9]
have loghi : Real.log 2 ≀ 1 := by linarith [Real.log_two_lt_d9]
have hlog : 0 < Real.log 2 := lt_of_lt_of_le (by norm_num) loglo
have hdle : d ≀ 1 := by linarith
have hsq : d^2 ≀ 1 := by nlinarith
have hdr : d^3 * r ≀ d * r := by
exact mul_le_mul_of_nonneg_right (by nlinarith [mul_nonneg hd.le (sub_nonneg.mpr hsq)]) hr.le
have hfloor : d * r / 20 ≀ (⌊d * r / 10βŒ‹β‚Š : ℝ) := by
have hh := Nat.lt_floor_add_one (d * r / 10)
have hdrl : 1000 ≀ d * r := hlarge.trans hdr
linarith
have heq : (d^2 * r / 64) / ((r : ℝ) * Real.log 2) = d^2 / (64 * Real.log 2) := by
field_simp
let Ξ΄ := d^2 / (64 * Real.log 2)
have hΞ΄ : 0 ≀ Ξ΄ := div_nonneg (sq_nonneg _) (by positivity)
have hΞ΄1 : Ξ΄ ≀ 1 := by
apply (div_le_iffβ‚€ (mul_pos (by norm_num) hlog)).mpr
nlinarith
have hΞ΄lo : d^2 / 64 ≀ Ξ΄ := by
apply (le_div_iffβ‚€ (mul_pos (by norm_num) hlog)).mpr
nlinarith [mul_nonneg (sq_nonneg d) (sub_nonneg.mpr loghi)]
have hlogΞ΄ : Ξ΄/2 ≀ Real.log (1+Ξ΄) := by
apply le_trans ?_ (Real.le_log_one_add_of_nonneg hΞ΄)
apply (le_div_iffβ‚€ (by linarith : 0 < Ξ΄+2)).mpr
nlinarith [mul_nonneg hΞ΄ (sub_nonneg.mpr hΞ΄1)]
rw [heq]
change _ ≀ (⌊d * r / 10βŒ‹β‚Š : ℝ) * Real.log (1+Ξ΄)
have hm := mul_le_mul hfloor hlogΞ΄ (by linarith) (Nat.cast_nonneg _)
have hm' : d^3 * r / 2560 ≀ (d*r/20) * (Ξ΄/2) := by
have := mul_le_mul_of_nonneg_left hΞ΄lo (by positivity : 0 ≀ d*r/40)
nlinarith
have hnon : 0 ≀ d^3 * r := by positivity
nlinarith
theorem rectangle_linear_large {r d : ℝ} (hr : 0 ≀ r) (hd : 0 ≀ d) (hd1 : d ≀ 1)
(hlarge : 1000 ≀ d^3 * r) : 1000 ≀ d*r := by
have hs : d^2 ≀ 1 := by nlinarith
have hc : d^3 ≀ d := by nlinarith [mul_nonneg hd (sub_nonneg.mpr hs)]
exact hlarge.trans (mul_le_mul_of_nonneg_right hc hr)
theorem rectangle_log_small {r : β„•} {d t u l : ℝ}
(hd : 0 < d) (hd1 : d ≀ 1/4) (ht : 1/2 ≀ t) (hu : 0 ≀ u) (hutu : u ≀ 2*t)
(hl : (r : ℝ)*t ≀ l) (hlarge : 1000 ≀ d^3 * r) :
Real.log 2 + (1-d)*l + (2*⌊d*r/10βŒ‹β‚Š : β„•)*u < l := by
have hdr : 1000 ≀ d*r := rectangle_linear_large (Nat.cast_nonneg _) hd.le (by linarith) hlarge
have hf : (⌊d*r/10βŒ‹β‚Š : ℝ) ≀ d*r/10 := Nat.floor_le (by positivity)
have hm : (2*⌊d*r/10βŒ‹β‚Š : β„•)*u ≀ (d*r/5)*(2*t) := by
push_cast
apply mul_le_mul (by linarith) hutu hu
positivity
have hdl := mul_le_mul_of_nonneg_left hl hd.le
have hdt : 500 ≀ d*r*t := by
have hh := mul_le_mul_of_nonneg_left ht (by positivity : 0 ≀ d*r)
nlinarith
have hlog : Real.log 2 ≀ 1 := by linarith [Real.log_two_lt_d9]
nlinarith
theorem rectangle_log_excess {r : β„•} {d t l : ℝ} (hd : 0 < d) (hd1 : d ≀ 1/4)
(ht : 1/2 ≀ t) (hl : (r : ℝ)*t ≀ l) (hlarge : 1000 ≀ d^3 * r) :
d*l/2 ≀ l - ((1-d)*l + Real.log 2/2) := by
have hdr : 1000 ≀ d*r := rectangle_linear_large (Nat.cast_nonneg _) hd.le (by linarith) hlarge
have h1 := mul_le_mul_of_nonneg_left hl hd.le
have h2 := mul_le_mul_of_nonneg_left ht (by positivity : 0 ≀ d*r)
have hlog : Real.log 2 ≀ 1 := by linarith [Real.log_two_lt_d9]
nlinarith
theorem rectangle_hoeffding_exponent {r d t l V : ℝ} (hr : 0 < r) (hd : 0 ≀ d)
(ht : 0 < t) (hl : r*t ≀ l) (hV : 0 < V) (hVup : V ≀ r*t^2) :
2*(d^2*r/64) ≀ (d*l/2)^2 / (2*V) := by
have hdl : 0 ≀ d*r*t/2 := by positivity
have hdl' : d*r*t/2 ≀ d*l/2 := by nlinarith [mul_le_mul_of_nonneg_left hl hd]
have hs : (d*r*t/2)^2 ≀ (d*l/2)^2 := (sq_le_sqβ‚€ hdl (hdl.trans hdl')).mpr hdl'
have hden : 0 < 2*r*t^2 := by positivity
calc
2*(d^2*r/64) ≀ d^2*r/8 := by nlinarith [mul_nonneg (sq_nonneg d) hr.le]
_ = (d*r*t/2)^2 / (2*r*t^2) := by field_simp; ring
_ ≀ (d*l/2)^2 / (2*r*t^2) := div_le_div_of_nonneg_right hs hden.le
_ ≀ (d*l/2)^2 / (2*V) := div_le_div_of_nonneg_left (sq_nonneg _) (by positivity) (by nlinarith)
theorem SplitSieve.log_period (s : SplitSieve) :
Real.log (s.period : ℝ) = βˆ‘ p : s.primes, Real.log (p : ℝ) := by
classical
unfold SplitSieve.period
push_cast
rw [Real.log_prod (fun p hp => by
exact_mod_cast (s.prime_mem p hp).ne_zero)]
exact (Finset.sum_coe_sort s.primes (fun p => Real.log (p : ℝ))).symm
noncomputable def SplitSieve.logVariance (s : SplitSieve) : ℝ :=
βˆ‘ p : s.primes, (((β€–Real.log (p : ℝ)β€–β‚Š / 2)^2 : ℝβ‰₯0) : ℝ)
theorem SplitSieve.logVariance_pos (s : SplitSieve) (hr : 0 < s.primes.card) :
0 < s.logVariance := by
classical
obtain ⟨p,hp⟩ := Finset.card_pos.mp hr
have hlog : 0 < Real.log (p : ℝ) := Real.log_pos (by exact_mod_cast (s.prime_mem p hp).one_lt)
apply Finset.sum_pos' (fun _ _ => by positivity)
refine ⟨⟨p,hp⟩, Finset.mem_univ _, ?_⟩
simp only [NNReal.coe_pow, NNReal.coe_div, NNReal.coe_ofNat, coe_nnnorm, Real.norm_eq_abs,
abs_of_pos hlog]
positivity
theorem SplitSieve.logVariance_le (s : SplitSieve) {U : ℝ}
(hU : 1 ≀ U) (hpU : βˆ€ p ∈ s.primes, (p : ℝ) ≀ U) :
s.logVariance ≀ (s.primes.card : ℝ) * (Real.log U / 2)^2 := by
classical
calc
_ ≀ βˆ‘ _p : s.primes, (Real.log U / 2)^2 := by
apply Finset.sum_le_sum
intro p _
have hp : 0 < (p : ℝ) := by exact_mod_cast (s.prime_mem p p.property).pos
have hlog : 0 ≀ Real.log (p : ℝ) := Real.log_nonneg (by exact_mod_cast (s.prime_mem p p.property).one_le)
have hle := Real.log_le_log hp (hpU p p.property)
simp only [NNReal.coe_pow, NNReal.coe_div, NNReal.coe_ofNat, coe_nnnorm, Real.norm_eq_abs,
abs_of_nonneg hlog]
exact (sq_le_sqβ‚€ (by positivity) (div_nonneg (Real.log_nonneg hU) (by norm_num))).mpr (by linarith)
_ = _ := by simp [nsmul_eq_mul]
theorem SplitSieve.signed_rectangle (s : SplitSieve)
{T R W d : ℝ} {e : β„‚} (he : β€–eβ€– = 1) (hT : 5 ≀ T)
(hpT : βˆ€ p ∈ s.primes, T ≀ (p : ℝ) ∧ (p : ℝ) ≀ 2*T)
(hW : 1 ≀ W) (hWR : W ≀ R) (hd : 0 < d) (hd1 : d < 1/4)
(harea : R*W ≀ (s.period : ℝ)^(1-d)) (hlarge : 1000 ≀ d^3*s.primes.card) :
(signMeasure s.primes).real {Οƒ | s.badSign (orientedRectangle e R W) Οƒ} ≀
Real.exp (-(d^3*s.primes.card/5120)) := by
have hr : 0 < s.primes.card := by
by_contra h
have hz : s.primes.card = 0 := Nat.eq_zero_of_not_pos h
rw [hz, Nat.cast_zero, mul_zero] at hlarge
norm_num at hlarge
have hr' : 0 < (s.primes.card : ℝ) := by exact_mod_cast hr
have hP : 0 < (s.period : ℝ) := by exact_mod_cast s.period_pos
have hTpos : 0 < T := by linarith
have hU : 1 ≀ 2*T := by linarith
have hUpos : 0 < 2*T := by linarith
have hWpos : 0 < W := by linarith
have hRpos : 0 < R := lt_of_lt_of_le hWpos hWR
have hRW : 0 < R*W := mul_pos hRpos hWpos
have hlogT : 1/2 ≀ Real.log T := by
have hlog2T := Real.log_le_log (by norm_num : (0 : ℝ) < 2) (by linarith : 2 ≀ T)
linarith [Real.log_two_gt_d9]
have hlogU : Real.log (2*T) ≀ 2*Real.log T := by
rw [Real.log_mul (by norm_num) hTpos.ne']
have hh := Real.log_le_log (by norm_num : (0 : ℝ) < 2) (by linarith : 2 ≀ T)
linarith
have hlogP : (s.primes.card : ℝ)*Real.log T ≀ Real.log (s.period : ℝ) := by
rw [s.log_period]
calc
_ = βˆ‘ _p : s.primes, Real.log T := by simp [nsmul_eq_mul]
_ ≀ _ := Finset.sum_le_sum (fun p _ => Real.log_le_log hTpos (hpT p p.property).1)
have hlogPpos : 0 < Real.log (s.period : ℝ) :=
(mul_pos hr' (by linarith : 0 < Real.log T)).trans_le hlogP
have hRarea : R ≀ R*W := by nlinarith
have hlogR : Real.log R ≀ (1-d)*Real.log (s.period : ℝ) := by
have hh := Real.log_le_log hRpos (hRarea.trans harea)
simpa only [Real.log_rpow hP] using hh
have hlogB : Real.log (2*R*W) ≀ Real.log 2 + (1-d)*Real.log (s.period : ℝ) := by
have hh := Real.log_le_log hRW harea
rw [Real.log_rpow hP] at hh
have heq : 2*R*W = 2*(R*W) := by ring
rw [heq, Real.log_mul (by norm_num) hRW.ne']
linarith
have hlogN : Real.log (2*R^2)/2 ≀ (1-d)*Real.log (s.period : ℝ) + Real.log 2/2 := by
rw [Real.log_mul (by norm_num) (pow_pos hRpos 2).ne', Real.log_pow]
norm_num only [Nat.cast_ofNat]
linarith
let h : β„• := ⌊d*s.primes.card/10βŒ‹β‚Š
have hsmall : (2*R*W) * (2*T)^(2*h) < (s.period : ℝ) := by
apply (Real.log_lt_log_iff (mul_pos (by positivity) (pow_pos hUpos _)) hP).mp
rw [Real.log_mul (by positivity : 2*R*W β‰  0) (pow_pos hUpos _).ne', Real.log_pow]
have hh := rectangle_log_small hd hd1.le hlogT (Real.log_nonneg hU) hlogU hlogP hlarge
exact (add_le_add_left hlogB _).trans_lt hh
have hV := s.logVariance_pos hr
have hVup : s.logVariance ≀ (s.primes.card : ℝ)*(Real.log T)^2 := by
apply (s.logVariance_le hU (fun p hp => (hpT p hp).2)).trans
apply mul_le_mul_of_nonneg_left ?_ hr'.le
apply (sq_le_sqβ‚€ (div_nonneg (Real.log_nonneg hU) (by norm_num)) (by linarith : 0 ≀ Real.log T)).mpr
linarith
apply s.signed_region_exp_bound (orientedRectangle e R W) hr hU
(fun p hp => (hpT p hp).2) (by positivity) (fun _ hw _ hw' =>
rectangle_determinant_bound he hRpos.le hWpos.le hw hw') hsmall
(fun _ hz => rectangle_norm_bound he hWpos.le hWR hz)
(Ξ΅ := d*Real.log (s.period : ℝ)/2) (a := d^2*s.primes.card/64) (h := h)
Β· positivity
Β· rw [← s.log_period]
have hh := rectangle_log_excess hd hd1.le hlogT hlogP hlarge
linarith
Β· positivity
Β· have hc : d^3 ≀ d^2 := by nlinarith [sq_nonneg d, mul_nonneg (sq_nonneg d) (by linarith : 0 ≀ 1-d)]
have hh := mul_le_mul_of_nonneg_right hc hr'.le
nlinarith [mul_nonneg (sq_nonneg d) hr'.le]
Β· exact rectangle_hoeffding_exponent hr' hd.le (by linarith) hlogP hV hVup
Β· exact rectangle_cube_growth hd hd1.le hlarge
end GaussianMoat
end OAI