cfahlgren1's picture
download
raw
17.1 kB
23
def profile (x : V) : V → ℝ := fun z => dist x z
25
def polytope : Set (V → ℝ) := convexHull ℝ (Set.range (profile (V := V)))
28
lemma profile_mem (x : V) : profile x ∈ polytope :=
proof · 1 line
  subset_convexHull ℝ _ (Set.mem_range_self x)
32
lemma polytope_convex : Convex ℝ (polytope (V := V)) := convex_convexHull ℝ _
34
lemma polytope_compact : IsCompact (polytope (V := V)) :=
proof · 1 line
  (Set.finite_range (profile (V := V))).isCompact_convexHull ℝ
37
lemma profile_norm_sub (x y : V) : ‖profile x - profile y‖ = dist x y := by
proof · 12 lines
  apply le_antisymm
  · apply (pi_norm_le_iff_of_nonneg dist_nonneg).2
    intro z
    simp only [Pi.sub_apply, profile, Real.norm_eq_abs]
    apply abs_sub_le_iff.2
    constructor
    · linarith [dist_triangle x y z]
    · have h := dist_triangle y x z
      rw [dist_comm y x] at h
      linarith
  · have h := norm_le_pi_norm (profile x - profile y) y
    simpa [profile, Real.norm_eq_abs, abs_of_nonneg dist_nonneg] using h
51
lemma profile_isometry : Isometry (profile (V := V)) :=
proof · 1 line
  Isometry.of_dist_eq fun x y => by rw [dist_eq_norm, profile_norm_sub]
55
lemma norm_coordinate_convex (c : V → ℝ) (i : V) :
    Convex ℝ {x : V → ℝ | ‖x - c‖ ≤ x i} := by
proof · 15 lines
  intro x hx y hy a b ha hb hab
  change ‖a • x + b • y - c‖ ≤ (a • x + b • y) i
  have heq : a • x + b • y - c = a • (x - c) + b • (y - c) := by
    ext j
    simp only [Pi.sub_apply, Pi.add_apply, Pi.smul_apply, smul_eq_mul]
    linear_combination c j * hab
  rw [heq]
  calc
    ‖a • (x - c) + b • (y - c)‖ ≤ ‖a • (x - c)‖ + ‖b • (y - c)‖ := norm_add_le _ _
    _ = a * ‖x - c‖ + b * ‖y - c‖ := by
      rw [norm_smul, norm_smul, Real.norm_eq_abs, Real.norm_eq_abs,
        abs_of_nonneg ha, abs_of_nonneg hb]
    _ ≤ a * x i + b * y i := add_le_add (mul_le_mul_of_nonneg_left hx ha)
      (mul_le_mul_of_nonneg_left hy hb)
    _ = (a • x + b • y) i := rfl
73
theorem radius_eq_coordinate {q : V → ℝ} (hq : q ∈ polytope) (x : V) :
    ‖q - profile x‖ = q x := by
proof · 10 lines
  have hupper : ‖q - profile x‖ ≤ q x := by
    apply convexHull_min (t := {y : V → ℝ | ‖y - profile x‖ ≤ y x}) ?_
      (norm_coordinate_convex (profile x) x) hq
    rintro _ ⟨y, rfl⟩
    exact (profile_norm_sub y x).le
  have hlower : q x ≤ ‖q - profile x‖ := by
    have h := norm_le_pi_norm (q - profile x) x
    simp only [Pi.sub_apply, profile, dist_self, sub_zero, Real.norm_eq_abs] at h
    exact (le_abs_self _).trans h
  exact le_antisymm hupper hlower
86
lemma coordinate_nonneg {q : V → ℝ} (hq : q ∈ polytope) (x : V) : 0 ≤ q x := by
proof · 2 lines
  rw [← radius_eq_coordinate hq x]
  exact norm_nonneg _
