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.
I want to do game programming again like it's 1999. No more `npm i` or "accept all cookies" :/ rant off :)
Go make a game for the Sega Genesis https://mdengine.dev/
Or, the GameBoy Advance https://github.com/GValiente/butano
4 replies →
I mean we used dist^2 all the time for comparisons in our game engine back in the 90s (multiple different engines actually).
So it was a known thing...
>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).
yes, you are right. I realized my mistake afterwards but it was after the edit window.
> 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.