Class: PolynomialInterpreter
- Inherits:
-
Object
- Object
- PolynomialInterpreter
- Defined in:
- lib/polynomial_interpreter.rb
Defined Under Namespace
Modules: Errors
Class Method Summary collapse
Class Method Details
.read_coeffs(input_polynomial) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/polynomial_interpreter.rb', line 48 def self.read_coeffs(input_polynomial) if input_polynomial == "" raise Errors::POLYNOMIAL_INVALID end last_var = nil coeffs = Array.new unless input_polynomial[0].match /[-+]/ input_polynomial = "+" + input_polynomial end while input_polynomial.length > 0 do op = input_polynomial.slice!(0) unless op =~ /[-+]/ raise Errors::POLYNOMIAL_INVALID end input_polynomial.slice!(/^(\d+)\*?/) match_data_coe = Regexp.last_match input_polynomial.slice!(/^([a-zA-Z])(?:\^(\d+))?/) match_data_exp = Regexp.last_match if match_data_coe.nil? and match_data_exp.nil? raise Errors::POLYNOMIAL_INVALID else if match_data_exp.nil? coe = match_data_coe[1].to_i exp = 0 else unless last_var.nil? or last_var == match_data_exp[1] raise Errors::POLYNOMIAL_INVALID end last_var = match_data_exp[1] if match_data_coe.nil? coe = 1 else coe = match_data_coe[1].to_i end if match_data_exp[2].nil? exp = 1 else exp = match_data_exp[2].to_i end end end coeffs[exp] ||= 0 if op == "-" coeffs[exp] -= coe.to_i else coeffs[exp] += coe.to_i end end 0.upto(coeffs.length-1) do |idx| coeffs[idx] ||= 0 end coeffs end |
.read_congruence(input_congruence) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/polynomial_interpreter.rb', line 11 def self.read_congruence(input_congruence) match_data = input_congruence.match(/^(.*)=(.*) +(?:mod +(.*)|\(mod +(.*)\))$/) if match_data.nil? raise Errors::CONGRUENCE_INVALID end lhs = match_data[1] rhs = match_data[2] mod = match_data[3] || match_data[4] begin lh_coeffs = read_coeffs(lhs.gsub(" ", "")) rescue ArgumentError raise Errors::LHS_POLYNOMIAL_INVALID end begin rh_coeffs = read_coeffs(rhs.gsub(" ", "")) rescue ArgumentError raise Errors::RHS_POLYNOMIAL_INVALID end if mod !~ /\d+/ or mod.to_i < 2 raise Errors::MOD_INVALID end 0.upto rh_coeffs.length-1 do |idx| unless rh_coeffs[idx].nil? lh_coeffs[idx] ||= 0 lh_coeffs[idx] -= rh_coeffs[idx] end end [lh_coeffs, mod.to_i] end |