-
Notifications
You must be signed in to change notification settings - Fork 0
/
p21_Exp.py
461 lines (334 loc) · 10.6 KB
/
p21_Exp.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import math
class Exp:
def deriv(self, x):
pass
def eval(self, env:dict):
pass
def simplify(self):
return self
def get_vars(self):
pass
def __add__(self, other):
other = to_exp(other)
return Add(self, other).simplify()
def __radd__(self, other):
other = to_exp(other)
return Add(other, self).simplify()
def __sub__(self, other):
other = to_exp(other)
return Sub(self, other).simplify()
def __rsub__(self, other):
other = to_exp(other)
return Sub(other, self).simplify()
def __mul__(self, other):
other = to_exp(other)
return Mul(self, other).simplify()
def __rmul__(self, other):
other = to_exp(other)
return Mul(other, self).simplify()
def __neg__(self):
return Neg(self).simplify()
def __truediv__(self, other):
other = to_exp(other)
return Truediv(self, other).simplify()
def __rtruediv__(self, other):
other = to_exp(other)
return Truediv(other, self).simplify()
def __pow__(self, power, modulo=None):
power = to_exp(power)
return Pow(self, power).simplify()
def __rpow__(self, other):
other = to_exp(other)
return Pow(other, self).simplify()
def __abs__(self):
return Abs(self).simplify()
class Const(Exp):
def __init__(self, value):
self.value = value
def deriv(self, x):
return Const(0)
def eval(self, env:dict):
return self.value
def __repr__(self):
return str(self.value)
def get_vars(self):
return {}
class Variable(Exp):
def __init__(self, name):
self.name = name
def deriv(self, x):
return Const(1) if self is x else Const(0)
def eval(self, env:dict):
if self in env:
return env[self]
raise Exception('Variable %s is not found.' % self.name)
def __repr__(self):
return str(self.name)
def get_vars(self):
return {self}
def to_exp(v):
if isinstance(v, Exp):
return v
return Const(v)
class Binary(Exp):
def __init__(self, left, right):
self.left = left
self.right = right
def __repr__(self):
return '(%s %s %s)' % (self.left, self.get_op(), self.right)
def get_op(self):
raise Exception('get_op() is not defined')
def get_vars(self):
return self.left.get_vars().union(self.right.get_vars())
class Add(Binary):
def deriv(self, x):
return self.left.deriv(x) + self.right.deriv(x)
def eval(self, env):
return self.left.eval(env) + self.right.eval(env)
def get_op(self):
return '+'
def simplify(self):
if isinstance(self.left, Const):
if self.left.value == 0:
return self.right
if isinstance(self.right, Const):
return Const(self.left.value + self.right.value)
elif isinstance(self.right, Const) and self.right.value == 0:
return self.left
return self
class Sub(Binary):
def deriv(self, x):
return self.left.deriv(x) - self.right.deriv(x)
def eval(self, env):
return self.left.eval(env) - self.right.eval(env)
def get_op(self):
return '-'
def simplify(self):
if isinstance(self.left, Const):
if self.left.value == 0:
return -self.right
if isinstance(self.right, Const):
return Const(self.left.value - self.right.value)
elif isinstance(self.right, Const) and self.right.value == 0:
return self.left
return self
class Mul(Binary):
def deriv(self, x):
# (uv)' = uv' + u'v
u = self.left
v = self.right
return u * v.deriv(x) + u.deriv(x) * v
def eval(self, env):
return self.left.eval(env) * self.right.eval(env)
def get_op(self):
return '*'
def simplify(self):
if isinstance(self.left, Const):
if self.left.value == 0:
return Const(0)
if self.left.value == 1:
return self.right
if isinstance(self.right, Const):
return Const(self.left.value * self.right.value)
elif isinstance(self.right, Const):
if self.right.value == 0:
return Const(0)
if self.right.value == 1:
return self.left
return self
class Unary(Exp):
def __init__(self, value):
self.value = value
def get_vars(self):
return self.value.get_vars()
def __repr__(self):
return self.get_pattern() % (self.value)
def get_pattern(self):
pass
def simplify(self):
return self.get_value(self.value.value) if isinstance(self.value, Const) else self
def get_value(self, python_value):
pass
class Neg(Unary):
def get_pattern(self):
pass
def deriv(self, x):
return - self.exp.deriv(x)
def eval(self, env):
return - self.exp.eval(env)
def get_vars(self):
return self.exp.get_vars()
class Truediv(Binary):
def deriv(self, x):
# (u/v)' = (u'v - uv') / v**2
u = self.left
v = self.right
return (u.deriv(x) * v - u * v.deriv(x)) / v**2
def eval(self, env):
return self.left.eval(env) / self.right.eval(env)
def get_op(self):
return '/'
def simplify(self):
if isinstance(self.left, Const):
if self.left.value == 0:
return Const(0)
if isinstance(self.right, Const):
return Const(self.left.value / self.right.value)
elif isinstance(self.right, Const) and self.right.value == 1:
return self.left
return self
class Pow(Binary):
def deriv(self, x):
# (u**v)' = (v’ln(u) + v u’/u) * y
u = self.left
v = self.right
return (v.deriv(x) * log(u) + v * u.deriv(x)/u) * self
def eval(self, env):
return self.left.eval(env) ** self.right.eval(env)
def get_op(self):
return '**'
def simplify(self):
if isinstance(self.left, Const):
if self.left.value in (0, 1):
return Const(self.left.value)
if isinstance(self.right, Const):
return Const(self.left.value ** self.right.value)
elif isinstance(self.right, Const):
if self.right.value == 0:
return Const(1)
if self.right.value == 1:
return self.left
return self
class Abs(Unary):
def get_pattern(self):
return "|%s|"
def eval(self, env):
return abs(self.value.eval(env))
def get_value(self, python_value):
return abs(python_value)
def deriv(self, x):
d = self.value.deriv(x)
return cond(d, -d, self.value > 0)
def cond(true_value, false_value, cond): # cond
true_value = to_exp(true_value)
false_value = to_exp(false_value)
cond = to_exp(cond)
return Cond(true_value, false_value, cond).simplify()
class Cond(Exp):
def __init__(self, true_value, false_value, condition):
self.true_value = true_value
self.false_value = false_value
self.conditon = condition
def __repr__(self):
return "(%s if %s else %s)" % (self.true_value, self.condition, self.false_value)
def eval(self, env:dict):
c = self.conditon.eval(env)
if c:
return self.true_value.eval(env)
else:
return self.false_value.eval(env)
def simplify(self):
c = self.conditon
if isinstance(c, Const):
return self.true_value.simplify() if c.value else self.false_value.simplify()
return self
def get_vars(self):
return self.true_value.get_vars().union(self.false_value.get_vars())
def deriv(self, x):
return Cond(self.true_value.deriv(x), self.false_value.deriv(x), self.conditon)
e = Const(math.e)
pi = Const(math.pi)
one = Const(1)
zero = Const(0)
def log(value, base=e):
value = to_exp(value)
base = to_exp(base)
return Log(value, base).simplify()
class Log(Exp):
def __init__(self, value, base=e):
self.value = value
self.base = base
def __repr__(self):
return 'log(%s)' % (self.value if self.base is e else ('%s, %s' % (self.value, self.base)))
def simplify(self):
if isinstance(self.value, Const):
if self.value.value == 1:
return zero
if isinstance(self.base, Const):
return math.log(self.value.value, self.base.value)
return self
def eval(self, env):
return math.log(self.value.eval(env), self.base.eval(env))
def deriv(self, x):
# (log(u, v))' = (ln(u) / ln(v))' = (u'ln(v)/u - ln(u)v'/v)/ln(v)**2
u = self.value
v = self.base
return (u.deriv(x) * log(v)/u - log(u) * v.deriv(x)/v)/log(v)**2
def get_vars(self):
return self.value.get_vars().union(self.base.get_vars())
def sin(value):
value = to_exp(value)
return Sin(value).simplify()
class Sin(Exp):
def get_pattern(self):
return "sin(%s)"
def __repr__(self):
return 'sin(%s)' % self.value
def eval(self, env):
return math.sin(self.value.eval(env))
def simplify(self):
return math.sin(self.value.value) if isinstance(self.value, Const) else self
def deriv(self, x):
return cos(self.value) * self.value.deriv(x)
def get_vars(self):
return self.value.get_vars()
def cos(value):
value = to_exp(value)
return Cos(value).simplify()
class Cos(Unary):
def get_pattern(self):
return "cos(%s)"
def eval(self, env):
return math.cos(self.value.eval(env))
def simplify(self):
return math.cos(self.value.value) if isinstance(self.value, Const) else self
def deriv(self, x):
return -sin(self.value) * self.value.deriv(x)
def get_vars(self):
return self.value.get_vars()
c1 = Const(123)
print(c1)
print(c1.deriv('x'))
print(c1.eval(None))
x = Variable('x')
print(x)
print(x.eval({x:12345}))
print(x.deriv(x))
e1 = x + c1
print(e1, '=', e1.eval({x:456}))
e2 = e1.deriv(x)
print(e2, '=', e2.eval({x:100}))
y = Variable('y')
e2 = e1.deriv(y)
print(e2, '=', e2.eval({x:100, y:200}))
e3 = c1 + 400 - x
print(e3)
print(e3.deriv(x))
e4 = c1 * x + 20 * x
print(e4)
print(e4.deriv(x))
e5 = c1 * x / 20
print(e5)
print(e5.deriv(x))
e6 = 3 * x**2 - 12 * x + pi
print(e6)
print(e6.deriv(x))
e6 = x**5 + log(x**3)
print(e6)
print(e6.deriv(x))
y = Variable('y')
e7 = sin(x**2) + cos(y)**2 + sin(x*y)
print(e7)
print(e7.deriv(x))
print(e7.deriv(y))
print(e7.get_vars())