Class: Keisan::AST::UnaryMinus

Inherits:
UnaryOperator show all
Defined in:
lib/keisan/ast/unary_minus.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

#children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UnaryOperator

arity, associativity, #child, #initialize, priority, #to_s

Methods inherited from Operator

#arity, arity, #associativity, associativity, associativity_of_priority, #blank_value, #evaluate_assignments, #initialize, priority, #priority, #symbol, #to_s

Methods inherited from Parent

#==, #deep_dup, #freeze, #initialize, #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, #well_defined?, #|, #~

Constructor Details

This class inherits a constructor from Keisan::AST::UnaryOperator

Class Method Details

.symbolObject



12
13
14
# File 'lib/keisan/ast/unary_minus.rb', line 12

def self.symbol
  :"-"
end

Instance Method Details

#differentiate(variable, context = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/keisan/ast/unary_minus.rb', line 30

def differentiate(variable, context = nil)
  context ||= Context.new
  Times.new([
    -1.to_node,
    child.differentiate(variable, context)
  ]).simplify(context)
end

#evaluate(context = nil) ⇒ Object



8
9
10
# File 'lib/keisan/ast/unary_minus.rb', line 8

def evaluate(context = nil)
  -child.evaluate(context)
end

#simplify(context = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/keisan/ast/unary_minus.rb', line 16

def simplify(context = nil)
  context ||= Context.new

  case child
  when Number
    Number.new(-child.value(context)).simplify(context)
  else
    Times.new([
      Number.new(-1),
      child
    ]).simplify(context)
  end
end

#value(context = nil) ⇒ Object



4
5
6
# File 'lib/keisan/ast/unary_minus.rb', line 4

def value(context = nil)
  return -1 * child.value(context)
end