Module: VectorExtensions

Included in:
Vector
Defined in:
lib/classifier/extensions/vector.rb

Instance Method Summary collapse

Instance Method Details

#magnitudeObject



21
22
23
24
25
26
27
# File 'lib/classifier/extensions/vector.rb', line 21

def magnitude
  sum_of_squares = 0.to_r
  size.times do |i|
    sum_of_squares += self[i]**2.to_r
  end
  Math.sqrt(sum_of_squares.to_f)
end

#normalizeObject



29
30
31
32
33
34
35
36
# File 'lib/classifier/extensions/vector.rb', line 29

def normalize
  normalized_values = []
  magnitude_value = magnitude.to_r
  size.times do |i|
    normalized_values << (self[i] / magnitude_value)
  end
  Vector[*normalized_values]
end