Added cosine distance function
This commit is contained in:
parent
3e2735c005
commit
3b35b9b542
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue