Added cosine distance function

This commit is contained in:
Chris Watson 2019-06-17 15:18:33 -07:00
parent 3e2735c005
commit 3b35b9b542
No known key found for this signature in database
GPG Key ID: 37DAEF5F446370A4
2 changed files with 12 additions and 2 deletions

View File

@ -132,7 +132,7 @@ module Apatite
end
end
# Compute the cosine similarity between two vectors.
# Compute the cosine similarity of two vectors.
def similarity(x, y)
dot(x, y) / (
Math.sqrt(dot(x, x)) *
@ -140,6 +140,11 @@ module Apatite
)
end
# Compute the cosine distance between two vectors.
def cosine(x, y)
1.0 - similarity(x, y)
end
# Returns the angle between this vector and another in radians.
# If the vectors are mirrored across their axes this will return `nil`.
def angle_from(a, b)

View File

@ -450,11 +450,16 @@ module Apatite::LinearAlgebra
Apatite.angle_from(self, vector)
end
# Compute the cosine similarity between this vector and another.
# Compute the cosine similarity of this vector and another.
def similarity(other)
Apatite.similarity(self, other)
end
# Compute the cosine distance between this vector and another.
def cosine(other)
Apatite.cosine(self, other)
end
# Returns whether the vectors are parallel to each other.
def parallel_to?(vector)
Apatite.parallel_to?(self, vector)