This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
/
symder.spn
187 lines (175 loc) · 3.96 KB
/
symder.spn
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
let diff = fn node {
let literal_zero = {
"type": "literal",
"line": 0,
"column": 0,
"value": 0.0
};
let linearop = fn node -> {
"type": node.type,
"line": node.line,
"column": node.column,
"left": diff(node.left),
"right": diff(node.right)
};
let is_constant = fn node ->
node.type == "literal" or node.type == "ident" and node.name != "x";
let derivative_func = fn node {
let dfdx = {
"sin": { "line": 0, "column": 0, "type": "ident", "name": "cos" },
"exp": { "line": 0, "column": 0, "type": "ident", "name": "exp" },
"sinh": { "line": 0, "column": 0, "type": "ident", "name": "cosh" },
"cosh": { "line": 0, "column": 0, "type": "ident", "name": "sinh" }
}[node.name];
return {
"type": "call",
"line": node.line,
"column": node.column,
"func": dfdx,
"children": []
};
};
let nodetypes = {
"+": linearop,
"-": linearop,
"*": fn node {
// if at least one of the terms is a constant, we can simplify
if is_constant(node.left) and is_constant(node.right) {
return literal_zero;
}
if is_constant(node.left) {
return {
"type": "*",
"line": node.line,
"column": node.column,
"left": node.left,
"right": diff(node.right)
};
}
if is_constant(node.right) {
return {
"type": "*",
"line": node.line,
"column": node.column,
"left": diff(node.left),
"right": node.right
};
}
return {
"type": "+",
"line": node.line,
"column": node.column,
"left": {
"type": "*",
"line": node.line,
"column": node.column,
"left": diff(node.left),
"right": node.right
},
"right": {
"type": "*",
"line": node.line,
"column": node.column,
"left": node.left,
"right": diff(node.right)
}
};
},
"/": fn node {
// can simplify only when denominator is constant
if is_constant(node.right) {
if is_constant(node.left) {
return literal_zero;
}
return {
"type": "/",
"line": node.line,
"column": node.column,
"left": diff(node.left),
"right": node.right
};
}
// g^2
let gsq = {
"type": "*",
"line": node.line,
"column": node.column,
"left": node.right,
"right": node.right
};
// f'g
let dfg = {
"type": "*",
"line": node.line,
"column": node.column,
"left": diff(node.left),
"right": node.right
};
// fg'
let fdg = {
"type": "*",
"line": node.line,
"column": node.column,
"left": node.left,
"right": diff(node.right)
};
return {
"type": "/",
"line": node.line,
"column": node.column,
"left": {
"type": "-",
"line": node.line,
"column": node.column,
"left": dfg,
"right": fdg
},
"right": gsq
};
},
"un_plus": fn node -> diff(node.right),
"un_minus": fn node -> {
"type": "un_minus",
"line": node.line,
"column": node.column,
"right": diff(node.right)
},
"call": fn node {
let inner = node.children[0];
let douter = derivative_func(node.func);
douter.children.push(inner);
// don't treat f'(x) as f'(x) * dx/dx...
if inner.type == "ident" and inner.name == "x" {
return douter;
}
let dinner = diff(inner); // bon appetite !
return {
"type": "*",
"line": 0,
"column": 0,
"left": douter,
"right": dinner
};
},
"literal": fn node -> literal_zero,
"ident": fn node -> {
"type": "literal",
"line": node.line,
"column": node.column,
"value": node.name == "x" ? 1.0 : 0.0
}
};
return nodetypes[node.type](node);
};
let derivative = fn src {
let ast = parseexpr(src);
let dast = diff(ast.children[0].expr);
let ret = parse("let x = $[0]; return nil;");
ret.children[1].expr = dast;
return compileast(ret);
};
let f = derivative("exp(sin(x)) + x * cosh(x)");
// reference function: fcheck ought to be the same as f
let fcheck = fn x -> exp(sin(x)) * cos(x) + x * sinh(x) + cosh(x);
let x = 1;
print(f(x), " ", fcheck(x)); // these should be equal