class Algebra::Factors

Public Class Methods

[](*a) click to toggle source
# File lib/algebra/factors.rb, line 11
def self.[](*a)
  fact = new
  a.each do |x|
    fact.multiply(x)
  end
  fact
end
mk(*a) click to toggle source
# File lib/algebra/factors.rb, line 18
def self.mk(*a)
  k = 0
  b = []
  while k < a.size
    f = a[k]; k += 1
    i = a[k]; k += 1
    raise "parameter error (#{i})" unless i.is_a? Integer
    b.push [f, i]
  end
  new(b)
end
new(array = []) click to toggle source
# File lib/algebra/factors.rb, line 36
def initialize(array = [])
  @bone = array
end
seki(a) click to toggle source
# File lib/algebra/factors.rb, line 29
def self.seki(a)
  k = a.first
  a[1..-1].each do |m|
    k *= m
  end
  k
end

Public Instance Methods

*(o) click to toggle source
# File lib/algebra/factors.rb, line 112
def *(o)
  case o
  when self.class
    dup.concat(o)
  else
    #self.class.new(dup.multiply(o).to_a)
    dup.multiply(o)
  end
end
**(n) click to toggle source
# File lib/algebra/factors.rb, line 124
def **(n)
  fac = self.class.new
  n.times do
    fac *= self
  end
  fac
end
<<(other, n = 1)
Alias for: multiply
==(fact) click to toggle source
# File lib/algebra/factors.rb, line 145
def ==(fact)
  if pi == fact.pi
    # flag = false
    @bone.each do |f, i|
      fact.each do |g, j|
        if f == g || f == -g # this will be improved.
          unless i == j
            return false
          end
        end
        end
      end
      true
  else
          false
  end
end
collect(&b) click to toggle source
# File lib/algebra/factors.rb, line 56
def collect(&b); self.class.new(@bone.collect(&b)); end
collect!(&b) click to toggle source
# File lib/algebra/factors.rb, line 58
def collect!(&b); @bone.collect!(&b); self; end
concat(facts) click to toggle source
# File lib/algebra/factors.rb, line 105
def concat(facts)
  facts.each do |f, i|
    multiply f, i
  end
  self
end
correct_lc!(f) click to toggle source
# File lib/algebra/factors.rb, line 255
def correct_lc!(f)
  k = f.ground.unity
  each_with_index do |(g, i), j|
    glc = g.lc
    if glc != glc.unity && glc.unit?
      @bone[j] = [g/glc, i]
    else
      k *= glc ** i
    end
  end
  m = f.lc / k
  unless m == f.ground.unity
    unshift([m*f.unity, 1])
  end
  self
end
dup() click to toggle source
# File lib/algebra/factors.rb, line 39
def dup; self.class.new(@bone.collect{|f, i| [f, i]}); end
each(&b) click to toggle source
# File lib/algebra/factors.rb, line 42
def each(&b); @bone.each(&b); end
each_product() { |f| ... } click to toggle source
# File lib/algebra/factors.rb, line 185
def each_product #create non trivial powers
  idx0 = (0...size).to_a
  indices = []
  idx0.each do |i|
    break if i >= size
    indices.push [idx0[i...size], [i], fact(i)]
  end

  avoid = []
  until indices.empty?
    idx, rdx, f = indices.shift
    # i = idx.first
    next if avoid.find{|x| !(rdx & x).empty? }
    #print rdx.inspect + " : "
    r = yield(f)
    if r
      avoid.push rdx
      next
    end
    idx.each_index do |k|
      if k > 0 and k < idx.size
        idx1 = idx[k..-1]
        rdx1 = rdx + [idx[k]]
        next if avoid.find{|x| !(rdx1 & x).empty?}
        f1 = f * fact(idx[k])
        indices.push [idx1, rdx1, f1]
      end
    end
  end
end
each_product0() { |fact0| ... } click to toggle source
# File lib/algebra/factors.rb, line 172
def each_product0 #create non trivial powers
  if size > 0
    fact0, _e0 = head
    yield fact0
    tail.each_product do |fact, e|
      yield fact
    end
    tail.each_product do |fact, e|
      yield fact0*fact # e is ignored
    end
  end