96
def commonRadius : ℝ := 1 + 2 * ∑ x : V, ∑ y : V, dist x y
98
lemma commonRadius_pos : 0 < commonRadius V := by
proof · 4 lines
  have h : 0 ≤ ∑ x : V, ∑ y : V, dist x y :=
    sum_nonneg fun _ _ => sum_nonneg fun _ _ => dist_nonneg
  unfold commonRadius
  linarith
104
lemma twice_dist_lt_commonRadius (x y : V) : 2 * dist x y < commonRadius V := by
proof · 7 lines
  have h₁ : dist x y ≤ ∑ z : V, dist x z :=
    single_le_sum (fun z _ => (dist_nonneg : (0 : ℝ) ≤ dist x z)) (mem_univ y)
  have h₂ : (∑ z : V, dist x z) ≤ ∑ u : V, ∑ z : V, dist u z :=
    single_le_sum (fun u _ => sum_nonneg fun z _ =>
      (dist_nonneg : (0 : ℝ) ≤ dist u z)) (mem_univ x)
  unfold commonRadius
  linarith
114
structure Doubled where
body · 3 lines
  sign : Bool
  point : V
  deriving Fintype
121
def doubledDist (a b : Doubled V) : ℝ :=
body · 2 lines
  if a.sign = b.sign then dist a.point b.point
  else commonRadius V - dist a.point b.point
125
lemma doubledDist_self (a : Doubled V) : doubledDist a a = 0 := by
proof · 1 line
  simp [doubledDist]
128
lemma doubledDist_comm (a b : Doubled V) : doubledDist a b = doubledDist b a := by
proof · 1 line
  simp only [doubledDist, eq_comm, dist_comm]
131
lemma doubledDist_triangle (a b c : Doubled V) :
    doubledDist a c ≤ doubledDist a b + doubledDist b c := by
proof · 15 lines
  have hab := twice_dist_lt_commonRadius V a.point b.point
  have hbc := twice_dist_lt_commonRadius V b.point c.point
  have hac := twice_dist_lt_commonRadius V a.point c.point
  have hR := commonRadius_pos V
  have h₁ := dist_triangle a.point b.point c.point
  have h₂ := dist_triangle a.point c.point b.point
  have h₃ := dist_triangle b.point a.point c.point
  rw [dist_comm c.point b.point] at h₂
  rw [dist_comm b.point a.point] at h₃
  rcases a with ⟨a, x⟩
  rcases b with ⟨b, y⟩
  rcases c with ⟨c, z⟩
  cases a <;> cases b <;> cases c <;>
    simp_all only [doubledDist, Bool.false_eq_true, Bool.true_eq_false,
      ↓reduceIte] <;> linarith
149
instance : PseudoMetricSpace (Doubled V) where
body · 4 lines
  dist := doubledDist
  dist_self := doubledDist_self
  dist_comm := doubledDist_comm
  dist_triangle := doubledDist_triangle
155
def plus (x : V) : Doubled V := ⟨true, x⟩
156
def minus (x : V) : Doubled V := ⟨false, x⟩
158
lemma dist_plus (x y : V) : dist (plus x) (plus y) = dist x y := by
proof · 2 lines
  change doubledDist _ _ = _
  simp [doubledDist, plus]
162
lemma dist_minus (x y : V) : dist (minus x) (minus y) = dist x y := by
proof · 2 lines
  change doubledDist _ _ = _
  simp [doubledDist, minus]
166
lemma opposite_dist (x : V) : dist (plus x) (minus x) = commonRadius V := by
proof · 2 lines
  change doubledDist _ _ = _
  simp [doubledDist, plus, minus]
170
lemma profile_opposite_sum (x : V) (a : Doubled V) :
    profile a (plus x) + profile a (minus x) = commonRadius V := by
proof · 3 lines
  change doubledDist a _ + doubledDist a _ = _
  rcases a with ⟨s, y⟩
  cases s <;> simp [doubledDist, plus, minus]
176
lemma opposite_coordinate_sum {q : Doubled V → ℝ}
    (hq : q ∈ polytope) (x : V) : q (plus x) + q (minus x) = commonRadius V := by
