class Algebra::LocalizedRing

Attributes

denominator[R]
numerator[R]

Public Class Methods

[](num, den = nil) click to toggle source
# File lib/algebra/localized-ring.rb, line 69
def self.[](num, den = nil)
  den ? reduce(num, den) : unity * num
end
create(ground) click to toggle source
Calls superclass method
# File lib/algebra/localized-ring.rb, line 48
def self.create(ground)
  klass = super(ground)
  klass.sysvar(:reducible, true)
  klass
end
new(num, den = ground.unity) click to toggle source
# File lib/algebra/localized-ring.rb, line 113
def initialize(num, den = ground.unity)
  if den.respond_to?(:<=>) && den < ground.zero
    num = -num
    den = -den
  end
  @numerator = num
  @denominator = den
end
reduce(num, den) click to toggle source
# File lib/algebra/localized-ring.rb, line 73
def self.reduce(num, den)
  reducible ? simplify(num, den) : new(num, den)
end
reducible() click to toggle source
# File lib/algebra/localized-ring.rb, line 54
def self.reducible
  @@reducible
end
reducible=(sw) click to toggle source
# File lib/algebra/localized-ring.rb, line 58
def self.reducible=(sw)
  @@reducible = sw
end
simplify(num, den) click to toggle source
# File lib/algebra/localized-ring.rb, line 77
def self.simplify(num, den)
  raise ZeroDivisionError, 'denometor is 0' if den.zero?
  return zero if num.zero?

  if ground.field?
    num /= den
    den = ground.unit
  elsif ground.ufd? # ground.euclidian?
    gcd = num.gcd(den)
    num /= gcd
    den /= gcd
    #      num = num.div(gcd)
    #      den = den.div(gcd)
  end

  # regulate the leading coefficient of polynomil
  if defined?(Polynomial) && ground <= Polynomial
    if ground.ground.field?
      m = den.lc
      num /= m
      den /= m
    elsif ground.ground.euclidian? # high cost!
      m = num.cont.gcd(den.cont)
      num /= m
      den /= m
    end
    #    elsif defined?(MPolynomial) && ground <= MPolynomial
  end

  #    if den.respond_to?(:<) and den < 0
  #      num = -num
  #      den = -den
  #    end
  new(num, den)
end

Public Instance Methods

*(o) click to toggle source
Calls superclass method Algebra::AlgebraBase#*
# File lib/algebra/localized-ring.rb, line 148
def *(o)
  super do |o|
    num = @numerator * o.numerator
    den = @denominator * o.denominator
    self.class.reduce(num, den)
  end
end
**(other) click to toggle source

def % (o)

raise ZeroDivisionError, "devided by 0" if o.zero?
den, a, = @denominator.gcd_coeff(o)
num = (@numerator * a) % o
q, r = num.divmod den
raise "#@denominator can not divide #@numerator mod #{o}" unless r.zero?
q

end

# File lib/algebra/localized-ring.rb, line 182
def **(other)
  case other
  when Integer
    if other > 0
      num = @numerator**other
      den = @denominator**other
      self.class.new(num, den)
    elsif other < 0
      num = @denominator**-other
      den = @numerator**-other
      self.class.new(num, den)
    elsif other.zero?
      unity
    end
  else
    x, y = other.coerce(self)
    x**y
  end
end
+(o) click to toggle source
Calls superclass method Algebra::AlgebraBase#+
# File lib/algebra/localized-ring.rb, line 132
def +(o)
  super do |o|
    num = @numerator * o.denominator
    num_o = o.numerator * @denominator
    self.class.reduce(num + num_o, @denominator * o.denominator)
  end
end
-(o) click to toggle source
Calls superclass method Algebra::AlgebraBase#-
# File lib/algebra/localized-ring.rb, line 140
def -(o)
  super do |o|
    num = @numerator * o.denominator
    num_o = o.numerator * @denominator
    self.class.reduce(num - num_o, @denominator * o.denominator)
  end
end
/(o) click to toggle source
Calls superclass method Algebra::AlgebraBase#/
# File lib/algebra/localized-ring.rb, line 156
def /(o)
  raise ZeroDivisionError, 'devided by 0' if o.zero?
  super do |o|
    num = @numerator * o.denominator
    den = @denominator * o.numerator
    self.class.reduce(num, den)
  end
end
==(o) click to toggle source
Calls superclass method Algebra::AlgebraBase#==
# File lib/algebra/localized-ring.rb, line 126
def ==(o)
  super do |o|
    @numerator * o.denominator == o.numerator * @denominator
  end
end
hash() click to toggle source
# File lib/algebra/localized-ring.rb, line 243
def hash
  raise 'hash is undefined'
end
inspect() click to toggle source
# File lib/algebra/localized-ring.rb, line 238
def inspect
  to_s
  #    sprintf("%s(%s/%s)", self.class, @numerator.inspect, @denominator.inspect)
end
monomial?() click to toggle source
# File lib/algebra/localized-ring.rb, line 65
def monomial?
  true
end
need_paren_in_coeff?() click to toggle source
# File lib/algebra/localized-ring.rb, line 202
def need_paren_in_coeff?
  if @denominator.unity?
    if @numerator.respond_to?(:need_paren_in_coeff?)
      @numerator.need_paren_in_coeff?
    elsif @numerator.is_a?(Numeric)
      false
    else
      true
    end
  else
    false
  end
end
pdivmod(other) click to toggle source
# File lib/algebra/localized-ring.rb, line 169
def pdivmod(other)
  [self / other, zero]
end
simplify() click to toggle source
# File lib/algebra/localized-ring.rb, line 122
def simplify
  self.class.simplify(@numerator, @denominator)
end
to_s() click to toggle source
# File lib/algebra/localized-ring.rb, line 216
def to_s
  n = if @numerator.respond_to?(:need_paren_in_coeff?) &&
         @numerator.need_paren_in_coeff? &&
         !@denominator.unity?
        "(#{@numerator})"
      else
        @numerator.to_s
      end
  d = if @denominator.respond_to?(:need_paren_in_coeff?) &&
         @denominator.need_paren_in_coeff? &&
         !@denominator.is_a?(Integer)
        "(#{@denominator})"
      else
        @denominator.to_s
      end
  if @denominator.unity?
    n.to_s
  else
    n + '/' + d
  end
end
unit?() click to toggle source
# File lib/algebra/localized-ring.rb, line 165
def unit?
  !zero? # some about
end