Sha256: 4e609b4ce36ffa75e3db33249ba9c3db2201f5a11783274dd219ee9806060222

Contents?: true

Size: 1.66 KB

Versions: 4

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module HenselCode
  # truncated finite p-adic expansion hensel code class
  class TruncatedFinitePadicExpansion < PAdicBase
    def modulus
      prime**exponent
    end

    def to_i
      hensel_code
    end

    def to_s
      hensel_code.to_s
    end

    def inspect
      "<HenselCode: #{hensel_code}>"
    end

    def inverse
      new_hensel_code = mod_inverse(hensel_code, modulus)
      self.class.new prime, exponent, new_hensel_code
    end

    private

    def evaluate(operation, other)
      if operation == "/"
        other_hensel_code = mod_inverse(other.hensel_code, modulus)
        op = "*"
      else
        other_hensel_code = other.hensel_code
        op = operation
      end
      self.class.new prime, exponent, hensel_code.send(op, other_hensel_code) % modulus
    end

    def valid_number?(number)
      case number
      when Rational
        @rational = number
      when Integer
        @hensel_code = number
        decode
      else
        raise WrongHenselCodeInputType, "number must be a Rational or an\
                                        Integer object and it was a #{number.class}"
      end
    end

    def valid_hensel_code?(new_hensel_code)
      message = "must be an integer up to #{modulus - 1}"
      raise WrongHenselCodeInputType, message unless new_hensel_code < modulus
    end

    def encode
      denominator_inverse = mod_inverse(denominator, modulus)
      @hensel_code = (numerator * denominator_inverse) % modulus
    end

    def decode
      eea_vars = eea_core(modulus, hensel_code, n)
      i, x, y = eea_vars[2..4]
      @rational = Rational(*[x, y].map { |e| ((-1)**(i + 1)) * e })
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hensel_code-0.4.1 lib/hensel_code/truncated_finite_padic_expansion.rb
hensel_code-0.4.0 lib/hensel_code/truncated_finite_padic_expansion.rb
hensel_code-0.3.1 lib/hensel_code/truncated_finite_padic_expansion.rb
hensel_code-0.3.0 lib/hensel_code/truncated_finite_padic_expansion.rb