Class: Keisan::AST::UnaryInverse

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

Instance Method Summary collapse

Methods inherited from UnaryOperator

arity, associativity, #child, #initialize, priority

Methods inherited from Operator

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

Methods inherited from Parent

#==, #deep_dup, #freeze, #initialize, #is_constant?, #replace, #traverse, #unbound_functions, #unbound_variables

Methods inherited from Node

#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiated, #equal, #evaluate_assignments, #evaluated, #false?, #is_constant?, #not_equal, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #unbound_variables, #well_defined?, #|, #~

Constructor Details

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

Instance Method Details

#differentiate(variable, context = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/keisan/ast/unary_inverse.rb', line 28

def differentiate(variable, context = nil)
  context ||= Context.new
  Times.new(
    [
      UnaryMinus.new(child.differentiate(variable, context)),
      UnaryInverse.new(
        Exponent.new([
          child.deep_dup, Number.new(2)
        ])
      )
    ]
  ).simplify(context)
end

#evaluate(context = nil) ⇒ Object



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

def evaluate(context = nil)
  1.to_node / child.evaluate(context)
end

#simplify(context = nil) ⇒ Object



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

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

  @children = [child.simplify(context)]
  case child
  when Number
    Number.new(child.value**-1)
  else
    (child ** -1).simplify(context)
  end
end

#to_sObject



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

def to_s
  "(#{child.to_s})**(-1)"
end

#value(context = nil) ⇒ Object



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

def value(context = nil)
  return Rational(1, child.value(context))
end