Class: HenselCode::Polynomial

Inherits:
Object
  • Object
show all
Includes:
ModularArithmetic, Tools
Defined in:
lib/hensel_code/polynomial.rb

Overview

polynomial class

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModularArithmetic

#cauchy_product, #multiplication

Methods included from Tools

#crt, #eea_core, #extended_gcd, #mod_inverse, #random_distinct_numbers, #random_integer, #random_prime, #random_rational

Constructor Details

#initialize(prime, coefficients, fixed_length: true) ⇒ Polynomial

Returns a new instance of Polynomial.



11
12
13
14
15
16
17
# File 'lib/hensel_code/polynomial.rb', line 11

def initialize(prime, coefficients, fixed_length: true)
  @prime = prime
  @coefficients = coefficients
  @fixed_length = fixed_length
  valid_prime?
  valid_coefficients?
end

Instance Attribute Details

#coefficientsObject

Returns the value of attribute coefficients.



9
10
11
# File 'lib/hensel_code/polynomial.rb', line 9

def coefficients
  @coefficients
end

#fixed_lengthObject

Returns the value of attribute fixed_length.



9
10
11
# File 'lib/hensel_code/polynomial.rb', line 9

def fixed_length
  @fixed_length
end

#primeObject

Returns the value of attribute prime.



9
10
11
# File 'lib/hensel_code/polynomial.rb', line 9

def prime
  @prime
end

Instance Method Details

#*(other) ⇒ Object



31
32
33
34
35
# File 'lib/hensel_code/polynomial.rb', line 31

def *(other)
  valid_operands?(other)
  new_coefficients = multiplication(prime, coefficients, other.coefficients)
  self.class.new prime, new_coefficients[0..coefficients.size - 1]
end

#+(other) ⇒ Object



19
20
21
22
23
# File 'lib/hensel_code/polynomial.rb', line 19

def +(other)
  valid_operands?(other)
  new_coefficients = addition(prime, coefficients, other.coefficients)
  self.class.new prime, new_coefficients
end

#-(other) ⇒ Object



25
26
27
28
29
# File 'lib/hensel_code/polynomial.rb', line 25

def -(other)
  valid_operands?(other)
  new_coefficients = subtraction(prime, coefficients, other.coefficients)
  self.class.new prime, new_coefficients
end

#/(other) ⇒ Object



37
38
39
40
41
# File 'lib/hensel_code/polynomial.rb', line 37

def /(other)
  valid_operands?(other)
  new_coefficients = (self * other.inverse).coefficients
  self.class.new prime, new_coefficients[0..coefficients.size - 1]
end

#degreeObject



60
61
62
# File 'lib/hensel_code/polynomial.rb', line 60

def degree
  coefficients.size - 1
end

#inspectObject



56
57
58
# File 'lib/hensel_code/polynomial.rb', line 56

def inspect
  "<Polynomial: #{polynomial_form}>"
end

#inverseObject



43
44
45
46
47
48
# File 'lib/hensel_code/polynomial.rb', line 43

def inverse
  x = generate_padic_x
  two = generate_padic_constant_integer(2)
  x = (two * x) - (self * x * x) while (x * self).coefficients != [1] + Array.new(coefficients.size - 1, 0)
  x
end

#to_sObject



50
51
52
53
54
# File 'lib/hensel_code/polynomial.rb', line 50

def to_s
  coefficients.map.with_index do |c, i|
    "#{c}#{polynomial_variable(i)}"
  end.join(" + ")
end