proof · 13 lines
  have hconv : Convex ℝ {q : Doubled V → ℝ |
      q (plus x) + q (minus x) = commonRadius V} := by
    intro q hq r hr a b _ _ hab
    change (a • q + b • r) (plus x) + (a • q + b • r) (minus x) = _
    simp only [Pi.add_apply, Pi.smul_apply, smul_eq_mul]
    change q (plus x) + q (minus x) = commonRadius V at hq
    change r (plus x) + r (minus x) = commonRadius V at hr
    calc
      _ = a * (q (plus x) + q (minus x)) +
          b * (r (plus x) + r (minus x)) := by ring
      _ = a * commonRadius V + b * commonRadius V := by rw [hq, hr]
      _ = commonRadius V := by rw [← add_mul, hab, one_mul]
  exact convexHull_min (by rintro _ ⟨a, rfl⟩; exact profile_opposite_sum x a) hconv hq
192
theorem opposite_radii {q : Doubled V → ℝ} (hq : q ∈ polytope) (x : V) :
    ‖q - profile (plus x)‖ + ‖q - profile (minus x)‖ = commonRadius V := by
proof · 2 lines
  rw [radius_eq_coordinate hq, radius_eq_coordinate hq]
  exact opposite_coordinate_sum hq x
197
theorem original_isometry : Isometry (fun x : V => profile (plus x)) :=
proof · 1 line
  Isometry.of_dist_eq fun x y => by rw [dist_eq_norm, profile_norm_sub, dist_plus]
206
def mix (q r : E) (t : ℝ) : E := (1 - t) • q + t • r
208
@[simp] lemma mix_zero (q r : E) : mix q r 0 = q := by simp [mix]
209
@[simp] lemma mix_one (q r : E) : mix q r 1 = r := by simp [mix]
211
lemma mix_sub_mix (q r : E) (t s : ℝ) :
    mix q r t - mix q r s = (t - s) • (r - q) := by
proof · 2 lines
  unfold mix
  module
216
lemma segment_distance (q r : E) (t s : ℝ) :
    ‖mix q r t - mix q r s‖ = |t - s| * ‖r - q‖ := by
proof · 1 line
  rw [mix_sub_mix, norm_smul, Real.norm_eq_abs]
220
theorem segment_subdivision_length (q r : E) (N : ℕ) (t : ℕ → ℝ)
    (ht : ∀ i < N, t i ≤ t (i + 1)) (hzero : t 0 = 0) (hone : t N = 1) :
    ∑ i ∈ Finset.range N, ‖mix q r (t (i + 1)) - mix q r (t i)‖ = ‖r - q‖ := by
proof · 10 lines
  have hsum : (∑ i ∈ Finset.range N, (t (i + 1) - t i)) = 1 := by
    rw [Finset.sum_range_sub, hone, hzero, sub_zero]
  calc
    _ = ∑ i ∈ Finset.range N, (t (i + 1) - t i) * ‖r - q‖ := by
      apply Finset.sum_congr rfl
      intro i hi
      rw [segment_distance, abs_of_nonneg (sub_nonneg.mpr (ht i (mem_range.mp hi)))]
    _ = (∑ i ∈ Finset.range N, (t (i + 1) - t i)) * ‖r - q‖ :=
      (Finset.sum_mul _ _ _).symm
    _ = ‖r - q‖ := by rw [hsum, one_mul]
240
lemma mix_mem {q r : V → ℝ} (hq : q ∈ polytope) (hr : r ∈ polytope)
    {t : ℝ} (ht : t ∈ Set.Icc (0 : ℝ) 1) : mix q r t ∈ polytope :=
proof · 1 line
  polytope_convex hq hr (sub_nonneg.mpr ht.2) ht.1 (by ring)
