Comment by stephantul

22 days ago

Amazing post, I didn’t think this through a lot, but since you are normalizing the vectors and calculating the euclidean distance, you will get the same results using a simple matmul, because euclidean distance over normalized vectors is a linear transform of the cosine distance.

Since you are just interested in the ranking, not the actual distance, you could also consider skipping the sqrt. This gives the same ranking, but will be a little faster.

It's stuff like this I would have loved to know when I was doing game engine dev in the 90s.

>you will get the same results using a simple matmul, because euclidean distance over normalized vectors is a linear transform of the cosine distance.

Squared euclidean distance of normalized vectors is an affine transform of their cosine similarity (the cosine of the angle between them).

  EuclideanDistance(x, y) = sqrt(dot(x - y, x - y)) = sqrt(dot(x, x) - 2dot(x, y) + dot(y, y)) = sqrt(2 - 2dot(x, y))

> you could also consider skipping the sqrt.

This is a trick I reach for all the time: it’s cheaper to compare squared distances than completing the Euclidean calculation. For example, to determine whether to stop calculating lerp: x*x+y*y <= epsilon.