import Mathlib import OAI.Probability.GaussianInformation.Kernel.Model namespace OAI open MeasureTheory ProbabilityTheory Metric Set open scoped ENNReal namespace KernelPotential open Filter open scoped Topology NNReal namespace Geometry section Cover variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [FiniteDimensional ℝ E] lemma card_separated_le (s : Finset E) {R δ : ℝ} (hR : 0 ≤ R) (hδ : 0 < δ) (hs : ∀ c ∈ s, ‖c‖ ≤ R) (hsep : ∀ c ∈ s, ∀ d ∈ s, c ≠ d → δ ≤ ‖c - d‖) : (s.card : ℝ) ≤ ((2 * R + δ) / δ) ^ Module.finrank ℝ E := by borelize E let μ : Measure E := Measure.addHaar have hsmall : 0 < δ / 2 := by positivity have hlarge : 0 < R + δ / 2 := by positivity let A := ⋃ c ∈ s, ball c (δ / 2) have hdisj : (s : Set E).PairwiseDisjoint (fun c => ball c (δ / 2)) := by intro c hc d hd hcd apply ball_disjoint_ball rw [dist_eq_norm] convert hsep c hc d hd hcd using 1 ring have hsub : A ⊆ ball (0 : E) (R + δ / 2) := by refine iUnion₂_subset fun x hx => ball_subset_ball' ?_ rw [dist_zero_right] linarith [hs x hx] have hb : (s.card : ℝ≥0∞) * ENNReal.ofReal ((δ / 2)^Module.finrank ℝ E) * μ (ball 0 1) ≤ ENNReal.ofReal ((R + δ / 2)^Module.finrank ℝ E) * μ (ball 0 1) := by calc _ = μ A := by rw [measure_biUnion_finset hdisj (fun _ _ => measurableSet_ball)] simp only [μ.addHaar_ball_of_pos _ hsmall, Finset.sum_const, nsmul_eq_mul, mul_assoc] _ ≤ μ (ball 0 (R + δ / 2)) := measure_mono hsub _ = _ := μ.addHaar_ball_of_pos _ hlarge have hb' := (ENNReal.mul_le_mul_iff_left (measure_ball_pos μ (0 : E) zero_lt_one).ne' measure_ball_lt_top.ne).mp hb have hbR : (s.card : ℝ) * (δ / 2)^Module.finrank ℝ E ≤ (R + δ / 2)^Module.finrank ℝ E := by simpa [ENNReal.toReal_ofReal (pow_nonneg hsmall.le _)] using ENNReal.toReal_le_of_le_ofReal (pow_nonneg hlarge.le _) hb' calc (s.card : ℝ) ≤ (R + δ / 2)^Module.finrank ℝ E / (δ / 2)^Module.finrank ℝ E := (le_div_iff₀ (pow_pos hsmall _)).mpr hbR _ = ((2 * R + δ) / δ)^Module.finrank ℝ E := by rw [← div_pow] congr 1 field_simp lemma exists_ball_cover_card_le {R δ : ℝ} (hR : 0 ≤ R) (hδ : 0 < δ) : ∃ s : Finset E, (∀ c ∈ s, ‖c‖ ≤ R) ∧ (∀ x : E, ‖x‖ ≤ R → ∃ c ∈ s, dist x c ≤ δ) ∧ (s.card : ℝ) ≤ ((2 * R + δ) / δ)^Module.finrank ℝ E := by classical let ε : ℝ≥0 := ⟨δ, hδ.le⟩ let A := closedBall (0 : E) R have hεpos : 0 < ε := hδ have hε : ε ≠ 0 := ne_of_gt hεpos obtain ⟨N, _, hNfin, hNcov⟩ := Metric.exists_finite_isCover_of_isCompact (s := A) (ε := ε / 2) (by positivity) (isCompact_closedBall _ _) have hp : Metric.packingNumber ε A < ⊤ := by calc _ = Metric.packingNumber (2 * (ε / 2)) A := by congr 1; ring _ ≤ Metric.externalCoveringNumber (ε / 2) A := Metric.packingNumber_two_mul_le_externalCoveringNumber _ _ _ ≤ N.encard := hNcov.externalCoveringNumber_le_encard _ < ⊤ := hNfin.encard_lt_top let S := Metric.maximalSeparatedSet ε A have hSfin : S.Finite := Set.encard_lt_top_iff.mp (by change (Metric.maximalSeparatedSet ε A).encard < ⊤ rw [Metric.encard_maximalSeparatedSet hp.ne] exact hp) have hSsub : ∀ c ∈ hSfin.toFinset, ‖c‖ ≤ R := by intro c hc have : c ∈ S := by simpa using hc simpa [A] using (Metric.maximalSeparatedSet_subset this) refine ⟨hSfin.toFinset, hSsub, ?_, card_separated_le _ hR hδ hSsub ?_⟩ · intro x hx have hcov := Metric.isCover_iff_subset_iUnion_closedBall.mp (Metric.isCover_maximalSeparatedSet hp.ne) have hm : x ∈ A := by simpa [A] using hx obtain ⟨c, hc, hxc⟩ := mem_iUnion₂.mp (hcov hm) exact ⟨c, by simpa using hc, hxc⟩ · intro c hc d hd hcd have hs := Metric.isSeparated_maximalSeparatedSet (ε := ε) (A := A) (by simpa using hc) (by simpa using hd) hcd have ht := ENNReal.toReal_mono (edist_ne_top c d) hs.le change δ ≤ (edist c d).toReal at ht simpa [ε, edist_dist, dist_eq_norm] using ht end Cover section PlaneCover variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [FiniteDimensional ℝ E] lemma exists_affine_tube_cover (V : AffineSubspace ℝ E) (hV : (V : Set E).Nonempty) {R δ : ℝ} (hR : 0 ≤ R) (hδ : 0 < δ) : ∃ s : Finset E, (∀ x : E, ‖x‖ ≤ R → infDist x (V : Set E) ≤ δ → ∃ c ∈ s, dist x c ≤ 2 * δ) ∧ (s.card : ℝ) ≤ ((4 * R + 5 * δ) / δ)^Module.finrank ℝ V.direction := by classical by_cases hempty : {x : E | ‖x‖ ≤ R ∧ infDist x (V : Set E) ≤ δ}.Nonempty · obtain ⟨x₀, hx₀n, hx₀d⟩ := hempty obtain ⟨v₀, hv₀V, hv₀d⟩ := V.closed_of_finiteDimensional.exists_infDist_eq_dist hV x₀ have hn₀ : ‖v₀‖ ≤ R + δ := by have ht := dist_triangle v₀ x₀ 0 rw [dist_zero_right, dist_zero_right, dist_comm v₀ x₀, ← hv₀d] at ht linarith obtain ⟨s, _, hcover, hcard⟩ := exists_ball_cover_card_le (E := V.direction) (R := 2 * (R + δ)) (δ := δ) (by positivity) hδ let f : V.direction ↪ E := ⟨fun v => (v : E) + v₀, by intro v w h exact Subtype.ext (add_right_cancel h)⟩ refine ⟨s.map f, ?_, ?_⟩ · intro x hxn hxd obtain ⟨v, hvV, hvd⟩ := V.closed_of_finiteDimensional.exists_infDist_eq_dist hV x have hnv : ‖v‖ ≤ R + δ := by have ht := dist_triangle v x 0 rw [dist_zero_right, dist_zero_right, dist_comm v x, ← hvd] at ht linarith let u : V.direction := ⟨v - v₀, V.vsub_mem_direction hvV hv₀V⟩ have hnu : ‖u‖ ≤ 2 * (R + δ) := by change ‖v - v₀‖ ≤ _ exact (norm_sub_le v v₀).trans (by linarith) obtain ⟨a, ha, hu⟩ := hcover u hnu refine ⟨f a, Finset.mem_map.mpr ⟨a, ha, rfl⟩, ?_⟩ have he : dist v (f a) = dist u a := by rw [dist_eq_norm, dist_eq_norm] change ‖v - ((a : E) + v₀)‖ = ‖(v - v₀) - (a : E)‖ congr 1 abel calc dist x (f a) ≤ dist x v + dist v (f a) := dist_triangle _ _ _ _ ≤ δ + δ := add_le_add (hvd ▸ hxd) (he ▸ hu) _ = 2 * δ := by ring · simpa only [Finset.card_map, show 2 * (2 * (R + δ)) + δ = 4 * R + 5 * δ by ring] using hcard · refine ⟨∅, ?_, ?_⟩ · intro x hxn hxd exact (hempty ⟨x, hxn, hxd⟩).elim · simp only [Finset.card_empty, Nat.cast_zero] positivity end PlaneCover end Geometry end KernelPotential end OAI