File size: 17,065 Bytes
f0f05de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import OAI.Combinatorics.BinaryEdit.Model
import Mathlib.Data.Fintype.EquivFin
import Mathlib.Tactic

namespace OAI

/-! Distortion of finite metric spaces under injective maps into real ℓ₁. -/

noncomputable section
open scoped ENNReal

namespace TreeEdit.BinaryLower

variable {X Y : Type*}

/-- The two finite sets whose maxima define distortion. -/
def expansionRatios (ρ : X → X → ℝ) (f : X → RealL1) : Set ℝ :=
  {r | ∃ x y, x ≠ y ∧ r = ‖f x - f y‖ / ρ x y}

def contractionRatios (ρ : X → X → ℝ) (f : X → RealL1) : Set ℝ :=
  {r | ∃ x y, x ≠ y ∧ r = ρ x y / ‖f x - f y‖}

def embeddingDistortions (ρ : X → X → ℝ) : Set ℝ :=
  {D | ∃ f : X → RealL1, Function.Injective f ∧ D = distortion ρ f}

lemma expansionRatios_finite [Finite X] (ρ : X → X → ℝ) (f : X → RealL1) :
    (expansionRatios ρ f).Finite := by
  apply Set.Finite.subset (Set.finite_range (fun p : X × X => ‖f p.1 - f p.2‖ / ρ p.1 p.2))
  rintro r ⟨x, y, _, rfl⟩
  exact ⟨(x, y), rfl⟩

lemma contractionRatios_finite [Finite X] (ρ : X → X → ℝ) (f : X → RealL1) :
    (contractionRatios ρ f).Finite := by
  apply Set.Finite.subset (Set.finite_range (fun p : X × X => ρ p.1 p.2 / ‖f p.1 - f p.2‖))
  rintro r ⟨x, y, _, rfl⟩
  exact ⟨(x, y), rfl⟩

lemma expansionRatios_nonempty [Nontrivial X] (ρ : X → X → ℝ) (f : X → RealL1) :
    (expansionRatios ρ f).Nonempty := by
  obtain ⟨x, y, hxy⟩ := exists_pair_ne X
  exact ⟨_, x, y, hxy, rfl⟩

lemma contractionRatios_nonempty [Nontrivial X] (ρ : X → X → ℝ) (f : X → RealL1) :
    (contractionRatios ρ f).Nonempty := by
  obtain ⟨x, y, hxy⟩ := exists_pair_ne X
  exact ⟨_, x, y, hxy, rfl⟩

lemma expansion_nonneg [Finite X] [Nontrivial X] (ρ : X → X → ℝ)
    (hρ : ∀ x y, 0 ≤ ρ x y) (f : X → RealL1) :
    0 ≤ sSup (expansionRatios ρ f) := by
  obtain ⟨x, y, hxy⟩ := exists_pair_ne X
  exact (div_nonneg (norm_nonneg _) (hρ x y)).trans
    (le_csSup (expansionRatios_finite ρ f).bddAbove ⟨x, y, hxy, rfl⟩)

lemma contraction_nonneg [Finite X] [Nontrivial X] (ρ : X → X → ℝ)
    (hρ : ∀ x y, 0 ≤ ρ x y) (f : X → RealL1) :
    0 ≤ sSup (contractionRatios ρ f) := by
  obtain ⟨x, y, hxy⟩ := exists_pair_ne X
  exact (div_nonneg (hρ x y) (norm_nonneg _)).trans
    (le_csSup (contractionRatios_finite ρ f).bddAbove ⟨x, y, hxy, rfl⟩)

lemma distortion_nonneg [Finite X] [Nontrivial X] (ρ : X → X → ℝ)
    (hρ : ∀ x y, 0 ≤ ρ x y) (f : X → RealL1) : 0 ≤ distortion ρ f :=
  mul_nonneg (expansion_nonneg ρ hρ f) (contraction_nonneg ρ hρ f)

/-- Every finite set admits an injection into the full sequence space. -/
lemma exists_injective_L1 (X : Type*) [Finite X] :
    ∃ f : X → RealL1, Function.Injective f := by
  classical
  let := Fintype.ofFinite X
  let f : X → RealL1 := fun x => lp.single 1 0 ((Fintype.equivFin X x).val : ℝ)
  refine ⟨f, ?_⟩
  intro x y h
  have hv := congrArg (fun z : RealL1 => z 0) h
  simp only [f, lp.single_apply_self] at hv
  apply (Fintype.equivFin X).injective
  apply Fin.ext
  exact_mod_cast hv

