Module: Symbolic

Included in:
Constant, Expression, FunctionWrapper, Misc::Sum, Variable
Defined in:
lib/symbolic/printer.rb,
lib/symbolic.rb,
lib/symbolic/misc.rb,
lib/symbolic/coerced.rb,
lib/symbolic/factors.rb,
lib/symbolic/constant.rb,
lib/symbolic/function.rb,
lib/symbolic/summands.rb,
lib/symbolic/variable.rb,
lib/symbolic/expression.rb,
lib/symbolic/statistics.rb

Overview

This class intend to handle the String representation of a Symbolic expression Two formats will be soon supported: standard and LaTeX

Defined Under Namespace

Modules: Constants, Math, Misc Classes: Coerced, Constant, Expression, Factors, Function, FunctionWrapper, Printer, Summands, Variable

Constant Summary collapse

OPERATIONS =
[:+, :-, :*, :/, :**, :-@]

Instance Method Summary collapse

Instance Method Details

#*(var) ⇒ Object



18
19
20
# File 'lib/symbolic.rb', line 18

def *(var)
  Factors.add self, var
end

#**(var) ⇒ Object



26
27
28
# File 'lib/symbolic.rb', line 26

def **(var)
  Factors.power self, var
end

#+(var) ⇒ Object



10
11
12
# File 'lib/symbolic.rb', line 10

def +(var)
  Summands.add self, var
end

#+@Object



2
3
4
# File 'lib/symbolic.rb', line 2

def +@
  self
end

#-(var) ⇒ Object



14
15
16
# File 'lib/symbolic.rb', line 14

def -(var)
  Summands.subtract self, var
end

#-@Object



6
7
8
# File 'lib/symbolic.rb', line 6

def -@
  Factors.add self, -1
end

#/(var) ⇒ Object



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

def /(var)
  Factors.subtract self, var
end

#coerce(numeric) ⇒ Object



30
31
32
# File 'lib/symbolic.rb', line 30

def coerce(numeric)
  [Coerced.new(self), numeric]
end

#factorial(n) ⇒ Object



2
3
4
# File 'lib/symbolic/misc.rb', line 2

def factorial(n)
  (1..n).inject(1){|f,n| f*n}
end

#inspectObject



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

def inspect
  "Symbolic(#{to_s})"
end

#operationsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/symbolic/statistics.rb', line 3

def operations
  formula = to_s
  OPERATIONS.inject({}) { |stats, op|
    stats.merge({
      op => formula.scan(
      case op
      when :-  then /[^(]-/
      when :*  then /[^*]\*[^*]/
      when :-@ then /\(-|^-/
      else /#{Regexp.escape(op.to_s)}/
      end
      ).size
    })
  }
end

#subs(hsh) ⇒ Object

make multiple substitutions using a hash. Ex: (x+y+z).subs(x=>2*y,z=>y**2) results in y**2+3*y



53
54
55
56
57
# File 'lib/symbolic.rb', line 53

def subs(hsh)
  temp = self
  hsh.each{|k,v| temp = temp.subs(k,v)}
  temp
end

#taylor(var, about, numterms = 5) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/symbolic.rb', line 42

def taylor(var, about, numterms=5)
  term = self
  #inject needs initial value to prevent it from eating the first term
  (0..numterms-1).inject(0) do |sum,n|
    to_add = term.subs(var,about) * (var - about) ** n / factorial(n)
    term = term.diff(var) #save a little time by not having to do all the derivites every time
    sum + to_add
  end
end

#to_sObject



34
35
36
# File 'lib/symbolic.rb', line 34

def to_s
  Printer.print(self)
end