Class: OpenTox::Algorithm::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/similarity.rb

Class Method Summary collapse

Class Method Details

.dot_product(a, b) ⇒ Numeric

Get dot product

Parameters:

Returns:



9
10
11
12
# File 'lib/similarity.rb', line 9

def self.dot_product(a, b)
  products = a.zip(b).map{|a, b| a * b}
  products.inject(0) {|s,p| s + p}
end

.magnitude(point) ⇒ Object



14
15
16
17
# File 'lib/similarity.rb', line 14

def self.magnitude(point)
  squares = point.map{|x| x ** 2}
  Math.sqrt(squares.inject(0) {|s, c| s + c})
end