-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderiv.py
More file actions
230 lines (200 loc) · 8.37 KB
/
deriv.py
File metadata and controls
230 lines (200 loc) · 8.37 KB
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
from AST import *
def identityRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Var) and expr.expr == expr.sym:
return Num(1)
return expr
def constantRule(expr):
if isinstance(expr, Deriv):
if not expr.expr.contains(expr.sym):
return Num(0)
return expr
def negationRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Neg):
return Mul([Num(-1), takeDeriv(Deriv(expr.expr.exp, expr.sym))])
return expr
def constMultRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Mul):
consts = []
notconsts = []
for e in expr.expr.factors:
if isinstance(e, Num) or isinstance(e, Var) and e != expr.sym: #TODO check for containment of variable
consts.append(e)
else:
notconsts.append(e)
if notconsts == []:
return Num(0)
if consts == []:
return expr
c = consts[0] if len(consts) == 1 else Mul(consts)
nc = notconsts[0] if len(notconsts) == 1 else Mul(notconsts)
return Mul([c, takeDeriv(Deriv(nc, expr.sym))])
return expr
def sumRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Add):
return Add([takeDeriv(Deriv(c, expr.sym)) for c in expr.expr.terms])
return expr
def differenceRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Sub):
return Sub(takeDeriv(Deriv(expr.expr.left, expr.sym)), takeDeriv(Deriv(expr.expr.right, expr.sym)))
return expr
def powerRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Pow) \
and isinstance(expr.expr.base, Var) and expr.expr.base == expr.sym \
and not expr.expr.exp.contains(expr.sym):
return Mul([expr.expr.exp, Pow(expr.expr.base, Sub(expr.expr.exp, Num(1)))])
return expr
def productRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Mul):
if len(expr.expr.factors) == 2:
return Add([Mul([expr.expr.factors[0], takeDeriv(Deriv(expr.expr.factors[1], expr.sym))]),
Mul([expr.expr.factors[1], takeDeriv(Deriv(expr.expr.factors[0], expr.sym))])])
else:
return Add([Mul([expr.expr.factors[0], takeDeriv(Deriv(Mul(expr.expr.factors[1:]), expr.sym))])
,Mul([Mul(expr.expr.factors[1:]), takeDeriv(Deriv(expr.expr.factors[0], expr.sym))])])
return expr
def quotientRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Div):
return Div(Sub(Mul([takeDeriv(Deriv(expr.expr.top, expr.sym)), expr.expr.bottom]),
Mul([takeDeriv(Deriv(expr.expr.bottom, expr.sym)), expr.expr.top])),
Pow(expr.expr.bottom, Num(2)))
return expr
def exponentRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Pow) and expr.expr.exp.contains(expr.sym)\
and not expr.expr.base.contains(expr.sym):
return Mul([Pow(expr.expr.base, expr.expr.exp), Apply(Fun("ln"), expr.expr.base)])
return expr
def funExponentRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Pow) and expr.expr.exp.contains(expr.sym)\
and expr.expr.base.contains(expr.sym):
return Mul([expr.expr,
Add([Div(Mul([expr.expr.exp, takeDeriv(Deriv(expr.expr.base, expr.sym))]),
expr.expr.base),
Mul([Apply(Fun("ln"), expr.expr.base),
takeDeriv(Deriv(expr.expr.exp, expr.sym))])])])
return expr
def sinRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("sin")\
and expr.expr.expr == expr.sym:
return Apply(Fun("cos"), expr.expr.expr)
return expr
def cosRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("cos")\
and expr.expr.expr == expr.sym:
return Mul([Apply(Fun("sin"), expr.expr.expr), Num(-1)])
return expr
def tanRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("tan")\
and expr.expr.expr == expr.sym:
return Pow(Apply(Fun("sec"), expr.expr.expr), Num(2))
return expr
def secRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("sec")\
and expr.expr.expr == expr.sym:
return Mul([Apply(Fun("sec"), expr.expr.expr), Apply(Fun("tan"), expr.expr.expr)])
return expr
def cscRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("csc")\
and expr.expr.expr == expr.sym:
return Mul([Num(-1), Mul([Apply(Fun("csc"), expr.expr.expr), Apply(Fun("cot"), expr.expr.expr)])])
return expr
def cotRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("cot")\
and expr.expr.expr == expr.sym:
return Mul([Num(-1), Pow(Apply(Fun("csc"), expr.expr.expr), Num(2))])
return expr
def arcsinRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("arcsin")\
and expr.expr.expr == expr.sym:
return Div(Num(1), Pow(Sub(Num(1), Pow(expr.expr.expr, Num(2))), Div(Num(1),Num(2))))
return expr
def chainRule(expr):
if isinstance(expr, Deriv) and isinstance(expr.expr, Apply) and expr.expr.expr != expr.sym:
return Mul([takeDeriv(Deriv(Apply(expr.expr.fun, expr.sym), expr.sym)).sub(expr.sym, expr.expr.expr),\
takeDeriv(Deriv(expr.expr.expr, expr.sym))])
return expr
def arccosRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("arccos")\
and expr.expr.expr == expr.sym:
return Div(Num(-1), Pow(Sub(Num(1), Pow(expr.expr.expr, Num(2))), Div(Num(1),Num(2))))
return expr
def arctanRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("arctan")\
and expr.expr.expr == expr.sym:
return Div(Num(1), Add([Num(1), Pow(expr.expr.expr, Num(2))]))
return expr
def arcsecRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("arcsec")\
and expr.expr.expr == expr.sym:
return Div(Num(1), Mul([Apply(Fun("abs"), expr.expr.expr),\
(Pow(Sub(Pow(expr.expr.expr, Num(2)), Num(1)), Div(Num(1),Num(2))))]))
return expr
def arccscRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun== Fun("arccsc")\
and expr.expr.expr == expr.sym:
return Div(Num(-1), Mul([Apply(Fun("abs"), expr.expr.expr),\
(Pow(Sub(Pow(expr.expr.expr, Num(2)), Num(1)), Div(Num(1),Num(2))))]))
return expr
def arccotRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("arccot")\
and expr.expr.expr == expr.sym:
return Div(Num(-1), Add([Num(1), Pow(expr.expr.expr, Num(2))]))
return expr
def lnRule(expr):
if isinstance(expr, Deriv):
if isinstance(expr.expr, Apply) and expr.expr.fun == Fun("ln")\
and expr.expr.expr == expr.sym:
return Div(Num(1), expr.sym)
return expr
def takeDeriv(expr):
if not isinstance(expr, Deriv):
return expr
expr = negationRule(expr)
expr = identityRule(expr)
expr = constantRule(expr)
expr = constMultRule(expr)
expr = sumRule(expr)
expr = differenceRule(expr)
expr = powerRule(expr)
expr = productRule(expr)
expr = quotientRule(expr)
expr = exponentRule(expr)
expr = lnRule(expr)
expr = sinRule(expr)
expr = cosRule(expr)
expr = tanRule(expr)
expr = secRule(expr)
expr = cscRule(expr)
expr = cotRule(expr)
expr = arcsinRule(expr)
expr = arccosRule(expr)
expr = arctanRule(expr)
expr = arcsecRule(expr)
expr = arccscRule(expr)
expr = arccotRule(expr)
expr = chainRule(expr)
expr = funExponentRule(expr)
return expr
if __name__ == "__main__":
print(takeDeriv(Deriv(Pow(Var("e"), Var("x")), Var("x"))))