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
from support.expr_types import *
from functools import cache
from support.canonicalize import simplify
@cache
def evaluate_diff(expr, var):
"""
Relevant Expression Types (and fields):
- BinaryOperation b: b.left, b.right
- Function f: f.name, f.arguments
- SummationOperation s: s.values
- ProductOperation p: p.values
Variable types for below:
- OP_CHAR in ['+', '-', '*', '/', '^']
- FUNC_NAME in ['e', 'arctan']
- expr : Expression
- x : Expression
- y : Expression
- var : str
- xs : List[expr]
Available/Useful functions:
- is_constant(expr, var)
- is_op(expr, OP_CHAR)
- is_function(expr, FUNC_NAME)
- is_product(expr)
- is_summation(expr)
- Plus(x, y)
- Minus(x, y)
- Mul(x, y)
- Pow(x, y)
- Div(x, y)
- Summation(xs)
- Product(xs)
- Func(expr, xs)
- evaluate_diff(expr, var)
Differentation Rules to Handle:
- var
- constant w.r.t. var
- sum rule
- subtraction rule
- product rule
- division rule: DO THIS AS A RE-WRITE to multiplication, do not use division rule directly
- power rule for numerical exponent
- e^x rule (e^x is represented as a function e(x))
- arctan(x) rule
- sum rule for SummationOperation([x1, x2, x3, ...])
- product rule for ProductOperation([x1, x2, x3, ...])
"""
if is_constant(expr, var):
return None
if expr == var:
return None
if is_op(expr, '+'):
return None
# Uh oh...lots of cases are missing and the ones above seem wrong. :(
raise Exception("not implemented: " + str(expr))