Class: Keisan::AST::Function

Inherits:
Parent show all
Defined in:
lib/keisan/ast/function.rb

Instance Attribute Summary collapse

Attributes inherited from Parent

#children

Instance Method Summary collapse

Methods inherited from Parent

#deep_dup, #freeze, #replace, #unbound_variables

Methods inherited from Node

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

Constructor Details

#initialize(arguments = [], name) ⇒ Function

Returns a new instance of Function.



6
7
8
9
# File 'lib/keisan/ast/function.rb', line 6

def initialize(arguments = [], name)
  @name = name
  super(arguments)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/keisan/ast/function.rb', line 39

def ==(other)
  case other
  when Function
    name == other.name && super
  else
    false
  end
end

#differentiate(variable, context = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/keisan/ast/function.rb', line 74

def differentiate(variable, context = nil)
  function = function_from_context(context)
  function.differentiate(self, variable, context)

rescue Exceptions::UndefinedFunctionError, Exceptions::NotImplementedError
  unless unbound_variables(context).include?(variable.name)
    return Number.new(0)
  end

  self.class.new([self, variable], "diff")
end

#evaluate(context = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/keisan/ast/function.rb', line 48

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

  if function_defined?(context)
    function_from_context(context).evaluate(self, context)
  else
    @children = children.map {|child| child.evaluate(context).to_node}
    self
  end
end

#evaluate_assignments(context = nil) ⇒ Object



26
27
28
# File 'lib/keisan/ast/function.rb', line 26

def evaluate_assignments(context = nil)
  self
end

#function_defined?(context = nil) ⇒ Boolean

Returns:



30
31
32
33
# File 'lib/keisan/ast/function.rb', line 30

def function_defined?(context = nil)
  context ||= Context.new
  context.has_function?(name)
end

#function_from_context(context) ⇒ Object



35
36
37
# File 'lib/keisan/ast/function.rb', line 35

def function_from_context(context)
  context.function(name)
end

#simplify(context = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/keisan/ast/function.rb', line 59

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

  if function_defined?(context)
    function_from_context(context).simplify(self, context)
  else
    @children = children.map {|child| child.simplify(context).to_node}
    self
  end
end

#to_sObject



70
71
72
# File 'lib/keisan/ast/function.rb', line 70

def to_s
  "#{name}(#{children.map(&:to_s).join(',')})"
end

#unbound_functions(context = nil) ⇒ Object



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

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

  functions = children.inject(Set.new) do |res, child|
    res | child.unbound_functions(context)
  end

  context.has_function?(name) ? functions : functions | Set.new([name])
end

#value(context = nil) ⇒ Object



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

def value(context = nil)
  context ||= Context.new
  function_from_context(context).value(self, context)
end