Class: Symbolic::Printer

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

Class Method Summary collapse

Class Method Details

.brackets(var) ⇒ Object



12
13
14
# File 'lib/symbolic/printer.rb', line 12

def brackets(var)
  [Numeric, Variable, Function].any? { |c| var.is_a? c } ? var.to_s : "(#{var})"
end

.coef(c) ⇒ Object



20
21
22
# File 'lib/symbolic/printer.rb', line 20

def coef(c)
  "#{'-' if c < 0}#{"#{rational c.abs}*" if c.abs != 1}"
end

.coef_with_sign(c) ⇒ Object



23
24
25
# File 'lib/symbolic/printer.rb', line 23

def coef_with_sign(c)
  "#{ c < 0 ? '-' : '+'}#{"#{rational c.abs}*" if c.abs != 1}"
end

.constant(c) ⇒ Object



70
71
72
# File 'lib/symbolic/printer.rb', line 70

def constant(c)
  "#{c.name || :unnamed_variable}"
end

.exponent(base, exponent) ⇒ Object



44
45
46
# File 'lib/symbolic/printer.rb', line 44

def exponent(base, exponent)
  "#{brackets base}#{"**#{brackets exponent}" if exponent != 1}"
end

.factors(f) ⇒ Object

Factors



28
29
30
31
32
33
34
35
36
# File 'lib/symbolic/printer.rb', line 28

def factors(f)
  rfactors, factors = f.symbolic.partition { |b,e| e.is_a?(Numeric) && e < 0 }
  rfactors = rfactors.empty? ? nil : [ 1, Hash[*rfactors.flatten] ]
  factors = factors.empty? ? nil : [ f.numeric, Hash[*factors.flatten] ]

  s = (factors ? output(factors) : rational(f.numeric))
  s << "/#{reversed_output rfactors}" if rfactors
  s
end

.functionwrapper(f) ⇒ Object

Function



66
67
68
# File 'lib/symbolic/printer.rb', line 66

def functionwrapper(f)
  "#{f.name}(#{f.argument})"
end

.output(f) ⇒ Object

This has to change ! can be inline in ::factors, but needed also in reversed_output



37
38
39
# File 'lib/symbolic/printer.rb', line 37

def output(f) # This has to change ! can be inline in ::factors, but needed also in reversed_output
  coef(f[0]) << f[1].map {|b,e| exponent b,e }.join('*')
end


8
9
10
# File 'lib/symbolic/printer.rb', line 8

def print(o)
  send(o.class.simple_name.downcase, o)
end

.rational(r) ⇒ Object



16
17
18
# File 'lib/symbolic/printer.rb', line 16

def rational(r)
  "#{r.round == r ? r.to_i : r.to_f}"
end

.remainder(n) ⇒ Object



56
57
58
# File 'lib/symbolic/printer.rb', line 56

def remainder(n)
  "#{'+' if n > 0}#{n unless n.zero?}"
end

.reversed_output(f) ⇒ Object

This has to change ! Please make this non dependent of output ;)



40
41
42
43
# File 'lib/symbolic/printer.rb', line 40

def reversed_output(f) # This has to change ! Please make this non dependent of output ;)
  result = output [f[0], Hash[*f[1].map {|b,e| [b,-e] }.flatten]]
  (f[1].length > 1) ? "(#{result})" : result
end

.sum(s) ⇒ Object

Sums



74
75
76
# File 'lib/symbolic/printer.rb', line 74

def sum(s)
  "Sum(#{s.term}, #{s.index} = #{s.lb}..#{s.ub})"
end

.summands(s) ⇒ Object

Summands



49
50
51
52
53
54
# File 'lib/symbolic/printer.rb', line 49

def summands(s)
  out = s.symbolic.map { |base, coef| coef_with_sign(coef) + brackets(base) }
  out << remainder(s.numeric)
  out[0].sub!(/^\+/, '')
  out.join
end

.variable(v) ⇒ Object

Variable



61
62
63
# File 'lib/symbolic/printer.rb', line 61

def variable(v)
  "#{v.name || :unnamed_variable}"
end