Skip to content

Lengths and Distances

Once you have an inner product, length and distance come for free. The length of a vector is the square root of its inner product with itself; the distance between two vectors is the length of their difference. These two quantities power k-NN, clustering, and every “how close are these?” question in machine learning.

A real-life example: nearest neighbors

A recommendation engine represents each song as a vector of audio features. To recommend “songs like this one,” it finds the vectors with the smallest distance to your current track. Change the notion of distance and you change the recommendations — which is why the length/distance machinery below is not academic, it’s the product.

Length from an inner product

Any inner product induces a norm:

x:=x,x.\lVert \mathbf{x} \rVert := \sqrt{\langle \mathbf{x}, \mathbf{x}\rangle}.

With the dot product this is the familiar Euclidean length xx\sqrt{\mathbf{x}^\top\mathbf{x}}. Not every norm comes from an inner product (the Manhattan norm doesn’t) — but the ones that do inherit a beautiful bonus:

The Cauchy-Schwarz inequality

x,yxy.|\langle \mathbf{x}, \mathbf{y}\rangle| \le \lVert \mathbf{x} \rVert\,\lVert \mathbf{y} \rVert.

The inner product can never exceed the product of the lengths. This single inequality is what makes the angle on the next page well-defined (it keeps cosω\cos\omega inside [1,1][-1, 1]).

Distance and metrics

The distance between two vectors is the length of their difference:

d(x,y):=xy=xy,xy.d(\mathbf{x}, \mathbf{y}) := \lVert \mathbf{x} - \mathbf{y} \rVert = \sqrt{\langle \mathbf{x}-\mathbf{y},\, \mathbf{x}-\mathbf{y}\rangle}.

With the dot product this is Euclidean distance. A distance function that satisfies three axioms is called a metric:

diagram Diagram mermaid

The triangle inequality, live

The triangle inequality says a detour through a third point y\mathbf{y} is never shorter than going straight from x\mathbf{x} to z\mathbf{z}. Drag the middle point around (it orbits) and watch the direct distance stay \le the two-leg path:

sketch The triangle inequality p5.js
Direct distance d(x,z) versus the detour d(x,y)+d(y,z) through a moving middle point y. The detour is always at least as long — equal only when y sits on the straight segment.

NumPy

lengths_distances.py
import numpy as np
 
x = np.array([1.0, 1.0])
y = np.array([4.0, 5.0])
 
# length from the (dot-product) inner product
print("‖x‖ =", np.sqrt(x @ x))              # √2 ≈ 1.414
 
# Euclidean distance
print("d(x, y) =", np.linalg.norm(x - y))   # 5.0
 
# Cauchy-Schwarz: |⟨x,y⟩| ≤ ‖x‖‖y‖
lhs = abs(x @ y)
rhs = np.linalg.norm(x) * np.linalg.norm(y)
print(f"Cauchy-Schwarz: {lhs:.3f}{rhs:.3f} -> {lhs <= rhs}")
lengths_distances.py
import numpy as np
 
x = np.array([1.0, 1.0])
y = np.array([4.0, 5.0])
 
# length from the (dot-product) inner product
print("‖x‖ =", np.sqrt(x @ x))              # √2 ≈ 1.414
 
# Euclidean distance
print("d(x, y) =", np.linalg.norm(x - y))   # 5.0
 
# Cauchy-Schwarz: |⟨x,y⟩| ≤ ‖x‖‖y‖
lhs = abs(x @ y)
rhs = np.linalg.norm(x) * np.linalg.norm(y)
print(f"Cauchy-Schwarz: {lhs:.3f}{rhs:.3f} -> {lhs <= rhs}")
text
‖x‖ = 1.4142135623730951
d(x, y) = 5.0
Cauchy-Schwarz: 9.0009.220 -> True
text
‖x‖ = 1.4142135623730951
d(x, y) = 5.0
Cauchy-Schwarz: 9.0009.220 -> True

Why this matters for ML

  • k-Nearest Neighbors classifies a point by the labels of the closest training points — “closest” is a distance.
  • Clustering (k-means) minimizes within-cluster squared distances.
  • Loss functions like mean squared error are squared Euclidean distances between predictions and targets.

🧪 Try It Yourself

Exercise 1 – Length and distance

Exercise 2 – Nearest neighbor

Exercise 3 – Distance is symmetric

Recap

  • An inner product induces a length x=x,x\lVert \mathbf{x}\rVert = \sqrt{\langle \mathbf{x},\mathbf{x}\rangle} and a distance d(x,y)=xyd(\mathbf{x},\mathbf{y}) = \lVert \mathbf{x}-\mathbf{y}\rVert.
  • Cauchy-Schwarz (x,yxy|\langle \mathbf{x},\mathbf{y}\rangle| \le \lVert\mathbf{x}\rVert\lVert\mathbf{y}\rVert) keeps angles well-defined.
  • A metric is positive-definite, symmetric, and obeys the triangle inequality.
  • Distance powers k-NN, clustering, and squared-error losses.

Next: the same inner product measures the angle between vectors.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did