Class: SparseVector
- Inherits:
-
Hash
- Object
- Hash
- SparseVector
- Defined in:
- lib/zipf/SparseVector.rb
Class Method Summary collapse
- .from_a(a) ⇒ Object
- .from_file(fn, sep = '=') ⇒ Object
- .from_h(h) ⇒ Object
- .from_json(s) ⇒ Object
- .from_kv(s, sep = '=', join = /\s/) ⇒ Object
- .from_s(s) ⇒ Object
- .mean(a) ⇒ Object
Instance Method Summary collapse
- #*(scalar) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(scalar) ⇒ Object
- #approx_eql?(other, p = 10**-10) ⇒ Boolean
- #average ⇒ Object
- #cosinus_sim(other) ⇒ Object
- #dot(other) ⇒ Object
- #euclidian_dist(other) ⇒ Object
- #from_a(a) ⇒ Object
- #from_file(fn, sep = '=') ⇒ Object
- #from_h(h) ⇒ Object
- #from_json(s) ⇒ Object
- #from_kv(s, sep = '=', join = /\s/) ⇒ Object
- #from_s(s) ⇒ Object
-
#initialize(arg = nil) ⇒ SparseVector
constructor
A new instance of SparseVector.
- #join_keys(other) ⇒ Object
- #magnitude ⇒ Object
- #stddev ⇒ Object
- #sum ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_kv(sep = '=', join = ' ') ⇒ Object
- #variance ⇒ Object
- #zeros(n) ⇒ Object
Constructor Details
#initialize(arg = nil) ⇒ SparseVector
Returns a new instance of SparseVector.
5 6 7 8 9 10 11 |
# File 'lib/zipf/SparseVector.rb', line 5 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
17 18 19 20 21 |
# File 'lib/zipf/SparseVector.rb', line 17 def self.from_a a v = SparseVector.new v.from_a a return v end |
.from_file(fn, sep = '=') ⇒ Object
93 94 95 96 97 |
# File 'lib/zipf/SparseVector.rb', line 93 def self.from_file fn, sep='=' v = SparseVector.new v.from_file fn, sep return v end |
.from_h(h) ⇒ Object
33 34 35 36 37 |
# File 'lib/zipf/SparseVector.rb', line 33 def self.from_h h v = SparseVector.new v.from_h h return v end |
.from_json(s) ⇒ Object
78 79 80 81 82 |
# File 'lib/zipf/SparseVector.rb', line 78 def self.from_json s v = SparseVector.new v.from_json s return v end |
.from_kv(s, sep = '=', join = /\s/) ⇒ Object
64 65 66 67 68 |
# File 'lib/zipf/SparseVector.rb', line 64 def self.from_kv s, sep='=', join=/\s/ v = SparseVector.new v.from_kv s, sep, join return v end |
.from_s(s) ⇒ Object
43 44 45 46 47 |
# File 'lib/zipf/SparseVector.rb', line 43 def self.from_s s v = SparseVector.new v.from_s s return v end |
.mean(a) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/zipf/SparseVector.rb', line 191 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
173 174 175 176 177 178 179 180 |
# File 'lib/zipf/SparseVector.rb', line 173 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
157 158 159 160 161 162 163 |
# File 'lib/zipf/SparseVector.rb', line 157 def + other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]+other[k] } return new end |
#-(other) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/zipf/SparseVector.rb', line 165 def - other new = SparseVector.new join_keys(other).each { |k| new[k] = self[k]-other[k] } return new end |
#/(scalar) ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/zipf/SparseVector.rb', line 182 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 |
#approx_eql?(other, p = 10**-10) ⇒ Boolean
107 108 109 110 111 112 113 114 115 |
# File 'lib/zipf/SparseVector.rb', line 107 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 |
#average ⇒ Object
117 118 119 |
# File 'lib/zipf/SparseVector.rb', line 117 def average self.sum/self.size.to_f end |
#cosinus_sim(other) ⇒ Object
146 147 148 |
# File 'lib/zipf/SparseVector.rb', line 146 def cosinus_sim other self.dot(other)/(self.magnitude*other.magnitude) end |
#dot(other) ⇒ Object
132 133 134 135 136 |
# File 'lib/zipf/SparseVector.rb', line 132 def dot other sum = 0.0 self.each_pair { |k,v| sum += v * other[k] } return sum end |
#euclidian_dist(other) ⇒ Object
150 151 152 153 154 155 |
# File 'lib/zipf/SparseVector.rb', line 150 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
13 14 15 |
# File 'lib/zipf/SparseVector.rb', line 13 def from_a a a.each_with_index { |i,j| self[j] = i } end |
#from_file(fn, sep = '=') ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/zipf/SparseVector.rb', line 84 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
29 30 31 |
# File 'lib/zipf/SparseVector.rb', line 29 def from_h h h.each_pair { |k,v| self[k] = v } end |
#from_json(s) ⇒ Object
74 75 76 |
# File 'lib/zipf/SparseVector.rb', line 74 def from_json s from_h JSON.load(s) end |
#from_kv(s, sep = '=', join = /\s/) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/zipf/SparseVector.rb', line 57 def from_kv s, sep='=', join=/\s/ s.strip.split(join).each { |i| k,v = i.split(sep) self[k] = v.to_f } end |
#from_s(s) ⇒ Object
39 40 41 |
# File 'lib/zipf/SparseVector.rb', line 39 def from_s s from_h eval(s.strip) end |
#join_keys(other) ⇒ Object
99 100 101 |
# File 'lib/zipf/SparseVector.rb', line 99 def join_keys other self.keys + other.keys end |
#magnitude ⇒ Object
142 143 144 |
# File 'lib/zipf/SparseVector.rb', line 142 def magnitude Math.sqrt self.values.inject { |sum,i| sum+i**2 } end |
#stddev ⇒ Object
128 129 130 |
# File 'lib/zipf/SparseVector.rb', line 128 def stddev Math.sqrt self.variance end |
#sum ⇒ Object
103 104 105 |
# File 'lib/zipf/SparseVector.rb', line 103 def sum self.values.inject(:+) end |
#to_h ⇒ Object
23 24 25 26 27 |
# File 'lib/zipf/SparseVector.rb', line 23 def to_h h = {} self.each_pair { |k,v| h[k] = v } return h end |
#to_json ⇒ Object
70 71 72 |
# File 'lib/zipf/SparseVector.rb', line 70 def to_json JSON.dump self.to_h end |
#to_kv(sep = '=', join = ' ') ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/zipf/SparseVector.rb', line 49 def to_kv sep='=', join=' ' a = [] self.each_pair { |k,v| a << "#{k}#{sep}#{v}" } return a.join join end |
#variance ⇒ Object
121 122 123 124 125 126 |
# File 'lib/zipf/SparseVector.rb', line 121 def variance avg = self.average var = 0.0 self.values.each { |i| var += (avg - i)**2 } return var end |
#zeros(n) ⇒ Object
138 139 140 |
# File 'lib/zipf/SparseVector.rb', line 138 def zeros n (0).upto(n-1) { |i| self[i] = 0.0 } end |