Class: Keisan::AST::Function
Instance Attribute Summary collapse
Attributes inherited from Parent
#children
Instance Method Summary
collapse
Methods inherited from Parent
#deep_dup, #freeze, #replace, #traverse
Methods inherited from Node
#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiated, #equal, #evaluated, #false?, #not_equal, #or, #replace, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #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
#name ⇒ Object
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
48
49
50
51
52
53
54
55
|
# File 'lib/keisan/ast/function.rb', line 48
def ==(other)
case other
when Function
name == other.name && super
else
false
end
end
|
#differentiate(variable, context = nil) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/keisan/ast/function.rb', line 83
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
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/keisan/ast/function.rb', line 57
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
35
36
37
|
# File 'lib/keisan/ast/function.rb', line 35
def evaluate_assignments(context = nil)
self
end
|
#function_defined?(context = nil) ⇒ Boolean
39
40
41
42
|
# File 'lib/keisan/ast/function.rb', line 39
def function_defined?(context = nil)
context ||= Context.new
context.has_function?(name)
end
|
#function_from_context(context) ⇒ Object
44
45
46
|
# File 'lib/keisan/ast/function.rb', line 44
def function_from_context(context)
context.function(name)
end
|
#simplify(context = nil) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/keisan/ast/function.rb', line 68
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_s ⇒ Object
79
80
81
|
# File 'lib/keisan/ast/function.rb', line 79
def to_s
"#{name}(#{children.map(&:to_s).join(',')})"
end
|
#unbound_functions(context = nil) ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/keisan/ast/function.rb', line 25
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
|
#unbound_variables(context = nil) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/keisan/ast/function.rb', line 16
def unbound_variables(context = nil)
context ||= Context.new
if context.has_function?(name)
function_from_context(context).unbound_variables(children, context)
else
super
end
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
|