Datasets:
File size: 11,106 Bytes
8ca48d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | /-
Copyright 2025 The Formal Conjectures Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
module
public import Mathlib.Analysis.SpecialFunctions.Log.Basic
public import FormalConjecturesForMathlib.Algebra.Order.Group.Pointwise.Interval
public import FormalConjecturesForMathlib.Data.Set.Interval
public import FormalConjecturesForMathlib.Order.Interval.Finset.Basic
public import FormalConjecturesForMathlib.Order.Interval.Finset.Nat
public import Batteries.Util.ProofWanted
public import Mathlib.Tactic
@[expose] public section
open Filter
open scoped Topology
namespace Set
/--
Given a set `S` and an element `b` in an order `β`, where all intervals bounded above are finite,
we define the partial density of `S` (relative to a set `A`) to be the proportion of elements in
`{x ∈ A | x < b}` that lie in `S ∩ A`.
This definition was inspired from https://github.com/b-mehta/unit-fractions
-/
@[inline]
noncomputable abbrev partialDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) (b : β) : ℝ :=
((S ∩ A) ∩ Iio b).ncard / (A ∩ Iio b).ncard
theorem partialDensity_le_one {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) (b : β) : S.partialDensity A b ≤ 1 := by
apply div_le_one_of_le₀ _ (Nat.cast_nonneg _)
exact mod_cast Set.ncard_le_ncard <| Set.inter_subset_inter_left _ inter_subset_right
/--
Given a set `S` in an order `β`, where all intervals bounded above are finite, we define the upper
density of `S` (relative to a set `A`) to be the limsup of the partial densities of `S`
(relative to `A`) for `b → ∞`.
-/
noncomputable def upperDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : ℝ :=
atTop.limsup fun (b : β) ↦ S.partialDensity A b
/--
Given a set `S` in an order `β`, where all intervals bounded above are finite, we define the lower
density of `S` (relative to a set `A`) to be the liminf of the partial densities of `S`
(relative to `A`) for `b → ∞`.
-/
noncomputable def lowerDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : ℝ :=
atTop.liminf fun (b : β) ↦ S.partialDensity A b
theorem lowerDensity_le_one {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : S.lowerDensity A ≤ 1 := by
by_cases h : atTop (α := β) = ⊥
· simp [h, Set.lowerDensity, Filter.liminf_eq]
· have : (atTop (α := β)).NeBot := ⟨h⟩
apply Real.sSup_le (fun x hx ↦ ?_) one_pos.le
simpa using hx.mono fun y hy ↦ hy.trans (Set.partialDensity_le_one _ _ y)
theorem lowerDensity_nonneg {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : 0 ≤ S.lowerDensity A := by
rw [Set.lowerDensity, Filter.liminf_eq]
exact (em _).elim (le_csSup · <| .of_forall fun _ ↦ by positivity)
(Real.sSup_of_not_bddAbove · |>.ge)
/--
A set `S` in an order `β` where all intervals bounded above are finite is said to have
density `α : ℝ` (relative to a set `A`) if the proportion of `x ∈ S` such that `x < n`
in `A` tends to `α` as `n → ∞`.
When `β = ℕ` this by default defines the natural density of a set
(i.e., relative to all of `ℕ`).
-/
def HasDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (α : ℝ) (A : Set β := Set.univ) : Prop :=
Tendsto (fun (b : β) => S.partialDensity A b) atTop (𝓝 α)
/--
A set `S` in an order `β` where all intervals bounded above are finite is said to have
positive density (relative to a set `A`) if there exists a positive `α : ℝ` such that
`S` has density `α` (relative to a set `A`).
-/
def HasPosDensity {β : Type*} [Preorder β] [LocallyFiniteOrderBot β]
(S : Set β) (A : Set β := Set.univ) : Prop :=
∃ α > 0, S.HasDensity α A
/--
The two-sided partial natural density of a set of integers, counted inside the interval
`[-N, N]`.
-/
noncomputable def intPartialDensity (S : Set ℤ) (N : ℕ) : ℝ :=
open scoped Classical in
(((Finset.Icc (-(N : ℤ)) (N : ℤ)).filter (fun m => m ∈ S)).card : ℝ) /
(2 * (N : ℝ) + 1)
/-- A set of integers has two-sided natural density `α`. -/
def HasIntDensity (S : Set ℤ) (α : ℝ) : Prop :=
Tendsto (fun N : ℕ => S.intPartialDensity N) atTop (𝓝 α)
@[simp]
theorem intPartialDensity_empty (N : ℕ) : intPartialDensity (∅ : Set ℤ) N = 0 := by
simp [intPartialDensity]
namespace HasIntDensity
@[simp]
theorem empty : HasIntDensity (∅ : Set ℤ) 0 := by
simp [HasIntDensity]
end HasIntDensity
namespace HasDensity
-- TODO(mercuris): generalise these to non-univ `A`
/-- In a non-trivial partial order with a least element, the set of all
elements has density one. -/
@[simp]
theorem univ {β : Type*} [PartialOrder β] [LocallyFiniteOrder β] [OrderBot β] [Nontrivial β] :
(@Set.univ β).HasDensity 1 := by
by_cases h : atTop (α := β) = ⊥
· simp [h, HasDensity]
· simp [HasDensity, partialDensity]
let ⟨b, hb⟩ := Set.Iio_eventually_ncard_ne_zero β
refine tendsto_const_nhds.congr' ?_
exact (eventually_ge_atTop b).mono fun n hn ↦ (div_self <| mod_cast hb n hn).symm
theorem univ_nat_hasDensity_one : (@Set.univ ℕ).HasDensity 1 := univ
@[simp]
theorem empty {β : Type*} [Preorder β] [LocallyFiniteOrderBot β] (A : Set β := Set.univ) :
Set.HasDensity (∅ : Set β) 0 A := by
simpa [HasDensity, partialDensity] using tendsto_const_nhds
theorem mono {β : Type*} [PartialOrder β] [LocallyFiniteOrder β] [OrderBot β]
{S T : Set β} {αS αT : ℝ} [(atTop (α := β)).NeBot] (h : S ⊆ T) (hS : S.HasDensity αS)
(hT : T.HasDensity αT) : αS ≤ αT := by
rw [HasDensity] at hS hT
apply le_of_tendsto_of_tendsto hS hT
filter_upwards [eventually_ge_atTop ⊥] with b hb
apply div_le_div_of_nonneg_right
grw [Set.ncard_le_ncard (inter_subset_inter_left _ (inter_subset_inter_left _ h))]
exact Nat.cast_nonneg _
theorem nonneg {β : Type*} [Preorder β] [LocallyFiniteOrderBot β] [(atTop : Filter β).NeBot]
{S : Set β} {α : ℝ} (h : S.HasDensity α) : 0 ≤ α :=
le_of_tendsto_of_tendsto' empty h fun b => by simp [div_nonneg, partialDensity]
end Set.HasDensity
namespace Nat
open Set
/--
The natural density of the set of even numbers is `1 / 2`.
-/
theorem hasDensity_even : {n : ℕ | Even n}.HasDensity (1 / 2) := by
simp [HasDensity, partialDensity]
have h {n : ℕ} (hn : 1 ≤ n) : (({n : ℕ | Even n} ∩ Iio n).ncard : ℝ) / n =
if Even n then 2⁻¹ else (n + 1 : ℝ) / n * 2⁻¹ := by
split_ifs with h
· rw [← image_mul_two_Iio_even h, ncard_image_of_injective _
(mul_right_injective₀ (by simp)), ncard_Iio,
cast_div_charZero (even_iff_two_dvd.mp h), cast_ofNat,
div_div_cancel_left' <| cast_ne_zero.2 (by linarith)]
· replace h : Even (n + 1) := by simpa [Nat.even_add', ← Nat.not_even_iff_odd]
rw [← image_mul_two_Iio n, ncard_image_of_injective _
(mul_right_injective₀ (by simp)), ncard_Iio,
cast_div (even_iff_two_dvd.mp h) (by norm_num), cast_add]; ring
refine Tendsto.congr' (eventually_atTop.2 ⟨1, fun n hn => (h hn).symm⟩)
(Tendsto.if' tendsto_const_nhds ?_)
replace h : Tendsto (fun (k : ℕ) => 1 + 1 / (k : ℝ)) atTop (𝓝 1) := by
simpa using Tendsto.const_add (M := ℝ) _ tendsto_one_div_atTop_nhds_zero_nat
simpa using Tendsto.mul_const _ <|
Tendsto.congr' (eventually_atTop.2 ⟨1, fun k hk => by field_simp⟩) h
/-- A finite set has natural density zero. -/
theorem hasDensity_zero_of_finite {S : Set ℕ} (h : S.Finite) : S.HasDensity 0 := by
simp [HasDensity, partialDensity]
have (n : ℕ) : ((S ∩ Set.Iio n).ncard : ℝ) / n ≤ S.ncard / n := by
by_cases h₀ : n = 0; simp [h₀]
exact div_le_div₀ (by simp) (by simpa using Set.ncard_inter_le_ncard_left _ _ h)
(by simpa using n.pos_of_ne_zero h₀) le_rfl
exact tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds
(tendsto_const_div_atTop_nhds_zero_nat _)
(fun _ => div_nonneg (cast_nonneg _) (cast_nonneg _)) this
/-- A finite set of natural numbers has density `0` relative to any infinite set `A` of natural
numbers, because the number of elements of `A` below `b` tends to infinity. -/
lemma hasDensity_zero_of_finite_of_infinite {T A : Set ℕ} (hT : T.Finite) (hA : A.Infinite) :
T.HasDensity 0 A := by
have hden : Filter.Tendsto (fun b : ℕ => ((A ∩ Set.Iio b).ncard : ℝ)) Filter.atTop
Filter.atTop := by
refine tendsto_natCast_atTop_atTop.comp (Filter.tendsto_atTop_atTop_of_monotone
(fun m n hmn => Set.ncard_le_ncard (Set.inter_subset_inter_right _
(Set.Iio_subset_Iio hmn)) ((Set.finite_Iio n).inter_of_right A)) fun N => ?_)
obtain ⟨t, htA, htc⟩ := hA.exists_subset_card_eq N
refine ⟨t.sup id + 1, ?_⟩
have hsub : (t : Set ℕ) ⊆ A ∩ Set.Iio (t.sup id + 1) := fun x hx =>
⟨htA hx, Nat.lt_succ_of_le (Finset.le_sup (f := id) hx)⟩
simpa [htc] using Set.ncard_le_ncard hsub ((Set.finite_Iio _).inter_of_right A)
refine tendsto_of_tendsto_of_tendsto_of_le_of_le tendsto_const_nhds
(hden.const_div_atTop (T.ncard : ℝ)) (fun b => ?_) (fun b => ?_)
· exact div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _)
· refine div_le_div_of_nonneg_right ?_ (Nat.cast_nonneg _)
refine Nat.cast_le.2 ?_
exact Set.ncard_le_ncard (fun x hx => hx.1.1) hT
/-- A set of positive natural density is infinite. -/
theorem infinite_of_hasDensity_pos {S : Set ℕ} {α : ℝ} (h : S.HasDensity α) (hα : α ≠ 0) :
S.Infinite :=
mt hasDensity_zero_of_finite fun h' => hα (tendsto_nhds_unique h h')
end Nat
/-! ## Logarithmic Density -/
section LogarithmicDensity
open Finset Real
/--
A set `A` of natural numbers has logarithmic density `d` if the sequence
$(1 / \log n) \cdot \sum_{k \in A, k \le n} (1/k)$ converges to `d`.
Logarithmic density is a weaker notion than natural density: if a set has natural density `d`,
then it also has logarithmic density `d` (see `Set.HasDensity.hasLogDensity`), but the converse
is false.
-/
def Set.HasLogDensity (A : Set ℕ) (d : ℝ) : Prop :=
open scoped Classical in
Tendsto (fun n : ℕ => (∑ k ≤ n with k ∈ A, (k : ℝ)⁻¹ / .log n : ℝ)) atTop (𝓝 d)
/-- If a set has natural density `d`, then it also has logarithmic density `d`. -/
proof_wanted Set.HasDensity.hasLogDensity {A : Set ℕ} {d : ℝ} (h : A.HasDensity d) : A.HasLogDensity d
end LogarithmicDensity
|