Class: SparseVector

Inherits:
Hash
  • Object
show all
Defined in:
lib/zipf/SparseVector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ SparseVector

Returns a new instance of SparseVector.



3
4
5
6
7
8
9
# File 'lib/zipf/SparseVector.rb', line 3

def initialize arg=nil
  super
  self.default = 0
  if arg.is_a? Array
    from_a arg
  end
end

Class Method Details

.from_a(a) ⇒ Object



15
16
17
18
19
# File 'lib/zipf/SparseVector.rb', line 15

def self.from_a a
  v = SparseVector.new
  v.from_a a
  return v
end

.from_file(fn, sep = '=') ⇒ Object



71
72
73
74
75
# File 'lib/zipf/SparseVector.rb', line 71

def self.from_file fn, sep='='
  v = SparseVector.new
  v.from_file fn, sep
  return v
end

.from_h(h) ⇒ Object



25
26
27
28
29
# File 'lib/zipf/SparseVector.rb', line 25

def self.from_h h
  v = SparseVector.new
  v.from_h h
  return v
end

.from_kv(s) ⇒ Object



56
57
58
59
60
# File 'lib/zipf/SparseVector.rb', line 56

def self.from_kv s
  v = SparseVector.new
  v.from_kv s
  return v
end

.from_s(s) ⇒ Object



35
36
37
38
39
# File 'lib/zipf/SparseVector.rb', line 35

def self.from_s s
  v = SparseVector.new
  v.from_s s
  return v
end

.mean(a) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/zipf/SparseVector.rb', line 160

def self.mean a
  mean = SparseVector.new
  a.each { |i|
    i.each_pair { |k,v|
      mean[k] += v
    }
  }
  n = a.size.to_f
  mean.each_pair { |k,v| mean[k] = v/n }
  return mean
end

Instance Method Details

#*(scalar) ⇒ Object

Raises:

  • (ArgumentError)


151
152
153
154
155
156
157
158
# File 'lib/zipf/SparseVector.rb', line 151

def * scalar
  raise ArgumentError, "Arg is not numeric #{scalar}" unless scalar.is_a? Numeric
  new = SparseVector.new
  self.keys.each { |k|
    new[k] = self[k] * scalar
  }
  return new
end

#+(other) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/zipf/SparseVector.rb', line 135

def + other
  new = SparseVector.new
  join_keys(other).each { |k|
    new[k] = self[k]+other[k]
  }
  return new
end

#-(other) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/zipf/SparseVector.rb', line 143

def - other
  new = SparseVector.new
  join_keys(other).each { |k|
    new[k] = self[k]-other[k]
  }
  return new
end

#approx_eql?(other, p = 10**-10) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
# File 'lib/zipf/SparseVector.rb', line 85

def approx_eql? other, p=10**-10
  return false if !other
  return false if other.size!=self.size
  return false if other.keys.sort!=self.keys.sort
  self.keys.each { |k|
    return false if (self[k]-other[k]).abs>p
  }
  return true
end

#averageObject



95
96
97
# File 'lib/zipf/SparseVector.rb', line 95

def average
  self.sum/self.size.to_f
end

#cosinus_sim(other) ⇒ Object



124
125
126
# File 'lib/zipf/SparseVector.rb', line 124

def cosinus_sim other
  self.dot(other)/(self.magnitude*other.magnitude)
end

#dot(other) ⇒ Object



110
111
112
113
114
# File 'lib/zipf/SparseVector.rb', line 110

def dot other
  sum = 0.0
  self.each_pair { |k,v| sum += v * other[k] }
  return sum
end

#euclidian_dist(other) ⇒ Object



128
129
130
131
132
133
# File 'lib/zipf/SparseVector.rb', line 128

def euclidian_dist other
  dims = [self.keys, other.keys].flatten.uniq
  sum = 0.0
  dims.each { |d| sum += (self[d] - other[d])**2 }
  return Math.sqrt(sum)
end

#from_a(a) ⇒ Object



11
12
13
# File 'lib/zipf/SparseVector.rb', line 11

def from_a a
  a.each_with_index { |i,j| self[j] = i }
end

#from_file(fn, sep = '=') ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/zipf/SparseVector.rb', line 62

def from_file fn, sep='='
  f = ReadFile.new(fn)
  while line = f.gets
    key, value = line.strip.split sep
    value = value.to_f
    self[key] = value
  end
end

#from_h(h) ⇒ Object



21
22
23
# File 'lib/zipf/SparseVector.rb', line 21

def from_h h
  h.each_pair { |k,v| self[k] = v }
end

#from_kv(s) ⇒ Object



49
50
51
52
53
54
# File 'lib/zipf/SparseVector.rb', line 49

def from_kv s
  s.split.each { |i|
    k,v = i.split('=')
    self[k] = v.to_f
  }
end

#from_s(s) ⇒ Object



31
32
33
# File 'lib/zipf/SparseVector.rb', line 31

def from_s s
  from_h eval(s)
end

#join_keys(other) ⇒ Object



77
78
79
# File 'lib/zipf/SparseVector.rb', line 77

def join_keys other
  self.keys + other.keys
end

#magnitudeObject



120
121
122
# File 'lib/zipf/SparseVector.rb', line 120

def magnitude
  Math.sqrt self.values.inject { |sum,i| sum+i**2 }
end

#stddevObject



106
107
108
# File 'lib/zipf/SparseVector.rb', line 106

def stddev
  Math.sqrt self.variance
end

#sumObject



81
82
83
# File 'lib/zipf/SparseVector.rb', line 81

def sum
  self.values.inject(:+)
end

#to_kv(sep = '=', join = ' ') ⇒ Object



41
42
43
44
45
46
47
# File 'lib/zipf/SparseVector.rb', line 41

def to_kv sep='=', join=' '
  a = []
  self.each_pair { |k,v|
    a << "#{k}#{sep}#{v}"
  }
  return a.join join
end

#varianceObject



99
100
101
102
103
104
# File 'lib/zipf/SparseVector.rb', line 99

def variance
  avg = self.average
  var = 0.0
  self.values.each { |i| var += (avg - i)**2 }
  return var
end

#zeros(n) ⇒ Object



116
117
118
# File 'lib/zipf/SparseVector.rb', line 116

def zeros n
  (0).upto(n-1) { |i| self[i] = 0.0 }
end