Class: Keisan::AST::Times
- Inherits:
-
ArithmeticOperator
- Object
- Node
- Parent
- Operator
- ArithmeticOperator
- Keisan::AST::Times
- Defined in:
- lib/keisan/ast/times.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
- #evaluate(context = nil) ⇒ Object
-
#initialize(children = [], parsing_operators = []) ⇒ Times
constructor
A new instance of Times.
- #simplify(context = nil) ⇒ Object
Methods inherited from Operator
#arity, arity, associativity, #associativity, associativity_of_priority, #evaluate_assignments, #priority, priority, #symbol, #to_s, #value
Methods inherited from Parent
#==, #deep_dup, #freeze, #replace, #unbound_functions, #unbound_variables
Methods inherited from Node
#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<=, #>, #>=, #^, #and, #coerce, #deep_dup, #equal, #evaluate_assignments, #evaluated, #false?, #not_equal, #or, #replace, #simplified, #to_cell, #to_node, #true?, #unbound_functions, #unbound_variables, #value, #well_defined?, #|, #~
Constructor Details
#initialize(children = [], parsing_operators = []) ⇒ Times
Returns a new instance of Times.
4 5 6 7 |
# File 'lib/keisan/ast/times.rb', line 4 def initialize(children = [], parsing_operators = []) super convert_divide_to_inverse! end |
Class Method Details
.symbol ⇒ Object
9 10 11 |
# File 'lib/keisan/ast/times.rb', line 9 def self.symbol :* end |
Instance Method Details
#blank_value ⇒ Object
13 14 15 |
# File 'lib/keisan/ast/times.rb', line 13 def blank_value 1 end |
#differentiate(variable, context = nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/keisan/ast/times.rb', line 53 def differentiate(variable, context = nil) # Product rule Plus.new( children.map.with_index do |child,i| Times.new( children.slice(0,i) + [child.differentiate(variable, context)] + children.slice(i+1,children.size) ) end ).simplify(context) end |
#evaluate(context = nil) ⇒ Object
17 18 19 |
# File 'lib/keisan/ast/times.rb', line 17 def evaluate(context = nil) children[1..-1].inject(children.first.evaluate(context)) {|total, child| total * child.evaluate(context)} end |
#simplify(context = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/keisan/ast/times.rb', line 21 def simplify(context = nil) context ||= Context.new super(context) # Commutative, so pull in operands of any `Times` operators @children = children.inject([]) do |new_children, cur_child| case cur_child when Times new_children + cur_child.children else new_children << cur_child end end constants, non_constants = *children.partition {|child| child.is_a?(Number)} constant = constants.inject(Number.new(1), &:*).simplify(context) return Number.new(0) if constant.value(context) == 0 if non_constants.empty? constant else @children = constant.value(context) == 1 ? [] : [constant] @children += non_constants return @children.first.simplify(context) if @children.size == 1 self end end |