end
empty?() click to toggle source
# File lib/algebra/factors.rb, line 51
def empty?; @bone.empty?; end
fact(i) click to toggle source
# File lib/algebra/factors.rb, line 50
def fact(i); @bone[i].first; end
fact_all(f) click to toggle source
# File lib/algebra/factors.rb, line 216
def fact_all(f)
  facts = self.class.new
  each do |g, e|
    next if e <= 0
    i = 0
    loop do
      q, r = f.divmod_ED g
      if r.zero?
        f = q
        i += 1
      else
        facts.push [g, i]
        break
      end
    end
  end
  facts
end
fact_all_u(f) click to toggle source
# File lib/algebra/factors.rb, line 235
def fact_all_u(f)
  facts = self.class.new
  each do |g, e|
    next if e <= 0
    next if g.constant?
    i = 0
    loop do
      q, r = f.divmod g
      if r.zero?
        f = q.first
        i += 1
      else
        facts.push [g, i]
        break
      end
    end
  end
  facts
end
fact_size() click to toggle source
# File lib/algebra/factors.rb, line 45
def fact_size;
  n = 0
  each do |f, i|; n += i; end
  n
end
gcd(other) click to toggle source
# File lib/algebra/factors.rb, line 93
def gcd(other)
  bone = []
  @bone.each do |f, n|
    i = n
    until i == 0 || (other % [f ** i]).zero?
      i -= 1
    end
    bone.push [f, i]
  end
  self.class.new(bone)
end
head() click to toggle source
# File lib/algebra/factors.rb, line 52
def head; @bone.first; end
inspect()
Alias for: to_s
map() { |f| ... } click to toggle source
# File lib/algebra/factors.rb, line 57
def map(&b); self.class.new(@bone.collect{|f, i| [yield(f), i]}); end
map!() { |f| ... } click to toggle source
# File lib/algebra/factors.rb, line 59
def map!(&b); @bone.collect!{|f, i| [yield(f), i]}; self; end
mcorrect_lc!(f, an) { |m| ... } click to toggle source
# File lib/algebra/factors.rb, line 272
    def mcorrect_lc!(f, an)
#      k = f.ground.unity
      k = f.unity
      each_with_index do |(g, i), j|
        glc = g.lc_at(an)
        if glc.unit? && glc != glc.unity
          @bone[j] = [g/glc, i]
        else
          k *= glc ** i
        end
      end
      m = f.lc_at(an).divmod(k).first.first
      unless  m == f.ground.unity
        concat yield(m)
      end
      self
    end
multiply(other, n = 1) click to toggle source
# File lib/algebra/factors.rb, line 78
def multiply(other, n = 1)
  flag = false
  @bone.each do |fact|
    f, _i = fact
    if f == other
      fact[1] = fact[1]+ 1
      flag = true
      break
    end
  end
  @bone.push [other, n] unless flag
  normalize!
  self
end
Also aliased as: <<
normalize!() click to toggle source
# File lib/algebra/factors.rb, line 61
def normalize!
  c = nil
  @bone.delete_if do |fact|
    f, i = fact
    if f.respond_to?(:constant?) and f.constant?
      c and c *= f**i or c = f**i
      true
    end
  end
  if c
    unless c.respond_to?(:unity?) && c.unity? && @bone.size > 0
      @bone.unshift [c, 1]
    end
  end
  self
end
pi() click to toggle source
# File lib/algebra/factors.rb, line 132
def pi
  x = 1
  @bone.each do |f_i|
    f, i = f_i
    if i
      x *= f**i
    else
      x *= f
    end
  end
  x
end
push(other) click to toggle source
# File lib/algebra/factors.rb, line 54
def push(other); @bone.push other; self; end
shift() click to toggle source
# File lib/algebra/factors.rb, line 44
def shift; @bone.shift; end
size() click to toggle source
# File lib/algebra/factors.rb, line 43
def size; @bone.size; end
tail() click to toggle source
# File lib/algebra/factors.rb, line 53
def tail; self.class.new(@bone[1..-1]); end
to_a() click to toggle source
# File lib/algebra/factors.rb, line 40
def to_a; @bone; end
Also aliased as: to_ary
to_ary()
Alias for: to_a
to_s() click to toggle source
# File lib/algebra/factors.rb, line 163
def to_s
  return "1" if @bone.empty?
  a, b = @bone.size == 1 && @bone.first.last == 1? ["", ""] : ["(", ")"]
  @bone.collect{|x|
    "#{a}#{x.first}#{b}" + (x.last > 1 ? "^#{x.last}" : "")
  }.join("")
end
Also aliased as: inspect
unshift(other) click to toggle source
# File lib/algebra/factors.rb, line 55
def unshift(other); @bone.unshift(other); self; end