blob: c749ed9fc8f395c22e7709b550f76b0d303a48f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
class Vector < Array
def sum
self.inject(:+)
end
def average
self.sum/self.size.to_f
end
def variance
avg = self.average
var = 0.0
self.each { |i| var += (avg - i)**2 }
var
end
def stddev list_of_values, decimal_places=-1
Math.sqrt self.variance
end
def dot other
return nil if self.size!=other.size
sum = 0.0
self.each_with_index { |i,j| sum += i*other[j] }
return sum
end
def magnitude
Math.sqrt self.inject { |sum,i| sum+i**2 }
end
def cosinus_sim other
return self.dot(other)/(self.mag*other.mag)
end
def euclidian_dist other
sum = 0.0
self.each_with_index { |i,j| sum += (i - other[j])**2 }
return Math.sqrt(sum)
end
end
|