BIZNIQUE / LINEAR ALGEBRA / 29
Compute distances without losing their meaning
Compute distances without losing their meaning
Use an algebraic identity to organise repeated Euclidean comparisons.
BIZNIQUE / LINEAR ALGEBRA / 29
Use an algebraic identity to organise repeated Euclidean comparisons.
THE IDEA
The identity expresses squared distance through norms and a dot product. For many pairs, these terms can be organised as array operations and norms can be reused. The example compares a point with the origin.
‖x−y‖₂² = ‖x‖₂² + ‖y‖₂² − 2xᵀy
Roundoff can produce tiny negative values when subtracting nearly equal floating-point quantities. A numerical safeguard is different from accepting a materially invalid distance.
TRY IT YOURSELF
What is the distance between (0, 0) and (3, 4)?
The squared distance is 25, so the Euclidean distance is 5.