Class: Symbolic_Prod

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

Constant Summary collapse

@@powersym =
'\*\*'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

a = [‘e’,‘PI’],b = “32e**2PI**4+31e**5PI**3 + 30PI + Natural(29)



3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
# File 'lib/m500.rb', line 3237

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

Instance Attribute Details

#exponentsObject

mk private



3248
3249
3250
# File 'lib/m500.rb', line 3248

def exponents
  @exponents
end

#orig_statementObject

Returns the value of attribute orig_statement.



3247
3248
3249
# File 'lib/m500.rb', line 3247

def orig_statement
  @orig_statement
end

#symbolic_labelsObject

Returns the value of attribute symbolic_labels.



3247
3248
3249
# File 'lib/m500.rb', line 3247

def symbolic_labels
  @symbolic_labels
end

Instance Method Details

#*(o) ⇒ Object



3423
3424
3425
# File 'lib/m500.rb', line 3423

def *(o)
  
end

#+(o) ⇒ Object



3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
# File 'lib/m500.rb', line 3390

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



3426
3427
3428
# File 'lib/m500.rb', line 3426

def -(o)
 
end

#/(o) ⇒ Object



3429
3430
3431
# File 'lib/m500.rb', line 3429

def /(o)
 
end

#add_exponent_accessor(a) ⇒ Object



3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
# File 'lib/m500.rb', line 3371

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

#arityObject

private_class_method :new



3250
3251
3252
# File 'lib/m500.rb', line 3250

def arity
  @symbolic_labels.size
end

#cannonical_term_sorted_listObject

end



3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
# File 'lib/m500.rb', line 3491

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_methodsObject



3383
3384
3385
3386
3387
3388
3389
# File 'lib/m500.rb', line 3383

def coef_methods
  tmp = []
  methods.each{|md|
    tmp << md if md.match(/coef_at_/)
  }
  tmp
end

#coerce(o) ⇒ Object



3482
3483
3484
# File 'lib/m500.rb', line 3482

def coerce(o)
  [self, o]
end

#exponent_full_sorted_listObject



3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
# File 'lib/m500.rb', line 3333

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_listObject

look like a = la1b3_lb2l34_lb3l45, b=coeff , ‘la1b3_lb2l34_lb3l45’=>coeff



3358
3359
3360
3361
3362
# File 'lib/m500.rb', line 3358

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



3363
3364
3365
3366
3367
3368
3369
3370
# File 'lib/m500.rb', line 3363

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



3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
# File 'lib/m500.rb', line 3253

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_statementObject



3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
# File 'lib/m500.rb', line 3457

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_sObject



3485
3486
3487
# File 'lib/m500.rb', line 3485

def to_s
  to_full_statement
end

#to_statementObject



3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
# File 'lib/m500.rb', line 3432

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