Class: QRCode::Encoder::Polynomial

Inherits:
Object
  • Object
show all
Defined in:
lib/qrcode/encoder/polynomial.rb

Instance Method Summary collapse

Constructor Details

#initialize(num, shift) ⇒ Polynomial

Returns a new instance of Polynomial.

Raises:

  • (RuntimeError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/qrcode/encoder/polynomial.rb', line 12

def initialize(num, shift)
  raise RuntimeError, "#{num.size}/#{shift}" if num.empty?
  offset = 0
  
  while offset < num.size && num[offset] == 0
    offset += 1
  end
  
  @num = Array.new(num.size - offset + shift)
  
  (0...num.size - offset).each do |i|
    @num[i] = num[i + offset]
  end
end

Instance Method Details

#get(index) ⇒ Object



27
28
29
# File 'lib/qrcode/encoder/polynomial.rb', line 27

def get(index)
  @num[index]
end

#get_lengthObject



31
32
33
# File 'lib/qrcode/encoder/polynomial.rb', line 31

def get_length
  @num.size
end

#mod(e) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/qrcode/encoder/polynomial.rb', line 48

def mod(e)
  if get_length - e.get_length < 0
    return self
  end
  
  ratio = Encoder::Math.glog(get(0)) - Encoder::Math.glog(e.get(0))
  num = Array.new(get_length)
  
  (0...get_length).each do |i|
    num[i] = get(i)
  end
  
  (0...e.get_length).each do |i|
    tmp = num[i].nil? ? 0 : num[i]
    num[i] = tmp ^ Encoder::Math.gexp(Encoder::Math.glog(e.get(i)) + ratio)
  end
  
  Encoder::Polynomial.new(num, 0).mod(e)
end

#multiply(e) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/qrcode/encoder/polynomial.rb', line 35

def multiply(e)
  num = Array.new(get_length + e.get_length - 1)
  
  (0...get_length).each do |i|
    (0...e.get_length).each do |j|
      tmp = num[i + j].nil? ? 0 : num[i + j]
      num[i + j] = tmp ^ Encoder::Math.gexp(Encoder::Math.glog(get(i)) + Encoder::Math.glog(e.get(j)))
    end
  end
  
  Encoder::Polynomial.new(num, 0)
end