class Symbolic_Prod
Attributes
exponents[RW]
orig_statement[RW]
symbolic_labels[RW]
Public Class Methods
new(a,b='',c='t')
click to toggle source
# File lib/m500.rb, line 3261 def initialize(a,b='',c='t') #a = ['e','PI'],b = "32e**2PI**4+31e**5PI**3 + 30PI + Natural(29) @exponents ={} @symbolic_labels = a @orig_statement = b parse(b) 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 3447 def *(o) end
+(o)
click to toggle source
# File lib/m500.rb, line 3414 def +(o) tmp = Symbolic_Prod.new(@symbolic_label) coeffs = public_methods.select{|i| i[/coef_at[_\d+]+$/] } tmp = self.dup str = "" tplt = '_0' first = true arity.times{ first ? tplt = '1' : tplt = '_0' str += tplt first = false if first } if o.kind_of?(Symbolic_Prod) then sym_list = (symbolic_labels + o.symbolic_labels).uniq elsif o.kind_of?(Symbolic_Term) then if @symbolic_labels.contain(o.symbolic_label) then else end elsif o.kind_of?(Quotient) or o.kind_of?(Zahlen) or o.kind_of?(Counting) or o.kind_of?(Natural) p o.to_Q str = "" tplt = '_0' first = true arity.times{ first ? tplt = '0' : tplt = '_0' str += tplt first = false } @exponents.has_key?(str) ? @exponents[str].to_Q += o.to_Q : tmp.add_exponent_accessor(str) ; @exponents[str] = o.to_Q tmp.orig_statement += " + #{@exponents[str]}" end tmp end
-(o)
click to toggle source
# File lib/m500.rb, line 3450 def -(o) end
/(o)
click to toggle source
# File lib/m500.rb, line 3453 def /(o) end
add_exponent_accessor(a)
click to toggle source
# File lib/m500.rb, line 3395 def add_exponent_accessor(a) z = "" unless @exponents.has_key?(a)then z = " def coef_at_#{a} @exponents[\"#{a}\"] end " end instance_eval(z) @exponents[a] = '1' end
arity()
click to toggle source
private_class_method :new
# File lib/m500.rb, line 3274 def arity @symbolic_labels.size end
cannonical_term_sorted_list()
click to toggle source
def inspect
"Symbolic_Prod(#{@symbolic_labels},#{@orig_statment})"
end
# File lib/m500.rb, line 3515 def cannonical_term_sorted_list cannonical_terms = {} complete_terms = {} st1 = "" st2 = "" st3 = "" str2 = false exponent_sorted_list.each{|a| tmp = a.split('_') first = true symbolic_labels.each_index{|i| if tmp.at(i) == '0' then str2 = true first ? st2 += "1" : st2 += ".1" st3 = "1" elsif tmp.at(i) == '1' then str2 = true first ? st2 += "#{symbolic_labels.at(i)}" : st2 += ".#{symbolic_labels.at(i)}" end (first ? st2 += "#{symbolic_labels.at(i)}**#{tmp.at(i)}" : st1 += ".#{symbolic_labels.at(i)}**#{tmp.at(i)}") unless str2 str2 = false first ? st1 += "#{symbolic_labels.at(i)}**#{tmp.at(i)}" : st1 += ".#{symbolic_labels.at(i)}**#{tmp.at(i)}" first = false } cannonical_terms[st1] = a complete_terms[st2.gsub('.1','')] = a st1 = "" st2 = "" st3 = "" } complete_terms.merge!(cannonical_terms) complete_terms end
coef_methods()
click to toggle source
# File lib/m500.rb, line 3407 def coef_methods tmp = [] methods.each{|md| tmp << md if md.match(/coef_at_/) } tmp end
coerce(o)
click to toggle source
# File lib/m500.rb, line 3506 def coerce(o) [self, o] end
exponent_full_sorted_list()
click to toggle source
# File lib/m500.rb, line 3357 def exponent_full_sorted_list test = {} st1 = "" st2 = "" str2 = false exponent_sorted_list.each{|a| tmp = a.split('_') first = true symbolic_labels.each_index{|i| if tmp.at(i) == '0' then str2 = true first ? st2 += "1" : st2 += ".1" elsif tmp.at(i) == '1' then str2 = true first ? st2 += "#{symbolic_labels.at(i)}" : st2 += ".#{symbolic_labels.at(i)}" end first ? st1 += "#{symbolic_labels.at(i)}**#{tmp.at(i)}" : st1 += ".#{symbolic_labels.at(i)}**#{tmp.at(i)}" first = false } test[st1] = a test[st2] = a ; str2 = false if str2 st1 = "" } test end
exponent_sorted_list()
click to toggle source
# File lib/m500.rb, line 3382 def exponent_sorted_list #look like a = la1b3_lb2l34_lb3l45, b=coeff , {'la1b3_lb2l34_lb3l45'=>coeff} tmp = [] @exponents.sort.map{|a,b| tmp << a} tmp end
exponents_add(a,b)
click to toggle source
# File lib/m500.rb, line 3387 def exponents_add(a,b) a = a.to_s # b = b.to_s a.gsub!('/','_') if a.match('/') add_exponent_accessor(a) unless @exponents.has_key?(a) # @exponents[a] = b @exponents[a] = b.to_Q end
parse(b)
click to toggle source
# File lib/m500.rb, line 3277 def parse(b) #scan + #32(e**2PI**4)+1(e**5.PI**3)+ 30PI reg0 = /(-?[0-9]+)\(([^\)]*)\)/ #32(e**2PI**4) => m[1] = 3 m[2]= (e**2PI**4) reg1 = /([a-zA-Z]+)\*\*([0-9]+)/ 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 coeffc = "" labels = [] expnts = [] lbsort = [] md.each{|re| re.strip! ans1 = re.split('.') if ans1.size < @symbolic_labels.size + 1 then # we have missing labels .. so all exponents are 0 ansdup = ans1.dup missing_lbls = @symbolic_labels.dup poss_coeff = ansdup.shift if poss_coeff.match(reg1) then # then is not a number..ie no coeff.. ie implied coeff == one ans1.unshift("Natural(1)") ansdup.unshift(poss_coeff) # put back the label! elsif poss_coeff.match(/[0-9]/) # could be other numbers! ansdup.map!{|item| item.to_s + "**1"} ansdup.unshift(poss_coeff) # put back the label! ans1 = ansdup.dup ansdup.shift else poss_coeff += "**1" if poss_coeff.match(/\*\*/).nil? # no power end ansdup.each{|t| tst2 = t.match(reg1) missing_lbls.delete(tst2[1]) if @symbolic_labels.include?(tst2[1]) } missing_lbls.map!{|item| item.to_s + "**0"} ans1 += missing_lbls end ans1.each{|re1| # each labl re2 = re1.match(reg1) if re1.match(reg1) labels << re2[1] lbsort << re2[1] expnts << re2[2] else # "coeffiecient" coeffc = re1 end } if labels.empty? and lbsort.empty? and expnts.empty? # "constant" labels = @symbolic_labels if labels.size == 0 lbsort = @symbolic_labels if lbsort.size == 0 expnts = Array.new(labels.size,'0') if expnts.size ==0 else # "coeff" end tmp = "" tmp0 = "" lbsort.sort! first = true lbsort.each_index{|i| first ? tmp += "#{labels.at(labels.index(lbsort.at(i)))}_#{expnts.at(labels.index(lbsort.at(i)))}" : tmp += ".#{labels.at(labels.index(lbsort.at(i)))}_#{expnts.at(labels.index(lbsort.at(i)))}" first ? tmp0 += "#{expnts.at(labels.index(lbsort.at(i)))}" : tmp0 += "_#{expnts.at(labels.index(lbsort.at(i)))}" first = false } labels = [] expnts = [] lbsort = [] add_exponent_accessor(tmp0) unless @exponents.has_key?(tmp0) ncoef = instance_eval(coeffc) ncoef.kind_of?(Numeric) ? @exponents[tmp0] = ncoef.to_Q : @exponents[tmp0] = coeffc } end
to_full_statement()
click to toggle source
# File lib/m500.rb, line 3481 def to_full_statement tmp = '' first = true exponent_sorted_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 += (Quotient(-1,1)*a0).to_s + @symbolic_label.to_s : tmp += (Quotient(-1,1)*a0).to_s else tmp += (Quotient(-1,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
to_s()
click to toggle source
# File lib/m500.rb, line 3509 def to_s to_full_statement end
to_statement()
click to toggle source
# File lib/m500.rb, line 3456 def to_statement tmp = '' first = true exponent_sorted_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 += (Quotient(-1,1)*a0).to_s + @symbolic_label.to_s : tmp += (Quotient(-1,1)*a0).to_s else tmp += (Quotient(-1,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