forked from jiggzson/nerdamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculus.js
299 lines (278 loc) · 11.9 KB
/
Calculus.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
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
/*
* Author : Martin Donk
* Website : http://www.nerdamer.com
* Email : martin.r.donk@gmail.com
* Source : https://github.com/jiggzson/nerdamer
*/
if((typeof module) !== 'undefined' && typeof nerdamer === 'undefined') {
nerdamer = require('./nerdamer.core.js');
// require('./Algebra.js');
}
(function() {
"use strict";
var core = nerdamer.getCore(),
_ = core.PARSER,
Frac = core.Frac,
isSymbol = core.Utils.isSymbol,
FN = core.groups.FN,
Symbol = core.Symbol,
text = core.Utils.text,
inBrackets = core.Utils.inBrackets,
N = core.groups. N,
S = core.groups.S,
FN = core.groups.FN,
PL = core.groups.PL,
CP = core.groups.CP,
CB = core.groups.CB,
EX = core.groups.EX,
P = core.groups.P;
var __ = core.Calculus = {
version: '1.1.3',
sum: function(fn, index, start, end) {
if(!(index.group === core.groups.S)) throw new Error('Index must be symbol. '+text(index)+' provided');
index = index.value;
var retval;
if(core.Utils.isNumericSymbol(start) && core.Utils.isNumericSymbol(end)) {
start = start.multiplier;
end = end.multiplier;
var variables = core.Utils.variables(fn);
if(variables.length === 1 && index === variables[0]) {
var f = core.Utils.build(fn);
retval = 0;
for(var i=start; i<=end; i++) {
retval += f.call(undefined, i);
}
}
else {
var f = fn.text(),
subs = {'~': true}, //lock subs
retval = new core.Symbol(0);
for(var i=start; i<=end; i++) {
subs[index] = new Symbol(i);
retval = _.add(retval, _.parse(f, subs)); //verrrrryyy sllloooowww
}
}
}
else {
retval = _.symfunction('sum',arguments);
}
return retval;
},
diff: function(symbol, wrt, nth) {
var d = isSymbol(wrt) ? wrt.text() : wrt;
nth = isSymbol(nth) ? nth.multiplier : nth || 1;
if(d === undefined) d = core.Utils.variables(symbol)[0];
//unwrap sqrt
if(symbol.group === FN && symbol.fname === 'sqrt') {
var s = symbol.args[0],
sp = symbol.power.clone();
//these groups go to zero anyway so why waste time?
if(s.group !== N || s.group !== P) {
s.power = isSymbol(s.power) ? _.multiply(s.power, _.multiply(new Symbol(1/2)), sp) : s.power.multiply(new Frac(0.5)).multiply(sp);
s.multiplier = s.multiplier.multiply(symbol.multiplier);
}
symbol = s;
}
if(symbol.group === FN && !isSymbol(symbol.power)) {
var a = derive(symbol);
var b = __.diff(symbol.args[0].clone(), d);
symbol = _.multiply(a, b);//chain rule
}
else {
symbol = derive(symbol);
}
if(nth > 1) {
nth--;
symbol = __.diff(symbol, wrt, nth);
}
return symbol;
// Equivalent to "derivative of the outside".
function polydiff(symbol) {
if(symbol.value === d || symbol.contains(d, true)) {
symbol.multiplier = symbol.multiplier.multiply(symbol.power);
symbol.power = symbol.power.subtract(new Frac(1));
if(symbol.power.equals(0)) {
symbol = Symbol(symbol.multiplier);
}
}
return symbol;
};
function derive(symbol) {
var g = symbol.group, a, b, cp;
if(g === N || g === S && symbol.value !== d || g === P) {
symbol = Symbol(0);
}
else if(g === S) {
symbol = polydiff(symbol);
}
else if(g === CB) {
var m = symbol.multiplier.clone();
symbol.toUnitMultiplier();
var retval = _.multiply(product_rule(symbol),polydiff(symbol.clone()));
retval.multiplier = retval.multiplier.multiply(m);
return retval;
}
else if(g === FN && symbol.power.equals(1)) {
// Table of known derivatives
switch(symbol.fname) {
case 'log':
cp = symbol.clone();
symbol = symbol.args[0].clone();//get the arguments
symbol.power = symbol.power.negate();
symbol.multiplier = cp.multiplier.divide(symbol.multiplier);
break;
case 'cos':
//cos -> -sin
symbol.fname = 'sin';
symbol.multiplier.negate();
break;
case 'sin':
//sin -> cos
symbol.fname = 'cos';
break;
case 'tan':
//tan -> sec^2
symbol.fname = 'sec';
symbol.power = new Frac(2);
break;
case 'sec':
// Use a clone if this gives errors
symbol = qdiff(symbol, 'tan');
break;
case 'csc':
symbol = qdiff(symbol, '-cot');
break;
case 'cot':
symbol.fname = 'csc';
symbol.multiplier.negate();
symbol.power = new Frac(2);
break;
case 'asin':
symbol = _.parse('(sqrt(1-('+text(symbol.args[0])+')^2))^(-1)');
break;
case 'acos':
symbol = _.parse('-(sqrt(1-('+text(symbol.args[0])+')^2))^(-1)');
break;
case 'atan':
symbol = _.parse('(1+('+text(symbol.args[0])+')^2)^(-1)');
break;
case 'abs':
m = symbol.multiplier.clone();
symbol.toUnitMultiplier();
//depending on the complexity of the symbol it's easier to just parse it into a new symbol
//this should really be readdressed soon
b = symbol.args[0].clone();
b.toUnitMultiplier();
symbol = _.parse(inBrackets(text(symbol.args[0]))+'/abs'+inBrackets(text(b)));
symbol.multiplier = m;
break;
case 'parens':
//see product rule: f'.g goes to zero since f' will return zero. This way we only get back
//1*g'
symbol = Symbol(1);
break;
case 'cosh':
//cos -> -sin
symbol.fname = 'sinh';
break;
case 'sinh':
//sin -> cos
symbol.fname = 'cosh';
break;
case 'tanh':
//tanh -> sech^2
symbol.fname = 'sech';
symbol.power = new Frac(2);
break;
case 'sech':
// Use a clone if this gives errors
symbol = qdiff(symbol, '-tanh');
break;
case 'asinh':
symbol = _.parse('(sqrt(1+('+text(symbol.args[0])+')^2))^(-1)');
break;
case 'acosh':
symbol = _.parse('(sqrt(-1+('+text(symbol.args[0])+')^2))^(-1)');
break;
case 'atanh':
symbol = _.parse('(1-('+text(symbol.args[0])+')^2)^(-1)');
break;
}
}
else if(g === EX || g === FN && isSymbol(symbol.power)) {
var value;
if(g === EX) {
value = symbol.value;
}
else if(g === FN && symbol.contains(d)) {
value = symbol.fname + inBrackets(text(symbol.args[0]));
}
else {
value = symbol.value + inBrackets(text(symbol.args[0]));
}
a = _.multiply(_.parse('log'+inBrackets(value)), symbol.power.clone());
b = __.diff(_.multiply(_.parse('log'+inBrackets(value)), symbol.power.clone()), d);
symbol = _.multiply(symbol, b);
}
else if(g === FN && !symbol.power.equals(1)) {
b = symbol.clone();
b.toLinear();
b.toUnitMultiplier();
symbol = _.multiply(polydiff( symbol.clone(), d ), derive(b));
}
else if( g === CP || g === PL ) {
var result = new Symbol(0);
for(var x in symbol.symbols) {
result = _.add(result, __.diff(symbol.symbols[x].clone(), d));
}
symbol = _.multiply(polydiff(symbol.clone()), result);
}
symbol.updateHash();
return symbol;
};
function qdiff(symbol, val, altVal) {
return _.multiply(symbol, _.parse(val+inBrackets(altVal || text(symbol.args[0]))));
};
function product_rule(symbol) {
//grab all the symbols within the CB symbol
var symbols = symbol.collectSymbols(),
result = new Symbol(0),
l = symbols.length;
//loop over all the symbols
for(var i=0; i<l; i++) {
var df = __.diff(symbols[i].clone(), d);
for(var j=0; j<l; j++) {
//skip the symbol of which we just pulled the derivative
if(i !== j) {
//multiply out the remaining symbols
df = _.multiply(df, symbols[j].clone());
}
}
//add the derivative to the result
result = _.add(result, df);
}
return result; //done
};
}
};
nerdamer.register([
{
name: 'diff',
visible: true,
numargs: [1,3],
build: function(){ return __.diff; }
},
{
name: 'differentiate',
visible: true,
numargs: [1,3],
build: function(){ return __.diff; }
},
{
name: 'sum',
visible: true,
numargs: 4,
build: function(){ return __.sum; }
}
]);
})();