Cosine.lean
Lean 4 source, lean/DataMiningAsObservation/Cosine.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Cosine.lean. Chapter 0 section 0.1 and equation 0.1, the cosine.
Cosine similarity is the dot product divided by the product of the two lengths. The file
states the Cauchy–Schwarz bound that puts it between minus one and one, that a vector has
cosine one with itself, that rescaling either vector by a positive factor does not change
it, and that it is the dot product of the two row-normalized vectors, which is how chapter
3 reads it as a statement about a quotient.
-/
import Mathlib
import DataMiningAsObservation.SpectralEmbedding
namespace DMO.Cosine
open DMO.SpectralEmbedding
variable {n : ℕ}
/-- Cosine similarity, equation 0.1. -/
noncomputable def cosine (u v : Fin n → ℝ) : ℝ :=
(u ⬝ᵥ v) / (Real.sqrt (u ⬝ᵥ u) * Real.sqrt (v ⬝ᵥ v))
/-- **Cauchy–Schwarz for the dot product.** -/
theorem abs_dot_le (u v : Fin n → ℝ) :
|u ⬝ᵥ v| ≤ Real.sqrt (u ⬝ᵥ u) * Real.sqrt (v ⬝ᵥ v) := by
rw [← Real.sqrt_mul (dot_self_nonneg u), ← Real.sqrt_sq_eq_abs]
apply Real.sqrt_le_sqrt
simpa [dotProduct, pow_two] using Finset.sum_mul_sq_le_sq_mul_sq Finset.univ u v
/-- **The cosine is at most one.** -/
theorem cosine_le_one (u v : Fin n → ℝ) (hu : u ⬝ᵥ u ≠ 0) (hv : v ⬝ᵥ v ≠ 0) :
cosine u v ≤ 1 := by
unfold cosine
have hu' : 0 < Real.sqrt (u ⬝ᵥ u) :=
Real.sqrt_pos.mpr (lt_of_le_of_ne (dot_self_nonneg u) (Ne.symm hu))
have hv' : 0 < Real.sqrt (v ⬝ᵥ v) :=
Real.sqrt_pos.mpr (lt_of_le_of_ne (dot_self_nonneg v) (Ne.symm hv))
rw [div_le_one (mul_pos hu' hv')]
exact le_trans (le_abs_self _) (abs_dot_le u v)
/-- **The cosine is at least minus one.** -/
theorem neg_one_le_cosine (u v : Fin n → ℝ) (hu : u ⬝ᵥ u ≠ 0) (hv : v ⬝ᵥ v ≠ 0) :
-1 ≤ cosine u v := by
unfold cosine
have hu' : 0 < Real.sqrt (u ⬝ᵥ u) :=
Real.sqrt_pos.mpr (lt_of_le_of_ne (dot_self_nonneg u) (Ne.symm hu))
have hv' : 0 < Real.sqrt (v ⬝ᵥ v) :=
Real.sqrt_pos.mpr (lt_of_le_of_ne (dot_self_nonneg v) (Ne.symm hv))
rw [le_div_iff₀ (mul_pos hu' hv')]
have := abs_dot_le u v
linarith [neg_abs_le (u ⬝ᵥ v)]
/-- **A vector has cosine one with itself.** -/
theorem cosine_self (u : Fin n → ℝ) (hu : u ⬝ᵥ u ≠ 0) : cosine u u = 1 := by
unfold cosine
rw [Real.mul_self_sqrt (dot_self_nonneg u), div_self hu]
/-- **Rescaling a vector by a positive factor does not change the cosine.** -/
theorem cosine_smul (c : ℝ) (hc : 0 < c) (u v : Fin n → ℝ) :
cosine (c • u) v = cosine u v := by
unfold cosine
rw [smul_dotProduct, smul_dotProduct, dotProduct_smul, smul_eq_mul, smul_eq_mul, smul_eq_mul,
show c * (c * (u ⬝ᵥ u)) = c ^ 2 * (u ⬝ᵥ u) by ring, Real.sqrt_mul (sq_nonneg c),
Real.sqrt_sq hc.le,
show c * Real.sqrt (u ⬝ᵥ u) * Real.sqrt (v ⬝ᵥ v)
= c * (Real.sqrt (u ⬝ᵥ u) * Real.sqrt (v ⬝ᵥ v)) by ring,
mul_div_mul_left _ _ hc.ne']
/-- **The cosine is the dot product of the row-normalized vectors.** -/
theorem cosine_eq_dot_rowNormalize (u v : Fin n → ℝ) :
cosine u v = rowNormalize u ⬝ᵥ rowNormalize v :=
(dot_rowNormalize u v).symm
end DMO.Cosine