Module: Documentrix::Utils::Math

Included in:
Documents::Cache::Common, Documents::Splitters::Semantic, ColorizeTexts
Defined in:
lib/documentrix/utils/math.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_vector(vector) ⇒ Numo::NArray

Converts an array to a Numo NArray.

Examples:

Convert an array to a Numo NArray

convert_to_vector([1, 2, 3]) # => Numo::NArray[1, 2, 3]

Parameters:

  • vector (Array)

    The input array to be converted.

Returns:

  • (Numo::NArray)

    The converted NArray, or the original if it's already 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.

Examples:

Calculate the cosine similarity between two vectors

cosine_similarity(a: [1, 2], b: [3, 4])

Parameters:

  • a (Vector)

    The first vector

  • b (Vector)

    The second vector

  • a_norm (Hash) (defaults to: norm(a))

    a customizable set of options

  • b_norm (Hash) (defaults to: norm(b))

    a customizable set of options

Options Hash (a_norm:):

  • a (Float)

    The Euclidean norm of vector a (default: calculated from a)

Options Hash (b_norm:):

  • b (Float)

    The Euclidean norm of vector b (default: calculated from b)

Returns:

  • (Float)

    The cosine similarity between the two vectors

See Also:



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.

Examples:

norm([3, 4]) # => 5.0

Parameters:

  • vector (Array)

    The input vector.

Returns:

  • (Float)

    The magnitude of the 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