lemma embeddingDistortions_nonempty [Finite X] (ρ : X → X → ℝ) :
    (embeddingDistortions ρ).Nonempty := by
  obtain ⟨f, hf⟩ := exists_injective_L1 X
  exact ⟨distortion ρ f, f, hf, rfl⟩

lemma embeddingDistortions_bddBelow [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y) :
    BddBelow (embeddingDistortions ρ) := by
  refine ⟨0, ?_⟩
  rintro D ⟨f, _, rfl⟩
  exact distortion_nonneg ρ hρ f

lemma c1_nonneg [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y) : 0 ≤ c1 X ρ := by
  apply le_csInf (embeddingDistortions_nonempty ρ)
  rintro D ⟨f, _, rfl⟩
  exact distortion_nonneg ρ hρ f

lemma c1_le_distortion [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (f : X → RealL1) (hf : Function.Injective f) : c1 X ρ ≤ distortion ρ f := by
  exact csInf_le (embeddingDistortions_bddBelow ρ hρ) ⟨f, hf, rfl⟩

lemma le_c1 [Finite X] (ρ : X → X → ℝ) (D : ℝ)
    (h : ∀ f : X → RealL1, Function.Injective f → D ≤ distortion ρ f) :
    D ≤ c1 X ρ := by
  apply le_csInf (embeddingDistortions_nonempty ρ)
  rintro e ⟨f, hf, rfl⟩
  exact h f hf

lemma expansionRatios_comp_subset (ρ : X → X → ℝ) (σ : Y → Y → ℝ)
    (g : X → Y) (hg : Function.Injective g)
    (hiso : ∀ x y, σ (g x) (g y) = ρ x y) (f : Y → RealL1) :
    expansionRatios ρ (f ∘ g) ⊆ expansionRatios σ f := by
  rintro r ⟨x, y, hxy, rfl⟩
  exact ⟨g x, g y, fun h => hxy (hg h), by simp [hiso]⟩

lemma contractionRatios_comp_subset (ρ : X → X → ℝ) (σ : Y → Y → ℝ)
    (g : X → Y) (hg : Function.Injective g)
    (hiso : ∀ x y, σ (g x) (g y) = ρ x y) (f : Y → RealL1) :
    contractionRatios ρ (f ∘ g) ⊆ contractionRatios σ f := by
  rintro r ⟨x, y, hxy, rfl⟩
  exact ⟨g x, g y, fun h => hxy (hg h), by simp [hiso]⟩

/-- Restriction to an isometric subspace cannot increase distortion. -/
lemma distortion_comp_le [Finite X] [Nontrivial X] [Finite Y] [Nontrivial Y]
    (ρ : X → X → ℝ) (σ : Y → Y → ℝ) (hσ : ∀ x y, 0 ≤ σ x y)
    (g : X → Y) (hg : Function.Injective g)
    (hiso : ∀ x y, σ (g x) (g y) = ρ x y) (f : Y → RealL1) :
    distortion ρ (f ∘ g) ≤ distortion σ f := by
  have hρ : ∀ x y, 0 ≤ ρ x y := fun x y => (hiso x y) ▸ hσ (g x) (g y)
  apply mul_le_mul
  · apply csSup_le (expansionRatios_nonempty ρ (f ∘ g))
    intro r hr
    exact le_csSup (expansionRatios_finite σ f).bddAbove
      (expansionRatios_comp_subset ρ σ g hg hiso f hr)
  · apply csSup_le (contractionRatios_nonempty ρ (f ∘ g))
    intro r hr
    exact le_csSup (contractionRatios_finite σ f).bddAbove
      (contractionRatios_comp_subset ρ σ g hg hiso f hr)
  · exact contraction_nonneg ρ hρ (f ∘ g)
  · exact expansion_nonneg σ hσ f

/-- The optimal ℓ₁ distortion is monotone under injective isometries. -/
lemma c1_mono_isometry [Finite X] [Nontrivial X] [Finite Y] [Nontrivial Y]
    (ρ : X → X → ℝ) (σ : Y → Y → ℝ) (hσ : ∀ x y, 0 ≤ σ x y)
    (g : X → Y) (hg : Function.Injective g)
    (hiso : ∀ x y, σ (g x) (g y) = ρ x y) :
    c1 X ρ ≤ c1 Y σ := by
  have hρ : ∀ x y, 0 ≤ ρ x y := fun x y => (hiso x y) ▸ hσ (g x) (g y)
  apply le_c1
  intro f hf
  exact (c1_le_distortion ρ hρ (f ∘ g) (hf.comp hg)).trans
    (distortion_comp_le ρ σ hσ g hg hiso f)

lemma c1_eq_of_equiv [Finite X] [Nontrivial X] [Finite Y] [Nontrivial Y]
    (ρ : X → X → ℝ) (σ : Y → Y → ℝ) (hσ : ∀ x y, 0 ≤ σ x y)
    (e : X ≃ Y) (hiso : ∀ x y, σ (e x) (e y) = ρ x y) :
    c1 X ρ = c1 Y σ := by
  apply le_antisymm (c1_mono_isometry ρ σ hσ e e.injective hiso)
  apply c1_mono_isometry σ ρ
  · intro x y
    exact (hiso x y) ▸ hσ (e x) (e y)
  · exact e.symm.injective
  · intro x y
    simpa using (hiso (e.symm x) (e.symm y)).symm

/-- A noncontracting embedding with expansion at most `D` gives the usual
upper bound on optimal distortion. No existence theorem is assumed here. -/
lemma c1_le_of_bounds [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (f : X → RealL1) (D : ℝ) (hD : 0 ≤ D)
    (hlower : ∀ x y, ρ x y ≤ ‖f x - f y‖)
    (hupper : ∀ x y, ‖f x - f y‖ ≤ D * ρ x y) :
    c1 X ρ ≤ D := by
  have hf : Function.Injective f := by
    intro x y h
    by_contra hxy
    have ht := hlower x y
    rw [h, sub_self, norm_zero] at ht
    exact (not_le_of_gt (hpos x y hxy)) ht
  apply (c1_le_distortion ρ hρ f hf).trans
  have he : sSup (expansionRatios ρ f) ≤ D := by
    apply csSup_le (expansionRatios_nonempty ρ f)
    rintro r ⟨x, y, hxy, rfl⟩
    exact (div_le_iff₀ (hpos x y hxy)).2 (hupper x y)
  have hc : sSup (contractionRatios ρ f) ≤ 1 := by
    apply csSup_le (contractionRatios_nonempty ρ f)
    rintro r ⟨x, y, hxy, rfl⟩
    have hn : 0 < ‖f x - f y‖ := (hpos x y hxy).trans_le (hlower x y)
    exact (div_le_iff₀ hn).2 (by simpa using hlower x y)
  calc
    distortion ρ f ≤ D * 1 :=
      mul_le_mul he hc (contraction_nonneg ρ hρ f) hD
    _ = D := mul_one D

/-- Comparing two metrics through a scaled bi-Lipschitz injection costs only
its ratio of expansion to contraction in the distortion product. -/
lemma distortion_comp_le_bilip [Finite X] [Nontrivial X] [Finite Y] [Nontrivial Y]
    (ρ : X → X → ℝ) (σ : Y → Y → ℝ)
    (hρ : ∀ x y, 0 ≤ ρ x y) (hσ : ∀ x y, 0 ≤ σ x y)
    (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (g : X → Y) (hg : Function.Injective g)
    (a b : ℝ) (ha : 0 < a) (hb : 0 ≤ b)
    (hlower : ∀ x y, a * ρ x y ≤ σ (g x) (g y))
    (hupper : ∀ x y, σ (g x) (g y) ≤ b * ρ x y) (f : Y → RealL1) :
    distortion ρ (f ∘ g) ≤ (b / a) * distortion σ f := by
  have he : sSup (expansionRatios ρ (f ∘ g)) ≤ b * sSup (expansionRatios σ f) := by
    apply csSup_le (expansionRatios_nonempty ρ (f ∘ g))
    rintro r ⟨x, y, hxy, rfl⟩
    have ht := hpos x y hxy
    have hs : 0 < σ (g x) (g y) := (mul_pos ha ht).trans_le (hlower x y)
    have hratio : ‖f (g x) - f (g y)‖ / σ (g x) (g y) ≤
        sSup (expansionRatios σ f) :=
      le_csSup (expansionRatios_finite σ f).bddAbove
        ⟨g x, g y, fun h => hxy (hg h), rfl⟩
    calc
      ‖(f ∘ g) x - (f ∘ g) y‖ / ρ x y ≤
          (b * ‖f (g x) - f (g y)‖) / σ (g x) (g y) := by
        apply (div_le_div_iff₀ ht hs).2
        have hn := norm_nonneg (f (g x) - f (g y))
        change ‖f (g x) - f (g y)‖ * σ (g x) (g y) ≤
          b * ‖f (g x) - f (g y)‖ * ρ x y
        nlinarith [mul_le_mul_of_nonneg_left (hupper x y) hn]
      _ = b * (‖f (g x) - f (g y)‖ / σ (g x) (g y)) := by ring
      _ ≤ b * sSup (expansionRatios σ f) := mul_le_mul_of_nonneg_left hratio hb
  have hc : sSup (contractionRatios ρ (f ∘ g)) ≤
      (1 / a) * sSup (contractionRatios σ f) := by
    apply csSup_le (contractionRatios_nonempty ρ (f ∘ g))
    rintro r ⟨x, y, hxy, rfl⟩
    have hratio : σ (g x) (g y) / ‖f (g x) - f (g y)‖ ≤
        sSup (contractionRatios σ f) :=
      le_csSup (contractionRatios_finite σ f).bddAbove
        ⟨g x, g y, fun h => hxy (hg h), rfl⟩
    have hmetric : ρ x y ≤ σ (g x) (g y) / a := by
      apply (le_div_iff₀ ha).2
      simpa [mul_comm] using hlower x y
    calc
      ρ x y / ‖(f ∘ g) x - (f ∘ g) y‖ ≤
          (σ (g x) (g y) / a) / ‖f (g x) - f (g y)‖ :=
        div_le_div_of_nonneg_right hmetric (norm_nonneg _)
      _ = (1 / a) * (σ (g x) (g y) / ‖f (g x) - f (g y)‖) := by ring
      _ ≤ (1 / a) * sSup (contractionRatios σ f) :=
        mul_le_mul_of_nonneg_left hratio (by positivity)
  calc
    distortion ρ (f ∘ g) ≤
        (b * sSup (expansionRatios σ f)) *
          ((1 / a) * sSup (contractionRatios σ f)) :=
      mul_le_mul he hc (contraction_nonneg ρ hρ (f ∘ g))
        (mul_nonneg hb (expansion_nonneg σ hσ f))
    _ = (b / a) * distortion σ f := by unfold distortion; unfold expansionRatios contractionRatios; ring

/-- Optimal distortion transfer through any scaled bi-Lipschitz injection.
In particular, `a=P/804` and `b=P` give constant distortion loss. -/
lemma c1_le_bilip_mul [Finite X] [Nontrivial X] [Finite Y] [Nontrivial Y]
    (ρ : X → X → ℝ) (σ : Y → Y → ℝ)
    (hρ : ∀ x y, 0 ≤ ρ x y) (hσ : ∀ x y, 0 ≤ σ x y)
    (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (g : X → Y) (hg : Function.Injective g)
    (a b : ℝ) (ha : 0 < a) (hb : 0 < b)
    (hlower : ∀ x y, a * ρ x y ≤ σ (g x) (g y))
    (hupper : ∀ x y, σ (g x) (g y) ≤ b * ρ x y) :
    c1 X ρ ≤ (b / a) * c1 Y σ := by
  have hba : 0 < b / a := div_pos hb ha
  have h : c1 X ρ / (b / a) ≤ c1 Y σ := by
    apply le_c1
    intro f hf
    apply (div_le_iff₀ hba).2
    calc
      c1 X ρ ≤ distortion ρ (f ∘ g) := c1_le_distortion ρ hρ (f ∘ g) (hf.comp hg)
      _ ≤ (b / a) * distortion σ f :=
        distortion_comp_le_bilip ρ σ hρ hσ hpos g hg a b ha hb.le hlower hupper f
      _ = distortion σ f * (b / a) := mul_comm _ _
  simpa [mul_comm] using (div_le_iff₀ hba).1 h

/-- Rescaling by the maximal contraction ratio makes an injective embedding
noncontracting without changing the distortion bound. -/
lemma exists_normalized_embedding [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (hdiag : ∀ x, ρ x x = 0) (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (f : X → RealL1) (hf : Function.Injective f) :
    ∃ g : X → RealL1, Function.Injective g ∧
      ∀ x y, ρ x y ≤ ‖g x - g y‖ ∧ ‖g x - g y‖ ≤ distortion ρ f * ρ x y := by
  let C := sSup (contractionRatios ρ f)
  let E := sSup (expansionRatios ρ f)
  have hC : 0 ≤ C := contraction_nonneg ρ hρ f
  let g : X → RealL1 := fun x => C • f x
  have hnorm (x y : X) : ‖g x - g y‖ = C * ‖f x - f y‖ := by
    dsimp [g]
    rw [← smul_sub, norm_smul, Real.norm_of_nonneg hC]
  have hbounds : ∀ x y, ρ x y ≤ ‖g x - g y‖ ∧
      ‖g x - g y‖ ≤ distortion ρ f * ρ x y := by
    intro x y
    by_cases hxy : x = y
    · subst y
      simp [hdiag]
    have hr : 0 < ρ x y := hpos x y hxy
    have hn : 0 < ‖f x - f y‖ := norm_pos_iff.mpr (sub_ne_zero.mpr (fun h => hxy (hf h)))
    have hc : ρ x y / ‖f x - f y‖ ≤ C :=
      le_csSup (contractionRatios_finite ρ f).bddAbove ⟨x, y, hxy, rfl⟩
    have he : ‖f x - f y‖ / ρ x y ≤ E :=
      le_csSup (expansionRatios_finite ρ f).bddAbove ⟨x, y, hxy, rfl⟩
    constructor
    · rw [hnorm]
      exact (div_le_iff₀ hn).1 hc
    · rw [hnorm]
      have ht := mul_le_mul_of_nonneg_left ((div_le_iff₀ hr).1 he) hC
      change C * ‖f x - f y‖ ≤ (E * C) * ρ x y
      nlinarith
  refine ⟨g, ?_, hbounds⟩
  intro x y h
  by_contra hxy
  have ht := (hbounds x y).1
  rw [h, sub_self, norm_zero] at ht
  exact (not_le_of_gt (hpos x y hxy)) ht

/-- An obstruction proved for all normalized embeddings gives a lower bound
on the infimum defining `c1`. This is the passage used by the two tree routes. -/
lemma le_c1_of_normalized [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (hdiag : ∀ x, ρ x x = 0) (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (D : ℝ)
    (hobstruction : ∀ (g : X → RealL1) (A : ℝ),
      (∀ x y, ρ x y ≤ ‖g x - g y‖ ∧ ‖g x - g y‖ ≤ A * ρ x y) → D ≤ A) :
    D ≤ c1 X ρ := by
  apply le_c1
  intro f hf
  obtain ⟨g, _, hg⟩ := exists_normalized_embedding ρ hρ hdiag hpos f hf
  exact hobstruction g (distortion ρ f) hg

/-- The dual normalization used verbatim by the finite-tree obstruction:
the image distance lies between `ρ / distortion` and `ρ`. -/
lemma exists_nonexpanding_embedding [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (hdiag : ∀ x, ρ x x = 0) (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (f : X → RealL1) (hf : Function.Injective f) :
    0 < distortion ρ f ∧ ∃ g : X → RealL1, Function.Injective g ∧
      ∀ x y, ρ x y / distortion ρ f ≤ ‖g x - g y‖ ∧ ‖g x - g y‖ ≤ ρ x y := by
  obtain ⟨g, _, hg⟩ := exists_normalized_embedding ρ hρ hdiag hpos f hf
  obtain ⟨x₀, y₀, hxy⟩ := exists_pair_ne X
  have hr := hpos x₀ y₀ hxy
  have hD : 0 < distortion ρ f := by
    have ht := (hg x₀ y₀).1.trans (hg x₀ y₀).2
    nlinarith
  let g' : X → RealL1 := fun x => (1 / distortion ρ f) • g x
  have hnorm (x y : X) : ‖g' x - g' y‖ = ‖g x - g y‖ / distortion ρ f := by
    dsimp [g']
    rw [← smul_sub, norm_smul, Real.norm_of_nonneg (by positivity)]
    ring
  have hb : ∀ x y, ρ x y / distortion ρ f ≤ ‖g' x - g' y‖ ∧
      ‖g' x - g' y‖ ≤ ρ x y := by
    intro x y
    rw [hnorm]
    constructor
    · exact div_le_div_of_nonneg_right (hg x y).1 hD.le
    · apply (div_le_iff₀ hD).2
      simpa [mul_comm] using (hg x y).2
  refine ⟨hD, g', ?_, hb⟩
  intro x y h
  by_contra hxy
  have ht := (hb x y).1
  rw [h, sub_self, norm_zero] at ht
  exact (not_le_of_gt (div_pos (hpos x y hxy) hD)) ht

lemma le_c1_of_nonexpanding [Finite X] [Nontrivial X]
    (ρ : X → X → ℝ) (hρ : ∀ x y, 0 ≤ ρ x y)
    (hdiag : ∀ x, ρ x x = 0) (hpos : ∀ x y, x ≠ y → 0 < ρ x y)
    (D : ℝ)
    (hobstruction : ∀ (g : X → RealL1) (A : ℝ), 0 < A →
      (∀ x y, ρ x y / A ≤ ‖g x - g y‖ ∧ ‖g x - g y‖ ≤ ρ x y) → D ≤ A) :
    D ≤ c1 X ρ := by
  apply le_c1
  intro f hf
  obtain ⟨hD, g, _, hg⟩ := exists_nonexpanding_embedding ρ hρ hdiag hpos f hf
  exact hobstruction g (distortion ρ f) hD hg

end TreeEdit.BinaryLower

end

end OAI