244
lemma radius_mix {q r : V → ℝ} (hq : q ∈ polytope) (hr : r ∈ polytope)
    (x : V) {t : ℝ} (ht : t ∈ Set.Icc (0 : ℝ) 1) :
    ‖mix q r t - profile x‖ = (1 - t) * ‖q - profile x‖ + t * ‖r - profile x‖ := by
proof · 3 lines
  rw [radius_eq_coordinate (mix_mem hq hr ht), radius_eq_coordinate hq,
    radius_eq_coordinate hr]
  rfl
251
lemma inward_radius {q : Doubled V → ℝ} (hq : q ∈ polytope)
    (x : V) {t : ℝ} (ht : t ∈ Set.Icc (0 : ℝ) 1) :
    ‖mix q (profile (plus x)) t - profile (plus x)‖ =
      (1 - t) * ‖q - profile (plus x)‖ := by
proof · 2 lines
  rw [radius_mix hq (profile_mem _) _ ht]
  simp
258
lemma inward_distance (q : Doubled V → ℝ) (x : V) {t : ℝ} (ht : 0 ≤ t) :
    ‖mix q (profile (plus x)) t - q‖ = t * ‖q - profile (plus x)‖ := by
proof · 2 lines
  simpa only [mix_zero, sub_zero, abs_of_nonneg ht, norm_sub_rev (profile (plus x)) q]
    using segment_distance q (profile (plus x)) t 0
263
lemma outward_radius {q : Doubled V → ℝ} (hq : q ∈ polytope)
    (x : V) {t : ℝ} (ht : t ∈ Set.Icc (0 : ℝ) 1) :
    ‖mix q (profile (minus x)) t - profile (plus x)‖ =
      ‖q - profile (plus x)‖ + t * (commonRadius V - ‖q - profile (plus x)‖) := by
proof · 2 lines
  rw [radius_mix hq (profile_mem _) _ ht, profile_norm_sub, dist_comm, opposite_dist]
  ring
270
lemma outward_distance {q : Doubled V → ℝ} (hq : q ∈ polytope)
    (x : V) {t : ℝ} (ht : 0 ≤ t) :
    ‖mix q (profile (minus x)) t - q‖ =
      t * (commonRadius V - ‖q - profile (plus x)‖) := by
proof · 5 lines
  have h : ‖q - profile (minus x)‖ = commonRadius V - ‖q - profile (plus x)‖ := by
    linarith [opposite_radii hq x]
  have hm := segment_distance q (profile (minus x)) t 0
  simpa only [mix_zero, sub_zero, abs_of_nonneg ht, norm_sub_rev (profile (minus x)) q, h]
    using hm
280
theorem radial_motion {q : Doubled V → ℝ} (hq : q ∈ polytope)
    (x : V) {u : ℝ} (hu : u ∈ Set.Icc (0 : ℝ) (commonRadius V)) :
    ∃ z ∈ polytope, ‖z - profile (plus x)‖ = u ∧
      ‖z - q‖ = |u - ‖q - profile (plus x)‖| := by
