Sha256: 667d51ba2fb0609d12e63c993976ffd7d17920cf988bf383c4c1130e1b78b1a9

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module HenselCode
  # base hensel code class
  class PAdicBase
    include Tools
    include PAdicVerifier

    attr_accessor :prime, :exponent, :rational, :hensel_code, :n
    private :prime=, :exponent=, :rational=, :hensel_code=

    def initialize(prime, exponent, number)
      can_initilize?
      @prime = prime
      @exponent = exponent
      @n = Integer.sqrt(((prime**exponent) - 1) / 2)
      valid_number?(number)
      encode
      decode
    end

    def numerator
      rational.numerator
    end

    def denominator
      rational.denominator
    end

    def to_r
      decode
      rational
    end

    def +(other)
      valid?(other)
      evaluate("+", other)
    end

    def -(other)
      valid?(other)
      evaluate("-", other)
    end

    def *(other)
      valid?(other)
      evaluate("*", other)
    end

    def /(other)
      valid?(other)
      evaluate("/", other)
    end

    def replace_prime(new_prime)
      replace_attribute("prime=", new_prime, 0)
    end

    def replace_exponent(new_exponent)
      replace_attribute("exponent=", new_exponent, 0)
    end

    def replace_rational(new_rational)
      replace_attribute("rational=", new_rational, 0)
    end

    def replace_hensel_code(new_hensel_code)
      valid_hensel_code?(new_hensel_code)
      replace_attribute("hensel_code=", new_hensel_code, 1)
    end

    private

    def can_initilize?
      message = "#{self.class} can only be inherited."
      raise NonInitializableClass, message if instance_of?(HenselCode::PAdicBase)
    end

    def replace_attribute(attribute, new_value, order)
      send(attribute, new_value)
      order.zero? ? [encode, decode] : [decode, encode]
      self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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