From b6820b015c1a2767916e3d894cd9cd76d67b94b9 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Thu, 16 Jul 2015 15:51:20 +0200 Subject: SparseVector: norm, unit --- lib/zipf/SparseVector.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb index 5d219b8..976ecaf 100644 --- a/lib/zipf/SparseVector.rb +++ b/lib/zipf/SparseVector.rb @@ -199,5 +199,20 @@ class SparseVector < Hash mean.each_pair { |k,v| mean[k] = v/n } return mean end + + def norm + return Math.sqrt(self.dot(self)) + end + + def unit! + n = self.norm + self.each_pair { |k,v| + self[k] /= n + } + end + + def unit + return SparseVector.new(self).unit! + end end -- cgit v1.2.3