-
Notifications
You must be signed in to change notification settings - Fork 0
/
Derivate.js
61 lines (59 loc) · 2.46 KB
/
Derivate.js
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
/**
* Derivative
*/
(function () {
var deriv;
function deriveFunc(func, arg, va) {
switch (func) {
case 'sin':
return ['call', 'cos', arg];
case 'cos':
return ['.-', ['call', 'sin', arg]];
case 'tan':
return ['/', ['num', 1], ['*', ['call', 'cos', arg], ['call', 'cos', arg]]];
case 'acos':
return ['.-', ['/', ['num', 1], ['call', 'sqrt', ['-', ['num', 1], ['^', arg, 2]]]]];
case 'asin':
return ['/', ['num', 1], ['call', 'sqrt', ['-', ['num', 1], ['^', arg, 2]]]];
case 'atan':
return ['/', ['num', 1], ['+', ['num', 1], ['^', arg, ['num', 2]]]];
case 'sqrt':
return ['/', ['num', 0.5], ['call', 'sqrt', arg]];
case 'exp':
return ['call', 'exp', arg];
case 'log':
return ['/', ['num', 1], arg];
case 'sinc':
return deriv(['/', ['call', 'sin', arg], arg], va);
case 'sh':
return ['call', 'ch', arg];
case 'ch':
return ['call', 'sh', arg];
case 'th':
return ['/', ['num', 1], ['*', ['call', 'ch', arg], ['call', 'ch', arg]]];
}
}
window.derivExp = deriv = function (ast, va) {
if (ast[0] === '.+' || ast[0] === '.-') {
return [ast[0], deriv(ast[1], va)];
} else if (ast[0] == '+' || ast[0] == '-') {
return [ast[0], deriv(ast[1], va), deriv(ast[2], va)];
} else if (ast[0] == '*') {
return ['+', ['*', deriv(ast[1], va), ast[2]], ['*', ast[1], deriv(ast[2], va)]];
} else if (ast[0] == '/') {
return ['-', ['/', deriv(ast[1], va), ast[2]], ['*', ast, ['/', deriv(ast[2], va), ast[2]]]];
} else if (ast[0] == '^') {
if (ast[2][0] == 'num') {
return ['*', ['*', ast[2], ['^', ast[1], ['-', ast[2], ['num', 1]]]], deriv(ast[1], va)];
} else {
return ['*', ast, ['+', ['*', ast[2], ['/', deriv(ast[1], va) , ast[1]]], ['*', ['call', 'log', ast[1]], deriv(ast[2], va)]]];
}
} else if (ast[0] == 'num') {
return ['num', 0];
} else if (ast[0] == 'ident') {
return ['num', ast[1] == va ? 1 : 0];
} else if (ast[0] == 'call') {
return ['*', deriveFunc(ast[1], ast[2], va), deriv(ast[2], va)]
}
}
})();