module public import ConditionalGoldbach.Definitions.Operators public import ConditionalGoldbach.Proofs.Elementary public import Mathlib.Analysis.Asymptotics.SpecificAsymptotics /-! Natural density on the positive even integers and the norm of the full residual operator. -/ @[expose] public section set_option autoImplicit false noncomputable section open Filter Finset open scoped Topology BigOperators namespace ConditionalGoldbach namespace ResidualAverage attribute [local instance] Classical.propDecidable /-- Index zero is the first positive even integer. -/ def evenAt (j : ℕ) : ℕ := 2 * (j + 1) /-- The mean of the first `M` entries, with the usual real division convention at zero. -/ def prefixMean (u : ℕ → ℝ) (M : ℕ) : ℝ := (∑ j ∈ range M, u j) / M /-- The indices in this block correspond to the positive integers `X < k ≤ 2*X`. -/ def blockMean (u : ℕ → ℝ) (X : ℕ) : ℝ := prefixMean (fun j => u (X + j)) X /-- The real indicator of a set of indices. -/ def hit (G : Set ℕ) (j : ℕ) : ℝ := if j ∈ G then 1 else 0 /-- The relative frequency of a set of indices in a finite prefix. -/ def frequency (G : Set ℕ) (M : ℕ) : ℝ := prefixMean (hit G) M theorem frequency_eq_card (G : Set ℕ) (M : ℕ) : frequency G M = (((range M).filter (· ∈ G)).card : ℝ) / M := by simp only [frequency, prefixMean, hit, sum_boole] /-- One set of positive even integers has relative natural density one, and along it the nonnegative ratio is eventually below each positive threshold. -/ def DensityOneLimit (r : ℕ → ℝ) : Prop := ∃ E : Set ℕ, (∀ N ∈ E, 0 < N ∧ Even N) ∧ Tendsto (frequency {j | evenAt j ∈ E}) atTop (𝓝 1) ∧ ∀ ε : ℝ, 0 < ε → ∃ n : ℕ, ∀ N ∈ E, n ≤ N → r N < ε private lemma prefixMean_nonneg {u : ℕ → ℝ} (hu : ∀ j, 0 ≤ u j) (M : ℕ) : 0 ≤ prefixMean u M := div_nonneg (sum_nonneg fun j _ => hu j) (Nat.cast_nonneg _) private lemma prefixMean_mono {u v : ℕ → ℝ} (h : ∀ j, u j ≤ v j) (M : ℕ) : prefixMean u M ≤ prefixMean v M := div_le_div_of_nonneg_right (sum_le_sum fun j _ => h j) (Nat.cast_nonneg _) private lemma frequency_compl {G : Set ℕ} {M : ℕ} (hM : 0 < M) : frequency Gᶜ M = 1 - frequency G M := by have he (j : ℕ) : hit Gᶜ j = 1 - hit G j := by by_cases hj : j ∈ G <;> simp [hit, hj] simp only [frequency, prefixMean, he, sum_sub_distrib, sum_const, card_range, nsmul_eq_mul, mul_one, sub_div, div_self (show (M : ℝ) ≠ 0 by positivity)] private lemma frequency_compl_tendsto (G : Set ℕ) {l : ℝ} (h : Tendsto (frequency G) atTop (𝓝 l)) : Tendsto (frequency Gᶜ) atTop (𝓝 (1 - l)) := by apply (tendsto_const_nhds.sub h).congr' filter_upwards [eventually_ge_atTop 1] with M hM exact (frequency_compl (G := G) hM).symm /-- Choose an index if some later prefix makes its entry at most the moving threshold. Every omitted index in a prefix is then larger than that prefix's own threshold. -/ private lemma extract_indices (u : ℕ → ℝ) (hu : ∀ j, 0 ≤ u j) (h : Tendsto (prefixMean u) atTop (𝓝 0)) : ∃ G : Set ℕ, Tendsto (frequency G) atTop (𝓝 1) ∧ ∀ ε : ℝ, 0 < ε → ∃ n : ℕ, ∀ j ∈ G, n ≤ j → u j < ε := by let g (M : ℕ) := Real.sqrt (prefixMean u M) + 1 / ((M : ℝ) + 1) have hg : Tendsto g atTop (𝓝 0) := by simpa [g] using h.sqrt.add (tendsto_one_div_add_atTop_nhds_zero_nat (𝕜 := ℝ)) have hgpos (M : ℕ) : 0 < g M := by dsimp [g]; positivity let G : Set ℕ := {j | ∃ M ≥ j, u j ≤ g M} have hbad : Tendsto (frequency Gᶜ) atTop (𝓝 0) := by apply squeeze_zero (fun M => prefixMean_nonneg (u := hit Gᶜ) (fun j => by unfold hit; split_ifs <;> norm_num) M) _ hg intro M have hcount : ∑ j ∈ range M, hit Gᶜ j * g M ≤ ∑ j ∈ range M, u j := by apply sum_le_sum intro j hj by_cases hjG : j ∈ Gᶜ · simpa only [hit, ite_eq_left hjG, one_mul] using (le_of_not_ge (fun hju => hjG ⟨M, (mem_range.mp hj).le, hju⟩) : g M ≤ u j) · simpa only [hit, ite_eq_right hjG, zero_mul] using hu j have hprod : frequency Gᶜ M * g M ≤ prefixMean u M := by unfold frequency prefixMean rw [div_mul_eq_mul_div, sum_mul] exact div_le_div_of_nonneg_right hcount (Nat.cast_nonneg _) have hs : prefixMean u M ≤ (g M) ^ 2 := by rw [← Real.sq_sqrt (prefixMean_nonneg hu M)] apply (sq_le_sq₀ (Real.sqrt_nonneg _) (hgpos M).le).mpr dsimp [g] exact le_add_of_nonneg_right (by positivity) exact le_of_mul_le_mul_right (hprod.trans (by simpa only [pow_two] using hs)) (hgpos M) refine ⟨G, by simpa using frequency_compl_tendsto Gᶜ hbad, ?_⟩ intro ε hε obtain ⟨n, hn⟩ := eventually_atTop.mp (hg.eventually_lt_const hε) exact ⟨n, fun j ⟨M, hjM, hju⟩ hj => hju.trans_lt (hn M (hj.trans hjM))⟩ /-- Vanishing clipped square mean is equivalent to convergence to zero on one density-one set of positive even integers. The entries may be unbounded on its complement. -/ theorem clippedMean_iff {r : ℕ → ℝ} (hr : ∀ N, 0 ≤ r N) : Tendsto (prefixMean fun j => min 1 (r (evenAt j) ^ 2)) atTop (𝓝 0) ↔ DensityOneLimit r := by let u (j : ℕ) := min 1 (r (evenAt j) ^ 2) have hu (j : ℕ) : 0 ≤ u j := le_min zero_le_one (sq_nonneg _) constructor · intro h obtain ⟨G, hG, hsmall⟩ := extract_indices u hu h let E := evenAt '' G have hmem (j : ℕ) : evenAt j ∈ E ↔ j ∈ G := by constructor · rintro ⟨i, hi, he⟩ have hij : i = j := by unfold evenAt at he; omega simpa only [← hij] using hi · exact fun hj => ⟨j, hj, rfl⟩ refine ⟨E, ?_, ?_, ?_⟩ · rintro N ⟨j, _, rfl⟩ exact ⟨by unfold evenAt; omega, even_two_mul _⟩ · have he : {j | evenAt j ∈ E} = G := Set.ext hmem rwa [he] · intro ε hε obtain ⟨n, hn⟩ := hsmall (min 1 (ε ^ 2)) (lt_min zero_lt_one (sq_pos_of_pos hε)) refine ⟨evenAt n, ?_⟩ rintro _ ⟨j, hj, rfl⟩ hnj have hjn : n ≤ j := by unfold evenAt at hnj; omega by_contra! he exact (hn j hj hjn).not_ge (min_le_min_left 1 ((sq_le_sq₀ hε.le (hr _)).2 he)) · rintro ⟨E, _, hd, hE⟩ let G : Set ℕ := {j | evenAt j ∈ E} let v (j : ℕ) := if j ∈ G then r (evenAt j) else 0 have hv : Tendsto v atTop (𝓝 0) := by apply Metric.tendsto_atTop.mpr intro ε hε obtain ⟨n, hn⟩ := hE ε hε refine ⟨n, fun j hj => ?_⟩ by_cases hjG : j ∈ G · simpa [v, hjG, Real.dist_eq, abs_of_nonneg (hr _)] using hn (evenAt j) hjG (by unfold evenAt; omega) · simpa [v, hjG] using hε have hv2 : Tendsto (prefixMean fun j => v j ^ 2) atTop (𝓝 0) := by change Tendsto (fun M : ℕ => (∑ j ∈ range M, v j ^ 2) / M) atTop (𝓝 0) simpa [div_eq_inv_mul] using (hv.pow 2).cesaro have hb : Tendsto (frequency Gᶜ) atTop (𝓝 0) := by simpa using frequency_compl_tendsto G hd apply squeeze_zero (prefixMean_nonneg hu) _ (by simpa using hv2.add hb) intro M calc prefixMean u M ≤ prefixMean (fun j => v j ^ 2 + hit Gᶜ j) M := by apply prefixMean_mono intro j by_cases hj : j ∈ G · simp [v, u, hit, hj] · simp [v, u, hit, hj] _ = _ := by simp [prefixMean, frequency, sum_add_distrib, add_div] /-- The untruncated dyadic block means also suffice. -/ theorem prefixMean_of_blockMean {u : ℕ → ℝ} (hu : ∀ j, 0 ≤ u j) (h : Tendsto (blockMean u) atTop (𝓝 0)) : Tendsto (prefixMean u) atTop (𝓝 0) := by let S (m : ℕ) := ∑ j ∈ range m, u j have hmono {m n : ℕ} (hmn : m ≤ n) : S m ≤ S n := sum_le_sum_of_subset_of_nonneg (range_mono hmn) (fun j _ _ => hu j) apply Metric.tendsto_atTop.mpr intro ε hε let δ := ε / 4 have hδ : 0 < δ := by dsimp [δ]; positivity obtain ⟨K, hK⟩ := eventually_atTop.mp (h.eventually_lt_const hδ) let B := 2 * K + 4 have hbound (m : ℕ) : S m ≤ S B + 2 * δ * m := by induction m using Nat.strong_induction_on with | h m ih => by_cases hbase : m ≤ B · exact (hmono hbase).trans (le_add_of_nonneg_right (by positivity)) · let x := (m + 1) / 2 have hx : K ≤ x ∧ 0 < x ∧ x < m ∧ m ≤ x + x ∧ 3 * x ≤ 2 * m := by dsimp [x, B] at *; omega have hblock := hK x hx.1 have hsum : ∑ j ∈ range x, u (x + j) ≤ δ * x := (div_le_iff₀ (Nat.cast_pos.mpr hx.2.1)).mp hblock.le have hsplit : S (x + x) = S x + ∑ j ∈ range x, u (x + j) := sum_range_add u x x have hscale : (3 : ℝ) * x ≤ 2 * m := by exact_mod_cast hx.2.2.2.2 calc S m ≤ S (x + x) := hmono hx.2.2.2.1 _ ≤ (S B + 2 * δ * x) + δ * x := by rw [hsplit]; exact add_le_add (ih x hx.2.2.1) hsum _ ≤ _ := by nlinarith only [mul_le_mul_of_nonneg_left hscale hδ.le] obtain ⟨n, hn⟩ := eventually_atTop.mp ((tendsto_const_div_atTop_nhds_zero_nat (S B)).eventually_lt_const hδ) refine ⟨max n 1, fun m hm => ?_⟩ have hmpos : (0 : ℝ) < m := Nat.cast_pos.mpr (le_trans (le_max_right _ _) hm) have hratio : prefixMean u m ≤ S B / m + 2 * δ := by apply (div_le_iff₀ hmpos).mpr calc S m ≤ S B + 2 * δ * m := hbound m _ = _ := by field_simp have hfirst := hn m (le_trans (le_max_left _ _) hm) rw [Real.dist_eq, sub_zero, abs_of_nonneg (prefixMean_nonneg hu m)] dsimp [δ] at hfirst hratio linarith /-- For nonnegative entries the full prefixes and all the blocks `X < k ≤ 2*X` have zero mean in the limit simultaneously. -/ theorem prefixMean_iff_blockMean {u : ℕ → ℝ} (hu : ∀ j, 0 ≤ u j) : Tendsto (prefixMean u) atTop (𝓝 0) ↔ Tendsto (blockMean u) atTop (𝓝 0) := by refine ⟨fun h => ?_, prefixMean_of_blockMean hu⟩ have h2 : Tendsto (fun X : ℕ => X + X) atTop atTop := tendsto_atTop.mpr fun K => (eventually_ge_atTop K).mono fun X hX => by omega have ht : Tendsto (fun X : ℕ => 2 * prefixMean u (X + X)) atTop (𝓝 0) := by simpa using (h.comp h2).const_mul 2 apply squeeze_zero (fun X => prefixMean_nonneg (fun j => hu (X+j)) X) _ ht intro X have he : 2 * prefixMean u (X+X) = prefixMean u X + blockMean u X := by unfold blockMean prefixMean rw [sum_range_add, Nat.cast_add] by_cases hx : X = 0 · simp [hx] · have hx' : (X : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hx have hxx : (X : ℝ) + X ≠ 0 := by positivity field_simp ring rw [he] exact le_add_of_nonneg_left (prefixMean_nonneg hu X) /-- The project's actual norm ratio on its standard strip, extended by zero over the finite prefix where the strip parameters are unavailable. -/ def residualRatio (N : ℕ) : ℝ := if h : Real.exp 10 < (N : ℝ) then letI : NeZero N := ⟨by have := (large_parameters h).1; omega⟩ ‖residualOperator (large_parameters h).2.2 N‖ / singularSeries N else 0 theorem residualRatio_nonneg (N : ℕ) : 0 ≤ residualRatio N := by unfold residualRatio split_ifs · exact div_nonneg (norm_nonneg _) (singularSeries_pos N).le · exact le_rfl /-- The clipped mean criterion for the full residual operator. -/ theorem residual_clippedMean_iff : Tendsto (prefixMean fun j => min 1 (residualRatio (evenAt j) ^ 2)) atTop (𝓝 0) ↔ DensityOneLimit residualRatio := clippedMean_iff residualRatio_nonneg /-- A mean square estimate at all prefixes gives one density-one set for the residual. -/ theorem residual_of_meanSquare (h : Tendsto (prefixMean fun j => residualRatio (evenAt j) ^ 2) atTop (𝓝 0)) : DensityOneLimit residualRatio := residual_clippedMean_iff.mp (squeeze_zero (prefixMean_nonneg fun _ => le_min zero_le_one (sq_nonneg _)) (prefixMean_mono fun _ => min_le_right _ _) h) /-- A mean square estimate for `X < k ≤ 2*X` gives the same density-one conclusion. -/ theorem residual_of_blockMeanSquare (h : Tendsto (blockMean fun j => residualRatio (evenAt j) ^ 2) atTop (𝓝 0)) : DensityOneLimit residualRatio := residual_of_meanSquare (prefixMean_of_blockMean (fun _ => sq_nonneg _) h) end ResidualAverage end ConditionalGoldbach