Class: Keisan::AST::Exponent
- Inherits:
-
ArithmeticOperator
- Object
- Node
- Parent
- Operator
- ArithmeticOperator
- Keisan::AST::Exponent
- Defined in:
- lib/keisan/ast/exponent.rb
Constant Summary
Constants inherited from Operator
Operator::ARITIES, Operator::ARITY_PRIORITY_ASSOCIATIVITY, Operator::ASSOCIATIVITIES, Operator::ASSOCIATIVITY_OF_PRIORITY, Operator::PRIORITIES
Instance Attribute Summary
Attributes inherited from Parent
Class Method Summary collapse
Instance Method Summary collapse
- #blank_value ⇒ Object
-
#differentiate(variable, context = nil) ⇒ Object
d ( a^b ) = d ( exp(b log(a)) ) = a^b * d (b log(a)) = a^b * [ db * log(a) + da * b/a] = da b a^(b-1) + db log(a) a^b.
- #evaluate(context = nil) ⇒ Object
- #simplify(context = nil) ⇒ Object
Methods inherited from Operator
#arity, arity, #associativity, associativity, associativity_of_priority, #evaluate_assignments, #initialize, priority, #priority, #symbol, #to_s, #value
Methods inherited from Parent
#==, #deep_dup, #freeze, #initialize, #replace, #traverse, #unbound_functions, #unbound_variables
Methods inherited from Node
#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiated, #equal, #evaluate_assignments, #evaluated, #false?, #not_equal, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #value, #well_defined?, #|, #~
Constructor Details
This class inherits a constructor from Keisan::AST::Operator
Class Method Details
.symbol ⇒ Object
4 5 6 |
# File 'lib/keisan/ast/exponent.rb', line 4 def self.symbol :** end |
Instance Method Details
#blank_value ⇒ Object
8 9 10 |
# File 'lib/keisan/ast/exponent.rb', line 8 def blank_value 1 end |
#differentiate(variable, context = nil) ⇒ Object
d ( a^b ) = d ( exp(b log(a)) ) = a^b * d (b log(a))
a^b * [ db * log(a) + da * b/a] = da b a^(b-1) + db log(a) a^b
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/keisan/ast/exponent.rb', line 48 def differentiate(variable, context = nil) context ||= Context.new # Reduce to binary form unless children.count == 2 return simplify(context).differentiate(variable, context) end base = children[0].simplified(context) exponent = children[1].simplified(context) # Special simple case where exponent is a pure number if exponent.is_a?(Number) return (exponent * base.differentiate(variable, context) * base ** (exponent -1)).simplify(context) end base_diff = base.differentiate(variable, context) exponent_diff = exponent.differentiate(variable, context) res = base ** exponent * ( exponent_diff * Function.new([base], "log") + base_diff * exponent / base ) res.simplify(context) end |
#evaluate(context = nil) ⇒ Object
42 43 44 |
# File 'lib/keisan/ast/exponent.rb', line 42 def evaluate(context = nil) children.reverse[1..-1].inject(children.last.evaluate(context)) {|total, child| child.evaluate(context) ** total} end |
#simplify(context = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/keisan/ast/exponent.rb', line 12 def simplify(context = nil) context ||= Context.new super(context) # Reduce to basic binary exponents reduced = children.reverse[1..-1].inject(children.last) do |total, child| child ** total end unless reduced.is_a?(Exponent) return reduced.simplify(context) end if reduced.children.count != 2 raise Exceptions::InternalError.new("Exponent should be binary") end @children = reduced.children if children[1].is_a?(Number) && children[1].value(context) == 1 return children[0] end if children.all? {|child| child.is_a?(Number)} (children[0] ** children[1]).simplify(context) else self end end |