-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.py
319 lines (273 loc) · 8.82 KB
/
expr.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
import ast
from fractions import Fraction as Frac
# Transform the `ast` module's nodes into redefined nodes
def trans_ast(node):
if isinstance(node, ast.BinOp):
return BinOp(trans_ast(node.left), node.op, trans_ast(node.right))
elif isinstance(node, ast.UnaryOp):
return UnaryOp(node.op, trans_ast(node.operand))
elif isinstance(node, ast.Num):
return Num(node.n)
elif isinstance(node, ast.Name):
return Name(node.id)
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
return Call(node.func.id, trans_ast(node.args[0]))
else:
return Undefined()
def sqr(u):
return u ** Num(2)
def sqrt(u):
return u ** Num(Frac(1, 2))
def make_func(id):
return lambda x: Call(id, x)
# Make AST node shorthand constructors
for fn in ['ln', 'sin', 'cos', 'tan', 'csc', 'sec', 'cot']:
globals()[fn] = make_func(fn)
class Expr(ast.Expr):
def __add__(self, other):
return BinOp(self, ast.Add(), other)
def __sub__(self, other):
return BinOp(self, ast.Sub(), other)
def __mul__(self, other):
return BinOp(self, ast.Mult(), other)
def __truediv__(self, other):
return BinOp(self, ast.Div(), other)
def __pow__(self, other):
return BinOp(self, ast.Pow(), other)
def __neg__(self):
return UnaryOp(ast.USub(), self)
def simpl(self):
return self
class Undefined(Expr):
def __str__(self, parent_prec=0):
return 'undefined'
def deriv(self):
return Undefined()
class BinOp(Expr):
def __init__(self, f, op, g):
self.f = f
self.g = g
self.op = op
def __str__(self, parent_prec=0):
ops = {
ast.Add: ('+', 1),
ast.Sub: ('-', 1),
ast.Mult: ('*', 2),
ast.Div: ('/', 2),
ast.Pow: ('^', 3),
}
op_s, prec = ops[self.op.__class__]
f_s = self.f.__str__(prec)
g_s = self.g.__str__(prec)
if op_s == '^':
s = f'{f_s}^{g_s}'
else:
s = f'{f_s} {op_s} {g_s}'
if prec < parent_prec:
s = f'({s})'
return s
def deriv(self):
op = self.op
f = self.f
g = self.g
df = f.deriv()
dg = g.deriv()
if isinstance(op, (ast.Add, ast.Sub)):
return BinOp(df, op, dg)
elif isinstance(op, ast.Mult):
return f * dg + g * df
elif isinstance(op, ast.Div):
return (df * g - dg * f) / sqr(g)
elif isinstance(op, (ast.Pow, ast.BitXor)):
return f ** (g - Num(1)) * (g * df + f * ln(f) * dg)
else:
return Undefined()
def simpl(self):
op = self.op
f = self.f.simpl()
g = self.g.simpl()
if isinstance(f, Undefined) or isinstance(g, Undefined):
return Undefined()
f_is_num = isinstance(f, Num)
g_is_num = isinstance(g, Num)
if isinstance(op, (ast.Add, ast.Sub)):
if f_is_num and g_is_num:
if isinstance(op, ast.Add):
return Num(f.val + g.val)
else:
return Num(f.val - g.val)
elif f_is_num and f.val == 0:
return g
elif g_is_num and g.val == 0:
return f
else:
return BinOp(f, op, g)
elif isinstance(op, ast.Mult):
if f_is_num and g_is_num:
return Num(f.val * g.val)
elif f_is_num and f.val == 1:
return g
elif g_is_num and g.val == 1:
return f
elif f_is_num and f.val == 0 or g_is_num and g.val == 0:
return Num(0)
else:
return f * g
elif isinstance(op, ast.Div):
if f_is_num and g_is_num:
return Num(Frac(f.val, g.val))
elif g_is_num and g.val == 1:
return f
else:
return f / g
elif isinstance(op, (ast.Pow, ast.BitXor)):
if f_is_num and g_is_num:
return Num(f.val ** g.val)
elif f_is_num and f.val == 0:
return Num(0)
elif g_is_num and g.val == 1:
return f
elif f_is_num and f.val == 1 or g_is_num and g.val == 0:
return Num(1)
elif g_is_num and g.val < 0:
return Num(1) / f ** Num(-g.val)
else:
return f ** g
else:
return Undefined
class UnaryOp(Expr):
def __init__(self, op, operand):
self.op = op
self.operand = operand
def __str__(self, parent_prec=0):
ops = {
ast.UAdd: '+',
ast.USub: '-',
}
op_s = ops[self.op.__class__]
if isinstance(self.operand, BinOp):
return f'{op_s}({self.operand})'
else:
return f'{op_s}{self.operand}'
def deriv(self):
if isinstance(self.op, ast.UAdd):
return self.operand.deriv()
elif isinstance(self.op, ast.USub):
return -self.operand.deriv()
else:
return Undefined()
def simpl(self):
op = self.op
operand = self.operand.simpl()
if isinstance(operand, Undefined):
return Undefined()
if isinstance(op, ast.UAdd):
return operand
elif isinstance(op, ast.USub):
if isinstance(operand, Num):
return Num(-operand.val)
else:
return -operand
else:
return Undefined()
class Num(Expr):
def __init__(self, val):
self.val = val
def __str__(self, parent_prec=0):
return str(self.val)
def deriv(self):
return Num(0)
class Name(Expr):
def __init__(self, id):
self.id = id
def __str__(self, parent_prec=0):
return self.id
def deriv(self):
if self.id == 'x':
return Num(1)
else:
return Num(0)
class Call(Expr):
def __init__(self, id, arg):
self.id = id
self.arg = arg
def __str__(self, parent_prec=0):
return f'{self.id}({self.arg})'
def deriv(self):
u = self.arg
du = u.deriv()
if self.id == 'ln':
return Num(1) / u * du
elif self.id == 'sqrt':
return Num(Frac(1, 2)) * u ** Num(-Frac(1, 2)) * du
elif self.id == 'sin':
return cos(u) * du
elif self.id == 'cos':
return -sin(u) * du
elif self.id == 'tan':
return sqr(sec(u)) * du
elif self.id == 'cot':
return -sqr(csc(u)) * du
elif self.id == 'sec':
return sec(u) * tan(u) * du
elif self.id == 'csc':
return -(csc(u) * cot(u)) * du
elif self.id in ['arcsin', 'asin']:
return (Num(1) / sqrt(Num(1) - sqr(u))) * du
elif self.id in ['arccos', 'acos']:
return -(Num(1) / sqrt(Num(1) - sqr(u))) * du
elif self.id in ['arctan', 'atan']:
return (Num(1) / (u ** Num(2) + Num(1))) * du
elif self.id in ['arccsc', 'acsc']:
return -(Num(1) / (abs(u) * sqrt(sqr(u) - 1))) * du
elif self.id in ['arcsec', 'asec']:
return (Num(1) / (abs(u) * sqrt(sqr(u) - 1))) * du
elif self.id in ['arccot', 'acot']:
return -(Num(1) / (u ** Num(2) + Num(1))) * du
else:
return Undefined()
def simpl(self):
u = self.arg.simpl()
if isinstance(u, Undefined):
return Undefined()
u_is_num = isinstance(u, Num)
if self.id == 'ln':
if u_is_num and u.val <= 0:
return Undefined()
elif u_is_num and u.val == 1:
return Num(0)
else:
return ln(u)
elif self.id == 'sqrt':
if u_is_num and u.val < 0:
return Undefined()
else:
return sqrt(u)
elif self.id == 'sin':
return sin(u)
elif self.id == 'cos':
return cos(u)
elif self.id == 'tan':
return tan(u)
elif self.id == 'cot':
return cot(u)
elif self.id == 'sec':
return sec(u)
elif self.id == 'csc':
return csc(u)
elif self.id in ['arcsin', 'asin']:
return asin(u)
elif self.id in ['arccos', 'acos']:
return acos(u)
elif self.id in ['arctan', 'atan']:
return atan(u)
elif self.id in ['arccsc', 'acsc']:
return acsc(u)
elif self.id in ['arcsec', 'asec']:
return asec(u)
elif self.id in ['arccot', 'acot']:
return acot(u)
else:
return Undefined()
def parse(s):
return trans_ast(ast.parse(s.replace('^', '**')).body[0].value)