Class: Symbolic::Summands
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, #inspect, #operations
Class Method Details
.factors(factors) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/symbolic/summands.rb', line 17
def factors(factors)
if factors.symbolic.length == 1 && factors.symbolic.values.first == 1
new identity_element, factors.symbolic.keys.first => factors.numeric
else
new identity_element, Factors.new(1, factors.symbolic) => factors.numeric
end
end
|
.identity_element ⇒ Object
9
10
11
|
# File 'lib/symbolic/summands.rb', line 9
def identity_element
0
end
|
.operation ⇒ Object
5
6
7
|
# File 'lib/symbolic/summands.rb', line 5
def operation
'+'
end
|
.simplify(numeric, symbolic) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/symbolic/summands.rb', line 29
def simplify(numeric, symbolic)
if symbolic.empty?
numeric
elsif numeric == identity_element && symbolic.size == 1
symbolic.values.first * symbolic.keys.first
end
end
|
.simplify_expression!(summands) ⇒ Object
25
26
27
|
# File 'lib/symbolic/summands.rb', line 25
def simplify_expression!(summands)
summands[1].delete_if {|base, coef| coef == 0 }
end
|
.summands(summands) ⇒ Object
13
14
15
|
# File 'lib/symbolic/summands.rb', line 13
def summands(summands)
summands
end
|
Instance Method Details
#coef_to_string(coef) ⇒ Object
55
56
57
|
# File 'lib/symbolic/summands.rb', line 55
def coef_to_string(coef)
"#{(coef > 0) ? '+' : '-' }#{ "#{rational_to_string(coef.abs)}*" if coef.abs != 1}"
end
|
#rational_to_string(numeric) ⇒ Object
63
64
65
|
# File 'lib/symbolic/summands.rb', line 63
def rational_to_string(numeric)
((numeric.round == numeric) ? numeric.to_i : numeric.to_f).to_s
end
|
#remainder_to_string(numeric) ⇒ Object
59
60
61
|
# File 'lib/symbolic/summands.rb', line 59
def remainder_to_string(numeric)
"#{'+' if numeric > 0}#{numeric}"
end
|
#reverse ⇒ Object
51
52
53
|
# File 'lib/symbolic/summands.rb', line 51
def reverse
self.class.new -numeric, Hash[*symbolic.map {|k,v| [k,-v]}.flatten]
end
|
#to_s ⇒ Object
44
45
46
47
48
49
|
# File 'lib/symbolic/summands.rb', line 44
def to_s
output = symbolic.map {|base, coef| coef_to_string(coef) + base.to_s }
output << remainder_to_string(numeric) if numeric != 0
output[0].sub!(/^\+/, '')
output.join
end
|
#value ⇒ Object
38
39
40
41
42
|
# File 'lib/symbolic/summands.rb', line 38
def value
if variables.all? &:value
symbolic.inject(numeric) {|value, (base, coef)| value + base.value * coef.value }
end
end
|