-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.py
217 lines (196 loc) · 5.76 KB
/
function.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from abc import ABC, abstractmethod
from math import log
from utils import factorial
class IFunction(ABC):
def __eq__(self, other):
return str(self) == str(other)
@abstractmethod
def optimize(self):
pass
@abstractmethod
def derivative(self):
pass
@abstractmethod
def getVariables(self):
pass
@abstractmethod
def operation(self, coordinate):
pass
class Variable(IFunction):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
def optimize(self):
return self
def derivative(self):
return Constant(1)
def getVariables(self):
return [self]
def operation(self, coordinate):
return coordinate[self.name]
class Constant(IFunction):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
def optimize(self):
return self
def derivative(self):
return Constant(0)
def getVariables(self):
return []
def operation(self, coordinate):
return self.value
class BinaryFunction(IFunction):
def __init__(self, x1, x2, keyWord=''):
self.x1=x1
self.x2=x2
self.keyWord = keyWord
def __str__(self):
return self.keyWord+'('+str(self.x1)+', '+str(self.x2)+')'
def getVariables(self):
vars1 = self.x1.getVariables()
if not self.x2: return vars1
return vars1 + [v for v in self.x2.getVariables() if v not in vars1]
def derivative(self):
if len(self.getVariables()) > 1:
print("Only functions with at most one variable can be derived at this time")
return
return self._derivative()
def _derivative(self):
pass
def operation(self,coords):
if self.x2 == None:
return self._operation(self.x1.operation(coords), None)
return self._operation(self.x1.operation(coords), self.x2.operation(coords))
def _operation(self, x1, x2):
pass
def optimize(self):
pass
class Identity(BinaryFunction):
def __init__(self, x1):
super(Identity, self).__init__(x1, None, keyWord='Id')
def _operation(self, x, y):
return x
def _derivative(self):
return Identity(self.x1.derivative())
def optimize(self):
return self.x1.optimize()
class Addition(BinaryFunction):
def __init__(self, x1, x2):
super(Addition, self).__init__(x1, x2, keyWord='Add')
def _operation(self, x, y):
return x+y
def _derivative(self):
return Addition(self.x1.derivative(),self.x2.derivative())
def optimize(self):
if self.x1==Constant(0):
return self.x2.optimize()
if self.x2==Constant(0):
return self.x1.optimize()
class Subtraction(BinaryFunction):
def __init__(self, x1, x2):
super(Subtraction, self).__init__(x1, x2, keyWord='Sub')
def _operation(self, x, y):
return x-y
def _derivative(self):
return Subtraction(self.x1.derivative(),self.x2.derivative())
def optimize(self):
if self.x1==Constant(0):
return self.x2.optimize()
if self.x2==Constant(0):
return self.x1.optimize()
if self.x2==self.x1:
return Constant(0)
class Multiplication(BinaryFunction):
def __init__(self, x1, x2):
super(Multiplication, self).__init__(x1, x2, keyWord='Mult')
def _operation(self, x, y):
return x*y
def _derivative(self):
return Addition(
Multiplication(self.x1.derivative(), self.x2),
Multiplication(self.x1,self.x2.derivative())
)
def optimize(self):
if self.x1==Constant(0) or self.x2==Constant(0):
return Constant(0)
if self.x2==Constant(1):
return self.x1.optimize()
if self.x1==Constant(1):
return self.x2.optimize()
class Division(BinaryFunction):
def __init__(self, x1, x2):
super(Division, self).__init__(x1, x2, keyWord='Div')
def _operation(self, x, y):
return x/y
def _derivative(self):
# (f'*g - f*g')/(g^2)
return\
Division(
Subtraction(
Multiplication(self.x1.derivative(), self.x2),
Multiplication(self.x1, self.x2.derivative())
),
Power(self.x2, 2)
)
def optimize(self):
if self.x2==Constant(1):
return self.x1.optimize()
if self.x1==Constant(1):
return self.x2.optimize()
if self.x1==self.x2:
return Constant(1)
class Power(BinaryFunction):
def __init__(self, x1, x2):
super(Power, self).__init__(x1, x2, keyWord='Pow')
def _operation(self, x, y):
return x**y
def _derivative(self):
# derivative of f^g = (f^g)*((f'*g)/f + ln(f)*g')
return \
Multiplication(
Power(self.x1, self.x2),
Addition(
Division(Multiplication(self.x1.derivative(), self.x2), self.x1),
Multiplication(Logarithm(self.x1, Constant(2.718)), self.x2.derivative()) # TODO
)
)
def optimize(self):
if self.x2==Constant(1):
return self.x1.optimize()
if (self.x1==Constant(1) and self.x2>=Constant(0)) or self.x2==Constant(0):
return Constant(1)
class Root(BinaryFunction):
def __init__(self, x1, x2):
super(Root, self).__init__(x1, x2, keyWord='Root')
def _operation(self, x, y):
return x**(1/y)
def _derivative(self):
return Power(self.x1, Division(1, self.x2)).derivative()
def optimize(self):
if self.x2==Constant(1):
return self.x1.optimize()
if self.x2==Constant(0):
return Constant(1)
class Logarithm(BinaryFunction):
def __init__(self, x1, x2):
super(Logarithm, self).__init__(x1, x2, keyWord='Log')
def _operation(self, x, y):
return log(x,y)
def _derivative(self):
pass # TODO
def optimize(self):
if self.x1==self.x2:
return Constant(1)
class Factorial(BinaryFunction):
def __init__(self, x1):
super(Factorial, self).__init__(x1, None, keyWord='Fact')
def _operation(self, x, y):
return factorial(x)
def _derivative(self):
pass # TODO
def optimize(self):
if self.x1==Constant(0) or self.x1==Constant(1):
return Constant(1)