Class: ST

Inherits:
Numeric show all
Defined in:
lib/m500.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#irrational?, #rational?, #sgml_id, #to_Dec, #to_Frac, #to_K, #to_N, #to_N0, #to_Q, #to_R, #to_Sig, #to_Z, #to_sgml

Methods included from SGML

#sgml_id, #tog_sgml_id

Constructor Details

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

class SymbolicTerm<Numeric



441
442
443
444
445
446
447
448
449
450
# File 'lib/m500.rb', line 441

def initialize(a,b=0,c='t') # a*(c) + b
  if a.kind_of?(String)
    regex = /(\d+[a-z])*(\+)?(\d+)?/
    m = regex.match(a)
    n = /(\d+)([a-z])/.match(m[1]) if m[1]
    @a = [n.nil? ? 0 : n[1] ? n[1] : 0, m[3] ? m[3] : 0, n.nil? ? c : n[2] ? n[2] : 0]
  else
    @a = [a,b,c]
  end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



451
452
453
# File 'lib/m500.rb', line 451

def a
  @a
end

Instance Method Details

#*(o) ⇒ Object



461
462
463
464
465
466
467
468
469
# File 'lib/m500.rb', line 461

def *(o)
  if o.kind_of?(ST) then
    "#{@a.at(0).to_i*o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i*o.a.at(1)}"
  elsif o == 0
    "0"
  else
    "#{@a.at(0).to_i*o}#{@a.at(2)}+#{@a.at(1).to_i*o}" if o.kind_of?(Numeric)
  end
end

#+(o) ⇒ Object



452
453
454
455
456
457
458
459
460
# File 'lib/m500.rb', line 452

def +(o)
  if o.kind_of?(ST) then
    "#{@a.at(0).to_i+o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i+o.a.at(1).to_i}"
  elsif o == 0
    self
  else
    a.at(0).to_i ==0 ? o : "#{@a.at(0)}#{@a.at(2)}+#{@a.at(1).to_i+o}" if o.kind_of?(Numeric)
  end
end

#-(o) ⇒ Object



470
471
472
473
474
475
476
# File 'lib/m500.rb', line 470

def -(o)
  if o.kind_of?(ST) then
    "#{@a.at(0).to_i-o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1)-o.a.at(1)}"
  else
    "#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)-o}" if o.kind_of?(Numeric)
  end
end

#/(o) ⇒ Object



477
478
479
480
481
482
483
484
485
# File 'lib/m500.rb', line 477

def /(o)
  if o.kind_of?(ST) then
    "#{@a.at(0).to_i/o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i/o.a.at(1)}"
  elsif o == 0
    "OO"
  else
    "#{@a.at(0).to_i/o}#{@a.at(2)}+#{@a.at(1).to_i/o}" if o.kind_of?(Numeric)
  end
end

#coerce(o) ⇒ Object



486
487
488
# File 'lib/m500.rb', line 486

def coerce(o)
  [self, o]
end

#inspectObject



492
493
494
# File 'lib/m500.rb', line 492

def inspect
  "'#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)}'.to_ST"
end

#to_sObject



489
490
491
# File 'lib/m500.rb', line 489

def to_s
  "'#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)}'"
end