Class: Symbolic_Term

Inherits:
Object
  • Object
show all
Defined in:
lib/m500.rb

Overview

<ST

Constant Summary collapse

@@powersym =
'\*\*'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b = '', c = 't') ⇒ Symbolic_Term

a = PI,b = “32PI**4+31PI**3 + 30PI + Natural(29)



3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
# File 'lib/m500.rb', line 3031

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

Instance Attribute Details

#symbolic_labelObject

Returns the value of attribute symbolic_label.



3043
3044
3045
# File 'lib/m500.rb', line 3043

def symbolic_label
  @symbolic_label
end

Instance Method Details

#*(o) ⇒ Object



3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
# File 'lib/m500.rb', line 3144

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) ⇒ Object



3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
# File 'lib/m500.rb', line 3117

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) ⇒ Object



3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
# File 'lib/m500.rb', line 3164

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) ⇒ Object



3191
3192
3193
3194
3195
3196
3197
3198
# File 'lib/m500.rb', line 3191

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) ⇒ Object



3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
# File 'lib/m500.rb', line 3102

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) ⇒ Object



3224
3225
3226
# File 'lib/m500.rb', line 3224

def coerce(o)
  [self, o]
end

#exponent_listObject



3091
3092
3093
3094
3095
# File 'lib/m500.rb', line 3091

def exponent_list
  tmp = []
  @exponents.sort.map{|a,b| tmp << a}
  tmp
end

#exponents_add(a, b) ⇒ Object



3096
3097
3098
3099
3100
3101
# File 'lib/m500.rb', line 3096

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

#inspectObject



3231
3232
3233
# File 'lib/m500.rb', line 3231

def inspect
  "Symbolic_Term(#{@symbolic_label}#{@orig_statment})"
end

#parse(b) ⇒ Object

private_class_method :new



3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
# File 'lib/m500.rb', line 3045

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_sObject



3227
3228
3229
3230
# File 'lib/m500.rb', line 3227

def to_s
#   @orig_statment == '' ? to_statement : @orig_statment
  to_statement
end

#to_statementObject



3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
# File 'lib/m500.rb', line 3199

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