EuclideanDistance.lean
Lean 4 source, lean/DataMiningAsObservation/EuclideanDistance.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
EuclideanDistance.lean. Chapter 3 section 3.2, Euclidean distance.
The squared Euclidean distance is the identity reader's quadratic form on the difference,
so it reads every coordinate at the scale it arrives in. Rescaling one coordinate changes
which of two rows is nearer. With the query at the origin, one candidate at distance one
along the first axis and another at one and a half along the second, doubling the first
axis makes the second candidate the nearer one. A choice of scale is therefore a choice of
reader, which is what chapter 3 means by a distance being a choice of what to ignore.
-/
import Mathlib
import DataMiningAsObservation.ReadDistortion
namespace DMO.EuclideanDistance
open DMO.ReadDistortion
variable {n : ℕ}
/-- The squared Euclidean distance. -/
def distSq (x y : Fin n → ℝ) : ℝ := (x - y) ⬝ᵥ (x - y)
/-- **Euclidean distance is the identity reader** on the difference. -/
theorem distSq_eq_quad_one (x y : Fin n → ℝ) :
distSq x y = quad (1 : Matrix (Fin n) (Fin n) ℝ) (x - y) := by
rw [quad_one]; rfl
/-- The distance is symmetric. -/
theorem distSq_comm (x y : Fin n → ℝ) : distSq x y = distSq y x := by
unfold distSq
rw [← neg_sub, neg_dotProduct, dotProduct_neg, neg_neg]
/-- The distance is zero exactly between a row and itself. -/
theorem distSq_eq_zero_iff (x y : Fin n → ℝ) : distSq x y = 0 ↔ x = y := by
unfold distSq
rw [dotProduct_self_eq_zero, sub_eq_zero]
/-- **Rescaling a coordinate changes the ranking.** With the query at the origin, the
candidate at `(1, 0)` is nearer than the one at `(0, 1.5)`, and after doubling the first
coordinate it is farther. -/
theorem ranking_flips :
distSq (![0, 0] : Fin 2 → ℝ) ![1, 0] < distSq (![0, 0] : Fin 2 → ℝ) ![0, 1.5] ∧
distSq (![0, 0] : Fin 2 → ℝ) ![2, 0] > distSq (![0, 0] : Fin 2 → ℝ) ![0, 1.5] := by
unfold distSq
simp [dotProduct, Fin.sum_univ_two]
norm_num
end DMO.EuclideanDistance