Class: Symbolic::Summands

Inherits:
Expression show all
Defined in:
lib/symbolic/summands.rb

Constant Summary collapse

OPERATION =
:+
IDENTITY =
0

Constants included from Symbolic

OPERATIONS

Instance Attribute Summary

Attributes inherited from Expression

#numeric, #symbolic

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#==, add, convert, #initialize, numeric, one, simple?, subtract, unite, unite_numeric, unite_symbolic, #variables

Methods included from Symbolic

#*, #**, #+, #+@, #-, #-@, #/, #coerce, #factorial, #inspect, #operations, #taylor, #to_s

Constructor Details

This class inherits a constructor from Symbolic::Expression

Class Method Details

.factors(factors) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/symbolic/summands.rb', line 13

def factors(factors)
  if factors.symbolic.length == 1 && factors.symbolic.first[1] == Factors::IDENTITY
    new IDENTITY, factors.symbolic.first[0] => factors.numeric
  else
    new IDENTITY, Factors.new(1, factors.symbolic) => factors.numeric
  end
end

.simplify(numeric, symbolic) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/symbolic/summands.rb', line 25

def simplify(numeric, symbolic)
  if symbolic.empty? #only the numeric portion
    numeric
  elsif numeric == IDENTITY && symbolic.size == 1 #no numeric to add and only one symbolic, so can just return the one base*coefficient
    symbolic.first[1] * symbolic.first[0]
  elsif symbolic.size > 1 #let's look to see if any base gets repeated, so that they can be combined
    temp = []
    symbolic.each_key do |base1|
      temp = symbolic.find_all{|base2,v2| base1 == base2} #temp is an array of form [[b,c1],[b,c2]...]
      break if temp.size > 1 #found a duplicate base
    end
    if temp.size > 1
      repeated_base = temp[0][0]
      new_coef = temp.inject(0){|sum, (b,coeff)| sum + coeff} #sum up the old coefficients
      #it could be that there is more than one repeated base, but the next line effectively is recursion, and it'll take care of that
      Summands.new(numeric, symbolic.reject{|k,v| k == repeated_base}) + new_coef * repeated_base
    else
      nil
    end
  end
end

.simplify_expression!(summands) ⇒ Object



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

def simplify_expression!(summands)
  summands[1].delete_if {|base, coef| coef == 0 }
end

.summands(summands) ⇒ Object



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

def summands(summands)
  summands
end

Instance Method Details

#diff(wrt) ⇒ Object



68
69
70
# File 'lib/symbolic/summands.rb', line 68

def diff(wrt)
  @symbolic.inject(0){|m,(base,coefficient)| m + coefficient * base.diff(wrt)}
end

#reverseObject



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

def reverse
  self.class.new( -numeric, Hash[*symbolic.map {|k,v| [k,-v]}.flatten] )
end

#subs(to_replace, replacement = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/symbolic/summands.rb', line 60

def subs(to_replace, replacement=nil)
  if replacement == nil and to_replace.is_a?(Hash)
	super(to_replace)
  else
	@symbolic.inject(@numeric){|m,(base,coefficient)| m + coefficient * base.subs(to_replace, replacement)}
  end
end

#valueObject



48
49
50
51
52
# File 'lib/symbolic/summands.rb', line 48

def value
  if variables.all?(&:value)
    symbolic.inject(numeric) {|value, (base, coef)| value + base.value * coef.value }
  end
end