proof · 38 lines
  let ρ := ‖q - profile (plus x)‖
  have hρ : 0 ≤ ρ := norm_nonneg _
  by_cases h : u ≤ ρ
  · by_cases hz : ρ = 0
    · have hu0 : u = 0 := le_antisymm (hz ▸ h) hu.1
      refine ⟨q, hq, ?_, ?_⟩
      · exact hz.trans hu0.symm
      · simp only [sub_self, norm_zero]
        change 0 = |u - ρ|
        simp [hz, hu0]
    · have hpos : 0 < ρ := lt_of_le_of_ne hρ (Ne.symm hz)
      let t := (ρ - u) / ρ
      have ht0 : 0 ≤ t := div_nonneg (sub_nonneg.mpr h) hρ
      have ht1 : t ≤ 1 := (div_le_one hpos).2 (by linarith [hu.1])
      have htr : t * ρ = ρ - u := div_mul_cancel₀ _ hz
      refine ⟨mix q (profile (plus x)) t, mix_mem hq (profile_mem _) ⟨ht0, ht1⟩, ?_, ?_⟩
      · rw [inward_radius hq x ⟨ht0, ht1⟩]
        change (1 - t) * ρ = u
        nlinarith
      · rw [inward_distance q x ht0]
        change t * ρ = |u - ρ|
        rw [htr, abs_of_nonpos (sub_nonpos.mpr h)]
        ring
  · have hlt : ρ < u := lt_of_not_ge h
    have hpos : 0 < commonRadius V - ρ := by linarith [hu.2]
    let t := (u - ρ) / (commonRadius V - ρ)
    have ht0 : 0 ≤ t := div_nonneg (sub_pos.mpr hlt).le hpos.le
    have ht1 : t ≤ 1 := (div_le_one hpos).2 (by linarith [hu.2])
    have htr : t * (commonRadius V - ρ) = u - ρ :=
      div_mul_cancel₀ _ (ne_of_gt hpos)
    refine ⟨mix q (profile (minus x)) t, mix_mem hq (profile_mem _) ⟨ht0, ht1⟩, ?_, ?_⟩
    · rw [outward_radius hq x ⟨ht0, ht1⟩]
      change ρ + t * (commonRadius V - ρ) = u
      rw [htr]
      ring
    · rw [outward_distance hq x ht0]
      change t * (commonRadius V - ρ) = |u - ρ|
      rw [htr, abs_of_nonneg (sub_nonneg.mpr hlt.le)]
323
theorem ball_intersects_shell {q : Doubled V → ℝ} (hq : q ∈ polytope) (x : V)
    {a b δ : ℝ} (ha : 0 ≤ a) (hab : a < b) (hb : b ≤ commonRadius V)
    (hδ : 0 < δ) (hl : a - δ < ‖q - profile (plus x)‖)
    (hu : ‖q - profile (plus x)‖ < b + δ) :
    ∃ z ∈ polytope, ‖z - q‖ < δ ∧
      a < ‖z - profile (plus x)‖ ∧ ‖z - profile (plus x)‖ < b := by
proof · 17 lines
  let ρ := ‖q - profile (plus x)‖
  have hgap : max a (ρ - δ) < min b (ρ + δ) := by
    apply max_lt
    · exact lt_min hab (by dsimp [ρ]; linarith)
    · exact lt_min (by dsimp [ρ]; linarith) (by linarith)
  obtain ⟨u, hlu, huu⟩ := exists_between hgap
  have hal : a < u := (le_max_left _ _).trans_lt hlu
  have hbu : u < b := huu.trans_le (min_le_left _ _)
  have hρl : ρ - δ < u := (le_max_right _ _).trans_lt hlu
  have hρu : u < ρ + δ := huu.trans_le (min_le_right _ _)
  obtain ⟨z, hz, hrad, hdist⟩ := radial_motion hq x ⟨ha.trans hal.le, hbu.le.trans hb⟩
  refine ⟨z, hz, ?_, ?_, ?_⟩
  · rw [hdist]
    change |u - ρ| < δ
    exact abs_lt.mpr ⟨by linarith, by linarith⟩
  · rwa [hrad]
  · rwa [hrad]
351
def PiecewiseAffineOn (s : Set ℝ) (f : ℝ → ℝ) : Prop :=
body · 3 lines
  ∃ (N : ℕ) (pieces : Fin N → Set ℝ) (slopes intercepts : Fin N → ℝ),
    (∀ i, Convex ℝ (pieces i)) ∧ (∀ t ∈ s, ∃ i, t ∈ pieces i) ∧
    ∀ i t, t ∈ s → t ∈ pieces i → f t = slopes i * t + intercepts i
356
lemma PiecewiseAffineOn.mono {s t : Set ℝ} {f : ℝ → ℝ}
    (h : PiecewiseAffineOn s f) (ht : t ⊆ s) : PiecewiseAffineOn t f := by
