class Symbolic_Term

Attributes

symbolic_label[RW]

Public Class Methods

new(a,b='',c='t') click to toggle source
# File lib/m500.rb, line 3055
def initialize(a,b='',c='t') #a = PI,b = "32PI**4+31PI**3 + 30PI + Natural(29)
  @exponents ={}
  exponents_add(0,1)
  #p     @exponents
  @symbolic_label = a
  @orig_statment = b
  @symbolic_expressions = []
  if b.kind_of?(String)
    b == '' ?  add_exponent_accessor(1) : parse(b)
  else
  end
end

Public Instance Methods

*(o) click to toggle source
# File lib/m500.rb, line 3168
def *(o)
  tmp = Symbolic_Term.new(@symbolic_label)
  if o.kind_of?(Quotient) or o.kind_of?(Zahlen) or o.kind_of?(Counting) or o.kind_of?(Natural)
      exponent_list.each{|a|
        tmp.exponents_add(a,(o.to_Q * send("coef_at_#{a}")))
      }
  elsif o.kind_of?(Symbolic_Term)
    if @symbolic_label == o.symbolic_label then
      exponent_list.each{|a|
        o.exponent_list.each{|b|
          tmp.exponents_add((a+b),( send("coef_at_#{a}") * o.send("coef_at_#{b}")))
        }
      }
    else
      empty_set
      #normally would return a SymbolicExpression, but should be ordered first
    end
  end
  tmp
end
+(o) click to toggle source
# File lib/m500.rb, line 3141
def +(o)
  tmp = Symbolic_Term.new(@symbolic_label)
  if o.kind_of?(Symbolic_Term) then
    exp_list = (exponent_list + o.exponent_list).uniq
    if @symbolic_label == o.symbolic_label then
      exp_list.each{|a|
        if respond_to?("coef_at_#{a}") then
          if o.respond_to?("coef_at_#{a}") then
            tmp.exponents_add(a,send("coef_at_#{a}") + o.send("coef_at_#{a}"))
          else
            tmp.exponents_add(a,send("coef_at_#{a}"))
          end
        else
          tmp.exponents_add(a,o.send("coef_at_#{a}")) if o.respond_to?("coef_at_#{a}")
        end
      }
      tmp
    else
    end
    elsif o.kind_of?(Quotient) or o.kind_of?(Zahlen) or o.kind_of?(Counting) or o.kind_of?(Natural)
      exponent_list.each{|a|
        a == '0' ? tmp.exponents_add(a,(o + coef_at_0 )) : tmp.exponents_add(a,send("coef_at_#{a}"))

      }
  end
  tmp
end
-(o) click to toggle source
# File lib/m500.rb, line 3188
def -(o)
  tmp = Symbolic_Term.new(@symbolic_label)
  if o.kind_of?(Symbolic_Term) then
    exp_list = (exponent_list + o.exponent_list).uniq
    if @symbolic_label == o.symbolic_label then
      exp_list.each{|a|
        if respond_to?("coef_at_#{a}") then
          if o.respond_to?("coef_at_#{a}") then
            tmp.exponents_add(a,send("coef_at_#{a}") - o.send("coef_at_#{a}"))
          else
            tmp.exponents_add(a,send("coef_at_#{a}"))
          end
        else
          tmp.exponents_add(a,(-1 * o.send("coef_at_#{a}"))) if o.respond_to?("coef_at_#{a}")
        end
      }
      tmp
    else
    end
  elsif o.kind_of?(Quotient) or o.kind_of?(Zahlen) or o.kind_of?(Counting) or o.kind_of?(Natural)
    exponent_list.each{|a|
      a == '0' ? tmp.exponents_add(a,(coef_at_0 - o )) : tmp.exponents_add(a,send("coef_at_#{a}"))
      
    }
  end
  tmp
end
/(o) click to toggle source
# File lib/m500.rb, line 3215
def /(o)
  if o.kind_of?(Quotient) or o.kind_of?(Zahlen) or o.kind_of?(Counting) or o.kind_of?(Natural)
    exponent_list.each{|a|
      tmp.exponents_add(a,(send("coef_at_#{a}")/o.to_Q))
    }
  end
  tmp
