lsh 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,6 @@ module LSH
21
21
  attr_reader :projections, :buckets
22
22
 
23
23
  def initialize(dim, k, w = Float::INFINITY, l = 150)
24
- @math = MathUtil.new
25
24
  @window = w
26
25
  @dim = dim
27
26
  @number_of_random_vectors = k
@@ -88,7 +87,7 @@ module LSH
88
87
  hash << 0
89
88
  end
90
89
  else
91
- b = bias ? @math.random_uniform : 0.0
90
+ b = bias ? MathUtil.random_uniform : 0.0
92
91
  hash << (b + dot_product / @window).floor
93
92
  end
94
93
  end
@@ -126,15 +125,15 @@ module LSH
126
125
 
127
126
  def random_vector_unit(dim)
128
127
  r = random_vector(dim)
129
- r /= @math.norm(r)
128
+ r /= MathUtil.norm(r)
130
129
  end
131
130
 
132
131
  def random_vector(dim)
133
- @math.random_gaussian_vector(dim)
132
+ MathUtil.random_gaussian_vector(dim)
134
133
  end
135
134
 
136
135
  def similarity(v1, v2)
137
- @math.dot(v1, v2)
136
+ MathUtil.dot(v1, v2)
138
137
  end
139
138
 
140
139
  end
@@ -20,24 +20,25 @@ module LSH
20
20
 
21
21
  class MathUtil
22
22
 
23
- def initialize
24
- @gsl_random = GSL::Rng.alloc
25
- @gsl_random.set(rand(1000)) # Overriding seed
23
+ @@gsl_random = GSL::Rng.alloc
24
+
25
+ def self.random_uniform
26
+ @@gsl_random.uniform
26
27
  end
27
28
 
28
- def random_uniform
29
- @gsl_random.uniform
29
+ def self.random_gaussian_vector(dim)
30
+ @@gsl_random.gaussian(1, dim)
30
31
  end
31
32
 
32
- def random_gaussian_vector(dim)
33
- @gsl_random.gaussian(1, dim)
33
+ def self.random_gaussian_matrix(k, l)
34
+ GSL::Matrix.randn(k, l)
34
35
  end
35
36
 
36
- def dot(v1, v2)
37
+ def self.dot(v1, v2)
37
38
  v1 * v2.col
38
39
  end
39
40
 
40
- def norm(v)
41
+ def self.norm(v)
41
42
  v.norm
42
43
  end
43
44
 
@@ -20,19 +20,23 @@ module LSH
20
20
 
21
21
  class MathUtil
22
22
 
23
- def random_uniform
23
+ def self.random_uniform
24
24
  JBLAS.rand[0,0]
25
25
  end
26
26
 
27
- def random_gaussian_vector(dim)
28
- JBLAS.randn(dim, 1)
27
+ def self.random_gaussian_vector(dim)
28
+ JBLAS.randn(1, dim)
29
29
  end
30
30
 
31
- def dot(v1, v2)
32
- (v1.t * v2)[0,0]
31
+ def self.random_gaussian_matrix(k, l)
32
+ JBLAS.randn(k, l)
33
33
  end
34
34
 
35
- def norm(v)
35
+ def self.dot(v1, v2)
36
+ (v1 * v2.t)[0,0]
37
+ end
38
+
39
+ def self.norm(v)
36
40
  v.norm2
37
41
  end
38
42
 
metadata CHANGED
@@ -1,34 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lsh
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.2
4
+ version: 0.0.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Yves Raimond
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-13 00:00:00.000000000 Z
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: jblas-ruby
16
- version_requirements: !ruby/object:Gem::Requirement
15
+ name: gsl
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
17
18
  requirements:
18
- - - ">="
19
+ - - ! '>='
19
20
  - !ruby/object:Gem::Version
20
- version: !binary |-
21
- MA==
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
22
25
  none: false
23
- requirement: !ruby/object:Gem::Requirement
24
26
  requirements:
25
- - - ">="
27
+ - - ! '>='
26
28
  - !ruby/object:Gem::Version
27
- version: !binary |-
28
- MA==
29
- none: false
30
- prerelease: false
31
- type: :runtime
29
+ version: '0'
32
30
  description: An implementation of LSH in Ruby, using JBLAS for JRuby and GSL for MRI
33
31
  email: yves.raimond@bbc.co.uk
34
32
  executables: []
@@ -39,30 +37,28 @@ files:
39
37
  - lib/lsh/index.rb
40
38
  - lib/lsh/math_util_gsl.rb
41
39
  - lib/lsh/math_util_jblas.rb
42
- homepage:
40
+ homepage:
43
41
  licenses: []
44
- post_install_message:
42
+ post_install_message:
45
43
  rdoc_options: []
46
44
  require_paths:
47
45
  - lib
48
46
  required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
49
48
  requirements:
50
- - - ">="
49
+ - - ! '>='
51
50
  - !ruby/object:Gem::Version
52
- version: !binary |-
53
- MA==
54
- none: false
51
+ version: '0'
55
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
56
54
  requirements:
57
- - - ">="
55
+ - - ! '>='
58
56
  - !ruby/object:Gem::Version
59
- version: !binary |-
60
- MA==
61
- none: false
57
+ version: '0'
62
58
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 1.8.24
65
- signing_key:
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.23
61
+ signing_key:
66
62
  specification_version: 3
67
63
  summary: Locality Sensitive Hashing gem
68
64
  test_files: []