Class: Vector

Inherits:
Object show all
Defined in:
lib/classifier/extensions/vector.rb

Instance Method Summary collapse

Instance Method Details

#magnitudeObject



16
17
18
19
20
21
22
# File 'lib/classifier/extensions/vector.rb', line 16

def magnitude
  sumsqs = 0.0
  self.size.times do |i|
    sumsqs += self[i] ** 2.0 
  end
  Math.sqrt(sumsqs)
end

#normalizeObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/classifier/extensions/vector.rb', line 23

def normalize
  nv = []
  mag = self.magnitude
  self.size.times do |i|

    nv << (self[i] / mag)

  end
  Vector[*nv]
end