YingfanWang/PaCMAP

Cosine distance has a call to sqrt

Closed this issue · 2 comments

danr commented

@numba.njit("f4(f4[:],f4[:])", cache=True)
def angular_dist(x1, x2):
"""
Angular (i.e. cosine) distance between two vectors.
"""
x1_norm = np.maximum(l2_norm(x1), 1e-20)
x2_norm = np.maximum(l2_norm(x2), 1e-20)
result = 0.0
for i in range(x1.shape[0]):
result += x1[i] * x2[i]
return np.sqrt(2.0 - 2.0 * result / x1_norm / x2_norm)

Cosine distance as defined does not have a square root call. I think the np.sqrt should be removed here.

Hi Dan! Thank you for your interest in PaCMAP!

Here, we chose to follow the definition of angular distance provided in annoy for consistency. The angular distance is defined as the Euclidean distance of the two normalized vector u, v.

euclidean(u, v) = sqrt(u^2 + v^2 - 2uv) = sqrt(2-2uv) = sqrt(2-2 cos theta)

Let me know if you have further questions!

danr commented

Thanks, then it makes sense!