end
add_exponent_accessor(a) click to toggle source
# File lib/m500.rb, line 3126
def add_exponent_accessor(a)
  if exponent_list.include?(a)then
  else
    z = " @exponent_#{a} = 1
    def exp_#{a}
     @exponents[\"#{a}\"]
    end
    def coef_at_#{a}
     @exponents[\"#{a}\"]
    end
    "
    instance_eval(z)
    @exponents[a.to_s] = '1'
  end
end
coerce(o) click to toggle source
# File lib/m500.rb, line 3248
def coerce(o)
  [self, o]
end
exponent_list() click to toggle source
# File lib/m500.rb, line 3115
def exponent_list
  tmp = []
  @exponents.sort.map{|a,b| tmp << a}
  tmp
end
exponents_add(a,b) click to toggle source
# File lib/m500.rb, line 3120
def exponents_add(a,b)
  a = a.to_s
  a.gsub!('/','_') if a.match('/')
  add_exponent_accessor(a) unless @exponents.has_key?(a)
  @exponents[a] = b
end
inspect() click to toggle source
# File lib/m500.rb, line 3255
def inspect
  "Symbolic_Term(#{@symbolic_label}#{@orig_statment})"
end
parse(b) click to toggle source

private_class_method :new

# File lib/m500.rb, line 3069
  def parse(b)
    reg0 = /([^\+]+)/
    reg1 = /(-?[0-9\(\)]+)(\/?[0-9]*)([a-zA-Z\(\)0-9]+)\*\*(-?[0-9\(\)]+)(\/?[0-9]*)/
    #    md =  b.match(reg0)
    md =  ""
    frstrmneg = false
    frstrmneg = true if b.match(/^-/)
    if  frstrmneg then
      b.gsub!(/^-/,'')
      b.gsub!('-','+-')
      md = b.split('+')
      md[0] = '-' + md[0]
    else
      b.gsub!('-','+-')
      md = b.split('+')
    end
    md.each{|re|
      ans = re.match(reg1)
      if ans
#p ans[1],ans[2],ans[3],ans[4],ans[5]
 #       exponents_add(instance_eval(ans[3]),instance_eval(ans[1]))
        if ans[2]=='' and ans[5]=='' then
          exponents_add(instance_eval(ans[4]),instance_eval(ans[1]))
        elsif ans[5]==''
          exponents_add(instance_eval(ans[4]),instance_eval("Quotient(#{ans[1]},#{ans[2].gsub('/','')})"))
        elsif ans[2]==''
          exponents_add(instance_eval("Quotient(#{ans[4]},#{ans[5].gsub('/','')})"),instance_eval("Quotient(#{ans[1]},#{ans[2].gsub('/','')})"))
        else
          exponents_add(instance_eval("Quotient(#{ans[4]},#{ans[5].gsub('/','')})"),instance_eval("Quotient(#{ans[1]},#{ans[2].gsub('/','')})"))
        end
      else
        ans1 = re.match(/#{@symbolic_label}/)
        if ans1 then
          #p "#{@symbolic_label}#{@@powersym.gsub('\\','')}1"
          re.gsub!(@symbolic_label,'')
#          exponents_add('1',instance_eval(re.strip!))
          exponents_add('1',re.strip!)
        else
          #p "constant?"
          re.strip!
          re.gsub!('-','Zahlen(-1)*')
         exponents_add('0',instance_eval(re))
        end
      end
    }   
  end
to_s() click to toggle source
# File lib/m500.rb, line 3251
  def to_s
#   @orig_statment == '' ? to_statement : @orig_statment
    to_statement
  end
to_statement() click to toggle source
# File lib/m500.rb, line 3223
def to_statement
  tmp = '' 
  first = true   
  exponent_list.reverse.each{|a|
    a0 = instance_eval("coef_at_#{a}")
    a0 = instance_eval(a0) if a0.kind_of?(String)
    if a0<0 then
      first ? tmp += "-" : tmp += " - "
      if a == '0' or a=='1' then
        a=='1' ? tmp += (Zahlen(-1)*a0).to_s + @symbolic_label.to_s : tmp += (Zahlen(-1)*a0).to_s
      else
        tmp += (Zahlen(-1)*a0).to_s + @symbolic_label.to_s + @@powersym.to_s.gsub('\\','') + a.to_s
      end
    else
      tmp += " + " unless first
      if a == '0' or a=='1' then
        a=='1' ? tmp += a0.to_s + @symbolic_label.to_s : tmp += a0.to_s
      else
        tmp += a0.to_s + @symbolic_label.to_s + @@powersym.to_s.gsub('\\','') + a.to_s
      end
    end
    first = false
  }
  tmp
end