-
Notifications
You must be signed in to change notification settings - Fork 10
/
function.rb
103 lines (84 loc) · 2.52 KB
/
function.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module Keisan
module AST
class Function < Parent
attr_reader :name
def initialize(arguments = [], name)
@name = name
super(arguments)
end
def value(context = nil)
context ||= Context.new
function_from_context(context).value(self, context)
end
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
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
def evaluate_assignments(context = nil)
self
end
def function_defined?(context = nil)
context ||= Context.new
context.has_function?(name)
end
def function_from_context(context)
context.function(name)
end
def ==(other)
case other
when Function
name == other.name && super
else
false
end
end
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
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
def to_s
"#{name}(#{children.map(&:to_s).join(',')})"
end
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
# Functions cannot be guaranteed to be constant even if the arguments
# are constants, because there might be randomness involved in the
# outputs.
def is_constant?
false
end
end
end
end