Class: VectorComparer

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_comparer.rb

Class Method Summary collapse

Class Method Details

.compare_vectorsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vector_comparer.rb', line 23

def self.compare_vectors
  puts "How many test vectors should there be?"
  @number_of_test_vectors = gets.chomp.to_i
  puts "What should the threshold be?"
  @threshold = gets.chomp.to_i

  #Generate 10, 128-d random vectors

  random_vectors = Array.new(@number_of_random_vectors)
  @number_of_random_vectors.times do |i|
    random_vector = Array.new(@dimensions)
    @dimensions.times do |n|
      random_vector[n] = random1 
    end
    random_vectors[i] = random_vector
  end

  #Generate x, 128-d test vectors

  test_vectors = Array.new(@number_of_test_vectors)
  @number_of_test_vectors.times do |i|
    test_vector = Array.new(@dimensions)
    @dimensions.times do |n|
      test_vector[n] = random1 
    end
    test_vectors[i] = test_vector
  end    

  puts "For threshold #{@threshold}..."
  test_vectors.each do |test_vector|
    number_near_orthogonal = 0
    random_vectors.each do |random_vector|
      dp = dot_prod(test_vector,random_vector)
      if dp.abs < @threshold 
        number_near_orthogonal += 1
      end
    end
    puts "The random test vector was nearly orthogonal to #{number_near_orthogonal} or the initial vectors"
  end
end

.dot_prod(v1, v2) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/vector_comparer.rb', line 15

def self.dot_prod(v1,v2)
  product = 0
  v1.length.times do |i|
    product += v1[i] * v2[i]
  end
  return product
end

.random1Object



6
7
8
9
10
11
12
13
# File 'lib/vector_comparer.rb', line 6

def self.random1
  number = rand(2)
  if number == 0 
    return -1
  else
    return 1
  end
end