summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2015-07-16 16:10:31 +0200
committerPatrick Simianer <p@simianer.de>2015-07-16 16:10:31 +0200
commite57559f8ae50b489dd5e239179e518d0834054da (patch)
treee2e8c0f59bc93f906f2e4784485d4eb40fa33547
parent4419077e34203849cbe4bce9855e857e6b1bbfa0 (diff)
SparseVector: fixes
-rw-r--r--lib/zipf/SparseVector.rb8
-rwxr-xr-xtest/test_sparsevector.rb4
2 files changed, 11 insertions, 1 deletions
diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb
index 976ecaf..456aeb2 100644
--- a/lib/zipf/SparseVector.rb
+++ b/lib/zipf/SparseVector.rb
@@ -7,6 +7,10 @@ class SparseVector < Hash
self.default = 0
if arg.is_a? Array
from_a arg
+ elsif arg.is_a? Hash
+ from_h arg
+ elsif arg.is_a? String
+ from_s arg
end
end
@@ -212,7 +216,9 @@ class SparseVector < Hash
end
def unit
- return SparseVector.new(self).unit!
+ v = SparseVector.new(self)
+ v.unit!
+ return v
end
end
diff --git a/test/test_sparsevector.rb b/test/test_sparsevector.rb
index a23a7d3..2263c73 100755
--- a/test/test_sparsevector.rb
+++ b/test/test_sparsevector.rb
@@ -10,9 +10,13 @@ class TestSparseVector < Test::Unit::TestCase
v[:a] = 1
v[:b] = 2
v[:c] = 3
+ w = SparseVector.new v
assert_equal Math.sqrt(1**2+2**2+3**2), v.norm
v.unit!
assert_equal v.norm, 1.0
+ q = w.unit
+ assert_equal q.norm, 1.0
+ assert_equal w.norm, Math.sqrt(1**2+2**2+3**2)
end
end