Module: Documentrix::Utils::Math
- Included in:
- Documents::Cache::Common, Documents::Splitters::Semantic, ColorizeTexts
- Defined in:
- lib/documentrix/utils/math.rb
Instance Method Summary collapse
-
#convert_to_vector(vector) ⇒ Numo::NArray
Converts an array to a Numo NArray.
-
#cosine_similarity(a:, b:, a_norm: norm(a), b_norm: norm(b)) ⇒ Float
Returns the cosine similarity between two vectors
a
andb
, 1.0 is exactly the same, 0.0 means decorrelated. -
#norm(vector) ⇒ Float
Returns the Euclidean norm (magnitude) of a vector.
Instance Method Details
#convert_to_vector(vector) ⇒ Numo::NArray
Converts an array to a Numo NArray.
44 45 46 47 |
# File 'lib/documentrix/utils/math.rb', line 44 def convert_to_vector(vector) vector.is_a?(Numo::NArray) and return vector Numo::NArray[*vector] end |
#cosine_similarity(a:, b:, a_norm: norm(a), b_norm: norm(b)) ⇒ Float
Returns the cosine similarity between two vectors a
and b
, 1.0 is
exactly the same, 0.0 means decorrelated.
17 18 19 20 |
# File 'lib/documentrix/utils/math.rb', line 17 def cosine_similarity(a:, b:, a_norm: norm(a), b_norm: norm(b)) a, b = convert_to_vector(a), convert_to_vector(b) a.dot(b) / (a_norm * b_norm) end |
#norm(vector) ⇒ Float
Returns the Euclidean norm (magnitude) of a vector.
30 31 32 33 34 |
# File 'lib/documentrix/utils/math.rb', line 30 def norm(vector) s = 0.0 vector.each { s += _1.abs2 } Math.sqrt(s) end |