summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/SparseVector.rb
diff options
context:
space:
mode:
authorPatrick Simianer <simianer@cl.uni-heidelberg.de>2014-02-14 15:54:29 +0100
committerPatrick Simianer <simianer@cl.uni-heidelberg.de>2014-02-14 15:54:29 +0100
commit7255d33914122e58b031108de49918b8910eebc6 (patch)
tree652c7e637475184ac7509a6ecb0e4ed7276cf925 /lib/nlp_ruby/SparseVector.rb
parent32f0468ecff9f89598a68598d45acbd67aa50ab5 (diff)
parent2e16486848a761d5f341ede096944a8526b97fc3 (diff)
Merge branch 'master' of github.com:pks/nlp_ruby
Diffstat (limited to 'lib/nlp_ruby/SparseVector.rb')
-rw-r--r--lib/nlp_ruby/SparseVector.rb15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/nlp_ruby/SparseVector.rb b/lib/nlp_ruby/SparseVector.rb
index cdf966c..1c0262b 100644
--- a/lib/nlp_ruby/SparseVector.rb
+++ b/lib/nlp_ruby/SparseVector.rb
@@ -1,8 +1,11 @@
class SparseVector < Hash
- def initialize
+ def initialize arg=nil
super
self.default = 0
+ if arg.is_a? Array
+ from_a arg
+ end
end
def from_a a
@@ -21,6 +24,16 @@ class SparseVector < Hash
self.values.inject(:+)
end
+ 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
+
def average
self.sum/self.size.to_f
end