-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtra.js
More file actions
353 lines (336 loc) · 10.4 KB
/
Extra.js
File metadata and controls
353 lines (336 loc) · 10.4 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
if (typeof module !== "undefined") {
nerdamer = require("./nerdamer.core.js");
require("./Calculus");
}
(function () {
var core = nerdamer.getCore(),
_ = core.PARSER,
Symbol = core.Symbol,
format = core.Utils.format,
isVector = core.Utils.isVector,
S = core.groups.S,
EX = core.groups.EX,
CB = core.groups.CB,
FN = core.groups.FN;
core.Settings.Laplace_integration_depth = 40;
var __ = (core.Extra = {
version: "1.2.0",
//http://integral-table.com/downloads/LaplaceTable.pdf
LaPlace: {
//Using: intgral_0_oo f(t)*e^(-s*t) dt
transform: function (symbol, t, s) {
t = t.toString();
//First try a lookup for a speed boost
symbol = Symbol.unwrapSQRT(symbol, true);
var retval,
coeff = symbol.stripVar(t),
g = symbol.group;
symbol = _.divide(symbol, coeff.clone());
if (symbol.isConstant() || !symbol.contains(t, true)) {
retval = _.parse(format("({0})/({1})", symbol, s));
} else if (g === S && core.Utils.isInt(symbol.power)) {
var n = String(symbol.power);
retval = _.parse(format("factorial({0})/({1})^({0}+1)", n, s));
} else if (symbol.group === S && symbol.power.equals(1 / 2)) {
retval = _.parse(format("sqrt(pi)/(2*({0})^(3/2))", s));
} else if (symbol.isComposite()) {
retval = new Symbol(0);
symbol.each(function (x) {
retval = _.add(retval, __.LaPlace.transform(x, t, s));
}, true);
} else if (
symbol.isE() &&
(symbol.power.group === S || symbol.power.group === CB)
) {
var a = symbol.power.stripVar(t);
retval = _.parse(format("1/(({1})-({0}))", a, s));
} else {
var fns = ["sin", "cos", "sinh", "cosh"];
//support for symbols in fns with arguments in the form a*t or n*t where a = symbolic and n = Number
if (
symbol.group === FN &&
fns.indexOf(symbol.fname) !== -1 &&
(symbol.args[0].group === S || symbol.args[0].group === CB)
) {
var a = symbol.args[0].stripVar(t);
switch (symbol.fname) {
case "sin":
retval = _.parse(format("({0})/(({1})^2+({0})^2)", a, s));
break;
case "cos":
retval = _.parse(format("({1})/(({1})^2+({0})^2)", a, s));
break;
case "sinh":
retval = _.parse(format("({0})/(({1})^2-({0})^2)", a, s));
break;
case "cosh":
retval = _.parse(format("({1})/(({1})^2-({0})^2)", a, s));
break;
}
} else {
//Try to integrate for a solution
//we need at least the Laplace integration depth
var depth_is_lower =
core.Settings.integration_depth <
core.Settings.Laplace_integration_depth;
if (depth_is_lower) {
var integration_depth = core.Settings.integration_depth; //save the depth
core.Settings.integration_depth = 40; //transforms need a little more room
}
core.Utils.block(
"PARSE2NUMBER",
function () {
var u = t;
var sym = symbol.sub(t, u);
var integration_expr = _.parse(
"e^(-" + s + "*" + u + ")*" + sym
);
retval = core.Calculus.integrate(integration_expr, u);
if (retval.hasIntegral())
_.error("Unable to compute transform");
retval = retval.sub(t, 0);
retval = _.expand(_.multiply(retval, new Symbol(-1)));
retval = retval.sub(u, t);
},
false
);
retval = core.Utils.block(
"PARSE2NUMBER",
function () {
return _.parse(retval);
},
true
);
if (depth_is_lower)
//put the integration depth as it was
core.Settings.integration_depth = integration_depth;
}
}
return _.multiply(retval, coeff);
}
},
Statistics: {
frequencyMap: function (arr) {
var map = {};
//get the frequency map
for (var i = 0, l = arr.length; i < l; i++) {
var e = arr[i],
key = e.toString();
if (!map[key])
//default it to zero
map[key] = 0;
map[key]++; //increment
}
return map;
},
sort: function (arr) {
return arr.sort(function (a, b) {
if (!a.isConstant() || !b.isConstant())
_.error("Unable to sort! All values must be numeric");
return a.multiplier.subtract(b.multiplier);
});
},
count: function (arr) {
return new Symbol(arr.length);
},
sum: function (arr, x_) {
var sum = new Symbol(0);
for (var i = 0, l = arr.length; i < l; i++) {
var xi = arr[i].clone();
if (x_) {
sum = _.add(_.pow(_.subtract(xi, x_.clone()), new Symbol(2)), sum);
} else sum = _.add(xi, sum);
}
return sum;
},
mean: function () {
var args = [].slice.call(arguments);
//handle arrays
if (isVector(args[0]))
return __.Statistics.mean.apply(this, args[0].elements);
return _.divide(__.Statistics.sum(args), __.Statistics.count(args));
},
median: function () {
var args = [].slice.call(arguments),
retval;
//handle arrays
if (isVector(args[0]))
return __.Statistics.median.apply(this, args[0].elements);
try {
var sorted = __.Statistics.sort(args);
var l = args.length;
if (core.Utils.even(l)) {
var mid = l / 2;
retval = __.Statistics.mean(sorted[mid - 1], sorted[mid]);
} else retval = sorted[Math.floor(l / 2)];
} catch (e) {
retval = _.symfunction("median", args);
}
return retval;
},
mode: function () {
var args = [].slice.call(arguments),
retval;
//handle arrays
if (isVector(args[0]))
return __.Statistics.mode.apply(this, args[0].elements);
var map = __.Statistics.frequencyMap(args),
max = [],
c = 0, //number of iterations
s = 0, //variable to measure if all values had equal frequency
fv;
for (var x in map) {
var e = map[x],
first_iter = c === 0;
if (first_iter) fv = e;
if (first_iter || e > max[1]) {
//if no max or this is greater
max[0] = x;
max[1] = e;
}
//starts with itself and then increments each time another max equals this number
if (e === fv) s++;
c++;
}
//check if s and c are equal then no max was found so return a sym function
if (s === c) retval = _.symfunction("mode", args);
else retval = _.parse(max[0]);
return retval;
},
gVariance: function (k, args) {
var x_ = __.Statistics.mean.apply(__.Statistics, args),
sum = __.Statistics.sum(args, x_);
return _.multiply(k, sum);
},
variance: function () {
var args = [].slice.call(arguments);
//handle arrays
if (isVector(args[0]))
return __.Statistics.variance.apply(this, args[0].elements);
var k = _.divide(new Symbol(1), __.Statistics.count(args));
return __.Statistics.gVariance(k, args);
},
sampleVariance: function () {
var args = [].slice.call(arguments);
//handle arrays
if (isVector(args[0]))
return __.Statistics.sampleVariance.apply(this, args[0].elements);
var k = _.divide(
new Symbol(1),
_.subtract(__.Statistics.count(args), new Symbol(1))
);
return __.Statistics.gVariance(k, args);
},
standardDeviation: function () {
var args = [].slice.call(arguments);
//handle arrays
if (isVector(args[0]))
return __.Statistics.standardDeviation.apply(this, args[0].elements);
return _.pow(
__.Statistics.variance.apply(__.Statistics, args),
new Symbol(1 / 2)
);
},
sampleStandardDeviation: function () {
var args = [].slice.call(arguments);
//handle arrays
if (isVector(args[0]))
return __.Statistics.sampleStandardDeviation.apply(
this,
args[0].elements
);
return _.pow(
__.Statistics.sampleVariance.apply(__.Statistics, args),
new Symbol(1 / 2)
);
},
zScore: function (x, mean, stdev) {
return _.divide(_.subtract(x, mean), stdev);
}
},
Units: {
table: {
foot: "12 inch",
meter: "100 cm",
decimeter: "10 cm"
}
}
});
nerdamer.register([
{
name: "laplace",
visible: true,
numargs: 3,
build: function () {
return __.LaPlace.transform;
}
},
//statistical
{
name: "mean",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.mean;
}
},
{
name: "median",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.median;
}
},
{
name: "mode",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.mode;
}
},
{
name: "smpvar",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.sampleVariance;
}
},
{
name: "variance",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.variance;
}
},
{
name: "smpstdev",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.sampleStandardDeviation;
}
},
{
name: "stdev",
visible: true,
numargs: -1,
build: function () {
return __.Statistics.standardDeviation;
}
},
{
name: "zscore",
visible: true,
numargs: 3,
build: function () {
return __.Statistics.zScore;
}
}
]);
//link registered functions externally
nerdamer.api();
})();