Method: Search::Simple::Vector#dot

Defined in:
lib/search/simple/vector.rb

#dot(vector) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/search/simple/vector.rb', line 25

def dot(vector)
  # We only need to calculate up to the end of the shortest vector
  limit = @max_bit
    # Commenting out the next line makes this vector the dominant
    # one when doing the comparison
  limit = vector.max_bit if limit > vector.max_bit
    
  # because both vectors have just ones or zeros in them,
  # we can pre-calculate the AnBn component
  # The vector's magnitude is Sqrt(num set bits)
  factor = Math.sqrt(1.0/@num_bits) * Math.sqrt(1.0/vector.num_bits)
    
  count = 0
  (limit+1).times {|i| count += 1 if @bits[i] ==1 && vector.bits[i] == 1}
    
  factor * count
end