proof · 3 lines
  obtain ⟨N, pieces, slopes, intercepts, hc, hcover, heq⟩ := h
  exact ⟨N, pieces, slopes, intercepts, hc, fun u hu => hcover u (ht hu),
    fun i u hu hp => heq i u (ht hu) hp⟩
362
lemma PiecewiseAffineOn.congr {s : Set ℝ} {f g : ℝ → ℝ}
    (h : PiecewiseAffineOn s f) (hfg : ∀ t ∈ s, f t = g t) : PiecewiseAffineOn s g := by
proof · 3 lines
  obtain ⟨N, pieces, slopes, intercepts, hc, hcover, heq⟩ := h
  exact ⟨N, pieces, slopes, intercepts, hc, hcover,
    fun i t ht hp => (hfg t ht).symm.trans (heq i t ht hp)⟩
368
lemma clamped_affine_piecewise {a b : ℝ} (hab : a ≤ b) (x y : ℝ) :
    PiecewiseAffineOn Set.univ (fun t : ℝ =>
      min b (max a ((1 - t) * x + t * y))) := by
proof · 32 lines
  let f : ℝ →ᵃ[ℝ] ℝ := AffineMap.lineMap x y
  refine ⟨3, ![f ⁻¹' Set.Iic a, f ⁻¹' Set.Icc a b, f ⁻¹' Set.Ici b],
    ![0, y - x, 0], ![a, x, b], ?_, ?_, ?_⟩
  · intro i
    fin_cases i
    · exact Convex.affine_preimage f (convex_Iic a)
    · exact Convex.affine_preimage f (convex_Icc a b)
    · exact Convex.affine_preimage f (convex_Ici b)
  · intro t _
    by_cases ha : f t ≤ a
    · exact ⟨0, ha⟩
    · by_cases hb : f t ≤ b
      · exact ⟨1, (lt_of_not_ge ha).le, hb⟩
      · exact ⟨2, (lt_of_not_ge hb).le⟩
  · intro i t _ hp
    fin_cases i
    · change f t ≤ a at hp
      have hp' : (1 - t) * x + t * y ≤ a := by
        simpa only [f, AffineMap.lineMap_apply_ring] using hp
      change min b (max a ((1 - t) * x + t * y)) = 0 * t + a
      rw [max_eq_left hp', min_eq_right hab, zero_mul, zero_add]
    · change a ≤ f t ∧ f t ≤ b at hp
      have hp' : a ≤ (1 - t) * x + t * y ∧ (1 - t) * x + t * y ≤ b := by
        simpa only [f, AffineMap.lineMap_apply_ring] using hp
      change min b (max a ((1 - t) * x + t * y)) = (y - x) * t + x
      rw [max_eq_right hp'.1, min_eq_right hp'.2]
      ring
    · change b ≤ f t at hp
      have hp' : b ≤ (1 - t) * x + t * y := by
        simpa only [f, AffineMap.lineMap_apply_ring] using hp
      change min b (max a ((1 - t) * x + t * y)) = 0 * t + b
      rw [max_eq_right (hab.trans hp'), min_eq_left hp', zero_mul, zero_add]
404
theorem clamped_radius_piecewise {V : Type*} [Fintype V] [PseudoMetricSpace V]
    {q r : V → ℝ} (hq : q ∈ polytope) (hr : r ∈ polytope) (x : V)
    {a b : ℝ} (hab : a ≤ b) :
    PiecewiseAffineOn (Set.Icc (0 : ℝ) 1)
      (fun t => min b (max a ‖mix q r t - profile x‖)) := by
proof · 5 lines
  have h := (clamped_affine_piecewise hab ‖q - profile x‖ ‖r - profile x‖).mono
    (Set.subset_univ (Set.Icc (0 : ℝ) 1))
  apply h.congr
  intro t ht
  rw [radius_mix hq hr x ht]

Xet Storage Details

Size:
17.1 kB
·
Xet hash:
4ac58e45cf6b9e53c61616f2fd8ead60352deab308be35a10b92ec855663b1b4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.